Rust core + N-API TypeScript bindings monorepo.
hive-ctx is the context engine powering The Hive —
a globally distributed AI agent platform.
crates/hive-ctx-core: Rust core compiled to a.nodebinary vianapi-rspackages/bindings: TypeScript bindings wrapping the native addon
Core modules (Rust): graph, memory, fingerprint, classifier, retrieval, pipeline.
- SQLite schema lives inside
crates/hive-ctx-core/src/graph.rs(tables:nodes,edgesplus indexes). - Nodes represent entities (
person,place,project,concept,emotion,state) with timestamps and a decay score for emotional/state nodes. - Entity extraction for
graph_add_noderuns regex-driven heuristics on raw text (crates/hive-ctx-core/src/graph.rs) so there are no ML/API dependencies. - Edges store typed relationships with timestamps, and traversal discovers neighbors up to
Nhops whilegraph_decay_updateages emotional/state nodes over time. - Exposed addon APIs:
graph_add_node,graph_add_edge,graph_query,graph_traverse,graph_decay_update(viaHiveCtxEngine).
crates/hive-ctx-core/src/memory.rsimplements a 3-tier episode archive backed by SQLite (tier1_entries,tier2_summaries,tier3_crystallized) plusmemory_meta.- Tier 1 holds raw conversations and expires after 24 hours; Tier 2 stores compressed 2-3 sentence summaries retained for 30 days; Tier 3 stores crystallized facts merged into the knowledge graph and never deleted.
memory_compressmoves Tier 1 → Tier 2 nightly while skipping unchanged text viablake3hashes;memory_crystallizeruns monthly to push Tier 2 summaries into Tier 3, runninggraph_add_nodeon the extracted facts.- Exposed addon APIs:
memory_store,memory_retrieve,memory_compress,memory_crystallize,memory_stats(viaHiveCtxEngine).
crates/hive-ctx-core/src/retrieval.rsranks nodes and memory records by combining temporal recency, graph centrality, semantic keyword overlap, and emotional relevance weighted according to the classifier's four axis scores.- Results merge knowledge-graph labels and episode-store entries, include token estimates, and can be reranked via
retrieval_rank. - Exposed addon APIs:
retrieval_search,retrieval_rank(viaHiveCtxEngine), both accept the four classifier weights so downstream clients pick the most contextually relevant content.
crates/hive-ctx-core/src/pipeline.rsorchestrates every module: it classifies the message, fetches retrieval context, compiles the fingerprint, and assembles the final prompt within a configurable token budget (default 300).- Layers are trimmed in order (episodes → graph nodes → fingerprint entries) when the budget is exceeded, and warm sessions only send fingerprint deltas thanks to the cached session state.
- Exposed addon API:
pipeline_build(viaHiveCtxEngine), which returns the compiled system prompt, actual token usage, and the list of layers that contributed.
packages/bindingsnow exportsHiveCtx(constructor config:storagePath, optionalbudgetTokens,model,profile) plusremember,episode, and plugin integration around thepipeline_buildcore.- Plugin authors implement
{ name, retrieve(message, weights) }to inject custom retrieval content if tokens remain; context builds append plugin contributions while tracking token usage in the returnedContextResult. - Examples under
packages/bindings/src/examples(basic.ts,hive-integration.ts) show the minimal usage pattern and how The Hive agent would integrate with the new APIs.
crates/hive-ctx-core/src/classifier.rsimplements a heuristic message classifier that scores each incoming message along temporal, personal, technical, and emotional axes (0.0–1.0) plus a type (casual,question,task,emotional) and session state (COLD_START,WARM,CONTEXT_SHIFT,EMOTIONAL_SHIFT,TASK_MODE).crates/hive-ctx-core/src/fingerprint.rscompiles profile data into a key-value token-efficient fingerprint, tracking deltas since the last compile and automatically expanding into a full compile whenever the classifier reports context shifts, task mode, or a cold start.- Exposed addon APIs:
classify_message,fingerprint_compile(viaHiveCtxEngine) so JS clients can reuse the same session context.
The main exported struct is HiveCtxEngine:
new(storage_path: string, budget_tokens?: number)
Build the native addon into packages/bindings/:
npm install npm run build:native
Build the TypeScript bindings:
npm run build
By default the bindings load packages/bindings/hive_ctx.node. Override with:
HIVE_CTX_NATIVE_PATH=/absolute/path/to/hive_ctx.node node -e "require('./packages/bindings/dist')"cargo check