diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts index 75eee7f6a50..1d795809849 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -296,15 +296,6 @@ export class AgentFollowerHostSocket { return batch.ops } - /** - * Make the host REFUSE subscribes, as it does when `docService` is nil, - * when it is overloaded, or at the per-session document cap. No catch-up - * follows a refusal, so the follower gets no canvas frame at all. - * - * `times` bounds the refusal: a finite count models a transient overload - * that the follower's own subscribe-retry ladder is meant to ride out, and - * the default (unbounded) models a host that never recovers. - */ refuseSubscribes( reason = 'overloaded', times = Number.POSITIVE_INFINITY @@ -321,7 +312,11 @@ export class AgentFollowerHostSocket { if (this.refuseReason !== null && this.refusalsLeft > 0) { this.refusalsLeft -= 1 this.refusedSubscribes += 1 - this.send(this.host.subscribeRefused(this.refuseReason)) + const refusal = this.host.subscribeRefused(this.refuseReason) + this.send({ + ...refusal, + data: { ...refusal.data, code: this.refuseReason } + }) this.resolveSubscribed?.() return } diff --git a/browser_tests/fixtures/agentTurnLockFixture.ts b/browser_tests/fixtures/agentTurnLockFixture.ts index 4e8919034a3..7c6c38477c9 100644 --- a/browser_tests/fixtures/agentTurnLockFixture.ts +++ b/browser_tests/fixtures/agentTurnLockFixture.ts @@ -14,6 +14,7 @@ import { zAgentPendingAsk, zAgentPostMessageRequest } from '@comfyorg/ingest-types/zod' +import { escapeRegExp } from 'es-toolkit' import { z } from 'zod' import enMessages from '@/locales/en/main.json' with { type: 'json' } @@ -532,14 +533,14 @@ export class AgentTurnLockHarness { name: enMessages.agent.runApproval.run, exact: true }) - // 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.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}` + `^(${[ + enMessages.agent.worked, + enMessages.agent.workedNotConfirmedOnCanvas + ] + .map(escapeRegExp) + .join('|')})$` ) }) this.workingRow = this.panel.getByText(enMessages.agent.working, { diff --git a/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts b/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts index 243c9864161..dcdca9d14fe 100644 --- a/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts +++ b/browser_tests/fixtures/helpers/AgentRemoteApplyHarness.ts @@ -1,5 +1,6 @@ import { expect } from '@playwright/test' import type { Locator, Page } from '@playwright/test' +import { escapeRegExp } from 'es-toolkit' import enMessages from '@/locales/en/main.json' with { type: 'json' } import type { @@ -11,7 +12,9 @@ import { HostDoc } from '@e2e/fixtures/agentConversationHostDoc' import type { HostFrame } from '@e2e/fixtures/agentConversationHostDoc' import { AgentFollowerHostSocket } from '@e2e/fixtures/agentFollowerHostSocket' import { + BLANK_WORKFLOW, bootAgentApp, + loadIntoBootWorkflow, mockAgentTurnApi, mockWorkflowPersistence } from '@e2e/fixtures/agentPanelFixture' @@ -63,18 +66,18 @@ export class AgentRemoteApplyHarness { }) this.workSummary = this.panel.getByRole('button', { name: new RegExp( - `^${enMessages.agent.worked.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}` + `^(${[ + enMessages.agent.worked, + enMessages.agent.workedNotConfirmedOnCanvas + ] + .map(escapeRegExp) + .join('|')})` ) }) } - /** - * Makes the host refuse `doc_subscribe`, as an overloaded one does. `times` - * bounds it: a finite count is a transient overload the follower's own - * retry ladder should ride out, and the default never recovers. - */ - refuseDocSubscribes(times?: number): void { - this.hostSocket.refuseSubscribes('overloaded', times) + refuseDocSubscribes(reason = 'overloaded', times?: number): void { + this.hostSocket.refuseSubscribes(reason, times) } /** @@ -101,6 +104,10 @@ export class AgentRemoteApplyHarness { return row.locator('[class*="lucide--wrench"]') } + unconfirmedGlyph(row: Locator): Locator { + return row.locator('[class*="lucide--triangle-alert"]') + } + async openWorkSummary(): Promise { await this.workSummary.click() await expect(this.workSummary).toHaveAttribute('aria-expanded', 'true') @@ -109,10 +116,6 @@ export class AgentRemoteApplyHarness { async setUp(): Promise { const { page } = this - // Registered before `bootAgentApp` (called with `objectInfo: 'server'`) - // so it wins over the empty handler the boot mocks would otherwise - // register for the same route: Playwright runs the most-recently - // registered matching handler first. await page.route('**/api/object_info', (route) => route.fulfill(jsonRoute(agentReplayNodeDefs)) ) @@ -132,10 +135,8 @@ export class AgentRemoteApplyHarness { 'Comfy.Graph.CanvasInfo': false } }) + await loadIntoBootWorkflow(page, BLANK_WORKFLOW) - // Registered only now: the boot mocks blanket-match `**/api/userdata**` - // for every method, and the most-recently-registered route wins, so this - // has to come after them to take over the workflow-save round trip. await mockWorkflowPersistence(page, WORKFLOW_ID) } diff --git a/browser_tests/tests/agent/agentCanvasClaimNeedsObservation.spec.ts b/browser_tests/tests/agent/agentCanvasClaimNeedsObservation.spec.ts new file mode 100644 index 00000000000..49c963ee013 --- /dev/null +++ b/browser_tests/tests/agent/agentCanvasClaimNeedsObservation.spec.ts @@ -0,0 +1,114 @@ +import { expect } from '@playwright/test' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import { STALE_AFTER_MS } from '@/workbench/extensions/agent/crdt/agentCrdtDocLifecycle' + +import { agentTest as test } from '@e2e/fixtures/agentPanelFixture' +import { + AGENT_NODE_IDS, + addLoadImageNode +} from '@e2e/fixtures/data/agent/agentRemoteApply' +import { AgentRemoteApplyHarness } from '@e2e/fixtures/helpers/AgentRemoteApplyHarness' + +const COPY = enMessages.agent + +test.describe( + 'the agent may not claim a canvas it has not been observed to change', + { tag: ['@cloud', '@agent', '@vue-nodes'] }, + () => { + test.describe.configure({ timeout: 60_000 }) + + test('export and save guidance is contradicted on an empty tab', async ({ + page + }) => { + const agent = new AgentRemoteApplyHarness(page) + await agent.setUp() + await agent.openAndTarget() + await agent.sendPrompt('Build me a text-to-image workflow.') + await agent.hostSocket.waitForSubscribe() + + agent.reportToolCall('print_workflow', 'call-print') + agent.finishTurn( + 'Your nodes are ready. Open the menu to export or save the workflow.' + ) + + await agent.expectTurnReportedFinished() + await expect( + agent.panel.getByTestId('agent-run-notice-emptyCanvas') + ).toContainText(COPY.canvasIsEmptyAfterTurn) + await expect(agent.workSummary).not.toContainText( + COPY.workedNotConfirmedOnCanvas + ) + expect(await agent.settledCanvasNodeIds()).toEqual([]) + }) + + test( + 'a template the server placed but the canvas never showed is not reported as done', + { + annotation: { + type: 'source', + description: + 'https://linear.app/comfyorg/issue/PM-1343/test-agent-completes-an-image-edit-but-leaves-the-canvas-with-zero#comment-1741744b' + } + }, + async ({ page }) => { + await page.clock.install() + const agent = new AgentRemoteApplyHarness(page) + await agent.setUp() + await agent.openAndTarget() + await agent.sendPrompt('Turn this forest scene to daytime.') + await agent.hostSocket.waitForSubscribe() + + agent.reportToolCall('get_template', 'call-template') + agent.finishTurn('Your workflow is ready to run.') + + await agent.expectTurnReportedFinished() + await expect( + agent.panel.getByTestId('agent-run-notice-emptyCanvas') + ).toContainText(COPY.canvasIsEmptyAfterTurn) + + await page.clock.fastForward(STALE_AFTER_MS + 1) + + await expect(agent.workSummary).toContainText( + COPY.workedNotConfirmedOnCanvas + ) + expect(await agent.settledCanvasNodeIds()).toEqual([]) + } + ) + + test('a write the canvas took reads as done through a redelivered frame, with no notice', async ({ + page + }) => { + await page.clock.install() + const agent = new AgentRemoteApplyHarness(page) + await agent.setUp() + await agent.openAndTarget() + await agent.sendPrompt('Add a Load Image node.') + await agent.hostSocket.waitForSubscribe() + + const [nodeId] = AGENT_NODE_IDS + agent.applyAndBroadcast([addLoadImageNode(nodeId, 0)]) + await expect(agent.node(nodeId)).toBeVisible() + agent.reportToolCall('add_node', 'call-add-node') + agent.reportToolCall('add_node', 'call-add-node') + agent.finishTurn('Added a Load Image node.') + + await agent.expectTurnReportedFinished() + await expect(agent.workSummary).toContainText(COPY.worked) + await expect(agent.workSummary).not.toContainText( + COPY.workedNotConfirmedOnCanvas + ) + await expect( + agent.panel.getByTestId('agent-run-notice-emptyCanvas') + ).toHaveCount(0) + + await page.clock.fastForward(STALE_AFTER_MS + 1) + + await expect(agent.workSummary).toContainText(COPY.worked) + await expect(agent.workSummary).not.toContainText( + COPY.workedNotConfirmedOnCanvas + ) + await expect(agent.node(nodeId)).toBeVisible() + }) + } +) diff --git a/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts index 97fa7b3f51a..c00d7038364 100644 --- a/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts +++ b/browser_tests/tests/agent/agentCanvasLagsToolCallCompletion.spec.ts @@ -317,7 +317,15 @@ async function driveThroughToolCallDone( }) await expect(panel.getByRole('button', { name: SEND_LABEL })).toBeVisible() - await expect(panel.getByRole('button', { name: /^Worked/ })).toBeVisible() + await expect( + panel.getByRole('button', { + name: + order === 'docUpdateBeforeToolCall' + ? enMessages.agent.worked + : enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) + ).toBeVisible() return { vueNodes, @@ -350,7 +358,10 @@ test.describe( // the fix under test lives one level down, on the tool call's OWN // displayed state inside that summary: expand it to look. const panel = page.locator('#agent-panel-root') - const workSummary = panel.getByRole('button', { name: /^Worked/ }) + const workSummary = panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) await workSummary.click() await expect(workSummary).toHaveAttribute('aria-expanded', 'true') @@ -378,6 +389,12 @@ test.describe( 0 ) await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible() + await expect( + panel.getByRole('button', { + name: enMessages.agent.worked, + exact: true + }) + ).toBeVisible() }) test('an unrelated doc_update does not settle the correlated tool call', async ({ @@ -387,7 +404,10 @@ test.describe( await driveThroughToolCallDone(page, 'toolCallBeforeDocUpdate', true) const panel = page.locator('#agent-panel-root') - const workSummary = panel.getByRole('button', { name: /^Worked/ }) + const workSummary = panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) await workSummary.click() await expect(workSummary).toHaveAttribute('aria-expanded', 'true') @@ -424,7 +444,10 @@ test.describe( ) const panel = page.locator('#agent-panel-root') - const workSummary = panel.getByRole('button', { name: /^Worked/ }) + const workSummary = panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) await workSummary.click() await expect(workSummary).toHaveAttribute('aria-expanded', 'true') @@ -481,7 +504,10 @@ test.describe( ) const panel = page.locator('#agent-panel-root') - const workSummary = panel.getByRole('button', { name: /^Worked/ }) + const workSummary = panel.getByRole('button', { + name: enMessages.agent.worked, + exact: true + }) await workSummary.click() await expect(workSummary).toHaveAttribute('aria-expanded', 'true') diff --git a/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts b/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts index d3793549ce5..887d444d877 100644 --- a/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts +++ b/browser_tests/tests/agent/agentGraphOpLostOnDisconnect.spec.ts @@ -243,7 +243,12 @@ test.describe( await expect(panel.getByRole('button', { name: STOP_LABEL })).toHaveCount( 0 ) - await expect(panel.getByRole('button', { name: /^Worked/ })).toBeVisible() + await expect( + panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) + ).toBeVisible() await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible({ timeout: SUBSCRIBE_ACK_TIMEOUT_MS + 10_000 diff --git a/browser_tests/tests/agent/agentRefusedDocSubscribe.spec.ts b/browser_tests/tests/agent/agentRefusedDocSubscribe.spec.ts index 086c2179c77..04839027148 100644 --- a/browser_tests/tests/agent/agentRefusedDocSubscribe.spec.ts +++ b/browser_tests/tests/agent/agentRefusedDocSubscribe.spec.ts @@ -1,5 +1,7 @@ import { expect } from '@playwright/test' +import enMessages from '@/locales/en/main.json' with { type: 'json' } + import { agentTest as test } from '@e2e/fixtures/agentPanelFixture' import { AGENT_NODE_IDS, @@ -17,7 +19,7 @@ test.describe( page }) => { const agent = new AgentRemoteApplyHarness(page) - agent.refuseDocSubscribes(2) + agent.refuseDocSubscribes('overloaded', 2) await agent.setUp() await agent.openAndTarget() await agent.sendPrompt('add a load image node') @@ -36,11 +38,11 @@ test.describe( ).toBe(2) }) - test('reports a canvas edit as succeeded when the document never arrived', async ({ + test('reports a canvas edit as unconfirmed when the document never arrived', async ({ page }) => { const agent = new AgentRemoteApplyHarness(page) - agent.refuseDocSubscribes() + agent.refuseDocSubscribes('unsupported') await agent.setUp() await agent.openAndTarget() await agent.sendPrompt('add a load image node') @@ -56,6 +58,9 @@ test.describe( await expect( agent.panel.getByText('Added a Load Image node.').first() ).toBeVisible() + await expect(agent.workSummary).toContainText( + enMessages.agent.workedNotConfirmedOnCanvas + ) await expect(agent.node(nodeId)).toHaveCount(0) expect(await agent.settledCanvasNodeIds()).toEqual(before) @@ -65,15 +70,8 @@ test.describe( await expect(addNodeRow).toBeVisible() await expect(agent.streamingGlyph(addNodeRow)).toHaveCount(0) - - // Expected failure: that terminal state must not read as a plain - // success while the canvas has nothing to show for the call. - // `agentToolGlyph.ts` already renders `circle-x` for a failed call, so a - // fix has a rendering to reach for; this asserts the absence of the - // success glyph rather than the presence of any particular replacement, - // so it does not prescribe which one. - test.fail() await expect(agent.settledSuccessGlyph(addNodeRow)).toHaveCount(0) + await expect(agent.unconfirmedGlyph(addNodeRow)).toBeVisible() }) } ) diff --git a/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts b/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts index 1936bc7ba4d..ba7439ff928 100644 --- a/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts +++ b/browser_tests/tests/agent/agentSchemaSkewTellsTheUser.spec.ts @@ -207,7 +207,12 @@ test.describe( await expect(panel.getByRole('button', { name: STOP_LABEL })).toHaveCount( 0 ) - await expect(panel.getByRole('button', { name: /^Worked/ })).toBeVisible() + await expect( + panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) + ).toBeVisible() await expect .poll(() => committedNodeIds, { timeout: 20_000 }) diff --git a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts index a11d32346e5..17f085ef1e5 100644 --- a/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts +++ b/browser_tests/tests/agent/agentToolCallHistoryPersistence.spec.ts @@ -28,7 +28,7 @@ const TRACE = [ THINKING_ROUND_1, 'Search nodes', THINKING_ROUND_2, - 'Add node', + enMessages.agent.toolNotConfirmedOnCanvas.replace('{tool}', 'Add node'), THINKING_FINAL ] @@ -148,7 +148,7 @@ test( await expect(reopenedPanel).toBeVisible({ timeout: 30_000 }) const summary = reopenedPanel.getByRole('button', { - name: enMessages.agent.worked, + name: enMessages.agent.workedNotConfirmedOnCanvas, exact: true }) await expect(summary).toBeVisible({ timeout: 10_000 }) diff --git a/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts b/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts index 4ef6fbac2ec..a81b5a286a1 100644 --- a/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts +++ b/browser_tests/tests/agent/agentZeroNodesOnUnreadableSchema.spec.ts @@ -254,7 +254,12 @@ test.describe( await expect(panel.getByRole('button', { name: STOP_LABEL })).toHaveCount( 0 ) - await expect(panel.getByRole('button', { name: /^Worked/ })).toBeVisible() + await expect( + panel.getByRole('button', { + name: enMessages.agent.workedNotConfirmedOnCanvas, + exact: true + }) + ).toBeVisible() await expect(vueNodes.getNodeLocator(String(ADDED_NODE_ID))).toBeVisible() }) diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 5796fdb872f..dc2304c322a 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -1436,11 +1436,6 @@ "count": 1 } }, - "src/workbench/extensions/agent/services/agent/agentToolGlyph.ts": { - "comfy/no-positional-boolean-params": { - "count": 1 - } - }, "src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts": { "comfy/prefer-array-containing": { "count": 1 diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 4e27b75343d..274602224f0 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5824,6 +5824,7 @@ "viewingDifferentWorkflow": "You’re viewing a different workflow from the target.", "showTarget": "Show target", "targetWorkflowUnavailable": "The target workflow is no longer available.", + "canvasIsEmptyAfterTurn": "This canvas is empty — nothing was added to it. There is nothing here to run, save or export yet.", "targetWorkflowOpenFailed": "Couldn’t open the target workflow.", "showTargetWorkflow": "Show target workflow: {workflowName}", "runApproval": { @@ -5999,6 +6000,8 @@ "helpful": "Helpful", "notHelpful": "Not helpful", "worked": "Worked", + "workedNotConfirmedOnCanvas": "Changes not confirmed on your canvas", + "toolNotConfirmedOnCanvas": "{tool} — not confirmed on your canvas", "workedForSeconds": "Worked for {seconds} seconds", "workedForMinutes": "Worked for {minutes}m {seconds}s", "copy": "Copy", diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 472d9e881f4..fbd4e52e051 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -6358,9 +6358,8 @@ describe('AgentPanelRoot lifecycle', () => { // PM-1575: the gate is reset to the always-safe default even though a // step three places ahead of it threw. expect(setCanvasSyncGate).toHaveBeenCalledOnce() - const [gate, outcomeCount] = setCanvasSyncGate.mock.lastCall ?? [] - expect(gate?.()).toBe(false) - expect(outcomeCount?.()).toBe(0) + const [gate] = setCanvasSyncGate.mock.lastCall ?? [] + expect(gate?.()).toBe('none') expect(reportError).toHaveBeenCalledWith( expect.objectContaining({ message: 'detach failed' }), expect.objectContaining({ @@ -6399,9 +6398,8 @@ describe('AgentPanelRoot lifecycle', () => { panel.unmount() expect(setCanvasSyncGate).toHaveBeenCalledOnce() - const [gate, outcomeCount] = setCanvasSyncGate.mock.lastCall ?? [] - expect(gate?.()).toBe(false) - expect(outcomeCount?.()).toBe(0) + const [gate] = setCanvasSyncGate.mock.lastCall ?? [] + expect(gate?.()).toBe('none') expect(errorHandler).not.toHaveBeenCalled() }) @@ -9484,6 +9482,152 @@ describe('AgentPanelRoot workflow binding', () => { ).toBeInTheDocument() }) + it('marks a mismatched or switched-away canvas unavailable', async () => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + const setCanvasSyncGate = vi.spyOn( + useAgentConversationStore(), + 'setCanvasSyncGate' + ) + + await renderAndSend('add a node') + + const [gate] = setCanvasSyncGate.mock.calls.at(-1) ?? [] + expect(gate?.('wf-other')).toBe('unavailable') + + workflowStore.activeWorkflow = addTab('workflows/other.json') + await nextTick() + + expect(gate?.('wf-42')).toBe('unavailable') + }) + + it('clears an empty-canvas correction when the thread changes', async () => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + + await renderAndSend('inspect this canvas') + dispatchApiEvent('agent_tool_call', { + tool_call_id: 'call-print-workflow', + tool_name: 'print_workflow', + status: 'success', + duration_ms: 90, + thread_id: 'th-1', + message_id: 'm-1' + }) + dispatchApiEvent('agent_message_done', { + message_id: 'm-1', + thread_id: 'th-1' + }) + + expect( + await screen.findByText(i18n.global.t('agent.canvasIsEmptyAfterTurn')) + ).toBeVisible() + + useAgentConversationStore().setThreadId('th-next') + await nextTick() + + expect( + screen.queryByText(i18n.global.t('agent.canvasIsEmptyAfterTurn')) + ).not.toBeInTheDocument() + }) + + type ToolOutcomes = [string, 'success' | 'error'][] + + function reportTools(tools: ToolOutcomes): void { + tools.forEach(([toolName, status], index) => { + dispatchApiEvent('agent_tool_call', { + tool_call_id: `call-${index}-${toolName}`, + tool_name: toolName, + status, + duration_ms: 90, + thread_id: 'th-1', + message_id: 'm-1' + }) + }) + } + + it.for([ + { name: 'a single clear', tools: [['clear_canvas', 'success']] }, + { + name: 'an inspect then clear', + tools: [ + ['print_workflow', 'success'], + ['clear_canvas', 'success'] + ] + }, + { + name: 'an add then clear', + tools: [ + ['add_node', 'success'], + ['clear_canvas', 'success'] + ] + } + ] satisfies { name: string; tools: ToolOutcomes }[])( + 'does not contradict an empty canvas after $name', + async ({ tools }) => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + + await renderAndSend('empty the canvas') + reportTools(tools) + dispatchApiEvent('agent_message_done', { + message_id: 'm-1', + thread_id: 'th-1' + }) + + expect( + await screen.findByRole('button', { + name: i18n.global.t('agent.worked') + }) + ).toBeVisible() + expect( + screen.queryByText(i18n.global.t('agent.canvasIsEmptyAfterTurn')) + ).not.toBeInTheDocument() + } + ) + + it.for([ + { + name: 'content was added after a clear', + tools: [ + ['clear_canvas', 'success'], + ['add_node', 'success'] + ] + }, + { + name: 'a clear after an add failed', + tools: [ + ['add_node', 'success'], + ['clear_canvas', 'error'] + ] + }, + { + name: 'nodes were added and one was deleted', + tools: [ + ['add_node', 'success'], + ['add_node', 'success'], + ['delete_node', 'success'] + ] + } + ] satisfies { name: string; tools: ToolOutcomes }[])( + 'flags an empty canvas when $name', + async ({ tools }) => { + makeTab('wf-42') + mockMessagesEndpoint('wf-42') + + await renderAndSend('build on the canvas') + reportTools(tools) + dispatchApiEvent('agent_message_done', { + message_id: 'm-1', + thread_id: 'th-1' + }) + + expect( + await screen.findByText(i18n.global.t('agent.canvasIsEmptyAfterTurn')) + ).toBeVisible() + } + ) + it('shows a sticky error toast when the doc-host permanently refuses the subscribe', async () => { makeTab('wf-42') mockMessagesEndpoint('wf-42') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 26278d564b8..73b51090f1e 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -2,7 +2,7 @@ import './agentPanel.css' import type { AgentAnswerRequest } from '@comfyorg/ingest-types' -import { useClipboard } from '@vueuse/core' +import { useClipboard, useEventListener } from '@vueuse/core' import { storeToRefs } from 'pinia' import { computed, @@ -139,6 +139,9 @@ import { toAgentPaywallCta, toAgentPaywallReason } from './services/agent/agentPaywallPresentation' +import type { CanvasConfirmation } from './services/agent/agentEventTransport' +import { toolCanClaimCanvasContent } from './services/agent/agentToolClassification' +import { areAgentWorkflowIdsEquivalent } from './services/agent/agentWorkflowId' import { createAgentEventSource } from './services/agent/agentEventSource' import { resolveAgentIdentity } from './services/agent/agentIdentity' import { useAgentChatHistoryStore } from './stores/agent/agentChatHistoryStore' @@ -1089,10 +1092,22 @@ const isCrdtDevPanelEnabled = resolveDebugPanelEnabled( ) const { activeTurnId: conversationTurnId } = storeToRefs(conversationStore) +function canvasConfirmationState(workflowId?: string): CanvasConfirmation { + const bound = boundWorkflowId.value ?? undefined + if (bound === undefined) return 'none' + if ( + workflowId !== undefined && + !areAgentWorkflowIdsEquivalent(bound, workflowId) + ) + return 'unavailable' + if (!isBoundWorkflowActive.value) return 'unavailable' + const status = crdtStatus.value + if (!status.enabled) return 'none' + if (status.connected) return 'awaiting' + return status.terminal === null ? 'none' : 'unavailable' +} conversationStore.setCanvasSyncGate( - (workflowId) => - crdtStatus.value.connected && - (workflowId === undefined || workflowId === crdtStatus.value.workflowId), + canvasConfirmationState, () => crdtStatus.value.outcomes.appliedLive ) @@ -1116,6 +1131,56 @@ function resumedTurnTabPath(): string | null { return context?.id === bound ? context.tabPath : null } +const boundCanvasNodeCount = ref(0) +function readBoundCanvasNodeCount(): void { + boundCanvasNodeCount.value = isBoundWorkflowActive.value + ? (workflowStore.activeWorkflow?.activeState?.nodes?.length ?? 0) + : 0 +} +watch([boundWorkflowId, isBoundWorkflowActive], readBoundCanvasNodeCount, { + immediate: true +}) +useEventListener( + api, + 'graphChanged', + (event: CustomEvent) => { + if (isBoundWorkflowActive.value) + boundCanvasNodeCount.value = event.detail.nodes?.length ?? 0 + } +) + +const completedTurnClaimedCanvasContent = ref(false) +function completedTurnCanClaimCanvasContent(): boolean { + const lastCanvasTool = conversationMessages.value + .at(-1) + ?.parts.findLast( + (part) => + part.type === 'tool' && + (part.name === 'clear_canvas' + ? part.ok === true + : part.ok !== false && toolCanClaimCanvasContent(part.name)) + ) + return ( + lastCanvasTool?.type === 'tool' && lastCanvasTool.name !== 'clear_canvas' + ) +} +const canvasEmptyAfterTurn = computed( + () => + completedTurnClaimedCanvasContent.value && + isBoundWorkflowActive.value && + boundCanvasNodeCount.value === 0 +) + +function refreshBillingAfterTurn(): void { + if (billingType.value !== 'workspace') return + void refreshBillingStatus().catch((error: unknown) => { + reportError(error, { + surface: 'agent', + errorType: 'error_refreshing_agent_billing_status' + }) + }) +} + // Adoption (onWorkflowAdopted) and tab activation (onAgentActiveTab) are the // primary spinner setters; the non-idle branch only re-arms it after the // stash/resume flip of a panel remount, where those setters never run. @@ -1123,33 +1188,32 @@ let wasTurnActive = false watch( [status, conversationTurnId], ([value, turnId]) => { - const completedTurn = wasTurnActive && value === 'idle' - if (value === 'idle') { - // The immediate idle value on remount is a hydration snapshot, not a - // completed turn. A real idle transition is observed after this pass. - if (completedTurn) graphActivity.finishTurn() - } else graphActivity.startTurn(turnId) - wasTurnActive = value !== 'idle' - if (value === 'idle') { - // A server-side LLM-hop refusal does not reach the browser as a 402. - // Refresh the authoritative effective-funds verdict after every observed - // turn completion so both exhaustion and external top-ups converge. - if (completedTurn && billingType.value === 'workspace') { - void refreshBillingStatus().catch((error: unknown) => { - reportError(error, { - surface: 'agent', - errorType: 'error_refreshing_agent_billing_status' - }) - }) - } - const completedPath = tabActivity.editingTabPath - tabActivity.setEditing(null) - if (completedPath !== null) tabActivity.markModified(completedPath) - } else if (tabActivity.editingTabPath === null) - tabActivity.setEditing(resumedTurnTabPath()) + const active = value !== 'idle' + const completedTurn = wasTurnActive && !active + if (completedTurn) + completedTurnClaimedCanvasContent.value = + completedTurnCanClaimCanvasContent() + else if (active) completedTurnClaimedCanvasContent.value = false + wasTurnActive = active + if (active) { + graphActivity.startTurn(turnId) + if (tabActivity.editingTabPath === null) + tabActivity.setEditing(resumedTurnTabPath()) + return + } + if (completedTurn) { + graphActivity.finishTurn() + refreshBillingAfterTurn() + } + const completedPath = tabActivity.editingTabPath + tabActivity.setEditing(null) + if (completedPath !== null) tabActivity.markModified(completedPath) }, { immediate: true, flush: 'sync' } ) +watch([threadId, boundWorkflowId], () => { + completedTurnClaimedCanvasContent.value = false +}) const executionErrorStore = useExecutionErrorStore() @@ -1433,10 +1497,7 @@ onBeforeUnmount(() => { // turn resumed in the meantime reading it -- reset to the always-safe // default instead of leaving whatever this instance last set. resetCanvasSyncGate: () => { - conversationStore.setCanvasSyncGate( - () => false, - () => 0 - ) + conversationStore.setCanvasSyncGate(() => 'none') } }) }) @@ -2107,6 +2168,7 @@ onMounted(() => agentPanelRenderStore.reach('drawn')) :select-tab="onSelectWorkflowTarget" :workflow-detached="workflowDetached" :target-unavailable="agentPanelStore.targetUnavailable" + :canvas-empty-after-turn="canvasEmptyAfterTurn" :get-mention-nodes="mentionableNodes" :paywall-presentation="paywallPresentation" :credits-exhausted="showStandingPaywall" diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.test.ts b/src/workbench/extensions/agent/components/agent/AgentPanel.test.ts index 4ffa8823519..4a174575c0d 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.test.ts +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.test.ts @@ -545,6 +545,29 @@ describe('AgentPanel', () => { ) }) + it('prioritizes an empty-canvas correction over target availability', () => { + render(AgentPanel, { + props: { + entries: [], + historyGroups: createHistoryGroups(), + activeTab: { path: 'workflows/portrait.json', name: 'portrait' }, + canvasEmptyAfterTurn: true, + targetUnavailable: true + }, + global: { + plugins: [i18n], + stubs: { Composer: true, EmptyState: true, PanelHeader: true } + } + }) + + expect(screen.getByRole('note')).toHaveTextContent( + i18n.global.t('agent.canvasIsEmptyAfterTurn') + ) + expect(screen.getByRole('note')).not.toHaveTextContent( + i18n.global.t('agent.targetWorkflowUnavailable') + ) + }) + it('groups chat options with the title and separates history navigation', () => { const title = 'A comfortably short title' render(AgentPanel, { diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.vue b/src/workbench/extensions/agent/components/agent/AgentPanel.vue index 16c28555c1b..e57523872df 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.vue +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.vue @@ -70,6 +70,7 @@ const { selectTab = async () => false, workflowDetached = false, targetUnavailable = false, + canvasEmptyAfterTurn = false, getMentionNodes = () => [], paywallPresentation = DEFAULT_AGENT_PAYWALL_PRESENTATION, creditsExhausted = false, @@ -107,6 +108,7 @@ const { selectTab?: (path: string) => Promise workflowDetached?: boolean targetUnavailable?: boolean + canvasEmptyAfterTurn?: boolean getMentionNodes?: () => SelectedNode[] paywallPresentation?: AgentPaywallPresentation /** @@ -164,6 +166,7 @@ const emit = defineEmits<{ }>() const targetNotice = computed(() => { + if (canvasEmptyAfterTurn) return 'emptyCanvas' if (targetUnavailable) return 'unavailable' if (workflowDetached || activeTab === null) return undefined if (visibleTabPath !== null && visibleTabPath !== activeTab.path) diff --git a/src/workbench/extensions/agent/components/agent/RunNoticeBanner.vue b/src/workbench/extensions/agent/components/agent/RunNoticeBanner.vue index ad4b226fdfd..15788de1f6c 100644 --- a/src/workbench/extensions/agent/components/agent/RunNoticeBanner.vue +++ b/src/workbench/extensions/agent/components/agent/RunNoticeBanner.vue @@ -13,7 +13,7 @@ const { expanded?: boolean workflowName?: string genericNoticeReady?: boolean - context?: 'following' | 'mismatch' | 'unavailable' + context?: 'following' | 'mismatch' | 'unavailable' | 'emptyCanvas' }>() const emit = defineEmits<{ showTarget: [] }>() @@ -21,7 +21,8 @@ const dismissed = useStorage('Comfy.AgentPanel.runNoticeDismissed', false) const contextMessages = { following: 'agent.targetFollowsVisibleWorkflow', mismatch: 'agent.viewingDifferentWorkflow', - unavailable: 'agent.targetWorkflowUnavailable' + unavailable: 'agent.targetWorkflowUnavailable', + emptyCanvas: 'agent.canvasIsEmptyAfterTurn' } as const satisfies Record, string> const defaultNotice = computed(() => expanded ? 'agent.runNoticeExpanded' : 'agent.runNotice' @@ -37,6 +38,7 @@ const visible = computed(
diff --git a/src/workbench/extensions/agent/components/agent/message/ActivityTrace.vue b/src/workbench/extensions/agent/components/agent/message/ActivityTrace.vue index 0e464a0b662..e0440b1952b 100644 --- a/src/workbench/extensions/agent/components/agent/message/ActivityTrace.vue +++ b/src/workbench/extensions/agent/components/agent/message/ActivityTrace.vue @@ -10,6 +10,7 @@ import type { ActivityPart, PartState } from '@/workbench/extensions/agent/services/agent/agentMessageParts' +import { isUnconfirmedCanvasWrite } from '@/workbench/extensions/agent/services/agent/agentMessageParts' import { toolGlyph, toolLabel @@ -35,10 +36,14 @@ function labelClass(state: PartState): string { function glyphOf(row: ActivityRow): string { return row.kind === 'thinking' ? 'icon-[lucide--brain]' - : toolGlyph(row.name, row.state, row.ok) + : toolGlyph(row.name, row.state, row) } function labelOf(row: Extract): string { + if (isUnconfirmedCanvasWrite(row)) + return t('agent.toolNotConfirmedOnCanvas', { + tool: toolLabel(row.name, row.state, t) + }) if (row.name === 'load_skill' && row.skill) { const label = row.state === 'streaming' @@ -51,8 +56,6 @@ function labelOf(row: Extract): string { return toolLabel(row.name, row.state, t) } -// Every part object is rebuilt on each token, so a settled row is only -// recognisable as unchanged by its contents. function rowSignature(row: ActivityRow): string { return row.kind === 'tool' ? JSON.stringify([ @@ -61,6 +64,7 @@ function rowSignature(row: ActivityRow): string { row.skill, row.state, row.ok, + row.canvasConfirmed, row.count ]) : `think:${row.state}:${row.text}` diff --git a/src/workbench/extensions/agent/components/agent/message/WorkSummary.test.ts b/src/workbench/extensions/agent/components/agent/message/WorkSummary.test.ts index ade2adfa84b..52d6de7aebb 100644 --- a/src/workbench/extensions/agent/components/agent/message/WorkSummary.test.ts +++ b/src/workbench/extensions/agent/components/agent/message/WorkSummary.test.ts @@ -300,6 +300,32 @@ describe('WorkSummary', () => { screen.getByRole('button', { name: /^worked$/i }) ).toBeInTheDocument() }) + + it('does not claim completed work while a successful canvas write awaits observation', () => { + render(WorkSummary, { + props: { parts: [tool('c1', 'add_node', 'streaming', true)] }, + global: { plugins: [i18n] } + }) + + expect( + screen.getByRole('button', { name: /not confirmed on your canvas/i }) + ).toBeInTheDocument() + }) + + it('warns when a successful canvas write settled without observation', () => { + render(WorkSummary, { + props: { + parts: [ + { ...tool('c1', 'add_node', 'done', true), canvasConfirmed: false } + ] + }, + global: { plugins: [i18n] } + }) + + expect( + screen.getByRole('button', { name: /not confirmed on your canvas/i }) + ).toBeInTheDocument() + }) }) describe('step entrance', () => { diff --git a/src/workbench/extensions/agent/components/agent/message/WorkSummary.vue b/src/workbench/extensions/agent/components/agent/message/WorkSummary.vue index 1e33fd9a57b..cb068819ac8 100644 --- a/src/workbench/extensions/agent/components/agent/message/WorkSummary.vue +++ b/src/workbench/extensions/agent/components/agent/message/WorkSummary.vue @@ -4,15 +4,29 @@ import { CollapsibleRoot, CollapsibleTrigger } from 'reka-ui' +import { computed } from 'vue' import { useI18n } from 'vue-i18n' import type { ActivityPart } from '@/workbench/extensions/agent/services/agent/agentMessageParts' +import { isUnconfirmedCanvasWrite } from '@/workbench/extensions/agent/services/agent/agentMessageParts' +import { toolCanMutateCanvas } from '@/workbench/extensions/agent/services/agent/agentToolClassification' import ActivityTrace from './ActivityTrace.vue' const { parts } = defineProps<{ parts: readonly ActivityPart[] }>() const { t } = useI18n() + +const unconfirmedCanvasWrite = computed(() => + parts.some( + (part) => + part.type === 'tool' && + (isUnconfirmedCanvasWrite(part) || + (part.ok === true && + part.state === 'streaming' && + toolCanMutateCanvas(part.name))) + ) +)