@waffle-os/runtime 0.0.153 → 0.0.155

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 (61) 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 +18 -0
  9. package/core/wasi.ts +75 -4
  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 +62 -4
  17. package/dist/fs/backend.js +7 -0
  18. package/dist/fs/client.js +75 -17
  19. package/dist/fs/dirty-admission.js +15 -8
  20. package/dist/fs/guest-watch.js +267 -94
  21. package/dist/fs/indexeddb-chunks.js +88 -11
  22. package/dist/fs/indexeddb.js +366 -110
  23. package/dist/fs/memory.js +106 -20
  24. package/dist/fs/multitab-backend.js +291 -74
  25. package/dist/fs/multitab-protocol.js +1 -0
  26. package/dist/fs/persistent.js +70 -16
  27. package/dist/fs/protocol.js +15 -7
  28. package/dist/fs/publication-attachment.js +9 -7
  29. package/dist/fs/service.js +62 -9
  30. package/dist/fs/shared.js +100 -6
  31. package/dist/fs/storage.js +107 -8
  32. package/dist/fs/watch-service.js +97 -2
  33. package/dist/fs/watch-subscriptions.js +121 -8
  34. package/dist/proc/command-stdio.js +1 -1
  35. package/dist/proc/intrinsics/core.js +5 -0
  36. package/dist/proc/intrinsics.js +1 -0
  37. package/dist/tooling/program/program-transformer.json +1 -1
  38. package/fs/backend.ts +17 -0
  39. package/fs/client.ts +99 -16
  40. package/fs/dirty-admission.ts +22 -7
  41. package/fs/guest-watch.ts +299 -103
  42. package/fs/indexeddb-chunks.ts +103 -9
  43. package/fs/indexeddb.ts +477 -121
  44. package/fs/memory.ts +120 -19
  45. package/fs/multitab-backend.ts +440 -95
  46. package/fs/multitab-protocol.ts +1 -0
  47. package/fs/persistent.ts +88 -16
  48. package/fs/protocol.ts +26 -7
  49. package/fs/publication-attachment.ts +9 -7
  50. package/fs/service.ts +62 -7
  51. package/fs/shared.ts +110 -8
  52. package/fs/storage.ts +128 -10
  53. package/fs/watch-service.ts +110 -3
  54. package/fs/watch-subscriptions.ts +115 -7
  55. package/package.json +1 -1
  56. package/proc/command-stdio.ts +1 -1
  57. package/proc/intrinsics/core.ts +7 -0
  58. package/proc/intrinsics.ts +1 -0
  59. package/testing/idle-workspace-cancellation-browser.ts +22 -13
  60. package/testing/indexeddb-storage-browser.ts +564 -1
  61. package/tooling/program/program-transformer.json +1 -1
