@waffle-os/runtime 0.0.153 → 0.0.154

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.
Files changed (48) hide show
  1. package/browser/net/helper.ts +114 -19
  2. package/browser/net/ring.ts +18 -6
  3. package/browser/net/runtime.ts +2 -2
  4. package/browser/notification-relay.ts +105 -18
  5. package/browser/notification-wait.ts +45 -4
  6. package/browser/notification-worker.ts +169 -76
  7. package/browser/preview.ts +15 -2
  8. package/core/filesystem.ts +4 -0
  9. package/core/wasi.ts +49 -0
  10. package/dist/browser/net/helper.js +83 -18
  11. package/dist/browser/net/ring.js +6 -5
  12. package/dist/browser/net/runtime.js +2 -2
  13. package/dist/browser/notification-relay.js +78 -15
  14. package/dist/browser/notification-wait.js +37 -5
  15. package/dist/browser/notification-worker.js +146 -67
  16. package/dist/core/wasi.js +40 -0
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +69 -15
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/indexeddb.js +288 -96
  21. package/dist/fs/memory.js +106 -20
  22. package/dist/fs/multitab-backend.js +121 -37
  23. package/dist/fs/multitab-protocol.js +1 -0
  24. package/dist/fs/persistent.js +70 -16
  25. package/dist/fs/protocol.js +15 -7
  26. package/dist/fs/publication-attachment.js +9 -7
  27. package/dist/fs/service.js +34 -9
  28. package/dist/fs/shared.js +96 -6
  29. package/dist/proc/command-stdio.js +1 -1
  30. package/dist/tooling/program/program-transformer.json +1 -1
  31. package/fs/backend.ts +11 -0
  32. package/fs/client.ts +93 -16
  33. package/fs/dirty-admission.ts +22 -7
  34. package/fs/guest-watch.ts +1 -1
  35. package/fs/indexeddb.ts +379 -110
  36. package/fs/memory.ts +120 -19
  37. package/fs/multitab-backend.ts +206 -61
  38. package/fs/multitab-protocol.ts +1 -0
  39. package/fs/persistent.ts +82 -16
  40. package/fs/protocol.ts +26 -7
  41. package/fs/publication-attachment.ts +9 -7
  42. package/fs/service.ts +35 -7
  43. package/fs/shared.ts +106 -8
  44. package/package.json +1 -1
  45. package/proc/command-stdio.ts +1 -1
  46. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  47. package/testing/indexeddb-storage-browser.ts +435 -1
  48. package/tooling/program/program-transformer.json +1 -1
package/dist/fs/memory.js CHANGED
@@ -75,6 +75,12 @@ function expandTreeLink(child, path, start, options, expansions, targetEnd) {
75
75
  targetEnd: target.length + (targetEnd > start ? 1 + targetEnd - start : 0)
76
76
  };
77
77
  }
