{{ message }}
Repository navigation
Expand file tree
/
Copy pathagent-host-e2e-coverage.ts
More file actions
368 lines (327 loc) · 13.5 KB
/
Copy pathagent-host-e2e-coverage.ts
File metadata and controls
368 lines (327 loc) · 13.5 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const childProcess: typeof import('child_process') = require('child_process');
const fs: typeof import('fs') = require('fs');
const path: typeof import('path') = require('path');
const { spawnSync } = childProcess;
const { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } = fs;
const { dirname, isAbsolute, join, relative, resolve, sep } = path;
const repoRoot = resolve(__dirname, '..');
const coverageRoot = join(repoRoot, '.build', 'agent-host-e2e-coverage');
const rawCoveragePath = join(coverageRoot, 'raw');
const reportPath = join(coverageRoot, 'report');
const summaryPath = join(reportPath, 'coverage-summary.json');
const observedSurfacePath = join(coverageRoot, 'protocol-surface', 'observed.json');
const e2eRoot = join(repoRoot, 'src', 'vs', 'platform', 'agentHost', 'test', 'node', 'e2e');
const statsPath = join(e2eRoot, 'coverage', 'summary.json');
const surfaceStatsPath = join(e2eRoot, 'coverage', 'protocol-surface.json');
const protocolRoot = join(repoRoot, 'src', 'vs', 'platform', 'agentHost', 'common', 'state', 'protocol');
const metricNames = ['statements', 'branches', 'functions', 'lines'] as const;
type MetricName = typeof metricNames[number];
interface ICoverageMetric {
readonly covered: number;
readonly total: number;
readonly percentage: number;
}
type Coverage = Record<MetricName, ICoverageMetric>;
const providerPackages = [
'@anthropic-ai/claude-agent-sdk',
'@github/copilot-sdk',
'@openai/codex',
];
const incompatibleFlags = [
'AGENT_HOST_REAL_CODEX',
'AGENT_HOST_REPLAY_RECORD',
'AGENT_HOST_UPDATE_AHP_SNAPSHOTS',
'AGENT_HOST_UPDATE_SNAPSHOTS',
];
function main(): void {
validateEnvironment();
const environment = { ...process.env };
delete environment.ELECTRON_RUN_AS_NODE;
delete environment.NODE_V8_COVERAGE;
rmSync(coverageRoot, { recursive: true, force: true });
mkdirSync(rawCoveragePath, { recursive: true });
run(process.execPath, [join(repoRoot, 'build', 'next', 'index.ts'), 'transpile'], environment);
const testEnvironment = {
...environment,
AGENT_HOST_E2E_COVERAGE: '1',
AGENT_HOST_RECORD_PROTOCOL_SURFACE: '1',
AGENT_HOST_PROTOCOL_SURFACE_OUT: observedSurfacePath,
};
run(process.execPath, [join(repoRoot, 'scripts', 'test-agent-host-e2e.ts')], testEnvironment);
const rawFiles = readdirSync(rawCoveragePath).filter(file => file.endsWith('.json'));
if (rawFiles.length === 0) {
throw new Error(`No raw V8 coverage files were written to ${rawCoveragePath}`);
}
const c8Path = join(repoRoot, 'node_modules', 'c8', 'bin', 'c8.js');
run(process.execPath, [
c8Path,
'report',
'--temp-directory', rawCoveragePath,
'--reports-dir', reportPath,
'--reporter', 'text',
'--reporter', 'html',
'--reporter', 'lcov',
'--reporter', 'json-summary',
'--include', 'out/vs/platform/agentHost/common/**/*.js',
'--include', 'out/vs/platform/agentHost/node/**/*.js',
'--exclude', 'out/vs/platform/agentHost/**/test/**',
'--exclude', 'out/vs/platform/agentHost/**/*.test.js',
'--exclude', 'out/vs/platform/agentHost/**/*.integrationTest.js',
'--exclude', 'out/vs/platform/agentHost/common/state/protocol/channels-*/{actions,commands,notifications,state}.js',
'--exclude', 'out/vs/platform/agentHost/common/state/protocol/common/state.js',
], environment);
writeStats();
console.log(`Agent host E2E coverage stats written to ${relative(repoRoot, statsPath)}`);
writeProtocolSurfaceStats();
console.log(`Agent host protocol surface stats written to ${relative(repoRoot, surfaceStatsPath)}`);
}
function validateEnvironment(): void {
const enabledFlags = incompatibleFlags.filter(flag => process.env[flag] === '1');
if (enabledFlags.length > 0) {
throw new Error(`Agent host E2E coverage requires deterministic replay; unset ${enabledFlags.join(', ')}`);
}
const missingPackages = providerPackages.filter(packageName => !existsSync(join(repoRoot, 'node_modules', ...packageName.split('/'))));
if (missingPackages.length > 0) {
throw new Error(`Agent host E2E coverage requires all provider dependencies; run npm install to add ${missingPackages.join(', ')}`);
}
}
function run(command: string, args: readonly string[], environment: NodeJS.ProcessEnv): void {
const result = spawnSync(command, args, {
cwd: repoRoot,
env: environment,
stdio: 'inherit',
});
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
const reason = result.signal ? `signal ${result.signal}` : `code ${result.status}`;
throw new Error(`${command} exited with ${reason}`);
}
}
function writeStats(): void {
const summary: unknown = JSON.parse(readFileSync(summaryPath, 'utf8'));
if (!isRecord(summary)) {
throw new Error('The c8 coverage summary must be an object');
}
const fileEntries = Object.entries(summary)
.filter(([filePath]) => filePath !== 'total')
.map(([filePath, coverage]): readonly [string, Coverage] => [toSourcePath(filePath), normalizeCoverage(coverage, filePath)])
.sort(([left], [right]) => left < right ? -1 : left > right ? 1 : 0);
if (fileEntries.length === 0) {
throw new Error('The c8 report did not contain any loaded agent host source files');
}
const files: Record<string, Coverage> = {};
for (const [filePath, coverage] of fileEntries) {
if (files[filePath]) {
throw new Error(`The c8 report contains duplicate source coverage for ${filePath}`);
}
files[filePath] = coverage;
}
const total = aggregateCoverage(Object.values(files));
const reportedTotal = normalizeCoverage(summary.total, 'total');
for (const metricName of metricNames) {
if (total[metricName].total !== reportedTotal[metricName].total || total[metricName].covered !== reportedTotal[metricName].covered) {
throw new Error(`The normalized ${metricName} total does not match c8's total`);
}
}
const stats = {
version: 1,
scope: {
loadedFilesOnly: true,
include: [
'src/vs/platform/agentHost/common/**/*.ts',
'src/vs/platform/agentHost/node/**/*.ts',
],
suites: ['conformance', 'claude', 'codex', 'copilotcli'],
},
total,
files,
};
writeJsonAtomically(statsPath, stats);
}
function toSourcePath(filePath: string): string {
const absolutePath = isAbsolute(filePath) ? filePath : resolve(repoRoot, filePath);
const repoRelativePath = relative(repoRoot, absolutePath).split(sep).join('/');
if (!/^src\/vs\/platform\/agentHost\/(?:common|node)\/.+\.ts$/.test(repoRelativePath) || repoRelativePath.includes('/test/')) {
throw new Error(`Unexpected file in agent host E2E coverage: ${filePath}`);
}
return repoRelativePath;
}
function normalizeCoverage(coverage: unknown, label: string): Coverage {
if (!isRecord(coverage)) {
throw new Error(`Missing coverage metrics for ${label}`);
}
return {
statements: normalizeMetric(coverage.statements, 'statements', label),
branches: normalizeMetric(coverage.branches, 'branches', label),
functions: normalizeMetric(coverage.functions, 'functions', label),
lines: normalizeMetric(coverage.lines, 'lines', label),
};
}
function normalizeMetric(metric: unknown, metricName: MetricName, label: string): ICoverageMetric {
if (!isRecord(metric)
|| typeof metric.total !== 'number'
|| typeof metric.covered !== 'number'
|| !Number.isInteger(metric.total)
|| !Number.isInteger(metric.covered)
|| metric.total < 0
|| metric.covered < 0
|| metric.covered > metric.total
) {
throw new Error(`Invalid ${metricName} coverage for ${label}`);
}
return createMetric(metric.covered, metric.total);
}
function aggregateCoverage(coverageEntries: readonly Coverage[]): Coverage {
const counts: Record<MetricName, { covered: number; total: number }> = {
statements: { covered: 0, total: 0 },
branches: { covered: 0, total: 0 },
functions: { covered: 0, total: 0 },
lines: { covered: 0, total: 0 },
};
for (const coverage of coverageEntries) {
for (const metricName of metricNames) {
counts[metricName].covered += coverage[metricName].covered;
counts[metricName].total += coverage[metricName].total;
}
}
return {
statements: createMetric(counts.statements.covered, counts.statements.total),
branches: createMetric(counts.branches.covered, counts.branches.total),
functions: createMetric(counts.functions.covered, counts.functions.total),
lines: createMetric(counts.lines.covered, counts.lines.total),
};
}
function createMetric(covered: number, total: number): ICoverageMetric {
const percentage = total === 0 ? 100 : Math.floor((100_000 * covered) / total / 10) / 100;
return { covered, total, percentage };
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
interface ISurfaceGroup {
readonly covered: number;
readonly total: number;
readonly percentage: number;
readonly uncovered: readonly string[];
}
/**
* Records how much of the Agent Host Protocol the E2E suites actually touch.
*
* Line coverage measures the current host implementation; this measures the
* contract, so it stays meaningful if the implementation behind
* `IAgentHostTarget` is replaced. A symbol counts as covered when it crosses
* the wire in either direction, which is a floor on how well it is tested — the
* value is in the `uncovered` lists, which name contract areas that no test
* exercises at all.
*/
function writeProtocolSurfaceStats(): void {
const observed = readObservedSurface();
const stats = {
version: 1,
scope: {
source: 'src/vs/platform/agentHost/common/state/protocol',
note: 'A symbol is "covered" when an E2E test sends or receives it; this does not measure how deeply its semantics are asserted.',
},
commands: buildSurfaceGroup(declaredCommands(), observed.commands),
notifications: buildSurfaceGroup(declaredNotifications(), observed.notifications),
actions: buildSurfaceGroup(declaredActions(), observed.actions),
};
writeJsonAtomically(surfaceStatsPath, stats);
}
function readObservedSurface(): { commands: Set<string>; notifications: Set<string>; actions: Set<string> } {
if (!existsSync(observedSurfacePath)) {
throw new Error(`No protocol surface observations were written to ${observedSurfacePath}`);
}
const parsed: unknown = JSON.parse(readFileSync(observedSurfacePath, 'utf8'));
if (!isRecord(parsed)) {
throw new Error('The protocol surface observation file must be an object');
}
return {
commands: toStringSet(parsed.commands, 'commands'),
notifications: toStringSet(parsed.notifications, 'notifications'),
actions: toStringSet(parsed.actions, 'actions'),
};
}
function toStringSet(value: unknown, label: string): Set<string> {
if (!Array.isArray(value) || value.some(entry => typeof entry !== 'string')) {
throw new Error(`Expected ${label} to be an array of strings in the protocol surface observations`);
}
return new Set(value as string[]);
}
function buildSurfaceGroup(declared: readonly string[], observed: ReadonlySet<string>): ISurfaceGroup {
if (declared.length === 0) {
throw new Error('Failed to extract any protocol symbols from the generated protocol sources');
}
const uncovered = declared.filter(symbol => !observed.has(symbol));
const covered = declared.length - uncovered.length;
return {
covered,
total: declared.length,
percentage: Math.floor((100_000 * covered) / declared.length / 10) / 100,
uncovered,
};
}
/**
* Extracts `@method` tags from the generated protocol sources. Both commands
* and notifications are documented the same way, so the two are told apart by
* the file they are declared in.
*/
function declaredMethods(fileName: 'commands.ts' | 'notifications.ts'): readonly string[] {
const methods = new Set<string>();
for (const directory of protocolSourceDirectories()) {
const filePath = join(directory, fileName);
if (!existsSync(filePath)) {
continue;
}
for (const match of readFileSync(filePath, 'utf8').matchAll(/@method\s+([A-Za-z][A-Za-z0-9/]*)/g)) {
methods.add(match[1]);
}
}
return [...methods].sort();
}
function declaredCommands(): readonly string[] {
return declaredMethods('commands.ts');
}
function declaredNotifications(): readonly string[] {
return declaredMethods('notifications.ts');
}
/** Extracts the `ActionType` enum members, which are the action discriminants. */
function declaredActions(): readonly string[] {
const source = readFileSync(join(protocolRoot, 'common', 'actions.ts'), 'utf8');
const enumBody = /export const enum ActionType \{([^}]*)\}/.exec(source);
if (!enumBody) {
throw new Error('Failed to locate the ActionType enum in the generated protocol sources');
}
const actions = new Set<string>();
for (const match of enumBody[1].matchAll(/=\s*'([^']+)'/g)) {
actions.add(match[1]);
}
return [...actions].sort();
}
function protocolSourceDirectories(): readonly string[] {
return readdirSync(protocolRoot, { withFileTypes: true })
.filter(entry => entry.isDirectory() && (entry.name === 'common' || entry.name.startsWith('channels-')))
.map(entry => join(protocolRoot, entry.name));
}
function writeJsonAtomically(filePath: string, value: unknown): void {
mkdirSync(dirname(filePath), { recursive: true });
const temporaryPath = `${filePath}.${process.pid}.tmp`;
try {
writeFileSync(temporaryPath, `${JSON.stringify(value, undefined, '\t')}\n`);
renameSync(temporaryPath, filePath);
} finally {
rmSync(temporaryPath, { force: true });
}
}
try {
main();
} catch (error) {
console.error(error instanceof Error ? error.stack : error);
process.exit(1);
}
You can’t perform that action at this time.
