@mwillbanks/ast-mcp 0.6.3 → 0.6.5
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/dist/ast-mcp.js +331 -330
- package/dist/ast-mcp.js.map +87 -30
- package/dist/workers/code-intelligence-worker.js +2 -2
- package/dist/workers/code-intelligence-worker.js.map +4 -4
- package/package.json +2 -2
- package/src/helpers/mcp-schema.ts +91 -8
- package/src/intelligence/mutation/journal.ts +143 -22
- package/src/intelligence/mutation/locks.ts +92 -28
- package/src/intelligence/mutation/service.ts +614 -46
- package/src/intelligence/mutation/types.ts +2 -0
- package/src/patch/engine.ts +213 -85
- package/src/runtime/attributes.ts +293 -1
- package/src/runtime/file-delete.ts +130 -78
- package/src/runtime/file-read.ts +34 -11
- package/src/runtime/file-rename.ts +141 -30
- package/src/runtime/hash.ts +42 -1
- package/src/runtime/parent-directories.ts +114 -0
- package/src/runtime/reference-target-index.ts +35 -0
- package/src/runtime/snapshot-blob.ts +733 -0
- package/src/tools/code-intelligence-compute.ts +14 -6
- package/src/tools/code-intelligence-worker.ts +11 -2
- package/src/tools/code-intelligence.ts +126 -40
- package/src/tools/files.ts +2 -2
- package/src/tools/lifecycle.ts +37 -34
- package/templates/AGENTS.md +1 -0
- package/templates/skills/ast-mcp/SKILL.md +2 -0
- package/templates/skills/ast-mcp/references/agents-guidance.md +1 -0
- package/templates/skills/ast-mcp/references/patch-state-machine.md +8 -2
- package/templates/skills/ast-mcp/references/tool-catalog.md +4 -1
|
@@ -1,4 +1,294 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { chmod, chown, lstat, open, type FileHandle } from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
stageFileSnapshot,
|
|
7
|
+
removeSnapshotBlob,
|
|
8
|
+
FileSnapshotCollection,
|
|
9
|
+
closeSnapshotHandles,
|
|
10
|
+
} from "./snapshot-blob";
|
|
11
|
+
|
|
12
|
+
export interface FileChattrGuard {
|
|
13
|
+
dev: number;
|
|
14
|
+
ino: number;
|
|
15
|
+
sha256: string;
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
beforeMutation?: () => void;
|
|
18
|
+
handle?: FileHandle;
|
|
19
|
+
durability?: FileChattrDurability;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface FileChattrDurability {
|
|
23
|
+
handle?: FileHandle;
|
|
24
|
+
dirty: boolean;
|
|
25
|
+
cleanupHandles?: Set<FileHandle>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function changedFile(filePath: string) {
|
|
29
|
+
return Object.assign(new Error("File changed before attribute mutation"), {
|
|
30
|
+
code: "stale_or_missing_hash",
|
|
31
|
+
details: { affectedPaths: [filePath] },
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function readFileChattrSnapshot(
|
|
36
|
+
filePath: string,
|
|
37
|
+
signal?: AbortSignal,
|
|
38
|
+
) {
|
|
39
|
+
const snapshot = await stageFileSnapshot(filePath, signal);
|
|
40
|
+
try {
|
|
41
|
+
// Retain read-only Windows identity until an authorized chmod enables write access.
|
|
42
|
+
const writable =
|
|
43
|
+
process.platform === "win32" && (snapshot.mode & 0o200) !== 0;
|
|
44
|
+
const handle = await open(
|
|
45
|
+
filePath,
|
|
46
|
+
(writable ? constants.O_RDWR : constants.O_RDONLY) |
|
|
47
|
+
constants.O_NONBLOCK |
|
|
48
|
+
constants.O_NOFOLLOW,
|
|
49
|
+
);
|
|
50
|
+
return {
|
|
51
|
+
...snapshot,
|
|
52
|
+
handle,
|
|
53
|
+
durability: {
|
|
54
|
+
handle: process.platform !== "win32" || writable ? handle : undefined,
|
|
55
|
+
dirty: false,
|
|
56
|
+
} as FileChattrDurability,
|
|
57
|
+
attributes: {
|
|
58
|
+
chmod: snapshot.mode & 0o777,
|
|
59
|
+
chown: { gid: snapshot.gid, uid: snapshot.uid },
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
} catch (error) {
|
|
63
|
+
try {
|
|
64
|
+
await removeSnapshotBlob(snapshot.sourceBlob, snapshot.namespace);
|
|
65
|
+
} catch (cleanupError) {
|
|
66
|
+
throw new AggregateError(
|
|
67
|
+
[error, cleanupError],
|
|
68
|
+
"Attribute snapshot open and cleanup failed",
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export class FileChattrSnapshots extends FileSnapshotCollection<
|
|
76
|
+
Awaited<ReturnType<typeof readFileChattrSnapshot>>
|
|
77
|
+
> {
|
|
78
|
+
override async [Symbol.asyncDispose](): Promise<void> {
|
|
79
|
+
try {
|
|
80
|
+
await closeSnapshotHandles(
|
|
81
|
+
new Set(
|
|
82
|
+
[...this.values()].flatMap((snapshot) => [
|
|
83
|
+
snapshot.handle,
|
|
84
|
+
...(snapshot.durability.handle ? [snapshot.durability.handle] : []),
|
|
85
|
+
...(snapshot.durability.cleanupHandles ?? []),
|
|
86
|
+
]),
|
|
87
|
+
),
|
|
88
|
+
);
|
|
89
|
+
} finally {
|
|
90
|
+
await super[Symbol.asyncDispose]();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function verifyChattrHandle(
|
|
96
|
+
handle: FileHandle,
|
|
97
|
+
filePath: string,
|
|
98
|
+
guard: FileChattrGuard,
|
|
99
|
+
) {
|
|
100
|
+
guard.signal?.throwIfAborted();
|
|
101
|
+
const metadata = await handle.stat();
|
|
102
|
+
const current = await lstat(filePath);
|
|
103
|
+
if (
|
|
104
|
+
!metadata.isFile() ||
|
|
105
|
+
current.isSymbolicLink() ||
|
|
106
|
+
metadata.dev !== guard.dev ||
|
|
107
|
+
metadata.ino !== guard.ino ||
|
|
108
|
+
current.dev !== guard.dev ||
|
|
109
|
+
current.ino !== guard.ino
|
|
110
|
+
)
|
|
111
|
+
throw changedFile(filePath);
|
|
112
|
+
const hash = createHash("sha256");
|
|
113
|
+
const buffer = Buffer.alloc(64 * 1024);
|
|
114
|
+
let position = 0;
|
|
115
|
+
for (;;) {
|
|
116
|
+
guard.signal?.throwIfAborted();
|
|
117
|
+
const { bytesRead } = await handle.read(
|
|
118
|
+
buffer,
|
|
119
|
+
0,
|
|
120
|
+
Math.min(buffer.length, metadata.size - position + 1),
|
|
121
|
+
position,
|
|
122
|
+
);
|
|
123
|
+
if (position + bytesRead > metadata.size) throw changedFile(filePath);
|
|
124
|
+
if (!bytesRead) break;
|
|
125
|
+
hash.update(buffer.subarray(0, bytesRead));
|
|
126
|
+
position += bytesRead;
|
|
127
|
+
}
|
|
128
|
+
if (hash.digest("hex") !== guard.sha256) throw changedFile(filePath);
|
|
129
|
+
const after = await lstat(filePath);
|
|
130
|
+
if (after.dev !== guard.dev || after.ino !== guard.ino)
|
|
131
|
+
throw changedFile(filePath);
|
|
132
|
+
guard.signal?.throwIfAborted();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async function applyGuardedChattr(
|
|
136
|
+
filePath: string,
|
|
137
|
+
chattr: FileChattr | undefined,
|
|
138
|
+
beforeChange: (() => Promise<void>) | undefined,
|
|
139
|
+
guard: FileChattrGuard,
|
|
140
|
+
) {
|
|
141
|
+
const validated = validateFileChattr(chattr);
|
|
142
|
+
const writable =
|
|
143
|
+
process.platform === "win32" &&
|
|
144
|
+
!guard.handle &&
|
|
145
|
+
((await lstat(filePath)).mode & 0o200) !== 0;
|
|
146
|
+
const handle =
|
|
147
|
+
guard.handle ??
|
|
148
|
+
(await open(
|
|
149
|
+
filePath,
|
|
150
|
+
(writable ? constants.O_RDWR : constants.O_RDONLY) |
|
|
151
|
+
constants.O_NONBLOCK |
|
|
152
|
+
constants.O_NOFOLLOW,
|
|
153
|
+
));
|
|
154
|
+
const durability: FileChattrDurability = guard.durability ?? {
|
|
155
|
+
handle: process.platform !== "win32" || writable ? handle : undefined,
|
|
156
|
+
dirty: false,
|
|
157
|
+
};
|
|
158
|
+
const errors: unknown[] = [];
|
|
159
|
+
try {
|
|
160
|
+
await verifyChattrHandle(handle, filePath, guard);
|
|
161
|
+
const before = await handle.stat();
|
|
162
|
+
let changed = false;
|
|
163
|
+
const check = async (rollback = false) => {
|
|
164
|
+
await beforeChange?.();
|
|
165
|
+
await verifyChattrHandle(
|
|
166
|
+
handle,
|
|
167
|
+
filePath,
|
|
168
|
+
rollback ? { ...guard, signal: undefined } : guard,
|
|
169
|
+
);
|
|
170
|
+
};
|
|
171
|
+
const acquireFlushHandle = async (rollback = false) => {
|
|
172
|
+
if (durability.handle) return;
|
|
173
|
+
await check(rollback);
|
|
174
|
+
const candidate = await open(
|
|
175
|
+
filePath,
|
|
176
|
+
constants.O_RDWR | constants.O_NONBLOCK | constants.O_NOFOLLOW,
|
|
177
|
+
);
|
|
178
|
+
try {
|
|
179
|
+
await verifyChattrHandle(
|
|
180
|
+
candidate,
|
|
181
|
+
filePath,
|
|
182
|
+
rollback ? { ...guard, signal: undefined } : guard,
|
|
183
|
+
);
|
|
184
|
+
await check(rollback);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
try {
|
|
187
|
+
await candidate.close();
|
|
188
|
+
} catch (closeError) {
|
|
189
|
+
(durability.cleanupHandles ??= new Set()).add(candidate);
|
|
190
|
+
throw Object.assign(
|
|
191
|
+
new AggregateError(
|
|
192
|
+
[error, closeError],
|
|
193
|
+
"File attribute descriptor verification and cleanup failed",
|
|
194
|
+
),
|
|
195
|
+
{ details: { affectedPaths: [filePath] } },
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
durability.handle = candidate;
|
|
201
|
+
};
|
|
202
|
+
const flush = async (rollback = false) => {
|
|
203
|
+
if (!durability.dirty) return;
|
|
204
|
+
await acquireFlushHandle(rollback);
|
|
205
|
+
await durability.handle!.sync();
|
|
206
|
+
durability.dirty = false;
|
|
207
|
+
};
|
|
208
|
+
try {
|
|
209
|
+
if (!durability.handle && (before.mode & 0o200) !== 0)
|
|
210
|
+
await acquireFlushHandle();
|
|
211
|
+
if (validated?.chown) {
|
|
212
|
+
await check();
|
|
213
|
+
guard.beforeMutation?.();
|
|
214
|
+
durability.dirty = true;
|
|
215
|
+
await handle.chown(validated.chown.uid, validated.chown.gid);
|
|
216
|
+
changed = true;
|
|
217
|
+
}
|
|
218
|
+
if (
|
|
219
|
+
validated?.chmod !== undefined &&
|
|
220
|
+
!(
|
|
221
|
+
process.platform === "win32" &&
|
|
222
|
+
!durability.handle &&
|
|
223
|
+
!durability.dirty &&
|
|
224
|
+
(before.mode & 0o200) === 0 &&
|
|
225
|
+
(validated.chmod & 0o200) === 0
|
|
226
|
+
)
|
|
227
|
+
) {
|
|
228
|
+
await check();
|
|
229
|
+
guard.beforeMutation?.();
|
|
230
|
+
durability.dirty = true;
|
|
231
|
+
await handle.chmod(validated.chmod);
|
|
232
|
+
changed = true;
|
|
233
|
+
}
|
|
234
|
+
await verifyChattrHandle(handle, filePath, guard);
|
|
235
|
+
await flush();
|
|
236
|
+
} catch (error) {
|
|
237
|
+
if (changed || durability.dirty) {
|
|
238
|
+
const acquisitionErrors: unknown[] = [];
|
|
239
|
+
try {
|
|
240
|
+
await check(true);
|
|
241
|
+
if (!durability.handle && ((await handle.stat()).mode & 0o200) !== 0)
|
|
242
|
+
await acquireFlushHandle(true).catch((failure) =>
|
|
243
|
+
acquisitionErrors.push(failure),
|
|
244
|
+
);
|
|
245
|
+
if (validated?.chown && process.platform !== "win32")
|
|
246
|
+
await handle.chown(before.uid, before.gid);
|
|
247
|
+
await check(true);
|
|
248
|
+
durability.dirty = true;
|
|
249
|
+
await handle.chmod(before.mode & 0o777);
|
|
250
|
+
await flush(true);
|
|
251
|
+
} catch (rollbackError) {
|
|
252
|
+
throw Object.assign(
|
|
253
|
+
new AggregateError(
|
|
254
|
+
[error, ...acquisitionErrors, rollbackError],
|
|
255
|
+
"File attribute mutation failed and rollback was incomplete",
|
|
256
|
+
),
|
|
257
|
+
{ details: { affectedPaths: [filePath] } },
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
throw error;
|
|
262
|
+
}
|
|
263
|
+
const after = await handle.stat();
|
|
264
|
+
return {
|
|
265
|
+
chmod: after.mode & 0o777,
|
|
266
|
+
chown: { gid: after.gid, uid: after.uid },
|
|
267
|
+
};
|
|
268
|
+
} catch (error) {
|
|
269
|
+
errors.push(error);
|
|
270
|
+
throw error;
|
|
271
|
+
} finally {
|
|
272
|
+
const owned = new Set<FileHandle>();
|
|
273
|
+
if (!guard.handle) owned.add(handle);
|
|
274
|
+
if (!guard.durability) {
|
|
275
|
+
if (durability.handle && durability.handle !== guard.handle)
|
|
276
|
+
owned.add(durability.handle);
|
|
277
|
+
for (const pending of durability.cleanupHandles ?? []) owned.add(pending);
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
await closeSnapshotHandles(owned);
|
|
281
|
+
} catch (cleanupError) {
|
|
282
|
+
throw Object.assign(
|
|
283
|
+
new AggregateError(
|
|
284
|
+
[...errors, cleanupError],
|
|
285
|
+
"File attribute mutation and descriptor cleanup failed",
|
|
286
|
+
),
|
|
287
|
+
{ details: { affectedPaths: [filePath] } },
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
2
292
|
|
|
3
293
|
export interface FileChattr {
|
|
4
294
|
chmod?: number;
|
|
@@ -44,7 +334,9 @@ export async function applyFileChattr(
|
|
|
44
334
|
filePath: string,
|
|
45
335
|
chattr: FileChattr | undefined,
|
|
46
336
|
beforeChange?: () => Promise<void>,
|
|
337
|
+
guard?: FileChattrGuard,
|
|
47
338
|
) {
|
|
339
|
+
if (guard) return applyGuardedChattr(filePath, chattr, beforeChange, guard);
|
|
48
340
|
const validated = validateFileChattr(chattr);
|
|
49
341
|
const before = await resultingFileChattr(filePath);
|
|
50
342
|
if (!validated) return before;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { createReadStream } from "node:fs";
|
|
2
|
+
import { createReadStream, type Dirent } from "node:fs";
|
|
3
3
|
import {
|
|
4
4
|
chmod,
|
|
5
5
|
chown,
|
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
readdir,
|
|
9
9
|
rm,
|
|
10
10
|
rmdir,
|
|
11
|
-
writeFile,
|
|
12
11
|
} from "node:fs/promises";
|
|
13
12
|
import path from "node:path";
|
|
14
13
|
|
|
@@ -24,7 +23,6 @@ import {
|
|
|
24
23
|
import { ParserWorkerPool } from "../intelligence/parser/index.ts";
|
|
25
24
|
import { currentWorkspace } from "../intelligence/workspace/context.ts";
|
|
26
25
|
import { detectAstLanguage } from "../patch/languages";
|
|
27
|
-
import { readFileSnapshot } from "./file-snapshot";
|
|
28
26
|
import { sha256File } from "./hash";
|
|
29
27
|
import { withFileLocks } from "./locks";
|
|
30
28
|
import { assertReadableTree } from "./path-policy";
|
|
@@ -35,6 +33,15 @@ import {
|
|
|
35
33
|
resolveWritablePath,
|
|
36
34
|
} from "./paths";
|
|
37
35
|
import { requireExpectedHash, verifyExpectedHash } from "./policy";
|
|
36
|
+
import {
|
|
37
|
+
createReferenceTargetIndex,
|
|
38
|
+
referencedTargets,
|
|
39
|
+
} from "./reference-target-index";
|
|
40
|
+
import {
|
|
41
|
+
stageFileSnapshot,
|
|
42
|
+
restoreSnapshotBlob,
|
|
43
|
+
FileSnapshotCollection,
|
|
44
|
+
} from "./snapshot-blob";
|
|
38
45
|
|
|
39
46
|
export interface FileDeleteRequest {
|
|
40
47
|
expectedSha256?: string;
|
|
@@ -47,8 +54,12 @@ interface ReferenceVerification {
|
|
|
47
54
|
pool: ParserWorkerPool;
|
|
48
55
|
signal: AbortSignal;
|
|
49
56
|
targets: Set<string>;
|
|
57
|
+
targetIndex: Map<string, string[]>;
|
|
50
58
|
candidates: number;
|
|
51
59
|
sourceBytes: number;
|
|
60
|
+
directories: Map<string, Dirent[]>;
|
|
61
|
+
imports: Map<string, string[]>;
|
|
62
|
+
roots: Map<string, Map<string, string[]>>;
|
|
52
63
|
}
|
|
53
64
|
|
|
54
65
|
function referenceFailure(message: string, cause?: unknown): Error {
|
|
@@ -68,12 +79,23 @@ async function* referenceCandidates(
|
|
|
68
79
|
const pending = [root];
|
|
69
80
|
while (pending.length) {
|
|
70
81
|
signal.throwIfAborted();
|
|
71
|
-
|
|
82
|
+
const directory = pending.pop()!;
|
|
83
|
+
// Cache inventories, including overlapping roots, within this batch only.
|
|
84
|
+
let entries = verification.directories.get(directory);
|
|
85
|
+
if (!entries) {
|
|
86
|
+
entries = [];
|
|
87
|
+
for await (const entry of await opendir(directory)) {
|
|
88
|
+
signal.throwIfAborted();
|
|
89
|
+
if (++verification.candidates > 100_000)
|
|
90
|
+
throw referenceFailure(
|
|
91
|
+
"Reference verification exceeds the inventory limit",
|
|
92
|
+
);
|
|
93
|
+
entries.push(entry);
|
|
94
|
+
}
|
|
95
|
+
verification.directories.set(directory, entries);
|
|
96
|
+
}
|
|
97
|
+
for (const entry of entries) {
|
|
72
98
|
signal.throwIfAborted();
|
|
73
|
-
if (++verification.candidates > 100_000)
|
|
74
|
-
throw referenceFailure(
|
|
75
|
-
"Reference verification exceeds the inventory limit",
|
|
76
|
-
);
|
|
77
99
|
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
78
100
|
const candidate = path.join(entry.parentPath, entry.name);
|
|
79
101
|
if (entry.isDirectory()) pending.push(candidate);
|
|
@@ -87,11 +109,12 @@ async function importersFor(
|
|
|
87
109
|
root: string,
|
|
88
110
|
verification: ReferenceVerification,
|
|
89
111
|
): Promise<string[]> {
|
|
90
|
-
const
|
|
91
|
-
|
|
112
|
+
const cached = verification.roots.get(root);
|
|
113
|
+
if (cached) return cached.get(filePath) ?? [];
|
|
114
|
+
const importers = new Map<string, string[]>();
|
|
92
115
|
try {
|
|
93
116
|
for await (const candidate of referenceCandidates(root, verification)) {
|
|
94
|
-
if (
|
|
117
|
+
if (verification.targets.has(candidate)) continue;
|
|
95
118
|
const language = detectAstLanguage(candidate);
|
|
96
119
|
if (
|
|
97
120
|
!language ||
|
|
@@ -100,60 +123,65 @@ async function importersFor(
|
|
|
100
123
|
)
|
|
101
124
|
)
|
|
102
125
|
continue;
|
|
103
|
-
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
126
|
+
let bases = verification.imports.get(candidate);
|
|
127
|
+
if (!bases) {
|
|
128
|
+
const file = Bun.file(candidate);
|
|
129
|
+
if (file.size > 2 * 1024 * 1024)
|
|
130
|
+
throw referenceFailure(
|
|
131
|
+
"Reference verification source exceeds the byte limit",
|
|
132
|
+
);
|
|
133
|
+
verification.signal.throwIfAborted();
|
|
134
|
+
const stream = createReadStream(candidate, {
|
|
135
|
+
end: 2 * 1024 * 1024,
|
|
136
|
+
signal: verification.signal,
|
|
137
|
+
});
|
|
138
|
+
const chunks: Buffer[] = [];
|
|
139
|
+
let bytes: Buffer;
|
|
140
|
+
try {
|
|
141
|
+
for await (const chunk of stream) {
|
|
142
|
+
verification.signal.throwIfAborted();
|
|
143
|
+
chunks.push(Buffer.from(chunk));
|
|
144
|
+
}
|
|
117
145
|
verification.signal.throwIfAborted();
|
|
118
|
-
|
|
146
|
+
bytes = Buffer.concat(chunks);
|
|
147
|
+
} finally {
|
|
148
|
+
stream.destroy();
|
|
119
149
|
}
|
|
120
|
-
verification.
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
{
|
|
136
|
-
languageId: language,
|
|
137
|
-
source,
|
|
138
|
-
},
|
|
139
|
-
{ signal: verification.signal, timeoutMs: 1_000 },
|
|
140
|
-
);
|
|
141
|
-
if (
|
|
142
|
-
facts.diagnostics.some((diagnostic) => diagnostic.severity === "error")
|
|
143
|
-
)
|
|
144
|
-
throw referenceFailure(
|
|
145
|
-
"Reference verification encountered invalid source",
|
|
146
|
-
);
|
|
147
|
-
const referenced = facts.imports.some((entry) => {
|
|
148
|
-
if (!entry.source.startsWith(".")) return false;
|
|
149
|
-
const base = path.resolve(path.dirname(candidate), entry.source);
|
|
150
|
-
return (
|
|
151
|
-
base === target ||
|
|
152
|
-
`${base}${path.extname(target)}` === target ||
|
|
153
|
-
path.join(base, `index${path.extname(target)}`) === target
|
|
150
|
+
verification.sourceBytes += bytes.byteLength;
|
|
151
|
+
if (
|
|
152
|
+
bytes.byteLength > 2 * 1024 * 1024 ||
|
|
153
|
+
verification.sourceBytes > 16 * 1024 * 1024
|
|
154
|
+
)
|
|
155
|
+
throw referenceFailure(
|
|
156
|
+
"Reference verification exceeds the source byte limit",
|
|
157
|
+
);
|
|
158
|
+
const source = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
159
|
+
const facts = await verification.pool.parse(
|
|
160
|
+
{
|
|
161
|
+
languageId: language,
|
|
162
|
+
source,
|
|
163
|
+
},
|
|
164
|
+
{ signal: verification.signal, timeoutMs: 1_000 },
|
|
154
165
|
);
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
if (
|
|
167
|
+
facts.diagnostics.some(
|
|
168
|
+
(diagnostic) => diagnostic.severity === "error",
|
|
169
|
+
)
|
|
170
|
+
)
|
|
171
|
+
throw referenceFailure(
|
|
172
|
+
"Reference verification encountered invalid source",
|
|
173
|
+
);
|
|
174
|
+
bases = facts.imports
|
|
175
|
+
.filter((entry) => entry.source.startsWith("."))
|
|
176
|
+
.map((entry) => path.resolve(path.dirname(candidate), entry.source));
|
|
177
|
+
verification.imports.set(candidate, bases);
|
|
178
|
+
}
|
|
179
|
+
verification.signal.throwIfAborted();
|
|
180
|
+
for (const target of referencedTargets(verification.targetIndex, bases)) {
|
|
181
|
+
const matches = importers.get(target) ?? [];
|
|
182
|
+
matches.push(path.relative(root, candidate));
|
|
183
|
+
importers.set(target, matches);
|
|
184
|
+
}
|
|
157
185
|
}
|
|
158
186
|
} catch (error) {
|
|
159
187
|
throw referenceFailure(
|
|
@@ -161,7 +189,9 @@ async function importersFor(
|
|
|
161
189
|
error,
|
|
162
190
|
);
|
|
163
191
|
}
|
|
164
|
-
|
|
192
|
+
for (const matches of importers.values()) matches.sort();
|
|
193
|
+
verification.roots.set(root, importers);
|
|
194
|
+
return importers.get(filePath) ?? [];
|
|
165
195
|
}
|
|
166
196
|
|
|
167
197
|
function ancestorPaths(filePath: string, root: string): string[] {
|
|
@@ -320,9 +350,13 @@ export async function deleteFilesSafely(
|
|
|
320
350
|
signal?: AbortSignal,
|
|
321
351
|
) {
|
|
322
352
|
const pool = new ParserWorkerPool({ minWorkers: 0, maxWorkers: 1 });
|
|
323
|
-
const verification = {
|
|
353
|
+
const verification: ReferenceVerification = {
|
|
354
|
+
directories: new Map(),
|
|
355
|
+
imports: new Map(),
|
|
356
|
+
roots: new Map(),
|
|
324
357
|
pool,
|
|
325
358
|
targets: new Set<string>(),
|
|
359
|
+
targetIndex: new Map<string, string[]>(),
|
|
326
360
|
candidates: 0,
|
|
327
361
|
sourceBytes: 0,
|
|
328
362
|
signal: AbortSignal.any([
|
|
@@ -336,6 +370,7 @@ export async function deleteFilesSafely(
|
|
|
336
370
|
verification.signal.throwIfAborted();
|
|
337
371
|
verification.targets.add(await resolveWritablePath(inputPath, "delete"));
|
|
338
372
|
}
|
|
373
|
+
verification.targetIndex = createReferenceTargetIndex(verification.targets);
|
|
339
374
|
for (const [inputPath, request] of Object.entries(requests)) {
|
|
340
375
|
verification.signal.throwIfAborted();
|
|
341
376
|
entries.push(await prepareDeleteEntry(inputPath, request, verification));
|
|
@@ -364,15 +399,12 @@ export async function deleteFilesSafely(
|
|
|
364
399
|
const fence = async () => {
|
|
365
400
|
for (const lease of leases) await lease.fence();
|
|
366
401
|
};
|
|
367
|
-
|
|
368
|
-
string,
|
|
369
|
-
Awaited<ReturnType<typeof readFileSnapshot>>
|
|
370
|
-
>();
|
|
402
|
+
await using snapshots = new FileSnapshotCollection();
|
|
371
403
|
for (const { filePath, request } of entries) {
|
|
372
|
-
const snapshot = await readFileSnapshot(filePath, signal);
|
|
373
404
|
await requireExpectedHash(request.expectedSha256, "file_delete");
|
|
374
|
-
|
|
405
|
+
const snapshot = await stageFileSnapshot(filePath, signal);
|
|
375
406
|
snapshots.set(filePath, snapshot);
|
|
407
|
+
verifyExpectedHash(request.expectedSha256, snapshot.sha256);
|
|
376
408
|
}
|
|
377
409
|
const workspace = currentWorkspace() ?? null;
|
|
378
410
|
const sourcePlan = entries.map(({ filePath }) => {
|
|
@@ -380,7 +412,8 @@ export async function deleteFilesSafely(
|
|
|
380
412
|
return {
|
|
381
413
|
candidateSha256: null,
|
|
382
414
|
filePath,
|
|
383
|
-
sourceContentBase64:
|
|
415
|
+
sourceContentBase64: null,
|
|
416
|
+
sourceBlob: snapshot?.sourceBlob ?? null,
|
|
384
417
|
sourceGid:
|
|
385
418
|
process.platform === "win32" ? null : (snapshot?.gid ?? null),
|
|
386
419
|
sourceMode: snapshot?.mode ?? null,
|
|
@@ -405,6 +438,7 @@ export async function deleteFilesSafely(
|
|
|
405
438
|
let began = false;
|
|
406
439
|
try {
|
|
407
440
|
signal?.throwIfAborted();
|
|
441
|
+
snapshots.disposable = false;
|
|
408
442
|
await beginMutation({
|
|
409
443
|
files: sourcePlan,
|
|
410
444
|
operationId,
|
|
@@ -412,6 +446,7 @@ export async function deleteFilesSafely(
|
|
|
412
446
|
workspace,
|
|
413
447
|
});
|
|
414
448
|
began = true;
|
|
449
|
+
await snapshots.verify(signal);
|
|
415
450
|
const files: Record<string, unknown> = {};
|
|
416
451
|
for (const {
|
|
417
452
|
filePath,
|
|
@@ -449,6 +484,7 @@ export async function deleteFilesSafely(
|
|
|
449
484
|
operationId,
|
|
450
485
|
commits,
|
|
451
486
|
);
|
|
487
|
+
snapshots.disposable = true;
|
|
452
488
|
return {
|
|
453
489
|
files,
|
|
454
490
|
...(intelligenceRefresh ? { intelligenceRefresh } : {}),
|
|
@@ -456,12 +492,13 @@ export async function deleteFilesSafely(
|
|
|
456
492
|
} catch (error) {
|
|
457
493
|
if (began) await ignoreFailure(failMutation(operationId, error));
|
|
458
494
|
const rollbackErrors: unknown[] = [];
|
|
495
|
+
const affectedPaths: string[] = [];
|
|
459
496
|
for (const commit of [...commits].reverse()) {
|
|
460
497
|
const snapshot = snapshots.get(commit.filePath);
|
|
461
498
|
if (!snapshot) continue;
|
|
462
499
|
try {
|
|
463
500
|
await fence();
|
|
464
|
-
await
|
|
501
|
+
await restoreSnapshotBlob(snapshot, commit.filePath, fence);
|
|
465
502
|
if (process.platform !== "win32") {
|
|
466
503
|
await fence();
|
|
467
504
|
await chown(commit.filePath, snapshot.uid, snapshot.gid);
|
|
@@ -481,15 +518,30 @@ export async function deleteFilesSafely(
|
|
|
481
518
|
)
|
|
482
519
|
throw new Error("file_delete_rollback_verification_failed");
|
|
483
520
|
} catch (rollbackError) {
|
|
484
|
-
|
|
521
|
+
affectedPaths.push(commit.filePath);
|
|
522
|
+
rollbackErrors.push(
|
|
523
|
+
Object.assign(
|
|
524
|
+
new Error("Delete rollback failed", { cause: rollbackError }),
|
|
525
|
+
{
|
|
526
|
+
details: { affectedPaths: [commit.filePath] },
|
|
527
|
+
},
|
|
528
|
+
),
|
|
529
|
+
);
|
|
485
530
|
}
|
|
486
531
|
}
|
|
487
|
-
if (began && rollbackErrors.length === 0)
|
|
488
|
-
|
|
532
|
+
if (began && rollbackErrors.length === 0) {
|
|
533
|
+
try {
|
|
534
|
+
await recordMutationRollback(operationId);
|
|
535
|
+
snapshots.disposable = true;
|
|
536
|
+
} catch {}
|
|
537
|
+
}
|
|
489
538
|
if (rollbackErrors.length > 0)
|
|
490
|
-
throw
|
|
491
|
-
|
|
492
|
-
|
|
539
|
+
throw Object.assign(
|
|
540
|
+
new AggregateError(
|
|
541
|
+
[error, ...rollbackErrors],
|
|
542
|
+
"file_delete failed and rollback requires recovery",
|
|
543
|
+
),
|
|
544
|
+
{ details: { affectedPaths } },
|
|
493
545
|
);
|
|
494
546
|
throw error;
|
|
495
547
|
}
|