agent_ui: Persist the most recently selected agent, not whichever write finishes last (#61147)
# Objective
`test_select_agent_action_updates_visible_draft` (added in #60452) is
order-dependent: it passes at seed 0, which CI runs, but fails on others
(e.g. 7, 11). Any unrelated change that schedules one extra task shifts
the deterministic test scheduler enough to flip it at seed 0 too — which
is how it surfaced, while working on #61009. The bug it exposes is real
and pre-existing (same class as #61142):
`set_selected_agent_and_persist` fires a detached write of the global
last-used agent each time it's called. Activating the draft persists the
default agent while selecting one persists the choice, and with several
writes in flight the one that finishes last wins — so the default can
clobber the user's selection, which then loses it on the next launch.
## Solution
Chain the writes so they land in call order and the most recent
selection is the one left persisted.
## Testing
- `test_select_agent_action_updates_visible_draft` now runs 30 seeds to
cover both orderings.
## Self-Review Checklist:
- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content adheres to Zed's UI standards
([UX/UI](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
and
[icon](https://github.com/zed-industries/zed/blob/main/crates/icons/README.md)
guidelines)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable
Release Notes:
- Fixed the agent panel occasionally forgetting the selected agent
---------
Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
91ebd505b9Toru Nayuki committed on 8/25/2026, 9:17:23 AM· committed by GitHubparent41d72f6