[PR #13821] fix: resolve agent by name fallback and guard undefined agent (#13790) #14824

Open
opened 2026-02-16 18:19:34 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/13821

State: open
Merged: No


Problem

When a custom primary agent's config key differs from its display name, Agent.get() fails to find the agent, returning undefined. This causes a TypeError: undefined is not an object (evaluating 'agent.variant') in createUserMessage.

Example config:

{
  "agent": {
    "build-guarded": {
      "name": "Build (guarded)",
      ...
    }
  }
}

The TUI passes the agent's display name (Build (guarded)) to Agent.get(), but the state map is keyed by config key (build-guarded), so the lookup returns undefined.

Fix

  1. Agent.get() now falls back to lookup by name field when key lookup fails, so agents can be found regardless of whether the caller uses the config key or the display name.
  2. createUserMessage() now throws a clear error (Agent "..." not found) instead of crashing with a cryptic TypeError when the agent is not found.

Changes

  • packages/opencode/src/agent/agent.ts: Add name-based fallback in Agent.get()
  • packages/opencode/src/session/prompt.ts: Guard against undefined agent in createUserMessage()

Closes #13790

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13821 **State:** open **Merged:** No --- ## Problem When a custom primary agent's config key differs from its display name, `Agent.get()` fails to find the agent, returning `undefined`. This causes a `TypeError: undefined is not an object (evaluating 'agent.variant')` in `createUserMessage`. **Example config:** ```json { "agent": { "build-guarded": { "name": "Build (guarded)", ... } } } ``` The TUI passes the agent's display name (`Build (guarded)`) to `Agent.get()`, but the state map is keyed by config key (`build-guarded`), so the lookup returns `undefined`. ## Fix 1. **`Agent.get()`** now falls back to lookup by `name` field when key lookup fails, so agents can be found regardless of whether the caller uses the config key or the display name. 2. **`createUserMessage()`** now throws a clear error (`Agent "..." not found`) instead of crashing with a cryptic TypeError when the agent is not found. ## Changes - `packages/opencode/src/agent/agent.ts`: Add name-based fallback in `Agent.get()` - `packages/opencode/src/session/prompt.ts`: Guard against undefined agent in `createUserMessage()` Closes #13790
yindo added the pull-request label 2026-02-16 18:19:34 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14824