From ee289e26d4f9cae93a5ee99a8a1600e6fec2364e Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 02:08:53 +0000 Subject: [PATCH 1/9] test(agent): pin the turn a transient socket reconnect abandons (PM-1199) A reconnect on the shared ComfyUI websocket settles the live agent turn client-side and never re-attaches, while the server keeps the assistant row streaming and answers the next post with 409 TURN_IN_PROGRESS. Adds red coverage at both levels: - useAgentSession (g3)/(g4) it.fails: the session settles the turn on a live->down->live blip and drops the deltas that follow. - agentTurnSurvivesSocketDrop.spec.ts: three test.fail cases driving a real socket close through the rendered panel, plus a passing audio control showing playback is incidental to the disconnect. agentTurnLockFixture models the server's single-active-turn guard; ws.ts gains nextWebSocket() so a test can await the post-reconnect socket. --- .../fixtures/agentTurnLockFixture.ts | 259 ++++++++++++++++++ browser_tests/fixtures/ws.ts | 48 +++- .../agent/agentTurnSurvivesSocketDrop.spec.ts | 132 +++++++++ .../composables/agent/useAgentSession.test.ts | 47 ++++ 4 files changed, 475 insertions(+), 11 deletions(-) create mode 100644 browser_tests/fixtures/agentTurnLockFixture.ts create mode 100644 browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts new file mode 100644 index 00000000000..a5c30212d24 --- /dev/null +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -0,0 +1,259 @@ +import { expect, mergeTests } from '@playwright/test' +import type { Locator, Page, WebSocketRoute } from '@playwright/test' + +import type { + AgentCancelAccepted, + AgentError, + AgentMessage, + AgentTurnAccepted +} from '@comfyorg/ingest-types' +import { zAgentPostMessageRequest } from '@comfyorg/ingest-types/zod' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +import { agentTest } from '@e2e/fixtures/agentPanelFixture' +import { workflowSelectionTest } from '@e2e/fixtures/agentWorkflowSelectionFixture' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { webSocketFixture } from '@e2e/fixtures/ws' + +const THREAD_ID = 'b9d0a2a1-0f2c-4f1a-9a5e-6b0f4f2c1d77' +const TURN_ID = '2dd4f367-3399-4cb4-8127-547f531c289a' +const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-000000000001' + +/** + * Verbatim from `services/agent/server/agent_handler.go`, which answers a post + * to a thread whose assistant row is still `streaming` with HTTP 409 and this + * body. The client renders it as `agent.sendFailed` + ': ' + this text. + */ +const TURN_IN_PROGRESS: AgentError = { + error: 'a turn is already in progress for this thread' +} + +const TURN_THINKING_TEXT = 'Wiring the audio output node.' +export const POST_RECONNECT_TEXT = 'Reconnected, and the graph is ready.' + +const TURN_THINKING_EVENT: AgentWsEvent = { + type: 'agent_thinking', + data: { delta: TURN_THINKING_TEXT, message_id: TURN_ID, thread_id: THREAD_ID } +} + +const TURN_TOOL_EVENT: AgentWsEvent = { + type: 'agent_tool_call', + data: { + tool_call_id: 'call-add-node', + tool_name: 'add_node', + status: 'success', + duration_ms: 1300, + message_id: TURN_ID, + thread_id: THREAD_ID + } +} + +export const POST_RECONNECT_EVENT: AgentWsEvent = { + type: 'agent_message_delta', + data: { + delta: POST_RECONNECT_TEXT, + message_id: TURN_ID, + thread_id: THREAD_ID + } +} + +/** + * The server's half of a turn, modelled on the real single-active-turn guard: + * an assistant row goes `streaming` when a turn starts and only leaves that + * state when the turn completes, fails, or is cancelled. Dropping the client's + * socket does not touch it — that asymmetry is what these specs exercise. + */ +class TurnLockServer { + private streaming = false + private prompt = '' + private rejected = 0 + + get turnIsStreaming(): boolean { + return this.streaming + } + + get rejectedPosts(): number { + return this.rejected + } + + completeTurn(): void { + this.streaming = false + } + + transcript(): AgentMessage[] { + return [ + { + id: 'user-1', + thread_id: THREAD_ID, + turn_id: TURN_ID, + seq: 1, + role: 'user', + status: 'complete', + workflow_id: WORKFLOW_ID, + content: { text: this.prompt } + }, + { + id: TURN_ID, + thread_id: THREAD_ID, + turn_id: TURN_ID, + seq: 2, + role: 'assistant', + status: this.streaming ? 'streaming' : 'complete', + workflow_id: WORKFLOW_ID + } + ] + } + + startTurn(prompt: string): AgentTurnAccepted { + this.prompt = prompt + this.streaming = true + return { message_id: TURN_ID, thread_id: THREAD_ID } + } + + rejectPost(): AgentError { + this.rejected++ + return TURN_IN_PROGRESS + } +} + +async function routeTurnLock( + page: Page, + server: TurnLockServer +): Promise { + await page.route('**/api/agent/threads/*/messages', (route) => { + if (route.request().method() === 'GET') + return route.fulfill(jsonRoute(server.transcript())) + if (server.turnIsStreaming) + return route.fulfill({ ...jsonRoute(server.rejectPost()), status: 409 }) + const request = zAgentPostMessageRequest.parse( + route.request().postDataJSON() + ) + return route.fulfill({ + ...jsonRoute(server.startTurn(request.content)), + status: 202 + }) + }) + + await page.route('**/api/agent/threads/*/messages/*/cancel', (route) => { + server.completeTurn() + const accepted: AgentCancelAccepted = { status: 'cancelling' } + return route.fulfill(jsonRoute(accepted)) + }) +} + +export class AgentTurnLockHarness { + public readonly panel: Locator + public readonly composer: Locator + public readonly sendButton: Locator + public readonly stopButton: Locator + public readonly workSummary: Locator + public readonly workingRow: Locator + public readonly userBubbles: Locator + + constructor( + private readonly page: Page, + private readonly server: TurnLockServer, + private readonly finishSave: (success: boolean) => void, + private readonly savedPaths: () => number, + private readonly getWebSocket: () => Promise, + private readonly nextWebSocket: () => Promise + ) { + this.panel = page.locator('#agent-panel-root') + this.composer = this.panel.getByRole('textbox') + this.sendButton = this.panel.getByRole('button', { + name: enMessages.agent.send, + exact: true + }) + this.stopButton = this.panel.getByRole('button', { + name: enMessages.agent.stop, + exact: true + }) + this.workSummary = this.panel.getByRole('button', { name: /^Worked for / }) + this.workingRow = this.panel.getByText(enMessages.agent.working, { + exact: true + }) + this.userBubbles = this.panel.getByTestId('user-message-bubble') + } + + rejectedPosts(): number { + return this.server.rejectedPosts + } + + /** Opens the panel on a blank workflow and points the composer at that tab. */ + async openOnBlankWorkflow(): Promise { + await expect( + this.page.getByTestId('integrated-tab-bar-actions') + ).toHaveAttribute('data-agent-gate-settled', 'true', { timeout: 15_000 }) + await this.page + .getByRole('button', { name: enMessages.agent.askComfyAgent }) + .click() + await expect(this.panel).toBeVisible() + await this.page + .getByRole('button', { + name: enMessages.sideToolbar.newBlankWorkflow, + exact: true + }) + .click() + await expect(this.panel).toBeVisible() + await this.panel + .getByRole('button', { name: enMessages.agent.switchWorkflow }) + .click() + await this.page + .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) + .click() + await expect.poll(() => this.savedPaths()).toBe(1) + this.finishSave(true) + } + + /** Sends a prompt and streams it to the point where a user sees work happening. */ + async startTurn(prompt: string): Promise { + const live = await this.getWebSocket() + await this.composer.fill(prompt) + await this.sendButton.click() + await expect(this.stopButton).toBeVisible() + this.push(live, TURN_THINKING_EVENT) + await expect(this.panel.getByText(TURN_THINKING_TEXT)).toBeVisible() + this.push(live, TURN_TOOL_EVENT) + await expect(this.workingRow).toBeVisible() + } + + push(ws: WebSocketRoute, event: AgentWsEvent): void { + ws.send(JSON.stringify(event)) + } + + /** Drops the live socket and resolves with the one the client reconnects on. */ + async dropSocket(): Promise { + const reconnected = this.nextWebSocket() + const live = await this.getWebSocket() + await live.close() + return reconnected + } +} + +export const agentTurnLockTest = mergeTests( + agentTest, + workflowSelectionTest, + webSocketFixture +).extend<{ turnLock: AgentTurnLockHarness }>({ + turnLock: async ( + { page, workflowSelection, getWebSocket, nextWebSocket }, + use + ) => { + // Workflow selection boots the app before these agent-specific routes. + void workflowSelection + const server = new TurnLockServer() + await routeTurnLock(page, server) + await use( + new AgentTurnLockHarness( + page, + server, + workflowSelection.finishSave, + () => workflowSelection.savedPaths.length, + getWebSocket, + nextWebSocket + ) + ) + } +}) diff --git a/browser_tests/fixtures/ws.ts b/browser_tests/fixtures/ws.ts index b54525c8bd9..716e9272615 100644 --- a/browser_tests/fixtures/ws.ts +++ b/browser_tests/fixtures/ws.ts @@ -1,6 +1,16 @@ import { networkIsolationFixture as base } from '@e2e/fixtures/networkIsolationFixture' import type { WebSocketRoute } from '@playwright/test' +/** + * The `/ws` connections the page opens, in order. A reconnect routes a second + * time, so a test that drops the socket needs the route the client opened + * *after* the drop, not the dead one it already holds. + */ +interface WebSocketTracker { + current: () => WebSocketRoute | undefined + waitForNext: () => Promise +} + function createWebSocketRouteHandler( connectWebSocketToServer: boolean, onRouted: (ws: WebSocketRoute) => void @@ -20,28 +30,44 @@ function createWebSocketRouteHandler( export const webSocketFixture = base.extend<{ connectWebSocketToServer: boolean getWebSocket: () => Promise + nextWebSocket: () => Promise + webSocketTracker: WebSocketTracker }>({ connectWebSocketToServer: [true, { option: true }], - getWebSocket: [ + webSocketTracker: [ async ({ context, connectWebSocketToServer }, use) => { - let latest: WebSocketRoute | undefined - let resolve: ((ws: WebSocketRoute) => void) | undefined + let current: WebSocketRoute | undefined + const waiters = new Set<(ws: WebSocketRoute) => void>() await context.routeWebSocket( /\/ws/, createWebSocketRouteHandler(connectWebSocketToServer, (ws) => { - latest = ws - resolve?.(ws) + current = ws + const pending = [...waiters] + waiters.clear() + for (const resolve of pending) resolve(ws) }) ) - await use(() => { - if (latest) return Promise.resolve(latest) - return new Promise((r) => { - resolve = r - }) + await use({ + current: () => current, + waitForNext: () => + new Promise((resolve) => waiters.add(resolve)) }) }, { auto: true } - ] + ], + getWebSocket: async ({ webSocketTracker }, use) => { + await use(() => { + const open = webSocketTracker.current() + return open ? Promise.resolve(open) : webSocketTracker.waitForNext() + }) + }, + /** + * Resolves with the NEXT socket the page opens. Call it before closing the + * current one so the waiter is armed before the client reconnects. + */ + nextWebSocket: async ({ webSocketTracker }, use) => { + await use(() => webSocketTracker.waitForNext()) + } }) diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts new file mode 100644 index 00000000000..85649671526 --- /dev/null +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -0,0 +1,132 @@ +import { expect } from '@playwright/test' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } + +import { + POST_RECONNECT_EVENT, + POST_RECONNECT_TEXT, + agentTurnLockTest as test +} from '@e2e/fixtures/agentTurnLockFixture' + +// PM-1199 / PM-1200. Reported after a long auto-mode run: the action stream +// stopped updating, the "Working..." row was replaced by a "Worked for 3m" +// summary as if the turn had finished, and the next message came back +// "Message failed to send: a turn is already in progress for this thread" — +// with no refresh and no tab close anywhere in the journey. +// +// The agent chat stream rides the shared ComfyUI websocket, so any transient +// close dispatches `reconnecting`. `useAgentSession.onStatus(false)` answers it +// with `abortActiveTurn()`, which settles the assistant message locally, while +// `onStatus(true)` only sets a flag — nothing re-attaches. The server never saw +// the socket go away, so its assistant row is still `streaming` and it keeps +// answering posts with 409. These specs close one socket and hold the client to +// the three things the user watched break. +// +// Deliberately not a refresh or a tab reopen: PM-1154 and PM-1043 cover those +// triggers. Here the page is never reloaded — only the socket blips. +test.describe.configure({ timeout: 120_000 }) +test.use({ connectWebSocketToServer: false }) + +test.describe( + 'Agent turn across a transient websocket reconnect', + { tag: ['@cloud', '@ui'] }, + () => { + const PROMPT = 'add an audio output node' + + test.beforeEach(async ({ turnLock }) => { + await turnLock.openOnBlankWorkflow() + await turnLock.startTurn(PROMPT) + }) + + test('keeps rendering the turn after the socket reconnects', async ({ + turnLock + }) => { + const reconnected = await turnLock.dropSocket() + + // The thread itself must survive the blip. A wipe here would be a + // different defect, and test.fail() below would swallow it. (The agent's + // narration is deliberately not asserted: settling the turn collapses it + // into the closed "Worked for ..." summary, which is the very thing the + // next test pins.) + await expect(turnLock.userBubbles).toHaveText([PROMPT]) + + // The server never stopped running this turn, so it keeps broadcasting the + // same message_id down the new socket. `agentConversationStore.ingest` + // drops every one of them: `transport` is null after the abort, and + // `dropBackgroundTurns()` emptied the map it would otherwise fall back to. + test.fail() + turnLock.push(reconnected, POST_RECONNECT_EVENT) + await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() + }) + + test('keeps the turn marked as running after the socket reconnects', async ({ + turnLock + }) => { + await expect(turnLock.stopButton).toBeVisible() + await expect(turnLock.workSummary).toHaveCount(0) + + await turnLock.dropSocket() + + // `abortActiveTurn()` calls `transport.settle()`, which flips + // `message.streaming` to false. AgentMessage.vue swaps ActivityTrace for + // WorkSummary on exactly that flag, so the live rows collapse into the + // "Worked for ..." button the user saw, the Working... row disappears and + // Stop reverts to Send — while the server still owns the turn. + test.fail() + await expect(turnLock.stopButton).toBeVisible() + await expect(turnLock.workSummary).toHaveCount(0) + await expect(turnLock.workingRow).toBeVisible() + }) + + test('does not reject the next message after the socket reconnects', async ({ + turnLock + }) => { + await turnLock.dropSocket() + + // The abandoned turn puts Send back in front of the user, so the nudge + // reaches a thread the server still has locked and comes back 409 + // TURN_IN_PROGRESS. Once the client re-attaches, the composer will offer + // Stop instead and this test should be rewritten around that; today it + // pins the dead end the report ends on. + test.fail() + await turnLock.composer.fill('are you still there?') + await turnLock.sendButton.click() + await expect(turnLock.panel.getByRole('alert')).toHaveCount(0) + expect(turnLock.rejectedPosts()).toBe(0) + }) + + // The report mentions audio playback right before the disconnect, so this is + // the control. It runs the same Web Audio work the audio-output widget and + // the asset-library preview do (`useWaveAudioPlayer.decodeAudioSource` + // builds an AudioContext, decodes into it, then closes it) while the turn is + // live. This one passes today: the three above need no audio at all, which + // is what makes the playback incidental rather than causal. + test('keeps the turn running while audio plays in the page', async ({ + turnLock, + getWebSocket + }) => { + await turnLock.panel.evaluate(async () => { + const context = new AudioContext() + const buffer = context.createBuffer( + 1, + context.sampleRate / 10, + context.sampleRate + ) + const source = context.createBufferSource() + source.buffer = buffer + source.connect(context.destination) + source.start() + await context.close() + }) + + await expect(turnLock.stopButton).toBeVisible() + await expect(turnLock.workSummary).toHaveCount(0) + + turnLock.push(await getWebSocket(), POST_RECONNECT_EVENT) + await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() + await expect( + turnLock.panel.getByText(enMessages.agent.sendFailed) + ).toHaveCount(0) + }) + } +) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 0c9a3ce53cb..c26387e7a6e 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -872,6 +872,53 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.isStreaming.value).toBe(true) }) + // PM-1199 / PM-1200. (g) above pins today's behaviour: a live->down->live + // socket blip settles the turn and the session does nothing on the way back + // up. The server never learned the socket went away, so it keeps running the + // turn and keeps broadcasting the same message_id. These two say what the + // user needs instead, and are the lowest level that proves it — the browser + // spec `agentTurnSurvivesSocketDrop.spec.ts` covers the same defect through + // the real socket and the rendered panel. + it.fails('(g3) KNOWN BUG: a reconnect leaves the turn running instead of settling it', async () => { + const rest = fakeRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + expect(session.isStreaming.value).toBe(true) + + status(false) + status(true) + + expect(session.isStreaming.value).toBe(true) + }) + + it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { + const rest = fakeRest() + const { source, emit, status } = fakeEvents() + const session = useAgentSession({ rest, events: source }) + session.start() + status(true) + + await session.sendMessage('go') + emit(delta('msg-1', 'partial')) + + status(false) + status(true) + emit(delta('msg-1', ' and the rest')) + + expect(session.entries.value).toMatchObject([ + { role: 'user', text: 'go' }, + { + role: 'assistant', + parts: [{ type: 'text', text: 'partial and the rest' }] + } + ]) + }) + it('(h) attachments pass through to the postMessage wire body', async () => { const rest = fakeRest() const { source } = fakeEvents() From 0c909a7da0151dcb5b2dfcac8bec6fb18bf78e64 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 02:58:17 +0000 Subject: [PATCH 2/9] test(agent): stabilise the socket-drop repro and tighten its assertions Review follow-ups: - startTurn() waited on Stop, which Composer renders from isSending before the POST even leaves. Frames pushed in that window are dropped by agentConversationStore.ingest with no retry, so the suite went red ~half the time inside beforeEach, where test.fail() cannot apply. Gate on the user bubble instead: recordUser() runs immediately before startTurn(). - The 409 case read route state straight after click(), so a lost race could turn both assertions green and flip the expected failure. Poll a post counter first. - Move test.fail() below the recoverable preconditions in that same case. - The audio control now runs real decodeAudioData over WAV bytes, matching useWaveAudioPlayer.decodeAudioSource; drop an assertion that could never fire. - (g4) asserts the joined reply text so a re-attach that opens a fresh text part still counts as fixed. - Compose AgentPanel and TestIds rather than re-deriving them. 6/6 clean runs of the spec, and the three failures still land on their documented assertions. --- .../fixtures/agentTurnLockFixture.ts | 82 ++++++++++++++++--- .../agent/agentTurnSurvivesSocketDrop.spec.ts | 38 ++++----- .../composables/agent/useAgentSession.test.ts | 15 ++-- 3 files changed, 95 insertions(+), 40 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index a5c30212d24..f2865e869ce 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -14,6 +14,8 @@ import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApi import { agentTest } from '@e2e/fixtures/agentPanelFixture' import { workflowSelectionTest } from '@e2e/fixtures/agentWorkflowSelectionFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { TestIds } from '@e2e/fixtures/selectors' import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' import { webSocketFixture } from '@e2e/fixtures/ws' @@ -69,6 +71,7 @@ class TurnLockServer { private streaming = false private prompt = '' private rejected = 0 + private posts = 0 get turnIsStreaming(): boolean { return this.streaming @@ -78,6 +81,15 @@ class TurnLockServer { return this.rejected } + /** Every post the server answered, accepted or rejected. */ + get postAttempts(): number { + return this.posts + } + + countPost(): void { + this.posts++ + } + completeTurn(): void { this.streaming = false } @@ -125,6 +137,7 @@ async function routeTurnLock( await page.route('**/api/agent/threads/*/messages', (route) => { if (route.request().method() === 'GET') return route.fulfill(jsonRoute(server.transcript())) + server.countPost() if (server.turnIsStreaming) return route.fulfill({ ...jsonRoute(server.rejectPost()), status: 409 }) const request = zAgentPostMessageRequest.parse( @@ -151,6 +164,7 @@ export class AgentTurnLockHarness { public readonly workSummary: Locator public readonly workingRow: Locator public readonly userBubbles: Locator + private readonly agentPanel: AgentPanel constructor( private readonly page: Page, @@ -160,7 +174,8 @@ export class AgentTurnLockHarness { private readonly getWebSocket: () => Promise, private readonly nextWebSocket: () => Promise ) { - this.panel = page.locator('#agent-panel-root') + this.agentPanel = new AgentPanel(page) + this.panel = this.agentPanel.root this.composer = this.panel.getByRole('textbox') this.sendButton = this.panel.getByRole('button', { name: enMessages.agent.send, @@ -181,15 +196,16 @@ export class AgentTurnLockHarness { return this.server.rejectedPosts } + postAttempts(): number { + return this.server.postAttempts + } + /** Opens the panel on a blank workflow and points the composer at that tab. */ async openOnBlankWorkflow(): Promise { await expect( - this.page.getByTestId('integrated-tab-bar-actions') + this.page.getByTestId(TestIds.topbar.integratedTabBarActions) ).toHaveAttribute('data-agent-gate-settled', 'true', { timeout: 15_000 }) - await this.page - .getByRole('button', { name: enMessages.agent.askComfyAgent }) - .click() - await expect(this.panel).toBeVisible() + await this.agentPanel.open() await this.page .getByRole('button', { name: enMessages.sideToolbar.newBlankWorkflow, @@ -197,9 +213,10 @@ export class AgentTurnLockHarness { }) .click() await expect(this.panel).toBeVisible() - await this.panel - .getByRole('button', { name: enMessages.agent.switchWorkflow }) - .click() + // Not AgentPanel.selectWorkflow(): it asserts the picker label straight + // after the menu click, but under this fixture the label only settles once + // the pending workflow save is released below. + await this.agentPanel.workflowPicker.click() await this.page .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) .click() @@ -212,7 +229,13 @@ export class AgentTurnLockHarness { const live = await this.getWebSocket() await this.composer.fill(prompt) await this.sendButton.click() - await expect(this.stopButton).toBeVisible() + // Stop is NOT an ack: Composer renders it from `isSending`, which + // useAgentSession sets before the POST (and before a prepare() race worth + // up to PREPARE_TIMEOUT_MS). The user bubble comes from recordUser(), one + // line above startTurn(), so it is the first signal that activeTurnId is + // set. Pushing a frame before that point gets it silently dropped by + // agentConversationStore.ingest, with no retry. + await expect(this.userBubbles).toHaveText([prompt]) this.push(live, TURN_THINKING_EVENT) await expect(this.panel.getByText(TURN_THINKING_TEXT)).toBeVisible() this.push(live, TURN_TOOL_EVENT) @@ -223,6 +246,45 @@ export class AgentTurnLockHarness { ws.send(JSON.stringify(event)) } + /** + * Mirrors `useWaveAudioPlayer.decodeAudioSource`: open an AudioContext, run + * real `decodeAudioData` over WAV bytes, play them, then close the context. + * The product's `api.fetchApi` step is deliberately left out — network + * isolation fails a test on any unmocked request — so the bytes are built in + * the page instead of downloaded. + */ + async playDecodedAudio(): Promise { + await this.page.evaluate(async () => { + const frames = 800 + const bytes = new ArrayBuffer(44 + frames * 2) + const view = new DataView(bytes) + const ascii = (offset: number, text: string) => { + for (let i = 0; i < text.length; i++) + view.setUint8(offset + i, text.charCodeAt(i)) + } + ascii(0, 'RIFF') + view.setUint32(4, 36 + frames * 2, true) + ascii(8, 'WAVEfmt ') + view.setUint32(16, 16, true) + view.setUint16(20, 1, true) + view.setUint16(22, 1, true) + view.setUint32(24, 8000, true) + view.setUint32(28, 16000, true) + view.setUint16(32, 2, true) + view.setUint16(34, 16, true) + ascii(36, 'data') + view.setUint32(40, frames * 2, true) + + const context = new AudioContext() + const decoded = await context.decodeAudioData(bytes) + const source = context.createBufferSource() + source.buffer = decoded + source.connect(context.destination) + source.start() + await context.close() + }) + } + /** Drops the live socket and resolves with the one the client reconnects on. */ async dropSocket(): Promise { const reconnected = this.nextWebSocket() diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index 85649671526..e296ed2abd0 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -1,7 +1,5 @@ import { expect } from '@playwright/test' -import enMessages from '@/locales/en/main.json' with { type: 'json' } - import { POST_RECONNECT_EVENT, POST_RECONNECT_TEXT, @@ -83,6 +81,13 @@ test.describe( }) => { await turnLock.dropSocket() + // Preconditions stay above the marker so a broken composer or a lost + // prompt reads as a real failure rather than the expected one. Send + // visibility is deliberately not asserted here: Send replacing Stop is + // itself part of the defect. + await expect(turnLock.composer).toBeVisible() + await expect(turnLock.userBubbles).toHaveText([PROMPT]) + // The abandoned turn puts Send back in front of the user, so the nudge // reaches a thread the server still has locked and comes back 409 // TURN_IN_PROGRESS. Once the client re-attaches, the composer will offer @@ -91,42 +96,29 @@ test.describe( test.fail() await turnLock.composer.fill('are you still there?') await turnLock.sendButton.click() + // Wait for the nudge to actually reach the server, or both assertions + // below could pass on a race and flip this expected failure green. + await expect.poll(() => turnLock.postAttempts()).toBe(2) await expect(turnLock.panel.getByRole('alert')).toHaveCount(0) expect(turnLock.rejectedPosts()).toBe(0) }) // The report mentions audio playback right before the disconnect, so this is - // the control. It runs the same Web Audio work the audio-output widget and - // the asset-library preview do (`useWaveAudioPlayer.decodeAudioSource` - // builds an AudioContext, decodes into it, then closes it) while the turn is - // live. This one passes today: the three above need no audio at all, which - // is what makes the playback incidental rather than causal. + // the control: `playDecodedAudio` runs the AudioContext + decodeAudioData + + // close sequence of `useWaveAudioPlayer.decodeAudioSource` while the turn is + // live. It passes today, and the three above need no audio at all — together + // that is what makes the playback incidental rather than causal. test('keeps the turn running while audio plays in the page', async ({ turnLock, getWebSocket }) => { - await turnLock.panel.evaluate(async () => { - const context = new AudioContext() - const buffer = context.createBuffer( - 1, - context.sampleRate / 10, - context.sampleRate - ) - const source = context.createBufferSource() - source.buffer = buffer - source.connect(context.destination) - source.start() - await context.close() - }) + await turnLock.playDecodedAudio() await expect(turnLock.stopButton).toBeVisible() await expect(turnLock.workSummary).toHaveCount(0) turnLock.push(await getWebSocket(), POST_RECONNECT_EVENT) await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() - await expect( - turnLock.panel.getByText(enMessages.agent.sendFailed) - ).toHaveCount(0) }) } ) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index c26387e7a6e..2b4b4711509 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -910,13 +910,14 @@ describe('useAgentSession (v1 composition root)', () => { status(true) emit(delta('msg-1', ' and the rest')) - expect(session.entries.value).toMatchObject([ - { role: 'user', text: 'go' }, - { - role: 'assistant', - parts: [{ type: 'text', text: 'partial and the rest' }] - } - ]) + // Joined rather than part-by-part: a re-attach that opens a fresh text part + // on reconnect still shows the user the whole reply, and must count as + // fixed. + const assistant = session.entries.value.at(-1) + const replyText = (assistant && 'parts' in assistant ? assistant.parts : []) + .flatMap((part) => (part.type === 'text' ? [part.text] : [])) + .join('') + expect(replyText).toBe('partial and the rest') }) it('(h) attachments pass through to the postMessage wire body', async () => { From 4c8ea87470d2cd145413696b4903beee0dc07014 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 03:34:18 +0000 Subject: [PATCH 3/9] test(agent): close the remaining review notes on the socket-drop repro - Derive the "Worked for ..." locator from the message catalog. It is used in three negative assertions, and the hardcoded regex missed the bare `agent.worked` branch WorkSummary renders at zero elapsed, so a copy or duration change could have made all three pass vacuously. - Give that locator its one positive assertion: the audio control now ends its turn and expects the summary to appear. - Bound the nudge click. Once the client re-attaches Send is gone, and an unbounded click would burn the 120s suite timeout; test.fail() rejects a timeout, so it stays loud either way. - Poll post attempts with an inequality so a future client-side retry cannot fail that line in place of the alert assertion. - decodeAudioLikeThePlayer now matches decodeAudioSource exactly: decode and close, no buffer source the product never creates. - Await the workflow picker after releasing the save it depends on. --- .../fixtures/agentTurnLockFixture.ts | 33 +++++++++----- .../agent/agentTurnSurvivesSocketDrop.spec.ts | 44 ++++++++++++------- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index f2865e869ce..906cc9909dd 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -61,6 +61,11 @@ export const POST_RECONNECT_EVENT: AgentWsEvent = { } } +export const TURN_DONE_EVENT: AgentWsEvent = { + type: 'agent_message_done', + data: { message_id: TURN_ID, thread_id: THREAD_ID } +} + /** * The server's half of a turn, modelled on the real single-active-turn guard: * an assistant row goes `streaming` when a turn starts and only leaves that @@ -185,7 +190,14 @@ export class AgentTurnLockHarness { name: enMessages.agent.stop, exact: true }) - this.workSummary = this.panel.getByRole('button', { name: /^Worked for / }) + // WorkSummary.vue renders three labels off the elapsed total: `worked` + // alone, `workedForSeconds`, or `workedForMinutes`. Anchoring on the + // shared `worked` stem matches all three, so the negative assertions on + // this locator cannot go vacuous when a turn is shorter or longer than the + // fixture's tool duration, or when the copy is reworded. + this.workSummary = this.panel.getByRole('button', { + name: new RegExp(`^${enMessages.agent.worked}`) + }) this.workingRow = this.panel.getByText(enMessages.agent.working, { exact: true }) @@ -222,6 +234,7 @@ export class AgentTurnLockHarness { .click() await expect.poll(() => this.savedPaths()).toBe(1) this.finishSave(true) + await expect(this.agentPanel.workflowPicker).toHaveText('Unsaved Workflow') } /** Sends a prompt and streams it to the point where a user sees work happening. */ @@ -247,13 +260,13 @@ export class AgentTurnLockHarness { } /** - * Mirrors `useWaveAudioPlayer.decodeAudioSource`: open an AudioContext, run - * real `decodeAudioData` over WAV bytes, play them, then close the context. - * The product's `api.fetchApi` step is deliberately left out — network - * isolation fails a test on any unmocked request — so the bytes are built in - * the page instead of downloaded. + * Runs the Web Audio half of `useWaveAudioPlayer.decodeAudioSource`: open an + * AudioContext, `decodeAudioData` real WAV bytes, then close the context. + * The product reaches those bytes through `api.fetchApi`, which is left out + * because network isolation fails a test on any unmocked request, so they are + * built in the page instead of downloaded. */ - async playDecodedAudio(): Promise { + async decodeAudioLikeThePlayer(): Promise { await this.page.evaluate(async () => { const frames = 800 const bytes = new ArrayBuffer(44 + frames * 2) @@ -276,11 +289,7 @@ export class AgentTurnLockHarness { view.setUint32(40, frames * 2, true) const context = new AudioContext() - const decoded = await context.decodeAudioData(bytes) - const source = context.createBufferSource() - source.buffer = decoded - source.connect(context.destination) - source.start() + await context.decodeAudioData(bytes) await context.close() }) } diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index e296ed2abd0..f396d366c19 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -3,6 +3,7 @@ import { expect } from '@playwright/test' import { POST_RECONNECT_EVENT, POST_RECONNECT_TEXT, + TURN_DONE_EVENT, agentTurnLockTest as test } from '@e2e/fixtures/agentTurnLockFixture' @@ -90,35 +91,48 @@ test.describe( // The abandoned turn puts Send back in front of the user, so the nudge // reaches a thread the server still has locked and comes back 409 - // TURN_IN_PROGRESS. Once the client re-attaches, the composer will offer - // Stop instead and this test should be rewritten around that; today it - // pins the dead end the report ends on. + // TURN_IN_PROGRESS. Once the client re-attaches Send is gone, and the + // bounded click below then times out in seconds — loudly red, since + // test.fail() accepts a failed assertion but not a timeout — rather than + // burning the suite timeout. Whoever lands the fix should rewrite this + // around the Stop state. test.fail() await turnLock.composer.fill('are you still there?') - await turnLock.sendButton.click() - // Wait for the nudge to actually reach the server, or both assertions - // below could pass on a race and flip this expected failure green. - await expect.poll(() => turnLock.postAttempts()).toBe(2) + await turnLock.sendButton.click({ timeout: 5_000 }) + // Wait for the nudge to actually reach the server, or the assertion below + // could pass on a race and flip this expected failure green. Inequality, + // so a future client-side retry cannot fail this line instead. + await expect.poll(() => turnLock.postAttempts()).toBeGreaterThanOrEqual(2) await expect(turnLock.panel.getByRole('alert')).toHaveCount(0) expect(turnLock.rejectedPosts()).toBe(0) }) - // The report mentions audio playback right before the disconnect, so this is - // the control: `playDecodedAudio` runs the AudioContext + decodeAudioData + - // close sequence of `useWaveAudioPlayer.decodeAudioSource` while the turn is - // live. It passes today, and the three above need no audio at all — together - // that is what makes the playback incidental rather than causal. - test('keeps the turn running while audio plays in the page', async ({ + // The report mentions audio playback right before the disconnect, so this + // is the control: the audio-output widget and the asset-library preview + // both decode through `useWaveAudioPlayer.decodeAudioSource`, and running + // that decode mid-turn leaves the turn alone. It passes today, and the + // three above need no audio at all — together that is what makes the + // playback incidental rather than causal. + // + // It also carries the only positive assertion on `workSummary`: a turn that + // ends normally must produce the summary, so the negative assertions above + // cannot be passing on a locator that matches nothing. + test('keeps the turn running while an audio preview decodes', async ({ turnLock, getWebSocket }) => { - await turnLock.playDecodedAudio() + const ws = await getWebSocket() + await turnLock.decodeAudioLikeThePlayer() await expect(turnLock.stopButton).toBeVisible() await expect(turnLock.workSummary).toHaveCount(0) - turnLock.push(await getWebSocket(), POST_RECONNECT_EVENT) + turnLock.push(ws, POST_RECONNECT_EVENT) await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() + + turnLock.push(ws, TURN_DONE_EVENT) + await expect(turnLock.workSummary).toBeVisible() + await expect(turnLock.sendButton).toBeVisible() }) } ) From dd87cfd21aa6404f4eda7744857bcc875af54744 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 04:04:14 +0000 Subject: [PATCH 4/9] test(agent): make the 409 repro able to report red, and drop two overclaims MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The bounded click I added last round was backwards. Playwright maps an error to `timedOut` only for TimeoutManagerError (workerProcessEntry.js: `root instanceof TimeoutManagerError`), so a locator-level TimeoutError becomes `failed` — exactly what test.fail() accepts. Once the client re-attaches and Send disappears, that test would have stayed green forever. Verified both halves with a throwaway spec: an act failing above the marker reports red, a bounded click failing below it is swallowed. The nudge now runs unbounded and above test.fail(), so any failure in the act is unexpected regardless of how Playwright classifies it. Also: - (g3)/(g4) now answer getMessages with a `streaming` transcript, so a fix that re-hydrates on reconnect satisfies them as well as one that re-attaches the local transport. - Narrow with assert() instead of a ternary, per the testing guidance. - The audio control no longer claims to clear the product's player; it covers Web Audio decoding beside a live turn and says so. The three socket specs are what show the playback was incidental. - Drop a no-op `void workflowSelection`. --- .../fixtures/agentTurnLockFixture.ts | 15 ++++--- .../agent/agentTurnSurvivesSocketDrop.spec.ts | 39 ++++++++++--------- .../composables/agent/useAgentSession.test.ts | 29 +++++++++++--- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 906cc9909dd..a066c3c9c5a 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -260,13 +260,14 @@ export class AgentTurnLockHarness { } /** - * Runs the Web Audio half of `useWaveAudioPlayer.decodeAudioSource`: open an - * AudioContext, `decodeAudioData` real WAV bytes, then close the context. - * The product reaches those bytes through `api.fetchApi`, which is left out - * because network isolation fails a test on any unmocked request, so they are - * built in the page instead of downloaded. + * Replays the Web Audio work an audio preview does while a turn is live: + * open an AudioContext, `decodeAudioData` real WAV bytes, close it. This is + * the shape of `useWaveAudioPlayer.decodeAudioSource`, not a call into it — + * the product's `api.fetchApi` step and its waveform pass are not exercised, + * so this shows Web Audio alone is harmless rather than clearing the whole + * player. */ - async decodeAudioLikeThePlayer(): Promise { + async decodeAudioLikeAPreview(): Promise { await this.page.evaluate(async () => { const frames = 800 const bytes = new ArrayBuffer(44 + frames * 2) @@ -312,8 +313,6 @@ export const agentTurnLockTest = mergeTests( { page, workflowSelection, getWebSocket, nextWebSocket }, use ) => { - // Workflow selection boots the app before these agent-specific routes. - void workflowSelection const server = new TurnLockServer() await routeTurnLock(page, server) await use( diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index f396d366c19..6789ee7d8a6 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -91,38 +91,41 @@ test.describe( // The abandoned turn puts Send back in front of the user, so the nudge // reaches a thread the server still has locked and comes back 409 - // TURN_IN_PROGRESS. Once the client re-attaches Send is gone, and the - // bounded click below then times out in seconds — loudly red, since - // test.fail() accepts a failed assertion but not a timeout — rather than - // burning the suite timeout. Whoever lands the fix should rewrite this - // around the Stop state. - test.fail() + // TURN_IN_PROGRESS. + // + // The nudge stays ABOVE test.fail() on purpose. Once the client + // re-attaches there is no Send button, and this unbounded click runs out + // the describe timeout, which Playwright reports as `timedOut` — the one + // status test.fail() does not accept, so the fix surfaces loudly. Giving + // the click its own `timeout` would raise an ordinary TimeoutError, which + // maps to `failed` and would be swallowed as the expected failure + // (workerProcessEntry.js: `root instanceof TimeoutManagerError`). await turnLock.composer.fill('are you still there?') - await turnLock.sendButton.click({ timeout: 5_000 }) - // Wait for the nudge to actually reach the server, or the assertion below - // could pass on a race and flip this expected failure green. Inequality, - // so a future client-side retry cannot fail this line instead. + await turnLock.sendButton.click() + // Inequality, so a future client-side retry cannot fail this line in + // place of the alert assertion below. await expect.poll(() => turnLock.postAttempts()).toBeGreaterThanOrEqual(2) + + test.fail() await expect(turnLock.panel.getByRole('alert')).toHaveCount(0) expect(turnLock.rejectedPosts()).toBe(0) }) - // The report mentions audio playback right before the disconnect, so this - // is the control: the audio-output widget and the asset-library preview - // both decode through `useWaveAudioPlayer.decodeAudioSource`, and running - // that decode mid-turn leaves the turn alone. It passes today, and the - // three above need no audio at all — together that is what makes the - // playback incidental rather than causal. + // The report mentions audio playback right before the disconnect. The + // evidence that it was incidental is really the three specs above, which + // reproduce all of it with no audio anywhere; this adds only that Web Audio + // decoding alongside a live turn does not disturb it. It does not run the + // product's player, so it does not clear the whole audio path. // // It also carries the only positive assertion on `workSummary`: a turn that // ends normally must produce the summary, so the negative assertions above // cannot be passing on a locator that matches nothing. - test('keeps the turn running while an audio preview decodes', async ({ + test('keeps the turn running while audio decoding runs alongside it', async ({ turnLock, getWebSocket }) => { const ws = await getWebSocket() - await turnLock.decodeAudioLikeThePlayer() + await turnLock.decodeAudioLikeAPreview() await expect(turnLock.stopButton).toBeVisible() await expect(turnLock.workSummary).toHaveCount(0) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 2b4b4711509..aa7e4138994 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -1,5 +1,5 @@ import type { AgentAdmissionError } from '@comfyorg/ingest-types' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { assert, beforeEach, describe, expect, it, vi } from 'vitest' import { nextTick } from 'vue' import { reportError } from '@/platform/telemetry/reportError' @@ -879,8 +879,26 @@ describe('useAgentSession (v1 composition root)', () => { // user needs instead, and are the lowest level that proves it — the browser // spec `agentTurnSurvivesSocketDrop.spec.ts` covers the same defect through // the real socket and the rendered panel. + // The server still has this turn open, so its history says `streaming`. That + // matters for what counts as fixed: re-attaching the local transport and + // re-hydrating from the server are both plausible repairs, and a double that + // answered with an empty transcript would keep these red after the second one + // shipped. + const streamingTurnRest = () => + fakeRest({ + getMessages: vi.fn( + async (): Promise => [ + historyRow(1, 'user', 'msg-1', 'go'), + { + ...historyRow(2, 'assistant', 'msg-1', 'partial', 'msg-1'), + status: 'streaming' + } + ] + ) + }) + it.fails('(g3) KNOWN BUG: a reconnect leaves the turn running instead of settling it', async () => { - const rest = fakeRest() + const rest = streamingTurnRest() const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start() @@ -893,11 +911,11 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) - expect(session.isStreaming.value).toBe(true) + await vi.waitFor(() => expect(session.isStreaming.value).toBe(true)) }) it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { - const rest = fakeRest() + const rest = streamingTurnRest() const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start() @@ -914,7 +932,8 @@ describe('useAgentSession (v1 composition root)', () => { // on reconnect still shows the user the whole reply, and must count as // fixed. const assistant = session.entries.value.at(-1) - const replyText = (assistant && 'parts' in assistant ? assistant.parts : []) + assert(assistant !== undefined && 'parts' in assistant) + const replyText = assistant.parts .flatMap((part) => (part.type === 'text' ? [part.text] : [])) .join('') expect(replyText).toBe('partial and the rest') From e3814b671892520c172c4332b7496b2e15a7b69b Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 04:36:47 +0000 Subject: [PATCH 5/9] test(agent): address the last review notes - (g4) now seeds the server transcript with the full reply, so a repair that re-hydrates on reconnect satisfies it as well as one that appends the missed delta to a re-attached transport. Seeding only 'partial' meant no amount of waiting could have turned it green. - Bound the nudge click after all. It sits above test.fail(), which only sets the expected status once it executes, so a throw there is already unexpected; the bound just names the missing button in seconds instead of running out the file timeout under CI retries. - Split the positive work-summary assertion into its own test. It was riding along inside the audio control, which made that test cover two behaviours and tied a load-bearing locator check to an unrelated one. - Say in the fixture docstring that the transcript and cancel routes are deliberately unexercised rather than leaving a reader to wonder. --- .../fixtures/agentTurnLockFixture.ts | 5 +++ .../agent/agentTurnSurvivesSocketDrop.spec.ts | 45 ++++++++++--------- .../composables/agent/useAgentSession.test.ts | 21 +++++---- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index a066c3c9c5a..ee89930e124 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -71,6 +71,11 @@ export const TURN_DONE_EVENT: AgentWsEvent = { * an assistant row goes `streaming` when a turn starts and only leaves that * state when the turn completes, fails, or is cancelled. Dropping the client's * socket does not touch it — that asymmetry is what these specs exercise. + * + * `transcript()` and the cancel route are not reached by the current specs. + * They are here so the fake stays a faithful server: a repair that re-hydrates + * on reconnect, or a spec that clicks Stop, needs both, and a half-modelled + * server would make such a fix look broken. */ class TurnLockServer { private streaming = false diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index 6789ee7d8a6..b9bf5e3688b 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -93,15 +93,13 @@ test.describe( // reaches a thread the server still has locked and comes back 409 // TURN_IN_PROGRESS. // - // The nudge stays ABOVE test.fail() on purpose. Once the client - // re-attaches there is no Send button, and this unbounded click runs out - // the describe timeout, which Playwright reports as `timedOut` — the one - // status test.fail() does not accept, so the fix surfaces loudly. Giving - // the click its own `timeout` would raise an ordinary TimeoutError, which - // maps to `failed` and would be swallowed as the expected failure - // (workerProcessEntry.js: `root instanceof TimeoutManagerError`). + // The nudge stays ABOVE test.fail(): body-level test.fail() only sets the + // expected status when it executes, so a throw up here is still an + // unexpected failure. Once the client re-attaches there is no Send + // button, and this click reports that by name in seconds instead of + // running out the file timeout four times over under CI retries. await turnLock.composer.fill('are you still there?') - await turnLock.sendButton.click() + await turnLock.sendButton.click({ timeout: 10_000 }) // Inequality, so a future client-side retry cannot fail this line in // place of the alert assertion below. await expect.poll(() => turnLock.postAttempts()).toBeGreaterThanOrEqual(2) @@ -111,15 +109,26 @@ test.describe( expect(turnLock.rejectedPosts()).toBe(0) }) - // The report mentions audio playback right before the disconnect. The - // evidence that it was incidental is really the three specs above, which - // reproduce all of it with no audio anywhere; this adds only that Web Audio - // decoding alongside a live turn does not disturb it. It does not run the + // Keeps the `workSummary` locator honest. Every other use of it above is a + // toHaveCount(0), which a locator that matched nothing would satisfy for + // free; this shows it does resolve once a turn ends. + test('summarises a turn that ends normally', async ({ + turnLock, + getWebSocket + }) => { + await expect(turnLock.workSummary).toHaveCount(0) + + turnLock.push(await getWebSocket(), TURN_DONE_EVENT) + + await expect(turnLock.workSummary).toBeVisible() + await expect(turnLock.sendButton).toBeVisible() + }) + + // The report mentions audio playback right before the disconnect. What + // shows it was incidental is really the three specs above, which reproduce + // every symptom with no audio anywhere; this only adds that Web Audio + // decoding beside a live turn leaves it alone. It does not call the // product's player, so it does not clear the whole audio path. - // - // It also carries the only positive assertion on `workSummary`: a turn that - // ends normally must produce the summary, so the negative assertions above - // cannot be passing on a locator that matches nothing. test('keeps the turn running while audio decoding runs alongside it', async ({ turnLock, getWebSocket @@ -132,10 +141,6 @@ test.describe( turnLock.push(ws, POST_RECONNECT_EVENT) await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() - - turnLock.push(ws, TURN_DONE_EVENT) - await expect(turnLock.workSummary).toBeVisible() - await expect(turnLock.sendButton).toBeVisible() }) } ) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index aa7e4138994..552089b4308 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -879,18 +879,18 @@ describe('useAgentSession (v1 composition root)', () => { // user needs instead, and are the lowest level that proves it — the browser // spec `agentTurnSurvivesSocketDrop.spec.ts` covers the same defect through // the real socket and the rendered panel. - // The server still has this turn open, so its history says `streaming`. That - // matters for what counts as fixed: re-attaching the local transport and - // re-hydrating from the server are both plausible repairs, and a double that - // answered with an empty transcript would keep these red after the second one - // shipped. - const streamingTurnRest = () => + // The server still has this turn open, so its history says `streaming` and + // carries what the turn has produced so far. That is what decides which + // repairs count as fixed: re-attaching the local transport and re-hydrating + // from the server are both plausible, and a double answering with an empty + // transcript would keep these red after the second one shipped. + const streamingTurnRest = (reply: string) => fakeRest({ getMessages: vi.fn( async (): Promise => [ historyRow(1, 'user', 'msg-1', 'go'), { - ...historyRow(2, 'assistant', 'msg-1', 'partial', 'msg-1'), + ...historyRow(2, 'assistant', 'msg-1', reply, 'msg-1'), status: 'streaming' } ] @@ -898,7 +898,7 @@ describe('useAgentSession (v1 composition root)', () => { }) it.fails('(g3) KNOWN BUG: a reconnect leaves the turn running instead of settling it', async () => { - const rest = streamingTurnRest() + const rest = streamingTurnRest('partial') const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start() @@ -915,7 +915,10 @@ describe('useAgentSession (v1 composition root)', () => { }) it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { - const rest = streamingTurnRest() + // The server already holds the whole reply, so a re-hydrate repair recovers + // it without the delta, and a transport re-attach appends the delta to the + // 'partial' it already had. Both land on the same text. + const rest = streamingTurnRest('partial and the rest') const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start() From 36409adc07b9a1dc990bdcdecc67e26fa13f49a2 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 05:08:48 +0000 Subject: [PATCH 6/9] test(agent): final review cleanups - (g4) retries its read, so a repair that reaches the reply through an async getMessages turns it green instead of sitting quietly red. - The 409 case asserts the alert carries the reported message rather than just that some alert exists. - dropSocket resolves the live route before arming the next one; with no socket open both calls would have queued on the same waiter and the close would have killed the route returned as the reconnect. - Escape the locale string interpolated into the work-summary regex. --- .../fixtures/agentTurnLockFixture.ts | 16 +++++++---- .../agent/agentTurnSurvivesSocketDrop.spec.ts | 7 ++++- .../composables/agent/useAgentSession.test.ts | 27 ++++++++++++------- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index ee89930e124..e11f9f66f27 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -28,9 +28,10 @@ const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-000000000001' * to a thread whose assistant row is still `streaming` with HTTP 409 and this * body. The client renders it as `agent.sendFailed` + ': ' + this text. */ -const TURN_IN_PROGRESS: AgentError = { - error: 'a turn is already in progress for this thread' -} +export const TURN_IN_PROGRESS_MESSAGE = + 'a turn is already in progress for this thread' + +const TURN_IN_PROGRESS: AgentError = { error: TURN_IN_PROGRESS_MESSAGE } const TURN_THINKING_TEXT = 'Wiring the audio output node.' export const POST_RECONNECT_TEXT = 'Reconnected, and the graph is ready.' @@ -201,7 +202,9 @@ export class AgentTurnLockHarness { // this locator cannot go vacuous when a turn is shorter or longer than the // fixture's tool duration, or when the copy is reworded. this.workSummary = this.panel.getByRole('button', { - name: new RegExp(`^${enMessages.agent.worked}`) + name: new RegExp( + `^${enMessages.agent.worked.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}` + ) }) this.workingRow = this.panel.getByText(enMessages.agent.working, { exact: true @@ -302,8 +305,11 @@ export class AgentTurnLockHarness { /** Drops the live socket and resolves with the one the client reconnects on. */ async dropSocket(): Promise { - const reconnected = this.nextWebSocket() + // Resolve the live route first: with no socket open yet both calls would + // queue on the same waiter and hand back the same route, so the close + // below would kill the one returned as the reconnect. const live = await this.getWebSocket() + const reconnected = this.nextWebSocket() await live.close() return reconnected } diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index b9bf5e3688b..8dd0e49fa64 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -4,6 +4,7 @@ import { POST_RECONNECT_EVENT, POST_RECONNECT_TEXT, TURN_DONE_EVENT, + TURN_IN_PROGRESS_MESSAGE, agentTurnLockTest as test } from '@e2e/fixtures/agentTurnLockFixture' @@ -105,7 +106,11 @@ test.describe( await expect.poll(() => turnLock.postAttempts()).toBeGreaterThanOrEqual(2) test.fail() - await expect(turnLock.panel.getByRole('alert')).toHaveCount(0) + await expect( + turnLock.panel + .getByRole('alert') + .filter({ hasText: TURN_IN_PROGRESS_MESSAGE }) + ).toHaveCount(0) expect(turnLock.rejectedPosts()).toBe(0) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 552089b4308..1e4e74826d7 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -917,7 +917,8 @@ describe('useAgentSession (v1 composition root)', () => { it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { // The server already holds the whole reply, so a re-hydrate repair recovers // it without the delta, and a transport re-attach appends the delta to the - // 'partial' it already had. Both land on the same text. + // 'partial' it already had. Both land on the same text, and exact equality + // still rejects a repair that does both and duplicates the suffix. const rest = streamingTurnRest('partial and the rest') const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) @@ -931,15 +932,21 @@ describe('useAgentSession (v1 composition root)', () => { status(true) emit(delta('msg-1', ' and the rest')) - // Joined rather than part-by-part: a re-attach that opens a fresh text part - // on reconnect still shows the user the whole reply, and must count as - // fixed. - const assistant = session.entries.value.at(-1) - assert(assistant !== undefined && 'parts' in assistant) - const replyText = assistant.parts - .flatMap((part) => (part.type === 'text' ? [part.text] : [])) - .join('') - expect(replyText).toBe('partial and the rest') + // Retried, because a re-hydrate repair reaches the reply through an async + // getMessages. Joined rather than part-by-part: a re-attach that opens a + // fresh text part on reconnect still shows the user the whole reply, and + // must count as fixed. + await vi.waitFor( + () => { + const assistant = session.entries.value.at(-1) + assert(assistant !== undefined && 'parts' in assistant) + const replyText = assistant.parts + .flatMap((part) => (part.type === 'text' ? [part.text] : [])) + .join('') + expect(replyText).toBe('partial and the rest') + }, + { timeout: 200 } + ) }) it('(h) attachments pass through to the postMessage wire body', async () => { From 0f5381b93236ace9f53ed8f0028e2119cb237c24 Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 05:27:35 +0000 Subject: [PATCH 7/9] test(agent): give both reconnect pins a 2s budget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Under it.fails a vi.waitFor that runs out its budget throws, and a throw is what marks the case green — so the 200ms budget I put on (g4) last round would have quietly disarmed it against any repair that debounces recovery after the socket flaps. Both pins now wait 2s, and both still exhaust it today. --- .../agent/composables/agent/useAgentSession.test.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 1e4e74826d7..4e40b848cdf 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -911,7 +911,13 @@ describe('useAgentSession (v1 composition root)', () => { status(false) status(true) - await vi.waitFor(() => expect(session.isStreaming.value).toBe(true)) + // Generous on purpose, and matched in (g4). Under it.fails a waitFor that + // runs out its budget THROWS, and a throw is what marks the case green — so + // a tight budget here would quietly disarm the tripwire against any repair + // that debounces recovery after the socket flaps. + await vi.waitFor(() => expect(session.isStreaming.value).toBe(true), { + timeout: 2000 + }) }) it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { @@ -945,7 +951,7 @@ describe('useAgentSession (v1 composition root)', () => { .join('') expect(replyText).toBe('partial and the rest') }, - { timeout: 200 } + { timeout: 2000 } ) }) From 40855274b81f9b4538b7ef00fa88ed3bf645528d Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 05:48:45 +0000 Subject: [PATCH 8/9] test(agent): keep the post-reconnect send above the expected-failure marker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CodeRabbit flagged that test.fail() covers everything after it, so an unrelated throw can read as the expected failure. That was live in the first spec: turnLock.push() is an action, and ws.send() throws on a dead route, so a bad route would have reported as the pinned defect without the assertion ever running. Only assertions sit below the marker now. The other two specs already kept their actions above it, and the unit pins rely on (g)/(g2) — unmarked, same arrange — to catch setup regressions that it.fails would otherwise absorb. Said so explicitly so nobody marks those two later. --- .../tests/agent/agentTurnSurvivesSocketDrop.spec.ts | 13 +++++++++---- .../agent/composables/agent/useAgentSession.test.ts | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts index 8dd0e49fa64..981d75575a6 100644 --- a/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts +++ b/browser_tests/tests/agent/agentTurnSurvivesSocketDrop.spec.ts @@ -51,11 +51,16 @@ test.describe( await expect(turnLock.userBubbles).toHaveText([PROMPT]) // The server never stopped running this turn, so it keeps broadcasting the - // same message_id down the new socket. `agentConversationStore.ingest` - // drops every one of them: `transport` is null after the abort, and - // `dropBackgroundTurns()` emptied the map it would otherwise fall back to. - test.fail() + // same message_id down the new socket. Sending it stays above the marker + // with the rest of the arrange: `ws.send()` throws on a dead route, and + // below test.fail() that throw would read as the expected failure without + // the assertion ever running. turnLock.push(reconnected, POST_RECONNECT_EVENT) + + // `agentConversationStore.ingest` drops every one of those frames: + // `transport` is null after the abort, and `dropBackgroundTurns()` emptied + // the map it would otherwise fall back to. + test.fail() await expect(turnLock.panel.getByText(POST_RECONNECT_TEXT)).toBeVisible() }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 4e40b848cdf..5b5b52f7797 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -879,6 +879,12 @@ describe('useAgentSession (v1 composition root)', () => { // user needs instead, and are the lowest level that proves it — the browser // spec `agentTurnSurvivesSocketDrop.spec.ts` covers the same defect through // the real socket and the rendered panel. + // + // `it.fails` accepts a throw from anywhere in the body, so the arrange these + // two share cannot guard itself. (g) and (g2) directly above are unmarked and + // drive exactly that arrange — start, sendMessage, emit, the isStreaming + // precondition — so a fixture or setup regression reddens there instead of + // being absorbed here. Keep them unmarked for as long as these two exist. // The server still has this turn open, so its history says `streaming` and // carries what the turn has produced so far. That is what decides which // repairs count as fixed: re-attaching the local transport and re-hydrating From 2dc48852c49e0e99ae9b2bdcf47475d17f921cbd Mon Sep 17 00:00:00 2001 From: Glary-Bot Date: Sat, 19 Sep 2026 15:28:01 +0000 Subject: [PATCH 9/9] test(agent): model a live turn as contentless, the way the server stores it The double gave the streaming assistant row persisted text, which the backend never does: cloud's newAssistantMessage creates the row with no content and only the terminal complete/fail writes set it. With text on the row, a repair that re-hydrates mid-turn would have satisfied these pins even though it blanks the reply the user can already see. Contentless now, so the reply has to come from a preserved transport. Both pins still fail on their documented assertions. --- .../composables/agent/useAgentSession.test.ts | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index 5b5b52f7797..bb3660b096d 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -885,18 +885,19 @@ describe('useAgentSession (v1 composition root)', () => { // drive exactly that arrange — start, sendMessage, emit, the isStreaming // precondition — so a fixture or setup regression reddens there instead of // being absorbed here. Keep them unmarked for as long as these two exist. - // The server still has this turn open, so its history says `streaming` and - // carries what the turn has produced so far. That is what decides which - // repairs count as fixed: re-attaching the local transport and re-hydrating - // from the server are both plausible, and a double answering with an empty - // transcript would keep these red after the second one shipped. - const streamingTurnRest = (reply: string) => + // Backend invariant (cloud `newAssistantMessage` + the complete/fail writes): + // an assistant row carries content only once it goes terminal, so a live turn + // has nothing to re-hydrate from. Keep this row contentless — giving it text + // would let a repair that re-hydrates mid-turn satisfy these pins, when that + // repair blanks the reply on screen. + const streamingTurnRest = () => fakeRest({ getMessages: vi.fn( async (): Promise => [ historyRow(1, 'user', 'msg-1', 'go'), { - ...historyRow(2, 'assistant', 'msg-1', reply, 'msg-1'), + ...historyRow(2, 'assistant', 'msg-1', '', 'msg-1'), + content: {}, status: 'streaming' } ] @@ -904,7 +905,7 @@ describe('useAgentSession (v1 composition root)', () => { }) it.fails('(g3) KNOWN BUG: a reconnect leaves the turn running instead of settling it', async () => { - const rest = streamingTurnRest('partial') + const rest = streamingTurnRest() const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start() @@ -927,11 +928,7 @@ describe('useAgentSession (v1 composition root)', () => { }) it.fails('(g4) KNOWN BUG: deltas that arrive after a reconnect still reach the turn', async () => { - // The server already holds the whole reply, so a re-hydrate repair recovers - // it without the delta, and a transport re-attach appends the delta to the - // 'partial' it already had. Both land on the same text, and exact equality - // still rejects a repair that does both and duplicates the suffix. - const rest = streamingTurnRest('partial and the rest') + const rest = streamingTurnRest() const { source, emit, status } = fakeEvents() const session = useAgentSession({ rest, events: source }) session.start()