@mwillbanks/ast-mcp 0.2.1 → 0.3.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/CONTRIBUTING.md +13 -0
- package/README.md +18 -14
- package/dist/ast-mcp.js +183 -137
- package/dist/ast-mcp.js.map +58 -42
- package/package.json +17 -11
- package/src/ast-bro/capability.ts +20 -10
- package/src/ast-bro/tools.json +22 -2
- package/src/cli.ts +10 -7
- package/src/config-cli.ts +166 -19
- package/src/config-migrate.ts +223 -0
- package/src/config-registry.ts +321 -0
- package/src/config-schema-common.ts +81 -0
- package/src/config-v2-schema.ts +97 -0
- package/src/config.ts +375 -149
- package/src/helpers/ast-bro.ts +1 -7
- package/src/helpers/config.ts +12 -3
- package/src/helpers/installer.ts +8 -0
- package/src/helpers/mcp-schema.ts +105 -4
- package/src/host-smoke.ts +182 -0
- package/src/http.ts +8 -3
- package/src/installer-transport.ts +6 -4
- package/src/installer.ts +192 -51
- package/src/patch/engine.ts +555 -279
- package/src/patch/languages.ts +9 -0
- package/src/patch/strategy/aiderBlock.ts +110 -0
- package/src/patch/strategy/ast.ts +170 -0
- package/src/patch/strategy.ts +61 -0
- package/src/runtime/approval.ts +438 -0
- package/src/runtime/dependencies.ts +1 -1
- package/src/runtime/document-inspection.ts +78 -0
- package/src/runtime/file-capabilities.ts +250 -0
- package/src/runtime/file-delete.ts +122 -20
- package/src/runtime/file-read.ts +142 -14
- package/src/runtime/file-rename.ts +7 -6
- package/src/runtime/format.ts +65 -27
- package/src/runtime/path-policy.ts +557 -0
- package/src/runtime/path-utils.ts +40 -0
- package/src/runtime/paths.ts +204 -43
- package/src/server.ts +2 -1
- package/src/tools/ast-bro.ts +197 -76
- package/src/tools/configuration.ts +450 -0
- package/src/tools/configured.ts +49 -21
- package/src/tools/files.ts +177 -73
- package/src/tools/index.ts +2 -0
- package/src/tools/lifecycle.ts +89 -45
- package/templates/AGENTS.md +15 -17
- package/templates/skills/ast-mcp/SKILL.md +50 -34
- package/templates/skills/ast-mcp/evals/batch.evals.json +9 -9
- package/templates/skills/ast-mcp/evals/evals.json +221 -9
- package/templates/skills/ast-mcp/evals/measure.ts +27 -4
- package/templates/skills/ast-mcp/evals/score.test.ts +46 -3
- package/templates/skills/ast-mcp/evals/score.ts +87 -0
- package/templates/skills/ast-mcp/evals/semantic.test.ts +209 -28
- package/templates/skills/ast-mcp/evals/semantic.ts +151 -37
- package/templates/skills/ast-mcp/references/installation.md +12 -12
- package/templates/skills/ast-mcp/references/patch-state-machine.md +15 -14
- package/templates/skills/ast-mcp/references/tool-catalog.md +16 -10
- package/templates/skills/ast-mcp/scripts/check-install.ts +8 -17
- package/src/ast-bro/rewrite-capability.ts +0 -9
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { realpath } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function pathWithin(root: string, target: string): boolean {
|
|
5
|
+
const relative = path.relative(root, target);
|
|
6
|
+
return (
|
|
7
|
+
relative === "" ||
|
|
8
|
+
(!relative.startsWith("..") && !path.isAbsolute(relative))
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function effectiveWorkspaceRoot(
|
|
13
|
+
projectRoot: string,
|
|
14
|
+
roots: string[],
|
|
15
|
+
): string {
|
|
16
|
+
const project = path.resolve(projectRoot);
|
|
17
|
+
const resolved = roots.map((root) => path.resolve(root));
|
|
18
|
+
return (
|
|
19
|
+
resolved.find((root) => pathWithin(project, root)) ??
|
|
20
|
+
resolved.find((root) => pathWithin(root, project)) ??
|
|
21
|
+
resolved[0] ??
|
|
22
|
+
project
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function canonicalizePath(targetPath: string): Promise<string> {
|
|
27
|
+
let existing = path.resolve(targetPath);
|
|
28
|
+
const missing: string[] = [];
|
|
29
|
+
while (true) {
|
|
30
|
+
try {
|
|
31
|
+
return path.join(await realpath(existing), ...missing);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
34
|
+
const parent = path.dirname(existing);
|
|
35
|
+
if (parent === existing) throw error;
|
|
36
|
+
missing.unshift(path.basename(existing));
|
|
37
|
+
existing = parent;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/runtime/paths.ts
CHANGED
|
@@ -1,29 +1,43 @@
|
|
|
1
1
|
import { lstat, realpath } from "node:fs/promises";
|
|
2
|
+
import os from "node:os";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { currentConfig } from "../config";
|
|
5
|
+
import {
|
|
6
|
+
assertPolicy,
|
|
7
|
+
evaluatePolicy,
|
|
8
|
+
type PathOperation,
|
|
9
|
+
} from "./path-policy";
|
|
10
|
+
import {
|
|
11
|
+
canonicalizePath,
|
|
12
|
+
effectiveWorkspaceRoot,
|
|
13
|
+
pathWithin,
|
|
14
|
+
} from "./path-utils";
|
|
15
|
+
|
|
16
|
+
async function realpathOrSelf(filePath: string): Promise<string> {
|
|
17
|
+
try {
|
|
18
|
+
return await realpath(filePath);
|
|
19
|
+
} catch {
|
|
20
|
+
return filePath;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
4
23
|
|
|
5
24
|
async function configuredRoots(): Promise<string[]> {
|
|
6
25
|
return (await currentConfig()).workspace.roots;
|
|
7
26
|
}
|
|
8
27
|
function within(root: string, target: string): boolean {
|
|
9
|
-
|
|
10
|
-
return (
|
|
11
|
-
relative === "" ||
|
|
12
|
-
(!relative.startsWith("..") && !path.isAbsolute(relative))
|
|
13
|
-
);
|
|
28
|
+
return pathWithin(root, target);
|
|
14
29
|
}
|
|
15
30
|
|
|
16
|
-
async function
|
|
31
|
+
async function workspaceRoots(): Promise<string[]> {
|
|
17
32
|
const config = await currentConfig();
|
|
18
33
|
const roots = await Promise.all(
|
|
19
|
-
(await configuredRoots()).map(
|
|
20
|
-
realpath(root).catch(() => root),
|
|
21
|
-
),
|
|
34
|
+
(await configuredRoots()).map(realpathOrSelf),
|
|
22
35
|
);
|
|
23
36
|
const trustedRoots = await Promise.all(
|
|
24
|
-
config.trustedRoots.map(
|
|
37
|
+
config.trustedRoots.map(realpathOrSelf),
|
|
25
38
|
);
|
|
26
39
|
if (
|
|
40
|
+
config.version === 1 &&
|
|
27
41
|
!config.safety.allowExternalRoots &&
|
|
28
42
|
roots.some(
|
|
29
43
|
(root) => !trustedRoots.some((trustedRoot) => within(trustedRoot, root)),
|
|
@@ -32,56 +46,203 @@ async function allowedRoots(): Promise<string[]> {
|
|
|
32
46
|
throw new Error(
|
|
33
47
|
"Configured workspace roots contain a root outside the trusted project boundary; set safety.allow_external_roots=true or AST_MCP_ALLOW_EXTERNAL_ROOTS=1 to opt in",
|
|
34
48
|
);
|
|
35
|
-
|
|
49
|
+
const primary = effectiveWorkspaceRoot(
|
|
50
|
+
await realpathOrSelf(config.projectRoot),
|
|
51
|
+
roots,
|
|
52
|
+
);
|
|
53
|
+
return [primary, ...roots.filter((root) => root !== primary)];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function fileOperationRoots(): Promise<string[]> {
|
|
57
|
+
const config = await currentConfig();
|
|
58
|
+
const roots = await workspaceRoots();
|
|
59
|
+
const policyRoots = (config.paths ?? []).map((rule) => rule.path);
|
|
60
|
+
const combined = [...new Set([...roots, ...policyRoots])];
|
|
61
|
+
if (!config.safety.allowTempDirectory) return combined;
|
|
62
|
+
const temporaryRoot = await realpathOrSelf(os.tmpdir());
|
|
63
|
+
return combined.includes(temporaryRoot)
|
|
64
|
+
? combined
|
|
65
|
+
: [...combined, temporaryRoot];
|
|
36
66
|
}
|
|
37
67
|
|
|
38
68
|
export async function primaryRoot(): Promise<string> {
|
|
39
|
-
return (await
|
|
69
|
+
return (await workspaceRoots())[0] as string;
|
|
40
70
|
}
|
|
41
71
|
|
|
42
|
-
|
|
43
|
-
|
|
72
|
+
async function configuredRootForPath(
|
|
73
|
+
filePath: string,
|
|
74
|
+
): Promise<string | undefined> {
|
|
75
|
+
return (await fileOperationRoots()).find((candidate) =>
|
|
44
76
|
within(candidate, filePath),
|
|
45
77
|
);
|
|
46
|
-
if (!root) throw new Error(`Path is outside AST_MCP_ROOTS: ${filePath}`);
|
|
47
|
-
return root;
|
|
48
78
|
}
|
|
49
|
-
|
|
79
|
+
|
|
80
|
+
export async function referenceRootForPath(
|
|
81
|
+
filePath: string,
|
|
82
|
+
): Promise<string | undefined> {
|
|
83
|
+
const candidates = await Promise.all(
|
|
84
|
+
(await fileOperationRoots()).map(async (candidate) =>
|
|
85
|
+
(await lstat(candidate).catch(() => undefined))?.isDirectory()
|
|
86
|
+
? candidate
|
|
87
|
+
: undefined,
|
|
88
|
+
),
|
|
89
|
+
);
|
|
90
|
+
return candidates
|
|
91
|
+
.filter((candidate): candidate is string => Boolean(candidate))
|
|
92
|
+
.filter((candidate) => within(candidate, filePath))
|
|
93
|
+
.sort((left, right) => right.length - left.length)[0];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function rootForPath(filePath: string): Promise<string> {
|
|
97
|
+
const root = await configuredRootForPath(filePath);
|
|
98
|
+
if (root) return root;
|
|
99
|
+
if ((await currentConfig()).safety.allowAnyPath)
|
|
100
|
+
return path.dirname(filePath);
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Path is outside configured file-operation roots: ${filePath}`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function assertSingleProjectRoot(
|
|
107
|
+
filePaths: string[],
|
|
108
|
+
): Promise<void> {
|
|
109
|
+
const selectedRoots = new Set(
|
|
110
|
+
await Promise.all(filePaths.map((filePath) => rootForPath(filePath))),
|
|
111
|
+
);
|
|
112
|
+
if (selectedRoots.size > 1)
|
|
113
|
+
throw Object.assign(
|
|
114
|
+
new Error(
|
|
115
|
+
"AST requests may not span project roots; issue one root-specific call per project",
|
|
116
|
+
),
|
|
117
|
+
{
|
|
118
|
+
code: "cross_root_ast_request",
|
|
119
|
+
retryable: true,
|
|
120
|
+
suggestedNextCall:
|
|
121
|
+
"Split the request by project root; independent read-only calls may run in parallel",
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function pathsShareRoot(
|
|
127
|
+
first: string,
|
|
128
|
+
second: string,
|
|
129
|
+
): Promise<boolean> {
|
|
50
130
|
const config = await currentConfig();
|
|
51
|
-
|
|
131
|
+
if (config.version === 2 || config.safety.allowAnyPath) return true;
|
|
132
|
+
return (await rootForPath(first)) === (await rootForPath(second));
|
|
133
|
+
}
|
|
134
|
+
async function canonicalCandidate(filePath: string, root: string) {
|
|
52
135
|
const absolute = path.isAbsolute(filePath)
|
|
53
136
|
? filePath
|
|
54
|
-
: path.resolve(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
137
|
+
: path.resolve(root, filePath);
|
|
138
|
+
return path.join(
|
|
139
|
+
await canonicalizePath(path.dirname(absolute)),
|
|
140
|
+
path.basename(absolute),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function assertWithinBoundary(
|
|
145
|
+
target: string,
|
|
146
|
+
roots: string[],
|
|
147
|
+
allowAnyPath: boolean,
|
|
148
|
+
message: string,
|
|
149
|
+
) {
|
|
150
|
+
if (!allowAnyPath && !roots.some((root) => within(root, target)))
|
|
151
|
+
throw new Error(message);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function enforcePolicy(
|
|
155
|
+
config: Awaited<ReturnType<typeof currentConfig>>,
|
|
156
|
+
decision: ReturnType<typeof evaluatePolicy>,
|
|
157
|
+
): void {
|
|
158
|
+
if (config.version === 2 || config.paths?.length)
|
|
159
|
+
assertPolicy(decision, config.generation);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async function resolvedSymlinkTarget(args: {
|
|
163
|
+
allowAnyPath: boolean;
|
|
164
|
+
boundary: string;
|
|
165
|
+
config: Awaited<ReturnType<typeof currentConfig>>;
|
|
166
|
+
filePath: string;
|
|
167
|
+
linkDecision: ReturnType<typeof evaluatePolicy>;
|
|
168
|
+
operation: PathOperation;
|
|
169
|
+
resolved: string;
|
|
170
|
+
roots: string[];
|
|
171
|
+
}): Promise<string> {
|
|
172
|
+
if (!args.linkDecision.symlinks)
|
|
71
173
|
throw new Error(
|
|
72
|
-
`
|
|
174
|
+
`Symbolic-link targets are not permitted by the winning path rule: ${args.filePath}`,
|
|
73
175
|
);
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
176
|
+
const target = await realpath(args.resolved);
|
|
177
|
+
assertWithinBoundary(
|
|
178
|
+
target,
|
|
179
|
+
args.roots,
|
|
180
|
+
args.allowAnyPath,
|
|
181
|
+
`Symbolic-link target is outside ${args.boundary}: ${args.filePath}`,
|
|
182
|
+
);
|
|
183
|
+
const targetDecision = evaluatePolicy(args.config, target, args.operation);
|
|
184
|
+
enforcePolicy(args.config, targetDecision);
|
|
185
|
+
if (!targetDecision.symlinks)
|
|
80
186
|
throw new Error(
|
|
81
|
-
`
|
|
187
|
+
`Resolved symbolic-link target is not permitted by the winning path rule: ${target}`,
|
|
82
188
|
);
|
|
83
189
|
return target;
|
|
84
190
|
}
|
|
191
|
+
|
|
192
|
+
async function resolvePath(
|
|
193
|
+
filePath: string,
|
|
194
|
+
roots: string[],
|
|
195
|
+
allowAnyPath: boolean,
|
|
196
|
+
boundary: string,
|
|
197
|
+
operation: PathOperation,
|
|
198
|
+
): Promise<string> {
|
|
199
|
+
const config = await currentConfig();
|
|
200
|
+
const resolved = await canonicalCandidate(filePath, roots[0] as string);
|
|
201
|
+
assertWithinBoundary(
|
|
202
|
+
resolved,
|
|
203
|
+
roots,
|
|
204
|
+
allowAnyPath,
|
|
205
|
+
`Path is outside ${boundary}: ${filePath}`,
|
|
206
|
+
);
|
|
207
|
+
const linkDecision = evaluatePolicy(config, resolved, operation);
|
|
208
|
+
enforcePolicy(config, linkDecision);
|
|
209
|
+
const metadata = await lstat(resolved).catch(() => undefined);
|
|
210
|
+
if (!metadata?.isSymbolicLink()) return resolved;
|
|
211
|
+
return resolvedSymlinkTarget({
|
|
212
|
+
allowAnyPath,
|
|
213
|
+
boundary,
|
|
214
|
+
config,
|
|
215
|
+
filePath,
|
|
216
|
+
linkDecision,
|
|
217
|
+
operation,
|
|
218
|
+
resolved,
|
|
219
|
+
roots,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export async function resolveWritablePath(
|
|
224
|
+
filePath: string,
|
|
225
|
+
operation: PathOperation = "write",
|
|
226
|
+
): Promise<string> {
|
|
227
|
+
const config = await currentConfig();
|
|
228
|
+
return resolvePath(
|
|
229
|
+
filePath,
|
|
230
|
+
await fileOperationRoots(),
|
|
231
|
+
config.safety.allowAnyPath,
|
|
232
|
+
"configured file-operation roots and paths rules",
|
|
233
|
+
operation,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export async function resolveWorkspacePath(filePath: string): Promise<string> {
|
|
238
|
+
return resolvePath(
|
|
239
|
+
filePath,
|
|
240
|
+
await fileOperationRoots(),
|
|
241
|
+
false,
|
|
242
|
+
"configured workspace and paths roots",
|
|
243
|
+
"read",
|
|
244
|
+
);
|
|
245
|
+
}
|
|
85
246
|
export async function rootsForDisplay(): Promise<string[]> {
|
|
86
247
|
return configuredRoots();
|
|
87
248
|
}
|
package/src/server.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/server";
|
|
2
|
+
import packageJson from "../package.json" with { type: "json" };
|
|
2
3
|
import tools from "./tools";
|
|
3
4
|
|
|
4
5
|
export function createServer() {
|
|
5
6
|
const server = new McpServer({
|
|
6
7
|
name: "ast-mcp",
|
|
7
|
-
version:
|
|
8
|
+
version: packageJson.version,
|
|
8
9
|
});
|
|
9
10
|
|
|
10
11
|
tools(server);
|
package/src/tools/ast-bro.ts
CHANGED
|
@@ -11,10 +11,23 @@ import {
|
|
|
11
11
|
parseAstBroJson,
|
|
12
12
|
} from "../ast-bro/result";
|
|
13
13
|
import metadata from "../ast-bro/tools.json";
|
|
14
|
+
import { currentConfig } from "../config";
|
|
15
|
+
import {
|
|
16
|
+
structuredUpstreamToolResult,
|
|
17
|
+
toolFailure,
|
|
18
|
+
toolOutputSchema,
|
|
19
|
+
} from "../helpers/mcp-schema";
|
|
14
20
|
import { replaceFileAtomically } from "../runtime/atomic";
|
|
21
|
+
import { inspectFileCapabilities } from "../runtime/file-capabilities";
|
|
15
22
|
import { assertFormattable, formatFileAtomically } from "../runtime/format";
|
|
16
23
|
import { withFileLocks } from "../runtime/locks";
|
|
17
|
-
import {
|
|
24
|
+
import { assertReadableTree } from "../runtime/path-policy";
|
|
25
|
+
import {
|
|
26
|
+
assertSingleProjectRoot,
|
|
27
|
+
primaryRoot,
|
|
28
|
+
resolveWorkspacePath,
|
|
29
|
+
resolveWritablePath,
|
|
30
|
+
} from "../runtime/paths";
|
|
18
31
|
import { type ConfiguredExecution, localExecution } from "./configured";
|
|
19
32
|
|
|
20
33
|
function upstreamSchema(
|
|
@@ -38,25 +51,164 @@ function upstreamSchema(
|
|
|
38
51
|
},
|
|
39
52
|
};
|
|
40
53
|
}
|
|
41
|
-
async function boundedPath(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
54
|
+
async function boundedPath(
|
|
55
|
+
root: string,
|
|
56
|
+
value: string,
|
|
57
|
+
operation: "read" | "write" = "read",
|
|
58
|
+
): Promise<string> {
|
|
59
|
+
const target = path.isAbsolute(value) ? value : path.resolve(root, value);
|
|
60
|
+
return operation === "write"
|
|
61
|
+
? resolveWritablePath(target, "write")
|
|
62
|
+
: resolveWorkspacePath(target);
|
|
45
63
|
}
|
|
46
64
|
|
|
47
65
|
const pathArguments = ["file", "path", "root"] as const;
|
|
48
66
|
|
|
49
|
-
async function
|
|
67
|
+
async function validateReadableScanPath(
|
|
68
|
+
root: string,
|
|
69
|
+
value: string,
|
|
70
|
+
): Promise<string> {
|
|
71
|
+
const resolved = await boundedPath(root, value);
|
|
72
|
+
const metadata = await lstat(resolved).catch(() => undefined);
|
|
73
|
+
if (metadata?.isDirectory())
|
|
74
|
+
assertReadableTree(await currentConfig(), resolved);
|
|
75
|
+
return resolved;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function resolveAstBroPaths(
|
|
50
79
|
args: Record<string, unknown>,
|
|
51
80
|
root: string,
|
|
52
|
-
): Promise<
|
|
81
|
+
): Promise<string[]> {
|
|
82
|
+
const candidates: string[] = [];
|
|
53
83
|
for (const name of pathArguments) {
|
|
54
84
|
const value = args[name];
|
|
55
|
-
if (typeof value === "string")
|
|
85
|
+
if (typeof value === "string") candidates.push(value);
|
|
86
|
+
}
|
|
87
|
+
if (Array.isArray(args.paths))
|
|
88
|
+
candidates.push(
|
|
89
|
+
...args.paths.filter(
|
|
90
|
+
(value): value is string => typeof value === "string",
|
|
91
|
+
),
|
|
92
|
+
);
|
|
93
|
+
if (candidates.length === 0) candidates.push(root);
|
|
94
|
+
return Promise.all(
|
|
95
|
+
candidates.map((value) => validateReadableScanPath(root, value)),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function validateRunPath(
|
|
100
|
+
filePath: string,
|
|
101
|
+
args: Record<string, unknown>,
|
|
102
|
+
): Promise<void> {
|
|
103
|
+
const metadata = await lstat(filePath);
|
|
104
|
+
if (!metadata.isFile()) return;
|
|
105
|
+
const language = typeof args.lang === "string" ? args.lang : undefined;
|
|
106
|
+
const capabilities = await inspectFileCapabilities(filePath, language);
|
|
107
|
+
const rewrite = typeof args.rewrite === "string";
|
|
108
|
+
const available = rewrite
|
|
109
|
+
? capabilities.effective.patch.includes("ast")
|
|
110
|
+
: capabilities.effective.read.includes("ast");
|
|
111
|
+
if (available) return;
|
|
112
|
+
throw Object.assign(
|
|
113
|
+
new Error(
|
|
114
|
+
`run ${rewrite ? "rewrite" : "search"} is unavailable for ${filePath}; inspect file_capabilities and select a supported workflow`,
|
|
115
|
+
),
|
|
116
|
+
{
|
|
117
|
+
code: "ast_capability_unavailable",
|
|
118
|
+
details: { capabilities, rewrite },
|
|
119
|
+
retryable: true,
|
|
120
|
+
suggestedNextCall: "file_capabilities",
|
|
121
|
+
},
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function validateAstBroPaths(
|
|
126
|
+
args: Record<string, unknown>,
|
|
127
|
+
root: string,
|
|
128
|
+
toolName: (typeof AST_BRO_TOOLS)[number],
|
|
129
|
+
): Promise<void> {
|
|
130
|
+
const resolvedPaths = await resolveAstBroPaths(args, root);
|
|
131
|
+
await assertSingleProjectRoot(resolvedPaths);
|
|
132
|
+
if (toolName !== "run") return;
|
|
133
|
+
await Promise.all(
|
|
134
|
+
resolvedPaths.map((filePath) => validateRunPath(filePath, args)),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function sameFiles(left: string[], right: string[]): boolean {
|
|
139
|
+
return (
|
|
140
|
+
left.length === right.length &&
|
|
141
|
+
left.every((filePath, index) => filePath === right[index])
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function snapshotAstBroFiles(
|
|
146
|
+
files: string[],
|
|
147
|
+
): Promise<Map<string, { content: string; mode: number }>> {
|
|
148
|
+
const snapshots = new Map<string, { content: string; mode: number }>();
|
|
149
|
+
for (const filePath of files) {
|
|
150
|
+
await assertFormattable(filePath);
|
|
151
|
+
snapshots.set(filePath, {
|
|
152
|
+
content: await readFile(filePath, "utf8"),
|
|
153
|
+
mode: (await lstat(filePath)).mode,
|
|
154
|
+
});
|
|
56
155
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
156
|
+
return snapshots;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function formatAstBroFiles(
|
|
160
|
+
result: Awaited<ReturnType<typeof callAstBro>>,
|
|
161
|
+
root: string,
|
|
162
|
+
snapshots: Map<string, { content: string; mode: number }>,
|
|
163
|
+
): Promise<void> {
|
|
164
|
+
const rewritten = await Promise.all(
|
|
165
|
+
astBroRewrittenFiles(result).map((file) =>
|
|
166
|
+
boundedPath(root, file, "write"),
|
|
167
|
+
),
|
|
168
|
+
);
|
|
169
|
+
try {
|
|
170
|
+
for (const filePath of rewritten) await formatFileAtomically(filePath);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
for (const filePath of rewritten) {
|
|
173
|
+
const snapshot = snapshots.get(filePath);
|
|
174
|
+
if (snapshot)
|
|
175
|
+
await replaceFileAtomically(filePath, snapshot.content, snapshot.mode);
|
|
176
|
+
}
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function executeLockedAstBroWrite(
|
|
182
|
+
args: Record<string, unknown>,
|
|
183
|
+
root: string,
|
|
184
|
+
previewArgs: Record<string, unknown>,
|
|
185
|
+
initialFiles: string[],
|
|
186
|
+
): Promise<Awaited<ReturnType<typeof callAstBro>>> {
|
|
187
|
+
const previewResult = await callAstBro("run", previewArgs, root);
|
|
188
|
+
const lockedPreview = parseAstBroJson(previewResult);
|
|
189
|
+
if (lockedPreview.capped)
|
|
190
|
+
throw new Error(
|
|
191
|
+
"ast-bro run preview was capped; narrow paths or pattern before writing",
|
|
192
|
+
);
|
|
193
|
+
const lockedFiles = await Promise.all(
|
|
194
|
+
astBroMatchFiles(previewResult).map((file) =>
|
|
195
|
+
boundedPath(root, file, "write"),
|
|
196
|
+
),
|
|
197
|
+
);
|
|
198
|
+
if (!sameFiles(initialFiles, lockedFiles))
|
|
199
|
+
throw new Error(
|
|
200
|
+
"ast-bro run targets changed while waiting for the write lock; preview again",
|
|
201
|
+
);
|
|
202
|
+
if (lockedFiles.length === 0) return previewResult;
|
|
203
|
+
|
|
204
|
+
const snapshots = await snapshotAstBroFiles(lockedFiles);
|
|
205
|
+
const result = await callAstBro(
|
|
206
|
+
"run",
|
|
207
|
+
{ ...args, json: true, paths: lockedFiles },
|
|
208
|
+
root,
|
|
209
|
+
);
|
|
210
|
+
await formatAstBroFiles(result, root, snapshots);
|
|
211
|
+
return result;
|
|
60
212
|
}
|
|
61
213
|
|
|
62
214
|
async function callAstBroWithFormatting(
|
|
@@ -85,55 +237,14 @@ async function callAstBroWithFormatting(
|
|
|
85
237
|
"ast-bro run preview was capped; narrow paths or pattern before writing",
|
|
86
238
|
);
|
|
87
239
|
const initialFiles = await Promise.all(
|
|
88
|
-
astBroMatchFiles(initialPreview).map((file) =>
|
|
240
|
+
astBroMatchFiles(initialPreview).map((file) =>
|
|
241
|
+
boundedPath(root, file, "write"),
|
|
242
|
+
),
|
|
89
243
|
);
|
|
90
244
|
|
|
91
|
-
return withFileLocks(initialFiles,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (lockedPreview.capped)
|
|
95
|
-
throw new Error(
|
|
96
|
-
"ast-bro run preview was capped; narrow paths or pattern before writing",
|
|
97
|
-
);
|
|
98
|
-
const lockedFiles = await Promise.all(
|
|
99
|
-
astBroMatchFiles(previewResult).map((file) => boundedPath(root, file)),
|
|
100
|
-
);
|
|
101
|
-
if (
|
|
102
|
-
initialFiles.length !== lockedFiles.length ||
|
|
103
|
-
initialFiles.some((filePath, index) => filePath !== lockedFiles[index])
|
|
104
|
-
)
|
|
105
|
-
throw new Error(
|
|
106
|
-
"ast-bro run targets changed while waiting for the write lock; preview again",
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const snapshots = new Map<string, { content: string; mode: number }>();
|
|
110
|
-
for (const filePath of lockedFiles) {
|
|
111
|
-
await assertFormattable(filePath);
|
|
112
|
-
snapshots.set(filePath, {
|
|
113
|
-
content: await readFile(filePath, "utf8"),
|
|
114
|
-
mode: (await lstat(filePath)).mode,
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
const result = await callAstBro("run", { ...args, json: true }, root);
|
|
118
|
-
const rewritten = await Promise.all(
|
|
119
|
-
astBroRewrittenFiles(result).map((file) => boundedPath(root, file)),
|
|
120
|
-
);
|
|
121
|
-
try {
|
|
122
|
-
for (const filePath of rewritten) await formatFileAtomically(filePath);
|
|
123
|
-
} catch (error) {
|
|
124
|
-
for (const filePath of rewritten) {
|
|
125
|
-
const snapshot = snapshots.get(filePath);
|
|
126
|
-
if (snapshot)
|
|
127
|
-
await replaceFileAtomically(
|
|
128
|
-
filePath,
|
|
129
|
-
snapshot.content,
|
|
130
|
-
snapshot.mode,
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
135
|
-
return result;
|
|
136
|
-
});
|
|
245
|
+
return withFileLocks(initialFiles, () =>
|
|
246
|
+
executeLockedAstBroWrite(args, root, previewArgs, initialFiles),
|
|
247
|
+
);
|
|
137
248
|
}
|
|
138
249
|
|
|
139
250
|
export default function registerAstBroTools(
|
|
@@ -142,35 +253,45 @@ export default function registerAstBroTools(
|
|
|
142
253
|
) {
|
|
143
254
|
for (const toolName of AST_BRO_TOOLS) {
|
|
144
255
|
const definition = metadata[toolName];
|
|
256
|
+
const writesState = ["find_related", "index", "run", "search"].includes(
|
|
257
|
+
toolName,
|
|
258
|
+
);
|
|
259
|
+
const destructive = toolName === "run";
|
|
145
260
|
server.registerTool(
|
|
146
261
|
toolName,
|
|
147
262
|
{
|
|
263
|
+
annotations: {
|
|
264
|
+
destructiveHint: destructive,
|
|
265
|
+
idempotentHint: !destructive,
|
|
266
|
+
openWorldHint: false,
|
|
267
|
+
readOnlyHint: !writesState,
|
|
268
|
+
},
|
|
148
269
|
description:
|
|
149
270
|
toolName === "run"
|
|
150
|
-
? "AST structural search and rewrite. Use for bounded inspection and previews; direct write is a lower-level escape hatch for exceptional cases. Normal agent edits belong in
|
|
271
|
+
? "AST structural search and rewrite. Use for bounded inspection and previews; direct write is a lower-level escape hatch for exceptional cases. Normal agent edits belong in file_patch's declared files batch, which supports ordered AST rules and Aider blocks."
|
|
151
272
|
: definition.description,
|
|
152
273
|
inputSchema: upstreamSchema(definition.inputSchema),
|
|
274
|
+
outputSchema: toolOutputSchema,
|
|
153
275
|
title: `ast-bro ${toolName}`,
|
|
154
276
|
},
|
|
155
|
-
async (args) => {
|
|
277
|
+
async (args, context) => {
|
|
156
278
|
try {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
279
|
+
const result = await execute(
|
|
280
|
+
args,
|
|
281
|
+
async () => {
|
|
282
|
+
const root = await primaryRoot();
|
|
283
|
+
await validateAstBroPaths(args, root, toolName);
|
|
284
|
+
return toolName === "run"
|
|
285
|
+
? await callAstBroWithFormatting(args, root)
|
|
286
|
+
: await callAstBro(toolName, args, root);
|
|
287
|
+
},
|
|
288
|
+
context,
|
|
289
|
+
toolName,
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
return structuredUpstreamToolResult(result, toolName);
|
|
164
293
|
} catch (error) {
|
|
165
|
-
return
|
|
166
|
-
content: [
|
|
167
|
-
{
|
|
168
|
-
text: error instanceof Error ? error.message : String(error),
|
|
169
|
-
type: "text",
|
|
170
|
-
},
|
|
171
|
-
],
|
|
172
|
-
isError: true,
|
|
173
|
-
};
|
|
294
|
+
return toolFailure(error);
|
|
174
295
|
}
|
|
175
296
|
},
|
|
176
297
|
);
|