diff --git a/AGENTS.md b/AGENTS.md index d4282d4db0e..3888d138060 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -180,6 +180,12 @@ const table = sqliteTable("session", { - A breaking move of shipped SDK or plugin API (including moving it under `experimental`) keeps the old path and type names as deprecated aliases of the new ones until the next minor, each marked `@deprecated` with the replacement and "Removed in the next minor". Generated SDK moves get them from the codegen (`compile(..., { aliases: true })` in `packages/client/script/generate.ts`), not by hand. - Experimental plugin hooks are named `experimental.*` (`experimental.ws.handshake`). +### Feature Flags + +- 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 are on in local and dev builds and off in releases; `experimental.` in config and `OPENCODE_FLAGS="key,-other"` override that. +- Ship a feature by deleting its flag and the checks that read it. + ## Testing - Avoid mocks as much as possible, you shouldn't be using globalThis.\* at all unless it's the only option. diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 877ff90e760..73c6bbd3af2 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -2548,10 +2548,10 @@ export type ConfigEntry = todo?: boolean background_compaction?: boolean media_blob?: boolean - part_rows?: boolean - image_generate?: boolean | { model?: string | { providerID: string; model: string; variant?: string } } schedules?: boolean artifacts?: boolean + part_rows?: boolean + image_generate?: boolean | { model?: string | { providerID: string; model: string; variant?: string } } subagent_depth?: number location_idle_timeout?: string | false policies?: Array<{ diff --git a/packages/core/src/artifact.ts b/packages/core/src/artifact.ts index 29cee93b7d9..d59263696c3 100644 --- a/packages/core/src/artifact.ts +++ b/packages/core/src/artifact.ts @@ -19,8 +19,10 @@ import type { SessionID } from "@opencode/schema/session-id" import { Global } from "@opencode/util/global" import { makeLocationNode } from "@opencode/util/effect/app-node" import { Clock, Context, Effect, Layer, Option, Schema } from "effect" +import { App } from "./app.js" import { Bus } from "./bus.js" import { Config } from "./config.js" +import { Flag } from "./flag.js" import { KV } from "./kv.js" import { Location } from "./location.js" import { Mime } from "./mime.js" @@ -209,6 +211,7 @@ const layer = Layer.effect( const kv = yield* KV.Service const location = yield* Location.Service const sessions = yield* Session.Service + const flags = yield* Flag.resolver const rootDir = path.join(global.data, "artifacts") const itemDir = (id: ID) => path.join(rootDir, id) @@ -265,7 +268,7 @@ const layer = Layer.effect( const isEnabled = Effect.fn("Artifact.enabled")(function* () { const entries = yield* config.entries() - return Config.latest(entries, "experimental")?.artifacts === true + return flags(entries).artifacts.enabled }) const requireEnabled = () => @@ -455,7 +458,7 @@ const layer = Layer.effect( export const node = makeLocationNode({ service: Service, layer, - deps: [Bus.node, Config.node, Global.node, KV.node, Location.node, Session.node], + deps: [App.node, Bus.node, Config.node, Global.node, KV.node, Location.node, Session.node], }) function encodeStringFiles(files: Readonly>) { diff --git a/packages/core/src/config/normalize.ts b/packages/core/src/config/normalize.ts index 86966dfec34..7e143dfd138 100644 --- a/packages/core/src/config/normalize.ts +++ b/packages/core/src/config/normalize.ts @@ -459,96 +459,12 @@ function normalizeExperimental( unsupportedExperimental.forEach((key) => unsupportedIfPresent(experimental, key, ["experimental", key], diagnostics), ) - if (own(experimental, "portable_shell_scanner")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.portable_shell_scanner, - experimental.portable_shell_scanner, - ["experimental", "portable_shell_scanner"], - diagnostics, - ) - if (value !== undefined) result.portable_shell_scanner = value - } - if (own(experimental, "todo")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.todo, - experimental.todo, - ["experimental", "todo"], - diagnostics, - ) - if (value !== undefined) result.todo = value - } - if (own(experimental, "background_compaction")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.background_compaction, - experimental.background_compaction, - ["experimental", "background_compaction"], - diagnostics, - ) - if (value !== undefined) result.background_compaction = value - } - if (own(experimental, "media_blob")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.media_blob, - experimental.media_blob, - ["experimental", "media_blob"], - diagnostics, - ) - if (value !== undefined) result.media_blob = value - } - if (own(experimental, "part_rows")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.part_rows, - experimental.part_rows, - ["experimental", "part_rows"], - diagnostics, - ) - if (value !== undefined) result.part_rows = value - } - if (own(experimental, "image_generate")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.image_generate, - experimental.image_generate, - ["experimental", "image_generate"], - diagnostics, - ) - if (value !== undefined) result.image_generate = value - } - if (own(experimental, "schedules")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.schedules, - experimental.schedules, - ["experimental", "schedules"], - diagnostics, - ) - if (value !== undefined) result.schedules = value - } - if (own(experimental, "artifacts")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.artifacts, - experimental.artifacts, - ["experimental", "artifacts"], - diagnostics, - ) - if (value !== undefined) result.artifacts = value - } - if (own(experimental, "subagent_depth")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.subagent_depth, - experimental.subagent_depth, - ["experimental", "subagent_depth"], - diagnostics, - ) - if (value !== undefined) result.subagent_depth = value - } - if (own(experimental, "location_idle_timeout")) { - const value = decodeEncoded( - ConfigExperimental.Info.fields.location_idle_timeout, - experimental.location_idle_timeout, - ["experimental", "location_idle_timeout"], - diagnostics, - ) - if (value !== undefined) result.location_idle_timeout = value - } + // `policies` merges into top-level `policies` in normalizePolicies. + Object.entries(ConfigExperimental.Info.fields).forEach(([key, schema]) => { + if (key === "policies" || !own(experimental, key)) return + const value = decodeEncoded(schema, experimental[key], ["experimental", key], diagnostics) + if (value !== undefined) result[key] = value + }) } } if (Object.keys(result).length || (isRecord(input.experimental) && !Object.keys(input.experimental).length)) diff --git a/packages/core/src/config/plugin/compaction.ts b/packages/core/src/config/plugin/compaction.ts index acd74c30363..7dd4cb32848 100644 --- a/packages/core/src/config/plugin/compaction.ts +++ b/packages/core/src/config/plugin/compaction.ts @@ -3,6 +3,7 @@ export * as ConfigCompactionPlugin from "./compaction.js" import { define } from "@opencode/plugin/effect/plugin" import { Effect } from "effect" import { Config } from "../../config.js" +import { Flag } from "../../flag.js" import { SessionCompaction } from "../../session/compaction.js" import { ConfigEntryObserver } from "./entry-observer.js" @@ -11,9 +12,10 @@ export const Plugin = define({ effect: Effect.fn(function* (ctx) { const config = yield* Config.Service const compaction = yield* SessionCompaction.Service + const flags = yield* Flag.resolver const loaded = yield* ConfigEntryObserver.observe(config, ctx.event, compaction.reload()) yield* compaction.transform((editor) => { - editor.configure({ precompute: Config.latest(loaded.entries, "experimental")?.background_compaction === true }) + editor.configure({ precompute: flags(loaded.entries).background_compaction.enabled }) for (const entry of loaded.entries) { if (entry.type !== "document" || !entry.info.compaction) continue editor.configure({ diff --git a/packages/core/src/flag.ts b/packages/core/src/flag.ts new file mode 100644 index 00000000000..118c9dd78fe --- /dev/null +++ b/packages/core/src/flag.ts @@ -0,0 +1,71 @@ +export * as Flag from "./flag.js" + +import { Context, Effect, Option, Record } from "effect" +import { ConfigFlag } from "@opencode/schema/config/flag" +import type { Entry } from "@opencode/schema/config" +import { App } from "./app.js" +import { Config } from "./config.js" + +export type Key = ConfigFlag.Key + +export interface State { + readonly enabled: boolean + /** Where `enabled` came from, highest priority first. */ + readonly source: "env" | "config" | "default" +} + +/** `OPENCODE_FLAGS`, a comma-separated list such as `schedules,-media_blob`; a leading `-` turns a flag off. */ +export const Overrides = Context.Reference("@opencode/Flag/Overrides", { + defaultValue: () => process.env.OPENCODE_FLAGS, +}) + +// Any other channel, such as a release or a branch preview build, keeps flags off so none turns on by accident. +const onByDefault = (channel: string) => channel === "local" || channel === "dev" + +/** + * Resolves every flag: `OPENCODE_FLAGS`, then `experimental.` from the highest-priority document that sets + * `experimental` (the same document every other `experimental` setting reads), then on for local and dev builds. + * A remote kill switch belongs above `OPENCODE_FLAGS` here. + */ +export function resolve(input: { + readonly entries: readonly Entry[] + readonly channel: string + readonly overrides?: string +}) { + const env = new Map( + (input.overrides ?? "") + .split(",") + .map((item) => item.trim()) + .filter(Boolean) + .map((item): [string, boolean] => (item.startsWith("-") ? [item.slice(1), false] : [item, true])), + ) + const experimental = Config.latest(input.entries, "experimental") + return Record.map(ConfigFlag.registry, (_, key): State => { + const override = env.get(key) + const configured = experimental?.[key] + const [enabled, source]: [boolean, State["source"]] = + override !== undefined + ? [override, "env"] + : configured !== undefined + ? [configured, "config"] + : [onByDefault(input.channel), "default"] + return { enabled, source } + }) +} + +/** {@link resolve} for this process's channel and `OPENCODE_FLAGS`, for synchronous readers of config entries. */ +export const resolver = Effect.gen(function* () { + const app = yield* App.Metadata + const overrides = yield* Overrides + return (entries: readonly Entry[]) => resolve({ entries, channel: app.channel, overrides }) +}) + +/** Every flag with its value and where it came from, read from the Config in scope on each call. */ +export const all = Effect.gen(function* () { + const config = yield* Effect.serviceOption(Config.Service) + const entries = Option.isSome(config) ? yield* config.value.entries() : [] + const read = yield* resolver + return read(entries) +}) + +export const enabled = (key: Key) => all.pipe(Effect.map((flags) => flags[key].enabled)) diff --git a/packages/core/src/plugin/internal.ts b/packages/core/src/plugin/internal.ts index e2858118a9f..a5fed55d534 100644 --- a/packages/core/src/plugin/internal.ts +++ b/packages/core/src/plugin/internal.ts @@ -9,6 +9,7 @@ import { AppProcess } from "@opencode/util/process" import { Context, Effect, Scope } from "effect" import { FetchHttpClient, HttpClient } from "effect/http" import { Agent } from "../agent.js" +import { App } from "../app.js" import { Artifact } from "../artifact.js" import { Model } from "../model.js" import { Provider } from "../provider.js" @@ -119,6 +120,8 @@ import { WellKnownPlugin } from "../wellknown/plugin.js" const services = [ Agent.Service, + // Activation clears the ambient context, so built-ins see the configured build channel only through this. + App.Metadata, FetchHttpClient.Fetch, ConsoleAccess.Options, Artifact.Service, @@ -179,6 +182,7 @@ export type Requirements = Context.Service.Identifier<(typeof services)[number]> export const requirements = LayerNode.group([ Agent.node, + App.node, ConsoleAccess.node, Artifact.node, AppProcess.node, diff --git a/packages/core/src/schedule.ts b/packages/core/src/schedule.ts index f9fdda76ac7..16dad0ac5ba 100644 --- a/packages/core/src/schedule.ts +++ b/packages/core/src/schedule.ts @@ -20,9 +20,11 @@ import { toCreateInput, When, } from "@opencode/schema/schedule" +import { App } from "./app.js" import { Config } from "./config.js" import { Database } from "./database/database.js" import { FileSystem } from "./filesystem.js" +import { Flag } from "./flag.js" import { Instance } from "./instance/service.js" import { KVTable } from "./kv/sql.js" import type { Permission } from "./permission.js" @@ -212,6 +214,7 @@ const layer = Layer.effect( const instances = yield* Instance.Service const execution = yield* SessionExecution.Service const doorbell = yield* Latch.make() + const flags = yield* Flag.resolver // Config is per location, so a schedule follows the config where its Session lives. const settings = (session: SessionSchema.Info) => @@ -220,7 +223,7 @@ const layer = Layer.effect( if (Option.isNone(config)) return { enabled: false, timezone: undefined } const entries = yield* config.value.entries() return { - enabled: Config.latest(entries, "experimental")?.schedules === true, + enabled: flags(entries).schedules.enabled, timezone: Config.latest(entries, "timezone"), } }).pipe(instances.provide(session)) @@ -827,5 +830,5 @@ const layer = Layer.effect( export const node: LayerNode.Provider = makeGlobalNode({ service: Service, layer, - deps: [Database.node, Session.node, SessionExecution.node, Instance.node], + deps: [App.node, Database.node, Session.node, SessionExecution.node, Instance.node], }) diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 8660a673c9b..8c1f40b7679 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -280,7 +280,7 @@ const layer = Layer.effect( const environments = yield* SessionEnvironment.Service const locations = yield* LocationServiceMap.Service const sessions = yield* Session.make() - const mediaBlob = (session: SessionSchema.Info) => SessionBlob.enabledFor(instances, session) + const mediaBlob = (session: SessionSchema.Info) => SessionBlob.enabledFor(instances, app, session) const isDurableSessionEvent = (item: Bus.LogItem): item is SessionEvent.DurableEvent => Object.hasOwn(SessionEvent.Durable.cases, item.type) diff --git a/packages/core/src/session/blob.ts b/packages/core/src/session/blob.ts index a264d41bc42..64b81c9698e 100644 --- a/packages/core/src/session/blob.ts +++ b/packages/core/src/session/blob.ts @@ -6,9 +6,11 @@ import { Cause, Context, Effect, Option, Schema } from "effect" import { map } from "effect/Array" import type { Attachment, FileAttachment } from "@opencode/schema/prompt" import type { Tool } from "@opencode/schema/tool" +import { App } from "../app.js" import { Config } from "../config.js" import { DatabaseBatch } from "../database/batch.js" import type { Database } from "../database/database.js" +import { Flag } from "../flag.js" import type { Instance } from "../instance/service.js" import type { SessionMessage } from "./message.js" import type { SessionSchema } from "./schema.js" @@ -257,19 +259,21 @@ export interface Owner { type Pending = Map type AssistantPart = SessionMessage.Assistant["content"][number] -/** Reads `experimental.media_blob` from the Config in scope on every call; without one, it is off. */ +/** Reads the `media_blob` flag from the Config in scope on every call; without one, it is off. */ export const enabled = Effect.gen(function* () { const config = yield* Effect.serviceOption(Config.Service) if (Option.isNone(config)) return false - return Config.latest(yield* config.value.entries(), "experimental")?.media_blob === true + return yield* Flag.enabled("media_blob") }) /** - * {@link enabled} as `session`'s location configures it, where its media is written. Off when that location can't - * boot or load its config, which surfaces as defects too, so a broken location never fails the caller. + * {@link enabled} as `session`'s location configures it, where its media is written, for the build channel in `app` + * (location services don't carry it). Off when that location can't boot or load its config, which surfaces as + * defects too, so a broken location never fails the caller. */ -export const enabledFor = (instances: Instance.Interface, session: SessionSchema.Info) => +export const enabledFor = (instances: Instance.Interface, app: App.Info, session: SessionSchema.Info) => enabled.pipe( + Effect.provideService(App.Metadata, app), instances.provide(session), Effect.catchCause((cause) => Cause.hasInterrupts(cause) diff --git a/packages/core/src/session/transfer.ts b/packages/core/src/session/transfer.ts index 5d6c8f05126..94795cd40d4 100644 --- a/packages/core/src/session/transfer.ts +++ b/packages/core/src/session/transfer.ts @@ -98,7 +98,7 @@ const layer = Layer.effect( const split = messages.map((message) => SessionBlob.split(message.data)) const media = split.some((row) => row.moved) && - (yield* SessionBlob.enabledFor(instances, { + (yield* SessionBlob.enabledFor(instances, app, { ...input.data.info, projectID: project.id, location: input.location, diff --git a/packages/core/src/tool/plugin/artifact.ts b/packages/core/src/tool/plugin/artifact.ts index 52875543289..9b353f16891 100644 --- a/packages/core/src/tool/plugin/artifact.ts +++ b/packages/core/src/tool/plugin/artifact.ts @@ -7,6 +7,7 @@ import { Effect, Schema, Stream } from "effect" import { Artifact } from "../../artifact.js" import { Config } from "../../config.js" import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js" +import { Flag } from "../../flag.js" export const CreateToolInput = Schema.Struct({ title: Schema.String.annotate({ description: "Human-readable artifact title." }), @@ -61,7 +62,8 @@ export const Plugin = { Effect.forkScoped({ startImmediately: true }), ) - const enabled = () => Config.latest(loaded.entries, "experimental")?.artifacts === true + const flags = yield* Flag.resolver + const enabled = () => flags(loaded.entries).artifacts.enabled const failure = (error: { readonly message: string }) => new ToolFailure({ message: error.message, error }) yield* ctx.tool diff --git a/packages/core/src/tool/plugin/schedule.ts b/packages/core/src/tool/plugin/schedule.ts index 781ef69a5bd..7b17f772167 100644 --- a/packages/core/src/tool/plugin/schedule.ts +++ b/packages/core/src/tool/plugin/schedule.ts @@ -7,6 +7,7 @@ import type { Session } from "@opencode/schema/session" import { DateTime, Effect, Option, Schema } from "effect" import { Config } from "../../config.js" import { ConfigEntryObserver } from "../../config/plugin/entry-observer.js" +import { Flag } from "../../flag.js" import PROMPT_SCHEDULE from "../../plugin/command/schedule.txt" import { Schedule } from "../../schedule.js" @@ -52,7 +53,8 @@ export const Plugin = { ctx.event, Effect.andThen(ctx.tool.reload(), ctx.command.reload()), ) - const enabled = () => Config.latest(loaded.entries, "experimental")?.schedules === true + const flags = yield* Flag.resolver + const enabled = () => flags(loaded.entries).schedules.enabled // A request keeps the tools it started with after config reloads, so each call rechecks the flag. const allowed = (sessionID: Session.ID) => diff --git a/packages/core/src/tool/plugin/todo.ts b/packages/core/src/tool/plugin/todo.ts index 68ce708e7b6..090c8232eb0 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.resolver + const enabled = () => flags(loaded.entries).todo.enabled yield* ctx.tool .transform((editor) => { diff --git a/packages/core/test/config/normalization.test.ts b/packages/core/test/config/normalization.test.ts index 884bb5feb4b..eed91b86a03 100644 --- a/packages/core/test/config/normalization.test.ts +++ b/packages/core/test/config/normalization.test.ts @@ -600,6 +600,14 @@ describe("ConfigNormalize", () => { } }) + test("normalizes every registered flag as a boolean", () => { + const result = normalized({ experimental: { todo: true, media_blob: false, schedules: "yes" } }) + expect(result.encoded.experimental).toEqual({ todo: true, media_blob: false }) + expect(result.diagnostics.map((item) => [item.kind, item.path])).toEqual([ + ["invalid", ["experimental", "schedules"]], + ]) + }) + test("never emits experimental policies", () => { expect( normalized({ experimental: { policies: [{ action: "provider.use", resource: "*", effect: "deny" }] } }).encoded, diff --git a/packages/core/test/flag.test.ts b/packages/core/test/flag.test.ts new file mode 100644 index 00000000000..ed80984db1b --- /dev/null +++ b/packages/core/test/flag.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, test } from "bun:test" +import { App } from "@opencode/core/app" +import { Config } from "@opencode/core/config" +import { Flag } from "@opencode/core/flag" +import { Document, Info } from "@opencode/schema/config" +import { ConfigExperimental } from "@opencode/schema/config/experimental" +import { Effect, Layer } from "effect" +import { testEffect } from "./lib/effect" + +const document = (experimental?: ConstructorParameters[0]) => + new Document({ + type: "document", + info: new Info(experimental === undefined ? {} : { experimental: new ConfigExperimental.Info(experimental) }), + }) + +describe("Flag.resolve", () => { + test("turns flags on by default only in local and dev builds", () => { + const table = ["local", "dev", "beta", "latest", "my-branch", "unknown"].map((channel) => [ + channel, + Flag.resolve({ entries: [], channel }).todo.enabled, + ]) + expect(Object.fromEntries(table)).toEqual({ + local: true, + dev: true, + beta: false, + latest: false, + "my-branch": false, + unknown: false, + }) + }) + + test("config overrides the default and OPENCODE_FLAGS overrides config", () => { + const entries = [document({ todo: true, schedules: false })] + const flags = Flag.resolve({ entries, channel: "dev", overrides: " media_blob , -todo,unknown_flag" }) + expect( + [flags.todo, flags.schedules, flags.media_blob, flags.artifacts].map((flag) => [flag.enabled, flag.source]), + ).toEqual([ + [false, "env"], + [false, "config"], + [true, "env"], + [true, "default"], + ]) + }) + + test("reads experimental from the highest-priority document that sets it, as before", () => { + const flags = Flag.resolve({ + entries: [document({ todo: true }), document({ subagent_depth: 2 }), document()], + channel: "latest", + }) + expect([flags.todo.enabled, flags.todo.source]).toEqual([false, "default"]) + }) +}) + +const it = testEffect( + Layer.mergeAll( + Config.testLayer([document({ artifacts: true })]), + App.layer({ channel: "latest" }), + Layer.succeed(Flag.Overrides, "schedules"), + ), +) + +describe("Flag.enabled", () => { + it.effect("reads the Config, channel and OPENCODE_FLAGS in scope", () => + Effect.gen(function* () { + expect(yield* Flag.enabled("artifacts")).toBe(true) + expect(yield* Flag.enabled("schedules")).toBe(true) + expect(yield* Flag.enabled("todo")).toBe(false) + expect((yield* Flag.all).schedules).toEqual({ enabled: true, source: "env" }) + }), + ) +}) diff --git a/packages/core/test/plugin/internal-flag.test.ts b/packages/core/test/plugin/internal-flag.test.ts new file mode 100644 index 00000000000..62554c2e5d2 --- /dev/null +++ b/packages/core/test/plugin/internal-flag.test.ts @@ -0,0 +1,90 @@ +import { describe, expect } from "bun:test" +import { Duration, Effect, Layer, LayerMap, Stream } from "effect" +import { App } from "@opencode/core/app" +import { ConfigPluginSource } from "@opencode/core/config/plugin/source" +import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" +import { Instance } from "@opencode/core/instance" +import { Location } from "@opencode/core/location" +import { LocationServiceMap } from "@opencode/core/location-services" +import { Plugin } from "@opencode/core/plugin" +import { SdkPlugins } from "@opencode/core/plugin/sdk" +import { AbsolutePath } from "@opencode/core/schema" +import { Tool } from "@opencode/core/tool" +import { TodoTool } from "@opencode/core/tool/plugin/todo" +import { LayerNode } from "@opencode/util/effect/layer-node" +import { Global } from "@opencode/util/global" +import { Npm } from "@opencode/util/npm" +import { Bus } from "../../src/bus" +import { Database } from "../../src/database/database" +import { tempGlobalLayer } from "../fixture/global" +import { offlineModels } from "../fixture/models" +import { tmpdirScoped } from "../fixture/tmpdir" +import { testEffect } from "../lib/effect" +import { toolDefinitions } from "../lib/tool" + +const unexpected = (pkg: string) => + Effect.fail(new Npm.InstallFailedError({ add: [pkg], dir: "", cause: new Error(`Unexpected Npm call: ${pkg}`) })) + +const instances = Layer.effect( + LocationServiceMap.Service, + Effect.gen(function* () { + const map = yield* LayerMap.make( + (ref: Location.Ref) => Instance.layer(ref, { discovery: false, replacements: bindings }), + { idleTimeToLive: Duration.infinity }, + ) + const bindings: LayerNode.Replacements = [ + App.node.replace(App.configured({ channel: "dev" })), + Global.node.replace(tempGlobalLayer), + offlineModels, + ConfigPluginSource.node.replace( + Layer.succeed( + ConfigPluginSource.Service, + ConfigPluginSource.Service.of({ operations: () => Effect.succeed([]), changes: () => Stream.never }), + ), + ), + Npm.node.replace( + Layer.succeed( + Npm.Service, + Npm.Service.of({ + add: unexpected, + resolve: (name) => Effect.succeed({ directory: "", name }), + check: () => Effect.succeed(false), + update: unexpected, + which: () => Effect.undefined, + }), + ), + ), + LocationServiceMap.node.replace(Layer.succeed(LocationServiceMap.Service, map)), + Instance.node.replace( + Layer.succeed(Instance.Service, { provide: (session) => Effect.provide(map.get(session.location)) }), + ), + ] + return map + }), +) + +const it = testEffect( + AppNodeBuilder.build(LayerNode.group([Database.node, Bus.node, SdkPlugins.node, LocationServiceMap.node]), [ + Global.node.replace(tempGlobalLayer), + offlineModels, + LocationServiceMap.node.replace(instances), + ]), +) + +describe("PluginInternal flags", () => { + it.live("turns a flagged built-in tool on for a dev-channel server with no config", () => + Effect.gen(function* () { + const directory = yield* tmpdirScoped() + const locations = yield* LocationServiceMap.Service + const names = yield* Effect.gen(function* () { + yield* (yield* Plugin.Service).awaitActivation + return (yield* toolDefinitions(yield* Tool.Service)).map((definition) => definition.name) + }).pipe( + Effect.scoped, + Effect.provide(locations.get(Location.Ref.make({ directory: AbsolutePath.make(directory.path) }))), + ) + + expect(names).toContain(TodoTool.name) + }), + ) +}) diff --git a/packages/core/test/session-prompt.test.ts b/packages/core/test/session-prompt.test.ts index 2bd2e9b6dbb..412ee75104c 100644 --- a/packages/core/test/session-prompt.test.ts +++ b/packages/core/test/session-prompt.test.ts @@ -6,6 +6,7 @@ import { pathToFileURL } from "url" import { eq } from "drizzle-orm" import { Database } from "@opencode/core/database/database" import { Agent } from "@opencode/core/agent" +import { App } from "@opencode/core/app" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { LayerNode } from "@opencode/util/effect/layer-node" import { makeGlobalNode } from "@opencode/util/effect/app-node" @@ -104,7 +105,9 @@ const locations = makeGlobalNode({ normalize: (resource, content) => content.mime === "image/heic" ? Effect.fail(new Image.DecodeError({ resource })) - : Effect.succeed(content.content.length > 5 * 1024 * 1024 ? { ...content, content: "AA==" } : content), + : Effect.succeed( + content.content.length > 5 * 1024 * 1024 ? { ...content, content: "AA==" } : content, + ), }), Layer.mock(Snapshot.Service, { capture: () => Effect.undefined, @@ -117,23 +120,27 @@ const locations = makeGlobalNode({ ), deps: [Bus.node], }) -const it = testEffect( - AppNodeBuilder.build( - LayerNode.group([ - Database.node, - Bus.node, - SessionProjector.node, - SessionStore.node, - Session.node, - SessionTransfer.node, - ]), - [ - Bus.node.replace(Bus.configured({ persist: true })), - SessionExecution.node.replace(execution), - LocationServiceMap.node.replace(locations), - ], - ), -) +const harness = (channel?: string) => + testEffect( + AppNodeBuilder.build( + LayerNode.group([ + Database.node, + Bus.node, + SessionProjector.node, + SessionStore.node, + Session.node, + SessionTransfer.node, + ]), + [ + Bus.node.replace(Bus.configured({ persist: true })), + SessionExecution.node.replace(execution), + LocationServiceMap.node.replace(locations), + App.node.replace(App.configured(channel ? { channel } : {})), + ], + ), + ) +const it = harness() +const devIt = harness("dev") const sessionID = Session.ID.make("ses_prompt_test") const pngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=" const messageID = SessionMessage.ID.create() @@ -699,6 +706,26 @@ describe("Session.prompt", () => { }), ) + devIt.effect("imports media into blobs by default on a dev-channel server with no config", () => + Effect.gen(function* () { + yield* setup + const { db } = yield* Database.Service + const session = yield* Session.Service + const transfer = yield* SessionTransfer.Service + const imported = yield* transfer.import({ + data: { + info: { ...(yield* session.get(sessionID)), id: Session.ID.make("ses_import_dev") }, + messages: mediaMessages("ses_import_dev"), + }, + location: { directory: AbsolutePath.make("/project") }, + }) + + const hash = SessionBlob.hashBytes(Buffer.from(pngBase64, "base64")) + expect(yield* db.select({ hash: BlobTable.hash }).from(BlobTable).all().pipe(Effect.orDie)).toEqual([{ hash }]) + expect(yield* session.messages({ sessionID: imported.id, order: "asc" })).toEqual(mediaMessages("ses_import_dev")) + }), + ) + it.effect("imports media into blobs only where the location enables media_blob", () => Effect.gen(function* () { yield* setup diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 27e04d73e4f..b45a7a289a8 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -16664,6 +16664,12 @@ "media_blob": { "type": "boolean" }, + "schedules": { + "type": "boolean" + }, + "artifacts": { + "type": "boolean" + }, "part_rows": { "type": "boolean" }, @@ -16707,12 +16713,6 @@ } ] }, - "schedules": { - "type": "boolean" - }, - "artifacts": { - "type": "boolean" - }, "subagent_depth": { "type": "integer", "minimum": 0, diff --git a/packages/schema/src/config/experimental.ts b/packages/schema/src/config/experimental.ts index 4f3eae37b8d..0c7428e8434 100644 --- a/packages/schema/src/config/experimental.ts +++ b/packages/schema/src/config/experimental.ts @@ -1,7 +1,8 @@ export * as ConfigExperimental from "./experimental.js" -import { Schema } from "effect" +import { Record, Schema } from "effect" import { NonNegativeInt, optional } from "../schema.js" +import { ConfigFlag } from "./flag.js" import { ConfigModel } from "./model.js" import { ConfigPolicy } from "./policy.js" @@ -9,17 +10,11 @@ export class Info extends Schema.Class("ConfigExperimental.Info")({ portable_shell_scanner: Schema.Boolean.pipe(optional).annotate({ description: "Deprecated: the built-in shell permission scanner is now always enabled.", }), - todo: Schema.Boolean.pipe(optional).annotate({ - description: "Enable the experimental todowrite tool. Defaults to false.", - }), - background_compaction: Schema.Boolean.pipe(optional).annotate({ - description: - "Enable experimental background compaction: summarize the conversation in the background as it nears the compaction threshold, so automatic compaction does not wait for the summary. Background summaries that are never used still cost tokens. Defaults to false.", - }), - media_blob: Schema.Boolean.pipe(optional).annotate({ - description: - "Store new session media once in a deduplicated blob table instead of inline in every message row. Defaults to false.", - }), + ...Record.map(ConfigFlag.registry, (description) => + Schema.Boolean.pipe(optional).annotate({ + description: `${description} Defaults to on in local and dev builds and off in releases.`, + }), + ), part_rows: Schema.Boolean.pipe(optional).annotate({ description: "Write each assistant part as its own session_part row instead of rewriting the whole message on every event. Defaults to false.", @@ -38,12 +33,6 @@ export class Info extends Schema.Class("ConfigExperimental.Info")({ description: "Settings for the experimental_image_generate tool, which is offered while a connected provider has an image model.", }), - schedules: Schema.Boolean.pipe(optional).annotate({ - description: "Enable experimental scheduled tasks that wake a session with a prompt. Defaults to false.", - }), - artifacts: Schema.Boolean.pipe(optional).annotate({ - description: "Enable experimental artifacts backed by pluggable providers. Defaults to false.", - }), subagent_depth: NonNegativeInt.pipe(optional).annotate({ description: "Maximum subagent nesting depth. Defaults to 1.", }), diff --git a/packages/schema/src/config/flag.ts b/packages/schema/src/config/flag.ts new file mode 100644 index 00000000000..948524c93ad --- /dev/null +++ b/packages/schema/src/config/flag.ts @@ -0,0 +1,15 @@ +export * as ConfigFlag from "./flag.js" + +/** + * Every feature flag, by key, with what it turns on. A flag is on in local and dev builds and off in released ones; + * `experimental.` in config and `OPENCODE_FLAGS` override that. Read one with Core's `Flag.enabled("")`. + */ +export const registry = { + todo: "Enable the experimental todowrite tool.", + background_compaction: + "Enable experimental background compaction: summarize the conversation in the background as it nears the compaction threshold, so automatic compaction does not wait for the summary. Background summaries that are never used still cost tokens.", + media_blob: "Store new session media once in a deduplicated blob table instead of inline in every message row.", + schedules: "Enable experimental scheduled tasks that wake a session with a prompt.", + artifacts: "Enable experimental artifacts backed by pluggable providers.", +} as const satisfies Record +export type Key = keyof typeof registry