CREATE TABLE IF NOT EXISTS sub_steps (
    id            INTEGER PRIMARY KEY AUTOINCREMENT,
    run_id        INTEGER NOT NULL REFERENCES runs(id) ON DELETE CASCADE,
    stage_name    TEXT    NOT NULL,
    ordinal       INTEGER NOT NULL,
    name          TEXT    NOT NULL,
    state         TEXT    NOT NULL DEFAULT 'pending',
    started_at    TIMESTAMP,
    completed_at  TIMESTAMP,
    summary_json  TEXT    NOT NULL DEFAULT '{}',
    UNIQUE (run_id, stage_name, ordinal)
);
CREATE INDEX IF NOT EXISTS idx_sub_steps_run ON sub_steps(run_id, stage_name, ordinal);
