@@ -3,7 +3,12 @@ import fs from 'node:fs/promises';
33import path from 'node:path' ;
44import test from 'node:test' ;
55
6- import { iosTargetActivationDisclosure } from '@agent-device/contracts/ios-target-activation' ;
6+ import {
7+ IOS_TARGET_ACTIVATION_PRIOR_STATES ,
8+ isIosTargetActivationReason ,
9+ iosTargetActivationDisclosure ,
10+ type IosTargetActivation ,
11+ } from '@agent-device/contracts/ios-target-activation' ;
712import { runCmd } from '@agent-device/host-kit/command' ;
813import {
914 cleanupSession ,
@@ -18,6 +23,10 @@ import {
1823 * actually activated the bound app, so this drives the real runner on a real simulator — a mocked
1924 * runner response proves the decoder, not the fact.
2025 *
26+ * What it asserts is the `data.warnings` sentence, because that is everything this branch publishes:
27+ * the typed `data.targetActivation` field is the daemon seam that lands in #2693, and a lane reading
28+ * it here would time out against this head.
29+ *
2130 * MANUAL, ENV-GATED LANE. It is deliberately absent from the `ios.yml` and `replays-manual.yml` test
2231 * lists: on the CI-hosted simulator the runner reads its own target as `.runningForeground` while a
2332 * foreign app is demonstrably on screen, so that host owes no repair and no disclosure
@@ -92,21 +101,7 @@ test(
92101 // Poll instead of sleeping a fixed window: the disclosure must arrive on the command that
93102 // paid for the repair, and a lane that never sees one is the regression this asserts against.
94103 const repaired = await captureUntilDisclosed ( context ) ;
95- const fact = repaired . json ! . data . targetActivation ;
96- assert . ok (
97- typeof fact . reason === 'string' && fact . reason . length > 0 ,
98- `disclosure carried no reason: ${ JSON . stringify ( fact ) } ` ,
99- ) ;
100- assert . match (
101- String ( fact . priorState ) ,
102- / ^ r u n n i n g ( B a c k g r o u n d | B a c k g r o u n d S u s p e n d e d ) $ | ^ n o t R u n n i n g $ | ^ u n k n o w n $ / ,
103- `prior state claims the repair's outcome, not its starting point: ${ fact . priorState } ` ,
104- ) ;
105- const warnings : unknown [ ] = repaired . json ! . data . warnings ?? [ ] ;
106- assert . ok (
107- warnings . includes ( iosTargetActivationDisclosure ( fact ) ) ,
108- `warnings did not carry the shared disclosure sentence: ${ JSON . stringify ( warnings ) } ` ,
109- ) ;
104+ activationFactFrom ( repaired . json ! . data . warnings ?? [ ] ) ;
110105
111106 // The repair is a fact about one command, not a property of the session: with the app already
112107 // foreground again, the next capture must say nothing.
@@ -115,17 +110,12 @@ test(
115110 '-i' ,
116111 ] ) ;
117112 assert . equal (
118- settled . json ?. data ?. targetActivation ,
113+ activationFactFrom ( settled . json ?. data ?. warnings ?? [ ] ) ,
119114 undefined ,
120115 `second capture re-disclosed a repair it did not perform: ${ JSON . stringify (
121116 settled . json ?. data ?. warnings ,
122117 ) } `,
123118 ) ;
124- assert . equal (
125- String ( settled . json ?. data ?. warning ?? '' ) . includes ( 'was not foreground' ) ,
126- false ,
127- 'second capture re-disclosed a repair it did not perform' ,
128- ) ;
129119 } catch ( error ) {
130120 throw await withRunnerLogEvidence ( context , error ) ;
131121 } finally {
@@ -156,15 +146,15 @@ async function withRunnerLogEvidence(context: LiveContext, error: unknown): Prom
156146 return new Error ( `${ message } \n${ note } ` , { cause : error } ) ;
157147}
158148
159- /** Poll `snapshot -i` until the runner reports the repair, or fail with the last response. */
149+ /** Poll `snapshot -i` until the runner discloses the repair, or fail with the last response. */
160150async function captureUntilDisclosed ( context : LiveContext ) {
161151 const deadline = Date . now ( ) + HANDOFF_DEADLINE_MS ;
162152 let last : Awaited < ReturnType < typeof runStep > > | undefined ;
163153 while ( Date . now ( ) < deadline ) {
164154 last = await runStep ( context , 'snapshot after handoff' , [ 'snapshot' , '-i' ] , {
165155 allowFailure : true ,
166156 } ) ;
167- if ( last . json ?. data ?. targetActivation !== undefined ) return last ;
157+ if ( activationFactFrom ( last . json ?. data ?. warnings ?? [ ] ) !== undefined ) return last ;
168158 await new Promise ( ( resolve ) => setTimeout ( resolve , HANDOFF_POLL_MS ) ) ;
169159 }
170160 assert . fail (
@@ -173,6 +163,45 @@ async function captureUntilDisclosed(context: LiveContext) {
173163 ) ;
174164}
175165
166+ /**
167+ * Recovers the repair fact from the disclosure sentence in `data.warnings` and hands it back to the
168+ * shared builder, which must reproduce the response's sentence exactly. That is what makes the lane
169+ * assert the disclosure rather than a paraphrase of it, and it keeps the typed object out of the
170+ * picture so the lane runs against a head that publishes only the warning. A prior state outside the
171+ * declared set fails as a fact the runner could not have stamped — the declared set omits
172+ * `runningForeground` because the runner never activates there.
173+ */
174+ function activationFactFrom ( warnings : unknown [ ] ) : IosTargetActivation | undefined {
175+ const sentence = warnings . find (
176+ ( warning ) : warning is string =>
177+ typeof warning === 'string' && warning . includes ( 'The session app was not foreground' ) ,
178+ ) ;
179+ if ( sentence === undefined ) return undefined ;
180+ const priorState = IOS_TARGET_ACTIVATION_PRIOR_STATES . find (
181+ ( state ) => state === / \( p r i o r s t a t e ( [ A - Z a - z ] + ) \) / . exec ( sentence ) ?. [ 1 ] ,
182+ ) ;
183+ const reason = / \( r e a s o n ( [ a - z _ ] + ) \) / . exec ( sentence ) ?. [ 1 ] ;
184+ const pid = / a c t i v e a c c e s s i b i l i t y s e s s i o n \( p i d ( \d + ) \) / . exec ( sentence ) ?. [ 1 ] ;
185+ assert . ok (
186+ priorState !== undefined && isIosTargetActivationReason ( reason ) ,
187+ `disclosure names no declared prior state and reason: ${ sentence } ` ,
188+ ) ;
189+ assert . match (
190+ sentence ,
191+ / R e - c a p t u r e n o w t h a t t h e s e s s i o n a p p a n s w e r s , o r d r i v e t h e o t h e r a p p i n i t s o w n s e s s i o n / ,
192+ ) ;
193+ const fact : IosTargetActivation = {
194+ reason,
195+ priorState,
196+ ...( pid === undefined ? { } : { otherActiveApplicationPid : Number ( pid ) } ) ,
197+ } ;
198+ assert . ok (
199+ warnings . includes ( iosTargetActivationDisclosure ( fact ) ) ,
200+ `disclosure is not the shared builder's sentence: ${ sentence } ` ,
201+ ) ;
202+ return fact ;
203+ }
204+
176205async function checksum ( filePath : string ) : Promise < string > {
177206 const crypto = await import ( 'node:crypto' ) ;
178207 return crypto
0 commit comments