From 509b74722c7e3dad0458dcddbbd4a0f76afc9038 Mon Sep 17 00:00:00 2001 From: Adam <2363879+adamdotdevin@users.noreply.github.com> Date: Wed, 4 Nov 2026 07:21:04 -0600 Subject: [PATCH] feat(core): add a console.required server option that holds model requests until Console config applies --- packages/core/src/console-access.ts | 20 +- packages/core/src/plugin/internal.ts | 3 + packages/core/src/plugin/provider/opencode.ts | 53 +++++- packages/core/src/session/context.ts | 11 +- packages/core/src/session/gate.ts | 39 ++++ packages/core/src/session/generate.ts | 3 +- packages/core/test/plugin/fixture.ts | 2 + .../test/plugin/provider-opencode.test.ts | 171 +++++++++++++++++- packages/core/test/session-runner.test.ts | 50 +++++ packages/server/src/options.ts | 10 +- packages/server/src/workerd.ts | 5 +- packages/server/test/options.test.ts | 4 +- packages/server/test/workerd.test.ts | 4 + .../src/docs/content/build/sdk/cloudflare.mdx | 6 + 14 files changed, 357 insertions(+), 24 deletions(-) create mode 100644 packages/core/src/session/gate.ts diff --git a/packages/core/src/console-access.ts b/packages/core/src/console-access.ts index f76d6a4f0aa..11c0585d319 100644 --- a/packages/core/src/console-access.ts +++ b/packages/core/src/console-access.ts @@ -5,15 +5,21 @@ import { makeGlobalNode } from "@opencode/util/effect/app-node" import type { Credential } from "./credential.js" /** - * How the host keeps a connected Console's config fresh. `poll` re-checks it every minute in the background; without - * it, the config refreshes only when a prompt starts, so a host that suspends between prompts stays idle. + * How the host uses a connected Console's config. `poll` re-checks it every minute in the background; without it, + * the config refreshes only when a prompt starts, so a host that suspends between prompts stays idle. `required` + * holds every model request until the active credential's config is loaded and applied, and fails it otherwise. */ -export const Options = Context.Reference<{ readonly poll: boolean }>("@opencode/ConsoleAccess/Options", { - defaultValue: () => ({ poll: true }), -}) +export const Options = Context.Reference<{ readonly poll: boolean; readonly required: boolean }>( + "@opencode/ConsoleAccess/Options", + { defaultValue: () => ({ poll: true, required: false }) }, +) -export const configured = (options?: { readonly poll?: boolean }) => - makeGlobalNode({ service: Options, layer: Layer.succeed(Options, { poll: options?.poll ?? true }), deps: [] }) +export const configured = (options?: { readonly poll?: boolean; readonly required?: boolean }) => + makeGlobalNode({ + service: Options, + layer: Layer.succeed(Options, { poll: options?.poll ?? true, required: options?.required ?? false }), + deps: [], + }) export const node = configured() diff --git a/packages/core/src/plugin/internal.ts b/packages/core/src/plugin/internal.ts index e2858118a9f..3ce4f95c347 100644 --- a/packages/core/src/plugin/internal.ts +++ b/packages/core/src/plugin/internal.ts @@ -65,6 +65,7 @@ import { Ripgrep } from "../ripgrep.js" import { Schedule } from "../schedule.js" import { Session } from "../session.js" import { SessionCompaction } from "../session/compaction.js" +import { SessionGate } from "../session/gate.js" import { SessionInstructions } from "../session/instructions.js" import { SubagentJob } from "../session/subagent-job.js" import { SharedCache } from "../shared-cache.js" @@ -159,6 +160,7 @@ const services = [ Schedule.Service, Session.Service, SessionCompaction.Service, + SessionGate.Service, SessionInstructions.Service, SubagentJob.Service, SharedCache.Service, @@ -219,6 +221,7 @@ export const requirements = LayerNode.group([ Schedule.node, Session.node, SessionCompaction.node, + SessionGate.node, SessionInstructions.node, SubagentJob.node, SharedCache.node, diff --git a/packages/core/src/plugin/provider/opencode.ts b/packages/core/src/plugin/provider/opencode.ts index ef42265eb6b..e5d0b798eca 100644 --- a/packages/core/src/plugin/provider/opencode.ts +++ b/packages/core/src/plugin/provider/opencode.ts @@ -16,6 +16,8 @@ import { Integration } from "../../integration.js" import { IntegrationConnection } from "../../integration/connection.js" import { Provider } from "../../provider.js" import { AbsolutePath } from "../../schema.js" +import { StepFailedError } from "../../session/error.js" +import { SessionGate } from "../../session/gate.js" import { SharedCache } from "../../shared-cache.js" import { Skill } from "../../skill.js" import { SkillDiscovery } from "../../skill/discovery.js" @@ -169,6 +171,7 @@ export const OpencodePlugin = define< | Bus.Service | Config.Service | InstructionDiscovery.Service + | SessionGate.Service | SharedCache.Service | Skill.Service | Scope.Scope @@ -186,6 +189,7 @@ export const OpencodePlugin = define< const cache = yield* SharedCache.Service const scope = yield* Scope.Scope const settings = yield* ConsoleAccess.Options + const gate = yield* SessionGate.Service const loading = Semaphore.makeUnsafe(1) type ActiveConnection = Effect.Success> let snapshot: { @@ -195,7 +199,9 @@ export const OpencodePlugin = define< organization: string | undefined // Fingerprint of an API key, so a key replaced in place counts as a new sign-in. access: string | undefined - } = { remote: undefined, connection: undefined, organization: undefined, access: undefined } + // Whether the Console answered for this connection, rather than a failed load leaving it empty. + loaded: boolean + } = { remote: undefined, connection: undefined, organization: undefined, access: undefined, loaded: false } let instructions: InstructionDiscovery.File[] = [] // Tagged with the account and workspace they were downloaded for, so a switch hides them right away. let librarySkills: { owner: string; items: LibrarySkill[] } = { owner: "", items: [] } @@ -219,7 +225,10 @@ export const OpencodePlugin = define< const connection = yield* ctx.integration.connection.active("opencode") if (!connection) { reported = undefined - return { _tag: "Loaded", next: { remote: undefined, connection, organization: undefined, access: undefined } } + return { + _tag: "Loaded", + next: { remote: undefined, connection, organization: undefined, access: undefined, loaded: true }, + } } const status = (status: IntegrationConnection.Status | undefined) => { reported = status @@ -228,7 +237,13 @@ export const OpencodePlugin = define< return yield* ctx.integration.connection.resolve(connection).pipe( Effect.flatMap((credential) => { if (!credential || credential.type === "external") - return Effect.succeed({ remote: undefined, connection, organization: undefined, access: undefined }) + return Effect.succeed({ + remote: undefined, + connection, + organization: undefined, + access: undefined, + loaded: true, + }) return cache .get({ key: `opencode-console:${workspaceKey(connection, credential)}`, @@ -251,6 +266,7 @@ export const OpencodePlugin = define< connection, organization: organizationName(credential), access: accessFingerprint(credential), + loaded: true, })), ) }), @@ -272,7 +288,7 @@ export const OpencodePlugin = define< const fallback = (connection: ActiveConnection): typeof snapshot => IntegrationConnection.key(connection) === IntegrationConnection.key(snapshot.connection) ? { ...snapshot, connection } - : { remote: undefined, connection, organization: undefined, access: undefined } + : { remote: undefined, connection, organization: undefined, access: undefined, loaded: false } // The Console document as config layered it. Its providers apply at this plugin's position in the provider chain, // while the rest of the document merges through config. let integrated: ConfigIntegration.Document | undefined @@ -529,6 +545,35 @@ export const OpencodePlugin = define< // 3 seconds; the turn then runs on the config it has. yield* ctx.session.hook("prompt", () => check(Duration.minutes(1), Duration.seconds(3))) + // With the `console.required` server option, no model request is built on another credential's config or on + // none: each waits until the active credential's config is loaded and applied, or fails. Until then every request + // loads again rather than reusing a failure; once applied, a stale config refreshes as a prompt's does and stays + // in effect if the Console doesn't answer. + const applied = Effect.gen(function* () { + const active = yield* ctx.integration.connection.active("opencode") + if (!active || !snapshot.loaded) return false + if (IntegrationConnection.key(active) !== IntegrationConnection.key(snapshot.connection)) return false + const credential = Option.getOrUndefined(yield* ctx.integration.connection.resolve(active).pipe(Effect.option)) + if (!credential) return false + return snapshot.access === (credential.type === "external" ? undefined : accessFingerprint(credential)) + }) + if (settings.required) + yield* gate.register( + Effect.gen(function* () { + yield* (yield* applied) + ? check(Duration.minutes(1), Duration.seconds(3)) + : check(Duration.zero, Duration.seconds(10)) + if (yield* applied) return + // A status the Console reported, such as an expired SSO sign-in, names the fix. + const reason = + reported?.message ?? + (snapshot.connection + ? "Couldn't load the OpenCode Console config for the connected account" + : "Connect OpenCode Console to continue") + return yield* new StepFailedError({ error: { type: "console.config-unavailable", message: `${reason}.` } }) + }), + ) + // Console config can change independently of local credential activity, so re-fetch // periodically and only rebuild the catalog and search providers when the snapshot differs. // Only while connected: a signed-out location runs no timer and no credential lookups. Hosts that suspend diff --git a/packages/core/src/session/context.ts b/packages/core/src/session/context.ts index 99c54114942..e60ab724000 100644 --- a/packages/core/src/session/context.ts +++ b/packages/core/src/session/context.ts @@ -16,7 +16,8 @@ import { McpTool } from "../tool/mcp.js" import { ReferenceInstructions } from "../reference/instructions.js" import { SkillInstructions } from "../skill/instructions.js" import { Tool } from "../tool.js" -import { AgentNotFoundError } from "./error.js" +import { AgentNotFoundError, type StepFailedError } from "./error.js" +import { SessionGate } from "./gate.js" import { SessionHistory } from "./history.js" import { InstructionEntry } from "./instruction-entry.js" import { SessionMessage } from "./message.js" @@ -48,8 +49,8 @@ export interface Loaded { * capabilities they need; request preparation stays separate from selection. */ export interface Interface { - /** Selects the Session, agent, instructions, and tools used by subsequent work. */ - readonly select: (sessionID: SessionSchema.ID) => Effect.Effect + /** Waits on the Session gates, then selects the Session, agent, instructions, and tools used by subsequent work. */ + readonly select: (sessionID: SessionSchema.ID) => Effect.Effect /** Resolves the model and active history for that selection. */ readonly load: (selection: Selection) => Effect.Effect readonly resolveModel: ( @@ -79,6 +80,7 @@ const layer = Layer.effect( const db = (yield* Database.Service).db const discovery = yield* InstructionDiscovery.Service const entries = yield* InstructionEntry.Service + const gate = yield* SessionGate.Service const location = yield* Location.Service const mcpInstructions = yield* McpInstructions.Service const mcpTools = yield* McpTool.Service @@ -129,6 +131,8 @@ const layer = Layer.effect( if (session.location.directory !== location.directory || session.location.workspaceID !== location.workspaceID) return yield* Effect.interrupt + // Before anything is read, so what a gate applies, such as an organization's config, shapes this request. + yield* gate.wait yield* mcpTools.flush const agent = yield* agents.select(session.agent) if (!agent.info) return yield* new AgentNotFoundError({ sessionID: session.id, agent: session.agent ?? agent.id }) @@ -218,6 +222,7 @@ export const node = makeLocationNode({ ReferenceInstructions.node, SessionRunnerModel.node, SessionModelRequest.node, + SessionGate.node, SessionStore.node, SkillInstructions.node, Tool.node, diff --git a/packages/core/src/session/gate.ts b/packages/core/src/session/gate.ts new file mode 100644 index 00000000000..350f31a0d86 --- /dev/null +++ b/packages/core/src/session/gate.ts @@ -0,0 +1,39 @@ +export * as SessionGate from "./gate.js" + +import { Context, Effect, Layer, Scope } from "effect" +import { makeLocationNode } from "@opencode/util/effect/app-node" +import type { State } from "../state.js" +import type { StepFailedError } from "./error.js" + +type Gate = Effect.Effect + +/** + * Conditions every model request waits on before its context is selected, so its instructions, tools and policies + * are read only once they hold. Internal plugins register them; a failure fails the execution before the request is + * built. + */ +export interface Interface { + readonly register: (gate: Gate) => Effect.Effect + /** Runs every registered gate in registration order. */ + readonly wait: Gate +} + +export class Service extends Context.Service()("@opencode/SessionGate") {} + +const layer = Layer.sync(Service, () => { + const gates = new Set<{ readonly gate: Gate }>() + return Service.of({ + register: Effect.fn("SessionGate.register")(function* (gate) { + const entry = { gate } + gates.add(entry) + const dispose = Effect.sync(() => { + gates.delete(entry) + }) + yield* Effect.addFinalizer(() => dispose) + return { dispose } + }), + wait: Effect.suspend(() => Effect.forEach(gates, (entry) => entry.gate, { discard: true })), + }) +}) + +export const node = makeLocationNode({ service: Service, layer, deps: [] }) diff --git a/packages/core/src/session/generate.ts b/packages/core/src/session/generate.ts index c83baed30bf..acd978932bf 100644 --- a/packages/core/src/session/generate.ts +++ b/packages/core/src/session/generate.ts @@ -8,7 +8,7 @@ import { Instance } from "../instance/service.js" import { Plugin } from "../plugin/service.js" import type { Instructions } from "../instructions/index.js" import { SessionContext } from "./context.js" -import type { AgentNotFoundError } from "./error.js" +import type { AgentNotFoundError, StepFailedError } from "./error.js" import { SessionHistory } from "./history.js" import { SessionModelRequest } from "./model-request.js" import { ModelRetry } from "../model-retry.js" @@ -17,6 +17,7 @@ import type { SessionSchema } from "./schema.js" export type Error = | AgentNotFoundError + | StepFailedError | Instructions.InitializationBlocked | SessionRunnerModel.Error | AIError diff --git a/packages/core/test/plugin/fixture.ts b/packages/core/test/plugin/fixture.ts index 0aae53cf2b3..42d911826ed 100644 --- a/packages/core/test/plugin/fixture.ts +++ b/packages/core/test/plugin/fixture.ts @@ -29,6 +29,7 @@ import { PluginHooks } from "@opencode/core/plugin/hooks" import { Provider } from "@opencode/core/provider" import { Session } from "@opencode/core/session" import { SharedCache } from "@opencode/core/shared-cache" +import { SessionGate } from "@opencode/core/session/gate" import { InstructionEntry } from "@opencode/core/session/instruction-entry" import { PersistentPty } from "@opencode/core/persistent-pty" import { LocationServiceMap } from "@opencode/core/location-service-map" @@ -117,6 +118,7 @@ export const makePluginTestLayer = (permission = permissionLayer, generate = gen KV.node, Mcp.node, Session.node, + SessionGate.node, SharedCache.node, InstructionEntry.node, PersistentPty.node, diff --git a/packages/core/test/plugin/provider-opencode.test.ts b/packages/core/test/plugin/provider-opencode.test.ts index 24979082bb6..8a92b4688c2 100644 --- a/packages/core/test/plugin/provider-opencode.test.ts +++ b/packages/core/test/plugin/provider-opencode.test.ts @@ -4,7 +4,7 @@ import { LLMClient, RequestExecutor } from "@opencode/ai/route" import { Document, Event, Info } from "@opencode/schema/config" import { ConfigPolicy } from "@opencode/schema/config/policy" import { Money } from "@opencode/schema/money" -import { Effect, Fiber, Layer, Schema, Stream } from "effect" +import { Cause, Clock, Effect, Exit, Fiber, Layer, Schema, Stream } from "effect" import { TestClock } from "effect/testing" import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" import { Agent } from "@opencode/core/agent" @@ -18,6 +18,7 @@ import { ConfigProviderPlugin } from "@opencode/core/config/plugin/provider" import { ConfigWebSearchPlugin } from "@opencode/core/config/plugin/websearch" import { ConsoleAccess } from "@opencode/core/console-access" import { Credential } from "@opencode/core/credential" +import { InstructionDiscovery } from "@opencode/core/instruction-discovery" import { Integration } from "@opencode/core/integration" import { Mcp } from "@opencode/core/mcp/index" import { Model } from "@opencode/core/model" @@ -29,10 +30,11 @@ import { OpenAIPlugin } from "@opencode/core/plugin/provider/openai" import { OpencodePlugin } from "@opencode/core/plugin/provider/opencode" import { Provider } from "@opencode/core/provider" import { Session } from "@opencode/core/session" +import { SessionGate } from "@opencode/core/session/gate" import { SessionMessage } from "@opencode/core/session/message" import { WebSearch } from "@opencode/core/websearch" import { withEnv } from "../fixture/env" -import { drain } from "../lib/clock" +import { advance, drain } from "../lib/clock" import { eventually } from "../lib/eventually" import { testEffect } from "../lib/effect" import { emptyMcp } from "../fixture/mcp" @@ -878,7 +880,10 @@ describe("OpencodePlugin", () => { integrationID: Integration.ID.make("opencode"), value: key(server), }) - yield* addPlugin().pipe(inProcess(server), Effect.provideService(ConsoleAccess.Options, { poll })) + yield* addPlugin().pipe( + inProcess(server), + Effect.provideService(ConsoleAccess.Options, { poll, required: false }), + ) yield* drain return credential }) @@ -992,6 +997,164 @@ describe("OpencodePlugin", () => { ) }) + describe("requiring Console config", () => { + // Answers each key with an instruction naming it, so the applied config shows which credential it came from. + const consoleApi = () => { + const state = { status: 200, body: undefined as unknown, hang: new Set(), requests: [] as string[] } + const hanging: (() => void)[] = [] + return { + state, + release: () => hanging.splice(0).forEach((resolve) => resolve()), + fetch: (request: Request) => { + const key = request.headers.get("authorization")?.replace("Bearer ", "") ?? "" + state.requests.push(key) + const response = () => + Response.json( + state.body ?? { providers: {}, library: { instructions: [{ id: key, content: `Use ${key}` }] } }, + { status: state.status }, + ) + if (!state.hang.has(key)) return response() + return new Promise((resolve) => hanging.push(() => resolve(response()))) + }, + } + } + type ConsoleApi = ReturnType + const key = (key: string) => Credential.Key.make({ type: "key", key, metadata: { server: "https://console.test" } }) + const connect = Effect.fn(function* (api: ConsoleApi, required = true) { + const credentials = yield* Credential.Service + const credential = yield* credentials.create({ + integrationID: Integration.ID.make("opencode"), + value: key("one"), + }) + // Startup waits out the Console's retries when it fails. + const started = yield* addPlugin().pipe( + inProcess(api), + Effect.provideService(ConsoleAccess.Options, { poll: false, required }), + Effect.forkChild({ startImmediately: true }), + ) + yield* advance(() => started.pollUnsafe() !== undefined) + yield* Fiber.join(started) + return credential + }) + // What every model request waits on before its instructions and tools are read. + const request = Effect.fn(function* () { + const gate = yield* SessionGate.Service + return yield* gate.wait.pipe(Effect.forkChild({ startImmediately: true })) + }) + const settled = Effect.fn(function* (fiber: Fiber.Fiber) { + const started = yield* Clock.currentTimeMillis + yield* advance(() => fiber.pollUnsafe() !== undefined) + return { exit: yield* Fiber.await(fiber), elapsed: (yield* Clock.currentTimeMillis) - started } + }) + const failure = (exit: Exit.Exit) => (Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined) + const unavailable = { _tag: "Session.StepFailedError", error: { type: "console.config-unavailable" } } + const instructions = Effect.gen(function* () { + const discovery = yield* InstructionDiscovery.Service + const files = yield* discovery.list() + return Array.isArray(files) ? files.map((file) => file.content) : [] + }) + + it.effect("fails a request on a cold start whose Console keeps answering 503", () => + Effect.gen(function* () { + const api = consoleApi() + api.state.status = 503 + yield* connect(api) + const sent = api.state.requests.length + + const result = yield* settled(yield* request()) + expect(failure(result.exit)).toMatchObject(unavailable) + // A fresh load with its retries, not the failure startup left in the cache. + expect(api.state.requests.length).toBe(sent * 2) + expect(result.elapsed).toBeGreaterThanOrEqual(7_000) + }), + ) + + it.effect("fails a request after 10 seconds when the Console doesn't answer", () => + Effect.gen(function* () { + const api = consoleApi() + api.state.status = 503 + yield* connect(api) + + api.state.hang.add("one") + const result = yield* settled(yield* request()) + expect(failure(result.exit)).toMatchObject(unavailable) + expect(result.elapsed).toBeGreaterThanOrEqual(10_000) + api.release() + }), + ) + + it.effect("doesn't count a malformed config as loaded", () => + Effect.gen(function* () { + const api = consoleApi() + api.state.body = "not a config" + yield* connect(api) + + expect(failure((yield* settled(yield* request())).exit)).toMatchObject(unavailable) + expect(yield* instructions).toEqual([]) + + api.state.body = undefined + expect(Exit.isSuccess((yield* settled(yield* request())).exit)).toBe(true) + expect(yield* instructions).toEqual(["Use one"]) + }), + ) + + it.effect("holds the first request after a credential switch until the new config applies", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const api = consoleApi() + yield* connect(api) + yield* Fiber.join(yield* request()) + expect(yield* instructions).toEqual(["Use one"]) + + api.state.hang.add("two") + yield* credentials.create({ integrationID: Integration.ID.make("opencode"), value: key("two") }) + const held = yield* request() + // Past the 3 seconds a prompt waits on an applied config's refresh. + yield* TestClock.adjust("4 seconds") + yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 5))) + expect(held.pollUnsafe()).toBeUndefined() + + api.release() + yield* Fiber.join(held) + expect(yield* instructions).toEqual(["Use two"]) + }), + ) + + it.effect("holds the first request after a key is replaced in place until its config applies", () => + Effect.gen(function* () { + const credentials = yield* Credential.Service + const api = consoleApi() + const credential = yield* connect(api) + yield* Fiber.join(yield* request()) + + api.state.hang.add("rotated") + yield* credentials.update(credential.id, { value: key("rotated") }) + const held = yield* request() + yield* TestClock.adjust("4 seconds") + yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 5))) + expect(held.pollUnsafe()).toBeUndefined() + + api.release() + yield* Fiber.join(held) + expect(yield* instructions).toEqual(["Use rotated"]) + }), + ) + + it.effect("holds no request without the option", () => + Effect.gen(function* () { + const api = consoleApi() + api.state.status = 503 + yield* connect(api, false) + const sent = api.state.requests.length + + const result = yield* settled(yield* request()) + expect(Exit.isSuccess(result.exit)).toBe(true) + expect(result.elapsed).toBe(0) + expect(api.state.requests.length).toBe(sent) + }), + ) + }) + it.effect("refreshes past future-only policies while retaining config on malformed statements", () => Effect.acquireUseRelease( Effect.sync(() => { @@ -2017,7 +2180,7 @@ describe("OpencodePlugin prompt refresh", () => { const host = yield* PluginHost.make(yield* Plugin.Service, "test") yield* OpencodePlugin.effect(host).pipe( inProcess(server), - Effect.provideService(ConsoleAccess.Options, { poll: false }), + Effect.provideService(ConsoleAccess.Options, { poll: false, required: false }), ) yield* ConfigMcpPlugin.Plugin.effect(host) yield* ConfigPolicyPlugin.Plugin.effect(host) diff --git a/packages/core/test/session-runner.test.ts b/packages/core/test/session-runner.test.ts index 669988427da..46945c1706f 100644 --- a/packages/core/test/session-runner.test.ts +++ b/packages/core/test/session-runner.test.ts @@ -46,6 +46,8 @@ import { SessionModelTransport } from "@opencode/core/session/model-transport" import { Money } from "@opencode/schema/money" import { SessionProjector } from "@opencode/core/session/projector" import { SessionExecution } from "@opencode/core/session/execution" +import { StepFailedError } from "@opencode/core/session/error" +import { SessionGate } from "@opencode/core/session/gate" import { SessionRunCoordinator } from "@opencode/core/session/run-coordinator" import { SessionRunner } from "@opencode/core/session/runner/index" import { SessionRunnerLLM } from "@opencode/core/session/runner/llm" @@ -497,6 +499,7 @@ const layer = Layer.unwrap( LayerNodePlatform.llmClient, SessionRunnerLLM.node, SessionExecution.node, + SessionGate.node, Session.node, ]), [ @@ -1511,6 +1514,53 @@ describe("SessionRunnerLLM", () => { expect(messageRoles(s.requests[0])).toEqual(["user"]) }) + scenario("fails an execution before any model request while a Session gate fails", function* (s) { + const gate = yield* SessionGate.Service + const error = { type: "test.gate", message: "Gate closed" } + yield* gate.register(new StepFailedError({ error })) + yield* s.admit("First") + + const exit = yield* s.resume.pipe(Effect.exit) + + expect(Exit.isFailure(exit) && Cause.squash(exit.cause)).toEqual(new StepFailedError({ error })) + expect(s.requests).toHaveLength(0) + expect(yield* SessionInbox.has(s.db, sessionID, "steer")).toBe(true) + }) + + scenario("selects a resumed execution's tools only after the Session gates pass", function* (s) { + const gate = yield* SessionGate.Service + const registry = yield* Tool.Service + const scope = yield* Scope.Scope + const open = yield* Deferred.make() + // Stands in for config a gate applies, such as the MCP servers an organization's config adds. + const gated = { + name: "gated", + description: "Added by the gate", + input: Schema.Struct({}), + output: Schema.Struct({}), + execute: () => Effect.succeed({ output: {} }), + } + yield* gate.register( + Deferred.await(open).pipe( + Effect.andThen(transformTools(registry, { gated }, { codemode: false })), + Effect.asVoid, + Scope.provide(scope), + ), + ) + yield* s.llm.push(TestLLM.text("Done", "text-done")) + yield* s.admit("First") + + const run = yield* s.resume.pipe(Effect.forkChild({ startImmediately: true })) + yield* Effect.yieldNow + expect(s.requests).toHaveLength(0) + expect(run.pollUnsafe()).toBeUndefined() + + yield* Deferred.succeed(open, undefined) + yield* Fiber.join(run) + expect(s.requests).toHaveLength(1) + expect(s.requests[0]?.tools.map((tool) => tool.name)).toContain("gated") + }) + scenario( "preserves instruction state and interrupts the source Location runner after a Session moves", function* (s) { diff --git a/packages/server/src/options.ts b/packages/server/src/options.ts index 54493a1e9d0..12efe148ff3 100644 --- a/packages/server/src/options.ts +++ b/packages/server/src/options.ts @@ -32,11 +32,15 @@ export const ServerOptions = Schema.Struct({ */ fetch: Schema.optional(Schema.declare((input): input is LayerNodePlatform.Fetch => typeof input === "function")), /** - * How a connected OpenCode Console's config stays fresh. `poll` (default `true`) re-checks it every minute in the + * How a connected OpenCode Console's config is used. `poll` (default `true`) re-checks it every minute in the * background. With `poll: false` it refreshes only when a prompt starts, so a host that suspends between prompts, - * such as a Durable Object, can stay idle. + * such as a Durable Object, can stay idle. `required` (default `false`) holds every model request, including resumed + * ones, until the active credential's config is loaded and applied, and fails the execution with a + * `console.config-unavailable` error when that doesn't happen within about 10 seconds. */ - console: Schema.optional(Schema.Struct({ poll: Schema.optional(Schema.Boolean) })), + console: Schema.optional( + Schema.Struct({ poll: Schema.optional(Schema.Boolean), required: Schema.optional(Schema.Boolean) }), + ), config: Schema.optional( Schema.Struct({ directory: Schema.optional(Schema.String), diff --git a/packages/server/src/workerd.ts b/packages/server/src/workerd.ts index ba052df5ff1..24484c6e447 100644 --- a/packages/server/src/workerd.ts +++ b/packages/server/src/workerd.ts @@ -48,7 +48,10 @@ export interface Options { readonly models?: ServerOptions["models"] /** Replaces `globalThis.fetch` for OpenCode's own HTTP requests, e.g. to route them through a Service Binding. */ readonly fetch?: ServerOptions["fetch"] - /** Console config refresh. `poll` defaults to `false` here, so an idle Durable Object can hibernate. */ + /** + * Console config refresh. `poll` defaults to `false` here, so an idle Durable Object can hibernate. `required` holds + * each model request until the active credential's config is applied. + */ readonly console?: ServerOptions["console"] } diff --git a/packages/server/test/options.test.ts b/packages/server/test/options.test.ts index cde1c3ab8aa..c19fa02e84e 100644 --- a/packages/server/test/options.test.ts +++ b/packages/server/test/options.test.ts @@ -41,7 +41,9 @@ test("accepts an embedder fetch and rejects other values", () => { expect(Option.isNone(decode({ fetch: "https://example.com" }))).toBe(true) }) -test("accepts Console refresh options", () => { +test("accepts Console options", () => { expect(Option.getOrThrow(decode({ console: { poll: false } })).console).toEqual({ poll: false }) + expect(Option.getOrThrow(decode({ console: { required: true } })).console).toEqual({ required: true }) expect(Option.isNone(decode({ console: { poll: "no" } }))).toBe(true) + expect(Option.isNone(decode({ console: { required: "yes" } }))).toBe(true) }) diff --git a/packages/server/test/workerd.test.ts b/packages/server/test/workerd.test.ts index b7cab8be078..417b77024c6 100644 --- a/packages/server/test/workerd.test.ts +++ b/packages/server/test/workerd.test.ts @@ -50,5 +50,9 @@ it.effect("turns off the background Console check unless the host asks for it", const storage = makeDurableObjectStorage() expect(ServerWorkerd.serverOptions({ storage }).console).toEqual({ poll: false }) expect(ServerWorkerd.serverOptions({ storage, console: { poll: true } }).console).toEqual({ poll: true }) + expect(ServerWorkerd.serverOptions({ storage, console: { required: true } }).console).toEqual({ + poll: false, + required: true, + }) }), ) diff --git a/services/www/src/docs/content/build/sdk/cloudflare.mdx b/services/www/src/docs/content/build/sdk/cloudflare.mdx index 3c7ff699fae..164d83ba1ab 100644 --- a/services/www/src/docs/content/build/sdk/cloudflare.mdx +++ b/services/www/src/docs/content/build/sdk/cloudflare.mdx @@ -77,6 +77,12 @@ With an [OpenCode Console](/console) connection, the Workerd profile refreshes t starts, not every minute, so an idle Durable Object can hibernate. Pass `console: { poll: true }` to refresh every minute as other hosts do. +By default, a turn runs on the last config OpenCode has when the Console doesn't answer within 3 seconds. Pass +`console: { required: true }` to make every model request, including turns resumed after an eviction, wait until the +connected credential's config is loaded and applied. If that doesn't happen within 10 seconds, or the Console returns +an error or a config OpenCode can't read, the turn fails with a `console.config-unavailable` error without sending the +request. After switching credentials or replacing a key, the next request waits for the new credential's config. + ## Customize Customize your OpenCode instance by registering plugins bundled with your