From 5ccbef037bc9fba3d29c8682bb669abed32f3327 Mon Sep 17 00:00:00 2001 From: Michael Suchacz <203725896+ibetitsmike@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:44:13 +0200 Subject: [PATCH] refactor(coderd/x/chatd): remove the no-op allowedBehaviorToolNames filter (#30864) Removes `allowedBehaviorToolNames`, `allToolNames`, and `TestAllowedBehaviorToolNames`, and the active-name check they fed in `appendDynamicTools`. The check never skipped a tool. `appendDynamicTools` returns early for Explore mode and plan mode, so at the check, `allowedBehaviorToolNames` always returned every tool name. A dynamic tool that shares a built-in tool's name is still dropped with a warning. Validation: `go test ./coderd/x/chatd/ -run 'Dynamic|AllowedExplore|StopAfterPlan'` passes, and the pre-commit hook (build, `lint/go`, `lint/go/new-code`) passed. ## Stack This stack splits #28757, as requested in review. It moves the code that prepares a chat turn into one file, `turn_environment.go`, and moves the tool policy tests from full turns to a table test. chatd behaves as on `main`, except for the debug log in #30865. 1. #30864 Remove the no-op `allowedBehaviorToolNames` filter 2. #30865 Log workspace agent lookup errors in turn preparation 3. #30866 Rename `prepareGeneration` to `buildTurnEnvironment` 4. #30867 Cover the turn tool policy at `buildTurnEnvironment` 5. #30868 Remove seven end-to-end tool policy tests 6. #30869 Name the `*Server` receiver `p` in `turn_environment.go` 7. #30870 Move the turn preparation helpers into `turn_environment.go` > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description. --- coderd/x/chatd/chatd.go | 28 ---------------------- coderd/x/chatd/chatd_internal_test.go | 34 --------------------------- 2 files changed, 62 deletions(-) diff --git a/coderd/x/chatd/chatd.go b/coderd/x/chatd/chatd.go index 3def4d708b2..26ac9caf39c 100644 --- a/coderd/x/chatd/chatd.go +++ b/coderd/x/chatd/chatd.go @@ -2475,14 +2475,6 @@ type runChatResult struct { HistoryTipMessageID int64 } -func allToolNames(allTools []fantasy.AgentTool) []string { - toolNames := make([]string, 0, len(allTools)) - for _, tool := range allTools { - toolNames = append(toolNames, tool.Info().Name) - } - return toolNames -} - func isExploreSubagentMode(mode database.NullChatMode) bool { return mode.Valid && mode.ChatMode == database.ChatModeExplore } @@ -2651,19 +2643,6 @@ func allowedExploreToolNames(allTools []fantasy.AgentTool) []string { return toolNames } -// allowedBehaviorToolNames runs only on non-plan turns because -// appendDynamicTools returns early for plan mode. Within that boundary, -// Explore mode wins over the default behavior that allows all tools. -func allowedBehaviorToolNames( - allTools []fantasy.AgentTool, - chatMode database.NullChatMode, -) []string { - if isExploreSubagentMode(chatMode) { - return allowedExploreToolNames(allTools) - } - return allToolNames(allTools) -} - func stopAfterPlanTools( planMode database.NullChatPlanMode, parentChatID uuid.NullUUID, @@ -3057,15 +3036,8 @@ func appendDynamicTools( } } - activeToolNames := make(map[string]struct{}, len(tools)) - for _, name := range allowedBehaviorToolNames(tools, chatMode) { - activeToolNames[name] = struct{}{} - } for _, t := range tools { info := t.Info() - if _, active := activeToolNames[info.Name]; !active { - continue - } if dynamicToolNames[info.Name] { logger.Warn(ctx, "dynamic tool name collides with built-in tool, built-in takes precedence", slog.F("tool_name", info.Name)) diff --git a/coderd/x/chatd/chatd_internal_test.go b/coderd/x/chatd/chatd_internal_test.go index fc3401b408b..cc3c6a94927 100644 --- a/coderd/x/chatd/chatd_internal_test.go +++ b/coderd/x/chatd/chatd_internal_test.go @@ -905,40 +905,6 @@ func TestAllowedExploreToolNames(t *testing.T) { require.NotContains(t, got, chattool.FindToolsName) } -func TestAllowedBehaviorToolNames(t *testing.T) { - t.Parallel() - - makeTools := func(names ...string) []fantasy.AgentTool { - tools := make([]fantasy.AgentTool, 0, len(names)) - for _, name := range names { - tools = append(tools, newTestAgentTool(name)) - } - return tools - } - - allTools := makeTools("read_file", "custom_tool", "spawn_agent") - exploreMode := database.NullChatMode{ - ChatMode: database.ChatModeExplore, - Valid: true, - } - - t.Run("DefaultModeReturnsAllTools", func(t *testing.T) { - t.Parallel() - require.Equal(t, []string{"read_file", "custom_tool", "spawn_agent"}, allowedBehaviorToolNames( - allTools, - database.NullChatMode{}, - )) - }) - - t.Run("ExploreModeUsesExploreAllowlist", func(t *testing.T) { - t.Parallel() - require.Equal(t, []string{"read_file"}, allowedBehaviorToolNames( - allTools, - exploreMode, - )) - }) -} - func TestStopAfterPlanTools(t *testing.T) { t.Parallel()