pulsehive-db 0.6.0

Embedded database for agentic AI systems — collective memory for multi-agent coordination
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# PulseDB: Developer Guide

> **Version:** 1.0.0  
> **Status:** Approved  
> **Last Updated:** February 2026  
> **Owner:** PulseDB Team

---

## 1. Getting Started

### 1.1 Prerequisites

| Requirement | Version | Notes |
|-------------|---------|-------|
| Rust | 1.75+ | MSRV |
| Cargo | Latest | Comes with Rust |
| (No C++ needed) || hnsw_rs is pure Rust (ADR-005) |

**Platform Support:**
- Linux (x86_64, aarch64)
- macOS (x86_64, aarch64/Apple Silicon)
- Windows (x86_64)

### 1.2 Clone and Build

```bash
# Clone repository
git clone https://github.com/pulsedb/pulsedb.git
cd pulsedb

# Build (debug)
cargo build

# Build (release)
cargo build --release

# Build without ONNX (smaller binary)
cargo build --release --no-default-features
```

### 1.3 Run Tests

```bash
# All tests
cargo test

# With output
cargo test -- --nocapture

# Specific test
cargo test test_experience_crud

# Integration tests only
cargo test --test '*'
```

### 1.4 Run Benchmarks

```bash
# All benchmarks
cargo bench

# Specific benchmark
cargo bench search_similar
```

---

## 2. Project Structure

```
pulsedb/
├── Cargo.toml              # Package manifest
├── README.md               # User-facing readme
├── LICENSE                 # MIT license
├── CHANGELOG.md            # Version history
│
├── src/
│   ├── lib.rs              # Library entry, re-exports
│   ├── db.rs               # PulseDB struct, lifecycle
│   ├── config.rs           # Configuration types
│   ├── types.rs            # Core types (IDs, timestamps)
│   ├── error.rs            # Error types
│   │
│   ├── collective/
│   │   ├── mod.rs          # Collective management
│   │   └── tests.rs        # Unit tests
│   │
│   ├── experience/
│   │   ├── mod.rs          # Experience CRUD
│   │   ├── types.rs        # Experience, ExperienceType
│   │   ├── validation.rs   # Input validation
│   │   └── tests.rs        # Unit tests
│   │
│   ├── search/
│   │   ├── mod.rs          # Search operations
│   │   ├── query.rs        # Query engine
│   │   ├── filter.rs       # Search filters
│   │   └── tests.rs        # Unit tests
│   │
│   ├── relation/
│   │   ├── mod.rs          # Relation storage
│   │   └── tests.rs        # Unit tests
│   │
│   ├── insight/
│   │   ├── mod.rs          # Insight storage
│   │   └── tests.rs        # Unit tests
│   │
│   ├── activity/
│   │   ├── mod.rs          # Activity tracking
│   │   └── tests.rs        # Unit tests
│   │
│   ├── watch/
│   │   ├── mod.rs          # Watch system
│   │   ├── channel.rs      # In-process channels
│   │   └── tests.rs        # Unit tests
│   │
│   ├── storage/
│   │   ├── mod.rs          # Storage abstraction
│   │   ├── redb.rs         # redb implementation
│   │   ├── tables.rs       # Table definitions
│   │   └── tests.rs        # Unit tests
│   │
│   ├── vector/
│   │   ├── mod.rs          # Vector index abstraction
│   │   ├── hnsw.rs         # HNSW wrapper
│   │   └── tests.rs        # Unit tests
│   │
│   ├── embedding/
│   │   ├── mod.rs          # Embedding service
│   │   ├── onnx.rs         # ONNX provider
│   │   └── tests.rs        # Unit tests
│   │
│   └── substrate/
│       ├── mod.rs          # SubstrateProvider trait
│       └── impl.rs         # PulseDBSubstrate implementation
│
├── tests/
│   ├── common/             # Test utilities
│   │   └── mod.rs
│   ├── experience_lifecycle.rs
│   ├── search_integration.rs
│   ├── collective_isolation.rs
│   ├── watch_integration.rs
│   └── e2e/
│       └── hive_mind_simulation.rs
│
├── benches/
│   ├── micro.rs            # Micro-benchmarks
│   ├── workloads.rs        # Workload benchmarks
│   └── concurrency.rs      # Concurrency benchmarks
│
├── fuzz/
│   └── fuzz_targets/
│       ├── record_experience.rs
│       └── search.rs
│
├── examples/
│   ├── basic_usage.rs      # Simple example
│   ├── multi_agent.rs      # Multi-agent demo
│   └── substrate_provider.rs # PulseHive integration
│
└── docs/
    ├── 01-PRD.md
    ├── 02-SRS.md
    ├── ...
    └── 12-Operations.md
```

