refactor(coderd/x/chatd): extract turn preparation into a deep TurnEnvironment module - #28757
Open
ibetitsmike wants to merge 4 commits into
Open
refactor(coderd/x/chatd): extract turn preparation into a deep TurnEnvironment module#28757ibetitsmike wants to merge 4 commits into
ibetitsmike wants to merge 4 commits into
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
βΉοΈ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with π while any review is running, comments if it has suggestions, and reacts with π once all reviews finish with no findings. |
Collaborator
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. π Reviewed commit: βΉοΈ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Chat turn preparation was smeared across generation_preparer.go (926 lines), chatd.go (turnWorkspaceContext plus tool policy and system prompt assembly, ~1,200 lines), and generation.go, which consumed a 25-field
generationPreparedbag with 8 correlated lookup maps and ~10 policy helpers onServer. Tool policy could only be tested by booting a full Server + database fixture, which is why ~950 lines of e2e tool-filtering tests existed.This extracts one deep module,
coderd/x/chatd/turn_environment.go:buildTurnEnvironment(ctx, server, input)returns a six-methodturnEnvironmentinterface (Turn,ModelConfig,Prompt,Toolset,CompactionConfig,Close) backed by cohesive turn/model/toolset state. Workspace agent connections, skills and instruction lookup, plan/explore/subagent tool policy, system prompt assembly, deferred MCP tool search setup, and dynamic tool wrapping are hidden behind it. generation.go consumes only the interface; the policy helpers moved offServer.Net diff: 12 files, +2,370/-3,414 (-1,044 lines) across production and tests.
Behavior
Behavior preserving by design: the generation decision still reads the user-visible message set unchanged while the provider prompt is built from the model-visible set, deferred MCP tool search is untouched semantically, tool admission/hook dispatch is untouched, and model-call resolution is wrapped, not moved. Remote dogfood UAT differentially compared this branch against main on the same deployment config: per-turn
tools[]inventories captured at the provider boundary were identical across normal/plan/explore/subagent/workspace/dynamic/deferred-MCP/inactive-filter scenarios, and the system prompt was byte-identical.Tests
TestActiveToolNamesForTurn,TestFilterExternalMCPConfigsForTurn,TestAllowedExploreToolNames,TestStopAfterPlanTools,TestStopAfterBehaviorTools, plus toolset metadata coverage).TestSubagentChatExcludesWorkspaceProvisioningToolsas the end-to-end smoke proving a turn still assembles.