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
//! Golden round-trip tests for the vendored decode-only bincode reader.
//!
//! # Oracle provenance — regen procedure (load-bearing; read before editing)
//!
//! These `*_GOLDEN` constants are **frozen** bincode-1.3 `DefaultOptions` bytes,
//! produced **once** by a throwaway generator and pasted in here. This module (and
//! the parent `legacy_bincode.rs`) carries **no call into the bincode crate**, so
//! it survives the VS-4.0.3/1.06 bincode-crate drop and proves the
//! "no maintained-crate dependency" property. The oracle is therefore *recorded*,
//! never *recomputed*. (Prose is kept call-token-free so it stays grep-clean.)
//!
//! ## How these bytes were generated (to regenerate, reproduce exactly)
//!
//! 1. Add a temporary `examples/oracle_gen.rs` (a *scratch* generator) that
//!    imports the real production types from the crate root and calls the bincode
//!    crate's `serialize(&value)` entry point on each fixture value.
//!    - `bincode` is still in `[dependencies]` at the 1.01 stage (it drops in
//!      1.06), so no extra dep is needed; if regenerating *after* 1.06, add a
//!      **temporary scratch** `bincode = "1.3"` dep and remove it before commit.
//! 2. **Exact config (critical):** the bincode crate's top-level `serialize` uses
//!    **`DefaultOptions`** — fixed-width little-endian integers (fixint). This is
//!    **NOT** the same wire format as the crate's `config::legacy()` /
//!    `options().with_varint_encoding()` (varint), which would differ on the wire
//!    and silently corrupt the oracle. The vendored decoder reproduces
//!    `DefaultOptions` (fixint LE), so the generator MUST use the plain top-level
//!    `serialize` / `deserialize` functions (which are `DefaultOptions`).
//! 3. Run `cargo run --example oracle_gen --features sync`, copy the printed
//!    `&[u8]` literals here, then **delete `examples/oracle_gen.rs`** (its
//!    serialize calls must not survive into committed source — 1.04 reuses these
//!    FROZEN constants, never the live serializer).
//!
//! ## Hand-verification (catches a systematically-wrong generator)
//!
//! [`COLLECTIVE_GOLDEN`] is verified byte-by-byte in the comment below, so a
//! mis-configured generator (e.g. accidental varint) would not silently certify
//! itself: the hand-decode is independent of the generator.
//!
//! ## Real v0.5.1 sample
//!
//! Status: deferred to VS-4.0.4 (per spec §3 — "only if genuinely infeasible does
//! the real-file proof fall fully to VS-4.0.4"). A real on-disk v0.5.1 store was
//! not available in this work item's worktree; VS-4.0.4 pins the real-fixture
//! golden against `legacy_bincode::decode`. The synthetic fixtures here exercise
//! the full data-model subset and are reused by 1.04 (`pub(crate)` below).
//!
//! ## Reuse by 1.04 (audit C2)
//!
//! The constants are `pub(crate)` so VS-4.0.3/1.04's `{v3, bincode}` migration
//! fixture decodes the *same* legacy bytes via `legacy_bincode::decode` rather
//! than re-freezing them (and so 1.04 never re-invokes the bincode serializer).

use std::collections::BTreeMap;

use super::{decode, LegacyBincodeError};
use crate::collective::Collective;
use crate::experience::{Experience, ExperienceType, Severity};
use crate::insight::{DerivedInsight, InsightType};
use crate::relation::{ExperienceRelation, RelationType};
use crate::storage::schema::{ExperienceV2, WatchEventRecord, WatchEventRecordV1};
use crate::types::{CollectiveId, ExperienceId, InstanceId, Timestamp};

// ============================================================================
// Frozen oracle byte constants (pub(crate) for 1.04 reuse — audit C2)
// ============================================================================

// --- Collective: id [0x11;16], name "demo", owner Some("owner"), dim 384, created 1000, updated 2000 ---
//
// HAND-VERIFIED byte-by-byte (the independent oracle check):
//   bytes  0..24 : id = CollectiveId(Uuid) -> serialize_bytes -> u64 len=16
//                   (10 00 00 00 00 00 00 00) + 16 * 0x11   == 24-BYTE UUID (the gotcha)
//   bytes 24..36 : name "demo" -> u64 len=4 (04 00..) + 64 65 6d 6f
//   bytes 36..50 : owner Some -> Option tag 01, then u64 len=5 (05 00..) + 6f 77 6e 65 72 ("owner")
//   bytes 50..52 : embedding_dimension u16 = 384 -> 80 01  (little-endian 0x0180)
//   bytes 52..60 : created_at Timestamp(i64 1000) -> e8 03 00 00 00 00 00 00
//   bytes 60..68 : updated_at Timestamp(i64 2000) -> d0 07 00 00 00 00 00 00
//   total = 68 bytes.
pub(crate) const COLLECTIVE_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x64, 0x65, 0x6d, 0x6f, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6f, 0x77, 0x6e,
    0x65, 0x72, 0x80, 0x01, 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0, 0x07, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
];

pub(crate) const COLLECTIVE_NO_OWNER_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x64, 0x65, 0x6d, 0x6f, 0x00, 0x80, 0x01, 0xe8, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd0,
    0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

// Experience (schema v3): Fact variant, multi-entry G-counter applications, Some task.
pub(crate) const EXPERIENCE_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21,
    0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x00, 0x00,
    0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72, 0x75, 0x73, 0x74, 0x04, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x6f, 0x63, 0x73, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x80,
    0x3e, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
    0x01, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02,
    0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07, 0x00, 0x00,
    0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x61, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x62, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x67, 0x65, 0x6e,
    0x74, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x61, 0x73, 0x6b, 0x6f, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xde, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

// Adversarial Experience: NaN importance, empty applications/domain, None task,
// Generic{None}, negative timestamp, archived = true.
pub(crate) const EXPERIENCE_ADVERSARIAL_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0,
    0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
];

// Difficulty variant (nested Severity::Critical enum, index 3).
pub(crate) const EXPERIENCE_DIFFICULTY_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x62, 0x6f, 0x6f, 0x6d, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x7f,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
];

// Solution variant: Option<ExperienceId> = Some, bool worked = true.
pub(crate) const EXPERIENCE_SOLUTION_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x10, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
    0x33, 0x33, 0x33, 0x33, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x69, 0x78,
    0x01, 0x00, 0x00, 0xc0, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
];

pub(crate) const RELATION_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42,
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43,
    0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x03, 0x00, 0x00, 0x00, 0x9a, 0x99, 0x59, 0x3f,
    0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7b, 0x7d, 0xe7, 0x03, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00,
];

pub(crate) const INSIGHT_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51,
    0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x6e, 0x73, 0x69, 0x67, 0x68, 0x74, 0x02,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0xbf, 0x02,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61,
    0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x10,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62,
    0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x03, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x3f, 0x01,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78,
    0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

pub(crate) const F32_NAN_GOLDEN: &[u8] = &[0x00, 0x00, 0xc0, 0x7f];

// ============================================================================
// Happy-path decode tests (12-target coverage)
// ============================================================================

#[test]
fn decode_collective_happy() {
    let c: Collective = decode(COLLECTIVE_GOLDEN).expect("collective decodes");
    assert_eq!(c.id, CollectiveId::from_bytes([0x11; 16]));
    assert_eq!(c.name, "demo");
    assert_eq!(c.owner_id.as_deref(), Some("owner"));
    assert_eq!(c.embedding_dimension, 384);
    assert_eq!(c.created_at, Timestamp::from_millis(1000));
    assert_eq!(c.updated_at, Timestamp::from_millis(2000));
}

#[test]
fn decode_collective_none_option() {
    let c: Collective = decode(COLLECTIVE_NO_OWNER_GOLDEN).expect("collective decodes");
    assert!(c.owner_id.is_none(), "Option None must decode from a 0 tag");
    assert_eq!(c.embedding_dimension, 384);
}

#[test]
fn decode_experience_gcounter_multi_entry() {
    let e: Experience = decode(EXPERIENCE_GOLDEN).expect("experience decodes");
    assert_eq!(e.id, ExperienceId::from_bytes([0x21; 16]));
    assert_eq!(e.collective_id, CollectiveId::from_bytes([0x11; 16]));
    assert_eq!(e.content, "hello");
    // #[serde(skip)] embedding must be empty (consumes no wire bytes).
    assert!(e.embedding.is_empty());
    assert!(matches!(e.experience_type, ExperienceType::Fact { .. }));
    assert_eq!(e.importance, 0.5);
    assert_eq!(e.confidence, 0.25);
    // Multi-entry BTreeMap<InstanceId,u32> with 24-byte-UUID keys.
    let mut expected: BTreeMap<InstanceId, u32> = BTreeMap::new();
    expected.insert(InstanceId::from_bytes([0x01; 16]), 3);
    expected.insert(InstanceId::from_bytes([0x02; 16]), 7);
    assert_eq!(e.applications, expected);
    assert_eq!(e.applications(), 10);
    assert_eq!(e.domain, vec!["a".to_string(), "bb".to_string()]);
    assert!(e.related_files.is_empty());
    assert_eq!(e.source_agent.as_str(), "agent");
    assert_eq!(e.source_task.as_ref().map(|t| t.as_str()), Some("task"));
    assert_eq!(e.timestamp, Timestamp::from_millis(111));
    assert_eq!(e.last_reinforced, Timestamp::from_millis(222));
    assert!(!e.archived);
}

