Skip to content

Storage Model

AgentCTX uses a hybrid storage architecture: a local content-addressed store for offline operation and SurrealDB for persistent, queryable storage.

Every piece of content is stored by its SHA-256 hash:

.context/objects/
├── a1/
│ └── a1b2c3d4... ← SHA-256 of content
├── e5/
│ └── e5f6g7h8...
└── i9/
└── i9j0k1l2...

Objects are stored in 2-character prefix subdirectories (like Git’s object storage) for filesystem performance at scale.

Benefits:

  • Deduplication — identical content is stored once, regardless of how many agents reference it
  • Integrity — content can’t be modified without changing its hash
  • Portability — objects are self-contained and can be synced to remote stores

Static content (tool schemas, documentation chunks, skill definitions) is registered once and referenced by hash. This is the mechanism behind AgentCTX’s 99.9% savings on static content:

First session: +s "deploy" "Steps: lint, test, build, deploy" → stored as SHA-256
Subsequent sessions: Reference by hash → 3 tokens instead of 50+

SurrealDB v3 is the primary persistent database, providing:

FeatureUse
Document storeMemory and knowledge storage
Graph relationsMemory relationships (RELATE edges)
HNSW vectorsSemantic similarity search (memory + knowledge via surrealqlNative)
LIVE SELECTReal-time subscriptions
Computed fieldsAutomatic decay scoring
FunctionsServer-side fn::compute_decay

AgentCTX ships with 10 SurrealQL schema files, loaded at startup by the surrealdb-init service:

SchemaPurpose
memory.surqlMemory table, HNSW index, CHANGEFEED 7d, fn::compute_decay, computed decay_score, ASYNC events
knowledge.surqlKnowledge table, HNSW index, CHANGEFEED 7d, full-text search
graph.surqlRelation tables: related, grounds, promoted_by (TYPE RELATION)
security.surqlRow-level security permissions, DEFINE ACCESS TYPE RECORD
telemetry.surqlDEFINE SEQUENCE telemetry_seq, telemetry table
api.surqlDEFINE API endpoints: /memory/search, /knowledge/search, /health
credentials.surqlCredential vault table, composite unique index, admin-only PERMISSIONS
change-set.surqlChange-set tracking table
process-group.surqlProcess group state table
process-archive.surqlProcess archive table

Modules that interact with SurrealDB use a dual-path design:

surrealqlNative = true → SurrealQL emitter (direct queries)
surrealqlNative = false → TypeScript wrapper (legacy fallback)

The SurrealQL-native path is the default in production. The TypeScript path remains as a fallback.

The storage module supports syncing objects to remote stores for distributed deployments:

  • Object sync — CAS objects replicated across nodes
  • Ref sync — reference pointers synchronized
  • Conflict resolution — content-addressed design means no merge conflicts

SurrealDB’s built-in ML capabilities are used for:

  • Automatic decay scoring (computed fields)
  • HNSW nearest-neighbor search on embedded memories
  • Server-side aggregation functions

Memory write operations can be enforced by WASM contracts (crates/memory-wasm/), providing:

  • Write gate validation before storage
  • Schema enforcement at the edge
  • Cross-platform consistency (same logic in Node.js and browser)