Plan: Close the Ingestion HITL Loop (Approval → Graph)¶
- Spec: spec.md (029-close-ingestion-hitl-loop)
- Approach: keep approval and commit as two distinct persisted states so a graph outage can never discard a reviewer's decision, and route the actual write through the one sanctioned writer rather than adding a second one (constitution §12 I1).
Why two states¶
approved alone cannot express "a human said yes but the graph write failed". Splitting into approved (human intent) + committed_at (system outcome) + commit_error (why not) makes the handoff idempotent, retryable, and auditable — and makes a stuck row visible instead of looking done.
Steps¶
- Model + migration —
reviewer,review_note,committed_at,commit_erroronPendingReview, plusis_committed/awaits_commithelpers. Migration0003copies the 0001/0002 pattern exactly:SeparateDatabaseAndState, PostgresADD COLUMN IF NOT EXISTS(adopts the legacy spec-004 table in place), SQLite portable form for the test fixture, plus a(approved, committed_at)index for the sweep query. All columns nullable/defaulted so existing rows back-fill correctly. IngestionScout.run_and_commit(force_commit=...)— the approval path must bypass the confidence gate that quarantined the row in the first place, butforce_quarantinekeeps precedence so approval-required sources can never auto-commit. Same method stays the sole:Casewriter.- Live HITL gate —
_hitl_gates_enabled()readsENABLE_HUMAN_APPROVAL_GATESat call time (not import time, so tests and operators can flip it per-run); disabling it skips quarantine and emits ahitl_gate_disabledprovenance event per item, because §5 says the gate is mandatory and an override must leave a trace. services/approval.py—record_decision(attribution + rationalequarantine_decisionevent),commit_approved_review(state checks, payload recovery for both envelope and legacy flat shapes, commit, persist outcome, emitquarantine_committed/quarantine_commit_failed),commit_approved_reviews(bounded idempotent sweep). Async ORM writes go throughsync_to_async.- Three operator surfaces, all delegating to the same service: Celery
task
commit_approved_reviews_task, admin actions Approve + commit / Reject, andmanage.py review_queue. The admin records the Django user as reviewer; the CLI takes--reviewerand warns when it is omitted. review_queue showanswers the audit's "a human cannot see WHY something is quarantined": reason, confidence, review state, commit error, and the full payload.
Test strategy¶
tests/ingestion/test_approval_commit.py (9 tests) with IngestionScout.run_and_commit mocked so no Neo4j is needed: commit path (asserting force_commit=True and the unwrapped payload), pending/rejected never committed, idempotent second sweep, graph-failure recording + retry state, ApprovalError on unapproved rows, legacy flat payload, decision attribution, and the Celery task summary.
Verification beyond unit tests: makemigrations --check clean (state matches the model), migration applied against real Postgres with columns and indexes confirmed via \d pending_review, and the full ingestion suite green.
Rollback¶
migrate ingestionapp 0002 reverses the columns (the reverse SQL is written); the service and surfaces are additive and can be reverted independently. No Neo4j data is touched by the rollback.