agent: Fix race condition when loading threads (#51366)

This fixes a race condition that could occur when using the sidebar:
`Failed to launch: project state not found`

We were accessing/creating the project state before an await point,
meaning that we could remove the state if session/close was called in
the meantime.

- [x] Added a solid test coverage and/or screenshots from doing manual
testing
- [x] Done a self-review taking into account security and performance
aspects
- [x] Aligned any UI changes with the [UI
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)

Release Notes:

- N/A
eeb034c31cBennet Bo Fenner committed on 3/12/2026, 9:38:54 AM· committed by GitHubparent4d5e25f
1 file changedLine totals unavailable