78
+ function climbedBase(base, aboveStart) {
79
+ return aboveStart && base === "start" ? "lost" : base;
80
+ }
81
+ function restartedBase(base, options) {
82
+ return options.rooted ? "root" : base;
83
+ }
78
84
  export function walkTree(root, path, options) {
79
85
  const error = treePathError(path, options.pathUnits, options.pathBytes);
80
86
  if (error)
@@ -83,6 +89,7 @@ export function walkTree(root, path, options) {
83
89
  const names = [];
84
90
  let node = root;
85
91
  let parent;
92
+ let base = "start";
86
93
  let name = "", terminalDot = false, expansions = 0, targetEnd = 0, start = 0;
87
94
  while (start < path.length) {
88
95
  let end = path.indexOf("/", start);
@@ -103,9 +110,11 @@ export function walkTree(root, path, options) {
103
110
  if (part === ".")
104
111
  continue;
105
112
  if (part === "..") {
106
- const up = dirs.pop() ?? climbAbove(node, root, options);
113
+ const popped = dirs.pop();
114
+ const up = popped ?? climbAbove(node, root, options);
107
115
  if (typeof up === "number")
108
116
  return up;
117
+ base = climbedBase(base, popped === undefined && up !== node);
109
118
  node = up;
110
119
  names.pop();
111
120
  parent = undefined;
@@ -120,6 +129,7 @@ export function walkTree(root, path, options) {
120
129
  if (!child)
121
130
  return {
122
131
  canonicalPath: "/" + names.concat(part).join("/"),
132
+ base,
123
133
  parent,
124
134
  name,
125
135
  node: undefined,
@@ -132,6 +142,7 @@ export function walkTree(root, path, options) {
132
142
  if (link?.absolute) {
133
143
  dirs.length = 0;
134
144
  names.length = 0;
145
+ base = restartedBase(base, options);
135
146
  }
136
147
  if (link) {
137
148
  expansions++;
@@ -151,7 +162,26 @@ export function walkTree(root, path, options) {
151
162
  const trailingSlash = path.endsWith("/");
152
163
  if (trailingSlash && node.kind !== "dir")
153
164
  return 54;
154
- return { parent, name, node, trailingSlash, terminalDot, canonicalPath: "/" + names.join("/") };
165
+ return {
166
+ parent,
167
+ name,
168
+ node,
169
+ trailingSlash,
170
+ terminalDot,
171
+ canonicalPath: "/" + names.join("/"),
172
+ base
173
+ };
174
+ }
175
+ function joinBelow(prefix, below) {
176
+ if (typeof prefix === "number" || below === "/")
177
+ return prefix;
178
+ return (prefix === "/" ? "" : prefix) + below;
179
+ }
180
+ function childName(parent, child) {
181
+ for (const [name, entry] of parent.children)
182
+ if (entry === child)
183
+ return name;
184
+ return;
155
185
  }
156
186
  function namespacePlan(edits = [], created = [], forgotten = [], links = []) {
157
187
  return {
@@ -204,7 +234,20 @@ export function decode(data) {
204
234
  export class MemFS {
205
235
  root;
206
236
  onChange;
237
+ onDirEntryDelta;
207
238
  onLinkChange;
239
+ setChild(parent, name, child) {
240
+ const existed = parent.children.has(name);
241
+ parent.children.set(name, child);
242
+ if (!existed)
243
+ this.onDirEntryDelta?.(parent, 64 + name.length * 2);
244
+ }
245
+ deleteChild(parent, name) {
246
+ const deleted = parent.children.delete(name);
247
+ if (deleted)
248
+ this.onDirEntryDelta?.(parent, -(64 + name.length * 2));
249
+ return deleted;
250
+ }
208
251
  changed(node) {
209
252
  this.onChange?.(node);
210
253
  }
@@ -289,12 +332,18 @@ export class MemFS {
289
332
  this.handles.delete(handle);
290
333
  if (node.kind === "dir") {
291
334
  this.removedDirectories.add(node);
292
- for (const child of node.children.values()) {
335
+ const trackDelta = this.onDirEntryDelta !== undefined;
336
+ let delta = 0;
337
+ for (const [name, child] of node.children) {
338
+ if (trackDelta)
339
+ delta -= 64 + name.length * 2;
293
340
  this.changeLinkCount(child, -1);
294
341
  this.forget(child);
295
342
  }
296
343
  this.directoryPages(node).clear();
297
344
  node.children.clear();
345
+ if (delta)
346
+ this.onDirEntryDelta?.(node, delta);
298
347
  }
299
348
  }
300
349
  nodeForHandle(handle) {
@@ -381,7 +430,21 @@ export class MemFS {
381
430
  return this.walk(this.handle(this.root), path.replace(/^\/+/, "") || ".", true, followFinal);
382
431
  }
383
432
  walk(directory, path, clampRoot, followFinal, pathBytes, rooted = false) {
384
- const resolved = walkTree(this.directory(directory), path, {
433
+ const resolved = this.walkNodes(this.directory(directory), path, clampRoot, followFinal, pathBytes, rooted);
434
+ if (typeof resolved === "number")
435
+ return resolved;
436
+ return {
437
+ canonicalPath: resolved.canonicalPath,
438
+ base: resolved.base,
439
+ name: resolved.name,
440
+ trailingSlash: resolved.trailingSlash,
441
+ terminalDot: resolved.terminalDot,
442
+ parent: resolved.parent ? this.handle(resolved.parent) : undefined,
443
+ node: resolved.node ? this.handle(resolved.node) : undefined
444
+ };
445
+ }
446
+ walkNodes(start, path, clampRoot, followFinal, pathBytes, rooted = false) {
447
+ return walkTree(start, path, {
385
448
  clampRoot,
386
449
  followFinal,
387
450
  pathBytes,
@@ -393,16 +456,39 @@ export class MemFS {
393
456
  return child;
394
457
  }
395
458
  });
459
+ }
460
+ realpath(directory, path, rooted = false) {
461
+ if (path.startsWith("/"))
462
+ return 63;
463
+ const start = this.directory(directory);
464
+ const resolved = this.walkNodes(start, path, false, true, undefined, rooted);
396
465
  if (typeof resolved === "number")
397
466
  return resolved;
398
- return {
399
- canonicalPath: resolved.canonicalPath,
400
- name: resolved.name,
401
- trailingSlash: resolved.trailingSlash,
402
- terminalDot: resolved.terminalDot,
403
- parent: resolved.parent ? this.handle(resolved.parent) : undefined,
404
- node: resolved.node ? this.handle(resolved.node) : undefined
405
- };
467
+ if (resolved.node === undefined)
468
+ return 44;
469
+ if (!rooted)
470
+ return resolved.canonicalPath.slice(1) || ".";
471
+ if (resolved.base === "root" || resolved.base === "start" && start === this.root)
472
+ return resolved.canonicalPath;
473
+ if (resolved.base === "start")
474
+ return joinBelow(this.pathOf(start), resolved.canonicalPath);
475
+ if (resolved.node.kind === "dir")
476
+ return this.pathOf(resolved.node);
477
+ return joinBelow(this.pathOf(resolved.parent), "/" + resolved.name);
478
+ }
479
+ pathOf(dir) {
480
+ const names = [];
481
+ for (let at = dir;at !== this.root; ) {
482
+ const parent = this.liveParent(at);
483
+ if (typeof parent === "number")
484
+ return parent === 63 ? 44 : parent;
485
+ const name = childName(parent, at);
486
+ if (name === undefined)
487
+ return 44;
488
+ names.push(name);
489
+ at = parent;
490
+ }
491
+ return "/" + names.reverse().join("/");
406
492
  }
407
493
  symlink(path, target) {
408
494
  const prepared = this.planSymlink(path, target, 4);
@@ -567,7 +653,7 @@ export class MemFS {
567
653
  const node = kind === "file" ? newFile() : kind === "socket" ? newSocket() : newDir();
568
654
  this.directoryPages(parent).reserve(1);
569
655
  this.pageEntry(parent, path.name, node);
570
- parent.children.set(path.name, node);
656
+ this.setChild(parent, path.name, node);
571
657
  this.changeLinkCount(node, 1);
572
658
  this.onLinkChange?.(node, 1);
573
659
  modified(parent, node.ctime);
@@ -675,7 +761,7 @@ export class MemFS {
675
761
  return parent;
676
762
  this.directoryPages(parent).reserve(1);
677
763
  this.pageEntry(parent, to.name, node);
678
- parent.children.set(to.name, node);
764
+ this.setChild(parent, to.name, node);
679
765
  this.changeLinkCount(node, 1);
680
766
  this.onLinkChange?.(node, 1);
681
767
  const time = fileTimeNow();
@@ -703,7 +789,7 @@ export class MemFS {
703
789
  if ((parent.mode & 192) !== 192)
704
790
  return 2;
705
791
  this.pageEntry(parent, path.name, undefined);
706
- parent.children.delete(path.name);
792
+ this.deleteChild(parent, path.name);
707
793
  this.changeLinkCount(node, -1);
708
794
  this.onLinkChange?.(node, -1);
709
795
  const time = fileTimeNow();
@@ -746,11 +832,11 @@ export class MemFS {
746
832
  this.directoryPages(parent).reserve(1);
747
833
  this.pageEntry(sourceParent, from.name, undefined);
748
834
  this.pageEntry(parent, to.name, node);
749
- sourceParent.children.delete(from.name);
835
+ this.deleteChild(sourceParent, from.name);
750
836
  const time = fileTimeNow();
751
837
  modified(sourceParent, time);
752
838
  node.ctime = time;
753
- parent.children.set(to.name, node);
839
+ this.setChild(parent, to.name, node);
754
840
  if (node.kind === "dir")
755
841
  this.directoryParents.set(node, parent);
756
842
  if (target) {
@@ -983,9 +1069,9 @@ export class MemFS {
983
1069
  if (edge.after)
984
1070
  this.changeLinkCount(edge.after, 1);
985
1071
  if (edge.after)
986
- edge.parent.children.set(edge.name, edge.after);
1072
+ this.setChild(edge.parent, edge.name, edge.after);
987
1073
  else
988
- edge.parent.children.delete(edge.name);
1074
+ this.deleteChild(edge.parent, edge.name);
989
1075
  if (edge.after?.kind === "dir")
990
1076
  this.directoryParents.set(edge.after, edge.parent);
991
1077
  if (edge.after && created.has(edge.after))
@@ -1079,7 +1165,7 @@ export class MemFS {
1079
1165
  return false;
1080
1166
  const { parent, name, node } = edge;
1081
1167
  this.pageEntry(parent, name, undefined);
1082
- const removed = parent.children.delete(name);
1168
+ const removed = this.deleteChild(parent, name);
1083
1169
  if (removed) {
1084
1170
  this.changeLinkCount(node, -1);
1085
1171
  this.onLinkChange?.(node, -1);
@@ -141,6 +141,11 @@ function waitForAbortOrCancel(signal, cancelled) {
141
141
  };
142
142
  return { promise, cleanup };
143
143
  }
144
+
145
+ class LeaseLapsedError extends Error {
146
+ }
147
+ const LEASE_LAPSED = new LeaseLapsedError("host lease lapsed");
148
+ const LEASE_RENEWAL_BUDGET_LEASES = 4;
144
149
  async function raceWithAbort(promise, signal, cancelled) {
145
150
  if (signal.aborted || cancelled?.())
146
151
  return;
@@ -345,6 +350,7 @@ export class MultiTabBackend {
345
350
  leaseTimer;
346
351
  leaseRenewInFlight;
347
352
  persistent;
353
+ unlinkEvents = 0;
348
354
  watchService;
349
355
  hostQueue = Promise.resolve();
350
356
  barrier = new BarrierGate;
@@ -533,6 +539,17 @@ export class MultiTabBackend {
533
539
  }
534
540
  return Promise.resolve();
535
541
  }
542
+ setPersistent(next) {
543
+ if (next === this.persistent)
544
+ return;
545
+ this.unlinkEvents += (this.persistent?.unlinkGeneration() ?? 0) + 1;
546
+ this.persistent = next;
547
+ }
548
+ unlinkGeneration() {
549
+ if (this.role !== "host" || !this.persistent)
550
+ return NaN;
551
+ return this.unlinkEvents + this.persistent.unlinkGeneration();
552
+ }
536
553
  isUnlinked(handle) {
537
554
  const entry = this.byLocalHandle.get(handle);
538
555
  if (!entry)
@@ -585,7 +602,7 @@ export class MultiTabBackend {
585
602
  const entry = this.byLocalHandle.get(handle);
586
603
  if (!entry || entry.stale || entry.remoteHandle === undefined)
587
604
  return;
588
- return this.persistent.prepareDescriptorRead(entry.remoteHandle, offset, length, () => cancelled() || !this.isPreparedReadValid() || this.hostTerm !== admittedTerm);
605
+ return this.persistent.prepareDescriptorRead(entry.remoteHandle, offset, length, () => cancelled() || this.hostTerm !== admittedTerm || !this.isPreparedReadValid() && !this.leaseOnlyLapsed(admittedTerm));
589
606
  }
590
607
  prepare(operation, args, _data) {
591
608
  if (this.role !== "host" || !this.persistent || !this.isLeaseValid())
@@ -717,7 +734,8 @@ export class MultiTabBackend {
717
734
  }
718
735
  if (this.role === "host") {
719
736
  const admittedTerm = this.hostTerm;
720
- this.assertHostReadyForLocal(admittedTerm);
737
+ const budget = {};
738
+ await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
721
739
  let resolvedTarget = target;
722
740
  if (typeof target === "number") {
723
741
  const entry = this.byLocalHandle.get(target);
@@ -728,7 +746,7 @@ export class MultiTabBackend {
728
746
  this.enterHostAdmitted();
729
747
  try {
730
748
  await this.hostQueue;
731
- this.assertHostReadyForLocal(admittedTerm);
749
+ await this.assertHostAfterRenewal(admittedTerm, undefined, budget);
732
750
  await this.persistent.flush(resolvedTarget);
733
751
  } finally {
734
752
  this.leaveHostAdmitted();
@@ -759,6 +777,9 @@ export class MultiTabBackend {
759
777
  throw new FilesystemError(8);
760
778
  if (this.role === "host") {
761
779
  const admittedTerm = this.hostTerm;
780
+ if (this.leaseOnlyLapsed(admittedTerm)) {
781
+ return this.watchOpenAfterRenewal(owner, handle, recursive, cancelled, admittedTerm);
782
+ }
762
783
  this.assertHostReadyForLocal(admittedTerm);
763
784
  if (entry.remoteHandle === undefined)
764
785
  throw new FilesystemError(8);
@@ -832,19 +853,32 @@ export class MultiTabBackend {
832
853
  }
833
854
  throw new FilesystemError(11);
834
855
  }
856
+ async watchOpenAfterRenewal(owner, handle, recursive, cancelled, admittedTerm) {
857
+ await this.renewLapsedLease(admittedTerm, cancelled);
858
+ return this.watchOpen(owner, handle, recursive, cancelled);
859
+ }
835
860
  async watchReadHostAfterRenew(owner, watch, cancelled) {
836
861
  if (this.disposing || cancelled())
837
862
  throw new FilesystemError(11);
838
- let ok = false;
839
- await raceWithAbort(this.renewLeaseNow().then((renewed) => {
840
- ok = renewed;
841
- }), this.disposeAbort.signal, cancelled);
863
+ if (this.leaseOnlyLapsed(watch.hostTerm)) {
864
+ await this.renewLapsedLease(watch.hostTerm, cancelled).catch(() => {});
865
+ } else {
866
+ await raceWithAbort(this.renewLeaseNow(), this.disposeAbort.signal, cancelled);
867
+ }
842
868
  if (this.disposing || cancelled())
843
869
  throw new FilesystemError(11);
844
- if (!ok || !this.isLeaseValid() || watch.overflowed || watch.hostTerm !== this.hostTerm || !this.watchService) {
870
+ const overflow = {
871
+ batch: { kind: "overflow", events: [] },
872
+ release() {},
873
+ rollback() {}
874
+ };
875
+ const termEnded = watch.hostTerm !== this.hostTerm || this.role !== "host" || this.ownershipLost || this.leaseClosed;
876
+ if (watch.overflowed || termEnded || !this.watchService) {
845
877
  watch.overflowed = true;
846
- return { batch: { kind: "overflow", events: [] }, release() {}, rollback() {} };
878
+ return overflow;
847
879
  }
880
+ if (!this.isLeaseValid())
881
+ return overflow;
848
882
  return this.watchService.readHeld(owner, watch.hostWatchId);
849
883
  }
850
884
  watchClose(owner, localWatchId) {
@@ -931,7 +965,7 @@ export class MultiTabBackend {
931
965
  }
932
966
  if (this.persistent) {
933
967
  const residual = this.persistent;
934
- this.persistent = undefined;
968
+ this.setPersistent(undefined);
935
969
  this.watchService = undefined;
936
970
  await this.retireFailedMountBackend(residual);
937
971
  }
@@ -1196,7 +1230,7 @@ export class MultiTabBackend {
1196
1230
  this.triggerExternalOwnershipLost();
1197
1231
  throw new FilesystemError(11, "workspace taken over by exclusive owner");
1198
1232
  }
1199
- this.persistent = backend;
1233
+ this.setPersistent(backend);
1200
1234
  this.watchService = new WatchService(backend);
1201
1235
  this.hostTerm = backend.initialReservationEra;
1202
1236
  this.hostStorageEpoch = storageEpoch;
@@ -1255,7 +1289,7 @@ export class MultiTabBackend {
1255
1289
  if (this.terminalFailure)
1256
1290
  throw wrapReleaseError(this.terminalFailure.reason, false);
1257
1291
  if (backend) {
1258
- this.persistent = undefined;
1292
+ this.setPersistent(undefined);
1259
1293
  this.watchService = undefined;
1260
1294
  await this.retireFailedMountBackend(backend);
1261
1295
  } else {
@@ -1585,7 +1619,7 @@ export class MultiTabBackend {
1585
1619
  return;
1586
1620
  }
1587
1621
  this.captureRetainedSegments(persistent);
1588
- this.persistent = undefined;
1622
+ this.setPersistent(undefined);
1589
1623
  this.watchService = undefined;
1590
1624
  if (this.sharedBuffer)
1591
1625
  persistent.shared.resetForRemount(this.retainedSegments);
@@ -1641,7 +1675,7 @@ export class MultiTabBackend {
1641
1675
  this.closeAllHostPeerSessions();
1642
1676
  if (this.persistent) {
1643
1677
  const persistent = this.persistent;
1644
- this.persistent = undefined;
1678
+ this.setPersistent(undefined);
1645
1679
  this.watchService = undefined;
1646
1680
  await persistent.retireFenced().catch(() => {});
1647
1681
  this.captureRetainedSegments(persistent);
@@ -1691,7 +1725,7 @@ export class MultiTabBackend {
1691
1725
  this.closeAllHostPeerSessions();
1692
1726
  if (this.persistent) {
1693
1727
  const persistent = this.persistent;
1694
- this.persistent = undefined;
1728
+ this.setPersistent(undefined);
1695
1729
  this.watchService = undefined;
1696
1730
  await persistent.retireFenced().catch(() => {});
1697
1731
  this.captureRetainedSegments(persistent);
@@ -1714,7 +1748,7 @@ export class MultiTabBackend {
1714
1748
  this.closeAllHostPeerSessions();
1715
1749
  if (this.persistent) {
1716
1750
  const persistent = this.persistent;
1717
- this.persistent = undefined;
1751
+ this.setPersistent(undefined);
1718
1752
  this.watchService = undefined;
1719
1753
  await persistent.retireFenced().catch(() => {});
1720
1754
  this.captureRetainedSegments(persistent);
@@ -4007,22 +4041,74 @@ export class MultiTabBackend {
4007
4041
  throw new FilesystemError(11, "host lease expired");
4008
4042
  }
4009
4043
  }
4044
+ leaseOnlyLapsed(admittedTerm) {
4045
+ return !this.isLeaseValid() && !this.leaseClosed && !this.ownershipLost && !this.terminalFailure && !this.persistent?.isTerminallyFailed() && !!this.persistent && this.role === "host" && this.hostTerm === admittedTerm && !this.steppingDown && !this.disposing;
4046
+ }
4047
+ assertLocalAttempt(admittedTerm, cancelled) {
4048
+ if (cancelled?.())
4049
+ throw new FilesystemError(11, "operation cancelled");
4050
+ if (this.leaseOnlyLapsed(admittedTerm))
4051
+ throw LEASE_LAPSED;
4052
+ this.assertHostReadyForLocal(admittedTerm);
4053
+ }
4054
+ localAttemptStale(admittedTerm, cancelled) {
4055
+ if (cancelled?.() || this.hostTerm !== admittedTerm)
4056
+ return true;
4057
+ return !this.isLeaseValid() && !this.leaseOnlyLapsed(admittedTerm);
4058
+ }
4059
+ async retryAfterLeaseRenewal(admittedTerm, cancelled, budget, attempt) {
4060
+ for (;; ) {
4061
+ try {
4062
+ return await attempt();
4063
+ } catch (error) {
4064
+ if (error !== LEASE_LAPSED)
4065
+ throw error;
4066
+ }
4067
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4068
+ }
4069
+ }
4070
+ async assertHostAfterRenewal(admittedTerm, cancelled, budget) {
4071
+ if (this.leaseOnlyLapsed(admittedTerm))
4072
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4073
+ this.assertHostReadyForLocal(admittedTerm);
4074
+ }
4075
+ async renewLapsedLease(admittedTerm, cancelled, budget = {}) {
4076
+ const fresh = budget.deadline === undefined;
4077
+ const deadline = budget.deadline ??= performance.now() + this.timings.leaseDurationMs * LEASE_RENEWAL_BUDGET_LEASES;
4078
+ for (let attempt = 0;this.leaseOnlyLapsed(admittedTerm); attempt++) {
4079
+ if ((attempt > 0 || !fresh) && performance.now() >= deadline) {
4080
+ throw new FilesystemError(11, "host lease could not be renewed");
4081
+ }
4082
+ if (attempt > 0)
4083
+ await this.pauseBeforeRenewal(cancelled);
4084
+ await raceWithAbort(this.renewLeaseNow(this.currentAttemptId), this.disposeAbort.signal, cancelled);
4085
+ if (cancelled?.())
4086
+ throw new FilesystemError(11, "operation cancelled");
4087
+ }
4088
+ }
4089
+ async pauseBeforeRenewal(cancelled) {
4090
+ await raceWithAbort(delay(this.timings.waitPollMs), this.disposeAbort.signal, cancelled);
4091
+ if (cancelled?.())
4092
+ throw new FilesystemError(11, "operation cancelled");
4093
+ }
4010
4094
  async executeLocalOnHost(operation, args, data, responseCookieTag, cancelled) {
4011
4095
  if (cancelled?.())
4012
4096
  throw new FilesystemError(11, "operation cancelled");
4013
4097
  const admittedTerm = this.hostTerm;
4098
+ const budget = {};
4014
4099
  if (!this.isLeaseValid()) {
4015
4100
  if (this.disposing)
4016
4101
  throw new FilesystemError(11, "host lease expired during dispose");
4017
- let ok = false;
4018
- await raceWithAbort(this.renewLeaseNow(this.currentAttemptId).then((renewed) => {
4019
- ok = renewed;
4020
- }), this.disposeAbort.signal, cancelled);
4102
+ if (this.leaseOnlyLapsed(admittedTerm)) {
4103
+ await this.renewLapsedLease(admittedTerm, cancelled, budget);
4104
+ } else {
4105
+ await raceWithAbort(this.renewLeaseNow(this.currentAttemptId), this.disposeAbort.signal, cancelled);
4106
+ }
4021
4107
  if (cancelled?.())
4022
4108
  throw new FilesystemError(11, "operation cancelled");
4023
4109
  if (this.disposing)
4024
4110
  throw new FilesystemError(11, "filesystem is disposing");
4025
- if (!ok || !this.isLeaseValid()) {
4111
+ if (!this.isLeaseValid()) {
4026
4112
  await this.awaitPendingFenceLoss(cancelled);
4027
4113
  if (cancelled?.())
4028
4114
  throw new FilesystemError(11, "operation cancelled");
@@ -4054,14 +4140,14 @@ export class MultiTabBackend {
4054
4140
  await this.hostQueue;
4055
4141
  if (cancelled?.())
4056
4142
  throw new FilesystemError(11, "operation cancelled");
4057
- this.assertHostReadyForLocal(admittedTerm);
4143
+ await this.assertHostAfterRenewal(admittedTerm, cancelled, budget);
4058
4144
  await persistent.flush(target);
4059
4145
  return operation === "sync" ? 0 : undefined;
4060
4146
  }
4061
4147
  if (operation === "read") {
4062
- return await this.executeLocalHostRead(persistent, admittedTerm, translated, cancelled);
4148
+ return await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () => this.executeLocalHostRead(persistent, admittedTerm, translated, cancelled));
4063
4149
  }
4064
- const result = await this.executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled);
4150
+ const result = await this.retryAfterLeaseRenewal(admittedTerm, cancelled, budget, () => this.executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled));
4065
4151
  return this.mapLocalHostHandles(persistent, admittedTerm, operation, result);
4066
4152
  } finally {
4067
4153
  this.leaveHostAdmitted();
@@ -4069,15 +4155,11 @@ export class MultiTabBackend {
4069
4155
  }
4070
4156
  async executeLocalHostRead(persistent, admittedTerm, translated, cancelled) {
4071
4157
  const [remoteHandle, length, offset] = translated;
4072
- const assertValid = () => {
4073
- if (cancelled?.())
4074
- throw new FilesystemError(11, "operation cancelled");
4075
- this.assertHostReadyForLocal(admittedTerm);
4076
- };
4158
+ const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
4077
4159
  for (;; ) {
4078
4160
  assertValid();
4079
4161
  const startVer = this.readVersion();
4080
- const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid());
4162
+ const prep = persistent.prepareDescriptorRead(remoteHandle, offset, length, () => this.localAttemptStale(admittedTerm, cancelled));
4081
4163
  if (prep) {
4082
4164
  const accepted = await this.revalidateColdReadBytes(prep, startVer, assertValid);
4083
4165
  if (accepted !== undefined)
@@ -4093,11 +4175,7 @@ export class MultiTabBackend {
4093
4175
  }
4094
4176
  }
4095
4177
  async executeLocalHostGeneral(persistent, admittedTerm, operation, translated, data, responseCookieTag, cancelled) {
4096
- const assertValid = () => {
4097
- if (cancelled?.())
4098
- throw new FilesystemError(11, "operation cancelled");
4099
- this.assertHostReadyForLocal(admittedTerm);
4100
- };
4178
+ const assertValid = () => this.assertLocalAttempt(admittedTerm, cancelled);
4101
4179
  for (;; ) {
4102
4180
  assertValid();
4103
4181
  const preparing = persistent.prepare(operation, translated);
@@ -4116,7 +4194,7 @@ export class MultiTabBackend {
4116
4194
  result = step.result;
4117
4195
  });
4118
4196
  if (dirtyRetryChange !== undefined) {
4119
- await persistent.waitForDirtyCapacity(dirtyRetryChange, () => !!cancelled?.() || this.hostTerm !== admittedTerm || !this.isLeaseValid());
4197
+ await persistent.waitForDirtyCapacity(dirtyRetryChange, () => this.localAttemptStale(admittedTerm, cancelled));
4120
4198
  continue;
4121
4199
  }
4122
4200
  return result;
@@ -4917,6 +4995,7 @@ export class MultiTabBackend {
4917
4995
  entry.remoteSessionId = sessionId;
4918
4996
  entry.pinToken = wire.pinToken;
4919
4997
  entry.unlinked = !!wire.unlinked;
4998
+ this.unlinkEvents++;
4920
4999
  entry.stale = false;
4921
5000
  }
4922
5001
  } finally {
@@ -4925,6 +5004,7 @@ export class MultiTabBackend {
4925
5004
  }
4926
5005
  rebindLocalHandlesOnHost(term) {
4927
5006
  const persistent = this.persistent;
5007
+ this.unlinkEvents++;
4928
5008
  for (const entry of this.byLocalHandle.values()) {
4929
5009
  const node = persistent.nodeByFileId(entry.fileId);
4930
5010
  if (!node) {
@@ -4996,6 +5076,8 @@ export class MultiTabBackend {
4996
5076
  this.notifyFailoverReadyWaiters();
4997
5077
  }
4998
5078
  ensureLocalHandle(fileId, remoteHandle, remoteTerm, remoteSessionId, pinToken, unlinked) {
5079
+ if (unlinked)
5080
+ this.unlinkEvents++;
4999
5081
  const existing = this.byFileId.get(fileId);
5000
5082
  if (existing) {
5001
5083
  if (remoteSessionId !== undefined && pinToken !== undefined) {
@@ -5058,6 +5140,7 @@ export class MultiTabBackend {
5058
5140
  }
5059
5141
  }
5060
5142
  detachHandlesForRemount() {
5143
+ this.unlinkEvents++;
5061
5144
  for (const entry of this.byLocalHandle.values()) {
5062
5145
  entry.remoteHandle = undefined;
5063
5146
  entry.remoteTerm = undefined;
@@ -5126,7 +5209,8 @@ export class MultiTabBackend {
5126
5209
  case "setTimes":
5127
5210
  case "readdir":
5128
5211
  case "readdirPage":
5129
- case "resolve": {
5212
+ case "resolve":
5213
+ case "realpath": {
5130
5214
  const remote = mapHandle(args[0]);
5131
5215
  if (remote === undefined)
5132
5216
  return;
@@ -143,6 +143,7 @@ const PURE_STATELESS_READ_OPS = new Set([
143
143
  "readdir",
144
144
  "readlink",
145
145
  "resolve",
146
+ "realpath",
146
147
  "readdirPage"
147
148
  ]);
148
149
  export function isReplayableRead(operation, args) {