[PR #7689] fix(tui): handle undefined agent.current() to prevent fatal TypeError #12476

Open
opened 2026-02-16 18:17:23 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7689

State: open
Merged: No


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.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7689 **State:** open **Merged:** No --- ## 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].name` → `agents()[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.
yindo added the pull-request label 2026-02-16 18:17:23 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12476