Skip to content

Commit 55dde88

Browse files
authored
feat(sdk): seed host plugins before recovery and harden workerd defaults (#54219)
1 parent 8eff035 commit 55dde88

15 files changed

Lines changed: 291 additions & 50 deletions

File tree

‎packages/core/src/plugin/provider/local.ts‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { define } from "@opencode/plugin/effect/plugin"
22
import type { Entry } from "@opencode/schema/config"
3-
import { Duration, Effect, Schedule, type Schema, Semaphore, Stream } from "effect"
3+
import { Duration, Effect, Option, Schedule, type Schema, Semaphore, Stream } from "effect"
44
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http"
5+
import { shellParserWasm } from "#shell-parser-wasm"
56
import { Config } from "../../config.js"
7+
import { Environment } from "../../environment/index.js"
8+
import { EnvironmentUnavailable } from "../../environment/unavailable.js"
9+
import { Location } from "../../location.js"
610
import { Model } from "../../model.js"
711
import type { PluginInternal } from "../internal.js"
812
import { foldSettings } from "./configured.js"
@@ -31,13 +35,22 @@ export function createLocalProviderPlugin<Discovered>(input: {
3135
const discoveryLock = Semaphore.makeUnsafe(1)
3236
const suffix = input.stripPathSuffix ?? /\/v1$/
3337

34-
return function make(origin = input.origin, interval: Duration.Input = "30 seconds") {
38+
return function make(origin?: string, interval: Duration.Input = "30 seconds") {
3539
return define({
3640
id: input.id,
3741
effect: Effect.fn(function* (ctx) {
3842
const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient)
3943
const config = yield* Config.Service
40-
const source = { current: configured(yield* config.entries(), input.providerID, origin, suffix) }
44+
const environment = yield* Effect.serviceOption(Environment.Service)
45+
const location = yield* Effect.serviceOption(Location.Service)
46+
const defaultOrigin =
47+
origin ??
48+
(shellParserWasm.bash === "" ||
49+
(Option.isSome(location) && location.value.workspaceID !== undefined) ||
50+
(Option.isSome(environment) && environment.value.spawner === EnvironmentUnavailable.spawner)
51+
? undefined
52+
: input.origin)
53+
const source = { current: configured(yield* config.entries(), input.providerID, defaultOrigin, suffix) }
4154
const loaded = { models: [] as readonly Discovered[], hash: "[]" }
4255

4356
yield* ctx.integration.transform((integrations) => {
@@ -115,7 +128,7 @@ export function createLocalProviderPlugin<Discovered>(input: {
115128
// Keep the last successful inventory through transient outages instead of flickering model availability.
116129
yield* refresh().pipe(Effect.ignore, Effect.repeat(Schedule.spaced(interval)), Effect.forkScoped)
117130
const reload = Effect.fn("LocalProviderPlugin.reload")(function* () {
118-
const next = configured(yield* config.entries(), input.providerID, origin, suffix)
131+
const next = configured(yield* config.entries(), input.providerID, defaultOrigin, suffix)
119132
if (
120133
next.baseURL === source.current.baseURL &&
121134
next.apiKey === source.current.apiKey &&
@@ -172,10 +185,14 @@ function createClient(
172185
}
173186
}
174187

175-
function configured(entries: readonly Entry[], providerID: string, origin: string, suffix: RegExp) {
188+
function configured(entries: readonly Entry[], providerID: string, origin: string | undefined, suffix: RegExp) {
176189
const settings = foldSettings(entries, providerID, undefined)
177190
const baseURL = (
178-
typeof settings?.baseURL === "string" ? settings.baseURL : `${origin.replace(/\/+$/, "")}/v1`
191+
typeof settings?.baseURL === "string"
192+
? settings.baseURL
193+
: origin !== undefined
194+
? `${origin.replace(/\/+$/, "")}/v1`
195+
: ""
179196
).replace(/\/+$/, "")
180197
const apiKey = typeof settings?.apiKey === "string" ? settings.apiKey : undefined
181198
if (!URL.canParse(baseURL)) return { baseURL, apiKey }

‎packages/core/src/shell.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ const layer = () =>
256256
before?: (input: ShellCreateBefore) => Effect.Effect<void, E, R>,
257257
) {
258258
const sessionID = input.metadata?.sessionID
259-
const sessionEnvironment =
260-
location.workspaceID === undefined && Schema.is(SessionSchema.ID)(sessionID)
261-
? yield* environments.get(sessionID)
262-
: undefined
259+
const baseEnv =
260+
location.workspaceID !== undefined
261+
? undefined
262+
: ((Schema.is(SessionSchema.ID)(sessionID) ? yield* environments.get(sessionID) : undefined) ?? process.env)
263263
const invocation: ShellCreateBefore = {
264264
command: input.command,
265265
cwd: input.cwd ?? location.directory,
266266
timeout: input.timeout ?? 0,
267267
shell: input.shell ?? (yield* shell.resolve({ priority: "config" })),
268268
env: {
269-
...(sessionEnvironment ?? process.env),
269+
...baseEnv,
270270
TERM: "xterm-256color",
271271
OPENCODE_TERMINAL: "1",
272272
},

‎packages/core/src/shell/parse.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,10 @@ export const scan = Effect.fnUntraced(function* (
165165
command: string,
166166
shell: string,
167167
cwd: string,
168-
options?: { portable?: boolean },
168+
options?: { portable?: boolean; wasm?: typeof shellParserWasm },
169169
) {
170-
if (options?.portable) return yield* scanPortable(command, shell, cwd)
170+
if (options?.portable || (options?.wasm ?? shellParserWasm).bash === "")
171+
return yield* scanPortable(command, shell, cwd)
171172
return yield* scanLegacy(command, shell, cwd)
172173
})
173174

‎packages/core/src/tool/plugin/shell.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { NonNegativeInt } from "../../schema.js"
1515
import { Session } from "../../session.js"
1616
import { SessionSchema } from "../../session/schema.js"
1717
import { Shell } from "../../shell.js"
18+
import { shellParserWasm } from "#shell-parser-wasm"
1819
import { ShellParse } from "../../shell/parse.js"
1920
import { ShellSelect } from "../../shell/select.js"
2021
import { ShellResult } from "../../shell/result.js"
@@ -124,7 +125,7 @@ export const Plugin = {
124125
const timeout = invocation.timeout
125126
const portable =
126127
Config.latest(yield* config.entries(), "experimental")?.portable_shell_scanner ??
127-
(ctx.app.channel === "local" || ctx.app.channel === "dev")
128+
(shellParserWasm.bash === "" || ctx.app.channel === "local" || ctx.app.channel === "dev")
128129
const parsed = yield* ShellParse.scan(invocation.command, invocation.shell, target.absolute, { portable })
129130
const directories = yield* Effect.forEach(parsed.directories, (directory) =>
130131
access.resolve({

‎packages/core/test/plugin/provider-ollama.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Bus } from "@opencode/core/bus"
22
import { Config } from "@opencode/core/config"
3+
import { Environment, makeFiles } from "@opencode/core/environment/index"
4+
import { EnvironmentUnavailable } from "@opencode/core/environment/unavailable"
35
import { Integration } from "@opencode/core/integration"
46
import { Model } from "@opencode/core/model"
57
import { Plugin } from "@opencode/core/plugin"
@@ -38,6 +40,25 @@ function eventually<A>(
3840
}
3941

4042
describe("OllamaPlugin", () => {
43+
it.live("skips unconfigured default local discovery when the location has no local execution plane", () =>
44+
Effect.gen(function* () {
45+
const plugin = yield* Plugin.Service
46+
const host = yield* PluginHost.make(plugin)
47+
const providers = yield* Provider.Service
48+
yield* OllamaPlugin.effect(host).pipe(
49+
Effect.provideService(
50+
Environment.Service,
51+
Environment.Service.of({
52+
files: makeFiles({ spawner: EnvironmentUnavailable.spawner }),
53+
spawner: EnvironmentUnavailable.spawner,
54+
}),
55+
),
56+
)
57+
yield* Effect.promise(() => Bun.sleep(20))
58+
expect(yield* providers.get(Provider.ID.make("ollama"))).toBeUndefined()
59+
}),
60+
)
61+
4162
it.live("discovers local completion models and native metadata", () =>
4263
Effect.acquireUseRelease(
4364
Effect.sync(() => {

‎packages/core/test/shell-parse.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Effect } from "effect"
33
import os from "os"
44
import path from "path"
55
import { ShellParse } from "@opencode/core/shell/parse"
6+
import { shellParserWasm } from "../src/shell/parser-wasm.workerd"
67

78
describe("ShellParse", () => {
89
test("splits bash commands and derives reusable prefixes", async () => {
@@ -18,6 +19,35 @@ describe("ShellParse", () => {
1819
})
1920
})
2021

22+
test("defaults to portable scanning when parser wasm assets are empty", async () => {
23+
const result = await Effect.runPromise(
24+
ShellParse.scan("git status && npm run test -- --watch", "/bin/bash", "/workspace", {
25+
wasm: shellParserWasm,
26+
}),
27+
)
28+
expect(result).toEqual({
29+
commands: [
30+
{ resource: "git status", save: "git status *" },
31+
{ resource: "npm run test -- --watch", save: "npm run test *" },
32+
],
33+
directories: [],
34+
})
35+
const malformed = await Effect.runPromiseExit(
36+
ShellParse.scan('echo "', "/bin/bash", "/workspace", { wasm: shellParserWasm }),
37+
)
38+
expect(malformed).toMatchObject({
39+
_tag: "Failure",
40+
cause: {
41+
reasons: [
42+
{
43+
_tag: "Fail",
44+
error: { message: expect.stringContaining("Portable shell scanner cannot analyze command") },
45+
},
46+
],
47+
},
48+
})
49+
})
50+
2151
test("portable scanning preserves supported command resources and directories", async () => {
2252
const commands = [
2353
"git status && npm run test -- --watch",

‎packages/core/test/shell-retention.test.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
66
import { Config } from "@opencode/core/config"
77
import { Environment } from "@opencode/core/environment/index"
88
import { Location } from "@opencode/core/location"
9+
import { AbsolutePath } from "@opencode/core/schema"
910
import { Shell } from "@opencode/core/shell"
11+
import { Workspace } from "@opencode/core/workspace"
1012
import { Global } from "@opencode/util/global"
1113
import { hostEnvironmentLayer } from "./fixture/environment"
1214
import { tempGlobalLayer } from "./fixture/global"
13-
import { tempLocationLayer } from "./fixture/location"
15+
import { location, tempLocationLayer } from "./fixture/location"
16+
import { tmpdirScoped } from "./fixture/tmpdir"
1417
import { it } from "./lib/effect"
1518

1619
it.live("eviction makes progress past an already-removed shell", () =>
@@ -97,3 +100,48 @@ it.live("eviction makes progress past an already-removed shell", () =>
97100
)
98101
}),
99102
)
103+
104+
it.live("does not inherit host process.env when creating a shell in a workspace-backed location", () =>
105+
Effect.gen(function* () {
106+
const tmp = yield* tmpdirScoped()
107+
let capturedEnv: Record<string, string | undefined> | undefined
108+
yield* Effect.gen(function* () {
109+
const shell = yield* Shell.Service
110+
const info = yield* shell.create({ shell: "/bin/sh", command: "printf ok", timeout: 0 }, (invocation) =>
111+
Effect.sync(() => {
112+
capturedEnv = { ...invocation.env }
113+
}),
114+
)
115+
expect(yield* shell.result(info)).toMatchObject({
116+
info: { status: "exited" },
117+
})
118+
}).pipe(
119+
Effect.provide(
120+
AppNodeBuilder.build(Shell.node, [
121+
Location.node.replace(
122+
Layer.succeed(
123+
Location.Service,
124+
Location.Service.of(
125+
location(
126+
Location.Ref.make({
127+
directory: AbsolutePath.make(tmp.path),
128+
workspaceID: Workspace.ID.make("wrk_workspace"),
129+
}),
130+
),
131+
),
132+
),
133+
),
134+
Global.node.replace(tempGlobalLayer),
135+
Config.node.replace(Config.testLayer()),
136+
Environment.node.replace(hostEnvironmentLayer),
137+
]),
138+
),
139+
)
140+
141+
expect(process.env.PATH).toBeDefined()
142+
expect(capturedEnv).toEqual({
143+
TERM: "xterm-256color",
144+
OPENCODE_TERMINAL: "1",
145+
})
146+
}),
147+
)

‎packages/sdk/src/effect/workerd.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ export type Configuration = WorkerdProfile.Configuration
99

1010
export interface CreateOptions<R = never> extends WorkerdProfile.Options {
1111
readonly log?: OpenCode.CreateOptions["log"]
12+
readonly plugins?: OpenCode.CreateOptions["plugins"]
1213
readonly workspaceProviders?: OpenCode.CreateOptions["workspaceProviders"]
1314
readonly instances?: OpenCode.CreateOptions<R>["instances"]
1415
}
1516

16-
export const create = <R = never>({ log, workspaceProviders, instances, ...options }: CreateOptions<R>) => {
17+
export const create = <R = never>({ log, plugins, workspaceProviders, instances, ...options }: CreateOptions<R>) => {
1718
const profile = WorkerdProfile.make(options)
1819
return OpenCode.create(
19-
{ ...profile.options, log, workspaceProviders, instances },
20+
{ ...profile.options, log, plugins, workspaceProviders, instances },
2021
{ overrides: profile.replacements },
2122
)
2223
}

‎packages/sdk/src/internal/host.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * as EmbeddedHost from "./host"
22

33
import { SdkPlugins } from "@opencode/core/plugin/sdk"
4+
import type { Plugin } from "@opencode/plugin/effect/plugin"
45
import { SessionRestart } from "@opencode/core/session/execution/restart"
56
import { Session } from "@opencode/core/session"
67
import { Workspace } from "@opencode/core/workspace"
@@ -16,6 +17,7 @@ import { SdkInstances } from "./instances"
1617

1718
export interface CreateOptions<R = never> extends Omit<ServerOptions, "hostname" | "port" | "password"> {
1819
readonly log?: LogOptions
20+
readonly plugins?: ReadonlyArray<Plugin>
1921
readonly workspaceProviders?: Readonly<Record<string, WorkspaceDriver.Interface>>
2022
readonly instances?: SdkInstances.Options<R>
2123
}
@@ -29,7 +31,7 @@ export const create = Effect.fn("EmbeddedHost.create")(function* <R = never>(
2931
options: CreateOptions<R> = {},
3032
embed: EmbedOptions = {},
3133
) {
32-
const { log, workspaceProviders, instances, ...server } = options
34+
const { log, plugins, workspaceProviders, instances, ...server } = options
3335
const selector = instances ? SdkInstances.provide(instances, yield* Effect.context<R>()) : undefined
3436
const runtime = ManagedRuntime.make(
3537
createEmbeddedRoutes(
@@ -47,6 +49,8 @@ export const create = Effect.fn("EmbeddedHost.create")(function* <R = never>(
4749

4850
return yield* Effect.gen(function* () {
4951
const services = yield* runtime.contextEffect
52+
const sdkPlugins = Context.get(services, SdkPlugins.Service)
53+
for (const plugin of plugins ?? []) yield* sdkPlugins.register(plugin)
5054
// The sweep is a no-op when nothing is suspended. ManagedRuntime owns the
5155
// fiber so recovery never delays startup but still stops with the host.
5256
runtime.runFork(Context.get(services, SessionRestart.Service).resumeSuspendedSessions)
@@ -58,7 +62,7 @@ export const create = Effect.fn("EmbeddedHost.create")(function* <R = never>(
5862
return {
5963
runtime,
6064
fetch: transport.fetch,
61-
plugins: Context.get(services, SdkPlugins.Service),
65+
plugins: sdkPlugins,
6266
sessions: Context.get(services, Session.Service),
6367
workspace: Context.get(services, Workspace.Service),
6468
close: transport.close,

‎packages/sdk/src/promise.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export interface InstanceOptions {
1717
readonly configure: (key: string) => InstanceConfiguration | Promise<InstanceConfiguration>
1818
}
1919

20-
export interface CreateOptions extends Omit<EmbeddedHost.CreateOptions, "workspaceProviders" | "instances"> {
20+
export interface CreateOptions
21+
extends Omit<EmbeddedHost.CreateOptions, "plugins" | "workspaceProviders" | "instances"> {
2122
readonly plugins?: ReadonlyArray<Plugin.Plugin>
2223
readonly instances?: InstanceOptions
2324
}
@@ -36,6 +37,7 @@ export async function create(options: CreateOptions = {}, embed: EmbeddedHost.Em
3637
EmbeddedHost.create(
3738
{
3839
...hostOptions,
40+
plugins: await adaptPlugins(plugins),
3941
instances: instances
4042
? {
4143
key: (session) => instances.key(Schema.encodeSync(Session.Info)(session)),
@@ -56,8 +58,6 @@ export async function create(options: CreateOptions = {}, embed: EmbeddedHost.Em
5658
const { PluginPromise } = await import("@opencode/core/plugin/promise")
5759
return host.runtime.runPromise(host.plugins.register(PluginPromise.fromPromise(plugin)))
5860
}
59-
for (const plugin of plugins ?? []) await register(plugin)
60-
6161
return {
6262
...client,
6363
sessions: client.session,
@@ -67,3 +67,9 @@ export async function create(options: CreateOptions = {}, embed: EmbeddedHost.Em
6767
[Symbol.asyncDispose]: host.close,
6868
}
6969
}
70+
71+
async function adaptPlugins(plugins: ReadonlyArray<Plugin.Plugin> | undefined) {
72+
if (!plugins?.length) return undefined
73+
const { PluginPromise } = await import("@opencode/core/plugin/promise")
74+
return plugins.map(PluginPromise.fromPromise)
75+
}

0 commit comments

Comments
 (0)