@waffle-os/runtime 0.0.155 → 0.0.157

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 (77) hide show
  1. package/browser/filesystem-backend.ts +11 -1
  2. package/browser/preview-protocol.ts +2 -0
  3. package/browser/preview.ts +128 -18
  4. package/browser/runtime-terminals.ts +65 -24
  5. package/browser/shared-fs.ts +141 -14
  6. package/browser/worker.ts +73 -6
  7. package/core/descriptors.ts +19 -2
  8. package/core/devices.ts +237 -2
  9. package/core/fd-reopen.ts +105 -20
  10. package/core/filesystem.ts +79 -0
  11. package/core/index.ts +4 -0
  12. package/core/open-file.ts +212 -18
  13. package/core/wasi.ts +449 -47
  14. package/dist/browser/filesystem-backend.js +4 -2
  15. package/dist/browser/preview.js +120 -17
  16. package/dist/browser/runtime-terminals.js +53 -10
  17. package/dist/browser/shared-fs.js +124 -11
  18. package/dist/browser/worker.js +59 -6
  19. package/dist/core/descriptors.js +9 -1
  20. package/dist/core/devices.js +211 -1
  21. package/dist/core/fd-reopen.js +79 -18
  22. package/dist/core/filesystem.js +14 -0
  23. package/dist/core/open-file.js +166 -13
  24. package/dist/core/wasi.js +402 -29
  25. package/dist/fs/backend.js +51 -3
  26. package/dist/fs/client.js +204 -7
  27. package/dist/fs/cold-read-cache.js +23 -0
  28. package/dist/fs/index.js +21 -1
  29. package/dist/fs/indexeddb.js +30 -2
  30. package/dist/fs/locks.js +1112 -0
  31. package/dist/fs/memory.js +126 -2
  32. package/dist/fs/multitab-backend.js +317 -196
  33. package/dist/fs/multitab-protocol.js +2 -0
  34. package/dist/fs/persistent.js +32 -7
  35. package/dist/fs/protocol.js +9 -3
  36. package/dist/fs/service.js +387 -22
  37. package/dist/fs/shared.js +54 -1
  38. package/dist/fs/storage-geometry.js +2 -0
  39. package/dist/interrupt/rewriter-internal.js +4 -1
  40. package/dist/proc/abi.js +80 -6
  41. package/dist/proc/errors.js +1 -0
  42. package/dist/proc/filesystem-codec.js +40 -9
  43. package/dist/proc/index.js +10 -1
  44. package/dist/proc/pty.js +193 -0
  45. package/dist/proc/worker-client.js +11 -2
  46. package/dist/proc/worker-pipes.js +51 -0
  47. package/dist/runtime.js +11 -2
  48. package/dist/search/engine.js +53 -0
  49. package/dist/tooling/program/program-transformer.json +1 -1
  50. package/fs/backend.ts +54 -2
  51. package/fs/client.ts +230 -6
  52. package/fs/cold-read-cache.ts +38 -0
  53. package/fs/index.ts +22 -1
  54. package/fs/indexeddb.ts +36 -2
  55. package/fs/locks.ts +1406 -0
  56. package/fs/memory.ts +132 -1
  57. package/fs/multitab-backend.ts +563 -304
  58. package/fs/multitab-protocol.ts +2 -0
  59. package/fs/persistent.ts +46 -4
  60. package/fs/protocol.ts +17 -5
  61. package/fs/service.ts +469 -23
  62. package/fs/shared.ts +60 -2
  63. package/fs/storage-geometry.ts +3 -0
  64. package/index.ts +2 -0
  65. package/interrupt/rewriter-internal.ts +4 -1
  66. package/package.json +2 -2
  67. package/proc/abi.ts +79 -6
  68. package/proc/errors.ts +1 -0
  69. package/proc/filesystem-codec.ts +39 -9
  70. package/proc/index.ts +11 -0
  71. package/proc/pty.ts +227 -0
  72. package/proc/worker-client.ts +28 -6
  73. package/proc/worker-pipes.ts +57 -0
  74. package/runtime.ts +12 -2
  75. package/search/engine.ts +49 -0
  76. package/testing/indexeddb-storage-browser.ts +173 -2
  77. package/tooling/program/program-transformer.json +1 -1
