diff --git a/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts new file mode 100644 index 00000000000..2859d88e873 --- /dev/null +++ b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts @@ -0,0 +1,452 @@ +import type { Page, WebSocketRoute } from '@playwright/test' +import { expect } from '@playwright/test' + +import { createI18n } from 'vue-i18n' + +import type { WidgetCatalog, WorkflowJSON } from '@comfyorg/comfy-multi-player' +import type { WorkflowListResponse } from '@comfyorg/ingest-types' +import type { UserDataFullInfo } from '@/schemas/apiSchema' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' + +import { + agentTest as test, + bootAgentApp +} from '@e2e/fixtures/agentPanelFixture' +import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' +import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' +import { VueNodeHelpers } from '@e2e/fixtures/VueNodeHelpers' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' + +/** + * Repro for PM-1575 / PM-1576 (stagingcloud, 2026-09): every agent tool call + * that touches the canvas reports done -- the chat trace's spinner settles, + * the turn's "Worked for Xs" summary appears -- roughly 10 real seconds + * before the node it added actually materializes on the canvas. Unlike + * PM-1355/#18179 (`agentCanvasBlanksDuringRunTurn.spec.ts`), this is not a + * `doc_reset`-triggered blank canvas recovering off a client heartbeat: it + * happens on an ORDINARY tool call, with no reset, refusal or disconnect in + * the picture, and it always eventually renders. + * + * What is confirmed, by reading the code (not by telemetry this task does not + * have access to): + * + * `createAgentEventTransport`'s `ingest()` (`agentEventTransport.ts:86-108`) + * flips a tool part's `state` to `'done'` -- the ONLY thing + * `AgentMessage.vue`'s `composing`/`status` computeds (`AgentMessage.vue:92-121`) + * read to decide whether to keep showing the spinner -- the instant an + * `agent_tool_call` frame with `status !== 'running'` arrives. That frame + * travels the chat half of the same websocket. Nothing in that function, or + * anywhere else this task found under + * `src/workbench/extensions/agent/services/agent/` or + * `src/workbench/extensions/agent/components/agent/message/`, reads + * `useAgentCrdtFollower`'s status, the bridge's `lastSequence`, or anything + * else from the CRDT doc half. The two signals are wired to completely + * disjoint listeners on `bridge`/`client` (`useAgentCrdtFollower.ts:593-603`) + * and `AgentEventTransport` (`agentEventTransport.ts`) respectively, and + * neither one waits on the other. So the "done" affordance is structurally + * incapable of reflecting whether the corresponding `doc_update` has landed -- + * confirmed, not hypothesized. + * + * What is NOT confirmed: why the doc-side broadcast specifically takes ~10 s + * to catch up in stagingcloud. That number does not match any client-side + * timer this task found (`STALE_AFTER_MS` is 30 s, `SUBSCRIBE_CATCHUP_GRACE_MS` + * is 2 s, `SUBSCRIBE_ACK_TIMEOUT_MS` is 15 s, `opSender.ts`'s + * `RESULT_TIMEOUT_MS` -- the closest 10 s constant in this codebase -- times + * out the HUMAN write leg's own result wait, not anything on the read/follower + * side an agent-authored update travels). The likely remaining explanation is + * a server-side ("doc host") propagation delay between committing an agent + * op and broadcasting its `doc_update` to subscribed followers, separate from + * the chat completion ack -- but this repo has no visibility into that + * service, so this spec pins the OBSERVABLE client-side gap (completion + * affordance fires; canvas does not reflect it for a long, but bounded, + * interval) rather than claiming a specific backend root cause. + * + * The fix (client-side decoupling only -- the backend propagation delay + * itself is out of scope): `createAgentEventTransport` now takes an optional + * `shouldAwaitCanvasSync` gate. While it returns true, a tool call's `state` + * is held at `'streaming'` when its frame reports done, instead of flipping + * straight to `'done'`, until either `notifyCanvasCaughtUp()` is called or + * `STALE_AFTER_MS` elapses (the same bound `agentCrdtDocLifecycle.ts`'s own + * passive heartbeat uses). `AgentPanelRoot.vue` wires the gate to + * `agentPanelStore.enabled` and calls `notifyCanvasCaughtUp()` whenever + * `useAgentCrdtFollower`'s `outcomes.applied` counter increases for the bound + * workflow -- mirroring, not reusing, PM-1355's own catch-up-confirmation + * primitive, since that one lives inside `AgentCrdtDocLifecycle` and answers + * "is THIS subscribe's own catch-up frame overdue", not "has some later + * update since landed", which is the question this bug needed answered. + */ + +const WORKFLOW_ID = 'c1d2e3f4-5a6b-4c7d-8e9f-0a1b2c3d4e5f' +const THREAD_ID = 'f0e1d2c3-b4a5-4968-8172-6354a3b2c1d0' +const MESSAGE_ID = '1e2d3c4b-5a69-4788-9061-52f3e4d5c6b7' +const ADDED_NODE_ID = 4242 +const NODE_TEXT = 'added while the doc broadcast lags' +const SOCKET_SID = 'a9b8c7d6-e5f4-4321-8a9b-0c1d2e3f4a5b' + +const CATALOG: WidgetCatalog = { + types: { MarkdownNote: { widget_order: ['text'] } } +} +const SEED: WorkflowJSON = { nodes: [], links: [] } + +const OPEN_AGENT_LABEL = enMessages.agent.askComfyAgent +const SEND_LABEL = enMessages.agent.send +const COMPOSER_LABEL = createI18n({ + legacy: false, + locale: 'en', + messages: { en: enMessages } +}).global.t('agent.placeholder') + +/** The follower's `doc_subscribe` payload for the workflow under test, or null. */ +function subscribeStateVectorOf(raw: Buffer | string): string | null { + const frame: unknown = JSON.parse(raw.toString()) + if (typeof frame !== 'object' || frame === null) return null + const { type, data } = frame as { type?: unknown; data?: unknown } + if (type !== 'doc_subscribe' || typeof data !== 'object' || data === null) + return null + const { workflow_id, state_vector_b64 } = data as { + workflow_id?: unknown + state_vector_b64?: unknown + } + if (workflow_id !== WORKFLOW_ID || typeof state_vector_b64 !== 'string') + return null + return state_vector_b64 +} + +/** + * Drives one "add a note to the canvas" turn through to the agent reporting + * it done -- the tool call's own completion AND the turn's "Ran N tool + * calls" summary (cloud/1.54's ToolCallGroup, not main's WorkSummary), + * matching the bug report's "every working affordance" -- while the + * doc host has ALREADY recorded the `add_node` op authoritatively + * (`host.apply` below) but its broadcast `doc_update` is deliberately held + * back. Callers decide when (if ever) to release it via the returned + * `sendDelayedDocUpdate`. + */ +async function driveThroughToolCallDone( + page: Page, + /** + * The healthy-doc-host ordering is the tool call's own terminal frame + * landing before the matching `doc_update` broadcast -- that is what every + * scenario above exercises. 'docUpdateBeforeToolCall' instead releases the + * (already-applied, see `host.apply` below) `doc_update` BEFORE the + * `agent_tool_call success` frame, to exercise the inverted ordering a + * lost-wakeup regression in `notifyCanvasCaughtUp`'s edge-triggering would + * strand at `STALE_AFTER_MS` even though the canvas had already caught up. + */ + order: + | 'toolCallBeforeDocUpdate' + | 'docUpdateBeforeToolCall' = 'toolCallBeforeDocUpdate' +): Promise<{ + vueNodes: VueNodeHelpers + sendDelayedDocUpdate: () => void +}> { + const host = new HostDoc(WORKFLOW_ID, SEED, CATALOG) + const vueNodes = new VueNodeHelpers(page) + + let socket: WebSocketRoute | null = null + const send = (frame: AgentWsEvent | HostFrame): void => { + if ( + (frame.type.startsWith('doc_') || frame.type === 'awareness') && + parseServerDocFrame(frame) === null + ) + throw new Error(`frame ${frame.type} is not a valid doc frame`) + if (!socket) throw new Error('the app has not opened /ws yet') + socket.send(JSON.stringify(frame)) + } + + await page.route('**/api/agent/threads', (route) => + route.fulfill(jsonRoute({ threads: [] })) + ) + await page.route('**/api/agent/threads/*/messages', (route) => { + if (route.request().method() === 'POST') { + return route.fulfill({ + status: 202, + contentType: 'application/json', + body: JSON.stringify({ + thread_id: THREAD_ID, + message_id: MESSAGE_ID, + workflow_id: WORKFLOW_ID + }) + }) + } + return route.fulfill(jsonRoute([])) + }) + await page.routeWebSocket(/\/ws/, (ws) => { + socket = ws + ws.send( + JSON.stringify({ + type: 'status', + data: { + status: { exec_info: { queue_remaining: 0 } }, + sid: SOCKET_SID + } + }) + ) + ws.onMessage((raw) => { + const stateVector = subscribeStateVectorOf(raw) + if (stateVector === null) return + send(host.subscribed()) + send(host.catchUp(stateVector)) + }) + }) + + await bootAgentApp(page, true, { + settings: { + 'Comfy.VueNodes.Enabled': true, + 'Comfy.Graph.CanvasInfo': false + } + }) + + const panel = page.locator('#agent-panel-root') + await page + .getByRole('button', { name: OPEN_AGENT_LABEL, exact: true }) + .click() + await expect(panel).toBeVisible({ timeout: 30_000 }) + + let savedName: string | undefined + await page.route('**/api/userdata/*', (route) => { + const request = route.request() + const path = decodeURIComponent( + new URL(request.url()).pathname.split('/userdata/')[1] + ) + if (request.method() !== 'POST' || !path.startsWith('workflows/')) + return route.fallback() + savedName = path.slice('workflows/'.length, -'.json'.length) + const saved: UserDataFullInfo = { + path, + modified: Date.now(), + size: request.postDataBuffer()?.length ?? 0 + } + return route.fulfill(jsonRoute(saved)) + }) + await page.route('**/api/workflows?*', (route) => { + const workflows: WorkflowListResponse = { + data: + savedName === undefined + ? [] + : [ + { + id: WORKFLOW_ID, + name: savedName, + created_at: '2026-09-01T00:00:00Z', + updated_at: '2026-09-01T00:00:00Z', + created_by: 'test-user-e2e', + latest_version: 1 + } + ], + pagination: { + has_more: false, + limit: 100, + offset: 0, + total: savedName === undefined ? 0 : 1 + } + } + return route.fulfill(jsonRoute(workflows)) + }) + + const picker = panel.getByRole('button', { + name: enMessages.agent.switchWorkflow + }) + await picker.click() + await page + .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) + .click() + await expect(picker).toHaveText('Unsaved Workflow') + + await expect.poll(() => socket !== null).toBe(true) + + const composer = panel.getByRole('textbox', { name: COMPOSER_LABEL }) + await composer.fill('Add a note to the canvas.') + await panel.getByRole('button', { name: SEND_LABEL }).click() + await expect( + panel.getByText('Add a note to the canvas.').first() + ).toBeVisible() + + send({ + type: 'agent_active_tab', + data: { + workflow_id: WORKFLOW_ID, + name: 'Unsaved Workflow', + thread_id: THREAD_ID, + message_id: MESSAGE_ID + } + }) + + // The doc host records the op on its authoritative document as soon as the + // tool actually executes -- this is not a race the agent lost, the op is + // real -- independent of when its broadcast reaches this client relative + // to the tool call's own terminal frame, which `order` controls below. + const update = host.apply([ + { + op: 'add_node', + pos: [0, 0], + node: { + id: ADDED_NODE_ID, + pos: [0, 0], + mode: 0, + size: [200, 100], + type: 'MarkdownNote', + flags: {}, + order: 0, + inputs: [], + outputs: [], + properties: {}, + widgets_values: [NODE_TEXT] + }, + node_id: ADDED_NODE_ID, + class_type: 'MarkdownNote' + } + ]) + expect(host.projection().nodes.map((n) => String(n.id))).toContain( + String(ADDED_NODE_ID) + ) + + // The inverted ordering: the doc_update broadcast reaches this client + // while the tool is still (from the chat frames' perspective) running -- + // the healthy-doc-host case most reviewers flagged as the lost-wakeup risk + // for notifyCanvasCaughtUp()'s edge triggering. + if (order === 'docUpdateBeforeToolCall') send(update) + + // The tool-call-completion affordance: `agentEventTransport.ts`'s `ingest()` + // flips this tool part `done` the instant this frame is read, purely from + // `status`, with nothing checked on the CRDT doc side (see file header). + send({ + type: 'agent_tool_call', + data: { + tool_call_id: 'call-add-node', + tool_name: 'add_node', + status: 'success', + duration_ms: 90, + thread_id: THREAD_ID, + message_id: MESSAGE_ID + } + }) + send({ + type: 'agent_message_delta', + data: { + delta: 'Added a note to the canvas.', + thread_id: THREAD_ID, + message_id: MESSAGE_ID + } + }) + send({ + type: 'agent_message_done', + data: { thread_id: THREAD_ID, message_id: MESSAGE_ID, usage: null } + }) + + // Setup checkpoint: every working affordance the report names has settled -- + // the composer is free again and the turn's own tool-call summary is up -- + // while the canvas (asserted by the caller) has nothing yet. + await expect(panel.getByRole('button', { name: SEND_LABEL })).toBeVisible() + await expect(panel.getByRole('button', { name: /^Ran/ })).toBeVisible() + + return { + vueNodes, + // A no-op once `order` already sent it above -- resending the same + // update twice is not part of what either ordering means to exercise. + sendDelayedDocUpdate: () => { + if (order !== 'docUpdateBeforeToolCall') send(update) + } + } +} + +test.describe( + 'Agent canvas lags every tool-call completion by the doc broadcast delay', + { tag: ['@cloud', '@agent'] }, + () => { + test.describe.configure({ timeout: 60_000 }) + + test('the tool call stays visibly syncing -- not done -- until the canvas actually catches up', async ({ + page + }) => { + const { vueNodes, sendDelayedDocUpdate } = + await driveThroughToolCallDone(page) + + // The turn's own summary settled instantly (asserted inside the arrange + // step above), matching the report's "every working affordance" -- but + // the fix under test lives one level down, on the tool call's OWN + // displayed state inside that summary. cloud/1.54's ToolCallGroup + // auto-expands (and stays expanded) while any of its own tool parts is + // still 'streaming' -- exactly this row's state here -- so there is + // nothing to click open; clicking an already-open trigger would only + // toggle it closed. + const panel = page.locator('#agent-panel-root') + const workSummary = panel.getByRole('button', { name: /^Ran/ }) + await expect(workSummary).toHaveAttribute('aria-expanded', 'true') + + const addNodeRow = panel + .getByRole('listitem') + .filter({ hasText: 'Add node' }) + await expect(addNodeRow).toBeVisible() + + // The fix: an `agent_tool_call` frame's own `status: 'success'` is no + // longer enough on its own to flip this row to "done" -- it stays on the + // same spinning glyph it showed while the tool was still running, + // because the canvas has not caught up to it yet (zero nodes, asserted + // below). Pre-fix, `ingest()` flipped this straight to the settled + // wrench glyph the instant the frame arrived, regardless of the canvas. + await expect(addNodeRow.locator('[class*="loader-circle"]')).toBeVisible() + await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toHaveCount( + 0 + ) + + // Once the doc host's delayed broadcast actually lands, the tool call's + // own displayed state catches up in the same moment its node does -- the + // "done" affordance and canvas reality are no longer decoupled. + sendDelayedDocUpdate() + await expect(addNodeRow.locator('[class*="loader-circle"]')).toHaveCount( + 0 + ) + await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible() + }) + + test('the node does eventually land once the delayed doc_update actually arrives', async ({ + page + }) => { + const { vueNodes, sendDelayedDocUpdate } = + await driveThroughToolCallDone(page) + + // Not a permanent blank canvas (unlike PM-1355): once the doc host's own + // broadcast finally goes out -- stagingcloud's own reported ~10 s -- + // the follower applies it exactly like any other live update and the + // node lands, with no reset or resubscribe involved. + await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toHaveCount( + 0 + ) + sendDelayedDocUpdate() + await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible() + }) + + // Regression for the lost-wakeup case flagged during review: on a + // healthy doc host the broadcast can just as well reach this client + // BEFORE the tool call's own terminal frame -- notifyCanvasCaughtUp() + // firing while nothing is pending yet must not be a no-op that strands + // the row at its full STALE_AFTER_MS fallback once the tool call's + // success frame arrives after the canvas already caught up. + test('settles immediately when the doc_update lands before the tool call reports done', async ({ + page + }) => { + const { vueNodes } = await driveThroughToolCallDone( + page, + 'docUpdateBeforeToolCall' + ) + + const panel = page.locator('#agent-panel-root') + const workSummary = panel.getByRole('button', { name: /^Ran/ }) + await workSummary.click() + await expect(workSummary).toHaveAttribute('aria-expanded', 'true') + + const addNodeRow = panel + .getByRole('listitem') + .filter({ hasText: 'Add node' }) + await expect(addNodeRow).toBeVisible() + await expect(addNodeRow.locator('[class*="loader-circle"]')).toHaveCount( + 0 + ) + await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible() + }) + } +) diff --git a/src/scripts/api.logs.test.ts b/src/scripts/api.logs.test.ts index 24b9b374658..4a00224966b 100644 --- a/src/scripts/api.logs.test.ts +++ b/src/scripts/api.logs.test.ts @@ -49,6 +49,7 @@ describe('api.getLogs', () => { outcomes: { received: 0, applied: 0, + appliedLive: 0, skipped: 0, errored: 0, gap: 0, diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index c76ac1e7712..9b58b3c6fd2 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -3569,6 +3569,39 @@ describe('AgentPanelRoot workflow binding', () => { expect(activity.editingTabPath).toBe('workflows/current.json') }) + // PM-1575 regression: the canvas-sync gate was wired to + // `agentPanelStore.enabled` (the panel feature flag), not to whether a CRDT + // doc subscription is actually connected. This suite -- like + // agentPanel.spec.ts and most of this file -- never sends a + // `doc_subscribed` frame, so the follower never connects; with the wrong + // gate, a mutating tool call's own successful frame was held at + // 'streaming' forever, since nothing was ever going to call + // `notifyCanvasCaughtUp()` to release it. Asserted on the store's own part + // state rather than the "Working..." composing status text upstream uses, + // since that composing affordance doesn't exist on this branch yet. + it('settles a mutating tool call immediately when no CRDT doc subscription is connected', async () => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + + await renderAndSend('add a node') + + ws.emit('agent_tool_call', { + tool_call_id: 'call-add-node', + tool_name: 'add_node', + status: 'success', + duration_ms: 90, + thread_id: 'th-1', + message_id: 'm-1' + }) + + await vi.waitFor(() => { + expect(useAgentConversationStore().messages[0].parts[0]).toMatchObject({ + type: 'tool', + state: 'done' + }) + }) + }) + it('moves the spinner to the tab the agent creates mid-turn', async () => { makeTab('wf-42') mockMessagesEndpoint('wf-42') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index f169fa98c2c..ad278d6a629 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -653,6 +653,52 @@ const { activeTurnId: conversationTurnId } = storeToRefs( useAgentConversationStore() ) +// PM-1575: a chat tool-call's own `status` says nothing about whether its +// effect has actually reached the canvas -- the CRDT doc_update travels a +// separate, unrelated listener (see agentEventTransport.ts's file header). +// Gate the transport's tool-call "done" affordance on canvas catch-up only +// while the CRDT follower is actually active, and re-check any parts it held +// back every time the bound workflow applies a fresh update. +// +// `agentPanelStore.enabled` alone is NOT that signal: it is the product +// feature flag ("is the agent panel available at all"), which is on in any +// environment or test that exercises the panel, whether or not a CRDT doc +// subscription for the bound workflow actually exists yet. Gating on it +// alone deferred every mutating tool call (add_node, set_widget, ...) to +// 'streaming' even when no doc_subscribed frame had ever been received -- +// e.g. in agentPanel.spec.ts and every other spec that drives chat events +// without also standing up a doc host -- so nothing was ever going to call +// notifyCanvasCaughtUp() to rescue it, and the row (and the composing +// "Working..." status derived from every part being settled) stayed stuck +// until the 30s STALE_AFTER_MS fallback. `crdtStatus.value.connected` is the +// actual "the follower is subscribed and could receive a doc_update" signal +// (flipped true only by a real `doc_subscribed { ok: true }` frame); a +// disabled panel never starts the follower, so this implies `enabled` too. +// Read `outcomes.appliedLive`, never `outcomes.applied`: `applied` also +// counts a subscribe's own one-time catch-up frame, which lands whenever the +// follower (re)subscribes to the bound workflow and has nothing to do with +// any tool call in flight. Gating on raw `applied` made the FIRST +// canvas-mutating tool call after any (re)subscribe -- effectively every +// tool call, since a `running` frame is never sent in practice, see +// agentEventTransport.ts's file header -- read that unrelated catch-up as +// its own matching update and settle to 'done' immediately, defeating the +// wait this gate exists for. +conversationStore.setCanvasSyncGate( + () => crdtStatus.value.connected, + () => crdtStatus.value.outcomes.appliedLive +) +watch( + () => crdtStatus.value.outcomes.appliedLive, + (applied, previouslyApplied) => { + // `useAgentCrdtFollower`'s status falls back to a disabled status with + // `applied: 0` when the follower is torn down, and a restarted follower + // counts from 0 again -- so toggling the panel mid-turn can drive this + // DOWN, not just up. A decrease is not a catch-up: nothing was applied, + // so it must not release parts that are still genuinely waiting. + if (applied > previouslyApplied) conversationStore.notifyCanvasCaughtUp() + } +) + // The resumed turn's own workflow outlives a panel remount (the session // binds it at ack; only newChat/loadThread reset it), while the active tab // may have changed since - prefer the bound tab over active-tab derivation. @@ -848,6 +894,15 @@ onBeforeUnmount(() => { tabActivity.setEditing(null) tabActivity.setCreating(false) agentMinimapLayer.dispose() + // PM-1575: the store singleton outlives this component. Without resetting + // the gate here, a remount's own setCanvasSyncGate() call is the only + // thing standing between the old (now torn-down) follower's gate and a + // turn resumed in the meantime reading it -- reset to the always-safe + // default instead of leaving whatever this instance last set. + conversationStore.setCanvasSyncGate( + () => false, + () => 0 + ) }) const history = useAgentChatHistoryStore() diff --git a/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts b/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts index 99e49e46b51..76413a646f2 100644 --- a/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts +++ b/src/workbench/extensions/agent/crdt/CrdtDevPanel.clipboard.test.ts @@ -48,6 +48,7 @@ const status: AgentCrdtStatus = { outcomes: { received: 1, applied: 1, + appliedLive: 1, skipped: 0, errored: 0, gap: 0, diff --git a/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts b/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts index 6192afbf022..8302c7e6ee4 100644 --- a/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts +++ b/src/workbench/extensions/agent/crdt/CrdtDevPanel.test.ts @@ -42,6 +42,7 @@ const STATUS: AgentCrdtStatus = { outcomes: { received: 5, applied: 4, + appliedLive: 4, skipped: 1, errored: 0, gap: 0, diff --git a/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts b/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts index c80610799a1..0bb54e08777 100644 --- a/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts +++ b/src/workbench/extensions/agent/crdt/crdtDebugReport.test.ts @@ -56,6 +56,7 @@ const SNAPSHOT: CrdtDebugSnapshot = { outcomes: { received: 3, applied: 3, + appliedLive: 3, skipped: 0, errored: 0, gap: 0, diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts index 41d53d27a9b..d83a272141c 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.test.ts @@ -685,6 +685,7 @@ describe('useAgentCrdtFollower', () => { expect(status().outcomes).toEqual({ received: 1, applied: 1, + appliedLive: 1, skipped: 0, errored: 0, gap: 0, @@ -802,6 +803,23 @@ describe('useAgentCrdtFollower', () => { unmount() }) + it('counts appliedLive for a live update but not for a subscribe catch-up frame', () => { + const { unmount, status } = mountFollower('wf-1') + + dispatchFrame('doc_update', { workflowId: 'wf-1', seq: 4, catchUp: true }) + expect(status().outcomes.applied).toBe(1) + expect(status().outcomes.appliedLive).toBe(0) + + dispatchFrame('doc_update', { + workflowId: 'wf-1', + seq: 5, + catchUp: false + }) + expect(status().outcomes.applied).toBe(2) + expect(status().outcomes.appliedLive).toBe(1) + unmount() + }) + it('accumulates received/applied/skipped across mixed frames without resetting on unrelated activity', () => { const { unmount, status } = mountFollower('wf-1') @@ -815,6 +833,7 @@ describe('useAgentCrdtFollower', () => { expect(status().outcomes).toEqual({ received: 3, applied: 2, + appliedLive: 2, skipped: 1, errored: 1, gap: 1, diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index f3a35a6fcb5..61beda0929c 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -168,6 +168,18 @@ interface AgentCrdtOutcomeCounters { received: number /** Passed this composable's own filter and the adapter had a bound session to apply it to. */ applied: number + /** + * PM-1575: same as `applied`, excluding a subscribe's own catch-up frame + * (`update.catchUp`) -- the one-time state-vector sync that lands whenever + * a workflow is (re)subscribed to, unrelated to any in-flight tool call. + * `applied` alone is unusable as a canvas-sync gate for that reason: a tool + * call's baseline, captured before that catch-up lands, would otherwise + * read the catch-up itself as "the matching update already arrived" for + * whichever tool call happens to be first after a (re)subscribe. Consumers + * that need "did a LIVE update land" (agentEventTransport.ts's canvas-sync + * baseline) must read this field, not `applied`. + */ + appliedLive: number /** Received but not applied: inactive target, workflow mismatch, or no bound adapter session. */ skipped: number /** The merged doc failed the KA-11 read gate (`schema_error`). */ @@ -335,6 +347,7 @@ export function useAgentCrdtFollower( outcomes: { received: 0, applied: 0, + appliedLive: 0, skipped: 0, errored: 0, gap: 0, @@ -397,6 +410,7 @@ function startAgentCrdtFollower( const outcomes = ref({ received: 0, applied: 0, + appliedLive: 0, skipped: 0, errored: 0, gap: 0, @@ -698,6 +712,12 @@ function startAgentCrdtFollower( outcomes.value = applied ? { ...outcomes.value, applied: outcomes.value.applied + 1 } : { ...outcomes.value, skipped: outcomes.value.skipped + 1 } + if (applied && !update.catchUp) { + outcomes.value = { + ...outcomes.value, + appliedLive: outcomes.value.appliedLive + 1 + } + } const materialized = applied ? reconcileLiveGraph(update.workflowId) : [] recordDevEvent('doc_update', { workflowId: update.workflowId, diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index 534ddc261ac..a9b7e6392a3 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -1,9 +1,12 @@ +import fs from 'fs' + import { describe, expect, it, vi } from 'vitest' import type { AgentMessages } from '../../schemas/agentApiSchema' import { toTurnId, zAgentWsEvent } from '../../schemas/agentApiSchema' import { normalizeAgentTranscript } from './agentTranscript' +import { STALE_AFTER_MS } from '../../crdt/useAgentCrdtFollower' import type { AgentChatEvent } from './agentEventTransport' import { createAgentEventTransport } from './agentEventTransport' import type { @@ -622,3 +625,246 @@ describe('agentEventTransport settle lifecycle', () => { ).toEqual(['wf-1', 'wf-2', 'wf-1']) }) }) + +describe('agentEventTransport canvas-sync gate (PM-1575)', () => { + it('holds a successful tool call at streaming until the canvas catches up', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + transport.ingest(toolCall('add_node', 'success')) + expect(toolParts(message)[0]).toMatchObject({ + ok: true, + state: 'streaming' + }) + + transport.notifyCanvasCaughtUp() + expect(toolParts(message)[0]).toMatchObject({ ok: true, state: 'done' }) + }) + + // A failed tool call never mutates anything, so there is no forthcoming + // doc_update for it to wait on -- gating it the same as a success would + // strand it at the spinner glyph until STALE_AFTER_MS (30s), and, for a + // turn whose other tool calls also never touch the canvas, + // notifyCanvasCaughtUp may never fire at all to rescue it early. + it('settles an errored tool call immediately, even while the gate is open', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + transport.ingest(toolCall('validate', 'error')) + + expect(toolParts(message)[0]).toMatchObject({ ok: false, state: 'done' }) + }) + + it('does not defer when the gate is closed', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => false) + + transport.ingest(toolCall('add_node', 'success')) + + expect(toolParts(message)[0]).toMatchObject({ ok: true, state: 'done' }) + }) + + // PM-1575 regression: most agent tools are read-only or navigational and + // never produce a doc_update at all (confirmed against every recorded + // conversation under browser_tests/fixtures/data/agent/conversations/ -- + // e.g. agent-rec-text-only-answer's switch_tab + print_workflow turn + // carries no graph_ops whatsoever). Gating those the same as a real graph + // edit stranded them at the spinner glyph for the full STALE_AFTER_MS, + // since nothing ever calls notifyCanvasCaughtUp for a turn with no canvas + // mutation to report. + it('settles a successful read-only tool call immediately, even while the gate is open', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + transport.ingest(toolCall('switch_tab', 'success')) + transport.ingest(toolCall('print_workflow', 'success', 'call-2')) + + expect(toolParts(message)).toEqual([ + expect.objectContaining({ name: 'switch_tab', ok: true, state: 'done' }), + expect.objectContaining({ + name: 'print_workflow', + ok: true, + state: 'done' + }) + ]) + }) + + // PM-1575 regression (finding #1, high): the normal ordering on a healthy + // doc host is the op broadcasting a doc_update WHILE the tool runs, with + // the success frame landing after. notifyCanvasCaughtUp() used to fire on + // that update while pendingCanvasSync was still empty (the terminal frame + // hadn't arrived yet to populate it) and be a silent no-op -- stranding + // the part at 'streaming' for the full STALE_AFTER_MS fallback, since + // nothing else would call notifyCanvasCaughtUp() again for a turn with no + // further doc activity. See browser_tests spec for the e2e counterpart. + it('settles immediately when the matching doc_update already applied before the terminal frame arrives', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + let outcomeCount = 0 + const transport = createAgentEventTransport( + message, + emit, + () => true, + () => outcomeCount + ) + + transport.ingest(toolCall('add_node', 'running')) + // The matching doc_update lands while the tool is still running. + outcomeCount += 1 + // No notifyCanvasCaughtUp() call in between: nothing was pending yet for + // it to release. + transport.ingest(toolCall('add_node', 'success')) + + expect(toolParts(message)[0]).toMatchObject({ ok: true, state: 'done' }) + }) + + it('still defers when the outcome counter has not moved since the tool started', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const outcomeCount = 3 + const transport = createAgentEventTransport( + message, + emit, + () => true, + () => outcomeCount + ) + + transport.ingest(toolCall('add_node', 'running')) + transport.ingest(toolCall('add_node', 'success')) + + expect(toolParts(message)[0]).toMatchObject({ + ok: true, + state: 'streaming' + }) + + transport.notifyCanvasCaughtUp() + expect(toolParts(message)[0]).toMatchObject({ ok: true, state: 'done' }) + }) + + // PM-1575 regression (finding #6, medium): a duplicate or redelivered + // terminal frame for the same tool_call_id (e.g. a websocket retry) used + // to overwrite the pending timer's map entry without clearing the + // previous handle, leaving it to fire at the ORIGINAL, now-stale deadline + // and settle the part early against the newer timer's own bookkeeping. + it('does not settle early from an orphaned timer left by a redelivered terminal frame', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + transport.ingest(toolCall('add_node', 'success', 'call-1')) + expect(toolParts(message)[0]).toMatchObject({ state: 'streaming' }) + + vi.advanceTimersByTime(15_000) + transport.ingest(toolCall('add_node', 'success', 'call-1')) + + vi.advanceTimersByTime(15_000) + expect(toolParts(message)[0]).toMatchObject({ state: 'streaming' }) + + vi.advanceTimersByTime(15_000) + expect(toolParts(message)[0]).toMatchObject({ state: 'done' }) + }) + + it('hasPendingCanvasSync reflects whether a tool call is held back', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + expect(transport.hasPendingCanvasSync()).toBe(false) + transport.ingest(toolCall('add_node', 'success')) + expect(transport.hasPendingCanvasSync()).toBe(true) + transport.notifyCanvasCaughtUp() + expect(transport.hasPendingCanvasSync()).toBe(false) + }) + + // PM-1575 (findings #4/#5): a transport torn down for a reason other than + // natural completion (abort, drop, reset, hydrate) must flush anything it + // is still holding and cancel its timers, rather than leaving a part + // stranded at 'streaming' or an orphaned timer that can fire later against + // a message object the store has since discarded or replaced. + it('dispose flushes held parts to done and cancels their timers', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit, () => true) + + transport.ingest(toolCall('add_node', 'success')) + expect(toolParts(message)[0]).toMatchObject({ state: 'streaming' }) + + transport.dispose() + expect(toolParts(message)[0]).toMatchObject({ state: 'done' }) + expect(transport.hasPendingCanvasSync()).toBe(false) + + const callsAfterDispose = emit.mock.calls.length + vi.advanceTimersByTime(STALE_AFTER_MS) + expect(emit.mock.calls.length).toBe(callsAfterDispose) + }) +}) + +// PM-1575 (finding #8, low): CANVAS_MUTATING_TOOLS is a hand-maintained set +// against an unconstrained `tool_name: z.string()` -- a renamed or newly +// added mutating tool would silently take the "settle immediately" branch +// and reintroduce the materialization-lag bug with no type or test failure. +// This pins every tool name actually seen in the recorded conversation +// fixtures against a hand-reviewed expectation, so a new, unreviewed name +// fails CI instead of silently defaulting to "not canvas-mutating". +describe('agentEventTransport CANVAS_MUTATING_TOOLS pinning (PM-1575)', () => { + const CONVERSATION_FIXTURES_DIR = + 'browser_tests/fixtures/data/agent/conversations' + + // Reviewed against every fixture under CONVERSATION_FIXTURES_DIR as of + // this test's authorship. A name appearing in a NEW or updated fixture + // that is not in this list fails the test below, forcing a reviewer to + // decide whether CANVAS_MUTATING_TOOLS needs it before updating this list. + const REVIEWED_TOOL_NAMES = new Set([ + 'add_node', + 'apply_ops', + 'clear_canvas', + 'connect', + 'delete_node', + 'find_nodes', + 'list_generate_models', + 'list_model_picks', + 'list_slots', + 'list_workflows', + 'ls_nodes', + 'print_workflow', + 'remember', + 'set_widget', + 'show_node', + 'switch_tab', + 'validate' + ]) + + function toolNamesInFixtures(): Set { + const names = new Set() + const collect = (value: unknown): void => { + if (Array.isArray(value)) { + for (const item of value) collect(item) + return + } + if (typeof value !== 'object' || value === null) return + for (const [key, nested] of Object.entries(value)) { + if (key === 'tool_name' && typeof nested === 'string') names.add(nested) + else collect(nested) + } + } + for (const file of fs.readdirSync(CONVERSATION_FIXTURES_DIR)) { + if (!file.endsWith('.json')) continue + const contents = fs.readFileSync( + `${CONVERSATION_FIXTURES_DIR}/${file}`, + 'utf-8' + ) + collect(JSON.parse(contents)) + } + return names + } + + it('pins every tool name recorded in the conversation fixtures', () => { + expect([...toolNamesInFixtures()].sort()).toEqual( + [...REVIEWED_TOOL_NAMES].sort() + ) + }) +}) diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index 65347002c8f..b7ab242aa6f 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -1,5 +1,6 @@ import type { AgentWsEvent } from '../../schemas/agentApiSchema' +import { STALE_AFTER_MS } from '../../crdt/useAgentCrdtFollower' import type { AssistantMessage, RunApprovalPart, @@ -23,14 +24,96 @@ export type AgentChatEvent = Extract< } > +type AgentToolCallEvent = Extract +type AgentActiveTabEvent = Extract +type AgentAskEvent = Extract +type AgentThinkingEvent = Extract +type AgentAskResolvedEvent = Extract< + AgentChatEvent, + { type: 'agent_ask_resolved' } +> +type AgentMessageDeltaEvent = Extract< + AgentChatEvent, + { type: 'agent_message_delta' } +> + +/** + * PM-1575: tool names whose execution can plausibly land a CRDT `doc_update` + * -- the only case `resolveToolCallState` below has anything to wait on. Most + * agent tools are read-only or purely navigational (`print_workflow`, + * `list_slots`, `switch_tab`, `remember`, ...) and never touch the doc at + * all: gating those the same way as a real graph edit strands them at the + * spinner glyph for the full `STALE_AFTER_MS`, since nothing -- no later + * `notifyCanvasCaughtUp()` in that turn, ever -- settles them early. Confirmed + * against every recorded conversation under + * `browser_tests/fixtures/data/agent/conversations/`: a read-only turn (e.g. + * `agent-rec-text-only-answer`'s `switch_tab` + `print_workflow`) carries no + * `graph_ops` at all, so this is not a hypothetical. + */ +const CANVAS_MUTATING_TOOLS = new Set([ + 'add_node', + 'delete_node', + 'set_widget', + 'connect', + 'disconnect', + 'clear_canvas', + 'apply_ops', + 'insert_workflow', + 'define_subgraph' +]) + export interface AgentEventTransport { ingest: (event: AgentChatEvent) => void settle: () => void + /** + * PM-1575: called whenever the bound workflow's CRDT follower applies a + * fresh doc update, so any tool-call parts this transport held back + * pending canvas catch-up (see `shouldAwaitCanvasSync` below) can settle to + * `done`. A no-op when nothing is pending. + */ + notifyCanvasCaughtUp: () => void + /** Whether any tool-call part is currently held pending canvas catch-up. */ + hasPendingCanvasSync: () => boolean + /** + * Tears the transport down for a reason other than natural completion + * (abort, drop, reset, hydrate). Flushes any tool-call parts held pending + * canvas catch-up to `done` and cancels their `STALE_AFTER_MS` timers, so + * none can fire later against a `message` object the store has since + * discarded or replaced with authoritative content under the same id. + */ + dispose: () => void } export function createAgentEventTransport( message: AssistantMessage, - emit: (m: AssistantMessage) => void + emit: (m: AssistantMessage) => void, + /** + * PM-1575: an `agent_tool_call` frame's own `status` says nothing about + * whether the effect it describes has actually reached the canvas -- that + * travels a separate, unrelated CRDT doc_update. When this returns `true` + * at the moment a tool call reports done, its part's chat-visible `state` + * is held at `'streaming'` (matching the in-progress icon/label) instead of + * flipping straight to `'done'`, until `notifyCanvasCaughtUp` is called or + * `STALE_AFTER_MS` elapses, whichever comes first -- reusing the same + * bound the CRDT follower's own passive heartbeat uses + * (`agentCrdtDocLifecycle.ts`), rather than inventing a new one. Defaults + * to never deferring, so a caller with no canvas to catch up on (or that + * never wires this up) keeps the immediate-done behavior. + */ + shouldAwaitCanvasSync: () => boolean = () => false, + /** + * PM-1575: the bound workflow's CRDT `outcomes.appliedLive` counter (or an + * equivalent monotonic count of LIVE frames actually applied to the doc -- + * deliberately excluding a subscribe's own catch-up frame, which is + * unrelated to any tool call and would otherwise look like "the matching + * update already arrived" to whichever tool call happens to be first after + * a (re)subscribe), read fresh whenever this transport needs it -- see + * `canvasSyncBaseline` below for why. Defaults to a constant so a caller + * with no counter to offer (e.g. a caller that also leaves + * `shouldAwaitCanvasSync` at its default) never spuriously looks "caught + * up". + */ + getCanvasSyncOutcomeCount: () => number = () => 0 ): AgentEventTransport { let openText: TextPart | null = null let openThinking: ThinkingPart | null = null @@ -46,6 +129,218 @@ export function createAgentEventTransport( ) let settled = false let lastTabTargetKey: string | undefined + // Tool parts whose frame reported done but whose displayed state is held at + // 'streaming' pending canvas catch-up. Each has its own bounded timer so a + // part that never gets a `notifyCanvasCaughtUp` call still settles. + const pendingCanvasSync = new Map>() + // PM-1575: the outcome count as of the last time some tool part actually + // claimed a canvas catch-up (see `claimCanvasSyncOutcome` below), starting + // at this transport's own creation. `canvasSyncBaseline` below hands each + // NEW tool part this shared watermark rather than a fresh + // `getCanvasSyncOutcomeCount()` read, because that read is worthless when a + // tool call's first-ever frame is already its terminal one (the common + // case -- recorded conversations never send a `running` frame at all, + // see the file header): capturing the baseline and checking it happen in + // the same synchronous call, so a fresh read is trivially equal to itself + // regardless of whether a `doc_update` already landed moments earlier. The + // watermark instead stays stale across that no-op `notifyCanvasCaughtUp()` + // call, so the next part created can see the count has moved since the + // watermark was last claimed and settle immediately. + let canvasSyncOutcomeWatermark = getCanvasSyncOutcomeCount() + // The outcome-count watermark handed to each tool part when it is first + // seen (see `canvasSyncOutcomeWatermark` above). + const canvasSyncBaseline = new Map() + + function claimCanvasSyncOutcome(): void { + canvasSyncOutcomeWatermark = getCanvasSyncOutcomeCount() + } + + function clearPendingCanvasSyncTimer(part: ToolPart): void { + const timer = pendingCanvasSync.get(part) + if (timer !== undefined) clearTimeout(timer) + pendingCanvasSync.delete(part) + } + + function settlePendingCanvasSync(part: ToolPart): void { + clearPendingCanvasSyncTimer(part) + part.state = 'done' + } + + function flushPendingCanvasSync(): void { + if (pendingCanvasSync.size === 0) return + for (const part of pendingCanvasSync.keys()) settlePendingCanvasSync(part) + claimCanvasSyncOutcome() + emit(snapshotMessage(message)) + } + + function notifyCanvasCaughtUp(): void { + flushPendingCanvasSync() + } + + function hasPendingCanvasSync(): boolean { + return pendingCanvasSync.size > 0 + } + + function dispose(): void { + flushPendingCanvasSync() + } + + /** + * PM-1575: applies a finished tool-call frame's outcome to its part, then + * decides whether the part's displayed `state` can flip straight to + * `'done'` or must be held at `'streaming'` pending canvas catch-up (see + * `shouldAwaitCanvasSync` above). Pulled out of `ingest` so that switch's + * `agent_tool_call` branch stays a simple call instead of an inline gate. + * + * Only a `'success'` outcome can be waited on: it is the only case where a + * matching `doc_update` might still be in flight. An `'error'` outcome + * means the tool never mutated anything, so there is no forthcoming canvas + * change to catch up to -- deferring it anyway just strands the part at the + * spinner glyph for up to `STALE_AFTER_MS` (30s) instead of showing the + * failure immediately, and, for a turn whose other tool calls also never + * touch the canvas, `notifyCanvasCaughtUp` may never fire at all to rescue + * it early. + * + * Even a successful outcome is only worth waiting on when the tool itself + * is one that can mutate the doc (`CANVAS_MUTATING_TOOLS` above) -- the + * same stranding risk applies to e.g. a successful `print_workflow` or + * `switch_tab`, which never produces a `doc_update` at all. + */ + function resolveToolCallState( + part: ToolPart, + status: 'success' | 'error', + durationMs: number | undefined + ): void { + part.ok = status === 'success' + part.durationMs = durationMs + // A duplicate or redelivered terminal frame (e.g. a websocket retry) must + // not leave a stale timer racing the one this call is about to arm -- + // clearing unconditionally, before either branch below, is what keeps + // that impossible regardless of which branch runs. + clearPendingCanvasSyncTimer(part) + const baseline = canvasSyncBaseline.get(part) + canvasSyncBaseline.delete(part) + if ( + status === 'success' && + CANVAS_MUTATING_TOOLS.has(part.name) && + shouldAwaitCanvasSync() + ) { + if (baseline !== undefined && getCanvasSyncOutcomeCount() > baseline) { + // The matching doc_update already applied -- either while the tool + // was still running, or before this tool call's first frame ever + // reached the transport -- see `canvasSyncBaseline` above. Nothing + // left to wait on. + part.state = 'done' + claimCanvasSyncOutcome() + } else { + pendingCanvasSync.set( + part, + setTimeout(() => { + settlePendingCanvasSync(part) + emit(snapshotMessage(message)) + }, STALE_AFTER_MS) + ) + } + } else { + part.state = 'done' + } + } + + /** + * Applies one `agent_tool_call` frame: finds or creates the part it + * targets, updates its name, and (once the frame reports a terminal + * status) resolves its displayed state via `resolveToolCallState`. Pulled + * out of `ingest`'s switch so that case is a single call. + */ + function handleToolCallEvent(data: AgentToolCallEvent['data']): void { + let part = tools.get(data.tool_call_id) + if (!part) { + part = { + type: 'tool', + callId: data.tool_call_id, + name: data.tool_name, + state: 'streaming' + } + tools.set(data.tool_call_id, part) + message.parts.push(part) + canvasSyncBaseline.set(part, canvasSyncOutcomeWatermark) + } + part.name = data.tool_name + if (data.status !== 'running') { + resolveToolCallState(part, data.status, data.duration_ms) + } + } + + /** + * Applies one `agent_active_tab` frame. The agent re-announces the same + * tab as it keeps working on it, with text and tool calls in between, so + * only a change of tab is worth another link in the transcript. Returns + * `false` when this frame repeats the last-announced tab and was + * otherwise a no-op, mirroring `ingest`'s early `return` for that case. + */ + function handleActiveTabEvent(data: AgentActiveTabEvent['data']): boolean { + const targetKey = `${data.workflow_id}\u0000${data.node_locator_id ?? ''}` + if (lastTabTargetKey === targetKey) return false + lastTabTargetKey = targetKey + closeOpenText() + closeOpenThinking() + message.thinking = false + message.thinkingText = undefined + message.parts.push({ + type: 'tabLink', + workflowId: data.workflow_id, + locatorId: data.node_locator_id, + name: data.name + }) + return true + } + + /** + * Applies one `agent_ask` frame. Only the `run_approval` kind renders a + * part; returns `false` for any other kind, mirroring `ingest`'s early + * `return` for that case. + */ + function handleAskEvent(data: AgentAskEvent['data']): boolean { + if (data.kind !== 'run_approval') return false + closeOpenText() + closeOpenThinking() + message.thinking = false + message.thinkingText = undefined + const part: RunApprovalPart = { + type: 'runApproval', + askId: data.ask_id, + workflowId: data.context?.workflow_id || undefined, + workflowName: data.context?.workflow_name || undefined + } + message.parts.push(part) + return true + } + + /** Applies one `agent_thinking` frame: appends its delta to the open + * thinking part, opening one first if none is open. */ + function handleThinkingEvent(data: AgentThinkingEvent['data']): void { + closeOpenText() + message.thinking = true + ;(openThinking ?? openNewThinking()).text += data.delta + message.thinkingText = openThinking?.text + } + + /** Applies one `agent_ask_resolved` frame: drops the matching run-approval + * part, since its ask is no longer pending. */ + function handleAskResolvedEvent(data: AgentAskResolvedEvent['data']): void { + message.parts = message.parts.filter( + (part) => part.type !== 'runApproval' || part.askId !== data.ask_id + ) + } + + /** Applies one `agent_message_delta` frame: appends its delta to the open + * text part, opening one first if none is open. */ + function handleMessageDeltaEvent(data: AgentMessageDeltaEvent['data']): void { + closeOpenThinking() + message.thinking = false + message.thinkingText = undefined + ;(openText ?? openNewText()).text += data.delta + } function closeOpenText(): void { if (openText) { @@ -82,90 +377,45 @@ export function createAgentEventTransport( return part } - function ingest(event: AgentChatEvent): void { - if (settled) return + /** + * Applies one chat event to `message` by dispatching to the per-type + * handler above, and reports whether `ingest` should emit a fresh + * snapshot afterwards. Most event types always want a snapshot; a + * repeated `agent_active_tab` or a non-`run_approval` `agent_ask` is a + * no-op (mirroring their handlers' own `false` return), and + * `agent_message_done` already emits via `settle()` so it says no too. + */ + function applyChatEvent(event: AgentChatEvent): boolean { switch (event.type) { case 'agent_thinking': - closeOpenText() - message.thinking = true - ;(openThinking ?? openNewThinking()).text += event.data.delta - message.thinkingText = openThinking?.text - break - case 'agent_tool_call': { - closeOpenText() - closeOpenThinking() - message.thinking = false - message.thinkingText = undefined - let part = tools.get(event.data.tool_call_id) - if (!part) { - part = { - type: 'tool', - callId: event.data.tool_call_id, - name: event.data.tool_name, - state: 'streaming' - } - tools.set(event.data.tool_call_id, part) - message.parts.push(part) - } - part.name = event.data.tool_name - if (event.data.status !== 'running') { - part.state = 'done' - part.ok = event.data.status === 'success' - part.durationMs = event.data.duration_ms - } - break - } - case 'agent_active_tab': { - // The agent re-announces the same tab as it keeps working on it, with - // text and tool calls in between, so the tail of parts is not the test; - // only a change of tab is worth another link in the transcript. - const targetKey = `${event.data.workflow_id}\u0000${event.data.node_locator_id ?? ''}` - if (lastTabTargetKey === targetKey) return - lastTabTargetKey = targetKey - closeOpenText() - closeOpenThinking() - message.thinking = false - message.thinkingText = undefined - message.parts.push({ - type: 'tabLink', - workflowId: event.data.workflow_id, - locatorId: event.data.node_locator_id, - name: event.data.name - }) - break - } - case 'agent_ask': { - if (event.data.kind !== 'run_approval') return + handleThinkingEvent(event.data) + return true + case 'agent_tool_call': closeOpenText() closeOpenThinking() message.thinking = false message.thinkingText = undefined - const part: RunApprovalPart = { - type: 'runApproval', - askId: event.data.ask_id, - workflowId: event.data.context?.workflow_id || undefined, - workflowName: event.data.context?.workflow_name || undefined - } - message.parts.push(part) - break - } + handleToolCallEvent(event.data) + return true + case 'agent_active_tab': + return handleActiveTabEvent(event.data) + case 'agent_ask': + return handleAskEvent(event.data) case 'agent_ask_resolved': - message.parts = message.parts.filter( - (part) => - part.type !== 'runApproval' || part.askId !== event.data.ask_id - ) - break + handleAskResolvedEvent(event.data) + return true case 'agent_message_delta': - closeOpenThinking() - message.thinking = false - message.thinkingText = undefined - ;(openText ?? openNewText()).text += event.data.delta - break + handleMessageDeltaEvent(event.data) + return true case 'agent_message_done': settle() - return + return false } - emit(snapshotMessage(message)) + } + + function ingest(event: AgentChatEvent): void { + if (settled) return + if (applyChatEvent(event)) emit(snapshotMessage(message)) } function settle(): void { @@ -179,5 +429,5 @@ export function createAgentEventTransport( emit(snapshotMessage(message)) } - return { ingest, settle } + return { ingest, settle, notifyCanvasCaughtUp, hasPendingCanvasSync, dispose } } diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index eb248ec3a3b..81ff4c631bd 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -1,6 +1,6 @@ import { createPinia, setActivePinia } from 'pinia' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { nextTick, watch } from 'vue' +import { nextTick, ref, watch } from 'vue' import type { AgentMessages, TurnId } from '../../schemas/agentApiSchema' import { zAgentMessages, zAgentWsEvent } from '../../schemas/agentApiSchema' @@ -241,6 +241,199 @@ describe('useAgentConversationStore', () => { }) }) + // PM-1575 regression: a tool call held back pending canvas catch-up must + // still receive notifyCanvasCaughtUp() after its turn settles. + // agent_message_done drops the active-turn transport out of the `transport` + // slot (clearActive()) the instant it lands, which used to leave the held + // part with no reachable transport for notifyCanvasCaughtUp() to forward + // to -- stranding it until its own STALE_AFTER_MS fallback fired, tens of + // seconds later than the canvas actually caught up. + it('still settles a canvas-sync-pending tool call after its turn has settled', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => true) + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + store.ingest(done('t1')) + + // The turn is settled (message.streaming is false), but the tool part + // itself is held at 'streaming' -- the spinner -- pending canvas catch-up. + expect(store.messages[0].streaming).toBe(false) + expect(store.messages[0].parts[0]).toMatchObject({ + type: 'tool', + state: 'streaming' + }) + + store.notifyCanvasCaughtUp() + + expect(store.messages[0].parts[0]).toMatchObject({ + type: 'tool', + state: 'done' + }) + }) + + // PM-1575 regression (finding #1/#9, high): the normal ordering on a + // healthy doc host is the matching doc_update applying WHILE the tool is + // still running, with the success frame landing after -- so by the time + // the terminal frame arrives there is nothing left to wait on. Threads the + // outcome-count getter end to end (store -> transport), unlike the + // transport-level unit test for the same finding. + it('settles a held tool call immediately when the canvas catches up while it is still running', () => { + const store = useAgentConversationStore() + let outcomeCount = 0 + store.setCanvasSyncGate( + () => true, + () => outcomeCount + ) + store.startTurn(T1) + + store.ingest(toolCall('t1', 'add_node', 'running')) + outcomeCount += 1 + store.ingest(toolCall('t1', 'add_node', 'success')) + + expect(store.messages[0].parts[0]).toMatchObject({ + type: 'tool', + state: 'done' + }) + }) + + // PM-1575 regression (finding #3, medium): settleActiveTurn used to keep + // only a single "settled but still holding" transport reachable. A second + // turn settling within the same window while ALSO holding a part + // overwrote that slot, stranding the first turn's held part until its own + // STALE_AFTER_MS fallback. + it('keeps an earlier settled turn reachable after a second turn also settles with a held part', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => true) + + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + store.ingest(done('t1')) + + store.startTurn(T2) + store.ingest(toolCall('t2', 'add_node', 'success')) + store.ingest(done('t2')) + + store.notifyCanvasCaughtUp() + + expect(store.messages[0].parts[0]).toMatchObject({ + type: 'tool', + state: 'done' + }) + expect(store.messages[1].parts[0]).toMatchObject({ + type: 'tool', + state: 'done' + }) + }) + + // PM-1575 regression (finding #4, medium): abortActiveTurn() used to drop + // the transport out of every reachable slot without flushing what it was + // still holding, stranding the part at 'streaming' until its own 30s + // fallback even though nothing will ever call notifyCanvasCaughtUp() for + // a turn nobody is tracking anymore. + it('flushes a held tool-call part immediately when its turn is aborted', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => true) + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + expect(store.messages[0].parts[0]).toMatchObject({ state: 'streaming' }) + + store.abortActiveTurn() + + expect(store.messages[0].parts[0]).toMatchObject({ state: 'done' }) + }) + + // PM-1575 regression (finding #4, medium): resumeBackgroundTurn()'s SECOND + // early return -- reached when a settled background turn's message is + // kept on screen but not reactivated as the live turn -- dropped the + // transport the same way as the paths above, leaving a held part + // reachable only via its own 30s STALE_AFTER_MS fallback instead of + // settling the moment it is clear nothing will ever resume it. + it('flushes a held tool-call part immediately when a settled background turn is resumed but not reactivated', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => true) + store.setThreadId('th') + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + store.stashActiveTurn() + store.ingest(done('t1')) + expect(store.messages[0].parts[0]).toMatchObject({ state: 'streaming' }) + + store.resumeBackgroundTurn() + + expect(store.messages[0].parts[0]).toMatchObject({ state: 'done' }) + }) + + // PM-1575 regression (finding #5, medium): nothing cancelled a held tool + // call's 30s timer when hydrate() discarded its transport. The orphaned + // timer could fire after hydrate() replaced the transcript with + // authoritative server content under the SAME turn id, overwriting it + // with the disposed transport's own stale snapshot. + it('does not let an orphaned canvas-sync timer overwrite hydrated content under the same turn id', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => true) + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + store.ingest(done('t1')) + + store.hydrate([ + historyRow(1, 'user', 't1', 'go'), + historyRow(2, 'assistant', 't1', 'authoritative reply') + ]) + + vi.advanceTimersByTime(30_000) + + expect(partTexts(store)).toEqual(['authoritative reply']) + }) + + // PM-1575 regression (finding #7, medium): canvasSyncGate used to be + // captured BY VALUE at transport-creation time, so a later + // setCanvasSyncGate() call (e.g. AgentPanelRoot.vue re-registering after a + // remount) never reached a transport created before it. + it('routes a later setCanvasSyncGate() call to a transport created before it', () => { + const store = useAgentConversationStore() + store.setCanvasSyncGate(() => false) + store.startTurn(T1) + + store.setCanvasSyncGate(() => true) + store.ingest(toolCall('t1', 'add_node', 'success')) + + expect(store.messages[0].parts[0]).toMatchObject({ state: 'streaming' }) + }) + + // PM-1575 regression (finding #10, medium): the source watcher this + // guards -- AgentPanelRoot.vue's `watch(() => crdtStatus.value.outcomes + // .applied, ...)` -- treated ANY change to the counter as catch-up, + // including a decrease. `useAgentCrdtFollower` resets `outcomes.applied` + // to 0 when its follower is torn down (e.g. toggling the panel mid-turn) + // and a restarted follower counts from 0 again, so that watcher's own + // guard, reproduced here against the real store, is what stops a mere + // reset from incorrectly releasing every held tool call. + it('does not release a held tool call when the applied counter decreases, only when it increases', () => { + const store = useAgentConversationStore() + const applied = ref(5) + store.setCanvasSyncGate( + () => true, + () => applied.value + ) + store.startTurn(T1) + store.ingest(toolCall('t1', 'add_node', 'success')) + expect(store.messages[0].parts[0]).toMatchObject({ state: 'streaming' }) + + watch( + applied, + (value, previous) => { + if (value > previous) store.notifyCanvasCaughtUp() + }, + { flush: 'sync' } + ) + + applied.value = 0 + expect(store.messages[0].parts[0]).toMatchObject({ state: 'streaming' }) + + applied.value = 6 + expect(store.messages[0].parts[0]).toMatchObject({ state: 'done' }) + }) + it('restores a pending run approval as the live turn and continues after it resolves', () => { const store = useAgentConversationStore() store.setThreadId('th') diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts index ea3a619a126..0790d67255a 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.ts @@ -48,15 +48,48 @@ export const useAgentConversationStore = defineStore( const latestWorkflowId = ref() let transport: AgentEventTransport | null = null let liveMessage: AssistantMessage | null = null + // PM-1575: whether a newly-created transport should hold a tool-call's + // chat "done" state back until canvas catch-up is confirmed (see + // agentEventTransport.ts). Defaults to never deferring, so a caller that + // never registers a gate (e.g. a headless/non-canvas conversation) keeps + // the pre-fix, immediate-done behavior. Read indirectly (via a wrapping + // closure, never passed by value) everywhere it is handed to a + // transport, so a later setCanvasSyncGate() call reaches transports + // that already exist -- passing the variable itself would freeze each + // transport onto whichever function this variable held at its own + // creation time. + let canvasSyncGate: () => boolean = () => false + // PM-1575: mirrors canvasSyncGate above, for the monotonic doc-update + // outcome counter a transport compares its per-tool-call baseline + // against (agentEventTransport.ts's canvasSyncBaseline). Same + // read-indirectly rule applies. + let canvasSyncOutcomeCount: () => number = () => 0 + // PM-1575: settled turns (agent_message_done already applied) whose + // transport is still holding at least one tool-call part back pending + // canvas catch-up. clearActive() drops the `transport` slot the moment a + // turn settles, same as it always has, so without this a settled + // transport becomes unreachable and notifyCanvasCaughtUp() below can + // never deliver the catch-up signal it is waiting for -- the part would + // then only ever settle via its own STALE_AFTER_MS fallback. A Set, not + // a single slot: a single slot lets turn B's settle overwrite turn A's + // entry while A is still holding a part, stranding A the same way. Each + // entry prunes itself out the first time notifyCanvasCaughtUp() finds it + // has nothing left pending. + const settledActiveTransports = new Set() const backgroundTurns = new Map() let hydratedMessageIds = new Set() let hydratedAssistantTurnIds = new Set() const activeIndex = ref(-1) function replaceActive(message: AssistantMessage): void { - const index = activeIndex.value - if (index >= 0 && messages.value[index]?.id === message.id) - messages.value[index] = message + // PM-1575: looked up by id, not `activeIndex.value`. A turn's own + // transport keeps emitting after settle -- notifyCanvasCaughtUp() can + // still land on it while a tool-call part is held pending canvas + // catch-up (see settledActiveTransports below) -- and by then + // clearActive() has already reset activeIndex.value to -1, even though + // the settled message is still sitting in `messages` at its own slot. + const index = messages.value.findIndex((m) => m.id === message.id) + if (index >= 0) messages.value[index] = message } function recordUser( @@ -125,17 +158,18 @@ export const useAgentConversationStore = defineStore( liveMessage = message activeTurnId.value = turnId activeIndex.value = messages.value.push(message) - 1 - transport = createAgentEventTransport(message, replaceActive) + transport = createAgentEventTransport( + message, + replaceActive, + () => canvasSyncGate(), + () => canvasSyncOutcomeCount() + ) } function ingest(event: AgentChatEvent): void { - if (transport && event.data.message_id === activeTurnId.value) { - if (event.type === 'agent_message_done') { - transport.settle() - clearActive() - return - } - transport.ingest(event) + const activeTransport = transport + if (activeTransport && event.data.message_id === activeTurnId.value) { + ingestActiveTurnEvent(event, activeTransport) return } const eventThreadId = event.data.thread_id @@ -145,12 +179,50 @@ export const useAgentConversationStore = defineStore( event.type === 'agent_active_tab' && event.data.message_id === undefined ) { - if (eventThreadId === undefined || eventThreadId === threadId.value) - transport?.ingest(event) - else backgroundTurns.get(eventThreadId)?.transport.ingest(event) + ingestActiveTabEvent(event, eventThreadId) return } if (eventThreadId === undefined) return + ingestBackgroundTurnEvent(event, eventThreadId) + } + + function ingestActiveTurnEvent( + event: AgentChatEvent, + activeTransport: AgentEventTransport + ): void { + if (event.type === 'agent_message_done') { + settleActiveTurn(activeTransport) + return + } + activeTransport.ingest(event) + } + + // PM-1575: capture the transport before clearActive() drops the + // `transport` slot, so notifyCanvasCaughtUp() can still reach it while a + // tool-call part is held pending canvas catch-up (see + // settledActiveTransports above). Only kept around when it actually has + // something pending -- a settled transport with nothing held has no + // reason to stay reachable. + function settleActiveTurn(activeTransport: AgentEventTransport): void { + activeTransport.settle() + if (activeTransport.hasPendingCanvasSync()) + settledActiveTransports.add(activeTransport) + clearActive() + } + + function ingestActiveTabEvent( + event: AgentChatEvent, + eventThreadId: string | undefined + ): void { + if (eventThreadId === undefined || eventThreadId === threadId.value) + transport?.ingest(event) + else backgroundTurns.get(eventThreadId)?.transport.ingest(event) + } + + function ingestBackgroundTurnEvent( + event: AgentChatEvent, + eventThreadId: string + ): void { const entry = backgroundTurns.get(eventThreadId) if (!entry || entry.messageId !== event.data.message_id) return if (event.type === 'agent_message_done') { @@ -161,9 +233,40 @@ export const useAgentConversationStore = defineStore( entry.transport.ingest(event) } + function setCanvasSyncGate( + gate: () => boolean, + outcomeCount: () => number = () => 0 + ): void { + canvasSyncGate = gate + canvasSyncOutcomeCount = outcomeCount + } + + /** + * PM-1575: forwarded to every live transport (the active turn and any + * stashed background ones) whenever the bound workflow's CRDT follower + * applies a fresh doc update, so tool-call parts held back pending canvas + * catch-up can settle to 'done'. A no-op on a transport with nothing + * pending. + */ + function notifyCanvasCaughtUp(): void { + transport?.notifyCanvasCaughtUp() + for (const settledTransport of settledActiveTransports) { + settledTransport.notifyCanvasCaughtUp() + if (!settledTransport.hasPendingCanvasSync()) + settledActiveTransports.delete(settledTransport) + } + for (const entry of backgroundTurns.values()) + entry.transport.notifyCanvasCaughtUp() + } + function abortActiveTurn(): void { if (!transport) return transport.settle() + // Not `settledActiveTransports`: an abort is not a natural completion + // whose held parts might still catch up, so flush them to `done` and + // cancel their timers now rather than leaving them reachable only by + // their own STALE_AFTER_MS fallback (or, worse, orphaned). + transport.dispose() clearActive() } @@ -198,8 +301,14 @@ export const useAgentConversationStore = defineStore( entry.settled && !poppedHydratedCopy && hydratedMessageIds.has(entry.messageId) - ) + ) { + // The persisted, authoritative copy is already on screen (kept, via + // the filter above) -- this entry's transport is now discarded for + // good, so flush anything it is still holding rather than leaving it + // unreachable until its own STALE_AFTER_MS fallback. + entry.transport.dispose() return + } if ( entry.userText !== undefined && !userTexts.value.has(entry.message.id) @@ -207,7 +316,15 @@ export const useAgentConversationStore = defineStore( userTexts.value.set(entry.message.id, entry.userText) const index = kept.push(entry.message) - 1 messages.value = kept - if (entry.settled) return + if (entry.settled) { + // PM-1575: this settled turn is kept on screen but not reactivated -- + // its transport is discarded for good right after this, same as the + // hydrated-copy-dropped branch above, so flush anything it is still + // holding rather than leaving it unreachable until its own + // STALE_AFTER_MS fallback. + entry.transport.dispose() + return + } activeTurnId.value = entry.messageId activeIndex.value = index transport = entry.transport @@ -235,13 +352,20 @@ export const useAgentConversationStore = defineStore( for (const [key, entry] of backgroundTurns) { if (entry.messageId !== turnId) continue entry.transport.settle() + // This turn is being dropped from the map here, unlike the + // agent_message_done path in ingestBackgroundTurnEvent -- nothing + // will keep it reachable afterwards, so flush its held parts now. + entry.transport.dispose() backgroundTurns.delete(key) return } } function dropBackgroundTurns(): void { - for (const entry of backgroundTurns.values()) entry.transport.settle() + for (const entry of backgroundTurns.values()) { + entry.transport.settle() + entry.transport.dispose() + } backgroundTurns.clear() } @@ -252,6 +376,19 @@ export const useAgentConversationStore = defineStore( activeTurnId.value = null } + // PM-1575: reset() and hydrate() both discard the active transport (if + // any) and every settled-but-still-holding one outright, with no + // background-turn stash to keep them reachable through. Without + // disposing them first, an orphaned STALE_AFTER_MS timer can fire later + // and write a stale snapshot back over transcript content hydrate() has + // since replaced under the same turn id. + function disposeActiveAndSettledTransports(): void { + transport?.dispose() + for (const settledTransport of settledActiveTransports) + settledTransport.dispose() + settledActiveTransports.clear() + } + function dropAttachmentPreviews(): void { for (const attachments of userAttachments.value.values()) { for (const { previewUrl } of attachments) { @@ -262,6 +399,7 @@ export const useAgentConversationStore = defineStore( } function reset(): void { + disposeActiveAndSettledTransports() messages.value = [] userTexts.value = new Map() userTags.value = new Map() @@ -275,6 +413,7 @@ export const useAgentConversationStore = defineStore( } function hydrate(history: AgentMessages): void { + disposeActiveAndSettledTransports() clearActive() const transcript = normalizeAgentTranscript(history) messages.value = transcript.messages @@ -292,7 +431,9 @@ export const useAgentConversationStore = defineStore( activeIndex.value = messages.value.indexOf(transcript.pending.message) transport = createAgentEventTransport( transcript.pending.message, - replaceActive + replaceActive, + () => canvasSyncGate(), + () => canvasSyncOutcomeCount() ) } } @@ -341,6 +482,8 @@ export const useAgentConversationStore = defineStore( setPaywallsResolved, startTurn, ingest, + setCanvasSyncGate, + notifyCanvasCaughtUp, abortActiveTurn, stashActiveTurn, resumeBackgroundTurn,