---

## 3. Code Style & Conventions

### 3.1 Rust Style

Follow the [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/).

```rust
// ✓ Good: Descriptive names, proper visibility
pub struct Experience {
    pub id: ExperienceId,
    pub content: String,
    // ...
}

// ✗ Bad: Abbreviated, unclear
pub struct Exp {
    pub i: ExpId,
    pub c: String,
}
```

### 3.2 Error Handling

```rust
// ✓ Good: Use Result, specific error types
pub fn record_experience(&self, exp: NewExperience) -> Result<ExperienceId, PulseDBError> {
    exp.validate()?;
    // ...
}

// ✗ Bad: Panic, generic errors
pub fn record_experience(&self, exp: NewExperience) -> ExperienceId {
    if exp.content.is_empty() {
        panic!("Content cannot be empty");  // Don't panic!
    }
    // ...
}
```

### 3.3 Documentation

```rust
/// Records a new experience to the collective consciousness.
///
/// This is the primary write operation. When an agent learns something,
/// it records an experience that other agents can perceive.
///
/// # Arguments
///
/// * `experience` - The new experience to record
///
/// # Returns
///
/// The unique identifier for the recorded experience.
///
/// # Errors
///
/// Returns `ValidationError` if:
/// - Content is empty or exceeds 100KB
/// - Importance/confidence not in 0.0-1.0 range
/// - Embedding dimension doesn't match collective
///
/// # Example
///
/// ```rust
/// let id = db.record_experience(NewExperience {
///     collective_id,
///     content: "User prefers concise responses".into(),
///     importance: 0.8,
///     ..Default::default()
/// })?;
/// ```
pub fn record_experience(&self, experience: NewExperience) -> Result<ExperienceId, PulseDBError> {
    // ...
}
```

### 3.4 Naming Conventions

| Type | Convention | Example |
|------|------------|---------|
| Types | PascalCase | `Experience`, `CollectiveId` |
| Functions | snake_case | `record_experience`, `search_similar` |
| Constants | SCREAMING_SNAKE_CASE | `MAX_CONTENT_SIZE` |
| Modules | snake_case | `experience`, `search` |
| Feature flags | kebab-case | `builtin-embeddings` |

### 3.5 Module Organization

```rust
// src/experience/mod.rs

// Re-exports at top
pub use self::types::{Experience, ExperienceId, ExperienceType, NewExperience};
pub use self::validation::ExperienceUpdate;

// Module declarations
mod types;
mod validation;

#[cfg(test)]
mod tests;

// Implementation
impl Experience {
    // Methods here
}
```

---

## 4. Architecture Decisions

### 4.1 ADR Template

```markdown
# ADR-XXX: Title

## Status
Proposed | Accepted | Deprecated | Superseded by ADR-YYY

## Context
What is the issue that we're seeing that is motivating this decision?

## Decision
What is the change that we're proposing/doing?

