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
//! Type definitions for experiences.
//!
//! An **experience** is the core data type in PulseDB — a unit of learned knowledge
//! that agents share through collectives. Each experience has content, an embedding
//! vector for semantic search, a rich type, and metadata.
//!
//! # Type Hierarchy
//!
//! ```text
//! ExperienceType (rich, with associated data)
//!     ↓ type_tag()
//! ExperienceTypeTag (compact 1-byte discriminant for index keys)
//! ```

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::storage::schema::ExperienceTypeTag;
use crate::types::{AgentId, CollectiveId, ExperienceId, InstanceId, TaskId, Timestamp};

// ============================================================================
// Severity
// ============================================================================

/// Severity level for difficulty experiences.
///
/// Used as associated data in [`ExperienceType::Difficulty`] to indicate
/// how impactful a problem was.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Severity {
    /// Minor impact, easily worked around.
    Low,
    /// Noticeable impact, workaround available.
    Medium,
    /// Significant impact, blocks progress.
    High,
    /// Showstopper, must be resolved immediately.
    Critical,
}

// ============================================================================
// ExperienceType — Rich enum with 9 variants (ADR-004)
// ============================================================================

/// Rich experience type with associated data per variant.
///
/// This is the full type stored in the experience record. For index keys,
/// use [`type_tag()`](Self::type_tag) to get the compact
/// [`ExperienceTypeTag`] discriminant.
///
/// # Variants
///
/// Each variant carries structured data specific to that kind of experience:
/// - **Difficulty** — A problem the agent encountered
/// - **Solution** — A fix for a problem, optionally linked to a Difficulty
/// - **ErrorPattern** — A reusable error signature with fix and prevention
/// - **SuccessPattern** — A proven approach with quality rating
/// - **UserPreference** — A user preference with strength
/// - **ArchitecturalDecision** — A design decision with rationale
/// - **TechInsight** — Technical knowledge about a technology
/// - **Fact** — A verified factual statement with source
/// - **Generic** — Catch-all for uncategorized experiences
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ExperienceType {
    /// Problem encountered by the agent.
    Difficulty {
        /// What the problem is.
        description: String,
        /// How severe the problem is.
        severity: Severity,
    },

    /// Fix for a problem, optionally linked to a Difficulty experience.
    Solution {
        /// Reference to the Difficulty experience this solves, if any.
        problem_ref: Option<ExperienceId>,
        /// The approach taken to solve the problem.
        approach: String,
        /// Whether the solution worked.
        worked: bool,
    },

    /// Reusable error signature with fix and prevention strategy.
    ErrorPattern {
        /// The error signature (e.g., error code, message pattern).
        signature: String,
        /// How to fix occurrences of this error.
        fix: String,
        /// How to prevent this error from occurring.
        prevention: String,
    },

    /// Proven approach with quality rating (0.0–1.0).
    SuccessPattern {
        /// The type of task this pattern applies to.
        task_type: String,
        /// The approach that works.
        approach: String,
        /// Quality rating of the outcome (0.0–1.0).
        quality: f32,
    },

    /// User preference with strength (0.0–1.0).
    UserPreference {
        /// The preference category (e.g., "style", "tooling").
        category: String,
        /// The specific preference.
        preference: String,
        /// How strongly the user feels about this (0.0–1.0).
        strength: f32,
    },

    /// Design decision with rationale.
    ArchitecturalDecision {
        /// The decision made.
        decision: String,
        /// Why this decision was made.
        rationale: String,
    },

    /// Technical knowledge about a specific technology.
    TechInsight {
        /// The technology this insight is about.
        technology: String,
        /// The insight or knowledge.
        insight: String,
    },

    /// Verified factual statement with source attribution.
    Fact {
        /// The factual statement.
        statement: String,
        /// Where this fact was verified.
        source: String,
    },

    /// Catch-all for uncategorized experiences.
    Generic {
        /// Optional category label.
        category: Option<String>,
    },
}

impl ExperienceType {
    /// Returns the compact [`ExperienceTypeTag`] for use in index keys.
    ///
    /// This bridges the rich type (with data) to the 1-byte discriminant
    /// stored in secondary index keys.
    pub fn type_tag(&self) -> ExperienceTypeTag {
        match self {
            Self::Difficulty { .. } => ExperienceTypeTag::Difficulty,
            Self::Solution { .. } => ExperienceTypeTag::Solution,
            Self::ErrorPattern { .. } => ExperienceTypeTag::ErrorPattern,
            Self::SuccessPattern { .. } => ExperienceTypeTag::SuccessPattern,
            Self::UserPreference { .. } => ExperienceTypeTag::UserPreference,
            Self::ArchitecturalDecision { .. } => ExperienceTypeTag::ArchitecturalDecision,
            Self::TechInsight { .. } => ExperienceTypeTag::TechInsight,
            Self::Fact { .. } => ExperienceTypeTag::Fact,
            Self::Generic { .. } => ExperienceTypeTag::Generic,
        }
    }
}

