返回 CodeWhale
tool_setup.rs
根目录 / crates / engine / src / core / engine / tool_setup.rs
1 //! Per-turn tool registry setup.
2 //!
3 //! This keeps mode/feature-specific registry construction out of the send path.
4
5 use super::*;
6 use crate::core::authority::shell_policy_for_mode;
7 use crate::tools::AgentToolSurfaceOptions;
8 use codewhale_agent_contract::worker_profile::ShellPolicy;
9
10 fn should_register_remember_tool(memory_enabled: bool) -> bool {
11 memory_enabled
12 }
13
14 impl Engine {
15 pub(super) fn agent_tool_surface_options(
16 &self,
17 shell_policy: ShellPolicy,
18 ) -> AgentToolSurfaceOptions {
19 let mut options = AgentToolSurfaceOptions::new(shell_policy);
20 options.apply_patch_enabled = self.config.features.enabled(Feature::ApplyPatch);
21 options.web_search_enabled = self.config.features.enabled(Feature::WebSearch);
22 options.memory_tool_enabled = should_register_remember_tool(self.config.memory_enabled);
23 options.vision_config = if self.config.features.enabled(Feature::VisionModel) {
24 self.config.vision_config.clone()
25 } else {
26 None
27 };
28 options.speech_output_dir = self.config.speech_output_dir.clone();
29 options.goal_state = Some(self.config.goal_state.clone());
30 options.goal_complete_judge = self.config.goal_complete_judge;
31 options.goal_blocked_judge = self.config.goal_blocked_judge;
32 options.user_input_limits = self.config.user_input_limits;
33 options.request_plugin_install_enabled = self.request_plugin_install_allowed();
34 options
35 }
36
37 /// `request_plugin_install` returns a TUI slash command and is a
38 /// proactive offer, so it exists only in the interactive TUI with
39 /// contextual tips on (0.10.1 plugin offering policy, rules 3 and 11).
40 /// Exec, ACP, and runtime-API hosts run with terminal chrome off. Applies
41 /// to every mode's surface and is inherited by child agents.
42 fn request_plugin_install_allowed(&self) -> bool {
43 self.config.terminal_chrome_enabled
44 && codewhale_runtime::settings::Settings::load_read_only()
45 .map(|settings| settings.contextual_tips)
46 .unwrap_or(true)
47 }
48
49 #[cfg(test)]
50 pub(super) fn build_turn_tool_registry_builder(
51 &self,
52 mode: AppMode,
53 todo_list: SharedTodoList,
54 ) -> ToolRegistryBuilder {
55 self.build_turn_tool_registry_builder_for_route(
56 mode,
57 self.session.allow_shell,
58 self.codewhale_client.clone(),
59 &self.session.model,
60 todo_list,
61 )
62 }
63
64 /// Build the registry from the route and authority already resolved for
65 /// this turn. Preview calls this before either is installed on the engine,
66 /// so reading `self.session` here would describe the previous turn's shell
67 /// posture, client, and model.
68 #[allow(clippy::too_many_arguments)]
69 pub(super) fn build_turn_tool_registry_builder_for_route(
70 &self,
71 mode: AppMode,
72 allow_shell: bool,
73 client: Option<CodewhaleClient>,
74 model: &str,
75 todo_list: SharedTodoList,
76 ) -> ToolRegistryBuilder {
77 let shell_policy = shell_policy_for_mode(mode, allow_shell);
78 if mode != AppMode::Plan {
79 let mut builder = ToolRegistryBuilder::new().with_agent_runtime_surface(
80 client.clone(),
81 model.to_string(),
82 self.agent_tool_surface_options(shell_policy),
83 todo_list,
84 );
85 if self.config.features.enabled(Feature::Mcp) {
86 builder = builder.with_registry_mcp_sync_tool();
87 }
88 // `start_mcp_server` belongs to every executable mode. Keep its
89 // handler aligned with the model catalog, which always loads the
90 // tool while MCP is enabled. The former early return registered
91 // it only in Plan mode, so Agent/Full Access advertised a tool
92 // that could never cross the execution boundary.
93 if let Some(ref pool) = self.mcp_pool {
94 builder = builder
95 .with_runtime_mcp_tool(Arc::clone(pool))
96 .with_registry_mcp_start_tool(Arc::clone(pool));
97 }
98 return builder;
99 }
100
101 let mut builder = ToolRegistryBuilder::new()
102 // Modes change authority, not the primitive tool identity.
103 // Plan advertises the same file names as Work; the turn-loop
104 // mode gate below the schema boundary blocks write/edit, and
105 // the ToolContext carries ShellPolicy::None.
106 .with_file_tools()
107 .with_present_tool()
108 .with_show_data_tool()
109 // Foreground-only shell registration: never add terminal_*
110 // lifecycle tools to Plan merely to keep the `bash` identity
111 // stable across modes.
112 .with_foreground_shell_tools()
113 .with_search_tools()
114 // Compaction replaces history in Plan exactly as it does in Work,
115 // so the archive read tool has to exist there too or the summary
116 // header would point at a name this mode never advertises.
117 .with_compaction_archive_tools()
118 .with_git_tools()
119 .with_git_history_tools()
120 .with_read_media_tool()
121 .with_skill_tools()
122 .with_validation_tools()
123 .with_handle_tools()
124 .with_runtime_read_only_task_tools()
125 .with_todo_tool(todo_list)
126 .with_goal_tools(
127 self.config.goal_state.clone(),
128 crate::tools::goal::GoalGate::from_flags(
129 self.config.goal_complete_judge,
130 self.config.goal_blocked_judge,
131 None,
132 ),
133 );
134
135 builder = builder
136 .with_review_tool(client.clone(), model.to_string())
137 .with_user_input_tool(self.config.user_input_limits);
138
139 if self.config.features.enabled(Feature::WebSearch) {
140 builder = builder.with_web_tools();
141 }
142
143 // Register the `remember` tool only when the user has opted in to
144 // user-memory (#489). Without that opt-in the tool would always
145 // fail; surfacing it would just waste catalog slots.
146 if should_register_remember_tool(self.config.memory_enabled) {
147 builder = builder.with_remember_tool().with_native_memory_tools();
148 }
149
150 // Register image_analyze tool when vision_model is configured and feature enabled.
151 if self.config.features.enabled(Feature::VisionModel)
152 && let Some(ref vision_config) = self.config.vision_config
153 {
154 builder = builder.with_vision_tools(vision_config.clone(), client.clone());
155 }
156
157 // Register the `notify` tool unconditionally (#1322). Interactive and
158 // headless entry points install the merged notification policy before
159 // tool setup, including method=off, quiet/category, and attention.
160 // The tool returns a truthful suppressed/delivered receipt.
161 builder = builder.with_notify_tool();
162
163 if self.request_plugin_install_allowed() {
164 builder = builder.with_request_plugin_install_tool();
165 }
166
167 // Register the `registry_sync` tool for fetching and caching
168 // MCP Registry server metadata. Rides on `Feature::Mcp` — the same
169 // flag that gates the rest of the MCP system (defaults to enabled;
170 // opt out via `[features]` in config.toml).
171 if self.config.features.enabled(Feature::Mcp) {
172 builder = builder.with_registry_mcp_sync_tool();
173 }
174
175 // Register the start_mcp_server tool so LLM can dynamically start
176 // MCP servers from conversation context. Only when the pool has been
177 // initialized (lazy via ensure_mcp_pool).
178 if let Some(ref pool) = self.mcp_pool {
179 builder = builder
180 .with_runtime_mcp_tool(Arc::clone(pool))
181 .with_registry_mcp_start_tool(Arc::clone(pool));
182 }
183
184 builder
185 }
186 }
187
188 #[cfg(test)]
189 mod tests {
190 use super::should_register_remember_tool;
191
192 #[test]
193 fn remember_tool_registration_requires_memory_opt_in() {
194 assert!(should_register_remember_tool(true));
195 assert!(!should_register_remember_tool(false));
196 }
197 }
198
198 lines RUST