Spec: Native-async HITL quarantine to Postgres¶
- ID: 004-async-quarantine-postgres
- Status: shipped
- Owner: @copilot
- Created: 2026-04-29
- Roadmap Epic: BL-20260429-02 (BACKLOG.md)
- Constitution Compliance: confirmed against docs/constitution.md §1 (open-source), §4 (auditability), §7 (HITL).
1. Problem & Motivation¶
IngestionScout._quarantine_postgres is implemented as a sync wrapper that calls asyncio.run() internally. Because run_and_commit is itself an async function, this path always raises RuntimeError("called from inside event loop") in production and falls back to JSONL. Result: low-confidence opinions never reach the pending_review Postgres table that the future HITL dashboard (BL-20260429-15) is supposed to query. JSONL is a dev-only escape hatch, not a HITL queue.
Surfaced in specs/001-ingest-courtlistener-opinions and recorded as BL-20260429-02.
2. Goals¶
- G1: When
run_and_commitruns inside an event loop and Postgres is reachable, low-confidence items land inpending_review(no JSONL fallback). - G2: Keep the JSONL fallback as a true failure path — only triggered when the Postgres write actually errors (driver missing, connection refused, schema absent).
- G3: Preserve unit-test ergonomics: tests must still run without
asyncpginstalled against a live database.
3. Non-Goals¶
- NG1: No HITL dashboard UI — that is BL-20260429-15.
- NG2: No new schema. The existing
pending_reviewtable frombackend/app/db/postgres_migrations/is reused. - NG3: No connection pooling/sharing across calls; per-call connect is acceptable for the ingestion volumes in scope.
4. User Stories¶
- As an ingestion operator, I want low-confidence opinions to be persisted in Postgres immediately so I can review them later in
psql(and, eventually, the dashboard) instead of grepping JSONL files. - As a test author, I want to be able to run
pytestwithout standing up Postgres or installingasyncpg.
5. Functional Requirements¶
- FR-1:
IngestionScout._quarantineMUST beasync defand MUST be awaited fromrun_and_commitfor low-confidence results. - FR-2:
IngestionScout._quarantine_postgresMUST beasync defand MUST use the existing event loop (noasyncio.run/asyncio.get_running_loopgymnastics). - FR-3: When the Postgres write succeeds, the JSONL fallback MUST NOT also be written.
- FR-4: When the Postgres write fails for any reason (
ImportError,OSError,asyncpg.PostgresError, genericException), the JSONL fallback MUST be written and the failure MUST be logged vialogger.warningwith the underlying exception class. - FR-5: The
asyncpgimport MUST stay lazy (inside the function) so unit tests do not need the package.
6. Non-Functional Requirements¶
- NFR-AUDIT-1: Both Postgres and JSONL paths MUST emit a
provenance.log_agent_callentry withoutput_data.store ∈ {"postgres", "jsonl"}so audit consumers can tell which sink fired. - NFR-PERF-1: Quarantine write MUST add < 50 ms (p95) at the application layer when Postgres is on localhost (excludes DB round-trip).
- NFR-COVERAGE-1:
app/ingestion/scout.pyline coverage MUST stay ≥ 80% (currently 81.60%). - NFR-COMPAT-1: No change to
IngestionScoutpublic surface (extract,run_and_commit, ctor) soscripts/ingest_sample.pykeeps working.
7. Data Model Impact¶
None. Reuses the pending_review table defined in backend/app/db/postgres_migrations/0001_pending_review.sql (id, source, payload jsonb, confidence, reason, created_at).
8. API Surface¶
No HTTP API changes.
9. Agent / Crew Impact¶
IngestionScout(see docs/agent-roles.md) — internal refactor only;agent_versionbumps to0.1.1.
10. Acceptance Criteria¶
- AC-1: Given a low-confidence payload and a stubbed
_quarantine_postgresthat succeeds, whenrun_and_commitis awaited, then no JSONL file is written and the returned summary haspending=True,committed=False. - AC-2: Given a low-confidence payload and a stubbed
_quarantine_postgresthat raises, whenrun_and_commitis awaited, then exactly one JSONL line is written underPENDING_REVIEW_DIRandprovenance.log_agent_callrecordsstore="jsonl". - AC-3: Given a low-confidence payload and a real-but-mocked
asyncpg.connect(no live Postgres), when_quarantine_postgresis awaited, thenconn.executeis called once with theINSERT … ON CONFLICTstatement andconn.closeis awaited even whenexecuteraises. - AC-4:
run_and_commitMUST NOT callasyncio.runorasyncio.get_running_loopanywhere in the quarantine code path. - AC-5: Existing test
test_low_confidence_routes_to_pending_jsonlkeeps passing without modification beyond switching the monkeypatched method to anasynccallable.
11. LegalBench Impact¶
- Tasks affected: none (refactor only).
- Expected delta: 0 pp.
12. Risks & Open Questions¶
- R-1: Per-call
asyncpg.connectadds a TCP handshake per low-confidence opinion. Mitigated by NG3 — pooling deferred until throughput requires it; tracked as a follow-up.
13. Out of Scope / Follow-ups¶
- Connection pooling / shared
asyncpg.Pool. - HITL approve/reject endpoints (BL-20260429-15).
- Docker-backed integration test (BL-20260429-01 already tracks this).