#[test]
fn decode_experience_adversarial_nan_empty_none() {
    let e: Experience = decode(EXPERIENCE_ADVERSARIAL_GOLDEN).expect("adversarial decodes");
    assert!(e.importance.is_nan(), "f32 NaN must round-trip");
    assert!(e.applications.is_empty());
    assert!(e.domain.is_empty());
    assert!(e.source_task.is_none());
    assert!(matches!(
        e.experience_type,
        ExperienceType::Generic { category: None }
    ));
    assert_eq!(e.timestamp, Timestamp::from_millis(-1));
    assert!(e.archived);
}

#[test]
fn decode_experience_difficulty_nested_enum() {
    let e: Experience = decode(EXPERIENCE_DIFFICULTY_GOLDEN).expect("difficulty decodes");
    match e.experience_type {
        ExperienceType::Difficulty {
            description,
            severity,
        } => {
            assert_eq!(description, "boom");
            assert_eq!(severity, Severity::Critical);
        }
        other => panic!("expected Difficulty, got {other:?}"),
    }
}

#[test]
fn decode_experience_solution_some_ref_and_bool() {
    let e: Experience = decode(EXPERIENCE_SOLUTION_GOLDEN).expect("solution decodes");
    match e.experience_type {
        ExperienceType::Solution {
            problem_ref,
            approach,
            worked,
        } => {
            assert_eq!(problem_ref, Some(ExperienceId::from_bytes([0x33; 16])));
            assert_eq!(approach, "fix");
            assert!(worked);
        }
        other => panic!("expected Solution, got {other:?}"),
    }
}

#[test]
fn decode_relation_enum_u32_tag() {
    let r: ExperienceRelation = decode(RELATION_GOLDEN).expect("relation decodes");
    // Supersedes is the 4th variant (index 3) — proves the u32 enum tag width.
    assert_eq!(r.relation_type, RelationType::Supersedes);
    assert_eq!(r.strength, 0.85);
    assert_eq!(r.metadata.as_deref(), Some("{}"));
    assert_eq!(r.created_at, Timestamp::from_millis(999));
}

#[test]
fn decode_insight_inline_vecs() {
    let i: DerivedInsight = decode(INSIGHT_GOLDEN).expect("insight decodes");
    assert_eq!(i.content, "insight");
    // Inline Vec<f32> embedding (NOT skipped on insights).
    assert_eq!(i.embedding, vec![0.5f32, -0.5f32]);
    assert_eq!(i.source_experience_ids.len(), 2);
    assert_eq!(
        i.source_experience_ids[0],
        ExperienceId::from_bytes([0x61; 16])
    );
    assert_eq!(i.insight_type, InsightType::Correlation);
    assert_eq!(i.confidence, 0.9);
}

// ============================================================================
// Legacy-shape decode tests (the three legacy structs the migration reads)
// ============================================================================

#[test]
fn decode_experience_v2_scalar_applications() {
    // ExperienceV2 has a SCALAR u32 `applications` (not a BTreeMap G-counter) and
    // NO `last_reinforced` field. EXPERIENCE_V2_GOLDEN was certified against the
    // real bincode crate (see regen procedure) via an ExperienceV2 field-mirror.
    let v2: ExperienceV2 = decode(EXPERIENCE_V2_GOLDEN).expect("ExperienceV2 decodes");
    assert_eq!(v2.content, "v2");
    assert_eq!(v2.applications, 42u32, "scalar v2 application count");
    assert_eq!(v2.importance, 0.5);
    assert!(matches!(v2.experience_type, ExperienceType::Generic { .. }));
}

#[test]
fn decode_watch_event_record_v2_has_entity_type() {
    let w: WatchEventRecord = decode(WATCH_EVENT_GOLDEN).expect("WatchEventRecord decodes");
    assert_eq!(w.entity_id, [0x71; 16]);
    assert_eq!(w.collective_id, [0x72; 16]);
    assert_eq!(w.timestamp_ms, 12345i64);
}

