Mental Model
Mental Model
How to think in CellState. Read this first if you’re new to the codebase.
For system internals, see ARCHITECTURE.md.
Core Philosophy
CellState is a hierarchical memory framework for AI agents. It models memory the way humans remember: some things are fleeting (what you had for breakfast), others persist (how to ride a bike).
The key insight: Turns are cheap and disposable. Artifacts and Notes are valuable and persist.
The Hierarchy
TENANT (isolation boundary — one per organization)
└── TRAJECTORY (task/goal — like a project folder)
├── SCOPE (context window — like a conversation)
│ ├── TURN (ephemeral — dies when scope closes)
│ └── TURN
├── ARTIFACT (persists — important outputs)
└── NOTE (cross-trajectory knowledge)
Entity Lifespans
| Entity | Lifespan | Analogy |
|---|---|---|
| Tenant | Permanent | Your organization |
| Trajectory | Task duration | A project folder |
| Scope | Session | A conversation thread |
| Turn | Scope only | Individual messages (deleted when scope closes) |
| Artifact | Configurable TTL | Saved documents |
| Note | Cross-trajectory | Learned knowledge |
Memory Types
| Type | Maps To | Lifespan | Example |
|---|---|---|---|
| Ephemeral | Turns | Scope only | ”The user just asked about X” |
| Working | Active scope | Session | ”We’re debugging the auth flow” |
| Episodic | Artifacts | Configurable | ”I fixed bug #123 yesterday” |
| Semantic | Notes | Long-term | ”TypeScript requires strict types” |
| Procedural | Notes (procedure type) | Permanent | ”How to deploy to production” |
What Survives When a Scope Closes?
- Turns are deleted (ephemeral conversation)
- Artifacts survive (extracted value)
- Notes survive (learned knowledge)
Before closing a scope, extract valuable information into Artifacts or Notes.
Abstraction Levels (L0 → L1 → L2)
Information compresses upward as context fills:
| Level | Name | Scope | Example |
|---|---|---|---|
| L0 | Raw | Single turn/artifact | Direct observations |
| L1 | Summary | Within trajectory | Synthesized from multiple L0s |
| L2 | Principle | Cross-trajectory | High-level abstractions |
L0 (Raw): "User asked about error handling in turn 3, wanted try/catch"
L1 (Summary): "This trajectory focused on error handling patterns in TypeScript"
L2 (Principle): "Always validate at system boundaries, fail fast internally"
L2 principles survive with minimal tokens. “Always validate inputs” is cheaper than storing 50 examples of input validation.
Token Budget Flow
Each Scope tracks token usage against a budget:
Scope(token_budget=8000)
├─ Turn(500 tokens) → used: 500
├─ Turn(400 tokens) → used: 900
├─ Turn(600 tokens) → used: 1500
├─ ...
└─ When 80% full → trigger summarization
Summarization triggers: token threshold exceeded, scope closing, turn count threshold, artifact count threshold, or manual API trigger.
TTL (Time-to-Live)
Persistent — never expires
Session — expires when session ends
Scope — expires when scope closes
Ephemeral — alias for Scope
ShortTerm — ~1 hour
MediumTerm — ~24 hours
LongTerm — ~7 days
Permanent — alias for Persistent
Duration(ms) — custom milliseconds
Multi-Agent Coordination
Delegation (Sub-task)
Agent A needs specialist help → creates Delegation to Agent B → Agent B works in a child trajectory → returns artifacts and result to Agent A.
Handoff (Transfer Control)
Agent A transfers full context to Agent B → Agent B takes over the trajectory → Agent A steps back. Use for escalation.
Message (Async Communication)
Agent A sends a notification/query to Agent B → Agent B processes when ready. Non-blocking.
Lock (Exclusive Access)
Agent A acquires an exclusive or shared lock on a resource → other agents wait → release when done. Prevents conflicts.
Graph Relationships (Edges)
Entities can be linked with typed relationships:
| Edge Type | Meaning | Example |
|---|---|---|
| Supports | A backs up B | Artifact supports Note |
| Contradicts | A conflicts with B | Two conflicting facts |
| Supersedes | A replaces B | Updated artifact |
| DerivedFrom | A came from B | Summary from turns |
| RelatesTo | Generic relation | Related topics |
| SynthesizedFrom | Output from multiple inputs | L1 from multiple L0s |
Mutation Pipeline
All writes flow through a 4-stage pipeline (see ARCHITECTURE.md for details):
- Assemble — fetch context and policy config (I/O)
- Gates — run pure policy checks (no I/O)
- Commit — atomic database transaction
- Receipt — return proof, broadcast event
Gates are pure functions. They receive a frozen ValidationContext and return Pass or Reject. No async, no database calls. This makes policy enforcement deterministic and testable.
Common Patterns
Task Execution
1. Create Trajectory (name="Fix bug #123")
2. Create Scope (token_budget=8000)
3. Add Turns (conversation with user/tools)
4. Extract Artifacts (code changes, decisions)
5. Close Scope (turns deleted, artifacts persist)
6. Complete Trajectory (outcome recorded)
Knowledge Extraction
1. During work, identify reusable knowledge
2. Create Note with type:
- Convention: "Always use async/await, not .then()"
- Strategy: "Start with tests for complex features"
- Gotcha: "The API returns 500 for auth failures, not 401"
3. Link to source artifacts/trajectories
4. Note persists across all future trajectories
Context Management
1. Check scope.tokens_used vs scope.token_budget
2. If approaching limit (>80%):
a. Summarize old turns → create Summary artifact
b. Create Note with L1/L2 abstractions
c. Close scope, open new one
3. New scope starts fresh but artifacts/notes persist
Quick Reference
When to Create What
| Situation | Create |
|---|---|
| Starting a new task | Trajectory |
| Beginning conversation | Scope |
| Each message exchange | Turn |
| Valuable output (code, decision) | Artifact |
| Reusable knowledge | Note |
| Sub-task for specialist | Delegation |
| Transfer control completely | Handoff |
| Async notification | Message |
| Prevent conflicts | Lock |
What Persists?
| Entity | After Scope Close | After Trajectory Complete |
|---|---|---|
| Turn | Deleted | Deleted |
| Artifact | Persists | Persists |
| Note | Persists | Persists |
| Scope | Record remains | Record remains |
| Trajectory | — | Record remains |
API Endpoints
| Resource | Endpoints |
|---|---|
| Trajectories | POST /trajectories, GET /:id, PATCH, DELETE |
| Scopes | POST /scopes, GET, PATCH, POST /:id/close |
| Turns | POST /turns, GET /:id |
| Artifacts | POST /artifacts, GET, PATCH, DELETE, POST /batch |
| Notes | POST /notes, GET, PATCH, DELETE, POST /batch |
| Agents | POST /agents/register, POST /heartbeat, DELETE |
| Locks | POST /locks/acquire, DELETE /release, PATCH /extend |
| Messages | POST /messages, GET, POST /:id/acknowledge |
| Delegations | POST /delegations, POST /:id/accept, /:id/complete |
| Handoffs | POST /handoffs, POST /:id/accept, /:id/complete |
| Context | POST /context/assemble |
| Search | POST /search |
| Pack Config | POST /pack-config/validate, POST /pack-config/parse |
Interactive API docs: build with --features openapi,swagger-ui and visit /docs.
Diagrams
State Container] Pack[Pack
Module Bundle] Circuit[Circuit
Execution Flow] Receipt[Receipt
Mutation Proof] end subgraph System Types Scope[Scope] Trajectory[Trajectory] MutPipeline[MutationPipeline] ReceiptEnv[ReceiptEnvelope] PackManifest[PackManifest] CompiledConfig[CompiledConfig] EventDAG[Event DAG] CircuitBreaker[CircuitBreaker] end Cell -.-> Scope Cell -.-> Trajectory Pack -.-> PackManifest Pack -.-> CompiledConfig Circuit -.-> MutPipeline Circuit -.-> EventDAG Circuit -.-> CircuitBreaker Receipt -.-> ReceiptEnv
Research Lab] BP[batterypack.dev
Dev Tools + SDK] CS[CellState
Agent Memory Infrastructure] FBF --> BP FBF --> CS BP --> SDK[TypeScript SDK] BP --> PythonSDK[Python SDK] BP --> CLI[CLI Tools] CS --> Pipeline[Mutation Pipeline] CS --> EventDAG[Event DAG] CS --> PackCompiler[Pack Compiler] CS --> PGExt[PostgreSQL Extension]
Domain Types + Event DAG] pcp[cellstate-pcp
Validation + Checkpointing] storage[cellstate-storage
LMDB Cache + Traits] pipeline[cellstate-pipeline
Pack Compiler] pg[cellstate-pg
PostgreSQL 18 Extension] api[cellstate-api
REST + WebSocket Server] cli[cellstate-cli
CLI Binary] test_utils[cellstate-test-utils
Proptest Generators] sdk[cellstate SDK
Rust Re-exports] fuzz[cellstate-fuzz
Fuzzing Harness] pcp --> core storage --> core pipeline --> core pipeline --> pcp pg --> core api --> core api --> pcp api --> storage api --> pipeline cli --> pipeline test_utils --> core test_utils --> pcp sdk --> core sdk --> pcp fuzz --> core fuzz --> pipeline