Skip to content

perf(chatd): read fork per-chat facts once per generation step - #350

Merged
ibetitsmike merged 7 commits into
mainfrom
mike/issue-299-step-facts
Nov 15, 2026
Merged

ibetitsmike merged 7 commits into
mainfrom
mike/issue-299-step-facts

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #299

What changed

buildTurnEnvironment installs per-step facts on its context (fork_step_facts.go) and seals them when it returns. While they are live, the fork hooks of the step read each per-chat fact once:

  • the chatd.chat_subagents row of the chat and of each ancestor the worktree walk visits, shared by chatWorktree, the depth check of subagentCanSpawn (stepSubagentDepth) and customAgentToolPolicyOfChat;
  • the chat's worktree, shared by forkWorktreeAgent and withForkWorktreeBinding;
  • the organization's subagent limits, shared by subagentCanSpawn and the spawn_agent description (stepSubagentLimits);
  • the chat's enabled plugin packs, shared by materializePluginPacks and every filterPackResources call of the step.

The upstream file gets one hook line in turn_environment.go.

Which value wins

The value read at the first lookup in a step wins for the rest of that step. The next step reads the new value. A failed lookup is not kept, and pack enablement is kept only when every pack lookup succeeded. Facts of another chat, and every read outside buildTurnEnvironment, read live. Tool calls, spawn and capacity admission run after the environment is built, when the facts are sealed, so subagentDepth and subagentLimits keep reading inside the spawn and admission transactions. Every hook reads with the step's context, so dbauthz checks are unchanged. TestForkStepFactsFirstReadWins covers this rule.

ARCHITECTURE.md gets a "Fork step facts" paragraph next to the "Fork context" note.

Findings from the issue

Checked on main at b4340bd:

  • Fixed here: chat_subagents rows (6 reads per step), organization limits (2), plugin pack enablement (4 runs, each with its ancestor walk).
  • Already once per step, so dropped: agent definition metadata, secret answers, token budget row, tool policy JSON parse.
  • Left out of scope: the cross-step caches with pubsub invalidation, the recursive and ANY($1) queries, async pack materialization, the tokenBudgetChatEligible read in syncForkContext, pack cleanup's enabledPacks for other chats, and the upstream ListChatContextResourcesByChatID reads.

Store calls of one step

TestForkStepFactsReadOnce counts store calls through dbmetrics for the second step of a depth-2 shared subagent under a worktree child, with a tool policy and two packs enabled on the root.

Query main this PR
GetChatSubagentByChatID 6 2
GetChatOrganizationSubagentLimits 2 1
GetPluginPacksByChatID 12 3
GetExcludedPluginPacksByChatID 12 3
GetChatByID 16 6

On main the test fails with GetChatSubagentByChatID: expected 2, actual 6.

Tests

  • go test ./coderd/x/chatd/... -count=1 -v: every package ok, 6276 passed, 0 failed, 4 skipped (provider round trip tests that need keys).
  • go test ./coderd/x/chatd/ -run 'TestForkStepFacts|Isolation|Worktree|ToolPolicy|NestedSubagent|SpawnAgent|PluginPack|Pack' -count=1: ok.
  • golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/: 0 issues. paralleltestctx and intxcheck on ./coderd/x/chatd/: clean.

Not verified: a live server run with real subagents.

buildTurnEnvironment now installs per-step facts on its context and seals
them when it returns. While they are live, the fork hooks share one read of
the chat_subagents rows, the chat's worktree, the organization's subagent
limits and the chat's enabled plugin packs. Tool calls, spawn and admission
run after the step is built and keep reading live, inside their
transactions.

Closes #299
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T21:21:33.571613Z cf6fd91 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: 7648182aaa

ℹ️ 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/ARCHITECTURE.md Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

The ARCHITECTURE.md paragraph is written in full on purpose: the maintainer's brief for #299 overrides the AGENTS.md TODO rule for this PR. Please review the code changes.

@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: 7648182aaa

ℹ️ 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_plugin_gate.go Resolved
…or lookup

The project and ancestor helpers report a failed lookup only by logging it,
so pack enablement now watches its logger for a warning and keeps the packs
for the step only when none was logged.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

The ARCHITECTURE.md paragraph is written in full on purpose: the maintainer's brief for #299 overrides the AGENTS.md TODO rule for this PR.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 20d19847ae

ℹ️ 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".

…ment explicitly

Replace the logger sink that detected failed lookups with returned errors.
The ancestor walk returns its error through walkAncestorChatIDs, and pack
enablement resolves the chat project with packProjectID, a fork-side copy of
resolveUsableChatProjectID that returns a failed lookup. Add a test where only
the project or ancestor lookup fails while the pack lookups succeed.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

Head ea4ebe9 replaces the logger sink that pack enablement used to detect failed project and ancestor lookups with returned errors (walkAncestorChatIDs, packProjectID), and adds a test where only those lookups fail. ARCHITECTURE.md is written in full per the issue brief, which overrides the TODO rule.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: ea4ebe9cc2

ℹ️ 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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

Head 8914077 merges origin/main (05bc672, #338) into ea4ebe9 with no conflicts.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 8914077ec7

ℹ️ 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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

Head ba67e4b adds a parity test that runs packProjectID and the upstream resolveUsableChatProjectID over the same cases and requires the same project.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: ba67e4b184

ℹ️ 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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

Head cf6fd91 merges origin/main (344810f, #345) into ba67e4b with no conflicts.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: cf6fd91b8e

ℹ️ 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 84ed040 into main Nov 15, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-299-step-facts branch November 15, 2026 21:54
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head cf6fd91b8e627e07ac2e8ed41c1d74e5bcc1997a as 84ed040, on main 344810f6783e302e38e54fece005105390eaea20, 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 43761312082 passed on this head.
  3. Codex receipt for this head: perf(chatd): read fork per-chat facts once per generation step #350 (comment) ("Didn't find any major issues").
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. Tests I ran on this head in a detached worktree, under flock ~/.fork-go.lock:
    • go test -count=1 -v -run 'TestForkStepFacts|Isolation|Worktree|ToolPolicy|NestedSubagent|SpawnAgent|PluginPack|Pack' ./coderd/x/chatd/: 82 top-level PASS, 0 FAIL.
    • Negative control: with stepFacts forced to return nil, TestForkStepFactsReadOnce, TestForkStepFactsFirstReadWins and both subtests of TestForkStepFactsFailedProjectOrAncestorLookupNotKept failed. The source was restored after.
    • An Opus review of the delta found no P1.

Not exercised: a live chat run on the deployed server with the fork experiments on. The per-step cache is checked by unit tests only.

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.

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

1 participant