#[test]
fn decode_watch_event_record_v1_without_entity_type() {
    // V1 has NO `entity_type` field — the trailing 4 bytes of the v2 record are
    // absent. Decoding the v1 bytes as V1 succeeds; the v2 layout would EOF.
    let w: WatchEventRecordV1 = decode(WATCH_EVENT_V1_GOLDEN).expect("WatchEventRecordV1 decodes");
    assert_eq!(w.experience_id, [0x71; 16]);
    assert_eq!(w.collective_id, [0x72; 16]);
    assert_eq!(w.timestamp_ms, 12345i64);
}

// ============================================================================
// Scalar / primitive sanity
// ============================================================================

#[test]
fn decode_f32_nan_standalone() {
    let v: f32 = decode(F32_NAN_GOLDEN).expect("f32 decodes");
    assert!(v.is_nan());
}

// ============================================================================
// Error-path tests — typed Err, never panic, never read past end
// ============================================================================

#[test]
fn decode_rejects_trailing_bytes() {
    let mut bytes = COLLECTIVE_GOLDEN.to_vec();
    bytes.push(0xFF); // one stray byte after a complete value
    let err = decode::<Collective>(&bytes).expect_err("trailing byte must error");
    assert_eq!(err, LegacyBincodeError::TrailingBytes);
}

#[test]
fn decode_rejects_truncated_buffer() {
    // Drop the last 4 bytes — mid-field truncation must yield Eof, not panic.
    let truncated = &COLLECTIVE_GOLDEN[..COLLECTIVE_GOLDEN.len() - 4];
    let err = decode::<Collective>(truncated).expect_err("truncation must error");
    assert_eq!(err, LegacyBincodeError::Eof);
}

#[test]
fn decode_rejects_oversized_length_prefix() {
    // A length prefix claiming more bytes than exist must error (no huge alloc).
    let bytes: &[u8] = &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00];
    let err = decode::<String>(bytes).expect_err("oversized length must error");
    assert_eq!(err, LegacyBincodeError::LengthOverflow);
}

#[test]
fn decode_rejects_invalid_enum_index() {
    // RelationType has 6 variants (0..=5); index 99 is invalid.
    let bytes: &[u8] = &[99, 0, 0, 0];
    let err = decode::<RelationType>(bytes).expect_err("invalid variant must error");
    // serde's derived Visitor rejects the out-of-range u32 -> Message error.
    assert!(matches!(err, LegacyBincodeError::Message(_)));
}

#[test]
fn decode_empty_buffer_errors() {
    let err = decode::<Collective>(&[]).expect_err("empty buffer must error");
    assert_eq!(err, LegacyBincodeError::Eof);
}

// ============================================================================
// Additional frozen constants for the legacy-shape + watch tests
// ============================================================================

// ExperienceV2: id [0;16], coll [0;16], content "v2", Generic{None},
// importance 0.5, confidence 0.0, applications(scalar u32)=42, domain[], related[],
// source_agent "", source_task None, timestamp 0, archived false.
pub(crate) const EXPERIENCE_V2_GOLDEN: &[u8] = &[
    0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x32, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];

// WatchEventRecord (v2): entity_id [0x71;16] as [u8;16] (FIXED ARRAY, NOT Uuid ->
// serialized as a tuple of 16 u8 = 16 bytes, NO length prefix), collective_id
// [0x72;16], event_type Updated (index 1 -> u32), timestamp_ms i64=12345,
// entity_type Experience (index 0 -> u32).
pub(crate) const WATCH_EVENT_GOLDEN: &[u8] = &[
    // entity_id: [u8;16] fixed array -> 16 raw bytes, no len prefix
    0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71,
    // collective_id: [u8;16]
    0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
    // event_type Updated = variant index 1 (u32)
    0x01, 0x00, 0x00, 0x00, // timestamp_ms i64 = 12345 (0x3039)
    0x39, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // entity_type Experience = index 0 (u32)
    0x00, 0x00, 0x00, 0x00,
];

// WatchEventRecordV1: same as v2 WITHOUT the trailing entity_type u32.
pub(crate) const WATCH_EVENT_V1_GOLDEN: &[u8] = &[
    0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71, 0x71,
    0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
    0x01, 0x00, 0x00, 0x00, 0x39, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];