Skip to content

perf(coderd/x/chatd): count notify_user sends in a fork table - #369

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/issue-307-notify-count
Nov 17, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/issue-307-notify-count

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #307 (item 1).

What changed

The hourly notify_user limit counted notification_messages rows by payload->'data'->>'chat_id', which no index serves, so every call scanned the user's custom notifications. It now counts a fork table.

  • New chatdmig node 01a1e3cc..._chat_user_notifications: table chat_user_notifications (id, chat_id, created_at) with indexes on (chat_id, created_at) and (created_at). No index is added to an upstream table.
  • forkdb/queries/chat_user_notifications.sql: InsertChatUserNotification, CountChatUserNotificationsSince(chat_id, since) and DeleteOldChatUserNotifications (at most limit_count rows older than a day). The old notification_messages count in chat_wakeups.sql is removed.
  • notify_user inserts a row once the call passes the limit, before the enqueue, so a send that fails after it queued some deliveries still counts.
  • A wake-up pause notification that was enqueued, or whose send failed (the enqueuer may have queued some deliveries before the error), also inserts a row. A deduped or undeliverable one does not. So pause notifications keep counting toward the limit as docs/fork/pstack-adapter.md documents.
  • dbpurge deletes rows older than a day, at most 1000 per tick, through the created_at index.
  • dbauthz: count is read on notification messages, insert is create on notification messages (the notifier subject has both), delete is system delete. dbmetrics wrappers added.
  • docs/fork/pstack-adapter.md: code citations updated.
  • TestInTx (forkdb, unchanged logic): each parallel subtest now makes its own context. They shared one 10s context of the parent and failed with context deadline exceeded in chat-tests run 43851884892 when they waited longer than that for a slot. This is a test-only fix for a flake that main has too.

Notifications sent before the node runs are not backfilled, so each chat may send one more full hour of them once after the deploy. A backfill would scan notification_messages at startup, which is what this change avoids.

Tests

All run with flock ~/.fork-go.lock.

  • go test ./coderd/database/forkdb/... ./coderd/database/dbpurge/ ./coderd/database/dbmetrics/ -count=1: ok.
  • go test ./coderd/database/dbauthz/ -run TestForkQuerier -count=1: ok.
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/... -count=1: ok. go run ./coderd/database/migrations/chatdmig/cmd/chatdmig check: exit 0.
  • go test ./coderd/x/chatd/ -run 'Fork|Notify|Wakeup|Loop|WaitForEvents' -count=1: ok, 481 PASS lines.
  • golangci-lint run --fix=false --new-from-rev=origin/main on the changed packages: 0 issues. gofmt -l: clean.

New or changed tests:

  • TestCountChatUserNotificationsSince (forkdb): counts per chat and window, and asserts with EXPLAIN that the plan reads only chat_user_notifications with no Seq Scan.
  • TestDeleteOldChatUserNotifications (dbpurge): rows older than a day go, newer rows stay.
  • TestNotifyUserTool: after 10 sends, deleting the user's notification_messages does not lift the limit; moving the fork rows back an hour does.
  • TestWakeupCaps/WakeupsOfCappedChatNotifyOnce: two capped wake-ups send one pause notification and record one row.
  • TestDeleteOldChatUserNotificationsBatch (forkdb): a batch of 2 deletes 2, then 1, then 0 of 3 old rows and keeps the recent one; with 10,000 recent rows the EXPLAIN of the delete has no Seq Scan. Ran -count=20: ok, and the full forkdb package 6 times in a row: ok.
  • TestWakeupCaps/FailedPauseNotificationCounts: an enqueuer that fails still leaves one row.

Negative controls (fix reverted, test failed, fix restored):

  • Insert removed from notify_user: TestNotifyUserTool FAIL.
  • Index removed from the node: TestCountChatUserNotificationsSince FAIL (Seq Scan in the plan).
  • Purge call stubbed out: TestDeleteOldChatUserNotifications FAIL.
  • Pause insert skipped: TestWakeupCaps/WakeupsOfCappedChatNotifyOnce FAIL (expected 1, actual 0).
  • created_at index removed from the node: TestDeleteOldChatUserNotificationsBatch FAIL (Seq Scan in the plan).
  • Batch limit raised by 1000 in the generated query: TestDeleteOldChatUserNotificationsBatch FAIL (expected 2, actual 3).
  • Failed send skipped again (err != nil || len(ids) == 0 returns): TestWakeupCaps/FailedPauseNotificationCounts FAIL (expected 1, actual 0).

Not verified

  • Not run against the production database, so the plan on real data volume is not measured.
  • The full chatd and coderd suites were not run locally; CI covers them.

The hourly notify_user limit counted notification_messages by a JSON
payload field, with no index that serves it. It now counts rows of the
fork table chat_user_notifications, indexed on (chat_id, created_at).
notify_user and sent wake-up pause notifications insert a row, and
dbpurge deletes rows older than a day.

Part of #307.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T03:30:36.176189Z a1f775d Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: df1720ad80

ℹ️ 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".

Comment thread coderd/x/chatd/fork_wakeup_caps.go Resolved
…se sends

DeleteOldChatUserNotifications deletes at most 1000 rows per tick
through a new created_at index. A pause notification whose send failed
counts toward the notify_user limit, since the enqueuer may have queued
some deliveries before the error.

Part of #307.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: ad4d130d7d

ℹ️ 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".

The parallel subtests shared a 10s context of the parent and failed with
context deadline exceeded when they waited for a slot longer than that.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 93e2c54992

ℹ️ 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".

…-count

# Conflicts:
#	coderd/database/forkdb/sqlc.yaml
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: a1f775dada

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit 76b39d6 into main Nov 17, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-307-notify-count branch November 17, 2026 04:02
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head a1f775dada801963c89de0d872a57ad6427e963f as 76b39d6, on main 99d1b24aed8dc13fe9e1fecbd9da1e82c12cd709, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).
  2. Every check run passed. fork-ci run 44010359084 passed on this head.
  3. Codex receipt for this head: perf(coderd/x/chatd): count notify_user sends in a fork table #369 (comment).
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

Tests I ran on a1f775dada (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v ./coderd/database/forkdb/... ./coderd/database/dbpurge/ ./coderd/database/dbmetrics/: 144 PASS, 0 FAIL.
  • go test -count=1 -v -run TestForkQuerier ./coderd/database/dbauthz/: 884 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/migrations/chatdmig/... with CHATDMIG_TEST_DATABASE_URL: 77 PASS, 0 FAIL. chatdmig check: exit 0, the new node 01a1e3cc..._chat_user_notifications in the graph.
  • go test -count=1 -race -v -run 'Fork|Notify|Wakeup|Loop|WaitForEvents' ./coderd/x/chatd/: 523 PASS, 0 FAIL.
  • Negative control: with notify_user recording its row two hours in the past, TestNotifyUserTool fails (the hourly limit never triggers). The source was restored after the control. The PR body lists the workstream's six other negative controls.

Review notes: the hourly notify_user limit now counts rows of a new fork table chat_user_notifications (indexed by chat and time) instead of scanning notification_messages. The migration is a new chatdmig node on a new table, so its plain CREATE INDEX statements lock nothing in use. No existing node changed. dbpurge deletes rows older than a day in batches. No experiment: a coderd-only read path with the same limit. The one difference, stated in the node comment, is that notifications sent before this deploy do not count, so each chat may send one more hour of them once. The PR also carries the TestInTx test fix that #375 already merged; the duplicate is gone after the main merge.

Not exercised: a running server sending real notifications, and two replicas racing on the limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant