Skip to content

refactor(storage): single-source SQL templates — fix 5 sync/async WHERE-clause divergences #784

Closed

Description

@Sinity

Problem

The codebase maintains parallel sync/async implementations of the same SQL statements, and duplicated utility functions across modules. These divergences cause real bugs.

SQL WHERE clause divergences (5 confirmed)

  1. sort_key missing from async conversation UPSERT WHERE (conversations_writes.py:53 vs ingest_batch.py:186)
  2. sort_key missing from async message UPSERT WHERE (message_query_writes.py:80 vs ingest_batch.py:212)
  3. _invalidate_embedding_state_sync missing sqlite-vec guard that async has (conversation_replacement.py:49 vs :67)
  4. content_blocks DELETE missing from async save path (ingest_batch.py:475 has it; repository/archive/writes/conversations.py:192 doesn't)
  5. content_hash computed differently between sync and async (pipeline/ids.py:190 vs repository/archive/writes/conversations.py:53)

Duplicated utility functions (6 families)

  1. _make_ref_id() — two implementations produce different IDs (16-char vs 32-char hex, with/without prefix)
  2. normalize_role() — canonical version maps "model"→"assistant"; surfaces copy returns raw str()
  3. chunked() — character-for-character identical in 3 modules
  4. iter_conversation_id_pages — same name, different SQL in 2 modules
  5. _topo_sort_message_tuples vs topo_sort_messages — same algorithm on different types
  6. replace_action_events — triplicated across 3 modules

Root cause

SQL statements and content hash logic are defined separately per code path. There is no canonical source. Utility functions are copy-pasted rather than shared. Every new feature risks being added to only one path.

Proposed fix

Single SQL definition module: Define each SQL statement exactly once. Both sync and async paths reference the same definitions. This eliminates the entire class of parallel-path divergence bugs — every future SQL change applies to both paths automatically.

Shared utility module (polylogue/core/common.py): Move canonical implementations of duplicated functions to one location. Delete all copies. For functions differing only in data type, make them generic.

Acceptance criteria

  • All conversation/message/content_block UPSERT SQL single-sourced
  • Content hash computation single-sourced (pipeline/ids.py is canonical)
  • FTS trigger management SQL single-sourced
  • All 5 WHERE divergences resolved
  • _make_ref_id() produces consistent IDs everywhere
  • normalize_role() normalized everywhere (delete surfaces copy)
  • chunked() has one implementation
  • Linter verifies no SQL constant duplication across sync/async file pairs
  • Linter verifies no duplicated function bodies across modules

Ref: deep audit R4, B1, B5, P3-2, P3-5. Ref #772. Closed #785 (consolidated here).

Activity

  1. Sinity commented on May 4, 2026

    @Sinity
    OwnerAuthor

    Resolved by PRs #787 (core/common.py with canonical SQL), #798 (imports from core/common in _models.py, linter exits 0).

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