@davideasden/pi-undo 0.3.3 → 0.3.4

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
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@davideasden/pi-undo",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Persistent workspace undo and redo for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -118,6 +118,10 @@ export class DurablePack {
118
118
  return this.entries.get(path)?.targetFingerprint;
119
119
  }
120
120
 
121
+ withStoragePath(storagePath: string): DurablePack {
122
+ return new DurablePack(this.opId, this.planDigest, storagePath, this.packChecksum, this.entries);
123
+ }
124
+
121
125
  leaf(path: string, fingerprint: string): DurableLeaf | undefined {
122
126
  const leaf = this.entries.get(path)?.variants.get(fingerprint);
123
127
  if (leaf === undefined) return undefined;
@@ -282,16 +286,36 @@ export async function hasDurablePack(journal: MutationJournal): Promise<boolean>
282
286
  }
283
287
  }
284
288
 
289
+ export async function publishResidentDurablePack(
290
+ cached: DurablePack,
291
+ journal: MutationJournal,
292
+ expectedPlanDigest: string,
293
+ ): Promise<DurablePack> {
294
+ if (cached.planDigest !== expectedPlanDigest) throw new Error("durable cache planDigest 不匹配");
295
+ const target = durablePackPath(journal);
296
+ await rm(target, { force: true });
297
+ try {
298
+ await link(cached.storagePath, target);
299
+ await fsyncDirectory(dirname(target));
300
+ } catch (error) {
301
+ if (!hasErrorCode(error, "EXDEV")) throw error;
302
+ await copyFile(cached.storagePath, target);
303
+ await fsyncFile(target);
304
+ await fsyncDirectory(dirname(target));
305
+ }
306
+ return cached.withStoragePath(target);
307
+ }
308
+
285
309
  export async function publishCachedDurablePack(
286
310
  cachedPath: string,
287
311
  journal: MutationJournal,
288
312
  expectedPlanDigest: string,
289
313
  expectedPackChecksum: string,
290
314
  ): Promise<DurablePack> {
291
- const target = durablePackPath(journal);
292
315
  if (checksum(await readFile(cachedPath)) !== expectedPackChecksum) {
293
316
  throw new Error("durable cache pack checksum 不匹配");
294
317
  }
318
+ const target = durablePackPath(journal);
295
319
  await rm(target, { force: true });
296
320
  try {
297
321
  await link(cachedPath, target);
@@ -11,6 +11,7 @@ import {
11
11
  hasDurablePack,
12
12
  loadDurablePack,
13
13
  publishCachedDurablePack,
14
+ publishResidentDurablePack,
14
15
  removeDurablePack,
15
16
  type DurableLeafInput,
16
17
  type DurablePack,
@@ -189,6 +190,7 @@ export class RestoreEngine {
189
190
  private readonly preparedPlans = new Map<string, PreparedRestorePlan>();
190
191
  private readonly durablePackCache = new Map<string, CachedDurablePack>();
191
192
  private readonly durablePackByPair = new Map<string, CachedDurablePair>();
193
+ private readonly verifiedDurablePacks = new Set<string>();
192
194
  private readonly durableIndexLock = new WorkspaceLock({
193
195
  leaseMs: 10_000,
194
196
  retryMs: 25,
@@ -343,6 +345,7 @@ export class RestoreEngine {
343
345
  false,
344
346
  );
345
347
  }
348
+ this.verifiedDurablePacks.add(`${pairKey}:${cached.planDigest}`);
346
349
  return true;
347
350
  } catch (error) {
348
351
  rethrowOperationFailure(error);
@@ -859,12 +862,25 @@ export class RestoreEngine {
859
862
  cached.currentManifestId === current.manifestId &&
860
863
  cached.targetManifestId === target.manifestId
861
864
  ) {
862
- durablePack = await publishCachedDurablePack(
863
- cached.path,
864
- options.mutationJournal,
865
- plan.planDigest,
866
- cached.packChecksum,
867
- );
865
+ const pairKey = durablePairKey(current.manifestId, target.manifestId, plan.scopePaths ?? []);
866
+ const residentKey = `${pairKey}:${plan.planDigest}`;
867
+ const resident = this.verifiedDurablePacks.has(residentKey)
868
+ ? this.durablePackByPair.get(pairKey)
869
+ : undefined;
870
+ if (resident?.planDigest === plan.planDigest && resident.pack.packChecksum === cached.packChecksum) {
871
+ durablePack = await publishResidentDurablePack(
872
+ resident.pack,
873
+ options.mutationJournal,
874
+ plan.planDigest,
875
+ );
876
+ } else {
877
+ durablePack = await publishCachedDurablePack(
878
+ cached.path,
879
+ options.mutationJournal,
880
+ plan.planDigest,
881
+ cached.packChecksum,
882
+ );
883
+ }
868
884
  } else {
869
885
  durablePack = await createDurablePack(options.mutationJournal, {
870
886
  opId: options.opId,