Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 82 additions & 10 deletions browser_tests/fixtures/agentTurnLockFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
import { z } from 'zod'

import enMessages from '@/locales/en/main.json' with { type: 'json' }
import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema'
import type {
AgentWsEvent,
zTurnInProgressError
} from '@/workbench/extensions/agent/schemas/agentApiSchema'

import { agentTest } from '@e2e/fixtures/agentPanelFixture'
import { workflowSelectionTest } from '@e2e/fixtures/agentWorkflowSelectionFixture'
Expand All @@ -29,20 +32,23 @@ const zAnswerRequest = z.object({ selected: z.array(z.string()) })

const THREAD_ID = 'b9d0a2a1-0f2c-4f1a-9a5e-6b0f4f2c1d77'
const TURN_ID = '2dd4f367-3399-4cb4-8127-547f531c289a'
const FOREIGN_TURN_ID = 'd3f2b1c0-8a4e-4d6f-9b11-0c7a5e2f4318'
const WORKFLOW_ID = 'a81718a4-02ae-41e6-ae85-000000000001'
const BACKGROUND_THREAD_TITLE = 'Audio workflow'
const OTHER_THREAD_ID = '4ccb6603-4bbc-49e2-8b7d-b985230285e3'
const OTHER_THREAD_TITLE = 'Earlier workflow'

/**
* 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.
*/
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 }
type TurnInProgressBody = AgentError & z.infer<typeof zTurnInProgressError>

const TURN_IN_PROGRESS: TurnInProgressBody = {
error: TURN_IN_PROGRESS_MESSAGE,
type: 'TURN_IN_PROGRESS',
active_message_id: TURN_ID,
turn_id: TURN_ID
}

const TURN_THINKING_TEXT = 'Wiring the audio output node.'
export const POST_RECONNECT_TEXT = 'Reconnected, and the graph is ready.'
Expand Down Expand Up @@ -80,6 +86,11 @@ export const TURN_DONE_EVENT: AgentWsEvent = {
data: { message_id: TURN_ID, thread_id: THREAD_ID }
}

const FOREIGN_TURN_DONE_EVENT: AgentWsEvent = {
type: 'agent_message_done',
data: { message_id: FOREIGN_TURN_ID, thread_id: THREAD_ID }
}

const RUN_APPROVAL_ASK_ID = `${TURN_ID}:call-run-workflow`

/**
Expand Down Expand Up @@ -163,6 +174,7 @@ function deferred(): Deferred {
class TurnLockServer {
private streaming = false
private prompt = ''
private foreignPrompt: string | undefined
private rejected = 0
private posts = 0
private readonly answered: string[][] = []
Expand Down Expand Up @@ -215,6 +227,7 @@ class TurnLockServer {

completeTurn(): void {
this.streaming = false
this.foreignPrompt = undefined
}

holdNextTranscript(): void {
Expand Down Expand Up @@ -282,7 +295,7 @@ class TurnLockServer {
content: { text: this.prompt }
}
]
if (!this.streaming) {
if (!this.streaming || this.foreignPrompt !== undefined) {
rows.push(
{
id: TURN_ID,
Expand Down Expand Up @@ -324,6 +337,28 @@ class TurnLockServer {
content: { text: PERSISTED_AFTER_TOOL_TEXT }
}
)
if (this.foreignPrompt !== undefined)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
rows.push(
{
id: 'user-2',
thread_id: THREAD_ID,
turn_id: FOREIGN_TURN_ID,
seq: 5,
role: 'user',
status: 'complete',
workflow_id: WORKFLOW_ID,
content: { text: this.foreignPrompt }
},
{
id: FOREIGN_TURN_ID,
thread_id: THREAD_ID,
turn_id: FOREIGN_TURN_ID,
seq: 6,
role: 'assistant',
status: 'streaming',
workflow_id: WORKFLOW_ID
}
)
return rows
}
rows.push({
Expand All @@ -345,9 +380,20 @@ class TurnLockServer {
return { message_id: TURN_ID, thread_id: THREAD_ID }
}

rejectPost(): AgentError {
lockThreadElsewhere(prompt: string): void {
this.foreignPrompt = prompt
this.streaming = true
}

rejectPost(): TurnInProgressBody {
this.rejected++
return TURN_IN_PROGRESS
return this.foreignPrompt === undefined
? TURN_IN_PROGRESS
: {
...TURN_IN_PROGRESS,
active_message_id: FOREIGN_TURN_ID,
turn_id: FOREIGN_TURN_ID
}
}
}

Expand Down Expand Up @@ -464,6 +510,7 @@ export class AgentTurnLockHarness {
public readonly workingRow: Locator
public readonly liveProgressRow: Locator
public readonly userBubbles: Locator
public readonly turnInProgressNotice: Locator
private readonly entryButton: Locator
private readonly dock: Locator
private readonly agentPanel: AgentPanel
Expand Down Expand Up @@ -516,6 +563,10 @@ export class AgentTurnLockHarness {
.filter({ visible: true })
.first()
this.userBubbles = this.panel.getByTestId('user-message-bubble')
this.turnInProgressNotice = this.panel.getByText(
enMessages.agent.sendTurnInProgress,
{ exact: true }
)
this.entryButton = this.agentPanel.openButton
this.dock = page.getByTestId('docked-agent-panel')
}
Expand Down Expand Up @@ -580,10 +631,31 @@ export class AgentTurnLockHarness {
this.push(ws, TURN_DONE_EVENT)
}

finishForeignTurn(ws: WebSocketRoute): void {
this.server.completeTurn()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
this.push(ws, FOREIGN_TURN_DONE_EVENT)
}

async waitForForeignTurnCancellation(): Promise<void> {
const request = await this.page.waitForRequest(
'**/api/agent/threads/*/messages/*/cancel'
)
const segments = new URL(request.url()).pathname
.split('/')
.map(decodeURIComponent)
expect(request.method()).toBe('POST')
expect(segments[segments.indexOf('threads') + 1]).toBe(THREAD_ID)
expect(segments[segments.indexOf('messages') + 1]).toBe(FOREIGN_TURN_ID)
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
}

