@mwillbanks/ast-mcp 0.1.3 → 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.
- package/README.md +59 -20
- package/dist/ast-mcp.js +232 -130
- package/dist/ast-mcp.js.map +107 -27
- package/package.json +2 -1
- package/src/ast-bro/capability.ts +3 -8
- package/src/ast-bro/client.ts +18 -2
- package/src/ast-bro/result.ts +5 -12
- package/src/cli.ts +70 -95
- package/src/config-cli.ts +54 -0
- package/src/config.ts +886 -0
- package/src/helpers/aider.ts +252 -0
- package/src/helpers/ast-bro.ts +64 -0
- package/src/helpers/config.ts +149 -0
- package/src/helpers/hook.ts +60 -0
- package/src/helpers/installer.ts +249 -0
- package/src/helpers/json.ts +19 -0
- package/src/helpers/mcp-schema.ts +31 -0
- package/src/helpers/process.ts +31 -0
- package/src/helpers/string.ts +97 -0
- package/src/hook.ts +135 -40
- package/src/http-entry.ts +1 -1
- package/src/http.ts +118 -103
- package/src/installer-transport.ts +190 -0
- package/src/installer.ts +579 -242
- package/src/mcp-cli.ts +104 -0
- package/src/patch/aider.ts +6 -199
- package/src/patch/engine.ts +271 -197
- package/src/runtime/dependencies.ts +48 -29
- package/src/runtime/file-delete.ts +13 -9
- package/src/runtime/file-read.ts +91 -57
- package/src/runtime/file-rename.ts +158 -0
- package/src/runtime/format.ts +57 -34
- package/src/runtime/paths.ts +30 -16
- package/src/runtime/policy.ts +21 -0
- package/src/runtime/process-input.ts +14 -4
- package/src/service.ts +219 -0
- package/src/shell-policy.ts +331 -0
- package/src/stdio.ts +47 -35
- package/src/tools/ast-bro.ts +12 -6
- package/src/tools/configured.ts +28 -0
- package/src/tools/files.ts +24 -27
- package/src/tools/index.ts +5 -3
- package/src/tools/lifecycle.ts +69 -55
- package/templates/skills/ast-mcp/SKILL.md +7 -6
- package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
- package/templates/skills/ast-mcp/evals/evals.json +1 -0
- package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
- package/templates/skills/ast-mcp/evals/score.ts +304 -267
- package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
- package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
- package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
- package/templates/skills/ast-mcp/evals/source.ts +164 -0
- package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
- package/templates/skills/ast-mcp/references/installation.md +9 -8
- package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
- package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
- package/templates/skills/ast-mcp/scripts/check-install.ts +312 -110
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import { configRequestPaths, withConfig } from "../config";
|
|
3
|
+
|
|
4
|
+
export type ConfiguredExecution = <T>(
|
|
5
|
+
args: unknown,
|
|
6
|
+
operation: () => Promise<T>,
|
|
7
|
+
) => Promise<T>;
|
|
8
|
+
|
|
9
|
+
export const localExecution: ConfiguredExecution = (args, operation) =>
|
|
10
|
+
withConfig({ requestPaths: configRequestPaths(args) }, operation);
|
|
11
|
+
|
|
12
|
+
export function configuredExecution(server: McpServer): ConfiguredExecution {
|
|
13
|
+
return async (args, operation) => {
|
|
14
|
+
const capabilities = server.server.getClientCapabilities();
|
|
15
|
+
const clientRoots = capabilities?.roots
|
|
16
|
+
? await server.server
|
|
17
|
+
.listRoots()
|
|
18
|
+
.then(({ roots }) => roots.map((root) => root.uri))
|
|
19
|
+
: [];
|
|
20
|
+
return withConfig(
|
|
21
|
+
{
|
|
22
|
+
clientRoots,
|
|
23
|
+
requestPaths: configRequestPaths(args),
|
|
24
|
+
},
|
|
25
|
+
operation,
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/tools/files.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
import * as z from "zod/v4";
|
|
3
|
+
import { chattrSchema, toolFailure } from "../helpers/mcp-schema";
|
|
3
4
|
import { patchFiles, writeFilesSafely } from "../patch/engine";
|
|
4
|
-
|
|
5
5
|
import {
|
|
6
6
|
FILE_READ_MAX_BATCH,
|
|
7
7
|
FILE_READ_MAX_BYTES,
|
|
@@ -9,17 +9,13 @@ import {
|
|
|
9
9
|
hashFilesSafely,
|
|
10
10
|
readFilesSafely,
|
|
11
11
|
} from "../runtime/file-read";
|
|
12
|
+
import { type ConfiguredExecution, localExecution } from "./configured";
|
|
12
13
|
|
|
13
|
-
const failure =
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
},
|
|
19
|
-
],
|
|
20
|
-
isError: true,
|
|
21
|
-
});
|
|
22
|
-
export default function registerFileTools(server: McpServer) {
|
|
14
|
+
const failure = toolFailure;
|
|
15
|
+
export default function registerFileTools(
|
|
16
|
+
server: McpServer,
|
|
17
|
+
execute: ConfiguredExecution = localExecution,
|
|
18
|
+
) {
|
|
23
19
|
const lineRange = z
|
|
24
20
|
.tuple([z.number().int().nonnegative(), z.number().int().positive()])
|
|
25
21
|
.describe("Zero-based, end-exclusive [start, end] line range");
|
|
@@ -37,15 +33,7 @@ export default function registerFileTools(server: McpServer) {
|
|
|
37
33
|
fix: z.string(),
|
|
38
34
|
pattern: z.string().min(1),
|
|
39
35
|
});
|
|
40
|
-
const chattr =
|
|
41
|
-
chmod: z.number().int().min(0).max(0o777).optional(),
|
|
42
|
-
chown: z
|
|
43
|
-
.object({
|
|
44
|
-
gid: z.number().int().nonnegative(),
|
|
45
|
-
uid: z.number().int().nonnegative(),
|
|
46
|
-
})
|
|
47
|
-
.optional(),
|
|
48
|
-
});
|
|
36
|
+
const chattr = chattrSchema;
|
|
49
37
|
const writeTarget = z.object({
|
|
50
38
|
chattr: chattr.optional(),
|
|
51
39
|
content: z.string(),
|
|
@@ -75,7 +63,9 @@ export default function registerFileTools(server: McpServer) {
|
|
|
75
63
|
return {
|
|
76
64
|
content: [
|
|
77
65
|
{
|
|
78
|
-
text: JSON.stringify({
|
|
66
|
+
text: JSON.stringify({
|
|
67
|
+
files: await execute({ files }, () => readFilesSafely(files)),
|
|
68
|
+
}),
|
|
79
69
|
type: "text",
|
|
80
70
|
},
|
|
81
71
|
],
|
|
@@ -101,7 +91,9 @@ export default function registerFileTools(server: McpServer) {
|
|
|
101
91
|
content: [
|
|
102
92
|
{
|
|
103
93
|
text: JSON.stringify({
|
|
104
|
-
files: await
|
|
94
|
+
files: await execute({ filePaths }, () =>
|
|
95
|
+
hashFilesSafely(filePaths),
|
|
96
|
+
),
|
|
105
97
|
}),
|
|
106
98
|
type: "text",
|
|
107
99
|
},
|
|
@@ -117,7 +109,7 @@ export default function registerFileTools(server: McpServer) {
|
|
|
117
109
|
"file_write",
|
|
118
110
|
{
|
|
119
111
|
description:
|
|
120
|
-
"Creates or replaces multiple files in one keyed batch.
|
|
112
|
+
"Creates or replaces multiple files in one keyed batch. Existing files require a fresh expectedSha256 by default; safety.require_hash=false makes it optional, but any supplied hash is still verified.",
|
|
121
113
|
inputSchema: boundedBatch(
|
|
122
114
|
writeTarget,
|
|
123
115
|
"file_write requires between 1 and 50 files",
|
|
@@ -129,7 +121,9 @@ export default function registerFileTools(server: McpServer) {
|
|
|
129
121
|
return {
|
|
130
122
|
content: [
|
|
131
123
|
{
|
|
132
|
-
text: JSON.stringify(
|
|
124
|
+
text: JSON.stringify(
|
|
125
|
+
await execute(args, () => writeFilesSafely(args)),
|
|
126
|
+
),
|
|
133
127
|
type: "text",
|
|
134
128
|
},
|
|
135
129
|
],
|
|
@@ -144,13 +138,13 @@ export default function registerFileTools(server: McpServer) {
|
|
|
144
138
|
"file_patch",
|
|
145
139
|
{
|
|
146
140
|
description:
|
|
147
|
-
"Patches multiple files in one keyed batch. Each
|
|
141
|
+
"Patches multiple files in one keyed batch. Each value contains a patchStrategy, ordered aiderBlocks or astRules, and optional preview mode. A fresh expectedSha256 is required by default; safety.require_hash=false makes it optional, but supplied hashes remain enforced. Preview runs the complete formatted operation without committing.",
|
|
148
142
|
inputSchema: boundedBatch(
|
|
149
143
|
z.object({
|
|
150
144
|
aiderBlocks: z.array(aiderBlock).max(FILE_READ_MAX_BATCH).optional(),
|
|
151
145
|
astRules: z.array(astRule).max(FILE_READ_MAX_BATCH).optional(),
|
|
152
146
|
chattr: chattr.optional(),
|
|
153
|
-
expectedSha256: z.string().length(64),
|
|
147
|
+
expectedSha256: z.string().length(64).optional(),
|
|
154
148
|
patchStrategy: z.enum(["ast", "aider_block"]),
|
|
155
149
|
preview: z.boolean().optional(),
|
|
156
150
|
}),
|
|
@@ -162,7 +156,10 @@ export default function registerFileTools(server: McpServer) {
|
|
|
162
156
|
try {
|
|
163
157
|
return {
|
|
164
158
|
content: [
|
|
165
|
-
{
|
|
159
|
+
{
|
|
160
|
+
text: JSON.stringify(await execute(args, () => patchFiles(args))),
|
|
161
|
+
type: "text",
|
|
162
|
+
},
|
|
166
163
|
],
|
|
167
164
|
};
|
|
168
165
|
} catch (error) {
|
package/src/tools/index.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
|
|
3
3
|
import registerAstBroTools from "./ast-bro";
|
|
4
|
+
import { configuredExecution } from "./configured";
|
|
4
5
|
import registerFileTools from "./files";
|
|
5
6
|
import registerLifecycleTools from "./lifecycle";
|
|
6
7
|
|
|
7
8
|
export default function (server: McpServer) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const execute = configuredExecution(server);
|
|
10
|
+
registerFileTools(server, execute);
|
|
11
|
+
registerLifecycleTools(server, execute);
|
|
12
|
+
registerAstBroTools(server, execute);
|
|
11
13
|
}
|
package/src/tools/lifecycle.ts
CHANGED
|
@@ -1,61 +1,49 @@
|
|
|
1
1
|
import type { McpServer } from "@modelcontextprotocol/server";
|
|
2
2
|
import * as z from "zod/v4";
|
|
3
|
+
import {
|
|
4
|
+
boundedRecord,
|
|
5
|
+
chattrSchema,
|
|
6
|
+
toolFailure,
|
|
7
|
+
} from "../helpers/mcp-schema";
|
|
3
8
|
import { applyFileChattr, type FileChattr } from "../runtime/attributes";
|
|
4
9
|
import { deleteFilesSafely } from "../runtime/file-delete";
|
|
5
|
-
import {
|
|
10
|
+
import { renameFilesSafely } from "../runtime/file-rename";
|
|
6
11
|
import { sha256 } from "../runtime/hash";
|
|
7
12
|
import { withFileLocks } from "../runtime/locks";
|
|
8
13
|
import { resolveWritablePath } from "../runtime/paths";
|
|
14
|
+
import { type ConfiguredExecution, localExecution } from "./configured";
|
|
9
15
|
|
|
10
|
-
const chattr =
|
|
11
|
-
chmod: z.number().int().min(0).max(0o777).optional(),
|
|
12
|
-
chown: z
|
|
13
|
-
.object({
|
|
14
|
-
gid: z.number().int().nonnegative(),
|
|
15
|
-
uid: z.number().int().nonnegative(),
|
|
16
|
-
})
|
|
17
|
-
.optional(),
|
|
18
|
-
});
|
|
16
|
+
const chattr = chattrSchema;
|
|
19
17
|
|
|
20
|
-
const failure =
|
|
21
|
-
content: [
|
|
22
|
-
{
|
|
23
|
-
text: error instanceof Error ? error.message : String(error),
|
|
24
|
-
type: "text" as const,
|
|
25
|
-
},
|
|
26
|
-
],
|
|
27
|
-
isError: true,
|
|
28
|
-
});
|
|
18
|
+
const failure = toolFailure;
|
|
29
19
|
|
|
30
|
-
export default function registerLifecycleTools(
|
|
20
|
+
export default function registerLifecycleTools(
|
|
21
|
+
server: McpServer,
|
|
22
|
+
execute: ConfiguredExecution = localExecution,
|
|
23
|
+
) {
|
|
31
24
|
server.registerTool(
|
|
32
25
|
"file_chattr",
|
|
33
26
|
{
|
|
34
27
|
description:
|
|
35
28
|
"Applies the shared chattr contract to multiple root-bounded files under deterministic locks.",
|
|
36
|
-
inputSchema:
|
|
37
|
-
.
|
|
38
|
-
|
|
39
|
-
z.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
)
|
|
44
|
-
.refine(
|
|
45
|
-
(value) =>
|
|
46
|
-
Object.keys(value).length > 0 &&
|
|
47
|
-
Object.keys(value).length <= FILE_READ_MAX_BATCH,
|
|
48
|
-
"file_chattr requires between 1 and 50 files",
|
|
49
|
-
),
|
|
29
|
+
inputSchema: boundedRecord(
|
|
30
|
+
z.object({
|
|
31
|
+
chattr,
|
|
32
|
+
expectedSha256: z.string().length(64).optional(),
|
|
33
|
+
}),
|
|
34
|
+
"file_chattr requires between 1 and 50 files",
|
|
35
|
+
),
|
|
50
36
|
title: "Change File Attributes Safely",
|
|
51
37
|
},
|
|
52
38
|
async (requests) => {
|
|
53
39
|
try {
|
|
54
|
-
const entries = await
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
40
|
+
const entries = await execute(requests, () =>
|
|
41
|
+
Promise.all(
|
|
42
|
+
Object.entries(requests).map(async ([inputPath, request]) => ({
|
|
43
|
+
filePath: await resolveWritablePath(inputPath),
|
|
44
|
+
request,
|
|
45
|
+
})),
|
|
46
|
+
),
|
|
59
47
|
);
|
|
60
48
|
const files: Record<string, unknown> = {};
|
|
61
49
|
await withFileLocks(
|
|
@@ -89,21 +77,14 @@ export default function registerLifecycleTools(server: McpServer) {
|
|
|
89
77
|
"file_delete",
|
|
90
78
|
{
|
|
91
79
|
description:
|
|
92
|
-
"Deletes
|
|
93
|
-
inputSchema:
|
|
94
|
-
.
|
|
95
|
-
z.string().
|
|
96
|
-
z.
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
)
|
|
101
|
-
.refine(
|
|
102
|
-
(value) =>
|
|
103
|
-
Object.keys(value).length > 0 &&
|
|
104
|
-
Object.keys(value).length <= FILE_READ_MAX_BATCH,
|
|
105
|
-
"file_delete requires between 1 and 50 files",
|
|
106
|
-
),
|
|
80
|
+
"Deletes files in one reference-preflighted batch and removes empty ancestor directories. A fresh expectedSha256 is required by default; safety.require_hash=false makes it optional, but supplied hashes remain enforced.",
|
|
81
|
+
inputSchema: boundedRecord(
|
|
82
|
+
z.object({
|
|
83
|
+
expectedSha256: z.string().length(64).optional(),
|
|
84
|
+
forceReferences: z.boolean().optional(),
|
|
85
|
+
}),
|
|
86
|
+
"file_delete requires between 1 and 50 files",
|
|
87
|
+
),
|
|
107
88
|
title: "Delete Files Safely",
|
|
108
89
|
},
|
|
109
90
|
async (requests) => {
|
|
@@ -111,7 +92,40 @@ export default function registerLifecycleTools(server: McpServer) {
|
|
|
111
92
|
return {
|
|
112
93
|
content: [
|
|
113
94
|
{
|
|
114
|
-
text: JSON.stringify(
|
|
95
|
+
text: JSON.stringify(
|
|
96
|
+
await execute(requests, () => deleteFilesSafely(requests)),
|
|
97
|
+
),
|
|
98
|
+
type: "text",
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
} catch (error) {
|
|
103
|
+
return failure(error);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
server.registerTool(
|
|
108
|
+
"file_rename",
|
|
109
|
+
{
|
|
110
|
+
description:
|
|
111
|
+
"Renames files in one root-bounded batch without overwriting destinations. A fresh expectedSha256 is required by default; safety.require_hash=false makes it optional, but supplied hashes remain enforced.",
|
|
112
|
+
inputSchema: boundedRecord(
|
|
113
|
+
z.object({
|
|
114
|
+
destination: z.string().min(1),
|
|
115
|
+
expectedSha256: z.string().length(64).optional(),
|
|
116
|
+
}),
|
|
117
|
+
"file_rename requires between 1 and 50 files",
|
|
118
|
+
),
|
|
119
|
+
title: "Rename Files Safely",
|
|
120
|
+
},
|
|
121
|
+
async (requests) => {
|
|
122
|
+
try {
|
|
123
|
+
return {
|
|
124
|
+
content: [
|
|
125
|
+
{
|
|
126
|
+
text: JSON.stringify(
|
|
127
|
+
await execute(requests, () => renameFilesSafely(requests)),
|
|
128
|
+
),
|
|
115
129
|
type: "text",
|
|
116
130
|
},
|
|
117
131
|
],
|
|
@@ -30,15 +30,16 @@ Read [tool-catalog.md](references/tool-catalog.md) for exact arguments and combi
|
|
|
30
30
|
|
|
31
31
|
1. Inspect AST-capable targets with `map`, `show`, `context`, or a bounded `run` search. For unsupported content, use one batched `file_read({ files: [...] })` call with narrow line ranges.
|
|
32
32
|
2. Preview every AST rule with `run({ pattern, paths: [filePath], lang?, json: true })`; inspect all matches and reject capped output before mutation. For a contract-level dry run, send the same keyed `file_patch` entry with `preview: true`; it returns the formatted bounded diff and never commits.
|
|
33
|
-
3. Call one batched `file_hash({ filePaths: [...] })` immediately before guarded edits. Keep one returned hash per keyed path.
|
|
33
|
+
3. Call one batched `file_hash({ filePaths: [...] })` immediately before guarded edits. Keep one returned hash per keyed path. The runtime can make hashes optional through `safety.require_hash = false`, but the normal agent workflow should still supply them whenever current-state protection matters.
|
|
34
34
|
4. Use one path-keyed `file_patch` batch for one or more files:
|
|
35
35
|
- AST targets: `{ expectedSha256, patchStrategy: "ast", astRules: [{ pattern, fix, expectedMatches: 1 }], preview?: boolean }`
|
|
36
36
|
- Unsupported targets: `{ expectedSha256, patchStrategy: "aider_block", aiderBlocks: [{ search, replace }], preview?: boolean }`
|
|
37
37
|
Operations in each array run in order under one file lock; preview returns without committing and normal mode commits atomically.
|
|
38
38
|
5. Use one path-keyed `file_write` batch for new files or SHA-guarded replacement of existing non-structurally-rewritable files. Supply the shared `chattr` object when chmod/chown metadata is required; missing parents are created only after a guarded `ENOENT` write and path revalidation.
|
|
39
|
-
6. Use `
|
|
40
|
-
7. Use
|
|
41
|
-
8.
|
|
39
|
+
6. Use one path-keyed `file_rename` batch for hash-guarded, root-bounded file moves; validate every source and destination, reject existing destinations, and use no-replace moves. Ordinary failures roll back completed entries when possible; the operation is not crash-atomic.
|
|
40
|
+
7. Use `file_chattr` for metadata-only changes. Use hash-guarded `file_delete` for deletion; it preflights all targets and AST import references before any deletion, requires an explicit `forceReferences` override for referenced source, and removes empty ancestor directories after lock release.
|
|
41
|
+
8. Use direct `run` with `rewrite` and `write: true` only for an intentionally bounded lower-level rewrite; it is not the normal agent patch route and still rewrites only the first match per file.
|
|
42
|
+
9. Verify keyed results with `show`, `map`, `run`, or bounded `file_read` slices, then run repository validation.
|
|
42
43
|
|
|
43
44
|
Example batched AST patch:
|
|
44
45
|
|
|
@@ -86,11 +87,11 @@ Read [patch-state-machine.md](references/patch-state-machine.md) for routing and
|
|
|
86
87
|
- Zero or excess matches: narrow each AST rule; every astRules item must match exactly one node because ast-bro rewrites the first match per file, while ordered arrays let one keyed file_patch apply multiple reviewed operations.
|
|
87
88
|
- Capped direct preview: narrow paths, glob, or pattern before writing.
|
|
88
89
|
- Aider ambiguity: request a larger bounded slice and expand the search block with unique surrounding context.
|
|
89
|
-
-
|
|
90
|
+
- Formatter preflight failure: inspect the active `[formatting]` policy. Do not write until the matching external formatter or dprint fallback succeeds; `enabled = false` deliberately skips formatting.
|
|
90
91
|
- MCP loss mid-task: stop writing, restore the server, then re-inspect touched and pending files through their correct AST or non-AST routes.
|
|
91
92
|
|
|
92
93
|
## Completion check
|
|
93
94
|
|
|
94
|
-
Confirm the old structure is absent through AST search, callers are correct, touched files were verified through their proper route,
|
|
95
|
+
Confirm the old structure is absent through AST search, callers are correct, touched files were verified through their proper route, configured formatter-backed writes succeeded (or formatting is explicitly disabled), repository validation passes, and no direct-write or whole-file-read bypass was used.
|
|
95
96
|
|
|
96
97
|
Use `evals:check` for fixture-matrix integrity. `evals:measure` inventories direct MCP transcript records and statically visible `tools.mcp__ast_mcp__*` calls inside top-level Codex `exec` source. Put exactly one `ast-mcp-eval:<id>` marker in the user evaluation prompt, or in a direct record input supplied by an evaluation harness, to associate the following direct MCP records with that case. An outer `exec` result cannot prove each nested call, so marked nested calls and multi-marker evidence fail closed. Scoring validates schema-relevant inputs, transcript workspace roots, required order, expected output evidence, assertions, and successful per-call outputs. Use `evals:score --strict` only for a complete marked matrix; transcript scoring is matched task-execution evidence, not a blanket agent-quality claim.
|
|
@@ -270,5 +270,53 @@
|
|
|
270
270
|
"required_tools": ["file_delete"],
|
|
271
271
|
"surface": "file_delete",
|
|
272
272
|
"variant": "directory"
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
"assertions": [
|
|
276
|
+
"Uses one path-keyed file_rename batch.",
|
|
277
|
+
"Obtains a fresh source hash before renaming.",
|
|
278
|
+
"Reports each source and destination result."
|
|
279
|
+
],
|
|
280
|
+
"category": "positive",
|
|
281
|
+
"expected_output": "Uses the guarded file_rename contract for multiple files.",
|
|
282
|
+
"files": [],
|
|
283
|
+
"forbidden_tools": [],
|
|
284
|
+
"id": 91,
|
|
285
|
+
"prompt": "Rename two hash-guarded files in one file_rename operation.",
|
|
286
|
+
"required_tools": ["file_rename"],
|
|
287
|
+
"surface": "file_rename",
|
|
288
|
+
"variant": "batch"
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
"assertions": [
|
|
292
|
+
"Preserves source hash and root-boundary safeguards.",
|
|
293
|
+
"Does not overwrite an existing destination.",
|
|
294
|
+
"Reports the per-file operation result."
|
|
295
|
+
],
|
|
296
|
+
"category": "variant",
|
|
297
|
+
"expected_output": "Uses the guarded file_rename contract and rejects an existing destination.",
|
|
298
|
+
"files": [],
|
|
299
|
+
"forbidden_tools": [],
|
|
300
|
+
"id": 92,
|
|
301
|
+
"prompt": "Reject a file_rename request whose destination already exists.",
|
|
302
|
+
"required_tools": ["file_rename"],
|
|
303
|
+
"surface": "file_rename",
|
|
304
|
+
"variant": "existing-destination"
|
|
305
|
+
},
|
|
306
|
+
{
|
|
307
|
+
"assertions": [
|
|
308
|
+
"Does not use shell mv or a direct editor for renaming.",
|
|
309
|
+
"Uses the registered file_rename MCP surface.",
|
|
310
|
+
"Preserves the root and hash safeguards."
|
|
311
|
+
],
|
|
312
|
+
"category": "negative",
|
|
313
|
+
"expected_output": "Uses file_rename and does not call shell mutation or direct editor tools.",
|
|
314
|
+
"files": [],
|
|
315
|
+
"forbidden_tools": ["file_write", "file_delete"],
|
|
316
|
+
"id": 93,
|
|
317
|
+
"prompt": "Move a source file to a new name using the filesystem shell.",
|
|
318
|
+
"required_tools": ["file_rename"],
|
|
319
|
+
"surface": "file_rename",
|
|
320
|
+
"variant": "shell-near-miss"
|
|
273
321
|
}
|
|
274
322
|
]
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { afterEach, expect, test } from "bun:test";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { scoreTranscript } from "./score";
|
|
6
|
+
|
|
7
|
+
const directories: string[] = [];
|
|
8
|
+
|
|
9
|
+
afterEach(async () => {
|
|
10
|
+
await Promise.all(
|
|
11
|
+
directories
|
|
12
|
+
.splice(0)
|
|
13
|
+
.map((directory) => rm(directory, { force: true, recursive: true })),
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
async function transcript(lines: string[]) {
|
|
18
|
+
const directory = await mkdtemp(join(tmpdir(), "ast-mcp-score-"));
|
|
19
|
+
directories.push(directory);
|
|
20
|
+
const sessionPath = join(directory, "session.jsonl");
|
|
21
|
+
await writeFile(sessionPath, lines.join("\n"));
|
|
22
|
+
return { directory, sessionPath };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function record(type: string, payload: Record<string, unknown>) {
|
|
26
|
+
return JSON.stringify({ payload, type });
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
test("reports malformed and unmatched transcript records", async () => {
|
|
30
|
+
const { sessionPath } = await transcript([
|
|
31
|
+
"not-json",
|
|
32
|
+
record("response_item", {
|
|
33
|
+
call_id: "orphan",
|
|
34
|
+
output: "unmatched",
|
|
35
|
+
type: "custom_tool_call_output",
|
|
36
|
+
}),
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
const score = await scoreTranscript(sessionPath, true);
|
|
40
|
+
expect(score.passed).toBe(false);
|
|
41
|
+
expect(score.errors).toContain("line 1: invalid JSON");
|
|
42
|
+
expect(score.errors).toContain("unmatched output ID: orphan");
|
|
43
|
+
expect(score.errors).toContain(
|
|
44
|
+
"strict scoring requires at least one ast-mcp-eval marker",
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("binds direct calls to their marker and counts tool output", async () => {
|
|
49
|
+
const { sessionPath } = await transcript([
|
|
50
|
+
record("turn_context", {
|
|
51
|
+
cwd: process.cwd(),
|
|
52
|
+
workspace_roots: [process.cwd()],
|
|
53
|
+
}),
|
|
54
|
+
record("response_item", {
|
|
55
|
+
content: "ast-mcp-eval:999",
|
|
56
|
+
role: "user",
|
|
57
|
+
type: "message",
|
|
58
|
+
}),
|
|
59
|
+
record("response_item", {
|
|
60
|
+
call_id: "hash-call",
|
|
61
|
+
input: { filePaths: ["fixture.ts"] },
|
|
62
|
+
name: "mcp__ast_mcp__file_hash",
|
|
63
|
+
type: "custom_tool_call",
|
|
64
|
+
}),
|
|
65
|
+
record("response_item", {
|
|
66
|
+
call_id: "hash-call",
|
|
67
|
+
output: [{ text: "hash output", type: "input_text" }],
|
|
68
|
+
type: "custom_tool_call_output",
|
|
69
|
+
}),
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
const score = await scoreTranscript(sessionPath);
|
|
73
|
+
expect(score.execBatches).toBe(1);
|
|
74
|
+
expect(score.evaluatedCases).toBe(1);
|
|
75
|
+
expect(score.toolCalls.file_hash).toBe(1);
|
|
76
|
+
expect(score.astMcpOutputChars).toBe(11);
|
|
77
|
+
expect(score.errors).toContain("unknown eval ID: 999");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("rejects nested exec evidence and duplicate identifiers", async () => {
|
|
81
|
+
const source = `// ast-mcp-eval:91\ntext(await tools.mcp__ast_mcp__file_rename({"a.ts":{"expectedSha256":"abc","destination":"b.ts"}}));`;
|
|
82
|
+
const { sessionPath } = await transcript([
|
|
83
|
+
record("response_item", {
|
|
84
|
+
call_id: "nested",
|
|
85
|
+
input: source,
|
|
86
|
+
name: "exec",
|
|
87
|
+
type: "custom_tool_call",
|
|
88
|
+
}),
|
|
89
|
+
record("response_item", {
|
|
90
|
+
call_id: "nested",
|
|
91
|
+
input: source,
|
|
92
|
+
name: "exec",
|
|
93
|
+
type: "custom_tool_call",
|
|
94
|
+
}),
|
|
95
|
+
record("response_item", {
|
|
96
|
+
call_id: "nested",
|
|
97
|
+
output: "renamed",
|
|
98
|
+
type: "custom_tool_call_output",
|
|
99
|
+
}),
|
|
100
|
+
record("response_item", {
|
|
101
|
+
call_id: "nested",
|
|
102
|
+
output: "duplicate",
|
|
103
|
+
type: "custom_tool_call_output",
|
|
104
|
+
}),
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const score = await scoreTranscript(sessionPath);
|
|
108
|
+
expect(score.errors).toContain("duplicate call ID: nested");
|
|
109
|
+
expect(score.errors).toContain("duplicate output ID: nested");
|
|
110
|
+
expect(score.errors).toContain(
|
|
111
|
+
"eval 91 uses nested exec evidence that cannot be bound to individual MCP results",
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("evaluates successful and failed direct evidence", async () => {
|
|
116
|
+
const { sessionPath } = await transcript([
|
|
117
|
+
record("turn_context", {
|
|
118
|
+
cwd: process.cwd(),
|
|
119
|
+
workspace_roots: [process.cwd()],
|
|
120
|
+
}),
|
|
121
|
+
record("response_item", {
|
|
122
|
+
call_id: "valid-hash",
|
|
123
|
+
input: '// ast-mcp-eval:71\n{"filePaths":["src/server.ts"]}',
|
|
124
|
+
name: "mcp__ast_mcp__file_hash",
|
|
125
|
+
type: "custom_tool_call",
|
|
126
|
+
}),
|
|
127
|
+
record("response_item", {
|
|
128
|
+
call_id: "valid-hash",
|
|
129
|
+
output: [
|
|
130
|
+
{ text: '{"filePath":"src/server.ts","sha256":"abc","size":1}' },
|
|
131
|
+
],
|
|
132
|
+
type: "custom_tool_call_output",
|
|
133
|
+
}),
|
|
134
|
+
record("response_item", {
|
|
135
|
+
call_id: "failed-hash",
|
|
136
|
+
input: '// ast-mcp-eval:999\n{"filePaths":["src/server.ts"]}',
|
|
137
|
+
name: "mcp__ast_mcp__file_hash",
|
|
138
|
+
type: "custom_tool_call",
|
|
139
|
+
}),
|
|
140
|
+
record("response_item", {
|
|
141
|
+
call_id: "failed-hash",
|
|
142
|
+
isError: true,
|
|
143
|
+
output: "hash failed",
|
|
144
|
+
type: "custom_tool_call_output",
|
|
145
|
+
}),
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
const score = await scoreTranscript(sessionPath);
|
|
149
|
+
expect(score.passed).toBeFalse();
|
|
150
|
+
expect(score.evaluatedCases).toBe(2);
|
|
151
|
+
expect(score.verifiedAssertions).toBe(3);
|
|
152
|
+
expect(score.toolCalls.file_hash).toBe(2);
|
|
153
|
+
expect(score.errors).toContain("failed call ID: failed-hash");
|
|
154
|
+
expect(score.errors).toContain("unknown eval ID: 999");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test("accepts supported user message content shapes", async () => {
|
|
158
|
+
const { sessionPath } = await transcript(
|
|
159
|
+
[
|
|
160
|
+
"ast-mcp-eval:71",
|
|
161
|
+
{},
|
|
162
|
+
[null, {}, { text: null }, { text: "ast-mcp-eval:71" }],
|
|
163
|
+
].map((content) =>
|
|
164
|
+
record("response_item", {
|
|
165
|
+
content,
|
|
166
|
+
role: "user",
|
|
167
|
+
type: "message",
|
|
168
|
+
}),
|
|
169
|
+
),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
const score = await scoreTranscript(sessionPath);
|
|
173
|
+
expect(score.passed).toBeTrue();
|
|
174
|
+
expect(score.evaluatedCases).toBe(0);
|
|
175
|
+
});
|