## Consequences
What becomes easier or harder because of this change?
```

### 4.2 Key ADRs

#### ADR-001: Use redb for Storage

**Status:** Accepted

**Context:** Need embedded key-value storage with ACID transactions.

**Decision:** Use redb instead of SQLite or RocksDB.

**Consequences:**
- ✓ Pure Rust, no C/C++ dependencies
- ✓ Simple API, good performance
- ✓ MVCC for concurrent reads
- ✗ Less battle-tested than SQLite/RocksDB
- ✗ Fewer features (no SQL)

#### ADR-002: Use hnswlib for Vector Index

**Status:** Superseded by ADR-005

#### ADR-005: Pure Rust HNSW via hnsw_rs

**Status:** Accepted

**Context:** Need fast ANN search without C++ FFI risks.

**Decision:** Use hnsw_rs (pure Rust) wrapped behind VectorIndex trait.

**Consequences:**
- ✓ No FFI risks (memory opacity, panic UB, concurrency conflicts)
- ✓ Native filtered search via FilterT trait
- ✓ Cross-compiles trivially (no C++ toolchain)
- ✓ Swappable in 3-5 days via VectorIndex trait

#### ADR-003: Single-Writer Concurrency Model

**Status:** Accepted

**Context:** Need simple, correct concurrency without complexity.

**Decision:** Single writer, multiple readers (SWMR).

**Consequences:**
- ✓ Simple to reason about
- ✓ No write conflicts
- ✓ Matches redb's model
- ✗ Write throughput limited to one thread
- ✗ Batch writes needed for high throughput

---

## 5. Contributing Guidelines

### 5.1 Contribution Workflow

```
1. Fork repository
2. Create feature branch (feat/my-feature)
3. Make changes
4. Run tests (cargo test)
5. Run lints (cargo clippy)
6. Format code (cargo fmt)
7. Submit pull request
8. Address review feedback
9. Merge after approval
```

### 5.2 Branch Naming

| Type | Pattern | Example |
|------|---------|---------|
| Feature | `feat/description` | `feat/batch-insert` |
| Bug fix | `fix/description` | `fix/search-filter` |
| Refactor | `refactor/description` | `refactor/storage-layer` |
| Docs | `docs/description` | `docs/api-examples` |
| Chore | `chore/description` | `chore/update-deps` |

### 5.3 Commit Messages

Follow [Conventional Commits](https://www.conventionalcommits.org/):

```
<type>(<scope>): <description>

[optional body]

[optional footer]
```

**Types:**
- `feat`: New feature
- `fix`: Bug fix
- `docs`: Documentation
- `refactor`: Code refactoring
- `test`: Adding tests
- `chore`: Maintenance

**Examples:**
```
feat(experience): add batch insert support

Adds `record_experiences_batch()` for inserting multiple
experiences in a single transaction.

Closes #42
```

```
fix(search): handle empty embedding gracefully

Previously, passing an empty embedding caused a panic.
Now returns ValidationError::InvalidField.
```

### 5.4 Pull Request Template

```markdown
## Description
Brief description of changes.

## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update

## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests added/updated
- [ ] Manual testing performed

## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Documentation updated
- [ ] No new warnings
```

---

## 6. Review Checklist

### 6.1 Code Review Checklist

```markdown
## Correctness
- [ ] Logic is correct
- [ ] Edge cases handled
- [ ] Error handling appropriate
- [ ] No panics in library code

## Performance
- [ ] No unnecessary allocations
- [ ] No unnecessary clones
- [ ] Appropriate data structures
- [ ] No blocking in async code

## Security
- [ ] Input validated
- [ ] No sensitive data in logs
- [ ] Resource limits respected

## Style
- [ ] Follows conventions
- [ ] Well-documented
- [ ] Tests included
- [ ] No commented-out code

## API
- [ ] API is intuitive
- [ ] Breaking changes documented
- [ ] Deprecations handled properly
```

### 6.2 Pre-Merge Checklist

```markdown
- [ ] CI passes
- [ ] Coverage maintained (>80%)
- [ ] No benchmark regressions (>10%)
- [ ] Documentation updated
- [ ] CHANGELOG updated
- [ ] Version bumped (if needed)
```

---

## 7. Release Process

### 7.1 Version Numbering

Follow [Semantic Versioning](https://semver.org/):

```
MAJOR.MINOR.PATCH

MAJOR: Breaking API changes
MINOR: New features, backward compatible
PATCH: Bug fixes, backward compatible
```

**Pre-1.0:** Breaking changes increment MINOR.

### 7.2 Release Checklist

```markdown
## Pre-Release
- [ ] All tests passing
- [ ] Benchmarks run
- [ ] CHANGELOG updated
- [ ] Version bumped in Cargo.toml
- [ ] Documentation generated
- [ ] Examples verified

## Release
- [ ] Create git tag (v0.1.0)
- [ ] Push tag
- [ ] Publish to crates.io (cargo publish)
- [ ] Create GitHub release
- [ ] Update documentation site

