zeph-context
Context budget, lifecycle management, compaction strategy, and stateless context assembler for the Zeph AI agent.
This crate contains the stateless and data-only parts of context management extracted from
zeph-core. It has no dependency on zeph-core — callers implement the IndexAccess trait and
populate ContextMemoryView before each assembly pass.
Usage
use ContextBudget;
let budget = new;
assert_eq!;
Modules
budget—ContextBudgetandBudgetAllocationfor token budget calculationmanager—ContextManagerstate machine andCompactionStatelifecycle trackingassembler—ContextAssemblerparallel fetch coordinator; classifies each slot into aTypedPage, enforces per-type fidelity invariants at compaction boundaries, and emits aCompactionAuditSinkrecord per compacted pageinput—ContextAssemblyInput,ContextMemoryView,IndexAccesstraitslot—ContextSlot,CompactionOutcome, message-chunking helperstyped_page—TypedPage(BLAKE3 content-hash id),PageTypeenum (ToolOutput,ConversationTurn,MemoryExcerpt,SystemContext), per-typePageInvariantimplementations,InvariantRegistry, and theCompactionAuditSinkbounded async mpsc channel that records one audit entry per compacted pagesummarization— pure prompt-building helpers for context compactiontool_result_compress— per-result and batch-level token-budget enforcement for tool outputscompression_feedback— context-loss detection and failure classificationmicrocompact— low-value tool detection helpers for time-based microcompactfidelity— heuristic fidelity scorer for Context-Adaptive Memory (CAM)turn_context—TurnIdandTurnContextper-turn carriererror—AssemblerError
ClawVM typed-page compaction
ContextAssembler::gather() classifies every context slot into a TypedPage keyed by a BLAKE3 content hash. Each PageType carries a PageInvariant that declares the minimum-fidelity guarantee for compaction:
| PageType | Invariant |
|---|---|
ToolOutput |
Preserve tool name, return code, and first/last N bytes of output |
ConversationTurn |
Preserve role, first sentence, and any code blocks |
MemoryExcerpt |
Preserve entity names and relationship predicates |
SystemContext |
Never compact — always injected verbatim |
At every compaction boundary the invariant is checked before the LLM compaction call. If the post-compaction result would violate the invariant the compaction is rejected and the original content is retained.
License
Licensed under either of MIT or Apache License, Version 2.0 at your option.