@mwillbanks/ast-mcp 0.1.3 → 0.2.1

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 +59 -20
  2. package/dist/ast-mcp.js +232 -130
  3. package/dist/ast-mcp.js.map +107 -27
  4. package/package.json +2 -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 +70 -95
  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 +604 -247
  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 +48 -29
  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 +217 -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 +9 -8
  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 +323 -131
@@ -0,0 +1,322 @@
1
+ import { expect, test } from "bun:test";
2
+ import { verifyEvaluation } from "./semantic";
3
+
4
+ const evaluation = {
5
+ assertions: ["Reports each source and destination result."],
6
+ expected_output: "reports each source and destination result",
7
+ files: [],
8
+ forbidden_tools: [],
9
+ id: 91,
10
+ required_tools: ["file_rename"],
11
+ };
12
+
13
+ test("rename evaluator accepts root-relative runtime results", () => {
14
+ const errors = verifyEvaluation(
15
+ evaluation,
16
+ [
17
+ {
18
+ index: 0,
19
+ input: JSON.stringify({
20
+ "source.txt": { destination: "renamed.txt", expectedSha256: "hash" },
21
+ }),
22
+ tool: "file_rename",
23
+ },
24
+ ],
25
+ JSON.stringify({
26
+ files: {
27
+ "/tmp/ast-mcp-root/source.txt": {
28
+ destinationPath: "/tmp/ast-mcp-root/renamed.txt",
29
+ renamed: true,
30
+ },
31
+ },
32
+ }),
33
+ ["/tmp/ast-mcp-root"],
34
+ );
35
+ expect(errors).toEqual([]);
36
+ });
37
+
38
+ test("rename evaluator rejects incomplete per-file results", () => {
39
+ const errors = verifyEvaluation(
40
+ evaluation,
41
+ [
42
+ {
43
+ index: 0,
44
+ input: JSON.stringify({
45
+ "/tmp/ast-mcp-root/one.txt": {
46
+ destination: "/tmp/ast-mcp-root/one-new.txt",
47
+ expectedSha256: "hash",
48
+ },
49
+ "/tmp/ast-mcp-root/two.txt": {
50
+ destination: "/tmp/ast-mcp-root/two-new.txt",
51
+ expectedSha256: "hash",
52
+ },
53
+ }),
54
+ tool: "file_rename",
55
+ },
56
+ ],
57
+ JSON.stringify({
58
+ files: {
59
+ "/tmp/ast-mcp-root/one.txt": {
60
+ destinationPath: "/tmp/ast-mcp-root/wrong.txt",
61
+ renamed: true,
62
+ },
63
+ },
64
+ }),
65
+ ["/tmp/ast-mcp-root"],
66
+ );
67
+ expect(
68
+ errors.some((error) => error.includes("assertion not proven")),
69
+ ).toBeTrue();
70
+ });
71
+
72
+ test("rename evaluator rejects xargs and busybox wrappers", () => {
73
+ const negative = {
74
+ ...evaluation,
75
+ assertions: ["Does not use shell mv or a direct editor for renaming."],
76
+ expected_output: "Does not use shell mv",
77
+ required_tools: [],
78
+ };
79
+ for (const source of [
80
+ "xargs mv source destination",
81
+ "busybox mv source destination",
82
+ ]) {
83
+ const errors = verifyEvaluation(
84
+ negative,
85
+ [{ index: 0, input: source, tool: "shell" }],
86
+ "blocked",
87
+ ["/tmp/ast-mcp-root"],
88
+ );
89
+ expect(
90
+ errors.some((error) => error.includes("assertion not proven")),
91
+ ).toBeTrue();
92
+ }
93
+ });
94
+
95
+ test("semantic evaluator proves every supported assertion shape", () => {
96
+ const root = "/tmp/ast-mcp-root";
97
+ const invocation = (tool: string, input: string, index = 0) => ({
98
+ index,
99
+ input,
100
+ tool,
101
+ });
102
+ const verify = (
103
+ assertion: string,
104
+ invocations: Array<{ index: number; input: string; tool: string }>,
105
+ output = "verification output",
106
+ requiredTools: string[] = [],
107
+ ) => {
108
+ expect(
109
+ verifyEvaluation(
110
+ {
111
+ assertions: [assertion],
112
+ expected_output: "verification output",
113
+ files: [],
114
+ forbidden_tools: [],
115
+ id: 92,
116
+ required_tools: requiredTools,
117
+ },
118
+ invocations,
119
+ output,
120
+ [root],
121
+ ),
122
+ ).toEqual([]);
123
+ };
124
+ const patch = `{ "src/file.ts": { expectedSha256: "${"a".repeat(64)}", patchStrategy: "ast", astRules: [{ pattern: "old", fix: "next", expectedMatches: 1 }], preview: true } }`;
125
+
126
+ verify(
127
+ "Does not overwrite an existing destination.",
128
+ [
129
+ invocation(
130
+ "file_rename",
131
+ JSON.stringify({
132
+ "source.txt": {
133
+ destination: "destination.txt",
134
+ expectedSha256: "a".repeat(64),
135
+ },
136
+ }),
137
+ ),
138
+ ],
139
+ "verification output: destination already exists",
140
+ );
141
+ verify("Does not use shell mutation.", []);
142
+ expect(
143
+ verifyEvaluation(
144
+ {
145
+ assertions: ["Does not use shell."],
146
+ expected_output: "verification output",
147
+ files: [],
148
+ forbidden_tools: ["shell"],
149
+ id: 96,
150
+ required_tools: [],
151
+ },
152
+ [],
153
+ "verification output",
154
+ [root],
155
+ ),
156
+ ).toEqual([]);
157
+ verify("Avoids direct editor filesystem access.", []);
158
+ verify("Uses bounded lines.", [
159
+ invocation(
160
+ "file_read",
161
+ '{ files: [{ filePath: "src/a.ts", lines: [0, 2], maxBytes: 1024 }] }',
162
+ ),
163
+ ]);
164
+ verify("Caps every read at maxBytes and 64 KiB.", [
165
+ invocation(
166
+ "file_read",
167
+ '{ files: [{ filePath: "src/a.ts", lines: [0, 2], maxBytes: 65536 }] }',
168
+ ),
169
+ ]);
170
+ for (const [tool, input] of [
171
+ ["file_hash", '{ filePaths: ["src/a.ts", "src/b.ts"] }'],
172
+ [
173
+ "file_read",
174
+ '{ files: [{ filePath: "src/a.ts" }, { filePath: "src/b.ts" }] }',
175
+ ],
176
+ [
177
+ "file_write",
178
+ JSON.stringify({
179
+ "src/a.ts": { content: "a" },
180
+ "src/b.ts": { content: "b" },
181
+ }),
182
+ ],
183
+ [
184
+ "file_chattr",
185
+ JSON.stringify({
186
+ "src/a.ts": { chattr: { chmod: 384 } },
187
+ "src/b.ts": { chattr: { chmod: 384 } },
188
+ }),
189
+ ],
190
+ [
191
+ "file_delete",
192
+ JSON.stringify({
193
+ "src/a.ts": { expectedSha256: "a".repeat(64) },
194
+ "src/b.ts": { expectedSha256: "b".repeat(64) },
195
+ }),
196
+ ],
197
+ [
198
+ "file_rename",
199
+ JSON.stringify({
200
+ "src/a.ts": {
201
+ destination: "src/a-new.ts",
202
+ expectedSha256: "a".repeat(64),
203
+ },
204
+ "src/b.ts": {
205
+ destination: "src/b-new.ts",
206
+ expectedSha256: "b".repeat(64),
207
+ },
208
+ }),
209
+ ],
210
+ ] as const)
211
+ verify("Uses one keyed batch.", [invocation(tool, input)]);
212
+
213
+ verify("Uses a fresh hash before mutation.", [
214
+ invocation("file_hash", '{ filePaths: ["src/a.ts"] }', 0),
215
+ invocation(
216
+ "file_write",
217
+ JSON.stringify({ "src/a.ts": { content: "next" } }),
218
+ 1,
219
+ ),
220
+ ]);
221
+ verify("Runs file_patch with preview true.", [
222
+ invocation("file_patch", patch),
223
+ ]);
224
+ verify("Performs a preview.", [invocation("file_patch", patch)]);
225
+ verify("Performs a dry run preview.", [
226
+ invocation("run", '{ pattern: "old", write: false }'),
227
+ ]);
228
+ verify("Requires exactly one match.", [invocation("file_patch", patch)]);
229
+ verify("Uses patchStrategy ast.", [invocation("file_patch", patch)]);
230
+ verify("Uses an Aider block.", [
231
+ invocation(
232
+ "file_patch",
233
+ `{ "README.md": { expectedSha256: "${"a".repeat(64)}", patchStrategy: "aider_block", aiderBlocks: [{ search: "old", replace: "new" }] } }`,
234
+ ),
235
+ ]);
236
+ verify("Reports verification output.", []);
237
+ verify(
238
+ "Uses the required map tool.",
239
+ [
240
+ invocation(
241
+ "map",
242
+ '/* block */ { paths: ["src"], note: "/* literal */" } // line',
243
+ ),
244
+ ],
245
+ "verification output",
246
+ ["map"],
247
+ );
248
+ });
249
+
250
+ test("semantic evaluator reports every validation failure class", () => {
251
+ const errors = verifyEvaluation(
252
+ {
253
+ assertions: ["Uses bounded lines."],
254
+ expected_output: "expected transport behavior",
255
+ files: ["required.txt"],
256
+ forbidden_tools: ["shell"],
257
+ id: 93,
258
+ required_tools: ["file_hash", "file_write"],
259
+ },
260
+ [
261
+ {
262
+ concurrent: true,
263
+ index: 0,
264
+ input: '{ "/etc/out.txt": { content: "x" } }',
265
+ tool: "file_write",
266
+ },
267
+ { index: 1, input: "rm required.txt", tool: "shell" },
268
+ { index: 2, input: "{}", tool: "file_hash" },
269
+ ],
270
+ "",
271
+ ["/tmp/ast-mcp-root"],
272
+ );
273
+ for (const message of [
274
+ "used forbidden tool: shell",
275
+ "file_hash has empty or schema-incomplete input",
276
+ "required tool sequence is concurrent and cannot be proven",
277
+ "required tool sequence is not satisfied at: file_write",
278
+ "file_write input escapes transcript workspace roots: /etc/out.txt",
279
+ "output does not prove file result: required.txt",
280
+ "expected output not proven: expected transport behavior",
281
+ "assertion not proven: Uses bounded lines.",
282
+ ])
283
+ expect(errors).toContain(message);
284
+ const noBatch = verifyEvaluation(
285
+ {
286
+ assertions: ["Uses one keyed batch."],
287
+ expected_output: "verification output",
288
+ files: [],
289
+ forbidden_tools: [],
290
+ id: 94,
291
+ required_tools: ["map"],
292
+ },
293
+ [{ index: 0, input: '{ paths: ["src"] }', tool: "map" }],
294
+ "verification output",
295
+ ["/tmp/ast-mcp-root"],
296
+ );
297
+ expect(noBatch).toContain("assertion not proven: Uses one keyed batch.");
298
+
299
+ const invalidRename = verifyEvaluation(
300
+ {
301
+ assertions: ["Reports each source and destination result."],
302
+ expected_output: "verification output",
303
+ files: [],
304
+ forbidden_tools: [],
305
+ id: 95,
306
+ required_tools: ["file_rename"],
307
+ },
308
+ [
309
+ {
310
+ index: 0,
311
+ input:
312
+ '{ "source.txt": { destination: "destination.txt", expectedSha256: "hash" } trailing',
313
+ tool: "file_rename",
314
+ },
315
+ ],
316
+ "verification output { files: {} }",
317
+ ["/tmp/ast-mcp-root"],
318
+ );
319
+ expect(invalidRename).toContain(
320
+ "assertion not proven: Reports each source and destination result.",
321
+ );
322
+ });