Skip to content

context governance

sherry-wiki edited this page Nov 17, 2026 · 13 revisions

🧭 Context Governance: Persistence, Eviction, Slices & Overflow Clip

Sherry Wiki · English · 中文 · 日本語 · 한국어

← Back to index

How raw history is kept durable and the model-visible context is kept small: write-once persistence at every model boundary and tool return, tool-result eviction to disk with a recoverable preview, execution-time read_file slicing, human-message eviction with a full-text state, media governance (an input size cap, per-request capability scrubbing, silent-degradation detection, compression-time offload, and block-wise token classing), a no-LLM overflow tail clip that runs before any compression route, and chain-summary filtering that keeps old summaries out of the conversation payload.

Every message the agent produces is valuable twice: once as raw history (what actually happened, for search and for compression) and once as model context (what fits in the window right now). This page documents the mechanisms that reconcile those two needs — they all share one rule: once a payload has entered the pipeline it is never lost, only the model view is shrunk, and every reduction leaves a pointer back to the full text.

Source of truth: agent/middlewares/context_eviction/core.py, agent/middlewares/message_persistence/core.py, agent/middlewares/message_persistence/prepare.py, pub/func/message/eviction.py, pub/func/message/overflow_clip.py, pub/func/message/target_truncation.py, pub/func/message/tool_args_truncate.py, agent/middlewares/summarization/core.py, agent/middlewares/summarization/media_offload.py, agent/middlewares/media_pipeline/scrub.py, agent/middlewares/media_pipeline/degradation.py, agent/middlewares/media_pipeline/media_handlers.py, agent/middlewares/llm_capability_cache.py, agent/middlewares/llm_retry/core.py, pub/func/estimate_tokens.py, context_engine/store/core.py, config/features/agent_side/tool_result_eviction.py, config/features/agent_side/summarization.py, config/features/agent_side/media_pipeline.py, config/features/agent_side/token_estimation.py. Every claim below was checked against that code.

Table of Contents

🎯 Overview & Pipeline

The mechanisms form one pipeline; each stage shrinks the model view a little further, and none of them destroys the raw record:

tool returns
  │  ContextEvictionMiddleware.wrap_tool_call
  │    generic tool, text > 20 000 chars → full text to evicted/, head+tail preview in state (P0-2)
  │    read_file                         → execution-time 4 000-char head slice, no file written (P2-4)
  ▼
graph state (tool results: preview only · human messages: full text + lc_evicted_to tag)
  │
  │  MessagePersistenceMiddleware
  │    wrap_tool_call  → flush the RAW result the moment the handler returns
  │    after_model     → flush new human/ai/tool messages at every model boundary
  ▼
MesMemory (full text; persisted_message_ids watermark = write-once)
  │
  │  context pressure triggers Summarization (T1–T5)
  │    1. tail clip      — no LLM: stub the trailing contiguous ToolMessage batch (P1-2)
  │    2. existing route — truncate_tool_results_only / compact_only / compact_then_truncate
  │    3. forced recovery (T4/T5 provider errors) — clip first, then compact + budget truncate, then retry
  ▼
session end → clear_session() removes the session folder (evicted/ + plans) and the watermark
Stage Mechanism LLM cost Effect on the model view
Tool return ContextEvictionMiddleware (P0-2 / P2-4) none generic > 20 000 chars → head/tail preview + file pointer; read_file → 4 000-char slice + notice
Model boundary MessagePersistenceMiddleware after_model none full text written to MesMemory (state untouched)
Human message ContextEvictionMiddleware (P1-9) none only user-origin messages: request view truncated to a preview; injected carriers untouched; state/MesMemory keep the full text
Overflow (first move) clip_overflow_tail (P1-2) none trailing tool results stubbed; message identity and pairing intact
Overflow (existing routes) summarization 4-route dispatch one auxiliary-LLM call on compact routes truncation and/or history compaction
Overflow (provider error) T4/T5 forced recovery one call per compact step clip → compact + budget truncate, retried up to 3 times
Media offload (compression) offload_inline_media none inline media in the summarized prefix → on-disk copy + [evicted to: …] pointer; preserved tail untouched

Browser pages follow the same rule. The agent-controllable browser (config/features/infra_side/browser_agent.py) contributes exactly ONE thing to this pipeline: the text (and screenshot path) the browser tool returns (legacy browser_* results replay the same way). The page itself — its DOM, its pixels, its screencast frames — never enters the message store: frames go to the panel over /browser/ws, snapshots are bounded by the manager (element / text caps) before they become tool results, and a screenshot travels to a vision-capable model as the image itself (the image_to_text skill is the fallback for a model without native vision, and the request-time history media budget drops an old screenshot only once the session's retained images exceed history_media_budget_bytes). Nothing a page displays can steer the agent except through a tool result the model itself asked for — and those results pass the untrusted-output fence and secret redaction like any other remote content (see the threat model).

🗂️ Information Sources

Everything that reaches graph state or MesMemory enters from one of the sources below. The origin column is being upgraded into a full-coverage source marker: NULL is a legacy user row written before tagging (the read side treats it as user), and a message carrying internal=True is not a user request — the summary's Unresolved list accepts only user-sent messages (positive identification). Non-message sources (eviction files, legacy plan knowledge) are listed too: they never become a messages row, and the legacy plan-knowledge directory is no longer model-injected. Rows marked planned / reserved are not implemented yet.

Information source origin / marker internal Produced when Persistence Injection behavior
Frontend WS user message origin='user' — the user sends a message in the client messages row origin='user' + full text (flushed at every boundary) resident in state/MesMemory; the model view may be evicted to a preview; the summary keeps the latest user request verbatim (latest_user_request; a multi-request list + per-request eviction pointer: planned)
Channel user message (QQ, …) origin='user' — the user sends through a channel adapter same as above same as above
TaskIntent steering / reminder origin='task_intent' True plan-active steering / task-intent arming (task_intent/core.py::_task_intent_message) messages row not a user request — excluded from the Unresolved list
Subagent completion carrier origin='subagent_completion' True a background subagent finishes and announces its result messages row (origin stamped at the persistence seam, context_engine/store/core.py) not a user request; visible to the model view
Heartbeat-triggered turn origin='heartbeat' — heartbeat-service turn (no such path today — reserved) — not a user request
Cron-triggered turn origin='cron' True a scheduled job's session turn (origin_for_source) messages row not a user request
Compression summary pair lc_source='summarization' (in additional_kwargs, not the origin column) — a compaction artifact (_build_new_messages) never persisted to MesMemory; a state summary pair <summary> resident in the model view; carried forward as <prior-summary>
Eviction file non-message — disk file — P0-2 / P1-9 eviction SESSIONS_DIR/<session_id>/evicted/ (byte-exact full text) on-demand read_file; the summary chain carries evicted_refs[] pointers in the structured summary doc
Media file non-message — disk file — upload processing (MultimodalProcessor), request-time history media budgeting (MultimodalProcessor._project_history_media_budget), or compression-time offload (offload_inline_media) SRC_DIR/<session_id>/artifacts/ (content-addressed upload copies named <family>-<sha256[:16]><ext>; identical bytes dedupe) plus SESSIONS_DIR/<session_id>/media/ (compression-time offload and non-pipeline media); a payload over max_media_bytes is never written media blocks when native; skill-path hints (artifact URIs resolved back to files) and per-request scrub placeholders carry the path; the summary chain keeps offloaded paths in evicted_refs[]
Legacy plan knowledge non-message — disk directory — retired from model-facing reads workspace/knowledge/plans/<plan_key>/ not injected; only the raw handoff-tree.json audit file remains
facts target MesMemory memory_entries rows (one global target) — SYSTEM-LEVEL / CROSS-PROJECT pitfalls and conventions: compression-time memory review + completed-plan skill review memory target (1 375-char limit; overflow archives the LEAST-ACTIVE entries) injected into every system prompt as a resident FACTS memory block

🧵 Turn Identity on Message Rows

Every message row carries two different turn identifiers:

Identifier Granularity Written where
turn_num one persist batch messages.turn_num
turn_id one conversation turn messages.turn_id and additional_kwargs["turn_id"]

_persist_batch resolves the server-owned GET_CURRENT_TURN_ID runtime hook (turn_scope) and stamps the same turn_id on every batch of one turn, so several turn_num values can share one turn_id. The hook is installed by server.service.trajectory_store.install_turn_id_hook; without a server assembly or an active scope it degrades to "", and older rows stay NULL. The (session_id, turn_id) partial index skips both values. This is the join key between messages and trajectory_events.turn_id; tests live in tests/context_engine/test_turn_id_stamp.py.

🔗 Interactions & Ordering

Ordering guarantees (verified in agent/core.py, list order = registration order):

Hook phase Order that matters here
before_agent (list order) MultimodalProcessor runs before the model loop, so P1-9 tagging always sees the final text with media hints already merged
before_model (list order) P1-9 tag runs before ToolCallNormalize / SubagentCompletionDrainMiddleware
wrap_model_call (outermost → innermost) ContextEviction (P1-9 view replacement) → … → Summarization → InvalidToolCallRepairMiddleware (innermost, closest to the LLM)
after_model (reverse order) MessagePersistenceMiddleware is the first hook after the model — the AI message is persisted before HITL strips denied tool calls or raises GraphInterrupt
wrap_tool_call (outermost → innermost) ToolGuardrails → ContextEviction → PathGuard → HeartbeatStaleness → HumanInTheLoop → MessagePersistence (innermost) — the raw result is flushed first, the preview is swapped in on the way out

The interaction map:

Interacting mechanism What happens
Summarization / compaction P1-9 keeps the full human text in state, so compression still sees it; summary pairs are filtered from persistence and from the next <conversation>
P1-2 overflow tail clip Only stubs ToolMessage contents via model_copy; it never touches HumanMessages, so the lc_evicted_to tag and the full state text survive every clip; stubs carry the P0-2 / P2-4 markers forward
Chain-summary filtering Removes lc_source="summarization" messages from the serialized conversation only; the state transcript and the MesMemory store are untouched
HITL Denials bypass the tool-return flush (HITL wraps persistence from outside) and persist at the next boundary; the persistence batch re-attaches denied tool calls so the denial keeps a paired AI row. HITL never sees HumanMessages, so eviction is orthogonal
message_search FTS5/SQLite search runs over MesMemory, where the full tool result and the full human text are archived — eviction only shrinks the model view
Prefix caching P1-9 updates the message in place by id (same content, same id) and rewrites nothing else, so the model-visible prefix is only invalidated when the preview view actually differs; tool eviction happens before the message ever enters state, so the preview is the only version the model ever sees
Tool pairing / sanitizer All replacements preserve id and tool_call_id; sanitize_tool_use_result_pairing never needs to repair an eviction, and the stubs remain valid pairing inputs
Subagent sessions Child pipelines register the SAME ContextEvictionMiddleware as the main chain, with no trimmed mode (tool results over the same 20 000-char threshold are written to the child's own evicted/ directory, which the session can read back; the shared human-message switch only matches origin="user", which child task messages never carry) but not MessagePersistenceMiddleware: child transcripts stay checkpoint-only

🧩 Provider Prompt Caching (Anthropic)

Every provider sherry supports caches automatically — except Anthropic, where nothing is cached unless the request MARKS a prefix with cache_control. The whole architecture above (a frozen system prompt, a session-stable tool face, live state pushed into the reminder layer) was built for automatic caches, so on Anthropic it produced zero hits. models/LLMs/anthropic_cache.py closes that gap: a BaseChatModel wrapper, mounted only when the provider is anthropic (the main client, the auxiliary client and the TASKFLOW_LLM / COMPRESSION_LLM families), rewrites each outgoing request with up to three breakpoints.

Breakpoint Placed on Why it is stable
BP1 the last text block of the SystemMessage frozen per session; Anthropic's prefix order is tools → system → messages, so this one also caches the tool list
BP2 the last tool definition the session's tool face (only matters after a thinking switch or an agent-config edit changes the system prompt while the tools stay put)
BP3 the last real history message (AI or tool) found when scanning back it sits BEFORE the reminder block and the new user text, so per-turn reminder churn cannot invalidate it

The invalidation points stay correct by construction. A thinking switch reloads the memory snapshot and deletes SYSTEM_PROMPT, so BP1 misses — the bytes changed, which is exactly what a byte-prefix cache should do. An agent-config edit invalidates BP1 and BP2 together. A compaction rewrites both the prompt and the message list, so BP1 and BP3 miss. And because BP3 anchors the previous turn's settled tail, a changing reminder never breaks it.

Cost. MODEL_PRICING_TABLE entries accept two optional prices, cache_read (a cached-prefix read) and cache_write (a write into the cache); TokenBudget splits one response's input_token_details into uncached / cache-read / cache-write and charges each at its own rate. Omitting the two fields prices cached tokens at the plain input rate — the pre-existing estimate, with no invented discount. OpenAI-shaped usage reports input_tokens INCLUDING the cached ones, so the middleware subtracts them before applying the uncached rate; Anthropic's input_tokens already excludes them.

Failure and scope. Injection failures, a disallowed tool conversion and SHERRY_ANTHROPIC_CACHE=0 are all pass-throughs; the caller's messages and tool list are never mutated (the request is built from copies). Scope is the native anthropic integration: Bedrock's Claude models go through langchain-aws (different cache API) and an OpenRouter-routed Claude request would have the marker dropped by the OpenAI-shaped client, so neither is claimed.

🧹 Checkpoint Retention & Orphan Session Sweep

The main and subagent graphs share src/checkpoints/sqlite.db. After a main turn, on_turn_finished prunes that session's thread to its newest top-level checkpoint plus its writes (the row aget_state reads); a child's _release_child_resources does the same for its own thread. tools:* / model:* rows are deleted; a missing top-level row keeps the newest row as a fallback. HITL-pending sessions are skipped because a suspended graph is not a turn end. Deleting a main session first suppresses and kills its descendant runs, then removes every descendant's registry row, checkpoint thread, session folder, planning rows and attachments. At boot, after init_registry(), one sweep deletes every run subtree whose root main session or parent run no longer exists. All cleanup paths are fail-open and idempotent.

📏 Compression Line & Context Usage

The compression line is an ABSOLUTE token count owned by agent/middlewares/summarization/thresholds.py: with MAIN_LLM_MAX_OUTPUT_TOKEN set it is window - max_output; unset keeps the legacy 80% behavior (the overflow router keeps its budget-relative band when unset). GET /context_usage returns max_output, compress_tokens and compress_ratio = compress_tokens / window; the ring and red dashed line use that ratio. A cap equal to or larger than the window is refused by /models-config; an old file clamps to a 0% line at runtime. compression_reserve_tokens remains a post-compaction target, not part of the line.

🛠️ Configuration

TOOL_RESULT_EVICTION (config/features/agent_side/tool_result_eviction.py):

Key Default Meaning
enabled True Master switch for tool-result eviction (P0-2)
evict_threshold_chars 20_000 Text above this many chars is evicted
preview_head_lines / preview_tail_lines 5 / 5 Preview head/tail line counts
eviction_subdir "evicted" Subdirectory under SESSIONS_DIR/<session_id>/
excluded_tools 8 names Never evicted (read_file takes the slice path instead)
human_evict_enabled True Master switch for human-message eviction (P1-9)
human_evict_threshold_chars 200_000 Human-message trigger threshold
human_preview_head_lines / human_preview_tail_lines 5 / 5 Human preview head/tail line counts

SUMMARIZATION (config/features/agent_side/summarization.py), the keys this page relies on:

Key Default Meaning
overflow_clip_enabled True P1-2 tail-clip master switch
overflow_clip_max_remove 10 Max trailing messages stubbed by one clip
overflow_clip_min_keep 5 Transcript floor: at/below this length, never clip
max_tool_output_tokens 500 Compression-time clip budget for a tool result (tokens; ~2 000 ASCII chars)
content_head_ratio / content_tail_ratio 0.3 / 0.3 Head/tail keep ratios for compression-time clips

TOKEN_ESTIMATION (config/features/agent_side/token_estimation.py), multimodal token classing:

Key Default Meaning
chars_per_token / chars_per_token_cjk 4 / 2 Text-estimate divisors (non-CJK / CJK)
tokens_per_image_block 85 Fixed cost per image block (aligned with langchain_core.count_tokens_approximately)
tokens_per_audio_block / tokens_per_video_block 256 / 1024 Conservative fixed costs (no duration metadata at estimation time)
tokens_per_unknown_block 85 Fixed cost for an unrecognised block — never its base64

MEDIA_PIPELINE (config/features/agent_side/media_pipeline.py), the input-side keys:

Key Default Meaning

Declarative capability catalog

The auto-mode decision does not start from "auto". get_capability resolves each media family in this order: the tracked config/model_capabilities.json catalog, overlaid by sherry.jsonc's model_capabilities object → the learned labels in src/data/model_capabilities_runtime.json → "auto". A declared model is answered without probing, and a runtime probe cannot override the declaration (the user override is the correction path). An undeclared model is probed once; a native success writes supported for the still-auto families and silent blindness writes unsupported, and both survive a restart. pdf is a first-class family (document / file blocks) with no skill fallback: an unsupported PDF is scrubbed to a placeholder asking for images or text.

| main_llm_native_multimodal | "auto" | Tri-state native switch: "true" keeps media blocks for the model, "false" always takes the skill path, "auto" decides per family through the capability cache; any other value fails safe to the skill path | | main_llm_silent_degradation_detection | True | Cache present families as "unsupported" when a native reply self-reports media blindness; a normal non-empty success learns "supported" for still-auto families | | max_media_bytes | 20 * 1024 * 1024 | Hard per-payload ceiling; an oversize payload is skipped before any disk write |

There are no environment variables for these knobs: they are code defaults in the feature TypedDicts above, by design.

🧪 Testing Map

Suite Covers
tests/agent/middlewares/context_eviction/test_context_eviction.py P0-2/P2-4 middleware behavior: eviction, exclusion, slice, watermark cover, fail-open
tests/agent/middlewares/context_eviction/test_human_eviction.py P1-9 tagging, in-place reducer update, model-view truncation, self-heal, media preservation
tests/agent/middlewares/message_persistence/test_message_persistence.py Boundary persistence, watermark write-once, denial re-pairing
tests/agent/middlewares/message_persistence/test_tool_result_persistence.py Tool-return flush and its id-less fingerprint / marker interaction
tests/agent/middlewares/message_persistence/test_compression_no_persistence.py The compression path writes nothing to MesMemory
tests/pub/func/message/test_eviction.py Pure eviction primitives: thresholds, previews, idempotency, unsafe session ids
tests/pub/func/message/test_read_file_slice.py P2-4 execution-time slice and its idempotency
tests/pub/func/message/test_overflow_clip.py P1-2 pure clip: trailing-batch detection, gates, token target, marker preservation
tests/agent/middlewares/test_summarization_overflow_clip.py P1-2 middleware integration: zero-LLM recovery, degradation, T4/T5, sync/async parity
tests/agent/middlewares/test_summary_message_filtering.py Chain-summary filtering and <prior-summary> injection
tests/agent/middlewares/test_compression_media_offload.py Compression-time inline-media offload: write + pointer, hash dedup, failure placeholder, preserved window untouched, sync/async, evicted_refs
tests/pub/func/test_estimate_tokens_media.py Block-wise multimodal estimation: fixed media costs, 5 MB-base64 regression, unknown-block media, str / None / empty-list edges
tests/agent/middlewares/test_multimodal_processor.py Tri-state native switch and per-request scrub: mixed families strip only unsupported blocks, supported blocks kept verbatim, state/kwargs/disk untouched, "true" never scrubbed; history media budget projection lives in media_pipeline/test_history_budget.py (fit / newest-first / protected latest user / remote URL / all dropped)
tests/agent/middlewares/test_media_size_limit.py max_media_bytes cap: oversize payloads skipped before any write, model-visible notice, exactly-at-limit allowed, declared Content-Length fast path, capped read
tests/agent/middlewares/test_media_degradation.py Silent-degradation detection: en / zh / ja / ko blindness regex + describe-request patterns, capable or unrelated replies stay clean, every present family cached, flag cleared either way, fallback-candidate attribution
tests/agent/middlewares/test_multimodal_native_fallback_e2e.py Rejection → cache + skill-path rewrite: next-session native skip, per-model-key isolation, explicit "true" never falls back
tests/context_engine/store/test_persisted_message_ids.py The persisted_message_ids watermark store
tests/full/test_context_governance_e2e.py Live-network e2e (real LLM + real graph): all mechanisms end to end — run explicitly
# Hermetic suites (CI gate)
uv run pytest tests/agent/middlewares/context_eviction \
    tests/agent/middlewares/message_persistence \
    tests/pub/func/message/test_eviction.py \
    tests/pub/func/message/test_read_file_slice.py \
    tests/pub/func/message/test_overflow_clip.py \
    tests/agent/middlewares/test_summarization_overflow_clip.py \
    tests/agent/middlewares/test_summary_message_filtering.py \
    tests/agent/middlewares/test_multimodal_processor.py \
    tests/agent/middlewares/test_media_size_limit.py \
    tests/agent/middlewares/test_media_degradation.py \
    tests/agent/middlewares/test_multimodal_native_fallback_e2e.py \
    tests/agent/middlewares/test_compression_media_offload.py \
    tests/pub/func/test_estimate_tokens_media.py \
    tests/context_engine/store/test_persisted_message_ids.py -q

# Live-network e2e — RUN EXPLICITLY, never part of the CI gate
uv run --no-sync pytest tests/full/test_context_governance_e2e.py -v

🧹 Clearing Semantics & Limitations

  • clear_session removes everything at once. server/service/session_cleanup_service.py::purge_session deletes the session's MesMemory rows (which drops the persisted_message_ids watermark with them), the checkpointer history, and the whole SESSIONS_DIR/<session_id>/ folder — so evicted/ files and plans/ are deleted together with the session (config/path.py::session_plans_dir documents the same wholesale contract). The in-memory registers are cleared last.
  • Eviction is a model-view reduction, never a deletion. Every payload this page shrinks is either archived in MesMemory, present in full in graph state (human messages), or on disk under evicted/ — and every preview carries a pointer to it. The child's own evicted/ tree is readable/writable without HITL, so its previews are recoverable too; only a failed offload falls back to an in-memory hard clip.
  • The evicted/ directory is session-owned but not garbage-collected. Files live until clear_session; there is no per-message TTL. Long sessions with many huge tool results can accumulate disk usage under workspace/sessions/<session_id>/evicted/.
  • A payload the line window cannot shrink falls back to a char-budgeted preview. With ten lines or fewer the 5-head/5-tail window overlaps and would reproduce the payload twice (a newline-free body is the extreme); the fallback caps each line (2 048 chars) and each side (8 192) instead, so a 50 000-char single line still evicts to a ~4 300-char preview. The len(preview) >= len(text) guard stays as the final net.
  • read_file slices are not recoverable from the slice itself — by design. The source file is the recovery path; the notice tells the model exactly how to continue. Only a missing/deleted file defeats it.
  • Human eviction is limited to the trailing message. A huge payload followed by another user turn is not re-examined; the gate is deliberately "last message only" to avoid revisiting settled history.
  • The tail clip only helps when the trailing batch is large. A transcript whose context is consumed by human turns or by non-tool messages degrades to the existing routes; P1-2 is an optimization, not a guarantee.
  • Subagent transcripts are bounded, not persisted. A child registers the same eviction configuration as the main chain: oversized tool results are written to the child's own evicted/ directory (the preview pointer is recoverable because the session's private tree is readable/writable without HITL), its task/continuation messages are never evicted (human eviction only matches origin="user", which they do not carry), and nothing enters the client-visible MesMemory history. The checkpoint holds the preview view.

Clone this wiki locally