index|idx_run_seats_item|run_seats|CREATE INDEX idx_run_seats_item ON run_seats(item_id)
index|idx_run_seats_run_status|run_seats|CREATE INDEX idx_run_seats_run_status ON run_seats(run_id, status)
index|uq_run_seats_active|run_seats|CREATE UNIQUE INDEX uq_run_seats_active ON run_seats(run_id, item_id, seat) WHERE status = 'active'
index|uq_run_seats_coordinator|run_seats|CREATE UNIQUE INDEX uq_run_seats_coordinator ON run_seats(run_id) WHERE seat = 'coordinator' AND status = 'active'
table|run_seats|run_seats|CREATE TABLE run_seats ( id BLOB PRIMARY KEY NOT NULL, run_id BLOB NOT NULL REFERENCES runs(id) ON DELETE CASCADE, item_id BLOB REFERENCES work_items(id) ON DELETE CASCADE, seat TEXT NOT NULL CHECK (length(seat) BETWEEN 1 AND 64), attempt INTEGER NOT NULL CHECK (attempt >= 1), principal_id TEXT NOT NULL, token_hash TEXT NOT NULL UNIQUE CHECK (length(token_hash) = 64), status TEXT NOT NULL CHECK (status IN ('active', 'ended')), outcome TEXT CHECK (outcome IS NULL OR outcome IN ('done', 'failed', 'abandoned', 'superseded', 'run_closed')), summary TEXT CHECK (summary IS NULL OR length(summary) <= 500), links TEXT, hints TEXT, assigned_at TEXT NOT NULL CHECK (length(assigned_at) = 23), ended_at TEXT CHECK (ended_at IS NULL OR length(ended_at) = 23), last_seen_at TEXT CHECK (last_seen_at IS NULL OR length(last_seen_at) = 23), usage_tokens INTEGER NOT NULL DEFAULT 0 CHECK (usage_tokens >= 0), usage_cost_micro INTEGER NOT NULL DEFAULT 0 CHECK (usage_cost_micro >= 0), CHECK ( (status = 'active' AND outcome IS NULL AND ended_at IS NULL) OR (status = 'ended' AND outcome IS NOT NULL AND ended_at IS NOT NULL) ) )
