Skip to content

perf(chatd): refetch fork panels on fork_change watch events - #386

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/issue-304-watch-invalidate
Nov 18, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/issue-304-watch-invalidate

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #304

What changed

chatd now publishes a fork_change event on the chat watch stream when state behind a fork panel changes, and the agents page refetches the panel the event names instead of polling it every few seconds.

Server (coderd/x/chatd/fork_panel_events.go, codersdk/fork_chat_watch.go)

  • New watch event kind fork_change. It reuses ChatWatchEvent and the existing pubsub path: pubsub.ChatWatchEventChannel(ownerID), i.e. chat:owner:{owner_id}. Chat is left empty; the payload is fork_change: {chat_id, root_chat_id, facets, workflow_run_id?}.
  • Facets and publish points, all after the write commits, from the code that owns the write:
    • questions: recordDescendantQuestion.
    • wakeups: wake-up created (schedule_wakeup, wait_for_events, /loop), paused, resumed or canceled through the API, delivery sent, failed or finished. An events poll that finds nothing publishes nothing.
    • workflows: every journal write in fork_workflow_journal.go that changed a row (run insert, status, phases, claim, step insert, update, report, reuse, close of open steps). A write that matched no row publishes nothing.
    • workflow_logs: the journal's log write, at most every 2 s per run.
  • Bulk writes no single chat owns (stale run sweep, run release, archive cascade of wake-ups) publish nothing; the resync covers them.
  • coderd/x/plugins/delivery.go ignores the new kind, so plugin event delivery is unchanged.
  • One hook line in chat_mutations.go. Prose in coderd/x/chatd/ARCHITECTURE.md under "Fork panel events".

