TypeError: undefined is not an object (evaluating 'agent.name') in createUserMessage #8334

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

Originally created by @dead-pool-aka-wilson on GitHub (Feb 2, 2026).

Originally assigned to: @rekram1-node on GitHub.

Bug Report

Error

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

Root Cause

In createUserMessage, Agent.get() can return undefined when the agent name cannot be resolved, but the code proceeds to access agent.name without a null check.

async function createUserMessage(input: PromptInput) {
    const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent()))
    // agent can be undefined here if:
    // 1. input.agent is invalid
    // 2. defaultAgent() returns an invalid name
    // 3. Agent.get() fails to find the agent
    
    // Later in the code:
    agent: agent.name,  // TypeError when agent is undefined

Suggested Fix

Add null check after Agent.get():

const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent()))
if (!agent) {
    throw new Error(`Agent not found: ${input.agent ?? 'default'}`)
}

Environment

  • OpenCode version: 1.1.48
  • Platform: macOS (darwin-arm64)

Reproduction

Occurs intermittently, likely when custom agent configuration is missing or malformed.

Originally created by @dead-pool-aka-wilson on GitHub (Feb 2, 2026). Originally assigned to: @rekram1-node on GitHub. ## Bug Report ### Error ``` TypeError: undefined is not an object (evaluating 'agent.name') at createUserMessage (src/session/prompt.ts:838:14) ``` ### Root Cause In `createUserMessage`, `Agent.get()` can return `undefined` when the agent name cannot be resolved, but the code proceeds to access `agent.name` without a null check. ```typescript async function createUserMessage(input: PromptInput) { const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent())) // agent can be undefined here if: // 1. input.agent is invalid // 2. defaultAgent() returns an invalid name // 3. Agent.get() fails to find the agent // Later in the code: agent: agent.name, // TypeError when agent is undefined ``` ### Suggested Fix Add null check after `Agent.get()`: ```typescript const agent = await Agent.get(input.agent ?? (await Agent.defaultAgent())) if (!agent) { throw new Error(`Agent not found: ${input.agent ?? 'default'}`) } ``` ### Environment - OpenCode version: 1.1.48 - Platform: macOS (darwin-arm64) ### Reproduction Occurs intermittently, likely when custom agent configuration is missing or malformed.
yindo closed this issue 2026-02-16 18:09:42 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Feb 2, 2026):

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

  • #11401: opentui fatal error accessing agent.current().name - same error pattern
  • #11262: Cannot read property 'name' of undefined (local.agent.current()) - identical root cause in same location
  • #7918: Crash when local.agent.current() returns undefined - systematic agent initialization issue
  • #9733: TypeError in session prompt handling - similar error in prompt.ts accessing undefined object properties
  • #11702: Non-null assertions on find() results causing runtime crashes - related null reference pattern
  • #11699: Race condition in session prompt state management - can cause undefined access errors
  • #11135, #11096, #11120, #11110, #11075, #11067, #11017, #11018, #11011, #11010, #11004, #11002, #10984, #10983, #10980, #10971, #10969, #10954: Additional instances of undefined agent access in TUI components

The common pattern across these issues suggests a systemic problem with insufficient null checking when agent objects return undefined. Consider consolidating these under a larger initiative to add null-safety checks throughout the codebase, particularly in Agent.get(), local.agent.current(), and session prompt handling.

Feel free to ignore if your specific case has unique requirements.

@github-actions[bot] commented on GitHub (Feb 2, 2026): This issue might be a duplicate of existing issues. Please check: - #11401: opentui fatal error accessing agent.current().name - same error pattern - #11262: Cannot read property 'name' of undefined (local.agent.current()) - identical root cause in same location - #7918: Crash when local.agent.current() returns undefined - systematic agent initialization issue - #9733: TypeError in session prompt handling - similar error in prompt.ts accessing undefined object properties - #11702: Non-null assertions on find() results causing runtime crashes - related null reference pattern - #11699: Race condition in session prompt state management - can cause undefined access errors - #11135, #11096, #11120, #11110, #11075, #11067, #11017, #11018, #11011, #11010, #11004, #11002, #10984, #10983, #10980, #10971, #10969, #10954: Additional instances of undefined agent access in TUI components The common pattern across these issues suggests a systemic problem with insufficient null checking when agent objects return undefined. Consider consolidating these under a larger initiative to add null-safety checks throughout the codebase, particularly in Agent.get(), local.agent.current(), and session prompt handling. Feel free to ignore if your specific case has unique requirements.
Author
Owner

@dead-pool-aka-wilson commented on GitHub (Feb 2, 2026):

Duplicate of #11110 / #7918 - same root cause: agent.current() returning undefined

@dead-pool-aka-wilson commented on GitHub (Feb 2, 2026): Duplicate of #11110 / #7918 - same root cause: agent.current() returning undefined
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8334