@mwillbanks/ast-mcp 0.6.5 → 0.7.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.
Files changed (55) hide show
  1. package/CONTRIBUTING.md +6 -0
  2. package/dist/ast-mcp.js +358 -337
  3. package/dist/ast-mcp.js.map +45 -32
  4. package/dist/workers/sql-postgres-worker.js +5 -0
  5. package/dist/workers/sql-postgres-worker.js.map +10 -0
  6. package/package.json +3 -2
  7. package/src/cli/doctor.ts +103 -0
  8. package/src/cli/index.ts +9 -1
  9. package/src/config/index.ts +61 -6
  10. package/src/config/registry.ts +209 -67
  11. package/src/config/v2-schema.ts +2 -0
  12. package/src/helpers/installer.ts +10 -1
  13. package/src/http/index.ts +12 -5
  14. package/src/installer/codex.ts +237 -0
  15. package/src/installer/diagnostics.ts +387 -0
  16. package/src/installer/directory-ancestry.ts +55 -0
  17. package/src/installer/index.ts +386 -119
  18. package/src/installer/ownership.ts +165 -0
  19. package/src/installer/service-state.ts +151 -0
  20. package/src/installer/service.ts +131 -7
  21. package/src/installer/transaction.ts +342 -0
  22. package/src/installer/transport.ts +22 -31
  23. package/src/intelligence/mutation/locks.ts +4 -1
  24. package/src/patch/engine.ts +157 -11
  25. package/src/runtime/approval.ts +427 -66
  26. package/src/runtime/config-write.ts +27 -1
  27. package/src/runtime/diagnostics.ts +115 -0
  28. package/src/runtime/file-capabilities.ts +2 -0
  29. package/src/runtime/hosts/codex.ts +16 -0
  30. package/src/runtime/path-policy.ts +2 -0
  31. package/src/runtime/release.ts +220 -0
  32. package/src/runtime/sql-assistance.ts +439 -0
  33. package/src/runtime/sql-options-schema.ts +31 -0
  34. package/src/runtime/sql-postgres-worker.ts +72 -0
  35. package/src/runtime/sql-postgres.ts +245 -0
  36. package/src/server/index.ts +35 -5
  37. package/src/server/lifecycle.ts +1 -1
  38. package/src/tools/configuration.ts +2 -0
  39. package/src/tools/diagnostics.ts +85 -0
  40. package/src/tools/files.ts +2 -0
  41. package/src/tools/index.ts +9 -0
  42. package/templates/skills/ast-mcp/SKILL.md +20 -1
  43. package/templates/skills/ast-mcp/evals/evals.json +274 -0
  44. package/templates/skills/ast-mcp/evals/semantic.ts +2 -0
  45. package/templates/skills/ast-mcp/evals/trigger-queries.json +4 -1
  46. package/templates/skills/ast-mcp/references/cli.md +97 -0
  47. package/templates/skills/ast-mcp/references/configuration.md +11 -3
  48. package/templates/skills/ast-mcp/references/diagnostics.md +106 -0
  49. package/templates/skills/ast-mcp/references/host-context.md +85 -0
  50. package/templates/skills/ast-mcp/references/installation.md +37 -0
  51. package/templates/skills/ast-mcp/references/issue-reporting.md +80 -0
  52. package/templates/skills/ast-mcp/references/patch-state-machine.md +10 -0
  53. package/templates/skills/ast-mcp/references/tool-catalog.md +120 -50
  54. package/templates/skills/ast-mcp/scripts/check-install.ts +223 -24
  55. package/templates/skills/ast-mcp/scripts/release.ts +220 -0
