# serialization-duplication

## AUDIT (2026-01-31)

Pattern: Duplicated rkyv serialization/deserialization boilerplate across crates

```rust
// ANTI-PATTERN: This 4-line pattern appears 7 times
let mut serializer = AllocSerializer::<4096>::default();
serializer.serialize_value(&value).map_err(...)?;
let bytes = serializer.into_serializer().into_inner();
```

Found: **7 production instances** in **4 files**

| File | Line | Function | Type |
|------|------|----------|------|
| stemedb-ingest/src/worker.rs | 61 | serialize_assertion | Serialize |
| stemedb-ingest/src/worker.rs | 75 | serialize_vote | Serialize |
| stemedb-ingest/src/worker.rs | 88 | serialize_epoch | Serialize |
| stemedb-storage/src/vote_store.rs | 155 | serialize_vote | Serialize |
| stemedb-storage/src/vote_store.rs | 162 | deserialize_vote | Deserialize |
| stemedb-query/src/engine.rs | 140 | deserialize_assertion | Deserialize |
| stemedb-lens/src/vote_aware_consensus.rs | 100 | compute_assertion_hash | Serialize |

**Excluded (test code):**
- stemedb-core/src/lib.rs (all in #[cfg(test)])
- stemedb-query/src/engine.rs:184 (test helper)
- stemedb-sim/src/main.rs (demo binary)

## Plan

1. CREATE: Add `stemedb-core/src/serde.rs` with generic serialize/deserialize helpers
2. FIX: Replace all 7 instances with calls to the new helpers
3. VERIFY: grep returns 0 for raw AllocSerializer usage outside tests
4. ENFORCE: Add CLAUDE.md rule prohibiting direct AllocSerializer usage
5. DOCUMENT: Update ai-lookup with the canonical pattern

## FIX Progress
- [x] CREATE utility module - `stemedb-core/src/serde.rs` with `serialize<T>()` and `deserialize<T>()`
- [ ] stemedb-ingest/src/worker.rs <- CURRENT
- [ ] stemedb-storage/src/vote_store.rs
- [ ] stemedb-query/src/engine.rs
- [ ] stemedb-lens/src/vote_aware_consensus.rs

## CREATE (2026-01-31)

Created `crates/stemedb-core/src/serde.rs`:
- `serialize<T>(value: &T) -> Result<Vec<u8>, SerdeError>` - Generic serialization
- `deserialize<T>(data: &[u8]) -> Result<T, SerdeError>` - Generic deserialization
- `SerdeError` enum for error handling
- 5 unit tests + 2 doc tests passing
