@davideasden/pi-undo 0.3.10 → 0.3.12
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.
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
package/src/durable-pack.ts
CHANGED
|
@@ -446,21 +446,17 @@ export async function finalizeDurablePack(
|
|
|
446
446
|
throw error;
|
|
447
447
|
});
|
|
448
448
|
if (targetArtifactExists) {
|
|
449
|
+
// identity 已证明 absolute 与 target artifact 指向同一 inode,pre/post fsync 各确认一次绑定即可。
|
|
449
450
|
await assertSameFileIdentity(absolute, targetArtifact, path);
|
|
450
|
-
if (await fingerprintFile(absolute, path) !== leaf.fingerprint) {
|
|
451
|
-
throw new Error(`durable finalization 文件 fingerprint 冲突:${path}`);
|
|
452
|
-
}
|
|
453
451
|
await fsyncFile(targetArtifact);
|
|
454
452
|
await assertSameFileIdentity(absolute, targetArtifact, path);
|
|
455
453
|
} else {
|
|
456
454
|
if (mutationStates?.get(path) !== "CLEANED") {
|
|
457
455
|
throw new Error(`durable finalization target ownership 缺失:${path}`);
|
|
458
456
|
}
|
|
459
|
-
if (await fingerprintFile(absolute, path) !== leaf.fingerprint) {
|
|
460
|
-
throw new Error(`durable finalization 文件 fingerprint 冲突:${path}`);
|
|
461
|
-
}
|
|
462
457
|
await fsyncFile(absolute);
|
|
463
458
|
}
|
|
459
|
+
// 落盘后必须从磁盘读回比对:pack bytes 只证明 pack 自身一致,不能证明工作区文件已正确写入。
|
|
464
460
|
if (await fingerprintFile(absolute, path) !== leaf.fingerprint) {
|
|
465
461
|
throw new Error(`durable finalization 后文件 fingerprint 冲突:${path}`);
|
|
466
462
|
}
|
package/src/restore-engine.ts
CHANGED
|
@@ -47,6 +47,8 @@ import { WorkspaceLock } from "./workspace-lock.ts";
|
|
|
47
47
|
|
|
48
48
|
const PREPARED_PLAN_CACHE_LIMIT = 2;
|
|
49
49
|
const PREPARED_PLAN_PATH_LIMIT = 8_192;
|
|
50
|
+
const SOURCE_PROOF_PATH_CACHE_LIMIT = 65_536;
|
|
51
|
+
const SOURCE_PROOF_MAX_AGE_MS = 10_000;
|
|
50
52
|
const DURABLE_CACHE_INDEX_LIMIT = 16;
|
|
51
53
|
const DURABLE_CACHE_INDEX_FILE = "index.json";
|
|
52
54
|
const DURABLE_PACK_MEMORY_MAX_BYTES = 8 * 1024 * 1024;
|
|
@@ -150,6 +152,12 @@ interface PreparedRestorePlan {
|
|
|
150
152
|
readonly targetPaths: ReadonlyMap<string, OwnedPath>;
|
|
151
153
|
}
|
|
152
154
|
|
|
155
|
+
interface PendingSourceProof {
|
|
156
|
+
readonly key: string;
|
|
157
|
+
readonly currentPaths: ReadonlyMap<string, OwnedPath>;
|
|
158
|
+
readonly createdAt: number;
|
|
159
|
+
}
|
|
160
|
+
|
|
153
161
|
interface DurableCacheIndexEntry {
|
|
154
162
|
readonly currentManifestId: ManifestId;
|
|
155
163
|
readonly targetManifestId: ManifestId;
|
|
@@ -205,6 +213,8 @@ export class RestoreEngine {
|
|
|
205
213
|
private readonly beforeMutation: RestoreEngineOptions["beforeMutation"];
|
|
206
214
|
private readonly preparedPlans = new Map<string, PreparedRestorePlan>();
|
|
207
215
|
private readonly preparedPathsByPlan = new WeakMap<RestorePlan, PreparedRestorePlan>();
|
|
216
|
+
private pendingSourceProof: PendingSourceProof | undefined;
|
|
217
|
+
private pendingSourceProofTimer: ReturnType<typeof setTimeout> | undefined;
|
|
208
218
|
private readonly durablePackCache = new Map<string, CachedDurablePack>();
|
|
209
219
|
private readonly durablePackByPair = new Map<string, CachedDurablePair>();
|
|
210
220
|
private readonly verifiedDurablePacks = new Set<string>();
|
|
@@ -279,6 +289,7 @@ export class RestoreEngine {
|
|
|
279
289
|
? undefined
|
|
280
290
|
: this.findPreparedPlan(current.manifestId, target.manifestId, canonicalScopePaths);
|
|
281
291
|
if (prepared !== undefined) {
|
|
292
|
+
this.clearPendingSourceProof();
|
|
282
293
|
// 后台 durable prepare 已读取并验证过同一对 immutable manifest;复用路径表,
|
|
283
294
|
// 避免 undo 前再执行一轮 listTree 与 diff。
|
|
284
295
|
const pathsStarted = performance.now();
|
|
@@ -288,9 +299,13 @@ export class RestoreEngine {
|
|
|
288
299
|
return cloneRestorePlan(prepared.plan);
|
|
289
300
|
}
|
|
290
301
|
|
|
302
|
+
if (canonicalScopePaths === undefined) this.clearPendingSourceProof();
|
|
291
303
|
const pathsStarted = performance.now();
|
|
304
|
+
const pendingSourcePaths = canonicalScopePaths === undefined
|
|
305
|
+
? undefined
|
|
306
|
+
: this.takePendingSourceProof(current.manifestId, target.manifestId, canonicalScopePaths);
|
|
292
307
|
const [currentPaths, targetPaths] = await allCompleted([
|
|
293
|
-
this.readOwnedPaths(current, canonicalScopePaths),
|
|
308
|
+
pendingSourcePaths ?? this.readOwnedPaths(current, canonicalScopePaths),
|
|
294
309
|
this.readOwnedPaths(target, canonicalScopePaths),
|
|
295
310
|
]);
|
|
296
311
|
reportDurablePackTiming(options, "plan.paths", pathsStarted);
|
|
@@ -426,6 +441,7 @@ export class RestoreEngine {
|
|
|
426
441
|
target: SnapshotManifest,
|
|
427
442
|
scopePaths: readonly string[],
|
|
428
443
|
): Promise<boolean> {
|
|
444
|
+
this.clearPendingSourceProof();
|
|
429
445
|
try {
|
|
430
446
|
assertManifest(current);
|
|
431
447
|
assertManifest(target);
|
|
@@ -475,6 +491,9 @@ export class RestoreEngine {
|
|
|
475
491
|
return false;
|
|
476
492
|
}
|
|
477
493
|
}
|
|
494
|
+
if (prepared === undefined) {
|
|
495
|
+
this.rememberPendingSourceProof(current.manifestId, target.manifestId, scope, currentPaths);
|
|
496
|
+
}
|
|
478
497
|
return true;
|
|
479
498
|
} catch (error) {
|
|
480
499
|
rethrowOperationFailure(error);
|
|
@@ -819,6 +838,47 @@ export class RestoreEngine {
|
|
|
819
838
|
return undefined;
|
|
820
839
|
}
|
|
821
840
|
|
|
841
|
+
private clearPendingSourceProof(): void {
|
|
842
|
+
this.pendingSourceProof = undefined;
|
|
843
|
+
if (this.pendingSourceProofTimer !== undefined) {
|
|
844
|
+
clearTimeout(this.pendingSourceProofTimer);
|
|
845
|
+
this.pendingSourceProofTimer = undefined;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
private rememberPendingSourceProof(
|
|
850
|
+
currentManifestId: ManifestId,
|
|
851
|
+
targetManifestId: ManifestId,
|
|
852
|
+
scopePaths: readonly string[],
|
|
853
|
+
currentPaths: ReadonlyMap<string, OwnedPath>,
|
|
854
|
+
): void {
|
|
855
|
+
if (currentPaths.size > SOURCE_PROOF_PATH_CACHE_LIMIT) return;
|
|
856
|
+
this.clearPendingSourceProof();
|
|
857
|
+
const pending: PendingSourceProof = {
|
|
858
|
+
key: durablePairKey(currentManifestId, targetManifestId, scopePaths),
|
|
859
|
+
currentPaths,
|
|
860
|
+
createdAt: performance.now(),
|
|
861
|
+
};
|
|
862
|
+
this.pendingSourceProof = pending;
|
|
863
|
+
this.pendingSourceProofTimer = setTimeout(() => {
|
|
864
|
+
if (this.pendingSourceProof === pending) this.clearPendingSourceProof();
|
|
865
|
+
}, SOURCE_PROOF_MAX_AGE_MS);
|
|
866
|
+
this.pendingSourceProofTimer.unref?.();
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
private takePendingSourceProof(
|
|
870
|
+
currentManifestId: ManifestId,
|
|
871
|
+
targetManifestId: ManifestId,
|
|
872
|
+
scopePaths: readonly string[],
|
|
873
|
+
): ReadonlyMap<string, OwnedPath> | undefined {
|
|
874
|
+
const pending = this.pendingSourceProof;
|
|
875
|
+
this.clearPendingSourceProof();
|
|
876
|
+
if (pending === undefined || performance.now() - pending.createdAt > SOURCE_PROOF_MAX_AGE_MS) return undefined;
|
|
877
|
+
return pending.key === durablePairKey(currentManifestId, targetManifestId, scopePaths)
|
|
878
|
+
? pending.currentPaths
|
|
879
|
+
: undefined;
|
|
880
|
+
}
|
|
881
|
+
|
|
822
882
|
private rememberPreparedPlan(
|
|
823
883
|
plan: RestorePlan,
|
|
824
884
|
currentPaths: ReadonlyMap<string, OwnedPath>,
|