Bug
opencode run --agent <name> crashes with InstanceRef not provided, while opencode run "message" (without --agent) works fine.
Reproduction
# ✅ Works
opencode run "hi"
# ❌ Crashes
opencode run --agent myagent "hi"
# ❌ Crashes
opencode run --agent myagent --model 'provider/model' 'message'
Output:
Error: Unexpected error, check log file at /home/.../.local/share/opencode/log/...log for more details
InstanceRef not provided
Environment
Root Cause
Commit 0c9cfe923 (refactor(instance): remove legacy runtime fallback #27757) removed Instance.restore() ALS fallback in effect-cmd.ts. The handler now calls AppRuntime.runPromise(opts.handler(args).pipe(Effect.provideService(InstanceRef, ctx))) directly without the ALS bridge.
In packages/opencode/src/cli/cmd/run.ts, the localAgent() function (line 507-529) is called inside Effect.promise(async () => { ... }). It does:
const entry = await Effect.runPromise(agentSvc.get(name))
Effect.runPromise() creates a new fiber that does not inherit the parent fiber's InstanceRef context. Without the ALS fallback, InstanceRef is lost across this async boundary, causing agentSvc.get(name) to fail when it internally tries to access InstanceRef.
Without --agent, localAgent() returns undefined immediately and never calls Effect.runPromise, so the bug doesn't trigger.
Suggested Fix
In run.ts, capture InstanceRef before entering the Effect.promise block and re-provide it when calling Effect.runPromise:
// In the handler, before Effect.promise:
const instanceCtx = yield* InstanceRef
// Then inside localAgent(), change:
const entry = await Effect.runPromise(agentSvc.get(name))
// To:
const entry = await Effect.runPromise(
agentSvc.get(name).pipe(Effect.provideService(InstanceRef, instanceCtx))
)
Alternatively, move localAgent() out of the Effect.promise block entirely so it runs in the outer Effect fiber where InstanceRef is available.
Bug
opencode run --agent <name>crashes withInstanceRef not provided, whileopencode run "message"(without--agent) works fine.Reproduction
Output:
Environment
Root Cause
Commit
0c9cfe923(refactor(instance): remove legacy runtime fallback #27757) removedInstance.restore()ALS fallback ineffect-cmd.ts. The handler now callsAppRuntime.runPromise(opts.handler(args).pipe(Effect.provideService(InstanceRef, ctx)))directly without the ALS bridge.In
packages/opencode/src/cli/cmd/run.ts, thelocalAgent()function (line 507-529) is called insideEffect.promise(async () => { ... }). It does:Effect.runPromise()creates a new fiber that does not inherit the parent fiber'sInstanceRefcontext. Without the ALS fallback,InstanceRefis lost across this async boundary, causingagentSvc.get(name)to fail when it internally tries to accessInstanceRef.Without
--agent,localAgent()returnsundefinedimmediately and never callsEffect.runPromise, so the bug doesn't trigger.Suggested Fix
In
run.ts, captureInstanceRefbefore entering theEffect.promiseblock and re-provide it when callingEffect.runPromise:Alternatively, move
localAgent()out of theEffect.promiseblock entirely so it runs in the outer Effect fiber whereInstanceRefis available.