# agent_ui: Persist the most recently selected agent, not whichever write finishes last \(\#61147\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/91ebd505b9d8b99fa8a3303698112a2e1e34aafd)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 91ebd505b9d8b99fa8a3303698112a2e1e34aafd

Requested commit: 91ebd505b9d8b99fa8a3303698112a2e1e34aafd

Commit: 91ebd505b9d8b99fa8a3303698112a2e1e34aafd

Tree: e3b73a02c8aa1138247f8e19a148e2156be95702

Author: Toru Nayuki

Committer: GitHub

## Message

```
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>
```

## Parents

- [41d72f6c1d9038f48a6e97d0f6de02f72625aaf0](https://git.cafe/gitcafe/zed/commit/41d72f6c1d9038f48a6e97d0f6de02f72625aaf0?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/91ebd505b9d8b99fa8a3303698112a2e1e34aafd?format=markdown)
