@sparkleideas/agentdb 3.0.0-alpha.3-patch.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2854 -0
- package/package.json +164 -0
- package/scripts/README.md +314 -0
- package/scripts/postinstall.cjs +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,2854 @@
|
|
|
1
|
+
# AgentDB v3
|
|
2
|
+
|
|
3
|
+
> Intelligent agentic vector database — learns from experience, optimizes itself, runs anywhere
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/agentdb)
|
|
6
|
+
[](https://www.npmjs.com/package/agentdb)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](docs/guides/)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## What is AgentDB?
|
|
14
|
+
|
|
15
|
+
**AgentDB is a vector database that gets smarter every time you use it.**
|
|
16
|
+
|
|
17
|
+
Most vector databases store and retrieve embeddings. AgentDB does that too — 150x faster — but it also watches which results your AI agent actually used, learns from that feedback, and returns better results next time. Search quality improves by up to **36% automatically**, with zero manual tuning.
|
|
18
|
+
|
|
19
|
+
Everything lives in a **single Cognitive Container** (`.rvf` file) — vectors, indexes, learning state, and a cryptographic audit trail. No external services. No API keys. No monthly bills.
|
|
20
|
+
|
|
21
|
+
> **3 lines to self-learning search:**
|
|
22
|
+
>
|
|
23
|
+
> ```typescript
|
|
24
|
+
> const backend = await SelfLearningRvfBackend.create({
|
|
25
|
+
> learning: true,
|
|
26
|
+
> storagePath: "./my.rvf",
|
|
27
|
+
> });
|
|
28
|
+
> const results = await backend.searchAsync(query, 10); // search
|
|
29
|
+
> backend.recordFeedback("q1", 0.9); // learn — next search is smarter
|
|
30
|
+
> ```
|
|
31
|
+
|
|
32
|
+
### Who is it for?
|
|
33
|
+
|
|
34
|
+
| If you're building... | AgentDB gives you... |
|
|
35
|
+
| ---------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
36
|
+
| **A RAG chatbot** | Search that learns which documents actually answer user questions |
|
|
37
|
+
| **A code review agent** | Pattern memory that remembers what worked across 1000s of reviews |
|
|
38
|
+
| **A research assistant** | Skill library that builds reusable tools from successful strategies |
|
|
39
|
+
| **An RL-powered agent** | 9 algorithms from Q-Learning to PPO, with bandit-guided algorithm selection and transfer learning |
|
|
40
|
+
| **A Claude Code / MCP tool** | 41 tools that plug in with one command |
|
|
41
|
+
| **An offline or edge app** | Full vector DB in the browser via WASM — no server needed |
|
|
42
|
+
| **An enterprise platform** | JWT auth, API key rotation, Argon2id hashing, SOC2/GDPR audit logs |
|
|
43
|
+
|
|
44
|
+
---
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
### Intelligence
|
|
49
|
+
|
|
50
|
+
| | |
|
|
51
|
+
| ------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
52
|
+
| **Self-Learning Search** | Gets 36% better over time — learns from feedback, no manual tuning |
|
|
53
|
+
| **Cognitive Memory** | 6 human-inspired patterns: learn from mistakes, build reusable skills, discover what causes what |
|
|
54
|
+
| **Decision Intelligence** | Thompson Sampling bandit auto-tunes skill selection, pattern ranking, compression tiers, and RL algorithm choice |
|
|
55
|
+
| **9 RL Algorithms** | Q-Learning, SARSA, DQN, PPO, Actor-Critic, Policy Gradient, Decision Transformer, MCTS, Model-Based RL |
|
|
56
|
+
| **Hybrid Search** | BM25 keyword + vector with Reciprocal Rank Fusion — exact matches + semantic understanding |
|
|
57
|
+
| **Graph Intelligence** | Cypher queries, causal reasoning, GNN 8-head attention (+12.4% recall) |
|
|
58
|
+
|
|
59
|
+
### Performance
|
|
60
|
+
|
|
61
|
+
| | |
|
|
62
|
+
| ----------------- | ---------------------------------------------------------------------------------- |
|
|
63
|
+
| **150x Faster** | 61us search with Rust+SIMD — 800x faster than Pinecone, 160x faster than Chroma |
|
|
64
|
+
| **4 Backends** | Auto-selects best available: RuVector (Rust) > RVF > HNSWLib (C++) > sql.js (WASM) |
|
|
65
|
+
| **Runs Anywhere** | Node.js, browsers (WASM), edge functions, fully offline |
|
|
66
|
+
| **AgentDBFast** | 50-200x faster programmatic API — skip CLI overhead for production workloads |
|
|
67
|
+
|
|
68
|
+
### Storage & Data
|
|
69
|
+
|
|
70
|
+
| | |
|
|
71
|
+
| ----------------------------- | ------------------------------------------------------------------------------------------ |
|
|
72
|
+
| **Cognitive Container (RVF)** | Single `.rvf` file — vectors, index, learning state, SHAKE-256 witness chain |
|
|
73
|
+
| **COW Branching** | Instant copy-on-write branches for experiments, with full lineage tracking |
|
|
74
|
+
| **Compression** | 5-tier auto-tiering (hot/warm/cool/cold/archive), scalar 8-bit/4-bit, product quantization |
|
|
75
|
+
| **Metadata Filtering** | 10 MongoDB-style operators ($eq, $gt, $in, $contains, $exists, etc.) |
|
|
76
|
+
|
|
77
|
+
### Integration & Tools
|
|
78
|
+
|
|
79
|
+
| | |
|
|
80
|
+
| ------------------ | ------------------------------------------------------------------------------------------ |
|
|
81
|
+
| **41 MCP Tools** | One command to connect to Claude Code, Cursor, or any MCP-compatible assistant |
|
|
82
|
+
| **Chat UI** | `@agentdb/chat` — deploy a self-learning RAG chatbot in one line |
|
|
83
|
+
| **LLM Router** | Auto-selects best provider: RuvLLM (local), OpenRouter (200+ models), Gemini, Claude, ONNX |
|
|
84
|
+
| **Real-Time Sync** | QUIC transport, multi-instance replication, 4 conflict resolution strategies |
|
|
85
|
+
|
|
86
|
+
### Enterprise & Security
|
|
87
|
+
|
|
88
|
+
| | |
|
|
89
|
+
| ----------------- | ------------------------------------------------------------------------------------------- |
|
|
90
|
+
| **Auth & Audit** | JWT tokens, Argon2id hashing, API key rotation, SOC2/GDPR/HIPAA logging |
|
|
91
|
+
| **Security** | Input validation, XSS/injection prevention, Cypher sanitization, witness chain verification |
|
|
92
|
+
| **Observability** | OpenTelemetry traces, Prometheus metrics, structured logging |
|
|
93
|
+
| **Benchmarking** | 5 built-in benchmarks, custom benchmark classes, Markdown/JSON reports |
|
|
94
|
+
|
|
95
|
+
> **Zero config, zero cost.** `npm install agentdb` and go — fully local, no API keys, no cloud fees.
|
|
96
|
+
|
|
97
|
+
### Install
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npm install agentdb # Latest stable (v2)
|
|
101
|
+
npm install agentdb@alpha # Latest alpha (v3 — unified .rvf, self-learning, SolverBandit)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Zero native dependencies.** AgentDB uses sql.js (WASM SQLite) — no Python, no C++ compiler, no `node-gyp`. Works on Windows, macOS, Linux, and CI containers out of the box. `better-sqlite3` is supported as an optional performance upgrade but never required.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Comparison
|
|
109
|
+
|
|
110
|
+
> **61 microseconds.** That's 800x faster than Pinecone, 160x faster than Chroma, and the only vector database that learns from your usage and gets better over time.
|
|
111
|
+
|
|
112
|
+
### vs. Other Vector Databases
|
|
113
|
+
|
|
114
|
+
#### Intelligence
|
|
115
|
+
|
|
116
|
+
| Feature | AgentDB v3 | Pinecone | Chroma | Weaviate | pgvector |
|
|
117
|
+
| ---------------------- | --------------------------------------- | -------- | ------ | --------- | -------- |
|
|
118
|
+
| Self-learning pipeline | SONA + contrastive + federated + bandit | No | No | No | No |
|
|
119
|
+
| Reinforcement learning | 9 algorithms (Q-Learning to MCTS) | No | No | No | No |
|
|
120
|
+
| Cognitive memory | 6 patterns | No | No | No | No |
|
|
121
|
+
| Hybrid search | BM25 + vector (RRF fusion) | No | No | BM25 only | No |
|
|
122
|
+
| Graph queries | Cypher | No | No | GraphQL | No |
|
|
123
|
+
|
|
124
|
+
#### Performance & Deployment
|
|
125
|
+
|
|
126
|
+
| Feature | AgentDB v3 | Pinecone | Chroma | Weaviate | pgvector |
|
|
127
|
+
| -------------- | ---------- | -------- | ------ | -------- | -------- |
|
|
128
|
+
| Search latency | **61us** | ~50ms | ~10ms | ~5ms | ~2ms |
|
|
129
|
+
| Runs offline | Full | No | Yes | Yes | Yes |
|
|
130
|
+
| Browser (WASM) | Yes | No | No | No | No |
|
|
131
|
+
|
|
132
|
+
#### Storage
|
|
133
|
+
|
|
134
|
+
| Feature | AgentDB v3 | Pinecone | Chroma | Weaviate | pgvector |
|
|
135
|
+
| ------------------- | ---------------------------------- | -------- | ------ | -------- | -------- |
|
|
136
|
+
| Cognitive Container | Single `.rvf` file + COW branching | No | No | No | No |
|
|
137
|
+
| Monthly cost | **$0** | $70+ | $0 | $0+ | $0+ |
|
|
138
|
+
|
|
139
|
+
#### Integration & Security
|
|
140
|
+
|
|
141
|
+
| Feature | AgentDB v3 | Pinecone | Chroma | Weaviate | pgvector |
|
|
142
|
+
| --------------- | -------------------------------- | ---------- | ------ | --------- | -------- |
|
|
143
|
+
| MCP integration | 41 tools | No | No | No | No |
|
|
144
|
+
| Auth & audit | JWT + Argon2id + compliance logs | No | No | No | No |
|
|
145
|
+
| Local-first | Yes | Cloud only | Yes | Self-host | Postgres |
|
|
146
|
+
|
|
147
|
+
### Backend Performance
|
|
148
|
+
|
|
149
|
+
| Backend | Latency | Recall@10 | Native | Best For |
|
|
150
|
+
| ----------------------------- | ------- | --------- | ------ | ---------------------------- |
|
|
151
|
+
| **RuVector** (Rust + SIMD) | 61us | 96.8% | Yes | Production, high throughput |
|
|
152
|
+
| **Cognitive Container** (RVF) | ~100us | 96.8% | Yes | Portable, branching, lineage |
|
|
153
|
+
| **HNSWLib** (C++) | ~500us | 95%+ | Yes | Compatibility |
|
|
154
|
+
| **sql.js** (WASM) | ~5ms | 90%+ | No | Zero-dependency fallback |
|
|
155
|
+
|
|
156
|
+
AgentDB auto-selects the best available: RuVector > Cognitive Container (RVF) > HNSWLib > sql.js
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Quick Start
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
npm install agentdb
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Unified AgentDB (recommended)
|
|
167
|
+
|
|
168
|
+
The `AgentDB` class is the simplest way to use AgentDB. Everything — vectors, relational data, learning state — lives in a single `.rvf` file:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
import { AgentDB } from "agentdb";
|
|
172
|
+
|
|
173
|
+
const db = new AgentDB({ dbPath: "./knowledge.rvf" });
|
|
174
|
+
await db.initialize();
|
|
175
|
+
|
|
176
|
+
// All controllers are ready — reflexion memory, skill library, causal graph
|
|
177
|
+
const reflexion = db.getController("reflexion");
|
|
178
|
+
const skills = db.getController("skills");
|
|
179
|
+
const causal = db.getController("causal");
|
|
180
|
+
|
|
181
|
+
// Store an episode
|
|
182
|
+
await reflexion.storeEpisode({
|
|
183
|
+
sessionId: "session-1",
|
|
184
|
+
task: "Fix auth bug",
|
|
185
|
+
reward: 0.95,
|
|
186
|
+
success: true,
|
|
187
|
+
critique: "OAuth2 PKCE was the right approach",
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Create a reusable skill
|
|
191
|
+
await skills.createSkill({
|
|
192
|
+
name: "jwt_auth",
|
|
193
|
+
description: "JWT authentication with refresh tokens",
|
|
194
|
+
code: "implementation...",
|
|
195
|
+
successRate: 0.92,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Save everything to one file and close
|
|
199
|
+
await db.save();
|
|
200
|
+
await db.close();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
**Configuration options:**
|
|
204
|
+
|
|
205
|
+
| Option | Default | Description |
|
|
206
|
+
| ----------------- | ------------ | ------------------------------------------------------------------------------------------ |
|
|
207
|
+
| `dbPath` | `':memory:'` | Path to `.rvf` file (or `.db` for legacy mode) |
|
|
208
|
+
| `vectorBackend` | `'rvf'` | Backend: `'rvf'` (unified), `'auto'`, `'ruvector'`, `'hnswlib'` |
|
|
209
|
+
| `vectorDimension` | `384` | Embedding dimension (384 for MiniLM, 768 for bge-base) |
|
|
210
|
+
| `forceWasm` | `false` | Force sql.js WASM backend (sql.js is the default; `better-sqlite3` used only if installed) |
|
|
211
|
+
| `enableAttention` | `false` | Enable GNN attention mechanisms |
|
|
212
|
+
|
|
213
|
+
**Controllers:** `db.getController('reflexion')` (ReflexionMemory), `db.getController('skills')` (SkillLibrary), `db.getController('causal')` (CausalMemoryGraph)
|
|
214
|
+
|
|
215
|
+
### CLI — try it in 10 seconds
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
npx agentdb init # Initialize a new database
|
|
219
|
+
npx agentdb doctor # Check system health
|
|
220
|
+
npx agentdb rvf status ./store.rvf # Inspect a Cognitive Container
|
|
221
|
+
npx agentdb rvf derive ./prod.rvf ./experiment.rvf # Branch for experiments (instant, COW)
|
|
222
|
+
npx agentdb mcp start # Start MCP server (41 tools)
|
|
223
|
+
npx @agentdb/chat serve --rvf ./kb.rvf --port 3000 # Launch chat UI
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
<details>
|
|
227
|
+
<summary><strong>Complete CLI Reference</strong> — 60+ commands across 8 categories</summary>
|
|
228
|
+
|
|
229
|
+
#### Core
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
npx agentdb init # Initialize database
|
|
233
|
+
npx agentdb status # System status and diagnostics
|
|
234
|
+
npx agentdb doctor [--fix] [--verbose] # Health check with auto-fix
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
#### Cognitive Container (RVF) Management
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
npx agentdb rvf status ./store.rvf # Container statistics
|
|
241
|
+
npx agentdb rvf compact ./store.rvf # Reclaim space
|
|
242
|
+
npx agentdb rvf derive ./src.rvf ./dst.rvf # COW branch
|
|
243
|
+
npx agentdb rvf segments ./store.rvf # Segment introspection
|
|
244
|
+
npx agentdb rvf detect # SDK availability
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
#### Learning & Training
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
npx agentdb learn --session <id> --train # Train a learning session
|
|
251
|
+
npx agentdb learn --status # Learning system status
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### Routing
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
npx agentdb route --query "search term" # Test semantic routing
|
|
258
|
+
npx agentdb route --list # Show learned intents
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
#### Attention & Hyperbolic
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
npx agentdb attention --benchmark # Attention performance test
|
|
265
|
+
npx agentdb hyperbolic --test # Hyperbolic embedding test
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
#### Simulation
|
|
269
|
+
|
|
270
|
+
```bash
|
|
271
|
+
npx agentdb simulate hnsw --iterations 3 # HNSW optimization
|
|
272
|
+
npx agentdb simulate attention --iterations 3 # GNN attention
|
|
273
|
+
npx agentdb simulate self-organizing --days 30 # Self-healing
|
|
274
|
+
npx agentdb simulate --wizard # Interactive wizard
|
|
275
|
+
npx agentdb simulate --custom ./my-scenario.json # Custom simulation
|
|
276
|
+
npx agentdb simulate --report ./output.md # Generate report
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
#### Migration & Import
|
|
280
|
+
|
|
281
|
+
```bash
|
|
282
|
+
npx agentdb migrate --source ./old.db --target ./new.db # Full v1→v2 migration
|
|
283
|
+
npx agentdb migrate --source ./v2.db --to v3 --rvf-path ./unified.rvf # v2→v3 unified .rvf
|
|
284
|
+
npx agentdb migrate --to rvf --rvf-path ./out.rvf # Export to Cognitive Container
|
|
285
|
+
npx agentdb migrate --dry-run --verbose # Preview changes
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
#### MCP Server
|
|
289
|
+
|
|
290
|
+
```bash
|
|
291
|
+
npx agentdb mcp start # Start MCP server (41 tools)
|
|
292
|
+
npx agentdb mcp start --port 8080 # Custom port
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
</details>
|
|
296
|
+
|
|
297
|
+
### Self-Learning Vector Search (the differentiator)
|
|
298
|
+
|
|
299
|
+
Most vector databases return the same results no matter how many times you query them. AgentDB closes the loop: after every search, you tell it which results were actually useful. A contrastive trainer (InfoNCE + hard-negative mining) adjusts a lightweight LoRA adapter in under 1 ms, and EWC++ consolidation keeps old knowledge intact. Over 500 feedback cycles, recall jumps from 54% to 90% — automatically.
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { SelfLearningRvfBackend } from "agentdb/backends/rvf/SelfLearningRvfBackend";
|
|
303
|
+
|
|
304
|
+
// Create a self-learning Cognitive Container
|
|
305
|
+
const backend = await SelfLearningRvfBackend.create({
|
|
306
|
+
dimension: 384,
|
|
307
|
+
metric: "cosine",
|
|
308
|
+
storagePath: "./knowledge.rvf",
|
|
309
|
+
learning: true, // enable the learning pipeline
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
// Insert documents
|
|
313
|
+
await backend.insertAsync("doc-1", embedding, { title: "Auth Guide" });
|
|
314
|
+
await backend.insertAsync("doc-2", embedding2, { title: "API Reference" });
|
|
315
|
+
|
|
316
|
+
// Search — automatically routed through the learning pipeline
|
|
317
|
+
const results = await backend.searchAsync(queryEmbedding, 10);
|
|
318
|
+
|
|
319
|
+
// Tell AgentDB which results were useful — this drives learning
|
|
320
|
+
backend.recordFeedback("query-1", 0.95); // great result
|
|
321
|
+
backend.recordFeedback("query-2", 0.2); // poor result
|
|
322
|
+
|
|
323
|
+
// Run a learning tick — trains the model, updates adapters
|
|
324
|
+
await backend.tick();
|
|
325
|
+
// Next search will be smarter based on your feedback
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Cognitive Memory (pattern learning)
|
|
329
|
+
|
|
330
|
+
Store and retrieve reasoning patterns — your agent remembers what worked:
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
import { createDatabase, ReasoningBank, EmbeddingService } from "agentdb";
|
|
334
|
+
|
|
335
|
+
const db = await createDatabase("./agent-memory.db");
|
|
336
|
+
const embedder = new EmbeddingService({ model: "Xenova/all-MiniLM-L6-v2" });
|
|
337
|
+
await embedder.initialize();
|
|
338
|
+
|
|
339
|
+
const reasoningBank = new ReasoningBank(db, embedder);
|
|
340
|
+
|
|
341
|
+
// Store what your agent learned
|
|
342
|
+
await reasoningBank.storePattern({
|
|
343
|
+
taskType: "code_review",
|
|
344
|
+
approach: "Security-first analysis",
|
|
345
|
+
successRate: 0.95,
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
// Find similar successful patterns
|
|
349
|
+
const patterns = await reasoningBank.searchPatterns({
|
|
350
|
+
task: "security code review",
|
|
351
|
+
k: 10,
|
|
352
|
+
});
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
### Unified Mode — Everything in One File
|
|
356
|
+
|
|
357
|
+
In v3, `AgentDB` stores vectors, relational data, learning state, and metadata in a single `.rvf` file:
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
import { AgentDB } from "agentdb";
|
|
361
|
+
|
|
362
|
+
// Single-file mode is the default — no configuration needed
|
|
363
|
+
const db = new AgentDB({ dbPath: "./agent.rvf" });
|
|
364
|
+
await db.initialize();
|
|
365
|
+
|
|
366
|
+
// Access the underlying database for advanced queries
|
|
367
|
+
const raw = db.database;
|
|
368
|
+
const stats = raw.prepare("SELECT COUNT(*) as c FROM episodes").get();
|
|
369
|
+
|
|
370
|
+
// Check mode
|
|
371
|
+
console.log(db.isUnifiedMode); // true — everything in one .rvf file
|
|
372
|
+
console.log(db.vectorBackendName); // 'rvf'
|
|
373
|
+
|
|
374
|
+
// Save and reopen — all data persists in agent.rvf
|
|
375
|
+
await db.save();
|
|
376
|
+
await db.close();
|
|
377
|
+
|
|
378
|
+
const db2 = new AgentDB({ dbPath: "./agent.rvf" });
|
|
379
|
+
await db2.initialize(); // All episodes, skills, vectors restored
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Chat UI (one-line deployment)
|
|
383
|
+
|
|
384
|
+
Deploy a self-learning RAG chatbot over any Cognitive Container:
|
|
385
|
+
|
|
386
|
+
```bash
|
|
387
|
+
npx @agentdb/chat serve --rvf ./kb.rvf --port 3000
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
---
|
|
391
|
+
|
|
392
|
+
## How the Learning Loop Works
|
|
393
|
+
|
|
394
|
+
Every search automatically triggers a learning cycle. No configuration needed — just call `recordFeedback()` after using results:
|
|
395
|
+
|
|
396
|
+
```mermaid
|
|
397
|
+
graph LR
|
|
398
|
+
A["Your Query"] --> B["Semantic Router<br/>classifies intent"]
|
|
399
|
+
B --> C["Embedding Adapter<br/>adjusts in <1ms"]
|
|
400
|
+
C --> D["HNSW Search<br/>61us, 96.8% recall"]
|
|
401
|
+
D --> E["Results"]
|
|
402
|
+
E --> F["Your Feedback<br/>0.0 - 1.0"]
|
|
403
|
+
F --> G["Contrastive Trainer<br/>learns what's good"]
|
|
404
|
+
G --> C
|
|
405
|
+
style G fill:#2d6,stroke:#1a4,color:#fff
|
|
406
|
+
style A fill:#36f,stroke:#24d,color:#fff
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
> **Result:** Over 10 sessions with 50 episodes each, search quality improved from 54% to 90% — a 36% gain with zero manual tuning.
|
|
410
|
+
|
|
411
|
+
## Architecture
|
|
412
|
+
|
|
413
|
+
```mermaid
|
|
414
|
+
graph TD
|
|
415
|
+
A[Your Application] --> B[AgentDB Core]
|
|
416
|
+
B --> C[Cognitive Memory<br/>6 patterns]
|
|
417
|
+
B --> D[Self-Learning Pipeline<br/>SONA + Contrastive + Federated]
|
|
418
|
+
B --> E[Backend Auto-Selection]
|
|
419
|
+
E --> F[RuVector<br/>Rust+SIMD 61us]
|
|
420
|
+
E --> G[Cognitive Container<br/>RVF single-file COW]
|
|
421
|
+
E --> H[HNSWLib<br/>C++ HNSW]
|
|
422
|
+
E --> I[sql.js<br/>WASM fallback]
|
|
423
|
+
B --> J[NativeAccelerator<br/>15 capability groups]
|
|
424
|
+
B --> K[MCP Server<br/>41 tools]
|
|
425
|
+
B --> L[Graph Intelligence<br/>Cypher + GNN]
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
---
|
|
429
|
+
|
|
430
|
+
## MCP Integration
|
|
431
|
+
|
|
432
|
+
> **One command** to give any AI assistant access to self-learning vector search, cognitive memory, and 37 specialized tools.
|
|
433
|
+
|
|
434
|
+
Connect AgentDB to Claude Code, Cursor, or any MCP-compatible AI assistant:
|
|
435
|
+
|
|
436
|
+
```bash
|
|
437
|
+
claude mcp add agentdb npx agentdb mcp start
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Or add to `~/.config/claude/claude_desktop_config.json`:
|
|
441
|
+
|
|
442
|
+
```json
|
|
443
|
+
{
|
|
444
|
+
"mcpServers": {
|
|
445
|
+
"agentdb": {
|
|
446
|
+
"command": "npx",
|
|
447
|
+
"args": ["agentdb", "mcp", "start"],
|
|
448
|
+
"env": { "AGENTDB_PATH": "./agentdb.rvf" }
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
### MCP Tools Overview
|
|
455
|
+
|
|
456
|
+
| Category | Tools | Examples |
|
|
457
|
+
| ------------ | ----- | ------------------------------------------------------------------------------------------------ |
|
|
458
|
+
| **Core DB** | 6 | init, insert, batch insert, search, delete, stats |
|
|
459
|
+
| **Patterns** | 4 | store, search, batch store, statistics |
|
|
460
|
+
| **Memory** | 9 | reflexion store/retrieve/batch, skill create/search/batch, causal edges/queries, nightly learner |
|
|
461
|
+
| **Learning** | 10 | RL sessions, predictions, feedback, training, transfer learning, explainability |
|
|
462
|
+
| **Recall** | 2 | explainable recall with provenance certificates, cache management |
|
|
463
|
+
| **Solver** | 4 | solver train, acceptance testing, policy inspection, witness chain audit |
|
|
464
|
+
| **Admin** | 6 | diagnostics, migration, pruning, experience recording, reward signals, legacy stats |
|
|
465
|
+
|
|
466
|
+
All 41 tools support parallel execution markers, batch operations, intelligent caching, and format parameters for token reduction.
|
|
467
|
+
|
|
468
|
+
---
|
|
469
|
+
|
|
470
|
+
## Chat Ecosystem
|
|
471
|
+
|
|
472
|
+
The `@agentdb/chat` package provides a one-line chat UI with self-learning RAG built on top of AgentDB:
|
|
473
|
+
|
|
474
|
+
```bash
|
|
475
|
+
# Serve a chat interface over your knowledge base
|
|
476
|
+
npx @agentdb/chat serve --rvf ./kb.rvf --port 3000
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
Programmatic usage:
|
|
480
|
+
|
|
481
|
+
```typescript
|
|
482
|
+
import { createChatServer } from "@agentdb/chat";
|
|
483
|
+
|
|
484
|
+
const server = await createChatServer("./kb.rvf", {
|
|
485
|
+
port: 3000,
|
|
486
|
+
learning: true,
|
|
487
|
+
});
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
The chat ecosystem includes:
|
|
491
|
+
|
|
492
|
+
- **Chat Server** — streaming inference with any LLM provider
|
|
493
|
+
- **Chat Persistence** — conversation history stored in Cognitive Containers
|
|
494
|
+
- **MCP Bridge** — expose all 37 AgentDB tools through chat
|
|
495
|
+
- **Chat UI** (`@agentdb/chat-ui`) — SvelteKit frontend with real-time tool invocation
|
|
496
|
+
|
|
497
|
+
See the [@agentdb/chat README](../agentdb-chat/README.md) for full documentation.
|
|
498
|
+
|
|
499
|
+
---
|
|
500
|
+
|
|
501
|
+
## Deep Dives
|
|
502
|
+
|
|
503
|
+
> Expand any section below for code examples, configuration, and architecture details.
|
|
504
|
+
|
|
505
|
+
**Getting Started** | [Guides](#guides) | [Tutorials](#tutorials) | [Advanced Usage](#advanced-usage)
|
|
506
|
+
|
|
507
|
+
**Intelligence & Learning** | [Self-Learning Pipeline](#self-learning-pipeline) | [Cognitive Memory](#cognitive-memory-patterns) | [Reinforcement Learning](#reinforcement-learning) | [Hybrid Search](#hybrid-search) | [Graph Intelligence](#graph-intelligence) | [Query Lifecycle](#query-lifecycle)
|
|
508
|
+
|
|
509
|
+
**Performance & Architecture** | [SIMD Acceleration](#simd--native-acceleration) | [Browser & WASM](#browser--wasm-deployment) | [Benchmarks](#performance--benchmarks) | [AgentDBFast](#agentdbfast) | [Benchmark Suite](#benchmark-suite)
|
|
510
|
+
|
|
511
|
+
**Storage & Data** | [Cognitive Container (RVF)](#cognitive-container-rvf-format) | [Quantization](#quantization--compression) | [Metadata Filtering](#metadata-filtering)
|
|
512
|
+
|
|
513
|
+
**Integration & Tools** | [LLM Router](#llm-router) | [Model Import/Export](#model-importexport--embedding-models) | [Multi-Database Sync](#multi-database-coordination) | [Real-Time Sync](#real-time-sync-quic)
|
|
514
|
+
|
|
515
|
+
**Enterprise & Security** | [Security](#security) | [Auth & Audit](#auth--audit) | [Observability](#observability)
|
|
516
|
+
|
|
517
|
+
---
|
|
518
|
+
|
|
519
|
+
### Getting Started
|
|
520
|
+
|
|
521
|
+
Step-by-step guides, tutorials, and advanced configuration for every skill level.
|
|
522
|
+
|
|
523
|
+
<details>
|
|
524
|
+
<summary><strong>Guides</strong> — backend selection, migration, health checks, federated learning</summary>
|
|
525
|
+
|
|
526
|
+
#### Backend Selection
|
|
527
|
+
|
|
528
|
+
```bash
|
|
529
|
+
# Auto-detect best backend (recommended)
|
|
530
|
+
npx agentdb init --backend auto
|
|
531
|
+
|
|
532
|
+
# Force RuVector (fastest, requires native bindings)
|
|
533
|
+
npx agentdb init --backend ruvector
|
|
534
|
+
|
|
535
|
+
# Force Cognitive Container (single-file, portable)
|
|
536
|
+
npx agentdb init --backend rvf --rvf-path ./vectors.rvf
|
|
537
|
+
|
|
538
|
+
# Force HNSWLib (C++ HNSW)
|
|
539
|
+
npx agentdb init --backend hnswlib
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### Migration
|
|
543
|
+
|
|
544
|
+
```bash
|
|
545
|
+
# Migrate v1 → v2
|
|
546
|
+
npx agentdb migrate --source ./old.db --target ./new.db
|
|
547
|
+
|
|
548
|
+
# Migrate v2 → v3 unified .rvf (same schemas, data copy)
|
|
549
|
+
npx agentdb migrate --source ./v2.db --to v3 --rvf-path ./unified.rvf
|
|
550
|
+
|
|
551
|
+
# Export to Cognitive Container
|
|
552
|
+
npx agentdb migrate --to rvf --rvf-path ./vectors.rvf
|
|
553
|
+
|
|
554
|
+
# Dry-run migration
|
|
555
|
+
npx agentdb migrate --dry-run --verbose
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
#### Health Checks
|
|
559
|
+
|
|
560
|
+
```bash
|
|
561
|
+
# Full diagnostic
|
|
562
|
+
npx agentdb doctor --verbose
|
|
563
|
+
|
|
564
|
+
# Check specific Cognitive Container
|
|
565
|
+
npx agentdb doctor --rvf-path ./vectors.rvf
|
|
566
|
+
|
|
567
|
+
# Auto-fix issues
|
|
568
|
+
npx agentdb doctor --fix
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
#### Federated Learning Setup
|
|
572
|
+
|
|
573
|
+
Configure cross-session federated learning for continuous improvement:
|
|
574
|
+
|
|
575
|
+
```typescript
|
|
576
|
+
import {
|
|
577
|
+
FederatedLearningCoordinator,
|
|
578
|
+
EphemeralLearningAgent,
|
|
579
|
+
} from "agentdb/services/federated-learning";
|
|
580
|
+
|
|
581
|
+
const coordinator = new FederatedLearningCoordinator({
|
|
582
|
+
agentId: "coordinator-1",
|
|
583
|
+
minQuality: 0.7,
|
|
584
|
+
maxAgents: 100,
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
const agent = new EphemeralLearningAgent({
|
|
588
|
+
agentId: "session-agent-1",
|
|
589
|
+
qualityFiltering: true,
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
const state = agent.exportState();
|
|
593
|
+
await coordinator.aggregate(state);
|
|
594
|
+
const merged = await coordinator.consolidate();
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
</details>
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
601
|
+
<details>
|
|
602
|
+
<summary><strong>Tutorials</strong> — 6 step-by-step walkthroughs from beginner to advanced</summary>
|
|
603
|
+
|
|
604
|
+
#### Tutorial 1: Build a Learning Code Review Agent
|
|
605
|
+
|
|
606
|
+
```typescript
|
|
607
|
+
import {
|
|
608
|
+
createDatabase,
|
|
609
|
+
ReasoningBank,
|
|
610
|
+
ReflexionMemory,
|
|
611
|
+
EmbeddingService,
|
|
612
|
+
} from "agentdb";
|
|
613
|
+
|
|
614
|
+
const db = await createDatabase("./code-reviewer.db");
|
|
615
|
+
const embedder = new EmbeddingService({ model: "Xenova/all-MiniLM-L6-v2" });
|
|
616
|
+
await embedder.initialize();
|
|
617
|
+
|
|
618
|
+
const reasoningBank = new ReasoningBank(db, embedder);
|
|
619
|
+
const reflexion = new ReflexionMemory(db, embedder);
|
|
620
|
+
|
|
621
|
+
await reasoningBank.storePattern({
|
|
622
|
+
taskType: "code_review",
|
|
623
|
+
approach: "Security scan > Type safety > Code quality > Performance",
|
|
624
|
+
successRate: 0.94,
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
const result = await performCodeReview(code);
|
|
628
|
+
await reflexion.storeEpisode({
|
|
629
|
+
sessionId: "review-1",
|
|
630
|
+
task: "Review authentication PR",
|
|
631
|
+
reward: result.issuesFound > 0 ? 0.9 : 0.6,
|
|
632
|
+
success: true,
|
|
633
|
+
critique: "Found SQL injection — security checks work!",
|
|
634
|
+
input: code,
|
|
635
|
+
output: result.findings,
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
const pastReviews = await reflexion.retrieveRelevant({
|
|
639
|
+
task: "authentication code review",
|
|
640
|
+
k: 5,
|
|
641
|
+
onlySuccesses: true,
|
|
642
|
+
});
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
#### Tutorial 2: RAG with Self-Learning Skills
|
|
646
|
+
|
|
647
|
+
```typescript
|
|
648
|
+
import { createDatabase, SkillLibrary, EmbeddingService } from "agentdb";
|
|
649
|
+
|
|
650
|
+
const db = await createDatabase("./rag-system.db");
|
|
651
|
+
const embedder = new EmbeddingService({ model: "Xenova/all-MiniLM-L6-v2" });
|
|
652
|
+
await embedder.initialize();
|
|
653
|
+
|
|
654
|
+
const skills = new SkillLibrary(db, embedder);
|
|
655
|
+
|
|
656
|
+
await skills.createSkill({
|
|
657
|
+
name: "expand_query",
|
|
658
|
+
description: "Expand user query with domain-specific synonyms",
|
|
659
|
+
signature: { inputs: { query: "string" }, outputs: { expanded: "string[]" } },
|
|
660
|
+
code: `const map = { 'bug': ['issue', 'defect', 'error'] }; ...`,
|
|
661
|
+
successRate: 0.92,
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
const applicable = await skills.searchSkills({
|
|
665
|
+
task: "find technical documentation",
|
|
666
|
+
k: 10,
|
|
667
|
+
});
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
#### Tutorial 3: Run Latent Space Simulations
|
|
671
|
+
|
|
672
|
+
```bash
|
|
673
|
+
npx agentdb simulate hnsw --iterations 3
|
|
674
|
+
npx agentdb simulate attention --iterations 3
|
|
675
|
+
npx agentdb simulate self-organizing --days 30
|
|
676
|
+
npx agentdb simulate --wizard
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
#### Tutorial 4: Cognitive Container Branching
|
|
680
|
+
|
|
681
|
+
```typescript
|
|
682
|
+
import { RvfBackend } from "agentdb/backends/rvf/RvfBackend";
|
|
683
|
+
|
|
684
|
+
const main = new RvfBackend({
|
|
685
|
+
dimension: 384,
|
|
686
|
+
metric: "cosine",
|
|
687
|
+
storagePath: "./main.rvf",
|
|
688
|
+
});
|
|
689
|
+
await main.initialize();
|
|
690
|
+
await main.insertAsync("doc-1", embedding1, { source: "production" });
|
|
691
|
+
await main.flush();
|
|
692
|
+
|
|
693
|
+
// Branch for experiment (copy-on-write, near-instant)
|
|
694
|
+
const experiment = await main.derive("./experiment.rvf");
|
|
695
|
+
await experiment.insertAsync("doc-exp-1", experimentEmbedding);
|
|
696
|
+
console.log(`Lineage depth: ${await experiment.lineageDepth()}`);
|
|
697
|
+
```
|
|
698
|
+
|
|
699
|
+
#### Tutorial 5: Self-Learning RAG Pipeline
|
|
700
|
+
|
|
701
|
+
```typescript
|
|
702
|
+
import { SelfLearningRvfBackend } from "agentdb/backends/rvf/SelfLearningRvfBackend";
|
|
703
|
+
|
|
704
|
+
const backend = await SelfLearningRvfBackend.create({
|
|
705
|
+
dimension: 384,
|
|
706
|
+
metric: "cosine",
|
|
707
|
+
storagePath: "./rag-vectors.rvf",
|
|
708
|
+
learning: true,
|
|
709
|
+
federated: true,
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
for (const doc of documents) {
|
|
713
|
+
await backend.insertAsync(doc.id, doc.embedding, { title: doc.title });
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const results = await backend.searchAsync(queryEmbedding, 10);
|
|
717
|
+
backend.recordFeedback("query-1", 0.95);
|
|
718
|
+
await backend.tick();
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
#### Tutorial 6: Chat UI with Custom Knowledge Base
|
|
722
|
+
|
|
723
|
+
```bash
|
|
724
|
+
npx agentdb init --backend rvf --rvf-path ./docs.rvf
|
|
725
|
+
npx agentdb import ./docs/ --rvf-path ./docs.rvf
|
|
726
|
+
npx @agentdb/chat serve --rvf ./docs.rvf --port 3000
|
|
727
|
+
```
|
|
728
|
+
|
|
729
|
+
</details>
|
|
730
|
+
|
|
731
|
+
---
|
|
732
|
+
|
|
733
|
+
<details>
|
|
734
|
+
<summary><strong>Advanced Usage</strong> — batch ops, caching, solver tuning, GNN training, context synthesis</summary>
|
|
735
|
+
|
|
736
|
+
#### Batch Operations
|
|
737
|
+
|
|
738
|
+
```typescript
|
|
739
|
+
import { BatchOperations } from "agentdb/optimizations/BatchOperations";
|
|
740
|
+
|
|
741
|
+
const batchOps = new BatchOperations(db, embedder, {
|
|
742
|
+
batchSize: 100,
|
|
743
|
+
parallelism: 4,
|
|
744
|
+
progressCallback: (done, total) => console.log(`${done}/${total}`),
|
|
745
|
+
});
|
|
746
|
+
|
|
747
|
+
await batchOps.insertSkills([...skills]);
|
|
748
|
+
await batchOps.insertEpisodes([...episodes]);
|
|
749
|
+
await batchOps.insertPatterns([...patterns]);
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
#### Custom Backend Selection
|
|
753
|
+
|
|
754
|
+
```typescript
|
|
755
|
+
import { createBackend, detectBackends } from "agentdb/backends/factory";
|
|
756
|
+
|
|
757
|
+
const detection = await detectBackends();
|
|
758
|
+
// { ruvector: { available, native, gnn }, rvf: { sdk, node, wasm }, hnswlib: { available } }
|
|
759
|
+
|
|
760
|
+
const backend = await createBackend({
|
|
761
|
+
type: "rvf",
|
|
762
|
+
dimension: 384,
|
|
763
|
+
metric: "cosine",
|
|
764
|
+
storagePath: "./vectors.rvf",
|
|
765
|
+
});
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
#### Adaptive Index Tuning
|
|
769
|
+
|
|
770
|
+
```typescript
|
|
771
|
+
import { AgentDBSolver } from "agentdb/backends/rvf/RvfSolver";
|
|
772
|
+
|
|
773
|
+
const solver = await AgentDBSolver.create();
|
|
774
|
+
const trainResult = solver.train({
|
|
775
|
+
count: 50,
|
|
776
|
+
minDifficulty: 1,
|
|
777
|
+
maxDifficulty: 10,
|
|
778
|
+
});
|
|
779
|
+
const policy = solver.policy();
|
|
780
|
+
// 18 context buckets (3 range x 3 distractor x 2 noise)
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
#### Federated Session Manager
|
|
784
|
+
|
|
785
|
+
```typescript
|
|
786
|
+
import {
|
|
787
|
+
FederatedLearningCoordinator,
|
|
788
|
+
EphemeralLearningAgent,
|
|
789
|
+
} from "agentdb/services/federated-learning";
|
|
790
|
+
|
|
791
|
+
const coordinator = new FederatedLearningCoordinator({
|
|
792
|
+
agentId: "coord-main",
|
|
793
|
+
minQuality: 0.7,
|
|
794
|
+
maxAgents: 100,
|
|
795
|
+
});
|
|
796
|
+
const sessionAgent = new EphemeralLearningAgent({
|
|
797
|
+
agentId: `session-${Date.now()}`,
|
|
798
|
+
qualityFiltering: true,
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
await sessionAgent.processTask(embedding, 0.9);
|
|
802
|
+
const state = sessionAgent.exportState();
|
|
803
|
+
await coordinator.aggregate(state);
|
|
804
|
+
const merged = await coordinator.consolidate();
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
#### Context Synthesizer
|
|
808
|
+
|
|
809
|
+
```typescript
|
|
810
|
+
import { ContextSynthesizer } from "agentdb/controllers/ContextSynthesizer";
|
|
811
|
+
|
|
812
|
+
const synthesizer = new ContextSynthesizer();
|
|
813
|
+
const context = await synthesizer.synthesize(memories);
|
|
814
|
+
// { summary, patterns, successRate, averageReward, recommendations, keyInsights }
|
|
815
|
+
```
|
|
816
|
+
|
|
817
|
+
#### GNN Learning Backend
|
|
818
|
+
|
|
819
|
+
```typescript
|
|
820
|
+
import { LearningBackend } from "agentdb/backends/LearningBackend";
|
|
821
|
+
|
|
822
|
+
const learner = new LearningBackend({ dimension: 384 });
|
|
823
|
+
const enhanced = learner.enhance(queryEmbedding, neighborEmbeddings, weights);
|
|
824
|
+
learner.addSample(embedding, "positive", 1.0, { source: "feedback" });
|
|
825
|
+
const result = await learner.train(10);
|
|
826
|
+
// { epochs: 10, finalLoss: 0.023, improvement: 15.2, durationMs: 340 }
|
|
827
|
+
```
|
|
828
|
+
|
|
829
|
+
</details>
|
|
830
|
+
|
|
831
|
+
---
|
|
832
|
+
|
|
833
|
+
### Intelligence & Learning
|
|
834
|
+
|
|
835
|
+
How AgentDB learns, reasons, and improves over time — from self-learning search to reinforcement learning and causal graphs.
|
|
836
|
+
|
|
837
|
+
<details>
|
|
838
|
+
<summary><strong>Self-Learning Pipeline</strong> — how search improves 36% automatically</summary>
|
|
839
|
+
|
|
840
|
+
AgentDB's self-learning pipeline continuously improves search quality through a six-phase cycle:
|
|
841
|
+
|
|
842
|
+
```mermaid
|
|
843
|
+
flowchart LR
|
|
844
|
+
Q[Query] --> R[Semantic Router]
|
|
845
|
+
R --> S[SONA Enhancement]
|
|
846
|
+
S --> T[HNSW Search]
|
|
847
|
+
T --> U[Results]
|
|
848
|
+
U --> V[Feedback]
|
|
849
|
+
V --> W[Contrastive Trainer<br/>InfoNCE + Hard Negatives]
|
|
850
|
+
W --> X[Micro-LoRA Update]
|
|
851
|
+
X --> Y[EWC++ Consolidation]
|
|
852
|
+
Y --> Z[Federated Aggregation]
|
|
853
|
+
```
|
|
854
|
+
|
|
855
|
+
### 1. Semantic Query Routing
|
|
856
|
+
|
|
857
|
+
The `SemanticQueryRouter` classifies query intent by comparing against learned intent centroids using efficient min-heap top-k selection (O(n log k)). It lazily loads `@ruvector/router` for sub-millisecond HNSW routing and falls back to brute-force cosine with pre-computed norms when native bindings aren't available.
|
|
858
|
+
|
|
859
|
+
The router also selects the adaptive ef-search arm for the query. Four ef-search levels (50, 100, 200, 400) are chosen based on the solver's Thompson Sampling policy for the matching context bucket.
|
|
860
|
+
|
|
861
|
+
```typescript
|
|
862
|
+
// Router learns intents from usage patterns
|
|
863
|
+
router.addIntent("technical_search", embedding); // up to 100 exemplars per intent
|
|
864
|
+
router.addIntent("conversational", embedding);
|
|
865
|
+
const { intent, confidence } = await router.route(queryEmbedding, k);
|
|
866
|
+
|
|
867
|
+
// Router state persists across restarts (debounced 5s save)
|
|
868
|
+
// Max 1,000 intents, JSON fallback or native persistence
|
|
869
|
+
```
|
|
870
|
+
|
|
871
|
+
### 2. SONA Enhancement
|
|
872
|
+
|
|
873
|
+
Context enrichment from trajectory history. The SONA engine (`@ruvector/sona`) tracks query trajectories and enriches embeddings with session context before search. SONA also applies the base LoRA adapter to the query embedding, adjusting it in sub-millisecond time based on learned patterns.
|
|
874
|
+
|
|
875
|
+
### 3. Contrastive Training
|
|
876
|
+
|
|
877
|
+
InfoNCE contrastive loss with hard negative mining inspired by NV-Retriever's positive-aware filtering:
|
|
878
|
+
|
|
879
|
+
- **Loss function:** `L = -log(exp(sim(anchor, positive) / τ) / Σ exp(sim(anchor, negative_i) / τ))`
|
|
880
|
+
- **Hard negative filtering:** Rejects candidates where `sim(candidate, positive) > 0.85`, eliminating ~70% of false negatives
|
|
881
|
+
- **3-stage curriculum:** Progressively increases difficulty — negatives per sample (4 → 8 → 16), hardness threshold (0.5 → 0.3 → 0.1), 100 batches per stage
|
|
882
|
+
- **AdamW optimizer:** β1=0.9, β2=0.999, decoupled weight decay, full bias correction
|
|
883
|
+
- **Temperature:** Bounded 0.01-1.0 (default: 0.07)
|
|
884
|
+
|
|
885
|
+
```typescript
|
|
886
|
+
// ContrastiveTrainer handles hard negative mining automatically
|
|
887
|
+
const result = trainer.trainBatch([
|
|
888
|
+
{
|
|
889
|
+
anchor: queryEmb,
|
|
890
|
+
positive: goodResultEmb,
|
|
891
|
+
negatives: [badResult1, badResult2],
|
|
892
|
+
},
|
|
893
|
+
]);
|
|
894
|
+
// result: { loss, gradients, batchSize }
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
When native SIMD is available, cosine similarity and AdamW steps delegate to the NativeAccelerator for hardware-accelerated computation.
|
|
898
|
+
|
|
899
|
+
### 4. Micro-LoRA Adaptation
|
|
900
|
+
|
|
901
|
+
Sub-millisecond embedding adjustment per query. A lightweight projection matrix (W: d×d + bias: d) is applied to the query embedding before search, specializing for each user's query patterns without modifying the base model. LoRA adapters are updated after each contrastive training batch.
|
|
902
|
+
|
|
903
|
+
### 5. EWC++ Consolidation
|
|
904
|
+
|
|
905
|
+
Elastic Weight Consolidation prevents catastrophic forgetting by penalizing changes to important parameters. The Fisher information matrix is updated incrementally via the NativeAccelerator's `ewcUpdateFisher()` and penalty is computed via `ewcPenalty()`, ensuring previously learned patterns survive new training.
|
|
906
|
+
|
|
907
|
+
### 6. Federated Session Aggregation
|
|
908
|
+
|
|
909
|
+
Cross-session LoRA merging for continuous improvement. Lightweight `EphemeralLearningAgent` instances (~5MB each) export their state to a `FederatedLearningCoordinator` for quality-filtered aggregation (minimum quality threshold: 0.7, up to 100 agents).
|
|
910
|
+
|
|
911
|
+
### How `tick()` Works
|
|
912
|
+
|
|
913
|
+
Each learning tick performs these steps in sequence:
|
|
914
|
+
|
|
915
|
+
1. **Flush SONA** — persist trajectory context
|
|
916
|
+
2. **Train contrastive batch** — 32 samples from recorded feedback
|
|
917
|
+
3. **Train solver** — 50 puzzles to update Thompson Sampling policy
|
|
918
|
+
4. **Decay access frequencies** — multiply by 0.99, prune entries below 0.001 (every 50 ticks)
|
|
919
|
+
5. **Update temporal compressor** — adjust compression tiers
|
|
920
|
+
6. **Assess health** — latency tracking via health monitor
|
|
921
|
+
7. **Consolidate federated state** — merge distributed agent states
|
|
922
|
+
8. **Acceptance check** — every 100 ticks, run A/B/C ablation (mode A: fixed heuristic, mode B: compiler-suggested, mode C: learned Thompson Sampling) to validate adaptive ef-search
|
|
923
|
+
|
|
924
|
+
### Full Example
|
|
925
|
+
|
|
926
|
+
```typescript
|
|
927
|
+
import { SelfLearningRvfBackend } from "agentdb/backends/rvf/SelfLearningRvfBackend";
|
|
928
|
+
|
|
929
|
+
const backend = await SelfLearningRvfBackend.create({
|
|
930
|
+
dimension: 384,
|
|
931
|
+
metric: "cosine",
|
|
932
|
+
storagePath: "./vectors.rvf",
|
|
933
|
+
learning: true,
|
|
934
|
+
federated: true,
|
|
935
|
+
positiveThreshold: 0.7,
|
|
936
|
+
negativeThreshold: 0.3,
|
|
937
|
+
trainingBatchSize: 32,
|
|
938
|
+
tickIntervalMs: 5000,
|
|
939
|
+
});
|
|
940
|
+
|
|
941
|
+
// Insert vectors
|
|
942
|
+
await backend.insertAsync("doc-1", embedding, { source: "docs" });
|
|
943
|
+
|
|
944
|
+
// Search (automatically enhanced by SONA + router)
|
|
945
|
+
const results = await backend.searchAsync(queryEmbedding, 10);
|
|
946
|
+
|
|
947
|
+
// Record feedback to drive learning
|
|
948
|
+
backend.recordFeedback("query-123", 0.9); // high quality result
|
|
949
|
+
|
|
950
|
+
// Run a learning tick (contrastive training + LoRA update + EWC consolidation)
|
|
951
|
+
await backend.tick();
|
|
952
|
+
|
|
953
|
+
// Check learning progress
|
|
954
|
+
const stats = backend.getLearningStats();
|
|
955
|
+
console.log(
|
|
956
|
+
`Trajectories: ${stats.trajectoryCount}, LoRA rank: ${stats.loraRank}`,
|
|
957
|
+
);
|
|
958
|
+
```
|
|
959
|
+
|
|
960
|
+
</details>
|
|
961
|
+
|
|
962
|
+
---
|
|
963
|
+
|
|
964
|
+
<details>
|
|
965
|
+
<summary><strong>Cognitive Memory Patterns</strong> — 6 ways your agent learns like a human</summary>
|
|
966
|
+
|
|
967
|
+
AgentDB implements six cognitive memory patterns inspired by how humans learn:
|
|
968
|
+
|
|
969
|
+
### 1. ReasoningBank — Pattern Learning
|
|
970
|
+
|
|
971
|
+
Store successful reasoning patterns and retrieve them by semantic similarity. The system learns which approaches work best for different task types.
|
|
972
|
+
|
|
973
|
+
```typescript
|
|
974
|
+
await reasoningBank.storePattern({
|
|
975
|
+
taskType: "bug_investigation",
|
|
976
|
+
approach: "Check logs > Reproduce > Binary search for root cause",
|
|
977
|
+
successRate: 0.92,
|
|
978
|
+
tags: ["debugging", "systematic"],
|
|
979
|
+
});
|
|
980
|
+
|
|
981
|
+
const patterns = await reasoningBank.searchPatterns({
|
|
982
|
+
task: "debug memory leak",
|
|
983
|
+
k: 10,
|
|
984
|
+
threshold: 0.7,
|
|
985
|
+
});
|
|
986
|
+
```
|
|
987
|
+
|
|
988
|
+
### 2. Reflexion Memory — Learn from Experience
|
|
989
|
+
|
|
990
|
+
Store complete task episodes with self-generated critiques, then replay them to improve future performance. Based on Shinn et al., 2023.
|
|
991
|
+
|
|
992
|
+
```typescript
|
|
993
|
+
await reflexion.storeEpisode({
|
|
994
|
+
sessionId: "session-1",
|
|
995
|
+
task: "Fix authentication bug",
|
|
996
|
+
reward: 0.95,
|
|
997
|
+
success: true,
|
|
998
|
+
critique: "OAuth2 PKCE flow was more secure than basic flow",
|
|
999
|
+
input: "Users cannot log in",
|
|
1000
|
+
output: "Working OAuth2 implementation",
|
|
1001
|
+
latencyMs: 1200,
|
|
1002
|
+
tokensUsed: 500,
|
|
1003
|
+
});
|
|
1004
|
+
|
|
1005
|
+
const similar = await reflexion.retrieveRelevant({
|
|
1006
|
+
task: "authentication issues",
|
|
1007
|
+
k: 10,
|
|
1008
|
+
onlySuccesses: true,
|
|
1009
|
+
});
|
|
1010
|
+
```
|
|
1011
|
+
|
|
1012
|
+
### 3. Skill Library — Lifelong Learning
|
|
1013
|
+
|
|
1014
|
+
Transform successful patterns into reusable, composable skills that improve over time.
|
|
1015
|
+
|
|
1016
|
+
```typescript
|
|
1017
|
+
await skills.createSkill({
|
|
1018
|
+
name: "jwt_authentication",
|
|
1019
|
+
description: "Generate and validate JWT tokens",
|
|
1020
|
+
signature: { inputs: { userId: "string" }, outputs: { token: "string" } },
|
|
1021
|
+
code: "implementation...",
|
|
1022
|
+
successRate: 0.92,
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
const applicable = await skills.searchSkills({
|
|
1026
|
+
task: "user authentication",
|
|
1027
|
+
k: 5,
|
|
1028
|
+
minSuccessRate: 0.7,
|
|
1029
|
+
});
|
|
1030
|
+
```
|
|
1031
|
+
|
|
1032
|
+
### 4. Causal Memory Graph — Intervention Causality
|
|
1033
|
+
|
|
1034
|
+
Track `p(y|do(x))` using doubly robust estimation. Learn what interventions cause what outcomes.
|
|
1035
|
+
|
|
1036
|
+
```typescript
|
|
1037
|
+
import { CausalMemoryGraph } from "agentdb";
|
|
1038
|
+
|
|
1039
|
+
const causalGraph = new CausalMemoryGraph(db);
|
|
1040
|
+
|
|
1041
|
+
const experimentId = causalGraph.createExperiment({
|
|
1042
|
+
name: "test_error_handling",
|
|
1043
|
+
hypothesis: "Try-catch reduces crash rate",
|
|
1044
|
+
treatmentId: 123,
|
|
1045
|
+
treatmentType: "episode",
|
|
1046
|
+
controlId: 124,
|
|
1047
|
+
startTime: Date.now(),
|
|
1048
|
+
sampleSize: 0,
|
|
1049
|
+
status: "running",
|
|
1050
|
+
});
|
|
1051
|
+
|
|
1052
|
+
const { uplift, pValue } = causalGraph.calculateUplift(experimentId);
|
|
1053
|
+
```
|
|
1054
|
+
|
|
1055
|
+
### 5. Explainable Recall — Provenance Certificates
|
|
1056
|
+
|
|
1057
|
+
Every retrieval comes with a cryptographic Merkle proof explaining why specific memories were selected.
|
|
1058
|
+
|
|
1059
|
+
```typescript
|
|
1060
|
+
const result = await causalRecall.recall(
|
|
1061
|
+
"query-123",
|
|
1062
|
+
"How to optimize API response time",
|
|
1063
|
+
12,
|
|
1064
|
+
["performance"],
|
|
1065
|
+
"internal",
|
|
1066
|
+
);
|
|
1067
|
+
|
|
1068
|
+
console.log(`Certificate: ${result.certificate.id}`);
|
|
1069
|
+
console.log(`Completeness: ${result.certificate.completenessScore}`);
|
|
1070
|
+
```
|
|
1071
|
+
|
|
1072
|
+
### 6. Nightly Learner — Automated Discovery
|
|
1073
|
+
|
|
1074
|
+
Background process that discovers causal patterns, consolidates skills, and prunes low-quality data automatically.
|
|
1075
|
+
|
|
1076
|
+
```typescript
|
|
1077
|
+
const learner = new NightlyLearner(db, embedder);
|
|
1078
|
+
const discovered = await learner.discover({
|
|
1079
|
+
minAttempts: 3,
|
|
1080
|
+
minSuccessRate: 0.6,
|
|
1081
|
+
minConfidence: 0.7,
|
|
1082
|
+
dryRun: false,
|
|
1083
|
+
});
|
|
1084
|
+
```
|
|
1085
|
+
|
|
1086
|
+
</details>
|
|
1087
|
+
|
|
1088
|
+
---
|
|
1089
|
+
|
|
1090
|
+
### Storage & Data
|
|
1091
|
+
|
|
1092
|
+
How AgentDB stores vectors, manages compression tiers, and provides MongoDB-style filtering — all in a single Cognitive Container.
|
|
1093
|
+
|
|
1094
|
+
<details>
|
|
1095
|
+
<summary><strong>Cognitive Container (RVF Format)</strong> — single-file storage with crash safety and branching</summary>
|
|
1096
|
+
|
|
1097
|
+
The **Cognitive Container** is AgentDB's native storage format (`.rvf` — RuVector Format). It packs vectors, indexes, learning state, and a cryptographic witness chain into a single file with crash safety, progressive indexing, and full lineage tracking.
|
|
1098
|
+
|
|
1099
|
+
### Structure
|
|
1100
|
+
|
|
1101
|
+
```mermaid
|
|
1102
|
+
graph TB
|
|
1103
|
+
subgraph "store.rvf"
|
|
1104
|
+
H[Header + Magic Bytes]
|
|
1105
|
+
S1[Segment 1<br/>Vectors + Metadata]
|
|
1106
|
+
S2[Segment 2]
|
|
1107
|
+
IX[HNSW Index<br/>Progressive]
|
|
1108
|
+
WC[Witness Chain<br/>SHAKE-256]
|
|
1109
|
+
LN[Lineage Record<br/>Parent ID + Depth]
|
|
1110
|
+
end
|
|
1111
|
+
H --> S1
|
|
1112
|
+
S1 --> S2
|
|
1113
|
+
S2 --> IX
|
|
1114
|
+
IX --> WC
|
|
1115
|
+
WC --> LN
|
|
1116
|
+
```
|
|
1117
|
+
|
|
1118
|
+
### Key Capabilities
|
|
1119
|
+
|
|
1120
|
+
| Feature | Description |
|
|
1121
|
+
| ------------------------- | ------------------------------------------------------------------------- |
|
|
1122
|
+
| **Single-file** | Everything in one Cognitive Container (`.rvf`) — vectors, index, metadata |
|
|
1123
|
+
| **Crash-safe** | Append-only log with checksums, safe concurrent access |
|
|
1124
|
+
| **COW Branching** | Create copy-on-write branches for experiments (near-instant) |
|
|
1125
|
+
| **Lineage Tracking** | `fileId()`, `parentId()`, `lineageDepth()` for file-level provenance |
|
|
1126
|
+
| **Progressive Indexing** | 3-layer HNSW quality tiers, index builds incrementally as data arrives |
|
|
1127
|
+
| **Witness Chains** | SHAKE-256 cryptographic verification (73 bytes per entry) |
|
|
1128
|
+
| **Filter Expressions** | 11 operators for metadata filtering, max 64 depth |
|
|
1129
|
+
| **Auto-Compaction** | `compact()` reclaims space, reports segments compacted & bytes reclaimed |
|
|
1130
|
+
| **Readonly Mode** | `openReadonly()` for concurrent readers without locks |
|
|
1131
|
+
| **Segment Introspection** | `segments()` returns id, type, and payload length per segment |
|
|
1132
|
+
| **Kernel Embedding** | Embed unikernel images directly into Cognitive Containers |
|
|
1133
|
+
| **eBPF Embedding** | Embed and extract eBPF bytecode for programmable queries |
|
|
1134
|
+
|
|
1135
|
+
### Configuration
|
|
1136
|
+
|
|
1137
|
+
```typescript
|
|
1138
|
+
const backend = new RvfBackend({
|
|
1139
|
+
dimension: 384,
|
|
1140
|
+
metric: "cosine", // 'cosine' | 'l2' | 'dotproduct'
|
|
1141
|
+
storagePath: "./vectors.rvf",
|
|
1142
|
+
rvfBackend: "auto", // 'auto' | 'node' (N-API) | 'wasm' (browser)
|
|
1143
|
+
batchThreshold: 100, // Auto-flush after N sync inserts (1-10,000)
|
|
1144
|
+
compression: "none", // 'none' (fp32) | 'scalar' (int8) | 'product' (PQ)
|
|
1145
|
+
hardwareProfile: 0, // 0=Generic | 1=Core | 2=Hot | 3=Full
|
|
1146
|
+
enableStats: true, // Track insert/search/flush/compaction timing
|
|
1147
|
+
});
|
|
1148
|
+
```
|
|
1149
|
+
|
|
1150
|
+
**Hardware profiles** tune internal parameters for your deployment:
|
|
1151
|
+
|
|
1152
|
+
| Profile | Level | Best For |
|
|
1153
|
+
| ----------- | --------------------------- | ----------------------- |
|
|
1154
|
+
| Generic (0) | Conservative defaults | Unknown hardware |
|
|
1155
|
+
| Core (1) | Balanced throughput/latency | Developer machines |
|
|
1156
|
+
| Hot (2) | Aggressive caching | High-throughput servers |
|
|
1157
|
+
| Full (3) | Maximum parallelism | Dedicated vector search |
|
|
1158
|
+
|
|
1159
|
+
### Witness Chains
|
|
1160
|
+
|
|
1161
|
+
Every mutation is recorded in a tamper-evident SHAKE-256 witness chain (73 bytes per entry). This provides cryptographic proof that data has not been modified after the fact:
|
|
1162
|
+
|
|
1163
|
+
```typescript
|
|
1164
|
+
const result = backend.verifyWitnessChain();
|
|
1165
|
+
// { valid: true, entries: 1042, algorithm: 'SHAKE-256' }
|
|
1166
|
+
|
|
1167
|
+
// Verification uses @ruvector/rvf-wasm: rvf_witness_verify(), rvf_witness_count()
|
|
1168
|
+
```
|
|
1169
|
+
|
|
1170
|
+
### Filter Expressions
|
|
1171
|
+
|
|
1172
|
+
RVF supports 11 filter operators for metadata-filtered search. Filters are serialized to a compact field-ID format with a maximum depth of 64 expressions for security:
|
|
1173
|
+
|
|
1174
|
+
```typescript
|
|
1175
|
+
import { FilterBuilder } from "agentdb/backends/rvf/FilterBuilder";
|
|
1176
|
+
|
|
1177
|
+
const filter = new FilterBuilder()
|
|
1178
|
+
.eq("status", "active")
|
|
1179
|
+
.gt("score", 0.5)
|
|
1180
|
+
.range("timestamp", startDate, endDate)
|
|
1181
|
+
.in("category", ["research", "engineering"])
|
|
1182
|
+
.build();
|
|
1183
|
+
|
|
1184
|
+
const results = await backend.searchAsync(queryVector, 10, { filter });
|
|
1185
|
+
|
|
1186
|
+
// Also supports filter-based deletion
|
|
1187
|
+
await backend.deleteByFilter(filter);
|
|
1188
|
+
```
|
|
1189
|
+
|
|
1190
|
+
**Available operators:** `eq`, `ne`, `lt`, `le`, `gt`, `ge`, `in`, `range`, `and`, `or`, `not`
|
|
1191
|
+
|
|
1192
|
+
### Performance Stats
|
|
1193
|
+
|
|
1194
|
+
The RVF backend tracks detailed performance counters:
|
|
1195
|
+
|
|
1196
|
+
```typescript
|
|
1197
|
+
const stats = backend.getPerformanceStats();
|
|
1198
|
+
// {
|
|
1199
|
+
// insertCount: 10432, insertTotalMs: 521.6, avgInsertMs: 0.05,
|
|
1200
|
+
// searchCount: 8891, searchTotalMs: 178.2, avgSearchMs: 0.02,
|
|
1201
|
+
// flushCount: 104, compactionCount: 3
|
|
1202
|
+
// }
|
|
1203
|
+
```
|
|
1204
|
+
|
|
1205
|
+
### Kernel & eBPF Embedding
|
|
1206
|
+
|
|
1207
|
+
Cognitive Containers can embed unikernel images and eBPF bytecode directly, enabling self-contained deployable artifacts:
|
|
1208
|
+
|
|
1209
|
+
```typescript
|
|
1210
|
+
// Embed a kernel image into the Cognitive Container
|
|
1211
|
+
await backend.embedKernel({
|
|
1212
|
+
arch: "x86_64", // 'x86_64' | 'aarch64' | 'riscv64'
|
|
1213
|
+
type: "firecracker", // 'firecracker' | 'qemu' | 'cloud-hypervisor'
|
|
1214
|
+
flags: 0,
|
|
1215
|
+
image: kernelBuffer,
|
|
1216
|
+
apiPort: 8080,
|
|
1217
|
+
cmdline: "console=ttyS0",
|
|
1218
|
+
});
|
|
1219
|
+
|
|
1220
|
+
// Extract it later
|
|
1221
|
+
const kernel = await backend.extractKernel();
|
|
1222
|
+
// { header: { arch, type, ... }, image: Uint8Array }
|
|
1223
|
+
|
|
1224
|
+
// eBPF bytecode for programmable query filters
|
|
1225
|
+
await backend.embedEbpf(ebpfBytecode);
|
|
1226
|
+
const ebpf = await backend.extractEbpf();
|
|
1227
|
+
```
|
|
1228
|
+
|
|
1229
|
+
### CLI Commands
|
|
1230
|
+
|
|
1231
|
+
```bash
|
|
1232
|
+
# Show store status
|
|
1233
|
+
npx agentdb rvf status ./store.rvf
|
|
1234
|
+
|
|
1235
|
+
# Compact to reclaim space
|
|
1236
|
+
npx agentdb rvf compact ./store.rvf
|
|
1237
|
+
|
|
1238
|
+
# Create a branch (copy-on-write)
|
|
1239
|
+
npx agentdb rvf derive ./parent.rvf ./experiment.rvf
|
|
1240
|
+
|
|
1241
|
+
# List segments
|
|
1242
|
+
npx agentdb rvf segments ./store.rvf
|
|
1243
|
+
|
|
1244
|
+
# Detect SDK availability
|
|
1245
|
+
npx agentdb rvf detect
|
|
1246
|
+
```
|
|
1247
|
+
|
|
1248
|
+
### Programmatic Usage
|
|
1249
|
+
|
|
1250
|
+
```typescript
|
|
1251
|
+
import { RvfBackend } from "agentdb/backends/rvf/RvfBackend";
|
|
1252
|
+
|
|
1253
|
+
const backend = new RvfBackend({
|
|
1254
|
+
dimension: 384,
|
|
1255
|
+
metric: "cosine",
|
|
1256
|
+
storagePath: "./vectors.rvf",
|
|
1257
|
+
rvfBackend: "auto", // 'node' for N-API, 'wasm' for browser
|
|
1258
|
+
});
|
|
1259
|
+
await backend.initialize();
|
|
1260
|
+
|
|
1261
|
+
// Async operations (native)
|
|
1262
|
+
await backend.insertAsync("vec-1", embedding, { tag: "example" });
|
|
1263
|
+
const results = await backend.searchAsync(queryVector, 10);
|
|
1264
|
+
await backend.flush();
|
|
1265
|
+
|
|
1266
|
+
// COW branching
|
|
1267
|
+
const branch = await backend.derive("./experiment.rvf");
|
|
1268
|
+
const lineage = await backend.lineageDepth();
|
|
1269
|
+
```
|
|
1270
|
+
|
|
1271
|
+
### Backend Selection
|
|
1272
|
+
|
|
1273
|
+
```bash
|
|
1274
|
+
# Initialize with RVF backend
|
|
1275
|
+
npx agentdb init --backend rvf --rvf-path ./vectors.rvf
|
|
1276
|
+
|
|
1277
|
+
# Migrate v2 database to v3 unified .rvf
|
|
1278
|
+
npx agentdb migrate --source ./v2.db --to v3 --rvf-path ./unified.rvf
|
|
1279
|
+
|
|
1280
|
+
# Migrate existing data to RVF
|
|
1281
|
+
npx agentdb migrate --to rvf --rvf-path ./vectors.rvf
|
|
1282
|
+
|
|
1283
|
+
# Check RVF in diagnostics
|
|
1284
|
+
npx agentdb doctor --rvf-path ./vectors.rvf
|
|
1285
|
+
```
|
|
1286
|
+
|
|
1287
|
+
</details>
|
|
1288
|
+
|
|
1289
|
+
---
|
|
1290
|
+
|
|
1291
|
+
### Performance & Architecture
|
|
1292
|
+
|
|
1293
|
+
The engine behind AgentDB's speed — SIMD acceleration, graph intelligence, browser WASM, and benchmarking.
|
|
1294
|
+
|
|
1295
|
+
<details>
|
|
1296
|
+
<summary><strong>SIMD & Native Acceleration</strong> — 15 capability groups with automatic fallbacks</summary>
|
|
1297
|
+
|
|
1298
|
+
The `NativeAccelerator` provides 15 capability groups bridging 11 `@ruvector` packages, with pure-JS fallbacks for every operation via `SimdFallbacks`.
|
|
1299
|
+
|
|
1300
|
+
### Capability Groups
|
|
1301
|
+
|
|
1302
|
+
| Group | Operations | Package |
|
|
1303
|
+
| -------------------------- | ---------------------------------------------------- | ---------------------- |
|
|
1304
|
+
| **Vector Distance & Math** | cosine, dot, L2, hamming, add, mul, scale, normalize | `ruvector` |
|
|
1305
|
+
| **Activations** | softmax, relu, gelu, sigmoid, layerNorm | `@ruvector/ruvllm` |
|
|
1306
|
+
| **Loss Functions** | InfoNCE (contrastive) | `@ruvector/sona` |
|
|
1307
|
+
| **Optimizers** | AdamW step | `@ruvector/sona` |
|
|
1308
|
+
| **Quantization** | scalar 8-bit/4-bit, product quantization | `@ruvector/rvf` |
|
|
1309
|
+
| **WASM Store** | create, ingest, query, export, close | `@ruvector/rvf-wasm` |
|
|
1310
|
+
| **Verification** | witness chain, segment header | `@ruvector/rvf-node` |
|
|
1311
|
+
| **Graph Transactions** | begin, commit, rollback | `@ruvector/graph-node` |
|
|
1312
|
+
| **Graph Batch Insert** | high-throughput node/edge ingestion | `@ruvector/graph-node` |
|
|
1313
|
+
| **Graph Cypher** | pattern matching, path queries | `@ruvector/graph-node` |
|
|
1314
|
+
| **Core Batch Insert** | native vector batch ingestion | `ruvector` |
|
|
1315
|
+
| **EWC Memory** | penalty calculation, Fisher update | `@ruvector/sona` |
|
|
1316
|
+
| **Router** | save/load state persistence | `@ruvector/router` |
|
|
1317
|
+
| **SONA** | context addition, flush, base LoRA | `@ruvector/sona` |
|
|
1318
|
+
| **Compression** | tensor compress/decompress | `@ruvector/rvf` |
|
|
1319
|
+
|
|
1320
|
+
### Auto-Detection
|
|
1321
|
+
|
|
1322
|
+
Each capability group is lazily loaded — only the packages actually installed on the system are loaded, and each load is tried independently so a missing package never blocks others:
|
|
1323
|
+
|
|
1324
|
+
```typescript
|
|
1325
|
+
import { NativeAccelerator } from "agentdb/backends/rvf/NativeAccelerator";
|
|
1326
|
+
|
|
1327
|
+
const accel = new NativeAccelerator();
|
|
1328
|
+
const stats = await accel.initialize();
|
|
1329
|
+
|
|
1330
|
+
console.log(stats);
|
|
1331
|
+
// AcceleratorStats:
|
|
1332
|
+
// {
|
|
1333
|
+
// simdAvailable: true, // Vector distance + element-wise math
|
|
1334
|
+
// simdActivationsAvailable: true, // softmax, relu, gelu, sigmoid, layerNorm
|
|
1335
|
+
// wasmVerifyAvailable: true, // Witness chain & header verification
|
|
1336
|
+
// wasmStoreAvailable: true, // In-browser HNSW store
|
|
1337
|
+
// wasmQuantizationAvailable: true, // Scalar/product quantization ops
|
|
1338
|
+
// nativeInfoNceAvailable: true, // Hardware-accelerated InfoNCE loss
|
|
1339
|
+
// nativeAdamWAvailable: true, // Hardware-accelerated AdamW optimizer
|
|
1340
|
+
// nativeTensorCompressAvailable: true, // SVD tensor compression
|
|
1341
|
+
// routerPersistAvailable: true, // Semantic router state persistence
|
|
1342
|
+
// sonaExtendedAvailable: true, // SONA trajectory context & base LoRA
|
|
1343
|
+
// }
|
|
1344
|
+
// Additional capabilities (probed separately):
|
|
1345
|
+
// accel.graphTxAvailable // Graph transactions
|
|
1346
|
+
// accel.graphBatchInsertAvailable // Graph batch insert
|
|
1347
|
+
// accel.graphCypherAvailable // Cypher query engine
|
|
1348
|
+
// accel.coreBatchInsertAvailable // Native vector batch insert
|
|
1349
|
+
// accel.ewcManagerAvailable // EWC++ memory consolidation
|
|
1350
|
+
```
|
|
1351
|
+
|
|
1352
|
+
### Pure-JS Fallbacks
|
|
1353
|
+
|
|
1354
|
+
Every SIMD operation has a pure-JS fallback with 4-wide loop unrolling for reasonable performance even without native bindings:
|
|
1355
|
+
|
|
1356
|
+
```typescript
|
|
1357
|
+
import { SimdFallbacks } from "agentdb/backends/rvf/SimdFallbacks";
|
|
1358
|
+
|
|
1359
|
+
// Same API as native, ~10-50x slower but always available
|
|
1360
|
+
const similarity = SimdFallbacks.jsCosineSimilarity(vecA, vecB);
|
|
1361
|
+
const loss = SimdFallbacks.jsInfoNceLoss(
|
|
1362
|
+
anchor,
|
|
1363
|
+
positive,
|
|
1364
|
+
negatives,
|
|
1365
|
+
temperature,
|
|
1366
|
+
);
|
|
1367
|
+
const result = SimdFallbacks.jsAdamWStep(
|
|
1368
|
+
params,
|
|
1369
|
+
grads,
|
|
1370
|
+
m,
|
|
1371
|
+
v,
|
|
1372
|
+
lr,
|
|
1373
|
+
beta1,
|
|
1374
|
+
beta2,
|
|
1375
|
+
weightDecay,
|
|
1376
|
+
t,
|
|
1377
|
+
);
|
|
1378
|
+
```
|
|
1379
|
+
|
|
1380
|
+
### Additional SIMD Operations
|
|
1381
|
+
|
|
1382
|
+
The `SIMDVectorOps` class provides batch operations and platform-specific detection:
|
|
1383
|
+
|
|
1384
|
+
```typescript
|
|
1385
|
+
import { SIMDVectorOps, detectSIMDSupport } from "agentdb/simd";
|
|
1386
|
+
|
|
1387
|
+
const simd = new SIMDVectorOps();
|
|
1388
|
+
const support = detectSIMDSupport();
|
|
1389
|
+
// { wasm: true, x64: false, avx2: false, neon: false }
|
|
1390
|
+
|
|
1391
|
+
const similarities = simd.batchCosineSimilarity(query, [vec1, vec2, vec3]);
|
|
1392
|
+
```
|
|
1393
|
+
|
|
1394
|
+
</details>
|
|
1395
|
+
|
|
1396
|
+
---
|
|
1397
|
+
|
|
1398
|
+
<details>
|
|
1399
|
+
<summary><strong>Graph Intelligence</strong> — Cypher queries, causal reasoning, GNN attention</summary>
|
|
1400
|
+
|
|
1401
|
+
AgentDB integrates `@ruvector/graph-node` for a full graph database with Cypher query support, transactional batch inserts, and GNN-powered attention.
|
|
1402
|
+
|
|
1403
|
+
### Cypher Queries
|
|
1404
|
+
|
|
1405
|
+
Full Neo4j-compatible Cypher syntax including MATCH, RETURN, WHERE, ORDER BY, LIMIT, relationship patterns, and graph traversal:
|
|
1406
|
+
|
|
1407
|
+
```typescript
|
|
1408
|
+
import { GraphDatabaseAdapter } from "agentdb/backends/graph/GraphDatabaseAdapter";
|
|
1409
|
+
|
|
1410
|
+
const graph = new GraphDatabaseAdapter({
|
|
1411
|
+
storagePath: "./graph.db",
|
|
1412
|
+
dimensions: 384,
|
|
1413
|
+
distanceMetric: "Cosine", // 'Cosine' | 'Euclidean' | 'DotProduct' | 'Manhattan'
|
|
1414
|
+
});
|
|
1415
|
+
await graph.initialize();
|
|
1416
|
+
|
|
1417
|
+
// Create nodes and edges
|
|
1418
|
+
await graph.storeEpisode(episode, embedding);
|
|
1419
|
+
await graph.storeSkill(skill, embedding);
|
|
1420
|
+
await graph.createCausalEdge(edge, embedding);
|
|
1421
|
+
|
|
1422
|
+
// Cypher queries
|
|
1423
|
+
const results = await graph.query(`
|
|
1424
|
+
MATCH (e:Episode)-[:CAUSED]->(s:Skill)
|
|
1425
|
+
WHERE e.reward > 0.8
|
|
1426
|
+
RETURN e, s
|
|
1427
|
+
ORDER BY e.reward DESC
|
|
1428
|
+
LIMIT 10
|
|
1429
|
+
`);
|
|
1430
|
+
```
|
|
1431
|
+
|
|
1432
|
+
### Graph Traversal
|
|
1433
|
+
|
|
1434
|
+
Traverse relationships with direction control, depth limits, and label filtering:
|
|
1435
|
+
|
|
1436
|
+
```typescript
|
|
1437
|
+
const paths = await graph.traverse({
|
|
1438
|
+
startNodeId: "episode-42",
|
|
1439
|
+
pattern: "()-[:RELATES_TO]->(:Skill)",
|
|
1440
|
+
direction: "outgoing", // 'outgoing' | 'incoming' | 'both'
|
|
1441
|
+
maxDepth: 3,
|
|
1442
|
+
relationshipTypes: ["CAUSED", "RELATES_TO"],
|
|
1443
|
+
nodeLabels: ["Episode", "Skill"],
|
|
1444
|
+
});
|
|
1445
|
+
```
|
|
1446
|
+
|
|
1447
|
+
### Hybrid Vector + Graph Search
|
|
1448
|
+
|
|
1449
|
+
Combine vector similarity with graph context filtering — find semantically similar nodes that are also connected in the knowledge graph:
|
|
1450
|
+
|
|
1451
|
+
```typescript
|
|
1452
|
+
// Vector similarity within graph structure
|
|
1453
|
+
const results = await graph.vectorSearch(queryEmbedding, 10);
|
|
1454
|
+
// Returns nodes ranked by embedding similarity with graph context
|
|
1455
|
+
|
|
1456
|
+
// Nodes support multi-label classification
|
|
1457
|
+
await graph.createNode({
|
|
1458
|
+
id: "analysis-1",
|
|
1459
|
+
label: ["Episode", "Analyzed", "Security"],
|
|
1460
|
+
properties: { reward: 0.95, task: "auth review" },
|
|
1461
|
+
embedding: embeddingVector,
|
|
1462
|
+
});
|
|
1463
|
+
```
|
|
1464
|
+
|
|
1465
|
+
### Transactional Batch Inserts
|
|
1466
|
+
|
|
1467
|
+
ACID-guaranteed batch operations with rollback support:
|
|
1468
|
+
|
|
1469
|
+
```typescript
|
|
1470
|
+
const txId = await graph.beginTransaction();
|
|
1471
|
+
try {
|
|
1472
|
+
await graph.batchInsert(
|
|
1473
|
+
nodes.map((n) => ({ id: n.id, label: n.type, properties: n.data })),
|
|
1474
|
+
edges.map((e) => ({
|
|
1475
|
+
from: e.source,
|
|
1476
|
+
to: e.target,
|
|
1477
|
+
label: e.type,
|
|
1478
|
+
properties: e.data,
|
|
1479
|
+
})),
|
|
1480
|
+
);
|
|
1481
|
+
await graph.commitTransaction(txId);
|
|
1482
|
+
} catch (err) {
|
|
1483
|
+
await graph.rollbackTransaction(txId);
|
|
1484
|
+
}
|
|
1485
|
+
```
|
|
1486
|
+
|
|
1487
|
+
### GNN 8-Head Attention
|
|
1488
|
+
|
|
1489
|
+
The GNN attention mechanism improves recall by +12.4% with a 3.8ms forward pass and 91% transfer learning retention. Three attention types are available:
|
|
1490
|
+
|
|
1491
|
+
| Type | Algorithm | Best For |
|
|
1492
|
+
| ------ | --------------------------- | ---------------------------------- |
|
|
1493
|
+
| `gat` | Graph Attention Network | Weighted neighbor aggregation |
|
|
1494
|
+
| `gcn` | Graph Convolutional Network | Uniform message passing |
|
|
1495
|
+
| `sage` | GraphSAGE | Inductive learning on unseen nodes |
|
|
1496
|
+
|
|
1497
|
+
```typescript
|
|
1498
|
+
import { AttentionService } from "agentdb/controllers/AttentionService";
|
|
1499
|
+
|
|
1500
|
+
const attention = new AttentionService({
|
|
1501
|
+
numHeads: 8,
|
|
1502
|
+
dimension: 384,
|
|
1503
|
+
attentionType: "gat", // 'gat' | 'gcn' | 'sage'
|
|
1504
|
+
});
|
|
1505
|
+
const enhanced = await attention.forward(queryEmbedding, neighborEmbeddings);
|
|
1506
|
+
```
|
|
1507
|
+
|
|
1508
|
+
### Causal Memory Graph
|
|
1509
|
+
|
|
1510
|
+
Track interventions with `p(y|do(x))` doubly robust estimation and optional Poincare embeddings for hierarchical relationships:
|
|
1511
|
+
|
|
1512
|
+
```typescript
|
|
1513
|
+
import { CausalMemoryGraph } from "agentdb";
|
|
1514
|
+
|
|
1515
|
+
const causal = new CausalMemoryGraph(db, graph, embedder, {
|
|
1516
|
+
ENABLE_HYPERBOLIC_ATTENTION: true, // Poincaré ball distance for hierarchies
|
|
1517
|
+
});
|
|
1518
|
+
|
|
1519
|
+
await causal.addCausalEdge({
|
|
1520
|
+
fromMemoryId: "episode-1",
|
|
1521
|
+
toMemoryType: "skill", // 'episode' | 'skill' | 'note' | 'fact'
|
|
1522
|
+
uplift: 0.15, // E[y|do(x)] - E[y]
|
|
1523
|
+
confidence: 0.92,
|
|
1524
|
+
mechanism: "error-handling improvement",
|
|
1525
|
+
sampleSize: 50,
|
|
1526
|
+
confounderScore: 0.03,
|
|
1527
|
+
});
|
|
1528
|
+
```
|
|
1529
|
+
|
|
1530
|
+
</details>
|
|
1531
|
+
|
|
1532
|
+
---
|
|
1533
|
+
|
|
1534
|
+
<details>
|
|
1535
|
+
<summary><strong>Browser & WASM Deployment</strong> — full vector DB in the browser, no server needed</summary>
|
|
1536
|
+
|
|
1537
|
+
AgentDB runs fully in the browser with WASM-backed vector storage, GNN attention, product quantization, and pure-JS fallbacks.
|
|
1538
|
+
|
|
1539
|
+
### WasmStoreBridge
|
|
1540
|
+
|
|
1541
|
+
The `WasmStoreBridge` provides a full HNSW vector database in the browser by wrapping `@ruvector/rvf-wasm`:
|
|
1542
|
+
|
|
1543
|
+
```typescript
|
|
1544
|
+
import { WasmStoreBridge } from "agentdb/backends/rvf/WasmStoreBridge";
|
|
1545
|
+
|
|
1546
|
+
const bridge = new WasmStoreBridge();
|
|
1547
|
+
await bridge.initialize();
|
|
1548
|
+
|
|
1549
|
+
// Create an in-browser vector store
|
|
1550
|
+
const handle = bridge.wasmStoreCreate(384, 0); // 384 dims, cosine metric
|
|
1551
|
+
|
|
1552
|
+
// Ingest vectors
|
|
1553
|
+
bridge.wasmStoreIngest(handle, vectors, ids, count);
|
|
1554
|
+
|
|
1555
|
+
// Query
|
|
1556
|
+
const results = bridge.wasmStoreQuery(handle, queryVector, 10, 0);
|
|
1557
|
+
// [{ id: 42, distance: 0.12 }, ...]
|
|
1558
|
+
|
|
1559
|
+
// Export to transferable binary (share between tabs/workers)
|
|
1560
|
+
const bytes = bridge.wasmStoreExport(handle);
|
|
1561
|
+
|
|
1562
|
+
bridge.wasmStoreClose(handle);
|
|
1563
|
+
```
|
|
1564
|
+
|
|
1565
|
+
### Browser Attention Mechanisms
|
|
1566
|
+
|
|
1567
|
+
The `AttentionBrowser` provides three advanced attention types compiled to WASM:
|
|
1568
|
+
|
|
1569
|
+
```typescript
|
|
1570
|
+
import { AttentionBrowser } from "agentdb/browser/AttentionBrowser";
|
|
1571
|
+
|
|
1572
|
+
const attention = new AttentionBrowser({ numHeads: 8 });
|
|
1573
|
+
// Loading states: 'idle' → 'loading' → 'loaded' | 'error'
|
|
1574
|
+
|
|
1575
|
+
// Standard multi-head attention
|
|
1576
|
+
const enhanced = await attention.forward(query, neighbors);
|
|
1577
|
+
|
|
1578
|
+
// Flash Attention — O(N) memory instead of O(N²)
|
|
1579
|
+
const flash = await attention.flashAttention(query, keys, values);
|
|
1580
|
+
|
|
1581
|
+
// Hyperbolic Attention — Poincaré ball distance for hierarchical data
|
|
1582
|
+
const hyper = await attention.hyperbolicAttention(query, neighbors);
|
|
1583
|
+
|
|
1584
|
+
// Memory Consolidation — agglomerative clustering to merge similar memories
|
|
1585
|
+
const consolidated = await attention.consolidateMemories(memories, {
|
|
1586
|
+
threshold: 0.85,
|
|
1587
|
+
minClusterSize: 3,
|
|
1588
|
+
});
|
|
1589
|
+
```
|
|
1590
|
+
|
|
1591
|
+
### Product Quantization (Browser)
|
|
1592
|
+
|
|
1593
|
+
Memory-efficient vector storage with three PQ compression levels:
|
|
1594
|
+
|
|
1595
|
+
| Variant | Compression | Subvectors | Centroids | Best For |
|
|
1596
|
+
| -------- | ----------- | ---------- | --------- | ----------------------- |
|
|
1597
|
+
| **PQ8** | 4x | 8 | 256 | Balanced quality/memory |
|
|
1598
|
+
| **PQ16** | 8x | 16 | 256 | Medium datasets |
|
|
1599
|
+
| **PQ32** | 16x | 32 | 256 | Maximum compression |
|
|
1600
|
+
|
|
1601
|
+
```typescript
|
|
1602
|
+
import {
|
|
1603
|
+
createProductQuantizedStore,
|
|
1604
|
+
createScalar8BitStore,
|
|
1605
|
+
} from "agentdb/browser/ProductQuantization";
|
|
1606
|
+
|
|
1607
|
+
// Product quantization with asymmetric distance computation (ADC)
|
|
1608
|
+
const pq = createProductQuantizedStore(384, {
|
|
1609
|
+
numSubvectors: 16,
|
|
1610
|
+
bitsPerCode: 8,
|
|
1611
|
+
// Uses K-means++ initialization for codebook training
|
|
1612
|
+
});
|
|
1613
|
+
pq.add("doc-1", embedding);
|
|
1614
|
+
const results = pq.search(query, 10);
|
|
1615
|
+
```
|
|
1616
|
+
|
|
1617
|
+
### Browser Configuration Presets
|
|
1618
|
+
|
|
1619
|
+
Pre-tuned configurations for common scenarios:
|
|
1620
|
+
|
|
1621
|
+
| Preset | Features | Target |
|
|
1622
|
+
| ------------------- | ----------------------------------- | ------------------ |
|
|
1623
|
+
| `SMALL_DATASET` | GNN only | <1K vectors |
|
|
1624
|
+
| `MEDIUM_DATASET` | HNSW + PQ8 | 1K-10K vectors |
|
|
1625
|
+
| `LARGE_DATASET` | Aggressive compression + HNSW | 10K-100K vectors |
|
|
1626
|
+
| `MEMORY_OPTIMIZED` | PQ32 + SVD (dim 64) | Memory-constrained |
|
|
1627
|
+
| `SPEED_OPTIMIZED` | HNSW max quality | Latency-critical |
|
|
1628
|
+
| `QUALITY_OPTIMIZED` | No compression, max attention heads | Accuracy-critical |
|
|
1629
|
+
|
|
1630
|
+
### Maximal Marginal Relevance (MMR)
|
|
1631
|
+
|
|
1632
|
+
Diversify search results to avoid returning near-duplicate items:
|
|
1633
|
+
|
|
1634
|
+
```typescript
|
|
1635
|
+
import { MMRDiversityRanker } from "agentdb/controllers/MMRDiversityRanker";
|
|
1636
|
+
|
|
1637
|
+
const ranker = new MMRDiversityRanker({
|
|
1638
|
+
lambda: 0.7, // 0=max diversity, 1=max relevance
|
|
1639
|
+
metric: "cosine", // 'cosine' | 'euclidean' | 'dot'
|
|
1640
|
+
});
|
|
1641
|
+
|
|
1642
|
+
// Rerank results: MMR = λ × Sim(Di,Q) - (1-λ) × max Sim(Di,Dj)
|
|
1643
|
+
const diverse = ranker.rerank(queryEmbedding, candidates, 10);
|
|
1644
|
+
const score = ranker.diversityScore(diverse); // average pairwise distance
|
|
1645
|
+
```
|
|
1646
|
+
|
|
1647
|
+
### Browser Bundle
|
|
1648
|
+
|
|
1649
|
+
```typescript
|
|
1650
|
+
import { AgentDB } from "agentdb/browser";
|
|
1651
|
+
|
|
1652
|
+
const db = new AgentDB({ dimension: 384 });
|
|
1653
|
+
await db.initialize();
|
|
1654
|
+
|
|
1655
|
+
await db.insert("doc-1", embedding, { title: "Getting Started" });
|
|
1656
|
+
const results = await db.search(queryEmbedding, 5);
|
|
1657
|
+
```
|
|
1658
|
+
|
|
1659
|
+
### sql.js & SIMD Fallbacks
|
|
1660
|
+
|
|
1661
|
+
For zero-dependency environments, AgentDB falls back to `sql.js` (SQLite compiled to WASM) automatically. Non-WASM browsers get pure-JS implementations with 4-wide loop unrolling (~10-50x slower than native SIMD but fully functional):
|
|
1662
|
+
|
|
1663
|
+
```typescript
|
|
1664
|
+
import { createDatabase } from "agentdb";
|
|
1665
|
+
import { detectSIMDSupport } from "agentdb/simd";
|
|
1666
|
+
|
|
1667
|
+
// In browsers, automatically uses sql.js WASM backend
|
|
1668
|
+
const db = await createDatabase(":memory:");
|
|
1669
|
+
|
|
1670
|
+
const support = detectSIMDSupport();
|
|
1671
|
+
// { wasm: true, x64: false, avx2: false, neon: false }
|
|
1672
|
+
```
|
|
1673
|
+
|
|
1674
|
+
</details>
|
|
1675
|
+
|
|
1676
|
+
---
|
|
1677
|
+
|
|
1678
|
+
<details>
|
|
1679
|
+
<summary><strong>Performance & Benchmarks</strong> — 61us latency, 96.8% recall, 32.6M ops/sec</summary>
|
|
1680
|
+
|
|
1681
|
+
### Core Operations
|
|
1682
|
+
|
|
1683
|
+
| Operation | Throughput | Latency |
|
|
1684
|
+
| ----------------- | ------------------- | --------------------------- |
|
|
1685
|
+
| Pattern search | 32.6M ops/sec | <1ms |
|
|
1686
|
+
| Pattern storage | 388K ops/sec | ~2.5us |
|
|
1687
|
+
| Episode retrieval | 957 ops/sec | ~1ms |
|
|
1688
|
+
| Skill search | 694 ops/sec | ~1.4ms |
|
|
1689
|
+
| Batch insert | 5,556-7,692 ops/sec | 3-4x faster than sequential |
|
|
1690
|
+
|
|
1691
|
+
### Scaling Behavior
|
|
1692
|
+
|
|
1693
|
+
```
|
|
1694
|
+
500 patterns: 1,475/sec, 2MB memory
|
|
1695
|
+
2,000 patterns: 3,818/sec, 0MB delta
|
|
1696
|
+
5,000 patterns: 4,536/sec, 4MB memory (super-linear)
|
|
1697
|
+
```
|
|
1698
|
+
|
|
1699
|
+
### Self-Learning Improvement
|
|
1700
|
+
|
|
1701
|
+
```
|
|
1702
|
+
Adaptive Learning (10 sessions, 50 episodes each)
|
|
1703
|
+
Initial success rate: 54%
|
|
1704
|
+
Final success rate: 90%
|
|
1705
|
+
Improvement: +36%
|
|
1706
|
+
```
|
|
1707
|
+
|
|
1708
|
+
### Latent Space Validation (31 scenarios, 98.2% reproducibility)
|
|
1709
|
+
|
|
1710
|
+
| Scenario | Result |
|
|
1711
|
+
| ------------------- | ----------------------------------------------- |
|
|
1712
|
+
| HNSW Optimization | 61us p50, 96.8% recall@10, 8.2x vs hnswlib |
|
|
1713
|
+
| GNN Attention | +12.4% recall, 3.8ms forward pass, 91% transfer |
|
|
1714
|
+
| Self-Healing | 97.9% degradation prevention, <100ms repair |
|
|
1715
|
+
| Neural Augmentation | +29.4% improvement, -32% memory, -52% hops |
|
|
1716
|
+
|
|
1717
|
+
### Adaptive Solver (Thompson Sampling)
|
|
1718
|
+
|
|
1719
|
+
The solver uses 18 context-bucketed bandits (3 range x 3 distractor x 2 noise levels) to dynamically tune search parameters:
|
|
1720
|
+
|
|
1721
|
+
```typescript
|
|
1722
|
+
const solver = await AgentDBSolver.create();
|
|
1723
|
+
|
|
1724
|
+
// Train the solver with puzzles of varying difficulty
|
|
1725
|
+
const result = solver.train({ count: 50, minDifficulty: 1, maxDifficulty: 10 });
|
|
1726
|
+
|
|
1727
|
+
// Inspect the policy — shows Thompson Sampling alpha/beta per context bucket
|
|
1728
|
+
const policy = solver.policy();
|
|
1729
|
+
console.log(policy.contextStats);
|
|
1730
|
+
// { '0_0_0': { alphaSafety: 12.5, betaSafety: 2.1, costEma: 0.03 }, ... }
|
|
1731
|
+
|
|
1732
|
+
// A/B/C acceptance testing
|
|
1733
|
+
const manifest = solver.acceptance({ holdoutSize: 100, cycles: 3 });
|
|
1734
|
+
console.log(`Passed: ${manifest.passed}, Score: ${manifest.score}`);
|
|
1735
|
+
|
|
1736
|
+
// SHAKE-256 witness chain for audit
|
|
1737
|
+
const chain = solver.witnessChain();
|
|
1738
|
+
```
|
|
1739
|
+
|
|
1740
|
+
Three-loop architecture:
|
|
1741
|
+
|
|
1742
|
+
1. **Fast loop** — constraint propagation (~0.1ms)
|
|
1743
|
+
2. **Medium loop** — PolicyKernel with Thompson Sampling (~1ms)
|
|
1744
|
+
3. **Slow loop** — KnowledgeCompiler for long-term patterns (~10ms)
|
|
1745
|
+
|
|
1746
|
+
### SolverBandit — Decision Intelligence Across Controllers (ADR-010)
|
|
1747
|
+
|
|
1748
|
+
The `SolverBandit` is a general-purpose Thompson Sampling bandit that provides explore/exploit decisions for any context-dependent selection problem. It powers adaptive decision-making in 5 AgentDB controllers:
|
|
1749
|
+
|
|
1750
|
+
| Controller | What the Bandit Decides | Context Key |
|
|
1751
|
+
| ---------------------- | -------------------------------------------------- | ------------------- |
|
|
1752
|
+
| **SkillLibrary** | Rerank retrieved skills by task type | `taskType` string |
|
|
1753
|
+
| **ReasoningBank** | Rerank matching patterns by task type | query text |
|
|
1754
|
+
| **NightlyLearner** | Prioritize experiment candidates | `'experiment'` |
|
|
1755
|
+
| **LearningSystem** | Select best RL algorithm (9 options) | task description |
|
|
1756
|
+
| **TemporalCompressor** | Choose compression tier (none/half/pq8/pq4/binary) | `hot`/`warm`/`cold` |
|
|
1757
|
+
|
|
1758
|
+
Each controller uses the bandit optionally — when no bandit is provided, behavior is unchanged from v2. When present, the bandit reranks results using Thompson-sampled Beta distributions, and controllers feed outcomes back via `recordReward()`.
|
|
1759
|
+
|
|
1760
|
+
```typescript
|
|
1761
|
+
import { SolverBandit } from "agentdb/backends/rvf/SolverBandit";
|
|
1762
|
+
|
|
1763
|
+
const bandit = new SolverBandit();
|
|
1764
|
+
|
|
1765
|
+
// Select best arm for a context
|
|
1766
|
+
const skill = bandit.selectArm("code_review", [
|
|
1767
|
+
"lint_first",
|
|
1768
|
+
"test_first",
|
|
1769
|
+
"security_scan",
|
|
1770
|
+
]);
|
|
1771
|
+
|
|
1772
|
+
// Record outcome
|
|
1773
|
+
bandit.recordReward("code_review", skill, 0.85, /* latencyMs */ 120);
|
|
1774
|
+
|
|
1775
|
+
// Rerank candidates by learned performance
|
|
1776
|
+
const ranked = bandit.rerank("code_review", [
|
|
1777
|
+
"lint_first",
|
|
1778
|
+
"test_first",
|
|
1779
|
+
"security_scan",
|
|
1780
|
+
]);
|
|
1781
|
+
|
|
1782
|
+
// Serialize for cross-session persistence
|
|
1783
|
+
const state = bandit.serialize(); // JSON-safe
|
|
1784
|
+
const restored = SolverBandit.deserialize(state);
|
|
1785
|
+
```
|
|
1786
|
+
|
|
1787
|
+
**Performance:** 100K `selectArm` calls in <200ms, 100K `recordReward` calls in <100ms.
|
|
1788
|
+
|
|
1789
|
+
**Wire into AgentDB controllers:**
|
|
1790
|
+
|
|
1791
|
+
```typescript
|
|
1792
|
+
import {
|
|
1793
|
+
SolverBandit,
|
|
1794
|
+
SkillLibrary,
|
|
1795
|
+
ReasoningBank,
|
|
1796
|
+
LearningSystem,
|
|
1797
|
+
} from "agentdb";
|
|
1798
|
+
|
|
1799
|
+
const bandit = new SolverBandit({ costWeight: 0.01, explorationBonus: 0.1 });
|
|
1800
|
+
|
|
1801
|
+
// Pass as optional last parameter to any controller
|
|
1802
|
+
const skills = new SkillLibrary(db, embedder, undefined, bandit);
|
|
1803
|
+
const patterns = new ReasoningBank(db, embedder, undefined, bandit);
|
|
1804
|
+
const learning = new LearningSystem(db, undefined, bandit);
|
|
1805
|
+
|
|
1806
|
+
// Controllers automatically rerank results and accept feedback
|
|
1807
|
+
const algorithm = learning.recommendAlgorithm("navigation task");
|
|
1808
|
+
learning.recordAlgorithmOutcome("navigation task", algorithm, 0.92, 340);
|
|
1809
|
+
```
|
|
1810
|
+
|
|
1811
|
+
</details>
|
|
1812
|
+
|
|
1813
|
+
---
|
|
1814
|
+
|
|
1815
|
+
### Enterprise & Security
|
|
1816
|
+
|
|
1817
|
+
Production-ready authentication, audit logging, and defense-in-depth security with cryptographic verification.
|
|
1818
|
+
|
|
1819
|
+
<details>
|
|
1820
|
+
<summary><strong>Security</strong> — defense-in-depth with cryptographic audit trails</summary>
|
|
1821
|
+
|
|
1822
|
+
AgentDB provides defense-in-depth security with validation at every system boundary.
|
|
1823
|
+
|
|
1824
|
+
### Input Validation
|
|
1825
|
+
|
|
1826
|
+
```typescript
|
|
1827
|
+
import {
|
|
1828
|
+
validateVector,
|
|
1829
|
+
validateVectorId,
|
|
1830
|
+
validateSearchOptions,
|
|
1831
|
+
validateHNSWParams,
|
|
1832
|
+
sanitizeMetadata,
|
|
1833
|
+
validateCypherParams,
|
|
1834
|
+
validateLabel,
|
|
1835
|
+
validateBatchSize,
|
|
1836
|
+
} from "agentdb/security/validation";
|
|
1837
|
+
|
|
1838
|
+
// Vector validation — prevents NaN/Infinity injection
|
|
1839
|
+
validateVector(embedding, 384, "embedding");
|
|
1840
|
+
|
|
1841
|
+
// ID validation — prevents path traversal (../, etc.)
|
|
1842
|
+
validateVectorId("doc-123", "id");
|
|
1843
|
+
|
|
1844
|
+
// Search bounds — k, threshold, efSearch range checking
|
|
1845
|
+
validateSearchOptions({ k: 10, threshold: 0.7 });
|
|
1846
|
+
|
|
1847
|
+
// HNSW parameter validation — M, efConstruction, efSearch
|
|
1848
|
+
validateHNSWParams({ M: 16, efConstruction: 200, efSearch: 100 });
|
|
1849
|
+
|
|
1850
|
+
// Metadata sanitization — strips password, token, key, apiKey, auth fields
|
|
1851
|
+
const safe = sanitizeMetadata(userInput);
|
|
1852
|
+
|
|
1853
|
+
// Cypher injection prevention
|
|
1854
|
+
validateCypherParams(params);
|
|
1855
|
+
```
|
|
1856
|
+
|
|
1857
|
+
### XSS & Injection Prevention
|
|
1858
|
+
|
|
1859
|
+
```typescript
|
|
1860
|
+
import {
|
|
1861
|
+
validateTaskString,
|
|
1862
|
+
validateNumericRange,
|
|
1863
|
+
validateEnum,
|
|
1864
|
+
} from "agentdb/security/input-validation";
|
|
1865
|
+
|
|
1866
|
+
const task = validateTaskString(input, "task"); // XSS pattern detection
|
|
1867
|
+
const k = validateNumericRange(kVal, "k", 1, 100);
|
|
1868
|
+
const format = validateEnum(fmt, "format", ["json", "concise", "detailed"]);
|
|
1869
|
+
```
|
|
1870
|
+
|
|
1871
|
+
### Security Limits
|
|
1872
|
+
|
|
1873
|
+
| Parameter | Limit |
|
|
1874
|
+
| ----------------------- | ---------- |
|
|
1875
|
+
| Max vectors | 10,000,000 |
|
|
1876
|
+
| Max dimension | 4,096 |
|
|
1877
|
+
| Max batch size | 10,000 |
|
|
1878
|
+
| Max k | 10,000 |
|
|
1879
|
+
| Max metadata size | 64 KB |
|
|
1880
|
+
| Max ID length | 256 chars |
|
|
1881
|
+
| Filter expression depth | 64 |
|
|
1882
|
+
| ef-search range | 1 - 1,000 |
|
|
1883
|
+
| ef-construction range | 4 - 500 |
|
|
1884
|
+
| M range | 2 - 64 |
|
|
1885
|
+
|
|
1886
|
+
### Witness Chain Verification
|
|
1887
|
+
|
|
1888
|
+
SHAKE-256 cryptographic witness chains provide tamper-evident audit trails:
|
|
1889
|
+
|
|
1890
|
+
```typescript
|
|
1891
|
+
const result = backend.verifyWitnessChain();
|
|
1892
|
+
if (!result.valid) {
|
|
1893
|
+
console.error("Data integrity compromised!");
|
|
1894
|
+
}
|
|
1895
|
+
```
|
|
1896
|
+
|
|
1897
|
+
### Provenance Certificates (Explainable Recall)
|
|
1898
|
+
|
|
1899
|
+
Every search result can include a Merkle proof explaining why it was selected:
|
|
1900
|
+
|
|
1901
|
+
```typescript
|
|
1902
|
+
const { results, certificate } = await causalRecall.recall(
|
|
1903
|
+
"query-id",
|
|
1904
|
+
"optimization strategies",
|
|
1905
|
+
10,
|
|
1906
|
+
["performance"],
|
|
1907
|
+
"internal",
|
|
1908
|
+
);
|
|
1909
|
+
console.log(`Certificate ID: ${certificate.id}`);
|
|
1910
|
+
console.log(`Completeness: ${certificate.completenessScore}`);
|
|
1911
|
+
```
|
|
1912
|
+
|
|
1913
|
+
</details>
|
|
1914
|
+
|
|
1915
|
+
---
|
|
1916
|
+
|
|
1917
|
+
<details>
|
|
1918
|
+
<summary><strong>Quantization & Compression</strong> — up to 32x memory reduction with 5-tier auto-tiering</summary>
|
|
1919
|
+
|
|
1920
|
+
AgentDB supports multiple quantization strategies to reduce memory footprint while maintaining search quality.
|
|
1921
|
+
|
|
1922
|
+
### Scalar Quantization
|
|
1923
|
+
|
|
1924
|
+
8-bit and 4-bit scalar quantization for memory-efficient storage:
|
|
1925
|
+
|
|
1926
|
+
```typescript
|
|
1927
|
+
import {
|
|
1928
|
+
createScalar8BitStore,
|
|
1929
|
+
createProductQuantizedStore,
|
|
1930
|
+
} from "agentdb/browser/ProductQuantization";
|
|
1931
|
+
|
|
1932
|
+
// 8-bit quantization — 4x memory reduction
|
|
1933
|
+
const store8 = createScalar8BitStore(384);
|
|
1934
|
+
store8.add("doc-1", embedding);
|
|
1935
|
+
const results = store8.search(query, 10);
|
|
1936
|
+
|
|
1937
|
+
// Product quantization — up to 32x memory reduction
|
|
1938
|
+
const storePQ = createProductQuantizedStore(384, {
|
|
1939
|
+
numSubvectors: 48,
|
|
1940
|
+
bitsPerCode: 8,
|
|
1941
|
+
});
|
|
1942
|
+
storePQ.add("doc-1", embedding);
|
|
1943
|
+
const results = storePQ.search(query, 10);
|
|
1944
|
+
```
|
|
1945
|
+
|
|
1946
|
+
### 5-Tier Temporal Compression
|
|
1947
|
+
|
|
1948
|
+
Vectors are automatically compressed based on age and access frequency:
|
|
1949
|
+
|
|
1950
|
+
| Tier | Compression | Memory | Quality | Trigger |
|
|
1951
|
+
| ----------- | -------------- | ------ | ------- | ---------------------------- |
|
|
1952
|
+
| **Hot** | None | 100% | 100% | Recent / frequently accessed |
|
|
1953
|
+
| **Warm** | Half-precision | 50% | ~99% | Moderate age |
|
|
1954
|
+
| **Cool** | PQ-8 | ~12% | ~95% | Older data |
|
|
1955
|
+
| **Cold** | PQ-4 | ~6% | ~90% | Rarely accessed |
|
|
1956
|
+
| **Archive** | Binary | ~3% | ~80% | Archival storage |
|
|
1957
|
+
|
|
1958
|
+
### Matryoshka Dimension Truncation
|
|
1959
|
+
|
|
1960
|
+
Reduce embedding dimensions while preserving the most important features:
|
|
1961
|
+
|
|
1962
|
+
```typescript
|
|
1963
|
+
// Models trained with Matryoshka loss support dimension truncation
|
|
1964
|
+
// 384d -> 128d with minimal quality loss
|
|
1965
|
+
const truncated = embedding.slice(0, 128);
|
|
1966
|
+
```
|
|
1967
|
+
|
|
1968
|
+
### NativeAccelerator Quantization
|
|
1969
|
+
|
|
1970
|
+
The NativeAccelerator provides hardware-accelerated quantization/dequantization:
|
|
1971
|
+
|
|
1972
|
+
```typescript
|
|
1973
|
+
const accel = new NativeAccelerator();
|
|
1974
|
+
await accel.initialize();
|
|
1975
|
+
|
|
1976
|
+
// Load scalar quantization parameters
|
|
1977
|
+
accel.loadSqParams(mins, scales);
|
|
1978
|
+
const restored = accel.dequantI8(quantizedBytes);
|
|
1979
|
+
|
|
1980
|
+
// Product quantization with codebook
|
|
1981
|
+
accel.loadPqCodebook(codebook, numSubvectors, bitsPerCode);
|
|
1982
|
+
const distances = accel.pqDistances(query, codes);
|
|
1983
|
+
```
|
|
1984
|
+
|
|
1985
|
+
</details>
|
|
1986
|
+
|
|
1987
|
+
---
|
|
1988
|
+
|
|
1989
|
+
### Integration & Tools
|
|
1990
|
+
|
|
1991
|
+
Connect AgentDB to LLM providers, embedding models, and distributed systems with real-time sync.
|
|
1992
|
+
|
|
1993
|
+
<details>
|
|
1994
|
+
<summary><strong>Model Import/Export & Embedding Models</strong> — 5 local models, no API keys</summary>
|
|
1995
|
+
|
|
1996
|
+
### Embedding Models
|
|
1997
|
+
|
|
1998
|
+
AgentDB supports multiple embedding models. No API key required — all Xenova models run locally.
|
|
1999
|
+
|
|
2000
|
+
| Model | Dimension | Quality | Speed | Best For |
|
|
2001
|
+
| ------------------------------ | --------- | --------- | -------- | ----------------- |
|
|
2002
|
+
| **all-MiniLM-L6-v2** (default) | 384 | Good | Fastest | Prototyping |
|
|
2003
|
+
| **bge-small-en-v1.5** | 384 | Excellent | Fast | Best 384d quality |
|
|
2004
|
+
| **bge-base-en-v1.5** | 768 | Excellent | Moderate | Production |
|
|
2005
|
+
| **all-mpnet-base-v2** | 768 | Excellent | Moderate | All-around |
|
|
2006
|
+
| **e5-base-v2** | 768 | Excellent | Moderate | Multilingual |
|
|
2007
|
+
|
|
2008
|
+
```bash
|
|
2009
|
+
# Default (384d, fast)
|
|
2010
|
+
npx agentdb init
|
|
2011
|
+
|
|
2012
|
+
# Production (768d, high quality)
|
|
2013
|
+
npx agentdb init --dimension 768 --model "Xenova/bge-base-en-v1.5"
|
|
2014
|
+
```
|
|
2015
|
+
|
|
2016
|
+
### Offline Embeddings (Bundled Model)
|
|
2017
|
+
|
|
2018
|
+
The default `all-MiniLM-L6-v2` model can be bundled into a `.rvf` file and shipped with the package. This eliminates the ~23MB first-run download and enables fully offline embedding generation.
|
|
2019
|
+
|
|
2020
|
+
**Resolution order** (automatic, no config needed):
|
|
2021
|
+
|
|
2022
|
+
1. `AGENTDB_MODEL_PATH` env var (user override)
|
|
2023
|
+
2. Bundled `.rvf` at `dist/models/all-MiniLM-L6-v2.rvf`
|
|
2024
|
+
3. Existing `@xenova/transformers` cache
|
|
2025
|
+
4. Network download (fallback)
|
|
2026
|
+
|
|
2027
|
+
```bash
|
|
2028
|
+
# Build the bundled model .rvf (requires model to be downloaded first)
|
|
2029
|
+
npm run build:model
|
|
2030
|
+
|
|
2031
|
+
# Or specify a custom source directory
|
|
2032
|
+
node scripts/build-model-rvf.mjs --source /path/to/model/files
|
|
2033
|
+
```
|
|
2034
|
+
|
|
2035
|
+
```typescript
|
|
2036
|
+
// Programmatic access
|
|
2037
|
+
import { ModelCacheLoader } from "agentdb/model";
|
|
2038
|
+
|
|
2039
|
+
const cached = await ModelCacheLoader.resolve("all-MiniLM-L6-v2");
|
|
2040
|
+
if (cached) {
|
|
2041
|
+
console.log(cached.localPath); // path to extracted model
|
|
2042
|
+
console.log(cached.fromBundle); // true if loaded from .rvf
|
|
2043
|
+
}
|
|
2044
|
+
```
|
|
2045
|
+
|
|
2046
|
+
Set `AGENTDB_MODEL_PATH` to point to a custom model directory:
|
|
2047
|
+
|
|
2048
|
+
```bash
|
|
2049
|
+
export AGENTDB_MODEL_PATH=/opt/models
|
|
2050
|
+
# expects: /opt/models/Xenova/all-MiniLM-L6-v2/onnx/model_quantized.onnx
|
|
2051
|
+
```
|
|
2052
|
+
|
|
2053
|
+
### LLM Router for Multi-Provider Integration
|
|
2054
|
+
|
|
2055
|
+
AgentDB can route queries through multiple LLM providers via `@ruvector/ruvllm`:
|
|
2056
|
+
|
|
2057
|
+
```typescript
|
|
2058
|
+
import { isRuvLLMInstalled } from "agentdb/wrappers";
|
|
2059
|
+
|
|
2060
|
+
if (await isRuvLLMInstalled()) {
|
|
2061
|
+
// LLM router available for enrichment and reranking
|
|
2062
|
+
}
|
|
2063
|
+
```
|
|
2064
|
+
|
|
2065
|
+
### RVF Export/Import
|
|
2066
|
+
|
|
2067
|
+
Export vector stores to the portable Cognitive Container format:
|
|
2068
|
+
|
|
2069
|
+
```bash
|
|
2070
|
+
# Migrate v2 database to v3 unified .rvf (vectors + relational in one file)
|
|
2071
|
+
npx agentdb migrate --source ./v2.db --to v3 --rvf-path ./unified.rvf
|
|
2072
|
+
|
|
2073
|
+
# Export existing database to Cognitive Container
|
|
2074
|
+
npx agentdb migrate --to rvf --rvf-path ./vectors.rvf
|
|
2075
|
+
|
|
2076
|
+
# Import from Cognitive Container into a new backend
|
|
2077
|
+
npx agentdb migrate --from rvf --rvf-path ./vectors.rvf --target ./new.db
|
|
2078
|
+
```
|
|
2079
|
+
|
|
2080
|
+
### WASM Export
|
|
2081
|
+
|
|
2082
|
+
Export a WASM store to a transferable binary for sharing between browser tabs or workers:
|
|
2083
|
+
|
|
2084
|
+
```typescript
|
|
2085
|
+
const bytes = bridge.wasmStoreExport(handle); // Uint8Array
|
|
2086
|
+
// Transfer to another worker or save to IndexedDB
|
|
2087
|
+
```
|
|
2088
|
+
|
|
2089
|
+
</details>
|
|
2090
|
+
|
|
2091
|
+
---
|
|
2092
|
+
|
|
2093
|
+
<details>
|
|
2094
|
+
<summary><strong>Query Lifecycle</strong> — what happens when you call searchAsync()</summary>
|
|
2095
|
+
|
|
2096
|
+
The full lifecycle of a query through AgentDB's self-learning pipeline:
|
|
2097
|
+
|
|
2098
|
+
```mermaid
|
|
2099
|
+
sequenceDiagram
|
|
2100
|
+
participant App
|
|
2101
|
+
participant Router as Semantic Router
|
|
2102
|
+
participant SONA
|
|
2103
|
+
participant HNSW as HNSW Index
|
|
2104
|
+
participant Trainer as Contrastive Trainer
|
|
2105
|
+
|
|
2106
|
+
App->>Router: query(embedding)
|
|
2107
|
+
Router->>Router: classify intent
|
|
2108
|
+
Router->>SONA: enhance(embedding, trajectories)
|
|
2109
|
+
SONA->>SONA: apply micro-LoRA
|
|
2110
|
+
SONA->>HNSW: searchAsync(enhanced, k, ef)
|
|
2111
|
+
HNSW-->>App: results[]
|
|
2112
|
+
App->>Trainer: recordFeedback(quality)
|
|
2113
|
+
Trainer->>Trainer: InfoNCE + hard negatives
|
|
2114
|
+
Trainer->>SONA: updateLoRA(gradients)
|
|
2115
|
+
Note over SONA: EWC++ consolidation<br/>prevents forgetting
|
|
2116
|
+
```
|
|
2117
|
+
|
|
2118
|
+
### Phase Breakdown
|
|
2119
|
+
|
|
2120
|
+
1. **Routing** — `SemanticQueryRouter` classifies query intent via HNSW-indexed embeddings and selects the optimal search strategy
|
|
2121
|
+
2. **Enhancement** — SONA engine enriches the query embedding using trajectory history and context
|
|
2122
|
+
3. **LoRA Application** — Micro-LoRA adapter adjusts the embedding in sub-millisecond time
|
|
2123
|
+
4. **Search** — HNSW index returns top-k results with adaptive ef-search
|
|
2124
|
+
5. **Feedback** — Application records result quality (0.0-1.0)
|
|
2125
|
+
6. **Training** — Contrastive trainer runs InfoNCE loss with hard negative mining
|
|
2126
|
+
7. **Update** — LoRA gradients update the adapter weights
|
|
2127
|
+
8. **Consolidation** — EWC++ prevents catastrophic forgetting of previously learned patterns
|
|
2128
|
+
|
|
2129
|
+
</details>
|
|
2130
|
+
|
|
2131
|
+
---
|
|
2132
|
+
|
|
2133
|
+
### Distributed Systems
|
|
2134
|
+
|
|
2135
|
+
Multi-database coordination, real-time replication, and production observability.
|
|
2136
|
+
|
|
2137
|
+
<details>
|
|
2138
|
+
<summary><strong>Multi-Database Coordination</strong> — distributed sync with conflict resolution</summary>
|
|
2139
|
+
|
|
2140
|
+
The `MultiDatabaseCoordinator` enables distributed sync between multiple AgentDB instances with configurable conflict resolution.
|
|
2141
|
+
|
|
2142
|
+
```typescript
|
|
2143
|
+
import { MultiDatabaseCoordinator } from "agentdb/coordination/MultiDatabaseCoordinator";
|
|
2144
|
+
|
|
2145
|
+
const coordinator = new MultiDatabaseCoordinator({
|
|
2146
|
+
replicationFactor: 3,
|
|
2147
|
+
syncIntervalMs: 5000,
|
|
2148
|
+
conflictResolution: "merge", // 'last-write-wins' | 'merge' | 'manual'
|
|
2149
|
+
autoFailover: true,
|
|
2150
|
+
maxRetries: 3,
|
|
2151
|
+
});
|
|
2152
|
+
|
|
2153
|
+
// Register database instances
|
|
2154
|
+
await coordinator.registerInstance({
|
|
2155
|
+
id: "primary",
|
|
2156
|
+
url: "file:///data/primary.rvf",
|
|
2157
|
+
role: "primary",
|
|
2158
|
+
});
|
|
2159
|
+
await coordinator.registerInstance({
|
|
2160
|
+
id: "replica-1",
|
|
2161
|
+
url: "file:///data/replica-1.rvf",
|
|
2162
|
+
role: "replica",
|
|
2163
|
+
});
|
|
2164
|
+
|
|
2165
|
+
// Sync a specific instance
|
|
2166
|
+
const result = await coordinator.syncInstance("replica-1");
|
|
2167
|
+
console.log(`Synced: ${result.recordsSynced}, Conflicts: ${result.conflicts}`);
|
|
2168
|
+
|
|
2169
|
+
// Broadcast operations to all instances
|
|
2170
|
+
await coordinator.broadcast("insertAsync", [
|
|
2171
|
+
"doc-1",
|
|
2172
|
+
embedding,
|
|
2173
|
+
{ source: "api" },
|
|
2174
|
+
]);
|
|
2175
|
+
|
|
2176
|
+
// Monitor instance status
|
|
2177
|
+
coordinator.onStatusChange((instanceId, status) => {
|
|
2178
|
+
console.log(`${instanceId}: ${status.state}`); // 'healthy' | 'degraded' | 'offline'
|
|
2179
|
+
});
|
|
2180
|
+
```
|
|
2181
|
+
|
|
2182
|
+
### Conflict Resolution Strategies
|
|
2183
|
+
|
|
2184
|
+
| Strategy | Description | Best For |
|
|
2185
|
+
| ------------------- | ------------------------------------- | --------------------------------------- |
|
|
2186
|
+
| **last-write-wins** | Most recent write wins (LWW) | Simple setups, eventual consistency |
|
|
2187
|
+
| **merge** | Quality-based weighted average | Learning systems, federated aggregation |
|
|
2188
|
+
| **manual** | Returns conflicts for user resolution | Critical data, compliance requirements |
|
|
2189
|
+
|
|
2190
|
+
</details>
|
|
2191
|
+
|
|
2192
|
+
---
|
|
2193
|
+
|
|
2194
|
+
<details>
|
|
2195
|
+
<summary><strong>Real-Time Sync (QUIC)</strong> — bidirectional replication with 0-RTT resumption</summary>
|
|
2196
|
+
|
|
2197
|
+
AgentDB supports real-time bidirectional sync between instances using QUIC transport and a high-level `SyncCoordinator`.
|
|
2198
|
+
|
|
2199
|
+
### QUIC Transport
|
|
2200
|
+
|
|
2201
|
+
QUIC provides multiplexed streams over a single connection with zero head-of-line blocking, connection migration between networks, and 0-RTT session resumption:
|
|
2202
|
+
|
|
2203
|
+
```typescript
|
|
2204
|
+
import { QUICServer } from "agentdb/controllers/QUICServer";
|
|
2205
|
+
|
|
2206
|
+
const server = new QUICServer({
|
|
2207
|
+
port: 4433,
|
|
2208
|
+
maxConnections: 100,
|
|
2209
|
+
rateLimit: {
|
|
2210
|
+
requestsPerMin: 1000,
|
|
2211
|
+
bytesPerMin: 10_000_000,
|
|
2212
|
+
},
|
|
2213
|
+
authToken: "secret", // optional token validation
|
|
2214
|
+
});
|
|
2215
|
+
|
|
2216
|
+
// Stale connections auto-cleaned after 5 minutes idle
|
|
2217
|
+
// Sync types: 'episodes' | 'skills' | 'edges' | 'full'
|
|
2218
|
+
// Incremental sync via `since` parameter and cursor-based pagination
|
|
2219
|
+
```
|
|
2220
|
+
|
|
2221
|
+
### Sync Coordinator
|
|
2222
|
+
|
|
2223
|
+
High-level bidirectional sync with conflict resolution, progress tracking, and auto-sync:
|
|
2224
|
+
|
|
2225
|
+
```typescript
|
|
2226
|
+
import { SyncCoordinator } from "agentdb/controllers/SyncCoordinator";
|
|
2227
|
+
|
|
2228
|
+
const sync = new SyncCoordinator({
|
|
2229
|
+
conflictStrategy: "latest-wins", // 'local-wins' | 'remote-wins' | 'latest-wins' | 'merge'
|
|
2230
|
+
batchSize: 100,
|
|
2231
|
+
autoSyncInterval: 60000, // Auto-sync every 60 seconds
|
|
2232
|
+
});
|
|
2233
|
+
|
|
2234
|
+
// Push local changes, pull remote changes, resolve conflicts
|
|
2235
|
+
const report = await sync.sync();
|
|
2236
|
+
console.log(report);
|
|
2237
|
+
// {
|
|
2238
|
+
// success: true,
|
|
2239
|
+
// durationMs: 342,
|
|
2240
|
+
// itemsPushed: 15,
|
|
2241
|
+
// itemsPulled: 8,
|
|
2242
|
+
// conflictsResolved: 2,
|
|
2243
|
+
// bytesTransferred: 48200,
|
|
2244
|
+
// errors: [],
|
|
2245
|
+
// }
|
|
2246
|
+
|
|
2247
|
+
// Progress tracking through 5 phases:
|
|
2248
|
+
// detecting → pushing → pulling → resolving → applying
|
|
2249
|
+
|
|
2250
|
+
// Sync state persists across restarts
|
|
2251
|
+
await sync.saveSyncState();
|
|
2252
|
+
// Tracks: lastSyncAt, lastEpisodeSync, lastSkillSync, lastEdgeSync,
|
|
2253
|
+
// totalItemsSynced, totalBytesSynced, syncCount
|
|
2254
|
+
```
|
|
2255
|
+
|
|
2256
|
+
### Conflict Resolution
|
|
2257
|
+
|
|
2258
|
+
| Strategy | Behavior |
|
|
2259
|
+
| ------------- | --------------------------------------- |
|
|
2260
|
+
| `local-wins` | Local version always kept |
|
|
2261
|
+
| `remote-wins` | Remote version always accepted |
|
|
2262
|
+
| `latest-wins` | Most recent timestamp wins |
|
|
2263
|
+
| `merge` | Quality-weighted merge of both versions |
|
|
2264
|
+
|
|
2265
|
+
</details>
|
|
2266
|
+
|
|
2267
|
+
---
|
|
2268
|
+
|
|
2269
|
+
<details>
|
|
2270
|
+
<summary><strong>Observability</strong> — OpenTelemetry traces and Prometheus metrics built in</summary>
|
|
2271
|
+
|
|
2272
|
+
AgentDB integrates with OpenTelemetry for production monitoring.
|
|
2273
|
+
|
|
2274
|
+
### Traces
|
|
2275
|
+
|
|
2276
|
+
Automatic span creation for search, insert, and sync operations:
|
|
2277
|
+
|
|
2278
|
+
```typescript
|
|
2279
|
+
import { initTelemetry } from "agentdb/observability";
|
|
2280
|
+
|
|
2281
|
+
initTelemetry({
|
|
2282
|
+
serviceName: "my-agent",
|
|
2283
|
+
exporterUrl: "http://localhost:4318", // OTLP HTTP endpoint
|
|
2284
|
+
});
|
|
2285
|
+
|
|
2286
|
+
// All AgentDB operations automatically generate spans:
|
|
2287
|
+
// - agentdb.search (with k, backend, latency attributes)
|
|
2288
|
+
// - agentdb.insert (with batchSize, backend)
|
|
2289
|
+
// - agentdb.sync (with itemsSynced, conflicts)
|
|
2290
|
+
```
|
|
2291
|
+
|
|
2292
|
+
### Metrics
|
|
2293
|
+
|
|
2294
|
+
Prometheus-compatible metrics for latency, throughput, and error rates:
|
|
2295
|
+
|
|
2296
|
+
- `agentdb_search_duration_ms` — search latency histogram
|
|
2297
|
+
- `agentdb_insert_duration_ms` — insert latency histogram
|
|
2298
|
+
- `agentdb_cache_hit_ratio` — MCP tool cache effectiveness
|
|
2299
|
+
- `agentdb_learning_loss` — contrastive training loss
|
|
2300
|
+
- `agentdb_vector_count` — total vectors stored
|
|
2301
|
+
|
|
2302
|
+
</details>
|
|
2303
|
+
|
|
2304
|
+
---
|
|
2305
|
+
|
|
2306
|
+
### Additional Capabilities
|
|
2307
|
+
|
|
2308
|
+
Reinforcement learning, hybrid search, LLM routing, authentication, and high-performance wrappers.
|
|
2309
|
+
|
|
2310
|
+
<details>
|
|
2311
|
+
<summary><strong>Reinforcement Learning</strong> — 9 algorithms from Q-Learning to Monte Carlo Tree Search</summary>
|
|
2312
|
+
|
|
2313
|
+
AgentDB includes a complete reinforcement learning system with 9 algorithms for building agents that learn optimal strategies through interaction:
|
|
2314
|
+
|
|
2315
|
+
### Available Algorithms
|
|
2316
|
+
|
|
2317
|
+
| Algorithm | Type | Best For |
|
|
2318
|
+
| --------------------------- | -------------- | ------------------------------------------------------- |
|
|
2319
|
+
| **Q-Learning** | Value-based | Simple discrete action spaces |
|
|
2320
|
+
| **SARSA** | Value-based | On-policy learning, safer exploration |
|
|
2321
|
+
| **Deep Q-Network (DQN)** | Value-based | Complex state spaces with neural function approximation |
|
|
2322
|
+
| **Policy Gradient** | Policy-based | Continuous action spaces |
|
|
2323
|
+
| **Actor-Critic** | Hybrid | Balances value and policy learning |
|
|
2324
|
+
| **PPO** | Policy-based | Stable training with clipped objectives |
|
|
2325
|
+
| **Decision Transformer** | Sequence-based | Offline RL from logged trajectories |
|
|
2326
|
+
| **Monte Carlo Tree Search** | Planning | Game-like decision trees, look-ahead planning |
|
|
2327
|
+
| **Model-Based RL** | Model-based | Sample-efficient learning with world models |
|
|
2328
|
+
|
|
2329
|
+
### Usage
|
|
2330
|
+
|
|
2331
|
+
```typescript
|
|
2332
|
+
import { LearningSystem } from "agentdb";
|
|
2333
|
+
|
|
2334
|
+
const learner = new LearningSystem(db, {
|
|
2335
|
+
algorithm: "ppo", // any of the 9 algorithms
|
|
2336
|
+
learningRate: 0.001,
|
|
2337
|
+
discountFactor: 0.99,
|
|
2338
|
+
explorationRate: 0.1,
|
|
2339
|
+
});
|
|
2340
|
+
|
|
2341
|
+
// Start a learning session
|
|
2342
|
+
const session = await learner.createSession({
|
|
2343
|
+
task: "optimize_api_responses",
|
|
2344
|
+
maxEpisodes: 100,
|
|
2345
|
+
});
|
|
2346
|
+
|
|
2347
|
+
// Predict next action
|
|
2348
|
+
const prediction = await learner.predict(session.id, currentState);
|
|
2349
|
+
// { action, confidence, explorationUsed }
|
|
2350
|
+
|
|
2351
|
+
// Record outcome
|
|
2352
|
+
await learner.recordReward(session.id, prediction.action, reward);
|
|
2353
|
+
|
|
2354
|
+
// Train on accumulated experience
|
|
2355
|
+
await learner.train(session.id);
|
|
2356
|
+
|
|
2357
|
+
// Transfer learning to new tasks
|
|
2358
|
+
await learner.transfer(sourceSessionId, targetSessionId);
|
|
2359
|
+
```
|
|
2360
|
+
|
|
2361
|
+
### MCP Tools for RL
|
|
2362
|
+
|
|
2363
|
+
All 9 algorithms are accessible through MCP tools:
|
|
2364
|
+
|
|
2365
|
+
- `agentdb_rl_create_session` — start a new learning session
|
|
2366
|
+
- `agentdb_rl_predict` — get next action recommendation
|
|
2367
|
+
- `agentdb_rl_record_reward` — record outcome feedback
|
|
2368
|
+
- `agentdb_rl_train` — trigger training round
|
|
2369
|
+
- `agentdb_rl_transfer` — transfer learned policy to new task
|
|
2370
|
+
- `agentdb_rl_explain` — explain why a prediction was made
|
|
2371
|
+
|
|
2372
|
+
</details>
|
|
2373
|
+
|
|
2374
|
+
---
|
|
2375
|
+
|
|
2376
|
+
<details>
|
|
2377
|
+
<summary><strong>Hybrid Search</strong> — BM25 keyword + vector fusion for best-of-both-worlds retrieval</summary>
|
|
2378
|
+
|
|
2379
|
+
Combine traditional keyword search (BM25) with vector similarity for retrieval that handles both exact matches and semantic meaning:
|
|
2380
|
+
|
|
2381
|
+
```typescript
|
|
2382
|
+
import { createHybridSearch, createKeywordIndex } from "agentdb";
|
|
2383
|
+
|
|
2384
|
+
// Create keyword index (BM25)
|
|
2385
|
+
const keywordIndex = createKeywordIndex({
|
|
2386
|
+
k1: 1.2, // term frequency saturation
|
|
2387
|
+
b: 0.75, // document length normalization
|
|
2388
|
+
avgDocLength: 100,
|
|
2389
|
+
});
|
|
2390
|
+
|
|
2391
|
+
// Index documents
|
|
2392
|
+
keywordIndex.add("doc-1", "JWT authentication with refresh token rotation");
|
|
2393
|
+
keywordIndex.add("doc-2", "OAuth2 PKCE flow for mobile applications");
|
|
2394
|
+
|
|
2395
|
+
// Create hybrid search (keyword + vector)
|
|
2396
|
+
const hybrid = createHybridSearch(keywordIndex, vectorBackend, {
|
|
2397
|
+
fusion: "rrf", // 'rrf' (Reciprocal Rank Fusion) | 'linear' | 'max'
|
|
2398
|
+
keywordWeight: 0.3, // 30% keyword, 70% vector
|
|
2399
|
+
vectorWeight: 0.7,
|
|
2400
|
+
});
|
|
2401
|
+
|
|
2402
|
+
// Search with both keyword and semantic matching
|
|
2403
|
+
const results = await hybrid.search({
|
|
2404
|
+
text: "JWT refresh token", // keyword query
|
|
2405
|
+
embedding: queryEmbedding, // vector query
|
|
2406
|
+
k: 10,
|
|
2407
|
+
});
|
|
2408
|
+
// Results fused from both sources — exact term matches + semantic similarity
|
|
2409
|
+
```
|
|
2410
|
+
|
|
2411
|
+
### Fusion Strategies
|
|
2412
|
+
|
|
2413
|
+
| Strategy | How it works | Best For |
|
|
2414
|
+
| ----------------- | ------------------------------------------------------------------- | -------------------------------- |
|
|
2415
|
+
| **RRF** (default) | `1/(k + rank)` — rank-based fusion, no score calibration needed | General use |
|
|
2416
|
+
| **Linear** | `α × keyword_score + β × vector_score` — weighted score combination | When you know the right balance |
|
|
2417
|
+
| **Max** | `max(keyword_score, vector_score)` — takes highest signal | When either source is sufficient |
|
|
2418
|
+
|
|
2419
|
+
> **When to use hybrid search:** When your data contains domain-specific terms (function names, error codes, product IDs) that vector embeddings might miss, but you also want semantic understanding for natural language queries.
|
|
2420
|
+
|
|
2421
|
+
</details>
|
|
2422
|
+
|
|
2423
|
+
---
|
|
2424
|
+
|
|
2425
|
+
<details>
|
|
2426
|
+
<summary><strong>LLM Router</strong> — auto-select from 5 providers based on cost, quality, and privacy</summary>
|
|
2427
|
+
|
|
2428
|
+
AgentDB can route LLM queries to the optimal provider based on your constraints:
|
|
2429
|
+
|
|
2430
|
+
```typescript
|
|
2431
|
+
import { LLMRouter } from "agentdb";
|
|
2432
|
+
|
|
2433
|
+
const router = new LLMRouter({
|
|
2434
|
+
providers: {
|
|
2435
|
+
ruvllm: { enabled: true }, // Local, SIMD-optimized, free
|
|
2436
|
+
openrouter: { apiKey: process.env.OPENROUTER_KEY }, // 200+ models, 99% cost savings
|
|
2437
|
+
gemini: { apiKey: process.env.GEMINI_KEY }, // Free tier available
|
|
2438
|
+
anthropic: { apiKey: process.env.ANTHROPIC_KEY }, // Claude models
|
|
2439
|
+
onnx: { enabled: true }, // Local models via transformers.js
|
|
2440
|
+
},
|
|
2441
|
+
strategy: "auto", // auto-selects based on constraints below
|
|
2442
|
+
constraints: {
|
|
2443
|
+
maxCostPerQuery: 0.001, // max $0.001 per query
|
|
2444
|
+
minQuality: 0.8, // minimum quality score
|
|
2445
|
+
preferLocal: true, // prefer local models when possible
|
|
2446
|
+
requirePrivacy: false, // if true, only uses local providers
|
|
2447
|
+
},
|
|
2448
|
+
});
|
|
2449
|
+
|
|
2450
|
+
const response = await router.generate("Summarize this document...", {
|
|
2451
|
+
maxTokens: 500,
|
|
2452
|
+
temperature: 0.7,
|
|
2453
|
+
});
|
|
2454
|
+
```
|
|
2455
|
+
|
|
2456
|
+
### Provider Comparison
|
|
2457
|
+
|
|
2458
|
+
| Provider | Latency | Cost | Privacy | Models |
|
|
2459
|
+
| -------------- | ------- | --------- | ------------ | ---------------------------------------- |
|
|
2460
|
+
| **RuvLLM** | Fastest | Free | Full (local) | SIMD-optimized local models |
|
|
2461
|
+
| **ONNX** | Fast | Free | Full (local) | transformers.js models |
|
|
2462
|
+
| **OpenRouter** | ~500ms | $0.0001+ | Cloud | 200+ models (GPT-4, Claude, Llama, etc.) |
|
|
2463
|
+
| **Gemini** | ~300ms | Free tier | Cloud | Gemini Pro, Flash |
|
|
2464
|
+
| **Anthropic** | ~1s | $0.003+ | Cloud | Claude Opus, Sonnet, Haiku |
|
|
2465
|
+
|
|
2466
|
+
```typescript
|
|
2467
|
+
// Check what's available locally
|
|
2468
|
+
import { isRuvLLMInstalled } from "agentdb";
|
|
2469
|
+
if (await isRuvLLMInstalled()) {
|
|
2470
|
+
// Local inference available — no API keys needed
|
|
2471
|
+
}
|
|
2472
|
+
```
|
|
2473
|
+
|
|
2474
|
+
</details>
|
|
2475
|
+
|
|
2476
|
+
---
|
|
2477
|
+
|
|
2478
|
+
<details>
|
|
2479
|
+
<summary><strong>Auth & Audit</strong> — JWT, API keys, Argon2id, and compliance-ready logging</summary>
|
|
2480
|
+
|
|
2481
|
+
Enterprise-ready authentication and audit logging for production deployments.
|
|
2482
|
+
|
|
2483
|
+
### Authentication
|
|
2484
|
+
|
|
2485
|
+
```typescript
|
|
2486
|
+
import { AuthService } from "agentdb/services/auth";
|
|
2487
|
+
|
|
2488
|
+
const auth = new AuthService(db, {
|
|
2489
|
+
jwtSecret: process.env.JWT_SECRET,
|
|
2490
|
+
accessTokenTTL: "15m",
|
|
2491
|
+
refreshTokenTTL: "7d",
|
|
2492
|
+
maxLoginAttempts: 5, // account lockout after 5 failures
|
|
2493
|
+
lockoutDuration: "30m",
|
|
2494
|
+
});
|
|
2495
|
+
|
|
2496
|
+
// User registration (Argon2id password hashing)
|
|
2497
|
+
const user = await auth.register({ email, password, role: "user" });
|
|
2498
|
+
|
|
2499
|
+
// Login — returns access + refresh token pair
|
|
2500
|
+
const { accessToken, refreshToken } = await auth.login(email, password);
|
|
2501
|
+
|
|
2502
|
+
// Token verification
|
|
2503
|
+
const payload = await auth.verifyAccessToken(accessToken);
|
|
2504
|
+
|
|
2505
|
+
// API key management
|
|
2506
|
+
const apiKey = await auth.generateApiKey(userId, {
|
|
2507
|
+
name: "prod-key",
|
|
2508
|
+
scopes: ["read", "write"],
|
|
2509
|
+
});
|
|
2510
|
+
await auth.rotateApiKey(apiKey.id); // automatic rotation
|
|
2511
|
+
```
|
|
2512
|
+
|
|
2513
|
+
### Audit Logging
|
|
2514
|
+
|
|
2515
|
+
```typescript
|
|
2516
|
+
import { AuditLogger } from "agentdb/services/audit-logger";
|
|
2517
|
+
|
|
2518
|
+
const audit = new AuditLogger({
|
|
2519
|
+
retention: "90d", // log retention period
|
|
2520
|
+
compliance: ["SOC2", "GDPR"], // compliance frameworks
|
|
2521
|
+
autoRotate: true, // automatic log rotation
|
|
2522
|
+
});
|
|
2523
|
+
|
|
2524
|
+
// 13 audit event types tracked automatically:
|
|
2525
|
+
// - jwt_auth, api_key_create, api_key_rotate, api_key_revoke
|
|
2526
|
+
// - login_success, login_failure, registration
|
|
2527
|
+
// - config_change, permission_change
|
|
2528
|
+
// - rate_limit_exceeded, sensitive_data_access
|
|
2529
|
+
// - data_export, data_deletion
|
|
2530
|
+
```
|
|
2531
|
+
|
|
2532
|
+
### Security Utilities
|
|
2533
|
+
|
|
2534
|
+
```typescript
|
|
2535
|
+
import {
|
|
2536
|
+
hashPassword,
|
|
2537
|
+
verifyPassword,
|
|
2538
|
+
generateApiKey,
|
|
2539
|
+
} from "agentdb/utils/crypto";
|
|
2540
|
+
|
|
2541
|
+
const hash = await hashPassword("user-password"); // Argon2id
|
|
2542
|
+
const valid = await verifyPassword("user-password", hash);
|
|
2543
|
+
const key = generateApiKey(); // cryptographically random
|
|
2544
|
+
```
|
|
2545
|
+
|
|
2546
|
+
</details>
|
|
2547
|
+
|
|
2548
|
+
---
|
|
2549
|
+
|
|
2550
|
+
<details>
|
|
2551
|
+
<summary><strong>Metadata Filtering</strong> — 10 MongoDB-style query operators</summary>
|
|
2552
|
+
|
|
2553
|
+
Filter search results and database queries using MongoDB-style operators:
|
|
2554
|
+
|
|
2555
|
+
```typescript
|
|
2556
|
+
import { MetadataFilter } from "agentdb";
|
|
2557
|
+
|
|
2558
|
+
const filter = new MetadataFilter();
|
|
2559
|
+
|
|
2560
|
+
// Episodes with reward > 0.8 and specific tags
|
|
2561
|
+
const episodes = await filter.filterEpisodes(db, {
|
|
2562
|
+
reward: { $gt: 0.8 },
|
|
2563
|
+
tags: { $contains: "security" },
|
|
2564
|
+
status: { $in: ["success", "partial"] },
|
|
2565
|
+
archived: { $exists: false },
|
|
2566
|
+
});
|
|
2567
|
+
|
|
2568
|
+
// Patterns matching complex criteria
|
|
2569
|
+
const patterns = await filter.filterPatterns(db, {
|
|
2570
|
+
successRate: { $gte: 0.7, $lte: 1.0 },
|
|
2571
|
+
taskType: { $ne: "deprecated" },
|
|
2572
|
+
createdAt: { $gt: lastWeekTimestamp },
|
|
2573
|
+
});
|
|
2574
|
+
```
|
|
2575
|
+
|
|
2576
|
+
### Supported Operators
|
|
2577
|
+
|
|
2578
|
+
| Operator | Description | Example |
|
|
2579
|
+
| ----------- | --------------------- | ----------------------------------- |
|
|
2580
|
+
| `$eq` | Equal | `{ status: { $eq: 'active' } }` |
|
|
2581
|
+
| `$ne` | Not equal | `{ role: { $ne: 'admin' } }` |
|
|
2582
|
+
| `$gt` | Greater than | `{ reward: { $gt: 0.5 } }` |
|
|
2583
|
+
| `$gte` | Greater than or equal | `{ score: { $gte: 0.8 } }` |
|
|
2584
|
+
| `$lt` | Less than | `{ age: { $lt: 90 } }` |
|
|
2585
|
+
| `$lte` | Less than or equal | `{ priority: { $lte: 3 } }` |
|
|
2586
|
+
| `$in` | In array | `{ type: { $in: ['a', 'b'] } }` |
|
|
2587
|
+
| `$nin` | Not in array | `{ status: { $nin: ['deleted'] } }` |
|
|
2588
|
+
| `$contains` | Contains substring | `{ tags: { $contains: 'auth' } }` |
|
|
2589
|
+
| `$exists` | Field exists | `{ metadata: { $exists: true } }` |
|
|
2590
|
+
|
|
2591
|
+
</details>
|
|
2592
|
+
|
|
2593
|
+
---
|
|
2594
|
+
|
|
2595
|
+
<details>
|
|
2596
|
+
<summary><strong>AgentDBFast</strong> — 50-200x faster programmatic API (skip the CLI overhead)</summary>
|
|
2597
|
+
|
|
2598
|
+
The `AgentDBFast` wrapper provides direct programmatic access that's 50-200x faster than going through the CLI:
|
|
2599
|
+
|
|
2600
|
+
```typescript
|
|
2601
|
+
import { createFastAgentDB } from "agentdb/wrappers/agentdb-fast";
|
|
2602
|
+
|
|
2603
|
+
const db = await createFastAgentDB({
|
|
2604
|
+
path: "./agent-memory.db",
|
|
2605
|
+
dimension: 384,
|
|
2606
|
+
model: "Xenova/all-MiniLM-L6-v2",
|
|
2607
|
+
});
|
|
2608
|
+
|
|
2609
|
+
// All operations bypass CLI parsing overhead
|
|
2610
|
+
await db.insert("doc-1", embedding, metadata);
|
|
2611
|
+
const results = await db.search(queryEmbedding, 10);
|
|
2612
|
+
await db.batchInsert(documents);
|
|
2613
|
+
|
|
2614
|
+
// Performance wrapper includes:
|
|
2615
|
+
// - Direct function calls (no CLI arg parsing)
|
|
2616
|
+
// - Pre-initialized connections (no startup cost per call)
|
|
2617
|
+
// - Batched operations
|
|
2618
|
+
// - Connection pooling
|
|
2619
|
+
```
|
|
2620
|
+
|
|
2621
|
+
### Wrapper Performance vs CLI
|
|
2622
|
+
|
|
2623
|
+
| Operation | CLI | AgentDBFast | Speedup |
|
|
2624
|
+
| ------------------ | ------ | ----------- | ------- |
|
|
2625
|
+
| Single insert | ~20ms | ~0.1ms | 200x |
|
|
2626
|
+
| Search (k=10) | ~15ms | ~0.08ms | 187x |
|
|
2627
|
+
| Batch insert (100) | ~500ms | ~10ms | 50x |
|
|
2628
|
+
|
|
2629
|
+
### Attention Wrappers
|
|
2630
|
+
|
|
2631
|
+
Production-ready attention implementations with 11-22x speedup over naive:
|
|
2632
|
+
|
|
2633
|
+
```typescript
|
|
2634
|
+
import {
|
|
2635
|
+
MultiHeadAttention,
|
|
2636
|
+
FlashAttention,
|
|
2637
|
+
LinearAttention,
|
|
2638
|
+
HyperbolicAttention,
|
|
2639
|
+
MoEAttention,
|
|
2640
|
+
} from "agentdb/wrappers";
|
|
2641
|
+
|
|
2642
|
+
// Flash Attention — O(N) memory instead of O(N^2)
|
|
2643
|
+
const flash = new FlashAttention({ numHeads: 8 });
|
|
2644
|
+
const result = await flash.forward(query, keys, values);
|
|
2645
|
+
|
|
2646
|
+
// Mixture of Experts — routes to specialized attention heads
|
|
2647
|
+
const moe = new MoEAttention({ numExperts: 4, topK: 2 });
|
|
2648
|
+
const result = await moe.forward(query, keys, values);
|
|
2649
|
+
```
|
|
2650
|
+
|
|
2651
|
+
### Embedding Providers
|
|
2652
|
+
|
|
2653
|
+
Multiple embedding backends with automatic fallback:
|
|
2654
|
+
|
|
2655
|
+
```typescript
|
|
2656
|
+
import {
|
|
2657
|
+
OpenAIEmbeddingService,
|
|
2658
|
+
TransformersEmbeddingService,
|
|
2659
|
+
MockEmbeddingService,
|
|
2660
|
+
} from "agentdb/wrappers";
|
|
2661
|
+
|
|
2662
|
+
// OpenAI (cloud)
|
|
2663
|
+
const openai = new OpenAIEmbeddingService({ apiKey: process.env.OPENAI_KEY });
|
|
2664
|
+
|
|
2665
|
+
// Transformers.js (local, free)
|
|
2666
|
+
const local = new TransformersEmbeddingService({
|
|
2667
|
+
model: "Xenova/all-MiniLM-L6-v2",
|
|
2668
|
+
});
|
|
2669
|
+
|
|
2670
|
+
// Mock (testing)
|
|
2671
|
+
const mock = new MockEmbeddingService({ dimension: 384 });
|
|
2672
|
+
```
|
|
2673
|
+
|
|
2674
|
+
</details>
|
|
2675
|
+
|
|
2676
|
+
---
|
|
2677
|
+
|
|
2678
|
+
<details>
|
|
2679
|
+
<summary><strong>Benchmark Suite</strong> — 5 built-in benchmarks with custom benchmark support</summary>
|
|
2680
|
+
|
|
2681
|
+
Run performance benchmarks to validate your deployment:
|
|
2682
|
+
|
|
2683
|
+
```typescript
|
|
2684
|
+
import { BenchmarkSuite, runBenchmarks, formatReportAsMarkdown } from "agentdb";
|
|
2685
|
+
|
|
2686
|
+
// Run all built-in benchmarks
|
|
2687
|
+
const report = await runBenchmarks({
|
|
2688
|
+
dimension: 384,
|
|
2689
|
+
iterations: 100,
|
|
2690
|
+
warmup: 10,
|
|
2691
|
+
});
|
|
2692
|
+
|
|
2693
|
+
console.log(formatReportAsMarkdown(report));
|
|
2694
|
+
```
|
|
2695
|
+
|
|
2696
|
+
### Built-in Benchmarks
|
|
2697
|
+
|
|
2698
|
+
| Benchmark | Measures | Key Metrics |
|
|
2699
|
+
| ------------------------- | --------------------- | --------------------------------------- |
|
|
2700
|
+
| **VectorInsertBenchmark** | Insert throughput | ops/sec, p50/p99 latency |
|
|
2701
|
+
| **VectorSearchBenchmark** | Search performance | ops/sec, recall@k, latency distribution |
|
|
2702
|
+
| **MemoryUsageBenchmark** | RAM consumption | bytes per vector, total footprint |
|
|
2703
|
+
| **ConcurrencyBenchmark** | Parallel performance | throughput under load, contention |
|
|
2704
|
+
| **QuantizationBenchmark** | Compression tradeoffs | quality loss vs memory savings |
|
|
2705
|
+
|
|
2706
|
+
### Custom Benchmarks
|
|
2707
|
+
|
|
2708
|
+
```typescript
|
|
2709
|
+
import { Benchmark, BenchmarkSuite } from "agentdb";
|
|
2710
|
+
|
|
2711
|
+
class MyBenchmark extends Benchmark {
|
|
2712
|
+
name = "my-custom-benchmark";
|
|
2713
|
+
|
|
2714
|
+
async setup() {
|
|
2715
|
+
/* prepare data */
|
|
2716
|
+
}
|
|
2717
|
+
async run() {
|
|
2718
|
+
/* measure this */
|
|
2719
|
+
}
|
|
2720
|
+
async teardown() {
|
|
2721
|
+
/* cleanup */
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
const suite = new BenchmarkSuite();
|
|
2726
|
+
suite.add(new MyBenchmark());
|
|
2727
|
+
const report = await suite.run({ iterations: 50 });
|
|
2728
|
+
|
|
2729
|
+
// Compare reports across runs
|
|
2730
|
+
import { formatComparisonAsMarkdown } from "agentdb";
|
|
2731
|
+
console.log(formatComparisonAsMarkdown(reportBefore, reportAfter));
|
|
2732
|
+
```
|
|
2733
|
+
|
|
2734
|
+
### CLI Benchmarks
|
|
2735
|
+
|
|
2736
|
+
```bash
|
|
2737
|
+
npm run benchmark # Quick benchmark
|
|
2738
|
+
npm run benchmark:full # Full suite
|
|
2739
|
+
npm run benchmark:attention # Attention mechanism performance
|
|
2740
|
+
npm run benchmark:backends # Compare all backends
|
|
2741
|
+
npm run benchmark:ruvector # RuVector-specific benchmarks
|
|
2742
|
+
npm run benchmark:all # Everything
|
|
2743
|
+
```
|
|
2744
|
+
|
|
2745
|
+
</details>
|
|
2746
|
+
|
|
2747
|
+
---
|
|
2748
|
+
|
|
2749
|
+
## Testing
|
|
2750
|
+
|
|
2751
|
+
<details>
|
|
2752
|
+
<summary><strong>Test Suite</strong> — unit, integration, performance, security, and browser tests</summary>
|
|
2753
|
+
|
|
2754
|
+
AgentDB ships with a comprehensive test suite covering every layer of the stack. All tests run via [Vitest](https://vitest.dev/) with parallel execution.
|
|
2755
|
+
|
|
2756
|
+
### Quick Commands
|
|
2757
|
+
|
|
2758
|
+
```bash
|
|
2759
|
+
npm test # Run all tests (watch mode)
|
|
2760
|
+
npm run test:unit # Unit tests only (single run)
|
|
2761
|
+
npm run test:browser # Browser bundle + WASM tests
|
|
2762
|
+
npm run test:ci # CI pipeline: browser tests + build + bundle verification
|
|
2763
|
+
```
|
|
2764
|
+
|
|
2765
|
+
### What's Tested
|
|
2766
|
+
|
|
2767
|
+
| Suite | Coverage | What it validates |
|
|
2768
|
+
| --------------------- | ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
|
|
2769
|
+
| **Unit tests** | Core controllers, memory patterns, embedding service, quantization | All 6 cognitive memory patterns, 9 RL algorithms, filter expressions, security validators |
|
|
2770
|
+
| **Browser tests** | WASM store, attention mechanisms, product quantization, SIMD fallbacks | Full vector DB in browser, Flash/Hyperbolic/MoE attention, PQ8/PQ16/PQ32 compression |
|
|
2771
|
+
| **Integration tests** | End-to-end flows, backend auto-selection, MCP tool execution | Self-learning pipeline, Cognitive Container operations, federated aggregation |
|
|
2772
|
+
| **Performance tests** | Latency benchmarks, throughput, scaling behavior | 61us search target, 32.6M ops/sec pattern search, batch insert speedup |
|
|
2773
|
+
| **Security tests** | Input validation, injection prevention, witness chain verification | XSS patterns, SQL injection, path traversal, Cypher sanitization, SHAKE-256 |
|
|
2774
|
+
|
|
2775
|
+
### Benchmarks
|
|
2776
|
+
|
|
2777
|
+
```bash
|
|
2778
|
+
npm run benchmark # Quick benchmark (insert + search)
|
|
2779
|
+
npm run benchmark:full # Full suite with all backends
|
|
2780
|
+
npm run benchmark:attention # Attention mechanism performance
|
|
2781
|
+
npm run benchmark:backends # Compare RuVector vs RVF vs HNSWLib vs sql.js
|
|
2782
|
+
npm run benchmark:ruvector # RuVector-specific benchmarks
|
|
2783
|
+
npm run benchmark:all # Run everything
|
|
2784
|
+
```
|
|
2785
|
+
|
|
2786
|
+
### CI Pipeline
|
|
2787
|
+
|
|
2788
|
+
The CI pipeline runs `test:ci` which executes:
|
|
2789
|
+
|
|
2790
|
+
1. **Browser bundle tests** — verifies WASM and JS fallbacks work
|
|
2791
|
+
2. **TypeScript build** — full `tsc` compilation
|
|
2792
|
+
3. **Bundle verification** — ensures all export paths resolve correctly
|
|
2793
|
+
|
|
2794
|
+
### Writing Custom Tests
|
|
2795
|
+
|
|
2796
|
+
```typescript
|
|
2797
|
+
import { describe, it, expect } from "vitest";
|
|
2798
|
+
import { createDatabase, ReasoningBank, EmbeddingService } from "agentdb";
|
|
2799
|
+
|
|
2800
|
+
describe("My Agent Memory", () => {
|
|
2801
|
+
it("stores and retrieves patterns", async () => {
|
|
2802
|
+
const db = await createDatabase(":memory:");
|
|
2803
|
+
const embedder = new EmbeddingService({ model: "Xenova/all-MiniLM-L6-v2" });
|
|
2804
|
+
await embedder.initialize();
|
|
2805
|
+
|
|
2806
|
+
const bank = new ReasoningBank(db, embedder);
|
|
2807
|
+
await bank.storePattern({
|
|
2808
|
+
taskType: "test",
|
|
2809
|
+
approach: "TDD",
|
|
2810
|
+
successRate: 0.95,
|
|
2811
|
+
});
|
|
2812
|
+
|
|
2813
|
+
const results = await bank.searchPatterns({ task: "testing", k: 5 });
|
|
2814
|
+
expect(results.length).toBeGreaterThan(0);
|
|
2815
|
+
});
|
|
2816
|
+
});
|
|
2817
|
+
```
|
|
2818
|
+
|
|
2819
|
+
</details>
|
|
2820
|
+
|
|
2821
|
+
---
|
|
2822
|
+
|
|
2823
|
+
## Documentation
|
|
2824
|
+
|
|
2825
|
+
| Document | Description |
|
|
2826
|
+
| ------------------------------------------------------------------------------------ | ------------------------------------------ |
|
|
2827
|
+
| [MCP Tool Optimization Guide](docs/guides/) | Comprehensive MCP patterns |
|
|
2828
|
+
| [Embedding Models Guide](docs/guides/) | Model selection and benchmarks |
|
|
2829
|
+
| [Simulation System](simulation/README.md) | 31 scenarios, interactive wizard |
|
|
2830
|
+
| [Security Hardening](docs/guides/) | Input validation and security patterns |
|
|
2831
|
+
| [Chat Ecosystem](../agentdb-chat/README.md) | `@agentdb/chat` integration guide |
|
|
2832
|
+
| [ADR-010: Solver Integration](docs/adrs/ADR-010-rvf-solver-v014-deep-integration.md) | SolverBandit + rvf-solver deep integration |
|
|
2833
|
+
|
|
2834
|
+
---
|
|
2835
|
+
|
|
2836
|
+
## Contributing
|
|
2837
|
+
|
|
2838
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines. Areas of interest: additional RL algorithms, performance optimizations, new backend integrations, documentation improvements.
|
|
2839
|
+
|
|
2840
|
+
---
|
|
2841
|
+
|
|
2842
|
+
## License
|
|
2843
|
+
|
|
2844
|
+
MIT OR Apache-2.0 — See [LICENSE-MIT](LICENSE-MIT) and [LICENSE-APACHE](LICENSE-APACHE).
|
|
2845
|
+
|
|
2846
|
+
---
|
|
2847
|
+
|
|
2848
|
+
**The only vector database that learns.** Start in 30 seconds:
|
|
2849
|
+
|
|
2850
|
+
```bash
|
|
2851
|
+
npm install agentdb
|
|
2852
|
+
```
|
|
2853
|
+
|
|
2854
|
+
[Quick Start](#quick-start) | [Tutorials](#tutorials) | [MCP Integration](#mcp-integration) | [GitHub](https://github.com/ruvnet/agentic-flow/tree/main/packages/agentdb) | [npm](https://www.npmjs.com/package/agentdb)
|