This document describes the stable product architecture of Cross-Session Memory. It is intentionally separate from docs/ARCHITECTURE.md, which is an auto-generated repository graph and may contain workspace-specific snapshots.
CSM exists to make continuity a first-class runtime capability.
The system must preserve useful history without flooding the active prompt, reconstruct current work without inventing state, and expose enough provenance for an agent or operator to understand why a piece of context was selected.
- Continuity is layered. Long-term memory, current project state, internal observations, work state, and active context are different concerns.
- History and projection are separate. Append-only evidence should not be confused with a mutable current-state view.
- Retrieval must be bounded. More stored information must not automatically mean more injected information.
- Provider differences must be explicit. Unsupported database features are removed from the tool surface.
- Promotion requires evidence. Derived beliefs and governance decisions remain revisable and provenance-aware.
- Failure should preserve recoverability. Checkpoints, event history, source attribution, and non-destructive merge behavior support rollback and audit.
- The fresh-session path matters most. Continuity must be available before an agent begins ordinary work.
flowchart TB
OC[OpenCode host] --> HR[Hook registration]
HR --> TH[Tool hooks]
HR --> SH[Session and system hooks]
TH --> MEM[Memory runtime]
TH --> AB[AgentBook runtime]
TH --> LS[Living State runtime]
TH --> CTX[Context and checkpoint runtime]
TH --> GOV[Governance runtime]
SH --> AB
SH --> RE[Re-entry compiler]
SH --> DOC[Auto-documentation]
SH --> CMP[Compaction and rollover]
MEM --> DB[(Database provider)]
AB --> DB
LS --> DB
CTX --> DB
GOV --> DB
RE --> DB
CMP --> DB
DB --> PG[PostgreSQL adapter]
DB --> SQ[SQLite adapter]
AB --> FP[AGENTBOOK_STATE.md]
RE --> INJ[Bounded context injection]
FP --> OC
INJ --> OC
src/hooks-registration.ts is the composition root for the OpenCode plugin. It initializes the configured provider, constructs runtime services, registers lifecycle hooks, and exposes tools.
src/hooks/tool-hooks.ts builds the runtime tool surface. Provider and runtime guards are applied after tool construction so unsupported tools do not remain visible.
Key responsibilities:
- create shared runtime dependencies
- register tool definitions
- attach session and system hooks
- enforce provider-aware availability
- apply re-entry source-only guards
- dispose resources cleanly
The storage layer is selected through configuration.
flowchart LR
CFG[Validated configuration] --> DB[Database facade]
DB --> PG[PostgreSQL]
DB --> SQ[SQLite]
PG --> PM[PostgreSQL migrations]
SQ --> SM[SQLite bootstrap and migrations]
PostgreSQL is the complete feature path. It supports the full memory, governance, Living State, context-cache, checkpoint, goal, and reporting stack.
SQLite is a local core path. It provides implemented core capabilities and removes PostgreSQL-only tools during registration.
The system does not present provider parity where parity does not exist.
The memory layer owns durable records and recall.
Primary responsibilities:
- save and classify memory
- search across vector and text surfaces
- filter by type, tags, entities, and importance
- traverse relationships
- store lessons and transcript-derived records
- distill content
- compact context
- backfill embeddings
- detect duplicates
- merge or supersede records
- generate maintenance and archive candidates
flowchart LR
INPUT[Conversation, tool result, lesson, project fact] --> MM[Memory manager]
MM --> RED[Redaction and normalization]
RED --> STORE[(Memory store)]
STORE --> VEC[Vector retrieval]
STORE --> TXT[Full-text retrieval]
STORE --> REL[Relationship retrieval]
VEC --> RANK[Recall ranking]
TXT --> RANK
REL --> RANK
RANK --> BOUND[Bounded context result]
AgentBook owns operational project continuity.
It deliberately separates immutable history from mutable projection.
flowchart LR
EVENT[Tool or session event] --> LOG[(Append-only event store)]
LOG --> SUM[Rolling summary]
LOG --> PROJ[Current-state projector]
RULES[(Explicit rules)] --> PROJ
SUM --> PROJ
PROJ --> PAGE[Front-page generator]
PAGE --> FILE[AGENTBOOK_STATE.md]
Components include:
- event store
- event classification
- rules store
- summary generator
- state projector
- front-page renderer
- event, state, and rule tools
AgentBook answers the immediate operational question: what is being worked on, what changed, what is blocked, and what should happen next?
The re-entry system compiles a fresh-session context block from durable sources.
Potential layers include:
- identity and self-continuity
- project and phase state
- goals and checkpoints
- constraints and rules
- relevant memories
- decisions and errors
- promoted knowledge
- advisories
- handoff state
- readiness and provenance
flowchart TB
S1[Identity] --> C[Re-entry compiler]
S2[Project state] --> C
S3[Goals and checkpoints] --> C
S4[Memories and decisions] --> C
S5[Rules and constraints] --> C
S6[Living State advisories] --> C
C --> P[Priority and token policy]
P --> I[Validated injection]
I --> H[Host system prompt]
The compiler does not inject every available record. It selects and trims according to priority and budget.
Living State converts structured experience into revisable internal knowledge.
flowchart LR
TC[Tool and session outcomes] --> EP[Experience packets]
EP --> SM[Self-model reconciliation]
EP --> BS[Belief scan]
BS --> BC[Belief candidates]
BC --> BP[Controlled promotion]
BP --> BK[Belief knowledge]
SM --> ADV[Advisory context]
BK --> ADV
Important boundaries:
- experience packets are observations, not conclusions
- capability confidence is revisable
- belief candidates are not automatically durable knowledge
- promotion is gated
- advisory output can be previewed and debugged
The context-control layer manages active prompt pressure and deferred retrieval.
It includes:
- token-bucket analysis
- compaction
- compaction quality and audit
- context rollover
- context cache
- context manifest
- selective context search and fetch
- file-region retrieval
- last-error and decision-log retrieval
- checkpoint injection
- goal-aware transforms
- context-fault recovery
flowchart LR
ACTIVE[Active context] --> PRESS[Pressure analysis]
PRESS -->|within budget| KEEP[Keep active]
PRESS -->|over budget| COMPACT[Compact]
PRESS -->|defer| CACHE[(Context cache)]
CACHE --> SEARCH[Search and selective fetch]
COMPACT --> AUDIT[Compaction audit]
SEARCH --> ACTIVE
Work continuity preserves execution state that should survive beyond one conversation.
It includes:
- active goals
- goal updates and history
- checkpoints
- checkpoint references
- decision and error evidence
- work-ledger survival
- handoff state
- causal links across sessions
This layer is narrower than long-term memory. It is optimized for resuming unfinished work safely.
Governance evaluates the quality and usability of stored continuity.
Key surfaces:
- recall-quality reporting
- continuity resilience reporting
- provenance completeness
- evidence strength
- duplicate detection
- archive candidates
- safe merge and supersede
- migration verification
- backup/restore drills
Governance is advisory unless a specific enforcement contract states otherwise.
- A hook or tool receives source material.
- Content is normalized and redacted according to configuration.
- Memory metadata and provenance are constructed.
- The provider writes the durable record.
- Embeddings and relationships are added when supported.
- Telemetry and follow-up maintenance surfaces observe the result.
- Tool or session activity is classified.
- File, command, result, and failure evidence are extracted.
- An append-only event is stored.
- Threshold summary generation is evaluated.
- Current state is projected.
- The front page is written to the active project directory.
- A structured outcome becomes an experience packet.
- Capability and belief scanners evaluate the packet.
- Candidates remain separate from durable promoted knowledge.
- Promotion occurs only through the configured policy.
- Advisory output is generated from current evidence.
- A query is normalized.
- Eligible retrieval surfaces execute.
- Results are combined and ranked.
- Filters and quality signals are applied.
- The result is bounded for the caller.
- OpenCode reads repository instructions, including
AGENTBOOK_STATE.md. - CSM constructs the configured re-entry layers.
- Sources are prioritized and trimmed.
- The injection contract is validated.
- The host receives a bounded continuity block.
- Source attribution remains available for diagnosis.
- Context is stored or indexed in the cache.
- The manifest records available deferred material.
- Search tools identify relevant entries.
- Fetch tools retrieve bounded regions or evidence.
- Only selected material re-enters the active prompt.
The architecture favors explicit degradation over hidden partial behavior.
- SQLite-only limitations remove tools at registration.
- Re-entry can run in preview mode.
- Source-only recovery can block ordinary tools while re-entry is unresolved.
- Front-page writes are best-effort where the markdown result can still be returned.
- Checkpoints and event history preserve recovery evidence.
- Merge and archive workflows avoid immediate destructive loss.
- CI retains schema and test diagnostics when a database matrix leg fails.
| Path | Ownership |
|---|---|
src/hooks-registration.ts |
Runtime composition |
src/hooks/ |
Host lifecycle integration |
src/tools.ts |
Core memory and continuity tools |
src/hooks/tool-hooks.ts |
Tool registration and provider guards |
src/agentbook-*.ts |
AgentBook |
src/reentry-*.ts, src/re-entry-protocol.ts |
Re-entry |
src/context-cache-*.ts |
Deferred context |
src/checkpoint-*.ts, src/goal-*.ts |
Work continuity |
src/living-state-*.ts, src/belief-*.ts, src/self-model-*.ts |
Living State |
src/database.ts, src/db/, src/schema/ |
Storage and migrations |
test/ |
Regression and contract verification |
scripts/ |
Operational and verification commands |
docs/ |
Product docs, contracts, reports, and history |
A new subsystem should:
- declare its durable data ownership
- define provider support explicitly
- register tools through the composition root
- include provenance and failure behavior
- respect context budgets
- add migrations and compatibility tests when storage changes
- add focused and full-suite verification
- update the feature map and product architecture