Skip to content

chatd: fork hooks re-read the same per-chat facts several times in every generation step #299

Closed

Description

@ibetitsmike

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

Summary

buildTurnEnvironment runs once per generation step, and the fork hooks in it each look up the same per-chat facts on their own. With every fork experiment on, a root chat with a workspace issues about 10 extra SQL statements per step, and a subagent in a worktree 8 to 20 or more, for facts that do not change during the turn: which plugin packs are enabled, the chat's chat_subagents row, the agent definition lists, the organization's subagent limits.

Resolve each fact once per step and pass it down.

Evidence

All per step (generation.go:653):

Fact Read by Statements per read
Enabled plugin packs of the chat materializePluginPacks (turn_environment.go:466) and again by filterPackResources through resolveTurnWorkspaceContext (turn_environment.go:470, fork_plugin_gate.go:94-101); again on the agent catalog path (fork_agent_type_catalog.go:116, fork_agent_definitions.go:171) 4 to 5, plus 2 per ancestor chat (fork_plugin_gate.go:282-335)
chat_subagents row of the chat forkWorktreeAgent (turn_environment.go:468), withForkWorktreeBinding inside appendForkTools (turn_environment.go:743), subagentDepth, customAgentToolPolicyOfChat (turn_environment.go:746, fork_agent_tool_policy.go:230-258, which also re-parses the JSON policy) 1 each, 2 with the dbauthz pre-read (#292); the worktree walk repeats per ancestor level
Agent definition metadata forkSubagentDefinitions for the spawn_agent description (fork_agent_definitions.go:214-313) 3 list queries plus the owner subject load
Organization subagent limits subagentCanSpawn and forkSpawnAgentDescription (fork_subagents.go:425-441, fork_subagents.go:550-565) 1 each
Secret answers withSecretAnswers (turn_environment.go:993) 1

The only cache on this path is the file list of a pack commit (fork_plugin_gate.go:237-255).

Separately, materializePluginPacks dials the workspace agent inline in the prepare phase with a 10 s cap, and writes up to 2,000 files per pack with one RPC each (fork_pack_materializer.go:33-52). That happens only on the first step after a pack install or an agent rebuild, but that step stalls before the model call.

Proposed design

  1. Add a step-scoped struct, built once at the top of buildTurnEnvironment by one fork hook and passed to the other hooks:

    // forkStepFacts holds what the fork hooks of one generation step need,
    // read once.
    type forkStepFacts struct {
        subagent     *forkdb.ChatSubagent   // nil for a chat with no row
        toolPolicy   *customAgentToolPolicy // parsed once
        worktree     *subagentWorktree      // resolved once, ancestors walked once
        enabledPacks []forkdb.PluginPack
        limits       forkSubagentLimits
    }

    forkWorktreeAgent, withForkWorktreeBinding, customAgentToolPolicyOfChat, subagentCanSpawn, forkSpawnAgentDescription, materializePluginPacks and filterPackResources take it instead of querying.

  2. Replace the per-ancestor enablement loop with one query that takes the chat's ancestor IDs (chat_id = ANY($1)) for packs and one for exclusions.

  3. Replace the ancestor walk of chatWorktree (2 statements per level) with one recursive query over parent_chat_id joined to chat_subagents, bounded at the same depth.

  4. Agent definition metadata: cache per (owner, organization, project) in the Server, invalidated by a pubsub message published by the agent definition write handlers (coderd/fork_agent_definitions.go, fork_organization_agent_definitions.go, fork_project_agent_definitions.go), with a 60 s TTL as the fallback.

  5. Organization subagent limits: same cache pattern, invalidated by the limits PUT handler (fork_subagent_limits.go:63-108).

  6. Pack materialization: start it in a goroutine at turn start and let the first step wait for at most 1 s; later steps see the packs once the marker is written. The existing marker and retry logic stay.

Acceptance criteria

  • A store-call test of one step for a depth-2 subagent with worktree isolation, a tool policy and two enabled packs asserts at most: 1 read of chat_subagents rows (the recursive query), 2 pack enablement queries, 0 agent definition list queries on a warm cache, 1 limits read on a cold cache and 0 on a warm one.
  • Changing a pack enablement or an agent definition is visible on the next step (cache invalidation test with two Server instances sharing a pubsub).
  • A test with an agent that never answers shows the first model call of a turn starts within 2 s.

Activity

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

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Delegated 2026-11-15 to a workstream on Claude Opus 5.5 (high reasoning). Branch mike/issue-299-*. Schedule in #311.

  3. ibetitsmike commented on Nov 15, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    Done. #350 merged as 84ed040 at head cf6fd91. Per-chat fork facts are now read once per generation step. Gate evidence: #350 (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