-- 回合完成检测：per-turn 审计记录 + session 上记录最后一次 stop reason。
-- last_stop_reason 不加 CHECK：agent 可能返回 vendor-specific 值，由 Go 层归一化但原值落库。
ALTER TABLE acp_sessions ADD COLUMN last_stop_reason TEXT;

CREATE TABLE acp_turns (
  id                 BIGSERIAL PRIMARY KEY,
  session_id         UUID NOT NULL REFERENCES acp_sessions(id) ON DELETE CASCADE,
  turn_index         INT NOT NULL,
  prompt_request_id  TEXT,
  status             TEXT NOT NULL,
  stop_reason        TEXT,
  update_count       INT NOT NULL DEFAULT 0,
  started_at         TIMESTAMPTZ NOT NULL DEFAULT now(),
  completed_at       TIMESTAMPTZ,
  CONSTRAINT acp_turns_status_check CHECK (status IN ('in_progress','completed','aborted')),
  CONSTRAINT acp_turns_session_index_unique UNIQUE (session_id, turn_index)
);
CREATE INDEX idx_acp_turns_session ON acp_turns(session_id, turn_index);
CREATE INDEX idx_acp_turns_created ON acp_turns(started_at);
