Repository navigation
perf: purge old fork rows and bound wake-up and report reads - #368
Conversation
Part of #307. - dbpurge deletes expired plugin link codes, completed and canceled wake-ups older than 30 days, and ended or interrupted workflow runs older than 30 days, in batches. - workflow_report rejects a value over 256 KiB with a tool error. - GET /api/v2/chats/fork/{chat}/wakeups takes a status filter, and the wake-ups pill asks for active and paused only.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df540a636a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Partial indexes on the deletable chat_wakeups and workflow_runs rows, by updated_at, so a purge batch does not scan the table. The expired plugin link code delete takes the same batch limit as the others. Part of #307.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ef2fbb93c9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fdd205742b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A wake-up that changed status between the two reads came back with no watches. The watch list now takes the IDs of the wake-ups the handler listed.
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…bounds # Conflicts: # coderd/database/forkdb/queries/workflow_runs.sql # coderd/database/forkdb/sqlc.yaml
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 006d1590b3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A run cascades to up to 50 steps of up to 256 KiB, so purge 100 runs a tick instead of 1000. Leave a TODO in the chatd architecture document for the 30 day retention of workflow runs.
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d407bd95bc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…rted runs Build the two retention indexes with CREATE INDEX CONCURRENTLY in their own transaction: none nodes, so a rolling upgrade does not block writes to chat_wakeups and workflow_runs. A step write now stamps its run's updated_at, so a report to an interrupted run restarts the run's 30 day retention clock.
@codex review |
Replace the retention TODO with the final text in the fork-workflows notes.
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…bounds # Conflicts: # coderd/database/dbpurge/fork_dbpurge.go # coderd/database/dbpurge/fork_dbpurge_test.go # coderd/database/forkdb/export_test.go # coderd/database/forkdb/sqlc.yaml
Merged
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 721334d6cf
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A purge batch picks old ended rows from its snapshot. When a resume or a step report updated a row while the DELETE waited for its lock, the join on the ID still matched the new row, so the DELETE removed a live run or wake-up. The DELETE now repeats the status and age filter, which Postgres rechecks on the updated row.
Pushed @codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f659d595fc
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…s its old step chats
Pushed @codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3d38cca53e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…rkflowStep An unreferenced data-modifying CTE runs after the main query, so the step update locked the step row before the run row, the reverse of the purge's cascade. A step report racing the purge could deadlock. The step update now joins the run update's RETURNING, so the run row is locked first.
Merged Also fixed the lock-order finding in Build, vet, forkdb, dbpurge, chatdmig, dbauthz, chatd and coderd workflow tests, and lint pass on the merged head. @codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Merged at head Gates on this exact head:
Tests I ran on
Review notes:
Not exercised: a concurrent index build on a large table, and a step report racing the purge of its 30-day-old run. Postgres would abort one side of a deadlock, and both sides retry. A failed concurrent build would leave an invalid index that Closes #307 with this merge. |
Closes #307. This PR has items 2, 3 and 6. Item 1 merged in #369. Items 4, 7 and 8 merged in #370. Item 5 is covered by #294.
What changed
fork_dbpurge.godeletes expiredplugin_link_codes(expires_at < now()), at most 1000 per tick.fork_dbpurge.godeletes, in batches of 100, completed, failed, stopped and interruptedworkflow_runswhoseupdated_atis older than 30 days (steps cascade; running runs stay). The batch is smaller because a run cascades to up to 50 steps of up to 256 KiB.UpdateWorkflowStepalso stamps the run'supdated_at, so a step report to an interrupted run restarts its retention clock. Both purge deletes repeat their status and age filter in the outerDELETE, so a row that a resume or a step report updates while the batch waits for its lock is rechecked on its new version and stays.UpdateWorkflowSteptouches the run in a CTE and updates the stepFROMit, so it locks the run row before the step row, the same order as the purge cascade, and the two cannot deadlock.workflow_reportrejects an input over 256 KiB with a tool error before any decode, so a step row cannot grow past it. Therun_workflowtool description lists the limit.ListChatWakeupsByChatIDtakes astatusesarray (empty means all).GET /api/v2/chats/fork/{chat}/wakeupsaccepts?status=active,paused(400 on an unknown status),codersdk.ChatWakeupstakes optional statuses, and the wake-ups pill asks foractiveandpausedonly.ListChatWakeupWatchesByChatIDtakes the IDs of the wake-ups the handler listed, so it loads only their watches, and a wake-up that changes status between the two reads keeps its watches.createChatWakeupstill lists every status, since a replay must find a completed wake-up. dbpurge deletes completed and canceled wake-ups older than 30 days (watches cascade, runs keep counting with a NULLwakeup_id).01a1e7a0-b608..._chat_wakeups_retention_indexand01a1e7a0-b6c0..._workflow_runs_retention_index: partial indexes onupdated_atof the deletablechat_wakeupsandworkflow_runsrows, so a purge batch does not scan the table. Each is atransaction: nonenode withCREATE INDEX CONCURRENTLY IF NOT EXISTS, so the build does not block writes.coderd/x/chatd/ARCHITECTURE.md: a new fork-owned "Fork-workflows retention" note in the workflow section: the 30 day window, the 100 per tick batch, the step update that restarts the clock, and whatresume_run_idand a lateworkflow_reportdo once a run is gone (a fresh run under the same ID reuses the old step chats whose spec matches, as a resume would). No upstream line changes.ResourceSystemdelete), and swagger for the new query parameter.Tests
All Go commands ran under
flock ~/.fork-go.lock, Postgres 16 local.go test -count=1 -v ./coderd/database/dbpurge/: 26 PASS, 0 FAIL. New:TestDeleteExpiredPluginLinkCodes,TestDeleteOldWorkflowRuns(old ended runs and steps deleted; a running run, a recent run and an old interrupted run whose step just reported kept),TestDeleteOldChatWakeups(old completed/canceled deleted, old active and recent kept).go test -count=1 -run TestForkChatWakeups ./coderd/: 3 PASS. NewTestForkChatWakeupsStatusFilterchecks the filtered list, the unfiltered list and the 400.go test -count=1 -run 'TestForkWorkflowReport$' ./coderd/x/chatd/: PASS, new subtestOversizedValueRejected.go test -count=1 -run 'Fork|Loop|Wakeup|WaitForEvents|ProcessEvents|PullRequestEvents|Workflow' ./coderd/x/chatd/: 146 PASS, 0 FAIL.go test -count=1 -run TestMethodTestSuite ./coderd/database/dbauthz/: ok../coderd/database/forkdb/...,./coderd/database/dbmetrics/: ok.go test -count=1 -run TestChatWakeupWatchRows ./coderd/database/forkdb/: PASS (anactivefilter returns the watches,completedandcanceledreturn none).go test -count=1 -run TestRetentionDeletePlans ./coderd/database/forkdb/: PASS. With 10,000 ended or open rows in each table,EXPLAINof both purge deletes has no Seq Scan.go test -count=3 -race -run TestRetentionDeleteRechecksLockedRows ./coderd/database/forkdb/: PASS. An uncommitted update resumes an old ended run (and an old canceled wake-up); the test waits until the batch blocks on the row lock inpg_stat_activity, commits, and expects 0 deleted and the row kept.go test -count=3 -race -run TestUpdateWorkflowStepLocksRunFirst ./coderd/database/forkdb/: PASS. The test holds the run row lock, waits until the step update is lock-waiting, checks the step row is still free (FOR UPDATE NOWAIT), then commits and sees the stepreported.mainatd310975509:go build ./coderd/...,go vet,go testofforkdb,dbpurge,chatdmig, dbauthzTestForkQuerier, chatd-run 'Workflow|Wakeup|Notify|Plugin|Pack'and coderd-run 'Workflow|Wakeup|Plugin': ok.generate.sh: no diff.go test -count=1 -run 'TestForkQuerier|TestMethodTestSuite' ./coderd/database/dbauthz/: ok.CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/...: ok.chatdmig check: exit 0.pnpm test src/pages/AgentsPage/components/ForkComposerSlot.test.tsx: 19 PASS (asserts the pill requests["active","paused"]).pnpm check: clean.pnpm lint: types, oxlint and circular deps pass;check-compilerfails onForkHtmlAttachmentViewer.tsxand knip lists 3 unused exports, all in files this PR does not touch (same onmain).golangci-lint run --fix=false --new-from-rev=origin/mainon changed packages: 0 issues.gofmt -l: clean.Negative controls
fork_dbpurge.gofrommain:TestDeleteExpiredPluginLinkCodes,TestDeleteOldWorkflowRuns,TestDeleteOldChatWakeupsall FAIL. Restored the fix: PASS.statuses[:0](no filter):TestForkChatWakeupsStatusFilterFAIL.w.status = 'active') put back inListChatWakeupWatchesByChatID:TestChatWakeupWatchRowsFAIL at line 273 (a wake-up that changed status after the list lost its watches). Restored: PASS.OversizedValueRejectedFAIL ("Value accepted" instead of the 256 KiB error).SELECT 1(checked on the earlier single node):TestRetentionDeletePlansFAIL onDeleteOldChatWakeups. Only theworkflow_runsindex removed: FAIL onDeleteOldWorkflowRuns.UpdateWorkflowStepput back to the plain step update:TestDeleteOldWorkflowRunsFAIL, the reported interrupted run is purged. Restored: PASS.DeleteOldWorkflowRuns, then fromDeleteOldChatWakeups:TestRetentionDeleteRechecksLockedRowsFAIL on each case (1 row deleted). Restored: PASS.UpdateWorkflowStepput back to the step update with an unreferenced run CTE:TestUpdateWorkflowStepLocksRunFirstFAIL ("the step update locked the step row before the run row"). Restored: PASS.ForkComposerSlot.test.tsx1 FAIL, 18 PASS.Not verified