Skip to content

opencode run --agent crashes with "InstanceRef not provided" #27833

@javazys

Description

@javazys

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

  • opencode version: 1.15.1

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions