From 231e41ebdab0f3351b7125b33a380ef7affe0018 Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 08:11:32 +0000 Subject: [PATCH 1/5] fix(agent): share one hosted-agent gate between the skill picker and the agent panel The skill-packs store read ComfyUI's `agent` server feature on its own, so the standalone harness (VITE_AGENT_STANDALONE) turned skill packs on from ComfyUI's agent while the agent panel ignored it. Both stores now read the feature through readComfyAgentFeature, which keeps the standalone guard. Also covers the case where the PostHog cohort flags turn on while the hosted agent is already ready: the catalog is listed only once. Follow-up to #21579 (BE-22010). --- src/composables/__mocks__/useFeatureFlags.ts | 1 - src/composables/useFeatureFlags.ts | 6 ---- .../remoteConfig/comfyAgentFeature.ts | 28 ++++++++++++++++++ .../skills/stores/skillPacksStore.test.ts | 29 +++++++++++++++++++ src/platform/skills/stores/skillPacksStore.ts | 9 +++--- .../agent/stores/agent/agentPanelStore.ts | 15 +++------- 6 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 src/platform/remoteConfig/comfyAgentFeature.ts diff --git a/src/composables/__mocks__/useFeatureFlags.ts b/src/composables/__mocks__/useFeatureFlags.ts index 305db1df8cf..c8f221522cf 100644 --- a/src/composables/__mocks__/useFeatureFlags.ts +++ b/src/composables/__mocks__/useFeatureFlags.ts @@ -11,7 +11,6 @@ const defaultFlags: FeatureFlags = { supportsPreviewMetadata: false, maxUploadSize: 0, supportsManagerV4: false, - agent: undefined, modelUploadButtonEnabled: false, assetDeletionEnabled: false, privateModelsEnabled: false, diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index ff218d9871d..901f9fb22bc 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -137,12 +137,6 @@ export function useFeatureFlags() { get supportsManagerV4() { return api.getServerFeature(ServerFeatureFlag.MANAGER_SUPPORTS_V4) }, - /** ComfyUI's `agent` server feature; callers validate `state`. */ - get agent() { - return api.getServerFeature<{ state?: unknown } | null | undefined>( - ServerFeatureFlag.AGENT - ) - }, get modelUploadButtonEnabled() { return resolveFlag( ServerFeatureFlag.MODEL_UPLOAD_BUTTON_ENABLED, diff --git a/src/platform/remoteConfig/comfyAgentFeature.ts b/src/platform/remoteConfig/comfyAgentFeature.ts new file mode 100644 index 00000000000..618c77916a2 --- /dev/null +++ b/src/platform/remoteConfig/comfyAgentFeature.ts @@ -0,0 +1,28 @@ +import { ServerFeatureFlag } from '@/platform/remoteConfig/serverFeatureFlag' +import { api } from '@/scripts/api' + +export interface ComfyAgentFeature { + state: 'starting' | 'ready' | 'failed' + reason?: string | null +} + +/** + * ComfyUI's `agent` server feature, or undefined while its state is unknown. + * The standalone harness runs its own agent, so it ignores ComfyUI's. + */ +export function readComfyAgentFeature(): ComfyAgentFeature | undefined { + if (import.meta.env.VITE_AGENT_STANDALONE === 'true') return undefined + const agent = api.getServerFeature<{ state?: unknown } | null | undefined>( + ServerFeatureFlag.AGENT + ) + return agent?.state === 'starting' || + agent?.state === 'ready' || + agent?.state === 'failed' + ? (agent as ComfyAgentFeature) + : undefined +} + +/** Whether ComfyUI hosts an agent that is ready to serve requests. */ +export function isComfyAgentReady(): boolean { + return readComfyAgentFeature()?.state === 'ready' +} diff --git a/src/platform/skills/stores/skillPacksStore.test.ts b/src/platform/skills/stores/skillPacksStore.test.ts index 5f66a00e153..b27ca0ef613 100644 --- a/src/platform/skills/stores/skillPacksStore.test.ts +++ b/src/platform/skills/stores/skillPacksStore.test.ts @@ -368,6 +368,35 @@ describe('skillPacksStore', () => { expect(listSkillPacks).toHaveBeenCalledOnce() }) + it('lists once when the cohort flags turn on while the hosted agent is ready', async () => { + mocks.isFeatureEnabled.mockReturnValue(undefined) + hostAgent('ready') + const store = useSkillPacksStore() + + await store.startFlagGate() + await vi.waitFor(() => expect(store.hasLoaded).toBe(true)) + + mocks.isFeatureEnabled.mockReturnValue(true) + const [onFlags] = mocks.onFeatureFlags.mock.calls[0] + onFlags([], {}, {}) + await nextTick() + + expect(store.flagsEnabled).toBe(true) + expect(listSkillPacks).toHaveBeenCalledOnce() + }) + + it("ignores ComfyUI's agent in the standalone harness, as the agent panel does", async () => { + vi.stubEnv('VITE_AGENT_STANDALONE', 'true') + mocks.isFeatureEnabled.mockReturnValue(undefined) + hostAgent('ready') + const store = useSkillPacksStore() + + await store.startFlagGate() + + expect(store.enabled).toBe(false) + expect(listSkillPacks).not.toHaveBeenCalled() + }) + it('hides the hosted-agent surface when its routes answer 404', async () => { mocks.isFeatureEnabled.mockReturnValue(undefined) vi.mocked(listSkillPacks).mockRejectedValue( diff --git a/src/platform/skills/stores/skillPacksStore.ts b/src/platform/skills/stores/skillPacksStore.ts index 6da62f8dcd8..de35eb04287 100644 --- a/src/platform/skills/stores/skillPacksStore.ts +++ b/src/platform/skills/stores/skillPacksStore.ts @@ -2,6 +2,7 @@ import { defineStore } from 'pinia' import { computed, ref, watch } from 'vue' import { useCurrentUser } from '@/composables/auth/useCurrentUser' +import { isComfyAgentReady } from '@/platform/remoteConfig/comfyAgentFeature' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { api } from '@/scripts/api' @@ -32,11 +33,9 @@ export const useSkillPacksStore = defineStore('skillPacks', () => { // Disabled backend gates answer 404, independently of PostHog flags. const routesAvailable = ref(true) - const agentHosted = computed( - () => - api.getServerFeature<{ state?: unknown } | null>('agent')?.state === - 'ready' - ) + // Shares the agent panel's gate. Cloud does not report the feature, so its + // PostHog flags stay the only gate there today. + const agentHosted = computed(isComfyAgentReady) const surfaceEnabled = computed(() => flagsEnabled.value || agentHosted.value) const enabled = computed(() => surfaceEnabled.value && routesAvailable.value) diff --git a/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts b/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts index 030faaec275..fa511988cc4 100644 --- a/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts @@ -11,7 +11,7 @@ import { watch } from 'vue' -import { useFeatureFlags } from '@/composables/useFeatureFlags' +import { readComfyAgentFeature } from '@/platform/remoteConfig/comfyAgentFeature' import { SIDEBAR_MIN_WIDTH, SIDE_TOOLBAR_WIDTH @@ -86,18 +86,11 @@ export const useAgentPanelStore = defineStore('agentPanel', () => { const gateSettled = ref(false) const heldSendGate = ref(null) const view = shallowRef({ screen: 'chat' }) - const { flags: featureFlags } = useFeatureFlags() const flagsSettled = computed(() => api.serverFeatureFlagsSettled.value) /** The server's `agent` feature; undefined unless its state is known. */ - const comfyAgent = computed(() => { - if (import.meta.env.VITE_AGENT_STANDALONE === 'true') return undefined - const agent = featureFlags.agent - return agent?.state === 'starting' || - agent?.state === 'ready' || - agent?.state === 'failed' - ? (agent as AgentServerFeature) - : undefined - }) + const comfyAgent = computed( + readComfyAgentFeature + ) const maximized = ref(false) const draggedWidth = ref(PANEL_MIN_WIDTH) const reservedWorkspaceWidth = ref(SIDE_TOOLBAR_WIDTH + SIDEBAR_MIN_WIDTH) From e2837ca872466a028e775da64f804e5f37ed0b6d Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 08:29:46 +0000 Subject: [PATCH 2/5] fix(agent): drop a non-string agent reason, cover standalone flag enablement Address review: readComfyAgentFeature keeps a served feature only when its reason is a string, null or absent, and otherwise returns just its state. The standalone-harness test now also turns the cohort flags on and checks that the catalog is listed once. Removes an inline comment in skillPacksStore; the rationale is in the PR description. --- .../remoteConfig/comfyAgentFeature.test.ts | 49 +++++++++++++++++++ .../remoteConfig/comfyAgentFeature.ts | 20 +++++--- .../skills/stores/skillPacksStore.test.ts | 10 +++- src/platform/skills/stores/skillPacksStore.ts | 2 - 4 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 src/platform/remoteConfig/comfyAgentFeature.test.ts diff --git a/src/platform/remoteConfig/comfyAgentFeature.test.ts b/src/platform/remoteConfig/comfyAgentFeature.test.ts new file mode 100644 index 00000000000..f5336cdc414 --- /dev/null +++ b/src/platform/remoteConfig/comfyAgentFeature.test.ts @@ -0,0 +1,49 @@ +import { describe, expect, it, vi } from 'vitest' + +import { api } from '@/scripts/api' + +import { isComfyAgentReady, readComfyAgentFeature } from './comfyAgentFeature' + +vi.mock(import('@/scripts/api')) + +function serve(agent: unknown): void { + vi.mocked(api.getServerFeature).mockImplementation( + (name: string, fallback?: T) => + (name === 'agent' ? agent : fallback) + ) +} + +describe('readComfyAgentFeature', () => { + it.for([undefined, null, {}, { state: 'stopped' }, { state: 1 }])( + 'ignores a feature without a known state: %j', + (agent) => { + serve(agent) + + expect(readComfyAgentFeature()).toBeUndefined() + expect(isComfyAgentReady()).toBe(false) + } + ) + + it('returns a known feature as served', () => { + const agent = { state: 'failed', reason: 'port in use' } + serve(agent) + + expect(readComfyAgentFeature()).toBe(agent) + expect(isComfyAgentReady()).toBe(false) + }) + + it('drops a reason that is not a string', () => { + serve({ state: 'ready', reason: { code: 7 } }) + + expect(readComfyAgentFeature()).toEqual({ state: 'ready' }) + expect(isComfyAgentReady()).toBe(true) + }) + + it("ignores ComfyUI's agent in the standalone harness", () => { + vi.stubEnv('VITE_AGENT_STANDALONE', 'true') + serve({ state: 'ready' }) + + expect(readComfyAgentFeature()).toBeUndefined() + expect(isComfyAgentReady()).toBe(false) + }) +}) diff --git a/src/platform/remoteConfig/comfyAgentFeature.ts b/src/platform/remoteConfig/comfyAgentFeature.ts index 618c77916a2..2251f657794 100644 --- a/src/platform/remoteConfig/comfyAgentFeature.ts +++ b/src/platform/remoteConfig/comfyAgentFeature.ts @@ -6,20 +6,24 @@ export interface ComfyAgentFeature { reason?: string | null } +function isKnownState(state: unknown): state is ComfyAgentFeature['state'] { + return state === 'starting' || state === 'ready' || state === 'failed' +} + /** * ComfyUI's `agent` server feature, or undefined while its state is unknown. * The standalone harness runs its own agent, so it ignores ComfyUI's. */ export function readComfyAgentFeature(): ComfyAgentFeature | undefined { if (import.meta.env.VITE_AGENT_STANDALONE === 'true') return undefined - const agent = api.getServerFeature<{ state?: unknown } | null | undefined>( - ServerFeatureFlag.AGENT - ) - return agent?.state === 'starting' || - agent?.state === 'ready' || - agent?.state === 'failed' - ? (agent as ComfyAgentFeature) - : undefined + const agent = api.getServerFeature< + { state?: unknown; reason?: unknown } | null | undefined + >(ServerFeatureFlag.AGENT) + if (!agent || !isKnownState(agent.state)) return undefined + const { state, reason } = agent + if (reason === undefined || reason === null || typeof reason === 'string') + return agent as ComfyAgentFeature + return { state } } /** Whether ComfyUI hosts an agent that is ready to serve requests. */ diff --git a/src/platform/skills/stores/skillPacksStore.test.ts b/src/platform/skills/stores/skillPacksStore.test.ts index b27ca0ef613..dcd846d9cd2 100644 --- a/src/platform/skills/stores/skillPacksStore.test.ts +++ b/src/platform/skills/stores/skillPacksStore.test.ts @@ -385,7 +385,7 @@ describe('skillPacksStore', () => { expect(listSkillPacks).toHaveBeenCalledOnce() }) - it("ignores ComfyUI's agent in the standalone harness, as the agent panel does", async () => { + it("ignores ComfyUI's agent in the standalone harness but still follows the cohort flags", async () => { vi.stubEnv('VITE_AGENT_STANDALONE', 'true') mocks.isFeatureEnabled.mockReturnValue(undefined) hostAgent('ready') @@ -395,6 +395,14 @@ describe('skillPacksStore', () => { expect(store.enabled).toBe(false) expect(listSkillPacks).not.toHaveBeenCalled() + + mocks.isFeatureEnabled.mockReturnValue(true) + const [onFlags] = mocks.onFeatureFlags.mock.calls[0] + onFlags([], {}, {}) + await nextTick() + + expect(store.enabled).toBe(true) + expect(listSkillPacks).toHaveBeenCalledOnce() }) it('hides the hosted-agent surface when its routes answer 404', async () => { diff --git a/src/platform/skills/stores/skillPacksStore.ts b/src/platform/skills/stores/skillPacksStore.ts index de35eb04287..6bae78bd47f 100644 --- a/src/platform/skills/stores/skillPacksStore.ts +++ b/src/platform/skills/stores/skillPacksStore.ts @@ -33,8 +33,6 @@ export const useSkillPacksStore = defineStore('skillPacks', () => { // Disabled backend gates answer 404, independently of PostHog flags. const routesAvailable = ref(true) - // Shares the agent panel's gate. Cloud does not report the feature, so its - // PostHog flags stay the only gate there today. const agentHosted = computed(isComfyAgentReady) const surfaceEnabled = computed(() => flagsEnabled.value || agentHosted.value) const enabled = computed(() => surfaceEnabled.value && routesAvailable.value) From c6c6b8f14934979398b2ce79bdd07b275da6d50b Mon Sep 17 00:00:00 2001 From: bymyself Date: Thu, 5 Nov 2026 08:45:54 +0000 Subject: [PATCH 3/5] test(agent): type the getServerFeature mock in comfyAgentFeature.test.ts --- src/platform/remoteConfig/comfyAgentFeature.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform/remoteConfig/comfyAgentFeature.test.ts b/src/platform/remoteConfig/comfyAgentFeature.test.ts index f5336cdc414..9536bba7f56 100644 --- a/src/platform/remoteConfig/comfyAgentFeature.test.ts +++ b/src/platform/remoteConfig/comfyAgentFeature.test.ts @@ -8,8 +8,8 @@ vi.mock(import('@/scripts/api')) function serve(agent: unknown): void { vi.mocked(api.getServerFeature).mockImplementation( - (name: string, fallback?: T) => - (name === 'agent' ? agent : fallback) + (name: string, fallback?: T): T | undefined => + name === 'agent' ? (agent as T) : fallback ) } From 4fb07230adedd41acbcbd4f8d68fe5b219931595 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 06:34:50 -0800 Subject: [PATCH 4/5] fix(agent): keep one declaration of ComfyUI's agent feature The shared reader introduced a second handwritten copy of the `agent` payload beside `AgentServerFeature`, so the two could still drift even with one gate. Keep the declaration next to the reader that validates it and drop the copy. No generated contract can supply it yet, and a comment in comfyAgentFeature.ts records why: ingest's GetFeaturesResponse['agent'] is Cloud's rollout-only { state: 'ready' } for a different server, and comfyui-api-types is generated from an openapi.yaml vendored byte-identical from core behind a sha256 pin, which declares no `agent` feature. --- .../tests/agent/agentEntryServerLifecycle.spec.ts | 8 ++++---- src/components/topbar/AgentEntryButton.vue | 4 ++-- src/platform/remoteConfig/comfyAgentFeature.ts | 15 +++++++++++++++ .../extensions/agent/schemas/agentApiSchema.ts | 6 ------ .../agent/stores/agent/agentPanelStore.ts | 5 +---- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/browser_tests/tests/agent/agentEntryServerLifecycle.spec.ts b/browser_tests/tests/agent/agentEntryServerLifecycle.spec.ts index 55f2af7be54..7ce2da8e40e 100644 --- a/browser_tests/tests/agent/agentEntryServerLifecycle.spec.ts +++ b/browser_tests/tests/agent/agentEntryServerLifecycle.spec.ts @@ -3,7 +3,7 @@ import { expect, mergeTests } from '@playwright/test' import { webSocketFixture } from '@e2e/fixtures/ws' import { agentTest } from '@e2e/tests/agent/agentPanelMocks' import enMessages from '@/locales/en/main.json' with { type: 'json' } -import type { AgentServerFeature } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import type { ComfyAgentFeature } from '@/platform/remoteConfig/comfyAgentFeature' const test = mergeTests(agentTest, webSocketFixture) @@ -34,7 +34,7 @@ test.describe( ws.send(JSON.stringify({ type, data })) await test.step('a starting agent shows on the button, inert', async () => { - const agent: AgentServerFeature = { state: 'starting', reason: null } + const agent: ComfyAgentFeature = { state: 'starting', reason: null } publish('feature_flags', { agent }) const starting = page.getByRole('button', { name: enMessages.agent.entryStarting, @@ -47,7 +47,7 @@ test.describe( }) await test.step('a failed agent names its reason, inert', async () => { - const status: AgentServerFeature = { + const status: ComfyAgentFeature = { state: 'failed', reason: 'not_found' } @@ -66,7 +66,7 @@ test.describe( }) await test.step('a ready agent offers itself to a signed-out user', async () => { - const status: AgentServerFeature = { state: 'ready', reason: null } + const status: ComfyAgentFeature = { state: 'ready', reason: null } publish('agent_status', status) await expect(agentPanel.openButton).not.toHaveAttribute( 'aria-disabled', diff --git a/src/components/topbar/AgentEntryButton.vue b/src/components/topbar/AgentEntryButton.vue index 4c78d11256d..6f85795640f 100644 --- a/src/components/topbar/AgentEntryButton.vue +++ b/src/components/topbar/AgentEntryButton.vue @@ -44,7 +44,7 @@ import { cn } from '@comfyorg/tailwind-utils' import Button from '@comfyorg/design-system/button/Button' -import type { AgentServerFeature } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import type { ComfyAgentFeature } from '@/platform/remoteConfig/comfyAgentFeature' const { active = false, @@ -55,6 +55,6 @@ const { /** Draw attention to the button for a user who has never opened the panel. */ inviting?: boolean /** ComfyUI's agent while it is not ready: shown, but inert. */ - comfyAgent?: AgentServerFeature + comfyAgent?: ComfyAgentFeature }>() diff --git a/src/platform/remoteConfig/comfyAgentFeature.ts b/src/platform/remoteConfig/comfyAgentFeature.ts index 2251f657794..65756b75d5f 100644 --- a/src/platform/remoteConfig/comfyAgentFeature.ts +++ b/src/platform/remoteConfig/comfyAgentFeature.ts @@ -1,6 +1,21 @@ import { ServerFeatureFlag } from '@/platform/remoteConfig/serverFeatureFlag' import { api } from '@/scripts/api' +/** + * The one declaration of ComfyUI's `agent` server feature, pushed again on + * every change. Both the agent panel and the skill picker read it through this + * module; a second copy anywhere else is what lets the two surfaces drift. + * + * Not sourced from a generated contract, and neither generated package can + * supply it today: + * - ingest's `GetFeaturesResponse['agent']` is `{ state: 'ready' }` — Cloud's + * rollout gate for a different server. Widening it by hand would make one + * type claim two unrelated payloads. + * - `@comfyorg/comfyui-api-types` is generated from `openapi.yaml`, vendored + * byte-identical from `Comfy-Org/ComfyUI` behind a sha256 pin that + * `check:generated` enforces, and that spec declares no `agent` feature. A + * generated source therefore needs the schema added in core first. + */ export interface ComfyAgentFeature { state: 'starting' | 'ready' | 'failed' reason?: string | null diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 03cc35118d8..7191dfe0dd8 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -425,9 +425,3 @@ export function parseAgentWsEvent( export const zAgentIdentity = zGeneratedAgentIdentity.extend({ user_id: z.string().min(1) }) - -/** The server's `agent` feature, pushed again on every change. */ -export interface AgentServerFeature { - state: 'starting' | 'ready' | 'failed' - reason?: string | null -} diff --git a/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts b/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts index fa511988cc4..5f419955e7e 100644 --- a/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts +++ b/src/workbench/extensions/agent/stores/agent/agentPanelStore.ts @@ -24,7 +24,6 @@ import type { import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { api } from '@/scripts/api' -import type { AgentServerFeature } from '@/workbench/extensions/agent/schemas/agentApiSchema' import type { CreditTransitionNoticeEvent, @@ -88,9 +87,7 @@ export const useAgentPanelStore = defineStore('agentPanel', () => { const view = shallowRef({ screen: 'chat' }) const flagsSettled = computed(() => api.serverFeatureFlagsSettled.value) /** The server's `agent` feature; undefined unless its state is known. */ - const comfyAgent = computed( - readComfyAgentFeature - ) + const comfyAgent = computed(readComfyAgentFeature) const maximized = ref(false) const draggedWidth = ref(PANEL_MIN_WIDTH) const reservedWorkspaceWidth = ref(SIDE_TOOLBAR_WIDTH + SIDEBAR_MIN_WIDTH) From 6697ffa725a0bedc2ee3502d30c35ec1e3bdb1ce Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Thu, 5 Nov 2026 06:40:29 -0800 Subject: [PATCH 5/5] test(agent): pin the shared hosted-agent gate in the browser harness Unit tests assert the reader's return value, which does not stop the skill picker and the agent panel from reading different sources again. Drive ComfyUI's published agent lifecycle over the socket and assert the picker offers a skill only while the panel can run it, and that an unreadable failure reason is named rather than stringified. --- .../agent/agentHostedGateIsShared.spec.ts | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 browser_tests/tests/agent/agentHostedGateIsShared.spec.ts diff --git a/browser_tests/tests/agent/agentHostedGateIsShared.spec.ts b/browser_tests/tests/agent/agentHostedGateIsShared.spec.ts new file mode 100644 index 00000000000..b7f5c2263ec --- /dev/null +++ b/browser_tests/tests/agent/agentHostedGateIsShared.spec.ts @@ -0,0 +1,118 @@ +import { expect, mergeTests } from '@playwright/test' + +import type { AgentSkillListResponse } from '@comfyorg/ingest-types' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { ComfyAgentFeature } from '@/platform/remoteConfig/comfyAgentFeature' + +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { webSocketFixture } from '@e2e/fixtures/ws' +import { agentTest } from '@e2e/tests/agent/agentPanelMocks' + +const test = mergeTests(agentTest, webSocketFixture) + +const SKILL_NAME = 'shared-gate' +const SKILLS: AgentSkillListResponse = { + skills: [ + { + id: 'shared-gate-skill', + name: SKILL_NAME, + description: 'A fixture skill for the shared hosted-agent gate.', + body: 'Reply briefly; do not run workflows.', + body_hash: 'b'.repeat(64), + created_at: '2026-11-05T00:00:00Z', + updated_at: '2026-11-05T00:00:00Z' + } + ] +} + +test.describe( + 'The skill picker and the agent panel share one hosted-agent gate', + { + tag: ['@cloud', '@agent', '@ui'], + annotation: { + type: 'issue', + description: + 'https://github.com/Comfy-Org/ComfyUI_frontend/pull/22985: the picker and the panel each decided whether ComfyUI hosts an agent, so they could disagree and offer a skill the panel cannot run' + } + }, + () => { + test.use({ connectWebSocketToServer: false }) + + test('offers a skill only while the panel can run it', async ({ + page, + agentPanel, + getWebSocket + }) => { + await page.route('**/api/agent/skills', (route) => + route.fulfill(jsonRoute(SKILLS)) + ) + const ws = await getWebSocket() + const publishAgent = (agent: ComfyAgentFeature) => + ws.send(JSON.stringify({ type: 'agent_status', data: agent })) + const skillOption = page.getByRole('menuitem', { + name: SKILL_NAME, + exact: true + }) + + // The boot mocks seed `agent: { state: 'ready' }`, and the browser + // fixtures disable PostHog flags, so ComfyUI's agent feature is the only + // thing that can enable either surface here. + await test.step('a ready agent opens the panel and offers the skill', async () => { + await agentPanel.open() + await agentPanel.selectWorkflow() + await agentPanel.composer.fill('/') + await expect(skillOption).toBeVisible() + }) + + await test.step('the same agent failing withdraws both surfaces together', async () => { + publishAgent({ state: 'failed', reason: 'crashed' }) + await expect(agentPanel.root).toHaveCount(0) + await expect(skillOption).toHaveCount(0) + const failed = page.getByRole('button', { + name: enMessages.agent.entryFailed, + exact: true + }) + await expect(failed).toHaveAttribute('aria-disabled', 'true') + }) + + await test.step('the agent returning restores both surfaces together', async () => { + publishAgent({ state: 'ready', reason: null }) + await expect(agentPanel.openButton).not.toHaveAttribute( + 'aria-disabled', + 'true' + ) + await agentPanel.open() + await agentPanel.composer.fill('/') + await expect(skillOption).toBeVisible() + }) + }) + + test('names an unreadable failure reason rather than rendering it', async ({ + page, + agentPanel, + getWebSocket + }) => { + const ws = await getWebSocket() + await expect(agentPanel.openButton).toBeVisible() + + // One reader validates the payload for both surfaces, so a reason the + // server did not send as a string is dropped instead of stringified. + ws.send( + JSON.stringify({ + type: 'agent_status', + data: { state: 'failed', reason: { code: 'not_found' } } + }) + ) + + const failed = page.getByRole('button', { + name: enMessages.agent.entryFailed, + exact: true + }) + await expect(failed).toHaveAttribute( + 'title', + enMessages.agent.entryFailedReason.replace('{reason}', 'unknown') + ) + }) + } +)