## Post-Release
- [ ] Announce release
- [ ] Monitor for issues
- [ ] Update dependent projects
```

### 7.3 CHANGELOG Format

```markdown
# Changelog

## [Unreleased]

### Added
- New feature X

### Changed
- Modified behavior of Y

### Fixed
- Bug in Z

### Removed
- Deprecated API W

## [0.1.0] - 2026-03-01

### Added
- Initial release
- Core storage functionality
- Vector search
- Experience CRUD
```

---

## 8. Debugging Tips

### 8.1 Logging

```rust
use tracing::{debug, info, warn, error, instrument};

#[instrument(skip(self))]
pub fn record_experience(&self, exp: NewExperience) -> Result<ExperienceId> {
    debug!(?exp.collective_id, content_len = exp.content.len(), "Recording experience");
    
    let id = self.storage.insert(&exp)?;
    info!(?id, "Experience recorded");
    
    Ok(id)
}
```

**Enable logging:**
```bash
RUST_LOG=pulsedb=debug cargo run
RUST_LOG=pulsedb::search=trace cargo run
```

### 8.2 Debugging Tests

```bash
# Run single test with output
cargo test test_name -- --nocapture

# Run with backtrace
RUST_BACKTRACE=1 cargo test test_name

# Run with debug logging
RUST_LOG=debug cargo test test_name -- --nocapture
```

### 8.3 Profiling

```bash
# CPU profiling (Linux)
perf record -F 99 -g target/release/benchmark
perf script | stackcollapse-perf.pl | flamegraph.pl > flame.svg

# CPU profiling (macOS)
cargo instruments -t "Time Profiler" --release --bench micro

# Memory profiling
cargo build --release
heaptrack ./target/release/benchmark
heaptrack_print heaptrack.benchmark.*.zst
```

### 8.4 Common Issues

| Issue | Cause | Solution |
|-------|-------|----------|
| "Database locked" | Another process has lock | Close other process or use different file |
| Dimension mismatch | Wrong embedding size | Check collective's embedding_dimension |
| Slow search | Large dataset, low ef | Increase ef_search or reduce k |
| High memory | HNSW index in memory | Expected; use smaller M or dimension |
| Crash on exit | Drop order issue | Ensure db.close() called |

---

## 9. IDE Setup

### 9.1 VS Code

**Recommended Extensions:**
- rust-analyzer
- Even Better TOML
- CodeLLDB (debugging)
- Error Lens

**settings.json:**
```json
{
    "rust-analyzer.cargo.features": "all",
    "rust-analyzer.checkOnSave.command": "clippy",
    "editor.formatOnSave": true,
    "[rust]": {
        "editor.defaultFormatter": "rust-lang.rust-analyzer"
    }
}
```

### 9.2 IntelliJ IDEA / CLion

- Install Rust plugin
- Enable Clippy: Settings → Languages & Frameworks → Rust → External Linters → Clippy
- Enable format on save: Settings → Tools → Actions on Save → Reformat code

---

## 10. Useful Commands

```bash
# Development
cargo build                    # Build debug
cargo build --release          # Build release
cargo test                     # Run tests
cargo clippy                   # Lint
cargo fmt                      # Format
cargo doc --open              # Generate and open docs

# Analysis
cargo bloat --release         # Binary size analysis
cargo tree                    # Dependency tree
cargo outdated               # Check for updates
cargo audit                  # Security audit

# Benchmarking
cargo bench                  # Run benchmarks
cargo bench -- --save-baseline main  # Save baseline

# Fuzzing (nightly required)
cargo +nightly fuzz run record_experience

# Coverage
cargo tarpaulin --out Html
```

---

## 11. References

- [Rust Book]https://doc.rust-lang.org/book/
- [Rust API Guidelines]https://rust-lang.github.io/api-guidelines/
- [Rust Performance Book]https://nnethercote.github.io/perf-book/
- [03-Architecture.md]./03-Architecture.md
- [08-Testing.md]./08-Testing.md

---

## Changelog

| Version | Date | Author | Changes |
|---------|------|--------|---------|
| 1.0.0 | February 2026 | PulseDB Team | Initial developer guide |