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/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') + ) + }) + } +) 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/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.test.ts b/src/platform/remoteConfig/comfyAgentFeature.test.ts new file mode 100644 index 00000000000..9536bba7f56 --- /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): T | undefined => + name === 'agent' ? (agent as T) : 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 new file mode 100644 index 00000000000..65756b75d5f --- /dev/null +++ b/src/platform/remoteConfig/comfyAgentFeature.ts @@ -0,0 +1,47 @@ +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 +} + +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; 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. */ +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..dcd846d9cd2 100644 --- a/src/platform/skills/stores/skillPacksStore.test.ts +++ b/src/platform/skills/stores/skillPacksStore.test.ts @@ -368,6 +368,43 @@ 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 but still follows the cohort flags", 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() + + 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 () => { 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..6bae78bd47f 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,7 @@ 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' - ) + 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/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 030faaec275..5f419955e7e 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 @@ -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, @@ -86,18 +85,9 @@ 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)