Skip to content

Commit 7eb7379

Browse files
thymikeeclaude
andcommitted
refactor(daemon): read configuration from the request host instead of the ambient scope
Handlers, claim writers and lease admission take the host (or its env) from the request execution scope, runtime-owned timers take it at construction. commandHostEnvironment() stays for command execution until the platform and root packages convert. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 665af8a commit 7eb7379

101 files changed

Lines changed: 835 additions & 174 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/daemon/src/core/session-runtime-admission.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DaemonResponse } from '@agent-device/kernel/contracts';
2+
import type { HostEnvironment } from '@agent-device/kernel/source-value';
23
import type { DaemonPlatformServices } from './platform-services.ts';
34
import type { PlatformRuntimeOperations } from '@agent-device/contracts/platform-runtime-operations';
45
import type {
@@ -14,6 +15,7 @@ import type { DaemonRequest } from './daemon-request.ts';
1415
import { unsupportedOperationResponse } from './runtime-admission.ts';
1516

1617
export type RuntimeCommandHandlerParams = Readonly<{
18+
host: HostEnvironment;
1719
req: DaemonRequest;
1820
sessionName: string;
1921
sessionStore: SessionStore;

‎packages/daemon/src/execution/handlers/__tests__/session-selector-dispatch.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ import {
3131
import type { SessionState } from '../../../core/session-state.ts';
3232
import { handleSessionCommands } from '../../../__tests__/support/handlers/session-command-harness.ts';
3333
import { refFrameState } from '../../../core/ref-frame.ts';
34-
import { createUnavailableRuntimeFactsForTest, mkdtempForTestSync } from '@agent-device/test-kit';
34+
import {
35+
createUnavailableRuntimeFactsForTest,
36+
mkdtempForTestSync,
37+
createTestHostEnvironment,
38+
} from '@agent-device/test-kit';
3539
import { makeIosSession } from '../../../__tests__/support/session-factories.ts';
3640
import { fakeCreateCommandSurface } from '../../../__tests__/support/command-surface-fake.ts';
3741

@@ -85,6 +89,7 @@ test.each(['rebuild', 'retire'] as const)(
8589
[Symbol.asyncDispose]: async () => {},
8690
};
8791
const running = handleSessionCommands({
92+
host: createTestHostEnvironment(),
8893
createCommandSurface: fakeCreateCommandSurface,
8994
platformServices: daemonPlatformServicesFixture(),
9095
req: {
@@ -187,6 +192,7 @@ test('keyboard dismiss crosses the ADR 0014 seam while keyboard status preserves
187192
// dismiss mutates the device → frame expires.
188193
const dismissRef = sessionStore.publish(sessionName, makeSession(sessionName, device));
189194
await handleSessionCommands({
195+
host: createTestHostEnvironment(),
190196
createCommandSurface: fakeCreateCommandSurface,
191197
platformServices: daemonPlatformServicesFixture(),
192198
req: {
@@ -209,6 +215,7 @@ test('keyboard dismiss crosses the ADR 0014 seam while keyboard status preserves
209215
sessionStore.retire(dismissRef);
210216
sessionStore.publish(sessionName, makeSession(sessionName, device));
211217
await handleSessionCommands({
218+
host: createTestHostEnvironment(),
212219
createCommandSurface: fakeCreateCommandSurface,
213220
platformServices: daemonPlatformServicesFixture(),
214221
req: {
@@ -254,6 +261,7 @@ test('keyboard dismiss expires the frame before the invocation runs, even when i
254261
sessionStore.publish(sessionName, makeSession(sessionName, device));
255262
await expect(
256263
handleSessionCommands({
264+
host: createTestHostEnvironment(),
257265
createCommandSurface: fakeCreateCommandSurface,
258266
platformServices: daemonPlatformServicesFixture(),
259267
req: {
@@ -277,6 +285,7 @@ test('keyboard dismiss expires the frame before the invocation runs, even when i
277285
test('keyboard requires an active session or explicit device selector', async () => {
278286
const sessionStore = makeSessionStore();
279287
const response = await handleSessionCommands({
288+
host: createTestHostEnvironment(),
280289
createCommandSurface: fakeCreateCommandSurface,
281290
platformServices: daemonPlatformServicesFixture(),
282291
req: {
@@ -306,6 +315,7 @@ test('keyboard dismiss requires active iOS session for explicit selectors', asyn
306315
const sessionStore = makeSessionStore();
307316

308317
const response = await handleSessionCommands({
318+
host: createTestHostEnvironment(),
309319
createCommandSurface: fakeCreateCommandSurface,
310320
platformServices: daemonPlatformServicesFixture(),
311321
req: {

‎packages/daemon/src/resources/__tests__/macos-app-lease.test.ts‎

Lines changed: 115 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,43 +68,61 @@ test('the leased app can be opened, driven, captured and closed', () => {
6868
assertMacOsAppLeaseAdmitsRequest(
6969
lease,
7070
request('open', ['com.example.app'], { platform: 'macos' }),
71+
undefined,
72+
process.env,
7173
);
7274
for (const command of ['snapshot', 'click', 'fill', 'type', 'press', 'scroll', 'screenshot']) {
73-
assertMacOsAppLeaseAdmitsRequest(lease, request(command), leasedSession);
75+
assertMacOsAppLeaseAdmitsRequest(lease, request(command), leasedSession, process.env);
7476
}
75-
assertMacOsAppLeaseAdmitsRequest(lease, request('close', ['com.example.app']), leasedSession);
77+
assertMacOsAppLeaseAdmitsRequest(
78+
lease,
79+
request('close', ['com.example.app']),
80+
leasedSession,
81+
process.env,
82+
);
7683
});
7784

7885
test('a lease of another backend is not confined', () => {
7986
assertMacOsAppLeaseAdmitsRequest(
8087
{ backend: 'ios-instance', deviceKey: 'ios:mobile:UDID' },
8188
request('install', ['/tmp/App.app']),
89+
undefined,
90+
process.env,
8291
);
8392
});
8493

8594
test('commands outside the allow list are refused, including ones added later', () => {
8695
for (const command of ['install', 'install_source', 'apps', 'alert', 'clipboard', 'settings']) {
8796
assertDenied(
88-
() => assertMacOsAppLeaseAdmitsRequest(lease, request(command), leasedSession),
97+
() => assertMacOsAppLeaseAdmitsRequest(lease, request(command), leasedSession, process.env),
8998
'command',
9099
);
91100
}
92101
assertDenied(
93-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('not-a-command-yet')),
102+
() =>
103+
assertMacOsAppLeaseAdmitsRequest(lease, request('not-a-command-yet'), undefined, process.env),
94104
'command',
95105
);
96106
});
97107

98108
test('open accepts only the leased bundle and nothing it could launch beside it', () => {
99109
assertDenied(
100-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.apple.finder'])),
110+
() =>
111+
assertMacOsAppLeaseAdmitsRequest(
112+
lease,
113+
request('open', ['com.apple.finder']),
114+
undefined,
115+
process.env,
116+
),
101117
'app',
102118
);
103119
assertDenied(
104120
() =>
105121
assertMacOsAppLeaseAdmitsRequest(
106122
lease,
107123
request('open', ['com.example.app', 'https://example.com']),
124+
undefined,
125+
process.env,
108126
),
109127
'app',
110128
);
@@ -113,18 +131,36 @@ test('open accepts only the leased bundle and nothing it could launch beside it'
113131
assertMacOsAppLeaseAdmitsRequest(
114132
lease,
115133
request('open', ['com.example.app'], { platform: 'ios' }),
134+
undefined,
135+
process.env,
116136
),
117137
'app',
118138
);
119139
});
120140

121141
test('nothing under the lease names a host path or launches beside the app', () => {
122142
const temp = '/tmp/agent-device-screenshot-1791219384278-ab12cd.png';
123-
assertMacOsAppLeaseAdmitsRequest(lease, request('screenshot', [temp]), leasedSession);
124-
assertMacOsAppLeaseAdmitsRequest(lease, request('screenshot', [], { out: temp }), leasedSession);
143+
assertMacOsAppLeaseAdmitsRequest(
144+
lease,
145+
request('screenshot', [temp]),
146+
leasedSession,
147+
process.env,
148+
);
149+
assertMacOsAppLeaseAdmitsRequest(
150+
lease,
151+
request('screenshot', [], { out: temp }),
152+
leasedSession,
153+
process.env,
154+
);
125155
for (const target of [['/Users/me/.zshrc'], ['/tmp/agent-device-screenshot-1-a.png/../x']]) {
126156
assertDenied(
127-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('screenshot', target), leasedSession),
157+
() =>
158+
assertMacOsAppLeaseAdmitsRequest(
159+
lease,
160+
request('screenshot', target),
161+
leasedSession,
162+
process.env,
163+
),
128164
'host-path',
129165
);
130166
}
@@ -134,6 +170,7 @@ test('nothing under the lease names a host path or launches beside the app', ()
134170
lease,
135171
request('screenshot', [], { out: '/Users/me/x.png' }),
136172
leasedSession,
173+
process.env,
137174
),
138175
'host-path',
139176
);
@@ -143,7 +180,13 @@ test('nothing under the lease names a host path or launches beside the app', ()
143180
{ saveScript: '/Users/me/x.ad' },
144181
]) {
145182
assertDenied(
146-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.example.app'], flags)),
183+
() =>
184+
assertMacOsAppLeaseAdmitsRequest(
185+
lease,
186+
request('open', ['com.example.app'], flags),
187+
undefined,
188+
process.env,
189+
),
147190
'host-path',
148191
);
149192
}
@@ -152,6 +195,8 @@ test('nothing under the lease names a host path or launches beside the app', ()
152195
assertMacOsAppLeaseAdmitsRequest(
153196
lease,
154197
request('open', ['com.example.app'], {}, { runtime: { launchUrl: 'other://open' } }),
198+
undefined,
199+
process.env,
155200
),
156201
'host-path',
157202
);
@@ -161,6 +206,7 @@ test('nothing under the lease names a host path or launches beside the app', ()
161206
lease,
162207
request('close', [], {}, { input: { saveScript: '/Users/me/x.ad' } }),
163208
leasedSession,
209+
process.env,
164210
),
165211
'host-path',
166212
);
@@ -172,6 +218,7 @@ test('nothing under the lease names a host path or launches beside the app', ()
172218
batchSteps: [{ command: 'screenshot', positionals: ['/Users/me/x.png'], input: {} }],
173219
}),
174220
leasedSession,
221+
process.env,
175222
),
176223
'host-path',
177224
);
@@ -184,6 +231,7 @@ test('close cannot name another app', () => {
184231
lease,
185232
request('close', ['com.apple.finder']),
186233
leasedSession,
234+
process.env,
187235
),
188236
'app',
189237
);
@@ -192,12 +240,22 @@ test('close cannot name another app', () => {
192240
test.each(['desktop', 'frontmost-app', 'menubar'])('the %s surface is refused', (surface) => {
193241
assertDenied(
194242
() =>
195-
assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.example.app'], { surface })),
243+
assertMacOsAppLeaseAdmitsRequest(
244+
lease,
245+
request('open', ['com.example.app'], { surface }),
246+
undefined,
247+
process.env,
248+
),
196249
'surface',
197250
);
198251
assertDenied(
199252
() =>
200-
assertMacOsAppLeaseAdmitsRequest(lease, request('snapshot', [], { surface }), leasedSession),
253+
assertMacOsAppLeaseAdmitsRequest(
254+
lease,
255+
request('snapshot', [], { surface }),
256+
leasedSession,
257+
process.env,
258+
),
201259
'surface',
202260
);
203261
});
@@ -209,6 +267,7 @@ test('screenshots stay on the app window', () => {
209267
lease,
210268
request('screenshot', [], { screenshotFullscreen: true }),
211269
leasedSession,
270+
process.env,
212271
),
213272
'capture',
214273
);
@@ -221,7 +280,7 @@ test('a session that is not the leased app is refused', () => {
221280
makeIosSession('ios', { appBundleId: 'com.example.app' }),
222281
]) {
223282
assertDenied(
224-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('snapshot'), session),
283+
() => assertMacOsAppLeaseAdmitsRequest(lease, request('snapshot'), session, process.env),
225284
'app',
226285
);
227286
}
@@ -239,6 +298,7 @@ test('a batch is refused whole when any step is not allowed', () => {
239298
],
240299
}),
241300
leasedSession,
301+
process.env,
242302
),
243303
'app',
244304
);
@@ -250,6 +310,7 @@ test('a batch is refused whole when any step is not allowed', () => {
250310
batchSteps: [{ command: 'snapshot', input: {}, flags: { surface: 'desktop' } }],
251311
}),
252312
leasedSession,
313+
process.env,
253314
),
254315
'surface',
255316
);
@@ -265,12 +326,23 @@ test('no device selector reaches a host device, and open and batch must name pla
265326
{ androidDeviceAllowlist: 'emulator-5554' },
266327
]) {
267328
assertDenied(
268-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.example.app'], selector)),
329+
() =>
330+
assertMacOsAppLeaseAdmitsRequest(
331+
lease,
332+
request('open', ['com.example.app'], selector),
333+
undefined,
334+
process.env,
335+
),
269336
'device',
270337
);
271338
assertDenied(
272339
() =>
273-
assertMacOsAppLeaseAdmitsRequest(lease, request('snapshot', [], selector), leasedSession),
340+
assertMacOsAppLeaseAdmitsRequest(
341+
lease,
342+
request('snapshot', [], selector),
343+
leasedSession,
344+
process.env,
345+
),
274346
'device',
275347
);
276348
assertDenied(
@@ -281,18 +353,24 @@ test('no device selector reaches a host device, and open and batch must name pla
281353
batchSteps: [{ command: 'snapshot', input: {}, flags: selector }],
282354
}),
283355
leasedSession,
356+
process.env,
284357
),
285358
'device',
286359
);
287360
}
288361
for (const command of ['open', 'batch']) {
289362
assertDenied(
290363
() =>
291-
assertMacOsAppLeaseAdmitsRequest(lease, {
292-
command,
293-
positionals: command === 'open' ? ['com.example.app'] : [],
294-
flags: {},
295-
}),
364+
assertMacOsAppLeaseAdmitsRequest(
365+
lease,
366+
{
367+
command,
368+
positionals: command === 'open' ? ['com.example.app'] : [],
369+
flags: {},
370+
},
371+
undefined,
372+
process.env,
373+
),
296374
'device',
297375
);
298376
}
@@ -310,6 +388,7 @@ test('a batch is refused whole when a later step carries a launch URL in its run
310388
],
311389
}),
312390
leasedSession,
391+
process.env,
313392
),
314393
'host-path',
315394
);
@@ -318,7 +397,23 @@ test('a batch is refused whole when a later step carries a launch URL in its run
318397
test('open needs the native app backend, which resolves every action inside the app', () => {
319398
vi.stubEnv('AGENT_DEVICE_MACOS_APP_BACKEND', 'xctest');
320399
assertDenied(
321-
() => assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.example.app'])),
400+
() =>
401+
assertMacOsAppLeaseAdmitsRequest(
402+
lease,
403+
request('open', ['com.example.app']),
404+
undefined,
405+
process.env,
406+
),
407+
'backend',
408+
);
409+
});
410+
411+
test('the backend is read from the request host, not the process', () => {
412+
assertDenied(
413+
() =>
414+
assertMacOsAppLeaseAdmitsRequest(lease, request('open', ['com.example.app']), undefined, {
415+
AGENT_DEVICE_MACOS_APP_BACKEND: 'xctest',
416+
}),
322417
'backend',
323418
);
324419
});

0 commit comments

Comments
 (0)