Skip to content

fix: resolve agent by name fallback and guard undefined agent (#13790)#13821

Closed
echoVic wants to merge 1 commit into
anomalyco:devfrom
echoVic:fix/13790-agent-variant-undefined
Closed

fix: resolve agent by name fallback and guard undefined agent (#13790)#13821
echoVic wants to merge 1 commit into
anomalyco:devfrom
echoVic:fix/13790-agent-variant-undefined

Conversation

@echoVic

@echoVic echoVic commented Feb 16, 2026

Copy link
Copy Markdown

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

…gents (anomalyco#13790)

Root cause: Agent.defaultAgent() returned agent.name (display name) instead
of the state map key. When a custom agent sets a different name (e.g.,
name: 'Build (guarded)' with config key 'build-guarded'), Agent.get()
would receive the display name, find no match in the map, and return
undefined — causing TypeError on agent.variant access.

Fix:
- defaultAgent() now returns the map key (config key) instead of agent.name
- Added defensive guard in createUserMessage to throw a clear error if
  Agent.get() returns undefined
@github-actions

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

@github-actions github-actions Bot closed this Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when trying to use custom primary agent (TypeError: undefined is not an object (evaluating 'agent.variant'))

1 participant