@mwillbanks/ast-mcp 0.6.4 → 0.6.6
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 +325 -324
- package/dist/ast-mcp.js.map +82 -26
- package/package.json +2 -2
- package/src/intelligence/mutation/journal.ts +42 -2
- package/src/intelligence/mutation/service.ts +614 -46
- package/src/intelligence/mutation/types.ts +2 -0
- package/src/patch/engine.ts +69 -20
- package/src/runtime/attributes.ts +161 -26
- package/src/runtime/file-delete.ts +32 -26
- package/src/runtime/file-read.ts +34 -11
- package/src/runtime/file-rename.ts +15 -10
- package/src/runtime/hash.ts +42 -1
- package/src/runtime/reference-target-index.ts +35 -0
- package/src/runtime/snapshot-blob.ts +733 -0
- package/src/tools/lifecycle.ts +9 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SnapshotBlob } from "../../runtime/snapshot-blob.ts";
|
|
1
2
|
import type { WorkspaceHandle } from "../workspace/context.ts";
|
|
2
3
|
|
|
3
4
|
export type MutationTool =
|
|
@@ -11,6 +12,7 @@ export interface MutationFilePlan {
|
|
|
11
12
|
candidateSha256: string | null;
|
|
12
13
|
filePath: string;
|
|
13
14
|
sourceContentBase64?: string | null;
|
|
15
|
+
sourceBlob?: SnapshotBlob | null;
|
|
14
16
|
sourceGid?: number | null;
|
|
15
17
|
sourceMode?: number | null;
|
|
16
18
|
sourceSha256: string | null;
|
package/src/patch/engine.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
chmod,
|
|
5
|
+
lstat,
|
|
6
|
+
open,
|
|
7
|
+
type FileHandle,
|
|
8
|
+
rename,
|
|
9
|
+
unlink,
|
|
10
|
+
writeFile,
|
|
11
|
+
} from "node:fs/promises";
|
|
4
12
|
import path from "node:path";
|
|
5
13
|
|
|
6
14
|
import { currentConfig } from "../config";
|
|
@@ -42,6 +50,10 @@ import {
|
|
|
42
50
|
} from "../runtime/parent-directories";
|
|
43
51
|
import { resolveWritablePath } from "../runtime/paths";
|
|
44
52
|
import { requireExpectedHash, verifyExpectedHash } from "../runtime/policy";
|
|
53
|
+
import {
|
|
54
|
+
FileSnapshotCollection,
|
|
55
|
+
stageFileSnapshot,
|
|
56
|
+
} from "../runtime/snapshot-blob";
|
|
45
57
|
import {
|
|
46
58
|
type AiderBlock,
|
|
47
59
|
type AstRule,
|
|
@@ -106,7 +118,9 @@ function temporary(filePath: string): string {
|
|
|
106
118
|
async function removeTemporary(filePath: string) {
|
|
107
119
|
try {
|
|
108
120
|
await unlink(filePath);
|
|
109
|
-
} catch {
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
123
|
+
}
|
|
110
124
|
}
|
|
111
125
|
|
|
112
126
|
async function ignoreFailure(operation: Promise<unknown>): Promise<void> {
|
|
@@ -131,7 +145,9 @@ async function commit(
|
|
|
131
145
|
const next = temporary(filePath);
|
|
132
146
|
const createdDirectories: string[] = [];
|
|
133
147
|
let committed = false;
|
|
134
|
-
let
|
|
148
|
+
let output: FileHandle | undefined;
|
|
149
|
+
let temporaryCreated = false;
|
|
150
|
+
const primaryFailures: unknown[] = [];
|
|
135
151
|
try {
|
|
136
152
|
await ensureParentDirectories(filePath, createdDirectories, beforeReplace);
|
|
137
153
|
const formatted = alreadyFormatted
|
|
@@ -139,12 +155,17 @@ async function commit(
|
|
|
139
155
|
: await formatContent(filePath, content);
|
|
140
156
|
await beforeReplace?.();
|
|
141
157
|
await revalidateWritePath(filePath);
|
|
142
|
-
await
|
|
158
|
+
output = await open(next, "wx", mode);
|
|
159
|
+
temporaryCreated = true;
|
|
160
|
+
await output.writeFile(formatted, { encoding: "utf8" });
|
|
143
161
|
if (mode !== undefined) {
|
|
144
162
|
await beforeReplace?.();
|
|
145
163
|
await chmod(next, mode);
|
|
146
164
|
}
|
|
147
165
|
await applyFileChattr(next, chattr, beforeReplace);
|
|
166
|
+
await output.sync();
|
|
167
|
+
await output.close();
|
|
168
|
+
output = undefined;
|
|
148
169
|
const committedChattr = await resultingFileChattr(next);
|
|
149
170
|
const committedSha256 = sha256(formatted);
|
|
150
171
|
if (expectedSha256) {
|
|
@@ -162,25 +183,40 @@ async function commit(
|
|
|
162
183
|
sha256: committedSha256,
|
|
163
184
|
};
|
|
164
185
|
} catch (error) {
|
|
165
|
-
|
|
186
|
+
primaryFailures.push(error);
|
|
166
187
|
throw error;
|
|
167
188
|
} finally {
|
|
168
|
-
|
|
189
|
+
const failures = [...primaryFailures];
|
|
190
|
+
const affectedPaths = new Set<string>();
|
|
191
|
+
try {
|
|
192
|
+
await output?.close();
|
|
193
|
+
} catch (error) {
|
|
194
|
+
failures.push(error);
|
|
195
|
+
affectedPaths.add(next);
|
|
196
|
+
}
|
|
197
|
+
try {
|
|
198
|
+
if (temporaryCreated) await removeTemporary(next);
|
|
199
|
+
} catch (error) {
|
|
200
|
+
failures.push(error);
|
|
201
|
+
affectedPaths.add(next);
|
|
202
|
+
}
|
|
169
203
|
if (!committed) {
|
|
170
204
|
try {
|
|
171
205
|
await removeCreatedDirectories(createdDirectories);
|
|
172
|
-
} catch (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
"File write failed and parent cleanup was incomplete",
|
|
177
|
-
),
|
|
178
|
-
{
|
|
179
|
-
details: { affectedPaths: createdDirectories },
|
|
180
|
-
},
|
|
181
|
-
);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
failures.push(error);
|
|
208
|
+
for (const directory of createdDirectories)
|
|
209
|
+
affectedPaths.add(directory);
|
|
182
210
|
}
|
|
183
211
|
}
|
|
212
|
+
if (failures.length > primaryFailures.length)
|
|
213
|
+
throw Object.assign(
|
|
214
|
+
new AggregateError(
|
|
215
|
+
failures,
|
|
216
|
+
"File write failed and cleanup was incomplete",
|
|
217
|
+
),
|
|
218
|
+
{ details: { affectedPaths: [...affectedPaths] } },
|
|
219
|
+
);
|
|
184
220
|
}
|
|
185
221
|
}
|
|
186
222
|
function previewDiff(filePath: string, before: string, after: string): string {
|
|
@@ -907,21 +943,28 @@ async function executeReservedFileBatch<T, Prepared>(
|
|
|
907
943
|
receiptState: ReceiptBatchState,
|
|
908
944
|
): Promise<Record<string, unknown>> {
|
|
909
945
|
signal?.throwIfAborted();
|
|
946
|
+
await using durableSnapshots = new FileSnapshotCollection();
|
|
910
947
|
const snapshots = await captureBatchSnapshots(entries);
|
|
911
948
|
const prepared = await prepareFileBatch(entries, preflight);
|
|
912
949
|
const files: Record<string, unknown> = {};
|
|
913
950
|
const committed: CommittedBatchEntry[] = [];
|
|
914
951
|
const workspace = currentWorkspace() ?? null;
|
|
915
952
|
const plannedEntries = entries.filter((entry) => mutates(entry.request));
|
|
953
|
+
for (const entry of plannedEntries) {
|
|
954
|
+
const snapshot = snapshots.get(entry.resolvedPath);
|
|
955
|
+
if (!snapshot) continue;
|
|
956
|
+
const durable = await stageFileSnapshot(entry.resolvedPath, signal);
|
|
957
|
+
durableSnapshots.set(entry.resolvedPath, durable);
|
|
958
|
+
verifyExpectedHash(sha256(snapshot.content), durable.sha256);
|
|
959
|
+
}
|
|
916
960
|
const sourcePlan = plannedEntries.map((entry) => {
|
|
917
961
|
const entryIndex = entries.indexOf(entry);
|
|
918
962
|
const snapshot = snapshots.get(entry.resolvedPath);
|
|
919
963
|
return {
|
|
920
964
|
candidateSha256: preparedCandidateSha256(prepared[entryIndex]),
|
|
921
965
|
filePath: entry.resolvedPath,
|
|
922
|
-
sourceContentBase64:
|
|
923
|
-
|
|
924
|
-
: null,
|
|
966
|
+
sourceContentBase64: null,
|
|
967
|
+
sourceBlob: durableSnapshots.get(entry.resolvedPath)?.sourceBlob ?? null,
|
|
925
968
|
sourceGid:
|
|
926
969
|
process.platform === "win32"
|
|
927
970
|
? null
|
|
@@ -947,6 +990,7 @@ async function executeReservedFileBatch<T, Prepared>(
|
|
|
947
990
|
signal?.throwIfAborted();
|
|
948
991
|
if (plannedEntries.length > 0) {
|
|
949
992
|
receiptState.safeToRelease = false;
|
|
993
|
+
durableSnapshots.disposable = false;
|
|
950
994
|
await beginMutation({
|
|
951
995
|
files: sourcePlan,
|
|
952
996
|
operationId,
|
|
@@ -954,6 +998,7 @@ async function executeReservedFileBatch<T, Prepared>(
|
|
|
954
998
|
workspace,
|
|
955
999
|
});
|
|
956
1000
|
began = true;
|
|
1001
|
+
await durableSnapshots.verify(signal);
|
|
957
1002
|
}
|
|
958
1003
|
for (const [
|
|
959
1004
|
index,
|
|
@@ -990,6 +1035,7 @@ async function executeReservedFileBatch<T, Prepared>(
|
|
|
990
1035
|
began && commits.length > 0
|
|
991
1036
|
? await completeMutation(operationId, commits)
|
|
992
1037
|
: null;
|
|
1038
|
+
durableSnapshots.disposable = true;
|
|
993
1039
|
receiptState.completed = true;
|
|
994
1040
|
return intelligenceRefresh ? { files, intelligenceRefresh } : { files };
|
|
995
1041
|
} catch (error) {
|
|
@@ -999,7 +1045,10 @@ async function executeReservedFileBatch<T, Prepared>(
|
|
|
999
1045
|
await patchSignals.run(undefined, () =>
|
|
1000
1046
|
rollbackFileBatch(committed, snapshots, fence),
|
|
1001
1047
|
);
|
|
1002
|
-
if (began)
|
|
1048
|
+
if (began) {
|
|
1049
|
+
await recordMutationRollback(operationId);
|
|
1050
|
+
durableSnapshots.disposable = true;
|
|
1051
|
+
}
|
|
1003
1052
|
// A failed begin can leave an uncertain journal even without file commits.
|
|
1004
1053
|
receiptState.safeToRelease = began || receiptState.safeToRelease;
|
|
1005
1054
|
} catch (rollbackError) {
|
|
@@ -2,7 +2,12 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
import { constants } from "node:fs";
|
|
3
3
|
import { chmod, chown, lstat, open, type FileHandle } from "node:fs/promises";
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
stageFileSnapshot,
|
|
7
|
+
removeSnapshotBlob,
|
|
8
|
+
FileSnapshotCollection,
|
|
9
|
+
closeSnapshotHandles,
|
|
10
|
+
} from "./snapshot-blob";
|
|
6
11
|
|
|
7
12
|
export interface FileChattrGuard {
|
|
8
13
|
dev: number;
|
|
@@ -10,6 +15,14 @@ export interface FileChattrGuard {
|
|
|
10
15
|
sha256: string;
|
|
11
16
|
signal?: AbortSignal;
|
|
12
17
|
beforeMutation?: () => void;
|
|
18
|
+
handle?: FileHandle;
|
|
19
|
+
durability?: FileChattrDurability;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface FileChattrDurability {
|
|
23
|
+
handle?: FileHandle;
|
|
24
|
+
dirty: boolean;
|
|
25
|
+
cleanupHandles?: Set<FileHandle>;
|
|
13
26
|
}
|
|
14
27
|
|
|
15
28
|
function changedFile(filePath: string) {
|
|
@@ -23,28 +36,59 @@ export async function readFileChattrSnapshot(
|
|
|
23
36
|
filePath: string,
|
|
24
37
|
signal?: AbortSignal,
|
|
25
38
|
) {
|
|
26
|
-
signal
|
|
27
|
-
const handle = await open(
|
|
28
|
-
filePath,
|
|
29
|
-
constants.O_RDONLY | constants.O_NONBLOCK | constants.O_NOFOLLOW,
|
|
30
|
-
);
|
|
39
|
+
const snapshot = await stageFileSnapshot(filePath, signal);
|
|
31
40
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
+
);
|
|
36
50
|
return {
|
|
51
|
+
...snapshot,
|
|
52
|
+
handle,
|
|
53
|
+
durability: {
|
|
54
|
+
handle: process.platform !== "win32" || writable ? handle : undefined,
|
|
55
|
+
dirty: false,
|
|
56
|
+
} as FileChattrDurability,
|
|
37
57
|
attributes: {
|
|
38
|
-
chmod:
|
|
39
|
-
chown: { gid:
|
|
58
|
+
chmod: snapshot.mode & 0o777,
|
|
59
|
+
chown: { gid: snapshot.gid, uid: snapshot.uid },
|
|
40
60
|
},
|
|
41
|
-
content,
|
|
42
|
-
dev: metadata.dev,
|
|
43
|
-
ino: metadata.ino,
|
|
44
|
-
sha256: sha256(content),
|
|
45
61
|
};
|
|
46
|
-
}
|
|
47
|
-
|
|
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
|
+
}
|
|
48
92
|
}
|
|
49
93
|
}
|
|
50
94
|
|
|
@@ -95,10 +139,23 @@ async function applyGuardedChattr(
|
|
|
95
139
|
guard: FileChattrGuard,
|
|
96
140
|
) {
|
|
97
141
|
const validated = validateFileChattr(chattr);
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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[] = [];
|
|
102
159
|
try {
|
|
103
160
|
await verifyChattrHandle(handle, filePath, guard);
|
|
104
161
|
const before = await handle.stat();
|
|
@@ -111,32 +168,90 @@ async function applyGuardedChattr(
|
|
|
111
168
|
rollback ? { ...guard, signal: undefined } : guard,
|
|
112
169
|
);
|
|
113
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
|
+
};
|
|
114
208
|
try {
|
|
209
|
+
if (!durability.handle && (before.mode & 0o200) !== 0)
|
|
210
|
+
await acquireFlushHandle();
|
|
115
211
|
if (validated?.chown) {
|
|
116
212
|
await check();
|
|
117
213
|
guard.beforeMutation?.();
|
|
214
|
+
durability.dirty = true;
|
|
118
215
|
await handle.chown(validated.chown.uid, validated.chown.gid);
|
|
119
216
|
changed = true;
|
|
120
217
|
}
|
|
121
|
-
if (
|
|
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
|
+
) {
|
|
122
228
|
await check();
|
|
123
229
|
guard.beforeMutation?.();
|
|
230
|
+
durability.dirty = true;
|
|
124
231
|
await handle.chmod(validated.chmod);
|
|
125
232
|
changed = true;
|
|
126
233
|
}
|
|
127
234
|
await verifyChattrHandle(handle, filePath, guard);
|
|
235
|
+
await flush();
|
|
128
236
|
} catch (error) {
|
|
129
|
-
if (changed) {
|
|
237
|
+
if (changed || durability.dirty) {
|
|
238
|
+
const acquisitionErrors: unknown[] = [];
|
|
130
239
|
try {
|
|
131
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
|
+
);
|
|
132
245
|
if (validated?.chown && process.platform !== "win32")
|
|
133
246
|
await handle.chown(before.uid, before.gid);
|
|
134
247
|
await check(true);
|
|
248
|
+
durability.dirty = true;
|
|
135
249
|
await handle.chmod(before.mode & 0o777);
|
|
250
|
+
await flush(true);
|
|
136
251
|
} catch (rollbackError) {
|
|
137
252
|
throw Object.assign(
|
|
138
253
|
new AggregateError(
|
|
139
|
-
[error, rollbackError],
|
|
254
|
+
[error, ...acquisitionErrors, rollbackError],
|
|
140
255
|
"File attribute mutation failed and rollback was incomplete",
|
|
141
256
|
),
|
|
142
257
|
{ details: { affectedPaths: [filePath] } },
|
|
@@ -150,8 +265,28 @@ async function applyGuardedChattr(
|
|
|
150
265
|
chmod: after.mode & 0o777,
|
|
151
266
|
chown: { gid: after.gid, uid: after.uid },
|
|
152
267
|
};
|
|
268
|
+
} catch (error) {
|
|
269
|
+
errors.push(error);
|
|
270
|
+
throw error;
|
|
153
271
|
} finally {
|
|
154
|
-
|
|
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
|
+
}
|
|
155
290
|
}
|
|
156
291
|
}
|
|
157
292
|
|
|
@@ -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,6 +54,7 @@ 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;
|
|
52
60
|
directories: Map<string, Dirent[]>;
|
|
@@ -168,20 +176,11 @@ async function importersFor(
|
|
|
168
176
|
.map((entry) => path.resolve(path.dirname(candidate), entry.source));
|
|
169
177
|
verification.imports.set(candidate, bases);
|
|
170
178
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
base === target ||
|
|
177
|
-
`${base}${path.extname(target)}` === target ||
|
|
178
|
-
path.join(base, `index${path.extname(target)}`) === target,
|
|
179
|
-
)
|
|
180
|
-
) {
|
|
181
|
-
const matches = importers.get(target) ?? [];
|
|
182
|
-
matches.push(path.relative(root, candidate));
|
|
183
|
-
importers.set(target, matches);
|
|
184
|
-
}
|
|
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);
|
|
185
184
|
}
|
|
186
185
|
}
|
|
187
186
|
} catch (error) {
|
|
@@ -357,6 +356,7 @@ export async function deleteFilesSafely(
|
|
|
357
356
|
roots: new Map(),
|
|
358
357
|
pool,
|
|
359
358
|
targets: new Set<string>(),
|
|
359
|
+
targetIndex: new Map<string, string[]>(),
|
|
360
360
|
candidates: 0,
|
|
361
361
|
sourceBytes: 0,
|
|
362
362
|
signal: AbortSignal.any([
|
|
@@ -370,6 +370,7 @@ export async function deleteFilesSafely(
|
|
|
370
370
|
verification.signal.throwIfAborted();
|
|
371
371
|
verification.targets.add(await resolveWritablePath(inputPath, "delete"));
|
|
372
372
|
}
|
|
373
|
+
verification.targetIndex = createReferenceTargetIndex(verification.targets);
|
|
373
374
|
for (const [inputPath, request] of Object.entries(requests)) {
|
|
374
375
|
verification.signal.throwIfAborted();
|
|
375
376
|
entries.push(await prepareDeleteEntry(inputPath, request, verification));
|
|
@@ -398,15 +399,12 @@ export async function deleteFilesSafely(
|
|
|
398
399
|
const fence = async () => {
|
|
399
400
|
for (const lease of leases) await lease.fence();
|
|
400
401
|
};
|
|
401
|
-
|
|
402
|
-
string,
|
|
403
|
-
Awaited<ReturnType<typeof readFileSnapshot>>
|
|
404
|
-
>();
|
|
402
|
+
await using snapshots = new FileSnapshotCollection();
|
|
405
403
|
for (const { filePath, request } of entries) {
|
|
406
|
-
const snapshot = await readFileSnapshot(filePath, signal);
|
|
407
404
|
await requireExpectedHash(request.expectedSha256, "file_delete");
|
|
408
|
-
|
|
405
|
+
const snapshot = await stageFileSnapshot(filePath, signal);
|
|
409
406
|
snapshots.set(filePath, snapshot);
|
|
407
|
+
verifyExpectedHash(request.expectedSha256, snapshot.sha256);
|
|
410
408
|
}
|
|
411
409
|
const workspace = currentWorkspace() ?? null;
|
|
412
410
|
const sourcePlan = entries.map(({ filePath }) => {
|
|
@@ -414,7 +412,8 @@ export async function deleteFilesSafely(
|
|
|
414
412
|
return {
|
|
415
413
|
candidateSha256: null,
|
|
416
414
|
filePath,
|
|
417
|
-
sourceContentBase64:
|
|
415
|
+
sourceContentBase64: null,
|
|
416
|
+
sourceBlob: snapshot?.sourceBlob ?? null,
|
|
418
417
|
sourceGid:
|
|
419
418
|
process.platform === "win32" ? null : (snapshot?.gid ?? null),
|
|
420
419
|
sourceMode: snapshot?.mode ?? null,
|
|
@@ -439,6 +438,7 @@ export async function deleteFilesSafely(
|
|
|
439
438
|
let began = false;
|
|
440
439
|
try {
|
|
441
440
|
signal?.throwIfAborted();
|
|
441
|
+
snapshots.disposable = false;
|
|
442
442
|
await beginMutation({
|
|
443
443
|
files: sourcePlan,
|
|
444
444
|
operationId,
|
|
@@ -446,6 +446,7 @@ export async function deleteFilesSafely(
|
|
|
446
446
|
workspace,
|
|
447
447
|
});
|
|
448
448
|
began = true;
|
|
449
|
+
await snapshots.verify(signal);
|
|
449
450
|
const files: Record<string, unknown> = {};
|
|
450
451
|
for (const {
|
|
451
452
|
filePath,
|
|
@@ -483,6 +484,7 @@ export async function deleteFilesSafely(
|
|
|
483
484
|
operationId,
|
|
484
485
|
commits,
|
|
485
486
|
);
|
|
487
|
+
snapshots.disposable = true;
|
|
486
488
|
return {
|
|
487
489
|
files,
|
|
488
490
|
...(intelligenceRefresh ? { intelligenceRefresh } : {}),
|
|
@@ -496,7 +498,7 @@ export async function deleteFilesSafely(
|
|
|
496
498
|
if (!snapshot) continue;
|
|
497
499
|
try {
|
|
498
500
|
await fence();
|
|
499
|
-
await
|
|
501
|
+
await restoreSnapshotBlob(snapshot, commit.filePath, fence);
|
|
500
502
|
if (process.platform !== "win32") {
|
|
501
503
|
await fence();
|
|
502
504
|
await chown(commit.filePath, snapshot.uid, snapshot.gid);
|
|
@@ -527,8 +529,12 @@ export async function deleteFilesSafely(
|
|
|
527
529
|
);
|
|
528
530
|
}
|
|
529
531
|
}
|
|
530
|
-
if (began && rollbackErrors.length === 0)
|
|
531
|
-
|
|
532
|
+
if (began && rollbackErrors.length === 0) {
|
|
533
|
+
try {
|
|
534
|
+
await recordMutationRollback(operationId);
|
|
535
|
+
snapshots.disposable = true;
|
|
536
|
+
} catch {}
|
|
537
|
+
}
|
|
532
538
|
if (rollbackErrors.length > 0)
|
|
533
539
|
throw Object.assign(
|
|
534
540
|
new AggregateError(
|