package/fs/client.ts CHANGED
@@ -103,6 +103,8 @@ import {
103
103
  reapFilesystem,
104
104
  unpack,
105
105
  wake,
106
+ wakeTransport,
107
+ SIGNAL_TRANSPORT,
106
108
  releaseFilesystemReservation,
107
109
  type FilesystemIdentity,
108
110
  type FilesystemReservation,
@@ -196,6 +198,19 @@ export interface DirectDisabledIOControl {
196
198
  deferWrite?(completion: FilesystemCompletion): void;
197
199
  }
198
200
 
201
+ /** Operations whose inline replies the service keeps nothing for. */
202
+ const PLAIN_REPLIES = new SetConstructor([
203
+ "stat",
204
+ "create",
205
+ "write",
206
+ "append",
207
+ "grow",
208
+ "resize",
209
+ "setTimes",
210
+ "setMode",
211
+ "read",
212
+ "readlink",
213
+ ]);
199
214
  export class FilesystemCompletion {
200
215
  static {
201
216
  waitForObservedResolve = (completion, cancellation) => completion.#wait(cancellation);
@@ -209,6 +224,8 @@ export class FilesystemCompletion {
209
224
  private loading?: Promise<unknown>;
210
225
  private consumed = false;
211
226
  private released = false;
227
+ /** Whether the service may keep state for this reply until its discard. */
228
+ retained = true;
212
229
  constructor(
213
230
  readonly id: number,
214
231
  readonly buffer: FilesystemRequestBuffer,
@@ -275,7 +292,9 @@ export class FilesystemCompletion {
275
292
  // release cancels that value and notifies its waiters before removing the completion.
276
293
  if (!this.consumed) this.cancel();
277
294
  else {
278
- this.client.connection.port.postMessage({ discard: this.id });
295
+ // The service keeps nothing for a plain inline reply, so its discard would do nothing.
296
+ if (this.retained || atomicsLoad(this.cells, 0) !== State.Complete)
297
+ this.client.connection.port.postMessage({ discard: this.id });
279
298
  this.notify();
280
299
  }
281
300
  mapDelete(this.client.completions, this.id);
@@ -291,6 +310,7 @@ export class FilesystemCompletion {
291
310
  }
292
311
  arrayAppend(this.backendWaiters, callback);
293
312
  this.client.watchRevocation();
313
+ this.client.watchBackendSettlement();
294
314
  if (this.backendSettled) this.notify();
295
315
  }
296
316
  /** If the service already published State.Complete, decode and return the inline result even
@@ -481,6 +501,7 @@ function clientWatches(
481
501
  cells: () => Int32Array,
482
502
  revoked: () => boolean,
483
503
  remote: () => boolean,
504
+ canDrain: () => boolean,
484
505
  call: (operation: string, args: unknown[]) => any,
485
506
  ): WatchFileSystem {
486
507
  return {
@@ -493,8 +514,12 @@ function clientWatches(
493
514
  get remote() {
494
515
  return remote();
495
516
  },
517
+ get canDrain() {
518
+ return canDrain();
519
+ },
496
520
  open: (handle, recursive) => call("watchOpen", [handle, recursive]),
497
521
  read: (id) => call("watchRead", [id]),
522
+ drain: () => call("watchDrain", []),
498
523
  close: (id) => {
499
524
  call("watchClose", [id]);
500
525
  },
@@ -587,7 +612,9 @@ export class FilesystemClient implements FileSystem {
587
612
  private capabilities: Int32Array;
588
613
  private epoch: Int32Array;
589
614
  /** Shared wake counter, viewed once: a blocking wait reads it on every loop. */
615
+ /** Word 0 of the shared signal, one word long, as poll's wake-word checks require. */
590
616
  readonly signal: Int32Array;
617
+ private readonly transportSignal: Int32Array;
591
618
  private revoked: () => boolean;
592
619
  private readonly section: FilesystemSection;
593
620
  readonly shared: SharedFilesystemState;
@@ -607,7 +634,8 @@ export class FilesystemClient implements FileSystem {
607
634
  this.requests = new FilesystemRequestAllocator(connection.requests);
608
635
  this.capabilities = new Int32ArrayConstructor(connection.capabilities, 0, CAPABILITY_LENGTH);
609
636
  this.epoch = new Int32ArrayConstructor(connection.epoch);
610
- this.signal = new Int32ArrayConstructor(connection.signal);
637
+ this.signal = new Int32ArrayConstructor(connection.signal, 0, 1);
638
+ this.transportSignal = new Int32ArrayConstructor(connection.signal, SIGNAL_TRANSPORT * 4, 1);
611
639
  const revoked = sharedRevoked(connection);
612
640
  this.revoked = () => this.disposed || revoked() || !!this.transportError();
613
641
  if (connection.residentPublication)
@@ -625,6 +653,7 @@ export class FilesystemClient implements FileSystem {
625
653
  () => this.shared.watch.cells,
626
654
  this.revoked,
627
655
  () => this.shared.isDirectDisabled(),
656
+ () => !this.shared.isWatchDrainDisabled(),
628
657
  (operation, args) => this.call(operation, args),
629
658
  );
630
659
  connection.port.onmessage = ({ data }) => {
@@ -633,7 +662,7 @@ export class FilesystemClient implements FileSystem {
633
662
  };
634
663
  connection.port.onmessageerror = () => {
635
664
  closeRequestMemory(this.requests.memory);
636
- wake(connection);
665
+ wakeTransport(connection);
637
666
  mapForEach(this.completions, (completion) => completion.notify());
638
667
  connection.port.postMessage({ transportClosed: true });
639
668
  connection.port.close();
@@ -696,7 +725,9 @@ export class FilesystemClient implements FileSystem {
696
725
  return client;
697
726
  }
698
727
  /** A single shared-signal waiter handles service death without a port completion message.
699
- * Normal replies still use the port; the watcher exits when the last completion is released.
728
+ * Normal replies still use the port. The waiter parks on the transport word, or on word 0
729
+ * while a deferred write waits for its backend, and is checked again on each wake; one left
730
+ * parked after the last completion is released serves the next request.
700
731
  */
701
732
  private disposed = false;
702
733
  private finishedDispose = false;
@@ -769,24 +800,55 @@ export class FilesystemClient implements FileSystem {
769
800
  this.watchingRevocation = true;
770
801
  this.monitorRevocation();
771
802
  }
803
+ /** A deferred write needs the backend's terminal state even if its port reply is lost, and
804
+ * that state wakes word 0 only. Move a waiter parked on the transport word over to it. */
805
+ watchBackendSettlement(): void {
806
+ this.backendWaiterVersion++;
807
+ if (this.revocationWord === this.transportSignal) atomicsNotify(this.transportSignal, 0);
808
+ }
809
+ /** The word the monitor is parked on, or undefined while it runs or is stopped. */
810
+ private revocationWord: Int32Array | undefined;
811
+ /** Bumped by every backend-waiter registration. A pass that saw it change, for example a
812
+ * settlement callback registering the next deferred write, chooses its word again. */
813
+ private backendWaiterVersion = 0;
814
+ private unsettledBackendWaiter(): boolean {
815
+ let found = false;
816
+ mapForEach(this.completions, (completion) => {
817
+ if (completion.hasBackendWaiters && !completion.backendSettled) found = true;
818
+ });
819
+ return found;
820
+ }
821
+ /** Completions arrive as port messages, so the waiter needs only revocation, reap, retirement
822
+ * and transport failure, which all wake SIGNAL_TRANSPORT. Two cases keep word 0: a deferred
823
+ * write's backend settlement publishes only there, and a publication domain's stop wakes
824
+ * nothing itself. */
825
+ private revocationSignal(): Int32Array {
826
+ return this.connection.publication || this.unsettledBackendWaiter()
827
+ ? this.signal
828
+ : this.transportSignal;
829
+ }
772
830
  private monitorRevocation(): void {
773
- const signal = this.signal;
774
831
  const stop = () => {
832
+ this.revocationWord = undefined;
775
833
  this.watchingRevocation = false;
776
834
  };
777
835
  const step = () => {
836
+ this.revocationWord = undefined;
778
837
  try {
779
838
  while (mapSize(this.completions)) {
839
+ const version = this.backendWaiterVersion;
840
+ const signal = this.revocationSignal();
780
841
  const observed = atomicsLoad(signal, 0);
781
- if (this.revoked()) {
782
- if (!this.sweepRevokedCompletions()) break;
783
- } else {
784
- this.notifySettledBackendWaiters();
785
- }
786
- if (!mapSize(this.completions)) break;
787
- const waiting = atomicsWaitAsync(signal, 0, observed);
788
- if (waiting.async) {
789
- afterNativePromise(waiting.value, step, stop);
842
+ let waiting = true;
843
+ if (this.revoked()) waiting = this.sweepRevokedCompletions();
844
+ else this.notifySettledBackendWaiters();
845
+ // Callbacks run above can register waiters; neither park nor stop on a stale choice.
846
+ if (version !== this.backendWaiterVersion) continue;
847
+ if (!waiting || !mapSize(this.completions)) break;
848
+ const parked = atomicsWaitAsync(signal, 0, observed);
849
+ if (parked.async) {
850
+ this.revocationWord = signal;
851
+ afterNativePromise(parked.value, step, stop);
790
852
  return;
791
853
  }
792
854
  }
@@ -957,14 +1019,28 @@ export class FilesystemClient implements FileSystem {
957
1019
  capacity: number,
958
1020
  publication?: PublicationAdmission,
959
1021
  ): FilesystemCompletion {
960
- const input = pack({ operation, args, publication });
1022
+ // An absent admission stays absent: encoding undefined would tag the envelope and send every
1023
+ // request down the service's tagged-value decode.
1024
+ const input = pack(publication ? { operation, args, publication } : { operation, args });
961
1025
  this.section.enter();
962
1026
  try {
963
- return this.postRequest(input, data, capacity, publication);
1027
+ const completion = this.postRequest(input, data, capacity, publication);
1028
+ completion.retained = this.retainsReply(operation);
1029
+ return completion;
964
1030
  } finally {
965
1031
  this.section.exit();
966
1032
  }
967
1033
  }
1034
+ /** The service keeps a reply until its discard for a resolve (its held references), a stage,
1035
+ * a walk, a watch, a workspace read and any overflowing reply. Only these operations are known
1036
+ * to keep nothing for an inline reply; the caller still checks overflow from the reply state. */
1037
+ private retainsReply(operation: string): boolean {
1038
+ return (
1039
+ !setHas(PLAIN_REPLIES, operation) ||
1040
+ this.connection.residentPublication !== undefined ||
1041
+ this.shared.publication !== undefined
1042
+ );
1043
+ }
968
1044
  private postRequest(
969
1045
  input: Uint8Array,
970
1046
  data: Uint8Array,
@@ -2529,6 +2605,13 @@ export class FilesystemClient implements FileSystem {
2529
2605
  return errno(error);
2530
2606
  }
2531
2607
  }
2608
+ realpath(directory: FileHandle, path: string, rooted = false): string | number {
2609
+ try {
2610
+ return this.call("realpath", [directory, path, rooted]);
2611
+ } catch (error) {
2612
+ return errno(error);
2613
+ }
2614
+ }
2532
2615
  readlink(handle: FileHandle): string {
2533
2616
  this.retain(handle);
2534
2617
  try {
@@ -151,14 +151,25 @@ export class DirtyAdmission {
151
151
  get revision(): number {
152
152
  return this.values[REVISION]!;
153
153
  }
154
- clean(revision: number, namespace: number): boolean {
155
- if (!this.enabled || this.revision !== revision) return false;
156
- this.values[DIRTY_BYTES] = 0;
157
- this.values[DIRTY_METADATA] = 0;
154
+ releaseSlot(bytes: number, metadata: number): void {
155
+ if (!this.enabled || (!bytes && !metadata)) return;
156
+ this.values[DIRTY_BYTES] = Math.max(0, this.values[DIRTY_BYTES]! - bytes);
157
+ this.values[DIRTY_METADATA] = Math.max(0, this.values[DIRTY_METADATA]! - metadata);
158
+ this.values[REVISION]!++;
159
+ this.notify();
160
+ }
161
+ reconcile(
162
+ namespace: number,
163
+ dirtyNamespace: number,
164
+ dirtyBytes: number,
165
+ dirtyMetadata: number,
166
+ ): void {
167
+ if (!this.enabled) return;
168
+ this.values[DIRTY_BYTES] = dirtyBytes;
169
+ this.values[DIRTY_METADATA] = dirtyMetadata;
158
170
  this.values[NAMESPACE] = namespace;
159
- this.values[BASE_NAMESPACE] = namespace;
171
+ this.values[BASE_NAMESPACE] = namespace - dirtyNamespace;
160
172
  this.notify();
161
- return true;
162
173
  }
163
174
  private notify(): void {
164
175
  Atomics.add(this.signals, 0, 1);
@@ -183,7 +194,11 @@ export class DirtyAdmission {
183
194
  }
184
195
  }
185
196
  get hasDebt(): boolean {
186
- return this.values[DIRTY_BYTES]! > 0 || this.values[DIRTY_METADATA]! > 0;
197
+ return (
198
+ this.values[DIRTY_BYTES]! > 0 ||
199
+ this.values[DIRTY_METADATA]! > 0 ||
200
+ this.values[NAMESPACE]! > this.values[BASE_NAMESPACE]!
201
+ );
187
202
  }
188
203
  capacityReleased(): void {
189
204
  if (this.enabled) this.notify();