Skip to content

chatd: interrupt, archive, spawn and chat tree paths read family members one at a time #302

Closed

Description

@ibetitsmike

Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.

Summary

Four family operations read or write one member at a time where one statement would do, and one validates the same limits three times. None is slow for a family of five. Each grows with family size, and two of them run on every interrupt and every chat list request.

Evidence and fixes

1. Every interrupt walks the subtree, then reads each descendant

chat_mutations.go:902 calls forkInterruptDescendants at the start of every InterruptChat, including for a leaf.

fork_cascade.go:54-80: GetChatSubtreeIDs (recursive CTE), then GetChatByID per descendant, then a full InterruptChat transaction per active descendant.

Fix: change GetChatSubtreeIDs to return id, status, archived and filter in SQL to active statuses (WHERE status IN ('running', 'interrupting', 'requires_action'), the set of isActiveChatStatus at fork_cascade.go:33-40). A leaf or an idle family then costs one statement and zero follow-up reads.

2. Archive settle re-reads every member on every wake

fork_cascade.go:132-150: each loop pass calls InterruptChat(top) (which repeats item 1) and activeChatIDs.

fork_cascade.go:153-165 activeChatIDs: one GetChatByID per family member. The loop wakes on every chat:update of every active member, so the total is (transitions) x (members) reads over up to 30 s.

Fix: one statement, SELECT id FROM chats WHERE id = ANY($1) AND status IN (...). Re-interrupt only the members that query returns in requires_action, not the whole cascade.

3. Spawn validates the same limits three times

subagent_operations.go:211 and subagent_operations.go:223: forkValidateSpawnParent before and after reloading the parent. Each runs the limits read, the depth read and CountLiveSubagentsInFamily (fork_subagents.go:73-89).

fork_subagents.go:361-388: the creation transaction runs all three again, then takes the root row lock and the family advisory lock.

Fix: keep the validation inside the transaction, which is the one that decides, and one validation before it for the early error message. Delete the call at line 211: the parent is reloaded two lines later and validated again.

4. The chat tree is fetched one level per query, for the chat list and for a single chat

fork_chat_tree.go:23-63 forkEmbeddedChildChats: one GetChildChatsByParentIDs per depth level, called from GET /chats and GET /chats/{chat} (exp_chats.go:584, exp_chats.go:1898). listDescendantChats does the same for list_agents (fork_list_agents.go:148-172). Each level recomputes has_unread per row.

Every descendant carries root_chat_id, and idx_chats_root_chat_id exists.

Fix: one fork query GetDescendantChatsByRootIDs(root_ids uuid[], archived) with the same row shape as GetChildChatsByParentIDs, selecting WHERE root_chat_id = ANY($1). Keep the authorization post-filter, and apply "a hidden chat hides its subtree" in Go over the returned rows. list_agents uses the same query and filters to the caller's subtree in Go.

5. wait_agent reads the child's whole history to get its last message

subagent_operations.go:629 latestSubagentAssistantMessage runs GetChatMessagesByChatID with no limit when the child is done. The workflow step path then scans the same history a second time (fork_workflow.go:820). This function is upstream code; the fork only needs the fix for its own second scan, which is in #303. Listed here so the reader does not look for it.

Acceptance criteria

  • Store-call tests:
    • interrupting a leaf chat issues 1 fork statement;
    • interrupting a root with 20 idle descendants issues 1 fork statement and no GetChatByID for descendants;
    • one settle pass over a 20-member family issues 1 status statement;
    • spawn_agent issues the limits, depth and live-count reads at most twice;
    • GET /chats/{chat} for a root with a 3-level family issues 1 descendant query.
  • The existing tree, archive and interrupt cascade tests pass, including "a descendant hidden by authorization hides its subtree".

Activity

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Started: workstream chat ee970415 (Opus 5.5, high) on branch mike/issue-302-family-reads, after #359 (closes #301) merged. It reports back to the lead for verification and merge.

  3. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Items 1 to 3 landed in #365 (merged as 5e28a43): the interrupt cascade reads descendant statuses from the subtree query, the archive settle pass reads the family in two statements per wake and rereads the subtree, and the spawn path validates the parent once. Item 4 (#366) and item 5 (#367) follow, one merge per deploy.

  4. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Item 4 landed: PR #366 merged as d5e4fc1 at 23:39:11Z. The chat tree and list_agents now read a family's descendants in one recursive statement (at most 64 levels), and families whose root was deleted keep their deeper chats. Gate evidence: #366 (comment). Item 5 (#367) restacks onto main next.

  5. ibetitsmike commented on Nov 17, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Item 5 landed: PR #367 merged as 57bea8f at 00:42:36Z. A subagent report is now read from the newest messages instead of the whole history. With #365, #366 and #367 merged, all five items of this issue are done. Gate evidence: #367 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions