Agent.get() returns undefined without error handling, causing crash #6817

Closed
opened 2026-02-16 18:05:21 -05:00 by yindo · 2 comments
Owner

Originally created by @rcdailey on GitHub (Jan 19, 2026).

Originally assigned to: @rekram1-node on GitHub.

Description

Agent.get() returns undefined when an agent doesn't exist, but callers like createUserMessage() don't handle this case, causing a crash:

TypeError: undefined is not an object (evaluating 'agent.name')
  at createUserMessage (src/session/prompt.ts:831:14)

The issue is in src/agent/agent.ts line 246-248:

export async function get(agent: string) {
  return state().then((x) => x[agent])  // Returns undefined if not found
}

And src/session/prompt.ts line 825, 833:

const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent()))
// ...
agent: agent.name,  // Crashes here when agent is undefined

This can be triggered by any caller passing an invalid agent name - in my case, it was the plannotator plugin defaulting to "build" when I had that agent disabled.

Plugins

@plannotator/opencode@0.6.6

OpenCode version

v1.1.25

Steps to reproduce

  1. Disable the default build agent in opencode config:
    { "agent": { "build": { "disable": true } } }
    
  2. Install plannotator plugin
  3. Use plannotator to create and approve a plan (without changing agent switch setting in plannotator UI)
  4. OpenCode crashes with TypeError

Screenshot and/or share link

N/A - crash occurs before UI can render

Operating System

Fedora Linux

Terminal

Ghostty


Suggested Fix

Either:

  1. Agent.get() should throw a descriptive error when agent not found
  2. Or createUserMessage() should validate and provide a helpful message like: "Agent 'build' not found. Available agents: [list]"

This is a defensive fix - callers shouldn't need to know that Agent.get() can return undefined.

Related

Originally created by @rcdailey on GitHub (Jan 19, 2026). Originally assigned to: @rekram1-node on GitHub. ### Description `Agent.get()` returns `undefined` when an agent doesn't exist, but callers like `createUserMessage()` don't handle this case, causing a crash: ``` TypeError: undefined is not an object (evaluating 'agent.name') at createUserMessage (src/session/prompt.ts:831:14) ``` The issue is in `src/agent/agent.ts` line 246-248: ```typescript export async function get(agent: string) { return state().then((x) => x[agent]) // Returns undefined if not found } ``` And `src/session/prompt.ts` line 825, 833: ```typescript const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent())) // ... agent: agent.name, // Crashes here when agent is undefined ``` This can be triggered by any caller passing an invalid agent name - in my case, it was the plannotator plugin defaulting to "build" when I had that agent disabled. ### Plugins @plannotator/opencode@0.6.6 ### OpenCode version v1.1.25 ### Steps to reproduce 1. Disable the default `build` agent in opencode config: ```json { "agent": { "build": { "disable": true } } } ``` 2. Install plannotator plugin 3. Use plannotator to create and approve a plan (without changing agent switch setting in plannotator UI) 4. OpenCode crashes with TypeError ### Screenshot and/or share link N/A - crash occurs before UI can render ### Operating System Fedora Linux ### Terminal Ghostty --- ### Suggested Fix Either: 1. **`Agent.get()` should throw** a descriptive error when agent not found 2. **Or `createUserMessage()` should validate** and provide a helpful message like: "Agent 'build' not found. Available agents: [list]" This is a defensive fix - callers shouldn't need to know that `Agent.get()` can return undefined. ### Related - plannotator issue: https://github.com/backnotprop/plannotator/issues/94 (addresses the root cause on their side) - Similar TUI-layer crashes: #9332, #9393, #9405 (same error pattern, different code path)
yindo closed this issue 2026-02-16 18:05:21 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Jan 19, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #7918: Crash when local.agent.current() returns undefined during startup/auth (same root cause of undefined agent handling)
  • #4578: OpenCode GitHub Actions can't work with disabled default agents (same scenario with disabled agents causing crashes)
  • #9332, #9393, #9405: Similar TUI-layer crashes with the same error pattern

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 19, 2026): This issue might be a duplicate of existing issues. Please check: - #7918: Crash when local.agent.current() returns undefined during startup/auth (same root cause of undefined agent handling) - #4578: OpenCode GitHub Actions can't work with disabled default agents (same scenario with disabled agents causing crashes) - #9332, #9393, #9405: Similar TUI-layer crashes with the same error pattern Feel free to ignore if none of these address your specific case.
Author
Owner

@rcdailey commented on GitHub (Jan 21, 2026):

@rekram1-node duplicate of what issue specifically?

@rcdailey commented on GitHub (Jan 21, 2026): @rekram1-node duplicate of what issue specifically?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6817