@@ -186,6 +186,7 @@ const PURE_STATELESS_READ_OPS = new Set<string>([
186
186
  "lookup",
187
187
  "listFiles",
188
188
  "resolvePath",
189
+ "statPath",
189
190
  "stat",
190
191
  "permissions",
191
192
  "read",
@@ -193,6 +194,7 @@ const PURE_STATELESS_READ_OPS = new Set<string>([
193
194
  "readlink",
194
195
  "resolve",
195
196
  "realpath",
197
+ "probe",
196
198
  "readdirPage",
197
199
  ]);
198
200
 
package/fs/persistent.ts CHANGED
@@ -103,8 +103,9 @@ export class PersistentBackend extends MemoryBackend {
103
103
  private readonly dirtyLimits: Partial<DirtyLimits> | false = {},
104
104
  startupRoot?: MemoryBudget,
105
105
  sharedRoot?: MemoryBackendSharedRoot,
106
+ machineId?: string,
106
107
  ) {
107
- super(fs, reservation, startupRoot, undefined, sharedRoot);
108
+ super(fs, reservation, startupRoot, undefined, sharedRoot, machineId);
108
109
  this.initialWorkspaceId = reservation.workspace;
109
110
  this.initialEra = reservation.era;
110
111
  this.loadedIndex = index !== undefined;
@@ -180,7 +181,10 @@ export class PersistentBackend extends MemoryBackend {
180
181
  return this.storage.terminalFailureReason?.();
181
182
  }
182
183
  setTerminalFailureListener(listener: (reason: unknown) => void): void {
183
- this.storage.setTerminalFailureListener?.(listener);
184
+ this.storage.setTerminalFailureListener?.((reason) => {
185
+ this.cancelTimer();
186
+ listener(reason);
187
+ });
184
188
  }
185
189
  private hasPendingChanges(): boolean {
186
190
  if (this.dirty.size > 0 || this.removed.size > 0 || this.shared.publication) return true;
@@ -192,7 +196,11 @@ export class PersistentBackend extends MemoryBackend {
192
196
  private startTimer(intervalMs: number): void {
193
197
  if (intervalMs > 0)
194
198
  this.timer = setInterval(() => {
195
- if (!this.stopped && !this.pendingCheckpoints && this.hasPendingChanges())
199
+ if (this.stopped || this.isTerminallyFailed()) {
200
+ this.cancelTimer();
201
+ return;
202
+ }
203
+ if (!this.pendingCheckpoints && this.hasPendingChanges())
196
204
  void this.checkpoint().catch(() => {});
197
205
  }, intervalMs);
198
206
  }
@@ -204,6 +212,7 @@ export class PersistentBackend extends MemoryBackend {
204
212
  dirtyLimits?: Partial<DirtyLimits> | false;
205
213
  startupRoot?: MemoryBudget;
206
214
  sharedRoot?: MemoryBackendSharedRoot;
215
+ machineId?: string;
207
216
  signal?: AbortSignal;
208
217
  } = {},
209
218
  ): Promise<PersistentBackend> {
@@ -225,6 +234,7 @@ export class PersistentBackend extends MemoryBackend {
225
234
  options.dirtyLimits,
226
235
  options.startupRoot,
227
236
  options.sharedRoot,
237
+ options.machineId,
228
238
  );
229
239
  const fs = restoreTree(index);
230
240
  const root = fs.root;
@@ -239,6 +249,7 @@ export class PersistentBackend extends MemoryBackend {
239
249
  options.dirtyLimits,
240
250
  options.startupRoot,
241
251
  options.sharedRoot,
252
+ options.machineId,
242
253
  );
243
254
  try {
244
255
  if (!index.nodes[0]?.identity) {
@@ -412,17 +423,48 @@ export class PersistentBackend extends MemoryBackend {
412
423
  const cached = this.coldReads.read(key, offset, count);
413
424
  if (cached) {
414
425
  this.storage.checkPayload(key, size);
426
+ this.coldReads.noteRead(key, offset + count);
415
427
  return this.cachedDescriptorRange(cached, cancelled);
416
428
  }
429
+ // A sequential scan fetches a page-aligned readahead window in one storage read; its pages
430
+ // are verified with it and serve the next requests from the cache.
431
+ const window = this.coldReads.readWindow(key, offset, count, size);
417
432
  const backing = this.coldBacking(node, key, undefined, cancelled);
433
+ this.coldReads.noteRead(key, offset + count);
418
434
  const drained = Promise.withResolvers<void>();
419
435
  this.ownWork(drained.promise);
420
- return this.readDescriptorRange(backing, offset, count, cancelled).finally(() => {
436
+ const reply = this.readDescriptorWindow(backing, offset, count, window, cancelled);
437
+ return reply.finally(() => {
421
438
  const release = Promise.resolve().then(() => backing.retention.release());
422
439
  void release.then(drained.resolve, drained.reject);
423
440
  return release;
424
441
  });
425
442
  }
443
+ private async readDescriptorWindow(
444
+ backing: Extract<ReadBacking, { kind: "cold" }>,
445
+ offset: number,
446
+ count: number,
447
+ window: { offset: number; length: number },
448
+ cancelled: () => boolean,
449
+ ): Promise<Uint8Array> {
450
+ if (window.offset === offset && window.length === count)
451
+ return this.readDescriptorRange(backing, offset, count, cancelled);
452
+ try {
453
+ const bytes = await this.readDescriptorRange(
454
+ backing,
455
+ window.offset,
456
+ window.length,
457
+ cancelled,
458
+ );
459
+ return bytes.slice(offset - window.offset, offset - window.offset + count);
460
+ } catch (error) {
461
+ if (cancelled() || this.stopped) throw new FilesystemError(11);
462
+ if (error instanceof FilesystemError && error.errno === 11) throw error;
463
+ // An error in speculative readahead bytes must not fail a valid read; retry only the
464
+ // caller's requested range, which still enforces ownership and cancellation.
465
+ return this.readDescriptorRange(backing, offset, count, cancelled);
466
+ }
467
+ }
426
468
  private async readDescriptorRange(
427
469
  backing: Extract<ReadBacking, { kind: "cold" }>,
428
470
  offset: number,
package/fs/protocol.ts CHANGED
@@ -134,6 +134,8 @@ export interface FilesystemConnection {
134
134
  sections: SharedArrayBuffer;
135
135
  /** Nonzero lock owner token; service clears it when this identity is reaped. */
136
136
  owner: number;
137
+ /** Optional logical-process POSIX lock owner token (shared by exec successors and thread members). */
138
+ posixOwner?: number;
137
139
  maxBytes: number;
138
140
  /** Service revocation in word 0, then per-owner liveness for independent process identities. */
139
141
  ownerStates: SharedArrayBuffer;
@@ -149,6 +151,8 @@ export interface FilesystemReservation {
149
151
  targetOwner: number;
150
152
  /** Closed once; transfer state: 0 pending, 1 adopted, 2 cancelled. */
151
153
  state: SharedArrayBuffer;
154
+ /** Shared open-file control block when transferring a file or directory description. */
155
+ ofd?: SharedArrayBuffer;
152
156
  }
153
157
  export type FilesystemIdentity = Omit<FilesystemConnection, "port" | "requests">;
154
158
  export interface FilesystemOwnerConstruction {
@@ -278,15 +282,20 @@ function identityPublication(
278
282
  return domain;
279
283
  }
280
284
  /** Processes share storage, not the lifetime of the process that spawned them. */
281
- export function createProcessFilesystemIdentity(parent: FilesystemIdentity): FilesystemIdentity {
285
+ export function createProcessFilesystemIdentity(
286
+ parent: FilesystemIdentity,
287
+ posixOwner?: number,
288
+ ): FilesystemIdentity {
282
289
  const identity = createFilesystemIdentity(parent);
283
290
  identity.revocations = identity.revocations.slice(0, 1);
291
+ if (posixOwner !== undefined) identity.posixOwner = posixOwner;
284
292
  return identity;
285
293
  }
286
- /** Idempotent across the controller and guest copies of a transferred description. */
287
- export function releaseFilesystemReservation(reservation: FilesystemReservation): void {
294
+ /** Idempotent across the controller and guest copies of a transferred description.
295
+ * Returns true for the single call that transitioned the reservation to closed. */
296
+ export function releaseFilesystemReservation(reservation: FilesystemReservation): boolean {
288
297
  const state = new Int32Array(reservation.state);
289
- if (Atomics.exchange(state, 0, 1)) return;
298
+ if (Atomics.exchange(state, 0, 1)) return false;
290
299
  const target = new Int32ArrayConstructor(reservation.target, 0, CAPABILITY_LENGTH);
291
300
  for (;;) {
292
301
  const count = Atomics.load(target, reservation.targetKey + 1);
@@ -296,12 +305,15 @@ export function releaseFilesystemReservation(reservation: FilesystemReservation)
296
305
  )
297
306
  break;
298
307
  }
299
- if (Atomics.compareExchange(state, 1, 0, 2) === 0)
308
+ if (Atomics.compareExchange(state, 1, 0, 2) === 0) {
309
+ if (reservation.ofd) Atomics.sub(new Int32ArrayConstructor(reservation.ofd), 6, 1);
300
310
  Atomics.sub(
301
311
  new Int32ArrayConstructor(reservation.source, 0, CAPABILITY_LENGTH),
302
312
  reservation.sourceKey + 2,
303
313
  1,
304
314
  );
315
+ }
316
+ return true;
305
317
  }
306
318
  export function reserveIdentityCapability(
307
319
  source: FilesystemIdentity,