[PR #11100] fix(tui): handle race condition when agents array is empty during initialization #13656

Closed
opened 2026-02-16 18:18:29 -05:00 by yindo · 0 comments
Owner

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

State: closed
Merged: No


Summary

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

This is a widespread issue (13+ duplicate issues reported) that occurs when the app starts before agents are fetched from the API.

Root Cause

Race condition between SyncProvider and LocalProvider initialization:

  1. SyncProvider starts with empty agent: []
  2. LocalProvider initializes immediately and tries to access agents()[0].name
  3. agents() returns [], so agents()[0] is undefined
  4. Accessing .name on undefined throws the TypeError

Changes

local.tsx

  • Changed agentStore.current type to string | undefined
  • Use optional chaining (agents()[0]?.name) for initialization
  • Updated current() to return undefined when no agents available
  • Added null checks in move(), cycle(), cycleFavorite(), set(), and createEffect

prompt/index.tsx

  • Added null check for agent.current() in submit() function
  • Updated createMemo functions to handle undefined agent

dialog-agent.tsx

  • Use optional chaining for agent.current()?.name

Test Plan

  • Start the application without any agents configured
  • Verify the app doesn't crash with the TypeError
  • Verify agents are loaded correctly once the API responds
  • Verify normal operation continues after agents are loaded

Fixes #11096

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11100 **State:** closed **Merged:** No --- ## Summary Fixes TypeError: undefined is not an object (evaluating 'local.agent.current().name') This is a widespread issue (13+ duplicate issues reported) that occurs when the app starts before agents are fetched from the API. ## Root Cause Race condition between `SyncProvider` and `LocalProvider` initialization: 1. `SyncProvider` starts with empty `agent: []` 2. `LocalProvider` initializes immediately and tries to access `agents()[0].name` 3. `agents()` returns `[]`, so `agents()[0]` is `undefined` 4. Accessing `.name` on `undefined` throws the TypeError ## Changes ### local.tsx - Changed `agentStore.current` type to `string | undefined` - Use optional chaining (`agents()[0]?.name`) for initialization - Updated `current()` to return `undefined` when no agents available - Added null checks in `move()`, `cycle()`, `cycleFavorite()`, `set()`, and `createEffect` ### prompt/index.tsx - Added null check for `agent.current()` in `submit()` function - Updated `createMemo` functions to handle undefined agent ### dialog-agent.tsx - Use optional chaining for `agent.current()?.name` ## Test Plan - Start the application without any agents configured - Verify the app doesn't crash with the TypeError - Verify agents are loaded correctly once the API responds - Verify normal operation continues after agents are loaded Fixes #11096
yindo added the pull-request label 2026-02-16 18:18:29 -05:00
yindo closed this issue 2026-02-16 18:18:29 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13656