@mwillbanks/ast-mcp 0.2.2 → 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 -16
- 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 +346 -148
- package/src/helpers/ast-bro.ts +1 -7
- package/src/helpers/config.ts +7 -0
- package/src/helpers/installer.ts +8 -0
- package/src/helpers/mcp-schema.ts +98 -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 +110 -37
- package/src/runtime/file-read.ts +142 -14
- package/src/runtime/file-rename.ts +5 -2
- 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 +142 -44
- package/src/server.ts +2 -1
- package/src/tools/ast-bro.ts +196 -75
- package/src/tools/configuration.ts +450 -0
- package/src/tools/configured.ts +49 -21
- package/src/tools/files.ts +168 -61
- package/src/tools/index.ts +2 -0
- package/src/tools/lifecycle.ts +82 -38
- package/templates/AGENTS.md +13 -15
- package/templates/skills/ast-mcp/SKILL.md +28 -20
- package/templates/skills/ast-mcp/evals/evals.json +221 -9
- package/templates/skills/ast-mcp/evals/score.ts +80 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +3 -1
- 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 +13 -7
- 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
|
@@ -2,29 +2,42 @@ import { lstat, realpath } from "node:fs/promises";
|
|
|
2
2
|
import os from "node:os";
|
|
3
3
|
import path from "node:path";
|
|
4
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
|
+
}
|
|
5
23
|
|
|
6
24
|
async function configuredRoots(): Promise<string[]> {
|
|
7
25
|
return (await currentConfig()).workspace.roots;
|
|
8
26
|
}
|
|
9
27
|
function within(root: string, target: string): boolean {
|
|
10
|
-
|
|
11
|
-
return (
|
|
12
|
-
relative === "" ||
|
|
13
|
-
(!relative.startsWith("..") && !path.isAbsolute(relative))
|
|
14
|
-
);
|
|
28
|
+
return pathWithin(root, target);
|
|
15
29
|
}
|
|
16
30
|
|
|
17
31
|
async function workspaceRoots(): Promise<string[]> {
|
|
18
32
|
const config = await currentConfig();
|
|
19
33
|
const roots = await Promise.all(
|
|
20
|
-
(await configuredRoots()).map(
|
|
21
|
-
realpath(root).catch(() => root),
|
|
22
|
-
),
|
|
34
|
+
(await configuredRoots()).map(realpathOrSelf),
|
|
23
35
|
);
|
|
24
36
|
const trustedRoots = await Promise.all(
|
|
25
|
-
config.trustedRoots.map(
|
|
37
|
+
config.trustedRoots.map(realpathOrSelf),
|
|
26
38
|
);
|
|
27
39
|
if (
|
|
40
|
+
config.version === 1 &&
|
|
28
41
|
!config.safety.allowExternalRoots &&
|
|
29
42
|
roots.some(
|
|
30
43
|
(root) => !trustedRoots.some((trustedRoot) => within(trustedRoot, root)),
|
|
@@ -33,21 +46,30 @@ async function workspaceRoots(): Promise<string[]> {
|
|
|
33
46
|
throw new Error(
|
|
34
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",
|
|
35
48
|
);
|
|
36
|
-
|
|
49
|
+
const primary = effectiveWorkspaceRoot(
|
|
50
|
+
await realpathOrSelf(config.projectRoot),
|
|
51
|
+
roots,
|
|
52
|
+
);
|
|
53
|
+
return [primary, ...roots.filter((root) => root !== primary)];
|
|
37
54
|
}
|
|
38
55
|
|
|
39
56
|
async function fileOperationRoots(): Promise<string[]> {
|
|
57
|
+
const config = await currentConfig();
|
|
40
58
|
const roots = await workspaceRoots();
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
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];
|
|
44
66
|
}
|
|
45
67
|
|
|
46
68
|
export async function primaryRoot(): Promise<string> {
|
|
47
69
|
return (await workspaceRoots())[0] as string;
|
|
48
70
|
}
|
|
49
71
|
|
|
50
|
-
|
|
72
|
+
async function configuredRootForPath(
|
|
51
73
|
filePath: string,
|
|
52
74
|
): Promise<string | undefined> {
|
|
53
75
|
return (await fileOperationRoots()).find((candidate) =>
|
|
@@ -55,6 +77,22 @@ export async function configuredRootForPath(
|
|
|
55
77
|
);
|
|
56
78
|
}
|
|
57
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
|
+
|
|
58
96
|
export async function rootForPath(filePath: string): Promise<string> {
|
|
59
97
|
const root = await configuredRootForPath(filePath);
|
|
60
98
|
if (root) return root;
|
|
@@ -65,34 +103,42 @@ export async function rootForPath(filePath: string): Promise<string> {
|
|
|
65
103
|
);
|
|
66
104
|
}
|
|
67
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
|
+
|
|
68
126
|
export async function pathsShareRoot(
|
|
69
127
|
first: string,
|
|
70
128
|
second: string,
|
|
71
129
|
): Promise<boolean> {
|
|
72
|
-
|
|
130
|
+
const config = await currentConfig();
|
|
131
|
+
if (config.version === 2 || config.safety.allowAnyPath) return true;
|
|
73
132
|
return (await rootForPath(first)) === (await rootForPath(second));
|
|
74
133
|
}
|
|
75
134
|
async function canonicalCandidate(filePath: string, root: string) {
|
|
76
135
|
const absolute = path.isAbsolute(filePath)
|
|
77
136
|
? filePath
|
|
78
137
|
: path.resolve(root, filePath);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return path.join(
|
|
84
|
-
await realpath(existing),
|
|
85
|
-
...missing,
|
|
86
|
-
path.basename(absolute),
|
|
87
|
-
);
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
90
|
-
const parent = path.dirname(existing);
|
|
91
|
-
if (parent === existing) throw error;
|
|
92
|
-
missing.unshift(path.basename(existing));
|
|
93
|
-
existing = parent;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
138
|
+
return path.join(
|
|
139
|
+
await canonicalizePath(path.dirname(absolute)),
|
|
140
|
+
path.basename(absolute),
|
|
141
|
+
);
|
|
96
142
|
}
|
|
97
143
|
|
|
98
144
|
function assertWithinBoundary(
|
|
@@ -105,12 +151,52 @@ function assertWithinBoundary(
|
|
|
105
151
|
throw new Error(message);
|
|
106
152
|
}
|
|
107
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)
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Symbolic-link targets are not permitted by the winning path rule: ${args.filePath}`,
|
|
175
|
+
);
|
|
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)
|
|
186
|
+
throw new Error(
|
|
187
|
+
`Resolved symbolic-link target is not permitted by the winning path rule: ${target}`,
|
|
188
|
+
);
|
|
189
|
+
return target;
|
|
190
|
+
}
|
|
191
|
+
|
|
108
192
|
async function resolvePath(
|
|
109
193
|
filePath: string,
|
|
110
194
|
roots: string[],
|
|
111
195
|
allowAnyPath: boolean,
|
|
112
196
|
boundary: string,
|
|
197
|
+
operation: PathOperation,
|
|
113
198
|
): Promise<string> {
|
|
199
|
+
const config = await currentConfig();
|
|
114
200
|
const resolved = await canonicalCandidate(filePath, roots[0] as string);
|
|
115
201
|
assertWithinBoundary(
|
|
116
202
|
resolved,
|
|
@@ -118,32 +204,44 @@ async function resolvePath(
|
|
|
118
204
|
allowAnyPath,
|
|
119
205
|
`Path is outside ${boundary}: ${filePath}`,
|
|
120
206
|
);
|
|
207
|
+
const linkDecision = evaluatePolicy(config, resolved, operation);
|
|
208
|
+
enforcePolicy(config, linkDecision);
|
|
121
209
|
const metadata = await lstat(resolved).catch(() => undefined);
|
|
122
210
|
if (!metadata?.isSymbolicLink()) return resolved;
|
|
123
|
-
|
|
124
|
-
throw new Error(`Symbolic-link targets are not permitted: ${filePath}`);
|
|
125
|
-
const target = await realpath(resolved);
|
|
126
|
-
assertWithinBoundary(
|
|
127
|
-
target,
|
|
128
|
-
roots,
|
|
211
|
+
return resolvedSymlinkTarget({
|
|
129
212
|
allowAnyPath,
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
213
|
+
boundary,
|
|
214
|
+
config,
|
|
215
|
+
filePath,
|
|
216
|
+
linkDecision,
|
|
217
|
+
operation,
|
|
218
|
+
resolved,
|
|
219
|
+
roots,
|
|
220
|
+
});
|
|
133
221
|
}
|
|
134
222
|
|
|
135
|
-
export async function resolveWritablePath(
|
|
223
|
+
export async function resolveWritablePath(
|
|
224
|
+
filePath: string,
|
|
225
|
+
operation: PathOperation = "write",
|
|
226
|
+
): Promise<string> {
|
|
136
227
|
const config = await currentConfig();
|
|
137
228
|
return resolvePath(
|
|
138
229
|
filePath,
|
|
139
230
|
await fileOperationRoots(),
|
|
140
231
|
config.safety.allowAnyPath,
|
|
141
|
-
"configured file-operation roots
|
|
232
|
+
"configured file-operation roots and paths rules",
|
|
233
|
+
operation,
|
|
142
234
|
);
|
|
143
235
|
}
|
|
144
236
|
|
|
145
237
|
export async function resolveWorkspacePath(filePath: string): Promise<string> {
|
|
146
|
-
return resolvePath(
|
|
238
|
+
return resolvePath(
|
|
239
|
+
filePath,
|
|
240
|
+
await fileOperationRoots(),
|
|
241
|
+
false,
|
|
242
|
+
"configured workspace and paths roots",
|
|
243
|
+
"read",
|
|
244
|
+
);
|
|
147
245
|
}
|
|
148
246
|
export async function rootsForDisplay(): Promise<string[]> {
|
|
149
247
|
return configuredRoots();
|
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
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
|
);
|