impl Default for ExperienceType {
    fn default() -> Self {
        Self::Generic { category: None }
    }
}

// ============================================================================
// Experience — The full stored record
// ============================================================================

/// A stored experience — the core data type in PulseDB.
///
/// Experiences are agent-learned knowledge units stored in collectives.
/// Each experience has content, a semantic embedding for vector search,
/// a rich type, and metadata for filtering and ranking.
///
/// # Serialization Note
///
/// The `embedding` field is marked `#[serde(skip)]` because embeddings are
/// stored in a separate `EMBEDDINGS_TABLE` for performance. The storage
/// layer reconstitutes the full struct by joining both tables on read.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Experience {
    /// Unique identifier (UUID v7, time-ordered).
    pub id: ExperienceId,

    /// The collective this experience belongs to.
    pub collective_id: CollectiveId,

    /// The experience content (text). Immutable after creation.
    pub content: String,

    /// Semantic embedding vector. Immutable after creation.
    ///
    /// Stored separately in EMBEDDINGS_TABLE; skipped during postcard
    /// serialization of the main experience record.
    #[serde(skip)]
    pub embedding: Vec<f32>,

    /// Rich experience type with associated data.
    pub experience_type: ExperienceType,

    /// Importance score (0.0–1.0). Higher = more important.
    pub importance: f32,

    /// Confidence score (0.0–1.0). Higher = more confident.
    pub confidence: f32,

    /// Per-instance application/reinforcement counters.
    ///
    /// The total application count is available via [`applications()`](Self::applications).
    pub applications: BTreeMap<InstanceId, u32>,

    /// Domain tags for categorical filtering (e.g., ["rust", "async"]).
    pub domain: Vec<String>,

    /// Related source file paths.
    pub related_files: Vec<String>,

    /// The agent that created this experience.
    pub source_agent: AgentId,

    /// Optional task context where this experience was created.
    pub source_task: Option<TaskId>,

    /// When this experience was recorded.
    pub timestamp: Timestamp,

    /// Last time this experience was explicitly reinforced.
    pub last_reinforced: Timestamp,

    /// Whether this experience is archived (soft-deleted).
    ///
    /// Archived experiences are excluded from search results but remain
    /// in storage and can be restored via `unarchive_experience()`.
    pub archived: bool,
}

impl Experience {
    /// Returns the total application count across all instance buckets.
    pub fn applications(&self) -> u32 {
        self.applications
            .values()
            .copied()
            .fold(0u32, u32::saturating_add)
    }
}

// ============================================================================
// NewExperience — Input for record_experience()
// ============================================================================

/// Input for creating a new experience via [`PulseDB::record_experience()`](crate::PulseDB).
///
/// Only the mutable fields are set here. The `id`, `timestamp`, `last_reinforced`,
/// `applications`, and `archived` fields are set automatically by the storage layer.
///
/// # Embedding
///
/// - **External provider**: `embedding` is required (must be `Some`)
/// - **Builtin provider**: `embedding` is optional; if `None`, PulseDB generates it
#[derive(Clone, Debug)]
pub struct NewExperience {
    /// The collective to store this experience in.
    pub collective_id: CollectiveId,

    /// The experience content (text).
    pub content: String,

    /// Rich experience type.
    pub experience_type: ExperienceType,

    /// Pre-computed embedding vector. Required for External provider.
    pub embedding: Option<Vec<f32>>,

    /// Importance score (0.0–1.0).
    pub importance: f32,

    /// Confidence score (0.0–1.0).
    pub confidence: f32,

    /// Domain tags for categorical filtering.
    pub domain: Vec<String>,

    /// Related source file paths.
    pub related_files: Vec<String>,

    /// The agent creating this experience.
    pub source_agent: AgentId,

    /// Optional task context.
    pub source_task: Option<TaskId>,
}

impl Default for NewExperience {
    fn default() -> Self {
        Self {
            collective_id: CollectiveId::nil(),
            content: String::new(),
            experience_type: ExperienceType::default(),
            embedding: None,
            importance: 0.5,
            confidence: 0.5,
            domain: Vec::new(),
            related_files: Vec::new(),
            source_agent: AgentId::new("anonymous"),
            source_task: None,
        }
    }
}

