Expand description
Automatic context rollover for subagent workers.
A worker is one brief followed by hundreds of assistant / tool_result
rounds, and before this its context only ever grew: every round re-read the
whole history at the cache-read price, and a worker that reached the window
died on a terminal “prompt is too long”. The foreground rollover
(continuation) does not fit workers: its successor keeps user requests
(a worker has one), it seals evicted history into the session’s memory
backend, and it needs a frontend to acknowledge the new head.
This is the worker’s host-only variant. The decision is the foreground’s
policy engine unchanged (assess_context, ContextThresholds,
target_tokens, successor_budget); only the successor and where evicted
history goes differ:
- the successor keeps the brief (message 0) verbatim, later orchestrator instructions verbatim (within a share of the target), a host-written note, and the most recent rounds as a contiguous suffix cut at an assistant message that no tool call/result group straddles. Assistant messages are never edited, so signed thinking blocks travel verbatim;
- the evicted messages are written as a plain-text transcript into the
worker’s archive directory (
<handle>.window-<N>.md, 0600), the same place, permissions and retention as the finalize archive. The worker can grep/read it; nothing goes to the memory backend.
See docs/design/subagent-rollover.md.
Structs§
- Rollover
Plan - A rollover ready to apply: write
transcripttotranscript_path, then replace the history withsuccessor. - Transcript
Ref - An earlier window’s transcript, as recorded in the note’s metadata.
- Worker
Context - A worker runtime’s rollover wiring. Active only with both halves: config on, and a sink to preserve what is evicted. Without a sink nothing is evicted (nothing may be lost), so the worker behaves as before.
- Worker
Context Settings - What config says about worker rollover. Set on every worker runtime by
apply_subagent_runtime_policy; inert until a transcript sink is attached. - Worker
Policy - A resolved worker policy for one model window.
- Worker
Rollover Report - What one rollover did (observer, notice, status).
- Worker
Transcript Sink - Where one worker’s evicted history goes. Attached per spawn: the archive directory and the worker’s handle id.
Functions§
- carried_
marker - The host metadata to carry onto a message that replaces a rolled history
(cold resume), so later rollovers keep numbering and listing earlier
windows.
Nonewhenhistorynever rolled over. - hard_
capacity_ budget - At hard capacity the configured target may leave the successor little room
to grow (a window whose reserves put hard capacity below the rollover band
reaches it first; see
WorkerPolicy::hard_capacity_has_roomfor one that cannot hold the target at all). Aim for two thirds of the history room the next round leaves, so the successor both fits and can grow. - plan_
is_ productive - Whether
planis worth applying: it must shrink the history estimate by more than [MIN_SHRINK_TOKENS] and keep every tool call paired as it was. - plan_
rollover - Plan a rollover of
messagesintobudget(message tokens, estimator units).transcript_path(window)names the file the evicted window is saved to.Nonewhen nothing can be evicted (no assistant round before the last one, or no valid cut); the caller treats that as no-shrink. - render_
transcript - Plain text of every evicted message, in order, for grep and ranged read. Thinking is omitted (private reasoning; signed blocks cannot be re-sent out of their turn anyway); images and documents are named only.
- roll_
over - Apply one worker rollover: plan it, refuse one that cannot shrink the
context or would start where the next round triggers another rollover
(the pressure band below hard capacity, the hard-capacity line at it),
save the evicted messages, and return the successor with its report.
hard_capacityisSome(line)for a rollover at hard capacity, wherelineis the footprint at which the next round is at hard capacity (context_window - required_next_round).Ok(None)= no-shrink (the caller defers like the foreground’s unproductive rollover). An error writing the transcript means no rollover: nothing is evicted that was not saved. - transcript_
path <dir>/<stem>.window-<window>.md.- unpaired_
tool_ ids - Tool ids with a call but no result after it, or a result with no call before it. A valid history has none; a successor must add none.
- window_
state - The current window (1 before any rollover) and every recorded earlier
transcript, from the host metadata on
messages: the rollover note, or the single message of a cold resume (which carries it on message 0).
Type Aliases§
- Rollover
Observer - Called after every successful worker rollover (the spawn path updates the worker’s state and its subagent panel line).