Skip to content

Commit ac10e38

Browse files
authored
refactor(daemon): extract shutdown handling from session-state dispatcher (#3456)
* refactor(daemon): move shutdown handling into its own module With boot gone the dispatcher still carried the shutdown branch: device resolution, facts admission, the active-session refusal, and the failure wire shapes. Shutdown now answers its own domain question in session-shutdown.ts, and the shutdown suite moves to session-shutdown.test.ts to match its subject one-to-one; the boot file keeps only boot tests. No behavior change. Part of #3424. * chore(gates): register the shutdown handler module in the daemon layer manifest session-shutdown.ts joins the daemon-sessions layer in the R81 manifest.
1 parent 4a1a9c0 commit ac10e38

6 files changed

Lines changed: 369 additions & 329 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
"src/daemon/handlers/session-runtime-command.ts",
208208
"src/daemon/handlers/session-runtime-port-reverse.ts",
209209
"src/daemon/handlers/session-script-publication.ts",
210+
"src/daemon/handlers/session-shutdown.ts",
210211
"src/daemon/handlers/session-state.ts",
211212
"src/daemon/handlers/session.ts",
212213
"src/daemon/handlers/snapshot.ts",

‎src/__tests__/command-descriptor-shutdown-runtime-execution.test.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { expect, test } from 'vitest';
33
import { shutdownTargetUse } from '@agent-device/contracts/platform-runtime-operations';
44
import { commandDescriptors } from '@agent-device/command-registry/registry';
55

6-
const sessionStateSource = readFileSync(
7-
new URL('../daemon/handlers/session-state.ts', import.meta.url),
6+
const sessionShutdownSource = readFileSync(
7+
new URL('../daemon/handlers/session-shutdown.ts', import.meta.url),
88
'utf8',
99
);
1010
const sessionCloseSource = readFileSync(
@@ -42,9 +42,7 @@ test('shutdown descriptor declares one canonical runtime operation', () => {
4242
});
4343

4444
test('canonical shutdown and close parity share one concrete teardown mechanic', () => {
45-
const shutdownRoute = sessionStateSource.slice(
46-
sessionStateSource.indexOf("if (req.command === 'shutdown')"),
47-
);
45+
const shutdownRoute = sessionShutdownSource;
4846
expect(shutdownRoute.match(/inspectFacts\(device\)/g)).toHaveLength(1);
4947
expect(shutdownRoute.match(/bindDevice\(device, shutdownTargetUse\)/g)).toHaveLength(1);
5048
expect(shutdownRoute).not.toContain('operations.ensureReady');

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

Lines changed: 4 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ import {
99
makeSession,
1010
noopInvoke,
1111
} from './session-test-harness.ts';
12+
import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable';
13+
import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime';
14+
import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime';
15+
import { handleSessionStateCommands } from '../session-state.ts';
1216
import type { SessionState } from '../../session-state.ts';
1317
import {
1418
handleSessionCommands,
1519
mockBindDeviceRuntime,
1620
mockEnsureReadyHeadlessRuntime,
1721
mockEnsureReadyRuntime,
1822
mockInspectDeviceRuntimeFacts,
19-
mockShutdownTargetRuntime,
2023
} from './session-command-harness.ts';
21-
import { createUnavailablePlatformRuntimeFacts } from '@agent-device/contracts/platform-runtime-unavailable';
22-
import { applicationLifecycleOperationFacts } from '@agent-device/contracts/application-lifecycle-runtime';
23-
import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime';
24-
import { handleSessionStateCommands } from '../session-state.ts';
2524
import { mkdtempForTestSync } from '../../../__tests__/test-utils/tmp-dir.ts';
2625
import { testCreateCommandSurface } from '../../__tests__/command-surface-fixture.ts';
2726

@@ -446,231 +445,6 @@ test('boot keeps --target validation before facts inspection', async () => {
446445
}
447446
});
448447

449-
test('shutdown turns off selected iOS simulator', async () => {
450-
const sessionStore = makeSessionStore();
451-
const selectedDevice: SessionState['device'] = {
452-
platform: 'apple',
453-
id: 'sim-2',
454-
name: 'iPhone 17 Pro',
455-
kind: 'simulator',
456-
target: 'mobile',
457-
booted: true,
458-
};
459-
mockResolveTargetDevice.mockResolvedValue(selectedDevice);
460-
461-
const response = await handleSessionCommands({
462-
createCommandSurface: testCreateCommandSurface,
463-
platformServices: daemonPlatformServicesFixture(),
464-
req: {
465-
token: 't',
466-
session: 'default',
467-
command: 'shutdown',
468-
positionals: [],
469-
flags: { platform: 'ios', device: 'iPhone 17 Pro' },
470-
},
471-
sessionName: 'default',
472-
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
473-
sessionStore,
474-
invoke: noopInvoke,
475-
});
476-
477-
expect(response).toBeTruthy();
478-
expect(response?.ok).toBe(true);
479-
expect(mockEnsureDeviceReady).not.toHaveBeenCalled();
480-
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
481-
expect(mockBindDeviceRuntime).toHaveBeenCalledOnce();
482-
expect(mockShutdownTargetRuntime).toHaveBeenCalledOnce();
483-
if (response && response.ok) {
484-
expect(response.data?.platform).toBe('ios');
485-
expect(response.data?.id).toBe('sim-2');
486-
expect(response.data?.shutdown).toEqual({
487-
success: true,
488-
exitCode: 0,
489-
stdout: '',
490-
stderr: '',
491-
});
492-
}
493-
});
494-
495-
test('shutdown rejects active session device and points to close --shutdown', async () => {
496-
const sessionStore = makeSessionStore();
497-
const sessionName = 'default';
498-
const selectedDevice: SessionState['device'] = {
499-
platform: 'apple',
500-
id: 'sim-2',
501-
name: 'iPhone 17 Pro',
502-
kind: 'simulator',
503-
target: 'mobile',
504-
booted: true,
505-
};
506-
sessionStore.publish(sessionName, makeSession(sessionName, selectedDevice));
507-
mockResolveTargetDevice.mockResolvedValue(selectedDevice);
508-
509-
const response = await handleSessionCommands({
510-
createCommandSurface: testCreateCommandSurface,
511-
platformServices: daemonPlatformServicesFixture(),
512-
req: {
513-
token: 't',
514-
session: sessionName,
515-
command: 'shutdown',
516-
positionals: [],
517-
flags: { platform: 'ios', device: 'iPhone 17 Pro' },
518-
},
519-
sessionName,
520-
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
521-
sessionStore,
522-
invoke: noopInvoke,
523-
});
524-
525-
expect(response).toBeTruthy();
526-
expect(response?.ok).toBe(false);
527-
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
528-
expect(mockBindDeviceRuntime).not.toHaveBeenCalled();
529-
expect(mockShutdownTargetRuntime).not.toHaveBeenCalled();
530-
if (response && !response.ok) {
531-
expect(response.error.code).toBe('DEVICE_IN_USE');
532-
expect(response.error.message).toMatch(/close --shutdown/i);
533-
expect(response.error.details?.hint).toBe(
534-
'Run agent-device close --shutdown --session default',
535-
);
536-
}
537-
});
538-
539-
test('shutdown turns off selected Android emulator', async () => {
540-
const sessionStore = makeSessionStore();
541-
mockResolveTargetDevice.mockResolvedValue({
542-
platform: 'android',
543-
id: 'emulator-5554',
544-
name: 'Pixel_9_Pro_XL',
545-
kind: 'emulator',
546-
target: 'mobile',
547-
booted: false,
548-
});
549-
550-
const response = await handleSessionCommands({
551-
createCommandSurface: testCreateCommandSurface,
552-
platformServices: daemonPlatformServicesFixture(),
553-
req: {
554-
token: 't',
555-
session: 'default',
556-
command: 'shutdown',
557-
positionals: [],
558-
flags: { platform: 'android', device: 'Pixel_9_Pro_XL' },
559-
},
560-
sessionName: 'default',
561-
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
562-
sessionStore,
563-
invoke: noopInvoke,
564-
});
565-
566-
expect(response).toBeTruthy();
567-
expect(response?.ok).toBe(true);
568-
expect(mockEnsureDeviceReady).not.toHaveBeenCalled();
569-
expect(mockResolveTargetDevice).toHaveBeenCalledWith(
570-
{ platform: 'android', device: 'Pixel_9_Pro_XL' },
571-
{ androidAvdSelection: 'include-stopped' },
572-
);
573-
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
574-
expect(mockBindDeviceRuntime).toHaveBeenCalledOnce();
575-
expect(mockShutdownTargetRuntime).toHaveBeenCalledOnce();
576-
if (response && response.ok) {
577-
expect(response.data?.platform).toBe('android');
578-
expect(response.data?.id).toBe('emulator-5554');
579-
expect(response.data?.shutdown).toEqual({
580-
success: true,
581-
exitCode: 0,
582-
stdout: '',
583-
stderr: '',
584-
});
585-
}
586-
});
587-
588-
test('shutdown rejects unsupported physical devices', async () => {
589-
const sessionStore = makeSessionStore();
590-
mockResolveTargetDevice.mockResolvedValue({
591-
platform: 'apple',
592-
id: 'device-1',
593-
name: 'iPhone',
594-
kind: 'device',
595-
target: 'mobile',
596-
booted: true,
597-
});
598-
599-
const response = await handleSessionCommands({
600-
createCommandSurface: testCreateCommandSurface,
601-
platformServices: daemonPlatformServicesFixture(),
602-
req: {
603-
token: 't',
604-
session: 'default',
605-
command: 'shutdown',
606-
positionals: [],
607-
flags: { platform: 'ios', udid: 'device-1' },
608-
},
609-
sessionName: 'default',
610-
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
611-
sessionStore,
612-
invoke: noopInvoke,
613-
});
614-
615-
expect(response).toBeTruthy();
616-
expect(response?.ok).toBe(false);
617-
expect(mockInspectDeviceRuntimeFacts).toHaveBeenCalledOnce();
618-
expect(mockBindDeviceRuntime).not.toHaveBeenCalled();
619-
expect(mockShutdownTargetRuntime).not.toHaveBeenCalled();
620-
if (response && !response.ok) {
621-
expect(response.error.code).toBe('UNSUPPORTED_OPERATION');
622-
expect(response.error.message).toMatch(/Apple simulators and Android emulators/i);
623-
}
624-
});
625-
626-
test('shutdown returns an error response when selected target shutdown fails', async () => {
627-
const sessionStore = makeSessionStore();
628-
const selectedDevice: SessionState['device'] = {
629-
platform: 'apple',
630-
id: 'sim-2',
631-
name: 'iPhone 17 Pro',
632-
kind: 'simulator',
633-
target: 'mobile',
634-
booted: true,
635-
};
636-
mockResolveTargetDevice.mockResolvedValue(selectedDevice);
637-
mockShutdownTargetRuntime.mockResolvedValue({
638-
success: false,
639-
exitCode: 149,
640-
stdout: '',
641-
stderr: 'simctl shutdown failed',
642-
});
643-
644-
const response = await handleSessionCommands({
645-
createCommandSurface: testCreateCommandSurface,
646-
platformServices: daemonPlatformServicesFixture(),
647-
req: {
648-
token: 't',
649-
session: 'default',
650-
command: 'shutdown',
651-
positionals: [],
652-
flags: { platform: 'ios', device: 'iPhone 17 Pro' },
653-
},
654-
sessionName: 'default',
655-
logPath: path.join(mkdtempForTestSync('daemon'), 'daemon.log'),
656-
sessionStore,
657-
invoke: noopInvoke,
658-
});
659-
660-
expect(response).toBeTruthy();
661-
expect(response?.ok).toBe(false);
662-
if (response && !response.ok) {
663-
expect(response.error.code).toBe('COMMAND_FAILED');
664-
expect(response.error.message).toBe('simctl shutdown failed');
665-
expect(response.error.details?.shutdown).toEqual({
666-
success: false,
667-
exitCode: 149,
668-
stdout: '',
669-
stderr: 'simctl shutdown failed',
670-
});
671-
}
672-
});
673-
674448
test('boot rejects --headless outside Android directly', async () => {
675449
const device = {
676450
platform: 'apple' as const,

0 commit comments

Comments
 (0)