# agent: Flush thread content to database on app quit \(\#58962\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 620ceaaaca40b346736660f12eefce38e235cb59

Requested commit: 620ceaaaca40b346736660f12eefce38e235cb59

Commit: 620ceaaaca40b346736660f12eefce38e235cb59

Tree: 5efc2d2325cdf8057a3a98b4e32453861f8f9ecc

Author: MartinYe1234

Committer: GitHub

## Message

```
agent: Flush thread content to database on app quit (#58962)

After prompting an agent, no response was received and only a spinner
was shown. After updating and reopening Zed, the thread still appeared
in the sidebar but failed to restore with `Failed to Launch` / `no
thread found with ID: SessionId(...)`, and the original prompt was lost.

## Root cause

A native agent thread is tracked by two independent persistence layers:

- **Sidebar metadata** (`ThreadMetadataStore`) and the **serialized
agent panel** record the thread's `session_id` through fast, separate
write paths.
- **Thread content** (`ThreadsDatabase`) is written by a per-session
async task (`NativeAgent::save_thread`) that can still be in flight when
the process exits.

When a graceful quit or update restart raced that async content save,
the metadata/serialized session id was persisted but the content row was
not. On restore, the metadata gate passes, then `load_thread` finds no
content row and hard-fails with `no thread found with ID`.

## Fix

Register an `on_app_quit` handler on `NativeAgent` that synchronously
commits every newly created non-empty thread's content to
`ThreadsDatabase` during shutdown, keeping the two stores consistent
across a graceful quit/restart (the auto-updater's path).

Closes AI-374

Release Notes:

- Fixed agent threads failing to restore after quitting or updating Zed
while a response was still in progress
```

## Parents

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

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