@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.
- package/README.md +32 -0
- package/browser/command-input.ts +86 -0
- package/browser/command-owner.ts +5 -0
- package/browser/display/guest.ts +2 -18
- package/browser/display/surface-info.ts +19 -0
- package/browser/operations/cells.ts +90 -0
- package/browser/operations/guest.ts +94 -0
- package/browser/operations/owner.ts +201 -0
- package/browser/operations/protocol.ts +16 -0
- package/browser/operations/types.ts +41 -0
- package/browser/operations/writer.ts +106 -0
- package/browser/preview1-worker.ts +8 -1
- package/browser/process-broker.ts +372 -22
- package/browser/process-start.ts +5 -2
- package/browser/run-command.ts +388 -0
- package/browser/shared-fs.ts +20 -3
- package/browser/worker-memory.ts +6 -0
- package/browser/worker.ts +121 -27
- package/dist/browser/command-input.js +82 -0
- package/dist/browser/command-owner.js +2 -0
- package/dist/browser/display/guest.js +2 -18
- package/dist/browser/display/surface-info.js +18 -0
- package/dist/browser/operations/cells.js +80 -0
- package/dist/browser/operations/guest.js +67 -0
- package/dist/browser/operations/owner.js +175 -0
- package/dist/browser/operations/protocol.js +0 -0
- package/dist/browser/operations/types.js +0 -0
- package/dist/browser/operations/writer.js +98 -0
- package/dist/browser/preview1-worker.js +4 -0
- package/dist/browser/process-broker.js +317 -18
- package/dist/browser/process-start.js +3 -0
- package/dist/browser/run-command.js +377 -0
- package/dist/browser/shared-fs.js +8 -3
- package/dist/browser/worker-memory.js +10 -1
- package/dist/browser/worker.js +83 -28
- package/dist/fs/client.js +81 -18
- package/dist/fs/resolve-observation.js +11 -0
- package/dist/fs/service.js +51 -4
- package/dist/proc/command-transfer.js +72 -0
- package/dist/proc/directory.js +9 -1
- package/dist/proc/intrinsics/host.js +4 -1
- package/dist/proc/intrinsics.js +1 -0
- package/dist/proc/pipes.js +37 -4
- package/dist/proc/registry.js +25 -14
- package/dist/proc/start-command.js +101 -0
- package/dist/proc/supervisor.js +34 -10
- package/dist/proc/worker-broker.js +86 -31
- package/dist/proc/worker-client.js +67 -31
- package/dist/programs/collection.js +2 -0
- package/dist/runtime.js +14 -1
- package/dist/tooling/program/program-transformer.json +1 -1
- package/fs/client.ts +113 -17
- package/fs/resolve-observation.ts +25 -0
- package/fs/service.ts +63 -4
- package/index.ts +5 -0
- package/package.json +9 -1
- package/proc/command-transfer.ts +96 -0
- package/proc/directory.ts +10 -1
- package/proc/intrinsics/host.ts +5 -1
- package/proc/intrinsics.ts +1 -0
- package/proc/pipes.ts +39 -6
- package/proc/registry.ts +52 -17
- package/proc/start-command.ts +157 -0
- package/proc/supervisor.ts +34 -9
- package/proc/worker-broker.ts +100 -38
- package/proc/worker-client.ts +84 -31
- package/programs/collection.ts +4 -0
- package/runtime.ts +44 -3
- package/scripts/test-browser.ts +49 -29
- package/tests/browser-controls-discovery.test.ts +2 -10
- package/tests/browser-matrix.test.ts +105 -59
- package/tests/browser-navigation.test.ts +1 -1
- package/tests/browser-runner-lifecycle.ts +23 -13
- package/tests/command-input.test.ts +40 -0
- package/tests/command-scope.test.ts +422 -0
- package/tests/filesystem-client-intrinsics.test.ts +2 -0
- package/tests/filesystem-refcount.test.ts +2 -0
- package/tests/filesystem-resolve-observation.test.ts +185 -0
- package/tests/fixtures/command-bounded-admission.ts +31 -0
- package/tests/fixtures/command-disposal.ts +47 -0
- package/tests/fixtures/command-guests.ts +123 -0
- package/tests/fixtures/operation-fs-worker.ts +114 -0
- package/tests/fixtures/operation-observer-worker.ts +41 -0
- package/tests/fixtures/operation-page.ts +522 -0
- package/tests/fixtures/process-start-worker.ts +32 -0
- package/tests/link-browser-runner.ts +95 -22
- package/tests/node-exports.test.ts +29 -0
- package/tests/operation-diagnostics-browser.ts +192 -0
- package/tests/operation-diagnostics.test.ts +291 -0
- package/tests/page-runner/browser-controls-process-fixture.ts +24 -0
- package/tests/page-runner/browser-controls-runner.test.ts +20 -0
- package/tests/page-runner/browser-controls-runner.ts +1 -0
- package/tests/page-runner/browser-controls-suite.ts +20 -3
- package/tests/page-runner/control-run.ts +9 -1
- package/tests/page-runner/driver.ts +2 -0
- package/tests/page-runner/playwright-driver.ts +1 -1
- package/tests/page-runner/suite-host.ts +26 -1
- package/tests/page-runner/suite-startup.test.ts +39 -0
- package/tests/page-runner/threads-runtime-browser.test.ts +4 -6
- package/tests/page-runner/threads-runtime-suite.test.ts +14 -5
- package/tests/page-runner/worker-capability-controls.ts +174 -15
- package/tests/pipe-reader-reuse-races.test.ts +44 -0
- package/tests/process-start-browser-runner.ts +12 -4
- package/tests/process-start-browser.ts +66 -5
- package/tests/program-build-tools.test.ts +1 -1
- package/tests/runtime-facade.test.ts +5 -0
- package/tests/start-command.test.ts +109 -0
- package/tests/threads-guest-fixture.ts +1 -1
- package/tests/threads-guest-module.ts +6 -3
- package/tests/threads-guest.test.ts +25 -0
- package/tests/threads-runtime-browser-runner.ts +36 -13
- package/tests/threads-runtime-browser.ts +231 -92
- package/tests/threads-runtime-cases.ts +18 -1
- package/tests/threads-runtime-cleanup.ts +5 -1
- package/tests/threads-runtime-fd-browser.ts +247 -0
- package/tests/threads-runtime-fd-control.ts +3 -385
- package/tests/threads-runtime-fd-descriptors.test.ts +15 -13
- package/tests/threads-runtime-fd-module.ts +13 -2
- package/tests/threads-runtime-fd-service.ts +23 -2
- package/tests/threads-runtime-growth-worker.ts +61 -0
- package/tests/threads-runtime-host.ts +17 -4
- package/tests/threads-runtime-modules.ts +23 -5
- package/tests/threads-runtime-regression-controls.ts +386 -0
- package/tests/threads-runtime-regression-modules.ts +242 -0
- package/tests/threads-runtime-service-barrier.ts +8 -1
- package/tests/webkit-diagnostics.ts +125 -0
- package/tests/webkit-error-output.test.ts +118 -0
- package/tests/worker-memory-bootstrap.test.ts +19 -1
- package/tests/worker-memory-broker.test.ts +16 -8
- package/tooling/ci-paths.test.ts +329 -61
- package/tooling/private-authority.ts +14 -0
- package/tooling/program/program-transformer.json +1 -1
- package/tooling/tarball-smoke.ts +3 -1
package/proc/worker-broker.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { commandExecTransfer } from "./command-transfer.js";
|
|
1
2
|
import { captureNativeReturn } from "./observation.js";
|
|
2
3
|
import { EXEC_COMPLETION_BYTES, publishExecCompletion } from "./exec.js";
|
|
3
4
|
import { allocateSharedControl } from "../memory/control.ts";
|
|
@@ -68,6 +69,9 @@ export interface WorkerProcessBrokerOptions {
|
|
|
68
69
|
/** Captures the exact lifetime once the supervisor owns its process record. */
|
|
69
70
|
nativeReturn?(transport: WorkerProcessTransport): () => void;
|
|
70
71
|
onAdmitted?(transport: WorkerProcessTransport): void;
|
|
72
|
+
beforeAdmit?(transport: WorkerProcessTransport): void;
|
|
73
|
+
transferCleanup?(transport: WorkerProcessTransport, cleanup: () => Promise<void>): Promise<void>;
|
|
74
|
+
retireRejected?(transport: WorkerProcessTransport): Promise<void> | undefined;
|
|
71
75
|
canExec?(transport: WorkerProcessTransport): boolean;
|
|
72
76
|
discardExec?(transport: WorkerProcessTransport): void | Promise<void>;
|
|
73
77
|
}
|
|
@@ -454,14 +458,58 @@ export class WorkerProcessBroker {
|
|
|
454
458
|
private async discardExec(
|
|
455
459
|
message: Extract<WorkerProcessMessage, { operation: "exec" }>,
|
|
456
460
|
error: unknown,
|
|
461
|
+
descriptors?: DescriptorTable,
|
|
457
462
|
): Promise<void> {
|
|
458
463
|
try {
|
|
459
|
-
this.
|
|
460
|
-
|
|
464
|
+
if (this.currentExecTransfer(message.transport))
|
|
465
|
+
await this.cleanupTransfer(message.transport, () =>
|
|
466
|
+
this.releaseExec(message.transport, descriptors),
|
|
467
|
+
);
|
|
461
468
|
} finally {
|
|
462
469
|
this.completeExec(message, -spawnError(error).errno, error);
|
|
463
470
|
}
|
|
464
471
|
}
|
|
472
|
+
private currentExecTransfer(transport: WorkerProcessTransport): boolean {
|
|
473
|
+
try {
|
|
474
|
+
const handoff = commandExecTransfer(transport);
|
|
475
|
+
return !handoff || controlLoad(handoff) === transport.generation;
|
|
476
|
+
} catch (error) {
|
|
477
|
+
if (!isCollectedProcess(error)) throw error;
|
|
478
|
+
return false;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
private confirmExec(transport: WorkerProcessTransport): void {
|
|
482
|
+
const handoff = commandExecTransfer(transport);
|
|
483
|
+
if (!handoff) return;
|
|
484
|
+
controlCas(handoff, 0, transport.generation, 0);
|
|
485
|
+
wakeWorkerProcess(transport);
|
|
486
|
+
}
|
|
487
|
+
private async releaseExec(
|
|
488
|
+
transport: WorkerProcessTransport,
|
|
489
|
+
descriptors?: DescriptorTable,
|
|
490
|
+
codec?: WorkerDescriptorCodec,
|
|
491
|
+
) {
|
|
492
|
+
const errors = this.discardResources(transport, codec);
|
|
493
|
+
try {
|
|
494
|
+
descriptors?.dispose();
|
|
495
|
+
} catch (error) {
|
|
496
|
+
errors.push(error);
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
await this.options.discardExec?.(transport);
|
|
500
|
+
} catch (error) {
|
|
501
|
+
errors.push(error);
|
|
502
|
+
}
|
|
503
|
+
this.throwCleanupErrors(errors, "Exec transfer cleanup failed");
|
|
504
|
+
this.confirmExec(transport);
|
|
505
|
+
}
|
|
506
|
+
private cleanupTransfer(transport: WorkerProcessTransport, cleanup: () => Promise<void>) {
|
|
507
|
+
return this.options.transferCleanup?.(transport, cleanup) ?? cleanup();
|
|
508
|
+
}
|
|
509
|
+
private throwCleanupErrors(errors: unknown[], message: string): void {
|
|
510
|
+
if (errors.length === 1) throw errors[0];
|
|
511
|
+
if (errors.length) throw new AggregateError(errors, message);
|
|
512
|
+
}
|
|
465
513
|
private async exec(
|
|
466
514
|
message: Extract<WorkerProcessMessage, { operation: "exec" }>,
|
|
467
515
|
senderPid?: number,
|
|
@@ -485,6 +533,10 @@ export class WorkerProcessBroker {
|
|
|
485
533
|
if (state.pid !== sender.pid || state.parentPid === 0 || !this.options.canExec?.(sender))
|
|
486
534
|
throw new ProcessError(ProcessErrno.NOTSUP, "exec requires an owned worker image");
|
|
487
535
|
const supervisor = await this.ready();
|
|
536
|
+
this.options.beforeAdmit?.(transport);
|
|
537
|
+
const handoff = commandExecTransfer(transport);
|
|
538
|
+
if (handoff && controlLoad(handoff) !== transport.generation)
|
|
539
|
+
throw new ProcessError(ProcessErrno.INVAL, "exec transfer is not current");
|
|
488
540
|
codec = this.codec(transport);
|
|
489
541
|
descriptors = new DescriptorTable();
|
|
490
542
|
installWorkerBindings(transport, descriptors, codec);
|
|
@@ -501,26 +553,32 @@ export class WorkerProcessBroker {
|
|
|
501
553
|
);
|
|
502
554
|
this.accesses.revoke(transport.pid, workerProcessDirectoryId(transport));
|
|
503
555
|
this.transports.set(transport.pid, transport);
|
|
556
|
+
this.confirmExec(transport);
|
|
504
557
|
},
|
|
505
|
-
discard: () =>
|
|
506
|
-
this.
|
|
507
|
-
return this.options.discardExec?.(transport);
|
|
508
|
-
},
|
|
558
|
+
discard: (staged) =>
|
|
559
|
+
this.cleanupTransfer(transport, () => this.releaseExec(transport, staged, codec)),
|
|
509
560
|
});
|
|
510
561
|
} catch (error) {
|
|
511
|
-
|
|
512
|
-
descriptors?.dispose();
|
|
513
|
-
} finally {
|
|
514
|
-
await this.discardExec(message, error);
|
|
515
|
-
}
|
|
562
|
+
await this.discardExec(message, error, descriptors);
|
|
516
563
|
}
|
|
517
564
|
}
|
|
518
|
-
private
|
|
565
|
+
private checkSpawnReservation(
|
|
519
566
|
transport: WorkerProcessTransport,
|
|
520
567
|
request: import("./types.js").SpawnRequest,
|
|
568
|
+
state: ReturnType<typeof workerProcessState>,
|
|
521
569
|
): void {
|
|
522
|
-
if (
|
|
570
|
+
if (transport.generation !== 1)
|
|
571
|
+
throw new ProcessError(ProcessErrno.INVAL, "spawn image generation must be one");
|
|
572
|
+
if (checkedProcessKind(request.processKind) !== state.processKind)
|
|
523
573
|
throw new ProcessError(ProcessErrno.INVAL, "spawn kind conflicts with published reservation");
|
|
574
|
+
if (
|
|
575
|
+
request.job <= 0 &&
|
|
576
|
+
(checkedJobLifetime(request.job, request.jobLifetime) !== state.jobLifetime ||
|
|
577
|
+
state.jobOwner !== state.parentPid)
|
|
578
|
+
)
|
|
579
|
+
throw new ProcessError(ProcessErrno.INVAL, "job lifetime conflicts with shared reservation");
|
|
580
|
+
if (transport.cwd !== request.cwd)
|
|
581
|
+
throw new ProcessError(ProcessErrno.INVAL, "spawn cwd conflicts with process transport");
|
|
524
582
|
}
|
|
525
583
|
private async spawn(
|
|
526
584
|
transport: WorkerProcessTransport,
|
|
@@ -539,20 +597,7 @@ export class WorkerProcessBroker {
|
|
|
539
597
|
let reason: ProcessAdmissionEvent["reason"] = "admission-failed";
|
|
540
598
|
try {
|
|
541
599
|
codec = this.codec(transport);
|
|
542
|
-
|
|
543
|
-
throw new ProcessError(ProcessErrno.INVAL, "spawn image generation must be one");
|
|
544
|
-
this.checkSpawnKind(transport, request);
|
|
545
|
-
if (
|
|
546
|
-
request.job <= 0 &&
|
|
547
|
-
(checkedJobLifetime(request.job, request.jobLifetime) !== state.jobLifetime ||
|
|
548
|
-
state.jobOwner !== state.parentPid)
|
|
549
|
-
)
|
|
550
|
-
throw new ProcessError(
|
|
551
|
-
ProcessErrno.INVAL,
|
|
552
|
-
"job lifetime conflicts with shared reservation",
|
|
553
|
-
);
|
|
554
|
-
if (transport.cwd !== request.cwd)
|
|
555
|
-
throw new ProcessError(ProcessErrno.INVAL, "spawn cwd conflicts with process transport");
|
|
600
|
+
this.checkSpawnReservation(transport, request, state);
|
|
556
601
|
if (existing)
|
|
557
602
|
throw new ProcessError(
|
|
558
603
|
ProcessErrno.SRCH,
|
|
@@ -577,6 +622,7 @@ export class WorkerProcessBroker {
|
|
|
577
622
|
reason = "broker-disposed";
|
|
578
623
|
throw new ProcessError(ProcessErrno.SRCH, "process broker is disposed");
|
|
579
624
|
}
|
|
625
|
+
this.options.beforeAdmit?.(transport);
|
|
580
626
|
// Reserve the authoritative leader/owner before a codec can fail. Transferred
|
|
581
627
|
// endpoints belong to this child, not the parent's mutable or already-reaped table.
|
|
582
628
|
this.transports.set(state.pid, transport);
|
|
@@ -640,7 +686,9 @@ export class WorkerProcessBroker {
|
|
|
640
686
|
this.pump();
|
|
641
687
|
} catch (error) {
|
|
642
688
|
this.trace(transport, "rejected", reason);
|
|
643
|
-
await this.
|
|
689
|
+
await this.cleanupTransfer(transport, () =>
|
|
690
|
+
this.rejectSpawn(transport, error, codec, admitted, existing),
|
|
691
|
+
);
|
|
644
692
|
|
|
645
693
|
throw error;
|
|
646
694
|
}
|
|
@@ -818,8 +866,11 @@ export class WorkerProcessBroker {
|
|
|
818
866
|
const state = workerProcessState(transport);
|
|
819
867
|
if (admitted) {
|
|
820
868
|
// Discard is idempotent with imported descriptions released by failStart.
|
|
821
|
-
|
|
869
|
+
const errors = this.discardResources(transport, codec);
|
|
870
|
+
if (errors.length) this.failedCleanup.add(state.pid);
|
|
822
871
|
this.supervisor!.failStart(state.pid, error);
|
|
872
|
+
if (transport.commandTransfer)
|
|
873
|
+
this.throwCleanupErrors(errors, "Spawn transfer cleanup failed");
|
|
823
874
|
return;
|
|
824
875
|
}
|
|
825
876
|
if (!existing) {
|
|
@@ -827,11 +878,22 @@ export class WorkerProcessBroker {
|
|
|
827
878
|
this.failedCleanup.add(state.pid);
|
|
828
879
|
}
|
|
829
880
|
claimOutcome(state.signals.outcome, startFailure(error), undefined, describeStartError(error));
|
|
830
|
-
const
|
|
881
|
+
const errors = this.discardResources(transport, codec);
|
|
882
|
+
const cleanup = { resourcesReleased: errors.length === 0, fenced: false };
|
|
831
883
|
if (!existing) this.rejectedCleanup.set(state.pid, cleanup);
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
884
|
+
if (transport.commandTransfer) {
|
|
885
|
+
try {
|
|
886
|
+
await this.options.quiesce?.(state.pid, transport);
|
|
887
|
+
} catch (error) {
|
|
888
|
+
errors.push(error);
|
|
889
|
+
}
|
|
890
|
+
this.throwCleanupErrors(errors, "Spawn transfer cleanup failed");
|
|
891
|
+
await this.options.retireRejected?.(transport);
|
|
892
|
+
} else {
|
|
893
|
+
const returned = this.options.nativeReturn?.(transport) ?? captureNativeReturn(transport);
|
|
894
|
+
await this.options.quiesce?.(state.pid, transport);
|
|
895
|
+
returned();
|
|
896
|
+
}
|
|
835
897
|
cleanup.fenced = true;
|
|
836
898
|
if (!cleanup.resourcesReleased) return;
|
|
837
899
|
if (!existing) {
|
|
@@ -845,18 +907,18 @@ export class WorkerProcessBroker {
|
|
|
845
907
|
private discardResources(
|
|
846
908
|
transport: WorkerProcessTransport,
|
|
847
909
|
codec?: WorkerDescriptorCodec,
|
|
848
|
-
):
|
|
849
|
-
|
|
910
|
+
): unknown[] {
|
|
911
|
+
const errors: unknown[] = [];
|
|
850
912
|
for (const token of transport.resources) {
|
|
851
913
|
try {
|
|
852
914
|
if (this.options.discard) this.options.discard(token);
|
|
853
915
|
else (codec ??= this.codec(transport)).discard(token);
|
|
854
916
|
} catch (error) {
|
|
855
|
-
|
|
917
|
+
errors.push(error);
|
|
856
918
|
this.report(error);
|
|
857
919
|
}
|
|
858
920
|
}
|
|
859
|
-
return
|
|
921
|
+
return errors;
|
|
860
922
|
}
|
|
861
923
|
dispose(): void {
|
|
862
924
|
if (this.disposed) return;
|
|
@@ -909,7 +971,7 @@ export class WorkerProcessBroker {
|
|
|
909
971
|
wakeWorkerProcess(transport);
|
|
910
972
|
continue;
|
|
911
973
|
}
|
|
912
|
-
const released = this.discardResources(transport);
|
|
974
|
+
const released = this.discardResources(transport).length === 0;
|
|
913
975
|
const gate = cleanup.get(pid);
|
|
914
976
|
if (gate && !released) gate.complete = false;
|
|
915
977
|
const rejected = this.rejectedCleanup.get(pid);
|
package/proc/worker-client.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
commandExecTransfer,
|
|
3
|
+
commandTransferFailed,
|
|
4
|
+
initializeCommandExec,
|
|
5
|
+
recordCommandTransferFailure,
|
|
6
|
+
} from "./command-transfer.js";
|
|
1
7
|
import { captureNativeReturn } from "./observation.js";
|
|
2
8
|
import { EXEC_COMPLETION_BYTES } from "./exec.js";
|
|
3
9
|
import type { PipePublicationRegistryHandle } from "./pipes.js";
|
|
@@ -25,7 +31,7 @@ import {
|
|
|
25
31
|
import { spawnError, startFailure } from "./start-failure.js";
|
|
26
32
|
import { describeStartError } from "./start-diagnostic.ts";
|
|
27
33
|
import { MAX_ACCESS_REQUESTS } from "./access.js";
|
|
28
|
-
import { controlLoad, controlStore } from "./control.js";
|
|
34
|
+
import { controlCas, controlLoad, controlStore } from "./control.js";
|
|
29
35
|
import {
|
|
30
36
|
createDirectory,
|
|
31
37
|
beginDirectoryAdmission,
|
|
@@ -102,6 +108,7 @@ export interface WorkerDescriptorCodec {
|
|
|
102
108
|
export interface WorkerProcessTransport {
|
|
103
109
|
readonly kind: "directory";
|
|
104
110
|
readonly generation: number;
|
|
111
|
+
readonly commandTransfer?: SharedArrayBuffer;
|
|
105
112
|
directory: SharedArrayBuffer;
|
|
106
113
|
readonly lifetime: SharedArrayBuffer;
|
|
107
114
|
identity: SharedArrayBuffer;
|
|
@@ -183,7 +190,10 @@ export type WorkerProcessMessage =
|
|
|
183
190
|
|
|
184
191
|
export const workerProcessState = directoryState;
|
|
185
192
|
function reserveRecord(
|
|
186
|
-
transport: Pick<
|
|
193
|
+
transport: Pick<
|
|
194
|
+
WorkerProcessTransport,
|
|
195
|
+
"directory" | "slot" | "pid" | "identity" | "commandTransfer"
|
|
196
|
+
>,
|
|
187
197
|
pid: number,
|
|
188
198
|
parentPid: number,
|
|
189
199
|
job: number,
|
|
@@ -194,7 +204,7 @@ function reserveRecord(
|
|
|
194
204
|
cwd = "/",
|
|
195
205
|
processKind: ProcessKind = "process",
|
|
196
206
|
): WorkerProcessTransport {
|
|
197
|
-
|
|
207
|
+
const reserved: WorkerProcessTransport = {
|
|
198
208
|
...reserveDirectoryProcess(
|
|
199
209
|
transport,
|
|
200
210
|
pid,
|
|
@@ -212,6 +222,10 @@ function reserveRecord(
|
|
|
212
222
|
resources: [],
|
|
213
223
|
cwd,
|
|
214
224
|
};
|
|
225
|
+
if (!transport.commandTransfer) return reserved;
|
|
226
|
+
const scoped = { ...reserved, commandTransfer: transport.commandTransfer };
|
|
227
|
+
initializeCommandExec(scoped);
|
|
228
|
+
return scoped;
|
|
215
229
|
}
|
|
216
230
|
export function workerProcessDirectoryId(transport: WorkerProcessTransport): string {
|
|
217
231
|
const header = new Uint32ArrayConstructor(transport.directory, 0, 8);
|
|
@@ -282,8 +296,9 @@ export interface WorkerProcessClientOptions {
|
|
|
282
296
|
descriptors: DescriptorTable;
|
|
283
297
|
postMessage(message: WorkerProcessMessage): void;
|
|
284
298
|
codec: WorkerDescriptorCodec;
|
|
285
|
-
|
|
286
|
-
|
|
299
|
+
beginSpawnTransfer?(): () => void;
|
|
300
|
+
prepareSpawn?(pid: number, cleanupFailed?: (error: unknown) => void): WorkerSpawnTransfer;
|
|
301
|
+
prepareExec?(pid: number, cleanupFailed?: (error: unknown) => void): WorkerSpawnTransfer;
|
|
287
302
|
registerRevocation?(hook: () => void): () => void;
|
|
288
303
|
/** Synchronously freeze shared endpoint cursors after default signal death is selected.
|
|
289
304
|
* A shared pipe publication registry supplies this hook without entering the guest worker.
|
|
@@ -354,7 +369,7 @@ export class WorkerProcessClient {
|
|
|
354
369
|
}
|
|
355
370
|
}
|
|
356
371
|
exec(pid: number, request: ExecRequest): SharedArrayBuffer {
|
|
357
|
-
this.
|
|
372
|
+
this.admitTransfer();
|
|
358
373
|
if (workerProcessState(this.options.transport).processKind === "thread")
|
|
359
374
|
throw new ProcessError(ProcessErrno.NOTSUP, "thread members cannot exec");
|
|
360
375
|
if ("processKind" in request)
|
|
@@ -380,12 +395,19 @@ export class WorkerProcessClient {
|
|
|
380
395
|
bindings: [],
|
|
381
396
|
resources: [],
|
|
382
397
|
};
|
|
398
|
+
const handoff = commandExecTransfer(staged);
|
|
399
|
+
let published = false;
|
|
383
400
|
try {
|
|
384
|
-
transfer = this.options.prepareExec(pid)
|
|
401
|
+
transfer = this.options.prepareExec(pid, (error) =>
|
|
402
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error),
|
|
403
|
+
);
|
|
385
404
|
codec = transfer.codec ?? codec;
|
|
386
405
|
staged.data = transfer.data;
|
|
387
406
|
this.exportBindings(staged, snapshot, sources, references, codec);
|
|
388
|
-
this.
|
|
407
|
+
this.admitTransfer();
|
|
408
|
+
if (handoff && controlCas(handoff, 0, 0, staged.generation) !== 0)
|
|
409
|
+
throw new ProcessError(ProcessErrno.NOTSUP, "an exec transfer is already pending");
|
|
410
|
+
published = true;
|
|
389
411
|
this.options.postMessage({
|
|
390
412
|
type: "runtime-proc",
|
|
391
413
|
operation: "exec",
|
|
@@ -400,10 +422,10 @@ export class WorkerProcessClient {
|
|
|
400
422
|
completion,
|
|
401
423
|
});
|
|
402
424
|
} catch (error) {
|
|
403
|
-
for (let index = 0; index < staged.resources.length; index++)
|
|
404
|
-
this.discardSpawnResource(codec, staged.resources[index]);
|
|
405
425
|
try {
|
|
406
|
-
|
|
426
|
+
if (!published || !handoff)
|
|
427
|
+
this.rollbackTransfer(codec, staged.resources, transfer, "exec", staged.generation);
|
|
428
|
+
else recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
407
429
|
} finally {
|
|
408
430
|
transfer?.release?.();
|
|
409
431
|
}
|
|
@@ -412,11 +434,13 @@ export class WorkerProcessClient {
|
|
|
412
434
|
try {
|
|
413
435
|
transfer?.commit?.();
|
|
414
436
|
} catch (error) {
|
|
437
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
415
438
|
this.report(error);
|
|
416
439
|
}
|
|
417
440
|
try {
|
|
418
441
|
transfer?.release?.();
|
|
419
442
|
} catch (error) {
|
|
443
|
+
recordCommandTransferFailure(this.options.transport, "exec", staged.generation, error);
|
|
420
444
|
this.report(error);
|
|
421
445
|
}
|
|
422
446
|
return completion;
|
|
@@ -447,6 +471,11 @@ export class WorkerProcessClient {
|
|
|
447
471
|
});
|
|
448
472
|
}
|
|
449
473
|
}
|
|
474
|
+
private admitTransfer(): void {
|
|
475
|
+
this.admit();
|
|
476
|
+
if (commandTransferFailed(this.options.transport.commandTransfer))
|
|
477
|
+
throw new ProcessError(ProcessErrno.SRCH, "command transfer cleanup failed");
|
|
478
|
+
}
|
|
450
479
|
private admit(): void {
|
|
451
480
|
if (readOutcome(this.signals.outcome)) {
|
|
452
481
|
this.retireAccesses();
|
|
@@ -554,14 +583,20 @@ export class WorkerProcessClient {
|
|
|
554
583
|
}
|
|
555
584
|
private reserveSpawn(parentPid: number, request: SpawnRequest): number {
|
|
556
585
|
// A collected caller has no authority even to stamp this shared directory.
|
|
557
|
-
this.
|
|
586
|
+
this.admitTransfer();
|
|
558
587
|
if (parentPid !== this.pid)
|
|
559
588
|
throw new ProcessError(ProcessErrno.SRCH, "invalid spawning process");
|
|
560
|
-
const
|
|
589
|
+
const endTransfer = this.options.beginSpawnTransfer?.();
|
|
590
|
+
let release: (() => void) | undefined;
|
|
561
591
|
try {
|
|
592
|
+
release = beginDirectoryAdmission(this.options.transport.directory);
|
|
562
593
|
return this.reserveSpawnIntent(parentPid, request);
|
|
563
594
|
} finally {
|
|
564
|
-
|
|
595
|
+
try {
|
|
596
|
+
release?.();
|
|
597
|
+
} finally {
|
|
598
|
+
endTransfer?.();
|
|
599
|
+
}
|
|
565
600
|
}
|
|
566
601
|
}
|
|
567
602
|
private reserveSpawnIntent(parentPid: number, request: SpawnRequest): number {
|
|
@@ -601,7 +636,9 @@ export class WorkerProcessClient {
|
|
|
601
636
|
try {
|
|
602
637
|
// Acquire the host admission fence before touching shared membership. Fatal
|
|
603
638
|
// teardown must not stop this worker with an anonymous outstanding reservation.
|
|
604
|
-
transfer = this.options.prepareSpawn?.(pid)
|
|
639
|
+
transfer = this.options.prepareSpawn?.(pid, (error) =>
|
|
640
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error),
|
|
641
|
+
);
|
|
605
642
|
codec = transfer?.codec ?? codec;
|
|
606
643
|
child = reserveRecord(
|
|
607
644
|
this.options.transport,
|
|
@@ -636,8 +673,10 @@ export class WorkerProcessClient {
|
|
|
636
673
|
request: snapshot,
|
|
637
674
|
});
|
|
638
675
|
} catch (error) {
|
|
639
|
-
if (publishing)
|
|
640
|
-
|
|
676
|
+
if (publishing) {
|
|
677
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
678
|
+
this.releaseUncertainSpawn(transfer);
|
|
679
|
+
} else this.rollbackSpawn(pid, child, transfer, codec, error);
|
|
641
680
|
throw error;
|
|
642
681
|
}
|
|
643
682
|
// Publication transferred ownership. Never discard published resources if a host
|
|
@@ -645,11 +684,13 @@ export class WorkerProcessClient {
|
|
|
645
684
|
try {
|
|
646
685
|
transfer?.commit?.();
|
|
647
686
|
} catch (error) {
|
|
687
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
648
688
|
this.report(error);
|
|
649
689
|
} finally {
|
|
650
690
|
try {
|
|
651
691
|
transfer?.release?.();
|
|
652
692
|
} catch (error) {
|
|
693
|
+
recordCommandTransferFailure(this.options.transport, "spawn", pid, error);
|
|
653
694
|
this.report(error);
|
|
654
695
|
}
|
|
655
696
|
}
|
|
@@ -662,16 +703,36 @@ export class WorkerProcessClient {
|
|
|
662
703
|
this.report(error);
|
|
663
704
|
}
|
|
664
705
|
}
|
|
665
|
-
private
|
|
706
|
+
private rollbackTransfer(
|
|
707
|
+
codec: WorkerDescriptorCodec,
|
|
708
|
+
resources: readonly unknown[],
|
|
709
|
+
transfer: WorkerSpawnTransfer | undefined,
|
|
710
|
+
kind: "spawn" | "exec",
|
|
711
|
+
target: number,
|
|
712
|
+
): boolean {
|
|
713
|
+
let cleaned = true;
|
|
714
|
+
const fail = (error: unknown) => {
|
|
715
|
+
cleaned = false;
|
|
716
|
+
recordCommandTransferFailure(this.options.transport, kind, target, error);
|
|
717
|
+
this.report(error);
|
|
718
|
+
};
|
|
719
|
+
for (let index = 0; index < resources.length; index++) {
|
|
720
|
+
try {
|
|
721
|
+
codec.discard(resources[index]);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
fail(error);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
666
726
|
try {
|
|
667
|
-
|
|
668
|
-
return true;
|
|
727
|
+
transfer?.cancel?.();
|
|
669
728
|
} catch (error) {
|
|
670
|
-
|
|
671
|
-
return false;
|
|
729
|
+
fail(error);
|
|
672
730
|
}
|
|
731
|
+
return cleaned;
|
|
673
732
|
}
|
|
733
|
+
|
|
674
734
|
private rollbackSpawn(
|
|
735
|
+
pid: number,
|
|
675
736
|
child: WorkerProcessTransport | undefined,
|
|
676
737
|
transfer: WorkerSpawnTransfer | undefined,
|
|
677
738
|
codec: WorkerDescriptorCodec,
|
|
@@ -689,15 +750,7 @@ export class WorkerProcessClient {
|
|
|
689
750
|
describeStartError(error),
|
|
690
751
|
);
|
|
691
752
|
}
|
|
692
|
-
|
|
693
|
-
for (let index = 0; index < resources.length; index++)
|
|
694
|
-
if (!this.discardSpawnResource(codec, resources[index])) cleaned = false;
|
|
695
|
-
try {
|
|
696
|
-
transfer?.cancel?.();
|
|
697
|
-
} catch (cleanupError) {
|
|
698
|
-
cleaned = false;
|
|
699
|
-
this.report(cleanupError);
|
|
700
|
-
}
|
|
753
|
+
cleaned = this.rollbackTransfer(codec, child?.resources ?? [], transfer, "spawn", pid);
|
|
701
754
|
if (child && state && cleaned) {
|
|
702
755
|
captureNativeReturn(child)();
|
|
703
756
|
controlStore(state.confirmed, 0, 1);
|
package/programs/collection.ts
CHANGED
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
} from "./index.js";
|
|
9
9
|
import { matchesProgram, packageIdentity, defineProgramPackage } from "./catalog.js";
|
|
10
10
|
|
|
11
|
+
/** Internal marker shared only with the broker and its built-in Preview 1 entry. */
|
|
12
|
+
export const preview1Launch = Symbol("Preview 1 package launch");
|
|
13
|
+
|
|
11
14
|
export type ProgramMode = "onDemand" | "preload";
|
|
12
15
|
export type ProgramState = ProgramLoadState | { readonly kind: "removed" };
|
|
13
16
|
export interface ProgramHandle {
|
|
@@ -325,6 +328,7 @@ export class ProgramCollection implements RuntimePrograms {
|
|
|
325
328
|
|
|
326
329
|
const preview1Launcher: GuestLauncher = (context, service) =>
|
|
327
330
|
service.launchWorker(context, {
|
|
331
|
+
[preview1Launch]: true,
|
|
328
332
|
workerUrl: new URL("../browser/preview1-worker.ts", import.meta.url).href,
|
|
329
333
|
request: {
|
|
330
334
|
module: context.module,
|
package/runtime.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { commandDisposals, commandOwners } from "./browser/command-owner.js";
|
|
1
2
|
import { installSharedControlAllocator } from "./memory/control.js";
|
|
2
3
|
import { defaultSharedMemoryPlatform, validateDeclaration } from "./memory/platform.js";
|
|
3
4
|
import {
|
|
@@ -40,6 +41,14 @@ import type {
|
|
|
40
41
|
BrowserProcessBrokerOptions,
|
|
41
42
|
} from "./browser/process-broker.js";
|
|
42
43
|
export type { WorkerMemoryObservation } from "./browser/process-broker.js";
|
|
44
|
+
import type { OperationDiagnostics } from "./browser/operations/types.js";
|
|
45
|
+
export type {
|
|
46
|
+
OperationDiagnostics,
|
|
47
|
+
OperationImage,
|
|
48
|
+
OperationPhase,
|
|
49
|
+
OperationResult,
|
|
50
|
+
OperationSummary,
|
|
51
|
+
} from "./browser/operations/types.js";
|
|
43
52
|
export type { ProcessStartState, ProcessStartStep } from "./browser/process-start.js";
|
|
44
53
|
export type { MemoryGroupLaunch, RootMemoryGroupMember } from "./browser/process-broker.js";
|
|
45
54
|
import type { MemoryGroupPolicyOptions } from "./group/state.js";
|
|
@@ -92,7 +101,10 @@ export interface RuntimeConsumer {
|
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
/** Filesystem authority is borrowed. Only the page runtime can close its service. */
|
|
95
|
-
export type RuntimeFilesystem = Omit<
|
|
104
|
+
export type RuntimeFilesystem = Omit<
|
|
105
|
+
SharedFilesystem,
|
|
106
|
+
"client" | "dispose" | "attachSocketScope" | "connectObserved"
|
|
107
|
+
>;
|
|
96
108
|
export type RuntimeSupervisor = Omit<
|
|
97
109
|
ProcessSupervisor,
|
|
98
110
|
| "dispose"
|
|
@@ -100,12 +112,24 @@ export type RuntimeSupervisor = Omit<
|
|
|
100
112
|
| "onJobRetirement"
|
|
101
113
|
| "jobRetired"
|
|
102
114
|
| "retireFencedProcesses"
|
|
115
|
+
| "rootConsumersRetired"
|
|
103
116
|
| "beginReplacement"
|
|
104
117
|
| "acknowledgeReplacement"
|
|
105
118
|
>;
|
|
106
119
|
export type ProcessService = Omit<
|
|
107
120
|
BrowserProcessBroker,
|
|
108
|
-
|
|
121
|
+
| "dispose"
|
|
122
|
+
| "suspend"
|
|
123
|
+
| "quiesce"
|
|
124
|
+
| "ready"
|
|
125
|
+
| "retainRootGraphics"
|
|
126
|
+
| "operationDiagnostics"
|
|
127
|
+
| "prepareOperationDiagnostics"
|
|
128
|
+
| "adoptCommandRoot"
|
|
129
|
+
| "retireCommandRoot"
|
|
130
|
+
| "commandPipeHost"
|
|
131
|
+
| "checkCommandCleanup"
|
|
132
|
+
| "stopCommandRoot"
|
|
109
133
|
> & {
|
|
110
134
|
ready(): Promise<RuntimeSupervisor>;
|
|
111
135
|
};
|
|
@@ -178,6 +202,8 @@ export interface RuntimeOptions {
|
|
|
178
202
|
/** Optional admission metadata; retain a bounded history in the consumer if needed.
|
|
179
203
|
* Observers must not reenter process control. Throws and rejected async returns are ignored. */
|
|
180
204
|
onProcessAdmission?: ProcessAdmissionObserver;
|
|
205
|
+
/** Capture bounded post-entry operation observations for eligible Preview 1 commands. */
|
|
206
|
+
readonly observeOperations?: boolean;
|
|
181
207
|
/** Optional filesystem startup milestones, never awaited. See docs/runtime-startup.md. */
|
|
182
208
|
onStartupStage?: RuntimeStartupObserver;
|
|
183
209
|
/** Memory group limits: members, parallelism, page budget. See docs/memory-groups.md. */
|
|
@@ -207,6 +233,8 @@ export interface Runtime {
|
|
|
207
233
|
/** Lazy display ownership. Does not activate processes or start a GPU service. */
|
|
208
234
|
display(): Promise<RuntimeDisplay>;
|
|
209
235
|
processes(): Promise<ProcessService>;
|
|
236
|
+
/** Pull a scalar snapshot without activating process services. */
|
|
237
|
+
operationDiagnostics(): OperationDiagnostics;
|
|
210
238
|
dispose(): Promise<void>;
|
|
211
239
|
}
|
|
212
240
|
|
|
@@ -422,6 +450,7 @@ function sourceTime(): number | undefined {
|
|
|
422
450
|
}
|
|
423
451
|
|
|
424
452
|
export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
453
|
+
const observeOperations = options.observeOperations === true;
|
|
425
454
|
if (owners[ownerKey] !== undefined) throw new Error("A live page runtime already owns this page");
|
|
426
455
|
const token: { display?: Promise<RuntimeDisplayOwner | undefined>; disposal?: Promise<void> } =
|
|
427
456
|
{};
|
|
@@ -647,6 +676,11 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
647
676
|
get disposed() {
|
|
648
677
|
return disposed;
|
|
649
678
|
},
|
|
679
|
+
operationDiagnostics() {
|
|
680
|
+
assertLive();
|
|
681
|
+
if (!observeOperations) return { status: "disabled" };
|
|
682
|
+
return host?.operationDiagnostics() ?? { status: "empty" };
|
|
683
|
+
},
|
|
650
684
|
registerAdapter(name, launch) {
|
|
651
685
|
return registerAdapter(name, "guest", launch);
|
|
652
686
|
},
|
|
@@ -815,7 +849,10 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
815
849
|
reservations,
|
|
816
850
|
hostIdentity,
|
|
817
851
|
filesystem: {
|
|
818
|
-
connect: (identity, headroom) =>
|
|
852
|
+
connect: (identity, headroom, operations) =>
|
|
853
|
+
operations
|
|
854
|
+
? filesystem.connectObserved(identity, headroom, operations)
|
|
855
|
+
: filesystem.connect(identity, headroom),
|
|
819
856
|
revoke: (identity) => filesystem.revoke(identity),
|
|
820
857
|
reap: (identity) => filesystem.reap(identity),
|
|
821
858
|
quiesce: (identity) => filesystem.quiesce(identity),
|
|
@@ -825,6 +862,7 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
825
862
|
registry: activeRegistry,
|
|
826
863
|
table,
|
|
827
864
|
onProcessAdmission: options.onProcessAdmission,
|
|
865
|
+
observeOperations,
|
|
828
866
|
memoryGroups: options.memoryGroups,
|
|
829
867
|
launchCommand: (context, capabilities) =>
|
|
830
868
|
ownLaunch(async () => {
|
|
@@ -843,6 +881,7 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
843
881
|
: import("./browser/process-broker.js").then(
|
|
844
882
|
({ BrowserProcessBroker }) => new BrowserProcessBroker(hostOptions),
|
|
845
883
|
));
|
|
884
|
+
if (observeOperations) await created.prepareOperationDiagnostics();
|
|
846
885
|
host = created;
|
|
847
886
|
return created;
|
|
848
887
|
});
|
|
@@ -867,6 +906,7 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
867
906
|
assertLive,
|
|
868
907
|
new Map([["ready", ready]]),
|
|
869
908
|
);
|
|
909
|
+
commandOwners.set(service, owner);
|
|
870
910
|
return service;
|
|
871
911
|
});
|
|
872
912
|
return activation;
|
|
@@ -965,6 +1005,7 @@ export function createRuntime(options: RuntimeOptions = {}): Runtime {
|
|
|
965
1005
|
// Publish owned process outcomes before consumer callbacks or display revocation can wake
|
|
966
1006
|
// guests. Physical stops still wait for terminationFence and the existing quiescence gates.
|
|
967
1007
|
suspend(failures);
|
|
1008
|
+
commandDisposals.get(runtime)?.abort();
|
|
968
1009
|
// Wake page operations before activation/drain can await startup. Keep the worker
|
|
969
1010
|
// and any acquired ownership alive until admitted consumer sections have fenced;
|
|
970
1011
|
// the normal final filesystem.dispose() then performs the startup abort handshake.
|