@@ -2,6 +2,7 @@ import { expect, test, vi } from 'vitest';
22import fs from 'node:fs' ;
33import path from 'node:path' ;
44import { mkdtempForTestSync } from '@agent-device/test-kit' ;
5+ import { AppError } from '@agent-device/kernel/errors' ;
56import { screenRecordingResourceStore } from '@agent-device/capture-kit/screen-recording-resource-store' ;
67import {
78 expectDecodedCompletedRecording ,
@@ -10,22 +11,33 @@ import {
1011} from './record-runtime.fixtures.ts' ;
1112
1213// The stop lane that runs WITHOUT a held session resolves its device through the selection
13- // dispatch. The daemon-restart tests below pin that lane, so the dispatch answers with the
14- // harness device here; the live-handle tests never reach it because their session is held.
15- const RESTART_SELECTED_DEVICE = {
14+ // dispatch. On a host that answers anything, an unconstrained request is exactly the #3674
15+ // symptom, so the dispatch double here behaves like a multi-device host: it only answers a
16+ // request that states a device identity, and an unconstrained one gets the ambiguity a
17+ // multi-boot host really returns. The restart tests below pass only while the recovery lane
18+ // resolves from the manifest instead of sweeping the host.
19+ const RESTART_RECORDED_DEVICE = {
1620 platform : 'android' ,
1721 id : 'emulator-5554' ,
1822 name : 'Pixel' ,
1923 kind : 'emulator' ,
2024} as const ;
25+ const resolveTargetDeviceMock = vi . fn ( async ( flags : { udid ?: string ; serial ?: string } = { } ) => {
26+ if ( flags . serial === RESTART_RECORDED_DEVICE . id || flags . udid === RESTART_RECORDED_DEVICE . id ) {
27+ return { ...RESTART_RECORDED_DEVICE } ;
28+ }
29+ throw new AppError ( 'AMBIGUOUS_MATCH' , 'Multiple attached devices detected' ) ;
30+ } ) ;
2131vi . mock ( '@agent-device/device-selection/dispatch-resolve' , ( ) => ( {
22- resolveTargetDevice : vi . fn ( async ( ) => ( { ...RESTART_SELECTED_DEVICE } ) ) ,
32+ resolveTargetDevice : ( ...args : unknown [ ] ) =>
33+ resolveTargetDeviceMock ( ...( args as [ Record < string , string > | undefined ] ) ) ,
2334} ) ) ;
2435
2536test ( 'record stop after a daemon restart replays the completed export for the named session' , async ( ) => {
2637 // The #3674 state itself: sessions are memory-only, so a restart drops `recording`, while the
2738 // on-disk manifest survives. The caller still names the session (the only address that finds
28- // the manifest at all), and `help record` promises the completed recording on that stop.
39+ // the manifest at all), and `help record` promises the completed recording on that stop — even
40+ // on a host whose unconstrained resolution would answer AMBIGUOUS_MATCH.
2941 const harness = makeRecordRuntimeHarness ( 'record-runtime-restart-stop-' ) ;
3042 const outPath = path . join (
3143 mkdtempForTestSync ( 'record-runtime-restart-stop-output-' ) ,
@@ -37,9 +49,93 @@ test('record stop after a daemon restart replays the completed export for the na
3749 const ref = harness . sessionStore . lookup ( harness . sessionName ) ;
3850 if ( ! ref ) throw new Error ( 'Expected the stopped recording session' ) ;
3951 harness . sessionStore . retire ( ref ) ;
52+ resolveTargetDeviceMock . mockClear ( ) ;
4053
4154 const recovered = await harness . run ( [ 'stop' ] , { sessionExplicit : true } ) ;
4255
56+ expect ( recovered ) . toMatchObject ( {
57+ ok : true ,
58+ data : { recording : 'stopped' , outPath, recordingBackend : 'adb screenrecord' } ,
59+ } ) ;
60+ // The recovery re-resolves the manifest's exact device, never an unconstrained sweep.
61+ expect ( resolveTargetDeviceMock ) . toHaveBeenCalledWith (
62+ expect . objectContaining ( { serial : 'emulator-5554' } ) ,
63+ ) ;
64+ } ) ;
65+
66+ test ( 'record stop for a named session with no decodable manifest refuses on the session' , async ( ) => {
67+ // No manifest means there is nothing to recover from and nothing to take a device from: the
68+ // answer is the named-session refusal, and the host is never swept for a device.
69+ const harness = makeRecordRuntimeHarness ( 'record-runtime-restart-stop-no-manifest-' ) ;
70+ const ref = harness . sessionStore . lookup ( harness . sessionName ) ;
71+ if ( ! ref ) throw new Error ( 'Expected the published session' ) ;
72+ harness . sessionStore . retire ( ref ) ;
73+
74+ const response = await harness . run ( [ 'stop' ] , { sessionExplicit : true } ) ;
75+
76+ expect ( response ) . toMatchObject ( {
77+ ok : false ,
78+ error : { code : 'SESSION_NOT_FOUND' , details : { reason : 'named_session_not_found' } } ,
79+ } ) ;
80+ expect ( resolveTargetDeviceMock ) . not . toHaveBeenCalled ( ) ;
81+ } ) ;
82+
83+ test ( 'a named stop recovery keeps failing closed when the manifest names another session' , async ( ) => {
84+ // The manifest-first lane reads the envelope before resolving, so the session proof must stay
85+ // on the served path: a manifest whose sessionId belongs to another address never answers.
86+ const harness = makeRecordRuntimeHarness ( 'record-runtime-restart-stop-cross-session-' , {
87+ sessionName : 'recording-a' ,
88+ } ) ;
89+ const outPath = path . join (
90+ mkdtempForTestSync ( 'record-runtime-restart-stop-cross-session-output-' ) ,
91+ 'capture.mp4' ,
92+ ) ;
93+ fs . writeFileSync ( outPath , 'mp4' ) ;
94+ await harness . run ( [ 'start' , outPath ] ) ;
95+ await harness . run ( [ 'stop' ] ) ;
96+ const resourcePath = recordingResourcePath ( harness . sessionStore , harness . sessionName ) ;
97+ const record = screenRecordingResourceStore . read ( resourcePath ) ;
98+ if ( record . status !== 'decoded' ) throw new Error ( 'Expected decoded recording manifest' ) ;
99+ screenRecordingResourceStore . write ( resourcePath , {
100+ ...record . envelope ,
101+ sessionId : 'recording-b' ,
102+ } ) ;
103+ const ref = harness . sessionStore . lookup ( harness . sessionName ) ;
104+ if ( ! ref ) throw new Error ( 'Expected the stopped recording session' ) ;
105+ harness . sessionStore . retire ( ref ) ;
106+
107+ const response = await harness . run ( [ 'stop' ] , { sessionExplicit : true } ) ;
108+
109+ expect ( response ) . toMatchObject ( {
110+ ok : false ,
111+ error : { code : 'COMMAND_FAILED' , details : { reason : 'runtime-contract-invalid' } } ,
112+ } ) ;
113+ } ) ;
114+
115+ test ( 'record stop after a daemon restart answers an explicitly targeted request from its device' , async ( ) => {
116+ // The closest positive: the same unheld named session, but the request states the very device
117+ // the manifest names, so the selector lane serves the recovery — the stated target still wins.
118+ const harness = makeRecordRuntimeHarness ( 'record-runtime-restart-stop-targeted-' ) ;
119+ const outPath = path . join (
120+ mkdtempForTestSync ( 'record-runtime-restart-stop-targeted-output-' ) ,
121+ 'capture.mp4' ,
122+ ) ;
123+ fs . writeFileSync ( outPath , 'mp4' ) ;
124+ await harness . run ( [ 'start' , outPath ] ) ;
125+ await harness . run ( [ 'stop' ] ) ;
126+ const ref = harness . sessionStore . lookup ( harness . sessionName ) ;
127+ if ( ! ref ) throw new Error ( 'Expected the stopped recording session' ) ;
128+ harness . sessionStore . retire ( ref ) ;
129+
130+ const recovered = await harness . run (
131+ [ 'stop' ] ,
132+ { sessionExplicit : true } ,
133+ {
134+ platform : 'android' ,
135+ serial : 'emulator-5554' ,
136+ } ,
137+ ) ;
138+
43139 expect ( recovered ) . toMatchObject ( {
44140 ok : true ,
45141 data : { recording : 'stopped' , outPath, recordingBackend : 'adb screenrecord' } ,
@@ -58,6 +154,7 @@ test('record start after a daemon restart still refuses the named missing sessio
58154 const ref = harness . sessionStore . lookup ( harness . sessionName ) ;
59155 if ( ! ref ) throw new Error ( 'Expected the live recording session' ) ;
60156 harness . sessionStore . retire ( ref ) ;
157+ resolveTargetDeviceMock . mockClear ( ) ;
61158
62159 const response = await harness . run ( [ 'start' , outPath ] , { sessionExplicit : true } ) ;
63160
@@ -66,6 +163,8 @@ test('record start after a daemon restart still refuses the named missing sessio
66163 error : { code : 'SESSION_NOT_FOUND' , details : { reason : 'named_session_not_found' } } ,
67164 } ) ;
68165 expect ( harness . runtime . start ) . toHaveBeenCalledOnce ( ) ;
166+ // A start has no manifest to recover from, so the refusal answers before any device is sought.
167+ expect ( resolveTargetDeviceMock ) . not . toHaveBeenCalled ( ) ;
69168} ) ;
70169
71170test ( 'record stop with a live handle binds no runtime and terminalizes the durable record' , async ( ) => {
0 commit comments