-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathdrift-report-collector.ts
More file actions
423 lines (373 loc) · 13.3 KB
/
drift-report-collector.ts
File metadata and controls
423 lines (373 loc) · 13.3 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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/// <reference types="node" />
/**
* Drift Report Collector
*
* Runs the drift test suite via subprocess with JSON reporter, parses the
* structured output, and writes a drift-report.json file that downstream
* scripts can use to construct auto-fix prompts.
*
* Exit codes:
* 0 — no critical diffs found (or no drift at all)
* 2 — at least one critical diff exists
* 1 — script error (unhandled exception)
*
* Usage:
* npx tsx scripts/drift-report-collector.ts [--out drift-report.json]
*/
import { execSync } from "node:child_process";
import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
import type { DriftEntry, DriftReport, DriftSeverity, ParsedDiff } from "./drift-types.js";
// ---------------------------------------------------------------------------
// Vitest JSON reporter types (subset we care about)
// ---------------------------------------------------------------------------
interface VitestJsonResult {
testResults: VitestTestFile[];
}
interface VitestTestFile {
assertionResults: VitestAssertion[];
}
interface VitestAssertion {
status: string;
ancestorTitles: string[];
title: string;
failureMessages: string[];
}
// ---------------------------------------------------------------------------
// Provider → file mapping
// ---------------------------------------------------------------------------
interface ProviderMapping {
builderFile: string;
builderFunctions: string[];
typesFile: string | null;
sdkShapesFile?: string;
}
const OPENAI_CHAT_MAPPING: ProviderMapping = {
builderFile: "src/helpers.ts",
builderFunctions: [
"buildTextCompletion",
"buildToolCallCompletion",
"buildTextChunks",
"buildToolCallChunks",
],
typesFile: "src/types.ts",
};
const OPENAI_RESPONSES_MAPPING: ProviderMapping = {
builderFile: "src/responses.ts",
builderFunctions: [
"buildTextResponse",
"buildToolCallResponse",
"buildTextStreamEvents",
"buildToolCallStreamEvents",
],
typesFile: null,
};
const ANTHROPIC_MAPPING: ProviderMapping = {
builderFile: "src/messages.ts",
builderFunctions: [
"buildClaudeTextResponse",
"buildClaudeToolCallResponse",
"buildClaudeTextStreamEvents",
"buildClaudeToolCallStreamEvents",
],
typesFile: null,
};
const GEMINI_MAPPING: ProviderMapping = {
builderFile: "src/gemini.ts",
builderFunctions: [
"buildGeminiTextResponse",
"buildGeminiToolCallResponse",
"buildGeminiTextStreamChunks",
"buildGeminiToolCallStreamChunks",
],
typesFile: null,
};
const OPENAI_EMBEDDINGS_MAPPING: ProviderMapping = {
builderFile: "src/helpers.ts",
builderFunctions: ["buildEmbeddingResponse", "generateDeterministicEmbedding"],
typesFile: null,
sdkShapesFile: "src/__tests__/drift/sdk-shapes.ts",
};
/**
* Maps provider names (from drift test describe blocks) to source files
* and builder function names. The function names are builder functions for
* each provider (internal or exported) — they are included so Claude Code
* can locate them via Read/Grep.
*/
const PROVIDER_MAP: Record<string, ProviderMapping> = {
"OpenAI Chat": OPENAI_CHAT_MAPPING,
"OpenAI Responses": OPENAI_RESPONSES_MAPPING,
Anthropic: ANTHROPIC_MAPPING,
"Anthropic Claude": ANTHROPIC_MAPPING,
"Google Gemini": GEMINI_MAPPING,
Gemini: GEMINI_MAPPING,
"OpenAI Realtime": {
builderFile: "src/ws-realtime.ts",
builderFunctions: ["handleWebSocketRealtime", "realtimeItemsToMessages"],
typesFile: null,
},
"OpenAI Responses WS": {
builderFile: "src/ws-responses.ts",
builderFunctions: ["handleWebSocketResponses"],
typesFile: null,
},
"Gemini Live": {
builderFile: "src/ws-gemini-live.ts",
builderFunctions: ["handleWebSocketGeminiLive"],
typesFile: null,
},
"OpenAI Embeddings": OPENAI_EMBEDDINGS_MAPPING,
};
const SDK_SHAPES_FILE = "src/__tests__/drift/sdk-shapes.ts";
// ---------------------------------------------------------------------------
// Parse the formatted drift report text from a vitest failure message
// ---------------------------------------------------------------------------
/**
* Parse a drift report block from raw vitest failure message content.
*
* The input is a raw vitest failureMessages string that may contain error boilerplate.
* The function scans for the API DRIFT DETECTED header and numbered entries.
*
* Expected format within the message (produced by formatDriftReport):
* ```
* API DRIFT DETECTED: OpenAI Chat (non-streaming text)
*
* 1. [critical] LLMOCK DRIFT — field in SDK + real API but missing from mock
* Path: choices[0].message.refusal
* SDK: null
* Real: null
* Mock: <absent>
* ```
*/
const VALID_SEVERITIES = new Set<DriftSeverity>(["critical", "warning", "info"]);
function parseDriftBlock(text: string): { context: string; diffs: ParsedDiff[] } | null {
const headerMatch = text.match(/API DRIFT DETECTED:\s*(.+)/);
if (!headerMatch) return null;
const context = headerMatch[1].trim();
const diffs: ParsedDiff[] = [];
// Match numbered entries: " 1. [severity] issue text\n Path:...\n SDK:...\n Real:...\n Mock:..."
const entryPattern =
/\d+\.\s*\[(\w+)\]\s*(.+)\n\s*Path:\s*(.+)\n\s*SDK:\s*(.+)\n\s*Real:\s*(.+)\n\s*Mock:\s*(.+)/g;
let match: RegExpExecArray | null;
while ((match = entryPattern.exec(text)) !== null) {
const severity = match[1].trim();
if (!VALID_SEVERITIES.has(severity as DriftSeverity)) {
console.warn(
`parseDriftBlock: unknown severity "${severity}" — skipping entry. ` +
`Known severities: ${[...VALID_SEVERITIES].join(", ")}`,
);
continue;
}
diffs.push({
severity: severity as DriftSeverity,
issue: match[2].trim(),
path: match[3].trim(),
expected: match[4].trim(),
real: match[5].trim(),
mock: match[6].trim(),
});
}
const expectedCount = (text.match(/\d+\.\s*\[/g) ?? []).length;
if (expectedCount > 0 && diffs.length < expectedCount) {
console.warn(`parseDriftBlock: parsed ${diffs.length} of ${expectedCount} entries`);
}
return { context, diffs };
}
/**
* Extract provider name from the describe block title or the drift report context.
*
* Examples:
* "OpenAI Chat Completions drift" → "OpenAI Chat"
* "OpenAI Chat (non-streaming text)" → "OpenAI Chat"
* "Anthropic Claude drift" → "Anthropic Claude"
*/
function extractProviderName(text: string): string | null {
// Try matching against known provider keys (longest first to avoid partial matches)
const sorted = Object.keys(PROVIDER_MAP).sort((a, b) => b.length - a.length);
for (const key of sorted) {
if (text.includes(key)) return key;
}
return null;
}
/**
* Extract scenario from the context string.
*
* "OpenAI Chat (non-streaming text)" → "non-streaming text"
* "Anthropic Claude (streaming tool call)" → "streaming tool call"
*/
function extractScenario(context: string): string {
const parenMatch = context.match(/\(([^)]+)\)/);
return parenMatch ? parenMatch[1] : context;
}
// ---------------------------------------------------------------------------
// Run drift tests and collect results
// ---------------------------------------------------------------------------
function extractJsonFromString(text: string): VitestJsonResult | null {
const jsonStart = text.indexOf("{");
const jsonEnd = text.lastIndexOf("}");
if (jsonStart === -1 || jsonEnd === -1) return null;
try {
const parsed = JSON.parse(text.slice(jsonStart, jsonEnd + 1)) as unknown;
if (
!parsed ||
typeof parsed !== "object" ||
!Array.isArray((parsed as Record<string, unknown>).testResults)
) {
console.error(
"extractJsonFromString: parsed JSON does not have testResults array, likely wrong fragment",
);
return null;
}
return parsed as VitestJsonResult;
} catch (err: unknown) {
console.error(
"extractJsonFromString: failed to parse.",
`Range: [${jsonStart}..${jsonEnd}], length: ${text.length}`,
err instanceof Error ? err.message : String(err),
);
return null;
}
}
function hasStdout(err: unknown): err is { stdout: string; stderr?: string } {
return (
typeof err === "object" &&
err !== null &&
"stdout" in err &&
typeof (err as { stdout: unknown }).stdout === "string"
);
}
function parseVitestOutput(stdout: string, context: string): VitestJsonResult | null {
try {
return JSON.parse(stdout) as VitestJsonResult;
} catch (parseErr: unknown) {
console.error(
`${context}:`,
parseErr instanceof Error ? parseErr.message : String(parseErr),
`stdout length: ${stdout.length}`,
);
return extractJsonFromString(stdout);
}
}
function runDriftTests(): VitestJsonResult {
try {
const stdout = execSync("pnpm test:drift --reporter=json", {
encoding: "utf-8",
stdio: ["pipe", "pipe", "pipe"],
maxBuffer: 50 * 1024 * 1024,
});
const result = parseVitestOutput(stdout, "JSON parse of successful vitest run failed");
if (result) return result;
throw new Error("Drift tests passed but produced unparseable output");
} catch (err: unknown) {
// execSync throws on non-zero exit — vitest exits 1 when tests fail
if (hasStdout(err)) {
const result = parseVitestOutput(err.stdout, "Primary JSON parse of vitest stdout failed");
if (result) return result;
console.error(
"Failed to parse JSON from drift test stdout. Original error:",
err instanceof Error ? err.message : String(err),
);
if (err.stderr) console.error("stderr:", err.stderr);
}
const msg = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to run drift tests: ${msg}`);
}
}
function collectDriftEntries(results: VitestJsonResult): DriftEntry[] {
const entries: DriftEntry[] = [];
const unmapped: string[] = [];
let unparseable = 0;
for (const file of results.testResults) {
for (const assertion of file.assertionResults) {
if (assertion.status !== "failed") continue;
if (assertion.failureMessages.length === 0) continue;
const fullMessage = assertion.failureMessages.join("\n");
const parsed = parseDriftBlock(fullMessage);
if (!parsed || parsed.diffs.length === 0) {
unparseable++;
continue;
}
// Determine provider from ancestor titles (describe block) or context
const ancestorText = assertion.ancestorTitles.join(" ");
const provider = extractProviderName(ancestorText) ?? extractProviderName(parsed.context);
if (!provider) {
unmapped.push(`${ancestorText} > ${assertion.title}`);
continue;
}
const mapping = PROVIDER_MAP[provider];
if (!mapping) {
unmapped.push(`${ancestorText} > ${assertion.title} (provider: ${provider})`);
continue;
}
entries.push({
provider,
scenario: extractScenario(parsed.context),
builderFile: mapping.builderFile,
builderFunctions: mapping.builderFunctions,
typesFile: mapping.typesFile,
sdkShapesFile: SDK_SHAPES_FILE,
diffs: parsed.diffs,
});
}
}
if (unmapped.length > 0) {
console.error(`ERROR: ${unmapped.length} drift failure(s) could not be mapped to a provider:`);
for (const u of unmapped) console.error(` - ${u}`);
throw new Error(`${unmapped.length} unmapped drift entries — update PROVIDER_MAP`);
}
if (unparseable > 0 && entries.length === 0) {
console.error(
`ERROR: ${unparseable} test failure(s) could not be parsed as drift reports.`,
"This may indicate broken test infrastructure or a changed report format.",
);
throw new Error(`${unparseable} unparseable test failures with 0 drift entries — investigate`);
} else if (unparseable > 0) {
console.warn(
`WARNING: ${unparseable} test failure(s) did not contain parseable drift data (${entries.length} drift entries collected).`,
);
}
return entries;
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
function main(): void {
const args = process.argv.slice(2);
const outIndex = args.indexOf("--out");
const outPath = resolve(
outIndex !== -1 && args[outIndex + 1] ? args[outIndex + 1] : "drift-report.json",
);
console.log("Running drift tests...");
const results = runDriftTests();
console.log("Collecting drift entries...");
const entries = collectDriftEntries(results);
const report: DriftReport = {
timestamp: new Date().toISOString(),
entries,
};
try {
writeFileSync(outPath, JSON.stringify(report, null, 2) + "\n", "utf-8");
} catch (err) {
console.error(`Failed to write drift report to ${outPath}:`, err);
console.log(JSON.stringify(report, null, 2));
process.exit(1);
}
console.log(`Drift report written to ${outPath}`);
console.log(` Entries: ${entries.length}`);
const criticalCount = entries.reduce(
(sum, e) => sum + e.diffs.filter((d) => d.severity === "critical").length,
0,
);
console.log(` Critical diffs: ${criticalCount}`);
if (criticalCount > 0) {
console.log("Exiting with code 2 (critical diffs found).");
process.exit(2);
}
console.log("No critical diffs found.");
}
try {
main();
} catch (err: unknown) {
console.error("Fatal error:", err);
process.exit(1);
}