Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
MUL-7247: bound cancelled chat pointer lookup (#8259)
Multica Evec784d539b013
diff --git a/server/cmd/migrate/README.md b/server/cmd/migrate/README.mdindex 7fac00cb61..b9c1cf9bbc 100644--- a/server/cmd/migrate/README.md+++ b/server/cmd/migrate/README.md@@ -1,5 +1,53 @@# Migration runner operations+## Prebuild the cancelled-chat session guard index++Migration 465 builds `idx_agent_task_queue_chat_with_session_created_at`, which+keeps `AdvanceCancelledChatSessionPointer` from scanning the global task queue+while a cancel or late session pin holds the chat row lock. This is a net-new+index on `agent_task_queue`, and most historical chat tasks may satisfy its+predicate. Measure the eligible population before scheduling the build:++```sql+SELECT count(*) AS indexed_rows+FROM agent_task_queue+WHERE chat_session_id IS NOT NULL+ AND session_id IS NOT NULL;+```++Migrations run during backend startup, whose Helm startup probe allows ten+minutes. On a large production table, check for long-running transactions and+prebuild the index in a low-traffic window. Run the statement by itself and+outside a transaction:++```sql+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_agent_task_queue_chat_with_session_created_at+ON agent_task_queue (chat_session_id, created_at DESC)+WHERE chat_session_id IS NOT NULL+ AND session_id IS NOT NULL;+```++Confirm the build is usable before deploying:++```sql+SELECT indexrelid::regclass AS index_name,+ pg_size_pretty(pg_relation_size(indexrelid)) AS size,+ indisvalid,+ indisready,+ indislive+FROM pg_index+WHERE indexrelid = to_regclass('idx_agent_task_queue_chat_with_session_created_at');+```++The migration then becomes a fast no-op. If an interrupted manual build leaves+the index invalid, drop that invalid index concurrently and retry the standalone+build before deploying. The migration runner also registers invalid-index+cleanup so an interrupted startup build can recover on its next attempt.++Application rollback is compatible with the extra index, so leave it in place.+Rolling back migration 465 drops the index and is functionally safe, but restores+the global scan and longer chat-lock hold time.+## Issue description search index retirementMigrations 463 and 464 retire the two historical issue-description searchdiff --git a/server/cmd/migrate/main.go b/server/cmd/migrate/main.goindex ed61b3df94..36ba5052ca 100644--- a/server/cmd/migrate/main.go+++ b/server/cmd/migrate/main.go@@ -302,6 +302,7 @@ var concurrentIndexCleanups = map[string]string{"458_activity_log_member_assignee_frequency_index": "idx_activity_log_member_assignee_frequency","459_chat_message_assistant_task_index": "idx_chat_message_assistant_task","460_agent_task_queue_autopilot_run_created_at_index": "idx_agent_task_queue_autopilot_run_created_at",+ "465_agent_task_queue_chat_with_session_index": "idx_agent_task_queue_chat_with_session_created_at",}// concurrentDownIndexCleanups covers every migration whose down directiondiff --git a/server/migrations/465_agent_task_queue_chat_with_session_index.down.sql b/server/migrations/465_agent_task_queue_chat_with_session_index.down.sqlnew file mode 100644index 0000000000..60e749dcbd--- /dev/null+++ b/server/migrations/465_agent_task_queue_chat_with_session_index.down.sql@@ -0,0 +1,3 @@+-- Removing this index is functionally safe, but returns+-- AdvanceCancelledChatSessionPointer to a global agent_task_queue scan.+DROP INDEX CONCURRENTLY IF EXISTS idx_agent_task_queue_chat_with_session_created_at;diff --git a/server/migrations/465_agent_task_queue_chat_with_session_index.up.sql b/server/migrations/465_agent_task_queue_chat_with_session_index.up.sqlnew file mode 100644index 0000000000..614c565dc5--- /dev/null+++ b/server/migrations/465_agent_task_queue_chat_with_session_index.up.sql@@ -0,0 +1,21 @@+-- Bound AdvanceCancelledChatSessionPointer's newer-task guard to one chat+-- instead of scanning the global task population (MUL-7247).+--+-- This is a net-new index on the hottest write table and may contain most+-- historical chat tasks: session_id is normally present once a chat task has+-- started. The partial predicate still excludes every issue task and chat tasks+-- that never established a provider session.+-- Measure the production build set before rollout with:+-- SELECT count(*) FROM agent_task_queue+-- WHERE chat_session_id IS NOT NULL AND session_id IS NOT NULL;+--+-- Keep this separate from idx_agent_task_queue_chat_pending_v3. That index's+-- status predicate is what keeps workspace-wide pending-task queries selective,+-- while this guard must see both in-flight and terminal tasks with sessions.+--+-- This file must stay single-statement: CREATE INDEX CONCURRENTLY cannot run in+-- a transaction or a multi-command string.+CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_agent_task_queue_chat_with_session_created_at+ON agent_task_queue (chat_session_id, created_at DESC)+WHERE chat_session_id IS NOT NULL+ AND session_id IS NOT NULL;diff --git a/server/pkg/db/generated/chat.sql.go b/server/pkg/db/generated/chat.sql.goindex 956debd065..2838d0b7f4 100644--- a/server/pkg/db/generated/chat.sql.go+++ b/server/pkg/db/generated/chat.sql.go@@ -85,6 +85,11 @@ WHERE t.id = $1// makes the late pin safe: a NEWER task on this chat that already recorded a// session owns the pointer, and a straggler must not drag the conversation// backwards onto the turn the user interrupted.+//+// The newer-task lookup depends on+// idx_agent_task_queue_chat_with_session_created_at (migration 465). Neither+// chat_pending_v3 nor chat_terminal_resume can replace it: this guard spans+// both in-flight and terminal tasks and compares created_at.func (q *Queries) AdvanceCancelledChatSessionPointer(ctx context.Context, taskID pgtype.UUID) error {_, err := q.db.Exec(ctx, advanceCancelledChatSessionPointer, taskID)return errdiff --git a/server/pkg/db/queries/chat.sql b/server/pkg/db/queries/chat.sqlindex f53e10c87d..7f5403e14a 100644--- a/server/pkg/db/queries/chat.sql+++ b/server/pkg/db/queries/chat.sql@@ -331,6 +331,11 @@ WHERE id = sqlc.arg('id')-- makes the late pin safe: a NEWER task on this chat that already recorded a-- session owns the pointer, and a straggler must not drag the conversation-- backwards onto the turn the user interrupted.+--+-- The newer-task lookup depends on+-- idx_agent_task_queue_chat_with_session_created_at (migration 465). Neither+-- chat_pending_v3 nor chat_terminal_resume can replace it: this guard spans+-- both in-flight and terminal tasks and compares created_at.UPDATE chat_session csSET session_id = t.session_id,runtime_id = t.runtime_id,