Skip to content

Commit b07d9ee

Browse files
committed
refactor(daemon): map boot, shutdown and appstate straight to handlers and retire the state-group dispatcher
1 parent 7da2934 commit b07d9ee

9 files changed

Lines changed: 100 additions & 74 deletions

‎scripts/layering/daemon-layer-manifest.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@
211211
"src/daemon/handlers/session-runtime-port-reverse.ts",
212212
"src/daemon/handlers/session-script-publication.ts",
213213
"src/daemon/handlers/session-shutdown.ts",
214-
"src/daemon/handlers/session-state.ts",
215214
"src/daemon/handlers/session.ts",
216215
"src/daemon/handlers/snapshot.ts",
217216
"src/daemon/handlers/trace-runtime.ts",

‎src/daemon/handlers/__tests__/session-appstate-device-runtime.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { daemonPlatformServicesFixture } from '../../__tests__/platform-services-fixture.ts';
22
import { test, expect, vi } from 'vitest';
33

4-
import { handleSessionStateCommands } from '../session-state.ts';
4+
import { handleAppStateCommand } from '../session-appstate.ts';
55
import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts';
66
import { withTestDeviceInventory } from '../../../__tests__/test-utils/device-inventory-gateways.ts';
77
import { unavailableDeploymentSnapshotAndShutdownOperationFacts } from '../../../__tests__/test-utils/runtime-operation-facts.ts';
@@ -39,7 +39,7 @@ test('appstate rejects web before Android app-state backend dispatch', async ()
3939
local: async () => [device],
4040
},
4141
async () =>
42-
await handleSessionStateCommands({
42+
await handleAppStateCommand({
4343
platformServices: daemonPlatformServicesFixture(),
4444
req: {
4545
token: 't',
@@ -160,7 +160,7 @@ test('appstate rejects a missing readiness fact even when appState is available'
160160
const response = await withTestDeviceInventory(
161161
{ local: async () => [device] },
162162
async () =>
163-
await handleSessionStateCommands({
163+
await handleAppStateCommand({
164164
platformServices: daemonPlatformServicesFixture(),
165165
req: {
166166
token: 't',
@@ -258,7 +258,7 @@ test('sessionless Android appstate inspects once, binds once, and preserves oper
258258
const response = await withTestDeviceInventory(
259259
{ local: async () => [device] },
260260
async () =>
261-
await handleSessionStateCommands({
261+
await handleAppStateCommand({
262262
platformServices: daemonPlatformServicesFixture(),
263263
req: {
264264
token: 't',

‎src/daemon/handlers/__tests__/session-boot-shutdown-device-claims.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import { LeaseRegistry } from '../../lease-registry.ts';
2222
import { acquireDeviceClaim } from '../../device/device-claims.ts';
2323
import { inspectDeviceClaims } from '../../device/device-claim-inspection.ts';
2424
import { createRequestExecutionScope } from '../../request-execution-scope.ts';
25-
import { handleSessionStateCommands } from '../session-state.ts';
25+
import { handleBootCommand } from '../session-boot.ts';
26+
import { handleShutdownCommand } from '../session-shutdown.ts';
2627
import type { DaemonRequest, DaemonResponse } from '../../daemon-request.ts';
2728

2829
// #1799: `boot`/`shutdown` route through a daemon that need not own the target
@@ -57,7 +58,7 @@ async function claimForeignDaemon(root: string): Promise<string> {
5758
async function runStateCommand(
5859
command: 'boot' | 'shutdown',
5960
store: SessionStore,
60-
): Promise<DaemonResponse | null> {
61+
): Promise<DaemonResponse> {
6162
const req: DaemonRequest = {
6263
token: 't',
6364
session: 'default',
@@ -80,14 +81,17 @@ async function runStateCommand(
8081
},
8182
});
8283
try {
83-
return await handleSessionStateCommands({
84+
const handlerParams = {
8485
platformServices: daemonPlatformServicesFixture(),
8586
req,
8687
sessionName: 'default',
8788
sessionStore: store,
8889
inspectFacts: scope.inspectFacts,
8990
bindDevice: scope.bindDevice,
90-
});
91+
};
92+
return command === 'boot'
93+
? await handleBootCommand(handlerParams)
94+
: await handleShutdownCommand(handlerParams);
9195
} catch (error) {
9296
return { ok: false, error: normalizeError(error) };
9397
} finally {

‎src/daemon/handlers/__tests__/session-boot.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable';
1313
import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime';
1414
import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime';
15-
import { handleSessionStateCommands } from '../session-state.ts';
15+
import { handleBootCommand } from '../session-boot.ts';
1616
import type { SessionState } from '../../session-state.ts';
1717
import {
1818
handleSessionCommands,
@@ -456,7 +456,7 @@ test('boot rejects --headless outside Android directly', async () => {
456456
booted: false,
457457
};
458458
mockResolveTargetDevice.mockResolvedValue(device);
459-
const response = await handleSessionStateCommands({
459+
const response = await handleBootCommand({
460460
platformServices: daemonPlatformServicesFixture(),
461461
req: {
462462
token: 't',

‎src/daemon/handlers/__tests__/session-state.test.ts‎

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { daemonPlatformServicesFixture } from '../../__tests__/platform-services-fixture.ts';
2+
import { expect, test } from 'vitest';
3+
import * as path from 'node:path';
4+
import { handleSessionCommands } from './session-command-harness.ts';
5+
import { makeSessionStore } from '../../../__tests__/test-utils/store-factory.ts';
6+
import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts';
7+
import { testCreateCommandSurface } from '../../__tests__/command-surface-fixture.ts';
8+
import type { DaemonRequest } from '../../daemon-request.ts';
9+
10+
function request(command: string): DaemonRequest {
11+
return { token: 't', session: 'default', command, positionals: [], flags: {} };
12+
}
13+
14+
async function dispatch(command: string) {
15+
return await handleSessionCommands({
16+
createCommandSurface: testCreateCommandSurface,
17+
platformServices: daemonPlatformServicesFixture(),
18+
req: request(command),
19+
sessionName: 'default',
20+
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
21+
sessionStore: makeSessionStore('agent-device-session-router-'),
22+
invoke: async () => ({ ok: true, data: {} }),
23+
});
24+
}
25+
26+
test('the session router stays silent for commands its table does not carry', async () => {
27+
expect(await dispatch('not_a_command')).toBeNull();
28+
});
29+
30+
test('the session router maps boot, shutdown and appstate straight to their handlers', async () => {
31+
// No session and no selector: each command reaches its own handler's guard rather than
32+
// the router's null, which is what distinguishes routing from silence.
33+
for (const command of ['boot', 'shutdown', 'appstate']) {
34+
const response = await dispatch(command);
35+
if (!response || response.ok) {
36+
throw new Error(
37+
`${command} should reach its handler's guard as an INVALID_ARGS refusal, got ${JSON.stringify(response)}`,
38+
);
39+
}
40+
expect(response.error.code, command).toBe('INVALID_ARGS');
41+
}
42+
});

‎src/daemon/handlers/session-state.ts‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/daemon/handlers/session.ts‎

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
handleSessionInventoryCommands,
1010
handleSessionOpenCommands,
1111
} from '../session-lifecycle/index.ts';
12-
import { handleSessionStateCommands } from './session-state.ts';
12+
import { handleBootCommand } from './session-boot.ts';
13+
import { handleShutdownCommand } from './session-shutdown.ts';
14+
import { handleAppStateCommand } from './session-appstate.ts';
1315
import { handleSessionObservabilityCommands } from '../session-observability/index.ts';
1416
import { handleReplayCommand, handleReplayTestCommand } from './session-replay-command.ts';
1517
import { handleSessionScriptPublication } from './session-script-publication.ts';
@@ -61,15 +63,49 @@ const handleSessionCloseCommandGroup: SessionCommandHandler = (params) =>
6163
platformResourceCleanup: params.platformResourceCleanup,
6264
} satisfies SessionCloseCommandInput);
6365

64-
const handleSessionStateCommandGroup: SessionCommandHandler = async ({
66+
const handleBootCommandEntry: SessionCommandHandler = async ({
6567
req,
6668
sessionName,
6769
sessionStore,
6870
inspectFacts,
6971
bindDevice,
7072
platformServices,
7173
}) =>
72-
await handleSessionStateCommands({
74+
await handleBootCommand({
75+
req,
76+
sessionName,
77+
sessionStore,
78+
inspectFacts,
79+
bindDevice,
80+
platformServices,
81+
});
82+
83+
const handleShutdownCommandEntry: SessionCommandHandler = async ({
84+
req,
85+
sessionName,
86+
sessionStore,
87+
inspectFacts,
88+
bindDevice,
89+
platformServices,
90+
}) =>
91+
await handleShutdownCommand({
92+
req,
93+
sessionName,
94+
sessionStore,
95+
inspectFacts,
96+
bindDevice,
97+
platformServices,
98+
});
99+
100+
const handleAppStateCommandEntry: SessionCommandHandler = async ({
101+
req,
102+
sessionName,
103+
sessionStore,
104+
inspectFacts,
105+
bindDevice,
106+
platformServices,
107+
}) =>
108+
await handleAppStateCommand({
73109
req,
74110
sessionName,
75111
sessionStore,
@@ -136,9 +172,9 @@ const SESSION_COMMAND_HANDLER_IMPLS = {
136172
inspectFacts,
137173
bindDevice,
138174
}),
139-
boot: handleSessionStateCommandGroup,
140-
shutdown: handleSessionStateCommandGroup,
141-
appstate: handleSessionStateCommandGroup,
175+
boot: handleBootCommandEntry,
176+
shutdown: handleShutdownCommandEntry,
177+
appstate: handleAppStateCommandEntry,
142178
session_save_script: async ({ req, sessionName, sessionStore }) =>
143179
handleSessionScriptPublication({ req, sessionName, sessionStore }),
144180
runtime: async ({

‎src/platform-runtime-gateway.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { viewportRuntimeOperationFacts } from '@agent-device/contracts/viewport-
1616
import type { DeviceInfo } from '@agent-device/kernel/device';
1717
import { createLimrunRuntime } from '@agent-device/provider-limrun';
1818
import { describe, expect, test, vi } from 'vitest';
19-
import { handleSessionStateCommands } from './daemon/handlers/session-state.ts';
19+
import { handleAppStateCommand } from './daemon/handlers/session-appstate.ts';
2020
import { createRequestRuntimeBindings } from './daemon/request-runtime-binding.ts';
2121
import { createRequestDispatchLedger } from './daemon/request-dispatch-ledger.ts';
2222
import { makeSessionStore } from './__tests__/test-utils/store-factory.ts';
@@ -344,7 +344,7 @@ describe('composed platform runtime gateway', () => {
344344
const response = await withTestDeviceInventory(
345345
{ local: async () => [staleDevice] },
346346
async () =>
347-
await handleSessionStateCommands({
347+
await handleAppStateCommand({
348348
platformServices: daemonPlatformServicesFixture(),
349349
req: {
350350
token: 't',

0 commit comments

Comments
 (0)