Post-M4 Technical Debt Cleanup — Benchmark Report
Generated: 2026-05-20
Branch: chore/tech-debt-post-m4

================================================================================
GATE RESULTS
================================================================================

  TypeScript (npx tsc --noEmit)    PASS — 0 errors  [noUncheckedIndexedAccess enabled]
  ESLint (npm run lint)            PASS — 0 errors
  Test suite (npm test)            PASS — 157/157 tests, 19 files
  Determinism (3 consecutive runs) PASS — all 3 runs: 157/157 (identical)

================================================================================
CHANGES FROM M4 BASELINE (190 tests / 22 files)
================================================================================

  Removed (dead code):
    - src/reference-detector/__tests__/signals.test.ts           18 tests  (dead)
    - src/reference-detector/__tests__/reference-detector.test.ts 9 tests  (dead)
    - src/reference-detector/__tests__/corpus.test.ts             4 tests  (dead)
    - src/orchestrator/__tests__/post-response-handler.test.ts    7 tests  (dead)
    Total removed: 38 tests across 4 files

  Added (live detector coverage + storage reliability):
    + src/references/__tests__/corpus-synthetic.test.ts           3 tests
    + src/storage/__tests__/storage.test.ts                      +3 tests (markStubs x2, corruption discriminator x1)

  Modified (assertions corrected, no count change):
    src/hooks/__tests__/post-response.test.ts    fail-open assertion fixed (unused_turns stays at seed, not incremented)

  Net: −32 tests (190 → 158 total; 157 passing + 1 skipped)

================================================================================
AUDIT DIMENSION IMPROVEMENTS
================================================================================

  D1 Spec Fidelity:         B+ → A   Dual module resolved; corpus gate now tests live src/references/ code
  D2 Type Safety:           A- → A   noUncheckedIndexedAccess enabled; 9 files guarded
  D3 Test Quality:          B  → A-  Dead tests removed; live detector covered by synthetic corpus gate
  D4 Cross-Milestone:       C  → A-  Single execution path; no split-brain between two reference detectors
  D5 Error Handling:        C+ → A-  DB corruption discriminator; markStubs atomicity; correct fail-open behavior
  D6 Architecture:          C  → A-  Dead code deleted; barrel curated to explicit public API exports
  D7 Security:              A  → A   Unchanged — was already clean
  D8 Hygiene:               B  → A-  Duplication eliminated; TEMP TABLE removed; vocabulary correct throughout

================================================================================
STORAGE RELIABILITY FIXES
================================================================================

  1. Corruption discriminator (src/storage/index.ts)
     isCorruptionError() now checks message for known SQLite corruption signals before
     renaming the database. Non-corruption errors (migration bugs, schema conflicts)
     propagate as thrown exceptions instead of silently destroying session history.

  2. Atomic stub writes (src/pruner/index.ts + src/storage/index.ts)
     Added markStubs() — a transactional batch method. pruneExpiredBlocks() now builds
     the full decision list first (no DB writes), then calls markStubs() atomically.
     Crash mid-prune no longer orphans blocks.

  3. TEMP TABLE → json_each (src/storage/index.ts)
     Replaced shared cachelane_referenced_ids TEMP TABLE with parameterized
     json_each(@ids_json) queries. Eliminates the latent concurrency hazard where
     concurrent updateBlockCounters calls could bleed IDs across sessions.

================================================================================
REFERENCE DETECTOR CONSOLIDATION
================================================================================

  Deleted: src/reference-detector/  (M4 plan's dead module — never on live execution path)
  Deleted: src/orchestrator/post-response-handler.ts  (duplicate handler; hooks version canonical)
  Live detector: src/references/three-signal-detector.ts (Rajan's implementation)
  Corpus gate: src/references/__tests__/corpus-synthetic.test.ts
    - 20 synthetic fixtures (corpus-001..020) adapted via format-adapter
    - Precision: 100% on synthetic baseline
    - Recall:    100% on synthetic baseline
