Skip to content

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

Closed

Description

@dead-pool-aka-wilson

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.

Activity

  1. github-actions commented on Feb 2, 2026

    @github-actions
    Contributor

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

    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.

  2. dead-pool-aka-wilson commented on Feb 2, 2026

    @dead-pool-aka-wilson
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions