@@ -41,9 +41,7 @@ import { finalizeDaemonSessionApplicationLifecycle } from '../application-lifecy
4141import { runtimeHintValues } from '../../sessions/session-runtime.ts' ;
4242import {
4343 closeDaemonServers ,
44- createRuntimeContext ,
4544 teardownDaemonSessionForShutdown ,
46- type RuntimeContext ,
4745 type WritableOutput ,
4846 createPluginClose ,
4947 createStartupRollback ,
@@ -69,7 +67,10 @@ import { createPerfCaptureAdmissionLedger } from '@agent-device/capture-kit/perf
6967import { createScreenRecordingAdmissionLedger } from '@agent-device/capture-kit/screen-recording-admission-ledger' ;
7068import { withCommandHost } from '@agent-device/host-kit/command' ;
7169import { cancelAllRequests } from '@agent-device/host-kit/request' ;
72- import type { RuntimeStateHandle } from '@agent-device/host-kit/runtime-slots' ;
70+ import {
71+ createRuntimeStateHandle ,
72+ type RuntimeStateHandle ,
73+ } from '@agent-device/host-kit/runtime-slots' ;
7374import { emitDiagnostic , type ResourceDiagnostic } from '@agent-device/host-kit/diagnostics' ;
7475import type { HostEnvironment } from '@agent-device/kernel/source-value' ;
7576import {
@@ -131,8 +132,6 @@ const DAEMON_RECORDING_REAP_TERM_TIMEOUT_MS = 5_000;
131132 * same factory, so the production assembly is the one every runtime test boots.
132133 */
133134export type DaemonRuntimeDeps = Readonly < {
134- /** State the composition's platform modules keep per daemon runtime (the Apple runner's). */
135- runtimeStates : readonly RuntimeStateHandle [ ] ;
136135 ownedProcessRecords : OwnedProcessRecordStore ;
137136 hostDiagnostics : HostDiagnostics ;
138137 ownerLifecycle : PlatformOwnerLifecycle ;
@@ -244,17 +243,23 @@ export function daemonStartupExitCode(error: unknown): number {
244243 return typeof exitCode === 'number' ? exitCode : 1 ;
245244}
246245
246+ /**
247+ * Starts one daemon runtime: the single entered runtime-state store, the command host, then the
248+ * composition and servers built inside both. Every read of per-runtime state (requests, close,
249+ * timers the startup spawns) runs within this handle, so two runtimes in one process never share
250+ * it and no module exposes a store of its own (#3794).
251+ */
247252export async function startDaemonRuntime (
248253 options : DaemonRuntimeOptions ,
249254) : Promise < DaemonRuntimeController > {
250- const runtimeContext = createRuntimeContext ( ) ;
251- return await runtimeContext . run (
255+ const runtimeState = createRuntimeStateHandle ( ) ;
256+ return await runtimeState . run (
252257 async ( ) =>
253258 await withCommandHost ( { host : options . host , requestEnv : undefined } , async ( ) => {
254259 const rollback = createStartupRollback ( ) ;
255- rollback . onRollback ( ( ) => runtimeContext . dispose ( ) ) ;
260+ rollback . onRollback ( ( ) => runtimeState . dispose ( ) ) ;
256261 try {
257- return await startDaemonRuntimeOnHost ( options , rollback , runtimeContext ) ;
262+ return await startDaemonRuntimeOnHost ( options , rollback , runtimeState ) ;
258263 } catch ( error ) {
259264 await rollback . run ( ) ;
260265 throw error ;
@@ -266,7 +271,7 @@ export async function startDaemonRuntime(
266271async function startDaemonRuntimeOnHost (
267272 options : DaemonRuntimeOptions ,
268273 rollback : StartupRollback ,
269- runtimeContext : RuntimeContext ,
274+ runtimeState : RuntimeStateHandle ,
270275) : Promise < DaemonRuntimeController > {
271276 const { host } = options ;
272277 const { env } = host ;
@@ -306,7 +311,6 @@ async function startDaemonRuntimeOnHost(
306311 } ) ,
307312 openWebSessionNames : ( ) => openWebSessionNames ( sessionStore ) ,
308313 } ) ;
309- runtimeContext . extend ( deps . runtimeStates ) ;
310314 const appLogAdmissionLedger = createAppLogAdmissionLedger ( ) ;
311315 const audioProbeAdmissionLedger = createAudioProbeAdmissionLedger ( ) ;
312316 const perfCaptureAdmissionLedger = createPerfCaptureAdmissionLedger ( ) ;
@@ -546,7 +550,7 @@ async function startDaemonRuntimeOnHost(
546550 const handleRequest : DaemonInvokeFn = quiesce . admit ( async ( req ) => {
547551 if ( shuttingDown ) return daemonShuttingDownResponse ( ) ;
548552 idleReap . cancel ( ) ;
549- const dispatch = runtimeContext . run ( ( ) => dispatchRequest ( req ) ) ;
553+ const dispatch = runtimeState . run ( ( ) => dispatchRequest ( req ) ) ;
550554 inFlightRequests . add ( dispatch ) ;
551555 try {
552556 return await dispatch ;
@@ -891,7 +895,7 @@ async function startDaemonRuntimeOnHost(
891895 ? undefined
892896 : deps . ownerLifecycle . clearDaemonLockConfiguration ( ) ,
893897 ] ) ;
894- if ( ! hasUnfinishedWork ( ) ) runtimeContext . dispose ( ) ;
898+ if ( ! hasUnfinishedWork ( ) ) runtimeState . dispose ( ) ;
895899 }
896900 } ;
897901
@@ -907,7 +911,7 @@ async function startDaemonRuntimeOnHost(
907911 closed . catch ( ( ) => { } ) ;
908912 const close = ( closeOptions : { cause ?: unknown } = { } ) : Promise < ShutdownReport > => {
909913 if ( ! closing ) {
910- closing = runtimeContext . run ( ( ) =>
914+ closing = runtimeState . run ( ( ) =>
911915 withCommandHost (
912916 { host, requestEnv : undefined } ,
913917 async ( ) => await closeOnHost ( closeOptions ) ,
@@ -924,7 +928,7 @@ async function startDaemonRuntimeOnHost(
924928 httpPort,
925929 close,
926930 closed,
927- run : runtimeContext . run ,
931+ run : runtimeState . run ,
928932 socketPort,
929933 token,
930934 } ;
0 commit comments