Add agent task sessions table, session types, and programmatic DB backup

Add agent_task_sessions table for per-task session state keyed by
(agent, adapter, taskKey). Add AgentTaskSession type, resetAgentSessionSchema
validator, and sessionDisplayId/sessionParamsJson to AgentRuntimeState.

Rework migration hash-resolution fallback ordering to prefer hash-based
matching over timestamp-based journal matching. Move backup-db.sh logic
into packages/db/src/backup.ts for programmatic use and simplify the shell
script to call the TypeScript implementation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Forgotten
2026-02-19 14:01:40 -06:00
parent 61db74d525
commit 4e3da49116
13 changed files with 3720 additions and 91 deletions

View File

@@ -0,0 +1,39 @@
import { pgTable, uuid, text, timestamp, jsonb, index, uniqueIndex } from "drizzle-orm/pg-core";
import { companies } from "./companies.js";
import { agents } from "./agents.js";
import { heartbeatRuns } from "./heartbeat_runs.js";
export const agentTaskSessions = pgTable(
"agent_task_sessions",
{
id: uuid("id").primaryKey().defaultRandom(),
companyId: uuid("company_id").notNull().references(() => companies.id),
agentId: uuid("agent_id").notNull().references(() => agents.id),
adapterType: text("adapter_type").notNull(),
taskKey: text("task_key").notNull(),
sessionParamsJson: jsonb("session_params_json").$type<Record<string, unknown>>(),
sessionDisplayId: text("session_display_id"),
lastRunId: uuid("last_run_id").references(() => heartbeatRuns.id),
lastError: text("last_error"),
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
},
(table) => ({
companyAgentTaskUniqueIdx: uniqueIndex("agent_task_sessions_company_agent_adapter_task_uniq").on(
table.companyId,
table.agentId,
table.adapterType,
table.taskKey,
),
companyAgentUpdatedIdx: index("agent_task_sessions_company_agent_updated_idx").on(
table.companyId,
table.agentId,
table.updatedAt,
),
companyTaskUpdatedIdx: index("agent_task_sessions_company_task_updated_idx").on(
table.companyId,
table.taskKey,
table.updatedAt,
),
}),
);

View File

@@ -3,6 +3,7 @@ export { agents } from "./agents.js";
export { agentConfigRevisions } from "./agent_config_revisions.js";
export { agentApiKeys } from "./agent_api_keys.js";
export { agentRuntimeState } from "./agent_runtime_state.js";
export { agentTaskSessions } from "./agent_task_sessions.js";
export { agentWakeupRequests } from "./agent_wakeup_requests.js";
export { projects } from "./projects.js";
export { goals } from "./goals.js";