@waffle-os/runtime 0.0.149 → 0.0.151

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 (41) hide show
  1. package/browser/diagnostic.ts +234 -2
  2. package/browser/process-broker.ts +42 -4
  3. package/browser/worker.ts +34 -5
  4. package/core/open-file.ts +126 -52
  5. package/dist/browser/diagnostic.js +150 -1
  6. package/dist/browser/process-broker.js +47 -2
  7. package/dist/browser/worker.js +24 -6
  8. package/dist/core/open-file.js +112 -50
  9. package/dist/fs/client.js +290 -46
  10. package/dist/fs/protocol.js +7 -4
  11. package/dist/fs/service.js +77 -16
  12. package/dist/fs/shared.js +6 -6
  13. package/dist/interrupt/rewriter-internal.js +2 -1
  14. package/dist/proc/abi.js +152 -16
  15. package/dist/proc/command-stdio.js +81 -33
  16. package/dist/proc/directory.js +111 -0
  17. package/dist/proc/errors.js +1 -0
  18. package/dist/proc/supervisor.js +130 -24
  19. package/dist/proc/table.js +9 -1
  20. package/dist/proc/types.js +5 -0
  21. package/dist/proc/worker-client.js +34 -5
  22. package/dist/runtime.js +3 -0
  23. package/dist/thread/service.js +85 -10
  24. package/dist/tooling/program/program-transformer.json +1 -1
  25. package/fs/client.ts +298 -44
  26. package/fs/protocol.ts +9 -4
  27. package/fs/service.ts +99 -19
  28. package/fs/shared.ts +7 -7
  29. package/interrupt/rewriter-internal.ts +2 -1
  30. package/package.json +1 -1
  31. package/proc/abi.ts +185 -17
  32. package/proc/command-stdio.ts +98 -41
  33. package/proc/directory.ts +131 -0
  34. package/proc/errors.ts +1 -0
  35. package/proc/supervisor.ts +173 -36
  36. package/proc/table.ts +11 -0
  37. package/proc/types.ts +6 -0
  38. package/proc/worker-client.ts +36 -5
  39. package/runtime.ts +3 -0
  40. package/thread/service.ts +125 -7
  41. package/tooling/program/program-transformer.json +1 -1
