From dd6d99953e0a808edafb962d486edf410a16d450 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 02:19:29 +0000 Subject: [PATCH 1/9] fix(agent): write agent consent with the workspace credential off-cloud The consent row Comfy.AgentPanel.ConsentAccepted is keyed by BOTH workspace_id and user_id, and cloud's grant check reads it with WorkspaceIDEQ plus UserIDEQ. Off-cloud the write went out under getUserAuthHeader(), a personal session header, which leaves the workspace to resolve on its own rather than pinning the one the row belongs to. .agents/checks/follower-boundary.md already forbids this twice: it flags any fallback to getUserAuthHeader() off-cloud, and it flags credential resolution outside agentAuth.ts. Main was in a state its own check disallows. Moves the choice into agentAuth.ts as agentSettingsAuthHeader(), which is where the one distribution branch lives. Off-cloud it reuses the same workspace bearer every other agent request carries (an API-key session still sends its key); on cloud it keeps the workspace header. The consent store calls that instead of reaching into platform/settings for a credential, which also removes a platform-imports-workbench direction the layering forbids. Tests move with the decision: the globalSettingsApi case that pinned the personal header off-cloud is replaced by three agentAuth cases covering workspace bearer, API key and cloud. Verified red by restoring the fallback. --- .../settings/globalSettingsApi.test.ts | 22 ------------ src/platform/settings/globalSettingsApi.ts | 8 ----- .../agent/services/agent/agentAuth.test.ts | 34 +++++++++++++++++++ .../agent/services/agent/agentAuth.ts | 15 ++++++++ .../stores/agent/agentConsentStore.test.ts | 7 ++-- .../agent/stores/agent/agentConsentStore.ts | 5 +-- 6 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/platform/settings/globalSettingsApi.test.ts b/src/platform/settings/globalSettingsApi.test.ts index 3c3a1016440..de4df559a14 100644 --- a/src/platform/settings/globalSettingsApi.test.ts +++ b/src/platform/settings/globalSettingsApi.test.ts @@ -5,12 +5,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import type { WebSessionSend } from '@/platform/auth/session/webSessionFetch' import { api } from '@/scripts/api' -import { useAuthStore } from '@/stores/authStore' import { GlobalSettingsApiError, getGlobalSetting, - getGlobalSettingsAuthHeader, setGlobalSetting } from './globalSettingsApi' @@ -140,26 +138,6 @@ describe('Global Settings transport', () => { ).rejects.toMatchObject({ status: 500 }) }) - it.for([ - { cloud: true, expected: 'Bearer workspace-token' }, - { cloud: false, expected: 'Bearer user-token' } - ])( - 'authenticates Cloud=$cloud with $expected', - async ({ cloud, expected }) => { - distribution.isCloud = cloud - const authStore = useAuthStore() - vi.mocked(authStore.getWorkspaceAuthHeader).mockResolvedValue({ - Authorization: 'Bearer workspace-token' - }) - vi.mocked(authStore.getUserAuthHeader).mockResolvedValue({ - Authorization: 'Bearer user-token' - }) - - await expect(getGlobalSettingsAuthHeader()).resolves.toEqual({ - Authorization: expected - }) - } - ) describe('on the web session', () => { const unavailable = () => new Response(null, { status: 503 }) diff --git a/src/platform/settings/globalSettingsApi.ts b/src/platform/settings/globalSettingsApi.ts index be7038d04cd..98a405f1b04 100644 --- a/src/platform/settings/globalSettingsApi.ts +++ b/src/platform/settings/globalSettingsApi.ts @@ -13,7 +13,6 @@ import { import { isCloud } from '@/platform/distribution/types' import { api } from '@/scripts/api' import type { WebSessionSend } from '@/platform/auth/session/webSessionFetch' -import { useAuthStore } from '@/stores/authStore' import type { AuthHeader } from '@/types/authTypes' import { retryingSend } from '@/utils/fetchWithRetry' @@ -32,13 +31,6 @@ function globalSettingsUrl(key?: GlobalSettingKey): string { return isCloud ? api.apiURL(path) : `${getComfyApiBaseUrl()}/api${path}` } -export async function getGlobalSettingsAuthHeader(): Promise { - const authStore = useAuthStore() - return isCloud - ? authStore.getWorkspaceAuthHeader() - : authStore.getUserAuthHeader() -} - async function responseBody(response: Response): Promise { try { return await response.json() diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts index a6b8b81803e..89e372987bf 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts @@ -17,6 +17,7 @@ import type { AuthCredential, AuthHeader, AuthScheme } from '@/types/authTypes' import { agentSocketAuthParams, ensureSignedIn, + agentSettingsAuthHeader, withAgentAuth } from './agentAuth' @@ -68,6 +69,39 @@ function signInWithout(token: undefined | Error, apiKey: boolean) { workspaceToken(token) } +describe('agentSettingsAuthHeader', () => { + // The consent row is keyed by workspace_id AND user_id, so the credential has to + // pin the workspace. Off-cloud this used to fall back to the personal header, which + // left the workspace to resolve on its own. + it('carries the workspace bearer off-cloud, not the personal header', async () => { + signInPersonal() + workspaceToken('workspace-token') + + await expect(agentSettingsAuthHeader()).resolves.toEqual({ + Authorization: 'Bearer workspace-token' + }) + }) + + it('sends a stored API key as itself off-cloud', async () => { + storedApiKey('comfyui-key') + + await expect(agentSettingsAuthHeader()).resolves.toEqual({ + 'X-API-KEY': 'comfyui-key' + }) + }) + + it('uses the workspace header on cloud', async () => { + distribution.isCloud = true + vi.spyOn(useAuthStore(), 'getWorkspaceAuthHeader').mockResolvedValue({ + Authorization: 'Bearer cloud-workspace' + }) + + await expect(agentSettingsAuthHeader()).resolves.toEqual({ + Authorization: 'Bearer cloud-workspace' + }) + }) +}) + describe('withAgentAuth', () => { it.for<{ account: string diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.ts b/src/workbench/extensions/agent/services/agent/agentAuth.ts index 3e7f7b4ccd5..3e56d6d2b89 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.ts @@ -34,6 +34,21 @@ async function agentAuthHeader(): Promise { } } +/** + * Credential for the agent's Global Settings rows, which today means consent. + * + * The row (`Comfy.AgentPanel.ConsentAccepted`) is keyed by BOTH `workspace_id` and + * `user_id`, and cloud's grant check reads it with `WorkspaceIDEQ` plus `UserIDEQ`, so + * the credential has to pin the workspace as well as identify the user. Off-cloud this + * is the same workspace bearer every other agent request carries: a personal session + * header would leave the workspace to resolve on its own and write the row under the + * wrong one. It lives here because this file owns the one distribution branch. + */ +export async function agentSettingsAuthHeader(): Promise { + if (isCloud) return useAuthStore().getWorkspaceAuthHeader() + return agentAuthHeader() +} + export async function withAgentAuth( init: AgentRequestInit ): Promise { diff --git a/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts index f7ecd2c6105..bed58c6363a 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConsentStore.test.ts @@ -20,9 +20,12 @@ const accountApi = vi.hoisted(() => ({ })) vi.mock(import('@/platform/settings/globalSettingsApi'), () => ({ getGlobalSetting: accountApi.get, - setGlobalSetting: accountApi.set, - getGlobalSettingsAuthHeader: accountApi.header + setGlobalSetting: accountApi.set })) +vi.mock( + import('@/workbench/extensions/agent/services/agent/agentAuth'), + () => ({ agentSettingsAuthHeader: accountApi.header }) +) vi.mock(import('@/composables/auth/useCurrentUser')) const stored: GlobalSetting = { diff --git a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts index ef01a2a970c..eefe9737f97 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts @@ -1,4 +1,6 @@ import { defineStore } from 'pinia' + +import { agentSettingsAuthHeader } from '@/workbench/extensions/agent/services/agent/agentAuth' import { computed, ref, watch } from 'vue' import { useCurrentUser } from '@/composables/auth/useCurrentUser' @@ -9,7 +11,6 @@ import { import { AGENT_CONSENT_SETTING_ID } from '@/platform/settings/constants/agent' import { getGlobalSetting, - getGlobalSettingsAuthHeader, setGlobalSetting } from '@/platform/settings/globalSettingsApi' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' @@ -94,7 +95,7 @@ export const useAgentConsentStore = defineStore('agentConsent', () => { async function requireAuth(sessionId: number, identity: string) { const send = webSessionRequests() ? await webSessionSend() : undefined - const auth = send ?? (await getGlobalSettingsAuthHeader()) + const auth = send ?? (await agentSettingsAuthHeader()) if (!stillOwns(sessionId, identity)) return null if (!auth) { throw new AgentConsentAuthenticationError( From c6e2d6e098874d8f1be5809b1663e79790b51a98 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 03:22:26 +0000 Subject: [PATCH 2/9] test(agent): add agentSettingsAuthHeader to the agentAuth manual mock The consent store now reads its credential through agentSettingsAuthHeader. Suites that mock the agentAuth module get the manual mock, which did not export it, so every consent path in them hit "undefined is not a function". --- .../extensions/agent/services/agent/__mocks__/agentAuth.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts index 2563c1635c8..b6a63a924d2 100644 --- a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts @@ -1,6 +1,7 @@ import { vi } from 'vitest' import type { + agentSettingsAuthHeader as realAgentSettingsAuthHeader, agentSocketAuthParams as realAgentSocketAuthParams, ensureSignedIn as realEnsureSignedIn, hasAgentCredential as realHasAgentCredential, @@ -11,6 +12,12 @@ export const withAgentAuth = vi.fn( async (init) => init ) +// Consent reads its credential through this. A suite that mocks this module and then +// touches the consent store gets `undefined is not a function` without it. +export const agentSettingsAuthHeader = vi.fn( + async () => ({ Authorization: 'Bearer workspace-token' }) +) + export const agentSocketAuthParams = vi.fn( async () => new URLSearchParams() ) From 114f76072fe39c2fb03c51a3df3501fe97eeec1e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Wed, 28 Oct 2026 03:25:31 +0000 Subject: [PATCH 3/9] [automated] Apply ESLint and Oxfmt fixes --- .../extensions/agent/services/agent/__mocks__/agentAuth.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts index b6a63a924d2..d5e877fdeba 100644 --- a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts @@ -14,9 +14,9 @@ export const withAgentAuth = vi.fn( // Consent reads its credential through this. A suite that mocks this module and then // touches the consent store gets `undefined is not a function` without it. -export const agentSettingsAuthHeader = vi.fn( - async () => ({ Authorization: 'Bearer workspace-token' }) -) +export const agentSettingsAuthHeader = vi.fn< + typeof realAgentSettingsAuthHeader +>(async () => ({ Authorization: 'Bearer workspace-token' })) export const agentSocketAuthParams = vi.fn( async () => new URLSearchParams() From 570586abc2776030e799b43500a8adfb6414d56e Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 03:27:24 +0000 Subject: [PATCH 4/9] fix(agent): resolve null instead of rejecting when no workspace credential agentAuthHeader() rejects rather than resolving null when a signed-in user's workspace token cannot be minted -- a failed exchange, or a tab that is not on the provided web session. Consent reads null as "authentication required", so the rejection reached it as a generic load/write failure plus a reportError where the old personal-header path had succeeded. --- .../agent/services/agent/agentAuth.test.ts | 20 ++++++++++++++++ .../agent/services/agent/agentAuth.ts | 23 +++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts index 89e372987bf..8f6fb5e8008 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts @@ -100,6 +100,26 @@ describe('agentSettingsAuthHeader', () => { Authorization: 'Bearer cloud-workspace' }) }) + + // Minting rejects rather than resolving null. Consent reads null as "authentication + // required"; a rejection reaches it as a generic load/write failure instead. + it.for(missingWorkspaceTokens)( + 'resolves null rather than rejecting with $label', + async ({ token, apiKey }) => { + signInWithout(token, apiKey) + + await expect(agentSettingsAuthHeader()).resolves.toBeNull() + } + ) + + it('resolves null when the cloud workspace header cannot be obtained', async () => { + distribution.isCloud = true + vi.spyOn(useAuthStore(), 'getWorkspaceAuthHeader').mockRejectedValue( + new Error('token exchange failed') + ) + + await expect(agentSettingsAuthHeader()).resolves.toBeNull() + }) }) describe('withAgentAuth', () => { diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.ts b/src/workbench/extensions/agent/services/agent/agentAuth.ts index 3e56d6d2b89..b66452ddf17 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.ts @@ -38,15 +38,24 @@ async function agentAuthHeader(): Promise { * Credential for the agent's Global Settings rows, which today means consent. * * The row (`Comfy.AgentPanel.ConsentAccepted`) is keyed by BOTH `workspace_id` and - * `user_id`, and cloud's grant check reads it with `WorkspaceIDEQ` plus `UserIDEQ`, so - * the credential has to pin the workspace as well as identify the user. Off-cloud this - * is the same workspace bearer every other agent request carries: a personal session - * header would leave the workspace to resolve on its own and write the row under the - * wrong one. It lives here because this file owns the one distribution branch. + * `user_id`, so the credential has to pin the workspace as well as identify the user. + * A personal session header would leave the workspace to resolve on its own and write + * the row under the wrong one. + * + * Resolves `null` rather than rejecting when no workspace credential can be obtained: + * callers read `null` as "authentication required", and `agentAuthHeader()` rejects + * (not resolves) when a signed-in user's workspace token cannot be minted -- a failed + * exchange, or a tab that is not on the provided web session. Letting that through + * would surface a generic load/write failure plus a `reportError` where the caller + * wants its authentication branch. */ export async function agentSettingsAuthHeader(): Promise { - if (isCloud) return useAuthStore().getWorkspaceAuthHeader() - return agentAuthHeader() + try { + if (isCloud) return await useAuthStore().getWorkspaceAuthHeader() + return await agentAuthHeader() + } catch { + return null + } } export async function withAgentAuth( From 1e1790537703a0b752a8e74debff242651b09393 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 06:14:12 +0000 Subject: [PATCH 5/9] test(agent): drive useAgentConsent with the workspace credential The suite mocked getUserAuthHeader, the personal credential this change replaces, and nothing mocked the workspace token, so every consent read resolved without auth and 28 of 36 tests sat waiting for a card that never opened. It now mints a workspace token, asserts the workspace bearer on the save, and exercises missing auth by failing the mint rather than the personal header. --- .../agent/composables/agent/useAgentConsent.test.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index 4ac77e0ada0..32d65221527 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -140,6 +140,12 @@ describe('useAgentConsent', () => { vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValue({ Authorization: 'Bearer account-a-token' }) + // Consent is written with the workspace credential, not the personal one: the + // row is keyed by workspace_id and user_id together. + Object.assign(useAuthStore(), { isAuthenticated: true }) + vi.mocked(useAuthStore().getWorkspaceAuthTokenOrThrow).mockResolvedValue( + 'workspace-a-token' + ) vi.mocked(api.fetchApi).mockReset() fetchWithUnifiedRemint.mockReset() fetchWithUnifiedRemint.mockResolvedValue(settingResponse(false)) @@ -560,7 +566,7 @@ describe('useAgentConsent', () => { expect.objectContaining({ method: 'POST', headers: expect.objectContaining({ - Authorization: 'Bearer account-a-token' + Authorization: 'Bearer workspace-a-token' }), body: JSON.stringify({ key: stored.key, value: true }) }), @@ -621,7 +627,9 @@ describe('useAgentConsent', () => { const onOpen = vi.fn() const request = useAgentConsent().withConsent('button_click', onOpen) const dialog = await waitForConsentDialog() - vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValueOnce(null) + vi.mocked( + useAuthStore().getWorkspaceAuthTokenOrThrow + ).mockRejectedValueOnce(new Error('token exchange failed')) ;(dialog.contentProps.onAccept as () => void)() await vi.waitFor(() => From 4d6d3c30bcb1a0f70e8253d5859f2db4a5ac72a5 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 06:53:44 +0000 Subject: [PATCH 6/9] test(agent): pin that no workspace token means no consent write Guards the security property this change exists for: a signed-in user who has a personal credential but cannot mint a workspace token must not fall back to the personal header. The row would land under the wrong workspace, and the grant check reads workspace_id and user_id together, so the user would appear never to have consented. --- .../composables/agent/useAgentConsent.test.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index 32d65221527..60794e01c2d 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -623,6 +623,35 @@ describe('useAgentConsent', () => { expect(reportError).toHaveBeenCalledOnce() }) + // The point of writing consent with the workspace credential is that a user who + // has a personal one but no workspace token must not quietly fall back to it: the + // row would land under the wrong workspace, and the grant check reads workspace_id + // and user_id together, so the user would appear not to have consented. + it('writes no consent when the account has no workspace token', async () => { + const onOpen = vi.fn() + void useAgentConsent().withConsent('button_click', onOpen) + const dialog = await waitForConsentDialog() + vi.mocked(useAuthStore().getWorkspaceAuthTokenOrThrow).mockRejectedValue( + new Error('token exchange failed') + ) + fetchWithUnifiedRemint.mockClear() + + ;(dialog.contentProps.onAccept as () => void)() + await vi.waitFor(() => + expect(dialog.contentProps.error).toBe( + i18n.global.t('agent.consent.saveError') + ) + ) + + expect(fetchWithUnifiedRemint).not.toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ method: 'POST' }), + expect.anything() + ) + expect(useAuthStore().getUserAuthHeader).not.toHaveBeenCalled() + expect(onOpen).not.toHaveBeenCalled() + }) + it('keeps a missing-auth save retryable in the same account', async () => { const onOpen = vi.fn() const request = useAgentConsent().withConsent('button_click', onOpen) From a4ed7a2f55761185439a4d8faa78ad85694248e0 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 07:35:21 +0000 Subject: [PATCH 7/9] test(agent): settle the no-workspace-token request before the test ends A failed save leaves the card open, so the request and its scope watcher outlived the test. Close the dialog and await the promise. --- .../agent/composables/agent/useAgentConsent.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index 60794e01c2d..10f66388eaa 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -629,7 +629,7 @@ describe('useAgentConsent', () => { // and user_id together, so the user would appear not to have consented. it('writes no consent when the account has no workspace token', async () => { const onOpen = vi.fn() - void useAgentConsent().withConsent('button_click', onOpen) + const request = useAgentConsent().withConsent('button_click', onOpen) const dialog = await waitForConsentDialog() vi.mocked(useAuthStore().getWorkspaceAuthTokenOrThrow).mockRejectedValue( new Error('token exchange failed') @@ -650,6 +650,11 @@ describe('useAgentConsent', () => { ) expect(useAuthStore().getUserAuthHeader).not.toHaveBeenCalled() expect(onOpen).not.toHaveBeenCalled() + + // A failed save leaves the card open, so settle the request rather than letting + // it and its scope watcher outlive the test. + useDialogStore().closeDialog({ key: 'agent-consent' }) + await request }) it('keeps a missing-auth save retryable in the same account', async () => { From 6d2d6949490e14dafd24fc04a01084e0b6aef60c Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 20:26:04 -0700 Subject: [PATCH 8/9] test(agent): prove workspace-scoped consent persistence --- .../fixtures/desktopAgentConsentFixture.ts | 19 ++++- .../tests/agent/desktopAgentConsent.spec.ts | 69 +++++++++++++++++++ .../composables/agent/useAgentConsent.test.ts | 8 --- .../services/agent/__mocks__/agentAuth.ts | 2 - .../agent/services/agent/agentAuth.test.ts | 5 -- .../agent/services/agent/agentAuth.ts | 16 +---- .../agent/stores/agent/agentConsentStore.ts | 5 +- 7 files changed, 88 insertions(+), 36 deletions(-) diff --git a/browser_tests/fixtures/desktopAgentConsentFixture.ts b/browser_tests/fixtures/desktopAgentConsentFixture.ts index cabaa1b1f9f..83a63623dd4 100644 --- a/browser_tests/fixtures/desktopAgentConsentFixture.ts +++ b/browser_tests/fixtures/desktopAgentConsentFixture.ts @@ -16,6 +16,7 @@ declare global { class DesktopConsentSignIn { requests = 0 + readonly consentAuthorizationHeaders: string[] = [] private resolve: (state: ComfyDesktop2AuthState) => void = () => {} private readonly result = new Promise((resolve) => { this.resolve = resolve @@ -40,16 +41,18 @@ export const desktopAgentConsentTest = mergeTests( webSocketFixture ).extend<{ desktopConsentSignIn: DesktopConsentSignIn + desktopWorkspaceToken: string | null }>({ desktopConsentSignIn: async ({ browserName: _browserName }, use) => { await use(new DesktopConsentSignIn()) }, - page: async ({ page, desktopConsentSignIn }, use) => { + desktopWorkspaceToken: ['mock-workspace-token', { option: true }], + page: async ({ page, desktopConsentSignIn, desktopWorkspaceToken }, use) => { await mockWorkspace(page, workspace('personal', 'owner'), []) await page.exposeFunction('__desktopConsentSignIn', () => desktopConsentSignIn.request() ) - await page.addInitScript(() => { + await page.addInitScript((workspaceToken) => { let state: ComfyDesktop2AuthState = { status: 'signed_out' } const listeners = new Set<(next: ComfyDesktop2AuthState) => void>() const push = (next: ComfyDesktop2AuthState) => { @@ -67,7 +70,7 @@ export const desktopAgentConsentTest = mergeTests( getState: async () => state, getWorkspaceToken: async (workspaceId: string) => state.status === 'signed_in' && state.workspaceId === workspaceId - ? 'mock-workspace-token' + ? workspaceToken : null, requestSignIn: async () => push(await window.__desktopConsentSignIn()), signOut: async () => push({ status: 'signed_out' }), @@ -83,6 +86,16 @@ export const desktopAgentConsentTest = mergeTests( Auth: auth } }) + }, desktopWorkspaceToken) + page.on('request', (request) => { + if ( + request.method() === 'POST' && + new URL(request.url()).pathname.endsWith('/api/global-settings') + ) { + desktopConsentSignIn.consentAuthorizationHeaders.push( + request.headers()['authorization'] ?? '' + ) + } }) await use(page) } diff --git a/browser_tests/tests/agent/desktopAgentConsent.spec.ts b/browser_tests/tests/agent/desktopAgentConsent.spec.ts index c7b2b8092aa..7e91a85ea41 100644 --- a/browser_tests/tests/agent/desktopAgentConsent.spec.ts +++ b/browser_tests/tests/agent/desktopAgentConsent.spec.ts @@ -93,6 +93,52 @@ test.describe('Desktop Agent consent', { tag: ['@desktop', '@ui'] }, () => { }) }) + test.describe('without a workspace credential', () => { + test.use({ desktopWorkspaceToken: null }) + + test('refuses to write consent with the personal credential', async ({ + comfyPage, + agentPanel, + desktopConsentSignIn, + agentConsentWrites, + postedMessages + }) => { + const signIn = comfyPage.page.getByTestId('desktop-host-sso') + const message = 'Keep this draft when the workspace credential is missing' + const semanticGraphBefore = await comfyPage.page.evaluate(() => + JSON.stringify(window.app!.graph.serialize()) + ) + + await agentPanel.consentRejectButton.click() + await agentPanel.composer.fill(message) + await agentPanel.sendButton.click() + await expect(agentPanel.consentDialog).toBeVisible() + await agentPanel.consentDialog + .getByRole('button', { + name: enMessages.agent.consent.agreeAndContinue + }) + .click() + await expect(signIn).toBeVisible() + await signIn.click() + await expect.poll(() => desktopConsentSignIn.requests).toBe(1) + desktopConsentSignIn.complete() + + await expect(agentPanel.consentDialog).toHaveCount(0) + await expect(agentPanel.root).toBeVisible() + await expect(agentPanel.composer).toBeVisible() + expect(agentConsentWrites).toEqual([]) + expect(postedMessages).toEqual([]) + expect(desktopConsentSignIn.consentAuthorizationHeaders).toEqual([]) + await expect + .poll(() => + comfyPage.page.evaluate(() => + JSON.stringify(window.app!.graph.serialize()) + ) + ) + .toBe(semanticGraphBefore) + }) + }) + test('sends the original signed-out submission once after sign-in and confirmed consent persistence', async ({ comfyPage, agentPanel, @@ -104,6 +150,9 @@ test.describe('Desktop Agent consent', { tag: ['@desktop', '@ui'] }, () => { }) => { const signIn = comfyPage.page.getByTestId('desktop-host-sso') const message = 'Continue this Desktop submission after sign-in' + const semanticGraphBefore = await comfyPage.page.evaluate(() => + JSON.stringify(window.app!.graph.serialize()) + ) let releaseSave = () => {} agentConsentSave.pending = new Promise((resolve) => { releaseSave = resolve @@ -136,6 +185,16 @@ test.describe('Desktop Agent consent', { tag: ['@desktop', '@ui'] }, () => { await expect.poll(() => agentConsentWrites).toEqual([true]) await expect(signIn).toHaveCount(0) await expect(agentPanel.consentDialog).toHaveCount(0) + expect(desktopConsentSignIn.consentAuthorizationHeaders).toEqual([ + 'Bearer mock-workspace-token' + ]) + await expect + .poll(() => + comfyPage.page.evaluate(() => + JSON.stringify(window.app!.graph.serialize()) + ) + ) + .toBe(semanticGraphBefore) expect(postedMessages).toEqual([]) }) @@ -155,6 +214,16 @@ test.describe('Desktop Agent consent', { tag: ['@desktop', '@ui'] }, () => { await expect(agentPanel.sendButton).toBeVisible() await expect(agentPanel.consentDialog).toHaveCount(0) expect(agentConsentWrites).toEqual([true]) + expect(desktopConsentSignIn.consentAuthorizationHeaders).toEqual([ + 'Bearer mock-workspace-token' + ]) + await expect + .poll(() => + comfyPage.page.evaluate(() => + JSON.stringify(window.app!.graph.serialize()) + ) + ) + .toBe(semanticGraphBefore) expect(postedMessages).toHaveLength(1) }) } finally { diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index 10f66388eaa..17a6a282f9b 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -140,8 +140,6 @@ describe('useAgentConsent', () => { vi.mocked(useAuthStore().getUserAuthHeader).mockResolvedValue({ Authorization: 'Bearer account-a-token' }) - // Consent is written with the workspace credential, not the personal one: the - // row is keyed by workspace_id and user_id together. Object.assign(useAuthStore(), { isAuthenticated: true }) vi.mocked(useAuthStore().getWorkspaceAuthTokenOrThrow).mockResolvedValue( 'workspace-a-token' @@ -623,10 +621,6 @@ describe('useAgentConsent', () => { expect(reportError).toHaveBeenCalledOnce() }) - // The point of writing consent with the workspace credential is that a user who - // has a personal one but no workspace token must not quietly fall back to it: the - // row would land under the wrong workspace, and the grant check reads workspace_id - // and user_id together, so the user would appear not to have consented. it('writes no consent when the account has no workspace token', async () => { const onOpen = vi.fn() const request = useAgentConsent().withConsent('button_click', onOpen) @@ -651,8 +645,6 @@ describe('useAgentConsent', () => { expect(useAuthStore().getUserAuthHeader).not.toHaveBeenCalled() expect(onOpen).not.toHaveBeenCalled() - // A failed save leaves the card open, so settle the request rather than letting - // it and its scope watcher outlive the test. useDialogStore().closeDialog({ key: 'agent-consent' }) await request }) diff --git a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts index d5e877fdeba..9c277871eb6 100644 --- a/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/__mocks__/agentAuth.ts @@ -12,8 +12,6 @@ export const withAgentAuth = vi.fn( async (init) => init ) -// Consent reads its credential through this. A suite that mocks this module and then -// touches the consent store gets `undefined is not a function` without it. export const agentSettingsAuthHeader = vi.fn< typeof realAgentSettingsAuthHeader >(async () => ({ Authorization: 'Bearer workspace-token' })) diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts index 8f6fb5e8008..00e260f8c99 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.test.ts @@ -70,9 +70,6 @@ function signInWithout(token: undefined | Error, apiKey: boolean) { } describe('agentSettingsAuthHeader', () => { - // The consent row is keyed by workspace_id AND user_id, so the credential has to - // pin the workspace. Off-cloud this used to fall back to the personal header, which - // left the workspace to resolve on its own. it('carries the workspace bearer off-cloud, not the personal header', async () => { signInPersonal() workspaceToken('workspace-token') @@ -101,8 +98,6 @@ describe('agentSettingsAuthHeader', () => { }) }) - // Minting rejects rather than resolving null. Consent reads null as "authentication - // required"; a rejection reaches it as a generic load/write failure instead. it.for(missingWorkspaceTokens)( 'resolves null rather than rejecting with $label', async ({ token, apiKey }) => { diff --git a/src/workbench/extensions/agent/services/agent/agentAuth.ts b/src/workbench/extensions/agent/services/agent/agentAuth.ts index b66452ddf17..517dce474ce 100644 --- a/src/workbench/extensions/agent/services/agent/agentAuth.ts +++ b/src/workbench/extensions/agent/services/agent/agentAuth.ts @@ -34,21 +34,7 @@ async function agentAuthHeader(): Promise { } } -/** - * Credential for the agent's Global Settings rows, which today means consent. - * - * The row (`Comfy.AgentPanel.ConsentAccepted`) is keyed by BOTH `workspace_id` and - * `user_id`, so the credential has to pin the workspace as well as identify the user. - * A personal session header would leave the workspace to resolve on its own and write - * the row under the wrong one. - * - * Resolves `null` rather than rejecting when no workspace credential can be obtained: - * callers read `null` as "authentication required", and `agentAuthHeader()` rejects - * (not resolves) when a signed-in user's workspace token cannot be minted -- a failed - * exchange, or a tab that is not on the provided web session. Letting that through - * would surface a generic load/write failure plus a `reportError` where the caller - * wants its authentication branch. - */ +/** Returns the credential for Agent-owned Global Settings rows. */ export async function agentSettingsAuthHeader(): Promise { try { if (isCloud) return await useAuthStore().getWorkspaceAuthHeader() diff --git a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts index eefe9737f97..a54923b3cbf 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConsentStore.ts @@ -1,7 +1,5 @@ -import { defineStore } from 'pinia' - -import { agentSettingsAuthHeader } from '@/workbench/extensions/agent/services/agent/agentAuth' import { computed, ref, watch } from 'vue' +import { defineStore } from 'pinia' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import { @@ -14,6 +12,7 @@ import { setGlobalSetting } from '@/platform/settings/globalSettingsApi' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' +import { agentSettingsAuthHeader } from '@/workbench/extensions/agent/services/agent/agentAuth' class AgentConsentAuthenticationError extends Error { override name = 'AgentConsentAuthenticationError' From ace3c186d47d3b523675dc8bd07403e85f9a3b70 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Wed, 28 Oct 2026 22:09:19 -0700 Subject: [PATCH 9/9] test(agent): assert typed consent save error --- .../agent/composables/agent/useAgentConsent.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts index beb119f2605..ff720913ada 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentConsent.test.ts @@ -708,11 +708,9 @@ describe('useAgentConsent', () => { fetchWithUnifiedRemint.mockClear() ;(dialog.contentProps.onAccept as () => void)() - await vi.waitFor(() => - expect(dialog.contentProps.error).toBe( - i18n.global.t('agent.consent.saveError') - ) - ) + await vi.waitFor(() => { + expect(dialog.contentProps.error).toBe('save') + }) expect(fetchWithUnifiedRemint).not.toHaveBeenCalledWith( expect.anything(),