forked from microsoft/vscode-copilot-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFailureTool.tsx
More file actions
275 lines (235 loc) · 9.66 KB
/
testFailureTool.tsx
File metadata and controls
275 lines (235 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import { BasePromptElementProps, PrioritizedList, PromptElement, PromptElementProps, PromptPiece, TextChunk } from '@vscode/prompt-tsx';
import type { CancellationToken, LanguageModelToolInvocationOptions, LanguageModelToolInvocationPrepareOptions, PreparedToolInvocation, ProviderResult, TestMessage, TestResultSnapshot } from 'vscode';
import { IGitExtensionService } from '../../../platform/git/common/gitExtensionService';
import { ITabsAndEditorsService } from '../../../platform/tabs/common/tabsAndEditorsService';
import { ITestFailure, ITestProvider } from '../../../platform/testing/common/testProvider';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { URI } from '../../../util/vs/base/common/uri';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { LanguageModelPromptTsxPart, LanguageModelTextPart, LanguageModelToolResult } from '../../../vscodeTypes';
import { renderPromptElementJSON } from '../../prompts/node/base/promptRenderer';
import { Tag } from '../../prompts/node/base/tag';
import { ToolName } from '../common/toolNames';
import { ICopilotTool, IEditFilterData, ToolRegistry } from '../common/toolsRegistry';
const enum Constant {
IdealMaxTokenUsageProportion = 1 / 5,
MinNumberOfTestsToInclude = 5,
RankActive = 4,
RankVisible = 3,
RankOpen = 2,
RankSCM = 1,
RankNone = 0,
MaxRank = Constant.RankActive,
}
/**
* Resolves `#testFailure` into zero or more test failures based on the API.
* It gathers all failures, and if there are less than five failures in the
* most recent test run or they would use less than 1/5th of the token budget,
* it renders them.
*
* Otherwise, they are ranked and trimmed in the order:
*
* 1. Tests or failures in editors that are open
* 2. Tests or failures in files that have SCM changes
* 3. Failures whose stacks touch editors that are open
* 4. Failures whose stacks touch files with SCM changes
*
*/
export class TestFailureTool implements ICopilotTool<{}> {
public static readonly toolName = ToolName.TestFailure;
constructor(
@ITestProvider private readonly testProvider: ITestProvider,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
) { }
async invoke({ tokenizationOptions }: LanguageModelToolInvocationOptions<{}>): Promise<LanguageModelToolResult> {
const failures = Array.from(this.testProvider.getAllFailures());
if (failures.length === 0) {
return new LanguageModelToolResult([
new LanguageModelTextPart(`No test failures were found yet, call the tool ${ToolName.RunTests} to run tests and find failures.`),
]);
}
const json = await renderPromptElementJSON(this.instantiationService, TestFailureList, { failures }, tokenizationOptions && {
...tokenizationOptions,
tokenBudget: tokenizationOptions.tokenBudget * Constant.IdealMaxTokenUsageProportion,
});
return new LanguageModelToolResult([
new LanguageModelPromptTsxPart(json)
]);
}
async filterEdits(resource: URI): Promise<IEditFilterData | undefined> {
if (await this.testProvider.hasTestsInUri(resource)) {
return {
title: l10n.t`Allow changing test assertions?`,
message: l10n.t`The model wants to change the assertions in \`${this.workspaceService.asRelativePath(resource)}\`. Do you want to allow this?`,
};
}
}
prepareInvocation(options: LanguageModelToolInvocationPrepareOptions<{}>, token: CancellationToken): ProviderResult<PreparedToolInvocation> {
return {
invocationMessage: l10n.t`Finding test failures`,
pastTenseMessage: l10n.t`Found test failures`,
};
}
provideInput(): Promise<{}> {
return Promise.resolve({}); // just to avoid an unnecessary model call
}
}
ToolRegistry.registerTool(TestFailureTool);
export class TestFailureList extends PromptElement<TestFailureListElementProps> {
constructor(
props: PromptElementProps<TestFailureListElementProps>,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@ITabsAndEditorsService private readonly tabsAndEditorsService: ITabsAndEditorsService,
@IGitExtensionService private readonly gitExtensionService: IGitExtensionService,
) {
super(props);
}
render() {
if (!this.props.failures.length) {
return <TextChunk priority={100}>No test failures were found.</TextChunk>;
}
return <>
<PrioritizedList priority={100} descending>
{this.sortByRanks(this.props.failures).map(f => <TestFailureElement failure={f} />)}
</PrioritizedList>
<TextChunk priority={101}>
## Rules:<br />
- Always try to find an error in the implementation code first. Don't suggest any changes in my test cases unless I tell you to.<br />
- If you need more information about anything in the codebase, use a tool like {ToolName.ReadFile}, {ToolName.ListDirectory}, or {ToolName.FindFiles} to find and read it. Never ask the user to provide it themselves.<br />
- If you make changes to fix the test, call {ToolName.RunTests} to run the tests and verify the fix.<br />
- Don't try to make the same changes you made before to fix the test. If you're stuck, ask the user for pointers.<br />
</TextChunk>
</>;
}
private sortByRanks(failures: readonly ITestFailure[]) {
const withRanks = failures.map((failure) => {
let rank = failure.snapshot.uri ? this.rankFile(failure.snapshot.uri) : Constant.RankNone;
for (const message of failure.task.messages) {
if (rank === Constant.MaxRank) {
return { failure, rank }; // abort early if there's nothing better
}
if (message.location) {
rank = Math.max(rank, this.rankFile(message.location.uri));
}
}
if (rank > Constant.RankNone) {
return { failure, rank };
}
for (const message of failure.task.messages) {
if (message.stackTrace) {
// limit to first 10 stack frames to avoid going too crazy on giant stacks
for (const frame of message.stackTrace.slice(0, 10)) {
if (frame.uri) {
rank = Math.max(rank, this.rankFile(frame.uri));
}
}
}
}
// Ranks from stacktraces are always less than 'primary' ranks, so /10
return { failure, rank: rank / 10 };
});
return withRanks.sort((a, b) => b.rank - a.rank).map(f => f.failure);
}
private rankFile(uri: URI): number {
if (this.tabsAndEditorsService.activeTextEditor?.document.uri.toString() === uri.toString()) {
return Constant.RankActive;
}
if (this.tabsAndEditorsService.visibleTextEditors?.some(e => e.document.uri.toString() === uri.toString())) {
return Constant.RankVisible;
}
if (this.workspaceService.textDocuments.some(d => d.uri.toString() === uri.toString())) {
return Constant.RankOpen;
}
// Check if file has SCM changes
const repository = this.gitExtensionService.getExtensionApi()?.getRepository(uri);
if (repository) {
const state = repository.state;
const indicies = [
state.indexChanges,
state.workingTreeChanges,
state.mergeChanges,
state.untrackedChanges
];
for (const changes of indicies) {
if (changes.some(change => change.uri.toString() === uri.toString())) {
return Constant.RankSCM;
}
}
}
return Constant.RankNone;
}
}
interface TestFailureElementProps extends BasePromptElementProps {
failure: ITestFailure;
}
export interface TestFailureListElementProps extends BasePromptElementProps {
failures: ITestFailure[];
}
class TestFailureElement extends PromptElement<TestFailureElementProps> {
constructor(
props: TestFailureElementProps,
) {
super(props);
}
render() {
const f = this.props.failure;
const namePartsInSameUri: string[] = [];
for (let n: TestResultSnapshot | undefined = f.snapshot; n; n = n.parent) {
if (n.uri?.toString() === f.snapshot.uri?.toString()) {
namePartsInSameUri.push(n.label);
}
}
return <>
<Tag name='testFailure' attrs={{
testCase: namePartsInSameUri.reverse().join(' '),
path: f.snapshot.uri?.fsPath,
}}>
{f.task.messages.map(m => <TestMessageElement message={m} />)}
</Tag>
</>;
}
}
interface TestMessageElementProps extends BasePromptElementProps {
message: TestMessage;
}
class TestMessageElement extends PromptElement<TestMessageElementProps> {
constructor(
props: TestMessageElementProps,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
) {
super(props);
}
render() {
const f = this.props.message;
const children: PromptPiece[] = [];
if (f.expectedOutput !== undefined && f.actualOutput !== undefined) {
children.push(
<Tag name='expectedOutput'>{f.expectedOutput}</Tag>,
<Tag name='actualOutput'>{f.actualOutput}</Tag>,
);
} else {
children.push(<Tag name='message'>{typeof f.message === 'string' ? f.message : f.message.value}</Tag>);
}
if (f.stackTrace) {
for (const { label, position, uri } of f.stackTrace) {
// if there's both a position and URI, the XML element alone is fully descriptive so omit the label
if (position && uri) {
children.push(
<Tag name='stackFrame' attrs={{ path: this.workspaceService.asRelativePath(uri), line: position.line, col: position.character }} />,
);
} else {
children.push(
<Tag name='stackFrame' attrs={{ path: uri && this.workspaceService.asRelativePath(uri), line: position?.line, col: position?.character }}>{label}</Tag>,
);
}
}
}
return <>{children}</>;
}
}