@@ -0,0 +1,342 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { randomUUID } from "node:crypto";
3
+ import {
4
+ lstatSync,
5
+ readFileSync,
6
+ realpathSync,
7
+ renameSync,
8
+ rmSync,
9
+ rmdirSync,
10
+ } from "node:fs";
11
+ import { chmod, lstat, mkdir, rm, writeFile } from "node:fs/promises";
12
+ import path from "node:path";
13
+
14
+ import {
15
+ assertConfigWriteTarget,
16
+ canonicalConfigPath,
17
+ withConfigTransaction,
18
+ } from "../config/transaction";
19
+ import {
20
+ createdDirectoryAncestors,
21
+ type DirectoryIdentity,
22
+ } from "./directory-ancestry";
23
+
24
+ type FileState = { bytes: Buffer; mode: number } | undefined;
25
+ type Entry = { before: FileState; after: FileState; configuration?: string };
26
+ type ParentState = DirectoryIdentity;
27
+ type TransactionOptions = {
28
+ rename?: typeof renameSync;
29
+ remove?: typeof rmSync;
30
+ };
31
+ const active = new AsyncLocalStorage<FileTransaction>();
32
+ function conflict(message: string) {
33
+ return Object.assign(new Error(message), {
34
+ code: "configuration_conflict",
35
+ retryable: true,
36
+ });
37
+ }
38
+ function state(file: string): FileState {
39
+ let info;
40
+ try {
41
+ info = lstatSync(file);
42
+ } catch (error) {
43
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
44
+ throw error;
45
+ }
46
+ if (!info.isFile())
47
+ throw new Error("Installer destination must be a regular file");
48
+ return { bytes: readFileSync(file), mode: info.mode & 0o777 };
49
+ }
50
+ function equal(left: FileState, right: FileState) {
51
+ return left === undefined || right === undefined
52
+ ? left === right
53
+ : left.mode === right.mode && left.bytes.equals(right.bytes);
54
+ }
55
+
56
+ class FileTransaction {
57
+ readonly entries = new Map<string, Entry>();
58
+ readonly compensations: Array<() => Promise<void>> = [];
59
+ readonly parents = new Map<string, ParentState>();
60
+ readonly directories = new Map<string, ParentState>();
61
+ constructor(readonly options: TransactionOptions) {}
62
+
63
+ guard(file: string) {
64
+ for (const [parent, before] of this.parents) {
65
+ if (!file.startsWith(parent + path.sep)) continue;
66
+ const current = lstatSync(parent);
67
+ if (
68
+ current.ino !== before.ino ||
69
+ current.dev !== before.dev ||
70
+ realpathSync(parent) !== before.real
71
+ )
72
+ throw conflict(
73
+ "Installer parent directory changed; preserving user files",
74
+ );
75
+ }
76
+ let parent = path.dirname(file);
77
+ while (!this.parents.has(parent)) {
78
+ let info;
79
+ try {
80
+ info = lstatSync(parent);
81
+ } catch (error) {
82
+ if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
83
+ }
84
+ if (info) {
85
+ this.parents.set(parent, {
86
+ real: realpathSync(parent),
87
+ ino: info.ino,
88
+ dev: info.dev,
89
+ });
90
+ break;
91
+ }
92
+ const next = path.dirname(parent);
93
+ if (next === parent) break;
94
+ parent = next;
95
+ }
96
+ }
97
+
98
+ async mkdir(directory: string) {
99
+ directory = path.resolve(directory);
100
+ this.guard(path.join(directory, ".installer-directory"));
101
+ const first = await mkdir(directory, { recursive: true });
102
+ if (first) {
103
+ const created = createdDirectoryAncestors(directory, first);
104
+ for (const [createdPath, identity] of created)
105
+ this.directories.set(createdPath, identity);
106
+ }
107
+ return first;
108
+ }
109
+
110
+ observe(file: string) {
111
+ file = path.resolve(file);
112
+ this.guard(file);
113
+ const current = state(file);
114
+ const entry = this.entries.get(file);
115
+ if (entry && !equal(current, entry.after))
116
+ throw conflict("Installer destination changed; reload and retry");
117
+ if (!entry) this.entries.set(file, { before: current, after: current });
118
+ return current;
119
+ }
120
+
121
+ async replace(
122
+ file: string,
123
+ next: FileState,
124
+ expected: FileState,
125
+ exclusive = false,
126
+ configuration?: string,
127
+ ) {
128
+ file = path.resolve(file);
129
+ this.guard(file);
130
+ const current = state(file);
131
+ const entry = this.entries.get(file);
132
+ if (!equal(current, expected) || (entry && !equal(current, entry.after)))
133
+ throw conflict(
134
+ "Installer destination changed; refusing to overwrite user edits",
135
+ );
136
+ if (exclusive && current)
137
+ throw new Error("Installer destination already exists");
138
+ if (equal(current, next)) return;
139
+ await this.commit(file, next, current, configuration, (after) => {
140
+ this.entries.set(file, {
141
+ before: entry ? entry.before : current,
142
+ after,
143
+ configuration: configuration ?? entry?.configuration,
144
+ });
145
+ });
146
+ }
147
+
148
+ async commit(
149
+ file: string,
150
+ next: FileState,
151
+ expected: FileState,
152
+ configuration: string | undefined,
153
+ committed: (after: FileState) => void,
154
+ ): Promise<void> {
155
+ this.guard(file);
156
+ const commit = (temporary?: string, mode?: number) => {
157
+ // Keep the final comparison and mutation synchronous, including the shared config fence.
158
+ const replace = () => {
159
+ if (configuration) assertConfigWriteTarget(configuration, file);
160
+ this.guard(file);
161
+ if (!equal(state(file), expected))
162
+ throw conflict("Installer destination changed before commit");
163
+ if (next) (this.options.rename ?? renameSync)(temporary!, file);
164
+ else (this.options.remove ?? rmSync)(file);
165
+ committed(next ? { bytes: next.bytes, mode: mode! } : undefined);
166
+ };
167
+ if (configuration) withConfigTransaction(file, replace);
168
+ else replace();
169
+ };
170
+ if (!next) return commit();
171
+ await this.mkdir(path.dirname(file));
172
+ this.guard(file);
173
+ const temporary = path.join(
174
+ path.dirname(file),
175
+ ".ast-mcp-" + randomUUID() + ".tmp",
176
+ );
177
+ try {
178
+ await writeFile(temporary, next.bytes, { flag: "wx", mode: next.mode });
179
+ await chmod(temporary, next.mode);
180
+ // Windows reports supported permission bits, rather than every requested POSIX bit.
181
+ const mode = (await lstat(temporary)).mode & 0o777;
182
+ return commit(temporary, mode);
183
+ } finally {
184
+ await rm(temporary, { force: true });
185
+ }
186
+ }
187
+
188
+ assertRestored(file: string) {
189
+ file = path.resolve(file);
190
+ this.guard(file);
191
+ const entry = this.entries.get(file);
192
+ if (!entry || !equal(state(file), entry.before))
193
+ throw conflict("Installer compensation requires its original file state");
194
+ }
195
+
196
+ async rollback() {
197
+ const failures: unknown[] = [];
198
+ for (const [file, entry] of [...this.entries].reverse()) {
199
+ try {
200
+ if (equal(entry.before, entry.after)) continue;
201
+ if (!equal(state(file), entry.after))
202
+ throw conflict(
203
+ "Installer rollback preserved a concurrently changed destination",
204
+ );
205
+ await this.commit(
206
+ file,
207
+ entry.before,
208
+ entry.after,
209
+ entry.configuration,
210
+ (after) => {
211
+ entry.after = after;
212
+ },
213
+ );
214
+ } catch (error) {
215
+ failures.push(error);
216
+ }
217
+ }
218
+ // Compensations validate their own dependencies; unrelated conflicts must not suppress recovery.
219
+ for (const compensate of this.compensations.reverse()) {
220
+ try {
221
+ await compensate();
222
+ } catch (error) {
223
+ failures.push(error);
224
+ }
225
+ }
226
+ for (const [directory, before] of [...this.directories].sort(
227
+ ([a], [b]) => b.length - a.length,
228
+ )) {
229
+ try {
230
+ const current = lstatSync(directory);
231
+ if (
232
+ current.ino === before.ino &&
233
+ current.dev === before.dev &&
234
+ realpathSync(directory) === before.real
235
+ )
236
+ rmdirSync(directory);
237
+ } catch (error) {
238
+ if (
239
+ !["ENOENT", "ENOTEMPTY", "EEXIST"].includes(
240
+ (error as NodeJS.ErrnoException).code ?? "",
241
+ )
242
+ )
243
+ failures.push(error);
244
+ }
245
+ }
246
+ return failures;
247
+ }
248
+ }
249
+
250
+ /** Memory-only rollback journal. Nested installer operations join the same transaction. */
251
+ export async function withInstallerFileTransaction<T>(
252
+ operation: () => Promise<T>,
253
+ options: TransactionOptions = {},
254
+ ): Promise<T> {
255
+ if (active.getStore()) return operation();
256
+ const transaction = new FileTransaction(options);
257
+ return active.run(transaction, async () => {
258
+ try {
259
+ return await operation();
260
+ } catch (error) {
261
+ const failures = await transaction.rollback();
262
+ if (failures.length)
263
+ throw new AggregateError(
264
+ [error, ...failures],
265
+ "Installer failed; rollback was incomplete. Concurrent user edits were preserved.",
266
+ { cause: error },
267
+ );
268
+ throw error;
269
+ }
270
+ });
271
+ }
272
+
273
+ export function assertInstallerRestored(file: string) {
274
+ const transaction = active.getStore();
275
+ if (!transaction)
276
+ throw new Error("Installer compensation requires an active transaction");
277
+ transaction.assertRestored(file);
278
+ }
279
+
280
+ export function onInstallerRollback(compensate: () => Promise<void>) {
281
+ active.getStore()?.compensations.push(compensate);
282
+ }
283
+
284
+ export async function mkdirInstallerDirectory(
285
+ directory: string,
286
+ options: { recursive: true } = { recursive: true },
287
+ ) {
288
+ const transaction = active.getStore();
289
+ return transaction ? transaction.mkdir(directory) : mkdir(directory, options);
290
+ }
291
+
292
+ export async function writeInstallerFile(
293
+ file: string,
294
+ content: string,
295
+ options: {
296
+ mode?: number;
297
+ exclusive?: boolean;
298
+ expected?: string;
299
+ configuration?: string;
300
+ } = {},
301
+ ): Promise<void> {
302
+ const transaction = active.getStore();
303
+ if (!transaction)
304
+ return withInstallerFileTransaction(() =>
305
+ writeInstallerFile(file, content, options),
306
+ );
307
+ if (options.configuration) file = canonicalConfigPath(file);
308
+ const current = transaction.observe(file);
309
+ if (
310
+ options.expected !== undefined &&
311
+ (current?.bytes.toString() ?? "") !== options.expected
312
+ )
313
+ throw conflict("Installer configuration changed; reload and retry");
314
+ const next = {
315
+ bytes: Buffer.from(content),
316
+ mode: options.mode ?? current?.mode ?? 0o666 & ~process.umask(),
317
+ };
318
+ await transaction.replace(
319
+ file,
320
+ next,
321
+ current,
322
+ options.exclusive,
323
+ options.configuration,
324
+ );
325
+ }
326
+
327
+ export async function readInstallerText(file: string) {
328
+ const transaction = active.getStore();
329
+ const current = transaction ? transaction.observe(file) : state(file);
330
+ if (!current)
331
+ throw Object.assign(new Error("Installer file does not exist"), {
332
+ code: "ENOENT",
333
+ });
334
+ return current.bytes.toString();
335
+ }
336
+
337
+ export async function removeInstallerFile(file: string): Promise<void> {
338
+ const transaction = active.getStore();
339
+ if (transaction)
340
+ return transaction.replace(file, undefined, transaction.observe(file));
341
+ return withInstallerFileTransaction(() => removeInstallerFile(file));
342
+ }
@@ -1,6 +1,3 @@
1
- import { randomUUID } from "node:crypto";
2
- import { readFileSync, renameSync } from "node:fs";
3
- import { chmod, mkdir, unlink, writeFile } from "node:fs/promises";
4
1
  import { isIP } from "node:net";
5
2
  import path from "node:path";
6
3
 
@@ -8,8 +5,12 @@ import { clearConfigCache, globalConfigPath, resolveConfig } from "../config";
8
5
  import {
9
6
  assertConfigWriteTarget,
10
7
  configWriteTarget,
11
- withConfigTransaction,
12
8
  } from "../config/transaction";
9
+ import {
10
+ mkdirInstallerDirectory as mkdir,
11
+ withInstallerFileTransaction,
12
+ writeInstallerFile,
13
+ } from "./transaction";
13
14
 
14
15
  export const transports = ["stdio", "http"] as const;
15
16
  export type McpTransport = (typeof transports)[number];
@@ -165,6 +166,17 @@ export function updateHttpToml(
165
166
 
166
167
  export async function resolveInstallerEndpoint(
167
168
  options: InstallerEndpointOptions,
169
+ ): Promise<HttpEndpoint> {
170
+ try {
171
+ return await withInstallerFileTransaction(() => resolveEndpoint(options));
172
+ } catch (error) {
173
+ clearConfigCache();
174
+ throw error;
175
+ }
176
+ }
177
+
178
+ async function resolveEndpoint(
179
+ options: InstallerEndpointOptions,
168
180
  ): Promise<HttpEndpoint> {
169
181
  const configFile =
170
182
  options.scope === "local"
@@ -186,33 +198,12 @@ export async function resolveInstallerEndpoint(
186
198
  host: options.host,
187
199
  port: options.port,
188
200
  });
189
- const temporary = `${target}.endpoint-${randomUUID()}`;
190
- try {
191
- await writeFile(temporary, updated, {
192
- flag: "wx",
193
- mode,
194
- });
195
- await chmod(temporary, mode);
196
- withConfigTransaction(target, () => {
197
- assertConfigWriteTarget(configFile, target);
198
- let latest = "";
199
- try {
200
- latest = readFileSync(target, "utf8");
201
- } catch (error) {
202
- if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
203
- }
204
- if (latest !== current)
205
- throw Object.assign(
206
- new Error(
207
- "Configuration changed before endpoint commit; reload and retry",
208
- ),
209
- { code: "configuration_conflict", retryable: true },
210
- );
211
- renameSync(temporary, target);
212
- });
213
- } finally {
214
- await unlink(temporary).catch(() => undefined);
215
- }
201
+ assertConfigWriteTarget(configFile, target);
202
+ await writeInstallerFile(target, updated, {
203
+ expected: current,
204
+ mode: mode & 0o777,
205
+ configuration: configFile,
206
+ });
216
207
  clearConfigCache();
217
208
  }
218
209
  const config = await resolveConfig({
@@ -815,7 +815,10 @@ async function withQueue<Result>(
815
815
  return await operation();
816
816
  } finally {
817
817
  releaseQueue();
818
- if (queues.get(filePath) === queued) queues.delete(filePath);
818
+ // A cancelled waiter must retain the preceding owner's queue lifetime.
819
+ void queued.then(() => {
820
+ if (queues.get(filePath) === queued) queues.delete(filePath);
821
+ });
819
822
  }
820
823
  }
821
824