Skip to content

Fix sidebar context % flickering during sub-agent transfers#2042

Merged
dgageot merged 1 commit intodocker:mainfrom
dgageot:board/the-context-is-still-flickering-a-lot-wh-dcbb263e
Mar 10, 2026
Merged

Fix sidebar context % flickering during sub-agent transfers#2042
dgageot merged 1 commit intodocker:mainfrom
dgageot:board/the-context-is-still-flickering-a-lot-wh-dcbb263e

Conversation

@dgageot
Copy link
Member

@dgageot dgageot commented Mar 10, 2026

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.

Fix

Detect stale state in currentSessionUsage() 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.

Test

Add a test that replays two sequential transfer_task round-trips and asserts contextPercent() stays correct at every step.

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
@dgageot dgageot requested a review from a team as a code owner March 10, 2026 17:28
@dgageot
Copy link
Member Author

dgageot commented Mar 10, 2026

@maxcleme does it fix your issue?

Copy link

@docker-agent docker-agent bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. A sub-agent's stream stopped
  2. The parent agent was restored (currentAgent updated)
  3. But currentSessionID still referenced the child's session
  4. 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.

Copy link
Member

@maxcleme maxcleme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems to fix my issue 👍

@dgageot dgageot merged commit a19756f into docker:main Mar 10, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants