Skip to content

fix(canary): read the configured SQL backend so the invariant harness is not blind on PostgreSQL (#1540) - #1690

Merged
vybe merged 6 commits into
devfrom
AndriiPasternak31/issue-1540
Jul 19, 2026
Merged

vybe merged 6 commits into
devfrom
AndriiPasternak31/issue-1540

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Fixes #1540

Summary

The CANARY-001 invariant harness opened a SQLite connection unconditionally (db.connection.get_db_connection hardcodes sqlite3 and ignores a non-SQLite DATABASE_URL), so on a PostgreSQL-backed deployment its SQL-tier collectors inspected a stale/empty /data/trinity.db instead of the live PG database. _collect_known_agents returned zero rows → snap.agents empty → the whole per-agent loop (S-01/E-05/B-02/E-04/G-04/B-01) plus the fleet-wide reads (E-02/E-03/G-03/E-06/L-03) went vacuously green. The harness was effectively blind to the live database at that tier.

This finishes the #1450 collector-wide migration:

  • 6 SQL-tier collectors → get_engine()/DATABASE_URL seam — _collect_known_agents, _collect_executions, _collect_terminal_executions, _collect_terminal_rows, _collect_enabled_schedules, _collect_orphan_refs now read through the same engine seam B-01 already used, so the whole harness is backend-consistent on PostgreSQL.
  • PRAGMA table_info → sqlalchemy.inspect() — the column-existence probes (older-image skips) are now portable across SQLite and PG instead of SQLite-only DDL introspection.
  • Composite-PK agent_tags handling — the orphan-scan primary key is taken from the static db/tables.py Table definition, not runtime reflection; agent_tags has a composite PK, so it maps to a synthetic row_id rather than assuming a single-column PK.

Verification

  • Canary suite: tests/test_canary_invariants.py → 139 passed, 1 skipped (the 1 skip is the real-Postgres dialect gate, TRINITY_TEST_PG_URL). Re-run at ship stage, still green.
  • Real-PG proof: the dialect path was exercised against a real postgres:16-alpine at both implement and review stages (via the TRINITY_TEST_PG_URL-gated test).
  • verify-local --skip-agent: PASS — build + import-smoke, boot + health, and the integration suite all green.

Notes

  • No schema change — this is a read-path repoint only, so the dual-track migration rule (Invariant Feature/vector log retention #3) does not apply.
  • The sqlite.* sources_unavailable labels are kept verbatim as an internal skip-contract prefix for L-03/E-02, not a backend claim — they are a stable contract string, not a description of the DB in use.

Follow-ups (for Andrii to file)

  • A runtime "silent re-blinding" operator alarm — a canary cycle whose SQL-tier collectors all read zero rows on a fleet that is provably non-empty should surface loudly rather than passing green (defense against a future regression of exactly this bug class).
  • The .claude submodule test-runner catalog entry for the new canary coverage is prepared locally but left uncommitted — the core team lands .claude changes via the private trinity-dev submodule, not through this public-repo PR.

🤖 Generated with Claude Code

AndriiPasternak31 and others added 5 commits July 18, 2026 17:36
…1540)

The canary snapshot collector opened a raw sqlite3 connection via
db.connection.get_db_connection(), which deliberately ignores a non-SQLite
DATABASE_URL. On a PostgreSQL deployment every SQL-tier collector read a
stale/empty /data/trinity.db, so _collect_known_agents() returned zero rows
-> snap.agents empty -> the whole per-agent loop (S-01/E-05/B-02/E-04/G-04
and B-01) plus the fleet-wide reads (E-02/E-03/G-03/E-06/L-03) went vacuously
green. The canary was blind exactly where the pull/work-stealing migration
(#1081) leans on it as the default-ON safety net.

Repoint the six remaining collectors (_collect_known_agents,
_collect_executions, _collect_terminal_executions, _collect_terminal_rows,
_collect_enabled_schedules, _collect_orphan_refs) onto get_engine()/DATABASE_URL
via SQLAlchemy Core, following the shipped _collect_queued_ids_via_engine
template. SQLite-only PRAGMA table_info introspection is replaced with portable
sqlalchemy.inspect(conn) reflection (fail-open on minimal/older DDLs, identical
to before). Orphan-scan PK is derived from the STATIC db/tables.py Table
(primary_key.columns), never reflection, so the composite-PK agent_tags table
does not KeyError. No schema change (read-path repoint only) -> Invariant #3
dual-track migration not triggered. Imports stay lazy inside each collector to
preserve the canary's low-dependency-leaf discipline. The sqlite.* labels in
sources_unavailable are preserved verbatim (a load-bearing skip-contract prefix
for L-03/E-02). db/connection.py is left untouched (sqlite-only maintenance seam).

Co-Authored-By: Claude Fable 5 <[email protected]>
… seam (#1540)

Three test updates required by the collector engine-seam port:

- test_b01_backend_consistency_on_diverged_backend: the whole collector now
  reads the ENGINE file, so put the agent + queued rows there and assert the
  RAW file is ignored (strengthens the #1450 test into a #1540 un-blinding proof:
  a RAW-only ghost agent is invisible; queued_exec_ids reads the engine too).
- test_column_absent_skips_collection: route the minimal DDL through
  DATABASE_URL instead of patching db.connection, so it exercises the
  inspect() column guard (fail-open identical to the old raw PRAGMA guard).
- agent_tags test DDL: fix to the production composite PK (agent_name, tag) with
  no id column, so the composite->synthetic row_id orphan-scan branch is actually
  exercised on the SQLite rail (guards the KeyError('id') a reflected-PK port
  would have shipped).

Co-Authored-By: Claude Fable 5 <[email protected]>
…PK, PG)

New TestIssue1540EngineSeam class:
- Un-blinding gate (split fixture): agents/rows written to the ENGINE file only
  populate snap.known_agents/snap.agents (the core proof the vacuous-green
  per-agent loop opened); RAW-only rows are ignored (reads moved off
  db.connection). S-01 fires on a synthetic engine slot-row mismatch and stays
  green on a clean bijection; L-03/E-05 un-blind on engine-only fleet-wide reads.
- Composite-PK agent_tags orphan scan yields synthetic 'agent_tags-row' row_id
  (regression for the KeyError('id') a reflected-PK port would crash on).
- Minimal-DDL _collect_executions fail-open through the engine (inspect() column
  guard: absent session/lease/queued-meta columns skip, not error).
- Engine-down preserves the sqlite.* sources_unavailable prefixes verbatim and
  L-03/E-02 skip on them (pins the label->skip contract; guards a cosmetic
  rename silently disabling the fail-open skip).
- Static import-guard: snapshot.py no longer imports db.connection.
- Env-gated real-Postgres test (TRINITY_TEST_PG_URL): drives collect_snapshot()
  against postgres:16-alpine, asserts sources_unavailable == [] (no collector
  errored on psycopg2) + S-01/L-03 fire. RUN and PASSING against a live
  postgres:16-alpine container.

Co-Authored-By: Claude Fable 5 <[email protected]>
…n G3 (#1540)

- architecture.md Canary section lead: reads state across Redis x the configured
  SQL backend (SQLite OR PostgreSQL via DATABASE_URL), not SQLite-only.
- architecture.md B-01 catalog row: the collector-wide migration landed (#1540)
  — all six SQL-tier collectors honor DATABASE_URL, so the whole harness (not
  just B-01) is un-blinded on PG; the sqlite.* sources_unavailable labels are
  kept verbatim as an internal L-03/E-02 skip-contract prefix.
- Pull-migration G3 caveat closed in PULL_MIGRATION_{TESTING,ROLLBACK,STATUS}:
  the canary is now a trustworthy PG signal (verified against postgres:16-alpine).

Co-Authored-By: Claude Fable 5 <[email protected]>
Canary lives inline in architecture.md (no dedicated flow doc), matching the
#1450/#1077/#1446 convention of a Recent Updates row pointing at architecture.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
@AndriiPasternak31 AndriiPasternak31 self-assigned this Jul 18, 2026
@AndriiPasternak31
AndriiPasternak31 marked this pull request as ready for review July 18, 2026 22:08
@AndriiPasternak31
AndriiPasternak31 requested review from dolho and vybe July 18, 2026 22:08
@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated via /validate-pr: engine-seam repoint un-blinds the canary on PG, named #1540 regression coverage incl. real-PG dialect gate, composite-PK handling for agent_tags. TRINITY_TEST_PG_URL is test-only (no compose wiring needed). All checks green. Approving.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants