Skip to main content

Module context

Module context 

Source
Expand description

Contexts: the durable, self-compacting working memory of the root agent (context/root) and of every A2A conversation (context/<contextId>). A context is a versioned record of messages, a structured summary block, the loaded skill set, the working plan, the last preflight verdict and a token estimate. The runtime is its single writer and checkpoints it after every turn, so nothing here needs locking and a crash loses at most the turn in flight.

The transcript representation here (Msg) is serializable (unlike the provider wire type) and converts to crate::wire::intel::Message at request time. Sub-modules: plan (the plan object), memory (the durable KV), compact (compaction planning + application), skills (skill catalogue/loaded set), tokens (estimates).

Modules§

compact
Compaction: when a context’s token estimate crosses context.compact_at × model_window (or context.compact is called), the older messages are summarized by a structured think into the summary block, the last keep_last messages stay verbatim, the plan stays verbatim, skill bodies not referenced in the kept window are evicted (the names stay), the version bumps and the record is checkpointed.
memory
Agent memory: a durable JSON key/value space in the instance’s store namespace — memory/<key> — with optional TTL, size caps, and prefix listing. Listing uses the store’s own list when it has one; a store without one is served from the memory/_index record this module maintains, so listing works on every backend rather than only the ones that can enumerate. Overridable by an MCP memory server through the registry.
plan
The context plan: a small ordered checklist the model owns for one context — created by the preflight or by plan.create, advanced by plan.update, cleared by plan.clear, rendered into every prompt, auto-advanced when a bound run, subagent or task reaches a terminal state. Temporary by intent (it belongs to the conversation, never to memory) and durable by construction (it is part of the context record, so it is checkpointed and restored with everything else the conversation knows).
prompt
The system-prompt template — the one place the agent’s standing context is assembled, and the surface an operator overrides.
skills
Skills: named instruction bundles — the SKILL.md idiom — discovered from MCP servers as prompts (prompts/list = catalogue, prompts/get = body) or resources (skill://<name> URIs or mimeType: text/x-skill+markdown), referenced as @skill:<name> in the instruction, a step, or a chat message, and preloaded into the calling context (progressive disclosure: the catalogue is always visible, a body only when referenced or skills.loaded). Bodies are cached by hash, never stored; the loaded set [{name, hash}] is part of the context record.
tokens
Token estimates. agentd never counts provider tokens itself — the provider’s usage is the truth once a call returns — but the governor’s reservation and the compaction trigger need a number before the call. The heuristic is the usual chars / 4 (English prose ≈ 4 chars a token; JSON and code run denser, which errs on the safe side for a reservation) plus a small per-message overhead. Deliberately simple and dependency-free; the estimate is only ever compared against generous thresholds.

Structs§

ContextState
The durable context record: everything about a conversation that must survive a restart.
Contexts
The in-memory registry of contexts + their durable mirror.
SkillRef
A loaded skill reference: its name plus the hash of the version that was loaded, so a skill edited on disk is detectable rather than silently diverging from what the transcript was built against.
Summary
The structured summary block a compaction produces: what the dropped messages amounted to, kept in fields so a later compaction can merge two summaries instead of summarising a summary.

Enums§

ContextKind
The kind of a context.
Msg
One transcript entry. ts is wall-clock ms; tool results keep the parsed JSON when the tool returned structured content (text otherwise).

Constants§

ROOT
The root context id: the one context that always exists, holding the root agent’s own working memory rather than any caller’s conversation.