Phase 8D Active Batch Query Implementation
Function: get_active_ingestion_batch_id(tenant_id, period_label)
Location: api/bigquery/intake_queries.py
Query Logic
Tenant Isolation
✅ Confirmed: Query filters byb.tenant_id = @tenant_id as the first WHERE condition, ensuring strict tenant isolation.
Deterministic Ordering
✅ Confirmed:- Primary:
b.uploaded_at DESC(most recent first) - Tie-break:
b.batch_id ASC(deterministic UUID ordering)
Status Filtering
✅ Confirmed: Only returns batches with statusMAPPED or PROCESSED.
Period Association
✅ Confirmed: Joins withingestion_batch_businesses table which stores period_label per discovery run. Only batches with discovery records for the given period_label are considered.
Phase 8D Invariant
✅ Confirmed: Filtersd.org_id IS NULL to match Phase 8D platform-scoped discovery.
Fix 1 Implementation
All three endpoints (preflight, discover-businesses, /process) now:
- ✅ Check batch status is
MAPPEDorPROCESSED - ✅ NEW: Verify
received_batch_id == active_batch_idfor(tenant_id, period_label) - ✅ Return
409 ConflictwithBatchMismatcherror if mismatch detected
Error Response Format
Degraded Mode
Ifget_active_ingestion_batch_id fails (BigQuery unavailable), endpoints log error but do not block (allows degraded operation).
Edge Cases
- No active batch exists: Logs warning but allows request (first discovery scenario)
- Invalid period_label format: Logs warning, fails later in flow
- BigQuery unavailable: Logs error, allows degraded operation