From 062b0cb6bf877ee12d1239d8cadd8ccb4c25cd96 Mon Sep 17 00:00:00 2001 From: gavrielc Date: Sat, 11 Apr 2026 17:18:34 +0300 Subject: [PATCH] fix(agent-runner): add updated_at column to session_state on older DBs session_state was added after the initial v2 schema with a lazy `CREATE TABLE IF NOT EXISTS` in getOutboundDb(), so older session outbound.db files have a session_state table from before updated_at existed. The lazy create is a no-op when the table already exists, leaving the column missing and causing: Error: table session_state has no column named updated_at on every `INSERT OR REPLACE INTO session_state` call. Follow up the CREATE IF NOT EXISTS with a PRAGMA table_info check and ALTER TABLE ADD COLUMN when updated_at is missing. Cheap on every open, only runs DDL once per DB. Co-Authored-By: Claude Opus 4.6 (1M context) --- container/agent-runner/src/db/connection.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/container/agent-runner/src/db/connection.ts b/container/agent-runner/src/db/connection.ts index 1f1c407..4036232 100644 --- a/container/agent-runner/src/db/connection.ts +++ b/container/agent-runner/src/db/connection.ts @@ -40,7 +40,9 @@ export function getOutboundDb(): Database.Database { _outbound.pragma('foreign_keys = ON'); // Lightweight forward-compat: session_state was added after the initial // v2 schema, so older session DBs don't have it. Create it on demand - // instead of requiring a formal migration pass. + // instead of requiring a formal migration pass. Also handle the case + // where an earlier revision of this table existed without updated_at — + // ALTER TABLE to add any missing columns. _outbound.exec(` CREATE TABLE IF NOT EXISTS session_state ( key TEXT PRIMARY KEY, @@ -48,6 +50,12 @@ export function getOutboundDb(): Database.Database { updated_at TEXT NOT NULL ); `); + const cols = new Set( + (_outbound.prepare("PRAGMA table_info('session_state')").all() as Array<{ name: string }>).map((c) => c.name), + ); + if (!cols.has('updated_at')) { + _outbound.exec(`ALTER TABLE session_state ADD COLUMN updated_at TEXT NOT NULL DEFAULT ''`); + } } return _outbound; }