Skip to content

perf(coderd): read a chat family's descendants in one statement for the chat tree and list_agents - #366

Merged
ibetitsmike merged 13 commits into
mainfrom
mike/issue-302-chat-tree
Nov 16, 2026
Merged

ibetitsmike merged 13 commits into
mainfrom
mike/issue-302-chat-tree

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 second PR of a stack. #365 (items 1 to 3) merged as 5e28a43, and this PR is now based on main with main merged in. It covers item 4 of the issue, the chat tree. The wait_agent item follows in #367, which is based on this one.

What changed

With fork-nested-subagents on, a chat family's descendants are read in one statement instead of one query per level.

  • New forkdb query GetDescendantChatsByRootIDs in coderd/database/forkdb/queries/chat_tree.sql. It walks parent_chat_id down from the given chats with a recursive CTE that stops at the caller's max_depth (capped at 64, like GetChatSubtreeIDs), served by idx_chats_parent_chat_id. It does not use root_chat_id, so a family whose root was deleted (both foreign keys are ON DELETE SET NULL) still returns its whole subtree. The archive filter applies at each step of the walk, so an archived chat's subtree is never walked. It has the row shape, has_unread and archive filter of upstream's GetChildChatsByParentIDs. dbauthz uses the same per-row post-filter as upstream (fetchWithPostFilter, read action), and dbmetrics observes it.
  • forkdb.DescendantsByLevel keeps only rows whose parent was reached from the roots, level by level, each level in the query's created_at DESC order. A chat that the archive filter or authorization drops therefore still hides its own subtree, and the output order is the one the per-level walk produced.
  • coderd/fork_chat_tree.go: forkEmbeddedChildChats (chat list and single-chat GET of a root) runs that one query. With the experiment off it is still upstream's single direct-children query.
  • coderd/x/chatd/fork_list_agents.go: listDescendantChats reads the listed chat's subtree in one statement that stops at the requested depth (one level for chat_id without recursive), still unarchived only. list_agents stays limited to the caller's subtree, since the chat_id descendant check is unchanged.
  • forkdb.ChatsExpanded.Chat() converts the new row to database.Chat. sqlc.yaml maps the chats_expanded enum and JSON columns to the upstream types.
  • coderd/x/chatd/ARCHITECTURE.md: the fork list_agents bullet (from feat(coderd/x/chatd): list a chat's descendants with list_agents #278) now says that chat_id and recursive read the listed chat's subtree in one GetDescendantChatsByRootIDs statement, following parent_chat_id through unarchived chats only. No upstream line changed.

Tests

  • coderd/fork_chat_tree_test.go TestForkChatTreeStatements: counts the queries through a wrapped store. For a root with child, grandchild and great-grandchild, the single-chat GET and the chat list each run one GetDescendantChatsByRootIDs and no GetChildChatsByParentIDs, and embed the family level by level. A grandchild the member cannot read hides its own child. An archived child hides its unarchived subtree from the unarchived list. After the root of a 4-level family is deleted, the GET of the promoted root runs one statement and embeds its child and grandchild.
  • coderd/x/chatd/fork_list_agents_internal_test.go TestForkListAgentsAfterRootDeleted: after the root of a 4-level family is deleted, recursive list_agents from the promoted root returns its child and the deepest chat.
  • coderd/x/chatd/fork_family_reads_internal_test.go TestForkListDescendantsStatements: list_agents over a root with 20 descendants on 2 levels, from the root and from a child, runs exactly one GetDescendantChatsByRootIDs. It also checks the subtree cut, the one-level listing and that an archived child hides its subtree.
  • coderd/database/forkdb/chat_tree_test.go: each row equals upstream's row for the same chat, the archive filter, a legacy child without root_chat_id, a family whose root was deleted, the max_depth bound, another family left out, DescendantsByLevel order, the hidden parent rule and the depth limit, and ChatsExpanded.Chat() copying every column.
  • coderd/database/dbauthz/fork_forkdb_test.go: the new query's authorization.

Negative controls: with the old per-level walk put back, TestForkChatTreeStatements fails with 4 GetChildChatsByParentIDs for the GET, and TestForkListDescendantsStatements fails with GetChildChatsByParentIDs: 3 instead of one GetDescendantChatsByRootIDs. With the earlier root_chat_id OR parent_chat_id predicate put back, the deleted-root cases fail in TestForkChatTreeStatements (only the promoted root's child is embedded), TestForkListAgentsAfterRootDeleted and TestGetDescendantChatsByRootIDs.

Plan: EXPLAIN ANALYZE on 100,000 chats uses idx_chats_parent_chat_id in both arms of the recursive CTE and chats_pkey for the join to chats_expanded.

Commands run:

  • go test ./coderd/ -run 'TestForkChat|TestChat' -count=1: ok
  • go test ./coderd/x/chatd/ ./coderd/database/forkdb/... -count=1: ok
  • go test ./coderd/database/dbauthz/ -run TestForkQuerier -count=1: ok
  • golangci-lint run --fix=false ./coderd/ ./coderd/x/chatd/ ./coderd/database/forkdb/...: nothing reported in the changed files

Not verified

  • No manual run against a live server or the sidebar UI.
  • make lint was not run. golangci-lint run --fix=false on the changed packages 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-16T22:58:03.145032Z 8011e07 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: 1c515d7261

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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@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: 778e0e567a

ℹ️ 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/ARCHITECTURE.md Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review\n\nThe architecture TODO is intentional per AGENTS.md line 75 and is already flagged for the author. Please review the rest of the change.

Base automatically changed from mike/issue-302-family-reads to main November 16, 2026 07:38
@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@codex review\n\nRestacked on main after #365 merged. Head 1cd7c5c drops the agent-authored architecture TODO. The note for the author is in the PR description.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 1cd7c5cc4b

ℹ️ 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 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: 238401c22f

ℹ️ 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\n\nThe chat tree finding is answered in its thread: both callers pass only root chats.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@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: dd835876e2

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

Deleting a family root sets root_chat_id and the promoted child's
parent_chat_id to NULL, so the root_chat_id predicate returned only one
level below the promoted root. GetDescendantChatsByRootIDs now walks
parent_chat_id with a recursive CTE, and list_agents starts the walk at
the listed chat.
@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: 194c5da5db

ℹ️ 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 one-level list_agents call walked up to 64 levels and discarded every
row below the direct children. GetDescendantChatsByRootIDs now takes a
max_depth, capped at 64.
@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: 849d942f9c

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

@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: 849d942f9c

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

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d9e54e8fbf

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

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 8011e078c2

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

fork-ci run 43974407705 on 8011e07 failed only in TestForkWorkflowLiveLogs/StopLinesBypassTheEchoSuppression (duplicate key value violates unique constraint "workflow_steps_pkey"), the known flake tracked in #373, which this PR does not touch. The token cannot rerun jobs, so I am closing and reopening this PR to start a fresh fork-ci run on the same head.

@ibetitsmike ibetitsmike reopened this Nov 16, 2026
@ibetitsmike
ibetitsmike merged commit d5e4fc1 into main Nov 16, 2026
17 of 18 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-302-chat-tree branch November 16, 2026 23:39
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 8011e078c21540e320b6f967fee55137df522387 as d5e4fc1, on main 6eaa6a28804df2fd5325e2fc2afcae3064366bbb, 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 43978412162 passed (run 43974407705 on the same head failed only in the test: fix the flaky fork tests that block autodeploy on main #373 flake TestForkWorkflowLiveLogs/StopLinesBypassTheEchoSuppression; the PR was closed and reopened for a fresh run) on this head.
  3. Codex receipt for this head: perf(coderd): read a chat family's descendants in one statement for the chat tree and list_agents #366 (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 8011e078c2 (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v ./coderd/x/chatd/: 2636 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/forkdb/...: 66 PASS, 0 FAIL.
  • go test -count=1 -v -run 'TestForkQuerier|TestMethodTestSuite' ./coderd/database/dbauthz/: 5443 PASS, 0 FAIL.
  • go test -count=1 -v -run 'TestForkChat|TestChat' ./coderd/: 418 PASS, 0 FAIL.
  • Negative control on 849d942 (the PR's code is the same on this head; the later commit only merges main): with the recursive query capped at depth 2 instead of 64, TestGetDescendantChatsByRootIDs and TestForkChatTreeStatements fail. On dd83587, keeping orphans in the per-level walk failed TestDescendantsByLevel and TestForkChatTreeStatements. The source was restored after each control.

Review notes: the Codex P2 about a family whose root was deleted was a real regression, because both chat foreign keys are ON DELETE SET NULL. It is fixed with a recursive CTE over parent_chat_id (at most 64 levels), and TestForkListAgentsAfterRootDeleted covers it. A chat_id call without recursive still reads only the direct children, as on main. The archive filter applies at each level, so an archived middle chat hides its subtree, as the per-level walk on main did.

Not exercised: a running server and the sidebar UI.

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