// ============================================================================
// ExperienceUpdate — Partial update for mutable fields
// ============================================================================

/// Partial update for an experience's mutable fields.
///
/// Only fields set to `Some(...)` will be updated. Content and embedding
/// are immutable — create a new experience if content changes.
#[derive(Clone, Debug, Default)]
pub struct ExperienceUpdate {
    /// New importance score (0.0–1.0).
    pub importance: Option<f32>,

    /// New confidence score (0.0–1.0).
    pub confidence: Option<f32>,

    /// Replace domain tags entirely.
    pub domain: Option<Vec<String>>,

    /// Replace related files entirely.
    pub related_files: Option<Vec<String>>,

    /// Set archived status (used internally by archive/unarchive).
    pub archived: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;

    // ====================================================================
    // Severity tests
    // ====================================================================

    #[test]
    fn test_severity_postcard_roundtrip() {
        for severity in [
            Severity::Low,
            Severity::Medium,
            Severity::High,
            Severity::Critical,
        ] {
            let bytes = postcard::to_stdvec(&severity).unwrap();
            let restored: Severity = postcard::from_bytes(&bytes).unwrap();
            assert_eq!(severity, restored);
        }
    }

    // ====================================================================
    // ExperienceType tests
    // ====================================================================

    #[test]
    fn test_experience_type_default() {
        let et = ExperienceType::default();
        assert!(matches!(et, ExperienceType::Generic { category: None }));
    }

    #[test]
    fn test_experience_type_tag_mapping() {
        let cases: Vec<(ExperienceType, ExperienceTypeTag)> = vec![
            (
                ExperienceType::Difficulty {
                    description: "test".into(),
                    severity: Severity::High,
                },
                ExperienceTypeTag::Difficulty,
            ),
            (
                ExperienceType::Solution {
                    problem_ref: None,
                    approach: "test".into(),
                    worked: true,
                },
                ExperienceTypeTag::Solution,
            ),
            (
                ExperienceType::ErrorPattern {
                    signature: "test".into(),
                    fix: "test".into(),
                    prevention: "test".into(),
                },
                ExperienceTypeTag::ErrorPattern,
            ),
            (
                ExperienceType::SuccessPattern {
                    task_type: "test".into(),
                    approach: "test".into(),
                    quality: 0.9,
                },
                ExperienceTypeTag::SuccessPattern,
            ),
            (
                ExperienceType::UserPreference {
                    category: "test".into(),
                    preference: "test".into(),
                    strength: 0.8,
                },
                ExperienceTypeTag::UserPreference,
            ),
            (
                ExperienceType::ArchitecturalDecision {
                    decision: "test".into(),
                    rationale: "test".into(),
                },
                ExperienceTypeTag::ArchitecturalDecision,
            ),
            (
                ExperienceType::TechInsight {
                    technology: "test".into(),
                    insight: "test".into(),
                },
                ExperienceTypeTag::TechInsight,
            ),
            (
                ExperienceType::Fact {
                    statement: "test".into(),
                    source: "test".into(),
                },
                ExperienceTypeTag::Fact,
            ),
            (
                ExperienceType::Generic {
                    category: Some("test".into()),
                },
                ExperienceTypeTag::Generic,
            ),
        ];

        for (experience_type, expected_tag) in cases {
            assert_eq!(
                experience_type.type_tag(),
                expected_tag,
                "Tag mismatch for {:?}",
                experience_type,
            );
        }
    }

    #[test]
    fn test_experience_type_postcard_roundtrip_all_variants() {
        let variants = vec![
            ExperienceType::Difficulty {
                description: "compile error".into(),
                severity: Severity::High,
            },
            ExperienceType::Solution {
                problem_ref: Some(ExperienceId::new()),
                approach: "added lifetime annotation".into(),
                worked: true,
            },
            ExperienceType::ErrorPattern {
                signature: "E0308 mismatched types".into(),
                fix: "check return type".into(),
                prevention: "use clippy".into(),
            },
            ExperienceType::SuccessPattern {
                task_type: "refactoring".into(),
                approach: "extract method".into(),
                quality: 0.95,
            },
            ExperienceType::UserPreference {
                category: "style".into(),
                preference: "snake_case".into(),
                strength: 0.9,
            },
            ExperienceType::ArchitecturalDecision {
                decision: "use redb over SQLite".into(),
                rationale: "pure Rust, ACID, no FFI".into(),
            },
            ExperienceType::TechInsight {
                technology: "tokio".into(),
                insight: "spawn_blocking for CPU-bound work".into(),
            },
            ExperienceType::Fact {
                statement: "redb uses shadow paging".into(),
                source: "redb docs".into(),
            },
            ExperienceType::Generic { category: None },
        ];

        for variant in variants {
            let bytes = postcard::to_stdvec(&variant).unwrap();
            let restored: ExperienceType = postcard::from_bytes(&bytes).unwrap();
            // Compare tags as a proxy (associated data is different types per variant)
            assert_eq!(variant.type_tag(), restored.type_tag());
        }
    }

