Repository navigation
Description
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
-
Add a step-scoped struct, built once at the top of
buildTurnEnvironmentby 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,materializePluginPacksandfilterPackResourcestake it instead of querying. -
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. -
Replace the ancestor walk of
chatWorktree(2 statements per level) with one recursive query overparent_chat_idjoined tochat_subagents, bounded at the same depth. -
Agent definition metadata: cache per
(owner, organization, project)in theServer, 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. -
Organization subagent limits: same cache pattern, invalidated by the limits PUT handler (
fork_subagent_limits.go:63-108). -
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_subagentsrows (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
Serverinstances sharing a pubsub). - A test with an agent that never answers shows the first model call of a turn starts within 2 s.
Activity
ibetitsmike commentedon Nov 15, 2026
Delegated 2026-11-15 to a workstream on Claude Opus 5.5 (high reasoning). Branch
mike/issue-299-*. Schedule in #311.ibetitsmike commentedon Nov 15, 2026
Done. #350 merged as 84ed040 at head cf6fd91. Per-chat fork facts are now read once per generation step. Gate evidence: #350 (comment)