Skip to content

perf(chatd): cache wake-up organization spend and purge old run counts - #358

Merged
ibetitsmike merged 13 commits into
mainfrom
mike/issue-297-wakeup-spend-cache
Nov 16, 2026
Merged

ibetitsmike merged 13 commits into
mainfrom
mike/issue-297-wakeup-spend-cache

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part 2 of 2 for #297 (perf plan #311). Part 1, #357, merged as 2523de1 and added the counter table and its trigger. This PR is based on main.

What changed

  • SumOrganizationWakeupChatCostMicrosSince takes its chat set from chat_wakeup_run_counts in the cap window (runs_since, the start of the hour 24 hours back) instead of every chat of the organization that ever had a wake-up. A chat whose wake-ups ran only before the window no longer counts.
  • Each chatd Server caches the organization spend per organization for 60 seconds (wakeupSpendCache, hook line forkWakeupSpend in the Server struct). The cache uses the now of the check, which comes from the Server's quartz clock. It reads again once the value is 60 s old, when now is before the time it was read, or when the cached value is at or above the organization's cap, so a pause never rests on a cached value. A delivery under the caps with the spend cached issues 3 cap statements: caps row, run sums, chat spend.
  • The order of checks is unchanged: caps row, chat runs, org runs, chat spend, org spend, returning on the first cap reached.
  • The fork dbpurge hook deletes counter rows older than 7 days (DeleteOldChatWakeupRunCounts, system delete in dbauthz) and logs the count at debug level.
  • ARCHITECTURE.md describes the cache, its lag and the purge. docs/fork/pstack-adapter.md says the organization spend limit covers the chats with a wake-up run in the last 24 hours and that it can stop runs up to a minute late.

Tradeoff

The organization spend cap can lag by up to 60 seconds, per replica. At the default cap that lets an organization overshoot by the spend of one minute of wake-ups. The run caps are not cached. The organization spend no longer counts a chat with a wake-up but no delivered run in the last 24 hours, so human spend in such a chat does not hold back other chats' wake-ups.

Rollback

This PR adds no migration node. Rolling back this PR alone needs no snapshot. A rollback past #357 needs the database snapshot, as #357 says.

Tests

All run locally against Postgres 16:

  • go test ./coderd/x/chatd/... passed.
  • go test ./coderd/database/forkdb/ ./coderd/database/dbauthz/ ./coderd/database/dbpurge/ passed.
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/... passed.
  • New TestWakeupCapStatements (gomock forkdbmock, each statement Times(1)): the first check issues 4 statements, a check 59 s later issues 3, a check at 60 s reads the spend again, a check with the clock moved back reads it again, and a cached spend at the cap pauses once and is read again on the next check.
  • New TestWakeupCaps/OrganizationSpendOfChatsWithoutRecentRunsIgnored: spend of a chat whose last run is older than the window does not reach the cap.
  • TestSumChatCostMicrosSince (forkdb) adds a stale chat that must not count. Its runs are inserted as delivered chat_wakeup_runs, so the perf(chatd): count wake-up runs in hourly buckets #357 trigger fills the counter.
  • New TestDeleteOldChatWakeupRunCounts (dbpurge): rows older than 7 days go, newer rows stay.
  • golangci-lint run --fix=false on the touched packages reports nothing new in the changed files. It reports one existing finding in fork_dbpurge.go:29 (a short log message on main). gofmt -l is clean.

Negative controls: with the cache hit disabled, TestWakeupCapStatements fails with an unexpected call to SumOrganizationWakeupChatCostMicrosSince. With the at-cap re-read removed, it fails because the next check still pauses.

Not verified

  • The cache is per replica, and only one replica ran in the tests.
  • No live deploy test.

Closes #297

The daily run caps counted chat_wakeup_runs rows over 24 hours on every
delivery, one scan of a chat and one of a whole organization. A delivery
now upserts a per-chat hourly bucket in chat_wakeup_run_counts in the
send's transaction, and one statement sums at most 25 buckets for the
chat and the organization. A chatdmig node creates the table and
backfills it from the delivered runs of the last 25 hours.

