Skip to content

Commit bd233d5

Browse files
authored
refactor(daemon): absorb androidAdbExecutor DI slot into the generic provider scope (#1742)
* refactor(daemon): absorb androidAdbExecutor DI slot into the generic provider scope request-handler-chain.ts threaded a platform-concrete androidAdbExecutor parameter through the generic RequestHandlerChainParams, flattened out of request-router.ts's already-existing generic per-request provider-injection mechanism (RequestPlatformProviderScope, built by withRequestPlatformProviderScope in request-platform-providers.ts) and passed down as a separately named chain-level slot. RequestHandlerChainParams now carries the neutral providerScope: RequestPlatformProviderScope instead of a platform-named field. The session route (the sole in-chain consumer) reads params.providerScope.androidAdbExecutor when building handleSessionCommands' own params, which is unchanged. request-router.ts passes the whole resolved providerScope straight through instead of extracting androidAdbExecutor out of it first. The generic chain signature no longer names any platform. ## Why this is behavior-neutral - The exact same RequestPlatformProviderScope object (and its androidAdbExecutor field, resolved the same way by withRequestPlatformProviderScope) now reaches the session handler; only the path it travels through RequestHandlerChainParams changed, not its value or resolution. - session.ts, session-doctor.ts, session-doctor-android.ts, session-native-perf.ts, and session-observability.ts — everything downstream of handleSessionCommands — are untouched. - Full provider-integration coverage (android-lifecycle.test.ts's "Android Settings flow uses scripted ADB provider", doctor.test.ts, android-recording.test.ts, etc.) exercises the real end-to-end wiring through the daemon route and passes unchanged. ## Validation - New wiring-equivalence test (request-handler-chain-provider-scope.test.ts) mocks handleSessionCommands and proves the exact androidAdbExecutor function reference passed into providerScope reaches it unchanged for a session-routed request, and that an empty provider scope forwards undefined (as before). - pnpm exec tsc -p tsconfig.json — clean - pnpm check:layering — 136/136 structural tests pass - pnpm check:affected --run — 80 test files / 337 tests passed (including the full provider-integration suite), plus format, lint, layering, build/declarations, and vitest-related checks Part of #1739 (wave 0). * test: trim history-narration header comment in provider-scope wiring test Per review: comments should state constraints code can't show, not narrate migration history that goes stale at merge. Reduce the header to what the tests prove.
1 parent 73b54c1 commit bd233d5

5 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎src/daemon/__tests__/request-handler-catalog.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ async function runCatalogCommandThroughHandlerChain(
258258
sessionStore,
259259
leaseRegistry,
260260
invoke: async () => ({ ok: true, data: {} }),
261-
androidAdbExecutor: async () => ({ stdout: '', stderr: '', exitCode: 0 }),
261+
providerScope: {
262+
androidAdbExecutor: async () => ({ stdout: '', stderr: '', exitCode: 0 }),
263+
},
262264
bindDevice: unavailableBindDevice,
263265
bindExactDevice: unavailableBindExactDevice,
264266
reconcileOrphanedDeviceClaim: async () => ({
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Proves the exact androidAdbExecutor reference reaches the session handler
2+
// through the neutral providerScope, and that an empty scope forwards none.
3+
import assert from 'node:assert/strict';
4+
import { test, vi } from 'vitest';
5+
6+
const handleSessionCommandsMock = vi.fn(async (_params: unknown) => ({ ok: true, data: {} }));
7+
vi.mock('../handlers/session.ts', () => ({
8+
handleSessionCommands: handleSessionCommandsMock,
9+
}));
10+
11+
import { INTERNAL_COMMANDS } from '../../command-catalog.ts';
12+
import { LeaseRegistry } from '../lease-registry.ts';
13+
import { runRequestHandlerChain } from '../request-handler-chain.ts';
14+
import { makeIosSession } from '../../__tests__/test-utils/index.ts';
15+
import { makeSessionStore } from '../../__tests__/test-utils/store-factory.ts';
16+
import {
17+
unavailableBindDevice,
18+
unavailableBindExactDevice,
19+
} from './test-device-runtime-gateway.ts';
20+
import { createScreenRecordingAdmissionLedger } from '../screen-recording-admission-ledger.ts';
21+
import type { DaemonRequest } from '../types.ts';
22+
import type { AndroidAdbExecutor } from '../../platforms/android/adb-executor.ts';
23+
24+
function makeRequest(command: string, sessionName: string): DaemonRequest {
25+
return {
26+
command,
27+
token: 'test-token',
28+
session: sessionName,
29+
positionals: [],
30+
flags: {},
31+
meta: { requestId: `req-${command}` },
32+
};
33+
}
34+
35+
function baseChainParams(sessionName: string) {
36+
const sessionStore = makeSessionStore('agent-device-provider-scope-');
37+
sessionStore.set(sessionName, makeIosSession(sessionName));
38+
return {
39+
req: makeRequest(INTERNAL_COMMANDS.runtime, sessionName),
40+
sessionName,
41+
logPath: '/tmp/agent-device-provider-scope.log',
42+
sessionStore,
43+
leaseRegistry: new LeaseRegistry(),
44+
invoke: async () => ({ ok: true, data: {} }) as const,
45+
bindDevice: unavailableBindDevice,
46+
bindExactDevice: unavailableBindExactDevice,
47+
reconcileOrphanedDeviceClaim: async () => ({
48+
status: 'retained' as const,
49+
reason: 'test-no-recovery',
50+
}),
51+
screenRecordingAdmissionLedger: createScreenRecordingAdmissionLedger(),
52+
requestScope: {
53+
signal: new AbortController().signal,
54+
diagnostics: { emit: () => {} },
55+
progress: { report: () => {} },
56+
},
57+
retainDeviceExecutionLock: async () => {},
58+
throwIfCanceled: () => {},
59+
contextFromFlags: () => ({ logPath: '/tmp/agent-device-provider-scope.log' }),
60+
};
61+
}
62+
63+
test('the android adb executor from the generic provider scope reaches the session handler by the exact same reference', async () => {
64+
handleSessionCommandsMock.mockClear();
65+
const androidAdbExecutor: AndroidAdbExecutor = async () => ({
66+
stdout: '',
67+
stderr: '',
68+
exitCode: 0,
69+
});
70+
71+
await runRequestHandlerChain({
72+
...baseChainParams('provider-scope-test'),
73+
providerScope: { androidAdbExecutor },
74+
});
75+
76+
assert.equal(handleSessionCommandsMock.mock.calls.length, 1);
77+
const forwardedParams = handleSessionCommandsMock.mock.calls[0]?.[0] as {
78+
androidAdbExecutor?: AndroidAdbExecutor;
79+
};
80+
assert.equal(forwardedParams.androidAdbExecutor, androidAdbExecutor);
81+
});
82+
83+
test('an empty provider scope forwards no android adb executor to the session handler', async () => {
84+
handleSessionCommandsMock.mockClear();
85+
86+
await runRequestHandlerChain({
87+
...baseChainParams('provider-scope-empty'),
88+
providerScope: {},
89+
});
90+
91+
assert.equal(handleSessionCommandsMock.mock.calls.length, 1);
92+
const forwardedParams = handleSessionCommandsMock.mock.calls[0]?.[0] as {
93+
androidAdbExecutor?: AndroidAdbExecutor;
94+
};
95+
assert.equal(forwardedParams.androidAdbExecutor, undefined);
96+
});

‎src/daemon/__tests__/request-handler-chain.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function makeChainParams(req: DaemonRequest) {
4545
sessionStore,
4646
leaseRegistry: new LeaseRegistry(),
4747
invoke: async (): Promise<DaemonResponse> => ({ ok: true, data: {} }),
48+
providerScope: {},
4849
bindDevice: unavailableBindDevice,
4950
bindExactDevice: unavailableBindExactDevice,
5051
reconcileOrphanedDeviceClaim: async () => ({

‎src/daemon/request-handler-chain.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { CommandFlags } from '@agent-device/contracts/command';
22
import type { CloudArtifactProvider } from '@agent-device/contracts/observability';
3-
import type { AndroidAdbExecutor } from '../platforms/android/adb-executor.ts';
43
import { AppError } from '@agent-device/kernel/errors';
54
import { getDaemonCommandRoute } from './daemon-command-registry.ts';
65
import * as genericRequestHandlerModule from './request-generic-dispatch.ts';
@@ -14,6 +13,7 @@ import type { DeviceClaimReconciler } from './device-claims.ts';
1413
import type { AppLogAdmissionLedger } from './app-log-admission-ledger.ts';
1514
import type { ScreenRecordingAdmissionLedger } from './screen-recording-admission-ledger.ts';
1615
import type { PlatformRequestScope } from '@agent-device/contracts/platform';
16+
import type { RequestPlatformProviderScope } from './request-platform-providers.ts';
1717

1818
type RequestHandlerChainParams = {
1919
req: DaemonRequest;
@@ -27,7 +27,13 @@ type RequestHandlerChainParams = {
2727
cloudArtifactProvider?: CloudArtifactProvider;
2828
invoke: DaemonInvokeFn;
2929
invokeReplayAction?: DaemonInvokeFn;
30-
androidAdbExecutor?: AndroidAdbExecutor;
30+
/**
31+
* Per-request platform-provider injections resolved by the generic
32+
* `withRequestPlatformProviderScope` mechanism. Route handlers pick their own
33+
* platform-specific field back out of this neutral scope instead of the chain
34+
* carrying one named slot per platform (e.g. `androidAdbExecutor`).
35+
*/
36+
providerScope: RequestPlatformProviderScope;
3137
bindDevice: BindDeviceRuntime;
3238
bindExactDevice: BindExactDeviceRuntime;
3339
reconcileOrphanedDeviceClaim: DeviceClaimReconciler;
@@ -129,7 +135,7 @@ async function runSessionHandler(
129135
leaseLifecycleProvider: params.leaseLifecycleProvider,
130136
invoke: params.invoke,
131137
invokeReplayAction: params.invokeReplayAction,
132-
androidAdbExecutor: params.androidAdbExecutor,
138+
androidAdbExecutor: params.providerScope.androidAdbExecutor,
133139
bindDevice: params.bindDevice,
134140
bindExactDevice: params.bindExactDevice,
135141
reconcileOrphanedDeviceClaim: params.reconcileOrphanedDeviceClaim,

‎src/daemon/request-router.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export function createRequestHandler(deps: RequestRouterDeps): DaemonInvokeFn {
275275
invokeReplayAction: allowReplayActions
276276
? createReplayScopedActionInvoker(lockedScope, providerScope)
277277
: undefined,
278-
androidAdbExecutor: providerScope.androidAdbExecutor,
278+
providerScope,
279279
bindDevice: lockedScope.bindDevice,
280280
bindExactDevice: lockedScope.bindExactDevice,
281281
reconcileOrphanedDeviceClaim: createDeviceClaimReconciler({

0 commit comments

Comments
 (0)