Skip to content

fix(coderd/x/chatd): keep the waker loop free of full slots and poke new wake-ups - #377

Merged
ibetitsmike merged 7 commits into
mainfrom
mike/issue-347-waker-followups
Nov 17, 2026
Merged

ibetitsmike merged 7 commits into
mainfrom
mike/issue-347-waker-followups

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #347

Follow-ups to the waker of fork-chat-wakeups (coderd/x/chatd/fork_waker.go). No new experiment, no migration.

Changes per item

  1. A read that does not fit the time left re-runs at once. Not changed here. feat: the workspace agent reports chat process events #376 (chatd, agent: the agent reports process exits and matching output instead of chatd dialing it every 15 s #295) removed the stalled process read, wakeupReadFits and withWakeupDeadline, so this item no longer applies. On main a delivery no longer reads pull requests, since the pull request poller does that off the delivery path. feat: the workspace agent reports chat process events #376 merged as ada0207617, and this branch is restacked on it by a merge of main.
  2. Full delivery slots stall the waker loop. Fixed. dispatch no longer waits for a slot. When all 8 slots are taken, it skips the due wake-up and queues its chat. While every slot is taken, a pass over a poked chat queues the chat too, with no query. The end of a delivery signals the loop, and the loop gives each free slot to the queued chat that waited longest. The slot and the queue change under the same lock, so a skipped wake-up is never lost. A freed slot costs about one pass and one query, however many chats wait. A full pass still runs while the slots are taken, so the timer stays right.
  3. Children-only events wake-ups rely on pokes and the 60 s sweep. No code change needed. Every path that makes a watched child done publishes on the wrapped pubsub or pokes the child directly. The new tests cover each one:
    • the end of the child's turn (existing TestWakerDeliversWhenChildFinishes),
    • the child's own events wake-up completing at its expiry (the dispatch poke),
    • a control canceling the child's wake-up (SetChatWakeupStatus pokes),
    • an answer to the child's question (SendMessage starts a turn, and the end of that turn publishes),
    • archive_agent archiving the child (the publish buffer of setSubtreeArchived).
  4. A wake-up created while the chat is idle does not poke. Fixed. schedule_wakeup and wait_for_events poke the chat after the create commit. /loop creates its wake-up inside the message send, and that commit publishes. No API route creates wake-ups.
  5. Nits.
    • quartz for the delivery deadline: not changed. feat: the workspace agent reports chat process events #376 removed withWakeupDeadline and wakeupReadFits, so this nit no longer applies.
    • The one-shot run: now safe to run again. When a run returns, it resets started, the timer, the pending pokes and the skipped chats. A second call while a run is active still returns at once. The doc comment says so.

ARCHITECTURE.md ("Wake-up delivery") now describes the tool pokes, the skip of a full slot and the poke after it, the child-done paths, and the second run.

Tests

New tests in coderd/x/chatd/fork_waker_internal_test.go:

Item Test Control
2 TestWakerFullSlotsDoNotStallTheLoop/SlotsFull: 8 slow deliveries hold every slot. The first pass still ends and arms the timer for a later wake-up. The skipped wake-up is delivered once a slow delivery ends. /SlotLeft: 7 slow deliveries, so the plain wake-up is delivered at once.
2 TestWakerFreedSlotCostsOneList: 8 deliveries hold every slot and 20 chats wait. When one slot frees, the chat skipped first is delivered. The other 19 stay queued in order, no chat is poked, and the run costs 2 lists in total. Fail-before: with the full-slot check off, 21 lists.
3 TestWakerChildDonePathsDeliver/{ChildWakeupExpires,ChildWakeupCanceled,QuestionAnswered,ChildArchived}/Pokes: the idle parent's wake-up is delivered right after the change. /NoPokes: with pokes cut off, nothing is delivered until the next full pass of the clock.
4 TestWakerScheduleWakeupArmsTimer/Pokes: the tool call arms the timer for 120 s at once. TestWakerWaitForEventsDeliversDoneChild/Pokes: a wait on a child that is already done delivers with no clock advance. /NoPokes: the timer is armed, or the wake-up delivered, only at the sweep.
2 TestWakerQueueBeforeOtherChats/{FreshPoke,FullPass}: one slot is free and chat A waits in the queue. Chat B is poked in the same drain, or a full pass lists B first. A gets the slot and B waits behind it. Fail-before: FreshPoke fails with the drain of ef8a530111 (B gets the slot); FullPass fails without the queue check in dispatch.
5b TestWakerRunsAgain: a second run while one is active returns. A new run after the first returned delivers a new wake-up. n/a

startWaker now gives the server a mock clock of its own, set to the waker's time, so the tools create wake-ups at the test's time. The server's tickers stay off the waker's clock.

Commands run on head 7f9bf6c7df (restacked on ada0207617, plus the queue order fixes) (all under flock ~/.fork-go.lock, cold build):

  • go test ./coderd/x/chatd/ -run 'TestWakerFullSlots|TestWakerFreedSlot|TestWakerChildDonePaths|TestWakerScheduleWakeupArmsTimer|TestWakerWaitForEventsDeliversDoneChild|TestWakerRunsAgain' -count=20 -race -v (with TestWakerQueueBeforeOtherChats added to the pattern): PASS: 540 PASS lines (tests and subtests), 0 FAIL.
  • go test ./coderd/x/chatd/ -run 'TestWaker|TestScheduleWakeup|TestWaitForEvents|TestProcess|TestPullRequest|TestWakeup|TestArchiveAgent|TestSecret|TestDescendantQuestion' -count=1 -race: PASS, 219 PASS lines, 0 FAIL.
  • go test ./coderd/x/chatd/... -count=1: PASS, 21 packages ok.
  • golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/...: 0 issues. paralleltestctx and intxcheck on the package: clean.

Fail-before checks (each fix reverted alone, then restored):

  • Without the two tool pokes: TestWakerScheduleWakeupArmsTimer/Pokes and TestWakerWaitForEventsDeliversDoneChild/Pokes FAIL (timeout). The NoPokes controls pass.
  • With the old blocking slot acquire: TestWakerFullSlotsDoNotStallTheLoop/SlotsFull FAILS (the pass never arms the timer). /SlotLeft passes.
  • Without the reset of started: TestWakerRunsAgain FAILS.
  • With the full-slot check in drain off: TestWakerFreedSlotCostsOneList FAILS (21 lists, expected 2).
  • With the drain of ef8a530111, which passed poked chats before the queue: the fresh poke case FAILS (the poked chat gets the slot).
  • Without the queue check in dispatch: TestWakerQueueBeforeOtherChats/FullPass FAILS (the chat the full pass lists first gets the slot).
  • Item 3 needs no fix, so there is no fail-before run. Its tests guard the existing behavior, and the NoPokes control shows that each delivery depends on a poke.

An earlier Codex finding, the shared empty skipped map, no longer applies: the end of a delivery does not read the skipped chats any more. The queue order and the no-poke assertions have no fail-before run, because the older code has no queue to inspect. ChildWakeupExpires/NoPokes now waits until the child's wake-up completes before it checks the parent.

Not exercised: a real multi-replica deployment, and a real slow delivery holding the slots. The tests use a stub delivery that blocks. The old shared-map race was not reproduced under -race.

…new wake-ups

A pass that finds every delivery slot taken now skips the wake-up and
records its chat, and the next delivery to end pokes it, so slow
deliveries no longer stall pokes, the timer and the sweep.

schedule_wakeup and wait_for_events poke the waker after the commit of
a new wake-up. The waker can run again after its run returned.

Tests cover full slots, each path that makes a watched child done, the
two tool pokes and a second run, each with a control without pokes or
with a free slot.

Refs #347
@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 17, 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-17T08:49:40.104252Z 7f9bf6c 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: 5d44c6957e

ℹ️ 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_waker.go Resolved
Comment thread coderd/x/chatd/fork_waker.go Resolved
A freed slot pokes every skipped chat. Each poked chat ran its own due
query, so one freed slot cost one query per waiting chat. A pass over a
poked chat now records the chat as skipped, with no query, while every
slot is taken. A full pass still runs.

The end of a delivery takes the skipped set only when it holds chats,
so it never ranges the map that dispatch writes after the unlock.

Refs #347
@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: b7c29874d2

ℹ️ 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_waker.go Resolved
The end of a delivery poked every skipped chat, and the loop then put
all but one of them back, so a large backlog cost quadratic map writes
and pokes. The skipped chats now wait in a queue. The end of a delivery
only signals the loop, and the loop gives each free slot to the chat
that waited longest.

The ChildWakeupExpires control now waits until the child's wake-up
completes before it checks that the parent got nothing.

Refs #347
@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. What shall we delve into next?

Reviewed commit: b384574695

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

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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Restacked on main ada0207617 (#376) by a merge, head ef8a530111. Items 1 and 5a stay dropped. @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: ef8a530111

ℹ️ 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_waker.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Fixed the P2 on queue order in 2a2371e9de. @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: 2a2371e9de

ℹ️ 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_waker.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_waker.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Fixed the full-pass queue order P2 in 7f9bf6c7df. Declined the doc and empty-probe P2s with reasons in their threads. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 7f9bf6c7df

ℹ️ 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 1a19c25 into main Nov 17, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-347-waker-followups branch November 17, 2026 09:03
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 7f9bf6c7df9b31b6b58ec2a69c2bfadaa2226581 as 1a19c25, on main ada02076171afabe841b7fb4bd25a02684ec9b25, 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 44049401446 passed on this head.
  3. Codex receipt for this head: fix(coderd/x/chatd): keep the waker loop free of full slots and poke new wake-ups #377 (comment) ("Didn't find any major issues").
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

Tests I ran on 7f9bf6c7df (detached worktree, under flock ~/.fork-go.lock, cold build):

  • go test -count=1 -race -v -run TestWaker ./coderd/x/chatd/: 36 PASS, 0 FAIL.
  • go test -count=10 -race -v -run 'TestWakerFullSlotsDoNotStallTheLoop|TestWakerFreedSlotCostsOneList|TestWakerQueueBeforeOtherChats' ./coderd/x/chatd/: 70 PASS, 0 FAIL.
  • go test -count=1 -race -v -run 'TestWaitForEvents|TestWakeup|TestScheduleWakeup' ./coderd/x/chatd/: 143 PASS, 0 FAIL.
  • go test -count=1 ./coderd/x/chatd/...: 21 packages ok.
  • Negative control: with the "only the chat served from the queue takes a slot" check in dispatch removed, TestWakerQueueBeforeOtherChats/FullPass fails. The source was restored after the control.

Review notes:

Not exercised:

  • wakers on more than one replica;
  • real slow deliveries that hold every slot (the tests use a blocking stub delivery).

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 waker: follow-ups from the #294 review (stalled reads, slot stall, poke gaps)

1 participant