Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(db): add ON DELETE CASCADE to agent_task_queue.conversation_id (#46)

Genera9d5f1457c75
diff --git a/src/shared/src/db/schema.ts b/src/shared/src/db/schema.ts
index 0f9b48cb1..7bd379b1b 100644
--- a/src/shared/src/db/schema.ts
+++ b/src/shared/src/db/schema.ts
@@ -311,13 +311,13 @@ export const agentTaskQueue = sqliteTable(
agentId: text("agent_id").notNull(),
runtimeId: text("runtime_id")
.notNull()
- .references(() => agentRuntime.id),
+ .references(() => agentRuntime.id, { onDelete: "cascade" }),
workspaceId: text("workspace_id")
.notNull()
- .references(() => workspace.id),
+ .references(() => workspace.id, { onDelete: "cascade" }),
conversationId: text("conversation_id")
.notNull()
- .references(() => conversation.id),
+ .references(() => conversation.id, { onDelete: "cascade" }),
prompt: text("prompt").notNull(),
type: text("type").notNull().default(TASK_TYPES.USER_DM_MESSAGE),
contextKey: text("context_key"),
diff --git a/src/web/migrations/0017_task_queue_cascade_conversation.sql b/src/web/migrations/0017_task_queue_cascade_conversation.sql
new file mode 100644
index 000000000..8f0ada825
--- /dev/null
+++ b/src/web/migrations/0017_task_queue_cascade_conversation.sql
@@ -0,0 +1,56 @@
+-- Rebuild agent_task_queue to fix FK cascade behavior:
+-- 1. conversation_id: ON DELETE CASCADE — deleting a channel (which cascades
+-- to its conversations) no longer fails due to dangling task references.
+-- 2. workspace_id: ON DELETE CASCADE — same logic for workspace deletion.
+-- 3. runtime_id: ON DELETE CASCADE — matches existing app-level behavior
+-- (deleteRuntimesByDaemonId already deletes associated tasks).
+
+PRAGMA foreign_keys = OFF;
+
+-- Clean up orphaned task rows that reference deleted parents
+DELETE FROM agent_task_queue WHERE runtime_id NOT IN (SELECT id FROM agent_runtime);
+DELETE FROM agent_task_queue WHERE workspace_id NOT IN (SELECT id FROM workspace);
+DELETE FROM agent_task_queue WHERE conversation_id NOT IN (SELECT id FROM conversation);
+
+CREATE TABLE agent_task_queue_new (
+ id TEXT PRIMARY KEY,
+ agent_id TEXT NOT NULL,
+ runtime_id TEXT NOT NULL REFERENCES agent_runtime(id) ON DELETE CASCADE,
+ workspace_id TEXT NOT NULL REFERENCES workspace(id) ON DELETE CASCADE,
+ conversation_id TEXT NOT NULL REFERENCES conversation(id) ON DELETE CASCADE,
+ prompt TEXT NOT NULL,
+ type TEXT NOT NULL DEFAULT 'user_dm_message',
+ status TEXT NOT NULL DEFAULT 'queued',
+ priority INTEGER NOT NULL DEFAULT 0,
+ result TEXT,
+ context TEXT,
+ session_id TEXT,
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
+ dispatched_at TEXT,
+ started_at TEXT,
+ completed_at TEXT,
+ error TEXT,
+ context_key TEXT,
+ FOREIGN KEY (agent_id, workspace_id) REFERENCES agent(id, workspace_id) ON DELETE CASCADE
+);
+
+INSERT INTO agent_task_queue_new SELECT * FROM agent_task_queue;
+
+DROP TABLE agent_task_queue;
+
+ALTER TABLE agent_task_queue_new RENAME TO agent_task_queue;
+
+CREATE INDEX idx_task_queue_pending
+ ON agent_task_queue(agent_id, status)
+ WHERE status IN ('queued', 'dispatched');
+
+CREATE INDEX idx_task_queue_workspace_active
+ ON agent_task_queue(workspace_id, status, agent_id)
+ WHERE status IN ('queued', 'dispatched', 'running');
+
+CREATE INDEX idx_task_queue_agent_history
+ ON agent_task_queue(agent_id, workspace_id, created_at);
+
+PRAGMA foreign_keys = ON;
+
+PRAGMA foreign_key_check;