Skip to content

agent_ui: Replace stale draft thread when selected agent changes (#54023) (cherry-pick to preview)#54079

Closed
zed-zippy[bot] wants to merge 1 commit intov0.233.xfrom
cherry-pick-v0.233.x-a9a77f1c
Closed

agent_ui: Replace stale draft thread when selected agent changes (#54023) (cherry-pick to preview)#54079
zed-zippy[bot] wants to merge 1 commit intov0.233.xfrom
cherry-pick-v0.233.x-a9a77f1c

Conversation

@zed-zippy
Copy link
Copy Markdown
Contributor

@zed-zippy zed-zippy Bot commented Apr 16, 2026

Cherry-pick of #54023 to preview


Problem

When creating a new agent thread from the sidebar's + button (or via
the NewThread / NewThreadInGroup actions), the panel always returned
the cached draft thread — even if the user had since switched to a
different agent. This meant the new thread would use the wrong agent
(typically the default Zed Agent instead of the last-used external
agent).

The root cause was in ensure_draft(): it cached the draft and returned
it unconditionally on subsequent calls without checking whether its
agent
matched the current selected_agent.

Meanwhile, cmd-n from within the agent panel dispatched
NewExternalAgentThread, which calls create_thread() — this always
creates a fresh thread with the correct selected_agent, so it worked
fine.

Fix

ensure_draft() now compares the cached draft's agent against the
desired agent. When they differ and the draft's editor is empty, the
stale draft is discarded and a new one is created with the correct
agent.
If the draft has user-typed content, it is preserved to avoid losing
work.

Test

Added test_draft_replaced_when_selected_agent_changes which verifies:

  1. A draft created with NativeAgent gets replaced when
    selected_agent
    changes to a custom agent
  2. The replacement draft uses the correct agent
  3. Calling activate_draft again with the same agent reuses the cached
    draft (no unnecessary replacement)

The test fails without the fix (returns the stale NativeAgent draft)
and passes with it.

Release Notes:

  • N/A

)

## Problem

When creating a new agent thread from the sidebar's **+** button (or via
the `NewThread` / `NewThreadInGroup` actions), the panel always returned
the cached draft thread — even if the user had since switched to a
different agent. This meant the new thread would use the wrong agent
(typically the default Zed Agent instead of the last-used external
agent).

The root cause was in `ensure_draft()`: it cached the draft and returned
it unconditionally on subsequent calls without checking whether its
agent
matched the current `selected_agent`.

Meanwhile, **cmd-n from within the agent panel** dispatched
`NewExternalAgentThread`, which calls `create_thread()` — this always
creates a fresh thread with the correct `selected_agent`, so it worked
fine.

## Fix

`ensure_draft()` now compares the cached draft's agent against the
desired agent. When they differ and the draft's editor is empty, the
stale draft is discarded and a new one is created with the correct
agent.
If the draft has user-typed content, it is preserved to avoid losing
work.

## Test

Added `test_draft_replaced_when_selected_agent_changes` which verifies:
1. A draft created with `NativeAgent` gets replaced when
`selected_agent`
   changes to a custom agent
2. The replacement draft uses the correct agent
3. Calling `activate_draft` again with the same agent reuses the cached
   draft (no unnecessary replacement)

The test fails without the fix (returns the stale `NativeAgent` draft)
and passes with it.

Release Notes:

- N/A
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Apr 16, 2026
@zed-codeowner-coordinator zed-codeowner-coordinator Bot requested review from a team, mikayla-maki and osyvokon and removed request for a team April 16, 2026 15:14
@zed-community-bot zed-community-bot Bot added the bot Pull requests authored by a bot label Apr 16, 2026
@eholk eholk mentioned this pull request Apr 16, 2026
18 tasks
@eholk eholk closed this Apr 16, 2026
eholk added a commit that referenced this pull request Apr 16, 2026
We're doing another preview release today to get our latest parallel
agents features out there. This PR is a rollup of all the changes we
need to cherry pick.

- [x] 7a26e48 from
#54081
- [x] #54066
- [x] #53992
- [x] #54079
- [x] #54057
- [x] #54056
- [x] #54052
- [x] #53999
- [x] #54009
- [x] #53539 (already cherry
picked as 25e02cb)
- [x] #54070
- [x] #54053
- fix `run_tests.yml`: 67e92b5
- [x] #53979
- [x] #53884
- [x] #54067
- [x] #54014 (already on
branch)
- [x] #54030 (already on
branch)
- [x] #54094

Release Notes:

- N/A

---------

Co-authored-by: Lukas Wirth <me@lukaswirth.dev>
Co-authored-by: Bennet Bo Fenner <bennet@zed.dev>
Co-authored-by: Nathan Sobo <nathan@zed.dev>
Co-authored-by: Ben Brandt <benjamin.j.brandt@gmail.com>
Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
Co-authored-by: Danilo Leal <67129314+danilo-leal@users.noreply.github.com>
Co-authored-by: Neel <neel@zed.dev>
Co-authored-by: Richard Feldman <richard@zed.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot Pull requests authored by a bot cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants