@@ -84,6 +84,30 @@ const DEPLOY_DOCS = resolve(REPO_ROOT, 'content/docs/deployment/index.mdx');
8484 * anyway. Only reached when the record never arrives early — i.e. when the
8585 * contract is broken — and exists so that failure is an assertion rather than a
8686 * suite that hangs until the runner kills it.
87+ *
88+ * ## The clock starts at device-code issuance, not at spawn (#6855)
89+ *
90+ * This budget is armed when the endpoint hands the CLI its device code, because
91+ * that is the first instant at which the contract is even measurable: from
92+ * there the CLI holds the verification URL and owes it to stdout. Everything
93+ * before it — `script(1)`, the `tsx` transform of the whole oclif command tree,
94+ * module loading — is process startup, about which #6531/#6730 say nothing.
95+ *
96+ * Armed at spawn instead, this budget policed startup rather than the contract,
97+ * and that is what made the assertion flaky. Measured on an idle machine, of
98+ * the latency from spawn to the record being readable:
99+ *
100+ * | segment | measured |
101+ * |--------------------------------------|-----------|
102+ * | spawn → device-code request (startup)| 3423–3713 ms |
103+ * | device-code response → record readable (the contract) | 16–28 ms |
104+ *
105+ * So ~99.4% of the old budget was spent on work the contract does not govern,
106+ * leaving startup needing only a ~5.7x slowdown to exhaust 20 s — routine on a
107+ * merge-queue runner executing 84 tasks for 11½ minutes. It duly ejected two
108+ * unrelated PRs (#6847 spec-only, #6835 docs-only). Anchored here, the budget
109+ * covers a ~20 ms window with ~1000x headroom, and the number itself is
110+ * unchanged: this is a re-anchoring, NOT a widened timeout.
87111 */
88112const RELEASE_DEADLINE_MS = 20_000 ;
89113
@@ -111,6 +135,14 @@ function startDeviceEndpoint(outcome: 'token' | 'access_denied') {
111135 let released = false ;
112136 let authorizedAt : number | null = null ;
113137
138+ // Resolved the moment the CLI has been handed its device code — the instant
139+ // the emission contract starts running, and so the anchor for the release
140+ // deadline. Definite-assignment: the Promise executor runs synchronously.
141+ let markDeviceCodeIssued ! : ( ) => void ;
142+ const deviceCodeIssued = new Promise < void > ( ( res ) => {
143+ markDeviceCodeIssued = res ;
144+ } ) ;
145+
114146 const server : Server = createServer ( ( req , res ) => {
115147 req . resume ( ) ;
116148 req . on ( 'end' , ( ) => {
@@ -121,6 +153,7 @@ function startDeviceEndpoint(outcome: 'token' | 'access_denied') {
121153 const { pathname } = new URL ( req . url ?? '/' , 'http://placeholder' ) ;
122154
123155 if ( pathname === '/api/v1/auth/device/code' ) {
156+ markDeviceCodeIssued ( ) ;
124157 return send ( 200 , {
125158 device_code : 'DEV-CODE-6730' ,
126159 user_code : 'WXYZ-6730' ,
@@ -153,6 +186,7 @@ function startDeviceEndpoint(outcome: 'token' | 'access_denied') {
153186 release : ( ) => {
154187 released = true ;
155188 } ,
189+ deviceCodeIssued,
156190 authorizedAt : ( ) => authorizedAt ,
157191 listen : ( ) =>
158192 new Promise < number > ( ( res ) => {
@@ -218,12 +252,20 @@ async function runCloudDeviceLogin(opts: {
218252 }
219253 } , WATCH_MS ) ;
220254
221- const deadline = setTimeout ( ( ) => {
222- if ( urlSeenAt === null ) {
223- releasedByDeadline = true ;
224- endpoint . release ( ) ;
225- }
226- } , RELEASE_DEADLINE_MS ) ;
255+ // Armed on device-code issuance rather than here, so the budget covers the
256+ // window the contract governs and not the child's startup — see
257+ // {@link RELEASE_DEADLINE_MS } for the measurements behind that (#6855).
258+ // Stays unarmed if the CLI never reaches the device flow at all; that run
259+ // ends when the child exits, and the assertions below name the absence.
260+ let deadline : ReturnType < typeof setTimeout > | undefined ;
261+ void endpoint . deviceCodeIssued . then ( ( ) => {
262+ deadline = setTimeout ( ( ) => {
263+ if ( urlSeenAt === null ) {
264+ releasedByDeadline = true ;
265+ endpoint . release ( ) ;
266+ }
267+ } , RELEASE_DEADLINE_MS ) ;
268+ } ) ;
227269
228270 const shell = [
229271 `'${ TSX } ' '${ CLI } ' cloud login${ opts . json ? ' --json' : '' } --no-browser` ,
@@ -330,7 +372,13 @@ describe('os cloud login --json — the declared NDJSON stream (#6730)', () => {
330372 it ( 'hands over the verification URL BEFORE authorization — the reason this route was chosen' , ( ) => {
331373 // The endpoint only authorized because the watcher had already read the
332374 // record off stdout, so these two facts are the run's own history.
333- expect ( ok . releasedByDeadline , 'the device record never reached stdout early' ) . toBe ( false ) ;
375+ expect (
376+ ok . releasedByDeadline ,
377+ `the device record did not reach stdout within ${ RELEASE_DEADLINE_MS } ms of the CLI ` +
378+ 'receiving its device code — the buffered-emit regression this route was chosen to ' +
379+ 'prevent. This window excludes process startup (#6855), so a slow runner is not a ' +
380+ 'cause: at the moment it opens the CLI already holds the verification URL.' ,
381+ ) . toBe ( false ) ;
334382 expect ( ok . urlSeenAt ) . not . toBeNull ( ) ;
335383 expect ( ok . authorizedAt ) . not . toBeNull ( ) ;
336384 expect ( ok . urlSeenAt ! ) . toBeLessThanOrEqual ( ok . authorizedAt ! ) ;
0 commit comments