Skip to content

feat(feature-flags): add wizard feature-flags install skill - #378

Draft
fristovic wants to merge 6 commits into
PostHog:mainfrom
fristovic:feat/feature-flags-program
Draft

feat(feature-flags): add wizard feature-flags install skill#378
fristovic wants to merge 6 commits into
PostHog:mainfrom
fristovic:feat/feature-flags-program

Conversation

@fristovic

@fristovic fristovic commented Sep 1, 2026

Copy link
Copy Markdown

The SDK can already evaluate flags. This skill does not add a flags engine. It installs the Next.js App Router pattern the SDK allows and a typical install almost never does: cheap, fast, safe to merge.

Default wizard is product analytics. wizard audit feature-flags is read-only. MCP / PostHog AI create and manage flags. Neither wires this pattern into the app. This is that install.

What it puts in the repo (vs SDK defaults)

  • One /flags call per request, not one per check. evaluateFlags(distinctId) once in the root layout, then flags.isEnabled(key). getFeatureFlag / isFeatureEnabled / getAllFlags each hit /flags again. PostHog bills those.
  • no second client fetch. Server values are bootstrapped into PostHogProvider. instrumentation-client.ts cannot take per-request bootstrap, so existing init there is moved into the provider.
  • Same distinct_id on server and client so percentage rollout is real. Anonymous apps get a ph_distinct_id cookie; identified apps reuse the real user id.
  • CI does not poll. advanced_disable_feature_flags in test/CI. No local-evaluation 30s poll on idle servers.
  • Optional worked example, off until they turn it on. One wizard_ask, skip first. Confirm → one boolean flag at 0% and one additive UI path. Auth / checkout / mutations are out of bounds. The SDK will not stop you from wrapping checkout at 100%.

Who this is for

A Next App Router team that wants the cheap/correct first hour, not “can I use flags?”. Skip is valid. If they already evaluate once on the server and bootstrap, they already have the benefit.

Pairs with wizard: PostHog/wizard#1192.


Docs: PostHog/posthog.com#19911 (/docs/feature-flags/installation/ai-wizard). Do not add that URL to mill shared_docs until https://posthog.com/docs/feature-flags/installation/ai-wizard.md returns 200.

@fristovic
fristovic requested a review from a team as a code owner September 1, 2026 19:14
@fristovic
fristovic marked this pull request as draft September 1, 2026 20:36

`advanced_disable_feature_flags: true` in test/CI stops forgotten CI jobs from polling `/flags` (see `cutting-costs.md`). A missing token in production is a no-op (render `children`); in development throw the missing-config error named in the framework guidelines.

**If `instrumentation-client.ts` (or `.js`) already inits `posthog-js`:** move that init into this provider so bootstrap can be passed per request. Keep the existing `api_host` / `defaults` / other options. Do not leave both inits in place. Do not follow any framework note that says to keep init in `instrumentation-client.ts` — that path cannot take per-request bootstrap.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might work better as a commandment (check and see if it already is, it might be) and see what this might contradict with in commandments in general

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this already is a commandment, I missed that. I'll have to add an exception here I think because we want this skill to do the opposite so it can pass per-request bootstrap.

I will add a nextjs-flags-bootstrap commandment and tagg only this skill.

@gewenyu99 gewenyu99 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, this is a great start. I have some suggestions:

  • I'm not sure if I understand who would want/need this Wizard
  • Try setting up feature flags yourself, think about the pain points/difficult bits that someone is likely to fumble on, see if this resolves those issues.
  • I would think about this program's relationship to the main PostHog integration program and shared skill reuse. I think this contains a less reliable but duplicate version of many of they paths.


Record: package manager, whether `src/` is used, and whether PostHog is already initialized (`posthog.init`, `PostHogProvider`, `instrumentation-client`, `posthog-js` / `posthog-node` in dependencies). If flags are already wired the way this skill describes (`evaluateFlags` + bootstrap + a gated call site), verify they are correct and skip to STEP 10.

### STEP 2: Credentials

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're installing the package, we can init this for them. There's examples you can copy from, the OAuth session will let you grab the token on the wizard side to add them. I think it'd be pretty odd to have an environment variable -> have no integration.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this was a sequencing leftover. This skill will now require an existing client init. OAuth still fills a missing phc_ into existing env names. If there's no init, we abort and point at npx @posthog/wizard instead of shipping a second integration.

```ts
import { PostHog } from 'posthog-node'

export function PostHogServer(token: string) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna throw you one more wrench, you'll need to figure out a reasonable lower bound for the patterns you apply here. The way Next versions work across the board differs, and changes how PostHog need to be initialized. There are examples here in the repo

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, thats not a wrench at all, makes total sense. I went with Next.js App Router 15.3+, matching the mill Next commandment and example-apps/next-app-router (15.5.x).
We read package.json's next version and abort below that. On 15.3+ we still relocate instrumentation-client into the provider when bootstrap is required.


Flags change production UI. Ask **once**, then create a flag and gate only if they picked a target.

1. Scan pages and components for additive surfaces (a banner, an extra card, an empty-state illustration). Prefer a new element over wrapping existing critical logic.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 Do you think someone is more likely to come to this program with a thing they have in mind and wanna gate, or do you think they'd be looking for suggestions on what to gate?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both can be right depending on the user. That's why I built both options here:

  • Skip first = "I already know what I'll gate" (they add useFeatureFlagEnabled on the wiring we just installed)
  • Suggestions = "show me the loop, I don't know how this works"

Right? Feel free to correct me If I'm missing something here?

const token = process.env.NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN
const cookieStore = await cookies()
const distinctId =
cookieStore.get('ph_distinct_id')?.value ?? crypto.randomUUID()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostHog always generates a distinct_id on the frontend. You can call posthog.get_distinct_id(). If they're not identified, they'd have an anonymous distinct Id that's merged with the user on identify.

@fristovic

Copy link
Copy Markdown
Author

Hey, this is a great start. I have some suggestions:

  • I'm not sure if I understand who would want/need this Wizard

I can see this easily being used by someone who wants to evaluate cheaply feature-flags (server eval, bootstrap, CI off). I don't see it as a flag factory. Default wizard won’t install this pattern.

On the fumbles: the ones I initially meant to hit are double /flags, first-paint render issue, CI polling overnight, and the deprecated per-call APIs (getFeatureFlag / getAllFlags). The weaker copy of the main integration (init, env, packages, Next version) was a real miss that I will address in the other comments.

On reuse: agreed. This skill will now extend an existing PostHog install instead of reimplementing it.

@fristovic

Copy link
Copy Markdown
Author

Official docs page (draft): PostHog/posthog.com#19911/docs/feature-flags/installation/ai-wizard. Do not add that URL to shared_docs until it returns HTTP 200.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants