Repository navigation
Description
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)
sort_keymissing from async conversation UPSERT WHERE (conversations_writes.py:53vsingest_batch.py:186)sort_keymissing from async message UPSERT WHERE (message_query_writes.py:80vsingest_batch.py:212)_invalidate_embedding_state_syncmissing sqlite-vec guard that async has (conversation_replacement.py:49vs:67)- content_blocks DELETE missing from async save path (
ingest_batch.py:475has it;repository/archive/writes/conversations.py:192doesn't) - content_hash computed differently between sync and async (
pipeline/ids.py:190vsrepository/archive/writes/conversations.py:53)
Duplicated utility functions (6 families)
_make_ref_id()— two implementations produce different IDs (16-char vs 32-char hex, with/without prefix)normalize_role()— canonical version maps "model"→"assistant"; surfaces copy returns raw str()chunked()— character-for-character identical in 3 modulesiter_conversation_id_pages— same name, different SQL in 2 modules_topo_sort_message_tuplesvstopo_sort_messages— same algorithm on different typesreplace_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.pyis 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
refactor: SQL single-sourcing, CLI dispatch, daemon http rewrite (#784,
refactor: SQL dedup — import from core/common, remove 138 lines of du…
Sinity commentedon May 4, 2026
Resolved by PRs #787 (core/common.py with canonical SQL), #798 (imports from core/common in _models.py, linter exits 0).
refactor: SQL single-sourcing, CLI dispatch, daemon http rewrite (#784,
refactor: SQL dedup — import from core/common, remove 138 lines of du…