Skip to content

perf(coderd/x/chatd): read a subagent's report from its newest messages - #367

Merged
ibetitsmike merged 19 commits into
mainfrom
mike/issue-302-wait-agent
Nov 17, 2026
Merged

ibetitsmike merged 19 commits into
mainfrom
mike/issue-302-wait-agent

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #302

This is the last PR of a stack of three for #302:

  1. perf(coderd/x/chatd): read family statuses in one statement for cascade, settle and spawn #365: items 1 to 3, the family reads of the interrupt cascade, the archive settle and spawn validation. Merged as 5e28a43.
  2. perf(coderd): read a chat family's descendants in one statement for the chat tree and list_agents #366: item 4, the chat tree and list_agents read a family's descendants in one statement. Merged as d5e4fc1.
  3. This PR (base main, with main merged in): item 5, wait_agent reads a subagent's report from its newest messages.

The issue deferred item 5 to #303. The lead asked for it in this stack, so it is here.

Problem

latestSubagentAssistantMessage in coderd/x/chatd/subagent_operations.go reads the whole visible history of the subagent with GetChatMessagesByChatID and scans it from the end for the newest assistant message with text. wait_agent, and every report that needs a subagent's last answer, pays for a full history read. A long subagent can have thousands of messages.

Change

  • New fork query GetChatReportMessages in coderd/database/forkdb/queries/chat_subagent_reports.sql. It returns the assistant messages of a chat that the user sees and that are not deleted, newest first, below an optional id bound, with a limit. It selects only id, content and content_version. The index idx_chat_messages_chat_role_id (chat_id, role, id DESC) WHERE deleted = false serves it.
  • dbauthz authorizes it as a read of the chat, like GetChatMessagesByChatID. dbmetrics wraps it.
  • forkLatestSubagentReport in coderd/x/chatd/fork_subagent_report.go reads one message first. It reads pages of 50 only when the newest assistant messages have no text, for example tool calls only. It parses and trims each message the same way as upstream.
  • One hook in latestSubagentAssistantMessage. A store without fork queries, such as the gomock stores in TestStoreSubagentReportSummary, keeps the upstream full read.

The result is the same string as before, so the change is not behind an experiment.

Tests

TestForkLatestSubagentReport (coderd/x/chatd/fork_subagent_report_internal_test.go) counts the pages, the rows and the full history reads. Each case also runs the upstream full read and requires the same report.

  • NewestMessage: 91 messages, newest is the report. 1 page, 1 row, 0 full reads.
  • PastMessagesWithoutReport: the report sits under 60 tool-call-only messages, a model-only message, a deleted message, a user message and a blank assistant message. 3 pages, 63 rows, 0 full reads.
  • NoReport and NoMessages: empty report with 2 and 1 pages.

Negative control: with the hook removed every case fails with {pages:0, rows:0, fullReads:1}.

Commands run, all with flock ~/.fork-go.lock:

  • go test ./coderd/x/chatd/... ./coderd/database/forkdb/... -count=1: PASS.
  • go test ./coderd/database/dbauthz/ ./coderd/database/dbmetrics/ -count=1: PASS, including TestForkQuerier/GetChatReportMessages.
  • golangci-lint run --fix=false on the changed packages: no findings in the changed files.
  • gofmt -l: clean.

Not verified

  • No live server run of wait_agent.
  • Full make lint and make gen were not run. The fork queries were generated with coderd/database/forkdb/generate.sh.

@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 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-16T23:44:58.872877Z 4bee9c4 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.

@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. Already looking forward to the next diff.

Reviewed commit: f0e5fadfdb

ℹ️ 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\nMerged the restacked #366 head 1cd7c5c into this branch.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d7178f5d50

ℹ️ 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: 0976e9c8ed

ℹ️ 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. Already looking forward to the next diff.

Reviewed commit: 4909bb5106

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

Copy link
Copy Markdown

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

Reviewed commit: 9aca919460

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

Copy link
Copy Markdown

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

Reviewed commit: 7981539494

ℹ️ 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-302-chat-tree to main November 16, 2026 23:39
…gent

# Conflicts:
#	coderd/database/dbmetrics/fork_forkdb.go
@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. 🎉

Reviewed commit: 4bee9c419e

ℹ️ 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 57bea8f into main Nov 17, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-302-wait-agent branch November 17, 2026 00:42
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 4bee9c419ee97d1a813cdd24fe0d3ca09249cdd3 as 57bea8f, on main d5e4fc17d1e6bfe9a59211d9aa9c62688ae619ef, 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 43981534698 passed on this head.
  3. Codex receipt for this head: perf(coderd/x/chatd): read a subagent's report from its newest messages #367 (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 4bee9c419e (detached worktree, under flock ~/.fork-go.lock, Postgres 16 at 127.0.0.1:5432):

  • go test -count=1 -v ./coderd/x/chatd/: 2641 PASS, 0 FAIL.
  • go test -count=1 -v ./coderd/database/forkdb/... ./coderd/database/dbmetrics/: 70 PASS, 0 FAIL.
  • go test -count=1 -v -run 'TestForkQuerier|TestMethodTestSuite' ./coderd/database/dbauthz/: 5448 PASS, 0 FAIL.
  • Negative control: with forkLatestSubagentReport returning the newest assistant message even when it has no text, TestForkLatestSubagentReport fails (NoReport, PastMessagesWithoutReport). The source was restored after the control. The workstream also showed that removing the hook makes all four cases fail.

Review notes: the change is a read path only. latestSubagentAssistantMessage asks the fork query first, reading the newest message, then pages of 50 older ones, until it finds a visible assistant message with text. It keeps upstream's full read for a store without fork queries. The upstream file gets one 3-line hook. No experiment: the result is the same as upstream's, only with fewer rows read.

Not exercised: a running server and wait_agent against real subagents.

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: interrupt, archive, spawn and chat tree paths read family members one at a time

1 participant