Site (site/src/api/queries/forkChatWatch.ts, AgentsPageLayout.tsx)

  • The layout's existing watch socket hands each event to createForkChatWatch. A fork_change event invalidates only the keys its facets name, and returns early so it never reaches the chat list merge or cancels a list refetch.
  • status_change, created and deleted of a descendant invalidate the family capacity and questions of its root (the issue's "treat it as family"). deleted also invalidates the chat's wake-ups.
  • Each key refetches at most once per 3 s (leading and trailing; a key's timer is dropped once its gap ends quietly). invalidateQueries refetches only mounted queries.
  • Every time the socket opens, all fork panel root keys are invalidated, because the stream is at most once.
  • Polls: questions, wake-ups, runs list and a running run's detail use forkPanelResyncInterval = 60 s. Family capacity stays at 30 s (see below). Wake-ups poll at 30 s, as on main, while an active events wake-up is listed: its empty polls move next_run_at and publish nothing.
  • The embedded agent page (/agents/:agentId/embed) renders outside AgentsPageLayout, so it mounts useForkChatWatch, which opens its own watch socket for the fork events once signed in.

Statements per minute

Per-call costs from #304: questions 3, runs list 6, run detail 6, family up to 6, wake-ups 3.

Page state Before (#304) After, no changes After, event-driven on top
Idle root chat page about 54 (questions 36, list 12, wake-ups 6) 12 (3 + 6 + 3) none
Running subagent, family expanded about 100 to 120 24 (adds family 12) one family + questions refetch per descendant status change, at most one per key per 3 s
Visible running workflow about 190 30 (adds detail 6) list: one refetch per run or step write, at most 20/min (120 statements). Detail: at most 20/min (120 statements) while logs stream every 2 s and the detail or a live tool card is mounted

With an active events wake-up, wake-ups stay at their main cost (6 per minute instead of 3). Between changes an open page now costs 12 to 30 statements per minute instead of 54 to 190, per tab. The one case that does not shrink is the run detail while a step streams logs: it refetches at the 3 s throttle, about the old 3 s poll, but only while logs actually change. Moving logs onto a stream is out of scope here.

Choices

  • No experiment. This is a performance change behind the existing fork panels, which already sit behind their experiments. The new event is only published from those fork code paths.
  • Old clients: they get an extra event kind with an empty chat. The existing handler merges nothing for it. The await_chat toolsdk path matches events by chat ID, which an empty chat never matches.
  • Old server, new client: no events arrive; panels refresh on the 60 s resync and on socket open.
  • Authorization: events go only to the owner channel, which the watch endpoint subscribes for the caller's own user ID, so a user receives events only for their own chats. Another user who can read a chat (shared chat) sees its panels through the 60 s resync. No new endpoint or subscription is added.
  • Family capacity stays at 30 s, not 60 s (deviation from the issue's acceptance): admitting a subagent that was held for capacity, and freeing a slot in another family, raise no event the client sees, so a shorter resync keeps the badge honest. Every descendant status, create and delete event refetches it at once.
  • Questions retirement: an answered or dropped question raises no questions event. The client refetches the root's questions on every descendant status event, which covers the answer moving the child out of waiting.
  • PR site: show workflow args, step values, live logs and older runs #372 (run detail UI) uses the same chatWorkflowRunsKey(chatId) key. This PR changes only the intervals and key construction in forkWorkflows.ts; whichever lands second merges main.

Size

Outside generated files the diff is about 1,245 changed lines: about 520 of code and docs, 725 of tests (forkChatWatch.test.ts alone is 362). It is not stacked: fork-ci runs only for PRs based on main, and the server half alone would publish events nothing reads.

Out of scope

Tests

  • flock ~/.fork-go.lock go test ./coderd/x/chatd/... ./codersdk/ -count=1: pass. TestForkChangeEvents subscribes to the owner's watch channel and asserts one fork_change per write, with the right facet, chat, root and run, for a descendant question, a scheduled wake-up and its pause, and workflow journal writes (run insert, step insert, step update, logs, run status). A step update that matches no row publishes nothing. It fails with the publish calls removed.
  • flock ~/.fork-go.lock go test ./coderd/x/plugins/ -count=1: pass (delivery ignores fork_change).
  • pnpm exec vitest run --project unit src/pages/AgentsPage src/api/queries: 197 files, 3443 passed, 2 skipped.
    • forkChatWatch.test.ts: a questions event invalidates that root's questions query and no other fork query; each facet maps to its keys; throttle, dispose and open behavior; each panel query does not poll at the old interval and refetches at the resync (fails with a 5 s interval), including the 30 s wake-ups poll with an active events wake-up.
    • hooks/useForkChatWatch.test.tsx: the embed hook routes a fork_change event and the socket open to the panel queries, closes the socket on unmount, and opens none while disabled.
    • AgentEmbedPage/AgentEmbedPage.test.tsx: the embed route publishes a workflow_logs fork_change on the mocked socket and the run query is invalidated (fails without the useForkChatWatch line).
    • AgentsPageLayout.test.tsx: a fork_change event during an in-flight chat list refetch invalidates the wake-ups query and does not cancel the refetch (fails without the early return); socket open invalidates the panels (fails without onOpen).
  • pnpm check, pnpm lint:check, pnpm lint:oxlint, pnpm lint:types, pnpm lint:circular-deps: pass. pnpm lint:compiler fails on ForkHtmlAttachmentViewer.tsx: onFrameLoad and knip reports chatPinnedSkillsKey and excludedPluginPacksKey; both are on main and outside this diff.
  • make gen output is committed (apidoc, docs/reference/api, typesGenerated.ts).
  • Frontend deep review run on the change; its findings were applied (root key constants, unexported helpers, behavioral polling tests).

Not verified

  • The manual check on the :3100 UAT server (network panel on an idle chat page for 5 minutes) was not run; this workstream has no access to that server.
  • Event delivery across replicas was not exercised beyond the pubsub used in the Go test.

Questions, wake-ups, workflow runs and workflow logs now publish a
fork_change event on the owner's chat watch channel at the commit
points chatd owns. The site can refetch a panel on the event instead
of polling it every few seconds. Refs #304.
The agents layout routes fork_change events from the chat watch socket
to the panel queries they name, throttled per query, and refetches
every panel when the socket opens. The panel polls become a 60 s
resync, and the family capacity poll a 30 s one. Closes #304.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T09:56:12.825043Z 5e8df1c 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: d0e7f10175

ℹ️ 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_wakeups.go Resolved
Comment thread site/src/api/queries/forkWorkflows.ts Resolved
@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: d0e7f10175

ℹ️ 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 site/src/api/queries/forkChatFamily.ts Resolved
The embed route renders outside AgentsPageLayout, so it now opens its
own chat watch socket for fork_change events. The wake-ups query polls
at 30 s while an events wake-up is active, since its empty polls move
next_run_at without an event. Addresses the Codex review of #386.
@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: d7439fadec

ℹ️ 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 site/src/api/queries/forkChatWatch.ts 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. Can't wait for the next one!

Reviewed commit: 5e8df1c961

ℹ️ 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 01326aa into main Nov 18, 2026
11 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-304-watch-invalidate branch November 18, 2026 13:14
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 5e8df1c96183041f8514c51ac60ec2257d192b74 as 01326aa, on main 0e26fd0755f61f95304dad17a05c4d228f1f8f2e, 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 44277968462 passed on this head.
  3. Codex receipt for this head: perf(chatd): refetch fork panels on fork_change watch events #386 (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 5e8df1c961 (detached worktree, under flock ~/.fork-go.lock):

  • go test -count=1 -race -v -run 'TestForkChangeEvents|Workflow|Wakeup|DescendantQuestion' ./coderd/x/chatd/: 340 PASS, 0 FAIL.
  • go test -count=1 ./coderd/x/chatd/... ./codersdk/ ./coderd/x/plugins/: 23 packages ok.
  • pnpm exec vitest run --project unit on forkChatWatch.test.ts, useForkChatWatch.test.tsx, AgentsPageLayout.test.tsx and AgentEmbedPage: 4 files, 76 tests passed.
  • Negative control: with the publish after a workflow run insert and after a descendant question removed, TestForkChangeEvents fails. The source was restored after it.

Review notes:

  • chatd publishes a fork_change event on the owner's existing chat watch stream after the commit behind each panel: descendant questions, wake-ups, workflow runs and steps, and workflow logs. The site invalidates the matching query keys, at most once per 3 s per panel, and keeps a slow resync instead of the old 3 s and 5 s polls.
  • Other readers of the watch stream ignore the new kind: plugin delivery returns early, and the toolsdk wait matches on the chat ID, which is empty in this event.
  • A user gets events only for chats they own, since the stream is per user. Other readers of a chat see changes at the next resync.
  • Family capacity and an active events wake-up still poll at 30 s, because some of their changes raise no event. The PR body says this.
  • The PR is about 520 lines of code and docs plus 725 lines of tests, over the 500-line guideline. It was not split, because the server half alone publishes events that nothing reads.
  • No new experiment: the change keeps behavior, and each publish sits in code that is already behind its fork experiment.

Not exercised: the network view of an idle chat page on a running server over several minutes, and the statement-per-minute effect, which the PR body estimates against the #304 numbers.

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.

site, coderd: five fork panels poll; publish one chat watch event and invalidate on it

1 participant