diff --git a/AGENTS.md b/AGENTS.md index 946ee9d8230..de2903e42dc 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -182,8 +182,8 @@ const table = sqliteTable("session", { - When you open a PR, decide whether the change needs a flag: new user-visible behavior, risky core changes, and anything not ready to release get one; fixes, refactors, docs and tests do not. - Gate risky behavior, or behavior that lands over several PRs, with a key and a description in `packages/schema/src/config/flag.ts`, and read it with `Flag.enabled("")` from `@opencode/core/flag`. -- Flags default to on in local and dev builds and off in releases; `experimental.` in config and `OPENCODE_FLAGS="key,-other"` override that. -- Say in the PR body which flag guards the change. The reserved switches `OPENCODE_FLAGS=all` and `experimental.all_flags: true` enable every flag; use `all,-` or `experimental.: false` to opt out within the respective source. +- Flags default to on in local and dev builds and off in releases, except `optInOnly` flags (such as `todo`), which require an explicit `OPENCODE_FLAGS=` or `experimental.: true` in every build. +- Say in the PR body which flag guards the change. The reserved switches `OPENCODE_FLAGS=all` and `experimental.all_flags: true` skip opt-in-only flags; use `all,-` or `experimental.: false` to opt out within the respective source. - Ship a feature by deleting its flag and the checks that read it. ## Testing diff --git a/packages/core/src/flag.ts b/packages/core/src/flag.ts index 7a019bf2951..b68c9b14a3d 100644 --- a/packages/core/src/flag.ts +++ b/packages/core/src/flag.ts @@ -25,7 +25,7 @@ export class Service extends Context.Service()("@opencode/Fl /** * `OPENCODE_FLAGS`, a comma-separated list such as `automations,-media_blob`; a leading `-` turns a flag off, and - * `all` turns every flag on unless the same list opts one out, as in `all,-media_blob`. + * `all` enables all flags except opt-in-only flags and any disabled in the same list, as in `all,-media_blob`. */ export const Overrides = Context.Reference("@opencode/Flag/Overrides", { defaultValue: () => process.env.OPENCODE_FLAGS, @@ -39,6 +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. * A remote kill switch belongs above `OPENCODE_FLAGS` here. */ export function resolve(input: { @@ -61,17 +62,19 @@ export function resolve(input: { )?.info.experimental?.all_flags === true const names = Object.keys(ConfigFlag.aliases) as Array return Record.map(ConfigFlag.registry, (_, key): State => { + 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 ? true : undefined) + const override = + env.get(key) ?? (old === undefined ? undefined : env.get(old)) ?? (all && !optInOnly ? true : undefined) const value = experimental?.[key] ?? (old === undefined ? undefined : experimental?.[old]) // A flag that takes settings is on when given them. - const configured = typeof value === "object" ? true : (value ?? (allFlags ? true : undefined)) + const configured = typeof value === "object" ? true : (value ?? (allFlags && !optInOnly ? true : undefined)) const [enabled, source]: [boolean, State["source"]] = override !== undefined ? [override, "env"] : configured !== undefined ? [configured, "config"] - : [onByDefault(input.channel), "default"] + : [!optInOnly && onByDefault(input.channel), "default"] return { enabled, source } }) } diff --git a/packages/core/src/tool/plugin/todo.ts b/packages/core/src/tool/plugin/todo.ts index 68ce708e7b6..23e4952c70b 100644 --- a/packages/core/src/tool/plugin/todo.ts +++ b/packages/core/src/tool/plugin/todo.ts @@ -5,6 +5,7 @@ import type { Context } from "@opencode/plugin/effect/plugin" import { Effect, Schema } from "effect" import { Config } from "../../config.js" import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js" +import { Flag } from "../../flag.js" import { Permission } from "../../permission.js" import DESCRIPTION from "../todowrite.txt" @@ -47,7 +48,8 @@ export const Plugin = { const permission = yield* Permission.Service // Internal plugins are not re-run on config changes, so reload the tool transform instead. const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, ctx.tool.reload()) - const enabled = () => Config.latest(loaded.entries, "experimental")?.todo === true + const flags = yield* Flag.Service + const enabled = () => flags.resolve(loaded.entries).todo.enabled yield* ctx.tool .transform((editor) => { diff --git a/packages/core/test/flag.test.ts b/packages/core/test/flag.test.ts index 219df95cd5d..7cc024b6562 100644 --- a/packages/core/test/flag.test.ts +++ b/packages/core/test/flag.test.ts @@ -26,7 +26,11 @@ const document = (experimental?: ConstructorParameters Record.map(ConfigFlag.registry, () => state) +// todo is opt-in only, so the build defaults and both wildcards leave it off. +const every = (state: Flag.State): Record => ({ + ...Record.map(ConfigFlag.registry, () => state), + todo: { enabled: false, source: "default" }, +}) describe("Flag.resolve", () => { test.each([ @@ -40,6 +44,21 @@ describe("Flag.resolve", () => { expect(Flag.resolve({ entries: [], channel })).toEqual(every({ enabled, source: "default" })) }) + test("todo is opt-in only: on only when named in OPENCODE_FLAGS or experimental.todo", () => { + expect(Flag.resolve({ entries: [], channel: "dev", overrides: "all" }).todo).toEqual({ + enabled: false, + source: "default", + }) + expect(Flag.resolve({ entries: [], channel: "latest", overrides: "todo" }).todo).toEqual({ + enabled: true, + source: "env", + }) + expect(Flag.resolve({ entries: [document({ todo: true })], channel: "latest" }).todo).toEqual({ + enabled: true, + source: "config", + }) + }) + test("full_codemode defaults to on in local and dev builds, off in releases, and follows overrides and wildcards", () => { expect(Flag.resolve({ entries: [], channel: "local" }).full_codemode).toEqual({ enabled: true, diff --git a/packages/core/test/tool-todo.test.ts b/packages/core/test/tool-todo.test.ts index c6c18166fd9..b7c0dd5f17c 100644 --- a/packages/core/test/tool-todo.test.ts +++ b/packages/core/test/tool-todo.test.ts @@ -1,7 +1,7 @@ import { describe, expect } from "bun:test" import { Effect, Stream } from "effect" -import { Config } from "@opencode/core/config" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Flag } from "@opencode/core/flag" import { Session } from "@opencode/core/session" import { Tool } from "@opencode/core/tool" import { TodoTool } from "@opencode/core/tool/plugin/todo" @@ -25,7 +25,7 @@ const it = testEffect(AppNodeBuilder.build(Tool.node, toolNodeReplacements())) const setup = (experimental: ConfigExperimental.Info | undefined) => Effect.gen(function* () { yield* registerToolPlugin(TodoTool.Plugin, { event: { subscribe: () => Stream.never } }).pipe( - Effect.provide(Config.testLayer([document(experimental)])), + Effect.provide(Flag.testLayer([document(experimental)])), Effect.provide(permissionLayer({ assert: () => Effect.void })), ) return yield* Tool.Service @@ -39,7 +39,6 @@ describe("TodoTool", () => { Effect.gen(function* () { expect(yield* names(yield* setup(undefined))).not.toContain(TodoTool.name) expect(yield* names(yield* setup(new ConfigExperimental.Info({ todo: false })))).not.toContain(TodoTool.name) - expect(yield* names(yield* setup(new ConfigExperimental.Info({ all_flags: true })))).not.toContain(TodoTool.name) }), ) diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index 2165fe4c4ac..99d7d5833f0 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -15,14 +15,13 @@ export class Info extends Schema.Class("ConfigExperimental.Info")({ }), all_flags: Schema.Boolean.pipe(optional).annotate({ description: - "Turn every feature flag on regardless of the build channel, like OPENCODE_FLAGS=all; explicit per-flag settings still win, and false is not a kill switch.", + "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.", }), - todo: Schema.Boolean.pipe(optional).annotate({ - description: "Enable the experimental todowrite tool. Defaults to false.", - }), - ...Record.map(ConfigFlag.registry, (description) => + ...Record.map(ConfigFlag.registry, (description, key) => Schema.Boolean.pipe(optional).annotate({ - description: `${description} Defaults to on in local and dev builds and off in releases.`, + description: ConfigFlag.optInOnly.has(key) + ? description + : `${description} Defaults to on in local and dev builds and off in releases.`, }), ), // The flag also takes settings; an object turns it on. diff --git a/packages/schema/src/config/flag.ts b/packages/schema/src/config/flag.ts index 6e41ec9e50f..a2e905df557 100644 --- a/packages/schema/src/config/flag.ts +++ b/packages/schema/src/config/flag.ts @@ -1,14 +1,14 @@ export * as ConfigFlag from "./flag.js" -/** The `OPENCODE_FLAGS` entry that turns every flag on, as in `all,-media_blob`. It is not a flag key. */ +/** The `OPENCODE_FLAGS` entry that enables all flags except opt-in-only flags, as in `all,-media_blob`. */ export const wildcard = "all" /** - * Every feature flag, by key, with what it turns on. Flags default to on in local and dev builds and off in releases; - * `experimental.` in config and `OPENCODE_FLAGS` override that, and `OPENCODE_FLAGS=all` or - * `experimental.all_flags: true` turn every flag on. Read one with Core's `Flag.enabled("")`. + * Flags default to on in local and dev builds and off in releases, except for `optInOnly` flags, which must be + * named explicitly in `experimental.` or `OPENCODE_FLAGS`. Read a flag with Core's `Flag.enabled("")`. */ export const registry = { + todo: "Enable the experimental todowrite tool. Off by default in every build, including local and dev; opt in with OPENCODE_FLAGS=todo or experimental.todo: true.", media_blob: "Store new session media once in a deduplicated blob table instead of inline in every message row.", automations: "Run a prompt or Code Mode action on a timer, or on a connected MCP server's push event when mcp_events is enabled.", @@ -23,6 +23,8 @@ export const registry = { } as const satisfies Record export type Key = keyof typeof registry +export const optInOnly: ReadonlySet = new Set(["todo"]) + /** Earlier names of a flag, still read from `experimental.` and `OPENCODE_FLAGS` when the new key is unset. */ export const aliases = { schedules: "automations",