fix(tui): handle race condition when agents array is empty during initialization#11100
Conversation
…tialization Fixes TypeError: undefined is not an object (evaluating 'local.agent.current().name') The issue was a race condition between SyncProvider and LocalProvider initialization: - SyncProvider starts with empty agent: [] - LocalProvider initializes immediately and tries to access agents()[0].name - agents() returns [], so agents()[0] is undefined - Accessing .name on undefined throws the TypeError Changes: 1. local.tsx: Changed agentStore.current type to string | undefined 2. local.tsx: Use optional chaining (agents()[0]?.name) for initialization 3. local.tsx: Updated current() to return undefined when no agents available 4. local.tsx: Added null checks in move(), cycle(), cycleFavorite(), set(), and createEffect 5. prompt/index.tsx: Added null check for agent.current() in submit() and createMemo functions 6. dialog-agent.tsx: Use optional chaining for agent.current()?.name This prevents the crash when the app starts before agents are fetched from the API.
|
The following comment was made by an LLM, it may be inaccurate: Potential Duplicate PRs FoundI found several related PRs that address the same underlying issue - handling undefined
These PRs all appear to be attempting to fix similar or related issues with the same TypeError. Your PR #11100 may be addressing a recurring issue that has been attempted multiple times before. It would be worth investigating whether these previous PRs were merged or if there's a reason this issue is still occurring. |
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
SyncProviderandLocalProviderinitialization:SyncProviderstarts with emptyagent: []LocalProviderinitializes immediately and tries to accessagents()[0].nameagents()returns[], soagents()[0]isundefined.nameonundefinedthrows the TypeErrorChanges
local.tsx
agentStore.currenttype tostring | undefinedagents()[0]?.name) for initializationcurrent()to returnundefinedwhen no agents availablemove(),cycle(),cycleFavorite(),set(), andcreateEffectprompt/index.tsx
agent.current()insubmit()functioncreateMemofunctions to handle undefined agentdialog-agent.tsx
agent.current()?.nameTest Plan
Fixes #11096