finishTurnOnServer(): void {
this.server.completeTurn()
}

lockThreadFromAnotherClient(prompt: string): void {
this.server.lockThreadElsewhere(prompt)
}

/** Makes an ask available through transcript hydration, independently of WS delivery. */
primePendingAsk(event: AgentWsEvent): void {
this.server.recordAsk(event)
Expand Down
53 changes: 53 additions & 0 deletions browser_tests/tests/agent/agentSendRefusedByRunningTurn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from '@playwright/test'

import { agentTurnLockTest as test } from '@e2e/fixtures/agentTurnLockFixture'

test.use({ connectWebSocketToServer: false })
test.describe.configure({ timeout: 120_000 })

test.describe(
'a send refused because another client holds the thread',
{ tag: ['@cloud', '@ui'] },
() => {
test('re-attaches to the running turn and offers Stop instead of a dead end', async ({
turnLock
}) => {
await turnLock.openOnBlankWorkflow()
const live = await turnLock.liveSocket()
await turnLock.startTurn('add an audio output node')
turnLock.finishTurn(live)
await expect(turnLock.sendButton).toBeVisible()

await test.step('another client starts a turn on the same thread', async () => {
turnLock.lockThreadFromAnotherClient('run it again')
await expect(turnLock.sendButton).toBeVisible()
})

await test.step('the refused send re-attaches rather than dead-ending', async () => {
await turnLock.composer.fill('and now add a save node')
await turnLock.sendButton.click()

await expect.poll(() => turnLock.rejectedPosts()).toBe(1)
await expect(turnLock.stopButton).toBeVisible({ timeout: 20_000 })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The cloud project's test timeout is 15s (playwright.config.ts), and this spec has no test.describe.configure (the sibling turn-lock specs set timeout: 120_000 when they need it). A 20s assertion timeout can't take effect. If Stop never shows up, the test fails with a generic test timeout instead of this locator's message. The default retrying assertion is enough here.

Suggested change
await expect(turnLock.stopButton).toBeVisible({ timeout: 20_000 })
await expect(turnLock.stopButton).toBeVisible()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed and the deviation is deliberate, so flagging rather than resolving. playwright.config.ts:172-176 sets the cloud project's timeout: 15000, project timeout beats the top-level 30_000 that PLAYWRIGHT_LOCAL sets, and this spec had no test.describe.configure — so the 20s bound could not take effect locally or on CI.

I took the second option you offered instead of dropping the bounds, because measuring it turned up a second problem: the happy path runs 10.2s locally against that 15s budget, so the spec was already spending two thirds of it on a passing run, with the reattach's own 5s RECONCILE_TIMEOUT_MS inside that. Dropping to the default assertion timeout leaves the budget risk in place; CI's retries: 3 would have hidden it. ba36b61ad5 adds test.describe.configure({ timeout: 120_000 }), matching agentTurnSurvivesPanelMinimize and agentTurnSurvivesSocketDrop, which do comparable multi-turn work. The 20s bound then means what it says and still fails with this locator's message.

Happy to drop both bounds instead if you would rather the spec not carry explicit timeouts — say so and I will.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in ba36b61ad5: the spec now sets test.describe.configure({ timeout: 120_000 }), so the 20s locator timeout fits the test budget. That matches the sibling turn-lock specs.

await expect(turnLock.sendButton).toHaveCount(0)
await expect(turnLock.liveProgressRow).toBeVisible()
await expect(turnLock.panel.getByText('run it again')).toBeVisible()
await expect(turnLock.turnInProgressNotice).toBeVisible()
})

await test.step('stopping it unlocks the composer and the retry is accepted', async () => {
const foreignTurnCancelled = turnLock.waitForForeignTurnCancellation()
await turnLock.stopButton.click()
await foreignTurnCancelled
turnLock.finishForeignTurn(live)

await expect(turnLock.sendButton).toBeVisible({ timeout: 20_000 })
await turnLock.composer.fill('and now add a save node')
await turnLock.sendButton.click()

await expect.poll(() => turnLock.postAttempts()).toBe(3)
expect(turnLock.rejectedPosts()).toBe(1)
})
})
}
)
1 change: 1 addition & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5616,6 +5616,7 @@
"sendFailed": "Message failed to send",
"recoveredTurnFailed": "The agent could not complete this response.",
"sendBusy": "A message is already being sent",
"sendTurnInProgress": "This chat still has a response running. Wait for it to finish, or stop it, then send again.",
"retryAfterSeconds": "You can try again in {seconds}s.",
"malformedEvent": "The agent sent an event this panel could not read",
"coachProgress": "{current} of {total}",
Expand Down
Loading
Loading