From d2540d93c4e83ef5824e050b054d7e4d4dd3739b Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 26 Oct 2026 22:17:58 +0000 Subject: [PATCH 1/4] test(agent): prove a document reset keeps other workflows' mint cursors (#21964) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Black-box proof for #19871: resetting one workflow's document must not reset the human op sender's mint cursor for another workflow. The case edits workflow B, visits and resets workflow A, returns to B, and edits again before B's resubscribe is acknowledged; the second B edit must carry a higher `base_version` than the first. Test and harness only — no production change. Supersedes #19876, whose three production commits already merged in #19871 and whose harness no longer applied to the redesigned `AgentFollowerHostSocket` on `main`. ## Changes - `AgentFollowerHostSocket` can serve several documents on one routed socket (`addWorkflow`), hold one workflow's subscribe answer (`pauseSubscribes` / `resumeSubscribe`), record each op's wire `base_version`, count subscribes per workflow, and judge or release a human batch against the document that sent it. Existing single-document behaviour is unchanged. - `AgentConversationHarness` gains `activateEmptyWorkflow`, `sendDocumentReset`, and `activateRecordedWorkflowBeforeSubscribeAck`. - New spec `browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts`. ## Verification - `pnpm exec playwright test browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts --project=cloud --repeat-each=3`: 3/3 pass on this head. - Mutation check: replacing the per-workflow `lastMintedVersions.delete(resetWorkflowId)` in `opSender.abortAll` with `lastMintedVersions.clear()` fails the spec (`Expected: > 1, Received: 0`). - The 25 `@cloud` agent specs that use the routed host's subscribe, hold/release, or refusal controls: 46/46 tests pass; two failed once on a cold dev server with two workers and passed on rerun. - `vitest run browser_tests/fixtures/agentFollowerHostSocket.test.ts`: 17/17. `pnpm typecheck` and `pnpm typecheck:browser` clean. --------- Co-authored-by: Alexander Brown Co-authored-by: mrseek-reviews[bot] <337131926+mrseek-reviews[bot]@users.noreply.github.com> Co-authored-by: GitHub Action (cherry picked from commit a421e90d5ae717c18dae8aec800948708c249af3) --- .../fixtures/agentConversationFixture.ts | 89 +++++++++++- .../fixtures/agentFollowerHostSocket.test.ts | 52 +++++++ .../fixtures/agentFollowerHostSocket.ts | 135 +++++++++++++----- ...ntHumanEditAfterOtherWorkflowReset.spec.ts | 74 ++++++++++ 4 files changed, 315 insertions(+), 35 deletions(-) create mode 100644 browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index fe878bcae85..207d5571062 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -16,7 +16,10 @@ import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' import { parseAgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { parseServerDocFrame } from '@/workbench/extensions/agent/crdt/docFrameClient' +import { + DOC_PROTOCOL_VERSION, + parseServerDocFrame +} from '@/workbench/extensions/agent/crdt/docFrameClient' import type { GraphOperation } from '@/workbench/extensions/agent/crdt/graphOperations' import type { useAgentWorkflowTabBindingStore } from '@/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore' @@ -771,6 +774,67 @@ export class AgentConversationHarness { return this.hostSocket.clientDocFrames() } + async activateEmptyWorkflow(workflowId: string, name: string): Promise { + const seed = structuredClone(this.conversation.workflow.seed) + seed.nodes = [] + seed.links = [] + seed.groups = [] + this.hostSocket.addWorkflow( + workflowId, + new HostDoc(workflowId, seed, this.conversation.workflow.catalog) + ) + const subscribes = this.subscribeCount(workflowId) + this.hostSocket.send({ + type: 'agent_active_tab', + data: { workflow_id: workflowId, name } + }) + await expect(this.topbar.getActiveTab()).toContainText(name) + await expect + .poll(() => this.subscribeCount(workflowId)) + .toBeGreaterThanOrEqual(subscribes + 1) + } + + resumeWorkflowSubscribe(workflowId: string): void { + this.hostSocket.resumeSubscribe(workflowId) + } + + sendDocumentReset(workflowId: string): void { + this.hostSocket.send({ + type: 'doc_reset', + data: { + v: DOC_PROTOCOL_VERSION, + workflow_id: workflowId, + seq: 1, + lineage_seq: 1, + actor: 'system:test-reset' + } + }) + } + + /** Returns to the recorded workflow's tab (the first) and holds its subscribe ack. */ + async activateRecordedWorkflowBeforeSubscribeAck(): Promise { + const { id, name } = this.conversation.workflow + this.hostSocket.pauseSubscribes(id) + const sent = this.clientDocFrames().filter( + (frame) => frame.type === 'doc_subscribe' && frame.workflowId === id + ).length + this.hostSocket.send({ + type: 'agent_active_tab', + data: { workflow_id: id, name } + }) + await expect + .poll( + () => + this.clientDocFrames().filter( + (frame) => frame.type === 'doc_subscribe' && frame.workflowId === id + ).length + ) + .toBeGreaterThanOrEqual(sent + 1) + await expect( + this.topbar.getTab(0).and(this.topbar.getActiveTab()) + ).toBeVisible() + } + /** The applier's verdict on every human op the host has judged so far. */ humanOpOutcomes(): ApplyOutcome[] { return this.hostSocket.humanOpOutcomes() @@ -840,14 +904,33 @@ export class AgentConversationHarness { // Rises once per follower subscribe; a tab return re-subscribes and the // host answers with the catch-up frame this counter has just sent. - subscribeCount(): number { - return this.hostSocket.subscribeCount() + subscribeCount(workflowId?: string): number { + return this.hostSocket.subscribeCount(workflowId) } humanOpFrames(): ClientDocFrame[] { return this.clientDocFrames().filter((frame) => frame.type === 'doc_ops') } + async humanAddNodeBaseVersion( + workflowId: string, + nodeId: string + ): Promise { + const find = () => { + for (const frame of this.humanOpFrames()) { + if (frame.workflowId !== workflowId) continue + const index = frame.ops.indexOf(`add_node:${nodeId}`) + if (index !== -1) return frame.baseVersions[index] + } + return undefined + } + await expect.poll(find).toEqual(expect.any(Number)) + const baseVersion = find() + if (typeof baseVersion !== 'number') + throw new Error(`no numeric base_version for add_node:${nodeId}`) + return baseVersion + } + leaveNextHumanBatchUnanswered(): void { this.hostSocket.leaveHumanBatchesUnanswered(1) } diff --git a/browser_tests/fixtures/agentFollowerHostSocket.test.ts b/browser_tests/fixtures/agentFollowerHostSocket.test.ts index 7117e4acc2d..fe108393d50 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.test.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.test.ts @@ -127,6 +127,30 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { }) }) + it('records each op base_version, null when it is not a number', async () => { + const { hostSocket, emitClientFrame } = await installHost() + + const numbered = validOp('b'.repeat(32), 2) + const missing = validOp('c'.repeat(32), 3) + const malformed = validOp('d'.repeat(32), 4) + delete missing.base_version + malformed.base_version = 'not-a-number' + emitClientFrame({ + type: 'doc_ops', + data: { + workflow_id: WORKFLOW_ID, + tab: 'tab-1', + ops: [numbered, missing, malformed] + } + }) + + expect(hostSocket.clientDocFrames().at(-1)?.baseVersions).toEqual([ + 1, + null, + null + ]) + }) + it('still applies a well-formed doc_ops batch through the real applier', async () => { const { hostSocket, emitClientFrame, sentFrames } = await installHost() @@ -175,6 +199,34 @@ describe('AgentFollowerHostSocket human doc_ops handling', () => { node: { id: nodeId, type: 'TestNode', pos: [10, 20] } }) + it('releases a held batch against the workflow that sent it', async () => { + const { + host: primaryHost, + hostSocket, + emitClientFrame + } = await installHost('hold') + const secondaryWorkflowId = 'wf-2' + const secondaryHost = new HostDoc( + secondaryWorkflowId, + { nodes: [], links: [] }, + { types: {} } + ) + hostSocket.addWorkflow(secondaryWorkflowId, secondaryHost) + + emitClientFrame({ + type: 'doc_ops', + data: { + workflow_id: secondaryWorkflowId, + tab: 'tab-1', + ops: [validOp('c'.repeat(32), 3)] + } + }) + hostSocket.releaseHeldClientOps() + + expect(primaryHost.graph().nodes).toEqual({}) + expect(secondaryHost.graph().nodes).toHaveProperty('3') + }) + it.for([ { label: 'a missing tab', diff --git a/browser_tests/fixtures/agentFollowerHostSocket.ts b/browser_tests/fixtures/agentFollowerHostSocket.ts index c7572dc86e3..b605918595a 100644 --- a/browser_tests/fixtures/agentFollowerHostSocket.ts +++ b/browser_tests/fixtures/agentFollowerHostSocket.ts @@ -68,6 +68,8 @@ export interface ClientDocFrame { /** `op:node_id` per op for a `doc_ops` frame; empty otherwise. */ ops: string[] opIds: string[] + /** Wire `base_version` per op for a `doc_ops` frame; `null` when not a number. */ + baseVersions: Array writers: Array connection: number comfyConnection: number @@ -135,6 +137,9 @@ function parseClientDocFrame( } export class AgentFollowerHostSocket { + private readonly hosts = new Map() + private readonly pausedSubscribes = new Set() + private readonly pendingSubscribes = new Map() private refuseReason: string | null = null private refusalsLeft = 0 private refusedSubscribes = 0 @@ -146,7 +151,7 @@ export class AgentFollowerHostSocket { private comfyConnections = 0 private connections = 0 private unansweredHumanBatches = 0 - private subscribes = 0 + private readonly subscribes = new Map() private subscribeAnswer = (stateVector: string): HostFrame[] => [ this.host.subscribed(), this.host.catchUp(stateVector) @@ -154,6 +159,7 @@ export class AgentFollowerHostSocket { private readonly createdAt = Date.now() private readonly clientFrames: ClientDocFrame[] = [] private readonly heldBatches: Array<{ + workflowId: string tab: string | null ops: WireOpEnvelope[] }> = [] @@ -171,7 +177,27 @@ export class AgentFollowerHostSocket { private readonly host: HostDoc, private readonly humanOpsHost: HumanOpsHost = 'hold', private readonly hostUpdateSink?: HostUpdateSink - ) {} + ) { + this.hosts.set(workflowId, host) + } + + addWorkflow(workflowId: string, host: HostDoc): void { + this.hosts.set(workflowId, host) + } + + /** Holds subscribe answers for `workflowId` until {@link resumeSubscribe}. */ + pauseSubscribes(workflowId: string): void { + this.pausedSubscribes.add(workflowId) + } + + /** Ends the hold and answers the newest subscribe held by {@link pauseSubscribes}. */ + resumeSubscribe(workflowId: string): void { + this.pausedSubscribes.delete(workflowId) + const stateVector = this.pendingSubscribes.get(workflowId) + if (stateVector === undefined) return + this.pendingSubscribes.delete(workflowId) + this.answerSubscribe(workflowId, stateVector) + } /** Pins the `status` sid; `''` models a socket with no id. */ reportSocketSid(sid: string): this { @@ -284,11 +310,11 @@ export class AgentFollowerHostSocket { const frame = parseClientDocFrame(raw) if (!frame) return this.recordClientFrame(frame) - if (frame.workflowId !== this.workflowId) { + if (frame.workflowId === null || !this.hosts.has(frame.workflowId)) { this.rejectForeignOps(frame) return } - this.routeClientDocFrame(frame) + this.routeClientDocFrame(frame, frame.workflowId) } private recordClientFrame(frame: ParsedClientDocFrame): void { @@ -302,31 +328,53 @@ export class AgentFollowerHostSocket { knownLineageSeq: frame.knownLineageSeq, ops: ops.map((op) => opLabel(op)), opIds: ops.map((op) => op.op_id), + baseVersions: ops.map((op) => + 'base_version' in op && typeof op.base_version === 'number' + ? op.base_version + : null + ), writers: ops.map(claimedWriter), connection: this.connections, comfyConnection: this.comfyConnections }) } - private routeClientDocFrame(frame: ParsedClientDocFrame): void { + private routeClientDocFrame( + frame: ParsedClientDocFrame, + workflowId: string + ): void { if (frame.type === 'doc_subscribe' && frame.stateVector !== null) { - this.answerSubscribe(frame.stateVector) + if (this.pausedSubscribes.has(workflowId)) { + this.pendingSubscribes.set(workflowId, frame.stateVector) + return + } + this.answerSubscribe(workflowId, frame.stateVector) return } - if (frame.type === 'doc_ops') this.receiveHumanOps(frame) + if (frame.type === 'doc_ops') this.receiveHumanOps(frame, workflowId) } - private receiveHumanOps(frame: ParsedClientDocFrame): void { - if (this.unansweredHumanBatches > 0 && this.wouldAcceptHumanOps(frame)) { + private receiveHumanOps( + frame: ParsedClientDocFrame, + workflowId: string + ): void { + if ( + this.unansweredHumanBatches > 0 && + this.wouldAcceptHumanOps(frame, workflowId) + ) { this.unansweredHumanBatches -= 1 return } if (this.humanOpsHost === 'apply') { - this.judgeHumanOps(frame.opsResult, frame.tab) + this.judgeHumanOps(workflowId, frame.opsResult, frame.tab) return } if (frame.opsResult.ok) { - this.heldBatches.push({ tab: frame.tab, ops: frame.opsResult.ops }) + this.heldBatches.push({ + workflowId, + tab: frame.tab, + ops: frame.opsResult.ops + }) } } @@ -339,7 +387,11 @@ export class AgentFollowerHostSocket { releaseHeldClientOps(): WireOpEnvelope[] { const batch = this.heldBatches.shift() if (!batch) return [] - this.judgeHumanOps({ ok: true, ops: batch.ops }, batch.tab) + this.judgeHumanOps( + batch.workflowId, + { ok: true, ops: batch.ops }, + batch.tab + ) return batch.ops } @@ -355,11 +407,16 @@ export class AgentFollowerHostSocket { this.subscribeAnswer = answer } - private answerSubscribe(stateVector: string): void { - if (this.refuseReason !== null && this.refusalsLeft > 0) { + private answerSubscribe(workflowId: string, stateVector: string): void { + const host = this.hosts.get(workflowId)! + if ( + workflowId === this.workflowId && + this.refuseReason !== null && + this.refusalsLeft > 0 + ) { this.refusalsLeft -= 1 this.refusedSubscribes += 1 - const refusal = this.host.subscribeRefused(this.refuseReason) + const refusal = host.subscribeRefused(this.refuseReason) this.send({ ...refusal, data: { ...refusal.data, code: this.refuseReason } @@ -367,8 +424,12 @@ export class AgentFollowerHostSocket { this.resolveSubscribed?.() return } - for (const frame of this.subscribeAnswer(stateVector)) this.send(frame) - this.subscribes += 1 + const answer = + workflowId === this.workflowId + ? this.subscribeAnswer(stateVector) + : [host.subscribed(), host.catchUp(stateVector)] + for (const frame of answer) this.send(frame) + this.subscribes.set(workflowId, this.subscribeCount(workflowId) + 1) this.resolveSubscribed?.() } @@ -382,28 +443,37 @@ export class AgentFollowerHostSocket { this.unansweredHumanBatches += count } - private wouldAcceptHumanOps({ opsResult, tab }: ParsedClientDocFrame) { - return 'ops' in this.classifyHumanOps(opsResult, tab) + private wouldAcceptHumanOps( + { opsResult, tab }: ParsedClientDocFrame, + workflowId: string + ) { + return 'ops' in this.classifyHumanOps(workflowId, opsResult, tab) } private classifyHumanOps( + workflowId: string, opsResult: ParsedWireBatch, tab: string | null ): { ops: WireOpEnvelope[] } | { refusal: HostFrame } { if (!opsResult.ok || !tab || !isValidDocOpsBatch(opsResult.ops)) - return { refusal: this.invalidFrameResult() } + return { refusal: this.invalidFrameResult(workflowId) } if (!claimsOneFrameActor(opsResult.ops, tab)) - return { refusal: this.forbiddenResult() } + return { refusal: this.forbiddenResult(workflowId) } return { ops: opsResult.ops } } - private judgeHumanOps(opsResult: ParsedWireBatch, tab: string | null): void { - const verdict = this.classifyHumanOps(opsResult, tab) + private judgeHumanOps( + workflowId: string, + opsResult: ParsedWireBatch, + tab: string | null + ): void { + const verdict = this.classifyHumanOps(workflowId, opsResult, tab) if ('refusal' in verdict) { this.send(verdict.refusal) return } - const { result, update, outcomes } = this.host.applyWire(verdict.ops) + const host = this.hosts.get(workflowId)! + const { result, update, outcomes } = host.applyWire(verdict.ops) this.humanOutcomes.push(...outcomes) this.send(result) if (update) (this.hostUpdateSink ?? ((frame) => this.send(frame)))(update) @@ -423,17 +493,17 @@ export class AgentFollowerHostSocket { applied: [], skipped: [], code: 'unknown_workflow', - message: 'the fake host serves one workflow' + message: 'the fake host does not serve this workflow' } }) } - private invalidFrameResult(): HostFrame { + private invalidFrameResult(workflowId: string): HostFrame { return { type: 'doc_ops_result', data: { v: DOC_PROTOCOL_VERSION, - workflow_id: this.workflowId, + workflow_id: workflowId, ok: false, code: 'invalid_frame', message: 'doc_ops frame was not structurally valid' @@ -441,12 +511,12 @@ export class AgentFollowerHostSocket { } } - private forbiddenResult(): HostFrame { + private forbiddenResult(workflowId: string): HostFrame { return { type: 'doc_ops_result', data: { v: DOC_PROTOCOL_VERSION, - workflow_id: this.workflowId, + workflow_id: workflowId, ok: false, code: 'forbidden', message: 'op actor does not match the authenticated writer' @@ -457,10 +527,11 @@ export class AgentFollowerHostSocket { /** * Rises once per subscribe the host accepted, after its answer was sent. * A custom answer from {@link answerSubscribesWith} may carry no catch-up; - * refused subscribes count only in {@link refusedSubscribeCount}. + * refused subscribes count only in {@link refusedSubscribeCount}. Counts the + * primary workflow unless another served workflow id is passed. */ - subscribeCount(): number { - return this.subscribes + subscribeCount(workflowId = this.workflowId): number { + return this.subscribes.get(workflowId) ?? 0 } connectionCount(): number { diff --git a/browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts b/browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts new file mode 100644 index 00000000000..cd54f8c0baa --- /dev/null +++ b/browser_tests/tests/agent/agentHumanEditAfterOtherWorkflowReset.spec.ts @@ -0,0 +1,74 @@ +import { comfyExpect as expect } from '@e2e/fixtures/ComfyPage' +import { agentConversationTest as test } from '@e2e/fixtures/agentConversationFixture' + +test.describe( + 'Human edit after another workflow resets', + { + tag: ['@cloud', '@agent', '@vue-nodes'], + annotation: { + type: 'issue', + description: 'https://github.com/Comfy-Org/ComfyUI_frontend/pull/19871' + } + }, + () => { + const RESET_WORKFLOW_ID = '93ba31ab-65a2-42fe-bd12-b96bf4ef3591' + test.use({ + conversationCase: 'agent-rec-text-only-answer', + humanOpsHost: 'apply' + }) + + test('keeps the original workflow mint cursor before its resubscribe is acknowledged', async ({ + agentConversation + }) => { + const { recordedWorkflowId, firstBaseVersion } = + await test.step('Edit the original workflow and record its wire version', async () => { + await agentConversation.runTurns() + const recordedWorkflowId = agentConversation.conversation.workflow.id + const firstNodeId = await agentConversation.addNodeOfType( + 'Note', + [400, 400] + ) + await expect( + agentConversation.vueNodes.getNodeLocator(firstNodeId) + ).toBeVisible() + await agentConversation.waitForHumanOps(1) + const firstBaseVersion = + await agentConversation.humanAddNodeBaseVersion( + recordedWorkflowId, + firstNodeId + ) + return { recordedWorkflowId, firstBaseVersion } + }) + + await test.step('Reset the other workflow', async () => { + await agentConversation.activateEmptyWorkflow( + RESET_WORKFLOW_ID, + 'Reset target' + ) + const beforeReset = agentConversation.subscribeCount(RESET_WORKFLOW_ID) + agentConversation.sendDocumentReset(RESET_WORKFLOW_ID) + await expect + .poll(() => agentConversation.subscribeCount(RESET_WORKFLOW_ID)) + .toBeGreaterThanOrEqual(beforeReset + 1) + }) + + await test.step('Edit before the resubscribe acknowledgement', async () => { + await agentConversation.activateRecordedWorkflowBeforeSubscribeAck() + const secondNodeId = await agentConversation.addNodeOfType( + 'Note', + [650, 400] + ) + await expect( + agentConversation.vueNodes.getNodeLocator(secondNodeId) + ).toBeVisible() + expect( + await agentConversation.humanAddNodeBaseVersion( + recordedWorkflowId, + secondNodeId + ) + ).toBeGreaterThan(firstBaseVersion) + agentConversation.resumeWorkflowSubscribe(recordedWorkflowId) + }) + }) + } +) From 21b34b70d78f1d35638a5c0319253ec70f72f2e5 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Mon, 2 Nov 2026 23:09:27 -0800 Subject: [PATCH 2/4] fix(agent-crdt): track unconfirmed bind targets as unsynced (FRONT-3581) (#22596) - Records unsynced state only when a real local graph intent occurs while the bound workflow document is not yet confirmed; merely binding or abandoning a target stays clean. - Preserves genuine unsynced ownership across follower/product-gate restarts, then lets the replacement follower retire it after confirmation. - Keeps the existing restore warning and bind guard unchanged. Linear: FRONT-3581 (split from FE-1969 / #21147). - [x] Cloud black-box harness: `agentFollowerRestoredBindingReload.spec.ts` makes and saves a widget edit while identity is pending, restores again, resolves identity, and verifies the visible edit survives without an unsafe second subscription. - [x] Parent-behavior RED proof: removing only the production intent-to-follower wiring makes that test fail (`expected subscribe count 1, received 2`); restored wiring is GREEN. - [x] Focused Vitest after live-main rebase: 729 tests passed across AgentPanelRoot, docOpMinter, unsynced ownership, and product-gate restart coverage. - [x] Commit hooks: app/browser typecheck, type-aware lint, ESLint, stylelint, server-facts all passed. - [x] Push hooks: dependency-cruiser, knip, and fallow changed-file audit passed with zero findings. (cherry picked from commit c76f916cead7b4c430f27c6a35921638b71d7a55) --- ...agentFollowerRestoredBindingReload.spec.ts | 46 ++++++++++ .../extensions/agent/AgentPanelRoot.test.ts | 84 +++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 2 + .../extensions/agent/crdt/docOpMinter.test.ts | 16 ++++ .../extensions/agent/crdt/docOpMinter.ts | 28 ++++++- .../agent/crdt/followerProductGate.test.ts | 38 +++++++++ .../agent/crdt/unsyncedHumanOps.test.ts | 11 +++ .../extensions/agent/crdt/unsyncedHumanOps.ts | 9 ++ .../agent/crdt/useAgentCrdtFollower.ts | 19 +++-- 9 files changed, 244 insertions(+), 9 deletions(-) diff --git a/browser_tests/tests/agent/agentFollowerRestoredBindingReload.spec.ts b/browser_tests/tests/agent/agentFollowerRestoredBindingReload.spec.ts index f68ec563b1c..8b22d23ff29 100644 --- a/browser_tests/tests/agent/agentFollowerRestoredBindingReload.spec.ts +++ b/browser_tests/tests/agent/agentFollowerRestoredBindingReload.spec.ts @@ -123,5 +123,51 @@ test.describe( expect(await posted).toContain('local edit before automatic restore') }) }) + + test('keeps a saved local edit made while restored-target identity is pending', async ({ + agentConversation, + page + }) => { + test.setTimeout(90_000) + const reload = new RestoredBindingReloadDriver(page, agentConversation) + await agentConversation.persistSavedWorkflow() + await agentConversation.runTurns() + const prompt = agentConversation.vueNodes + .getNodeLocator('6') + .getByRole('textbox') + await agentConversation.topbar.saveWorkflowAs('Restored reload') + await reload.persistThreadTranscript() + + let releaseIdentity!: () => void + const identityPending = new Promise((resolve) => { + releaseIdentity = resolve + }) + await page.route('**/api/agent/identity', async (route) => { + await identityPending + await route.fallback() + }) + + await reload.reloadIntoRestoredTarget('Restored reload') + await prompt.fill('local edit while identity is pending') + await agentConversation.topbar.saveWorkflowAs('Restored reload') + + const subscribesBeforeSecondReload = agentConversation.subscribeCount() + await reload.reloadIntoRestoredTarget('Restored reload') + await expect(prompt).toHaveValue('local edit while identity is pending') + + const identityResponse = page.waitForResponse('**/api/agent/identity') + releaseIdentity() + await identityResponse + await page.evaluate( + () => + new Promise((resolve) => + requestAnimationFrame(() => requestAnimationFrame(() => resolve())) + ) + ) + expect(agentConversation.subscribeCount()).toBe( + subscribesBeforeSecondReload + ) + await expect(prompt).toHaveValue('local edit while identity is pending') + }) } ) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 013d3ac0051..d4296c13cd3 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -10662,6 +10662,90 @@ describe('AgentPanelRoot workflow binding', () => { } }) + it('leaves a workflow unbound after a local edit made before its follower subscribe confirms', async () => { + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ status: 'pending' }), + reset: vi.fn(), + stop: () => {} + }) + makeTab('wf-42') + const target = addTab('workflows/target.json', { + activeState: fromPartial({ id: 'graph-target' }) + }) + useAgentWorkflowTabBindingStore().bind('wf-target', target.path) + const messagesFor = (threadId: string, text: string): AgentMessages => [ + { + id: `row-${threadId}`, + thread_id: threadId, + seq: 1, + role: 'user', + status: 'complete', + turn_id: 'turn', + workflow_id: 'wf-target', + content: { text } + } + ] + vi.mocked(fetch).mockImplementation(async (input: RequestInfo | URL) => { + const url = String(input) + + if (url.includes('/th-first/messages')) + return json(200, messagesFor('th-first', 'First request')) + if (url.includes('/th-second/messages')) + return json(200, messagesFor('th-second', 'Second request')) + if (url.includes('/workflows')) + return json(200, { + data: [{ id: 'wf-target', name: 'target' }], + pagination: { offset: 0, limit: 100, total: 1, has_more: false } + }) + return json( + 200, + agentThreadList([ + agentThread({ + id: 'th-first', + title: 'First chat', + last_message_at: '2026-09-01T00:00:00Z' + }), + agentThread({ + id: 'th-second', + title: 'Second chat', + last_message_at: '2026-08-01T00:00:00Z' + }) + ]) + ) + }) + renderWithSelectedTarget() + expect(docOpMinterDeps.current?.isDocBound()).toBe(false) + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.showChatHistory') + }) + ) + await userEvent.click(await screen.findByText('First chat')) + await screen.findAllByText('First request') + await vi.waitFor(() => + expect(useAgentChatHistoryStore().activeId).toBe('th-first') + ) + await vi.waitFor(() => + expect(docOpMinterDeps.current?.boundRootGraphId()).not.toBeNull() + ) + expect(docOpMinterDeps.current?.isDocBound()).toBe(false) + docOpMinterDeps.current?.onUnboundLocalIntent?.() + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.showChatHistory') + }) + ) + await userEvent.click(await screen.findByText('Second chat')) + await screen.findAllByText('Second request') + await vi.waitFor(() => + expect(useAgentChatHistoryStore().activeId).toBe('th-second') + ) + expect(docOpMinterDeps.current?.boundRootGraphId()).toBeNull() + expect(docOpMinterDeps.current?.isDocBound()).toBe(false) + }) + it('binds the session to the workflow a history thread restored, not only a reload', async () => { makeTab('wf-42') const old = addTab('workflows/old.json', { diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 36066e59af7..22a159931a8 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1083,6 +1083,7 @@ const { status: crdtStatus, debugSnapshot: crdtDebugSnapshot, enqueueHumanOperations, + noteUnboundLocalIntent, retrySubscription, docInputNames, docWidgetValue, @@ -1158,6 +1159,7 @@ function followerCanDeliver(): boolean { const docOpMinter = attachDocOpMinter({ isEnabled: () => agentPanelStore.enabled, isDocBound: followerCanDeliver, + onUnboundLocalIntent: noteUnboundLocalIntent, enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), boundRootGraphId, diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts index 0663bc74be0..3d077a2db17 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts @@ -303,6 +303,7 @@ describe('attachDocOpMinter', () => { let refusals: Array<[string, string]> let seededPromotedDocs: Set> let dynamicComboDocs: Set> + let unboundLocalIntents: number function registeredDynamicComboScenario() { const scenario = dynamicComboScenario(graph) @@ -326,9 +327,11 @@ describe('attachDocOpMinter', () => { refusals = [] seededPromotedDocs = new Set() dynamicComboDocs = new Set() + unboundLocalIntents = 0 minter = attachDocOpMinter({ isEnabled: () => enabled, isDocBound: () => bound, + onUnboundLocalIntent: () => unboundLocalIntents++, enqueue: (operations) => minted.push(...operations), getGraph: () => (graphAvailable ? graph : null), boundRootGraphId: () => (unbound ? null : rootGraphId), @@ -572,6 +575,19 @@ describe('attachDocOpMinter', () => { expect(source.widgets![0].value).toBe(6) expect(minted).toEqual([]) + expect(unboundLocalIntents).toBe(1) + }) + + it('marks only edits to the bound workflow while its document is unconfirmed', async () => { + const { source } = seedGraph(graph) + bound = false + source.widgets![0].value = 5 + unbound = true + source.widgets![0].value = 6 + await afterFlush() + + expect(unboundLocalIntents).toBe(1) + expect(minted).toEqual([]) }) it('snapshots a pasted node after its same-tick configure, folding the widget and field writes into add_node', async () => { diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.ts b/src/workbench/extensions/agent/crdt/docOpMinter.ts index 3ad8c259555..6846055ae1a 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.ts @@ -63,6 +63,8 @@ export interface DocOpMinterDeps { isEnabled(): boolean /** A semantic doc is bound for the active workflow. */ isDocBound(): boolean + /** Records a local edit that cannot yet enter the semantic document. */ + onUnboundLocalIntent?(): void /** Receives minted semantic operations (the sender's inbox). */ enqueue(operations: GraphOperation[]): void /** The live root graph, or null when no workflow is open. */ @@ -1186,14 +1188,32 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { schedule({ kind: 'add_node', graph: event.graph, node: event.node }) } + function discardHeldWrite(event: GraphIntentEvent): void { + if (event.type === 'set_widget') + heldPromotedWrites.delete(heldWriteKey(event)) + } + + function noteUnboundLocalIntent(event: GraphIntentEvent): void { + const graph = deps.getGraph() + const boundRootGraphId = deps.boundRootGraphId() + const eventRootGraphId = + 'graph' in event + ? event.graph.rootGraph.id + : graph !== null && reachableIntentGraph(graph, event.graphId) !== null + ? graph.id + : null + if (boundRootGraphId !== null && eventRootGraphId === boundRootGraphId) + deps.onUnboundLocalIntent?.() + } + function onIntent(event: GraphIntentEvent): void { if (event.source !== 'local') return - if (!deps.isEnabled() || !deps.isDocBound()) { - if (event.type === 'set_widget') - heldPromotedWrites.delete(heldWriteKey(event)) + if (deps.isEnabled() && deps.isDocBound()) { + mintIntent(event) return } - mintIntent(event) + if (deps.isEnabled()) noteUnboundLocalIntent(event) + discardHeldWrite(event) } function mintIntent(event: GraphIntentEvent): void { diff --git a/src/workbench/extensions/agent/crdt/followerProductGate.test.ts b/src/workbench/extensions/agent/crdt/followerProductGate.test.ts index 81ee362c5f2..41dedc8d002 100644 --- a/src/workbench/extensions/agent/crdt/followerProductGate.test.ts +++ b/src/workbench/extensions/agent/crdt/followerProductGate.test.ts @@ -5,6 +5,7 @@ import { defineComponent, nextTick, ref } from 'vue' import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import { createFakeAgentSocket } from './__fixtures__/agentSocket' +import { hasUnsyncedHumanOps } from './unsyncedHumanOps' import { useAgentCrdtFollower } from './useAgentCrdtFollower' it('gates real document transport and removes reconnect listeners on revocation', async () => { @@ -67,3 +68,40 @@ it('gates real document transport and removes reconnect listeners on revocation' data: { workflow_id: 'wf-2' } }) }) + +it('hands an unconfirmed local edit to the follower created after a gate restart', async () => { + const agentSocket = createFakeAgentSocket() + const store = useAgentPanelStore() + store.enabled = true + const workflowId = ref('wf-gate-restart') + let follower!: ReturnType + const { unmount } = render( + defineComponent({ + setup() { + follower = useAgentCrdtFollower(workflowId, { + transport: agentSocket.transport + }) + return () => null + } + }) + ) + onTestFinished(unmount) + + follower.noteUnboundLocalIntent() + expect(hasUnsyncedHumanOps('wf-gate-restart')).toBe(true) + + store.enabled = false + await nextTick() + expect(hasUnsyncedHumanOps('wf-gate-restart')).toBe(true) + + store.enabled = true + await nextTick() + agentSocket.receive('doc_subscribed', { + v: 1, + workflow_id: 'wf-gate-restart', + ok: true, + seq: 0, + lineage_seq: 0 + }) + expect(hasUnsyncedHumanOps('wf-gate-restart')).toBe(false) +}) diff --git a/src/workbench/extensions/agent/crdt/unsyncedHumanOps.test.ts b/src/workbench/extensions/agent/crdt/unsyncedHumanOps.test.ts index 0a89349015b..62f2327efa0 100644 --- a/src/workbench/extensions/agent/crdt/unsyncedHumanOps.test.ts +++ b/src/workbench/extensions/agent/crdt/unsyncedHumanOps.test.ts @@ -67,4 +67,15 @@ describe('unsyncedHumanOps', () => { expect(page.hasUnsyncedHumanOps('wf-a')).toBe(false) expect((await loadPage()).hasUnsyncedHumanOps('wf-a')).toBe(false) }) + + it("hands a disposed recorder's workflows to its successor", async () => { + const page = await loadPage() + page.recordUnsyncedHumanOps('tab-1', ['wf-a']) + page.releaseUnsyncedHumanOpsRecorder('tab-1') + page.recordUnsyncedHumanOps('tab-1', []) + + expect(page.hasUnsyncedHumanOps('wf-a')).toBe(true) + page.forgetUnsyncedAtLoad('wf-a') + expect(page.hasUnsyncedHumanOps('wf-a')).toBe(false) + }) }) diff --git a/src/workbench/extensions/agent/crdt/unsyncedHumanOps.ts b/src/workbench/extensions/agent/crdt/unsyncedHumanOps.ts index 8273269ec63..2a5c0226151 100644 --- a/src/workbench/extensions/agent/crdt/unsyncedHumanOps.ts +++ b/src/workbench/extensions/agent/crdt/unsyncedHumanOps.ts @@ -48,6 +48,15 @@ export function recordUnsyncedHumanOps( writeStored([...unsyncedNow()]) } +/** Preserves a disposed recorder's warnings for its successor. */ +export function releaseUnsyncedHumanOpsRecorder(recorder: string): void { + const recorded = unsyncedByRecorder.get(recorder) + if (recorded === undefined) return + for (const workflowId of recorded) loadOnce().add(workflowId) + unsyncedByRecorder.delete(recorder) + writeStored([...unsyncedNow()]) +} + /** Whether `workflowId`'s canvas holds edits the shared document may lack. */ export function hasUnsyncedHumanOps(workflowId: string): boolean { return unsyncedNow().has(workflowId) diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 976ea4c3075..0627c214ba6 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -59,7 +59,8 @@ import type { BatchOutcome, OpsResultView } from './opSender' import { createRejectedOpNotifier } from './rejectedOpNotice' import { forgetUnsyncedAtLoad, - recordUnsyncedHumanOps + recordUnsyncedHumanOps, + releaseUnsyncedHumanOpsRecorder } from './unsyncedHumanOps' export { STALE_AFTER_MS, SUBSCRIBE_CATCHUP_GRACE_MS } @@ -371,6 +372,7 @@ export function useAgentCrdtFollower( }), enqueueHumanOperations: (operations: GraphOperation[]) => follower.value?.enqueueHumanOperations(operations), + noteUnboundLocalIntent: () => follower.value?.noteUnboundLocalIntent(), retrySubscription: () => follower.value?.retrySubscription(), docInputNames: (nodeId: NodeId) => follower.value?.docInputNames(nodeId) ?? null, @@ -536,9 +538,9 @@ function startAgentCrdtFollower( } const workflowByUnsettledOpId = new Map() - const workflowsWithLostOps = new Set() + const workflowsWithUnsyncedOps = new Set() const recordUnsynced = (): void => { - const unsynced = new Set(workflowsWithLostOps) + const unsynced = new Set(workflowsWithUnsyncedOps) for (const { workflowId, ops } of sender.pendingOps()) { unsynced.add(workflowId) for (const op of ops) workflowByUnsettledOpId.set(op.op_id, workflowId) @@ -549,7 +551,7 @@ function startAgentCrdtFollower( const workflowId = workflowByUnsettledOpId.get(outcome.ops[0]?.op_id ?? '') for (const op of outcome.ops) workflowByUnsettledOpId.delete(op.op_id) if (workflowId !== undefined && outcome.state !== 'acknowledged') - workflowsWithLostOps.add(workflowId) + workflowsWithUnsyncedOps.add(workflowId) recordUnsynced() } @@ -907,7 +909,7 @@ function startAgentCrdtFollower( projection.replaceOnNextFrame(detail.workflowId) reportPendingFrames(detail.workflowId, 'discarded', NOTHING_APPLIED) sender.abortAll() - workflowsWithLostOps.delete(detail.workflowId) + workflowsWithUnsyncedOps.delete(detail.workflowId) recordUnsynced() events.onReset?.(detail.workflowId) connected.value = false @@ -1214,6 +1216,7 @@ function startAgentCrdtFollower( () => bridge.destroy(), () => client.destroy() ]) + releaseUnsyncedHumanOpsRecorder(tabId) }) const status = computed(() => ({ @@ -1250,6 +1253,12 @@ function startAgentCrdtFollower( projection.noteLocalWrites(operations) coalescer.enqueue(operations) }, + noteUnboundLocalIntent: () => { + const target = workflowId.value + if (target === null) return + workflowsWithUnsyncedOps.add(target) + recordUnsynced() + }, docInputNames: (nodeId: NodeId) => readDocSlotNames(bridge.follower.doc, String(nodeId), 'inputs'), docWidgetValue: (nodeId: NodeId, widgetName: string) => From 7bb58421924c1cfca53c8371a3a39e754e12869a Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 3 Nov 2026 11:16:11 +0000 Subject: [PATCH 3/4] fix(agent): bind the workflow a followed chat opens (FRONT-3804) (#22792) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Agent edits to a workflow whose tab is in the background now appear when you switch back. Before this, they appeared only after a reload (1.60 regression from #21512, [FRONT-3804](https://linear.app/comfyorg/issue/FRONT-3804)). Root cause: clicking a saved tab opens that tab's chat. Loading a chat clears the session binding that the CRDT follower subscribes to, and the follow path never set it again. The follower stayed detached, and the next turn's acknowledgement was the only thing that reattached it. The fix binds the followed chat's workflow the same way a chat restored from history already does, with the same unsynced-human-ops guard (#21147).
Full context for agent readers - **Regressing commit:** 1a6324c76d (#21512, "carry workflow chat follow onto current main"). It changed `useAgentSession`'s `restored` hook from `onWorkflowRestoredAndBound` to `restoreChatWorkflow`. The follow branch of that hook calls `adoptWorkflowTarget` + `adoptRestoredWorkflowTab` but never `bindWorkflow`, while `loadThread` → `openThread` → `forgetBinding()` has already nulled `boundWorkflowId`. With `boundWorkflowId === null`, `isFollowerActive` is false, so `useAgentCrdtFollower` unsubscribes and never resubscribes. - **Matches QA CNV06 step 5:** the first `add_node` landed live before the switch. After the follow, nothing else reached the canvas, so the node stayed untitled, the turn read "Changes not confirmed on your canvas", and a reload restored everything. - **Bisect:** the canvas-only variant of the new spec is green at f9f5b8dc17 (the parent of 1a6324c76d) and red at 1a6324c76d. 1.59.0.dev2 (864bfc939e) predates #21512. - **Change:** `AgentPanelRoot.vue` extracts `bindRestoredWorkflow` from `onWorkflowRestoredAndBound`. The follow path's `adoptWorkflowTarget` dep now also binds. No change to the follower, the projection or the applier. No reconcile pass. Remote apply still goes through the graph API. - **Unsynced guard on the follow bind (review round 1):** before this round, an unbound local edit was recorded as unsynced only when it hit the *bound* workflow. An edit to the visible tab while another workflow, or none, was bound went unrecorded, so the follow could bind that tab and run catch-up over a canvas the document never saw. `docOpMinter` now reports those edits through `onUnboundVisibleIntent`. `AgentPanelRoot` records them under the visible tab's confirmed binding (`bindingStore.workflowIdFor`), so `bindRestoredWorkflow`'s existing `hasUnsyncedHumanOps` guard refuses the bind. Tabs with no confirmed binding are not recorded, so edits made before a workflow's first chat do not block its later follow bind. No reconcile pass. - **Why the spec exists:** the agent edits saved workflow A while the user is on saved workflow B, whose own chat the panel follows. Returning to A must show every edit the agent made there, without a reload. Following a tab's chat loads that chat, which clears the session binding the CRDT follower subscribes to. Unless the followed chat binds its workflow again, the follower stays detached. Source: frontend 1.60 QA check CNV06 step 5, [FRONT-3804](https://linear.app/comfyorg/issue/FRONT-3804). - **Harness:** `agentConversationFixture` gains `applyOpsOnHostUnsent` (a host edit made while this client follows another workflow; the host fans it out only to subscribers, so it reaches this client only in the catch-up that answers its next subscribe), `serveWorkflow` (a second workflow's document) and `holdTranscript` (holds one thread's transcript read so a test can act while a chat loads). The spec's workflows, threads and transcripts live in `fixtures/data/agent/backgroundTabEdits.ts` and are served by the existing `AgentWorkflowChatsMock`.
## Verification - New black-box spec `browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts`: red on `main` a8c2ace17f (`[data-node-id="801"]` title never appears), green with the fix, 4/4 on `--repeat-each=4`. - Second case in the same spec, `do not rebind a tab the user edited while its chat was loading`: red with only the visible-intent wiring removed (B re-subscribed, subscribe count 2, expected 1), green with it. Both cases pass 6/6 on `--repeat-each=3` at `2e1e1c4f9b`. The first case is still red with the follow bind removed. - Neighbouring agent specs green: follows-chat, tab-chat transitions, restored-binding reload, tab-switch catch-up, reload binding, history reopen/focus, duplicate-name binding, stale tab binding, new-chat target, human-add tab switch. One failure, `agentWorkflowSelection` "picks an unsaved tab a reload left unopened", fails identically on unmodified `main` on this host, so it is unrelated. - `AgentPanelRoot.test.ts` and `useAgentWorkflowChatFollow.test.ts` pass (575/575). `typecheck` and `typecheck:browser` pass, and pre-commit/pre-push hooks ran. - **Backport:** `core/1.60` and `cloud/1.60` (the pin decision), plus `core/1.59` and `cloud/1.59`, whose tips already carry #21512. `cloud/v1.59.1` and later tags ship it. Tag `v1.59.0` does not. (cherry picked from commit 6dd3d827a3441d5dd2b7ef50d265e30bcb0e40d5) --- .../fixtures/agentConversationFixture.ts | 35 ++++- .../fixtures/data/agent/backgroundTabEdits.ts | 99 ++++++++++++ .../agent/agentBackgroundTabEditsLand.spec.ts | 146 ++++++++++++++++++ .../extensions/agent/AgentPanelRoot.vue | 27 +++- .../extensions/agent/crdt/docOpMinter.test.ts | 17 ++ .../extensions/agent/crdt/docOpMinter.ts | 24 ++- .../agent/crdt/useAgentCrdtFollower.ts | 6 +- 7 files changed, 338 insertions(+), 16 deletions(-) create mode 100644 browser_tests/fixtures/data/agent/backgroundTabEdits.ts create mode 100644 browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts diff --git a/browser_tests/fixtures/agentConversationFixture.ts b/browser_tests/fixtures/agentConversationFixture.ts index 207d5571062..39e1b1c655a 100644 --- a/browser_tests/fixtures/agentConversationFixture.ts +++ b/browser_tests/fixtures/agentConversationFixture.ts @@ -68,7 +68,7 @@ import { mockSavedWorkflowPersistence } from '@e2e/fixtures/utils/savedWorkflowP import { loadSeedIntoActiveTab } from '@e2e/fixtures/utils/seedActiveTab' import { nextFrame } from '@e2e/fixtures/utils/timing' -const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10' +export const THREAD_ID = 'e9a2f3d1-7c44-4b2e-9a01-5f6d8c7b3a10' // One synthetic message id per turn; the recorded ids never reach the page. const turnId = (turn: number): string => `0c5b1e77-2d4a-4f9e-8b63-1a2c3d4e5${turn.toString(16).padStart(3, '0')}` @@ -902,6 +902,39 @@ export class AgentConversationHarness { ]) } + applyOpsOnHostUnsent(operations: RecordedGraphOperation[]): void { + this.host.apply(operations) + for (const id of Object.keys(this.host.graph().nodes)) this.seenIds.add(id) + } + + /** Holds `threadId`'s transcript reads until `release` is called. */ + async holdTranscript( + threadId: string + ): Promise<{ requested: Promise; release: () => void }> { + let release!: () => void + let reached!: () => void + const held = new Promise((resolve) => (release = resolve)) + const requested = new Promise((resolve) => (reached = resolve)) + await this.page.route( + `**/api/agent/threads/${threadId}/messages`, + async (route) => { + if (route.request().method() !== 'GET') return route.fallback() + reached() + await held + return route.fallback() + } + ) + return { requested, release } + } + + /** Serves a second workflow's document beside the recorded one. */ + serveWorkflow(workflowId: string, seed: WorkflowJSON): void { + this.hostSocket.addWorkflow( + workflowId, + new HostDoc(workflowId, seed, this.conversation.workflow.catalog) + ) + } + // Rises once per follower subscribe; a tab return re-subscribes and the // host answers with the catch-up frame this counter has just sent. subscribeCount(workflowId?: string): number { diff --git a/browser_tests/fixtures/data/agent/backgroundTabEdits.ts b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts new file mode 100644 index 00000000000..c8c9c3e0fb2 --- /dev/null +++ b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts @@ -0,0 +1,99 @@ +import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema' + +import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' +import { + agentThreadSummary, + agentUserTranscript +} from '@e2e/fixtures/data/agent/agentThreadList' +import type { AgentWorkflowChatsMock } from '@e2e/fixtures/helpers/agentWorkflowChats' + +type WorkflowChats = ConstructorParameters[0] + +interface ChatWorkflow { + id: string + name: string + threadId: string + request: string +} + +export const BACKGROUND_TAB_CASE = 'agent-rec-set-widget-existing' +export const FOREGROUND_NAME = 'Unsaved Workflow' +export const BACKGROUND: ChatWorkflow = { + id: '3c1e5b7a-9d2f-4e8b-a6c4-0f1e2d3c4b5a', + name: 'Other Study', + threadId: '7a2b4c6d-8e0f-4a1b-9c3d-5e7f9a1b3c5d', + request: 'Tidy up the other study' +} +export const BACKGROUND_NODES = [ + { id: 801, title: 'b1', x: 1400 }, + { id: 802, title: 'b2', x: 1700 }, + { id: 803, title: 'b3', x: 2000 } +] + +const EMPTY_GRAPH: ComfyWorkflowJSON = { + last_node_id: 0, + last_link_id: 0, + nodes: [], + links: [], + groups: [], + config: {}, + extra: {}, + version: 0.4 +} + +export function backgroundEdits(): RecordedGraphOperation[] { + const type = 'EmptyLatentImage' + return BACKGROUND_NODES.flatMap(({ id, title, x }) => [ + { + op: 'add_node', + node_id: id, + class_type: type, + pos: [x, 600], + node: { + id, + type, + pos: [x, 600], + size: [240, 134], + flags: {}, + order: 0, + mode: 0, + inputs: [], + outputs: [{ name: 'LATENT', type: 'LATENT', links: [] }], + properties: {}, + widgets_values: [512, 512, 1] + } + }, + { op: 'set_node_field', node_id: id, field: 'title', value: title } + ]) +} + +export function backgroundTabChats( + foreground: Omit +): WorkflowChats { + const chats = [ + { ...foreground, name: FOREGROUND_NAME, at: '2026-11-02T09:00:00Z' }, + { ...BACKGROUND, at: '2026-11-02T10:00:00Z' } + ] + return { + workflows: chats.map(({ id, name }) => ({ + id, + name, + content: EMPTY_GRAPH + })), + threads: chats.map(({ id, name, threadId, request, at }) => + agentThreadSummary(threadId, `${name} chat`, { + preview: request, + workflow_id: id, + last_message_at: at + }) + ), + transcripts: Object.fromEntries( + chats.map(({ id, threadId, request }) => [ + threadId, + agentUserTranscript(threadId, id, request) + ]) + ) + } +} + +export const USER_NOTE_POSITION: [number, number] = [400, 400] diff --git a/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts b/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts new file mode 100644 index 00000000000..2084a0d9028 --- /dev/null +++ b/browser_tests/tests/agent/agentBackgroundTabEditsLand.spec.ts @@ -0,0 +1,146 @@ +import { expect } from '@playwright/test' + +import { + agentConversationTest as test, + THREAD_ID +} from '@e2e/fixtures/agentConversationFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { + BACKGROUND, + BACKGROUND_NODES, + BACKGROUND_TAB_CASE, + backgroundEdits, + backgroundTabChats, + FOREGROUND_NAME, + USER_NOTE_POSITION +} from '@e2e/fixtures/data/agent/backgroundTabEdits' +import { AgentWorkflowChatsMock } from '@e2e/fixtures/helpers/agentWorkflowChats' + +test.describe( + 'Agent edits to a background workflow tab', + { tag: ['@cloud', '@agent', '@vue-nodes'] }, + () => { + test.use({ conversationCase: BACKGROUND_TAB_CASE }) + + test.beforeEach(async ({ agentConversation, page }) => { + test.setTimeout(120_000) + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const aRequest = agentConversation.conversation.turns[0].request.content + + await test.step('agent edits workflow A', () => + agentConversation.runTurns()) + + await test.step('workflow B has a saved chat of its own', async () => { + agentConversation.serveWorkflow(BACKGROUND.id, { + ...agentConversation.conversation.workflow.seed, + nodes: [], + links: [] + }) + await new AgentWorkflowChatsMock( + backgroundTabChats({ + id: agentConversation.conversation.workflow.id, + threadId: THREAD_ID, + request: aRequest + }) + ).route(page) + }) + + await test.step('user opens workflow B and its chat', async () => { + await panel.historyButton.click() + await panel.root + .getByRole('button', { name: `${BACKGROUND.name} chat`, exact: true }) + .click() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + topbar.getWorkflowTab(BACKGROUND.name).and(topbar.getActiveTab()) + ).toBeVisible() + }) + + await test.step('user returns to A by its tab', async () => { + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect(panel.userMessageBubbles).toHaveText([aRequest]) + }) + }) + + test('are on the canvas when the user returns to that tab', async ({ + agentConversation, + page + }) => { + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const aRequest = agentConversation.conversation.turns[0].request.content + + await test.step('user switches to B by its tab', async () => { + await topbar.getWorkflowTab(BACKGROUND.name).click() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + topbar.getWorkflowTab(BACKGROUND.name).and(topbar.getActiveTab()) + ).toBeVisible() + }) + + await test.step('agent adds three titled nodes to A while B is shown', () => + agentConversation.applyOpsOnHostUnsent(backgroundEdits())) + + await test.step('returning to A shows every background edit', async () => { + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect( + topbar.getWorkflowTab(FOREGROUND_NAME).and(topbar.getActiveTab()) + ).toBeVisible() + await expect(panel.userMessageBubbles).toHaveText([aRequest]) + for (const { id, title } of BACKGROUND_NODES) + await expect( + agentConversation.vueNodes + .getNodeLocator(String(id)) + .getByTestId('node-title') + ).toHaveText(title) + }) + }) + + test('do not rebind a tab the user edited while its chat was loading', async ({ + agentConversation, + page + }) => { + const panel = new AgentPanel(page) + const topbar = agentConversation.topbar + const subscribesBefore = agentConversation.subscribeCount(BACKGROUND.id) + const transcript = await agentConversation.holdTranscript( + BACKGROUND.threadId + ) + + const noteId = + await test.step('user edits B before its chat loads', async () => { + await topbar.getWorkflowTab(BACKGROUND.name).click() + await transcript.requested + const id = await agentConversation.addNoteThroughSearchBox({ + x: USER_NOTE_POSITION[0], + y: USER_NOTE_POSITION[1] + }) + await expect( + agentConversation.vueNodes.getNodeLocator(id) + ).toBeVisible() + return id + }) + + await test.step('B keeps the edit once its chat loads', async () => { + transcript.release() + await expect(panel.userMessageBubbles).toHaveText([BACKGROUND.request]) + await expect( + agentConversation.vueNodes.getNodeLocator(noteId) + ).toBeVisible() + }) + + await test.step('the next subscribe, to A, finds B never re-subscribed', async () => { + const aId = agentConversation.conversation.workflow.id + const aSubscribes = agentConversation.subscribeCount(aId) + await topbar.getWorkflowTab(FOREGROUND_NAME).click() + await expect + .poll(() => agentConversation.subscribeCount(aId)) + .toBeGreaterThan(aSubscribes) + expect(agentConversation.subscribeCount(BACKGROUND.id)).toBe( + subscribesBefore + ) + }) + }) + } +) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 22a159931a8..a3ca938cfaa 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -926,12 +926,23 @@ async function onWorkflowRestoredAndBound( isCurrent ) if (committedWorkflowId === null || !isCurrent()) return ready - if (hasUnsyncedHumanOps(committedWorkflowId)) return ready - if (boundWorkflowId.value !== committedWorkflowId) - bindWorkflow(committedWorkflowId) + bindRestoredWorkflow(committedWorkflowId) return ready } +function bindRestoredWorkflow(workflowId: string): void { + if (hasUnsyncedHumanOps(workflowId)) return + if (boundWorkflowId.value !== workflowId) bindWorkflow(workflowId) +} + +function adoptFollowedWorkflow( + workflow: ComfyWorkflow, + workflowId: string +): void { + adoptWorkflowTarget(workflow, workflowId) + bindRestoredWorkflow(workflowId) +} + function warnWorkflowUnavailable(): void { toast.warning(t('agent.targetNavigationUnavailable'), { duration: 5000 }) } @@ -1156,10 +1167,18 @@ function boundRootGraphId(): RootGraphId | null { function followerCanDeliver(): boolean { return isFollowerActive.value && subscriptionSent() } +function noteUnboundVisibleIntent(): void { + const visible = workflowStore.activeWorkflow + const workflowId = visible + ? bindingStore.workflowIdFor(visible.path) + : undefined + if (workflowId !== undefined) noteUnboundLocalIntent(workflowId) +} const docOpMinter = attachDocOpMinter({ isEnabled: () => agentPanelStore.enabled, isDocBound: followerCanDeliver, onUnboundLocalIntent: noteUnboundLocalIntent, + onUnboundVisibleIntent: noteUnboundVisibleIntent, enqueue: enqueueHumanOperations, getGraph: () => (app.isGraphReady ? app.rootGraph : null), boundRootGraphId, @@ -1771,7 +1790,7 @@ const workflowChatFollow = useAgentWorkflowChatFollow({ trackThreadStarted: () => useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }), restoreThreadWorkflow, - adoptWorkflowTarget, + adoptWorkflowTarget: adoptFollowedWorkflow, adoptRestoredWorkflowTab }) diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts index 3d077a2db17..eb020cfbbe0 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.test.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.test.ts @@ -304,6 +304,7 @@ describe('attachDocOpMinter', () => { let seededPromotedDocs: Set> let dynamicComboDocs: Set> let unboundLocalIntents: number + let unboundVisibleIntents: number function registeredDynamicComboScenario() { const scenario = dynamicComboScenario(graph) @@ -328,10 +329,12 @@ describe('attachDocOpMinter', () => { seededPromotedDocs = new Set() dynamicComboDocs = new Set() unboundLocalIntents = 0 + unboundVisibleIntents = 0 minter = attachDocOpMinter({ isEnabled: () => enabled, isDocBound: () => bound, onUnboundLocalIntent: () => unboundLocalIntents++, + onUnboundVisibleIntent: () => unboundVisibleIntents++, enqueue: (operations) => minted.push(...operations), getGraph: () => (graphAvailable ? graph : null), boundRootGraphId: () => (unbound ? null : rootGraphId), @@ -590,6 +593,20 @@ describe('attachDocOpMinter', () => { expect(minted).toEqual([]) }) + it('marks an unbound edit to the visible workflow while another one is bound', async () => { + const { source } = seedGraph(graph) + bound = false + rootGraphId = toRootGraphId('other-root-graph') + source.widgets![0].value = 5 + unbound = true + source.widgets![0].value = 6 + await afterFlush() + + expect(unboundLocalIntents).toBe(0) + expect(unboundVisibleIntents).toBe(2) + expect(minted).toEqual([]) + }) + it('snapshots a pasted node after its same-tick configure, folding the widget and field writes into add_node', async () => { const node = new TestSource() graph.add(node) diff --git a/src/workbench/extensions/agent/crdt/docOpMinter.ts b/src/workbench/extensions/agent/crdt/docOpMinter.ts index 6846055ae1a..f96834f16c7 100644 --- a/src/workbench/extensions/agent/crdt/docOpMinter.ts +++ b/src/workbench/extensions/agent/crdt/docOpMinter.ts @@ -65,6 +65,8 @@ export interface DocOpMinterDeps { isDocBound(): boolean /** Records a local edit that cannot yet enter the semantic document. */ onUnboundLocalIntent?(): void + /** Records a local edit to the visible workflow while another one, or none, is bound. */ + onUnboundVisibleIntent?(): void /** Receives minted semantic operations (the sender's inbox). */ enqueue(operations: GraphOperation[]): void /** The live root graph, or null when no workflow is open. */ @@ -1193,17 +1195,23 @@ export function attachDocOpMinter(deps: DocOpMinterDeps): DocOpMinter { heldPromotedWrites.delete(heldWriteKey(event)) } + function intentRootGraphId( + event: GraphIntentEvent, + graph: LGraph | null + ): string | null { + if ('graph' in event) return event.graph.rootGraph.id + return graph !== null && reachableIntentGraph(graph, event.graphId) !== null + ? graph.id + : null + } + function noteUnboundLocalIntent(event: GraphIntentEvent): void { const graph = deps.getGraph() - const boundRootGraphId = deps.boundRootGraphId() - const eventRootGraphId = - 'graph' in event - ? event.graph.rootGraph.id - : graph !== null && reachableIntentGraph(graph, event.graphId) !== null - ? graph.id - : null - if (boundRootGraphId !== null && eventRootGraphId === boundRootGraphId) + const eventRootGraphId = intentRootGraphId(event, graph) + if (eventRootGraphId === null) return + if (eventRootGraphId === deps.boundRootGraphId()) deps.onUnboundLocalIntent?.() + else if (eventRootGraphId === graph?.id) deps.onUnboundVisibleIntent?.() } function onIntent(event: GraphIntentEvent): void { diff --git a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts index 0627c214ba6..b70e92f4dce 100644 --- a/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts +++ b/src/workbench/extensions/agent/crdt/useAgentCrdtFollower.ts @@ -372,7 +372,8 @@ export function useAgentCrdtFollower( }), enqueueHumanOperations: (operations: GraphOperation[]) => follower.value?.enqueueHumanOperations(operations), - noteUnboundLocalIntent: () => follower.value?.noteUnboundLocalIntent(), + noteUnboundLocalIntent: (target?: string) => + follower.value?.noteUnboundLocalIntent(target), retrySubscription: () => follower.value?.retrySubscription(), docInputNames: (nodeId: NodeId) => follower.value?.docInputNames(nodeId) ?? null, @@ -1253,8 +1254,7 @@ function startAgentCrdtFollower( projection.noteLocalWrites(operations) coalescer.enqueue(operations) }, - noteUnboundLocalIntent: () => { - const target = workflowId.value + noteUnboundLocalIntent: (target = workflowId.value) => { if (target === null) return workflowsWithUnsyncedOps.add(target) recordUnsynced() From 3dda9c5f43ee7d121944afc4710339e22aa067bb Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Tue, 3 Nov 2026 04:23:45 -0800 Subject: [PATCH 4/4] test(agent): import the workflow schema from its 1.59 path in the background-tab fixture core/1.59 predates the graph-model package extraction (#22053), so the fixture added by #22792 imports ComfyWorkflowJSON from @/platform/workflow/validation/schemas/workflowSchema, as the train's other agent fixtures do. --- browser_tests/fixtures/data/agent/backgroundTabEdits.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/browser_tests/fixtures/data/agent/backgroundTabEdits.ts b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts index c8c9c3e0fb2..b5659f1f4f5 100644 --- a/browser_tests/fixtures/data/agent/backgroundTabEdits.ts +++ b/browser_tests/fixtures/data/agent/backgroundTabEdits.ts @@ -1,4 +1,4 @@ -import type { ComfyWorkflowJSON } from '@comfyorg/graph-model/workflowSchema' +import type { ComfyWorkflowJSON } from '@/platform/workflow/validation/schemas/workflowSchema' import type { RecordedGraphOperation } from '@e2e/fixtures/data/agent/agentConversation' import {