From 2e6ded9ddf23877a9949c49ca57e4d67735c6738 Mon Sep 17 00:00:00 2001 From: SakshiKoli-CS Date: Wed, 18 Nov 2026 11:40:25 +0530 Subject: [PATCH] fix(logs): print deployment log lines that become readable late MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The deployment log follow dropped lines. Logs become searchable out of timestamp order (the logs background-jobs service batches its bulk writes every 2 s and the deployment-logs index has two shards that refresh on their own), and a deployment turns LIVE before its last lines are indexed. The CLI asked only for logs after its newest printed line minus 1 ms and stopped reading at the first empty fetch after a terminal status, so an older line indexed late was never asked for again. Seen as "Edge functions deployed successfully." and "Build and hosting completed." missing before "Site is live". - LogTail asks from 10 s (DEPLOYMENT_LOG_LOOKBACK_MS) before the newest line and skips lines already printed, matched on timestamp, stage and message with a repeat count. - After a terminal status the watcher makes logSettleMs / pollDelayMs more reads (10 s / 2 s = 5), a failed one followed by the next. It settles only when the watch saw the deployment in flight or its updatedAt is within the window, so logs:get on an old deployment returns at once; waitUntilLive passes logSettleMs: 0. - Integration timing stubs pass logSettleMs: 0; a new integration test replays the reported sequence over the wire. AGENTS.md follows. The .talismanrc checksums for AGENTS.md and the edited test files (watcher, deployments:create, and the logs-get, deployments-create, projects-create and environments-create integration suites) are refreshed for these changes; they hold only fixture ids and key/value fields, no secret. Refs CL-8805. #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Co-authored-by: Sakshi Koli --- .talismanrc | 14 +-- AGENTS.md | 23 +++-- .../launch/deployments/create.test.ts | 7 ++ src/deployments/deployment.follower.test.ts | 9 +- src/deployments/deployment.follower.ts | 1 + src/deployments/deployment.log-tail.test.ts | 37 ++++++-- src/deployments/deployment.log-tail.ts | 55 ++++++------ src/deployments/deployment.watcher.test.ts | 88 ++++++++++++++++--- src/deployments/deployment.watcher.ts | 49 +++++++++-- test/integration/deployments-create.test.ts | 38 ++++++++ test/integration/environments-create.test.ts | 1 + test/integration/logs-get.test.ts | 13 +-- test/integration/projects-create.test.ts | 4 +- 13 files changed, 261 insertions(+), 78 deletions(-) diff --git a/.talismanrc b/.talismanrc index 99c7e54a..a2cb1be7 100644 --- a/.talismanrc +++ b/.talismanrc @@ -2,7 +2,7 @@ fileignoreconfig: - filename: src/logs/logs.inputs.test.ts checksum: 7b4df8790179c7dc4385acd1d380bb91397523bab111593742e2fc53c94da375 - filename: test/integration/logs-get.test.ts - checksum: 909965f29b2e6a0b6220b22c175b4a39edc05759fba1274a7a1f2f750a7e0e3b + checksum: 89ce4f3864c6316e8075e08fb86fb07d848cbe717f434c3b3e228622b263eed2 - filename: src/commands/launch/logs/get.test.ts checksum: d94d390de150956c799f64a84adfe0e02b5b21c66ddcc5cf55a1329aa67ec1a7 - filename: src/core/failure.test.ts @@ -93,7 +93,7 @@ fileignoreconfig: - "'SECRET=hunter2'" - "formFieldKey" - filename: test/integration/projects-create.test.ts - checksum: 5dd8e9b7c0d595f716b8046c2561cb240ef4144d0afddd5ed42e243442961515 + checksum: 7fe9bd2822624f634e3a83907c327223249cbb132d3591d7ae517b38d4fee161 - filename: test/fixtures/README.md checksum: d801c84c8b7d7f38f967573e9a3a70da4405b7cbcbf10d2a6e5be3d65fb3daa1 allowed_patterns: @@ -236,7 +236,7 @@ fileignoreconfig: - "a project name passed on argv to the project resolver" - "expect\\(Object.keys\\(DEPENDENCIES\\)\\).toEqual\\(\\['project', 'env'\\]\\)" - filename: AGENTS.md - checksum: 3379e8c597c8306e7377e60667f7df6e599e45e289928bc88b217356aa43feb1 + checksum: ecab79e8abbaae85038afe4f9e8e90e310eef5b0c002cee2b014926850be2c56 allowed_patterns: - "Do not add an exemption to a guard to make a new file pass" - "passes its own error wording to `client.request\\(req, MESSAGES\\)`" @@ -337,7 +337,7 @@ fileignoreconfig: - "-p, --password" - "-u, --username" - filename: test/integration/environments-create.test.ts - checksum: aaf11e8bc45b980aee892489d507e22362cf3d48d333d7c22f46b3a506944aca + checksum: 23d388585ff2f40f8a13bf068c50694fefe423ccf312922cf8887d9f2607af22 - filename: src/commands/launch/variables/list.test.ts checksum: 87ccb762f4931326fc50bca1c01aeb4a16d552e4dfa141d00370fdfafa4363be - filename: src/commands/launch/variables/set.test.ts @@ -435,13 +435,13 @@ fileignoreconfig: - filename: src/deployments/deployment.presenter.test.ts checksum: e393d6bb6ce3fd92920f5003603b161186085ea8939fae704e6f71bd9e1778bc - filename: test/integration/deployments-create.test.ts - checksum: 2d3a8ed873ca9bf6274b074393b73f94bdf958bc2f74ef31a7558ae41c475eb4 + checksum: fef71ae5b4bed7bcca111e90870238a26518e7a376475aa55768f5ffbe07ba36 - filename: src/deployments/deployment.watcher.test.ts - checksum: 0a9dab3e2436531279078bb5c0b3ef8524cebf7824b0602b7685ae0b4ef47923 + checksum: b04213c987716247cfb4f1ccac37ba96f0c9a73b4564bd45b4da8ee5def0b9ca - filename: src/deployments/deployment.create.ts checksum: ca64f3b29a68c4a2755a88a141e2cd9dbd6fa851a2776876d78c40f3168492e3 - filename: src/commands/launch/deployments/create.test.ts - checksum: 50c3f35c7246b49f6ebf5e48a9a9646ccbcb563c0c0e042fd38917e2f98cbba3 + checksum: bcbb2f85e0a78d38036af4723a0ce691f2692de05f5c7c9c3f97a5cca13497a6 - filename: src/deployments/deployment.create.test.ts checksum: 6545afa21a23b7e5de18ad8ff417f88a184577e7042713cfda90758ccbcd9869 - filename: test/integration/deployments-rollback.test.ts diff --git a/AGENTS.md b/AGENTS.md index 83c0c9bd..8ce30da1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -713,11 +713,15 @@ waits `SITE_OPEN_DELAY_MS` (six seconds, V1's wait, because a site opened the mo live can still answer "site not reachable") and opens that url in the browser, terminal or not, as V1 did. The wait goes through the injected `sleep`, so it is awaited and the browser opens before the command returns. -The first read asks from `DEPLOYMENT_LOGS_FROM` (the epoch) and every later one from the last -parseable timestamp printed: with a `timestamp` the service returns what came strictly after it in -ascending order, and without one it returns the newest page in **descending** order, so never let the -cursor drop the query. The poll that sees the terminal status still reads the logs once more before -returning, so the last lines of a build are never lost. The logs are auxiliary: a +The first read asks from `DEPLOYMENT_LOGS_FROM` (the epoch) and every later one from +`DEPLOYMENT_LOG_LOOKBACK_MS` (10 s) before the newest parseable timestamp printed: with a `timestamp` +the service returns what came strictly after it in ascending order, and without one it returns the +newest page in **descending** order, so never let the cursor drop the query. The look-back exists +because logs become searchable **out of timestamp order** (the logs background-jobs service batches +its bulk writes every 2 s, and the `deployment-logs` index has two shards that refresh on their own), +so a cursor at the newest line skips an older line indexed after it (CL-8805). `LogTail` remembers each +line it printed by timestamp, stage and message with a repeat count, so a re-read prints nothing twice +and identical lines in one millisecond still all print. The logs are auxiliary: a failed read prints nothing, is retried from the same cursor on the next poll, and never changes the outcome - the status poll alone decides that. @@ -725,7 +729,14 @@ outcome - the status poll alone decides that. reading from the advancing `tail.since()` until `tail.fresh` returns nothing new, at most `MAX_PAGES` reads, so a build that wrote more than one page after the last in-flight poll is never cut short. An in-flight poll still makes exactly one read. A read that fails mid-drain prints nothing, ends the drain and never changes the -outcome. `stop` (an `AbortSignal` on `DeploymentWatchDeps`, `DeploymentFollower.watch(scope, stop)`) ends the wait +outcome. After the drain the watcher **lets the logs settle**: a deployment turns LIVE before its last +lines are searchable (the agent logs `Build and hosting completed.` after it starts the job that marks +the site live), so it makes `logSettleMs / pollDelayMs` more reads (`DEPLOYMENT_LOG_SETTLE_MS` 10 s / +2 s = 5), sleeping the poll delay before each, a failed one simply followed by the next. It settles only +when the watch saw the deployment in flight or its `updatedAt` is within `logSettleMs`, so `logs:get` on a +long-finished deployment returns at once; `waitUntilLive` reads no logs and passes `logSettleMs: 0`. +`logSettleMs` is optional on `WatchTiming` and absent means no settling, so a test timing without it +keeps its old read count. `stop` (an `AbortSignal` on `DeploymentWatchDeps`, `DeploymentFollower.watch(scope, stop)`) ends the wait with `stop.reason` wherever it is noticed: before a poll, after one, during the drain, and at the sleep - a signal already aborted when the loop would sleep ends it at once rather than sleeping the full delay, and the default `sleep` takes the signal as its second argument and clears its timer on abort, so no timer outlives an interrupt. diff --git a/src/commands/launch/deployments/create.test.ts b/src/commands/launch/deployments/create.test.ts index 63e21eec..a6d33ca8 100644 --- a/src/commands/launch/deployments/create.test.ts +++ b/src/commands/launch/deployments/create.test.ts @@ -3,6 +3,7 @@ import { UsageError } from '../../../core/errors'; import type { UxLike } from '../../../core/prompt'; import { PICK_COMMIT } from '../../../deployments/deployment.inputs'; import { firstDeploymentFailureMessage } from '../../../deployments/deployment.presenter'; +import * as watcher from '../../../deployments/deployment.watcher'; import DeploymentsCreate from './create'; const DATA_DIR = '/work/site'; @@ -40,7 +41,13 @@ function commandUnderTest(resolved: Record, projectType = 'GITP return { command, lines, created }; } +const realWatchTiming = watcher.defaultWatchTiming; + describe('launch:deployments:create', () => { + beforeEach(() => { + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), sleep: async () => undefined })); + }); + it('declares org, project and env as required inputs and commit and last-upload as optional ones', () => { expect(DeploymentsCreate.inputs).toEqual({ org: { required: true }, diff --git a/src/deployments/deployment.follower.test.ts b/src/deployments/deployment.follower.test.ts index b15bf2f1..65e208e8 100644 --- a/src/deployments/deployment.follower.test.ts +++ b/src/deployments/deployment.follower.test.ts @@ -42,20 +42,21 @@ function harness(options: { withoutOpenUrl?: boolean; statuses?: string[] } = {} pollDelayMs: 1000, maxBackoffSteps: 3, timeoutMs: 60_000, + logSettleMs: 3000, }; return { follower: new DeploymentFollower(services, timing), timeline, polled, logged }; } describe('DeploymentFollower.watch', () => { - it('polls the deployment and reads its logs in the scope it was given', async () => { - const { follower, polled, logged } = harness(); + it('polls the deployment and reads its logs in the scope it was given, keeping on reading them while they settle', async () => { + const { follower, polled, logged } = harness({ statuses: ['DEPLOYING', 'LIVE'] }); const outcome = await follower.watch(SCOPE); expect(outcome).toEqual(expect.objectContaining({ kind: 'success', status: 'LIVE' })); - expect(polled).toEqual([SCOPE]); - expect(logged).toEqual([{ ...SCOPE, timestamp: '1970-01-01T00:00:00.000Z' }]); + expect(polled).toEqual([SCOPE, SCOPE]); + expect(logged).toEqual(Array(5).fill({ ...SCOPE, timestamp: '1970-01-01T00:00:00.000Z' })); }); }); diff --git a/src/deployments/deployment.follower.ts b/src/deployments/deployment.follower.ts index 60fbb45b..2327b36d 100644 --- a/src/deployments/deployment.follower.ts +++ b/src/deployments/deployment.follower.ts @@ -45,6 +45,7 @@ export class DeploymentFollower { outputIsTTY: this.services.outputIsTTY === true, loader: silentLoader, stop, + logSettleMs: 0, logs: async () => [], poll: () => this.services.api.deployments.get(scope), until: (deployment) => normalizeStatus(deployment.status) === LIVE_STATUS, diff --git a/src/deployments/deployment.log-tail.test.ts b/src/deployments/deployment.log-tail.test.ts index 9f5396cc..ef0c02df 100644 --- a/src/deployments/deployment.log-tail.test.ts +++ b/src/deployments/deployment.log-tail.test.ts @@ -1,4 +1,4 @@ -import { DEPLOYMENT_LOGS_FROM, LogTail } from './deployment.log-tail'; +import { DEPLOYMENT_LOG_LOOKBACK_MS, DEPLOYMENT_LOGS_FROM, LogTail } from './deployment.log-tail'; import type { DeploymentLog } from './types'; describe('log tail', () => { @@ -13,12 +13,13 @@ describe('log tail', () => { expect(tail.fresh(logs)).toEqual(logs); }); - it('asks from one millisecond before the last line it handed back', () => { + it('asks from the look-back window before the newest line it handed back, so a line that arrives late is still asked for', () => { const tail = new LogTail(); - tail.fresh([log('one', '2026-09-25T10:00:00.123Z'), log('two', '2026-09-25T10:00:01.456Z')]); + tail.fresh([log('one', '2026-09-25T10:00:00.123Z'), log('two', '2026-09-25T10:00:11.456Z')]); - expect(tail.since()).toBe('2026-09-25T10:00:01.455Z'); + expect(tail.since()).toBe('2026-09-25T10:00:01.456Z'); + expect(DEPLOYMENT_LOG_LOOKBACK_MS).toBe(10_000); }); it.each([[undefined], [''], ['not a time']])('keeps its place past a line with the timestamp %j', (timestamp) => { @@ -26,7 +27,7 @@ describe('log tail', () => { const logs = [log('one', '2026-09-25T10:00:00.123Z'), log('two', timestamp)]; expect(tail.fresh(logs)).toEqual(logs); - expect(tail.since()).toBe('2026-09-25T10:00:00.122Z'); + expect(tail.since()).toBe('2026-09-25T09:59:50.123Z'); }); it('skips the lines it already handed back at the last millisecond but not a new one sharing it', () => { @@ -51,6 +52,32 @@ describe('log tail', () => { expect(tail.fresh([retrying, retrying, retrying])).toEqual([retrying]); }); + it('hands back an older line that arrived after a newer one was already handed back, and the newer one only once', () => { + const tail = new LogTail(); + tail.fresh([log('Site is live', '2026-09-25T10:00:03.000Z')]); + + const next = tail.fresh([ + log('Edge functions deployed', '2026-09-25T10:00:02.198Z'), + log('Build and hosting completed', '2026-09-25T10:00:02.201Z'), + log('Site is live', '2026-09-25T10:00:03.000Z'), + ]); + + expect(next.map((entry) => entry.message)).toEqual(['Edge functions deployed', 'Build and hosting completed']); + }); + + it('skips every line it already handed back inside the look-back window, not only those at the last millisecond', () => { + const tail = new LogTail(); + tail.fresh([log('one', '2026-09-25T10:00:01.000Z'), log('two', '2026-09-25T10:00:02.000Z')]); + + const next = tail.fresh([ + log('one', '2026-09-25T10:00:01.000Z'), + log('two', '2026-09-25T10:00:02.000Z'), + log('three', '2026-09-25T10:00:03.000Z'), + ]); + + expect(next.map((entry) => entry.message)).toEqual(['three']); + }); + it('treats the same message from another stage as a different line', () => { const tail = new LogTail(); tail.fresh([{ ...log('done', '2026-09-25T10:00:00.123Z'), stage: 'Build' }]); diff --git a/src/deployments/deployment.log-tail.ts b/src/deployments/deployment.log-tail.ts index 1954c65b..b63e7b50 100644 --- a/src/deployments/deployment.log-tail.ts +++ b/src/deployments/deployment.log-tail.ts @@ -1,58 +1,57 @@ import type { DeploymentLog } from './types'; export const DEPLOYMENT_LOGS_FROM = new Date(0).toISOString(); +export const DEPLOYMENT_LOG_LOOKBACK_MS = 10_000; -interface LogCursor { - at: number; - printed: Map; -} - -function logIdentity(log: DeploymentLog): string { - return JSON.stringify([log.stage, log.message]); +function logIdentity(log: DeploymentLog, at: number): string { + return JSON.stringify([at, log.stage, log.message]); } function timeOf(log: DeploymentLog): number { return log.timestamp ? Date.parse(log.timestamp) : Number.NaN; } +/** + * Logs become searchable out of timestamp order, so every read re-asks for a look-back window + * behind the newest line and hands back only the copies of each line not yet handed back. + */ export class LogTail { - private cursor: LogCursor | undefined; + private newest: number | undefined; + private readonly printed = new Map(); since(): string { - return this.cursor === undefined ? DEPLOYMENT_LOGS_FROM : new Date(this.cursor.at - 1).toISOString(); + if (this.newest === undefined) { + return DEPLOYMENT_LOGS_FROM; + } + + return new Date(this.newest - DEPLOYMENT_LOG_LOOKBACK_MS).toISOString(); } fresh(logs: DeploymentLog[]): DeploymentLog[] { - const resumeAt = this.cursor?.at; - const repeats = new Map(this.cursor?.printed); + const seenInThisRead = new Map(); const fresh: DeploymentLog[] = []; for (const log of logs) { const at = timeOf(log); - const identity = logIdentity(log); - const seen = repeats.get(identity) ?? 0; - if (at === resumeAt && seen > 0) { - repeats.set(identity, seen - 1); + if (Number.isNaN(at)) { + fresh.push(log); continue; } - fresh.push(log); - this.remember(at, identity); - } - - return fresh; - } + const identity = logIdentity(log, at); + const copy = (seenInThisRead.get(identity) ?? 0) + 1; + seenInThisRead.set(identity, copy); - private remember(at: number, identity: string): void { - if (Number.isNaN(at)) { - return; - } + if (copy <= (this.printed.get(identity) ?? 0)) { + continue; + } - if (this.cursor === undefined || at !== this.cursor.at) { - this.cursor = { at, printed: new Map() }; + this.printed.set(identity, copy); + this.newest = this.newest === undefined ? at : Math.max(this.newest, at); + fresh.push(log); } - this.cursor.printed.set(identity, (this.cursor.printed.get(identity) ?? 0) + 1); + return fresh; } } diff --git a/src/deployments/deployment.watcher.test.ts b/src/deployments/deployment.watcher.test.ts index 81a08aca..563e31d6 100644 --- a/src/deployments/deployment.watcher.test.ts +++ b/src/deployments/deployment.watcher.test.ts @@ -8,6 +8,7 @@ import { DEPLOYMENT_MAX_POLL_ERRORS, DEPLOYMENT_POLL_DELAY_MS, DEPLOYMENT_LOADER_MESSAGE, + DEPLOYMENT_LOG_SETTLE_MS, DEPLOYMENT_WAIT_TIMEOUT_MS, defaultWatchTiming, watchDeployment, @@ -358,6 +359,8 @@ describe('deployment wait loop', () => { expect(timing.pollDelayMs).toBe(DEPLOYMENT_POLL_DELAY_MS); expect(timing.maxBackoffSteps).toBe(DEPLOYMENT_MAX_BACKOFF_STEPS); expect(timing.timeoutMs).toBe(DEPLOYMENT_WAIT_TIMEOUT_MS); + expect(timing.logSettleMs).toBe(DEPLOYMENT_LOG_SETTLE_MS); + expect(DEPLOYMENT_LOG_SETTLE_MS).toBe(10_000); }); }); @@ -417,7 +420,7 @@ describe('deployment log streaming', () => { ]); }); - it('asks for everything on the first fetch and then from one millisecond before the last line it printed', async () => { + it('asks for everything on the first fetch and then from the look-back window before the newest line it printed', async () => { const { deps, since } = streamingHarness( ['DEPLOYING', 'DEPLOYING', 'DEPLOYING', 'LIVE'], [ @@ -435,9 +438,9 @@ describe('deployment log streaming', () => { expect(since).toEqual([ DEPLOYMENT_LOGS_FROM, - '2026-09-25T10:00:01.455Z', - '2026-09-25T10:00:01.455Z', - '2026-09-25T10:00:01.999Z', + '2026-09-25T09:59:51.456Z', + '2026-09-25T09:59:51.456Z', + '2026-09-25T09:59:52.000Z', ]); expect(DEPLOYMENT_LOGS_FROM).toBe('1970-01-01T00:00:00.000Z'); }); @@ -463,7 +466,7 @@ describe('deployment log streaming', () => { DEPLOYMENT_LOGS_FROM, DEPLOYMENT_LOGS_FROM, DEPLOYMENT_LOGS_FROM, - '2026-09-25T10:00:04.999Z', + '2026-09-25T09:59:55.000Z', ]); }); @@ -536,7 +539,7 @@ describe('deployment log streaming', () => { await watchDeployment(deps); - expect(since).toEqual([DEPLOYMENT_LOGS_FROM, '2026-09-25T10:00:00.122Z']); + expect(since).toEqual([DEPLOYMENT_LOGS_FROM, '2026-09-25T09:59:50.123Z']); expect(lines).toContain('two'); }, ); @@ -833,9 +836,9 @@ describe('deployment log drain on the terminal poll', () => { ]); expect(since).toEqual([ DEPLOYMENT_LOGS_FROM, - '2026-09-25T10:00:00.999Z', - '2026-09-25T10:00:01.999Z', - '2026-09-25T10:00:02.999Z', + '2026-09-25T09:59:51.000Z', + '2026-09-25T09:59:52.000Z', + '2026-09-25T09:59:53.000Z', ]); expect(slept).toEqual([]); }); @@ -864,10 +867,10 @@ describe('deployment log drain on the terminal poll', () => { expect(since).toEqual([ DEPLOYMENT_LOGS_FROM, - '2026-09-25T10:00:00.999Z', - '2026-09-25T10:00:01.999Z', - '2026-09-25T10:00:02.999Z', - '2026-09-25T10:00:03.999Z', + '2026-09-25T09:59:51.000Z', + '2026-09-25T09:59:52.000Z', + '2026-09-25T09:59:53.000Z', + '2026-09-25T09:59:54.000Z', ]); }); @@ -929,6 +932,65 @@ describe('deployment log drain on the terminal poll', () => { }); }); +describe('deployment log settling after the terminal poll', () => { + it('keeps reading for the settle window and prints a line that only became readable after the deployment ended', async () => { + const { deps, lines } = streamingHarness( + ['DEPLOYING', 'LIVE'], + [[], [logAt(3, 'Site is live')], [], [logAt(2, 'Build and hosting completed')], []], + ); + + const outcome = await watchDeployment({ ...deps, logSettleMs: 3000 }); + + expect(outcome.kind).toBe('success'); + expect(lines).toEqual([ + '2026-09-25 10:00:03.000: Site is live', + '2026-09-25 10:00:02.000: Build and hosting completed', + ]); + }); + + it('does not wait for the logs to settle on a deployment that ended before the watch began', async () => { + const { deps, since } = streamingHarness(['LIVE'], [[logAt(1, 'one')], []]); + const slept: number[] = []; + + await watchDeployment({ ...deps, logSettleMs: 3000, sleep: async (ms) => void slept.push(ms) }); + + expect(slept).toEqual([]); + expect(since).toHaveLength(2); + }); + + it('still waits for the logs to settle on a deployment that ended within the settle window before the watch began', async () => { + const { deps } = streamingHarness(['LIVE'], [[]]); + const slept: number[] = []; + const endedJustNow = async (): Promise => ({ uid: UID, status: 'LIVE', updatedAt: new Date(-2500).toISOString() }); + + await watchDeployment({ ...deps, poll: endedJustNow, logSettleMs: 3000, sleep: async (ms) => void slept.push(ms) }); + + expect(slept).toEqual([1000, 1000, 1000]); + }); + + it('reads again after a read that failed while the logs settle, rather than giving up on them', async () => { + const { deps, lines } = streamingHarness( + ['DEPLOYING', 'FAILED'], + [[], [], new Error('Gateway timeout'), [logAt(2, 'npm ERR! build failed')], []], + ); + + const outcome = await watchDeployment({ ...deps, logSettleMs: 3000 }); + + expect(outcome.kind).toBe('failure'); + expect(lines).toEqual(['2026-09-25 10:00:02.000: npm ERR! build failed']); + }); + + it('sleeps the poll delay between settle reads, reading the logs settle window / poll delay times', async () => { + const { deps, since } = streamingHarness(['DEPLOYING', 'LIVE'], [[]]); + const slept: number[] = []; + + await watchDeployment({ ...deps, logSettleMs: 3500, sleep: async (ms) => void slept.push(ms) }); + + expect(slept).toEqual([1000, 1000, 1000, 1000]); + expect(since).toHaveLength(5); + }); +}); + describe('deployment wait loop already stopped when it would sleep', () => { function stoppedAtSleep(statuses: string[]) { const controller = new AbortController(); diff --git a/src/deployments/deployment.watcher.ts b/src/deployments/deployment.watcher.ts index bd108238..f8b773ac 100644 --- a/src/deployments/deployment.watcher.ts +++ b/src/deployments/deployment.watcher.ts @@ -11,6 +11,7 @@ export const DEPLOYMENT_POLL_DELAY_MS = 2000; export const DEPLOYMENT_MAX_BACKOFF_STEPS = 5; export const DEPLOYMENT_WAIT_TIMEOUT_MS = 20 * 60 * 1000; export const DEPLOYMENT_MAX_POLL_ERRORS = 3; +export const DEPLOYMENT_LOG_SETTLE_MS = 10_000; export const DEPLOYMENT_LOADER_MESSAGE = 'Loading deployment logs...'; export interface WatchTiming { @@ -19,6 +20,7 @@ export interface WatchTiming { pollDelayMs: number; maxBackoffSteps: number; timeoutMs: number; + logSettleMs?: number; } export interface DeploymentWatchDeps extends WatchTiming { @@ -66,6 +68,7 @@ export function defaultWatchTiming(): WatchTiming { pollDelayMs: DEPLOYMENT_POLL_DELAY_MS, maxBackoffSteps: DEPLOYMENT_MAX_BACKOFF_STEPS, timeoutMs: DEPLOYMENT_WAIT_TIMEOUT_MS, + logSettleMs: DEPLOYMENT_LOG_SETTLE_MS, }; } @@ -120,6 +123,35 @@ async function freshLogs(deps: DeploymentWatchDeps, tail: LogTail): Promise { + const logs = await freshLogs(deps, tail); + + deps.stop?.throwIfAborted(); + + for (const log of logs) { + deps.ux.print(deploymentLogLine(log, deps.outputIsTTY, process.stdout.columns)); + } + + return logs.length; +} + +/** Logs reach the logs service seconds after the deployment ends, so keep reading for a while. */ +async function settleLogs(deps: DeploymentWatchDeps, tail: LogTail): Promise { + const reads = Math.floor((deps.logSettleMs ?? 0) / deps.pollDelayMs); + + for (let read = 0; read < reads; read += 1) { + deps.loader.start(DEPLOYMENT_LOADER_MESSAGE); + await sleepUnlessStopped(deps, deps.pollDelayMs); + await printFreshLogs(deps, tail); + } +} + +function endedWithin(deployment: Deployment, windowMs: number, now: number): boolean { + const endedAt = deployment.updatedAt ? Date.parse(deployment.updatedAt) : Number.NaN; + + return now - endedAt <= windowMs; +} + function untilKind(until: (deployment: Deployment) => boolean, deployment: Deployment): 'success' | 'in-flight' { return until(deployment) ? 'success' : 'in-flight'; } @@ -130,6 +162,7 @@ async function waitFor(deps: DeploymentWatchDeps): Promise { let attempt = 0; let consecutiveErrors = 0; const tail = new LogTail(); + let sawInFlight = false; for (;;) { deps.stop?.throwIfAborted(); @@ -159,23 +192,21 @@ async function waitFor(deps: DeploymentWatchDeps): Promise { const terminal = kind === 'success' || kind === 'failure'; for (let page = 0; page < (terminal ? MAX_PAGES : 1); page += 1) { - const logs = await freshLogs(deps, tail); - - deps.stop?.throwIfAborted(); - - for (const log of logs) { - deps.ux.print(deploymentLogLine(log, deps.outputIsTTY, process.stdout.columns)); - } - - if (logs.length === 0) { + if ((await printFreshLogs(deps, tail)) === 0) { break; } } if (terminal) { + if (sawInFlight || endedWithin(deployment, deps.logSettleMs ?? 0, deps.now())) { + await settleLogs(deps, tail); + } + return { kind, status, deployment }; } + sawInFlight = true; + attempt += 1; const delay = backoff.delayFor(Math.min(attempt, deps.maxBackoffSteps)); diff --git a/test/integration/deployments-create.test.ts b/test/integration/deployments-create.test.ts index 8e9a4e42..0bc3a084 100644 --- a/test/integration/deployments-create.test.ts +++ b/test/integration/deployments-create.test.ts @@ -164,6 +164,7 @@ describe('integration: launch:deployments:create on the wire', () => { jest.spyOn(authHandler, 'compareOAuthExpiry').mockResolvedValue(undefined); jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async () => undefined, })); }); @@ -546,6 +547,42 @@ describe('integration: launch:deployments:create on the wire', () => { expect(onWire).toEqual([]); }); + it('keeps reading the logs after LIVE and prints the lines that became readable late, each once', async () => { + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), sleep: async () => undefined })); + const edge = { message: 'Deploying edge functions.', timestamp: '2026-09-25T10:00:20.000Z' }; + const live = { message: 'Site is live', timestamp: '2026-09-25T10:00:23.000Z' }; + const late = [ + { message: 'Edge functions deployed successfully.', timestamp: '2026-09-25T10:00:22.198Z' }, + { message: 'Build and hosting completed.', timestamp: '2026-09-25T10:00:22.201Z' }, + ]; + const lookBack = { timestamp: '2026-09-25T10:00:13.000Z' }; + gitProject(); + captureCreate(); + for (const status of ['DEPLOYING', 'LIVE']) { + hub() + .get(`${DEPLOYMENTS}/${DEPLOYMENT_UID}`) + .query({}) + .reply(200, { deployment: { uid: DEPLOYMENT_UID, status, deploymentUrl: 'my-site.example.test' } }); + } + hub().get(LOGS).query({ timestamp: '1970-01-01T00:00:00.000Z' }).reply(200, { deploymentLogs: [edge] }); + hub().get(LOGS).query({ timestamp: '2026-09-25T10:00:10.000Z' }).reply(200, { deploymentLogs: [edge, live] }); + hub().get(LOGS).query(lookBack).reply(200, { deploymentLogs: [] }); + hub().get(LOGS).query(lookBack).times(5).reply(200, { deploymentLogs: [edge, ...late, live] }); + + const { error, stdout } = await runCommand(createArgs('--data-dir', dataDir), config); + + expect(error).toBeUndefined(); + expect(stdout.split('\n').filter((line) => line.includes(': '))).toEqual([ + '2026-09-25 10:00:20.000: Deploying edge functions.', + '2026-09-25 10:00:23.000: Site is live', + '2026-09-25 10:00:22.198: Edge functions deployed successfully.', + '2026-09-25 10:00:22.201: Build and hosting completed.', + ]); + expect(stdout).toContain('Deployment URL: https://my-site.example.test'); + expect(nock.pendingMocks()).toEqual([]); + expect(onWire).toEqual([]); + }); + it('T33: exits 1 on a FAILED deployment, naming FAILED and the logs command with the full scope', async () => { gitProject(); captureCreate(); @@ -566,6 +603,7 @@ describe('integration: launch:deployments:create on the wire', () => { followUp(['DEPLOYING']); jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async () => undefined, timeoutMs: 1, })); diff --git a/test/integration/environments-create.test.ts b/test/integration/environments-create.test.ts index 0dbe264d..c2ce5198 100644 --- a/test/integration/environments-create.test.ts +++ b/test/integration/environments-create.test.ts @@ -117,6 +117,7 @@ describe('integration: launch:env:create on the wire', () => { jest.spyOn(authHandler, 'compareOAuthExpiry').mockResolvedValue(undefined); jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async () => undefined, })); }); diff --git a/test/integration/logs-get.test.ts b/test/integration/logs-get.test.ts index 3aad9c8b..76cbdd50 100644 --- a/test/integration/logs-get.test.ts +++ b/test/integration/logs-get.test.ts @@ -140,6 +140,7 @@ describe('integration: launch:logs:get on the wire', () => { jest.spyOn(authHandler, 'compareOAuthExpiry').mockResolvedValue(undefined); jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async () => undefined, })); }); @@ -321,6 +322,7 @@ describe('integration: launch:logs:get on the wire', () => { let clock = NOW; jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, now: () => clock, sleep: async (ms: number) => { clock += ms; @@ -345,7 +347,7 @@ describe('integration: launch:logs:get on the wire', () => { }); it('T47: stops on Ctrl-C with the yellow line and exit 3, no Error line, no extra poll, nothing left behind', async () => { - jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => realWatchTiming()); + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), logSettleMs: 0 })); const restoreStdout = stdoutReportingTTY(true); jest.spyOn(cliux, 'loaderV2').mockReturnValue(undefined as never); const listenersBefore = process.listenerCount('SIGINT'); @@ -390,7 +392,7 @@ describe('integration: launch:logs:get on the wire', () => { }); it('T47: prints the interrupt line plain on a pipe', async () => { - jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => realWatchTiming()); + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), logSettleMs: 0 })); ownedProject(); hub().get(DEPLOYMENT_PATH).times(5).reply(200, { deployment: { uid: DEPLOYMENT_UID, status: 'DEPLOYING' } }); hub() @@ -910,7 +912,7 @@ describe('integration: launch:logs:get on the wire', () => { }); it('T88: ends the command at the server-limit frame although the stream stays open with heartbeats and more frames', async () => { - jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => realWatchTiming()); + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), logSettleMs: 0 })); const listenersBefore = process.listenerCount('SIGINT'); const timers = trackTimers(EVENT_STREAM_IDLE_TIMEOUT_MS); const events: string[] = []; @@ -1057,6 +1059,7 @@ describe('integration: launch:logs:get on the wire', () => { const sleeps: number[] = []; jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async (ms: number) => void sleeps.push(ms), })); const attempts = liveRoute().times(3).replyWithError({ code: 'ECONNRESET', message: 'socket hang up' }); @@ -1100,7 +1103,7 @@ describe('integration: launch:logs:get on the wire', () => { [true, `${ESCAPE}[33mStopped following the logs.${ESCAPE}[39m`], [false, 'Stopped following the logs.'], ])('T92: Ctrl-C on a live stream (terminal=%s) prints the stop line, exits 3 and leaves nothing behind', async (tty, stopLine) => { - jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => realWatchTiming()); + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), logSettleMs: 0 })); const restoreStdout = stdoutReportingTTY(tty); jest.spyOn(cliux, 'loaderV2').mockReturnValue(undefined as never); const listenersBefore = process.listenerCount('SIGINT'); @@ -1138,7 +1141,7 @@ describe('integration: launch:logs:get on the wire', () => { }); it('T93: Ctrl-C while the command waits to reconnect cancels the wait and opens nothing more', async () => { - jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => realWatchTiming()); + jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), logSettleMs: 0 })); const listenersBefore = process.listenerCount('SIGINT'); const timers = trackTimers(1000); ownedProject(); diff --git a/test/integration/projects-create.test.ts b/test/integration/projects-create.test.ts index a29db001..81cb931c 100644 --- a/test/integration/projects-create.test.ts +++ b/test/integration/projects-create.test.ts @@ -10,6 +10,7 @@ import nock from 'nock'; import open from 'open'; +import { DEPLOYMENT_LOG_LOOKBACK_MS } from '../../src/deployments/deployment.log-tail'; import * as watcher from '../../src/deployments/deployment.watcher'; import { CTRL_C, answerPrompts, onTerminal } from '../support/terminal'; import { loadCliConfig, routeConsoleLogToStdout } from '../support/oclif'; @@ -211,7 +212,7 @@ function stubFollowUp(status: string, deploymentLogs: unknown[] = []): nock.Scop hub() .get(logsPath) .matchHeader('x-project-uid', PROJECT_UID) - .query({ timestamp: new Date(Date.parse(last) - 1).toISOString() }) + .query({ timestamp: new Date(Date.parse(last) - DEPLOYMENT_LOG_LOOKBACK_MS).toISOString() }) .reply(200, { deploymentLogs: [] }); } @@ -285,6 +286,7 @@ describe('integration: launch:projects:create on the wire', () => { slept = []; jest.spyOn(watcher, 'defaultWatchTiming').mockImplementation(() => ({ ...realWatchTiming(), + logSettleMs: 0, sleep: async (ms: number) => { slept.push(ms); },