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) <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-04-11 17:18:34 +03:00
parent e92b245399
commit 062b0cb6bf
+9 -1
View File
@@ -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;
}