Skip to content

Commit 8380273

Browse files
thymikeeclaude
andcommitted
refactor(daemon): require runtime access when constructing request execution
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 144bd59 commit 8380273

11 files changed

Lines changed: 111 additions & 28 deletions

‎packages/daemon/src/__tests__/support/unavailable-device-runtime-gateway.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
type DeviceRuntimeGateway,
55
} from '@agent-device/contracts/platform-runtime';
66
import type { PlatformRuntimeOperations } from '@agent-device/contracts/platform-runtime-operations';
7+
import type { PlatformRequestScope } from '@agent-device/contracts/platform-runtime-host';
78
import type { DeviceInfo } from '@agent-device/kernel/device';
89
import { createUnavailableRuntimeFactsForTest } from '@agent-device/test-kit';
910
import type {
@@ -34,15 +35,20 @@ export const unavailableDeviceRuntimeGateway: DeviceRuntimeGateway<PlatformRunti
3435
shutdown: async () => {},
3536
});
3637

38+
/** A request scope that is never canceled and discards diagnostics and progress. */
39+
export function platformRequestScopeFixture(): PlatformRequestScope {
40+
return {
41+
signal: new AbortController().signal,
42+
diagnostics: { emit: () => {} },
43+
progress: { report: () => {} },
44+
};
45+
}
46+
3747
export async function unavailableBinding(device: DeviceInfo) {
3848
return await unavailableDeviceRuntimeGateway.bind({
3949
device,
4050
intent: { kind: 'ordinary' },
41-
scope: {
42-
signal: new AbortController().signal,
43-
diagnostics: { emit: () => {} },
44-
progress: { report: () => {} },
45-
},
51+
scope: platformRequestScopeFixture(),
4652
});
4753
}
4854

‎packages/daemon/src/server/__tests__/request-execution-scope-host.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import {
77
import { makeSessionStore } from '../../__tests__/support/store-factory.ts';
88
import { LeaseRegistry } from '../../resources/lease-registry.ts';
99
import { createRequestExecutionScope } from '../request-execution-scope.ts';
10+
import {
11+
platformRequestScopeFixture,
12+
unavailableDeviceRuntimeGateway,
13+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
1014

1115
test('createRequestExecutionScope exposes the host of the daemon serving the request', async () => {
1216
const host = createTestHostEnvironment({
@@ -20,6 +24,8 @@ test('createRequestExecutionScope exposes the host of the daemon serving the req
2024
leaseRegistry: new LeaseRegistry(),
2125
platformServices: daemonPlatformServicesFixture(),
2226
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
27+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
28+
platformRequestScope: platformRequestScopeFixture(),
2329
});
2430

2531
expect(scope.host).toBe(host);

‎packages/daemon/src/server/__tests__/request-execution-scope-lease-expiry.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import { makeSessionStore } from '../../__tests__/support/store-factory.ts';
1313
import { LeaseRegistry } from '../../resources/lease-registry.ts';
1414
import { createRequestExecutionScope } from '../request-execution-scope.ts';
1515
import type { DaemonRequest } from '../../core/daemon-request.ts';
16+
import {
17+
platformRequestScopeFixture,
18+
unavailableDeviceRuntimeGateway,
19+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
1620

1721
bindTestCommandHost();
1822

@@ -58,6 +62,8 @@ test('expired leases remove owned sessions before the next command and free capa
5862
host: createTestHostEnvironment(),
5963
platformServices: daemonPlatformServicesFixture(),
6064
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
65+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
66+
platformRequestScope: platformRequestScopeFixture(),
6167
req: makeRequest({ command: 'snapshot' }),
6268
sessionStore,
6369
leaseRegistry,
@@ -114,6 +120,8 @@ test.each(['rebuild', 'retire'] as const)(
114120
host: createTestHostEnvironment(),
115121
platformServices: daemonPlatformServicesFixture(),
116122
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
123+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
124+
platformRequestScope: platformRequestScopeFixture(),
117125
req: makeRequest({ session: address, flags: { session: address } }),
118126
sessionStore: store,
119127
leaseRegistry: leases,

‎packages/daemon/src/server/__tests__/request-execution-scope-macos-app-lease.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { parseDaemonPolicy } from '@agent-device/daemon-contracts/daemon-policy-
1010
import type { DaemonRequest } from '../../core/daemon-request.ts';
1111
import { LeaseRegistry } from '../../resources/lease-registry.ts';
1212
import { createRequestExecutionScope } from '../request-execution-scope.ts';
13+
import {
14+
platformRequestScopeFixture,
15+
unavailableDeviceRuntimeGateway,
16+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
1317

1418
const LEASE_ID = 'a1b2c3d4e5f60718293a4b5c6d7e8f90';
1519

@@ -45,6 +49,8 @@ async function runScoped(params: {
4549
host: createTestHostEnvironment(),
4650
platformServices: daemonPlatformServicesFixture(),
4751
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
52+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
53+
platformRequestScope: platformRequestScopeFixture(),
4854
req: { token: 't', session: 'default', positionals: [], ...params.req },
4955
sessionStore: params.sessionStore,
5056
leaseRegistry: params.leaseRegistry,
@@ -129,6 +135,8 @@ test('a request under a pid-pinned macos-app lease does not run once that proces
129135
host: createTestHostEnvironment(),
130136
platformServices: daemonPlatformServicesFixture(),
131137
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
138+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
139+
platformRequestScope: platformRequestScopeFixture(),
132140
req: { token: 't', session: 'default', command: 'snapshot', positionals: [], flags: {} },
133141
sessionStore,
134142
leaseRegistry,

‎packages/daemon/src/server/__tests__/request-execution-scope-open-device-wait.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import type { DaemonRequest } from '../../core/daemon-request.ts';
1515
import { LeaseRegistry } from '../../resources/lease-registry.ts';
1616
import { readOpenWaitAttempt } from '../../sessions/open-device-contention-wait.ts';
1717
import { createRequestExecutionScope } from '../request-execution-scope.ts';
18+
import {
19+
platformRequestScopeFixture,
20+
unavailableDeviceRuntimeGateway,
21+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
1822

1923
// Everything `open --wait <ms>` is for sits between two facts. A waiting open must not hold the
2024
// device execution lock, because `close` — the thing that could free the device — needs that same
@@ -122,6 +126,8 @@ test('an open that lost the race to a free device re-waits and opens rather than
122126
host: createTestHostEnvironment(),
123127
platformServices: daemonPlatformServicesFixture(),
124128
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
129+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
130+
platformRequestScope: platformRequestScopeFixture(),
125131
req: openRequest('first-opener'),
126132
sessionStore,
127133
leaseRegistry,
@@ -130,6 +136,8 @@ test('an open that lost the race to a free device re-waits and opens rather than
130136
host: createTestHostEnvironment(),
131137
platformServices: daemonPlatformServicesFixture(),
132138
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
139+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
140+
platformRequestScope: platformRequestScopeFixture(),
133141
req: openRequest('second-opener'),
134142
sessionStore,
135143
leaseRegistry,
@@ -165,6 +173,8 @@ test('a close that frees the device mid-wait gets through while the open is wait
165173
host: createTestHostEnvironment(),
166174
platformServices: daemonPlatformServicesFixture(),
167175
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
176+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
177+
platformRequestScope: platformRequestScopeFixture(),
168178
req: openRequest('waiter'),
169179
sessionStore,
170180
leaseRegistry,
@@ -180,6 +190,8 @@ test('a close that frees the device mid-wait gets through while the open is wait
180190
host: createTestHostEnvironment(),
181191
platformServices: daemonPlatformServicesFixture(),
182192
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
193+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
194+
platformRequestScope: platformRequestScopeFixture(),
183195
req: closeRequest('holder'),
184196
sessionStore,
185197
leaseRegistry,
@@ -214,6 +226,8 @@ test('an out-of-range wait budget is refused before resolving the target device'
214226
host: createTestHostEnvironment(),
215227
platformServices: daemonPlatformServicesFixture(),
216228
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
229+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
230+
platformRequestScope: platformRequestScopeFixture(),
217231
req: openRequest('unbounded-opener', max + 1, 'request-unbounded'),
218232
sessionStore: makeSessionStore('agent-device-open-wait-bounds-'),
219233
leaseRegistry: new LeaseRegistry(),

‎packages/daemon/src/server/__tests__/request-execution-scope.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import {
2828
} from '@agent-device/test-kit';
2929
import { makeTestScreenRecordingResource } from '../../__tests__/support/screen-recording-live-handle.ts';
3030
import { handleSessionCloseCommands } from '../../sessions/session-lifecycle/internal/session-close.ts';
31+
import {
32+
platformRequestScopeFixture,
33+
unavailableDeviceRuntimeGateway,
34+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
3135

3236
bindTestCommandHost();
3337

@@ -898,7 +902,11 @@ test('router: an existing lease-less session under tenant isolation still refuse
898902
function createTestRequestScope(
899903
params: Omit<
900904
Parameters<typeof createRequestExecutionScope>[0],
901-
'platformServices' | 'claimRecoveryGateway' | 'host'
905+
| 'platformServices'
906+
| 'claimRecoveryGateway'
907+
| 'deviceRuntimeGateway'
908+
| 'platformRequestScope'
909+
| 'host'
902910
> &
903911
Partial<Pick<Parameters<typeof createRequestExecutionScope>[0], 'host'>>,
904912
) {
@@ -907,6 +915,8 @@ function createTestRequestScope(
907915
...params,
908916
platformServices: daemonPlatformServicesFixture(),
909917
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
918+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
919+
platformRequestScope: platformRequestScopeFixture(),
910920
});
911921
}
912922

‎packages/daemon/src/server/request-execution-scope.ts‎

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,6 @@ const requestScopeFinalizers = new WeakMap<
162162
(response: DaemonResponse) => DaemonResponse
163163
>();
164164

165-
const gatewayMissing = async (): Promise<never> => {
166-
throw new AppError(
167-
'COMMAND_FAILED',
168-
'Device runtime gateway is not configured for this request scope',
169-
{ reason: 'runtime-gateway-missing' },
170-
);
171-
};
172-
173165
export type RequestExecutionScope = AsyncDisposable & {
174166
/** The environment and working directory of the daemon serving this request. */
175167
host: HostEnvironment;
@@ -226,10 +218,10 @@ export async function createRequestExecutionScope(params: {
226218
sessionStore: SessionStore;
227219
leaseRegistry: LeaseRegistry;
228220
leaseLifecycleProvider?: LeaseLifecycleProvider;
229-
deviceRuntimeGateway?: DeviceRuntimeGateway<PlatformRuntimeOperations>;
221+
deviceRuntimeGateway: DeviceRuntimeGateway<PlatformRuntimeOperations>;
230222
platformServices: DaemonPlatformServices;
231223
claimRecoveryGateway: ClaimRecoveryGatewayFactory;
232-
platformRequestScope?: PlatformRequestScope;
224+
platformRequestScope: PlatformRequestScope;
233225
platformResourceCleanup?: PlatformResourceCleanup;
234226
providerAppCatalog?: ProviderAppCatalog;
235227
daemonPolicy?: DaemonPolicy;
@@ -333,9 +325,9 @@ export async function createRequestExecutionScope(params: {
333325
const disposeBindings = (): Promise<void> => {
334326
bindingCleanup ??= (async () => {
335327
try {
336-
await runtimeBindings?.[Symbol.asyncDispose]();
328+
await runtimeBindings[Symbol.asyncDispose]();
337329
} finally {
338-
await claimAdmission?.[Symbol.asyncDispose]();
330+
await claimAdmission[Symbol.asyncDispose]();
339331
}
340332
})();
341333
return bindingCleanup;
@@ -360,9 +352,9 @@ export async function createRequestExecutionScope(params: {
360352
startedAtMs,
361353
retainDeviceExecutionLock: async (deviceId) =>
362354
await requestExecutionLocks.retainDevice(deviceId),
363-
bindDevice: runtimeBindings?.bindDevice ?? gatewayMissing,
364-
inspectFacts: runtimeBindings?.inspectFacts ?? gatewayMissing,
365-
bindExactDevice: runtimeBindings?.bindExactDevice ?? gatewayMissing,
355+
bindDevice: runtimeBindings.bindDevice,
356+
inspectFacts: runtimeBindings.inspectFacts,
357+
bindExactDevice: runtimeBindings.bindExactDevice,
366358
platformServices: { ...params.platformServices },
367359
dispatchLedger,
368360
throwIfCanceled: () => throwIfRequestCanceled(scopedReq.meta?.requestId),
@@ -554,18 +546,15 @@ function createRequestDeviceAccess(params: {
554546
workspace: string;
555547
callerCwd: string | undefined;
556548
stateDir: string;
557-
deviceRuntimeGateway: DeviceRuntimeGateway<PlatformRuntimeOperations> | undefined;
558-
platformRequestScope: PlatformRequestScope | undefined;
549+
deviceRuntimeGateway: DeviceRuntimeGateway<PlatformRuntimeOperations>;
550+
platformRequestScope: PlatformRequestScope;
559551
daemonPolicy: DaemonPolicy | undefined;
560552
claimRecoveryGateway: ClaimRecoveryGatewayFactory;
561553
}): {
562-
claimAdmission: DeviceClaimAdmission | undefined;
563-
runtimeBindings: RequestRuntimeBindings | undefined;
554+
claimAdmission: DeviceClaimAdmission;
555+
runtimeBindings: RequestRuntimeBindings;
564556
} {
565557
const { deviceRuntimeGateway, platformRequestScope, daemonPolicy } = params;
566-
if (!deviceRuntimeGateway || !platformRequestScope) {
567-
return { claimAdmission: undefined, runtimeBindings: undefined };
568-
}
569558
const claimAdmission = createDeviceClaimAdmission({
570559
policy: resolveCommandDeviceClaimPolicy(params.command),
571560
command: params.command,

‎packages/daemon/src/sessions/__tests__/request-recording-health.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import { LeaseRegistry } from '../../resources/lease-registry.ts';
1414
import { makeTestScreenRecordingResource } from '../../__tests__/support/screen-recording-live-handle.ts';
1515

1616
import { refreshRecordingHealth } from '../request-recording-health.ts';
17+
import {
18+
platformRequestScopeFixture,
19+
unavailableDeviceRuntimeGateway,
20+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
1721

1822
// The observation spy hangs off the port, not the platform module behind it
1923
// (ADR 0019 section 11): the fixture's appleSessionObservation member is replaced wholesale.
@@ -208,6 +212,8 @@ test.each(['rebuild', 'retire'] as const)(
208212
// the fixture whose observation member is the test's spy.
209213
platformServices,
210214
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
215+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
216+
platformRequestScope: platformRequestScopeFixture(),
211217
});
212218
const prepared = scope.runLocked(() =>
213219
prepareLockedRequestScope({

‎packages/daemon/src/sessions/__tests__/session-idle-activity.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ import {
3131
import { withKeyedLock } from '@agent-device/kernel/keyed-lock';
3232
import type { DaemonRequest } from '../../core/daemon-request.ts';
3333
import { mkdtempForTestSync, createTestHostEnvironment } from '@agent-device/test-kit';
34+
import {
35+
platformRequestScopeFixture,
36+
unavailableDeviceRuntimeGateway,
37+
} from '../../__tests__/support/unavailable-device-runtime-gateway.ts';
3438

3539
const TEST_ROOT = mkdtempForTestSync('agent-device-session-idle-activity-');
3640
const LOG_PATH = path.join(TEST_ROOT, 'diagnostics.log');
@@ -62,6 +66,8 @@ test('a command that completes under the session lock stamps the session it ran
6266
host: createTestHostEnvironment(),
6367
platformServices: daemonPlatformServicesFixture(),
6468
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
69+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
70+
platformRequestScope: platformRequestScopeFixture(),
6571
req: makeRequest({ command: 'snapshot' }),
6672
sessionStore,
6773
leaseRegistry: new LeaseRegistry(),
@@ -92,6 +98,8 @@ test('ordinary failed lease admission retains the session activity stamp', async
9298
host: createTestHostEnvironment(),
9399
platformServices: daemonPlatformServicesFixture(),
94100
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
101+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
102+
platformRequestScope: platformRequestScopeFixture(),
95103
req: makeRequest({ command: 'home', meta: { leaseId: 'foreign-lease' } }),
96104
sessionStore,
97105
leaseRegistry,
@@ -111,6 +119,8 @@ test('a lock-exempt inventory command reports no activity, even against a claim-
111119
host: createTestHostEnvironment(),
112120
platformServices: daemonPlatformServicesFixture(),
113121
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
122+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
123+
platformRequestScope: platformRequestScopeFixture(),
114124
req: makeRequest({ command: 'devices' }),
115125
sessionStore,
116126
leaseRegistry: new LeaseRegistry(),
@@ -134,6 +144,8 @@ test('a command whose client hung up mid-flight preserves no activity', async ()
134144
host: createTestHostEnvironment(),
135145
platformServices: daemonPlatformServicesFixture(),
136146
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
147+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
148+
platformRequestScope: platformRequestScopeFixture(),
137149
req: makeRequest({ command: 'snapshot', meta: { requestId } }),
138150
sessionStore,
139151
leaseRegistry: new LeaseRegistry(),
@@ -164,6 +176,8 @@ test('a command still running when the expiry arrives blocks it until the stamp
164176
host: createTestHostEnvironment(),
165177
platformServices: daemonPlatformServicesFixture(),
166178
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
179+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
180+
platformRequestScope: platformRequestScopeFixture(),
167181
req: makeRequest({ command: 'snapshot' }),
168182
sessionStore,
169183
leaseRegistry,
@@ -217,6 +231,8 @@ test('the reaper never observes a mid-command session through the shared map', a
217231
host: createTestHostEnvironment(),
218232
platformServices: daemonPlatformServicesFixture(),
219233
claimRecoveryGateway: daemonClaimRecoveryGatewayFixture(),
234+
deviceRuntimeGateway: unavailableDeviceRuntimeGateway,
235+
platformRequestScope: platformRequestScopeFixture(),
220236
req: makeRequest({ command: 'snapshot' }),
221237
sessionStore,
222238
leaseRegistry,

0 commit comments

Comments
 (0)