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
//! Core type definitions for PulseDB identifiers and timestamps.
//!
//! This module defines the fundamental ID types used throughout PulseDB.
//! All ID types use UUID v7 for time-ordered unique identification.

use std::fmt;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Collective identifier (UUID v7 for time-ordering).
///
/// Collectives are isolated namespaces for agent experiences, typically one per project.
/// Each collective has its own HNSW index and embedding dimension.
///
/// # Example
/// ```
/// use pulsedb::CollectiveId;
///
/// let id = CollectiveId::new();
/// println!("Created collective: {}", id);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CollectiveId(pub Uuid);

impl CollectiveId {
    /// Creates a new CollectiveId with a UUID v7 (time-ordered).
    #[inline]
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    /// Creates a nil (all zeros) CollectiveId.
    /// Useful for testing or sentinel values.
    #[inline]
    pub fn nil() -> Self {
        Self(Uuid::nil())
    }

    /// Returns the raw UUID bytes for storage.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.0.as_bytes()
    }

    /// Creates a CollectiveId from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(Uuid::from_bytes(bytes))
    }
}

impl Default for CollectiveId {
    /// Returns a nil (all zeros) CollectiveId.
    ///
    /// For a new unique ID, use [`CollectiveId::new()`].
    fn default() -> Self {
        Self::nil()
    }
}

impl fmt::Display for CollectiveId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Experience identifier (UUID v7 for time-ordering).
///
/// Experiences are the core unit of learned knowledge in PulseDB.
/// Each experience belongs to exactly one collective.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ExperienceId(pub Uuid);

impl ExperienceId {
    /// Creates a new ExperienceId with a UUID v7 (time-ordered).
    #[inline]
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    /// Creates a nil (all zeros) ExperienceId.
    #[inline]
    pub fn nil() -> Self {
        Self(Uuid::nil())
    }

    /// Returns the raw UUID bytes for storage.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.0.as_bytes()
    }

    /// Creates an ExperienceId from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(Uuid::from_bytes(bytes))
    }
}

impl Default for ExperienceId {
    /// Returns a nil (all zeros) ExperienceId.
    ///
    /// For a new unique ID, use [`ExperienceId::new()`].
    fn default() -> Self {
        Self::nil()
    }
}

impl fmt::Display for ExperienceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Unique identifier for a PulseDB database instance.
///
/// Each database mints one stable instance id on first open and stores it in
/// metadata. Temporal decay uses this id as the G-counter key for local
/// reinforcement counts; the sync protocol also uses it to identify peers.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct InstanceId(pub Uuid);

impl InstanceId {
    /// Creates a new InstanceId with a UUID v7 (time-ordered).
    #[inline]
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    /// Creates a nil (all zeros) InstanceId.
    ///
    /// Useful for tests and explicitly reserved sentinel values.
    #[inline]
    pub fn nil() -> Self {
        Self(Uuid::nil())
    }

    /// Returns the raw UUID bytes for storage.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.0.as_bytes()
    }

    /// Creates an InstanceId from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(Uuid::from_bytes(bytes))
    }
}

impl Default for InstanceId {
    /// Returns a nil (all zeros) InstanceId.
    ///
    /// For a new unique ID, use [`InstanceId::new()`].
    fn default() -> Self {
        Self::nil()
    }
}

impl fmt::Display for InstanceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Unix timestamp in milliseconds.
///
/// Using i64 allows representing dates far into the future and past.
/// Millisecond precision is sufficient for agent operations.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Timestamp(pub i64);

impl Timestamp {
    /// Creates a timestamp for the current moment.
    ///
    /// If the system clock is before the Unix epoch (should never happen
    /// in practice), returns a timestamp of 0 (epoch) rather than panicking.
    #[inline]
    pub fn now() -> Self {
        use std::time::{SystemTime, UNIX_EPOCH};
        let duration = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default();
        Self(duration.as_millis() as i64)
    }

    /// Creates a timestamp from Unix milliseconds.
    #[inline]
    pub const fn from_millis(millis: i64) -> Self {
        Self(millis)
    }

    /// Returns the timestamp as Unix milliseconds.
    #[inline]
    pub const fn as_millis(&self) -> i64 {
        self.0
    }

    /// Returns big-endian bytes for storage (enables lexicographic ordering).
    #[inline]
    pub fn to_be_bytes(&self) -> [u8; 8] {
        self.0.to_be_bytes()
    }
}

impl fmt::Display for Timestamp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Relation identifier (UUID v7 for time-ordering).
///
/// Relations connect two experiences within the same collective,
/// enabling agents to understand how knowledge connects.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RelationId(pub Uuid);

impl RelationId {
    /// Creates a new RelationId with a UUID v7 (time-ordered).
    #[inline]
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    /// Creates a nil (all zeros) RelationId.
    #[inline]
    pub fn nil() -> Self {
        Self(Uuid::nil())
    }

    /// Returns the raw UUID bytes for storage.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.0.as_bytes()
    }

    /// Creates a RelationId from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(Uuid::from_bytes(bytes))
    }
}

impl Default for RelationId {
    /// Returns a nil (all zeros) RelationId.
    ///
    /// For a new unique ID, use [`RelationId::new()`].
    fn default() -> Self {
        Self::nil()
    }
}

impl fmt::Display for RelationId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Insight identifier (UUID v7 for time-ordering).
///
/// Insights are derived knowledge synthesized from multiple experiences.
/// Each insight belongs to exactly one collective.
///
/// # Example
/// ```
/// use pulsedb::InsightId;
///
/// let id = InsightId::new();
/// println!("Created insight: {}", id);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InsightId(pub Uuid);

