feat(core): support env-only flags

This commit is contained in:
neriousy 2026-11-18 17:23:33 +01:00
parent a61f5ea834
commit bfca9d99a1
4 changed files with 23 additions and 5 deletions

View file

@ -39,7 +39,7 @@ const onByDefault = (channel: string) => channel === "local" || channel === "dev
* document that sets `experimental` (the same document every other `experimental` setting reads), then `all_flags: true`
* from the highest-priority document that sets `all_flags`, so a global switch survives a project's own `experimental`
* block, then on for local and dev builds. `all_flags: false` falls through rather than turning flags off.
* Opt-in-only flags skip both wildcards and default to off on every channel.
* Opt-in-only flags skip both wildcards and default to off on every channel. Env-only flags skip config entirely.
* A remote kill switch belongs above `OPENCODE_FLAGS` here.
*/
export function resolve(input: {
@ -65,8 +65,9 @@ export function resolve(input: {
const optInOnly = ConfigFlag.optInOnly.has(key)
const old = names.find((name) => ConfigFlag.aliases[name] === key)
const override = env.get(key) ?? (old === undefined ? undefined : env.get(old)) ?? (all && !optInOnly ? true : undefined)
const configured =
experimental?.[key] ?? (old === undefined ? undefined : experimental?.[old]) ?? (allFlags && !optInOnly ? true : undefined)
const configured = !ConfigFlag.configurable(key)
? undefined
: (experimental?.[key] ?? (old === undefined ? undefined : experimental?.[old]) ?? (allFlags && !optInOnly ? true : undefined))
const [enabled, source]: [boolean, State["source"]] =
override !== undefined
? [override, "env"]

View file

@ -1,6 +1,6 @@
export * as ConfigExperimental from "./experimental.js"
import { Record, Schema } from "effect"
import { Record, Schema, Struct } from "effect"
import { NonNegativeInt, optional } from "../schema.js"
import { ConfigFlag } from "./flag.js"
import { ConfigModel } from "./model.js"
@ -17,7 +17,7 @@ export class Info extends Schema.Class<Info>("ConfigExperimental.Info")({
description:
"Enable all flags except opt-in-only flags on any build channel, like OPENCODE_FLAGS=all. Per-flag config settings take precedence over all_flags; false leaves the build defaults in effect.",
}),
...Record.map(ConfigFlag.registry, (description, key) =>
...Record.map(Struct.omit(ConfigFlag.registry, ConfigFlag.envOnly), (description, key) =>
Schema.Boolean.pipe(optional).annotate({
description: ConfigFlag.optInOnly.has(key)
? description

View file

@ -25,6 +25,17 @@ export type Key = keyof typeof registry
export const optInOnly: ReadonlySet<Key> = new Set(["full_codemode"])
/**
* Flags read before any config loads, such as when the database opens. Only `OPENCODE_FLAGS` and the build channel
* set them: they have no `experimental.<key>` setting, and `experimental.all_flags` leaves them alone.
*/
export const envOnly = [] as const satisfies ReadonlyArray<Key>
/** A flag that `experimental.<key>` can set. */
export type ConfigKey = Exclude<Key, (typeof envOnly)[number]>
const envOnlyKeys: ReadonlySet<Key> = new Set(envOnly)
export const configurable = (key: Key): key is ConfigKey => !envOnlyKeys.has(key)
/** Earlier names of a flag, still read from `experimental.<name>` and `OPENCODE_FLAGS` when the new key is unset. */
export const aliases = {
schedules: "automations",

View file

@ -2,6 +2,7 @@ import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { Config } from "../src/config.js"
import { ConfigAgent } from "../src/config/agent.js"
import { ConfigExperimental } from "../src/config/experimental.js"
import { ConfigFlag } from "../src/config/flag.js"
import { ConfigMCP } from "../src/config/mcp.js"
import { ConfigPolicy } from "../src/config/policy.js"
@ -192,3 +193,8 @@ test("wildcard switches are reserved flag names", () => {
expect(ConfigFlag.registry).not.toHaveProperty("all_flags")
expect(ConfigFlag.registry).not.toHaveProperty(ConfigFlag.wildcard)
})
test("config settings cover every flag except env-only flags", () => {
const settings = Object.keys(ConfigExperimental.Info.fields)
expect(Object.keys(ConfigFlag.registry).filter((key) => !settings.includes(key))).toEqual([...ConfigFlag.envOnly])
})