@mwillbanks/ast-mcp 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +71 -22
  2. package/dist/ast-mcp.js +301 -101
  3. package/dist/ast-mcp.js.map +107 -27
  4. package/package.json +3 -1
  5. package/src/ast-bro/capability.ts +3 -8
  6. package/src/ast-bro/client.ts +18 -2
  7. package/src/ast-bro/result.ts +5 -12
  8. package/src/cli.ts +112 -13
  9. package/src/config-cli.ts +54 -0
  10. package/src/config.ts +886 -0
  11. package/src/helpers/aider.ts +252 -0
  12. package/src/helpers/ast-bro.ts +64 -0
  13. package/src/helpers/config.ts +149 -0
  14. package/src/helpers/hook.ts +60 -0
  15. package/src/helpers/installer.ts +249 -0
  16. package/src/helpers/json.ts +19 -0
  17. package/src/helpers/mcp-schema.ts +31 -0
  18. package/src/helpers/process.ts +31 -0
  19. package/src/helpers/string.ts +97 -0
  20. package/src/hook.ts +135 -40
  21. package/src/http-entry.ts +1 -1
  22. package/src/http.ts +118 -103
  23. package/src/installer-transport.ts +190 -0
  24. package/src/installer.ts +596 -235
  25. package/src/mcp-cli.ts +104 -0
  26. package/src/patch/aider.ts +6 -199
  27. package/src/patch/engine.ts +271 -197
  28. package/src/runtime/dependencies.ts +161 -8
  29. package/src/runtime/file-delete.ts +13 -9
  30. package/src/runtime/file-read.ts +91 -57
  31. package/src/runtime/file-rename.ts +158 -0
  32. package/src/runtime/format.ts +57 -34
  33. package/src/runtime/paths.ts +30 -16
  34. package/src/runtime/policy.ts +21 -0
  35. package/src/runtime/process-input.ts +14 -4
  36. package/src/service.ts +219 -0
  37. package/src/shell-policy.ts +331 -0
  38. package/src/stdio.ts +47 -35
  39. package/src/tools/ast-bro.ts +12 -6
  40. package/src/tools/configured.ts +28 -0
  41. package/src/tools/files.ts +24 -27
  42. package/src/tools/index.ts +5 -3
  43. package/src/tools/lifecycle.ts +69 -55
  44. package/templates/skills/ast-mcp/SKILL.md +7 -6
  45. package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
  46. package/templates/skills/ast-mcp/evals/evals.json +1 -0
  47. package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
  48. package/templates/skills/ast-mcp/evals/score.ts +304 -267
  49. package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
  50. package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
  51. package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
  52. package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
  53. package/templates/skills/ast-mcp/evals/source.ts +164 -0
  54. package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
  55. package/templates/skills/ast-mcp/references/installation.md +24 -11
  56. package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
  57. package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
  58. package/templates/skills/ast-mcp/scripts/check-install.ts +312 -110
@@ -5,6 +5,7 @@ import {
5
5
  type EvalCase as SemanticEvalCase,
6
6
  verifyEvaluation,
7
7
  } from "./semantic";
8
+ import { extractExecInvocations as extractExecInvocationsHelper } from "./source";
8
9
 
9
10
  type EvalCase = SemanticEvalCase;
10
11
 
@@ -68,108 +69,8 @@ function messageText(content: unknown) {
68
69
  .join("\n");
69
70
  }
70
71
 
71
- function executableSource(source: string) {
72
- let state: "code" | "line" | "block" | "single" | "double" | "template" =
73
- "code";
74
- let escaped = false;
75
- let masked = "";
76
- for (let index = 0; index < source.length; index += 1) {
77
- const character = source[index];
78
- const next = source[index + 1];
79
- if (state === "line") {
80
- if (character === "\n") {
81
- state = "code";
82
- masked += character;
83
- } else masked += " ";
84
- continue;
85
- }
86
- if (state === "block") {
87
- if (character === "*" && next === "/") {
88
- masked += " ";
89
- index += 1;
90
- state = "code";
91
- } else masked += character === "\n" ? "\n" : " ";
92
- continue;
93
- }
94
- if (state !== "code") {
95
- masked += character === "\n" ? "\n" : " ";
96
- if (escaped) escaped = false;
97
- else if (character === "\\") escaped = true;
98
- else if (
99
- (state === "single" && character === "'") ||
100
- (state === "double" && character === '"') ||
101
- (state === "template" && character === "`")
102
- )
103
- state = "code";
104
- continue;
105
- }
106
- if (character === "/" && next === "/") {
107
- masked += " ";
108
- index += 1;
109
- state = "line";
110
- } else if (character === "/" && next === "*") {
111
- masked += " ";
112
- index += 1;
113
- state = "block";
114
- } else if (character === "'") {
115
- masked += " ";
116
- state = "single";
117
- } else if (character === '"') {
118
- masked += " ";
119
- state = "double";
120
- } else if (character === "`") {
121
- masked += " ";
122
- state = "template";
123
- } else masked += character;
124
- }
125
- return masked;
126
- }
127
- function isInsidePromiseAll(source: string, position: number) {
128
- const executable = executableSource(source);
129
- const promise = executable.lastIndexOf("Promise.all", position);
130
- if (promise < 0) return false;
131
- const open = executable.indexOf("(", promise);
132
- if (open < 0 || open >= position) return false;
133
- let depth = 1;
134
- for (let index = open + 1; index < position; index += 1) {
135
- if (executable[index] === "(") depth += 1;
136
- else if (executable[index] === ")") depth -= 1;
137
- }
138
- return depth > 0;
139
- }
140
-
141
72
  function extractExecInvocations(source: string) {
142
- const invocations: Array<Omit<Invocation, "index">> = [];
143
- const expression = /tools\.mcp__ast_mcp__(\w+)\s*\(/g;
144
- for (const match of executableSource(source).matchAll(expression)) {
145
- const open = (match.index ?? 0) + match[0].length - 1;
146
- let depth = 1;
147
- let quote = "";
148
- let escaped = false;
149
- let end = open + 1;
150
- for (; end < source.length && depth > 0; end += 1) {
151
- const character = source[end];
152
- if (quote) {
153
- if (escaped) escaped = false;
154
- else if (character === "\\") escaped = true;
155
- else if (character === quote) quote = "";
156
- continue;
157
- }
158
- if (character === '"' || character === "'" || character === "`") {
159
- quote = character;
160
- continue;
161
- }
162
- if (character === "(") depth += 1;
163
- else if (character === ")") depth -= 1;
164
- }
165
- if (depth !== 0) continue;
166
- invocations.push({
167
- concurrent: isInsidePromiseAll(source, match.index ?? 0),
168
- input: source.slice(open + 1, end - 1).trim(),
169
- tool: match[1],
170
- });
171
- }
172
- return invocations;
73
+ return extractExecInvocationsHelper(source);
173
74
  }
174
75
 
175
76
  function outputText(output: unknown) {
@@ -207,211 +108,347 @@ async function loadCases() {
207
108
  ];
208
109
  }
209
110
 
210
- export async function scoreTranscript(
211
- sessionPath: string,
212
- strict = false,
213
- ): Promise<TranscriptScore> {
214
- const errors: string[] = [];
215
- const calls = new Map<string, TranscriptCall>();
216
- const knownCallIds = new Set<string>();
217
- const outputs = new Map<string, RecordPayload>();
218
- const roots = new Set<string>();
219
- const toolCalls: Record<string, number> = {};
220
- let invocationIndex = 0;
221
- let activeEvalIds: number[] = [];
222
- const lines = (await readFile(sessionPath, "utf8"))
223
- .split(String.fromCharCode(10))
224
- .filter(Boolean);
111
+ class TranscriptCollector {
112
+ readonly calls = new Map<string, TranscriptCall>();
113
+ readonly errors: string[] = [];
114
+ readonly knownCallIds = new Set<string>();
115
+ readonly outputs = new Map<string, RecordPayload>();
116
+ readonly roots = new Set<string>();
117
+ private activeEvalIds: number[] = [];
118
+ private invocationIndex = 0;
225
119
 
226
- for (const [index, line] of lines.entries()) {
120
+ processLine(line: string, index: number) {
227
121
  let record: TranscriptRecord;
228
122
  try {
229
123
  record = JSON.parse(line) as TranscriptRecord;
230
124
  } catch {
231
- errors.push(`line ${index + 1}: invalid JSON`);
232
- continue;
125
+ this.errors.push(`line ${index + 1}: invalid JSON`);
126
+ return;
233
127
  }
128
+ this.processRecord(record);
129
+ }
234
130
 
235
- const payload = record.payload;
236
- if (record.type === "turn_context") {
237
- if (typeof payload?.cwd === "string")
238
- roots.add(path.resolve(payload.cwd));
239
- for (const root of payload?.workspace_roots ?? [])
240
- roots.add(path.resolve(root));
241
- continue;
242
- }
243
- if (
244
- record.type === "response_item" &&
245
- payload?.type === "message" &&
246
- payload.role === "user"
247
- ) {
248
- activeEvalIds = [
249
- ...new Set(
250
- [...messageText(payload.content).matchAll(evalPattern)].map((match) =>
251
- Number(match[1]),
252
- ),
253
- ),
254
- ];
255
- if (activeEvalIds.length > 1)
256
- errors.push(
257
- "user evaluation prompt carries multiple eval markers; evidence must be isolated per case",
258
- );
259
- continue;
260
- }
131
+ private processRecord(record: TranscriptRecord) {
132
+ const payload = record.payload ?? {};
133
+ if (record.type === "turn_context") return this.recordRoots(payload);
134
+ if (record.type !== "response_item") return;
135
+ responseRecordHandlers[payload.type ?? ""]?.(this, payload);
136
+ }
261
137
 
262
- if (
263
- record.type === "response_item" &&
264
- payload?.type === "custom_tool_call" &&
265
- typeof payload.call_id === "string" &&
266
- typeof payload.name === "string"
267
- ) {
268
- knownCallIds.add(payload.call_id);
269
- if (calls.has(payload.call_id)) {
270
- errors.push(`duplicate call ID: ${payload.call_id}`);
271
- continue;
272
- }
273
- const source = inputText(payload.input);
274
- const rawInvocations = payload.name.startsWith(directToolPrefix)
275
- ? [
276
- {
277
- input: source,
278
- tool: payload.name.slice(directToolPrefix.length),
279
- },
280
- ]
281
- : payload.name === "exec"
282
- ? extractExecInvocations(source)
283
- : [];
284
- if (rawInvocations.length === 0) continue;
285
- const invocations = rawInvocations.map((invocation) => ({
286
- ...invocation,
287
- index: invocationIndex++,
288
- }));
289
- const evalIds = [
290
- ...new Set([
291
- ...activeEvalIds,
292
- ...[...source.matchAll(evalPattern)].map((match) => Number(match[1])),
293
- ]),
294
- ];
295
- if (evalIds.length > 1)
296
- errors.push(
297
- `call ${payload.call_id} carries multiple eval markers; evidence must be isolated per case`,
298
- );
299
- calls.set(payload.call_id, {
300
- evalIds,
301
- id: payload.call_id,
302
- invocations,
303
- source,
304
- tools: invocations.map((invocation) => invocation.tool),
305
- verifiable: payload.name.startsWith(directToolPrefix),
306
- });
307
- continue;
308
- }
138
+ recordRoots(payload: RecordPayload) {
139
+ if (typeof payload.cwd === "string")
140
+ this.roots.add(path.resolve(payload.cwd));
141
+ for (const root of payload.workspace_roots ?? [])
142
+ this.roots.add(path.resolve(root));
143
+ }
144
+
145
+ recordMessage(payload: RecordPayload) {
146
+ if (payload.role !== "user") return;
147
+ this.activeEvalIds = markerIds(messageText(payload.content));
148
+ this.validateIsolated(this.activeEvalIds, "user evaluation prompt");
149
+ }
309
150
 
310
- if (
311
- record.type === "response_item" &&
312
- payload?.type === "custom_tool_call_output" &&
313
- typeof payload.call_id === "string"
314
- ) {
315
- if (outputs.has(payload.call_id))
316
- errors.push(`duplicate output ID: ${payload.call_id}`);
317
- else outputs.set(payload.call_id, payload);
151
+ recordCall(payload: RecordPayload) {
152
+ if (typeof payload.call_id !== "string" || typeof payload.name !== "string")
153
+ return;
154
+ this.knownCallIds.add(payload.call_id);
155
+ if (this.calls.has(payload.call_id)) {
156
+ this.errors.push(`duplicate call ID: ${payload.call_id}`);
157
+ return;
318
158
  }
159
+ const source = inputText(payload.input);
160
+ const rawInvocations = invocationsFor(payload.name, source);
161
+ if (rawInvocations.length === 0) return;
162
+ const invocations = rawInvocations.map((invocation) => ({
163
+ ...invocation,
164
+ index: this.invocationIndex++,
165
+ }));
166
+ const evalIds = markerIds(source, this.activeEvalIds);
167
+ this.validateIsolated(evalIds, `call ${payload.call_id}`);
168
+ this.calls.set(payload.call_id, {
169
+ evalIds,
170
+ id: payload.call_id,
171
+ invocations,
172
+ source,
173
+ tools: invocations.map((invocation) => invocation.tool),
174
+ verifiable: payload.name.startsWith(directToolPrefix),
175
+ });
176
+ }
177
+
178
+ recordOutput(payload: RecordPayload) {
179
+ if (typeof payload.call_id !== "string") return;
180
+ if (this.outputs.has(payload.call_id))
181
+ this.errors.push(`duplicate output ID: ${payload.call_id}`);
182
+ else this.outputs.set(payload.call_id, payload);
319
183
  }
320
184
 
185
+ private validateIsolated(evalIds: number[], source: string) {
186
+ if (evalIds.length > 1)
187
+ this.errors.push(
188
+ `${source} carries multiple eval markers; evidence must be isolated per case`,
189
+ );
190
+ }
191
+ }
192
+
193
+ type ResponseRecordHandler = (
194
+ collector: TranscriptCollector,
195
+ payload: RecordPayload,
196
+ ) => void;
197
+
198
+ const responseRecordHandlers: Record<string, ResponseRecordHandler> = {
199
+ custom_tool_call: (collector, payload) => collector.recordCall(payload),
200
+ custom_tool_call_output: (collector, payload) =>
201
+ collector.recordOutput(payload),
202
+ message: (collector, payload) => collector.recordMessage(payload),
203
+ };
204
+
205
+ function markerIds(source: string, initial: number[] = []) {
206
+ return [
207
+ ...new Set([
208
+ ...initial,
209
+ ...[...source.matchAll(evalPattern)].map((match) => Number(match[1])),
210
+ ]),
211
+ ];
212
+ }
213
+
214
+ function invocationsFor(name: string, source: string) {
215
+ if (name.startsWith(directToolPrefix))
216
+ return [{ input: source, tool: name.slice(directToolPrefix.length) }];
217
+ if (name === "exec") return extractExecInvocations(source);
218
+ return [];
219
+ }
220
+
221
+ async function collectTranscript(sessionPath: string) {
222
+ const collector = new TranscriptCollector();
223
+ const lines = (await readFile(sessionPath, "utf8"))
224
+ .split(String.fromCharCode(10))
225
+ .filter(Boolean);
226
+ for (const [index, line] of lines.entries())
227
+ collector.processLine(line, index);
228
+ return collector;
229
+ }
230
+
231
+ function acceptedRenameFailure(
232
+ call: TranscriptCall,
233
+ evaluationId?: number,
234
+ assertions: string[] = [],
235
+ ) {
236
+ if (!call.output || !outputFailed(call.output)) return false;
237
+ if (!(evaluationId === 92 || call.evalIds.includes(92))) return false;
238
+ if (
239
+ !assertions.some((assertion) =>
240
+ /does not overwrite|existing destination/i.test(assertion),
241
+ ) &&
242
+ evaluationId !== undefined
243
+ )
244
+ return false;
245
+ if (!call.invocations.some((invocation) => invocation.tool === "file_rename"))
246
+ return false;
247
+ return /destination.*exist|already exists/i.test(
248
+ outputText(call.output.output),
249
+ );
250
+ }
251
+
252
+ function validateCallOutput(
253
+ call: TranscriptCall,
254
+ output: RecordPayload,
255
+ errors: string[],
256
+ ) {
257
+ if (outputFailed(output) && !acceptedRenameFailure({ ...call, output }))
258
+ errors.push(`failed call ID: ${call.id}`);
259
+ const text = outputText(output.output);
260
+ if (call.tools.length > 0 && text.length === 0)
261
+ errors.push(`empty output for ast-mcp call ID: ${call.id}`);
262
+ return text.length;
263
+ }
264
+
265
+ function countTools(call: TranscriptCall, toolCalls: Record<string, number>) {
266
+ for (const invocation of call.invocations)
267
+ toolCalls[invocation.tool] = (toolCalls[invocation.tool] ?? 0) + 1;
268
+ }
269
+
270
+ function bindOutputs(
271
+ collector: TranscriptCollector,
272
+ toolCalls: Record<string, number>,
273
+ ) {
321
274
  let outputChars = 0;
322
- for (const call of calls.values()) {
323
- const output = outputs.get(call.id);
275
+ for (const call of collector.calls.values()) {
276
+ const output = collector.outputs.get(call.id);
324
277
  if (!output) {
325
- errors.push(`missing output for call ID: ${call.id}`);
278
+ collector.errors.push(`missing output for call ID: ${call.id}`);
326
279
  continue;
327
280
  }
328
281
  call.output = output;
329
- if (outputFailed(output)) errors.push(`failed call ID: ${call.id}`);
330
- const text = outputText(output.output);
331
- outputChars += text.length;
332
- if (call.tools.length > 0 && text.length === 0)
333
- errors.push(`empty output for ast-mcp call ID: ${call.id}`);
334
- for (const invocation of call.invocations)
335
- toolCalls[invocation.tool] = (toolCalls[invocation.tool] ?? 0) + 1;
282
+ outputChars += validateCallOutput(call, output, collector.errors);
283
+ countTools(call, toolCalls);
336
284
  }
285
+ for (const outputId of collector.outputs.keys()) {
286
+ if (!collector.knownCallIds.has(outputId))
287
+ collector.errors.push(`unmatched output ID: ${outputId}`);
288
+ }
289
+ return outputChars;
290
+ }
337
291
 
338
- for (const outputId of outputs.keys())
339
- if (!knownCallIds.has(outputId))
340
- errors.push(`unmatched output ID: ${outputId}`);
292
+ interface GroupedEvaluations {
293
+ evaluations: Map<number, TranscriptCall[]>;
294
+ unscoredBatches: number;
295
+ }
341
296
 
342
- const cases = new Map(
343
- (await loadCases()).map((evaluation) => [evaluation.id, evaluation]),
344
- );
345
- let evaluatedCases = 0;
346
- let unscoredBatches = 0;
347
- let verifiedAssertions = 0;
297
+ function groupEvaluations(
298
+ calls: Map<string, TranscriptCall>,
299
+ ): GroupedEvaluations {
348
300
  const evaluations = new Map<number, TranscriptCall[]>();
301
+ let unscoredBatches = 0;
349
302
  for (const call of calls.values()) {
350
303
  if (call.evalIds.length === 0) {
351
304
  unscoredBatches += 1;
352
305
  continue;
353
306
  }
354
- if (call.evalIds.length > 1) continue;
355
- for (const evalId of call.evalIds) {
356
- const evaluationCalls = evaluations.get(evalId) ?? [];
357
- evaluationCalls.push(call);
358
- evaluations.set(evalId, evaluationCalls);
359
- }
307
+ if (call.evalIds.length !== 1) continue;
308
+ const evalId = call.evalIds[0] ?? 0;
309
+ const evaluationCalls = evaluations.get(evalId) ?? [];
310
+ evaluationCalls.push(call);
311
+ evaluations.set(evalId, evaluationCalls);
312
+ }
313
+ return { evaluations, unscoredBatches };
314
+ }
315
+
316
+ function callExecutionFailed(call: TranscriptCall, evaluation: EvalCase) {
317
+ if (!call.output) return true;
318
+ if (!outputFailed(call.output)) return false;
319
+ return !acceptedRenameFailure(call, evaluation.id, evaluation.assertions);
320
+ }
321
+
322
+ function evaluationText(calls: TranscriptCall[]) {
323
+ return calls
324
+ .map((call) => outputText(call.output?.output))
325
+ .join(String.fromCharCode(10));
326
+ }
327
+
328
+ function evaluationInvocations(calls: TranscriptCall[]) {
329
+ return calls
330
+ .flatMap((call) => call.invocations)
331
+ .sort((left, right) => left.index - right.index);
332
+ }
333
+
334
+ interface EvaluationResult {
335
+ evaluatedCases: number;
336
+ verifiedAssertions: number;
337
+ }
338
+
339
+ function evaluateCase(
340
+ evalId: number,
341
+ calls: TranscriptCall[],
342
+ evaluation: EvalCase | undefined,
343
+ roots: string[],
344
+ errors: string[],
345
+ ) {
346
+ if (!evaluation) {
347
+ errors.push(`unknown eval ID: ${evalId}`);
348
+ return 0;
360
349
  }
350
+ if (calls.some((call) => !call.verifiable)) {
351
+ errors.push(
352
+ `eval ${evalId} uses nested exec evidence that cannot be bound to individual MCP results`,
353
+ );
354
+ return 0;
355
+ }
356
+ const executionFailed = calls.some((call) =>
357
+ callExecutionFailed(call, evaluation),
358
+ );
359
+ if (executionFailed)
360
+ errors.push(`eval ${evalId} has no successful execution output`);
361
+ const semanticErrors = verifyEvaluation(
362
+ evaluation,
363
+ evaluationInvocations(calls),
364
+ evaluationText(calls),
365
+ roots,
366
+ );
367
+ for (const error of semanticErrors) errors.push(`eval ${evalId} ${error}`);
368
+ return !executionFailed && semanticErrors.length === 0
369
+ ? evaluation.assertions.length
370
+ : 0;
371
+ }
361
372
 
362
- for (const [evalId, evaluationCalls] of evaluations) {
373
+ function evaluateGroups(
374
+ grouped: GroupedEvaluations,
375
+ cases: Map<number, EvalCase>,
376
+ roots: string[],
377
+ errors: string[],
378
+ ): EvaluationResult {
379
+ let evaluatedCases = 0;
380
+ let verifiedAssertions = 0;
381
+ for (const [evalId, calls] of grouped.evaluations) {
363
382
  evaluatedCases += 1;
364
- const evaluation = cases.get(evalId);
365
- if (!evaluation) {
366
- errors.push(`unknown eval ID: ${evalId}`);
367
- continue;
368
- }
369
- if (evaluationCalls.some((call) => !call.verifiable)) {
370
- errors.push(
371
- `eval ${evalId} uses nested exec evidence that cannot be bound to individual MCP results`,
372
- );
373
- continue;
374
- }
375
- const executionFailed = evaluationCalls.some(
376
- (call) => !call.output || outputFailed(call.output),
383
+ verifiedAssertions += evaluateCase(
384
+ evalId,
385
+ calls,
386
+ cases.get(evalId),
387
+ roots,
388
+ errors,
377
389
  );
378
- if (executionFailed)
379
- errors.push(`eval ${evalId} has no successful execution output`);
380
- const text = evaluationCalls
381
- .map((call) => outputText(call.output?.output))
382
- .join(String.fromCharCode(10));
383
- const invocations = evaluationCalls
384
- .flatMap((call) => call.invocations)
385
- .sort((left, right) => left.index - right.index);
386
- const semanticErrors = verifyEvaluation(evaluation, invocations, text, [
387
- ...roots,
388
- ]);
389
- for (const error of semanticErrors) errors.push(`eval ${evalId} ${error}`);
390
- if (!executionFailed && semanticErrors.length === 0)
391
- verifiedAssertions += evaluation.assertions.length;
392
390
  }
391
+ return { evaluatedCases, verifiedAssertions };
392
+ }
393
393
 
394
- if (strict && evaluatedCases === 0)
394
+ function validateStrictMatrix(
395
+ strict: boolean,
396
+ result: EvaluationResult,
397
+ cases: Map<number, EvalCase>,
398
+ evaluations: Map<number, TranscriptCall[]>,
399
+ errors: string[],
400
+ ) {
401
+ if (!strict) return;
402
+ if (result.evaluatedCases === 0) {
395
403
  errors.push("strict scoring requires at least one ast-mcp-eval marker");
396
- if (strict && evaluatedCases > 0)
397
- for (const evaluation of cases.values())
398
- if (!evaluations.has(evaluation.id))
399
- errors.push(
400
- `strict scoring requires complete evaluation matrix; missing eval ${evaluation.id}`,
401
- );
404
+ return;
405
+ }
406
+ for (const evaluation of cases.values()) {
407
+ if (!evaluations.has(evaluation.id))
408
+ errors.push(
409
+ `strict scoring requires complete evaluation matrix; missing eval ${evaluation.id}`,
410
+ );
411
+ }
412
+ }
402
413
 
414
+ export async function scoreTranscript(
415
+ sessionPath: string,
416
+ strict = false,
417
+ ): Promise<TranscriptScore> {
418
+ const collector = await collectTranscript(sessionPath);
419
+ const toolCalls: Record<string, number> = {};
420
+ const outputChars = bindOutputs(collector, toolCalls);
421
+ const cases = new Map(
422
+ (await loadCases()).map((evaluation) => [evaluation.id, evaluation]),
423
+ );
424
+ const grouped = groupEvaluations(collector.calls);
425
+ const result = evaluateGroups(
426
+ grouped,
427
+ cases,
428
+ [...collector.roots],
429
+ collector.errors,
430
+ );
431
+ validateStrictMatrix(
432
+ strict,
433
+ result,
434
+ cases,
435
+ grouped.evaluations,
436
+ collector.errors,
437
+ );
403
438
  const mutationCalls =
404
- (toolCalls.file_patch ?? 0) + (toolCalls.file_write ?? 0);
439
+ (toolCalls.file_patch ?? 0) +
440
+ (toolCalls.file_write ?? 0) +
441
+ (toolCalls.file_rename ?? 0);
405
442
  return {
406
443
  astMcpOutputChars: outputChars,
407
- errors,
408
- evaluatedCases,
409
- execBatches: calls.size,
444
+ errors: collector.errors,
445
+ evaluatedCases: result.evaluatedCases,
446
+ execBatches: collector.calls.size,
410
447
  mutationCalls,
411
- passed: errors.length === 0,
448
+ passed: collector.errors.length === 0,
412
449
  session: sessionPath,
413
450
  toolCalls,
414
- unscoredBatches,
415
- verifiedAssertions,
451
+ unscoredBatches: grouped.unscoredBatches,
452
+ verifiedAssertions: result.verifiedAssertions,
416
453
  };
417
454
  }