diff --git a/packages/core/src/database/database.ts b/packages/core/src/database/database.ts index d61adf047ea..ab86a37e74c 100644 --- a/packages/core/src/database/database.ts +++ b/packages/core/src/database/database.ts @@ -2,9 +2,9 @@ export * as Database from "./database" import { EffectDrizzleSqlite } from "@opencode-ai/effect-drizzle-sqlite" import { layer as sqliteLayer } from "#sqlite" -import { Context, Effect, Layer } from "effect" +import { Config, Context, Effect, Layer, Option } from "effect" import { Global } from "../global" -import { Flag } from "../flag/flag" +import { truthy } from "../flag/flag" import { isAbsolute, join } from "path" import { DatabaseMigration } from "./migration" import { InstallationChannel } from "../installation/version" @@ -40,18 +40,33 @@ export function layerFromPath(filename: string) { return layer.pipe(Layer.provide(sqliteLayer({ filename }))) } -export function path() { - if (Flag.OPENCODE_DB) { - if (Flag.OPENCODE_DB === ":memory:" || isAbsolute(Flag.OPENCODE_DB)) return Flag.OPENCODE_DB - return join(Global.Path.data, Flag.OPENCODE_DB) +/** One placement rule shared by the config-backed layer and the V1 `path()` helper. */ +export function resolvePath(input: { readonly file: string | undefined; readonly disableChannelDb: boolean }) { + if (input.file) { + if (input.file === ":memory:" || isAbsolute(input.file)) return input.file + return join(Global.Path.data, input.file) } - if ( - ["latest", "beta", "prod"].includes(InstallationChannel) || - process.env.OPENCODE_DISABLE_CHANNEL_DB === "1" || - process.env.OPENCODE_DISABLE_CHANNEL_DB === "true" - ) + if (["latest", "beta", "prod"].includes(InstallationChannel) || input.disableChannelDb) return join(Global.Path.data, "opencode.db") return join(Global.Path.data, `opencode-${InstallationChannel.replace(/[^a-zA-Z0-9._-]/g, "-")}.db`) } -export const node = makeGlobalNode({ service: Service, layer: layerFromPath(path()), deps: [] }) +/** V1 compatibility helper; reads the process environment at call time. */ +export function path() { + return resolvePath({ + file: process.env.OPENCODE_DB, + disableChannelDb: truthy("OPENCODE_DISABLE_CHANNEL_DB"), + }) +} + +// Placement is resolved through Effect Config when the layer is built, not at +// module import, so tests and tooling can override it with a ConfigProvider. +const configuredLayer = Layer.unwrap( + Effect.gen(function* () { + const file = yield* Config.option(Config.string("OPENCODE_DB")) + const disableChannelDb = yield* Config.boolean("OPENCODE_DISABLE_CHANNEL_DB").pipe(Config.withDefault(false)) + return layerFromPath(resolvePath({ file: Option.getOrUndefined(file), disableChannelDb })) + }).pipe(Effect.orDie), +) + +export const node = makeGlobalNode({ service: Service, layer: configuredLayer, deps: [] }) diff --git a/packages/core/src/flag/flag.ts b/packages/core/src/flag/flag.ts index a0eb78a13e2..47222a5e40f 100644 --- a/packages/core/src/flag/flag.ts +++ b/packages/core/src/flag/flag.ts @@ -44,7 +44,6 @@ export const Flag = { copy === undefined ? process.platform === "win32" : truthy("OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT"), OPENCODE_MODELS_URL: process.env["OPENCODE_MODELS_URL"], OPENCODE_MODELS_PATH: process.env["OPENCODE_MODELS_PATH"], - OPENCODE_DB: process.env["OPENCODE_DB"], OPENCODE_WORKSPACE_ID: process.env["OPENCODE_WORKSPACE_ID"], OPENCODE_EXPERIMENTAL_WORKSPACES: enabledByExperimental("OPENCODE_EXPERIMENTAL_WORKSPACES"), diff --git a/packages/core/src/tool/websearch.ts b/packages/core/src/tool/websearch.ts index 6d62236316a..efd126f253a 100644 --- a/packages/core/src/tool/websearch.ts +++ b/packages/core/src/tool/websearch.ts @@ -1,11 +1,10 @@ export * as WebSearchTool from "./websearch" import { ToolFailure } from "@opencode-ai/llm" -import { Context, Duration, Effect, Layer, Schema } from "effect" +import { Config, Context, Duration, Effect, Layer, Option, Redacted, Schema } from "effect" import { HttpClient, HttpClientRequest } from "effect/unstable/http" import { makeLocationNode } from "../effect/app-node" import { LayerNodePlatform } from "../effect/app-node-platform" -import { truthy } from "../flag/flag" import { InstallationVersion } from "../installation/version" import { PositiveInt } from "../schema" import { PermissionV2 } from "../permission" @@ -69,19 +68,33 @@ export interface Config { export class ConfigService extends Context.Service()("@opencode/v2/WebSearchConfig") {} -/** Isolates the retained product environment contract from the generic tool implementation. */ -export const defaultConfigLayer = Layer.sync(ConfigService, () => - ConfigService.of({ - provider: - process.env.OPENCODE_WEBSEARCH_PROVIDER === "exa" || process.env.OPENCODE_WEBSEARCH_PROVIDER === "parallel" - ? process.env.OPENCODE_WEBSEARCH_PROVIDER - : undefined, - enableExa: truthy("OPENCODE_EXPERIMENTAL") || truthy("OPENCODE_ENABLE_EXA") || truthy("OPENCODE_EXPERIMENTAL_EXA"), - enableParallel: truthy("OPENCODE_ENABLE_PARALLEL") || truthy("OPENCODE_EXPERIMENTAL_PARALLEL"), - exaApiKey: process.env.EXA_API_KEY, - parallelApiKey: process.env.PARALLEL_API_KEY, +const flag = (name: string) => Config.boolean(name).pipe(Config.withDefault(false)) + +/** + * Isolates the retained product environment contract from the generic tool + * implementation. Reads through Effect `Config` when the layer is built, so + * tests can override values with a `ConfigProvider` instead of mutating + * `process.env`. Malformed values fail the layer instead of silently + * disabling a provider the user asked for. + */ +export const defaultConfigLayer = Layer.effect( + ConfigService, + Effect.gen(function* () { + const provider = yield* Config.option(Config.literals(["exa", "parallel"], "OPENCODE_WEBSEARCH_PROVIDER")) + const exaApiKey = yield* Config.option(Config.redacted("EXA_API_KEY")) + const parallelApiKey = yield* Config.option(Config.redacted("PARALLEL_API_KEY")) + return ConfigService.of({ + provider: Option.getOrUndefined(provider), + enableExa: + (yield* flag("OPENCODE_EXPERIMENTAL")) || + (yield* flag("OPENCODE_ENABLE_EXA")) || + (yield* flag("OPENCODE_EXPERIMENTAL_EXA")), + enableParallel: (yield* flag("OPENCODE_ENABLE_PARALLEL")) || (yield* flag("OPENCODE_EXPERIMENTAL_PARALLEL")), + exaApiKey: Option.getOrUndefined(Option.map(exaApiKey, Redacted.value)), + parallelApiKey: Option.getOrUndefined(Option.map(parallelApiKey, Redacted.value)), + }) }), -) +).pipe(Layer.orDie) export const configNode = makeLocationNode({ service: ConfigService, layer: defaultConfigLayer, deps: [] }) diff --git a/packages/core/test/database-path.test.ts b/packages/core/test/database-path.test.ts new file mode 100644 index 00000000000..20ab38957c4 --- /dev/null +++ b/packages/core/test/database-path.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, test } from "bun:test" +import path from "path" +import { ConfigProvider, Effect, Layer } from "effect" +import { Database } from "@opencode-ai/core/database/database" +import { Global } from "@opencode-ai/core/global" +import { LayerNode } from "@opencode-ai/core/effect/layer-node" +import { tmpdir } from "./fixture/tmpdir" + +describe("Database placement", () => { + test("resolves explicit files, relative names, and the channel default", () => { + expect(Database.resolvePath({ file: ":memory:", disableChannelDb: false })).toBe(":memory:") + expect(Database.resolvePath({ file: "/tmp/explicit.db", disableChannelDb: false })).toBe("/tmp/explicit.db") + expect(Database.resolvePath({ file: "relative.db", disableChannelDb: false })).toBe( + path.join(Global.Path.data, "relative.db"), + ) + expect(Database.resolvePath({ file: undefined, disableChannelDb: true })).toBe( + path.join(Global.Path.data, "opencode.db"), + ) + }) + + test("reads placement from the active ConfigProvider when the layer is built", async () => { + await using tmp = await tmpdir() + const file = path.join(tmp.path, "config-seam.sqlite") + + // The preload sets OPENCODE_DB=":memory:" in the process environment, so a + // database appearing at this path proves the layer reads through the + // replaced ConfigProvider rather than the environment snapshot. + await Effect.runPromise( + Layer.build( + LayerNode.compile(LayerNode.group([Database.node])).pipe( + Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ OPENCODE_DB: file }))), + ), + ).pipe(Effect.scoped, Effect.asVoid), + ) + + expect(await Bun.file(file).exists()).toBe(true) + }) +}) diff --git a/packages/core/test/tool-websearch.test.ts b/packages/core/test/tool-websearch.test.ts index 0b99a80ecb3..55345b1a51e 100644 --- a/packages/core/test/tool-websearch.test.ts +++ b/packages/core/test/tool-websearch.test.ts @@ -1,5 +1,5 @@ import { beforeEach, describe, expect, test } from "bun:test" -import { Effect, Layer, Schema } from "effect" +import { ConfigProvider, Effect, Exit, Layer, Schema } from "effect" import { HttpClient, HttpClientResponse } from "effect/unstable/http" import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder" import { LayerNode } from "@opencode-ai/core/effect/layer-node" @@ -47,6 +47,52 @@ describe("WebSearchTool provider selection", () => { }) }) +const readDefaultConfig = (env: Record) => + Effect.gen(function* () { + return yield* WebSearchTool.ConfigService + }).pipe( + Effect.provide( + WebSearchTool.defaultConfigLayer.pipe(Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown(env)))), + ), + ) + +describe("WebSearchTool default config", () => { + test("decodes an empty environment to defaults", async () => { + expect(await Effect.runPromise(readDefaultConfig({}))).toEqual({ + provider: undefined, + enableExa: false, + enableParallel: false, + exaApiKey: undefined, + parallelApiKey: undefined, + }) + }) + + test("decodes provider, truthy flags, and credentials from the active ConfigProvider", async () => { + expect( + await Effect.runPromise( + readDefaultConfig({ + OPENCODE_WEBSEARCH_PROVIDER: "parallel", + OPENCODE_ENABLE_EXA: "1", + OPENCODE_EXPERIMENTAL_PARALLEL: "true", + EXA_API_KEY: "exa-key", + PARALLEL_API_KEY: "parallel-key", + }), + ), + ).toEqual({ + provider: "parallel", + enableExa: true, + enableParallel: true, + exaApiKey: "exa-key", + parallelApiKey: "parallel-key", + }) + }) + + test("fails on an invalid provider instead of silently ignoring it", async () => { + const exit = await Effect.runPromiseExit(readDefaultConfig({ OPENCODE_WEBSEARCH_PROVIDER: "bing" })) + expect(Exit.isFailure(exit)).toBe(true) + }) +}) + describe("WebSearchTool MCP response parser", () => { test("parses plain JSON-RPC responses", async () => { expect(await Effect.runPromise(WebSearchTool.parseResponse(payload("search results")))).toBe("search results") diff --git a/packages/opencode/test/server/httpapi-exercise/environment.ts b/packages/opencode/test/server/httpapi-exercise/environment.ts index 9d3eaa0e532..985215c451a 100644 --- a/packages/opencode/test/server/httpapi-exercise/environment.ts +++ b/packages/opencode/test/server/httpapi-exercise/environment.ts @@ -19,7 +19,6 @@ export const exerciseDatabasePath = process.env.OPENCODE_HTTPAPI_EXERCISE_DB ?? path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-exercise-${process.pid}.db`) process.env.OPENCODE_DB = exerciseDatabasePath -Flag.OPENCODE_DB = exerciseDatabasePath export const original = { OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD, diff --git a/packages/sdk-next/test/embedded.test.ts b/packages/sdk-next/test/embedded.test.ts index 5c1b8b238a3..01cbec0e667 100644 --- a/packages/sdk-next/test/embedded.test.ts +++ b/packages/sdk-next/test/embedded.test.ts @@ -1,15 +1,20 @@ -import { expect, test } from "bun:test" +import { afterAll, expect, test } from "bun:test" import { mkdtemp, rm } from "node:fs/promises" import { tmpdir } from "node:os" import { join } from "node:path" -import { Flag } from "@opencode-ai/core/flag/flag" import { Deferred, Effect, Latch, Option, Schema, Stream } from "effect" import type { OpenCodeEvent } from "../src" +// The database layer resolves OPENCODE_DB through Effect Config, and the +// default ConfigProvider snapshots the process environment on first use, so +// database placement is process-wide. Point every embedded host in this file +// at one shared temporary database before anything builds a runtime. +const databaseDirectory = await mkdtemp(join(tmpdir(), "opencode-embedded-db-")) +process.env.OPENCODE_DB = join(databaseDirectory, "opencode.sqlite") +afterAll(() => rm(databaseDirectory, { recursive: true, force: true })) + test("embedded client uses the real router and handlers", async () => { const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-")) - const database = Flag.OPENCODE_DB - Flag.OPENCODE_DB = join(directory, "opencode.sqlite") const { AbsolutePath, Agent, Location, Model, OpenCode, Prompt, Provider, Session, Tool } = await import("../src") const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`) const model = Model.Ref.make({ id: Model.ID.make("embedded"), providerID: Provider.ID.make("test") }) @@ -99,15 +104,12 @@ test("embedded client uses the real router and handlers", async () => { }) await Effect.runPromise(Effect.scoped(program)) } finally { - Flag.OPENCODE_DB = database await rm(directory, { recursive: true, force: true }) } }) test("Location-owned runner events reach the ready global client", async () => { const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-events-")) - const database = Flag.OPENCODE_DB - Flag.OPENCODE_DB = join(directory, "opencode.sqlite") const { AbsolutePath, Location, OpenCode, Prompt, Session } = await import("../src") const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`) @@ -138,15 +140,12 @@ test("Location-owned runner events reach the ready global client", async () => { }) await Effect.runPromise(Effect.scoped(program)) } finally { - Flag.OPENCODE_DB = database await rm(directory, { recursive: true, force: true }) } }, 10_000) test("independent embedded hosts do not share live notifications", async () => { const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-hosts-")) - const database = Flag.OPENCODE_DB - Flag.OPENCODE_DB = join(directory, "opencode.sqlite") const { AbsolutePath, Agent, Location, OpenCode, Session } = await import("../src") const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`) @@ -181,15 +180,12 @@ test("independent embedded hosts do not share live notifications", async () => { }) await Effect.runPromise(Effect.scoped(program)) } finally { - Flag.OPENCODE_DB = database await rm(directory, { recursive: true, force: true }) } }, 10_000) test("embedded client is available as a Layer service", async () => { const directory = await mkdtemp(join(tmpdir(), "opencode-embedded-layer-")) - const database = Flag.OPENCODE_DB - Flag.OPENCODE_DB = join(directory, "opencode.sqlite") const { AbsolutePath, Location, OpenCode, Session } = await import("../src") const sessionID = Session.ID.make(`ses_embedded_${crypto.randomUUID()}`) @@ -206,7 +202,6 @@ test("embedded client is available as a Layer service", async () => { expect(created.id).toBe(sessionID) } finally { - Flag.OPENCODE_DB = database await rm(directory, { recursive: true, force: true }) } })