mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
feat(core): add a console.required server option that holds model requests until Console config applies
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<ReturnType<typeof ctx.integration.connection.active>>
|
||||
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
|
||||
|
||||
@@ -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<Selection, AgentNotFoundError>
|
||||
/** Waits on the Session gates, then selects the Session, agent, instructions, and tools used by subsequent work. */
|
||||
readonly select: (sessionID: SessionSchema.ID) => Effect.Effect<Selection, AgentNotFoundError | StepFailedError>
|
||||
/** Resolves the model and active history for that selection. */
|
||||
readonly load: (selection: Selection) => Effect.Effect<Loaded, SessionRunnerModel.Error>
|
||||
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,
|
||||
|
||||
@@ -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<void, StepFailedError>
|
||||
|
||||
/**
|
||||
* 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<State.Registration, never, Scope.Scope>
|
||||
/** Runs every registered gate in registration order. */
|
||||
readonly wait: Gate
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Interface>()("@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: [] })
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<string>(), 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<Response>((resolve) => hanging.push(() => resolve(response())))
|
||||
},
|
||||
}
|
||||
}
|
||||
type ConsoleApi = ReturnType<typeof consoleApi>
|
||||
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<void, unknown>) {
|
||||
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<void, unknown>) => (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)
|
||||
|
||||
@@ -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<void>()
|
||||
// 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) {
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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"]
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user