From 62a716365f304ed518182482b885fe1a5dbe4f29 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 21 Sep 2026 10:51:38 +0000 Subject: [PATCH 1/2] fix: release an agent workflow binding the server disowns A turn posted with a workflow_id the agent service will not serve to the caller's workspace comes back 403 "workflow not found or access denied". The tab -> workflow binding that produced that id is persisted, so the tab stayed wired to the dead workflow: every later turn from it re-posted the same id, and a reload re-affirmed the binding through the thread's own workflow pointer. Only opening a fresh tab escaped it. Release the binding (and the session's bound workflow) on that specific 403 so the next turn resolves the tab again. A thread-scoped 403 carries a different subject and leaves the binding alone. --- .../composables/agent/useAgentSession.test.ts | 47 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 31 +++++++++++- .../agent/schemas/agentApiSchema.ts | 10 ++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index ea3302ca1a2..c109b0b54fb 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -1488,6 +1488,53 @@ describe('useAgentSession (v1 composition root)', () => { expect(session.boundWorkflowId.value).toBeNull() }) + // FE-2501: the tab -> workflow binding is persisted, so a workflow the + // backend refuses to serve poisons its tab for good - every later turn from + // that tab re-posts the same dead id, and a reload re-affirms the binding + // from the thread's own workflow pointer. Only the workflow-scoped refusal + // releases the binding; the thread-scoped one names a different resource. + it.for([ + { refusal: 'workflow not found or access denied', released: true }, + { refusal: 'thread not found or access denied', released: false } + ])('(k2) 403 $refusal releases the binding: $released', async (scenario) => { + const tabPath = 'workflows/portrait.json' + useAgentWorkflowTabBindingStore().bind('wf-dead', tabPath) + const postMessage = vi + .fn() + .mockRejectedValue( + new AgentApiError(scenario.refusal, 403, { error: scenario.refusal }) + ) + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => ({ id: 'wf-dead', tabPath }), + adopted: vi.fn() + } + }) + session.start() + session.bindWorkflow('wf-dead') + + expect(await session.sendMessage('run it')).toBe(false) + + expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-dead')).toBe( + scenario.released ? undefined : tabPath + ) + expect(session.boundWorkflowId.value).toBe( + scenario.released ? null : 'wf-dead' + ) + expect(session.entries.value.at(-1)).toMatchObject({ + role: 'assistant', + parts: [ + { + type: 'notice', + level: 'error', + text: `Message failed to send: ${scenario.refusal}` + } + ] + }) + }) + it('(k) a failed POST records the user text plus a settled error reply and returns false', async () => { const postMessage = vi .fn< diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index ee0d2a806bb..47f49ef8bc4 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -12,7 +12,8 @@ import { isAgentEvent, parseAgentWsEvent, toTurnId, - zAgentAdmissionError + zAgentAdmissionError, + zDisownedWorkflowError } from '../../schemas/agentApiSchema' import { AgentApiError } from '../../services/agent/agentRestClient' import type { @@ -109,6 +110,14 @@ function parseAdmissionError(error: unknown) { return { ...parsed.data.error, retryAfterSeconds: error.retryAfterSeconds } } +function disownsWorkflow(error: unknown): boolean { + return ( + error instanceof AgentApiError && + error.status === 403 && + zDisownedWorkflowError.safeParse(error.body).success + ) +} + export function useAgentSession(deps: AgentSessionDeps) { const { rest, events, workflow } = deps @@ -388,6 +397,23 @@ export function useAgentSession(deps: AgentSessionDeps) { ) } + /** + * The server will not serve the id this turn was posted under, and the + * binding that produced it outlives the page. Left in place it poisons the + * tab: every later turn re-posts the same dead id, and a reload re-affirms + * the binding through the thread's own workflow pointer. Releasing it lets + * the next turn resolve the tab again. + */ + function releaseDisownedWorkflow( + sent: WorkflowTurnContext | undefined, + error: unknown + ): void { + if (sent?.id === undefined || !disownsWorkflow(error)) return + bindingStore.unbind(sent.tabPath) + if (boundWorkflowId.value === sent.id) boundWorkflowId.value = null + if (rememberedWorkflowId === sent.id) rememberedWorkflowId = null + } + async function performSend( text: string, attachments?: SentAttachment[], @@ -399,6 +425,7 @@ export function useAgentSession(deps: AgentSessionDeps) { const originContext = workflow?.current() const origin: TurnOrigin = originContext === undefined ? null : { tabPath: originContext.tabPath } + let sentContext: WorkflowTurnContext | undefined try { await prepareWorkflow() if (generation !== loadGeneration) return false @@ -407,6 +434,7 @@ export function useAgentSession(deps: AgentSessionDeps) { recordUnavailableTarget(text) return false } + sentContext = wfContext const ack = await postTurn( threadAtSend, text, @@ -421,6 +449,7 @@ export function useAgentSession(deps: AgentSessionDeps) { return true } catch (error) { if (generation !== loadGeneration) return false + releaseDisownedWorkflow(sentContext, error) recordSendError(error, text) return false } diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index b414dcd438e..5cbd8db6f42 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -118,6 +118,16 @@ export type CloudWorkflowEntry = z.infer< export const zAgentError = z.union([zGeneratedAgentError, zAgentAdmissionError]) +/** + * The 403 body the agent service returns when it will not serve the turn's + * `workflow_id` to the caller's workspace, whether the row is gone or belongs + * elsewhere. The message is the only discriminator on the wire: a 403 refusing + * the thread or the message carries the same shape with a different subject. + */ +export const zDisownedWorkflowError = z.object({ + error: z.literal('workflow not found or access denied') +}) + export const zUploadImageResult = z.object({ name: z.string(), subfolder: z.string(), From d9d5fd602ef8c64cc6a75c114ea3256a4e2868fe Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 21 Sep 2026 11:22:04 +0000 Subject: [PATCH 2/2] fix: key the disowned-workflow release by id, not by tab path Review of the previous commit surfaced three real races in the release path: - The release sat below the `generation !== loadGeneration` guard, but the binding store is page-global and persisted. A refusal landing after a mid-flight newChat()/loadThread() returned early and left the dead binding in place, which is the exact state this is meant to clear. - `unbind(tabPath)` removed whatever workflow occupied the path at that moment. agent_active_tab, reference navigation and commitWorkflowTarget all rebind a path without bumping the generation, so a late refusal could delete a newly bound, perfectly valid workflow. New store method `unbindWorkflow(id)` removes the named record wherever it now sits, and also covers the mirror case of a refused id that moved to another path. - Releasing the binding alone did not stop the next turn re-posting the id: `cloudIdFor` prefers the resolver's name-derived cloud index, and `refreshCloudWorkflowIds` swallows errors and races a 3s timeout. The session now reports the refused id through a `disowned` hook that evicts it from that index too. Attribution was also questioned, on the theory that workflow_references or open_tabs could trigger the same 403. They cannot: both go through resolveWorkflowContext, which drops or marks unavailable entries and never fails the turn. Only req.WorkflowID produces this refusal, so the turn's own workflow id is the correct and only attribution. --- .../extensions/agent/AgentPanelRoot.vue | 2 + .../composables/agent/useAgentSession.test.ts | 68 +++++++++++++++++++ .../composables/agent/useAgentSession.ts | 18 +++-- .../agent/useAgentWorkflowResolver.test.ts | 28 ++++++++ .../agent/useAgentWorkflowResolver.ts | 11 +++ .../agentWorkflowTabBindingStore.test.ts | 17 +++++ .../agent/agentWorkflowTabBindingStore.ts | 21 +++++- 7 files changed, 160 insertions(+), 5 deletions(-) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index c5a76a49598..67d9e4a5905 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -179,6 +179,7 @@ const workflowResolver = useAgentWorkflowResolver({ }) const { refreshCloudWorkflowIds, + forgetCloudWorkflowId, cloudIdFor, boundOrOpenWorkflowFor, storedWorkflowFor, @@ -571,6 +572,7 @@ const { prepare: async () => { await refreshCloudWorkflowIds() }, + disowned: forgetCloudWorkflowId, tabs: openTabsSnapshot, activeTab: enqueueActiveTab, draft: targetWorkflowDraft diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts index c109b0b54fb..da32c82e7a2 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.test.ts @@ -1535,6 +1535,74 @@ describe('useAgentSession (v1 composition root)', () => { }) }) + // The refusal names a workflow, so the release must be keyed by that id. + // `agent_active_tab`, reference navigation and commitWorkflowTarget all + // rebind the same tab path mid-flight without bumping the send generation, + // and a path-keyed release would delete whichever binding won that race. + it('(k3) a late refusal does not disturb a binding the tab was rebound to', async () => { + const tabPath = 'workflows/portrait.json' + const bindings = useAgentWorkflowTabBindingStore() + bindings.bind('wf-dead', tabPath) + const postMessage = vi.fn(async () => { + bindings.bind('wf-live', tabPath) + throw new AgentApiError('workflow not found or access denied', 403, { + error: 'workflow not found or access denied' + }) + }) + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => ({ id: 'wf-dead', tabPath }), + adopted: vi.fn() + } + }) + session.start() + session.bindWorkflow('wf-dead') + + expect(await session.sendMessage('run it')).toBe(false) + + expect(bindings.tabPathFor('wf-dead')).toBeUndefined() + expect(bindings.tabPathFor('wf-live')).toBe(tabPath) + expect(bindings.workflowIdFor(tabPath)).toBe('wf-live') + }) + + // The binding store is page-global and persisted, so it outlives the send + // generation. A refusal that lands after newChat()/loadThread() has moved on + // must still release, or the dead id survives exactly as before the fix. + it('(k4) releases the binding even when the send generation moved on', async () => { + const tabPath = 'workflows/portrait.json' + const bindings = useAgentWorkflowTabBindingStore() + bindings.bind('wf-dead', tabPath) + const disowned = vi.fn() + let abandon: () => void = () => {} + const postMessage = vi.fn(async () => { + abandon() + throw new AgentApiError('workflow not found or access denied', 403, { + error: 'workflow not found or access denied' + }) + }) + const session = useAgentSession({ + rest: fakeRest({ postMessage }), + events: fakeEvents().source, + workflow: { + current: () => ({ id: 'wf-dead', tabPath }), + adopted: vi.fn(), + disowned + } + }) + session.start() + session.bindWorkflow('wf-dead') + abandon = () => void session.newChat() + + expect(await session.sendMessage('run it')).toBe(false) + + expect(bindings.tabPathFor('wf-dead')).toBeUndefined() + // The resolver prefers its name-derived cloud index over the binding + // store, so the refused id has to leave that index too. + expect(disowned).toHaveBeenCalledWith('wf-dead') + }) + it('(k) a failed POST records the user text plus a settled error reply and returns false', async () => { const postMessage = vi .fn< diff --git a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts index 47f49ef8bc4..3f2d8764156 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentSession.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentSession.ts @@ -82,6 +82,8 @@ export interface AgentSessionDeps { isCurrent: () => boolean ): Promise | void prepare?(): Promise + /** The server refused this workflow id; forget every cached trace of it. */ + disowned?(workflowId: string): void tabs?(origin?: TurnOrigin): OpenTabsSnapshot | undefined activeTab?(data: AgentActiveTabData): void draft?(origin?: TurnOrigin): DraftSnapshot | undefined @@ -401,15 +403,20 @@ export function useAgentSession(deps: AgentSessionDeps) { * The server will not serve the id this turn was posted under, and the * binding that produced it outlives the page. Left in place it poisons the * tab: every later turn re-posts the same dead id, and a reload re-affirms - * the binding through the thread's own workflow pointer. Releasing it lets - * the next turn resolve the tab again. + * the binding through the thread's own workflow pointer. + * + * Everything here is keyed by the refused id, never by its tab path: the tab + * may already have been rebound to a healthy workflow while the POST was in + * flight. `disowned` evicts the id from the resolver's cloud index, which + * `cloudIdFor` consults ahead of the binding store. */ function releaseDisownedWorkflow( sent: WorkflowTurnContext | undefined, error: unknown ): void { if (sent?.id === undefined || !disownsWorkflow(error)) return - bindingStore.unbind(sent.tabPath) + bindingStore.unbindWorkflow(sent.id) + workflow?.disowned?.(sent.id) if (boundWorkflowId.value === sent.id) boundWorkflowId.value = null if (rememberedWorkflowId === sent.id) rememberedWorkflowId = null } @@ -448,8 +455,11 @@ export function useAgentSession(deps: AgentSessionDeps) { acceptTurn(ack, text, wfContext, attachments, tags, workflowReferences) return true } catch (error) { - if (generation !== loadGeneration) return false + // Before the generation guard: the binding store is page-global and + // persisted, so a refusal that lands after newChat()/loadThread() has + // moved on still has to release, or the dead id survives the reload. releaseDisownedWorkflow(sentContext, error) + if (generation !== loadGeneration) return false recordSendError(error, text) return false } diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts index 2c62b0461d3..26257f3f7a6 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.test.ts @@ -292,6 +292,34 @@ describe('Agent workflow resolution', () => { ]) }) + // cloudIdFor reads the name-derived index ahead of the binding store, so + // releasing the binding alone still leaves a refused id resolvable here - + // and refreshCloudWorkflowIds cannot be relied on to drop it, since it + // swallows its errors and the caller races it against a timeout. + it('stops resolving a refused cloud id once it is forgotten', async () => { + const portrait = workflow('workflows/portrait.json', 'Portrait') + const { resolver, bindings } = setup( + [portrait], + [ + { id: 'cloud-portrait', name: 'Portrait' }, + { id: 'cloud-other', name: 'Other' } + ] + ) + await resolver.refreshCloudWorkflowIds() + expect(resolver.cloudIdFor(portrait)).toBe('cloud-portrait') + + bindings.unbindWorkflow('cloud-portrait') + expect(resolver.cloudIdFor(portrait)).toBe('cloud-portrait') + + resolver.forgetCloudWorkflowId('cloud-portrait') + + expect(resolver.cloudIdFor(portrait)).toBeUndefined() + expect(resolver.openTabsSnapshot()).toBeUndefined() + expect(resolver.availableWorkflowReferences.value).toEqual([ + { id: 'cloud-other', name: 'Other' } + ]) + }) + it('rejects a stale binding that points a cloud id at a differently named saved tab', async () => { const portrait = workflow('workflows/portrait.json', 'Portrait') const { resolver, bindings, workflows } = setup( diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts index fe499222eba..5fd59b2bca0 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowResolver.ts @@ -62,6 +62,16 @@ export function useAgentWorkflowResolver({ } } + /** + * Drops a workflow the server has refused. `cloudIdFor` reads this index + * ahead of the binding store, and `refreshCloudWorkflowIds` both swallows + * its errors and races a timeout, so a stale entry would keep handing the + * refused id back to the next turn however often the binding is released. + */ + function forgetCloudWorkflowId(workflowId: string): void { + cloudIndex.value = cloudIndex.value.filter(({ id }) => id !== workflowId) + } + function cloudWorkflowName(workflow: ComfyWorkflow): string { return workflow.suffix === 'app.json' ? `${workflow.filename}.app` @@ -203,6 +213,7 @@ export function useAgentWorkflowResolver({ return { refreshCloudWorkflowIds, + forgetCloudWorkflowId, cloudIdFor, cloudWorkflowName, boundOrOpenWorkflowFor, diff --git a/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.test.ts index b50c61c9761..d3858e1b0eb 100644 --- a/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.test.ts @@ -392,6 +392,23 @@ describe('agentWorkflowTabBindingStore', () => { }) }) + it('unbindWorkflow drops only the named workflow, wherever it now sits', () => { + const store = useAgentWorkflowTabBindingStore() + store.bind('wf-1', 'workflows/a.json') + store.bind('wf-2', 'workflows/b.json') + + store.bind('wf-3', 'workflows/a.json') + store.unbindWorkflow('wf-1') + + expect(store.tabPathFor('wf-3')).toBe('workflows/a.json') + expect(store.workflowIdFor('workflows/a.json')).toBe('wf-3') + expect(store.tabPathFor('wf-2')).toBe('workflows/b.json') + expect(store.tabPathFor('wf-1')).toBeUndefined() + + store.unbindWorkflow('wf-unknown') + expect(store.tabPathFor('wf-3')).toBe('workflows/a.json') + }) + it('resolves both directions after a bind', () => { const store = useAgentWorkflowTabBindingStore() store.bind('wf-1', 'workflows/a.json') diff --git a/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.ts b/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.ts index df8da4a0684..9834b23dda6 100644 --- a/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentWorkflowTabBindingStore.ts @@ -152,6 +152,18 @@ export const useAgentWorkflowTabBindingStore = defineStore( } } + /** + * Drops one workflow's record wherever it sits. Path-keyed `unbind` would + * take whichever workflow occupies the path now, which is the wrong one + * once the tab has been rebound. + */ + function unbindWorkflow(workflowId: string): void { + if (!Object.hasOwn(tabByWorkflow.value, workflowId)) return + delete tabByWorkflow.value[workflowId] + boundInstances.delete(workflowId) + refusedInstances.delete(workflowId) + } + function releaseClosedTab({ tab, path }: OpenTab): void { const workflowId = recordIdFor(path) if (workflowId === undefined) return @@ -222,6 +234,13 @@ export const useAgentWorkflowTabBindingStore = defineStore( : undefined } - return { bind, unbind, matchesWorkflow, tabPathFor, workflowIdFor } + return { + bind, + unbind, + unbindWorkflow, + matchesWorkflow, + tabPathFor, + workflowIdFor + } } )