Fix sidebar context % flickering during sub-agent transfers#2042
Conversation
After a sub-agent's stream stops, handleTaskTransfer restores the parent agent via AgentInfo, but currentSessionID still references the child session. currentSessionUsage() used to prioritise currentSessionID unconditionally, so it returned the child's context usage instead of the parent's until the next StreamStarted event reset the session ID. Detect this stale state by checking whether the session's owning agent matches currentAgent. When they differ, skip the session-ID lookup and fall through to the agent-name search, which finds the correct parent session. Add a test that replays two sequential transfer_task round-trips and asserts contextPercent() stays correct at every step. Assisted-By: docker-agent
|
@maxcleme does it fix your issue? |
There was a problem hiding this comment.
Review Summary
🟢 APPROVE
This PR correctly fixes the sidebar context % flickering issue during sub-agent transfers. The implementation is sound and well-tested.
What This PR Fixes
The bug occurred when:
- A sub-agent's stream stopped
- The parent agent was restored (
currentAgentupdated) - But
currentSessionIDstill referenced the child's session - The OLD code would look up the child session and show incorrect context %
The fix adds a staleness check that detects when currentSessionID references a session owned by a different agent. When detected, it skips the direct session lookup and falls through to the agent-name search, which correctly finds the parent's session.
Code Quality
✅ Logic: The staleness check (owner != "" && m.currentAgent != "" && owner != m.currentAgent) correctly handles all edge cases:
- Empty owner (session not tracked) → not stale, continues normally
- Empty currentAgent → not stale, continues normally
- Matching agents → not stale, uses session lookup
✅ Testing: The new test comprehensively verifies the fix through two complete transfer round-trips and includes a scenario where the parent resumes streaming.
✅ Test Helpers: The testSidebar wrapper correctly simulates real runtime event handling, including the critical detail that StreamStopped clears workingAgent but NOT currentSessionID (matching actual production behavior).
No issues found in the changed code.
maxcleme
left a comment
There was a problem hiding this comment.
Yes, seems to fix my issue 👍
After a sub-agent's stream stops,
handleTaskTransferrestores the parent agent viaAgentInfo, butcurrentSessionIDstill references the child session.currentSessionUsage()used to prioritisecurrentSessionIDunconditionally, so it returned the child's context usage instead of the parent's until the nextStreamStartedevent reset the session ID.Fix
Detect stale state in
currentSessionUsage()by checking whether the session's owning agent matchescurrentAgent. When they differ, skip the session-ID lookup and fall through to the agent-name search, which finds the correct parent session.Test
Add a test that replays two sequential
transfer_taskround-trips and assertscontextPercent()stays correct at every step.