fix(tasks): route updateTaskSession/getTaskSession through task_sessions table

Stop writing new session data back to the legacy tasks.session_json
column; updateTaskSession now upserts the active task_sessions row
(updating the session pointed to by tasks.active_session_id, creating
one when absent), and getTaskSession reads from the same row so
session data has a single source of truth. Adds migration 27 to
backfill pre-existing tasks.session_json values into task_sessions
idempotently via INSERT OR IGNORE guarded on task_id. The legacy
column is kept for read-only history and is not dropped.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 21:53:12 -04:00
1 parent ccf0c2a500
commit a130dc29f5
2 files changed
+93 -13

No files matched your search

+42
View File
@@ -365,6 +365,48 @@ export function runMigrations(
version: 26, name: "add_source_run_id_for_causal_binding",
sql: `ALTER TABLE documents ADD COLUMN source_run_id TEXT; ALTER TABLE knowledge_base ADD COLUMN source_run_id TEXT`,
},
{
version: 27, name: "backfill_task_sessions_from_tasks_session_json",
fn: () => {
const legacyTasks = readAll<{
id: string;
companyId: string;
assigneeAgentId: string | null;
sessionJson: string;
sessionDisplayId: string | null;
updatedAt: string;
connectorId: string | null;
}>(
`SELECT t.id AS id, t.company_id AS companyId, t.assignee_agent_id AS assigneeAgentId,
t.session_json AS sessionJson, t.session_display_id AS sessionDisplayId,
t.updated_at AS updatedAt, a.connector_id AS connectorId
FROM tasks t
LEFT JOIN agents a ON a.id = t.assignee_agent_id
WHERE t.session_json IS NOT NULL
AND t.id NOT IN (SELECT task_id FROM task_sessions)`,
);
for (const task of legacyTasks) {
const sessionId = randomUUID();
db.prepare(
`INSERT OR IGNORE INTO task_sessions (id, company_id, task_id, agent_id, adapter_type, session_params_json, session_display_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
).run(
sessionId,
task.companyId,
task.id,
task.assigneeAgentId ?? "unassigned",
task.connectorId ?? "unknown",
task.sessionJson,
task.sessionDisplayId,
task.updatedAt,
task.updatedAt,
);
db.prepare(
"UPDATE tasks SET active_session_id = ? WHERE id = ? AND active_session_id IS NULL",
).run(sessionId, task.id);
}
},
},
];
for (const migration of migrations) {