Repository navigation
Description
Problem
Archive writes and schema side effects are split across multiple paths not governed by one contract. Every successful write must guarantee FTS repair, trigger restoration, cache invalidation, and readiness updates as one auditable post-write discipline.
Deep audit (2026-05-03) found concrete divergences between the sync and async write paths, plus architectural gaps.
Sync/async WHERE clause divergences
- sort_key missing from async conversation UPSERT (
conversations_writes.py:53-61— 8 WHERE conditions but no sort_key check;ingest_batch.py:186has it) - sort_key missing from async message UPSERT (
message_query_writes.py:67-91— same gap;ingest_batch.py:212has it) - content_blocks DELETE missing from async save path (
ingest_batch.py:475has explicit DELETE before repopulate;repository/archive/writes/conversations.py:192only uses ON CONFLICT DO UPDATE) - _invalidate_embedding_state_sync missing vec-guard (
conversation_replacement.py:49skips the sqlite-vec extension check that:67has)
Root cause: SQL statements defined separately per path. Fix: single-source SQL templates (#784).
Content hash divergence
pipeline/ids.py:190 (canonical) and repository/archive/writes/conversations.py:53 (bypass) compute content hashes differently in 5 ways: NFC normalization, content_blocks inclusion, attachment digest, timestamp formatting, and input model. Same logical conversation → different hashes. Fix: delete the bypass, route all hash computation through pipeline/ids.py.
Ingest side-effects after commit
ingest_batch.py:869-898: _commit_sync_ingest_side_effects runs AFTER conn.commit(). If FTS repair/trigger restoration fails, data is committed but FTS is broken, triggers suspended, cache stale. The conn.rollback() at line 895 is a no-op (already committed).
Missing ON CONFLICT
publications.py:63— plain INSERT, crashes on publication_id collisionruns.py:52— plain INSERT, crashes on run_id collision
FK gaps
conversations.raw_id REFERENCES raw_conversations(raw_id)— no ON DELETE action. Dangling reference on raw deletion.messages.parent_message_id REFERENCES messages(message_id)— no ON DELETE. Orphaned children on direct parent deletion.attachment_refsuses INSERT OR IGNORE — silently swallows FK violations instead of surfacing them.
Dead write gateway
archive/write_gateway.py:43 — ArchiveWriteGateway class is fully implemented but never instantiated. Only its WriteOperation enum is used. The daemon RPC stub (_delegate_to_daemon, line 92) always raises DaemonUnavailableError. Commit 1bda01e partially addressed daemon convergence but the write gateway remains dead.
2,850 lines of parallel sync/async code
12 files maintain dual _sync/_async implementations. ~2,850 lines (~50% of those files). Two additional divergences beyond the WHERE gaps: rebuild_fts_index_async supports targeted rebuild + progress callbacks (sync doesn't), and rebuild_action_event_read_model_async has progress callbacks (sync doesn't). Two async variants are dead (suspend_fts_triggers_async/restore_fts_triggers_async — no real async caller).
Acceptance criteria
- All 4 sync/async WHERE divergences resolved
- Content hash single-sourced through
pipeline/ids.py; bypass deleted - Post-commit side effects moved inside transaction or made atomic
- ON CONFLICT added to publications and runs inserts
- FK ON DELETE actions added or code-level cleanup implemented
-
INSERT OR IGNOREon attachment_refs replaced with explicit FK validation - Dead
ArchiveWriteGatewayeither wired or deleted - FTS trigger/table SQL single-sourced (see also refactor(storage): single-source SQL templates — fix 5 sync/async WHERE-clause divergences #784)
- Exact FTS row-count readiness moved off hot search paths
- Legacy session-product payload support removed from hot paths
Activity
Sinity commentedon May 3, 2026
Scope clarification
Sinity commentedon May 3, 2026
Concrete findings from deep audit (2026-05-03)
Divergent UPSERT WHERE clauses
The async write path (
conversations_writes.py:42-62) is missingOR IFNULL(sort_key, 0) != IFNULL(excluded.sort_key, 0)in the WHERE guard. The sync path (ingest_batch.py:186) has it. Same divergence inmessage_query_writes.py:67-91vsingest_batch.py:212. Result: sort_key changes silently dropped in async writes.Content hash divergence
pipeline/ids.py:190(canonical) andrepository/archive/writes/conversations.py:53(bypass) diverge in 5 ways: NFC normalization, content_blocks inclusion, attachment digest, timestamp formatting, input model. Same logical conversation produces different hashes.Ingest side-effects after commit
ingest_batch.py:869-898—_commit_sync_ingest_side_effectsruns AFTERconn.commit(). If it fails, data is committed but FTS repair, trigger restoration, and cache invalidation never happen. The subsequentconn.rollback()at line 895 is a no-op (already committed).Missing ON CONFLICT
publications.py:63andruns.py:52use plain INSERT — crash on ID collision.FK gaps
conversations.raw_idandmessages.parent_message_idboth missing ON DELETE actions.attachment_refsuses INSERT OR IGNORE which silently swallows FK violations.See
.agent/scratch/deep-audit-2026-05-03/PHASE_3_SYNTHESIS.mdfor full details.refactor: SQL single-sourcing, CLI dispatch, daemon http rewrite (#784,
Sinity commentedon May 4, 2026
Resolved by PRs #787 (content hash single-sourced, FK ON DELETE, ON CONFLICT), #788 (write-gateway affects).
refactor: SQL single-sourcing, CLI dispatch, daemon http rewrite (#784,