Skip to content

fix(agent): retire settled run journals so agent.db stays bounded - #891

Merged
huichen merged 1 commit into
mainfrom
fix/run-retention
Oct 1, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/run-retention

Conversation

@huichen

@huichen huichen commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

Why

run_events was effectively append-only: nothing ever deleted a settled run's rows, and ~95% of the table is per-token deltas (thinking_delta, tool_delta) that the transcript has already folded into entries/message_blocks. In the field one agent.db reached 8 GB (17.8M rows, 4 GB of payload, 96% of it from a single day), and prune_run_events - RPC, store method and tests - had no production caller.

What

  • SqliteStore::prune_settled_runs(session, keep) deletes the journals of a session's settled runs beyond the newest keep in one transaction, then reclaims the freed pages like the existing prune_events/delete paths.
  • The run task calls it before the run starts, awaited on the blocking pool, gated on !is_ephemeral, and never fatal: a failed prune leaves the bytes for the next run or the startup compaction.
  • SETTLED_RUN_JOURNALS_KEPT = 2: the run that just settled and the one before stay replayable for reconnect. The live run, running/unknown rows, undated rows, and session-scoped rows (run_id = '') are never touched.
  • Ordering uses the run markers' timestamps (started_at_ms, falling back to completed_at_ms); runs.run_sequence turned out to be NULL on the plain prompt path, which the first iteration of this fix tripped over (caught by the new end-to-end test).

Tests

  • settled_run_retention_keeps_the_newest_journals (store level): keeps the newest two, retires the older settled runs, never the undated or live rows; second pass is a no-op.
  • later_runs_retire_the_journals_of_settled_older_runs (end-to-end): four prompts; run 1's journal is gone, runs 2-4 remain replayable.
  • cargo fmt -p future-agent --check, cargo clippy -p future-agent --all-targets -- -D warnings, cargo test -p future-agent: green (2082 lib tests, all integration targets).

Docs: one caveat in docs/internals/desktop/ER.md + zh-CN twin, since the GUI's cursor replay is now bounded to recent Runs.

Every run kept its raw event journal forever, and ~95% of it is per-token
deltas the transcript has already folded into entries and message blocks.
One session's journal reached 8 GB (17.8M rows, 4 GB of payload), and
`prune_run_events` existed but had no caller.

A settled run's journal is only read while a client can still attach to
(or reconnect to) it, so each run now retires the journals of settled
runs beyond the newest two (`SETTLED_RUN_JOURNALS_KEPT`). The prune is
awaited on the blocking pool before the run starts - the run cannot race
its own cleanup - and is never fatal: a failure leaves the bytes for the
next run or the startup compaction.

Ordering uses the run markers' timestamps (`started_at_ms`, falling back
to `completed_at_ms`), because `runs.run_sequence` is NULL on the plain
prompt path; runs with no timestamp, the live run, and session-scoped
rows (empty run id) are never touched.
@huichen
huichen enabled auto-merge (squash) October 1, 2026 10:13
@huichen
huichen merged commit a9260ab into main Oct 1, 2026
12 checks passed
@huichen
huichen deleted the fix/run-retention branch October 1, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant