Agent memory lifecycle
Agent memory is the durable context layer for facts that can help in a later session. It stores supported user and system knowledge, selects only useful context for each request, and periodically keeps that knowledge current without blocking the agent turn. It is one part of agent execution, alongside the boundaries described in agent harness, LLM invocation, tool framework, and skill lifecycle and releases.
Memory records
An agent memory record is a named durable fact with a type, short description, body, time stamps, and optional provenance. The types separate facts about a person from facts about systems, repositories, and past investigations. Workspace knowledge uses separate organization and integration types and does not come from an agent turn.
| Record type | Durable purpose |
|---|---|
user | Identity and stable information about the user |
preference | How the user prefers the agent to work |
infrastructure | Reusable facts about systems and operating practice |
repository | Durable knowledge about a repository and its conventions |
investigation_learning | Lessons from failures and fixes that worked |
organization | Organization profile and team knowledge from workspace scans |
integration | Knowledge about a connected service from workspace scans |
One name identifies one record. A later write updates that record, and extraction can fold a new fact into a close existing record instead of adding a near-duplicate. This keeps recall focused while retaining new notes and stronger source evidence.
References: core/…/models.py• L9-49 core/…/store.py• L192-220 core/…/store.py• L277-307
Memory recall
Recall gives the action agent a small map of durable context and the full text of records that match the current work. The consolidated summary and index form stable prompt context. Request-specific records are rebuilt for each turn, and the memory_recall tool can return an exact record or search result when the map is not enough.
The following flow shows how stored knowledge reaches a turn without placing the whole store in every prompt.
flowchart LR
Store[Live memory records]
Summary[Consolidated summary]
Index[Memory index]
Request[Current request]
Relevant[Relevant memory bodies]
Agent[Action agent]
Recall[memory_recall]
Store --> Index
Store --> Relevant
Summary --> Index
Request --> Relevant
Index --> Agent
Relevant --> Agent
Agent --> Recall
Recall --> Store
The index lists live records in a stable order based on type and update time. Memory use does not change that order, so a session can reuse the index until a record or summary changes. Only live records enter recall. Fenced demo or sample records stay out of the index and search. The prompt also marks records as possibly old, so the agent must check facts that may have changed before it acts.
References: core/…/index.py• L1-11 core/…/index.py• L67-81 core/…/index.py• L111-132 core/…/recall.py• L1-7 core/…/recall.py• L39-47 core/…/recall.py• L90-119
Relevance policy
Relevance ranking matches words in the request against a record's name, description, and body. The current request has the strongest weight. Repository and integration context can add lower-weight terms, while user and preference records receive priority when the prompt selects context.
The ranker does not select broad context when the request has no useful terms. It also limits the number and size of full records, which keeps relevant memory from crowding out the task itself.
References: core/…/relevance.py• L1-11 core/…/relevance.py• L118-148 core/…/relevance.py• L158-221 core/…/recall.py• L122-164
Memory usage
The system records when a memory body reaches the model through relevant prompt context or an explicit recall result. Use count and last-use time help order the index's future upkeep and show which records remain active. The prompt index alone does not count as use.
Usage tracking is best effort. A missing or busy usage file does not block a turn and does not change whether the record can be recalled.
References: core/…/usage.py• L1-7 core/…/usage.py• L30-39 core/…/usage.py• L72-87 core/…/recall.py• L146-164
Provenance
Provenance records where a fact came from and whether its evidence supports the claim. A provenance record may name the user, a tool, or the assistant. System and incident facts need support from the user or tool evidence before the memory pipeline keeps them as trusted knowledge.
| Source | Policy |
|---|---|
user | Evidence must quote or support the user's claim |
tool | Evidence must appear in tool output and the fact is marked verified |
assistant | Personal facts may remain unverified; unsupported system or incident facts are rejected |
Evidence must support the memory itself. A true statement about another subject does not support the stored fact. Provenance remains visible when the record reaches the agent so the agent can judge how much to trust it.
References: core/…/models.py• L23-29 core/…/frontmatter.py• L7-9 core/…/store.py• L95-132 core/…/index.py• L135-154
Session-turn flows
The session turn is the boundary for collecting new durable knowledge. The session records user messages, assistant replies, and tool results. A later background pass turns that record into a bounded digest, checks each candidate against its source, and writes only facts that meet memory policy.
Turn recall
At turn start, the harness resolves the active memory scope and adds the memory prompt block to the action context. The index gives the agent a stable view of available knowledge. Relevance selection then adds only the records that fit the current request.
When a session resumes, the session repository rebuilds the selected branch. A compaction replacement becomes the new replay boundary, so restored conversation context and turn evidence stay aligned.
References: core/…/orchestrator.py• L185-203 core/…/assemble.py• L421-473 core/…/recall.py• L104-164 core/…/jsonl_repo.py• L64-121 core/…/jsonl_repo.py• L246-298
Turn extraction
After a turn is recorded, qualifying turns feed a background extraction pass. The pass runs at a fixed interval and runs again when a session closes or rotates. Demo and onboarding turns do not count as durable work.
The digest prefers the persisted session record because it includes tool activity. It also includes the newest in-memory turn when needed. The digest removes bookkeeping, redacts credential-shaped text, bounds large tool results, and keeps the newest material when it reaches its size limit.
The following flow shows how a turn becomes either a durable record or a bounded session summary.
sequenceDiagram
participant Turn as Recorded turn
participant Digest as Session digest
participant Model as Extraction model
participant Checks as Policy and provenance checks
participant Store as Durable memory store
participant Sessions as Session summaries
Turn->>Digest: Queue qualifying turn
Digest->>Digest: Remove demos and bookkeeping
Digest->>Digest: Redact and bound session data
Digest->>Model: Send session digest
Model-->>Checks: Return summary and candidates
Checks->>Store: Save supported records
Checks->>Sessions: Save bounded outcome summary
Extraction is best effort. A failed model call, malformed result, or write error does not fail the user's turn.
References: core/…/memory_turns.py• L1-19 core/…/memory_turns.py• L114-137 core/…/memory_digest.py• L1-22 core/…/memory_digest.py• L358-385 core/…/memory_extraction.py• L1-32 core/…/memory_extraction.py• L345-365
Session summaries
Each extraction pass may save a short outcome summary for the session. The agent does not receive these summaries directly. Consolidation uses recent summaries as evidence of recent work and recurring failures, then folds useful context into the consolidated memory summary.
The forget boundary excludes summaries from sessions that started before a forgotten fact was removed. This prevents old session text from restoring knowledge that the user deleted.
References: core/…/summaries.py• L1-11 core/…/summaries.py• L37-42 core/…/summaries.py• L137-175 core/…/summaries.py• L201-226
Consolidation lifecycle
Consolidation is background upkeep for durable memory. It first performs deterministic cleanup and then may ask a model to refresh the consolidated summary. It runs at most once every six hours for a memory scope, so upkeep does not compete with every turn.
The cleanup archives fenced or old unused records. Personal and workspace records do not age out. It also groups repository records by repository identity and merges other close records of the same type. The newest record remains live, while distinct notes are appended when they fit; a note that does not fit remains in its original live record.
Archived records remain outside the live store. Normal scans therefore cannot recall them, while a person can still inspect or restore them if needed.
References: core/…/consolidation.py• L1-23 core/…/consolidation.py• L102-195 core/…/files.py• L8-18 core/…/merge.py• L49-85
Consolidated summary
The consolidated summary is a short model-written view of live memory and recent session outcomes. It gives later prompts a compact account of user identity, preferences, repositories, and recurring investigation lessons before the detailed index.
The summarizer runs without the memory lock so turns can continue. Before writing, consolidation checks that its input is unchanged. If a record is changed or forgotten while the model runs, the result is discarded. Secret-shaped text is redacted before summarization and unsafe output is rejected.
References: core/…/consolidation.py• L79-99 core/…/consolidation.py• L198-257 core/…/index.py• L111-132
Storage boundaries
The memory system keeps one local Markdown store per active scope. Each live record has a named file. The index, consolidated summary, session summaries, usage data, archive, and upkeep state support the lifecycle but are not live records.
The harness carries the active scope into background work. A member's turn therefore reads and writes that member's store. Shared gateway memory is gated when no safe member scope exists, which prevents one person's facts from shaping another person's turn.
| Boundary | Effect |
|---|---|
| Memory disabled | Memory prompt blocks and memory tools are unavailable |
| Automatic extraction disabled | Existing records remain available; background extraction and upkeep stop |
| Shared gateway without opt-in | Memory is unavailable for the gateway turn |
| Member-scoped storage | Facts stay in that member's memory directory |
Knowledge writes also advance a process-local generation marker. A knowledge mirror can use the marker to notice recent writes quickly, while readers still load the store itself.
References: core/…/files.py• L8-18 core/…/members.py• L1-9 core/…/members.py• L97-108 core/…/changes.py• L1-9 core/…/settings.py• L22-51
Change checklist
When these files or sections are changed, remember to consider these:
- Keep record types aligned with the write policy, workspace scan rules, and index ordering.
- Keep scope propagation aligned across turn binding and background extraction or consolidation workers.
- Keep digest filtering aligned with provenance evidence so removing source text does not silently drop valid facts.
- Keep forget handling aligned with summary invalidation and the consolidation forget boundary.
- Keep relevance budgets aligned with usage accounting so the record shown to the model is the record marked as used.
- Keep live-store scans separate from archive and session-summary scans.
- Keep duplicate matching guards aligned with merge rules so different repositories, identifiers, or negated facts do not fold together.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A system fact has no user or tool support | The fact is rejected or remains untrusted | Provenance checks require relevant evidence |
| A demo or sample record is written | Synthetic context can pollute later prompts | Fencing excludes it from recall and consolidation archives it |
| A forgotten record remains in summary input | A later summary can restore the forgotten fact | Forget removes the summary and records a boundary |
| The store changes while summary generation runs | The summary can describe stale or deleted knowledge | Consolidation fingerprints input again and drops stale output |
| A background worker loses the turn's scope | Facts can be written where the originating user never reads them | Workers inherit the active storage scope |
| Similar records carry different identifiers or negation | Distinct facts can be merged | Similarity guards block the match |
Key files
| File | Why to read it |
|---|---|
| core/…/models.py | Defines record types, sources, and lifecycle classes |
| core/…/store.py | Shows durable save, update, merge, and forget behavior |
| core/…/index.py | Shows the stable prompt index and relevant-memory budgets |
| core/…/recall.py | Shows live-memory filtering, request selection, prompt rendering, and use accounting |
| core/…/usage.py | Shows best-effort use tracking used by memory upkeep |
| core/…/similarity.py | Defines duplicate and near-duplicate boundaries |
| core/…/consolidation.py | Defines cleanup, merge, archive, and summary replacement |
| core/…/frontmatter.py | Defines the durable record format and provenance fields |
| core/…/members.py | Defines member storage boundaries |
| core/…/memory_digest.py | Defines the session material used for extraction |
| core/…/memory_extraction.py | Defines when extraction runs and how candidates become records |
| core/…/jsonl_repo.py | Restores active session branches and compaction boundaries |
| core/…/assemble.py | Shows where durable and relevant memory enter the action prompt |