Skip to content

Conversation

@shoaibansari5398
Copy link

@shoaibansari5398 shoaibansari5398 commented Jan 16, 2026

What does this PR do?

Fixes #8858 - Prevents crash when local.agent.current() returns undefined.
Problem: The TUI crashed with:

TypeError: undefined is not an object (evaluating 'local.agent.current().name')
This happens when the agent state hasn't been initialized yet (e.g., during startup or when no agents are available).

How did you verify your code works?

  • local.agent.current().name
  • local.agent.current()?.name ?? "build"
  1. Code review confirmed all 7 usages of local.agent.current().name now have null safety
  2. The fallback value "build" matches the default agent name used elsewhere in the codebase
  3. Verified the fix pattern is consistent with other null safety fixes in the codebase

Changes

  • Added optional chaining (?.) to local.agent.current().
  • Added a nullish coalescing operator (?? "build") to provide a fallback name ("build") when the agent is undefined.
  • Applied these fixes to all 7 occurrences in:
    • packages/opencode/src/cli/cmd/tui/component/dialog-agent.tsx
    • packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx

Verification

  • Code Review: Verified that all usages of local.agent.current().name now have null safety.
  • Manual Verification: The fallback value "build" ensures the UI renders safe defaults instead of crashing.

Checklist

  • Linked to an existing issue (Fixes #8858)
  • Verified code works locally

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

Based on my search, I found several related PRs that address similar issues with agent.current() null safety in the TUI:

Potentially Related PRs:

  1. PR fix(tui): add null check for local.agent.current() #8745 - fix(tui): add null check for local.agent.current()

  2. PR fix(opencode): add null checks for agent.current() in prompt #7920 - fix(opencode): add null checks for agent.current() in prompt

  3. PR Fix TUI crashes from undefined values (#8588, #8579, #8580) #8635 - Fix TUI crashes from undefined values (opentui: fatal: undefined is not an object (evaluating 'enabledFormatters().length') after adding an MCP #8588, opentui: fatal: undefined is not an object (evaluating 'local.agent.current().name') #8579, linux 启动报错 The socket connection was closed unexpectedly. For more information, pass verbose: true in the second argument to fetch() #8580)

  4. PR fix(tui): handle undefined agent.current() to prevent crash on startup #7748 - fix(tui): handle undefined agent.current() to prevent crash on startup

  5. PR fix(tui): handle undefined agent.current() to prevent fatal TypeError #7689 - fix(tui): handle undefined agent.current() to prevent fatal TypeError

Most likely duplicate: PR #8745 appears to be the closest match as it directly addresses null checks for local.agent.current(). You may want to check if this PR has already been merged or if it covers the same 7 usages mentioned in PR #8882.

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.

opentui: fatal: undefined is not an object (evaluating 'local.agent.current().name')

1 participant