@davideasden/pi-undo 0.2.0 → 0.2.1
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 +147 -85
- package/extensions/pi-undo.ts +4 -1
- package/package.json +1 -1
- package/src/atomic-fs.ts +7 -2
- package/src/controller.ts +89 -35
- package/src/mutation-journal.ts +180 -40
- package/src/pi-runtime.ts +2 -2
- package/src/quarantine.ts +357 -17
- package/src/restore-engine.ts +269 -49
- package/src/snapshot-store.ts +316 -63
- package/src/status-reporter.ts +15 -2
package/src/mutation-journal.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { open, readFile } from "node:fs/promises";
|
|
1
|
+
import { open, readFile, stat } from "node:fs/promises";
|
|
2
2
|
import { dirname } from "node:path";
|
|
3
3
|
|
|
4
4
|
import { fsyncDirectory } from "./atomic-fs.ts";
|
|
@@ -14,6 +14,24 @@ export interface MutationIntent {
|
|
|
14
14
|
readonly targetFingerprint: string;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export interface MutationAdvance {
|
|
18
|
+
readonly ordinal: number;
|
|
19
|
+
readonly states: readonly MutationState[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface JournalRecords {
|
|
23
|
+
readonly latest: readonly MutationRecord[];
|
|
24
|
+
readonly tail: MutationRecord | undefined;
|
|
25
|
+
readonly durableEnd: number;
|
|
26
|
+
readonly hasNonDurableTail: boolean;
|
|
27
|
+
readonly fileExisted: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface CachedJournalRecords {
|
|
31
|
+
readonly records: JournalRecords;
|
|
32
|
+
readonly fingerprint: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
17
35
|
const stateOrder: readonly MutationState[] = [
|
|
18
36
|
"INTENT",
|
|
19
37
|
"SOURCE_QUARANTINED",
|
|
@@ -27,6 +45,7 @@ export class MutationJournal {
|
|
|
27
45
|
private readonly path: string;
|
|
28
46
|
private readonly opId: string;
|
|
29
47
|
private mutationQueue: Promise<void> = Promise.resolve();
|
|
48
|
+
private cachedRecords: CachedJournalRecords | undefined;
|
|
30
49
|
|
|
31
50
|
constructor(path: string, opId: string) {
|
|
32
51
|
this.path = path;
|
|
@@ -46,33 +65,53 @@ export class MutationJournal {
|
|
|
46
65
|
}
|
|
47
66
|
|
|
48
67
|
begin(intent: MutationIntent): Promise<MutationRecord> {
|
|
49
|
-
return this.enqueueMutation(() => this.
|
|
68
|
+
return this.enqueueMutation(async () => (await this.beginManyMutation([intent]))[0]!);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
beginMany(intents: readonly MutationIntent[]): Promise<readonly MutationRecord[]> {
|
|
72
|
+
return this.enqueueMutation(() => this.beginManyMutation(intents));
|
|
50
73
|
}
|
|
51
74
|
|
|
52
|
-
private async
|
|
75
|
+
private async beginManyMutation(intents: readonly MutationIntent[]): Promise<readonly MutationRecord[]> {
|
|
76
|
+
if (intents.length === 0) throw new Error("mutation 批量 intent 不能为空");
|
|
53
77
|
const current = await this.readRecords();
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
78
|
+
let tail = current.tail;
|
|
79
|
+
const records: MutationRecord[] = [];
|
|
80
|
+
for (let index = 0; index < intents.length; index += 1) {
|
|
81
|
+
const intent = intents[index]!;
|
|
82
|
+
const content = {
|
|
83
|
+
schemaVersion: 1 as const,
|
|
84
|
+
opId: this.opId,
|
|
85
|
+
ordinal: current.latest.length + index + 1,
|
|
86
|
+
state: "INTENT" as const,
|
|
87
|
+
kind: intent.kind,
|
|
88
|
+
path: intent.path,
|
|
89
|
+
sourceArtifact: intent.sourceArtifact,
|
|
90
|
+
targetArtifact: intent.targetArtifact,
|
|
91
|
+
sourceFingerprint: intent.sourceFingerprint,
|
|
92
|
+
targetFingerprint: intent.targetFingerprint,
|
|
93
|
+
previousChecksum: tail?.checksum ?? null,
|
|
94
|
+
};
|
|
95
|
+
const record = assertMutationRecord({ ...content, checksum: checksum(canonicalJson(content)) });
|
|
96
|
+
records.push(record);
|
|
97
|
+
tail = record;
|
|
98
|
+
}
|
|
99
|
+
await this.append(records, current);
|
|
100
|
+
return records;
|
|
70
101
|
}
|
|
71
102
|
|
|
72
103
|
advance(ordinal: number, state: MutationState): Promise<MutationRecord> {
|
|
73
104
|
return this.enqueueMutation(() => this.advanceMutation(ordinal, state));
|
|
74
105
|
}
|
|
75
106
|
|
|
107
|
+
advanceMany(ordinal: number, states: readonly MutationState[]): Promise<readonly MutationRecord[]> {
|
|
108
|
+
return this.enqueueMutation(() => this.advanceManyMutation(ordinal, states));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
advanceBatch(advances: readonly MutationAdvance[]): Promise<readonly MutationRecord[]> {
|
|
112
|
+
return this.enqueueMutation(() => this.advanceBatchMutation(advances));
|
|
113
|
+
}
|
|
114
|
+
|
|
76
115
|
markRollbackCleaned(ordinal: number): Promise<MutationRecord> {
|
|
77
116
|
return this.enqueueMutation(() => this.markRollbackCleanedMutation(ordinal));
|
|
78
117
|
}
|
|
@@ -87,16 +126,58 @@ export class MutationJournal {
|
|
|
87
126
|
}
|
|
88
127
|
|
|
89
128
|
private async advanceMutation(ordinal: number, state: MutationState): Promise<MutationRecord> {
|
|
129
|
+
const [record] = await this.advanceManyMutation(ordinal, [state]);
|
|
130
|
+
return record!;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private advanceManyMutation(
|
|
134
|
+
ordinal: number,
|
|
135
|
+
states: readonly MutationState[],
|
|
136
|
+
): Promise<readonly MutationRecord[]> {
|
|
137
|
+
return this.advanceBatchMutation([{ ordinal, states }]);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
private async advanceBatchMutation(
|
|
141
|
+
advances: readonly MutationAdvance[],
|
|
142
|
+
): Promise<readonly MutationRecord[]> {
|
|
143
|
+
if (advances.length === 0 || advances.some((advance) => advance.states.length === 0)) {
|
|
144
|
+
throw new Error("mutation 批量状态不能为空");
|
|
145
|
+
}
|
|
90
146
|
const current = await this.readRecords();
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
147
|
+
const latest = [...current.latest];
|
|
148
|
+
let tail = current.tail;
|
|
149
|
+
const records: MutationRecord[] = [];
|
|
150
|
+
for (const advance of advances) {
|
|
151
|
+
for (const state of advance.states) {
|
|
152
|
+
const previous = latest[advance.ordinal - 1];
|
|
153
|
+
if (previous === undefined || stateOrder.indexOf(state) !== stateOrder.indexOf(previous.state) + 1) {
|
|
154
|
+
throw new Error(`mutation state 必须严格推进:${previous?.state ?? "missing"} -> ${state}`);
|
|
155
|
+
}
|
|
156
|
+
const content = {
|
|
157
|
+
schemaVersion: previous.schemaVersion,
|
|
158
|
+
opId: previous.opId,
|
|
159
|
+
ordinal: previous.ordinal,
|
|
160
|
+
state,
|
|
161
|
+
kind: previous.kind,
|
|
162
|
+
path: previous.path,
|
|
163
|
+
sourceArtifact: previous.sourceArtifact,
|
|
164
|
+
targetArtifact: previous.targetArtifact,
|
|
165
|
+
sourceFingerprint: previous.sourceFingerprint,
|
|
166
|
+
targetFingerprint: previous.targetFingerprint,
|
|
167
|
+
previousChecksum: tail?.checksum ?? null,
|
|
168
|
+
};
|
|
169
|
+
const record = assertMutationRecord({ ...content, checksum: checksum(canonicalJson(content)) });
|
|
170
|
+
records.push(record);
|
|
171
|
+
latest[advance.ordinal - 1] = record;
|
|
172
|
+
tail = record;
|
|
173
|
+
}
|
|
94
174
|
}
|
|
95
|
-
|
|
175
|
+
await this.append(records, current);
|
|
176
|
+
return records;
|
|
96
177
|
}
|
|
97
178
|
|
|
98
179
|
private async appendState(
|
|
99
|
-
current:
|
|
180
|
+
current: JournalRecords,
|
|
100
181
|
previous: MutationRecord,
|
|
101
182
|
state: MutationState,
|
|
102
183
|
): Promise<MutationRecord> {
|
|
@@ -114,7 +195,7 @@ export class MutationJournal {
|
|
|
114
195
|
previousChecksum: current.tail?.checksum ?? null,
|
|
115
196
|
};
|
|
116
197
|
const record = assertMutationRecord({ ...content, checksum: checksum(canonicalJson(content)) });
|
|
117
|
-
await this.append(record, current
|
|
198
|
+
await this.append([record], current);
|
|
118
199
|
return record;
|
|
119
200
|
}
|
|
120
201
|
|
|
@@ -140,21 +221,26 @@ export class MutationJournal {
|
|
|
140
221
|
}
|
|
141
222
|
}
|
|
142
223
|
|
|
143
|
-
private async readRecords(): Promise<{
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}> {
|
|
224
|
+
private async readRecords(): Promise<JournalRecords> {
|
|
225
|
+
const before = await journalFileFingerprint(this.path);
|
|
226
|
+
if (before !== null && this.cachedRecords?.fingerprint === before) {
|
|
227
|
+
return this.cachedRecords.records;
|
|
228
|
+
}
|
|
149
229
|
let bytes: Buffer;
|
|
150
230
|
try {
|
|
151
231
|
bytes = await readFile(this.path);
|
|
152
232
|
} catch (error) {
|
|
153
233
|
if (hasErrorCode(error, "ENOENT")) {
|
|
154
|
-
|
|
234
|
+
const records = emptyJournalRecords();
|
|
235
|
+
this.cachedRecords = undefined;
|
|
236
|
+
return records;
|
|
155
237
|
}
|
|
156
238
|
throw error;
|
|
157
239
|
}
|
|
240
|
+
const after = await journalFileFingerprint(this.path);
|
|
241
|
+
if (before === null || after === null || before !== after) {
|
|
242
|
+
throw new Error("mutation journal 读取期间发生变化");
|
|
243
|
+
}
|
|
158
244
|
|
|
159
245
|
const durableEnd = bytes.at(-1) === 0x0a ? bytes.length : bytes.lastIndexOf(0x0a) + 1;
|
|
160
246
|
const durable = bytes.subarray(0, durableEnd).toString("utf8");
|
|
@@ -163,7 +249,7 @@ export class MutationJournal {
|
|
|
163
249
|
let tail: MutationRecord | undefined;
|
|
164
250
|
|
|
165
251
|
for (const line of lines) {
|
|
166
|
-
const record = assertMutationRecord(JSON.parse(line));
|
|
252
|
+
const record = Object.freeze(assertMutationRecord(JSON.parse(line)));
|
|
167
253
|
if (record.opId !== this.opId) throw new Error("mutation record opId 与 journal 不匹配");
|
|
168
254
|
if (record.previousChecksum !== (tail?.checksum ?? null)) {
|
|
169
255
|
throw new Error("mutation journal hash chain 断裂");
|
|
@@ -185,24 +271,78 @@ export class MutationJournal {
|
|
|
185
271
|
tail = record;
|
|
186
272
|
}
|
|
187
273
|
|
|
188
|
-
|
|
274
|
+
const records: JournalRecords = {
|
|
275
|
+
latest: Object.freeze(latest),
|
|
276
|
+
tail,
|
|
277
|
+
durableEnd,
|
|
278
|
+
hasNonDurableTail: durableEnd !== bytes.length,
|
|
279
|
+
fileExisted: true,
|
|
280
|
+
};
|
|
281
|
+
this.cachedRecords = { records, fingerprint: after };
|
|
282
|
+
return records;
|
|
189
283
|
}
|
|
190
284
|
|
|
191
|
-
private async append(
|
|
285
|
+
private async append(records: readonly MutationRecord[], current: JournalRecords): Promise<void> {
|
|
192
286
|
const directory = dirname(this.path);
|
|
287
|
+
const lines = records.map((record) => `${canonicalJson(record)}\n`).join("");
|
|
193
288
|
const handle = await open(this.path, "a+", 0o600);
|
|
194
289
|
try {
|
|
195
|
-
if (hasNonDurableTail) {
|
|
196
|
-
await handle.truncate(durableEnd);
|
|
290
|
+
if (current.hasNonDurableTail) {
|
|
291
|
+
await handle.truncate(current.durableEnd);
|
|
197
292
|
await handle.sync();
|
|
198
|
-
await fsyncDirectory(directory);
|
|
199
293
|
}
|
|
200
|
-
await handle.writeFile(
|
|
294
|
+
await handle.writeFile(lines);
|
|
201
295
|
await handle.sync();
|
|
202
296
|
} finally {
|
|
203
297
|
await handle.close();
|
|
204
298
|
}
|
|
205
|
-
await fsyncDirectory(directory);
|
|
299
|
+
if (!current.fileExisted) await fsyncDirectory(directory);
|
|
300
|
+
const fingerprint = await journalFileFingerprint(this.path);
|
|
301
|
+
if (fingerprint === null) throw new Error("mutation journal append 后丢失");
|
|
302
|
+
const latest = [...current.latest];
|
|
303
|
+
let tail = current.tail;
|
|
304
|
+
for (const record of records) {
|
|
305
|
+
const durableRecord = Object.freeze({ ...record });
|
|
306
|
+
latest[record.ordinal - 1] = durableRecord;
|
|
307
|
+
tail = durableRecord;
|
|
308
|
+
}
|
|
309
|
+
this.cachedRecords = {
|
|
310
|
+
records: {
|
|
311
|
+
latest: Object.freeze(latest),
|
|
312
|
+
tail,
|
|
313
|
+
durableEnd: current.durableEnd + Buffer.byteLength(lines),
|
|
314
|
+
hasNonDurableTail: false,
|
|
315
|
+
fileExisted: true,
|
|
316
|
+
},
|
|
317
|
+
fingerprint,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function emptyJournalRecords(): JournalRecords {
|
|
323
|
+
return {
|
|
324
|
+
latest: Object.freeze([]),
|
|
325
|
+
tail: undefined,
|
|
326
|
+
durableEnd: 0,
|
|
327
|
+
hasNonDurableTail: false,
|
|
328
|
+
fileExisted: false,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async function journalFileFingerprint(path: string): Promise<string | null> {
|
|
333
|
+
try {
|
|
334
|
+
const metadata = await stat(path, { bigint: true });
|
|
335
|
+
return [
|
|
336
|
+
metadata.dev,
|
|
337
|
+
metadata.ino,
|
|
338
|
+
metadata.mode,
|
|
339
|
+
metadata.size,
|
|
340
|
+
metadata.mtimeNs,
|
|
341
|
+
metadata.ctimeNs,
|
|
342
|
+
].join(":");
|
|
343
|
+
} catch (error) {
|
|
344
|
+
if (hasErrorCode(error, "ENOENT")) return null;
|
|
345
|
+
throw error;
|
|
206
346
|
}
|
|
207
347
|
}
|
|
208
348
|
|
package/src/pi-runtime.ts
CHANGED
|
@@ -38,12 +38,12 @@ export async function createPiUndoRuntime(context: ExtensionContext, pi: Extensi
|
|
|
38
38
|
const workspaceLock = new WorkspaceLock();
|
|
39
39
|
let commandContext: ExtensionCommandContext | undefined;
|
|
40
40
|
let internalNavigation = false;
|
|
41
|
-
const capture = async () => {
|
|
41
|
+
const capture = async (scopePaths?: readonly string[]) => {
|
|
42
42
|
const topology = await discovery.discover(context.cwd);
|
|
43
43
|
if (topology.workspaceIdentity !== initialTopology.workspaceIdentity) {
|
|
44
44
|
throw new Error("workspace identity 已变化");
|
|
45
45
|
}
|
|
46
|
-
return store.capture(topology);
|
|
46
|
+
return store.capture(topology, scopePaths);
|
|
47
47
|
};
|
|
48
48
|
const recovery = new JournalRecovery({
|
|
49
49
|
sessionIdentity,
|