Repository navigation
Description
Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.
Summary
Five fork panels keep themselves fresh with refetchInterval. The browser already holds a watch stream per user that carries chat events, and the state changes behind all five panels are committed by chatd, which can publish on that stream. One open chat page with a running subagent costs about 100 to 120 SQL statements per minute in polls; a visible running workflow raises that to about 190. With N tabs open the cost is N times that, whether or not anything changes.
Todos already do this correctly: a pubsub channel, a websocket, a 30 s resync (fork_chat_todos.go:97-181).
Evidence
| Query | File | Interval | Mounted | Statements per call |
|---|---|---|---|---|
| Descendant questions | forkChatQuestions.ts:16-25 |
5 s | every root chat page (ForkDescendantQuestionDock), with or without descendants |
3, one of them the JSONB rule over chat_messages |
| Workflow runs list | forkWorkflows.ts:11-21 |
5 s while a run is running, else 30 s | every root chat page (useForkWorkflowsTab), plus the panel |
6 |
| Workflow run detail | forkWorkflows.ts:27-37 |
3 s while running | each live run_workflow tool card and the open detail |
6, the run row is read 3 times |
| Family capacity | forkChatFamily.ts:11-19 |
5 s, 30 s after an error | sidebar row of an expanded family with a live descendant | up to 6 |
| Wake-ups | forkChatWakeups.ts:12-25 |
30 s | every chat page (ForkChatWakeupsPill) |
3 |
The comment in the first file states the cause: "A descendant's question raises no event on the root chat's stream."
The stream exists: chat:owner:{ownerID} carries ChatWatchEvent values (chatd.go:1572-1597), the kinds are listed at chats.go:2325-2349, and the frontend already switches on the kind to update caches (chats.ts:786-790).
Proposed design
One new watch event kind
// codersdk/fork_chat_watch.go
const ChatWatchEventKindForkChange ChatWatchEventKind = "fork_change"
// ForkChange names what changed, so the client invalidates only that.
type ChatWatchForkChange struct {
RootChatID uuid.UUID `json:"root_chat_id" format:"uuid"`
Facets []string `json:"facets"` // "questions", "wakeups", "workflows", "family"
}Carried in the existing ChatWatchEvent payload as an optional field. The payload is two UUIDs and a short list, far below the NOTIFY size limit.
Publish points (all after commit, on the existing owner channel)
| Facet | Published when | Where |
|---|---|---|
questions |
a question row is written or answered | recordDescendantQuestion, and the answer points of #300 |
wakeups |
a wake-up is created, paused, resumed, canceled, delivered or completed | createChatWakeup, the control handlers, the delivery transaction of #294 |
workflows |
a run or step row changes | the journal write functions in fork_workflow_journal.go (all writes are in that file) |
family |
a family member's capacity state changes | nothing new: the client already receives status_change for the member; treat it as family |
Frontend
- In the watch event handler in
site/src/api/queries/chats.ts, onfork_changeinvalidate the matching query keys forroot_chat_id(chatDescendantQuestionsKey, the workflow run keys,chatWakeups, the family capacity key). Onstatus_changeof a chat that has a parent, also invalidate the family capacity key of its root. - Remove the short
refetchIntervalvalues. Keep one fallback of 60 s on each query, and a refetch when the watch socket reconnects, because the stream is at most once. - The workflow run detail keeps no fast poll: step rows change only through journal writes, which publish.
Server side of the same polls
- The questions endpoint becomes an index read (chatd: descendant questions are polled every 2 s through a JSONB scan; store the answered state #300).
- The run list's step counts query aggregates every step of every run of the chat (
workflow_runs.sql:58-65); restrict it to the run IDs of the page (WHERE s.run_id = ANY($1)), and select only the columns the list item uses instead ofSELECT *with the full script.
Acceptance criteria
git grep -n 'refetchInterval' site/src/api/queries/fork*.tsshows only the 60 s fallbacks.- Vitest: delivering a
fork_changeevent with facetquestionsto the watch handler invalidates the questions query of that root and no other fork query. - A coderd test writes a question, creates a wake-up and journals a workflow step, and asserts one
fork_changeevent per write on the owner's watch stream with the right facet. - Manual check on the :3100 UAT server: with the network panel open on an idle chat page for 5 minutes, no request to
/chats/fork/other than the 60 s fallbacks. - Frontend deep review (
.claude/skills/frontend-deep-review) run on the change.
Activity
ibetitsmike commentedon Nov 18, 2026
Scheduled 2026-11-17 22:10Z: lead workstream on Claude Opus 5.5, high effort, branch mike/issue-304-watch-invalidate. It confirms each poll on current main first.
ibetitsmike commentedon Nov 18, 2026
Done in #386, merged 2026-11-18 13:15Z as 01326aa. Gates and test evidence: #386 (comment). Family capacity and active events wake-ups still poll at 30 s, as the PR body explains. The deploy is pending.