Skip to content

refactor(coderd/x/chatd): extract turn preparation into a deep TurnEnvironment module - #28757

Open
ibetitsmike wants to merge 4 commits into
mainfrom
mike/chatd-turn-env
Open

refactor(coderd/x/chatd): extract turn preparation into a deep TurnEnvironment module#28757
ibetitsmike wants to merge 4 commits into
mainfrom
mike/chatd-turn-env

Conversation

@ibetitsmike

Copy link
Copy Markdown
Collaborator

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 generationPrepared bag with 8 correlated lookup maps and ~10 policy helpers on Server. 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-method turnEnvironment interface (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 off Server.

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

  • Deleted seven e2e tool-policy tests from chatd_test.go (~950 lines) that booted a full Server + DB fixture; their assertions are covered by direct table-driven unit tests on the module (TestActiveToolNamesForTurn, TestFilterExternalMCPConfigsForTurn, TestAllowedExploreToolNames, TestStopAfterPlanTools, TestStopAfterBehaviorTools, plus toolset metadata coverage).
  • Kept TestSubagentChatExcludesWorkspaceProvisioningTools as the end-to-end smoke proving a turn still assembles.
  • ARCHITECTURE.md gets a TODO(human) marker only, per repo guardrails.

Xum acted on behalf of @ibetitsmike: implementation, validation, remote dogfood UAT, and this PR were produced by an AI agent under his direction.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 29, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
πŸ“ Code Review βœ… Completed 2026-08-29T12:38:55.569505Z 1add6c3 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.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. πŸš€

Reviewed commit: 1add6c35c6

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

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.

1 participant