mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 21:15:57 +03:00
feat(core): refresh Console config before prompts and add a console.poll server option (#61583)
This commit is contained in:
+18
-11
@@ -4,7 +4,7 @@ import { makeLocationNode } from "@opencode/util/effect/app-node"
|
||||
import path from "path"
|
||||
import { isDeepStrictEqual } from "node:util"
|
||||
import { applyEdits, modify, type ParseError, parse } from "jsonc-parser"
|
||||
import { Context, Effect, FiberMap, Layer, Option, PubSub, Ref, Schema, Semaphore, Stream } from "effect"
|
||||
import { Context, Effect, FiberMap, Layer, Option, PubSub, Schema, Semaphore, Stream } from "effect"
|
||||
import { Directory, Document, Info, type Patch, type Entry, Event } from "@opencode/schema/config"
|
||||
import { Credential } from "./credential.js"
|
||||
import { Bus } from "./bus.js"
|
||||
@@ -33,6 +33,11 @@ export function latest<K extends keyof Info>(entries: readonly Entry[], key: K):
|
||||
export interface Interface {
|
||||
/** Returns location config documents and discovery sources from lowest to highest priority. */
|
||||
readonly entries: () => Effect.Effect<Entry[]>
|
||||
/**
|
||||
* The entries `entries()` returns, read synchronously. Transforms read this, so a reload that runs right after a
|
||||
* config change, such as an integration replacing its document, sees the change before `config.updated` arrives.
|
||||
*/
|
||||
readonly snapshot: () => Entry[]
|
||||
/** Compatibility roots consumed by internal compatibility plugins. */
|
||||
readonly compatibility?: () => Effect.Effect<{
|
||||
readonly claude: readonly AbsolutePath[]
|
||||
@@ -91,23 +96,24 @@ export const testLayer = (
|
||||
) =>
|
||||
Layer.effectContext(
|
||||
Effect.gen(function* () {
|
||||
const entries = yield* Ref.make(initial)
|
||||
const integrations = yield* Ref.make(new Map<string, ConfigIntegration.Document>())
|
||||
const state = { entries: initial, integrations: new Map<string, ConfigIntegration.Document>() }
|
||||
const updates = yield* PubSub.unbounded<Watcher.Update>()
|
||||
const snapshot = () => [...state.integrations.values(), ...state.entries]
|
||||
const service = Test.of({
|
||||
entries: () =>
|
||||
Effect.all([Ref.get(integrations), Ref.get(entries)]).pipe(
|
||||
Effect.map(([documents, entries]) => [...documents.values(), ...entries]),
|
||||
),
|
||||
entries: () => Effect.sync(snapshot),
|
||||
snapshot,
|
||||
compatibility: () => Effect.succeed(compatibility),
|
||||
changes: () => Stream.fromPubSub(updates),
|
||||
setEntries: (next) => Ref.set(entries, next),
|
||||
setEntries: (next) =>
|
||||
Effect.sync(() => {
|
||||
state.entries = next
|
||||
}),
|
||||
emitChange: (update) => PubSub.publish(updates, update).pipe(Effect.asVoid),
|
||||
integration: (integrationID, document) =>
|
||||
Ref.update(integrations, (current) => {
|
||||
const next = new Map(current)
|
||||
Effect.sync(() => {
|
||||
const next = new Map(state.integrations)
|
||||
next.delete(integrationID)
|
||||
return document ? next.set(integrationID, document) : next
|
||||
state.integrations = document ? next.set(integrationID, document) : next
|
||||
}),
|
||||
})
|
||||
return Context.empty().pipe(Context.add(Service, service), Context.add(Test, service))
|
||||
@@ -489,6 +495,7 @@ export const layer = (options?: Options) =>
|
||||
entries: Effect.fnUntraced(function* () {
|
||||
return configs
|
||||
}),
|
||||
snapshot: () => configs,
|
||||
compatibility: () =>
|
||||
Effect.all({
|
||||
claude: Effect.filter(sources.claude, fs.isDir),
|
||||
|
||||
@@ -7,23 +7,24 @@ import { Effect, PubSub, Stream } from "effect"
|
||||
import path from "path"
|
||||
import { Config } from "../../config.js"
|
||||
|
||||
/**
|
||||
* Runs `reload` on every `config.updated`. The returned `entries` always reads the current config, so a reload that
|
||||
* runs before the event arrives already sees the change.
|
||||
*/
|
||||
export const observe = Effect.fnUntraced(function* (
|
||||
config: Config.Interface,
|
||||
event: EventDomain,
|
||||
reload: Effect.Effect<void>,
|
||||
) {
|
||||
const loaded = { entries: yield* config.entries() }
|
||||
const refresh = config.entries().pipe(
|
||||
Effect.tap((entries) => Effect.sync(() => (loaded.entries = entries))),
|
||||
Effect.andThen(reload),
|
||||
)
|
||||
yield* event.subscribe({ types: ["config.updated"] }).pipe(
|
||||
Stream.runForEach(() => refresh),
|
||||
Stream.runForEach(() => reload),
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
// Close the race between the first read and establishing the subscription.
|
||||
loaded.entries = yield* config.entries()
|
||||
return loaded
|
||||
return {
|
||||
get entries() {
|
||||
return config.snapshot()
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export * as ConfigMcpPlugin from "./mcp.js"
|
||||
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import { Document, type Entry } from "@opencode/schema/config"
|
||||
import { Document } from "@opencode/schema/config"
|
||||
import type { ServerConfig } from "@opencode/schema/mcp"
|
||||
import { Effect, Stream } from "effect"
|
||||
import { Config } from "../../config.js"
|
||||
@@ -19,25 +19,19 @@ export const register = Effect.fn("ConfigMCPPlugin.register")(function* (
|
||||
) {
|
||||
const config = yield* Config.Service
|
||||
const mcp = yield* Mcp.Service
|
||||
const loaded = { entries: [] as Entry[] }
|
||||
|
||||
yield* events.pipe(
|
||||
Stream.filter((event) => event.type === "config.updated"),
|
||||
Stream.runForEach(() =>
|
||||
config.entries().pipe(
|
||||
Effect.tap((entries) => Effect.sync(() => (loaded.entries = entries))),
|
||||
Effect.andThen(mcp.reload()),
|
||||
Effect.catchCause((cause) => Effect.logError("failed to reload MCP config", { cause })),
|
||||
),
|
||||
mcp.reload().pipe(Effect.catchCause((cause) => Effect.logError("failed to reload MCP config", { cause }))),
|
||||
),
|
||||
Effect.ignore,
|
||||
Effect.forkScoped({ startImmediately: true }),
|
||||
)
|
||||
|
||||
// Subscribe before the initial load so updates racing it trigger a rebuild.
|
||||
loaded.entries = yield* config.entries()
|
||||
// Read at reload time, so a reload right after a config change sees it before `config.updated` arrives.
|
||||
yield* mcp.transform((editor) => {
|
||||
const documents = loaded.entries.filter((entry): entry is Document => entry.type === "document")
|
||||
const documents = config.snapshot().filter((entry): entry is Document => entry.type === "document")
|
||||
// Global timeout defaults merge in config order; each server can override them.
|
||||
const timeout = Object.assign(
|
||||
{},
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
export * as ConsoleAccess from "./console-access.js"
|
||||
|
||||
import { Context, Layer } from "effect"
|
||||
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.
|
||||
*/
|
||||
export const Options = Context.Reference<{ readonly poll: boolean }>("@opencode/ConsoleAccess/Options", {
|
||||
defaultValue: () => ({ poll: true }),
|
||||
})
|
||||
|
||||
export const configured = (options?: { readonly poll?: boolean }) =>
|
||||
makeGlobalNode({ service: Options, layer: Layer.succeed(Options, { poll: options?.poll ?? true }), deps: [] })
|
||||
|
||||
export const node = configured()
|
||||
|
||||
export const defaultServer = "https://opencode.ai/console"
|
||||
|
||||
/** The OpenCode Console a credential signed in to. */
|
||||
|
||||
@@ -466,9 +466,10 @@ export const layer = (options?: Options) =>
|
||||
|
||||
const replaceServer = Effect.fnUntraced(function* (name: ServerName, serverConfig: Mcp.ServerConfig) {
|
||||
const previous = entries.get(name)
|
||||
if (previous) yield* disposeServer(previous)
|
||||
const entry = makeEntry(name, serverConfig, yield* Clock.currentTimeMillis)
|
||||
// Readers that arrive while the previous connection closes wait for this one instead of using that one.
|
||||
entries.set(name, entry)
|
||||
if (previous) yield* disposeServer(previous)
|
||||
yield* Effect.gen(function* () {
|
||||
yield* register(name, entry)
|
||||
if (serverConfig.disabled) {
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Model } from "../model.js"
|
||||
import { Provider } from "../provider.js"
|
||||
import { Command } from "../command.js"
|
||||
import { Config } from "../config.js"
|
||||
import { ConsoleAccess } from "../console-access.js"
|
||||
import { Credential } from "../credential.js"
|
||||
import { llmClient } from "../effect/app-node-platform.js"
|
||||
import { ConfigAgentPlugin } from "../config/plugin/agent.js"
|
||||
@@ -119,6 +120,7 @@ import { WellKnownPlugin } from "../wellknown/plugin.js"
|
||||
const services = [
|
||||
Agent.Service,
|
||||
FetchHttpClient.Fetch,
|
||||
ConsoleAccess.Options,
|
||||
Artifact.Service,
|
||||
AppProcess.Service,
|
||||
Provider.Service,
|
||||
@@ -177,6 +179,7 @@ export type Requirements = Context.Service.Identifier<(typeof services)[number]>
|
||||
|
||||
export const requirements = LayerNode.group([
|
||||
Agent.node,
|
||||
ConsoleAccess.node,
|
||||
Artifact.node,
|
||||
AppProcess.node,
|
||||
Provider.node,
|
||||
|
||||
@@ -185,6 +185,7 @@ export const OpencodePlugin = define<
|
||||
const skills = yield* Skill.Service
|
||||
const cache = yield* SharedCache.Service
|
||||
const scope = yield* Scope.Scope
|
||||
const settings = yield* ConsoleAccess.Options
|
||||
const loading = Semaphore.makeUnsafe(1)
|
||||
type ActiveConnection = Effect.Success<ReturnType<typeof ctx.integration.connection.active>>
|
||||
let snapshot: {
|
||||
@@ -192,7 +193,9 @@ export const OpencodePlugin = define<
|
||||
remote: typeof ConsoleConfig.Type | undefined
|
||||
connection: ActiveConnection
|
||||
organization: string | undefined
|
||||
} = { remote: undefined, connection: undefined, organization: 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 }
|
||||
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: [] }
|
||||
@@ -205,11 +208,18 @@ export const OpencodePlugin = define<
|
||||
let explainers: Effect.Success<ReturnType<typeof ctx.session.hook>>[] = []
|
||||
|
||||
// Every project signed in to the same workspace shares one fetch, reused while it is younger than `maxAge`.
|
||||
const load = Effect.fn("OpencodePlugin.load")(function* (maxAge: Duration.Input = Duration.minutes(1)) {
|
||||
// A failed load reports only its connection: what it falls back to depends on the snapshot, which is read under
|
||||
// `loading` when the outcome is applied.
|
||||
const load = Effect.fn("OpencodePlugin.load")(function* (
|
||||
maxAge: Duration.Input = Duration.minutes(1),
|
||||
): Effect.fn.Return<
|
||||
| { readonly _tag: "Loaded"; readonly next: typeof snapshot }
|
||||
| { readonly _tag: "Failed"; readonly connection: ActiveConnection }
|
||||
> {
|
||||
const connection = yield* ctx.integration.connection.active("opencode")
|
||||
if (!connection) {
|
||||
reported = undefined
|
||||
return { remote: undefined, connection, organization: undefined }
|
||||
return { _tag: "Loaded", next: { remote: undefined, connection, organization: undefined, access: undefined } }
|
||||
}
|
||||
const status = (status: IntegrationConnection.Status | undefined) => {
|
||||
reported = status
|
||||
@@ -218,7 +228,7 @@ 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 })
|
||||
return Effect.succeed({ remote: undefined, connection, organization: undefined, access: undefined })
|
||||
return cache
|
||||
.get({
|
||||
key: `opencode-console:${workspaceKey(connection, credential)}`,
|
||||
@@ -240,25 +250,29 @@ export const OpencodePlugin = define<
|
||||
remote,
|
||||
connection,
|
||||
organization: organizationName(credential),
|
||||
access: accessFingerprint(credential),
|
||||
})),
|
||||
)
|
||||
}),
|
||||
Effect.tap(() => status(undefined)),
|
||||
Effect.map((next) => ({ _tag: "Loaded" as const, next })),
|
||||
Effect.catch((cause) =>
|
||||
Effect.gen(function* () {
|
||||
// Only well-formed Console errors change the connection status; anything else may be transient.
|
||||
const next = connectionStatus(cause)
|
||||
if (next) yield* status(next)
|
||||
yield* Effect.logWarning("failed to load OpenCode provider config", { cause })
|
||||
// A load that fails for the connection already in place keeps its last config: dropping it
|
||||
// would lift organization policy while personal credentials keep working.
|
||||
return IntegrationConnection.key(connection) === IntegrationConnection.key(snapshot.connection)
|
||||
? { remote: snapshot.remote, connection, organization: snapshot.organization }
|
||||
: { remote: undefined, connection, organization: undefined }
|
||||
return { _tag: "Failed" as const, connection }
|
||||
}),
|
||||
),
|
||||
)
|
||||
})
|
||||
// A load that fails for the connection already in place keeps its last config: dropping it would lift
|
||||
// organization policy while personal credentials keep working.
|
||||
const fallback = (connection: ActiveConnection): typeof snapshot =>
|
||||
IntegrationConnection.key(connection) === IntegrationConnection.key(snapshot.connection)
|
||||
? { ...snapshot, connection }
|
||||
: { remote: undefined, connection, organization: undefined, access: undefined }
|
||||
// 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
|
||||
@@ -403,7 +417,8 @@ export const OpencodePlugin = define<
|
||||
editor.method.update({ integrationID: "opencode", method: { type: "key", label: "API key (service account)" } })
|
||||
})
|
||||
|
||||
snapshot = yield* load()
|
||||
const initial = yield* load()
|
||||
snapshot = initial._tag === "Loaded" ? initial.next : fallback(initial.connection)
|
||||
yield* publish(snapshot)
|
||||
yield* ctx.provider.transform((providers) => {
|
||||
if (integrated) ConfigProviderPlugin.integrate(providers, integrated)
|
||||
@@ -458,15 +473,27 @@ export const OpencodePlugin = define<
|
||||
{ concurrency: 5, discard: true },
|
||||
)
|
||||
})
|
||||
const refresh = () => loading.withPermit(load().pipe(Effect.andThen(apply)))
|
||||
// Annotated: `apply` re-registers the hooks that call `check`, which would otherwise make inference circular.
|
||||
const check = (maxAge?: Duration.Input): Effect.Effect<void> =>
|
||||
loading.withPermit(
|
||||
Effect.gen(function* () {
|
||||
const next = yield* load(maxAge)
|
||||
if (!Equal.equals(snapshot, next)) yield* apply(next)
|
||||
}),
|
||||
)
|
||||
// Loads run outside `loading`, so a slow one never holds up another check. `wait` bounds the load: one still
|
||||
// running carries on in `SharedCache`, retries included, for the next check, while the current snapshot stays.
|
||||
const check = (maxAge?: Duration.Input, wait?: Duration.Input): Effect.Effect<void> =>
|
||||
Effect.gen(function* () {
|
||||
const outcome =
|
||||
wait === undefined
|
||||
? yield* load(maxAge)
|
||||
: Option.getOrUndefined(yield* load(maxAge).pipe(Effect.timeoutOption(wait)))
|
||||
if (!outcome) return
|
||||
yield* loading.withPermit(
|
||||
Effect.gen(function* () {
|
||||
// A switch while this load ran makes it stale; the switch's own check applies the new connection.
|
||||
const loaded = outcome._tag === "Loaded" ? outcome.next.connection : outcome.connection
|
||||
const active = yield* ctx.integration.connection.active("opencode")
|
||||
if (IntegrationConnection.key(loaded) !== IntegrationConnection.key(active)) return
|
||||
const next = outcome._tag === "Loaded" ? outcome.next : fallback(outcome.connection)
|
||||
if (!Equal.equals(snapshot, next)) yield* apply(next)
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
// Console inference rejects a missing SSO sign-in with a generic 401/403. Re-check the Console config, which
|
||||
// reports the reason, and replace the response with instructions when the connection needs the user.
|
||||
@@ -497,18 +524,24 @@ export const OpencodePlugin = define<
|
||||
)
|
||||
})
|
||||
yield* guard(snapshot)
|
||||
// A prompt after the machine slept or the host suspended refreshes stale Console config, and the MCP gateway
|
||||
// tokens it carries, before the turn's tool calls run. An unreachable Console delays the prompt by at most
|
||||
// 3 seconds; the turn then runs on the config it has.
|
||||
yield* ctx.session.hook("prompt", () => check(Duration.minutes(1), Duration.seconds(3)))
|
||||
|
||||
// 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.
|
||||
// Only while connected: a signed-out location runs no timer and no credential lookups. Hosts that suspend
|
||||
// between prompts turn the timer off with the `console.poll` server option and rely on the prompt hook.
|
||||
const polling = yield* FiberHandle.make<void, never>()
|
||||
const poll = Effect.fnUntraced(function* () {
|
||||
if (!snapshot.connection) return yield* FiberHandle.clear(polling)
|
||||
if (!snapshot.connection || !settings.poll) return yield* FiberHandle.clear(polling)
|
||||
yield* FiberHandle.run(polling, Effect.sleep(Duration.minutes(1)).pipe(Effect.andThen(check()), Effect.forever), {
|
||||
onlyIfMissing: true,
|
||||
})
|
||||
})
|
||||
yield* ProviderRefresh.onSwitch("opencode", refresh().pipe(Effect.andThen(poll())))
|
||||
// A switch applies only a changed snapshot, so one a prompt already applied doesn't reload MCP servers mid-turn.
|
||||
yield* ProviderRefresh.onSwitch("opencode", check().pipe(Effect.andThen(poll())))
|
||||
yield* poll()
|
||||
}),
|
||||
})
|
||||
@@ -543,6 +576,8 @@ function fetchConfig(http: HttpClient.HttpClient, value: Credential.Key | Creden
|
||||
}),
|
||||
),
|
||||
Effect.scoped,
|
||||
// A transient failure, which `SharedCache` retries.
|
||||
Effect.timeout(Duration.seconds(5)),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -580,9 +615,14 @@ function workspaceKey(
|
||||
ConsoleAccess.server(credential),
|
||||
IntegrationConnection.key(connection),
|
||||
credential.metadata?.orgID,
|
||||
accessFingerprint(credential),
|
||||
])
|
||||
}
|
||||
|
||||
function accessFingerprint(credential: Credential.Key | Credential.OAuth) {
|
||||
return credential.type === "key" ? SharedCache.fingerprint(credential.key) : undefined
|
||||
}
|
||||
|
||||
function organizationName(credential: Credential.Value) {
|
||||
return typeof credential.metadata?.orgName === "string" ? credential.metadata.orgName : undefined
|
||||
}
|
||||
|
||||
@@ -2,42 +2,38 @@ import { describe, expect } from "bun:test"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { Document, Info, type Entry } from "@opencode/schema/config"
|
||||
import { Event } from "@opencode/schema/event"
|
||||
import { Deferred, Effect, PubSub, Ref, Stream } from "effect"
|
||||
import { Deferred, Effect, PubSub, Stream } from "effect"
|
||||
import { ConfigEntryObserver } from "../../src/config/plugin/entry-observer"
|
||||
import { it } from "../lib/effect"
|
||||
import { host } from "../plugin/host"
|
||||
|
||||
describe("ConfigEntryObserver", () => {
|
||||
it.effect("closes the startup race and reloads later updates", () =>
|
||||
it.effect("reads the current config and reloads on updates", () =>
|
||||
Effect.gen(function* () {
|
||||
const current = yield* Ref.make([document("first")])
|
||||
const state = { current: [document("first")] }
|
||||
const updates = yield* PubSub.unbounded<ReturnType<typeof updated>>()
|
||||
const reloaded = yield* Deferred.make<void>()
|
||||
const config = Config.Service.of({
|
||||
entries: () => Ref.get(current),
|
||||
entries: () => Effect.sync(() => state.current),
|
||||
snapshot: () => state.current,
|
||||
changes: () => Stream.empty,
|
||||
})
|
||||
const event = host({
|
||||
event: {
|
||||
subscribe: () =>
|
||||
Stream.unwrap(Ref.set(current, [document("raced")]).pipe(Effect.as(Stream.fromPubSub(updates)))),
|
||||
},
|
||||
}).event
|
||||
const event = host({ event: { subscribe: () => Stream.fromPubSub(updates) } }).event
|
||||
|
||||
const loaded = yield* ConfigEntryObserver.observe(
|
||||
config,
|
||||
event,
|
||||
Deferred.succeed(reloaded, undefined).pipe(Effect.asVoid),
|
||||
)
|
||||
expect(Config.latest(loaded.entries, "shell")).toBe("first")
|
||||
|
||||
expect(Config.latest(loaded.entries, "shell")).toBe("raced")
|
||||
// Visible before `config.updated` arrives, so a reload triggered another way already sees it.
|
||||
state.current = [document("later")]
|
||||
expect(Config.latest(loaded.entries, "shell")).toBe("later")
|
||||
expect(yield* Deferred.isDone(reloaded)).toBe(false)
|
||||
|
||||
yield* Ref.set(current, [document("later")])
|
||||
yield* PubSub.publish(updates, updated())
|
||||
yield* Deferred.await(reloaded)
|
||||
|
||||
expect(Config.latest(loaded.entries, "shell")).toBe("later")
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -49,19 +49,25 @@ describe("ConfigImagePlugin.Plugin", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.live("refetches config after subscribing to updates", () =>
|
||||
it.live("reads the config current when the image service reloads", () =>
|
||||
Effect.gen(function* () {
|
||||
const image = yield* Image.Service
|
||||
const plugins = yield* Plugin.Service
|
||||
let reads = 0
|
||||
const current = { width: 1_200 }
|
||||
const read = () => [document({ max_width: current.width, max_base64_bytes: 1 })]
|
||||
const config = Config.Service.of({
|
||||
entries: () => Effect.sync(() => [document({ max_width: reads++ === 0 ? 1_200 : 700, max_base64_bytes: 1 })]),
|
||||
entries: () => Effect.sync(read),
|
||||
snapshot: read,
|
||||
changes: () => Stream.empty,
|
||||
})
|
||||
yield* ConfigImagePlugin.Plugin.effect(yield* PluginHost.make(plugins, "test")).pipe(
|
||||
Effect.provideService(Config.Service, config),
|
||||
)
|
||||
expect(yield* limits(image)).toEqual({ maxWidth: 1_200, maxHeight: 2_000, maxBytes: 1 })
|
||||
|
||||
// No `config.updated` yet: a reload for another reason already sees the change.
|
||||
current.width = 700
|
||||
yield* image.reload()
|
||||
expect(yield* limits(image)).toEqual({ maxWidth: 700, maxHeight: 2_000, maxBytes: 1 })
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -395,6 +395,7 @@ describe("LocationWatcher subscriptions", () => {
|
||||
Config.Service,
|
||||
Config.Service.of({
|
||||
entries: () => Effect.sync(() => entries.current),
|
||||
snapshot: () => entries.current,
|
||||
changes: () => Stream.never,
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -339,6 +339,7 @@ function resourceMcpLayer(
|
||||
) {
|
||||
const directory = AbsolutePath.make(import.meta.dir)
|
||||
const unusedIntegration = () => Effect.die("unused integration service")
|
||||
const entries = overrides?.entries
|
||||
return Layer.effectDiscard(
|
||||
Effect.gen(function* () {
|
||||
const bus = yield* Bus.Service
|
||||
@@ -355,13 +356,10 @@ function resourceMcpLayer(
|
||||
Layer.provideMerge(Form.layer),
|
||||
Layer.provide(
|
||||
Layer.mergeAll(
|
||||
overrides?.entries
|
||||
entries
|
||||
? Layer.succeed(
|
||||
Config.Service,
|
||||
Config.Service.of({
|
||||
entries: overrides.entries,
|
||||
changes: () => Stream.never,
|
||||
}),
|
||||
Config.Service.of({ entries, snapshot: () => Effect.runSync(entries()), changes: () => Stream.never }),
|
||||
)
|
||||
: Config.testLayer([
|
||||
new Document({
|
||||
@@ -1496,9 +1494,7 @@ test("acknowledges completed MCP URL elicitations without returning internal con
|
||||
const result = yield* Fiber.join(call)
|
||||
expect(yield* forms.state(form.id)).toEqual({ status: "answered", answer: { elicitation: true } })
|
||||
return result
|
||||
}).pipe(
|
||||
Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))),
|
||||
)
|
||||
}).pipe(Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))))
|
||||
|
||||
expect(result.structured).toEqual({ action: "accept" })
|
||||
}),
|
||||
@@ -1519,10 +1515,12 @@ test("cancels MCP elicitation forms when the server gives up on them", async ()
|
||||
const { form } = yield* Deferred.await(created)
|
||||
yield* Fiber.await(call)
|
||||
// The server's cancellation and its tool error race; the form settles shortly after either.
|
||||
const state = yield* forms.state(form.id).pipe(
|
||||
Effect.repeat({ until: (state) => state.status !== "pending", schedule: Schedule.spaced("10 millis") }),
|
||||
Effect.timeout("2 seconds"),
|
||||
)
|
||||
const state = yield* forms
|
||||
.state(form.id)
|
||||
.pipe(
|
||||
Effect.repeat({ until: (state) => state.status !== "pending", schedule: Schedule.spaced("10 millis") }),
|
||||
Effect.timeout("2 seconds"),
|
||||
)
|
||||
expect(state.status).toBe("cancelled")
|
||||
}).pipe(Effect.provide(resourceMcpLayer(server.url, (form) => recordForm(created, form))))
|
||||
}),
|
||||
@@ -3053,84 +3051,80 @@ for (const modern of [false, true]) {
|
||||
}
|
||||
|
||||
for (const source of ["config", "organization"] as const) {
|
||||
testEffect(Config.testLayer()).live(
|
||||
`revokes and restores a connected MCP through ${source} policy`,
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const server = yield* resourceServer()
|
||||
const config = yield* Config.Service
|
||||
const test = yield* Config.Test
|
||||
const updates = yield* PubSub.unbounded<{ readonly type: string }>()
|
||||
const document = (effect: ConfigPolicy.Effect) =>
|
||||
new Document({
|
||||
type: "document",
|
||||
info: new Info({
|
||||
mcp: new ConfigMCP.Info({
|
||||
servers: { resources: new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }) },
|
||||
}),
|
||||
policies: [{ action: "mcp.use", resource: "*", effect }],
|
||||
}),
|
||||
})
|
||||
yield* test.setEntries([document("allow")])
|
||||
yield* Effect.gen(function* () {
|
||||
const service = yield* Mcp.Service
|
||||
expect(yield* settled(service)).toEqual({ status: "connected" })
|
||||
expect((yield* service.tools()).length).toBeGreaterThan(0)
|
||||
if (source === "config") yield* test.setEntries([document("deny")])
|
||||
if (source === "organization")
|
||||
yield* setOrganizationPolicy([{ action: "mcp.use", resource: "*", effect: "deny" }])
|
||||
yield* PubSub.publish(updates, { type: Event.Updated.type })
|
||||
yield* service.servers().pipe(
|
||||
Effect.filterOrFail(
|
||||
(servers) => servers.length === 0,
|
||||
() => new Error("MCP policy was not applied"),
|
||||
),
|
||||
Effect.retry({ times: 200, schedule: Schedule.spaced("10 millis") }),
|
||||
)
|
||||
expect(yield* service.servers()).toEqual([])
|
||||
expect(yield* service.tools()).toEqual([])
|
||||
expect(yield* service.instructions()).toEqual([])
|
||||
expect(yield* service.prompts()).toEqual([])
|
||||
expect(yield* service.resourceCatalog()).toEqual({ resources: [], templates: [] })
|
||||
expect(Exit.isFailure(yield* service.resources({ server: "resources" }).pipe(Effect.exit))).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.prompt({ server: "resources", name: "greet" }).pipe(Effect.exit))).toBe(
|
||||
true,
|
||||
)
|
||||
expect(
|
||||
Exit.isFailure(
|
||||
yield* service.readResource({ server: "resources", uri: "docs://readme" }).pipe(Effect.exit),
|
||||
),
|
||||
).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.connect("resources").pipe(Effect.exit))).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.callTool({ server: "resources", name: "echo" }).pipe(Effect.exit))).toBe(
|
||||
true,
|
||||
)
|
||||
expect(server.state.toolCalls).toEqual([])
|
||||
expect(server.state.resourceReads).toEqual([])
|
||||
yield* Effect.promise(server.restart)
|
||||
if (source === "config") yield* test.setEntries([document("allow")])
|
||||
if (source === "organization") yield* setOrganizationPolicy(undefined)
|
||||
yield* PubSub.publish(updates, { type: Event.Updated.type })
|
||||
yield* service.servers().pipe(
|
||||
Effect.filterOrFail(
|
||||
(servers) => servers.some((server) => server.status.status === "connected"),
|
||||
() => new Error("MCP policy was not restored"),
|
||||
),
|
||||
Effect.retry({ times: 200, schedule: Schedule.spaced("10 millis") }),
|
||||
)
|
||||
expect(yield* settled(service)).toEqual({ status: "connected" })
|
||||
expect((yield* service.tools()).length).toBeGreaterThan(0)
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
resourceMcpLayer(server.url, undefined, undefined, {
|
||||
entries: config.entries,
|
||||
subscribe: ((definition?: { readonly type: string }) =>
|
||||
Stream.fromPubSub(updates).pipe(
|
||||
Stream.filter((event) => !definition || event.type === definition.type),
|
||||
)) as Bus.Interface["subscribe"],
|
||||
testEffect(Config.testLayer()).live(`revokes and restores a connected MCP through ${source} policy`, () =>
|
||||
Effect.gen(function* () {
|
||||
const server = yield* resourceServer()
|
||||
const config = yield* Config.Service
|
||||
const test = yield* Config.Test
|
||||
const updates = yield* PubSub.unbounded<{ readonly type: string }>()
|
||||
const document = (effect: ConfigPolicy.Effect) =>
|
||||
new Document({
|
||||
type: "document",
|
||||
info: new Info({
|
||||
mcp: new ConfigMCP.Info({
|
||||
servers: { resources: new ConfigMCP.Remote({ type: "remote", url: server.url, oauth: false }) },
|
||||
}),
|
||||
policies: [{ action: "mcp.use", resource: "*", effect }],
|
||||
}),
|
||||
})
|
||||
yield* test.setEntries([document("allow")])
|
||||
yield* Effect.gen(function* () {
|
||||
const service = yield* Mcp.Service
|
||||
expect(yield* settled(service)).toEqual({ status: "connected" })
|
||||
expect((yield* service.tools()).length).toBeGreaterThan(0)
|
||||
if (source === "config") yield* test.setEntries([document("deny")])
|
||||
if (source === "organization")
|
||||
yield* setOrganizationPolicy([{ action: "mcp.use", resource: "*", effect: "deny" }])
|
||||
yield* PubSub.publish(updates, { type: Event.Updated.type })
|
||||
yield* service.servers().pipe(
|
||||
Effect.filterOrFail(
|
||||
(servers) => servers.length === 0,
|
||||
() => new Error("MCP policy was not applied"),
|
||||
),
|
||||
Effect.retry({ times: 200, schedule: Schedule.spaced("10 millis") }),
|
||||
)
|
||||
}),
|
||||
expect(yield* service.servers()).toEqual([])
|
||||
expect(yield* service.tools()).toEqual([])
|
||||
expect(yield* service.instructions()).toEqual([])
|
||||
expect(yield* service.prompts()).toEqual([])
|
||||
expect(yield* service.resourceCatalog()).toEqual({ resources: [], templates: [] })
|
||||
expect(Exit.isFailure(yield* service.resources({ server: "resources" }).pipe(Effect.exit))).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.prompt({ server: "resources", name: "greet" }).pipe(Effect.exit))).toBe(
|
||||
true,
|
||||
)
|
||||
expect(
|
||||
Exit.isFailure(yield* service.readResource({ server: "resources", uri: "docs://readme" }).pipe(Effect.exit)),
|
||||
).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.connect("resources").pipe(Effect.exit))).toBe(true)
|
||||
expect(Exit.isFailure(yield* service.callTool({ server: "resources", name: "echo" }).pipe(Effect.exit))).toBe(
|
||||
true,
|
||||
)
|
||||
expect(server.state.toolCalls).toEqual([])
|
||||
expect(server.state.resourceReads).toEqual([])
|
||||
yield* Effect.promise(server.restart)
|
||||
if (source === "config") yield* test.setEntries([document("allow")])
|
||||
if (source === "organization") yield* setOrganizationPolicy(undefined)
|
||||
yield* PubSub.publish(updates, { type: Event.Updated.type })
|
||||
yield* service.servers().pipe(
|
||||
Effect.filterOrFail(
|
||||
(servers) => servers.some((server) => server.status.status === "connected"),
|
||||
() => new Error("MCP policy was not restored"),
|
||||
),
|
||||
Effect.retry({ times: 200, schedule: Schedule.spaced("10 millis") }),
|
||||
)
|
||||
expect(yield* settled(service)).toEqual({ status: "connected" })
|
||||
expect((yield* service.tools()).length).toBeGreaterThan(0)
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
resourceMcpLayer(server.url, undefined, undefined, {
|
||||
entries: config.entries,
|
||||
subscribe: ((definition?: { readonly type: string }) =>
|
||||
Stream.fromPubSub(updates).pipe(
|
||||
Stream.filter((event) => !definition || event.type === definition.type),
|
||||
)) as Bus.Interface["subscribe"],
|
||||
}),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ const permissionLayer = Layer.succeed(
|
||||
}),
|
||||
)
|
||||
|
||||
export const makePluginTestLayer = (permission = permissionLayer, generate = generateLayer) =>
|
||||
export const makePluginTestLayer = (permission = permissionLayer, generate = generateLayer, mcp = emptyMcpLayer) =>
|
||||
AppNodeBuilder.build(
|
||||
LayerNode.group([
|
||||
AppProcess.node,
|
||||
@@ -138,7 +138,7 @@ export const makePluginTestLayer = (permission = permissionLayer, generate = gen
|
||||
Location.node.replace(tempLocationLayer),
|
||||
Npm.node.replace(npmLayer),
|
||||
Config.node.replace(configLayer),
|
||||
Mcp.node.replace(emptyMcpLayer),
|
||||
Mcp.node.replace(mcp),
|
||||
Generate.node.replace(generate),
|
||||
Permission.node.replace(permission),
|
||||
offlineModels,
|
||||
|
||||
@@ -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, Layer, Schema, Stream } from "effect"
|
||||
import { Effect, Fiber, Layer, Schema, Stream } from "effect"
|
||||
import { TestClock } from "effect/testing"
|
||||
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
@@ -12,11 +12,14 @@ import { App } from "@opencode/core/app"
|
||||
import { Bus } from "@opencode/core/bus"
|
||||
import { Config } from "@opencode/core/config"
|
||||
import { ConfigIntegration } from "@opencode/core/config/integration"
|
||||
import { ConfigMcpPlugin } from "@opencode/core/config/plugin/mcp"
|
||||
import { ConfigPolicyPlugin } from "@opencode/core/config/plugin/policy"
|
||||
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 { Integration } from "@opencode/core/integration"
|
||||
import { Mcp } from "@opencode/core/mcp/index"
|
||||
import { Model } from "@opencode/core/model"
|
||||
import { ModelResolver } from "@opencode/core/model-resolver"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
@@ -26,12 +29,14 @@ 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 { SessionMessage } from "@opencode/core/session/message"
|
||||
import { WebSearch } from "@opencode/core/websearch"
|
||||
import { withEnv } from "../fixture/env"
|
||||
import { drain } from "../lib/clock"
|
||||
import { eventually } from "../lib/eventually"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { PluginTestLayer } from "./fixture"
|
||||
import { emptyMcp } from "../fixture/mcp"
|
||||
import { makePluginTestLayer, PluginTestLayer } from "./fixture"
|
||||
|
||||
const it = testEffect(PluginTestLayer)
|
||||
|
||||
@@ -72,6 +77,51 @@ const inProcess = (server: { fetch: (request: Request) => Response | Promise<Res
|
||||
),
|
||||
)
|
||||
|
||||
// Without `drain`: what the hook applied must be visible when it returns.
|
||||
const prompt = Effect.fn(function* () {
|
||||
const hooks = yield* PluginHooks.Service
|
||||
yield* hooks.trigger("session", "prompt", {
|
||||
sessionID: Session.ID.make("ses_console_prompt"),
|
||||
messageID: SessionMessage.ID.make("msg_console_prompt"),
|
||||
prompt: { text: "hello" },
|
||||
delivery: "steer",
|
||||
})
|
||||
})
|
||||
|
||||
// Records the MCP servers config transforms produce, rebuilt on each reload and registration as the MCP service does.
|
||||
type McpServerConfig = NonNullable<ReturnType<Mcp.Editor["get"]>>
|
||||
const recordingMcp = (() => {
|
||||
const transforms = new Set<(editor: Mcp.Editor) => void>()
|
||||
const configured = new Map<string, McpServerConfig>()
|
||||
const reload = () =>
|
||||
Effect.sync(() => {
|
||||
const servers = new Map<string, McpServerConfig>()
|
||||
const editor: Mcp.Editor = {
|
||||
list: () => Array.from(servers, ([name, config]) => [Mcp.ServerName.make(name), config] as const),
|
||||
get: (name) => servers.get(name),
|
||||
set: (name, config) => servers.set(name, structuredClone(config) as McpServerConfig),
|
||||
update: (name, update) => {
|
||||
const config = servers.get(name)
|
||||
if (config) update(config)
|
||||
},
|
||||
remove: (name) => servers.delete(name),
|
||||
}
|
||||
transforms.forEach((transform) => transform(editor))
|
||||
configured.clear()
|
||||
servers.forEach((config, name) => configured.set(name, config))
|
||||
})
|
||||
const service = Mcp.Service.of({
|
||||
...emptyMcp,
|
||||
transform: (callback) =>
|
||||
Effect.sync(() => {
|
||||
transforms.add(callback)
|
||||
return { dispose: Effect.sync(() => transforms.delete(callback)).pipe(Effect.andThen(reload)) }
|
||||
}).pipe(Effect.tap(reload)),
|
||||
reload,
|
||||
})
|
||||
return { configured, layer: Layer.succeed(Mcp.Service, service) }
|
||||
})()
|
||||
|
||||
function consoleServer(orgID: string | null | undefined, unavailable = false) {
|
||||
const config: { authorization: string | null; orgID: string | null }[] = []
|
||||
const requests: string[] = []
|
||||
@@ -761,6 +811,187 @@ describe("OpencodePlugin", () => {
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("applies a credential switch only when it changes the Console config", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => Bun.serve({ port: 0, fetch: () => Response.json({ providers: {} }) })),
|
||||
(server) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const providers = yield* Provider.Service
|
||||
const bus = yield* Bus.Service
|
||||
const rebuilds = { count: 0 }
|
||||
const key = (key: string) =>
|
||||
Credential.Key.make({ type: "key", key, metadata: { server: server.url.origin } })
|
||||
yield* credentials.create({ integrationID: Integration.ID.make("opencode"), value: key("first") })
|
||||
yield* providers.transform(() => {
|
||||
rebuilds.count++
|
||||
})
|
||||
yield* addPlugin().pipe(inProcess(server))
|
||||
yield* drain
|
||||
const initial = rebuilds.count
|
||||
|
||||
const second = yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: key("second"),
|
||||
})
|
||||
yield* drain
|
||||
expect(rebuilds.count).toBe(initial + 1)
|
||||
|
||||
// A switch queued behind a prompt that already applied this credential's config.
|
||||
yield* bus.publish(Credential.Event.Switched, {
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
credentialID: second.id,
|
||||
})
|
||||
yield* drain
|
||||
expect(rebuilds.count).toBe(initial + 1)
|
||||
}),
|
||||
(server) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
describe("refreshing Console config", () => {
|
||||
const serve = () =>
|
||||
Effect.sync(() => {
|
||||
const state = { advertised: false, requests: [] as (string | null)[], hang: false }
|
||||
const hanging: (() => void)[] = []
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: (request) => {
|
||||
state.requests.push(request.headers.get("authorization"))
|
||||
const response = Response.json({
|
||||
providers: {},
|
||||
...(state.advertised ? { websearch: { provider: "opencode" } } : {}),
|
||||
})
|
||||
if (!state.hang) return response
|
||||
return new Promise<Response>((resolve) => hanging.push(() => resolve(response)))
|
||||
},
|
||||
})
|
||||
return { server, state, release: () => hanging.splice(0).forEach((resolve) => resolve()) }
|
||||
})
|
||||
const key = (server: Bun.Server<undefined>, key = "secret") =>
|
||||
Credential.Key.make({ type: "key", key, metadata: { server: server.url.origin } })
|
||||
// A real macrotask, so a request can reach the server without advancing the TestClock as `drain` does.
|
||||
const settle = Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 5)))
|
||||
const connect = Effect.fn(function* (server: Bun.Server<undefined>, poll = false) {
|
||||
const credentials = yield* Credential.Service
|
||||
const credential = yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: key(server),
|
||||
})
|
||||
yield* addPlugin().pipe(inProcess(server), Effect.provideService(ConsoleAccess.Options, { poll }))
|
||||
yield* drain
|
||||
return credential
|
||||
})
|
||||
|
||||
it.effect("schedules no background Console check", () =>
|
||||
Effect.acquireUseRelease(
|
||||
serve(),
|
||||
({ server, state }) =>
|
||||
Effect.gen(function* () {
|
||||
yield* connect(server)
|
||||
expect(state.requests).toHaveLength(1)
|
||||
yield* TestClock.adjust("5 minutes")
|
||||
yield* drain
|
||||
expect(state.requests).toHaveLength(1)
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("refreshes Console config older than a minute when a prompt starts", () =>
|
||||
Effect.acquireUseRelease(
|
||||
serve(),
|
||||
({ server, state }) =>
|
||||
Effect.gen(function* () {
|
||||
const websearch = yield* WebSearch.Service
|
||||
yield* connect(server)
|
||||
state.advertised = true
|
||||
|
||||
yield* TestClock.adjust("50 seconds")
|
||||
yield* prompt()
|
||||
expect(state.requests).toHaveLength(1)
|
||||
expect(yield* websearch.providers()).toEqual([])
|
||||
|
||||
yield* TestClock.adjust("20 seconds")
|
||||
yield* prompt()
|
||||
expect(state.requests).toHaveLength(2)
|
||||
expect(yield* websearch.providers()).toContainEqual(consoleSearch)
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("admits a prompt after 3 seconds on the config it has when the Console doesn't answer", () =>
|
||||
Effect.acquireUseRelease(
|
||||
serve(),
|
||||
({ server, state, release }) =>
|
||||
Effect.gen(function* () {
|
||||
const websearch = yield* WebSearch.Service
|
||||
state.advertised = true
|
||||
yield* connect(server)
|
||||
expect(yield* websearch.providers()).toContainEqual(consoleSearch)
|
||||
|
||||
state.hang = true
|
||||
state.advertised = false
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
const admitted = yield* prompt().pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* TestClock.adjust("3 seconds")
|
||||
yield* Fiber.join(admitted)
|
||||
expect(state.requests).toHaveLength(2)
|
||||
expect(yield* websearch.providers()).toContainEqual(consoleSearch)
|
||||
release()
|
||||
}),
|
||||
({ server, release }) => Effect.sync(release).pipe(Effect.andThen(Effect.promise(() => server.stop(true)))),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("admits a prompt within 3 seconds while a background check waits on the Console", () =>
|
||||
Effect.acquireUseRelease(
|
||||
serve(),
|
||||
({ server, state, release }) =>
|
||||
Effect.gen(function* () {
|
||||
const websearch = yield* WebSearch.Service
|
||||
state.advertised = true
|
||||
yield* connect(server, true)
|
||||
|
||||
state.hang = true
|
||||
state.advertised = false
|
||||
yield* TestClock.adjust("1 minute")
|
||||
yield* settle
|
||||
expect(state.requests).toHaveLength(2)
|
||||
|
||||
const admitted = yield* prompt().pipe(Effect.forkChild({ startImmediately: true }))
|
||||
yield* settle
|
||||
yield* TestClock.adjust("3 seconds")
|
||||
yield* Fiber.join(admitted)
|
||||
// The prompt joined the background check's request rather than sending its own.
|
||||
expect(state.requests).toHaveLength(2)
|
||||
expect(yield* websearch.providers()).toContainEqual(consoleSearch)
|
||||
release()
|
||||
}),
|
||||
({ server, release }) => Effect.sync(release).pipe(Effect.andThen(Effect.promise(() => server.stop(true)))),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("fetches again under a key replaced in place, even while the last config is fresh", () =>
|
||||
Effect.acquireUseRelease(
|
||||
serve(),
|
||||
({ server, state }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const credential = yield* connect(server)
|
||||
expect(state.requests).toEqual(["Bearer secret"])
|
||||
|
||||
yield* credentials.update(credential.id, { value: key(server, "rotated") })
|
||||
yield* TestClock.adjust("10 seconds")
|
||||
yield* prompt()
|
||||
expect(state.requests).toEqual(["Bearer secret", "Bearer rotated"])
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
it.effect("refreshes past future-only policies while retaining config on malformed statements", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
@@ -1745,6 +1976,67 @@ describe("OpencodePlugin", () => {
|
||||
)
|
||||
})
|
||||
|
||||
describe("OpencodePlugin prompt refresh", () => {
|
||||
const it = testEffect(makePluginTestLayer(undefined, undefined, recordingMcp.layer))
|
||||
|
||||
it.effect("applies a rotated MCP header and a new policy before the prompt hook returns", () =>
|
||||
Effect.acquireUseRelease(
|
||||
Effect.sync(() => {
|
||||
const state = { header: "Bearer one", deny: false }
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
fetch: () =>
|
||||
Response.json({
|
||||
providers: { remote: { name: "Remote" } },
|
||||
mcp: {
|
||||
servers: {
|
||||
gateway: {
|
||||
type: "remote",
|
||||
url: "https://gateway.example/mcp",
|
||||
headers: { authorization: state.header },
|
||||
},
|
||||
},
|
||||
},
|
||||
policies: state.deny ? [{ action: "provider.use", resource: "remote", effect: "deny" }] : [],
|
||||
}),
|
||||
})
|
||||
return { server, state }
|
||||
}),
|
||||
({ server, state }) =>
|
||||
Effect.gen(function* () {
|
||||
const credentials = yield* Credential.Service
|
||||
const providers = yield* Provider.Service
|
||||
const header = () => {
|
||||
const gateway = recordingMcp.configured.get("gateway")
|
||||
return gateway?.type === "remote" ? gateway.headers?.authorization : undefined
|
||||
}
|
||||
yield* credentials.create({
|
||||
integrationID: Integration.ID.make("opencode"),
|
||||
value: Credential.Key.make({ type: "key", key: "secret", metadata: { server: server.url.origin } }),
|
||||
})
|
||||
const host = yield* PluginHost.make(yield* Plugin.Service, "test")
|
||||
yield* OpencodePlugin.effect(host).pipe(
|
||||
inProcess(server),
|
||||
Effect.provideService(ConsoleAccess.Options, { poll: false }),
|
||||
)
|
||||
yield* ConfigMcpPlugin.Plugin.effect(host)
|
||||
yield* ConfigPolicyPlugin.Plugin.effect(host)
|
||||
yield* drain
|
||||
expect(header()).toBe("Bearer one")
|
||||
expect(yield* providers.get(Provider.ID.make("remote"))).toBeDefined()
|
||||
|
||||
state.header = "Bearer two"
|
||||
state.deny = true
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
yield* prompt()
|
||||
expect(header()).toBe("Bearer two")
|
||||
expect(yield* providers.get(Provider.ID.make("remote"))).toBeUndefined()
|
||||
}),
|
||||
({ server }) => Effect.promise(() => server.stop(true)),
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
describe("OpencodePlugin with a Console config", () => {
|
||||
const $schema = "https://opencode.ai/config.json"
|
||||
const deny: ConfigPolicy.Info = { action: "tool.use", resource: "shell:*", effect: "deny" }
|
||||
|
||||
@@ -14,6 +14,7 @@ const config = (plugins: Info["plugins"] = []) =>
|
||||
Config.Service,
|
||||
Config.Service.of({
|
||||
entries: () => Effect.succeed([new Document({ type: "document", info: new Info({ plugins }) })]),
|
||||
snapshot: () => [new Document({ type: "document", info: new Info({ plugins }) })],
|
||||
changes: () => Stream.never,
|
||||
}),
|
||||
)
|
||||
@@ -72,12 +73,7 @@ describe("SkillPlugin.Plugin", () => {
|
||||
expect(report?.content).toContain("- Active plugins: -disabled, local.ts, package-plugin, package-plugin")
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
config([
|
||||
"package-plugin",
|
||||
"-disabled",
|
||||
"local.ts",
|
||||
{ package: "package-plugin", options: { enabled: true } },
|
||||
]),
|
||||
config(["package-plugin", "-disabled", "local.ts", { package: "package-plugin", options: { enabled: true } }]),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -92,14 +92,15 @@ const enabled = Config.testLayer(enabledEntries)
|
||||
const disabled = Config.testLayer(disabledEntries)
|
||||
/** A location whose flag the test turns on, counting every config read. */
|
||||
const toggle = { on: false, reads: 0 }
|
||||
const readToggle = () => {
|
||||
toggle.reads++
|
||||
return toggle.on ? enabledEntries : disabledEntries
|
||||
}
|
||||
const toggled = Layer.succeed(
|
||||
Config.Service,
|
||||
Config.Service.of({
|
||||
entries: () =>
|
||||
Effect.sync(() => {
|
||||
toggle.reads++
|
||||
return toggle.on ? enabledEntries : disabledEntries
|
||||
}),
|
||||
entries: () => Effect.sync(() => readToggle()),
|
||||
snapshot: () => readToggle(),
|
||||
changes: () => Stream.empty,
|
||||
}),
|
||||
)
|
||||
@@ -1244,51 +1245,53 @@ describe("Schedule", () => {
|
||||
}
|
||||
`
|
||||
|
||||
it.effect("seeds initial state at t=0 so replies before the first tick are caught, and counts ends.times by wakes", () =>
|
||||
Effect.gen(function* () {
|
||||
const schedules = yield* setup()
|
||||
slack.replies = 3
|
||||
const schedule = yield* schedules.create({
|
||||
sessionID,
|
||||
name: "thread",
|
||||
when: { type: "every", every: "2 minutes" },
|
||||
if: { type: "code", code: watchThread },
|
||||
ends: { times: 1 },
|
||||
action: { type: "prompt", prompt: "New thread activity." },
|
||||
})
|
||||
// Seeded at creation (t=0): 1 read, baseline state = { replies: 3, runs: 0 }.
|
||||
expect(slack.reads).toBe(1)
|
||||
it.effect(
|
||||
"seeds initial state at t=0 so replies before the first tick are caught, and counts ends.times by wakes",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const schedules = yield* setup()
|
||||
slack.replies = 3
|
||||
const schedule = yield* schedules.create({
|
||||
sessionID,
|
||||
name: "thread",
|
||||
when: { type: "every", every: "2 minutes" },
|
||||
if: { type: "code", code: watchThread },
|
||||
ends: { times: 1 },
|
||||
action: { type: "prompt", prompt: "New thread activity." },
|
||||
})
|
||||
// Seeded at creation (t=0): 1 read, baseline state = { replies: 3, runs: 0 }.
|
||||
expect(slack.reads).toBe(1)
|
||||
|
||||
// Quiet tick at +2m (still 3 replies): remainingRuns stays 1!
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
yield* schedules.tick
|
||||
expect(yield* pending).toEqual([])
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "active",
|
||||
runCount: 0,
|
||||
remainingRuns: 1,
|
||||
lastOutcome: "checked",
|
||||
})
|
||||
// Quiet tick at +2m (still 3 replies): remainingRuns stays 1!
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
yield* schedules.tick
|
||||
expect(yield* pending).toEqual([])
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "active",
|
||||
runCount: 0,
|
||||
remainingRuns: 1,
|
||||
lastOutcome: "checked",
|
||||
})
|
||||
|
||||
// Reply arrives -> wakes once and stops because ends.times = 1.
|
||||
slack.replies = 4
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
yield* schedules.tick
|
||||
expect(yield* pending).toEqual([messageID(schedule, 1)])
|
||||
const { db } = yield* Database.Service
|
||||
expect(yield* SessionInbox.find(db, messageID(schedule, 1))).toMatchObject({
|
||||
payload: {
|
||||
text: "New thread activity.\n\n4 replies",
|
||||
metadata: { schedule: { id: schedule.id, run: 1, check: { message: "4 replies" } } },
|
||||
},
|
||||
})
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "done",
|
||||
runCount: 1,
|
||||
remainingRuns: 0,
|
||||
lastOutcome: "fired",
|
||||
})
|
||||
}),
|
||||
// Reply arrives -> wakes once and stops because ends.times = 1.
|
||||
slack.replies = 4
|
||||
yield* TestClock.adjust("2 minutes")
|
||||
yield* schedules.tick
|
||||
expect(yield* pending).toEqual([messageID(schedule, 1)])
|
||||
const { db } = yield* Database.Service
|
||||
expect(yield* SessionInbox.find(db, messageID(schedule, 1))).toMatchObject({
|
||||
payload: {
|
||||
text: "New thread activity.\n\n4 replies",
|
||||
metadata: { schedule: { id: schedule.id, run: 1, check: { message: "4 replies" } } },
|
||||
},
|
||||
})
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "done",
|
||||
runCount: 1,
|
||||
remainingRuns: 0,
|
||||
lastOutcome: "fired",
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("supports boolean and state-free returns for until checks, including immediate t=0 match", () =>
|
||||
@@ -1435,45 +1438,47 @@ describe("Schedule", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("fails schedule_create when a t=0 code run throws, and pauses on strike 1 when a later tick returns an invalid shape", () =>
|
||||
Effect.gen(function* () {
|
||||
const schedules = yield* setup()
|
||||
const thrown = yield* Effect.flip(
|
||||
schedules.create({
|
||||
it.effect(
|
||||
"fails schedule_create when a t=0 code run throws, and pauses on strike 1 when a later tick returns an invalid shape",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const schedules = yield* setup()
|
||||
const thrown = yield* Effect.flip(
|
||||
schedules.create({
|
||||
sessionID,
|
||||
when: { type: "cron", cron: "0 9 * * *" },
|
||||
if: {
|
||||
type: "code",
|
||||
code: `const t = await tools.slack.read_thread({}); return t.nonexistent.length > 0`,
|
||||
},
|
||||
action: { type: "prompt", prompt: "Morning check." },
|
||||
}),
|
||||
)
|
||||
expect(thrown).toMatchObject({ _tag: "Schedule.InvalidError" })
|
||||
|
||||
// First run (state === null) returns valid { wake: false, state: { ok: true } };
|
||||
// first scheduled tick (state !== null) returns an invalid string, pausing immediately on strike 1.
|
||||
const schedule = yield* schedules.create({
|
||||
sessionID,
|
||||
when: { type: "cron", cron: "0 9 * * *" },
|
||||
when: { type: "every", every: "1 minute" },
|
||||
if: {
|
||||
type: "code",
|
||||
code: `const t = await tools.slack.read_thread({}); return t.nonexistent.length > 0`,
|
||||
code: `if (state === null) return { wake: false, state: { ok: true } }; return "not-a-check-result"`,
|
||||
},
|
||||
action: { type: "prompt", prompt: "Morning check." },
|
||||
}),
|
||||
)
|
||||
expect(thrown).toMatchObject({ _tag: "Schedule.InvalidError" })
|
||||
|
||||
// First run (state === null) returns valid { wake: false, state: { ok: true } };
|
||||
// first scheduled tick (state !== null) returns an invalid string, pausing immediately on strike 1.
|
||||
const schedule = yield* schedules.create({
|
||||
sessionID,
|
||||
when: { type: "every", every: "1 minute" },
|
||||
if: {
|
||||
type: "code",
|
||||
code: `if (state === null) return { wake: false, state: { ok: true } }; return "not-a-check-result"`,
|
||||
},
|
||||
action: { type: "prompt", prompt: "Watch." },
|
||||
})
|
||||
yield* TestClock.adjust("1 minute")
|
||||
yield* schedules.tick
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "paused",
|
||||
lastOutcome: "failed",
|
||||
runCount: 1,
|
||||
})
|
||||
const { db } = yield* Database.Service
|
||||
expect(yield* SessionInbox.find(db, messageID(schedule, 1))).toMatchObject({
|
||||
payload: { text: expect.stringContaining("[Schedule paused after invalid check:") },
|
||||
})
|
||||
}),
|
||||
action: { type: "prompt", prompt: "Watch." },
|
||||
})
|
||||
yield* TestClock.adjust("1 minute")
|
||||
yield* schedules.tick
|
||||
expect(yield* schedules.get(schedule.id)).toMatchObject({
|
||||
status: "paused",
|
||||
lastOutcome: "failed",
|
||||
runCount: 1,
|
||||
})
|
||||
const { db } = yield* Database.Service
|
||||
expect(yield* SessionInbox.find(db, messageID(schedule, 1))).toMatchObject({
|
||||
payload: { text: expect.stringContaining("[Schedule paused after invalid check:") },
|
||||
})
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("picks up a schedule created by another process sharing the DB while idle", () =>
|
||||
|
||||
@@ -38,7 +38,7 @@ const client = Layer.mock(LLMClient.Service)({
|
||||
},
|
||||
generate: () => Effect.die("unused"),
|
||||
})
|
||||
const config = Layer.mock(Config.Service)({ entries: () => Effect.succeed([]) })
|
||||
const config = Layer.mock(Config.Service)({ entries: () => Effect.succeed([]), snapshot: () => [] })
|
||||
const models = Layer.mock(SessionRunnerModel.Service)({
|
||||
resolve: () =>
|
||||
Effect.succeed(
|
||||
|
||||
@@ -93,7 +93,12 @@ const setup = (experimental: ConfigExperimental.Info | undefined) =>
|
||||
},
|
||||
session: { prompt: (input) => Effect.sync(() => prompts.push(input)).pipe(Effect.as(undefined as never)) },
|
||||
}).pipe(
|
||||
Effect.provide(Layer.mock(Config.Service)({ entries: () => Effect.succeed([document(experimental)]) })),
|
||||
Effect.provide(
|
||||
Layer.mock(Config.Service)({
|
||||
entries: () => Effect.succeed([document(experimental)]),
|
||||
snapshot: () => [document(experimental)],
|
||||
}),
|
||||
),
|
||||
Effect.provide(Layer.succeed(Schedule.Service, schedules.service)),
|
||||
Effect.provide(permissionLayer({ assert: () => Effect.void })),
|
||||
)
|
||||
|
||||
@@ -30,7 +30,12 @@ const it = testEffect(
|
||||
const setup = (experimental: ConfigExperimental.Info | undefined) =>
|
||||
Effect.gen(function* () {
|
||||
yield* registerToolPlugin(TodoTool.Plugin, { event: { subscribe: () => Stream.never } }).pipe(
|
||||
Effect.provide(Layer.mock(Config.Service)({ entries: () => Effect.succeed([document(experimental)]) })),
|
||||
Effect.provide(
|
||||
Layer.mock(Config.Service)({
|
||||
entries: () => Effect.succeed([document(experimental)]),
|
||||
snapshot: () => [document(experimental)],
|
||||
}),
|
||||
),
|
||||
Effect.provide(permissionLayer({ assert: () => Effect.void })),
|
||||
)
|
||||
return yield* Tool.Service
|
||||
|
||||
@@ -31,6 +31,12 @@ export const ServerOptions = Schema.Struct({
|
||||
* example, a Cloudflare Worker can route requests through a Service Binding.
|
||||
*/
|
||||
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
|
||||
* 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.
|
||||
*/
|
||||
console: Schema.optional(Schema.Struct({ poll: Schema.optional(Schema.Boolean) })),
|
||||
config: Schema.optional(
|
||||
Schema.Struct({
|
||||
directory: Schema.optional(Schema.String),
|
||||
|
||||
@@ -29,6 +29,7 @@ import { InstructionDiscovery } from "@opencode/core/instruction-discovery"
|
||||
import { LocationServiceMap } from "@opencode/core/location-service-map"
|
||||
import { LocationActivity } from "@opencode/core/location-activity"
|
||||
import { ModelsDev } from "@opencode/core/models-dev"
|
||||
import { ConsoleAccess } from "@opencode/core/console-access"
|
||||
import { SessionRestart } from "@opencode/core/session/execution/restart"
|
||||
import { Schedule } from "@opencode/core/schedule"
|
||||
import { PluginUpdate } from "@opencode/core/plugin/update"
|
||||
@@ -128,6 +129,7 @@ function makeRoutes<AuthError, AuthServices>(
|
||||
App.node.replace(App.configured(options.app)),
|
||||
ModelsDev.node.replace(ModelsDev.configured(options.models)),
|
||||
httpFetch.replace(fetchLayer(options.fetch)),
|
||||
ConsoleAccess.node.replace(ConsoleAccess.configured(options.console)),
|
||||
Watcher.node.replace(Watcher.configured({ enabled: options.fs?.filewatcher })),
|
||||
FileWatch.node.replace(FileWatch.configured({ enabled: options.fs?.filewatcher })),
|
||||
FileSystemSearch.node.replace(FileSystemSearch.configured({ fff: options.fs?.fff })),
|
||||
|
||||
@@ -48,6 +48,8 @@ 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. */
|
||||
readonly console?: ServerOptions["console"]
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,6 +76,7 @@ export function serverOptions(options: Options): ServerOptions {
|
||||
config: { content: options.config?.content },
|
||||
models: options.models,
|
||||
fetch: options.fetch,
|
||||
console: { poll: false, ...options.console },
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,3 +40,8 @@ test("accepts an embedder fetch and rejects other values", () => {
|
||||
expect(Option.getOrThrow(decode({ fetch })).fetch).toBe(fetch)
|
||||
expect(Option.isNone(decode({ fetch: "https://example.com" }))).toBe(true)
|
||||
})
|
||||
|
||||
test("accepts Console refresh options", () => {
|
||||
expect(Option.getOrThrow(decode({ console: { poll: false } })).console).toEqual({ poll: false })
|
||||
expect(Option.isNone(decode({ console: { poll: "no" } }))).toBe(true)
|
||||
})
|
||||
|
||||
@@ -40,5 +40,15 @@ it.effect("fails Npm.add with a clear workerd error instead of running the insta
|
||||
const error = yield* npm.add("some-package").pipe(Effect.flip)
|
||||
expect(error).toBeInstanceOf(Npm.InstallFailedError)
|
||||
expect(String(error.cause)).toContain("Npm.add is unavailable in the workerd profile")
|
||||
}).pipe(Effect.provide(AppNodeBuilder.build(Npm.node, ServerWorkerd.replacements({ storage: makeDurableObjectStorage() })))),
|
||||
}).pipe(
|
||||
Effect.provide(AppNodeBuilder.build(Npm.node, ServerWorkerd.replacements({ storage: makeDurableObjectStorage() }))),
|
||||
),
|
||||
)
|
||||
|
||||
it.effect("turns off the background Console check unless the host asks for it", () =>
|
||||
Effect.sync(() => {
|
||||
const storage = makeDurableObjectStorage()
|
||||
expect(ServerWorkerd.serverOptions({ storage }).console).toEqual({ poll: false })
|
||||
expect(ServerWorkerd.serverOptions({ storage, console: { poll: true } }).console).toEqual({ poll: true })
|
||||
}),
|
||||
)
|
||||
|
||||
@@ -71,6 +71,12 @@ OpenCodeWorkerd.create({
|
||||
})
|
||||
```
|
||||
|
||||
## OpenCode Console
|
||||
|
||||
With an [OpenCode Console](/console) connection, the Workerd profile refreshes the Console's config when a prompt
|
||||
starts, not every minute, so an idle Durable Object can hibernate. Pass `console: { poll: true }` to refresh every
|
||||
minute as other hosts do.
|
||||
|
||||
## Customize
|
||||
|
||||
Customize your OpenCode instance by registering plugins bundled with your
|
||||
|
||||
@@ -315,12 +315,12 @@ Blocked by Acme's policy
|
||||
|
||||
Statements follow the connected account.
|
||||
|
||||
| Event | Result |
|
||||
| --------------------------- | -------------------------------------------------- |
|
||||
| Console policy changes | Applied on the next fetch, about once a minute |
|
||||
| Switch to another workspace | Statements replaced with that workspace's |
|
||||
| Disconnect | Statements cleared |
|
||||
| Console unreachable | Last statements for that connection stay in effect |
|
||||
| Event | Result |
|
||||
| --------------------------- | ---------------------------------------------------------------------------------------------------------- |
|
||||
| Console policy changes | Applied on the next fetch: about once a minute, or when a prompt starts over a minute after the last fetch |
|
||||
| Switch to another workspace | Statements replaced with that workspace's |
|
||||
| Disconnect | Statements cleared |
|
||||
| Console unreachable | Last statements for that connection stay in effect |
|
||||
|
||||
The Console plugin and the policy plugin cannot be disabled from `plugins`; see
|
||||
[plugin control](/plugins#control).
|
||||
|
||||
Reference in New Issue
Block a user