Skip to content

[Audit] Consolidate duplicated dialect/DB/chain helpers into shared modules #1313

Description

@stranske

Why

The dual-dialect DB layer is implemented by per-module copies of the same helpers instead of shared functions, so every new flow re-implements (and can drift on) dialect handling. Verified copies at e5764a5:

  • _placeholder(conn) / _is_sqlite(conn) (pick ? vs %s, isinstance(conn, sqlite3.Connection)) duplicated across 12 modules: etl/{activism_detection,activism_flow,conviction_flow,daily_diff_flow,digest_flow,edgar_flow,evaluation_flow,ingest_flow,news_flow}.py, api/{signals,chat,activism}.py.
  • _columns / _get_columns (SQLite PRAGMA/pragma_table_info vs PG information_schema.columns) in etl/digest_flow.py:84, etl/edgar_flow.py:62, api/search.py:47.
  • api_usage table DDL in etl/evaluation_flow.py:101, llm/cost_tracking.py:25, adapters/base.py:83/102.
  • Chain helpers: _extract_json_text (chains/filing_summary.py:169, chains/holdings_analysis.py:155), _cursor_rows_to_dicts (same two), _guard_context/_acquire_connection (chains/rag_search.py, chains/nl_query.py), _normalize_cik (api/managers.py:245, scripts/seed_universe.py:23, scripts/resolve_aliases.py:29).

Est. ~210 LOC of duplication; the risk is divergence (a fix applied to one copy and not the others) — the same defect class behind several other audit findings.

Scope

Consolidate the duplicated dialect/DB/chain helpers into shared locations (adapters/base.py for DB; chains/utils.py for chain helpers; a utils normalizer for CIK) and update call sites to import them.

Non-Goals

  • Do NOT change runtime behavior — this is a behavior-preserving consolidation.
  • Do NOT alter the check_dialect_portability.py gate semantics.

Tasks

  • Add is_sqlite(conn), get_placeholder(conn), get_table_columns(conn, table), and public ensure_api_usage_schema(conn) to adapters/base.py; replace the 12+3+3 call sites.
  • Add extract_json_text() and rows_to_dicts() to chains/utils.py; replace the chain copies. Centralize _guard_context/_acquire_connection.
  • Add a shared normalize_cik() and replace the 3 copies.

Acceptance Criteria

  • Existing suite stays green; no behavior change.
  • A grep gate (or test) asserts there is exactly one definition of each consolidated helper (no per-module re-definitions reintroduced).
  • Deliberate-break demonstration: re-adding a local _placeholder copy in any module trips the single-definition gate.

Implementation Notes

Audit baseline: main @ e5764a5 on 2026-06-28. Call sites enumerated by the duplication sweep (Code/Audits/Manager-Database/2026-06-28-02-duplication-quality.md).

Activity

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

Metadata

Metadata

Labels

agent:codexAssign to Codex agentagents:formattedIssue has been formatted to AGENT_ISSUE_TEMPLATEenhancementNew feature or requestpriority:normalNormal-priority weekly repo-review workstatus: readyReady for Orchestrator opener pickuptestingTests or coverage work

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions