@@ -5,7 +5,7 @@ import test from 'node:test';
55
66import {
77 IOS_TARGET_ACTIVATION_PRIOR_STATES ,
8- isIosTargetActivationReason ,
8+ IOS_TARGET_ACTIVATION_REASONS ,
99 iosTargetActivationDisclosure ,
1010 type IosTargetActivation ,
1111} from '@agent-device/contracts/ios-target-activation' ;
@@ -158,48 +158,81 @@ async function captureUntilDisclosed(context: LiveContext) {
158158 await new Promise ( ( resolve ) => setTimeout ( resolve , HANDOFF_POLL_MS ) ) ;
159159 }
160160 assert . fail (
161- `no foreground disclosure within ${ HANDOFF_DEADLINE_MS } ms of the handoff to ` +
162- `${ HANDOFF_URL } : ${ JSON . stringify ( last ?. json ?? null ) } ` ,
161+ `no foreground disclosure within ${ HANDOFF_DEADLINE_MS } ms of the handoff to ${ HANDOFF_URL } . ` +
162+ `${ missingDisclosureEvidence ( last ) } ${ JSON . stringify ( last ?. json ?? null ) } ` ,
163163 ) ;
164164}
165165
166166/**
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.
167+ * Says which failure the lane actually hit. A warning that opens like the builder's stem but matches
168+ * none of its rebuilds is a sentence that changed shape, which is a different bug from a device that
169+ * never repaired anything — and the old 90-second timeout could not tell them apart.
173170 */
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 } ` ,
171+ function missingDisclosureEvidence ( last : Awaited < ReturnType < typeof runStep > > | undefined ) : string {
172+ const stem = disclosureStem ( ) ;
173+ const warnings = ( ( last ?. json ?. data ?. warnings ?? [ ] ) as unknown [ ] ) . filter (
174+ ( warning ) : warning is string => typeof warning === 'string' ,
188175 ) ;
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 } ` ,
176+ return warnings . some ( ( warning ) => warning . startsWith ( stem ) )
177+ ? 'A warning opened like the shared disclosure sentence but no declared fact rebuilt it: '
178+ : `No warning opened like the shared disclosure sentence ("${ stem } …"). ` ;
179+ }
180+
181+ /**
182+ * Every fact the decoder can stamp: the declared sets, so a prior state the runner never activates in
183+ * (`runningForeground`) is unprobeable here for the same reason the decoder drops it.
184+ */
185+ const DECLARED_ACTIVATION_FACTS : IosTargetActivation [ ] = IOS_TARGET_ACTIVATION_PRIOR_STATES . flatMap (
186+ ( priorState ) => IOS_TARGET_ACTIVATION_REASONS . map ( ( reason ) => ( { reason, priorState } ) ) ,
187+ ) ;
188+
189+ /**
190+ * A pid no live process can have, so the builder's own pid clause stays greppable inside the sentence
191+ * it produces and can be opened into a capture group afterwards.
192+ */
193+ const PID_PROBE = 88888888 ;
194+
195+ /**
196+ * Recognises the disclosure by rebuilding it, never by quoting it. Each declared fact goes through
197+ * `iosTargetActivationDisclosure` — once bare, once with {@link PID_PROBE} where the real pid is
198+ * captured — and only a response carrying one of those exact sentences counts. A reword of the
199+ * builder therefore rewrites the probe with it instead of silently returning `undefined` here and
200+ * reviving the old 90-second wait for a disclosure the lane had stopped being able to see.
201+ */
202+ function activationFactFrom ( warnings : unknown [ ] ) : IosTargetActivation | undefined {
203+ const sentences = warnings . filter ( ( warning ) : warning is string => typeof warning === 'string' ) ;
204+ for ( const fact of DECLARED_ACTIVATION_FACTS ) {
205+ if ( sentences . includes ( iosTargetActivationDisclosure ( fact ) ) ) return fact ;
206+ const pidSentence = iosTargetActivationDisclosure ( {
207+ ...fact ,
208+ otherActiveApplicationPid : PID_PROBE ,
209+ } ) ;
210+ const pattern = new RegExp (
211+ escapeRegExp ( pidSentence ) . replace ( String ( PID_PROBE ) , String . raw `(\d+)` ) ,
212+ ) ;
213+ const disclosed = sentences . find ( ( sentence ) => pattern . test ( sentence ) ) ;
214+ if ( disclosed !== undefined ) {
215+ return { ...fact , otherActiveApplicationPid : Number ( pattern . exec ( disclosed ) ! [ 1 ] ) } ;
216+ }
217+ }
218+ return undefined ;
219+ }
220+
221+ /** The longest opening every builder sentence shares — prose the lane never copies, only derives. */
222+ function disclosureStem ( ) : string {
223+ const sentences = DECLARED_ACTIVATION_FACTS . map ( ( fact ) => iosTargetActivationDisclosure ( fact ) ) ;
224+ const [ first = '' , ...rest ] = sentences ;
225+ return rest . reduce (
226+ ( stem , sentence ) => {
227+ const end = [ ...stem ] . findIndex ( ( char , index ) => sentence [ index ] !== char ) ;
228+ return end < 0 ? stem : stem . slice ( 0 , end ) ;
229+ } ,
230+ first . replace ( / [ ^ ] * $ / , '' ) ,
201231 ) ;
202- return fact ;
232+ }
233+
234+ function escapeRegExp ( text : string ) : string {
235+ return text . replaceAll ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, String . raw `\$&` ) ;
203236}
204237
205238async function checksum ( filePath : string ) : Promise < string > {
0 commit comments