@@ -15,7 +15,7 @@ import contentionRetryReporter, {
1515 runBlockers ,
1616 writeFailureReport ,
1717} from './contention-retry-reporter.ts' ;
18- import { RUNNER_TIMEOUT_META } from './runner-timeout-meta.ts' ;
18+ import { RUNNER_TIMEOUT_META , RUNNER_TIMEOUT_TOKEN_ENV } from './runner-timeout-meta.ts' ;
1919import {
2020 CONTENTION_RETRY_FILES ,
2121 expiredRetryEntries ,
@@ -54,8 +54,11 @@ function assertionFailure(overrides: Partial<TestFailure> = {}): TestFailure {
5454 return failure ( { message : ASSERTION_MESSAGE , timeout : false , ...overrides } ) ;
5555}
5656
57+ /** The run's secret, minted by the lane and never visible to a test module. */
58+ const TOKEN = 'a4ec0a4a-0f77-4f2e-9d4a-6d7f1d3f0d21' ;
59+
5760/** What the runner-timeout setup file writes on a test the runner aborted. */
58- const RUNNER_ABORTED = { [ RUNNER_TIMEOUT_META ] : true } ;
61+ const RUNNER_ABORTED = { [ RUNNER_TIMEOUT_META ] : TOKEN } ;
5962
6063function testCaseStub (
6164 errors : ReadonlyArray < Record < string , unknown > > ,
@@ -152,26 +155,34 @@ test('the expiry gate fails the run before any test executes', async () => {
152155} ) ;
153156
154157test ( 'only the runner-owned abort mark classifies a failure as a timeout' , ( ) => {
155- assert . ok ( isRunnerTimeout ( [ VITEST_TIMEOUT ] , RUNNER_ABORTED ) ) ;
158+ assert . ok ( isRunnerTimeout ( [ VITEST_TIMEOUT ] , RUNNER_ABORTED , TOKEN ) ) ;
159+ // `task.meta` is test-writable, so a mark that is not the run's secret is worthless.
160+ for ( const forged of [ true , 'true' , 'agentDeviceRunnerTimeout' , '' , undefined ] ) {
161+ assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , { [ RUNNER_TIMEOUT_META ] : forged } , TOKEN ) ) ;
162+ }
163+ // No secret configured means nothing is eligible.
164+ assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , RUNNER_ABORTED , undefined ) ) ;
165+ assert . ok ( ! isRunnerTimeout ( [ VITEST_TIMEOUT ] , RUNNER_ABORTED , '' ) ) ;
156166 // Without the runner's mark, no message and no error shape can earn a retry.
157167 for ( const impostor of [
158168 { name : 'Error' , message : VITEST_TIMEOUT . message } ,
159169 { name : 'AssertionError' , message : VITEST_TIMEOUT . message } ,
160170 { name : 'Error' , message : 'connect ETIMEDOUT 127.0.0.1:8080' } ,
161171 { name : 'Error' , message : 'Closing timeout while tearing down the daemon' } ,
162172 ] ) {
163- assert . ok ( ! isRunnerTimeout ( [ impostor ] , { } ) , `${ impostor . name } : ${ impostor . message } ` ) ;
164- assert . ok ( ! isRunnerTimeout ( [ impostor ] , undefined ) ) ;
165- assert . ok ( ! isRunnerTimeout ( [ impostor ] , { [ RUNNER_TIMEOUT_META ] : 'yes' } ) ) ;
173+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , { } , TOKEN ) , `${ impostor . name } : ${ impostor . message } ` ) ;
174+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , undefined , TOKEN ) ) ;
175+ assert . ok ( ! isRunnerTimeout ( [ impostor ] , { [ RUNNER_TIMEOUT_META ] : 'yes' } , TOKEN ) ) ;
166176 }
167177 // An aborted test that also produced an assertion diff is a regression.
168178 assert . ok (
169179 ! isRunnerTimeout (
170180 [ VITEST_TIMEOUT , { name : 'AssertionError' , message : 'x' , expected : 1 , actual : 2 } ] ,
171181 RUNNER_ABORTED ,
182+ TOKEN ,
172183 ) ,
173184 ) ;
174- assert . ok ( ! isRunnerTimeout ( [ ] , RUNNER_ABORTED ) ) ;
185+ assert . ok ( ! isRunnerTimeout ( [ ] , RUNNER_ABORTED , TOKEN ) ) ;
175186} ) ;
176187
177188test ( 'a real Vitest run marks only runner-aborted tests as retry-eligible' , ( ) => {
@@ -181,7 +192,11 @@ test('a real Vitest run marks only runner-aborted tests as retry-eligible', () =
181192 fs . rmSync ( report , { force : true } ) ;
182193 runCmdSync ( 'pnpm' , [ 'exec' , 'vitest' , 'run' , '--config' , FIXTURE_CONFIG ] , {
183194 cwd : repoRoot ,
184- env : { ...process . env , CONTENTION_RETRY_FAILURES : report } ,
195+ env : {
196+ ...process . env ,
197+ CONTENTION_RETRY_FAILURES : report ,
198+ [ RUNNER_TIMEOUT_TOKEN_ENV ] : TOKEN ,
199+ } ,
185200 allowFailure : true ,
186201 } ) ;
187202 const { failures } = parseFailureReport ( JSON . parse ( fs . readFileSync ( report , 'utf8' ) ) , repoRoot ) ;
@@ -193,6 +208,11 @@ test('a real Vitest run marks only runner-aborted tests as retry-eligible', () =
193208 'timeout message thrown after blocking the event loop past the budget' : false ,
194209 'assertion failure after blocking the event loop past the budget' : false ,
195210 'plain assertion failure' : false ,
211+ // `task.meta` is test-writable, but the mark is the run's secret and the
212+ // setup file takes it out of the environment before any test module loads.
213+ 'test writes the provenance marker itself' : false ,
214+ 'test writes the marker after blocking the event loop past the budget' : false ,
215+ 'test writes the marker from the environment it can still read' : false ,
196216 } ) ;
197217 // Only the genuine timeout may reach a rerun.
198218 const listed = failures . map ( ( entry ) => ( { ...entry , file : LISTED } ) ) ;
@@ -206,6 +226,7 @@ test('an assertion message quoting a timeout still fails on the first run', asyn
206226 testCaseStub ( [ { name : 'AssertionError' , message : VITEST_TIMEOUT . message } ] , {
207227 meta : { } ,
208228 } ) as never ,
229+ TOKEN ,
209230 ) ;
210231 assert . ok ( impostor ) ;
211232 assert . equal ( impostor . timeout , false ) ;
@@ -222,19 +243,20 @@ test('a test that timed out AND failed an assertion is not retry-eligible', () =
222243 VITEST_TIMEOUT ,
223244 { name : 'AssertionError' , message : 'expected 1 to be 2' , expected : 2 , actual : 1 } ,
224245 ] ) as never ,
246+ TOKEN ,
225247 ) ;
226248 assert . equal ( mixed ?. timeout , false ) ;
227249} ) ;
228250
229251test ( 'the lane reporter keeps the real error message a timeout is classified by' , ( ) => {
230- const failed = failedTestCase ( testCaseStub ( [ VITEST_TIMEOUT ] ) as never ) ;
252+ const failed = failedTestCase ( testCaseStub ( [ VITEST_TIMEOUT ] ) as never , TOKEN ) ;
231253 assert . deepEqual ( failed , {
232254 file : `${ repoRoot } /${ LISTED } ` ,
233255 testName : 'opens a session' ,
234256 message : TIMEOUT_MESSAGE ,
235257 timeout : true ,
236258 } ) ;
237- assert . equal ( failedTestCase ( testCaseStub ( [ ] , { state : 'passed' } ) as never ) , null ) ;
259+ assert . equal ( failedTestCase ( testCaseStub ( [ ] , { state : 'passed' } ) as never , TOKEN ) , null ) ;
238260
239261 const target = path . join ( repoRoot , '.tmp/contention-retry/reporter-gate.json' ) ;
240262 writeFailureReport ( { failures : [ failure ( ) ] , blockers : [ ] } , target ) ;
0 commit comments