opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix(tui): handle undefined agent.current() to prevent fatal TypeError

Open peremajoral opened this issue 6 days ago • 2 comments

Summary

Fixes the fatal error: TypeError: undefined is not an object (evaluating 'local.agent.current().name')

Root Cause

When the agents list is empty or the stored agent name doesn't match any available agent, local.agent.current() returns undefined. The code then crashes when accessing .name on undefined.

Affected locations:

  • packages/opencode/src/cli/cmd/tui/context/local.tsx - Source of the issue
  • packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx - 6 consumer locations
  • packages/opencode/src/cli/cmd/tui/component/dialog-agent.tsx - 1 consumer location

Changes

local.tsx

  • Line 41: agents()[0].nameagents()[0]?.name ?? "" for safe initialization
  • Line 57: Removed non-null assertion ! from current() return
  • Line 74: Added null check before setting agent store
  • Lines 181-185, 230, 256, 275, 370: Added null guards before accessing agent.current()

prompt/index.tsx

  • Added optional chaining with "build" fallback at all 6 locations:
    • Shell handler (line 537)
    • Command handler (line 558)
    • Prompt handler (line 574)
    • Highlight memo (line 694)
    • Spinner definition (line 705)
    • JSX display (line 936)

dialog-agent.tsx

  • Added optional chaining with empty string fallback

Testing

The fix adds defensive null checks that gracefully handle edge cases without changing normal behavior when agents are properly loaded.

peremajoral avatar Jan 10 '26 19:01 peremajoral