@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/patch/engine.ts
CHANGED
|
@@ -5,37 +5,38 @@ import {
|
|
|
5
5
|
mkdir,
|
|
6
6
|
readFile,
|
|
7
7
|
rename,
|
|
8
|
+
rmdir,
|
|
8
9
|
unlink,
|
|
9
10
|
writeFile,
|
|
10
11
|
} from "node:fs/promises";
|
|
11
12
|
import path from "node:path";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { astRewritable } from "../ast-bro/rewrite-capability";
|
|
13
|
+
import { currentConfig } from "../config";
|
|
14
|
+
import { approvalSessionId } from "../runtime/approval";
|
|
15
15
|
import {
|
|
16
16
|
applyFileChattr,
|
|
17
17
|
type FileChattr,
|
|
18
18
|
resultingFileChattr,
|
|
19
|
+
validateFileChattr,
|
|
19
20
|
} from "../runtime/attributes";
|
|
21
|
+
import {
|
|
22
|
+
type FileCapabilities,
|
|
23
|
+
inspectFileCapabilities,
|
|
24
|
+
validateStructuredCandidate,
|
|
25
|
+
} from "../runtime/file-capabilities";
|
|
20
26
|
import { FILE_READ_MAX_BATCH } from "../runtime/file-read";
|
|
21
27
|
import { formatContent } from "../runtime/format";
|
|
22
28
|
import { sha256 } from "../runtime/hash";
|
|
23
|
-
import { withFileLock } from "../runtime/locks";
|
|
29
|
+
import { withFileLock, withFileLocks } from "../runtime/locks";
|
|
24
30
|
import { resolveWritablePath } from "../runtime/paths";
|
|
25
31
|
import { requireExpectedHash, verifyExpectedHash } from "../runtime/policy";
|
|
32
|
+
import {
|
|
33
|
+
type AiderBlock,
|
|
34
|
+
type AstRule,
|
|
35
|
+
type PatchStrategyContext,
|
|
36
|
+
type PatchStrategyMetadata,
|
|
37
|
+
patchStrategyAdapter,
|
|
38
|
+
} from "./strategy";
|
|
26
39
|
|
|
27
|
-
import { applyAiderBlock } from "./aider";
|
|
28
|
-
import { detectAstLanguage } from "./languages";
|
|
29
|
-
|
|
30
|
-
export interface AstRule {
|
|
31
|
-
expectedMatches?: number;
|
|
32
|
-
fix: string;
|
|
33
|
-
pattern: string;
|
|
34
|
-
}
|
|
35
|
-
export interface AiderBlock {
|
|
36
|
-
replace: string;
|
|
37
|
-
search: string;
|
|
38
|
-
}
|
|
39
40
|
export interface PatchRequest {
|
|
40
41
|
aiderBlock?: AiderBlock;
|
|
41
42
|
astRule?: AstRule;
|
|
@@ -50,8 +51,9 @@ export interface PatchBatchRequest {
|
|
|
50
51
|
astRules?: AstRule[];
|
|
51
52
|
chattr?: FileChattr;
|
|
52
53
|
expectedSha256?: string;
|
|
53
|
-
patchStrategy
|
|
54
|
+
patchStrategy?: "ast" | "aider_block";
|
|
54
55
|
preview?: boolean;
|
|
56
|
+
previewReceipt?: string;
|
|
55
57
|
}
|
|
56
58
|
|
|
57
59
|
export type PatchBatch = Record<string, PatchBatchRequest>;
|
|
@@ -95,26 +97,46 @@ async function ensureParent(filePath: string): Promise<string[]> {
|
|
|
95
97
|
return created;
|
|
96
98
|
}
|
|
97
99
|
|
|
100
|
+
async function ignoreFailure(operation: Promise<unknown>): Promise<void> {
|
|
101
|
+
try {
|
|
102
|
+
await operation;
|
|
103
|
+
} catch {}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function removeCreatedDirectories(directories: string[]): Promise<void> {
|
|
107
|
+
for (const directory of [...directories].reverse())
|
|
108
|
+
await ignoreFailure(rmdir(directory));
|
|
109
|
+
}
|
|
110
|
+
|
|
98
111
|
async function commit(
|
|
99
112
|
filePath: string,
|
|
100
113
|
content: string,
|
|
101
114
|
mode?: number,
|
|
102
115
|
expectedSha256?: string,
|
|
103
116
|
chattr?: FileChattr,
|
|
104
|
-
|
|
117
|
+
alreadyFormatted = false,
|
|
118
|
+
): Promise<{
|
|
119
|
+
chattr: Awaited<ReturnType<typeof resultingFileChattr>>;
|
|
120
|
+
createdDirectories: string[];
|
|
121
|
+
sha256: string;
|
|
122
|
+
}> {
|
|
105
123
|
const next = temporary(filePath);
|
|
106
124
|
let createdDirectories: string[] = [];
|
|
125
|
+
let committed = false;
|
|
107
126
|
try {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
const parentExists = await lstat(path.dirname(filePath)).then(
|
|
128
|
+
() => true,
|
|
129
|
+
() => false,
|
|
130
|
+
);
|
|
131
|
+
createdDirectories = parentExists ? [] : await ensureParent(filePath);
|
|
132
|
+
const formatted = alreadyFormatted
|
|
133
|
+
? content
|
|
134
|
+
: await formatContent(filePath, content);
|
|
135
|
+
await writeFile(next, formatted, { encoding: "utf8", flag: "wx", mode });
|
|
116
136
|
if (mode !== undefined) await chmod(next, mode);
|
|
117
137
|
await applyFileChattr(next, chattr);
|
|
138
|
+
const committedChattr = await resultingFileChattr(next);
|
|
139
|
+
const committedSha256 = sha256(formatted);
|
|
118
140
|
if (expectedSha256) {
|
|
119
141
|
const current = await readFile(filePath, "utf8");
|
|
120
142
|
const actual = sha256(current);
|
|
@@ -124,9 +146,15 @@ async function commit(
|
|
|
124
146
|
);
|
|
125
147
|
}
|
|
126
148
|
await rename(next, filePath);
|
|
127
|
-
|
|
149
|
+
committed = true;
|
|
150
|
+
return {
|
|
151
|
+
chattr: committedChattr,
|
|
152
|
+
createdDirectories,
|
|
153
|
+
sha256: committedSha256,
|
|
154
|
+
};
|
|
128
155
|
} finally {
|
|
129
156
|
await removeTemporary(next);
|
|
157
|
+
if (!committed) await removeCreatedDirectories(createdDirectories);
|
|
130
158
|
}
|
|
131
159
|
}
|
|
132
160
|
function previewDiff(filePath: string, before: string, after: string): string {
|
|
@@ -162,22 +190,112 @@ function previewDiff(filePath: string, before: string, after: string): string {
|
|
|
162
190
|
: lines.join("\n");
|
|
163
191
|
}
|
|
164
192
|
|
|
165
|
-
interface PatchContext {
|
|
193
|
+
interface PatchContext extends PatchStrategyContext {
|
|
166
194
|
actual: string;
|
|
167
|
-
|
|
168
|
-
astRules: AstRule[];
|
|
169
|
-
filePath: string;
|
|
170
|
-
language: ReturnType<typeof detectAstLanguage>;
|
|
171
|
-
original: string;
|
|
195
|
+
capabilities: FileCapabilities;
|
|
172
196
|
request: PatchBatchRequest;
|
|
173
|
-
rewritable: boolean;
|
|
174
197
|
}
|
|
175
198
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
199
|
+
type PreviewMetadata = PatchStrategyMetadata;
|
|
200
|
+
|
|
201
|
+
interface PreviewReceipt {
|
|
202
|
+
candidate: string;
|
|
203
|
+
candidateSha256: string;
|
|
204
|
+
chattr?: FileChattr;
|
|
205
|
+
configurationIdentity: string;
|
|
206
|
+
expiresAt: number;
|
|
207
|
+
expiryTimer: ReturnType<typeof setTimeout>;
|
|
208
|
+
filePath: string;
|
|
209
|
+
generation: number;
|
|
210
|
+
metadata: PreviewMetadata;
|
|
211
|
+
normalizedOperations: Pick<
|
|
212
|
+
PatchBatchRequest,
|
|
213
|
+
"aiderBlocks" | "astRules" | "chattr" | "patchStrategy"
|
|
214
|
+
>;
|
|
215
|
+
payloadBytes: number;
|
|
216
|
+
sessionId: string;
|
|
217
|
+
sourceSha256: string;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const PREVIEW_RECEIPT_TTL_MS = 5 * 60 * 1000;
|
|
221
|
+
const previewConfigurationIdentity = (
|
|
222
|
+
config: Awaited<ReturnType<typeof currentConfig>>,
|
|
223
|
+
) =>
|
|
224
|
+
sha256(
|
|
225
|
+
JSON.stringify({
|
|
226
|
+
dependencies: config.dependencies,
|
|
227
|
+
formatting: config.formatting,
|
|
228
|
+
projectRoot: config.projectRoot,
|
|
229
|
+
safety: config.safety,
|
|
230
|
+
sources: config.sources,
|
|
231
|
+
trustedRoots: config.trustedRoots,
|
|
232
|
+
version: config.version,
|
|
233
|
+
workspace: config.workspace,
|
|
234
|
+
}),
|
|
235
|
+
);
|
|
236
|
+
const MAX_PREVIEW_CANDIDATE_BYTES = 4 * 1024 * 1024;
|
|
237
|
+
const MAX_PREVIEW_SESSION_BYTES = 16 * 1024 * 1024;
|
|
238
|
+
const MAX_PREVIEW_SESSION_RECEIPTS = 32;
|
|
239
|
+
const previewReceipts = new Map<string, PreviewReceipt>();
|
|
240
|
+
|
|
241
|
+
function previewPayloadBytes(
|
|
242
|
+
sessionId: string,
|
|
243
|
+
candidate: string,
|
|
244
|
+
operations: PreviewReceipt["normalizedOperations"],
|
|
245
|
+
): number {
|
|
246
|
+
const candidateBytes = Buffer.byteLength(candidate);
|
|
247
|
+
const payloadBytes =
|
|
248
|
+
candidateBytes + Buffer.byteLength(JSON.stringify(operations));
|
|
249
|
+
const outstanding = [...previewReceipts.values()].filter(
|
|
250
|
+
(receipt) => receipt.sessionId === sessionId,
|
|
251
|
+
);
|
|
252
|
+
const retainedBytes = outstanding.reduce(
|
|
253
|
+
(total, receipt) => total + receipt.payloadBytes,
|
|
254
|
+
0,
|
|
255
|
+
);
|
|
256
|
+
if (
|
|
257
|
+
candidateBytes > MAX_PREVIEW_CANDIDATE_BYTES ||
|
|
258
|
+
outstanding.length >= MAX_PREVIEW_SESSION_RECEIPTS ||
|
|
259
|
+
retainedBytes + payloadBytes > MAX_PREVIEW_SESSION_BYTES
|
|
260
|
+
)
|
|
261
|
+
throw Object.assign(
|
|
262
|
+
new Error(
|
|
263
|
+
"Preview receipt storage limit exceeded; narrow the patch or commit/discard outstanding previews",
|
|
264
|
+
),
|
|
265
|
+
{ code: "preview_receipt_limit", retryable: true },
|
|
266
|
+
);
|
|
267
|
+
return payloadBytes;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
async function validatePreviewReceipt(filePath: string, token: string) {
|
|
271
|
+
const receipt = previewReceipts.get(token);
|
|
272
|
+
if (!receipt)
|
|
273
|
+
throw new Error("Preview receipt is unknown or has already been used");
|
|
274
|
+
if (receipt.expiresAt < Date.now()) {
|
|
275
|
+
previewReceipts.delete(token);
|
|
276
|
+
clearTimeout(receipt.expiryTimer);
|
|
277
|
+
throw new Error("Preview receipt has expired; preview the patch again");
|
|
278
|
+
}
|
|
279
|
+
if (receipt.filePath !== filePath)
|
|
280
|
+
throw new Error("Preview receipt does not belong to this file");
|
|
281
|
+
if (receipt.sessionId !== approvalSessionId())
|
|
282
|
+
throw new Error("Preview receipt belongs to a different MCP session");
|
|
283
|
+
const config = await currentConfig();
|
|
284
|
+
if (
|
|
285
|
+
receipt.generation !== config.generation ||
|
|
286
|
+
receipt.configurationIdentity !== previewConfigurationIdentity(config)
|
|
287
|
+
)
|
|
288
|
+
throw new Error("Preview receipt is stale because configuration changed");
|
|
289
|
+
await resolveWritablePath(filePath);
|
|
290
|
+
const source = await readFile(filePath, "utf8");
|
|
291
|
+
const actual = sha256(source);
|
|
292
|
+
if (actual !== receipt.sourceSha256)
|
|
293
|
+
throw new Error(
|
|
294
|
+
`Stale file context: expected ${receipt.sourceSha256}, found ${actual}`,
|
|
295
|
+
);
|
|
296
|
+
if (sha256(receipt.candidate) !== receipt.candidateSha256)
|
|
297
|
+
throw new Error("Preview receipt candidate integrity check failed");
|
|
298
|
+
return receipt;
|
|
181
299
|
}
|
|
182
300
|
|
|
183
301
|
async function createPatchContext(
|
|
@@ -188,16 +306,17 @@ async function createPatchContext(
|
|
|
188
306
|
const actual = sha256(original);
|
|
189
307
|
await requireExpectedHash(request.expectedSha256, "file_patch");
|
|
190
308
|
verifyExpectedHash(request.expectedSha256, actual);
|
|
191
|
-
const
|
|
309
|
+
const capabilities = await inspectFileCapabilities(filePath);
|
|
192
310
|
return {
|
|
193
311
|
actual,
|
|
194
312
|
aiderBlocks: request.aiderBlocks ?? [],
|
|
195
313
|
astRules: request.astRules ?? [],
|
|
314
|
+
capabilities,
|
|
196
315
|
filePath,
|
|
197
|
-
language,
|
|
316
|
+
language: capabilities.language,
|
|
317
|
+
mode: (await lstat(filePath)).mode,
|
|
198
318
|
original,
|
|
199
319
|
request,
|
|
200
|
-
rewritable: await astRewritable(filePath, language),
|
|
201
320
|
};
|
|
202
321
|
}
|
|
203
322
|
|
|
@@ -206,259 +325,462 @@ async function patchPreview(
|
|
|
206
325
|
candidate: string,
|
|
207
326
|
metadata: PreviewMetadata,
|
|
208
327
|
) {
|
|
209
|
-
const
|
|
328
|
+
const config = await currentConfig();
|
|
329
|
+
const receipt = randomUUID();
|
|
330
|
+
const expiresAt = Date.now() + PREVIEW_RECEIPT_TTL_MS;
|
|
331
|
+
const sessionId = approvalSessionId();
|
|
332
|
+
const normalizedOperations = {
|
|
333
|
+
aiderBlocks: context.request.aiderBlocks,
|
|
334
|
+
astRules: context.request.astRules,
|
|
335
|
+
chattr: context.request.chattr,
|
|
336
|
+
patchStrategy: context.request.patchStrategy,
|
|
337
|
+
};
|
|
338
|
+
const payloadBytes = previewPayloadBytes(
|
|
339
|
+
sessionId,
|
|
340
|
+
candidate,
|
|
341
|
+
normalizedOperations,
|
|
342
|
+
);
|
|
343
|
+
const expiryTimer = setTimeout(
|
|
344
|
+
previewReceipts.delete.bind(previewReceipts, receipt),
|
|
345
|
+
PREVIEW_RECEIPT_TTL_MS,
|
|
346
|
+
);
|
|
347
|
+
expiryTimer.unref?.();
|
|
348
|
+
previewReceipts.set(receipt, {
|
|
349
|
+
candidate,
|
|
350
|
+
candidateSha256: sha256(candidate),
|
|
351
|
+
chattr: context.request.chattr,
|
|
352
|
+
configurationIdentity: previewConfigurationIdentity(config),
|
|
353
|
+
expiresAt,
|
|
354
|
+
expiryTimer,
|
|
355
|
+
filePath: context.filePath,
|
|
356
|
+
generation: config.generation,
|
|
357
|
+
metadata,
|
|
358
|
+
normalizedOperations,
|
|
359
|
+
payloadBytes,
|
|
360
|
+
sessionId,
|
|
361
|
+
sourceSha256: context.actual,
|
|
362
|
+
});
|
|
210
363
|
return {
|
|
211
|
-
changed:
|
|
212
|
-
diff: previewDiff(context.filePath, context.original,
|
|
364
|
+
changed: candidate !== context.original,
|
|
365
|
+
diff: previewDiff(context.filePath, context.original, candidate),
|
|
213
366
|
filePath: context.filePath,
|
|
214
367
|
matches: metadata.matches,
|
|
215
368
|
matchMethods: metadata.matchMethods,
|
|
216
369
|
operations: metadata.operations,
|
|
217
370
|
preview: true,
|
|
218
|
-
|
|
371
|
+
previewReceipt: receipt,
|
|
372
|
+
receiptExpiresAt: new Date(expiresAt).toISOString(),
|
|
373
|
+
sha256: sha256(candidate),
|
|
219
374
|
strategy: metadata.strategy,
|
|
220
375
|
};
|
|
221
376
|
}
|
|
222
377
|
|
|
223
|
-
function
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
);
|
|
378
|
+
async function prepareStrategyPatch(context: PatchContext) {
|
|
379
|
+
const prepared = await patchStrategyAdapter(
|
|
380
|
+
context.request.patchStrategy,
|
|
381
|
+
).prepare(context);
|
|
382
|
+
await validateStructuredCandidate(context.capabilities, prepared.candidate);
|
|
383
|
+
return { candidate: prepared.candidate, metadata: prepared.metadata };
|
|
236
384
|
}
|
|
237
385
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
async function previewAstRule(
|
|
243
|
-
context: AstPatchContext,
|
|
244
|
-
next: string,
|
|
245
|
-
rule: AstRule,
|
|
386
|
+
async function formatCandidate(
|
|
387
|
+
filePath: string,
|
|
388
|
+
candidate: string,
|
|
389
|
+
capabilities: FileCapabilities,
|
|
246
390
|
) {
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
|
|
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;
|
|
391
|
+
const formatted = await formatContent(filePath, candidate);
|
|
392
|
+
await validateStructuredCandidate(capabilities, formatted);
|
|
393
|
+
return formatted;
|
|
272
394
|
}
|
|
273
395
|
|
|
274
|
-
async function
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
},
|
|
290
|
-
path.dirname(context.filePath),
|
|
291
|
-
),
|
|
396
|
+
async function applyLockedPatch(filePath: string, request: PatchBatchRequest) {
|
|
397
|
+
if (request.previewReceipt)
|
|
398
|
+
return applyPreparedPatch(
|
|
399
|
+
filePath,
|
|
400
|
+
request,
|
|
401
|
+
await preflightPatchBatch(filePath, request),
|
|
402
|
+
);
|
|
403
|
+
const context = await createPatchContext(filePath, request);
|
|
404
|
+
const prepared = await prepareStrategyPatch(context);
|
|
405
|
+
if (request.preview)
|
|
406
|
+
return patchPreview(context, prepared.candidate, prepared.metadata);
|
|
407
|
+
const candidate = await formatCandidate(
|
|
408
|
+
filePath,
|
|
409
|
+
prepared.candidate,
|
|
410
|
+
context.capabilities,
|
|
292
411
|
);
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
412
|
+
await commit(
|
|
413
|
+
filePath,
|
|
414
|
+
candidate,
|
|
415
|
+
context.mode,
|
|
416
|
+
context.actual,
|
|
417
|
+
request.chattr,
|
|
418
|
+
true,
|
|
419
|
+
);
|
|
420
|
+
return {
|
|
421
|
+
...prepared.metadata,
|
|
422
|
+
filePath,
|
|
423
|
+
preview: false,
|
|
424
|
+
sha256: sha256(candidate),
|
|
425
|
+
};
|
|
299
426
|
}
|
|
300
427
|
|
|
301
|
-
async function
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
428
|
+
async function applyPatchBatch(
|
|
429
|
+
filePath: string,
|
|
430
|
+
request: PatchBatchRequest,
|
|
431
|
+
): Promise<Record<string, unknown>> {
|
|
432
|
+
return withFileLock(filePath, () => applyLockedPatch(filePath, request));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export async function patchFile(
|
|
436
|
+
request: PatchRequest,
|
|
437
|
+
): Promise<Record<string, unknown>> {
|
|
438
|
+
return applyPatchBatch(await resolveWritablePath(request.filePath), {
|
|
439
|
+
aiderBlocks: request.aiderBlock ? [request.aiderBlock] : undefined,
|
|
440
|
+
astRules: request.astRule ? [request.astRule] : undefined,
|
|
441
|
+
expectedSha256: request.expectedSha256,
|
|
442
|
+
patchStrategy: request.patchStrategy,
|
|
443
|
+
preview: request.preview,
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
interface PreparedPatch {
|
|
448
|
+
candidate?: string;
|
|
449
|
+
chattr?: FileChattr;
|
|
450
|
+
mode?: number;
|
|
451
|
+
previewResult?: Record<string, unknown>;
|
|
452
|
+
receiptToken?: string;
|
|
453
|
+
result?: Record<string, unknown>;
|
|
454
|
+
sourceSha256?: string;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async function preflightPatchBatch(
|
|
458
|
+
filePath: string,
|
|
459
|
+
request: PatchBatchRequest,
|
|
460
|
+
): Promise<PreparedPatch> {
|
|
461
|
+
if (request.preview)
|
|
462
|
+
return { previewResult: await applyLockedPatch(filePath, request) };
|
|
463
|
+
if (request.previewReceipt) {
|
|
464
|
+
const receipt = await validatePreviewReceipt(
|
|
465
|
+
filePath,
|
|
466
|
+
request.previewReceipt,
|
|
467
|
+
);
|
|
468
|
+
return {
|
|
469
|
+
candidate: receipt.candidate,
|
|
470
|
+
chattr: receipt.chattr,
|
|
471
|
+
mode: (await lstat(filePath)).mode,
|
|
472
|
+
receiptToken: request.previewReceipt,
|
|
473
|
+
result: {
|
|
474
|
+
filePath,
|
|
475
|
+
matches: receipt.metadata.matches,
|
|
476
|
+
matchMethods: receipt.metadata.matchMethods,
|
|
477
|
+
operations: receipt.metadata.operations,
|
|
478
|
+
preview: false,
|
|
479
|
+
receiptCommitted: true,
|
|
480
|
+
sha256: receipt.candidateSha256,
|
|
481
|
+
strategy: receipt.metadata.strategy,
|
|
482
|
+
},
|
|
483
|
+
sourceSha256: receipt.sourceSha256,
|
|
484
|
+
};
|
|
306
485
|
}
|
|
307
|
-
|
|
486
|
+
|
|
487
|
+
const context = await createPatchContext(filePath, request);
|
|
488
|
+
const prepared = await prepareStrategyPatch(context);
|
|
489
|
+
return {
|
|
490
|
+
candidate: prepared.candidate,
|
|
491
|
+
chattr: request.chattr,
|
|
492
|
+
mode: context.mode,
|
|
493
|
+
result: {
|
|
494
|
+
...prepared.metadata,
|
|
495
|
+
filePath,
|
|
496
|
+
preview: false,
|
|
497
|
+
sha256: sha256(prepared.candidate),
|
|
498
|
+
},
|
|
499
|
+
sourceSha256: context.actual,
|
|
500
|
+
};
|
|
308
501
|
}
|
|
309
502
|
|
|
310
|
-
async function
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
)
|
|
316
|
-
if (
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
503
|
+
async function applyPreparedPatch(
|
|
504
|
+
filePath: string,
|
|
505
|
+
_request: PatchBatchRequest,
|
|
506
|
+
prepared: PreparedPatch,
|
|
507
|
+
): Promise<Record<string, unknown>> {
|
|
508
|
+
if (prepared.previewResult) return prepared.previewResult;
|
|
509
|
+
if (
|
|
510
|
+
prepared.candidate === undefined ||
|
|
511
|
+
prepared.mode === undefined ||
|
|
512
|
+
!prepared.result ||
|
|
513
|
+
prepared.sourceSha256 === undefined
|
|
514
|
+
)
|
|
515
|
+
throw new Error("Patch batch preflight did not produce a commit candidate");
|
|
516
|
+
const candidate = await formatCandidate(
|
|
517
|
+
filePath,
|
|
518
|
+
prepared.candidate,
|
|
519
|
+
await inspectFileCapabilities(filePath),
|
|
520
|
+
);
|
|
521
|
+
if (prepared.receiptToken) {
|
|
522
|
+
const receipt = previewReceipts.get(prepared.receiptToken);
|
|
523
|
+
if (receipt) clearTimeout(receipt.expiryTimer);
|
|
524
|
+
previewReceipts.delete(prepared.receiptToken);
|
|
525
|
+
}
|
|
322
526
|
await commit(
|
|
323
|
-
|
|
527
|
+
filePath,
|
|
324
528
|
candidate,
|
|
325
|
-
mode,
|
|
326
|
-
|
|
327
|
-
|
|
529
|
+
prepared.mode,
|
|
530
|
+
prepared.sourceSha256,
|
|
531
|
+
prepared.chattr,
|
|
532
|
+
true,
|
|
328
533
|
);
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
strategy: "ast",
|
|
338
|
-
};
|
|
534
|
+
return { ...prepared.result, sha256: sha256(candidate) };
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
interface PreparedWrite {
|
|
538
|
+
actual?: string;
|
|
539
|
+
content: string;
|
|
540
|
+
existing: boolean;
|
|
541
|
+
mode?: number;
|
|
339
542
|
}
|
|
340
543
|
|
|
341
|
-
async function
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
544
|
+
async function preflightFileWrite(
|
|
545
|
+
filePath: string,
|
|
546
|
+
request: FileWriteRequest,
|
|
547
|
+
): Promise<PreparedWrite> {
|
|
548
|
+
validateFileChattr(request.chattr);
|
|
549
|
+
const existing = await readFile(filePath, "utf8").catch(() => undefined);
|
|
550
|
+
let actual: string | undefined;
|
|
551
|
+
let mode: number | undefined;
|
|
552
|
+
if (existing !== undefined) {
|
|
553
|
+
await requireExpectedHash(request.expectedSha256, "file_write overwrite");
|
|
554
|
+
actual = sha256(existing);
|
|
555
|
+
verifyExpectedHash(request.expectedSha256, actual);
|
|
556
|
+
const capabilities = await inspectFileCapabilities(filePath);
|
|
557
|
+
if (capabilities.effective.patch.includes("ast"))
|
|
558
|
+
throw new Error(
|
|
559
|
+
"REJECTED: structurally rewritable existing files require file_patch with patchStrategy 'ast'",
|
|
560
|
+
);
|
|
561
|
+
mode = (await lstat(filePath)).mode;
|
|
562
|
+
}
|
|
563
|
+
const parentExists = await lstat(path.dirname(filePath)).then(
|
|
564
|
+
() => true,
|
|
565
|
+
() => false,
|
|
566
|
+
);
|
|
567
|
+
const createdDirectories = parentExists ? [] : await ensureParent(filePath);
|
|
345
568
|
try {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
const candidate = await readFile(next, "utf8");
|
|
353
|
-
return await astPatchResult(
|
|
354
|
-
context,
|
|
355
|
-
candidate,
|
|
356
|
-
metadata.mode,
|
|
357
|
-
totalMatches,
|
|
358
|
-
);
|
|
569
|
+
return {
|
|
570
|
+
actual,
|
|
571
|
+
content: await formatContent(filePath, request.content),
|
|
572
|
+
existing: existing !== undefined,
|
|
573
|
+
mode,
|
|
574
|
+
};
|
|
359
575
|
} finally {
|
|
360
|
-
await
|
|
576
|
+
await removeCreatedDirectories(createdDirectories);
|
|
361
577
|
}
|
|
362
578
|
}
|
|
363
579
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
580
|
+
interface ResolvedBatchEntry<T> {
|
|
581
|
+
inputPath: string;
|
|
582
|
+
request: T;
|
|
583
|
+
resolvedPath: string;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
type BatchFileSnapshot =
|
|
587
|
+
| undefined
|
|
588
|
+
| {
|
|
589
|
+
chattr: Awaited<ReturnType<typeof resultingFileChattr>>;
|
|
590
|
+
content: string;
|
|
591
|
+
mode: number;
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
interface CommittedBatchEntry {
|
|
595
|
+
resolvedPath: string;
|
|
596
|
+
result: Record<string, unknown>;
|
|
373
597
|
}
|
|
374
598
|
|
|
375
|
-
function
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
methods.push(result.method);
|
|
599
|
+
async function optionalContent(filePath: string): Promise<string | undefined> {
|
|
600
|
+
try {
|
|
601
|
+
return await readFile(filePath, "utf8");
|
|
602
|
+
} catch {
|
|
603
|
+
return undefined;
|
|
381
604
|
}
|
|
382
|
-
return { content, methods };
|
|
383
605
|
}
|
|
384
606
|
|
|
385
|
-
async function
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
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
|
-
};
|
|
607
|
+
async function captureBatchSnapshots<T>(
|
|
608
|
+
entries: ResolvedBatchEntry<T>[],
|
|
609
|
+
): Promise<Map<string, BatchFileSnapshot>> {
|
|
610
|
+
const snapshots = new Map<string, BatchFileSnapshot>();
|
|
611
|
+
for (const { resolvedPath } of entries) {
|
|
612
|
+
const content = await optionalContent(resolvedPath);
|
|
613
|
+
snapshots.set(
|
|
614
|
+
resolvedPath,
|
|
615
|
+
content === undefined
|
|
616
|
+
? undefined
|
|
617
|
+
: {
|
|
618
|
+
chattr: await resultingFileChattr(resolvedPath),
|
|
619
|
+
content,
|
|
620
|
+
mode: (await lstat(resolvedPath)).mode,
|
|
621
|
+
},
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
return snapshots;
|
|
413
625
|
}
|
|
414
626
|
|
|
415
|
-
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
627
|
+
function discardPreparedPreviews<Prepared>(prepared: Prepared[]): void {
|
|
628
|
+
for (const item of prepared) {
|
|
629
|
+
const token = (item as PreparedPatch).previewResult?.previewReceipt;
|
|
630
|
+
if (typeof token !== "string") continue;
|
|
631
|
+
const receipt = previewReceipts.get(token);
|
|
632
|
+
if (receipt) clearTimeout(receipt.expiryTimer);
|
|
633
|
+
previewReceipts.delete(token);
|
|
634
|
+
}
|
|
420
635
|
}
|
|
421
636
|
|
|
422
|
-
async function
|
|
423
|
-
|
|
424
|
-
request:
|
|
425
|
-
): Promise<
|
|
426
|
-
const
|
|
427
|
-
|
|
637
|
+
async function prepareFileBatch<T, Prepared>(
|
|
638
|
+
entries: ResolvedBatchEntry<T>[],
|
|
639
|
+
preflight: (filePath: string, request: T) => Promise<Prepared>,
|
|
640
|
+
): Promise<Prepared[]> {
|
|
641
|
+
const prepared: Prepared[] = [];
|
|
642
|
+
try {
|
|
643
|
+
for (const { request, resolvedPath } of entries)
|
|
644
|
+
prepared.push(await preflight(resolvedPath, request));
|
|
645
|
+
return prepared;
|
|
646
|
+
} catch (error) {
|
|
647
|
+
discardPreparedPreviews(prepared);
|
|
648
|
+
throw error;
|
|
649
|
+
}
|
|
428
650
|
}
|
|
429
651
|
|
|
430
|
-
|
|
431
|
-
|
|
652
|
+
async function rollbackFileBatch(
|
|
653
|
+
committed: CommittedBatchEntry[],
|
|
654
|
+
snapshots: Map<string, BatchFileSnapshot>,
|
|
655
|
+
): Promise<void> {
|
|
656
|
+
for (const { resolvedPath, result } of committed.reverse()) {
|
|
657
|
+
const snapshot = snapshots.get(resolvedPath);
|
|
658
|
+
if (snapshot) {
|
|
659
|
+
await ignoreFailure(
|
|
660
|
+
commit(
|
|
661
|
+
resolvedPath,
|
|
662
|
+
snapshot.content,
|
|
663
|
+
snapshot.mode,
|
|
664
|
+
undefined,
|
|
665
|
+
snapshot.chattr,
|
|
666
|
+
true,
|
|
667
|
+
),
|
|
668
|
+
);
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
await ignoreFailure(unlink(resolvedPath));
|
|
672
|
+
const createdDirectories = result.createdDirectories;
|
|
673
|
+
if (Array.isArray(createdDirectories))
|
|
674
|
+
await removeCreatedDirectories(
|
|
675
|
+
createdDirectories.filter(
|
|
676
|
+
(value): value is string => typeof value === "string",
|
|
677
|
+
),
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
async function executePreparedFileBatch<T, Prepared>(
|
|
683
|
+
entries: ResolvedBatchEntry<T>[],
|
|
684
|
+
handler: (
|
|
685
|
+
filePath: string,
|
|
686
|
+
request: T,
|
|
687
|
+
prepared: Prepared,
|
|
688
|
+
) => Promise<Record<string, unknown>>,
|
|
689
|
+
preflight: (filePath: string, request: T) => Promise<Prepared>,
|
|
690
|
+
mutates: (request: T) => boolean,
|
|
432
691
|
): Promise<Record<string, unknown>> {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
692
|
+
const snapshots = await captureBatchSnapshots(entries);
|
|
693
|
+
const prepared = await prepareFileBatch(entries, preflight);
|
|
694
|
+
const files: Record<string, unknown> = {};
|
|
695
|
+
const committed: CommittedBatchEntry[] = [];
|
|
696
|
+
try {
|
|
697
|
+
for (const [
|
|
698
|
+
index,
|
|
699
|
+
{ inputPath, request, resolvedPath },
|
|
700
|
+
] of entries.entries()) {
|
|
701
|
+
const result = await handler(
|
|
702
|
+
resolvedPath,
|
|
703
|
+
request,
|
|
704
|
+
prepared[index] as Prepared,
|
|
705
|
+
);
|
|
706
|
+
files[inputPath] = result;
|
|
707
|
+
if (mutates(request)) committed.push({ resolvedPath, result });
|
|
708
|
+
}
|
|
709
|
+
return { files };
|
|
710
|
+
} catch (error) {
|
|
711
|
+
discardPreparedPreviews(prepared);
|
|
712
|
+
await rollbackFileBatch(committed, snapshots);
|
|
713
|
+
throw error;
|
|
714
|
+
}
|
|
440
715
|
}
|
|
441
716
|
|
|
442
|
-
async function processFileBatch<T>(
|
|
717
|
+
async function processFileBatch<T, Prepared>(
|
|
443
718
|
tool: "file_patch" | "file_write",
|
|
444
719
|
requests: Record<string, T>,
|
|
445
|
-
handler: (
|
|
720
|
+
handler: (
|
|
721
|
+
filePath: string,
|
|
722
|
+
request: T,
|
|
723
|
+
prepared: Prepared,
|
|
724
|
+
) => Promise<Record<string, unknown>>,
|
|
725
|
+
preflight: (filePath: string, request: T) => Promise<Prepared>,
|
|
726
|
+
mutates: (request: T) => boolean,
|
|
446
727
|
): Promise<Record<string, unknown>> {
|
|
447
728
|
const entries = Object.entries(requests);
|
|
448
729
|
if (entries.length < 1 || entries.length > FILE_READ_MAX_BATCH)
|
|
449
730
|
throw new Error(
|
|
450
731
|
`${tool} requires between 1 and ${FILE_READ_MAX_BATCH} files`,
|
|
451
732
|
);
|
|
452
|
-
const
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
733
|
+
const resolvedEntries = await Promise.all(
|
|
734
|
+
entries.map(async ([inputPath, request]) => ({
|
|
735
|
+
inputPath,
|
|
736
|
+
request,
|
|
737
|
+
resolvedPath: await resolveWritablePath(inputPath),
|
|
738
|
+
})),
|
|
739
|
+
);
|
|
740
|
+
return withFileLocks(
|
|
741
|
+
resolvedEntries.map(({ resolvedPath }) => resolvedPath),
|
|
742
|
+
() =>
|
|
743
|
+
executePreparedFileBatch(resolvedEntries, handler, preflight, mutates),
|
|
744
|
+
);
|
|
456
745
|
}
|
|
457
746
|
|
|
458
747
|
export async function patchFiles(
|
|
459
748
|
requests: PatchBatch,
|
|
460
749
|
): Promise<Record<string, unknown>> {
|
|
461
|
-
return processFileBatch(
|
|
750
|
+
return processFileBatch(
|
|
751
|
+
"file_patch",
|
|
752
|
+
requests,
|
|
753
|
+
applyPreparedPatch,
|
|
754
|
+
preflightPatchBatch,
|
|
755
|
+
(request) => request.preview !== true,
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
async function writeFileResolved(
|
|
760
|
+
args: {
|
|
761
|
+
chattr?: FileChattr;
|
|
762
|
+
filePath: string;
|
|
763
|
+
content: string;
|
|
764
|
+
expectedSha256?: string;
|
|
765
|
+
},
|
|
766
|
+
prepared?: PreparedWrite,
|
|
767
|
+
): Promise<Record<string, unknown>> {
|
|
768
|
+
const candidate = prepared ?? (await preflightFileWrite(args.filePath, args));
|
|
769
|
+
const committed = await commit(
|
|
770
|
+
args.filePath,
|
|
771
|
+
candidate.content,
|
|
772
|
+
candidate.mode,
|
|
773
|
+
candidate.actual,
|
|
774
|
+
args.chattr,
|
|
775
|
+
true,
|
|
776
|
+
);
|
|
777
|
+
return {
|
|
778
|
+
chattr: committed.chattr,
|
|
779
|
+
created: !candidate.existing,
|
|
780
|
+
createdDirectories: committed.createdDirectories,
|
|
781
|
+
filePath: args.filePath,
|
|
782
|
+
sha256: committed.sha256,
|
|
783
|
+
};
|
|
462
784
|
}
|
|
463
785
|
|
|
464
786
|
export async function writeFileSafely(args: {
|
|
@@ -468,53 +790,26 @@ export async function writeFileSafely(args: {
|
|
|
468
790
|
expectedSha256?: string;
|
|
469
791
|
}): Promise<Record<string, unknown>> {
|
|
470
792
|
const filePath = await resolveWritablePath(args.filePath);
|
|
471
|
-
return withFileLock(filePath,
|
|
472
|
-
const existing = await readFile(filePath, "utf8").catch(() => undefined);
|
|
473
|
-
let createdDirectories: string[];
|
|
474
|
-
if (existing !== undefined) {
|
|
475
|
-
await requireExpectedHash(args.expectedSha256, "file_write overwrite");
|
|
476
|
-
const actual = sha256(existing);
|
|
477
|
-
verifyExpectedHash(args.expectedSha256, actual);
|
|
478
|
-
const language = detectAstLanguage(filePath);
|
|
479
|
-
if (await astRewritable(filePath, language))
|
|
480
|
-
throw new Error(
|
|
481
|
-
"REJECTED: structurally rewritable existing files require file_patch with patchStrategy 'ast'",
|
|
482
|
-
);
|
|
483
|
-
createdDirectories = await commit(
|
|
484
|
-
filePath,
|
|
485
|
-
args.content,
|
|
486
|
-
(await lstat(filePath)).mode,
|
|
487
|
-
actual,
|
|
488
|
-
args.chattr,
|
|
489
|
-
);
|
|
490
|
-
} else
|
|
491
|
-
createdDirectories = await commit(
|
|
492
|
-
filePath,
|
|
493
|
-
args.content,
|
|
494
|
-
undefined,
|
|
495
|
-
undefined,
|
|
496
|
-
args.chattr,
|
|
497
|
-
);
|
|
498
|
-
const updated = await readFile(filePath, "utf8");
|
|
499
|
-
return {
|
|
500
|
-
chattr: await resultingFileChattr(filePath),
|
|
501
|
-
created: existing === undefined,
|
|
502
|
-
createdDirectories,
|
|
503
|
-
filePath,
|
|
504
|
-
sha256: sha256(updated),
|
|
505
|
-
};
|
|
506
|
-
});
|
|
793
|
+
return withFileLock(filePath, () => writeFileResolved({ ...args, filePath }));
|
|
507
794
|
}
|
|
508
795
|
|
|
509
796
|
export async function writeFilesSafely(
|
|
510
797
|
requests: FileWriteBatch,
|
|
511
798
|
): Promise<Record<string, unknown>> {
|
|
512
|
-
return processFileBatch(
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
799
|
+
return processFileBatch(
|
|
800
|
+
"file_write",
|
|
801
|
+
requests,
|
|
802
|
+
(filePath, request, prepared) =>
|
|
803
|
+
writeFileResolved(
|
|
804
|
+
{
|
|
805
|
+
chattr: request.chattr,
|
|
806
|
+
content: request.content,
|
|
807
|
+
expectedSha256: request.expectedSha256,
|
|
808
|
+
filePath,
|
|
809
|
+
},
|
|
810
|
+
prepared,
|
|
811
|
+
),
|
|
812
|
+
preflightFileWrite,
|
|
813
|
+
() => true,
|
|
519
814
|
);
|
|
520
815
|
}
|