11import assert from 'node:assert/strict' ;
2+ import fs from 'node:fs' ;
3+ import path from 'node:path' ;
24import { afterEach , test , vi } from 'vitest' ;
35import { makeSnapshotState } from '../../__tests__/test-utils/index.ts' ;
6+ import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts' ;
47import { countDiagnosticEventsByPhase , withDiagnosticsScope } from '../../utils/diagnostics.ts' ;
58import {
69 buildInteractionSurfaceSignature ,
@@ -40,12 +43,24 @@ function markPostGestureStabilization(
4043 markDeferredInteractionOutcome ( { session, command : action , positionals, flags } ) ;
4144}
4245
46+ /**
47+ * Runs the loop under a diagnostics scope that also writes its NDJSON trace to
48+ * a file, and returns the veto events parsed back out of it.
49+ *
50+ * The counts alone are not the behavior under test: what an operator reads on
51+ * a `--debug` run is the emitted `reason` and divergence numbers, and a
52+ * regression that emits the wrong reason — or drops the counts entirely —
53+ * keeps every count at 1. Reading the serialized line (rather than an
54+ * in-memory event array) pins what actually reaches the log, redaction
55+ * included.
56+ */
4357async function runStabilization (
4458 session : ReturnType < typeof makeSession > ,
4559 capture : ( ) => ReturnType < typeof makeSnapshotState > ,
4660) {
4761 const captureFn = vi . fn ( async ( ) => capture ( ) ) ;
48- const resultPromise = withDiagnosticsScope ( { } , async ( ) => {
62+ const traceLogPath = path . join ( mkdtempForTestSync ( 'agent-device-veto-trace-' ) , 'trace.ndjson' ) ;
63+ const resultPromise = withDiagnosticsScope ( { traceLogPath } , async ( ) => {
4964 const result = await capturePostGestureStabilizedResult ( {
5065 session,
5166 capture : captureFn ,
@@ -59,7 +74,20 @@ async function runStabilization(
5974 } ;
6075 } ) ;
6176 await vi . advanceTimersByTimeAsync ( 10_000 ) ;
62- return await resultPromise ;
77+ const outcome = await resultPromise ;
78+ return { ...outcome , vetoEvents : readVetoEvents ( traceLogPath ) } ;
79+ }
80+
81+ /** The `post_gesture_no_effect_vetoed` payloads written to a diagnostics trace. */
82+ function readVetoEvents ( traceLogPath : string ) : Record < string , unknown > [ ] {
83+ if ( ! fs . existsSync ( traceLogPath ) ) return [ ] ;
84+ return fs
85+ . readFileSync ( traceLogPath , 'utf8' )
86+ . split ( '\n' )
87+ . filter ( Boolean )
88+ . map ( ( line ) => JSON . parse ( line ) as { phase : string ; data ?: Record < string , unknown > } )
89+ . filter ( ( event ) => event . phase === 'post_gesture_no_effect_vetoed' )
90+ . map ( ( event ) => event . data ?? { } ) ;
6391}
6492
6593test ( 'a backend flip mid-poll withholds the no-effect claim, and records the reason (#1620)' , async ( ) => {
@@ -89,16 +117,29 @@ test('a backend flip mid-poll withholds the no-effect claim, and records the rea
89117 rect : { x : 20 , y : - 80 , width : 200 , height : 44 } ,
90118 } ,
91119 ] ;
92- const { result, staleAccepts, rebased, vetoed } = await runStabilization ( session , ( ) =>
93- makeSnapshotState ( privateAxNodes , {
94- snapshotQuality : { state : 'recovered' , backend : 'private-ax' } ,
95- } ) ,
120+ const { result, staleAccepts, rebased, vetoed, vetoEvents } = await runStabilization (
121+ session ,
122+ ( ) =>
123+ makeSnapshotState ( privateAxNodes , {
124+ snapshotQuality : { state : 'recovered' , backend : 'private-ax' } ,
125+ } ) ,
96126 ) ;
97127
98128 assert . equal ( rebased , 1 ) ;
99129 assert . equal ( staleAccepts , 1 , 'the loop still accepts the stale read after the distrust budget' ) ;
100130 assert . equal ( result . gestureNoEffect , undefined ) ;
101- assert . equal ( vetoed , 1 , 'the withheld claim must be observable (reason: baseline_rebased)' ) ;
131+ assert . equal ( vetoed , 1 , 'the withheld claim must be observable' ) ;
132+ // The reason is the point: a rebase means the corroboration pair is
133+ // cross-backend, which is a categorically different answer from "the
134+ // surfaces moved" and must not be reported as one. No divergence counts
135+ // ride along, because there is no comparable pair to count over.
136+ assert . deepEqual ( vetoEvents , [
137+ {
138+ action : 'scroll' ,
139+ backend : 'private-ax' ,
140+ reason : 'baseline_rebased' ,
141+ } ,
142+ ] ) ;
102143} ) ;
103144
104145test ( 'a successful scroll that flips the capture backend must not claim no-effect' , async ( ) => {
@@ -114,7 +155,7 @@ test('a successful scroll that flips the capture backend must not claim no-effec
114155 } ) ;
115156 markPostGestureStabilization ( session , 'scroll' , [ 'down' ] ) ;
116157
117- const { result, rebased, vetoed } = await runStabilization ( session , ( ) =>
158+ const { result, rebased, vetoed, vetoEvents } = await runStabilization ( session , ( ) =>
118159 makeSnapshotState ( chromeWithListSnapshot ( [ 'row-3' , 'row-4' ] ) . nodes , {
119160 snapshotQuality : { state : 'recovered' , backend : 'private-ax' } ,
120161 } ) ,
@@ -127,6 +168,17 @@ test('a successful scroll that flips the capture backend must not claim no-effec
127168 'the scroll swapped every list cell — a no-effect claim here is a false positive' ,
128169 ) ;
129170 assert . equal ( vetoed , 1 ) ;
171+ // Rebase wins the reason even though the surfaces also genuinely diverged:
172+ // once the pair is cross-backend the divergence counts would describe two
173+ // incomparable captures, and reporting them as movement is the exact
174+ // misreading #1620 spent weeks on.
175+ assert . deepEqual ( vetoEvents , [
176+ {
177+ action : 'scroll' ,
178+ backend : 'private-ax' ,
179+ reason : 'baseline_rebased' ,
180+ } ,
181+ ] ) ;
130182} ) ;
131183
132184test ( 'no pre-gesture snapshot means no baseline, no rebase, and no no-effect claim' , async ( ) => {
@@ -174,11 +226,31 @@ test('scope drift accepts stale but is vetoed from claiming no-effect, observabl
174226 ( node ) => node . type !== 'Cell' ,
175227 ) as never ,
176228 ) ;
177- const { result, staleAccepts, vetoed } = await runStabilization ( session , ( ) => narrowed ) ;
229+ const { result, staleAccepts, vetoed, vetoEvents } = await runStabilization (
230+ session ,
231+ ( ) => narrowed ,
232+ ) ;
178233
179234 assert . equal ( staleAccepts , 1 ) ;
180235 assert . equal ( result . gestureNoEffect , undefined ) ;
181- assert . equal ( vetoed , 1 , 'reason: surface_divergence — rows only in the baseline' ) ;
236+ assert . equal ( vetoed , 1 ) ;
237+ // Scope drift reads as one-sided membership, never as movement: both cells
238+ // are missing from the narrowed capture, the shared chrome button has not
239+ // moved. An operator seeing rectMismatched: 0 alongside onlyInBaseline: 2
240+ // can tell "unexamined" from "moved" without re-deriving it.
241+ // No `backend` key at all: this capture carries no snapshotQuality, and the
242+ // serialized line omits the field rather than writing a null an aggregator
243+ // would have to special-case.
244+ assert . deepEqual ( vetoEvents , [
245+ {
246+ action : 'scroll' ,
247+ reason : 'surface_divergence' ,
248+ onlyInBaseline : 2 ,
249+ onlyInCurrent : 0 ,
250+ rectMismatched : 0 ,
251+ shared : 1 ,
252+ } ,
253+ ] ) ;
182254} ) ;
183255
184256test ( 'same-backend membership drift vetoes the claim and records the divergence (#1620 truncation drift)' , async ( ) => {
@@ -208,16 +280,33 @@ test('same-backend membership drift vetoes the claim and records the divergence
208280 rect : { x : 0 , y : 220 , width : 390 , height : 60 } ,
209281 } ,
210282 ] ;
211- const { result, staleAccepts, rebased, vetoed } = await runStabilization ( session , ( ) =>
212- makeSnapshotState ( driftedNodes , {
213- snapshotQuality : { state : 'recovered' , backend : 'private-ax' } ,
214- } ) ,
283+ const { result, staleAccepts, rebased, vetoed, vetoEvents } = await runStabilization (
284+ session ,
285+ ( ) =>
286+ makeSnapshotState ( driftedNodes , {
287+ snapshotQuality : { state : 'recovered' , backend : 'private-ax' } ,
288+ } ) ,
215289 ) ;
216290
217291 assert . equal ( rebased , 0 , 'same backend throughout: this is drift, not a flip' ) ;
218292 assert . equal ( staleAccepts , 1 ) ;
219293 assert . equal ( result . gestureNoEffect , undefined ) ;
220294 assert . equal ( vetoed , 1 ) ;
295+ // The counts are what distinguish this from the scope-drift case above:
296+ // one extra key on the CURRENT side, everything shared unmoved. Same veto,
297+ // opposite direction of membership drift — indistinguishable from an event
298+ // tally alone.
299+ assert . deepEqual ( vetoEvents , [
300+ {
301+ action : 'scroll' ,
302+ backend : 'private-ax' ,
303+ reason : 'surface_divergence' ,
304+ onlyInBaseline : 0 ,
305+ onlyInCurrent : 1 ,
306+ rectMismatched : 0 ,
307+ shared : 3 ,
308+ } ,
309+ ] ) ;
221310} ) ;
222311
223312test ( 'summarizeDiscriminatingSurfaceDivergence counts one-sided keys and moved rects, excluding non-discriminating entries' , ( ) => {
0 commit comments