---
description: Architecture review of the cnk set-compression crate.
---

Perform an architecture review of the `cnk` crate. Read all source files, then evaluate each section.

## 1. Module layering

Read all source files:
- `src/lib.rs` (root, re-exports, `IdCompressionMethod` enum)
- `src/roc.rs` (delta+varint compressor, named `RocCompressor`)
- `src/auto.rs` (method selection + compress/decompress)
- `src/choose.rs` (heuristic chooser, `CodecChoice`)
- `src/envelope.rs` (self-describing wire format)
- `src/stats.rs` (`IdListStats` for chooser input)
- `src/traits.rs` (`IdSetCompressor` trait)
- `src/error.rs` (`CompressionError` enum)
- `src/ans.rs` (ANS re-exports, feature-gated)
- `src/elias_fano.rs` (Elias-Fano, feature-gated on `sbits`)
- `src/partitioned_elias_fano.rs` (partitioned EF, feature-gated on `sbits`)

Evaluate:
- Is the dependency graph between modules acyclic?
- Are public re-exports in `lib.rs` complete and minimal?
- Is there dead code or unreachable modules?

## 2. Feature gate coherence

Features: `ans`, `sbits`, `full`.

Check:
- Does `--no-default-features` compile and produce a useful crate?
- Does each feature add exactly one capability?
- Are there cross-feature dependencies that should be documented?
- Does `full = ["ans", "sbits"]` actually enable everything?

## 3. Error type audit

Review `CompressionError` variants:
- `InvalidInput` -- used for validation errors
- `CompressionFailed` -- used for compression failures
- `DecompressionFailed` -- used for decompression failures
- `AnsError` -- used for ANS errors
- `Io` -- used for I/O errors

Check:
- Are all variants reachable?
- Is `AnsError` only reachable when `ans` feature is enabled?
- Should `CompressionError` derive `thiserror::Error` instead of manual impl?
  (Note: `thiserror` is already a dependency but not used for `CompressionError`.)

## 4. API surface

Review trait coherence:
- `IdSetCompressor` trait: compress_set, decompress_set, estimate_size, bits_per_id
- Implementors: `RocCompressor`, `EliasFanoCompressor`, `PartitionedEliasFanoCompressor`
- Are all implementors consistent in their error handling?

Review re-export completeness:
- Are all public types re-exported from `lib.rs`?
- Is the ANS re-export surface adequate for downstream bits-back ROC?

## 5. Wire format stability

Review `envelope.rs`:
- Is the V2 format stable and versioned?
- Is V1 backward compatibility maintained?
- Is there a migration path from V1 to V2?
- Is CRC32 sufficient for integrity checking?
- Should the envelope store the `cnk` crate version?

## 6. Dead code audit

Check for:
- `RocCompressor.ans_precision` field (currently `#[allow(dead_code)]`)
- Any unused imports or functions
- Methods that are pub but never called externally

## 7. Naming accuracy

The `RocCompressor` name implies Random Order Coding but the implementation is delta+varint.
- Is this confusing for users?
- Should it be renamed to `DeltaVarintCompressor` with `RocCompressor` reserved for the true bits-back implementation?
- What is the migration plan?

## Summary

Report:
- Module dependency graph (text or ASCII)
- Feature gate correctness
- Error type coverage
- API surface gaps
- Wire format stability assessment
- Dead code findings
- Naming recommendations
