Keep settled sub-agent task histories in their saved files instead of in every `DelegationTasks` owner's memory

Merged
DO
23d ago
+341-72

No issue: found in an audit of why idle CLAI 2 sessions held 1.7–3 GB each (ten of them ran a 40 GB container out of memory).

DelegationTasks.opened() loaded every task saved in its directory with its full message history and kept all of them in records. CLAI uses one task directory for all conversations, so every CLAI process held every sub-agent conversation ever saved: on a 549-file, 1.3 GB directory that was 2.4 GB of strings per process, right after startup and whatever the session's own context size.

[!WARNING]
Compatibility impact: Code that reads DelegationTasks.records[task_id].messages after the task has settled now gets an empty list. Observers still get the history on the event that says the task settled. For a settled task, history(task_id) reads the saved file, so async code should call it in a worker thread.

Why this can ship in a minor release: It's a bug fix, and keeping settled histories in memory was never documented.

Migration: Read a task's history with history(task_id). To copy a settled task with its history, use replace(task, messages=owner.history(task.id)).

New public surface: none.

User-visible behavior: once a task settles, is saved and its observers are told, its history stays in its saved file. history(task_id) reads it from there, and a resume or save loads it back. Closing the owner saves only the tasks that changed, instead of rewriting every file. In CLAI, the /tasks view and the web transcript read a settled task's history in a worker thread, and a task's usage is kept from when it settled.

Measured by resuming the same conversation in an isolated copy of the real state:

Task directoryRSS
empty282 MB
real (548 tasks), main2,751 MB
real (548 tasks), this branch603 MB

Verification: test_settled_histories_stay_in_their_files covers a reopened owner holding no histories, history(), a resume, cancel/allow_resume/restore keeping the history, an in-place edit saved on close, and an untouched file left alone on close. CLAI's test_a_settled_task_shows_the_history_and_usage_its_owner_saved covers the /tasks transcript and usage. Each assertion fails with its half of the fix reverted.

What changes for existing users: CLAI sessions start and stay ~2.1 GB smaller on a large task directory. Harness users who read settled histories from records should switch to history().

PR decisions

2026-11-12 · pre-push review 1 of 3 at c17880fd378bbc28eb9ca07eda72d97bd0905767 · iter -

2026-11-12 · pre-push review 1 of 3 at c17880fd378bbc28eb9ca07eda72d97bd0905767 · iter -

2026-11-12 · pre-push review 2 of 3 at 5e531d026d · iter -

2026-11-12 · pre-push review 2 of 3 at 5e531d026d · iter -

  • Decision: remediated: closed-owner restore() of a task without history keeps the saved one (f4e4031721); dismissed: swapped-in record from another run inheriting the saved history
  • Why: Review-1 findings verified fixed. Dismissal: history belongs to the task id, the rule resume relies on (run_id rotates before the acceptance save); the case needs direct mutation of records
  • Source: https://github.com/pydantic/pydantic-ai/tree/clai2-idle-memory
  • Session: 3c952642-1824-455b-9e24-2786bcd67c8d

2026-11-12 · pre-push review 3 of 3 at f4e403172137f2827fe80b6cdc2e523664e64bc6 · iter -

2026-11-12 · pre-push review 3 of 3 at f4e403172137f2827fe80b6cdc2e523664e64bc6 · iter -

2026-11-12 · CI fix after review budget at 9e6733a0004496408bef3f7041278a7a0d23a2d7 · iter -

  • Decision: pushed without a 4th review (budget exhausted): web-worker tests read via history(); harness tests and /tasks read settled history in a worker thread; history() documented as a file read
  • Why: CI run 43133918936: blockbuster flagged sync history() file read on the loop; 2 clai2 tests read released messages. Local: 46+52+2 pass, pyright clean
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43133918936
  • Session: 3c952642-1824-455b-9e24-2786bcd67c8d

2026-11-12 · CI fix after review budget at 4b763f5255049e4607f469c264df2920447b3f46 · iter -

  • Decision: observers are told a task settled with its history, then it is released; clai2 caches settled usage then; web transcript async + off-loop read; TaskView.history optional (API gate)
  • Why: CI 43139364130: clai2 3.13 web-worker tests hung (BlockingError in observer swallowed), quality checks flagged required TaskView param. Reproduced on 3.13 with BLOCKBUSTER_ENABLED; all task/web tests pass there now
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43139364130
  • Session: 3c952642-1824-455b-9e24-2786bcd67c8d

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code