Skip to content

perf(coderd/x/chatd): read family statuses in one statement for cascade, settle and spawn - #365

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-302-family-reads
Nov 16, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-302-family-reads

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #302. This is the bottom PR of a stack. It covers items 1 to 3 of the issue. The chat tree and wait_agent items follow in PRs based on this one.

What changed

  1. Interrupt cascade. GetChatSubtreeIDs now also returns each descendant's status. forkInterruptDescendants skips idle descendants from that result and interrupts the active ones by ID. It no longer reads each descendant with GetChatByID. InterruptChat locks the chat by ID and checks the transition against the locked row, so the stale status in the row is only a filter. I kept the filter in Go so the other callers of the query (lockSubtreeForArchive, archive_agent) keep the same rows and order.
  2. Archive settle. settleChatsForArchive interrupts the top chat once, which cascades leaves first. Each later wake runs settleArchivePass: one GetChatSubtreeIDs for the current subtree of the top chat, so a child spawned during the settle is covered, and one GetChatsByIDs for the top chat and that subtree. It then interrupts every active member again, except an interrupting member that a worker still owns, which only waits for that worker. An unowned interrupting member is interrupted again, so the transition finishes it inline. Before, each wake ran the whole cascade again and then one GetChatByID per member.
  3. Spawn validation. loadSubagentSpawnParentChat no longer validates the parent before it reloads it. The check on the reloaded parent still fails fast before prompt hooks run. The check in the creation transaction stays the deciding one, with the lock order from perf(coderd/x/chatd): count subagent capacity in one statement and lock per family #359 unchanged: owner check share locks, root share lock, then the family admission lock. Spawn now reads the limits, the depth and the live count twice instead of three times.

Tests

New file coderd/x/chatd/fork_family_reads_internal_test.go:

  • TestForkInterruptCascadeStatements: a leaf interrupt runs one GetChatSubtreeIDs and no GetChatByID. A root with 20 idle descendants over 3 levels runs exactly one statement. With 2 active descendants, there is still no GetChatByID, and both reach interrupting.
  • TestForkArchiveSettlePassStatements: one pass over a root with 20 descendants runs exactly one GetChatSubtreeIDs and one GetChatsByIDs, both while a member is interrupting and once it is settled. An owned interrupting member is not interrupted again.
  • TestForkArchiveSettlePassCoversLateAndUnownedMembers: a pass interrupts a running grandchild that was not in the family when the settle began, and finishes an unowned interrupting child. It fails when the pass skips every interrupting member or does not reread the subtree.
  • TestForkSpawnValidationReads: a spawn_agent call from a depth 1 child, counted inside and outside the creation transaction, reads the limits, the depth and the live count twice each. The counter only counts reads from the validation, not from capacity admission or the orchestration prompt.

coderd/database/forkdb/chat_subtree_test.go now checks the status column.

Negative controls: I put back the old per-descendant read, the per-member status read and the pre-reload validation, and the new tests failed: GetChatByID: 20 for the idle family and for the active one, GetChatByID: 20 instead of one GetChatsByIDs for the settle pass, and 3 instead of 2 for spawn. The leaf case passes either way, since a leaf has no descendants to read.

Commands run:

  • go test ./coderd/x/chatd/ -count=1: ok
  • go test ./coderd/database/forkdb/ -count=1: ok
  • go test ./coderd/database/dbauthz/ -run 'Fork|Subtree' -count=1: ok

Not verified

  • No manual run against a live server.
  • make lint was not run. golangci-lint run --fix=false on coderd/x/chatd and coderd/database/forkdb reports nothing in the changed files.

@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-16T07:15:29.885202Z d4628e8 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: 3e0714cc14

ℹ️ 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_cascade.go Resolved
Comment thread coderd/x/chatd/fork_cascade.go Resolved
Comment thread coderd/x/chatd/fork_cascade.go 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: d4628e8c2a

ℹ️ 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 5e28a43 into main Nov 16, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-302-family-reads branch November 16, 2026 07:38
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head d4628e8c2a9036219667b269b4c0f473c5434b76 as 5e28a43, on main d4b15393b533da2bdba371b391634f9a3add4027, 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 43822077260 passed on this head.
  3. Codex receipt for this head: perf(coderd/x/chatd): read family statuses in one statement for cascade, settle and spawn #365 (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 d4628e8c2a (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v ./coderd/x/chatd/: 2567 PASS, 0 FAIL. This includes TestForkInterruptCascadeStatements, TestForkArchiveSettlePassStatements, TestForkArchiveSettlePassCoversLateAndUnownedMembers, TestForkSpawnValidationReads and TestNestedInterruptCascade.
  • go test -count=1 -v -run Subtree ./coderd/database/forkdb/: TestGetChatSubtreeIDs PASS, 0 FAIL.
  • Negative control 1: with the per-descendant GetChatByID restored in forkInterruptDescendants and the duplicate forkValidateSpawnParent call before the reload restored, TestForkInterruptCascadeStatements (IdleFamily, ActiveDescendants) and TestForkSpawnValidationReads fail. The Leaf case still passes, as it has no descendants.
  • Negative control 2: with the settle pass skipping every interrupting member (not only owned ones), TestForkArchiveSettlePassCoversLateAndUnownedMembers fails. The source was restored after both controls.

Review notes: the cascade now passes database.Chat{ID: row.ID} to InterruptChat. I checked that InterruptChat uses only chat.ID before updateChat locks the row by ID, and the cascade context stops a second subtree walk. The removed spawn validation ran on the unreloaded parent; the same check still runs on the reloaded parent.

Not exercised: a running server or the UI; archive settle timing under real worker leases.

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