@mwillbanks/ast-mcp 0.2.2 → 0.4.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 +209 -147
- package/dist/ast-mcp.js.map +70 -50
- package/package.json +21 -15
- package/src/ast-bro/capability.ts +20 -10
- package/src/ast-bro/client.ts +12 -6
- package/src/ast-bro/result.ts +79 -0
- package/src/ast-bro/tools.json +29 -4
- package/src/cli.ts +10 -7
- package/src/config-cli.ts +166 -19
- package/src/config-edit.ts +236 -0
- package/src/config-migrate.ts +206 -0
- package/src/config-registry.ts +329 -0
- package/src/config-schema-common.ts +88 -0
- package/src/config-v2-schema.ts +109 -0
- package/src/config.ts +523 -170
- package/src/helpers/ast-bro.ts +1 -7
- package/src/helpers/config.ts +16 -3
- 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 +242 -54
- package/src/patch/engine.ts +575 -280
- 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/config-write.ts +327 -0
- package/src/runtime/dependencies.ts +14 -78
- 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 +134 -15
- package/src/runtime/file-rename.ts +5 -2
- package/src/runtime/format.ts +90 -30
- package/src/runtime/git-worktrees.ts +179 -0
- package/src/runtime/path-policy.ts +643 -0
- package/src/runtime/path-utils.ts +80 -0
- package/src/runtime/paths.ts +171 -47
- package/src/server.ts +2 -1
- package/src/tools/ast-bro.ts +294 -75
- package/src/tools/configuration.ts +668 -0
- package/src/tools/configured.ts +49 -21
- package/src/tools/files.ts +169 -62
- package/src/tools/index.ts +2 -0
- package/src/tools/lifecycle.ts +82 -38
- package/templates/AGENTS.md +17 -15
- package/templates/skills/ast-mcp/SKILL.md +57 -20
- package/templates/skills/ast-mcp/evals/evals.json +319 -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 +18 -8
- package/templates/skills/ast-mcp/scripts/binary-resolution.ts +112 -0
- package/templates/skills/ast-mcp/scripts/check-install.ts +165 -51
- package/templates/skills/ast-mcp/scripts/managed-hook.ts +34 -2
- package/src/ast-bro/rewrite-capability.ts +0 -9
package/src/runtime/format.ts
CHANGED
|
@@ -1,13 +1,30 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { lstat, readFile, unlink, writeFile } from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { currentConfig, type ResolvedConfig } from "../config";
|
|
4
5
|
import { replaceFileAtomically } from "./atomic";
|
|
5
6
|
import { configuredDprintBinary } from "./dependencies";
|
|
7
|
+
import { canonicalizePathSync, containingRoot, pathWithin } from "./path-utils";
|
|
6
8
|
import { runCommandInput } from "./process-input";
|
|
7
9
|
|
|
8
10
|
const dprint = configuredDprintBinary;
|
|
11
|
+
function formatterRoot(config: ResolvedConfig, filePath: string) {
|
|
12
|
+
const project = config.projectRoot;
|
|
13
|
+
if (
|
|
14
|
+
pathWithin(project, filePath) ||
|
|
15
|
+
pathWithin(canonicalizePathSync(project), canonicalizePathSync(filePath))
|
|
16
|
+
)
|
|
17
|
+
return project;
|
|
18
|
+
return (
|
|
19
|
+
containingRoot(
|
|
20
|
+
[...config.workspace.linkedWorktrees, ...config.workspace.roots],
|
|
21
|
+
filePath,
|
|
22
|
+
) ?? project
|
|
23
|
+
);
|
|
24
|
+
}
|
|
9
25
|
function formatterPath(config: ResolvedConfig, filePath: string) {
|
|
10
|
-
const
|
|
26
|
+
const root = formatterRoot(config, filePath);
|
|
27
|
+
const relative = path.relative(root, filePath);
|
|
11
28
|
return relative && !relative.startsWith("..") && !path.isAbsolute(relative)
|
|
12
29
|
? relative.split(path.sep).join("/")
|
|
13
30
|
: path.basename(filePath);
|
|
@@ -24,44 +41,89 @@ function matchesFormatter(
|
|
|
24
41
|
}
|
|
25
42
|
function formatterArgs(
|
|
26
43
|
config: ResolvedConfig,
|
|
27
|
-
|
|
44
|
+
sourceFile: string,
|
|
45
|
+
formatterFile: string,
|
|
28
46
|
args: string[],
|
|
29
47
|
) {
|
|
30
48
|
return args.map((argument) =>
|
|
31
49
|
argument
|
|
32
|
-
.replaceAll("{file}",
|
|
33
|
-
.replaceAll("{
|
|
50
|
+
.replaceAll("{file}", formatterFile)
|
|
51
|
+
.replaceAll("{source_file}", sourceFile)
|
|
52
|
+
.replaceAll("{project_root}", formatterRoot(config, sourceFile)),
|
|
34
53
|
);
|
|
35
54
|
}
|
|
36
55
|
|
|
56
|
+
async function removeFormatterStage(staged: string): Promise<void> {
|
|
57
|
+
try {
|
|
58
|
+
await unlink(staged);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT")
|
|
61
|
+
throw new Error(`Failed to remove formatter stage ${staged}`, {
|
|
62
|
+
cause: error,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
37
67
|
export async function formatContent(
|
|
38
68
|
filePath: string,
|
|
39
69
|
content: string,
|
|
40
70
|
): Promise<string> {
|
|
41
71
|
const config = await currentConfig();
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
72
|
+
const formatter = config.formatting.formatters.find(
|
|
73
|
+
(candidate) =>
|
|
74
|
+
(candidate.enabled ?? config.formatting.enabled) &&
|
|
75
|
+
matchesFormatter(config, filePath, candidate),
|
|
45
76
|
);
|
|
46
|
-
|
|
47
|
-
|
|
77
|
+
if (formatter) {
|
|
78
|
+
if (formatter.mode === "stdout") {
|
|
79
|
+
const result = await runCommandInput(
|
|
48
80
|
formatter.command,
|
|
49
|
-
formatterArgs(config, filePath, formatter.args),
|
|
81
|
+
formatterArgs(config, filePath, filePath, formatter.args),
|
|
50
82
|
content,
|
|
51
|
-
{
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
83
|
+
{
|
|
84
|
+
cwd: formatterRoot(config, filePath),
|
|
85
|
+
timeoutMs: formatter.timeoutMs,
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
return result.stdout;
|
|
89
|
+
}
|
|
90
|
+
const extension = path.extname(filePath);
|
|
91
|
+
const staged = path.join(
|
|
92
|
+
path.dirname(filePath),
|
|
93
|
+
`.ast-mcp-format-${randomUUID()}${extension}`,
|
|
94
|
+
);
|
|
95
|
+
try {
|
|
96
|
+
await writeFile(staged, content, { flag: "wx" });
|
|
97
|
+
await runCommandInput(
|
|
98
|
+
formatter.command,
|
|
99
|
+
formatterArgs(config, filePath, staged, formatter.args),
|
|
100
|
+
"",
|
|
101
|
+
{
|
|
102
|
+
cwd: formatterRoot(config, filePath),
|
|
103
|
+
timeoutMs: formatter.timeoutMs,
|
|
104
|
+
},
|
|
64
105
|
);
|
|
106
|
+
return await readFile(staged, "utf8");
|
|
107
|
+
} finally {
|
|
108
|
+
await removeFormatterStage(staged);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (!config.formatting.enabled || config.formatting.fallback === "preserve")
|
|
112
|
+
return content;
|
|
113
|
+
if (config.formatting.fallback === "reject")
|
|
114
|
+
throw new Error(`No enabled formatter matches ${filePath}`);
|
|
115
|
+
const result = await runCommandInput(
|
|
116
|
+
await dprint(),
|
|
117
|
+
[
|
|
118
|
+
"fmt",
|
|
119
|
+
"--config",
|
|
120
|
+
config.formatting.dprintConfig,
|
|
121
|
+
"--stdin",
|
|
122
|
+
formatterPath(config, filePath),
|
|
123
|
+
],
|
|
124
|
+
content,
|
|
125
|
+
{ cwd: formatterRoot(config, filePath) },
|
|
126
|
+
);
|
|
65
127
|
return result.stdout;
|
|
66
128
|
}
|
|
67
129
|
|
|
@@ -70,11 +132,9 @@ export async function assertFormattable(filePath: string): Promise<void> {
|
|
|
70
132
|
}
|
|
71
133
|
|
|
72
134
|
export async function formatFileAtomically(filePath: string): Promise<void> {
|
|
73
|
-
if (!(await currentConfig()).formatting.enabled) return;
|
|
74
135
|
const metadata = await lstat(filePath);
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await replaceFileAtomically(filePath, formatted, metadata.mode);
|
|
136
|
+
const source = await readFile(filePath, "utf8");
|
|
137
|
+
const formatted = await formatContent(filePath, source);
|
|
138
|
+
if (formatted !== source)
|
|
139
|
+
await replaceFileAtomically(filePath, formatted, metadata.mode);
|
|
80
140
|
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { lstat, readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
const cache = new Map<string, { fingerprint: string; paths: string[] }>();
|
|
5
|
+
|
|
6
|
+
export function clearGitWorktreeCache(): void {
|
|
7
|
+
cache.clear();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async function gitEntry(
|
|
11
|
+
start: string,
|
|
12
|
+
): Promise<{ gitPath: string; workTree: string } | undefined> {
|
|
13
|
+
let current = path.resolve(start);
|
|
14
|
+
while (true) {
|
|
15
|
+
const gitPath = path.join(current, ".git");
|
|
16
|
+
const metadata = await lstat(gitPath).catch(() => undefined);
|
|
17
|
+
if (metadata?.isDirectory() || metadata?.isFile())
|
|
18
|
+
return { gitPath, workTree: current };
|
|
19
|
+
const parent = path.dirname(current);
|
|
20
|
+
if (parent === current) return undefined;
|
|
21
|
+
current = parent;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function resolvePointer(baseDir: string, pointer: string): string {
|
|
26
|
+
return path.isAbsolute(pointer)
|
|
27
|
+
? path.resolve(pointer)
|
|
28
|
+
: path.resolve(baseDir, pointer);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function gitDirPointer(source: string, gitPath: string): string | undefined {
|
|
32
|
+
const match = /^\s*gitdir:\s*(.+?)\s*$/m.exec(source);
|
|
33
|
+
const value = match?.[1]?.trim();
|
|
34
|
+
return value ? resolvePointer(path.dirname(gitPath), value) : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function resolveGitDir(gitPath: string): Promise<string | undefined> {
|
|
38
|
+
const metadata = await lstat(gitPath).catch(() => undefined);
|
|
39
|
+
if (!metadata) return undefined;
|
|
40
|
+
if (metadata.isDirectory()) return gitPath;
|
|
41
|
+
if (!metadata.isFile()) return undefined;
|
|
42
|
+
const source = await readFile(gitPath, "utf8").catch(() => undefined);
|
|
43
|
+
return source ? gitDirPointer(source, gitPath) : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function commonGitDir(gitDir: string): Promise<string> {
|
|
47
|
+
const source = await readFile(path.join(gitDir, "commondir"), "utf8").catch(
|
|
48
|
+
() => undefined,
|
|
49
|
+
);
|
|
50
|
+
const value = source?.trim();
|
|
51
|
+
if (!value) return gitDir;
|
|
52
|
+
return path.isAbsolute(value)
|
|
53
|
+
? path.resolve(value)
|
|
54
|
+
: path.resolve(gitDir, value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function mainWorkTree(commonDir: string, discoveredWorkTree: string): string {
|
|
58
|
+
return path.basename(commonDir) === ".git"
|
|
59
|
+
? path.dirname(commonDir)
|
|
60
|
+
: discoveredWorkTree;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function gitDirBelongsTo(
|
|
64
|
+
gitDir: string,
|
|
65
|
+
gitPath: string,
|
|
66
|
+
): Promise<boolean> {
|
|
67
|
+
const metadata = await lstat(gitPath).catch(() => undefined);
|
|
68
|
+
if (metadata?.isDirectory()) return sameResolvedPath(gitDir, gitPath);
|
|
69
|
+
if (!metadata?.isFile()) return false;
|
|
70
|
+
const pointer = await readFile(path.join(gitDir, "gitdir"), "utf8").catch(
|
|
71
|
+
() => undefined,
|
|
72
|
+
);
|
|
73
|
+
if (!pointer?.trim()) return false;
|
|
74
|
+
return sameResolvedPath(resolvePointer(gitDir, pointer.trim()), gitPath);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function sameResolvedPath(left: string, right: string): Promise<boolean> {
|
|
78
|
+
const [canonicalLeft, canonicalRight] = await Promise.all([
|
|
79
|
+
realpath(left).catch(() => path.resolve(left)),
|
|
80
|
+
realpath(right).catch(() => path.resolve(right)),
|
|
81
|
+
]);
|
|
82
|
+
return canonicalLeft === canonicalRight;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function worktreeGitFile(
|
|
86
|
+
gitdirFile: string,
|
|
87
|
+
): Promise<string | undefined> {
|
|
88
|
+
const source = await readFile(gitdirFile, "utf8").catch(() => undefined);
|
|
89
|
+
const pointer = source?.trim();
|
|
90
|
+
if (!pointer) return undefined;
|
|
91
|
+
const gitFile = resolvePointer(path.dirname(gitdirFile), pointer);
|
|
92
|
+
if (path.basename(gitFile) !== ".git") return undefined;
|
|
93
|
+
const gitMetadata = await lstat(gitFile).catch(() => undefined);
|
|
94
|
+
return gitMetadata?.isFile() ? gitFile : undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function worktreeFromGitdirFile(
|
|
98
|
+
gitdirFile: string,
|
|
99
|
+
commonDir: string,
|
|
100
|
+
): Promise<string | undefined> {
|
|
101
|
+
const gitFile = await worktreeGitFile(gitdirFile);
|
|
102
|
+
if (!gitFile) return undefined;
|
|
103
|
+
const expectedGitDir = path.dirname(gitdirFile);
|
|
104
|
+
const back = gitDirPointer(
|
|
105
|
+
(await readFile(gitFile, "utf8").catch(() => undefined)) ?? "",
|
|
106
|
+
gitFile,
|
|
107
|
+
);
|
|
108
|
+
if (!back || !(await sameResolvedPath(back, expectedGitDir)))
|
|
109
|
+
return undefined;
|
|
110
|
+
if (!(await sameResolvedPath(await commonGitDir(expectedGitDir), commonDir)))
|
|
111
|
+
return undefined;
|
|
112
|
+
const metadata = await stat(path.dirname(gitFile)).catch(() => undefined);
|
|
113
|
+
return metadata?.isDirectory() ? path.dirname(gitFile) : undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function linkedFromGitdirFiles(commonDir: string): Promise<string[]> {
|
|
117
|
+
const worktreesDir = path.join(commonDir, "worktrees");
|
|
118
|
+
const entries = await readdir(worktreesDir, { withFileTypes: true }).catch(
|
|
119
|
+
() => [],
|
|
120
|
+
);
|
|
121
|
+
const paths: string[] = [];
|
|
122
|
+
for (const entry of entries) {
|
|
123
|
+
if (!entry.isDirectory()) continue;
|
|
124
|
+
const workTree = await worktreeFromGitdirFile(
|
|
125
|
+
path.join(worktreesDir, entry.name, "gitdir"),
|
|
126
|
+
commonDir,
|
|
127
|
+
);
|
|
128
|
+
if (workTree) paths.push(workTree);
|
|
129
|
+
}
|
|
130
|
+
return paths;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function cacheFingerprint(commonDir: string): Promise<string> {
|
|
134
|
+
const gitStat = await stat(commonDir).catch(() => undefined);
|
|
135
|
+
const worktreesDir = path.join(commonDir, "worktrees");
|
|
136
|
+
const worktreesStat = await stat(worktreesDir).catch(() => undefined);
|
|
137
|
+
const names = await readdir(worktreesDir).catch(() => []);
|
|
138
|
+
const gitdirStats = await Promise.all(
|
|
139
|
+
names.map(async (name) => {
|
|
140
|
+
const metadata = await stat(
|
|
141
|
+
path.join(worktreesDir, name, "gitdir"),
|
|
142
|
+
).catch(() => undefined);
|
|
143
|
+
return `${name}:${metadata?.mtimeMs ?? 0}`;
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
gitdirStats.sort();
|
|
147
|
+
return `${gitStat?.mtimeMs ?? 0}:${worktreesStat?.mtimeMs ?? 0}:${gitdirStats.join(",")}`;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function uniqueCanonical(paths: string[]): Promise<string[]> {
|
|
151
|
+
const unique: string[] = [];
|
|
152
|
+
const seen = new Set<string>();
|
|
153
|
+
for (const item of paths) {
|
|
154
|
+
const canonical = await realpath(item).catch(() => path.resolve(item));
|
|
155
|
+
if (seen.has(canonical)) continue;
|
|
156
|
+
seen.add(canonical);
|
|
157
|
+
unique.push(canonical);
|
|
158
|
+
}
|
|
159
|
+
return unique;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export async function linkedWorktrees(root: string): Promise<string[]> {
|
|
163
|
+
const entry = await gitEntry(root);
|
|
164
|
+
if (!entry) return [];
|
|
165
|
+
const gitDir = await resolveGitDir(entry.gitPath);
|
|
166
|
+
if (!gitDir || !(await gitDirBelongsTo(gitDir, entry.gitPath))) return [];
|
|
167
|
+
const commonDir = await commonGitDir(gitDir);
|
|
168
|
+
const common = await stat(commonDir).catch(() => undefined);
|
|
169
|
+
if (!common?.isDirectory()) return uniqueCanonical([entry.workTree]);
|
|
170
|
+
const fingerprint = await cacheFingerprint(commonDir);
|
|
171
|
+
const cached = cache.get(commonDir);
|
|
172
|
+
if (cached?.fingerprint === fingerprint) return cached.paths;
|
|
173
|
+
const paths = await uniqueCanonical([
|
|
174
|
+
mainWorkTree(commonDir, entry.workTree),
|
|
175
|
+
...(await linkedFromGitdirFiles(commonDir)),
|
|
176
|
+
]);
|
|
177
|
+
cache.set(commonDir, { fingerprint, paths });
|
|
178
|
+
return paths;
|
|
179
|
+
}
|