| 27 | } |
| 28 | |
| 29 | async function ensureTable(): Promise<void> { |
| 30 | if (!_initPromise) { |
| 31 | _initPromise = (async () => { |
| 32 | const client = getDbExec(); |
| 33 | await retryOnDdlRace(() => |
| 34 | client.execute(` |
| 35 | CREATE TABLE IF NOT EXISTS progress_runs ( |
| 36 | id TEXT PRIMARY KEY, |
| 37 | owner TEXT NOT NULL, |
| 38 | title TEXT NOT NULL, |
| 39 | step TEXT, |
| 40 | percent ${intType()}, |
| 41 | status TEXT NOT NULL DEFAULT 'running', |
| 42 | metadata TEXT, |
| 43 | started_at ${intType()} NOT NULL, |
| 44 | updated_at ${intType()} NOT NULL, |
| 45 | completed_at ${intType()} |
| 46 | ) |
| 47 | `), |
| 48 | ); |
| 49 | await retryOnDdlRace(() => |
| 50 | client.execute( |
| 51 | `CREATE INDEX IF NOT EXISTS idx_progress_runs_owner_status ON progress_runs (owner, status, started_at)`, |
| 52 | ), |
| 53 | ); |
| 54 | // NOTE: table name is `progress_runs` (not `agent_runs`) to avoid |
| 55 | // colliding with core's existing agent/run-store.ts which uses |
| 56 | // `agent_runs` for agent-chat turn lifecycle tracking. These are |
| 57 | // separate concerns — progress = user-facing task status, agent_runs = |
| 58 | // internal chat turn bookkeeping. |
| 59 | })().catch((err) => { |
| 60 | // Reset on failure so a transient DB outage doesn't poison the cached |
| 61 | // promise and reject every future insert/update call for the lifetime |
| 62 | // of the process. |
| 63 | _initPromise = undefined; |
| 64 | throw err; |
| 65 | }); |
| 66 | } |
| 67 | return _initPromise; |
| 68 | } |
| 69 | |
| 70 | function parseRow(row: Record<string, unknown>): AgentRun { |
| 71 | const percent = row.percent; |