CREATE TABLE IF NOT EXISTS runs (
    id              TEXT PRIMARY KEY,
    workflow_id     TEXT,
    issue_id        TEXT,
    card_id         TEXT,
    kanban_app      TEXT,
    mode            TEXT NOT NULL DEFAULT 'agent',
    branch          TEXT NOT NULL DEFAULT '',
    worktree_path   TEXT NOT NULL DEFAULT '',
    status          TEXT NOT NULL DEFAULT 'pending',
    started_at      INTEGER NOT NULL,
    finished_at     INTEGER,
    agent_pid       INTEGER,
    agent_log_path  TEXT,
    error           TEXT,
    FOREIGN KEY (workflow_id) REFERENCES workflows(id)
);

CREATE INDEX IF NOT EXISTS idx_runs_status      ON runs(status);
CREATE INDEX IF NOT EXISTS idx_runs_issue       ON runs(issue_id);
CREATE INDEX IF NOT EXISTS idx_runs_card        ON runs(card_id);
CREATE INDEX IF NOT EXISTS idx_runs_kanban_app  ON runs(kanban_app);
CREATE INDEX IF NOT EXISTS idx_runs_started     ON runs(started_at DESC);
