@mwillbanks/ast-mcp 0.1.3 → 0.2.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/README.md +59 -20
- package/dist/ast-mcp.js +232 -130
- package/dist/ast-mcp.js.map +107 -27
- package/package.json +2 -1
- package/src/ast-bro/capability.ts +3 -8
- package/src/ast-bro/client.ts +18 -2
- package/src/ast-bro/result.ts +5 -12
- package/src/cli.ts +70 -95
- package/src/config-cli.ts +54 -0
- package/src/config.ts +886 -0
- package/src/helpers/aider.ts +252 -0
- package/src/helpers/ast-bro.ts +64 -0
- package/src/helpers/config.ts +149 -0
- package/src/helpers/hook.ts +60 -0
- package/src/helpers/installer.ts +249 -0
- package/src/helpers/json.ts +19 -0
- package/src/helpers/mcp-schema.ts +31 -0
- package/src/helpers/process.ts +31 -0
- package/src/helpers/string.ts +97 -0
- package/src/hook.ts +135 -40
- package/src/http-entry.ts +1 -1
- package/src/http.ts +118 -103
- package/src/installer-transport.ts +190 -0
- package/src/installer.ts +579 -242
- package/src/mcp-cli.ts +104 -0
- package/src/patch/aider.ts +6 -199
- package/src/patch/engine.ts +271 -197
- package/src/runtime/dependencies.ts +48 -29
- package/src/runtime/file-delete.ts +13 -9
- package/src/runtime/file-read.ts +91 -57
- package/src/runtime/file-rename.ts +158 -0
- package/src/runtime/format.ts +57 -34
- package/src/runtime/paths.ts +30 -16
- package/src/runtime/policy.ts +21 -0
- package/src/runtime/process-input.ts +14 -4
- package/src/service.ts +219 -0
- package/src/shell-policy.ts +331 -0
- package/src/stdio.ts +47 -35
- package/src/tools/ast-bro.ts +12 -6
- package/src/tools/configured.ts +28 -0
- package/src/tools/files.ts +24 -27
- package/src/tools/index.ts +5 -3
- package/src/tools/lifecycle.ts +69 -55
- package/templates/skills/ast-mcp/SKILL.md +7 -6
- package/templates/skills/ast-mcp/evals/batch.evals.json +48 -0
- package/templates/skills/ast-mcp/evals/evals.json +1 -0
- package/templates/skills/ast-mcp/evals/score.test.ts +175 -0
- package/templates/skills/ast-mcp/evals/score.ts +304 -267
- package/templates/skills/ast-mcp/evals/semantic.test.ts +322 -0
- package/templates/skills/ast-mcp/evals/semantic.ts +390 -135
- package/templates/skills/ast-mcp/evals/shell-policy.ts +252 -0
- package/templates/skills/ast-mcp/evals/source.test.ts +46 -0
- package/templates/skills/ast-mcp/evals/source.ts +164 -0
- package/templates/skills/ast-mcp/evals/trigger-queries.json +1 -1
- package/templates/skills/ast-mcp/references/installation.md +9 -8
- package/templates/skills/ast-mcp/references/patch-state-machine.md +5 -5
- package/templates/skills/ast-mcp/references/tool-catalog.md +4 -2
- package/templates/skills/ast-mcp/scripts/check-install.ts +312 -110
package/src/patch/engine.ts
CHANGED
|
@@ -22,6 +22,7 @@ import { formatContent } from "../runtime/format";
|
|
|
22
22
|
import { sha256 } from "../runtime/hash";
|
|
23
23
|
import { withFileLock } from "../runtime/locks";
|
|
24
24
|
import { resolveWritablePath } from "../runtime/paths";
|
|
25
|
+
import { requireExpectedHash, verifyExpectedHash } from "../runtime/policy";
|
|
25
26
|
|
|
26
27
|
import { applyAiderBlock } from "./aider";
|
|
27
28
|
import { detectAstLanguage } from "./languages";
|
|
@@ -38,7 +39,7 @@ export interface AiderBlock {
|
|
|
38
39
|
export interface PatchRequest {
|
|
39
40
|
aiderBlock?: AiderBlock;
|
|
40
41
|
astRule?: AstRule;
|
|
41
|
-
expectedSha256
|
|
42
|
+
expectedSha256?: string;
|
|
42
43
|
filePath: string;
|
|
43
44
|
patchStrategy: "ast" | "aider_block";
|
|
44
45
|
preview?: boolean;
|
|
@@ -48,7 +49,7 @@ export interface PatchBatchRequest {
|
|
|
48
49
|
aiderBlocks?: AiderBlock[];
|
|
49
50
|
astRules?: AstRule[];
|
|
50
51
|
chattr?: FileChattr;
|
|
51
|
-
expectedSha256
|
|
52
|
+
expectedSha256?: string;
|
|
52
53
|
patchStrategy: "ast" | "aider_block";
|
|
53
54
|
preview?: boolean;
|
|
54
55
|
}
|
|
@@ -161,191 +162,269 @@ function previewDiff(filePath: string, before: string, after: string): string {
|
|
|
161
162
|
: lines.join("\n");
|
|
162
163
|
}
|
|
163
164
|
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
interface PatchContext {
|
|
166
|
+
actual: string;
|
|
167
|
+
aiderBlocks: AiderBlock[];
|
|
168
|
+
astRules: AstRule[];
|
|
169
|
+
filePath: string;
|
|
170
|
+
language: ReturnType<typeof detectAstLanguage>;
|
|
171
|
+
original: string;
|
|
172
|
+
request: PatchBatchRequest;
|
|
173
|
+
rewritable: boolean;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
interface PreviewMetadata {
|
|
177
|
+
matches?: number;
|
|
178
|
+
matchMethods?: string[];
|
|
179
|
+
operations: number;
|
|
180
|
+
strategy: "ast" | "aider_block";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async function createPatchContext(
|
|
184
|
+
filePath: string,
|
|
166
185
|
request: PatchBatchRequest,
|
|
167
|
-
): Promise<
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
186
|
+
): Promise<PatchContext> {
|
|
187
|
+
const original = await readFile(filePath, "utf8");
|
|
188
|
+
const actual = sha256(original);
|
|
189
|
+
await requireExpectedHash(request.expectedSha256, "file_patch");
|
|
190
|
+
verifyExpectedHash(request.expectedSha256, actual);
|
|
191
|
+
const language = detectAstLanguage(filePath);
|
|
192
|
+
return {
|
|
193
|
+
actual,
|
|
194
|
+
aiderBlocks: request.aiderBlocks ?? [],
|
|
195
|
+
astRules: request.astRules ?? [],
|
|
196
|
+
filePath,
|
|
197
|
+
language,
|
|
198
|
+
original,
|
|
199
|
+
request,
|
|
200
|
+
rewritable: await astRewritable(filePath, language),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
179
203
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
matches: metadata.matches,
|
|
199
|
-
matchMethods: metadata.matchMethods,
|
|
200
|
-
operations: metadata.operations,
|
|
201
|
-
preview: true,
|
|
202
|
-
sha256: sha256(formatted),
|
|
203
|
-
strategy: metadata.strategy,
|
|
204
|
-
};
|
|
205
|
-
};
|
|
204
|
+
async function patchPreview(
|
|
205
|
+
context: PatchContext,
|
|
206
|
+
candidate: string,
|
|
207
|
+
metadata: PreviewMetadata,
|
|
208
|
+
) {
|
|
209
|
+
const formatted = await formatContent(context.filePath, candidate);
|
|
210
|
+
return {
|
|
211
|
+
changed: formatted !== context.original,
|
|
212
|
+
diff: previewDiff(context.filePath, context.original, formatted),
|
|
213
|
+
filePath: context.filePath,
|
|
214
|
+
matches: metadata.matches,
|
|
215
|
+
matchMethods: metadata.matchMethods,
|
|
216
|
+
operations: metadata.operations,
|
|
217
|
+
preview: true,
|
|
218
|
+
sha256: sha256(formatted),
|
|
219
|
+
strategy: metadata.strategy,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
206
222
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
223
|
+
function validateAstStrategy(
|
|
224
|
+
context: PatchContext,
|
|
225
|
+
): asserts context is PatchContext & {
|
|
226
|
+
language: NonNullable<PatchContext["language"]>;
|
|
227
|
+
} {
|
|
228
|
+
if (!context.rewritable || !context.language)
|
|
229
|
+
throw new Error(
|
|
230
|
+
"REJECTED: non-structurally-rewritable files require patchStrategy 'aider_block' with aiderBlocks",
|
|
231
|
+
);
|
|
232
|
+
if (context.astRules.length === 0 || context.aiderBlocks.length > 0)
|
|
233
|
+
throw new Error(
|
|
234
|
+
"REJECTED: patchStrategy 'ast' requires astRules and no aiderBlocks",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
216
237
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
await writeFile(next, original, {
|
|
221
|
-
encoding: "utf8",
|
|
222
|
-
flag: "wx",
|
|
223
|
-
mode: metadata.mode,
|
|
224
|
-
});
|
|
225
|
-
let totalMatches = 0;
|
|
226
|
-
for (const rule of astRules) {
|
|
227
|
-
const preview = parseAstBroJson(
|
|
228
|
-
await callAstBro(
|
|
229
|
-
"run",
|
|
230
|
-
{
|
|
231
|
-
json: true,
|
|
232
|
-
lang: language,
|
|
233
|
-
paths: [next],
|
|
234
|
-
pattern: rule.pattern,
|
|
235
|
-
},
|
|
236
|
-
path.dirname(filePath),
|
|
237
|
-
),
|
|
238
|
-
);
|
|
239
|
-
if (preview.error_count)
|
|
240
|
-
throw new Error(
|
|
241
|
-
`ast-bro preview failed with ${preview.error_count} errors`,
|
|
242
|
-
);
|
|
243
|
-
const matches = Array.isArray(preview.matches)
|
|
244
|
-
? preview.matches.length
|
|
245
|
-
: 0;
|
|
246
|
-
const expected = rule.expectedMatches ?? 1;
|
|
247
|
-
if (matches !== expected)
|
|
248
|
-
throw new Error(
|
|
249
|
-
`AST rule matched ${matches} nodes; expected ${expected}`,
|
|
250
|
-
);
|
|
251
|
-
if (expected !== 1)
|
|
252
|
-
throw new Error(
|
|
253
|
-
"ast-bro run rewrites only the first match per file; narrow the AST rule to exactly one node",
|
|
254
|
-
);
|
|
238
|
+
type AstPatchContext = PatchContext & {
|
|
239
|
+
language: NonNullable<PatchContext["language"]>;
|
|
240
|
+
};
|
|
255
241
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
242
|
+
async function previewAstRule(
|
|
243
|
+
context: AstPatchContext,
|
|
244
|
+
next: string,
|
|
245
|
+
rule: AstRule,
|
|
246
|
+
) {
|
|
247
|
+
const preview = parseAstBroJson(
|
|
248
|
+
await callAstBro(
|
|
249
|
+
"run",
|
|
250
|
+
{
|
|
251
|
+
json: true,
|
|
252
|
+
lang: context.language,
|
|
253
|
+
paths: [next],
|
|
254
|
+
pattern: rule.pattern,
|
|
255
|
+
},
|
|
256
|
+
path.dirname(context.filePath),
|
|
257
|
+
),
|
|
258
|
+
);
|
|
259
|
+
if (preview.error_count)
|
|
260
|
+
throw new Error(
|
|
261
|
+
`ast-bro preview failed with ${preview.error_count} errors`,
|
|
262
|
+
);
|
|
263
|
+
const matches = Array.isArray(preview.matches) ? preview.matches.length : 0;
|
|
264
|
+
const expected = rule.expectedMatches ?? 1;
|
|
265
|
+
if (matches !== expected)
|
|
266
|
+
throw new Error(`AST rule matched ${matches} nodes; expected ${expected}`);
|
|
267
|
+
if (expected !== 1)
|
|
268
|
+
throw new Error(
|
|
269
|
+
"ast-bro run rewrites only the first match per file; narrow the AST rule to exactly one node",
|
|
270
|
+
);
|
|
271
|
+
return matches;
|
|
272
|
+
}
|
|
278
273
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
274
|
+
async function rewriteAstRule(
|
|
275
|
+
context: AstPatchContext,
|
|
276
|
+
next: string,
|
|
277
|
+
rule: AstRule,
|
|
278
|
+
) {
|
|
279
|
+
const rewritten = parseAstBroJson(
|
|
280
|
+
await callAstBro(
|
|
281
|
+
"run",
|
|
282
|
+
{
|
|
283
|
+
json: true,
|
|
284
|
+
lang: context.language,
|
|
285
|
+
paths: [next],
|
|
286
|
+
pattern: rule.pattern,
|
|
287
|
+
rewrite: rule.fix,
|
|
288
|
+
write: true,
|
|
289
|
+
},
|
|
290
|
+
path.dirname(context.filePath),
|
|
291
|
+
),
|
|
292
|
+
);
|
|
293
|
+
if (
|
|
294
|
+
rewritten.error_count ||
|
|
295
|
+
rewritten.rewrite_count !== 1 ||
|
|
296
|
+
rewritten.files?.[0]?.status !== "rewritten"
|
|
297
|
+
)
|
|
298
|
+
throw new Error("ast-bro run did not rewrite exactly one file");
|
|
299
|
+
}
|
|
286
300
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
return {
|
|
296
|
-
engine: "ast-bro.run",
|
|
297
|
-
filePath,
|
|
298
|
-
matches: totalMatches,
|
|
299
|
-
operations: astRules.length,
|
|
300
|
-
preview: false,
|
|
301
|
-
sha256: sha256(updated),
|
|
302
|
-
strategy: "ast",
|
|
303
|
-
};
|
|
304
|
-
} finally {
|
|
305
|
-
await removeTemporary(next);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
301
|
+
async function applyAstRules(context: AstPatchContext, next: string) {
|
|
302
|
+
let totalMatches = 0;
|
|
303
|
+
for (const rule of context.astRules) {
|
|
304
|
+
totalMatches += await previewAstRule(context, next, rule);
|
|
305
|
+
await rewriteAstRule(context, next, rule);
|
|
306
|
+
}
|
|
307
|
+
return totalMatches;
|
|
308
|
+
}
|
|
308
309
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
310
|
+
async function astPatchResult(
|
|
311
|
+
context: AstPatchContext,
|
|
312
|
+
candidate: string,
|
|
313
|
+
mode: number,
|
|
314
|
+
totalMatches: number,
|
|
315
|
+
) {
|
|
316
|
+
if (context.request.preview)
|
|
317
|
+
return patchPreview(context, candidate, {
|
|
318
|
+
matches: totalMatches,
|
|
319
|
+
operations: context.astRules.length,
|
|
320
|
+
strategy: "ast",
|
|
321
|
+
});
|
|
322
|
+
await commit(
|
|
323
|
+
context.filePath,
|
|
324
|
+
candidate,
|
|
325
|
+
mode,
|
|
326
|
+
context.actual,
|
|
327
|
+
context.request.chattr,
|
|
328
|
+
);
|
|
329
|
+
const updated = await readFile(context.filePath, "utf8");
|
|
330
|
+
return {
|
|
331
|
+
engine: "ast-bro.run",
|
|
332
|
+
filePath: context.filePath,
|
|
333
|
+
matches: totalMatches,
|
|
334
|
+
operations: context.astRules.length,
|
|
335
|
+
preview: false,
|
|
336
|
+
sha256: sha256(updated),
|
|
337
|
+
strategy: "ast",
|
|
338
|
+
};
|
|
339
|
+
}
|
|
317
340
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
341
|
+
async function applyAstPatch(context: PatchContext) {
|
|
342
|
+
validateAstStrategy(context);
|
|
343
|
+
const next = temporary(context.filePath);
|
|
344
|
+
const metadata = await lstat(context.filePath);
|
|
345
|
+
try {
|
|
346
|
+
await writeFile(next, context.original, {
|
|
347
|
+
encoding: "utf8",
|
|
348
|
+
flag: "wx",
|
|
349
|
+
mode: metadata.mode,
|
|
350
|
+
});
|
|
351
|
+
const totalMatches = await applyAstRules(context, next);
|
|
352
|
+
const candidate = await readFile(next, "utf8");
|
|
353
|
+
return await astPatchResult(
|
|
354
|
+
context,
|
|
355
|
+
candidate,
|
|
356
|
+
metadata.mode,
|
|
357
|
+
totalMatches,
|
|
358
|
+
);
|
|
359
|
+
} finally {
|
|
360
|
+
await removeTemporary(next);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
331
363
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
actual,
|
|
337
|
-
request.chattr,
|
|
364
|
+
function validateAiderStrategy(context: PatchContext) {
|
|
365
|
+
if (context.rewritable)
|
|
366
|
+
throw new Error(
|
|
367
|
+
"REJECTED: structurally rewritable files require patchStrategy 'ast' with astRules",
|
|
338
368
|
);
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
369
|
+
if (context.aiderBlocks.length === 0 || context.astRules.length > 0)
|
|
370
|
+
throw new Error(
|
|
371
|
+
"REJECTED: patchStrategy 'aider_block' requires aiderBlocks and no astRules",
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function applyAiderBlocks(content: string, blocks: AiderBlock[]) {
|
|
376
|
+
const methods: string[] = [];
|
|
377
|
+
for (const block of blocks) {
|
|
378
|
+
const result = applyAiderBlock(content, block.search, block.replace);
|
|
379
|
+
content = result.content;
|
|
380
|
+
methods.push(result.method);
|
|
381
|
+
}
|
|
382
|
+
return { content, methods };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
async function applyAiderPatch(context: PatchContext) {
|
|
386
|
+
validateAiderStrategy(context);
|
|
387
|
+
const { content, methods } = applyAiderBlocks(
|
|
388
|
+
context.original,
|
|
389
|
+
context.aiderBlocks,
|
|
390
|
+
);
|
|
391
|
+
if (context.request.preview)
|
|
392
|
+
return patchPreview(context, content, {
|
|
342
393
|
matchMethods: methods,
|
|
343
|
-
operations: aiderBlocks.length,
|
|
344
|
-
preview: false,
|
|
345
|
-
sha256: sha256(updated),
|
|
394
|
+
operations: context.aiderBlocks.length,
|
|
346
395
|
strategy: "aider_block",
|
|
347
|
-
};
|
|
348
|
-
|
|
396
|
+
});
|
|
397
|
+
await commit(
|
|
398
|
+
context.filePath,
|
|
399
|
+
content,
|
|
400
|
+
(await lstat(context.filePath)).mode,
|
|
401
|
+
context.actual,
|
|
402
|
+
context.request.chattr,
|
|
403
|
+
);
|
|
404
|
+
const updated = await readFile(context.filePath, "utf8");
|
|
405
|
+
return {
|
|
406
|
+
filePath: context.filePath,
|
|
407
|
+
matchMethods: methods,
|
|
408
|
+
operations: context.aiderBlocks.length,
|
|
409
|
+
preview: false,
|
|
410
|
+
sha256: sha256(updated),
|
|
411
|
+
strategy: "aider_block",
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async function applyLockedPatch(filePath: string, request: PatchBatchRequest) {
|
|
416
|
+
const context = await createPatchContext(filePath, request);
|
|
417
|
+
return request.patchStrategy === "ast"
|
|
418
|
+
? applyAstPatch(context)
|
|
419
|
+
: applyAiderPatch(context);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async function applyPatchBatch(
|
|
423
|
+
inputPath: string,
|
|
424
|
+
request: PatchBatchRequest,
|
|
425
|
+
): Promise<Record<string, unknown>> {
|
|
426
|
+
const filePath = await resolveWritablePath(inputPath);
|
|
427
|
+
return withFileLock(filePath, () => applyLockedPatch(filePath, request));
|
|
349
428
|
}
|
|
350
429
|
|
|
351
430
|
export async function patchFile(
|
|
@@ -360,20 +439,28 @@ export async function patchFile(
|
|
|
360
439
|
});
|
|
361
440
|
}
|
|
362
441
|
|
|
363
|
-
|
|
364
|
-
|
|
442
|
+
async function processFileBatch<T>(
|
|
443
|
+
tool: "file_patch" | "file_write",
|
|
444
|
+
requests: Record<string, T>,
|
|
445
|
+
handler: (filePath: string, request: T) => Promise<Record<string, unknown>>,
|
|
365
446
|
): Promise<Record<string, unknown>> {
|
|
366
447
|
const entries = Object.entries(requests);
|
|
367
448
|
if (entries.length < 1 || entries.length > FILE_READ_MAX_BATCH)
|
|
368
449
|
throw new Error(
|
|
369
|
-
|
|
450
|
+
`${tool} requires between 1 and ${FILE_READ_MAX_BATCH} files`,
|
|
370
451
|
);
|
|
371
452
|
const files: Record<string, unknown> = {};
|
|
372
453
|
for (const [filePath, request] of entries)
|
|
373
|
-
files[filePath] = await
|
|
454
|
+
files[filePath] = await handler(filePath, request);
|
|
374
455
|
return { files };
|
|
375
456
|
}
|
|
376
457
|
|
|
458
|
+
export async function patchFiles(
|
|
459
|
+
requests: PatchBatch,
|
|
460
|
+
): Promise<Record<string, unknown>> {
|
|
461
|
+
return processFileBatch("file_patch", requests, applyPatchBatch);
|
|
462
|
+
}
|
|
463
|
+
|
|
377
464
|
export async function writeFileSafely(args: {
|
|
378
465
|
chattr?: FileChattr;
|
|
379
466
|
filePath: string;
|
|
@@ -385,16 +472,9 @@ export async function writeFileSafely(args: {
|
|
|
385
472
|
const existing = await readFile(filePath, "utf8").catch(() => undefined);
|
|
386
473
|
let createdDirectories: string[];
|
|
387
474
|
if (existing !== undefined) {
|
|
388
|
-
|
|
389
|
-
throw new Error("Overwriting an existing file requires expectedSha256");
|
|
475
|
+
await requireExpectedHash(args.expectedSha256, "file_write overwrite");
|
|
390
476
|
const actual = sha256(existing);
|
|
391
|
-
|
|
392
|
-
throw new Error(
|
|
393
|
-
"Stale file context: expected " +
|
|
394
|
-
args.expectedSha256 +
|
|
395
|
-
", found " +
|
|
396
|
-
actual,
|
|
397
|
-
);
|
|
477
|
+
verifyExpectedHash(args.expectedSha256, actual);
|
|
398
478
|
const language = detectAstLanguage(filePath);
|
|
399
479
|
if (await astRewritable(filePath, language))
|
|
400
480
|
throw new Error(
|
|
@@ -429,18 +509,12 @@ export async function writeFileSafely(args: {
|
|
|
429
509
|
export async function writeFilesSafely(
|
|
430
510
|
requests: FileWriteBatch,
|
|
431
511
|
): Promise<Record<string, unknown>> {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
throw new Error(
|
|
435
|
-
`file_write requires between 1 and ${FILE_READ_MAX_BATCH} files`,
|
|
436
|
-
);
|
|
437
|
-
const files: Record<string, unknown> = {};
|
|
438
|
-
for (const [filePath, request] of entries)
|
|
439
|
-
files[filePath] = await writeFileSafely({
|
|
512
|
+
return processFileBatch("file_write", requests, (filePath, request) =>
|
|
513
|
+
writeFileSafely({
|
|
440
514
|
chattr: request.chattr,
|
|
441
515
|
content: request.content,
|
|
442
516
|
expectedSha256: request.expectedSha256,
|
|
443
517
|
filePath,
|
|
444
|
-
})
|
|
445
|
-
|
|
518
|
+
}),
|
|
519
|
+
);
|
|
446
520
|
}
|
|
@@ -2,8 +2,9 @@ import { accessSync, constants, readFileSync } from "node:fs";
|
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
+
import { currentConfig } from "../config";
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
const PACKAGE_ROOT = path.resolve(
|
|
7
8
|
import.meta.dir,
|
|
8
9
|
path.basename(import.meta.dir) === "dist" ? ".." : "../..",
|
|
9
10
|
);
|
|
@@ -97,50 +98,60 @@ function globalBinDirectories(binaryName: string, platform: NodeJS.Platform) {
|
|
|
97
98
|
return [...directories];
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
options: BinaryResolutionOptions = {},
|
|
104
|
-
) {
|
|
105
|
-
const platform = options.platform ?? process.platform;
|
|
106
|
-
const packageRoot = options.packageRoot ?? PACKAGE_ROOT;
|
|
107
|
-
const names = executableNames(binaryName, platform);
|
|
108
|
-
const candidates: string[] = [];
|
|
101
|
+
function executableCandidate(candidates: string[], platform: NodeJS.Platform) {
|
|
102
|
+
return candidates.find((candidate) => isExecutable(candidate, platform));
|
|
103
|
+
}
|
|
109
104
|
|
|
105
|
+
function ancestorBinaryCandidates(packageRoot: string, names: string[]) {
|
|
106
|
+
const candidates: string[] = [];
|
|
110
107
|
for (
|
|
111
108
|
let current = packageRoot;
|
|
112
109
|
path.dirname(current) !== current;
|
|
113
110
|
current = path.dirname(current)
|
|
114
|
-
)
|
|
111
|
+
) {
|
|
115
112
|
for (const name of names)
|
|
116
113
|
candidates.push(path.join(current, "node_modules/.bin", name));
|
|
114
|
+
}
|
|
115
|
+
return candidates;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function directoryBinaryCandidates(directories: string[], names: string[]) {
|
|
119
|
+
const candidates: string[] = [];
|
|
120
|
+
for (const directory of directories) {
|
|
121
|
+
if (!directory) continue;
|
|
122
|
+
for (const name of names) candidates.push(path.join(directory, name));
|
|
123
|
+
}
|
|
124
|
+
return candidates;
|
|
125
|
+
}
|
|
117
126
|
|
|
127
|
+
export function resolveDependencyBinary(
|
|
128
|
+
binaryName: string,
|
|
129
|
+
packageName = binaryName,
|
|
130
|
+
options: BinaryResolutionOptions = {},
|
|
131
|
+
) {
|
|
132
|
+
const platform = options.platform ?? process.platform;
|
|
133
|
+
const packageRoot = options.packageRoot ?? PACKAGE_ROOT;
|
|
134
|
+
const names = executableNames(binaryName, platform);
|
|
135
|
+
const localCandidates = ancestorBinaryCandidates(packageRoot, names);
|
|
118
136
|
const directPackageBinary =
|
|
119
137
|
options.packageBinary ?? packageBinary(packageName, binaryName);
|
|
120
|
-
if (directPackageBinary)
|
|
121
|
-
const local =
|
|
122
|
-
isExecutable(candidate, platform),
|
|
123
|
-
);
|
|
138
|
+
if (directPackageBinary) localCandidates.push(directPackageBinary);
|
|
139
|
+
const local = executableCandidate(localCandidates, platform);
|
|
124
140
|
if (local) return local;
|
|
125
|
-
|
|
126
141
|
const globalDirectories =
|
|
127
142
|
options.globalBinDirectories ?? globalBinDirectories(binaryName, platform);
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const global = candidates.find((candidate) =>
|
|
132
|
-
isExecutable(candidate, platform),
|
|
143
|
+
const global = executableCandidate(
|
|
144
|
+
directoryBinaryCandidates(globalDirectories, names),
|
|
145
|
+
platform,
|
|
133
146
|
);
|
|
134
147
|
if (global) return global;
|
|
135
|
-
|
|
136
|
-
candidates.length = 0;
|
|
137
|
-
for (const directory of (options.pathValue ?? process.env.PATH ?? "").split(
|
|
148
|
+
const pathDirectories = (options.pathValue ?? process.env.PATH ?? "").split(
|
|
138
149
|
path.delimiter,
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
150
|
+
);
|
|
151
|
+
return executableCandidate(
|
|
152
|
+
directoryBinaryCandidates(pathDirectories, names),
|
|
153
|
+
platform,
|
|
154
|
+
);
|
|
144
155
|
}
|
|
145
156
|
|
|
146
157
|
export const AST_BRO_VERSION = "3.0.0";
|
|
@@ -186,3 +197,11 @@ export const DPRINT_BINARY =
|
|
|
186
197
|
process.env.DPRINT_BINARY ??
|
|
187
198
|
resolveDependencyBinary("dprint", "dprint") ??
|
|
188
199
|
"dprint";
|
|
200
|
+
|
|
201
|
+
export async function configuredAstBroBinary(): Promise<string> {
|
|
202
|
+
return (await currentConfig()).dependencies.astBroBinary ?? AST_BRO_BINARY;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function configuredDprintBinary(): Promise<string> {
|
|
206
|
+
return (await currentConfig()).dependencies.dprintBinary ?? DPRINT_BINARY;
|
|
207
|
+
}
|