@@ -40,6 +40,8 @@ import {
40
40
  directorySlots,
41
41
  directoryJob,
42
42
  findDirectoryJob,
43
+ probeDirectoryJob,
44
+ probeDirectoryLogicalPid,
43
45
  directoryControls,
44
46
  isCollectedProcess,
45
47
  reserveDirectoryProcess,
@@ -72,6 +74,7 @@ import {
72
74
  ProcessErrno,
73
75
  ProcessError,
74
76
  checkedSignal,
77
+ checkedKillSignal,
75
78
  checkedJobLifetime,
76
79
  type JobLifetime,
77
80
  checkedProcessCwd,
@@ -476,6 +479,10 @@ export class WorkerProcessClient {
476
479
  if (commandTransferFailed(this.options.transport.commandTransfer))
477
480
  throw new ProcessError(ProcessErrno.SRCH, "command transfer cleanup failed");
478
481
  }
482
+ private checkAlive(): void {
483
+ if (readOutcome(this.signals.outcome))
484
+ throw new ProcessError(ProcessErrno.SRCH, "process has exited");
485
+ }
479
486
  private admit(): void {
480
487
  if (readOutcome(this.signals.outcome)) {
481
488
  this.retireAccesses();
@@ -909,6 +916,7 @@ export class WorkerProcessClient {
909
916
  }
910
917
  canSignal(callerPid: number, pid: number): boolean {
911
918
  if (callerPid !== this.pid) return false;
919
+ this.checkAlive();
912
920
  const target = this.find(pid);
913
921
  return directoryControls(
914
922
  this.options.transport.directory,
@@ -931,18 +939,41 @@ export class WorkerProcessClient {
931
939
  true,
932
940
  );
933
941
  } catch (error) {
934
- if (error instanceof ProcessError && error.errno === 63) return false;
942
+ if (error instanceof ProcessError && error.errno === ProcessErrno.PERM) return false;
935
943
  throw error;
936
944
  }
937
945
  }
946
+ canProbeJob(callerPid: number, job: number): boolean {
947
+ if (callerPid !== this.pid) return false;
948
+ this.checkAlive();
949
+ return probeDirectoryJob(this.options.transport.directory, this.pid, job);
950
+ }
951
+ canSignalProcess(callerPid: number, processId: number): boolean {
952
+ if (callerPid !== this.pid) return false;
953
+ this.checkAlive();
954
+ return probeDirectoryLogicalPid(
955
+ this.options.transport.directory,
956
+ callerPid,
957
+ processId,
958
+ this.options.transport.slot,
959
+ );
960
+ }
938
961
  kill(pid: number, signal: number): void {
962
+ const sig = checkedKillSignal(signal);
939
963
  if (!this.canSignal(this.pid, pid))
940
- throw new ProcessError(63, "process is outside the caller's ancestry");
941
- this.deliver([this.find(pid)], checkedSignal(signal));
964
+ throw new ProcessError(ProcessErrno.PERM, "process is outside the caller's ancestry");
965
+ if (sig === 0) return;
966
+ this.deliver([this.find(pid)], sig);
942
967
  }
943
968
  killJob(job: number, signal: number): void {
969
+ const sig = checkedKillSignal(signal);
970
+ if (sig === 0) {
971
+ if (!this.canProbeJob(this.pid, job))
972
+ throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
973
+ return;
974
+ }
944
975
  if (!this.canSignalJob(this.pid, job))
945
- throw new ProcessError(63, "job is not owned by the caller");
976
+ throw new ProcessError(ProcessErrno.PERM, "job is not owned by the caller");
946
977
  const directory = this.options.transport.directory;
947
978
  const slots = directorySlots(directory);
948
979
  const targets: ReturnType<typeof directoryState>[] = [];
@@ -957,7 +988,7 @@ export class WorkerProcessClient {
957
988
  if (!isCollectedProcess(error)) throw error;
958
989
  }
959
990
  }
960
- this.deliver(targets, checkedSignal(signal), true);
991
+ this.deliver(targets, sig, true);
961
992
  }
962
993
  private deliver(
963
994
  targets: { slot: number; pid: number; parentPid: number; signals: SignalTransport }[],
package/runtime.ts CHANGED
@@ -313,7 +313,10 @@ const supervisorCapabilities = {
313
313
  jobOf: "method",
314
314
  cwd: "method",
315
315
  canSignal: "method",
316
+ canProbe: "method",
316
317
  canSignalJob: "method",
318
+ canProbeJob: "method",
319
+ canSignalProcess: "method",
317
320
  canWaitJob: "method",
318
321
  accessStart: "method",
319
322
  accessTake: "method",
package/thread/service.ts CHANGED
@@ -272,12 +272,28 @@ export class ThreadService {
272
272
  await Promise.race([previous?.catch(() => {}), this.cancelled.promise]);
273
273
  this.live();
274
274
  const lease = await this.offset(resource);
275
+ const leaseState: { current: "held" | "settled" | "deferred" } = { current: "held" };
275
276
  try {
276
- return await this.file(operation, resource, lease.position, lease);
277
+ return await this.file(operation, resource, lease.position, lease, (next) => {
278
+ leaseState.current = next;
279
+ });
277
280
  } finally {
278
281
  // Revocation can reject value() while remote preparation still owns this operation.
279
- if (this.phase !== "open") await this.retire(operation);
280
- lease.release();
282
+ if (this.phase !== "open") await this.retireSequential(operation, lease, leaseState.current);
283
+ if (leaseState.current !== "deferred") lease.release();
284
+ }
285
+ }
286
+
287
+ private async retireSequential(
288
+ operation: Operation,
289
+ lease: AsyncFileOffsetLease,
290
+ leaseState: "held" | "settled" | "deferred",
291
+ ): Promise<void> {
292
+ try {
293
+ await this.retire(operation);
294
+ } catch (error) {
295
+ if (leaseState === "settled") lease.release();
296
+ throw error;
281
297
  }
282
298
  }
283
299
 
@@ -296,6 +312,7 @@ export class ThreadService {
296
312
  resource: FileResource,
297
313
  position: number,
298
314
  lease?: AsyncFileOffsetLease,
315
+ onLeaseState?: (state: "settled" | "deferred") => void,
299
316
  ): Promise<ThreadResult> {
300
317
  this.live();
301
318
  const request = operation.request;
@@ -311,6 +328,7 @@ export class ThreadService {
311
328
  this.position(next);
312
329
  this.live();
313
330
  lease!.commit(next);
331
+ onLeaseState?.("settled");
314
332
  return { kind: "ok", value: next };
315
333
  }
316
334
  if (request.kind !== "read" && request.kind !== "write") throw new FilesystemError(58);
@@ -325,6 +343,7 @@ export class ThreadService {
325
343
  position,
326
344
  ]);
327
345
  lease?.commit(position + bytes.length);
346
+ if (lease) onLeaseState?.("settled");
328
347
  this.live();
329
348
  this.scatter(request.spans, bytes);
330
349
  return { kind: "ok", value: bytes.length };
@@ -336,19 +355,45 @@ export class ThreadService {
336
355
  append ? "append" : "write",
337
356
  [resource.handle, position],
338
357
  data,
358
+ lease
359
+ ? {
360
+ lease,
361
+ commit: (committed) => {
362
+ const committedWritten: number = append ? committed.written : committed;
363
+ lease.commit(append ? committed.offset : position + committedWritten);
364
+ },
365
+ onState: onLeaseState,
366
+ }
367
+ : undefined,
339
368
  );
340
369
  const written: number = append ? result.written : result;
341
- lease?.commit(append ? result.offset : position + written);
342
370
  this.live();
343
371
  return { kind: "ok", value: written };
344
372
  }
345
373
 
346
- private async request(operation: Operation, name: string, args: unknown[], data?: Uint8Array) {
374
+ private async request(
375
+ operation: Operation,
376
+ name: string,
377
+ args: unknown[],
378
+ data?: Uint8Array,
379
+ writeLease?: {
380
+ lease: AsyncFileOffsetLease;
381
+ commit: (value: any) => void;
382
+ onState?: (state: "settled" | "deferred") => void;
383
+ },
384
+ ) {
347
385
  this.live();
348
386
  const completion = this.options.filesystem.submit(name, args, data);
349
387
  operation.completions.add(completion);
388
+ let committedApplied = false;
350
389
  try {
351
- return await completion.value();
390
+ const value = await completion.value();
391
+ if (writeLease) {
392
+ committedApplied = true;
393
+ writeLease.commit(value);
394
+ writeLease.onState?.("settled");
395
+ }
396
+ return value;
352
397
  } catch (error) {
353
398
  if (
354
399
  error instanceof FilesystemTransportError ||
@@ -357,10 +402,83 @@ export class ThreadService {
357
402
  this.cancelAll();
358
403
  throw error;
359
404
  } finally {
360
- if (this.phase !== "open") await this.retire(operation);
405
+ await this.finishRequest(operation, completion, committedApplied, writeLease);
406
+ }
407
+ }
408
+
409
+ private async finishRequest(
410
+ operation: Operation,
411
+ completion: FilesystemCompletion,
412
+ committedApplied: boolean,
413
+ writeLease?: {
414
+ lease: AsyncFileOffsetLease;
415
+ commit: (value: any) => void;
416
+ onState?: (state: "settled" | "deferred") => void;
417
+ },
418
+ ): Promise<void> {
419
+ const retiredError = this.phase !== "open" ? await this.tryRetire(operation) : undefined;
420
+ if (writeLease && !committedApplied) {
421
+ this.settleWriteLease(operation, completion, writeLease, retiredError === undefined);
422
+ } else if (retiredError === undefined || committedApplied || completion.backendSettled) {
361
423
  completion.release();
362
424
  operation.completions.delete(completion);
363
425
  }
426
+ if (retiredError !== undefined) throw retiredError;
427
+ }
428
+
429
+ private settleWriteLease(
430
+ operation: Operation,
431
+ completion: FilesystemCompletion,
432
+ writeLease: {
433
+ lease: AsyncFileOffsetLease;
434
+ commit: (value: any) => void;
435
+ onState?: (state: "settled" | "deferred") => void;
436
+ },
437
+ retiredCleanly: boolean,
438
+ ): void {
439
+ // Check monotonic backendSettled BEFORE tryConsumeCommitted so a Pending->Complete
440
+ // transition can never release the completion without applying the committed offset.
441
+ if (retiredCleanly || completion.backendSettled) {
442
+ this.consumeCommitted(completion, writeLease.commit);
443
+ writeLease.onState?.("settled");
444
+ completion.release();
445
+ operation.completions.delete(completion);
446
+ return;
447
+ }
448
+ writeLease.onState?.("deferred");
449
+ this.options.filesystem.trackDeferredWrite(completion, () => {
450
+ try {
451
+ this.consumeCommitted(completion, writeLease.commit);
452
+ } finally {
453
+ completion.release();
454
+ operation.completions.delete(completion);
455
+ writeLease.lease.release();
456
+ }
457
+ });
458
+ }
459
+
460
+ private async tryRetire(operation: Operation): Promise<unknown> {
461
+ try {
462
+ await this.retire(operation);
463
+ return undefined;
464
+ } catch (error) {
465
+ return error;
466
+ }
467
+ }
468
+
469
+ private consumeCommitted(
470
+ completion: FilesystemCompletion,
471
+ onCommitted: (value: any) => void,
472
+ ): boolean {
473
+ try {
474
+ const committed = completion.tryConsumeCommitted();
475
+ if (committed === undefined) return false;
476
+ onCommitted(committed);
477
+ return true;
478
+ } catch {
479
+ // Aborted or failed write: leave offset unchanged before releasing the completion.
480
+ return false;
481
+ }
364
482
  }
365
483
 
366
484
  private async stream(operation: Operation): Promise<ThreadResult> {
@@ -1 +1 @@
1
- {"commit":"7b0a762d210309fb18e4772f1a1cb50d96602dc1","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}
1
+ {"commit":"da522cacff05cf6f18e820967a2e0f52d2f19680","buildToolsSha256":"b617652c1601f8722bb85c3a688a1fca4985b53f2c2066c10c76c2e693081137","optimizerSourceSha256":"34c4a040f3cf639f039823880822c6541c384c9b4eaa116d27dda8d9cb5c9f9e"}