    // ====================================================================
    // Experience tests
    // ====================================================================

    #[test]
    fn test_experience_postcard_roundtrip() {
        let timestamp = Timestamp::now();
        let exp = Experience {
            id: ExperienceId::new(),
            collective_id: CollectiveId::new(),
            content: "Test experience content".into(),
            embedding: vec![0.1, 0.2, 0.3], // will be skipped by serde
            experience_type: ExperienceType::Fact {
                statement: "Rust is memory-safe".into(),
                source: "docs".into(),
            },
            importance: 0.8,
            confidence: 0.9,
            applications: BTreeMap::from([(InstanceId::new(), 5)]),
            domain: vec!["rust".into(), "safety".into()],
            related_files: vec!["src/main.rs".into()],
            source_agent: AgentId::new("agent-1"),
            source_task: Some(TaskId::new("task-42")),
            timestamp,
            last_reinforced: timestamp,
            archived: false,
        };

        let bytes = postcard::to_stdvec(&exp).unwrap();
        let restored: Experience = postcard::from_bytes(&bytes).unwrap();

        assert_eq!(exp.id, restored.id);
        assert_eq!(exp.collective_id, restored.collective_id);
        assert_eq!(exp.content, restored.content);
        // Embedding is skipped — restored should be empty
        assert!(restored.embedding.is_empty());
        assert_eq!(
            exp.experience_type.type_tag(),
            restored.experience_type.type_tag()
        );
        assert_eq!(exp.importance, restored.importance);
        assert_eq!(exp.confidence, restored.confidence);
        assert_eq!(exp.applications, restored.applications);
        assert_eq!(exp.applications(), restored.applications());
        assert_eq!(exp.domain, restored.domain);
        assert_eq!(exp.related_files, restored.related_files);
        assert_eq!(exp.source_agent, restored.source_agent);
        assert_eq!(exp.source_task, restored.source_task);
        assert_eq!(exp.timestamp, restored.timestamp);
        assert_eq!(exp.archived, restored.archived);
    }

    #[test]
    fn test_experience_embedding_skipped_in_serialization() {
        let timestamp = Timestamp::now();
        let exp = Experience {
            id: ExperienceId::new(),
            collective_id: CollectiveId::new(),
            content: "test".into(),
            embedding: vec![1.0; 384], // 384 floats = 1,536 bytes
            experience_type: ExperienceType::default(),
            importance: 0.5,
            confidence: 0.5,
            applications: BTreeMap::new(),
            domain: vec![],
            related_files: vec![],
            source_agent: AgentId::new("a"),
            source_task: None,
            timestamp,
            last_reinforced: timestamp,
            archived: false,
        };

        let bytes = postcard::to_stdvec(&exp).unwrap();
        // If embedding were included, size would be > 1,536 bytes.
        // With skip, it should be much smaller.
        assert!(
            bytes.len() < 500,
            "Serialized size {} suggests embedding was not skipped",
            bytes.len()
        );
    }

    // ====================================================================
    // NewExperience tests
    // ====================================================================

    #[test]
    fn test_new_experience_default() {
        let ne = NewExperience::default();
        assert_eq!(ne.collective_id, CollectiveId::nil());
        assert!(ne.content.is_empty());
        assert!(matches!(
            ne.experience_type,
            ExperienceType::Generic { category: None }
        ));
        assert!(ne.embedding.is_none());
        assert_eq!(ne.importance, 0.5);
        assert_eq!(ne.confidence, 0.5);
        assert!(ne.domain.is_empty());
        assert!(ne.related_files.is_empty());
        assert_eq!(ne.source_agent.as_str(), "anonymous");
        assert!(ne.source_task.is_none());
    }

    // ====================================================================
    // ExperienceUpdate tests
    // ====================================================================

    #[test]
    fn test_experience_update_default() {
        let update = ExperienceUpdate::default();
        assert!(update.importance.is_none());
        assert!(update.confidence.is_none());
        assert!(update.domain.is_none());
        assert!(update.related_files.is_none());
        assert!(update.archived.is_none());
    }
}