-
Notifications
You must be signed in to change notification settings - Fork 88
feat: add an environment-backed feature flag client wired through the context #2221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { EnvFeatureFlags } from "./env"; | ||
| import { FEATURE_FLAGS } from "./types"; | ||
|
|
||
| const VARIABLE = FEATURE_FLAGS.imperativeDeploy; | ||
|
|
||
| describe("EnvFeatureFlags", () => { | ||
| test("a flag whose variable is unset is disabled", () => { | ||
| const flags = new EnvFeatureFlags({}); | ||
| expect(flags.isEnabled("imperativeDeploy")).toBe(false); | ||
| expect(flags.enabled()).toEqual([]); | ||
| }); | ||
|
|
||
| test("exactly '1' enables a flag", () => { | ||
| const flags = new EnvFeatureFlags({ [VARIABLE]: "1" }); | ||
| expect(flags.isEnabled("imperativeDeploy")).toBe(true); | ||
| expect(flags.enabled()).toEqual(["imperativeDeploy"]); | ||
| }); | ||
|
|
||
| test("surrounding whitespace around '1' is ignored", () => { | ||
| expect(new EnvFeatureFlags({ [VARIABLE]: " 1 " }).isEnabled("imperativeDeploy")).toBe(true); | ||
| expect(new EnvFeatureFlags({ [VARIABLE]: "\t1\n" }).isEnabled("imperativeDeploy")).toBe(true); | ||
| }); | ||
|
|
||
| // The contract is "=1 enables", not truthiness: a value that reads as "on" in | ||
| // other tools must still leave the experiment off here. | ||
| test.each(["0", "true", "yes", "on", "", "11", "1.0"])("%j does not enable a flag", (value) => { | ||
| const flags = new EnvFeatureFlags({ [VARIABLE]: value }); | ||
| expect(flags.isEnabled("imperativeDeploy")).toBe(false); | ||
| expect(flags.enabled()).toEqual([]); | ||
| }); | ||
|
|
||
| test("an undefined value (present key, no value) is disabled", () => { | ||
| expect(new EnvFeatureFlags({ [VARIABLE]: undefined }).isEnabled("imperativeDeploy")).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| test("environment variables that are not flags are ignored", () => { | ||
| const flags = new EnvFeatureFlags({ | ||
| AGENTCORE_CLI_EXPERIMENTAL_SOMETHING_ELSE: "1", | ||
| IMPERATIVE_DEPLOY: "1", | ||
| imperativeDeploy: "1", | ||
| }); | ||
| expect(flags.enabled()).toEqual([]); | ||
| }); | ||
|
|
||
| test("reads the environment once, at construction", () => { | ||
| const env: Record<string, string | undefined> = { [VARIABLE]: "1" }; | ||
| const flags = new EnvFeatureFlags(env); | ||
| env[VARIABLE] = "0"; | ||
| expect(flags.isEnabled("imperativeDeploy")).toBe(true); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { FEATURE_FLAGS, type FeatureFlag, type FeatureFlags } from "./types"; | ||
|
|
||
| /** The one value that switches a flag on. Anything else — including "true" — leaves it off. */ | ||
| const ENABLED_VALUE = "1"; | ||
|
|
||
| /** | ||
| * Feature flags read from environment variables. | ||
| * | ||
| * Every flag maps to one variable (see {@link FEATURE_FLAGS}) and is enabled only | ||
| * when that variable's trimmed value is exactly "1". The contract is deliberately | ||
| * narrow — no truthiness rules — so an experiment cannot be turned on by accident | ||
| * and the README can state it in one line. | ||
| * | ||
| * The environment is read once at construction, not on every call: a flag that | ||
| * flipped mid-command could leave a deploy half in one mode and half in another. | ||
| * `src/index.ts` passes `process.env`; nothing here reaches for it. | ||
| */ | ||
| export class EnvFeatureFlags implements FeatureFlags { | ||
| private readonly enabledFlags: ReadonlySet<FeatureFlag>; | ||
|
|
||
| constructor(processEnv: Record<string, string | undefined>) { | ||
| const enabled = new Set<FeatureFlag>(); | ||
| for (const [flag, variable] of Object.entries(FEATURE_FLAGS) as [FeatureFlag, string][]) { | ||
| if (processEnv[variable]?.trim() === ENABLED_VALUE) enabled.add(flag); | ||
| } | ||
| this.enabledFlags = enabled; | ||
| } | ||
|
|
||
| isEnabled(flag: FeatureFlag): boolean { | ||
| return this.enabledFlags.has(flag); | ||
| } | ||
|
|
||
| enabled(): FeatureFlag[] { | ||
| return [...this.enabledFlags]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { FEATURE_FLAGS, type FeatureFlag, type FeatureFlags } from "./types"; | ||
| export { EnvFeatureFlags } from "./env"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** Every experimental switch the CLI knows, keyed by a stable code name. */ | ||
| export const FEATURE_FLAGS = { | ||
| imperativeDeploy: "AGENTCORE_CLI_EXPERIMENTAL_IMPERATIVE_DEPLOY", | ||
| } as const; | ||
|
|
||
| export type FeatureFlag = keyof typeof FEATURE_FLAGS; | ||
|
|
||
| /** | ||
| * Answers whether an experimental feature is switched on for this process. | ||
| * | ||
| * Declared here (rather than in a handler's types file) because a feature flag | ||
| * is cross-cutting: handlers, screens, and middleware all read it, and none of | ||
| * them owns it. Implementations live beside it. | ||
| */ | ||
| export interface FeatureFlags { | ||
| /** True when the flag's switch is on for this process. Never throws. */ | ||
| isEnabled(flag: FeatureFlag): boolean; | ||
| /** The flags currently enabled, for logging/diagnostics. */ | ||
| enabled(): FeatureFlag[]; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { createHandler, FeatureFlagsKey, Router } from "../router"; | ||
| import type { FeatureFlag } from "../featureFlags"; | ||
| import { TestFeatureFlags } from "../testing"; | ||
| import { withFeatureFlags } from "./withFeatureFlags"; | ||
|
|
||
| // A leaf that reports what it sees on the context, so the test asserts the | ||
| // middleware's effect the way a real handler would observe it. | ||
| function routeAndRead(flags: TestFeatureFlags): Promise<{ seen: FeatureFlag[]; on: boolean }> { | ||
| return new Promise((resolve, reject) => { | ||
| const app = new Router("myapp", "test app"); | ||
| app.use(withFeatureFlags(flags)); | ||
| app.handler( | ||
| createHandler({ | ||
| name: "leaf", | ||
| description: "reads the feature flags", | ||
| handle: async (ctx) => { | ||
| const seen = ctx.require(FeatureFlagsKey); | ||
| resolve({ seen: seen.enabled(), on: seen.isEnabled("imperativeDeploy") }); | ||
| }, | ||
| }), | ||
| ); | ||
| app.route(["node", "myapp", "leaf"]).catch(reject); | ||
| }); | ||
| } | ||
|
|
||
| describe("withFeatureFlags", () => { | ||
| test("pins the injected instance on the context for the leaf", async () => { | ||
| const result = await routeAndRead(new TestFeatureFlags(["imperativeDeploy"])); | ||
| expect(result).toEqual({ seen: ["imperativeDeploy"], on: true }); | ||
| }); | ||
|
|
||
| test("an instance with nothing enabled reports every flag off", async () => { | ||
| const result = await routeAndRead(new TestFeatureFlags()); | ||
| expect(result).toEqual({ seen: [], on: false }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { FeatureFlagsKey, type Middleware } from "../router"; | ||
| import type { FeatureFlags } from "../featureFlags"; | ||
|
|
||
| /** | ||
| * Middleware that pins a {@link FeatureFlags} instance on the context, making it | ||
| * available to every handler and screen beneath the mount point via | ||
| * `ctx.require(FeatureFlagsKey).isEnabled(...)`. | ||
| */ | ||
| export function withFeatureFlags(flags: FeatureFlags): Middleware { | ||
| return (h) => ({ | ||
| name: () => h.name(), | ||
| description: () => h.description(), | ||
| flags: () => h.flags(), | ||
| arguments: () => h.arguments(), | ||
| doesSupportTui: () => h.doesSupportTui(), | ||
| children: () => h.children(), | ||
| handle: async (ctx, handlerFlags, args) => { | ||
| await h.handle(ctx.withValue(FeatureFlagsKey, flags), handlerFlags, args); | ||
| }, | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about leveraging the global config for this? This could allow customers could do something like:
and
We could still allow an env var override to take precedence too.