Repository navigation
context governance eviction
Part of Context Governance: the persistence floor at every boundary, the three eviction paths, the
read_fileslice, media governance, the overflow tail clip, and chain-summary filtering.
agent/middlewares/message_persistence/core.py (MessagePersistenceMiddleware) is the persistence floor the whole page rests on. It writes at two timings:
| Message | Persisted at |
|---|---|
ToolMessage |
the moment the tool handler returns (wrap_tool_call / awrap_tool_call) |
HumanMessage |
the turn's first model boundary (after_model / aafter_model) |
AIMessage (including its tool_calls) |
the boundary right after the model produced it |
HITL denial (status="error" ToolMessage) |
the next model boundary — HITL wraps this middleware from the outside, so its short-circuit never reaches the flush |
Both timings share one batch pipeline: role/marker filter → watermark → HITL-denial re-pairing + tool-result dedup → write → mark. Details that matter:
-
persisted_message_idswatermark (write-once). Before writing,filter_persisted_message_idsdrops candidates whose lookup keys are already tombstoned for the session; after writing,mark_message_ids_persistedtombstones every candidate handed to the writer. The watermark is a SQLite table (context_engine/store/db.py), so it survives restarts. -
id + fingerprint lookup. A tool result is persisted on return before the graph reducer assigns it an id; the watermark lookup therefore checks both the LangGraph message id and a
sha1:content fingerprint (role + content +tool_call_id). The next boundary, and a post-restart replay, both match the same logical message on one of the two keys (_watermark_lookup_keysinprepare.py). -
Summary pairs are never persisted.
_is_persistabledrops every message taggedadditional_kwargs["lc_source"] == "summarization"(the AI half of the pair), andHumanMessageRowBuilder.build(context_engine/store/core.py) returnsNonefor the human half. Compression artifacts are prompt scaffolding, not conversation history. -
Fail-open. A missing
session_idskips silently; a writer error is logged and NOT tombstoned, so the same batch is retried at the next boundary. Persistence can never break a turn or a tool result.
This is why the rest of the page can be aggressive: every payload is already durable before any later stage shrinks it.
ContextEvictionMiddleware.wrap_tool_call intercepts the tool response before it reaches graph state (agent/middlewares/context_eviction/core.py; primitives in pub/func/message/eviction.py):
- A generic result whose extracted text is over
evict_threshold_chars(20 000 chars) is written toSESSIONS_DIR/<session_id>/evicted/<tool_call_id>_<md5[:8]>.txt, and the message content is replaced by a head/tail preview carrying the file path. -
Child mode uses the SAME configuration. A subagent registers the identical middleware (the old trimmed flags are gone): over the same threshold the result is written to the CHILD's own
evicted/directory and replaced by the usual head/tail preview. That directory is inside the session's privateSESSIONS_DIRtree, which the session may read/write without HITL, so the pointer is recoverable. The human-message switch is shared too, but it only matchesorigin="user"— which child task/continuation messages never carry. A failed offload falls back to an in-memory[output truncated: …]clip, so an oversized payload never reaches state raw. -
excluded_tools(8 names —read_file,write_file,patch_file,search_files,list_files,memory,skill_view,skill_list) pass through untouched: their payload already lives on the backend filesystem or is cheap to recover.read_fileadditionally takes the slice path below. - The replacement is built with
ToolMessage.model_copy, so the messageid,tool_call_id,name,status, andadditional_kwargssurvive — pairing, sanitizer, and watermark all keep matching the same logical message. - Multimodal non-text blocks (images / audio / video) are preserved verbatim; only the text block is replaced.
- Safety guards: an unsafe
session_id(empty /./../ separator) skips without touching disk; a message already carrying the[evicted to: …]marker is never evicted twice; a preview that would not be smaller than the original is skipped (with the capped fallback below, that only happens for a payload barely over the threshold). -
A payload the line window cannot shrink falls back to a CHAR-budgeted preview. The plain window takes 5 head + 5 tail LINES, so a payload of ten lines or fewer — a single newline-free line (a compact JSON body) is the extreme — has overlapping windows and would be reproduced twice. The fallback caps each kept line at
_PREVIEW_LINE_MAX_CHARS(2 048) and each side at_PREVIEW_HEAD/TAIL_MAX_CHARS(8 192), the mirror of the byte budgets the file-on-disk preview (terminal's fd path) already uses: a 50 000-char single line previews to ~4 300 chars, so it is offloaded like any other oversized result instead of riding the context window in full. The same caps apply to the PREFERRED line window (it keeps whole lines but caps each at 2 048 characters and each side at 8 192), so a payload that shrinks by only a line or two can no longer hand back a near-original-size preview with one giant line riding it; a render that is not smaller than the payload at all leaves the original message untouched (the caller's final net). -
A mid-line cut is called out, with the tools that can read past it. A preview clips an over-long line silently (the in-memory renderer appends
\u2026, the file-on-disk reader stops the chunk at_PREVIEW_LINE_MAX_BYTES), and a clipped line is indistinguishable from a normal line ending. Both previews therefore append a note when a shown line was cut in the middle — including the tail window's first line, a fragment whenever the backwards walk stopped inside a line — saying thatread_filepages by LINE and cannot read that line's remainder, and namingsearch_files/terminal/python_replas the tools that can. A normal preview keeps its exact previous shape (the note is absent, not empty).
Preview format (pub/func/message/eviction.py, preview_head_lines = preview_tail_lines = 5):
[evicted to: <path>]
--- head (5 lines) ---
<first 5 lines>
...
--- tail (5 lines) ---
<last 5 lines>
[full content: N chars, evicted at <ts>]
Use read_file(file_path='<path>', offset=0, limit=100) to read the full content in chunks.]
The three stores — where each copy lives after eviction:
| Where | What it holds |
|---|---|
| graph state / checkpointer / next model call | the preview only |
MesMemory (messages table) |
the full text, written by the inner persistence flush the moment the tool returned |
SESSIONS_DIR/<session_id>/evicted/ |
a byte-identical copy; load_evicted() / read_file retrieves it |
The ordering that makes this work: in the wrap_tool_call chain, MessagePersistenceMiddleware is innermost and ContextEvictionMiddleware sits outside it, so the raw result is flushed first and only the preview travels on to state. The watermark is covered either way: model_copy carries the inner flush's in-process _db_persisted marker, and when the raw write succeeded the replacement's watermark key is additionally tombstoned (_cover_with_watermark), which covers a restart where the marker is lost and the preview's fingerprint no longer matches the raw one. When the raw write failed, nothing is tombstoned and the next boundary retries with the preview content.
read_file is the FIRST cut, before any middleware sees the result: the window it returns is bounded by BOTH TOOLS_TIMEOUTS["read_file_token_budget"] (4 000, times read_file_budget_ratio 0.85) and the caller's line limit — the stricter wins. The budget is CJK-aware (CJK ≈ 2 chars/token, ASCII ≈ 4 — the ratios token_truncation uses), so the same number means the same context cost in every language: 2 000 lines of Chinese used to cost ~2.5× what 2 000 lines of English did. Truncation is always at a LINE boundary (numbering stays continuous), and a line longer than the whole page budget — a minified bundle — is clipped inside itself with a ...[N chars clipped]... marker and reported truncated: true, so it grants no write license. That per-line ceiling is read_file_max_line_tokens (4 000) and it applies WHEREVER the line sits in the page and even when the page budget is switched off — one minified line can never ride a response whole. Because offset counts LINES, read_file cannot page inside a clipped line: the hint names the tools that can (search_files with context, or terminal / python_repl). The line limits come from the registry (file_tools_read_default_limit / file_tools_read_max_limit, no longer hardcoded), and a budget of 0 restores the line-count window alone (the per-line ceiling stays). A re-read of a file this session READ in full (the license exists, provenance read) and that has not changed answers with a pointer (unchanged: true) instead of the content — the model's own conversation already holds it; a ranged read (offset ≥ 2) always returns content.
Because the tool budgets itself, the compression-time clip below is a FALLBACK: it skips a payload that already says truncated: true (the tool's page and its continuation hint are what the model needs) and still clips a truncated: false result that is merely long — a complete read can be up to the budget, which is larger than the middleware's own clip budget.
read_file results are not offloaded — the file already lives on disk, so writing a second copy would be pure duplication. slice_read_file_result (pub/func/message/eviction.py) instead replaces the content with the first _READ_FILE_SLICE_CHARS (4 000) characters plus a recovery notice ("...[Output was truncated due to eviction threshold. Use read_file with offset and limit to retrieve specific portions.]"). No eviction file is written, and the helper is idempotent: an already-sliced result (notice present) is returned untouched. A page the tool already budgeted itself (truncated: true) is skipped: its window is the intended size and its own hint is the recovery path.
This is the execution-time half of a two-stage reduction. The compression-time half lives in pub/func/message/target_truncation.py::_truncate_read_file_content: when compression clips context, it resolves each ToolMessage back to its read_file call by tool_call_id and keeps head 30 % + tail 30 % of max_tool_output_tokens (500 — ~2 000 ASCII characters, the CJK-aware equivalent), replacing the middle with a recovery notice that carries the parser-derived, 1-based continuation offset (Use offset=<N> to continue reading…; falls back to "re-read from the start" when the payload does not parse).
The two stages are complementary by construction: when compression later clips an execution-sliced payload, the truncated JSON no longer parses, so the compression notice deterministically falls back to the restart form — no incorrect offset is ever emitted, and the execution-time slice helper never re-slices its own output.
terminal writes its child's stdout straight to disk instead of into a pipe: both spawn sites hand the process a FILE (.sherry-tmp-stdout-*.log in the session directory, stderr merged into it) and wait for it, so an oversized payload never rides the parent's heap. Under TOOLS_TIMEOUTS["fd_write_threshold_bytes"] (100 000 bytes) the file is read back whole and deleted; at or above it the file is renamed into evicted/terminal-<sha256(first 64 KiB)[:16]>.log and only a byte-bounded head/tail preview (build_file_preview: 8 KiB per side, the total is stat().st_size BYTES — lines are never counted) reaches the model. Timeout / cancel / failure delete their temp file; hard-kill leftovers are swept by the shared .sherry-tmp- + .log contract in atomic_write.py.
That shape — the whole stdout is bulk text the parent only previews — is what decides whether fd-direct-write applies anywhere else. Judged against it, tool by tool:
| Path | stdout shape | What bounds it | fd-direct-write? |
|---|---|---|---|
terminal |
raw bulk text, never parsed | the file + the eviction preview (or a RETAINED file when run_in_background) |
✅ implemented |
python_repl |
an envelope FILE pair (stdout envelope + stderr fallback) | the file is read back after wait(); a killed grandchild can no longer hold a pipe open |
✅ fd-direct-write (two temp files; the timeout path uses the bounded reap_process) |
execute_code (PTC) |
JSON envelope pair; real tool calls ride the loopback RPC bridge | the files are read back after wait(); parent-side _truncate(..., max_stdout) still caps what the model sees |
✅ fd-direct-write (two temp files; reap_process on timeout, process group killed) |
search_files (ripgrep) |
streaming match lines, parsed as they arrive | per-call caps + ProcessWatchdog early kill |
❌ would REGRESS: with a file the parent cannot stop the search at its cap — rg would run to completion |
| ast-grep | JSON records |
ast_grep_max_matches / path / pattern caps, far below any threshold |
❌ already bounded |
| LSP client | JSON-RPC frames | the protocol itself | ❌ not a text payload |
web_search / RAG |
HTTP responses formatted per result | provider + per-result caps | ❌ no subprocess involved |
| middleware eviction (P0-2 / P1-9 / P2-4) | messages already in memory | the middleware's own thresholds | ➖ not a producer: it runs AFTER the tool, on data already on the heap |
The rule the table encodes: fd-direct-write needs a subprocess whose entire stdout is bulk text or a payload read back after exit — nothing the parent must drain to EOF while the child (or its descendants) run. A pipe that must be read INCREMENTALLY or cut short mid-stream (ripgrep's early kill) is the correct design there.
A user can paste a payload no tool produced: logs, documents, transcripts, whole codebases. ContextEvictionMiddleware ports DeepAgents' human-message eviction with a Sherry-specific split.
-
Trigger (
before_model/abefore_model):human_evict_enabledAND the last message is aHumanMessageAND it is real user input (is_user_message:metadata.origin == "user") AND it carries nolc_evicted_toAND its extracted text is overhuman_evict_threshold_chars(200 000 chars). Only the last message is ever examined, so a past user turn is never revisited. Injected carriers — system reminders, workspace notices, task-intent/continuation directives, subagent completion carriers — share the HumanMessage role but carry another origin or provenance; the positive origin check is fail-closed, so they are never tagged or previewed. -
Tag + offload (
evict_human_message): the full text is written toSESSIONS_DIR/<session_id>/evicted/human-<msg_id|timestamp>.md, then the hook returns a partial state update{"messages": [tagged]}wheretaggedhas the same id and content and only addsadditional_kwargs["lc_evicted_to"]. The standardadd_messagesreducer updates the message in place by id — no message-list rewrite, noDeltaChanneldependency, no prefix-cache invalidation from the state side. The file is written before the tag is produced, so a failed write never yields a dangling pointer. -
Model view (
wrap_model_call/awrap_model_call): every USERHumanMessagecarryinglc_evicted_tois replaced in the request only (request.override(messages=...)) by a preview built from the state text: eviction path, head/tail 5 lines each, and aread_filerecovery notice. Non-text blocks (images / audio / video) are preserved verbatim (_build_evicted_content) — media is never offloaded into a text file. -
Self-heal: if the eviction file is missing (session dir pruned, disk issue),
_heal_eviction_filerewrites it from the state text at the next model call — but only when the target is the session's ownevicted/directory; a foreign path in the tag is refused with a warning.
| Where | Tool results (P0-2) | Human messages (P1-9) |
|---|---|---|
| graph state / checkpointer | preview only |
full text + the lc_evicted_to tag |
MesMemory (messages table) |
full text | full text (the tag does not filter persistence) |
| next model call (request view only) | preview | preview (path + read_file hint) |
SESSIONS_DIR/<session_id>/evicted/ |
byte-identical copy | byte-identical copy |
The asymmetry follows from when the message is persisted. A tool result is flushed by the inner persistence layer at tool return, so state can safely hold the preview. A human message is persisted at the turn's first after_model boundary — if state held only the preview, MesMemory would archive the preview and both message_search and compression would lose the real text. So state keeps the full text and only the request view is truncated. Because the message id never changes, the watermark is untouched and no second row is written.
Media is governed on the way in and on the way out: entry-side rules keep unprocessable payloads out of the request and stop an unusable model from being probed twice, while compression and token estimation never treat base64 as text.
Input size cap. Every inbound media payload — an inline base64 / data: block or a remote URL download — is measured against MEDIA_PIPELINE["max_media_bytes"] (20 MiB) before any disk write. A payload over the cap is skipped: nothing is written, no path enters MediaPaths, a warning logs the byte count, and the message carries a model-visible [Uploaded media] line saying the attachment was not saved and was not sent to the model. A remote URL is judged by Content-Length when the server declares one and otherwise by a read capped at limit + 1 bytes, so a wrong header can never force an oversized write; the same gate covers the image, audio and video handlers (media_handlers.py::_exceeds_media_limit / _record_oversize). A payload exactly at the limit is allowed.
Per-request capability scrub. MultimodalProcessor.wrap_model_call (auto mode) rebuilds the request copy only — request.override(messages=...) — replacing every media block whose family the serving model is cached as "unsupported" with a text placeholder that names the block type, its recorded on-disk path and the matching builtin skill (image_to_text / speech_to_text / video_text_to_text). Supported and unprobed blocks pass through verbatim, so a mixed message keeps native media for the supported families and only the unsupported ones are stripped — block by block. Tool results are scrubbed by the same rule: an image a tool attached natively (a browser screenshot) is replaced by a text pointer to its saved path when the model is cached as image-blind. State, checkpointer and MesMemory are never written; when no block needs replacement the original request object is returned. The scrub runs only under "auto": "true" keeps every block for the model, "false" takes the skill path before the request is assembled.
Silent-degradation detection. A model can accept media blocks and answer as if it never saw them. On the success path of a call that started as an auto-mode native attempt, LLMRetryMiddleware evaluates the reply with detect_media_blindness() (media_pipeline/degradation.py) — a pure regex, en / zh / ja / ko, precision-first: a blindness phrase counts only when a media word sits within ±40 characters — plus an explicit "please describe the media" request pattern. On a hit every media family present in the request is cached "unsupported", so later turns go straight to the skill path; the per-turn native flag is cleared either way. main_llm_silent_degradation_detection (True) is the master switch. Attribution follows the serving model: when LLMRetryMiddleware re-binds the request to a sticky fallback candidate it first rewrites the per-turn native-model key to {candidate.provider}/{candidate.model_name}, so an error rejection and a silent one are both cached against the model that actually served the call.
Capability cache. All three entry-side behaviors share one process-level cache (agent/middlewares/llm_capability_cache.py), keyed "{provider}/{model_name}" with per-family values "auto" (untested) / "supported" / "unsupported". It lives for the process only: a restart starts clean (at most one wasted native probe), and a model switch — env change plus restart — is naturally a new key.
Compression-time offload. Before _apply_compression serializes the summarized prefix (current_messages[:cutoff]), offload_inline_media (agent/middlewares/summarization/media_offload.py) rewrites every inline media block in that range only:
- a
data:URL / base64 payload (image_url/audio_url/video_url, a barebase64field,audio_bytes/video_bytes, or an Anthropic-stylesource.data) is decoded and written toSESSIONS_DIR/<session_id>/media/{sha256(raw)[:16]}{ext}; the extension comes from the magic bytes viamedia_handlers._infer_extension; - identical bytes are written once — the content-hash filename deduplicates within and across compactions;
- the block becomes the text pointer
[evicted to: <path>], the same marker the P0-2 / P1-9 eviction paths emit, so_collect_evicted_refscollects it andSummaryDoc.evicted_refscarries the path onto the summary chain (rendered as Evicted References); - a block that cannot be decoded or written becomes
<media error="failed_to_offload" />— fail-open, never a crash.
The preserved tail window keeps its media. Both compression paths call the offload on the prefix slice — _apply_compression_under_lock (sync) and _aapply_compression_under_lock (async). The summary prompt carries the media-reference rules: preserve the pointers verbatim, do not invent visual / audio / video details, retrieve the payload from the path. Media files live under the session tree, so clear_session() removes them with evicted/ and plans/.
Token classing. pub/func/estimate_tokens.py counts a content list block by block: text blocks as text, media blocks at a fixed per-type cost from TOKEN_ESTIMATION (tokens_per_image_block = 85 — aligned with langchain_core.count_tokens_approximately; tokens_per_audio_block = 256; tokens_per_video_block = 1024; tokens_per_unknown_block = 85 for unrecognised blocks, including ones hiding a data: payload). Base64 is never serialized and never counted as text: the removed JSON path read 5 MB of base64 as ~1.25M tokens, which would fire compression on a single image. str content, None, and pure-text lists keep their text estimates.
The newest tool outputs are usually the largest context consumers and the most expendable — and every one of them is already persisted. pub/func/message/overflow_clip.py::clip_overflow_tail turns that into the first, zero-LLM move on every overflow path:
-
Before the routes.
Summarization._dispatch_overflow_route(T1/T2/T3) runs the clip for every non-fitsroute before dispatching;_forced_recovery_request(T4/T5) runs it before the forced compression step. When the clip ALONE drops the estimate below the line, the request returns with the stubbed list and the route never executes — no budget truncation, no auxiliary-LLM call. -
How it clips. It walks the trailing contiguous batch of
ToolMessages (stopping at the first non-ToolMessageor already-stubbed message) and replaces each content with a compact stub viamodel_copy. Nothing is removed, reordered, or injected:id,tool_call_id,name, andadditional_kwargssurvive, so tool-call/result pairing and the persistence watermark stay intact. -
Markers survive. A stubbed message re-emits the P0-2
[evicted to: …]pointer (plus aread_filehint) and the P2-4 slice notice verbatim, so recovery paths keep working after a clip. - Idempotent. A stub ends the scannable batch, so a second pass is a no-op — the T4/T5 retry budget cannot be burned on identical clips, and the next attempt degrades to compression.
-
Bounded.
overflow_clip_max_remove(10) caps how many messages one clip may stub;overflow_clip_min_keep(5) disables clipping on short transcripts;overflow_clip_enabledis the master switch.
| Situation | What runs | LLM calls |
|---|---|---|
| Clip alone drops the estimate below the threshold | stubbed list returned; route never executes | 0 |
| Clip insufficient (or disabled / no eligible batch) | clip result discarded; the existing route runs on the exact original list | route-dependent (compact routes call the auxiliary LLM) |
| T4/T5 provider error, first recovery attempt | clip first; if sufficient, retry the provider call with the stubbed list | 0 |
| T4/T5 provider error, clip insufficient | forced compact + budget truncation, then retry | 1 auxiliary-LLM call per compact step (≤ MAX_OVERFLOW_RETRIES = 3) |
Acceptance is strict: the clip is applied only when it alone brings the pure-local estimate under the line (estimate_messages_tokens(messages, reported_tokens=0) — a stale usage_metadata never drives recovery). An insufficient clip is discarded so the existing route operates on the original list.
A compression replaces history with a HumanMessage / AIMessage pair tagged lc_source="summarization" (the AI half carries the summary inside <summary> tags). On the next compression, feeding that old summary back into the serialized <conversation> would waste tokens and confuse the summarizer.
agent/middlewares/summarization/core.py handles this in two steps:
-
_extract_previous_summarypulls the previous summary text out of the transcript (searching for the newest taggedAIMessage, then a taggedHumanMessage). -
_filter_summary_messagesremoves everylc_source="summarization"message from the list that gets serialized into the new prompt._build_summary_promptinjects the extracted text separately as<prior-summary>, next to the update instructions; the model is told to construct one combined summary and that the prior summary is discarded after this.
The pair itself also never reaches MesMemory (see Persistence) — raw history stays raw.
Generated from the repository's wiki/ tree. Source