From 85a0da68ced5cfd62debf9f46aa0a8032dacfbc9 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sun, 27 Sep 2026 20:30:37 -0700 Subject: [PATCH] fix(agent): resolve the agent gate off /features and delete the posthog-js flag path (#19074) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit *PR Created by the Glary-Bot Agent* --- Fixes the root cause behind [PM-1707](https://linear.app/comfyorg/issue/PM-1707/test-agentcomfyorg-shows-the-agent-on-my-phone-but-not-on-desktop-in) / [PM-1706](https://linear.app/comfyorg/issue/PM-1706/fix-agent-does-not-appear-on-desktop-browsers-for-a-user-while-it) — an allowlisted user saw the agent on one browser but not another. Supersedes #18842, which only characterized the old behaviour. `agent-in-app-experience` was already published on `/features` (it's in `FrontendFlags` in the cloud repo, evaluated server-side per authenticated user) and the app already fetched that payload into `window.__CONFIG__` at bootstrap. The agent gate ignored it and re-asked PostHog over a second, independently-failing client path — the only flag in the codebase still doing so. Billing was migrated to this rail in Because posthog-js keys flags by its own `distinct_id` and caches them per-origin in `localStorage` while sharing identity cross-subdomain via cookie, two browsers on the same account could disagree. That's the divergence users saw. `agent-in-app-experience` is now a `ServerFeatureFlag` resolved through `useFeatureFlags` off `remoteConfig`, via a new `resolveWhitelistFlag()`: - `false` off the Cloud build, and `false` before the first authenticated answer (the anonymous bootstrap payload cannot answer a per-user allowlist) - after an authenticated answer, a *failed* refresh falls back to that answer, held **in memory for the session only** — never storage. A persisted grant is precisely what made two browsers disagree. - revocation propagates on the next successful poll; 401/403 and sign-out clear it `setupFlagGate` loses the dynamic imports, the `onFeatureFlags` subscription and the delivery plumbing, becoming two reactive watches. Deleted: `postHogFlagSource.ts`, `useAgentFeatureGate.ts` (dead code — nothing but its own test imported it, and it lacked the `errorsLoading` guard, so the two copies behaved differently), and their tests. `posthog-js` stays — telemetry still uses it. This removes only the flag path. Every guard is mutation-tested; each of these turns the suite red: | Mutation | | |---|---| | anonymous-window guard removed (fail-open before auth) | caught | | `isCloud` guard removed | caught | | fail-open before the first authenticated answer | caught | | grant fallback removed (a blip unmounts the panel) | caught | | grant survives sign-out / 401 | caught | | grant never recorded on a successful load | caught | | **grant persisted via `useStorage`** — the PM-1707 shape, under either the flag key *or* a renamed key | caught | | gate `enabled` unwired from the flag | caught | | gate settles on the anonymous config | caught | | signed-out settle fallback removed | caught | `pnpm typecheck`, `lint`, `knip`, `format:check` clean; 12,129 unit tests pass. The one broad-run failure (`agentSubgraphDefinitions`) reproduces identically on `main`. Two bugs were caught during review and fixed here rather than shipped: a transient `/features` failure would have unmounted the panel mid-session for a granted user (`DockedAgentPanel` is `v-if`, so composer draft and in-flight turn UI are destroyed), and the E2E fixtures were still feeding the flag through the deleted posthog channel, which would have regressed the whole agent E2E suite to a dead panel. - **E2E fixtures changed.** All three now put the key at the top level of the `/features` body. Specs can't be executed in my sandbox; the static chain is verified end to end (`cloudBootMocks` serves that body to both fetches, and `WorkspaceAuthGate` throws unless state is `authenticated`, so any spec that renders has reached the state the resolver needs). **One CI run of `--project=cloud` on a single agent spec would settle it conclusively.** - **Retired telemetry slug.** `agent_flag_gate_load_failure` no longer exists — the guarded dynamic import it reported on is gone. Any monitor keyed on it will go silently to zero. - **New Datadog facet.** The flag is added to `startFeatureFlagTelemetry`. It's the first dashed `ServerFeatureFlag`, and the sanitiser rewrites dashes — the RUM facet is `agent_in_app_experience`, not the hyphenated name. - **Deliberately dropped:** an in-session re-offer retry that existed only because `posthog.onFeatureFlags` re-delivered unchanged values. The real retry triggers (`offerHeld && screenIsClear`, identity change) are unaffected and still covered. - **Known residue:** the grant-retention window is unbounded, so a sustained `/features` outage leaves a revoked user with an inert panel until reload. Low impact — the backend enforces the flag independently at four choke points, so every call 404s regardless. --------- Co-authored-by: Glary-Bot --- browser_tests/fixtures/agentPanelFixture.ts | 8 +- browser_tests/tests/agent/agentPanelMocks.ts | 8 +- .../tests/agent/agentPaywallSubscribe.spec.ts | 6 +- docs/FEATURE_FLAGS.md | 148 ++++++++++++--- ...er-and-distribution-resolved-boundaries.md | 15 +- src/composables/useFeatureFlags.test.ts | 90 ++++++++- src/composables/useFeatureFlags.ts | 41 +++- src/extensions/core/agentPanel.test.ts | 175 +++++++++--------- src/extensions/core/agentPanel.ts | 62 ++++--- .../agentPanelGateDependencyFailure.test.ts | 70 ------- src/extensions/core/index.ts | 2 +- .../remoteConfig/refreshRemoteConfig.test.ts | 45 +++++ .../remoteConfig/refreshRemoteConfig.ts | 6 +- src/platform/remoteConfig/remoteConfig.ts | 8 + src/platform/remoteConfig/types.ts | 1 + .../agent/useAgentFeatureGate.test.ts | 83 --------- .../composables/agent/useAgentFeatureGate.ts | 24 --- .../agent/utils/postHogFlagSource.test.ts | 80 -------- .../agent/utils/postHogFlagSource.ts | 39 ---- 19 files changed, 443 insertions(+), 468 deletions(-) delete mode 100644 src/extensions/core/agentPanelGateDependencyFailure.test.ts delete mode 100644 src/workbench/extensions/agent/composables/agent/useAgentFeatureGate.test.ts delete mode 100644 src/workbench/extensions/agent/composables/agent/useAgentFeatureGate.ts delete mode 100644 src/workbench/extensions/agent/utils/postHogFlagSource.test.ts delete mode 100644 src/workbench/extensions/agent/utils/postHogFlagSource.ts diff --git a/browser_tests/fixtures/agentPanelFixture.ts b/browser_tests/fixtures/agentPanelFixture.ts index 746b20d127d..db686ddab1c 100644 --- a/browser_tests/fixtures/agentPanelFixture.ts +++ b/browser_tests/fixtures/agentPanelFixture.ts @@ -23,13 +23,9 @@ const APP_URL = process.env.PLAYWRIGHT_TEST_URL || 'http://localhost:8188' function agentFeatures(agentFlag: boolean): RemoteConfig { return { + 'agent-in-app-experience': agentFlag, posthog_project_token: 'phc_e2e_agent_panel', - posthog_config: { - advanced_disable_flags: true, - bootstrap: { - featureFlags: { 'agent-in-app-experience': agentFlag } - } - } + posthog_config: { advanced_disable_flags: true } } } diff --git a/browser_tests/tests/agent/agentPanelMocks.ts b/browser_tests/tests/agent/agentPanelMocks.ts index c6611788907..ad79893a35b 100644 --- a/browser_tests/tests/agent/agentPanelMocks.ts +++ b/browser_tests/tests/agent/agentPanelMocks.ts @@ -143,13 +143,9 @@ export const MESSAGE_DONE_EVENT: AgentWsEvent = { function agentFeatures(agentFlag: boolean): RemoteConfig { return { + 'agent-in-app-experience': agentFlag, posthog_project_token: 'phc_e2e_agent_panel', - posthog_config: { - advanced_disable_flags: true, - bootstrap: { - featureFlags: { 'agent-in-app-experience': agentFlag } - } - } + posthog_config: { advanced_disable_flags: true } } } diff --git a/browser_tests/tests/agent/agentPaywallSubscribe.spec.ts b/browser_tests/tests/agent/agentPaywallSubscribe.spec.ts index 1f620a59c24..11127971ce0 100644 --- a/browser_tests/tests/agent/agentPaywallSubscribe.spec.ts +++ b/browser_tests/tests/agent/agentPaywallSubscribe.spec.ts @@ -43,11 +43,9 @@ async function reconfigureBilling( { subscriptionRequired }: { subscriptionRequired: boolean } ): Promise { const features: RemoteConfig = { + 'agent-in-app-experience': true, posthog_project_token: 'phc_e2e_agent_panel', - posthog_config: { - advanced_disable_flags: true, - bootstrap: { featureFlags: { 'agent-in-app-experience': true } } - }, + posthog_config: { advanced_disable_flags: true }, subscription_required: subscriptionRequired } await page.route('**/api/features', (route) => diff --git a/docs/FEATURE_FLAGS.md b/docs/FEATURE_FLAGS.md index d5934c455c1..35219f0dfa4 100644 --- a/docs/FEATURE_FLAGS.md +++ b/docs/FEATURE_FLAGS.md @@ -129,10 +129,8 @@ classDiagram } class useFeatureFlags { - +serverSupports(name) boolean - +getServerFeature(name, default) T - +createServerFeatureFlag(name) ComputedRef - +extension: ExtensionFlags + +flags: Readonly~ReactiveFlags~ + +featureFlag(path, default) ComputedRef } class VueComponent { @@ -209,8 +207,8 @@ SERVER_FEATURE_FLAGS = { **Frontend Usage:** ```typescript -const { getServerFeature } = useFeatureFlags() -const maxUploadSize = getServerFeature('max_upload_size', 100 * 1024 * 1024) // Default 100MB +const { flags } = useFeatureFlags() +const maxUploadSize = flags.maxUploadSize ``` ## Using Feature Flags @@ -231,25 +229,42 @@ const maxSize = api.getServerFeature('max_upload_size', 100 * 1024 * 1024) 2. **Using the composable (recommended for reactive components):** +`useFeatureFlags()` returns `{ flags, featureFlag }`. + +`flags` is a readonly reactive object of named, camelCase properties. Most +correspond to an entry in the `ServerFeatureFlag` enum; a few resolve a bare +string path instead (`assetsEnabled` reads `'assets'`). Each is a getter, so +reading it always reflects the current value and tracks reactively: + ```typescript -const { serverSupports, getServerFeature, extension } = useFeatureFlags() +const { flags } = useFeatureFlags() // Check feature support -if (serverSupports('supports_preview_metadata')) { +if (flags.supportsPreviewMetadata) { // Use enhanced previews } -// Use reactive convenience properties (automatically update if flags change) -if (extension.manager.supportsV4.value) { +// Nested extension flags are exposed as named properties too +if (flags.supportsManagerV4) { // Use V4 manager API } ``` +`featureFlag(path, defaultValue)` returns a `ComputedRef` for a flag that has no +named property yet. It accepts dot-separated paths for nested flags: + +```typescript +const { featureFlag } = useFeatureFlags() + +const customFlag = featureFlag('extension.custom.nested.feature', false) +console.log(customFlag.value) +``` + 3. **Reactive usage in templates:** ```vue