impl InsightId {
    /// Creates a new InsightId with a UUID v7 (time-ordered).
    #[inline]
    pub fn new() -> Self {
        Self(Uuid::now_v7())
    }

    /// Creates a nil (all zeros) InsightId.
    #[inline]
    pub fn nil() -> Self {
        Self(Uuid::nil())
    }

    /// Returns the raw UUID bytes for storage.
    #[inline]
    pub fn as_bytes(&self) -> &[u8; 16] {
        self.0.as_bytes()
    }

    /// Creates an InsightId from raw bytes.
    #[inline]
    pub fn from_bytes(bytes: [u8; 16]) -> Self {
        Self(Uuid::from_bytes(bytes))
    }
}

impl Default for InsightId {
    /// Returns a nil (all zeros) InsightId.
    ///
    /// For a new unique ID, use [`InsightId::new()`].
    fn default() -> Self {
        Self::nil()
    }
}

impl fmt::Display for InsightId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Opaque user identifier.
///
/// PulseDB doesn't handle authentication - the consumer provides user IDs.
/// This allows integration with any auth system (OAuth, API keys, etc.).
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct UserId(pub String);

impl UserId {
    /// Creates a new UserId from a string.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the user ID as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for UserId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Agent identifier.
///
/// Identifies a specific AI agent instance within a collective.
/// Multiple agents can operate on the same collective simultaneously.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AgentId(pub String);

impl AgentId {
    /// Creates a new AgentId from a string.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the agent ID as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for AgentId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Task identifier.
///
/// Identifies a specific task or job that an agent is working on.
/// Used for tracking which experiences came from which task.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TaskId(pub String);

impl TaskId {
    /// Creates a new TaskId from a string.
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the task ID as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for TaskId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Embedding vector type alias.
///
/// Embeddings are f32 vectors of fixed dimension (typically 384 or 768).
pub type Embedding = Vec<f32>;

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

    #[test]
    fn test_collective_id_new_is_unique() {
        let id1 = CollectiveId::new();
        let id2 = CollectiveId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_collective_id_nil() {
        let id = CollectiveId::nil();
        assert_eq!(id.0, Uuid::nil());
    }

    #[test]
    fn test_collective_id_bytes_roundtrip() {
        let id = CollectiveId::new();
        let bytes = *id.as_bytes();
        let restored = CollectiveId::from_bytes(bytes);
        assert_eq!(id, restored);
    }

    #[test]
    fn test_collective_id_serialization() {
        let id = CollectiveId::new();
        let bytes = postcard::to_stdvec(&id).unwrap();
        let restored: CollectiveId = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(id, restored);
    }

    #[test]
    fn test_experience_id_new_is_unique() {
        let id1 = ExperienceId::new();
        let id2 = ExperienceId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_experience_id_serialization() {
        let id = ExperienceId::new();
        let bytes = postcard::to_stdvec(&id).unwrap();
        let restored: ExperienceId = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(id, restored);
    }

    #[test]
    fn test_relation_id_new_is_unique() {
        let id1 = RelationId::new();
        let id2 = RelationId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_relation_id_nil() {
        let id = RelationId::nil();
        assert_eq!(id.0, Uuid::nil());
    }

    #[test]
    fn test_relation_id_bytes_roundtrip() {
        let id = RelationId::new();
        let bytes = *id.as_bytes();
        let restored = RelationId::from_bytes(bytes);
        assert_eq!(id, restored);
    }

    #[test]
    fn test_relation_id_serialization() {
        let id = RelationId::new();
        let bytes = postcard::to_stdvec(&id).unwrap();
        let restored: RelationId = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(id, restored);
    }

    #[test]
    fn test_insight_id_new_is_unique() {
        let id1 = InsightId::new();
        let id2 = InsightId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_insight_id_nil() {
        let id = InsightId::nil();
        assert_eq!(id.0, Uuid::nil());
    }

    #[test]
    fn test_insight_id_bytes_roundtrip() {
        let id = InsightId::new();
        let bytes = *id.as_bytes();
        let restored = InsightId::from_bytes(bytes);
        assert_eq!(id, restored);
    }

    #[test]
    fn test_insight_id_serialization() {
        let id = InsightId::new();
        let bytes = postcard::to_stdvec(&id).unwrap();
        let restored: InsightId = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(id, restored);
    }

    #[test]
    fn test_timestamp_now() {
        let t1 = Timestamp::now();
        std::thread::sleep(std::time::Duration::from_millis(1));
        let t2 = Timestamp::now();
        assert!(t1 < t2, "Timestamps should be ordered");
    }

    #[test]
    fn test_timestamp_ordering() {
        let t1 = Timestamp::from_millis(1000);
        let t2 = Timestamp::from_millis(2000);
        assert!(t1 < t2);
    }

    #[test]
    fn test_timestamp_be_bytes() {
        // Big-endian ensures lexicographic ordering matches numeric ordering
        let t1 = Timestamp::from_millis(100);
        let t2 = Timestamp::from_millis(200);
        assert!(t1.to_be_bytes() < t2.to_be_bytes());
    }

    #[test]
    fn test_user_id() {
        let id = UserId::new("user-123");
        assert_eq!(id.as_str(), "user-123");
        assert_eq!(format!("{}", id), "user-123");
    }

    #[test]
    fn test_agent_id() {
        let id = AgentId::new("claude-opus");
        assert_eq!(id.as_str(), "claude-opus");
    }

    #[test]
    fn test_task_id() {
        let id = TaskId::new("task-456");
        assert_eq!(id.as_str(), "task-456");
    }
}