@waffle-os/runtime 0.0.145 → 0.0.146

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 (133) hide show
  1. package/README.md +32 -0
  2. package/browser/command-input.ts +86 -0
  3. package/browser/command-owner.ts +5 -0
  4. package/browser/display/guest.ts +2 -18
  5. package/browser/display/surface-info.ts +19 -0
  6. package/browser/operations/cells.ts +90 -0
  7. package/browser/operations/guest.ts +94 -0
  8. package/browser/operations/owner.ts +201 -0
  9. package/browser/operations/protocol.ts +16 -0
  10. package/browser/operations/types.ts +41 -0
  11. package/browser/operations/writer.ts +106 -0
  12. package/browser/preview1-worker.ts +8 -1
  13. package/browser/process-broker.ts +372 -22
  14. package/browser/process-start.ts +5 -2
  15. package/browser/run-command.ts +388 -0
  16. package/browser/shared-fs.ts +20 -3
  17. package/browser/worker-memory.ts +6 -0
  18. package/browser/worker.ts +121 -27
  19. package/dist/browser/command-input.js +82 -0
  20. package/dist/browser/command-owner.js +2 -0
  21. package/dist/browser/display/guest.js +2 -18
  22. package/dist/browser/display/surface-info.js +18 -0
  23. package/dist/browser/operations/cells.js +80 -0
  24. package/dist/browser/operations/guest.js +67 -0
  25. package/dist/browser/operations/owner.js +175 -0
  26. package/dist/browser/operations/protocol.js +0 -0
  27. package/dist/browser/operations/types.js +0 -0
  28. package/dist/browser/operations/writer.js +98 -0
  29. package/dist/browser/preview1-worker.js +4 -0
  30. package/dist/browser/process-broker.js +317 -18
  31. package/dist/browser/process-start.js +3 -0
  32. package/dist/browser/run-command.js +377 -0
  33. package/dist/browser/shared-fs.js +8 -3
  34. package/dist/browser/worker-memory.js +10 -1
  35. package/dist/browser/worker.js +83 -28
  36. package/dist/fs/client.js +81 -18
  37. package/dist/fs/resolve-observation.js +11 -0
  38. package/dist/fs/service.js +51 -4
  39. package/dist/proc/command-transfer.js +72 -0
  40. package/dist/proc/directory.js +9 -1
  41. package/dist/proc/intrinsics/host.js +4 -1
  42. package/dist/proc/intrinsics.js +1 -0
  43. package/dist/proc/pipes.js +37 -4
  44. package/dist/proc/registry.js +25 -14
  45. package/dist/proc/start-command.js +101 -0
  46. package/dist/proc/supervisor.js +34 -10
  47. package/dist/proc/worker-broker.js +86 -31
  48. package/dist/proc/worker-client.js +67 -31
  49. package/dist/programs/collection.js +2 -0
  50. package/dist/runtime.js +14 -1
  51. package/dist/tooling/program/program-transformer.json +1 -1
  52. package/fs/client.ts +113 -17
  53. package/fs/resolve-observation.ts +25 -0
  54. package/fs/service.ts +63 -4
  55. package/index.ts +5 -0
  56. package/package.json +9 -1
  57. package/proc/command-transfer.ts +96 -0
  58. package/proc/directory.ts +10 -1
  59. package/proc/intrinsics/host.ts +5 -1
  60. package/proc/intrinsics.ts +1 -0
  61. package/proc/pipes.ts +39 -6
  62. package/proc/registry.ts +52 -17
  63. package/proc/start-command.ts +157 -0
  64. package/proc/supervisor.ts +34 -9
  65. package/proc/worker-broker.ts +100 -38
  66. package/proc/worker-client.ts +84 -31
  67. package/programs/collection.ts +4 -0
  68. package/runtime.ts +44 -3
  69. package/scripts/test-browser.ts +49 -29
  70. package/tests/browser-controls-discovery.test.ts +2 -10
  71. package/tests/browser-matrix.test.ts +105 -59
  72. package/tests/browser-navigation.test.ts +1 -1
  73. package/tests/browser-runner-lifecycle.ts +23 -13
  74. package/tests/command-input.test.ts +40 -0
  75. package/tests/command-scope.test.ts +422 -0
  76. package/tests/filesystem-client-intrinsics.test.ts +2 -0
  77. package/tests/filesystem-refcount.test.ts +2 -0
  78. package/tests/filesystem-resolve-observation.test.ts +185 -0
  79. package/tests/fixtures/command-bounded-admission.ts +31 -0
  80. package/tests/fixtures/command-disposal.ts +47 -0
  81. package/tests/fixtures/command-guests.ts +123 -0
  82. package/tests/fixtures/operation-fs-worker.ts +114 -0
  83. package/tests/fixtures/operation-observer-worker.ts +41 -0
  84. package/tests/fixtures/operation-page.ts +522 -0
  85. package/tests/fixtures/process-start-worker.ts +32 -0
  86. package/tests/link-browser-runner.ts +95 -22
  87. package/tests/node-exports.test.ts +29 -0
  88. package/tests/operation-diagnostics-browser.ts +192 -0
  89. package/tests/operation-diagnostics.test.ts +291 -0
  90. package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
  91. package/tests/page-runner/browser-controls-runner.test.ts +20 -0
  92. package/tests/page-runner/browser-controls-runner.ts +1 -0
  93. package/tests/page-runner/browser-controls-suite.ts +20 -3
  94. package/tests/page-runner/control-run.ts +9 -1
  95. package/tests/page-runner/driver.ts +2 -0
  96. package/tests/page-runner/playwright-driver.ts +1 -1
  97. package/tests/page-runner/suite-host.ts +26 -1
  98. package/tests/page-runner/suite-startup.test.ts +39 -0
  99. package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
  100. package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
  101. package/tests/page-runner/worker-capability-controls.ts +174 -15
  102. package/tests/pipe-reader-reuse-races.test.ts +44 -0
  103. package/tests/process-start-browser-runner.ts +12 -4
  104. package/tests/process-start-browser.ts +66 -5
  105. package/tests/program-build-tools.test.ts +1 -1
  106. package/tests/runtime-facade.test.ts +5 -0
  107. package/tests/start-command.test.ts +109 -0
  108. package/tests/threads-guest-fixture.ts +1 -1
  109. package/tests/threads-guest-module.ts +6 -3
  110. package/tests/threads-guest.test.ts +25 -0
  111. package/tests/threads-runtime-browser-runner.ts +36 -13
  112. package/tests/threads-runtime-browser.ts +231 -92
  113. package/tests/threads-runtime-cases.ts +18 -1
  114. package/tests/threads-runtime-cleanup.ts +5 -1
  115. package/tests/threads-runtime-fd-browser.ts +247 -0
  116. package/tests/threads-runtime-fd-control.ts +3 -385
  117. package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
  118. package/tests/threads-runtime-fd-module.ts +13 -2
  119. package/tests/threads-runtime-fd-service.ts +23 -2
  120. package/tests/threads-runtime-growth-worker.ts +61 -0
  121. package/tests/threads-runtime-host.ts +17 -4
  122. package/tests/threads-runtime-modules.ts +23 -5
  123. package/tests/threads-runtime-regression-controls.ts +386 -0
  124. package/tests/threads-runtime-regression-modules.ts +242 -0
  125. package/tests/threads-runtime-service-barrier.ts +8 -1
  126. package/tests/webkit-diagnostics.ts +125 -0
  127. package/tests/webkit-error-output.test.ts +118 -0
  128. package/tests/worker-memory-bootstrap.test.ts +19 -1
  129. package/tests/worker-memory-broker.test.ts +16 -8
  130. package/tooling/ci-paths.test.ts +329 -61
  131. package/tooling/private-authority.ts +14 -0
  132. package/tooling/program/program-transformer.json +1 -1
  133. package/tooling/tarball-smoke.ts +3 -1
@@ -1,3 +1,9 @@
1
+ import {
2
+ commandExecTransfer,
3
+ commandTransferFailed,
4
+ initializeCommandExec,
5
+ readCommandTransferFailure
6
+ } from "../proc/command-transfer.js";
1
7
  import { WorkerStartError } from "../proc/start-diagnostic.js";
2
8
  import { requireGroupPlan } from "../link/issuance.js";
3
9
  import {
@@ -47,6 +53,9 @@ import {
47
53
  import { WorkerExecutableAccess, rejectWorkerAccess } from "../proc/worker-access.js";
48
54
  import {
49
55
  directoryGroups,
56
+ directoryCapacity,
57
+ collectDirectoryJob,
58
+ idleDirectoryAdmission,
50
59
  directoryProcesses,
51
60
  directoryAdmissionVersion,
52
61
  commitDirectoryAdmission,
@@ -90,7 +99,7 @@ import {
90
99
  workerProcessState
91
100
  } from "../proc/worker-client.js";
92
101
  import { createFilesystemDescriptorCodec } from "../proc/filesystem-codec.js";
93
- import { WorkerPipeBroker } from "../proc/worker-pipes.js";
102
+ import { WorkerPipeBroker, WorkerPipeHost } from "../proc/worker-pipes.js";
94
103
  import {
95
104
  attachPipePublicationRegistry,
96
105
  PipeEndpoint,
@@ -103,6 +112,7 @@ import {
103
112
  TerminalOutputBudget
104
113
  } from "./terminal-output.js";
105
114
  import { packageIdentity } from "../programs/catalog.js";
115
+ import { preview1Launch } from "../programs/collection.js";
106
116
  import {
107
117
  CommandStdioError
108
118
  } from "../proc/command.js";
@@ -154,6 +164,7 @@ export class BrowserProcessBroker {
154
164
  roots = new Map;
155
165
  reservedRoots = new Map;
156
166
  completedRoots = new Set;
167
+ commandRoots;
157
168
  rootLeases = new Map;
158
169
  controllers = new Map;
159
170
  signalPolicies = new Map;
@@ -169,6 +180,8 @@ export class BrowserProcessBroker {
169
180
  terminalOutputs = new Map;
170
181
  registries = new Map;
171
182
  children = new Map;
183
+ #operations;
184
+ #operationsUnavailable = false;
172
185
  launchStates = new WeakMap;
173
186
  fences = new Map;
174
187
  startupDiagnostics = new Set;
@@ -219,10 +232,15 @@ export class BrowserProcessBroker {
219
232
  nativeReturn: (transport) => this.processNativeReturn(transport),
220
233
  onAdmitted: (transport) => this.acquireProcess(transport),
221
234
  quiesce: (_pid, transport) => this.quiesceUnstarted(transport),
235
+ beforeAdmit: (transport) => this.checkCommandAdmission(transport),
236
+ transferCleanup: (transport, cleanup) => this.trackCommandTransfer(transport, cleanup),
237
+ retireRejected: (transport) => this.retireRejectedCommand(transport),
222
238
  canExec: (transport) => this.canExec(transport),
223
239
  discardExec: (transport) => {
224
240
  const filesystem = this.filesystem(transport);
225
241
  this.options.filesystem.revoke(filesystem);
242
+ if (this.commandScope(transport))
243
+ return this.options.filesystem.quiesce(filesystem);
226
244
  this.options.filesystem.reap(filesystem);
227
245
  }
228
246
  });
@@ -236,6 +254,7 @@ export class BrowserProcessBroker {
236
254
  return this.activation ??= (this.options.activate?.() ?? import("../proc/index.js")).then((proc) => {
237
255
  const supervisor = this.supervisor = new proc.ProcessSupervisor({
238
256
  registry: this.options.registry,
257
+ byteBackedGuestOnly: (rootPid) => rootPid !== undefined && !!this.commandRoots?.has(rootPid),
239
258
  table: this.options.table,
240
259
  launch: (context) => this.launchPrepared(context),
241
260
  launchCommand: (context) => this.launchPrepared(context),
@@ -445,6 +464,12 @@ export class BrowserProcessBroker {
445
464
  launchAdapter(context) {
446
465
  if (this.suspended)
447
466
  throw new Error("Process broker is disposed");
467
+ const transport = this.transport.get(context.pid);
468
+ const command = transport && this.commandScope(transport);
469
+ if (command) {
470
+ this.traceProcess(context, "launching");
471
+ this.checkCommandAdmission(transport);
472
+ }
448
473
  this.acquireObservation(context.observation, true);
449
474
  const observation = this.observationOwners.get(context.pid);
450
475
  const nativeReturned = this.nativeLaunchReturns.get(context.pid) ?? observation?.acquire();
@@ -466,7 +491,7 @@ export class BrowserProcessBroker {
466
491
  const invokeLauncher = () => {
467
492
  if ("command" in context)
468
493
  return this.launchCommand(context);
469
- if (context.execution?.kind === "host")
494
+ if (command || context.execution?.kind === "host")
470
495
  return this.options.launch(context);
471
496
  const controller = this.controllerFor(context.pid);
472
497
  if (!controller?.launch)
@@ -480,7 +505,8 @@ export class BrowserProcessBroker {
480
505
  };
481
506
  if (nativeReturned)
482
507
  this.nativeLaunchReturns.set(context.pid, nativeReturned);
483
- this.traceProcess(context, "launching");
508
+ if (!command)
509
+ this.traceProcess(context, "launching");
484
510
  const launch = this.launchState(context);
485
511
  launch.active = true;
486
512
  (this.launches ??= new Set).add(context.pid);
@@ -760,6 +786,7 @@ export class BrowserProcessBroker {
760
786
  if (root && !this.options.table.job(root.job)?.members.size)
761
787
  this.options.table.removeJob(root.job);
762
788
  this.completedRoots.delete(pid);
789
+ this.commandRoots?.delete(pid);
763
790
  this.reservedRoots.delete(pid);
764
791
  this.descendantHooks.delete(pid);
765
792
  this.signalPolicies.delete(pid);
@@ -826,13 +853,14 @@ export class BrowserProcessBroker {
826
853
  const data = transport.data;
827
854
  if (data.pipeRegistry && !this.options.reservations.ownsBuffer("pipe-registry", data.pipeRegistry.buffer))
828
855
  throw new TypeError("Root bootstrap requires a ledger-charged registry");
856
+ const guestControls = this.commandRoots?.get(state.pid)?.root !== transport;
829
857
  const ticket = this.options.reservations.admit({
830
858
  pid: workerProcessState(transport).pid,
831
859
  executable: "adopted root",
832
860
  guest: options.memory === "group" ? { kind: "group" } : { kind: "declared", memories: options.memory },
833
861
  group: "none",
834
862
  connection: false,
835
- controls: true
863
+ controls: guestControls
836
864
  });
837
865
  const previousData = transport.data;
838
866
  let changedData = false;
@@ -874,7 +902,8 @@ export class BrowserProcessBroker {
874
902
  this.descendantHooks.set(state.pid, descendant);
875
903
  this.rootAdmissions.set(state.pid, ticket);
876
904
  this.rootTickets.set(state.pid, ticket.launched());
877
- this.rootGrants.set(state.pid, ticket.control);
905
+ if (guestControls)
906
+ this.rootGrants.set(state.pid, ticket.control);
878
907
  } catch (error) {
879
908
  if (changedData)
880
909
  transport.data = previousData;
@@ -889,6 +918,199 @@ export class BrowserProcessBroker {
889
918
  this.activateRuntimeRoot(supervisor, transport);
890
919
  }
891
920
  }
921
+ async adoptCommandRoot(transport, options) {
922
+ if (this.suspended || !this.supervisor)
923
+ throw new Error("Process broker is unavailable");
924
+ const processCapacity = directoryCapacity(transport.directory);
925
+ const registry = new PipePublicationRegistry({
926
+ processCapacity,
927
+ cellsPerProcess: 16,
928
+ maxCells: processCapacity * 128,
929
+ wake: workerProcessState(transport).wake,
930
+ allocate: (length, maximum) => this.options.reservations.buffer("pipe-registry", {
931
+ kind: "growable-buffer",
932
+ initial: length,
933
+ maximum: bytes(maximum)
934
+ })
935
+ });
936
+ initializeCommandExec(transport);
937
+ transport.data = { filesystem: options.filesystem, pipeRegistry: registry.handle() };
938
+ (this.commandRoots ??= new Map).set(transport.pid, {
939
+ root: transport,
940
+ pending: new Set,
941
+ failures: new Map,
942
+ stopped: false
943
+ });
944
+ try {
945
+ await this.adoptRuntimeRoot(transport, options);
946
+ } catch (error) {
947
+ if (!this.roots.has(transport.pid))
948
+ this.commandRoots?.delete(transport.pid);
949
+ throw error;
950
+ }
951
+ }
952
+ commandScope(transport) {
953
+ if (!this.commandRoots?.size)
954
+ return;
955
+ let rootPid;
956
+ try {
957
+ rootPid = workerProcessState(transport).rootPid;
958
+ } catch (error) {
959
+ if (!isCollectedProcess(error))
960
+ throw error;
961
+ return;
962
+ }
963
+ const scope = this.commandRoots.get(rootPid);
964
+ if (scope && workerProcessDirectoryId(scope.root) === workerProcessDirectoryId(transport))
965
+ return scope;
966
+ }
967
+ checkCommandAdmission(transport) {
968
+ const scope = this.commandScope(transport);
969
+ if (scope && this.commandAdmissionClosed(scope))
970
+ throw new ProcessError(ProcessErrno.SRCH, "Command is stopping");
971
+ }
972
+ commandAdmissionClosed(scope) {
973
+ return scope.stopped || scope.failures.size !== 0 || commandTransferFailed(scope.root.commandTransfer);
974
+ }
975
+ trackCommandTransfer(transport, cleanup) {
976
+ const scope = this.commandScope(transport);
977
+ if (!scope)
978
+ return cleanup();
979
+ const pending = Promise.resolve().then(cleanup);
980
+ scope.pending.add(pending);
981
+ const settle = () => {
982
+ scope.pending.delete(pending);
983
+ wakeWorkerProcess(scope.root);
984
+ };
985
+ pending.then(settle, (error) => {
986
+ scope.failures.set(transport, error);
987
+ settle();
988
+ });
989
+ return pending;
990
+ }
991
+ async retireRejectedCommand(transport) {
992
+ const returned = this.processNativeReturn(transport);
993
+ if (this.acquiredProcesses.get(transport.pid) === this.processLifetime(transport)) {
994
+ await this.retireProcess(transport.pid, true);
995
+ this.retireChildController(transport.pid);
996
+ }
997
+ returned();
998
+ }
999
+ commandCleanupFailure(scope) {
1000
+ if (scope.failures.size) {
1001
+ const [cause] = scope.failures.values();
1002
+ return new Error("Command transfer cleanup failed", { cause });
1003
+ }
1004
+ const buffer = scope.root.commandTransfer;
1005
+ return buffer ? readCommandTransferFailure(buffer)?.error : undefined;
1006
+ }
1007
+ async joinCommandExec(transport) {
1008
+ const scope = this.commandScope(transport);
1009
+ const handoff = commandExecTransfer(transport);
1010
+ if (!scope || !handoff)
1011
+ return;
1012
+ const state = workerProcessState(transport);
1013
+ for (;; ) {
1014
+ const expected = Atomics.load(state.wake, 0);
1015
+ const buffer = scope.root.commandTransfer;
1016
+ const local = buffer && readCommandTransferFailure(buffer);
1017
+ if (local?.kind === "exec" && local.pid === transport.pid && local.generation === transport.generation)
1018
+ throw local.error;
1019
+ const staged = controlLoad(handoff);
1020
+ if (staged === 0)
1021
+ return;
1022
+ for (const [transfer, cause] of scope.failures) {
1023
+ if (transfer.pid === transport.pid && transfer.generation === staged)
1024
+ throw new Error("Exec transfer cleanup failed", { cause });
1025
+ }
1026
+ await atomicsWaitAsync(state.wake, 0, expected).value;
1027
+ }
1028
+ }
1029
+ commandPipeHost(transport, postMessage) {
1030
+ const registry = this.registries.get(transport.pid);
1031
+ if (!registry || !this.commandRoots?.has(transport.pid))
1032
+ throw new Error("Command root is not admitted");
1033
+ return new WorkerPipeHost({
1034
+ transport,
1035
+ registry,
1036
+ postMessage,
1037
+ allocateShared: (length) => this.options.reservations.buffer("control", {
1038
+ kind: "fixed-buffer",
1039
+ length: bytes(length)
1040
+ })
1041
+ });
1042
+ }
1043
+ stopCommandRoot(root) {
1044
+ const scope = this.commandRoots?.get(root.pid);
1045
+ if (!scope || scope.stopped)
1046
+ return;
1047
+ scope.stopped = true;
1048
+ this.stopCommandMembers(root);
1049
+ wakeWorkerProcess(root);
1050
+ }
1051
+ stopCommandMembers(root) {
1052
+ const members = Array.from(directoryProcesses(root.directory)).filter((state) => {
1053
+ try {
1054
+ return state.pid !== root.pid && !readOutcome(state.signals.outcome);
1055
+ } catch (error) {
1056
+ if (!isCollectedProcess(error))
1057
+ throw error;
1058
+ return false;
1059
+ }
1060
+ });
1061
+ for (const state of members)
1062
+ this.requestSignalIfPresent(state.signals, 9);
1063
+ for (const state of members) {
1064
+ try {
1065
+ this.revokeRuntimeProcess(state.pid, workerProcessDirectoryId(root));
1066
+ } catch (error) {
1067
+ if (!isCollectedProcess(error))
1068
+ throw error;
1069
+ }
1070
+ }
1071
+ }
1072
+ checkCommandCleanup(root, mayCollect) {
1073
+ const scope = this.commandRoots?.get(root.pid);
1074
+ const supervisor = this.supervisor;
1075
+ if (!scope || !supervisor)
1076
+ throw new Error("Command root is not admitted");
1077
+ const version = idleDirectoryAdmission(root.directory);
1078
+ if (scope.stopped)
1079
+ this.stopCommandMembers(root);
1080
+ this.transport.pump();
1081
+ this.serviceTerminalOutput(root.pid);
1082
+ const failure = this.commandCleanupFailure(scope);
1083
+ if (failure)
1084
+ throw failure;
1085
+ const retired = supervisor.rootConsumersRetired(root.pid);
1086
+ if (!mayCollect)
1087
+ return false;
1088
+ const ownJob = workerProcessState(root).job;
1089
+ for (const group of directoryGroups(root.directory)) {
1090
+ if (group.id === ownJob)
1091
+ continue;
1092
+ try {
1093
+ if (supervisor.job(group.id)) {
1094
+ if (supervisor.jobCleanupReady(root.pid, group.id))
1095
+ supervisor.reapJob(root.pid, group.id);
1096
+ } else if (controlLoad(group.pending) === 0) {
1097
+ collectDirectoryJob(root.directory, group.slot, group.id);
1098
+ }
1099
+ } catch (error) {
1100
+ if (!isCollectedProcess(error))
1101
+ throw error;
1102
+ }
1103
+ }
1104
+ return retired && scope.pending.size === 0 && !Array.from(directoryGroups(root.directory)).some((group) => group.id !== ownJob) && !this.terminalOutputs.get(root.pid)?.pending() && version !== undefined && idleDirectoryAdmission(root.directory) === version;
1105
+ }
1106
+ async retireCommandRoot(root) {
1107
+ const owner = this.observationOwners.get(root.pid);
1108
+ await this.quiesceProcess(root.pid);
1109
+ await this.rootExit(root.pid, 0);
1110
+ this.rootStopped(root.pid);
1111
+ await owner?.join();
1112
+ this.pipes.pump();
1113
+ }
892
1114
  bindPipeRegistry(transport) {
893
1115
  const state = workerProcessState(transport);
894
1116
  const registry = this.registries.get(state.rootPid);
@@ -1166,8 +1388,9 @@ export class BrowserProcessBroker {
1166
1388
  }
1167
1389
  }
1168
1390
  const revoked = !consumingReceipt && senderRevoked(result, filesystemRevoked);
1169
- if (!sender && !retainedNotice || this.suspended || revoked) {
1170
- this.rejectMessage(message, senderPid, this.suspended ? "broker-disposed" : !sender ? "unknown-sender" : result?.kind === "signaled" ? "sender-signaled" : "sender-revoked");
1391
+ const stopping = this.commandMessageStopped(message);
1392
+ if (!sender && !retainedNotice || this.suspended || revoked || stopping) {
1393
+ this.rejectMessage(message, senderPid, this.suspended ? "broker-disposed" : !sender ? "unknown-sender" : result?.kind === "signaled" ? "sender-signaled" : stopping ? "external-rejection" : "sender-revoked");
1171
1394
  return;
1172
1395
  }
1173
1396
  if (message.operation === "access" || message.operation === "access-cancel") {
@@ -1179,6 +1402,18 @@ export class BrowserProcessBroker {
1179
1402
  if (message.operation !== "pipe")
1180
1403
  this.watch();
1181
1404
  }
1405
+ commandMessageStopped(message) {
1406
+ if (message.operation !== "spawn" && message.operation !== "exec")
1407
+ return false;
1408
+ try {
1409
+ const scope = this.commandScope(message.transport);
1410
+ return scope !== undefined && this.commandAdmissionClosed(scope);
1411
+ } catch (error) {
1412
+ if (!isCollectedProcess(error))
1413
+ throw error;
1414
+ return false;
1415
+ }
1416
+ }
1182
1417
  async accessMessage(message, senderPid, sender, result) {
1183
1418
  if (!sender || result || workerProcessState(message.transport).pid !== senderPid || workerProcessDirectoryId(sender) !== workerProcessDirectoryId(message.transport)) {
1184
1419
  this.rejectMessage(message, senderPid, "wrong-sender");
@@ -1246,24 +1481,30 @@ export class BrowserProcessBroker {
1246
1481
  reject = fail;
1247
1482
  });
1248
1483
  this.rejectedTransfers.set(key, pending);
1249
- this.releaseRejectedTransfer(transport).then(resolve, reject);
1484
+ this.trackCommandTransfer(transport, () => this.releaseRejectedTransfer(transport)).then(resolve, reject);
1250
1485
  pending.then(() => this.rejectedTransfers.delete(key), (error) => this.report(error));
1251
1486
  }
1252
1487
  async releaseRejectedTransfer(transport) {
1253
- let cleaned = true;
1488
+ const errors = [];
1254
1489
  const codec = this.discardCodec();
1255
1490
  for (const token of transport.resources) {
1256
1491
  try {
1257
1492
  codec.discard(token);
1258
1493
  } catch (error) {
1259
- cleaned = false;
1494
+ errors.push(error);
1260
1495
  this.report(error);
1261
1496
  }
1262
1497
  }
1263
1498
  wakeWorkerProcess(transport);
1264
1499
  await this.quiesceUnstarted(transport);
1265
- if (!cleaned)
1500
+ const scope = this.commandScope(transport);
1501
+ if (errors.length) {
1502
+ if (scope)
1503
+ throw new AggregateError(errors, "Rejected spawn descriptor cleanup failed");
1266
1504
  return;
1505
+ }
1506
+ if (scope)
1507
+ await this.retireRejectedCommand(transport);
1267
1508
  controlStore(workerProcessState(transport).confirmed, 0, 1);
1268
1509
  confirmWorkerProcessJob(transport);
1269
1510
  }
@@ -1346,6 +1587,52 @@ export class BrowserProcessBroker {
1346
1587
  return { ...child.identity, bytes: bytes < 0n ? null : Number(bytes) };
1347
1588
  });
1348
1589
  }
1590
+ async prepareOperationDiagnostics() {
1591
+ try {
1592
+ const { OperationOwner } = await import("./operations/owner.js");
1593
+ this.#operations = new OperationOwner((length) => this.options.reservations.buffer("control", {
1594
+ kind: "fixed-buffer",
1595
+ length: bytes(length)
1596
+ }), ({ pid, generation, captureId }) => {
1597
+ const child = this.children.get(pid);
1598
+ return child?.generation === generation && child.operationCapture === captureId;
1599
+ });
1600
+ } catch {
1601
+ this.#operationsUnavailable = true;
1602
+ }
1603
+ }
1604
+ operationDiagnostics() {
1605
+ if (!this.options.observeOperations)
1606
+ return { status: "disabled" };
1607
+ try {
1608
+ return this.#operations?.snapshot() ?? {
1609
+ status: this.#operationsUnavailable ? "unavailable" : "empty"
1610
+ };
1611
+ } catch {
1612
+ return { status: "unavailable" };
1613
+ }
1614
+ }
1615
+ captureOperations(child, options, enabled) {
1616
+ if (!enabled || !this.#operations || options.memoryGroup || options.memories?.length)
1617
+ return;
1618
+ const memories = moduleMemoryDeclarations(options.request.module);
1619
+ if (memories?.length !== 1 || memories[0]?.kind !== "private")
1620
+ return;
1621
+ try {
1622
+ const binding = this.#operations.enroll(child.identity.pid, child.generation);
1623
+ if (binding)
1624
+ child.operationCapture = binding.captureId;
1625
+ return binding;
1626
+ } catch {
1627
+ return;
1628
+ }
1629
+ }
1630
+ connectWorkerFilesystem(filesystem, arena, binding) {
1631
+ if (!binding)
1632
+ return this.options.filesystem.connect(filesystem, arena);
1633
+ const { buffer, slot, captureId } = binding;
1634
+ return this.options.filesystem.connect(filesystem, arena, { buffer, slot, captureId });
1635
+ }
1349
1636
  startStates() {
1350
1637
  const states = [];
1351
1638
  for (const child of this.children.values()) {
@@ -1433,7 +1720,7 @@ export class BrowserProcessBroker {
1433
1720
  return this.launchThreadWorker(context, options, threaded);
1434
1721
  if (WebAssembly.Module.imports(options.request.module).some((entry) => entry.module === "wasi" && entry.name === "thread-spawn"))
1435
1722
  throw new Error("Threaded Preview 1 requires validated source bytes");
1436
- return this.launchOwnedWorker(context, { ...options, network, graphics });
1723
+ return this.launchOwnedWorker(context, { ...options, network, graphics }, !!this.#operations && options[preview1Launch] === true);
1437
1724
  } catch (error) {
1438
1725
  if (!this.children.has(context.pid) && context.execution?.kind !== "host" && this.options.table.get(context.pid)?.image.execution?.kind !== "host" && typeof error === "object" && error !== null)
1439
1726
  this.neverConstructed.set(error, context.pid);
@@ -1521,7 +1808,7 @@ export class BrowserProcessBroker {
1521
1808
  this.watch();
1522
1809
  return { terminate: () => this.stop(child) };
1523
1810
  }
1524
- launchOwnedWorker(context, options) {
1811
+ launchOwnedWorker(context, options, observeOperations) {
1525
1812
  if (this.suspended) {
1526
1813
  options.network?.dispose();
1527
1814
  throw new Error("Process broker is disposed");
@@ -1582,6 +1869,7 @@ export class BrowserProcessBroker {
1582
1869
  };
1583
1870
  this.captureImageState(child);
1584
1871
  this.children.set(context.pid, child);
1872
+ const operations = this.captureOperations(child, options, observeOperations);
1585
1873
  context.registerRevocation(() => this.disposeNetwork(child));
1586
1874
  let connection;
1587
1875
  let worker;
@@ -1606,7 +1894,7 @@ export class BrowserProcessBroker {
1606
1894
  });
1607
1895
  if (options.memoryGroup)
1608
1896
  child.group = this.admitGroupMember(context.pid, transport, options.memoryGroup, ticket.group);
1609
- connection = this.options.filesystem.connect(filesystem, ticket.arena);
1897
+ connection = this.connectWorkerFilesystem(filesystem, ticket.arena, operations);
1610
1898
  if (readOutcome(context.outcome))
1611
1899
  throw new ProcessError(ProcessErrno.SRCH, "process exited before worker construction");
1612
1900
  worker = child.worker = this.workers.take(options.workerUrl);
@@ -1663,7 +1951,8 @@ export class BrowserProcessBroker {
1663
1951
  ports.push(request.network.port);
1664
1952
  if (request.graphics)
1665
1953
  ports.push(request.graphics.port);
1666
- worker.postMessage(request, group ? ports.concat(group.transfer) : ports);
1954
+ const bootstrap = operations ? [request, operations] : request;
1955
+ worker.postMessage(bootstrap, group ? ports.concat(group.transfer) : ports);
1667
1956
  markStart(child.start, "start-posted");
1668
1957
  } catch (error) {
1669
1958
  child.failed = true;
@@ -1993,7 +2282,7 @@ export class BrowserProcessBroker {
1993
2282
  if (child.finalQuiet)
1994
2283
  return;
1995
2284
  const terminal = this.terminalLifetimes.get(pid)?.quiesce();
1996
- return child.finalFence ??= Promise.all([this.imageFence(child), terminal]).then(() => {
2285
+ return child.finalFence ??= Promise.all([this.imageFence(child), terminal]).then(() => this.joinCommandExec(child.transport)).then(() => {
1997
2286
  this.collectRuntimeProcess(pid);
1998
2287
  child.finalQuiet = true;
1999
2288
  });
@@ -2080,6 +2369,9 @@ export class BrowserProcessBroker {
2080
2369
  }
2081
2370
  }
2082
2371
  }
2372
+ const transport = this.transport.get(pid);
2373
+ if (transport && this.commandScope(transport))
2374
+ return Promise.all(pending).then(() => this.joinCommandExec(transport));
2083
2375
  if (pending.length)
2084
2376
  return Promise.all(pending).then(() => {});
2085
2377
  }
@@ -2465,10 +2757,10 @@ export class BrowserProcessBroker {
2465
2757
  (this.controllerStops ??= new Map).set(pid, { pending: stopping, controllerStop: true });
2466
2758
  return stopping;
2467
2759
  }
2468
- async retireProcess(pid) {
2760
+ async retireProcess(pid, rejectedTransferFenced = false) {
2469
2761
  await (this.children.get(pid)?.launch.pending ?? this.pendingLaunches?.get(pid))?.acquired;
2470
2762
  this.observationOwners.get(pid)?.close();
2471
- if (this.suspended) {
2763
+ if (this.suspended && !rejectedTransferFenced) {
2472
2764
  await this.retirementGate.promise;
2473
2765
  return;
2474
2766
  }
@@ -2677,6 +2969,12 @@ export class BrowserProcessBroker {
2677
2969
  async quiesce() {
2678
2970
  await this.transport.settled();
2679
2971
  await Promise.all(this.rejectedTransfers.values());
2972
+ for (const scope of this.commandRoots?.values() ?? []) {
2973
+ await Promise.all(scope.pending);
2974
+ const failure = this.commandCleanupFailure(scope);
2975
+ if (failure)
2976
+ throw failure;
2977
+ }
2680
2978
  const pids = new Set(Array.from(this.options.table.values(), (record) => record.pid).concat(Array.from(this.controllers.keys()), Array.from(this.children.keys())));
2681
2979
  for (const pid of pids)
2682
2980
  this.quiesceProcess(pid);
@@ -2789,6 +3087,7 @@ export class BrowserProcessBroker {
2789
3087
  this.completedRoots.clear();
2790
3088
  this.rootLeases.clear();
2791
3089
  this.roots.clear();
3090
+ this.commandRoots?.clear();
2792
3091
  this.outputs.clear();
2793
3092
  this.terminalOutputs.clear();
2794
3093
  this.registries.clear();
@@ -8,6 +8,9 @@ const steps = [
8
8
  "runtime-loaded",
9
9
  "group-joined",
10
10
  "modules-loaded",
11
+ "installing-bindings",
12
+ "bindings-installed",
13
+ "instantiating",
11
14
  "memory-bound",
12
15
  "instantiated",
13
16
  "running"