Skip to content

fix(storage): write-path unification — single post-write contract (#618 remaining) #772

Closed

Description

@Sinity

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

  1. sort_key missing from async conversation UPSERT (conversations_writes.py:53-61 — 8 WHERE conditions but no sort_key check; ingest_batch.py:186 has it)
  2. sort_key missing from async message UPSERT (message_query_writes.py:67-91 — same gap; ingest_batch.py:212 has it)
  3. content_blocks DELETE missing from async save path (ingest_batch.py:475 has explicit DELETE before repopulate; repository/archive/writes/conversations.py:192 only uses ON CONFLICT DO UPDATE)
  4. _invalidate_embedding_state_sync missing vec-guard (conversation_replacement.py:49 skips the sqlite-vec extension check that :67 has)

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 collision
  • runs.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_refs uses 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 IGNORE on attachment_refs replaced with explicit FK validation
  • Dead ArchiveWriteGateway either 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

Ref: deep audit B1, B2, B4, B5, P3-5, 3b. Ref #730, #784.

Activity

  1. Sinity commented on May 3, 2026

    @Sinity
    OwnerAuthor

    Scope clarification

    • Daemon owns all ingestion writes — no advisory lock fallback for ingest. The daemon is NOT optional for ingestion.
    • Metadata writes (tags, summaries, annotations) can be direct — lightweight, non-ingestion mutations don't need the daemon.
    • The ArchiveWriteGateway routes: reads always direct SQLite. Ingestion writes MUST go through daemon. Metadata writes are direct with advisory lock.
  2. Sinity commented on May 3, 2026

    @Sinity
    OwnerAuthor

    Concrete findings from deep audit (2026-05-03)

    Divergent UPSERT WHERE clauses

    The async write path (conversations_writes.py:42-62) is missing OR IFNULL(sort_key, 0) != IFNULL(excluded.sort_key, 0) in the WHERE guard. The sync path (ingest_batch.py:186) has it. Same divergence in message_query_writes.py:67-91 vs ingest_batch.py:212. Result: sort_key changes silently dropped in async writes.

    Content hash divergence

    pipeline/ids.py:190 (canonical) and repository/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_effects runs AFTER conn.commit(). If it fails, data is committed but FTS repair, trigger restoration, and cache invalidation never happen. The subsequent conn.rollback() at line 895 is a no-op (already committed).

    Missing ON CONFLICT

    publications.py:63 and runs.py:52 use plain INSERT — crash on ID collision.

    FK gaps

    conversations.raw_id and messages.parent_message_id both missing ON DELETE actions. attachment_refs uses INSERT OR IGNORE which silently swallows FK violations.

    See .agent/scratch/deep-audit-2026-05-03/PHASE_3_SYNTHESIS.md for full details.

  3. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Resolved by PRs #787 (content hash single-sourced, FK ON DELETE, ON CONFLICT), #788 (write-gateway affects).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions