-- 20260501-001 — Stage 3 queue routing columns for Phase A bulk-vs-single-episode routing
--
-- Adds four columns and one index to stage_3_queue, written by Stage 2 v2.2
-- and read by Stage 3 v2.3 to choose between bulk and single-episode ingest
-- pathways. See architecture doc and Phase A handoff (2026-05-01) for design.
--
-- Required by:
--   scripts/stage2_worker.py >= 2.2
--   scripts/stage3_worker.py >= 2.3
--
-- Idempotent: safe to re-apply against a database where the columns already
-- exist (was applied live before this file was created).

ALTER TABLE stage_3_queue
    ADD COLUMN IF NOT EXISTS state_type             TEXT,
    ADD COLUMN IF NOT EXISTS state_type_confidence  TEXT,
    ADD COLUMN IF NOT EXISTS supersedes_prior_state BOOLEAN,
    ADD COLUMN IF NOT EXISTS state_type_rationale   TEXT;

-- Index on the routing signal — Stage 3 reads this on every dequeue,
-- and observability queries (item 6: routing_decisions) will filter on it.
CREATE INDEX IF NOT EXISTS stage_3_queue_supersedes_idx
    ON stage_3_queue (supersedes_prior_state);