Refs #297
The organization spend cap summed the cost of a day of chat messages in
every chat that ever had a wake-up, on every delivery. Each Server now
caches that sum per organization for 60 seconds on its quartz clock, and
the query takes its chat set from the run counts of the window. dbpurge
deletes run counts older than 7 days.

Refs #297
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@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-16T03:51:12.645716Z e37d36d 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: 6f7ca8b4ab

ℹ️ 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
Comment thread coderd/x/chatd/fork_wakeup_caps.go Resolved
A replica that does not know chat_wakeup_run_counts still inserts
delivered runs during a rolling upgrade. A trigger on chat_wakeup_runs
counts each delivered run in the inserting transaction, so those runs
count against the daily caps too. The Go upsert is removed.
…ws-297-spend-cache

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@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: daba8ed0fd

ℹ️ 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
…sing

A cached spend at the cap could pause a wake-up after the spend fell
under the cap. Document that the organization spend cap covers the
chats with a wake-up run in the window, and the one-minute cache lag.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@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: f56110b58a

ℹ️ 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 docs/fork/pstack-adapter.md Resolved
Comment thread docs/fork/pstack-adapter.md Resolved
@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: df16af98df

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 704e1bfa7a

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

Base automatically changed from mike/issue-297-wakeup-cap-counters to main November 16, 2026 02:08
# Conflicts:
#	coderd/database/forkdb/chat_wakeups.sql.go
#	coderd/database/forkdb/querier.go
#	coderd/database/forkdb/queries/chat_wakeups.sql
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_wakeup_caps.go
#	coderd/x/chatd/fork_wakeup_run_counts_internal_test.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: cfa41fa6b7

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: f43a38f411

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: e37d36d80e

ℹ️ 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 d375e04 into main Nov 16, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-297-wakeup-spend-cache branch November 16, 2026 04:39
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head e37d36d80eb8e60d444cb15f9d5bbfcb69edb95d as d375e04, on main c71b3abcd970ea6ac6f62f9e6b595ab02e410d44, 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 43800297916 passed on this head.
  3. Codex receipt for this head: perf(chatd): cache wake-up organization spend and purge old run counts #358 (comment) ("Didn't find any major issues").
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. The 5 PR commits are unchanged since I tested them at cfa41fa6b7 (git range-diff shows all 5 identical). The head adds two main merges: from cfa41fa6b7 to f43a38f411 (perf(coderd/x/plugins): renew plugin leases without reading full rows #360; 6 files equal main's delta, 5 generated files touched by both carry the same PR lines), and from f43a38f411 to this head (feat(site): stop and resume workflow runs from the card and panel #346; all 22 files equal main's own delta, site only).

Tests I ran on the PR commits at cfa41fa6b7 (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432, CHATDMIG_TEST_DATABASE_URL set):

  • go test -count=1 -v -run 'TestWakeupCaps|TestWakeupRunCounts|TestWakeupCapStatements|Wakeup' ./coderd/x/chatd/: 136 PASS, 0 FAIL.
  • go test -count=1 -v -run 'Wakeup|TestSumChatCostMicrosSince|TestCountChatUserNotificationsSince' ./coderd/database/forkdb/: 10 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/dbpurge/: 72 PASS, 0 FAIL.
  • go test -count=1 -v -run Fork ./coderd/database/dbauthz/: 834 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/migrations/chatdmig/: 63 PASS, 0 FAIL, 0 SKIP.
  • Negative controls: with the cache hit disabled in organizationSpend, TestWakeupCapStatements fails; with the purge call replaced by a no-op in purgeForkInTx, TestDeleteOldChatWakeupRunCounts fails. The source was restored after.

On f43a38f411 (the same Go tree as this head) I also ran, in a detached worktree under the flock:

  • go test -count=1 -v -run 'TestWakeupCap|TestWakeupRunCounts|TestWakeupCapStatements' ./coderd/x/chatd/: 22 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/forkdb/ ./coderd/database/dbpurge/: 135 PASS, 0 FAIL.

Behavior note: the organization spend cap now counts only chats with a delivered run in the last 24 hours, as the PR body states.

Not exercised: a live deploy, and the per-replica spend cache with more than one replica.

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.

chatd: wake-up caps run five aggregates per delivery, one over a day of organization chat messages

1 participant