-- 020_session_reliability.up.sql
-- Plan step generation tracking + audit log session linkage.
-- See plans/done/2026-07-14-session-reliability-and-ux-audit.md.

-- Plan step generation: when the agent revises a plan mid-flight, new steps
-- get a higher generation number so the frontend can group/collapse old ones.
ALTER TABLE session_plan_steps ADD COLUMN IF NOT EXISTS generation INTEGER NOT NULL DEFAULT 1;

-- Track which session produced each audit-log entry so per-session analysis
-- (e.g. "did this task call update_entity_attributes?") is O(1) instead of
-- scanning the full log.
ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS session_id UUID;
CREATE INDEX IF NOT EXISTS idx_audit_log_session ON audit_log (session_id);

-- Efficient lookup of pending-approval executions by session.
CREATE INDEX IF NOT EXISTS idx_nomos_plan_executions_session
  ON nomos_plan_executions (session_id) WHERE continued_at IS NULL;
