Crate-agnostic governed context compaction for AI agents.
context-governor turns a long agent transcript into:
- compacted messages suitable for reinjection into an agent prompt
- a single-generation
ContextCompactionReceiptV1, or an append-only recursiveContextCompactionReceiptV2 - a
ContextAllocationPlanV1 - a structured loss/anchor report
- exact fallback records recoverable by item id
- searchable file-backed receipt storage
- recall-quality filters for speculative memory and artifact-pack noise
- CLI commands usable from any host agent
It is not an LLM summarizer and does not claim to extend a hosted model's hard context window. It improves effective context by reducing prompt damage and making loss visible.
agent transcript
|
v
+------------------+ +------------------+ +------------------+
| classify spans | --> | allocate budget | --> | compact prompt |
| authority/kind | | soft/hard/fail | | exact + summary |
+------------------+ +------------------+ +------------------+
| | |
v v v
recall quality gate allocation plan receipt + fallback
| |
v v
memory sink records search / expand / diff
Core API:
compact_context(CompactRequest) -> CompactResponsecompact_context_v2(CompactRequest) -> CompactResponseV2FileContextStore::compact_next_v2for restart-safe continuationFileContextStore::expand_lineagefor verified transitive exact fallbackFileContextStore::{prepare_v2, activate_v2, discard_pending_v2}for a pending-to-active host commit fencefilter_recall_candidatecontext_expandcontext_searchcontext_diffarchive_response_to_memory
Receipts and records:
ContextCompactionReceiptV1ContextCompactionReceiptV2,ParentReceiptRefV2, andOriginalSourceRefV2(seedocs/context-compaction-receipt-v2.md)PendingReceiptInfoV2andReceiptActivationRequestV2ContextAllocationPlanV1ContextItemV1SummaryLossReportV1ExactRecoveryStateV1—unavailable,in_response, or verifiedpersistedStructuredContextSummaryV1ExactStoredItemV1MemoryArchiveRecordV1
Policy controls:
BudgetMode::SoftWarn— current safety-first behavior, warn if budget is exceededBudgetMode::HardCascade— shrink/remove summary to fit when exact-preserve content allows it; emitshard budget not metwhen protected content itself overflowsBudgetMode::FailClosed— refuse when required exact content cannot fitTokenCounterKind::ApproxChars— explicit char/4 estimator recorded in receiptsTokenCounterKind::ProviderChatApprox— provider-style chat overhead heuristic, still recorded as approximateTokenCounterKind::TiktokenCl100k— the optionaltiktokenfeature usescl100k_basetext counts when tokenizer initialization succeeds; the default build falls back to the provider-chat approximation and emits a warning. This does not establish exact full-provider chat accounting
Content kinds:
- plain text
- JSON
- diffs
- Rust source
- Markdown
- Cargo output
- shell logs
- search results
File store:
FileContextStore::new(path)save(&CompactResponse)load(receipt_id)list_receipts()status()— receipt count, store bytes, tmp-file cleanup count, and in-memory index stateexpand(receipt_id, item_id, max_chars)search(query, top_k, scope)
CLI:
context-governor compact < request.json > response.json
context-governor store --dir .ctx < response.json
context-governor search --dir .ctx --query NEEDLE
context-governor expand --dir .ctx --receipt ctxr_... --item ctxi_...
context-governor status --dir .ctx
context-governor diff < response.jsonThose keyless commands are the V1/offline lane. Certified V2 commands require
inherited governed key/snapshot descriptors and use
compact-v2 → finalize-v2 → prepare-v2 → host commit → activate-v2.
See docs/context-compaction-receipt-v2.md; no-argument CLI mode remains
backwards-compatible with compact.
use context_governor::{compact_context, CompactRequest, CompactionPolicy, Message};
let response = compact_context(CompactRequest {
session_id: "session-1".into(),
messages: vec![Message {
id: None,
role: "user".into(),
content: "Build it. Acceptance gate: tests pass.".into(),
name: None,
metadata: Default::default(),
}],
policy: CompactionPolicy::default(),
focus: None,
hmac_key_path: None,
})?;
println!("{}", response.receipt.receipt_id);
# Ok::<(), context_governor::ContextGovernorError>(())Request shape:
{
"session_id": "s1",
"policy": {
"target_tokens": 4000,
"protect_first_n": 3,
"protect_last_n": 8,
"summary_max_chars": 8000,
"allocator": "deterministic_v1",
"semantic_memory_enabled": false,
"archive_memory_enabled": false,
"budget_mode": "soft_warn",
"token_counter": "provider_chat_approx"
},
"messages": [
{ "role": "system", "content": "You are an agent." },
{ "role": "user", "content": "Implement the feature. Acceptance gate: tests pass." },
{ "role": "tool", "content": "long output..." },
{ "role": "user", "content": "Latest active request." }
]
}Adapters for Hermes, OpenCode, Codex wrappers, Claude Code wrappers, or custom agents should:
- Map transcript records to
Vec<Message>. - Call
compact_contextnear the provider context threshold. - Persist the returned
CompactResponsewithFileContextStoreor host storage. - Inject
CompactResponse.compacted_messagesinto the next model call. - Expose
search,expand, anddiffto the agent. - Treat summaries as background only; the latest user message remains active.
- If archival is needed, implement
MemorySinkin the adapter and write real external IDs back in the host layer.
compact_context reports exact fallback as in_response, never as durable. Use
FileContextStore::save_with_status when the host must verify that fallback was
written, indexed, reloaded, and can truthfully be reported as persisted.
See:
docs/architecture.mddocs/integrations/hermes.mddocs/integrations/host-adapters.mddocs/eval-harness.mddocs/roi-audit-2026-06-27.mddocs/plans/2026-07-11-state-of-the-art-context-compaction-survey.mddocs/plans/2026-07-11-context-compactor-beyond-expectations-roadmap.md
Run:
cargo run --release --example perfLatest observed synthetic release-mode run on the Hermes runtime machine:
| Messages | Original approx tokens | Compacted approx tokens | Token reduction | Avg latency | P95 latency |
|---|---|---|---|---|---|
| 100 | 25,362 | 9,720 | 61.7% | 0.634 ms | 0.663 ms |
| 500 | 128,842 | 16,020 | 87.6% | 3.785 ms | 3.932 ms |
| 1,000 | 258,192 | 23,895 | 90.7% | 9.469 ms | 9.867 ms |
| 2,000 | 516,992 | 39,745 | 92.3% | 28.846 ms | 29.726 ms |
This is a synthetic compaction throughput benchmark, not an agent-task success benchmark.
examples/replay_eval.rsis a small deterministic demo.examples/replay_fixture.rsevaluates anyCompactRequestJSON fixture.scripts/hermes_replay_eval.pyextracts local Hermes sessions from~/.hermes/state.dband writes a privacy-preserving aggregate report.scripts/compare_context_engines_live.pyruns identical synthetic transcripts through full, naive head/tail, context-governor, and explicit unsupported receipts for Hermes built-in/offline external adapters.scripts/hermes_task_replay_eval.pyscores 10 local Hermes coding sessions with aggregate/private-safe historical answerability output.scripts/certify_all.py --quick --skip-hermesruns fmt, tests, adversarial eval, synthetic task success, same-transcript comparison, and bounded historical answerability.
Run:
cargo run --example replay_eval
python scripts/hermes_replay_eval.py --limit 8 --min-messages 20 --target-tokens-list 20000,80000,120000 --budget-mode hard_cascade
python3 scripts/compare_context_engines_live.py --quick
python3 scripts/hermes_task_replay_eval.py --limit 10 --min-messages 12 --target-tokens 1200
python3 scripts/certify_all.py --quick --skip-hermesLatest next-level local receipts from 2026-07-01:
- local policy comparison:
target/context-governor-comparisons/same-transcript-comparison.md - historical answerability replay:
target/historical-answerability/historical-answerability.md - certification:
target/certification/20260701-031403/certification.md
Observed in that pass: local policy comparison synthetic context-governor answerability 100.0%, recoverability 100.0%, token reduction 95.8%; historical 10-session answerability 100.0%, visible 28.0%, token reduction vs full 99.6%. These are local deterministic replay receipts, not universal downstream LLM quality or external-engine superiority proof.
Latest local Hermes replay aggregate over the 8 largest active sessions by text volume:
| Metric | Value |
|---|---|
| Runs | 24 |
| Avg full tokens | 315,660.5 |
| Avg context-governor tokens | 16,003.9 |
| Avg token reduction vs full | 94.9% |
| Avg naive head/tail recoverable rate | 2.7% |
| Avg context-governor visible rate | 43.8% |
| Avg context-governor recoverable rate | 98.8% |
| Active task visible | 24/24 |
Report:
docs/hermes-replay-eval-2026-06-27.md- machine-readable local report:
target/context-governor-replay/hermes-replay-report.json
Privacy boundary: the markdown report stores aggregate metrics only; raw transcript content is not written there.
This crate currently provides deterministic allocation, content-aware extractive previews, receipts, exact fallback, local search, CLI operations, and adapter-facing memory records.
It does not claim:
- semantic correctness equivalent to an LLM-written summary
- benchmarked LLM task-quality improvement
- KV-cache compression
- direct HyperQuant lattice compression of text
- provider-native long-context superiority
- built-in semantic-memory writes
HyperQuant-style use here is the rate-distortion allocation framing: spend scarce prompt tokens where loss would damage the task most.
Current local verification commands:
cargo fmt --check
cargo clippy --all-targets -- -D warnings
cargo test --all-targets
cargo check --all-targets
cargo run --release --example perf
cargo run --example replay_eval
cargo doc --no-deps --document-private-items
cargo publish --dry-run --allow-dirtyApache-2.0. See LICENSE.