Skip to main content

vtcode_core/tools/registry/execution_kernel/
mod.rs

1use anyhow::{Result, anyhow};
2use serde_json::Map;
3use serde_json::Value;
4use serde_json::json;
5
6use crate::config::constants::tools as tool_names;
7use crate::tools::apply_patch::{UNIFIED_FILE_MAX_PAYLOAD_BYTES_ENV, effective_max_payload_bytes};
8use crate::tools::error_messages::agent_execution;
9use crate::tools::names::canonical_tool_name;
10use crate::tools::registry::ToolCatalogSource;
11use crate::tools::tool_intent::ToolIntent;
12use crate::tools::validation::{commands, condensed_schema_hint, paths};
13use crate::utils::tool_name_parsing::MCP_QUALIFIED_TOOL_PREFIX;
14
15use super::ToolRegistry;
16
17const DESCRIPTION_FIELD: &str = "description";
18const DETAILS_ALIAS_FIELD: &str = "details";
19
20/// Path-field aliases accepted by file tools, in priority order.
21///
22/// `read_file`/`edit_file`/`unified_file` historically accept several path-key
23/// spellings. The required-arg check, the preflight path-safety check, and the
24/// execution-history path extraction must all agree on the same set (and order)
25/// so a value accepted as "present" by one stage is validated by the next.
26/// Shared across the `registry` module via `pub(super)`.
27pub(super) const PATH_ALIAS_KEYS: [&str; 5] = ["path", "file_path", "filepath", "target_path", "file"];
28
29/// The explicit set of model-hidden tools the harness path is allowed to
30/// dispatch when the public route lookup misses.
31///
32/// These are registered as real builtins (`with_llm_visibility(false)`) but
33/// deliberately excluded from the public route table
34/// (`assembly::is_removed_public_tool_name`). Only names in this allowlist may
35/// fall back to the inventory registration on the harness path; every other
36/// `with_llm_visibility(false)` helper stays inaccessible through the public
37/// execution entrypoint. This turns internal dispatch into an explicit,
38/// reviewed surface instead of "any inventory registration not in the public
39/// routes".
40const HARNESS_DISPATCHABLE_INTERNAL_TOOLS: [&str; 4] = [
41    tool_names::READ_FILE,
42    tool_names::WRITE_FILE,
43    tool_names::EDIT_FILE,
44    tool_names::LIST_FILES,
45];
46
47/// Entry point classification for a tool dispatch.
48///
49/// The public model surface exposes only the Codex baseline (apply_patch,
50/// exec_command, write_stdin, code_search). File helpers
51/// (read_file/write_file/list_files/edit_file) are registered builtins hidden
52/// from the model and excluded from the public route table, but remain
53/// dispatchable for the harness path. `DispatchMode` encodes that distinction in
54/// the type system so the "public is strict, harness is the only fallback"
55/// invariant lives in exactly one place and cannot be re-derived incorrectly by
56/// a future caller.
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub(super) enum DispatchMode {
59    /// Direct model-originated entry (e.g. `execute_public_tool_ref`,
60    /// integration tests). Rejects anything not in the public route table.
61    ModelPublic,
62    /// Harness entry (via `admit_public_tool_call`). Falls back to the internal
63    /// builtin registration for the harness-dispatchable allowlist when the
64    /// public route lookup misses.
65    Harness,
66}
67
68impl DispatchMode {
69    fn allows_internal_dispatch(self) -> bool {
70        matches!(self, DispatchMode::Harness)
71    }
72}
73
74#[derive(Debug, Clone)]
75pub struct ToolPreflightOutcome {
76    pub normalized_tool_name: String,
77    pub readonly_classification: bool,
78    pub parallel_safe_after_preflight: bool,
79    pub effective_args: Value,
80    /// The intent classified on the validation args that become the executed
81    /// text. Consumers must reuse it instead of re-classifying, so planning
82    /// enforcement cannot disagree with `readonly_classification`.
83    pub intent: ToolIntent,
84}
85
86fn required_args_for_tool(tool_name: &str) -> &'static [&'static str] {
87    match tool_name {
88        tool_names::READ_FILE => &["path"],
89        tool_names::WRITE_FILE => &["path", "content"],
90        tool_names::EDIT_FILE => &["path", "old_str", "new_str"],
91        tool_names::RUN_PTY_CMD | tool_names::CREATE_PTY_SESSION => &["command"],
92        tool_names::APPLY_PATCH => &["input"],
93        _ => &[],
94    }
95}
96
97fn is_missing_arg_value(args: &Value, key: &str) -> bool {
98    match args.get(key) {
99        Some(v) => v.is_null() || (v.is_string() && v.as_str().is_none_or(|s| s.trim().is_empty())),
100        None => true,
101    }
102}
103
104fn is_missing_apply_patch_payload(args: &Value) -> bool {
105    if args.is_string() {
106        return false;
107    }
108
109    let has_object_payload = |key: &str| args.get(key).is_some_and(Value::is_string);
110    !(has_object_payload("patch") || has_object_payload("input"))
111}
112
113fn is_missing_required_arg(tool_name: &str, args: &Value, key: &str) -> bool {
114    if tool_name == tool_names::READ_FILE && key == "path" {
115        return PATH_ALIAS_KEYS.iter().all(|candidate| is_missing_arg_value(args, candidate));
116    }
117    if tool_name == tool_names::EDIT_FILE {
118        return match key {
119            "old_str" => is_missing_arg_value(args, "old_str") && is_missing_arg_value(args, "old_string"),
120            "new_str" => is_missing_arg_value(args, "new_str") && is_missing_arg_value(args, "new_string"),
121            _ => is_missing_arg_value(args, key),
122        };
123    }
124    if tool_name == tool_names::APPLY_PATCH && key == "input" {
125        return is_missing_apply_patch_payload(args);
126    }
127    is_missing_arg_value(args, key)
128}
129
130/// Format a missing-required-argument failure with the canonical wording.
131///
132/// Single source of truth for the preflight failure message so the
133/// tool-specific and command-session required-arg checks cannot drift in
134/// phrasing (both feed into the same joined `failures` list).
135fn missing_required_arg_failure(key: &str) -> String {
136    format!("Missing required argument: {key}")
137}
138
139#[cfg(test)]
140fn parse_file_operation_max_payload_bytes(raw: Option<&str>) -> Option<usize> {
141    raw.and_then(|value| value.trim().parse::<usize>().ok())
142        .filter(|value| *value >= 1024)
143}
144
145fn configured_file_operation_max_payload_bytes() -> usize {
146    // Single source of truth for the cap: both preflight and the post-decode
147    // size check in `apply_patch` resolve the env-var override the same way
148    // (including the 1 KiB safety floor), so the two stages always agree.
149    effective_max_payload_bytes()
150}
151
152fn schema_uses_description_alias(schema_properties: &Map<String, Value>) -> bool {
153    schema_properties.contains_key(DESCRIPTION_FIELD) && !schema_properties.contains_key(DETAILS_ALIAS_FIELD)
154}
155
156fn normalize_description_alias(object: &mut Map<String, Value>, schema_properties: &Map<String, Value>) -> bool {
157    if !schema_uses_description_alias(schema_properties) || object.contains_key(DESCRIPTION_FIELD) {
158        return false;
159    }
160
161    let Some(details) = object.remove(DETAILS_ALIAS_FIELD) else {
162        return false;
163    };
164    object.insert(DESCRIPTION_FIELD.to_string(), details);
165    true
166}
167
168fn normalize_schema_aliases_in_place(value: &mut Value, schema: &Value) -> bool {
169    let Some(schema_object) = schema.as_object() else {
170        return false;
171    };
172
173    let mut changed = false;
174
175    if let Value::Object(object) = value
176        && let Some(properties) = schema_object.get("properties").and_then(Value::as_object)
177    {
178        changed |= normalize_description_alias(object, properties);
179        for (property_name, property_schema) in properties {
180            if let Some(property_value) = object.get_mut(property_name) {
181                // Coerce string-encoded JSON into the schema-declared type before
182                // recursing, so nested alias/type normalization sees the real value.
183                // The output-budget field is skipped: it has a dedicated strict
184                // validator (`output_limits::max_output_tokens`) that deliberately
185                // rejects string/float values, and bypassing it would weaken that
186                // guardrail.
187                if property_name != vtcode_utility_tool_specs::MAX_OUTPUT_TOKENS_FIELD {
188                    changed |= coerce_string_to_schema_type_in_place(property_value, property_schema);
189                }
190                changed |= normalize_schema_aliases_in_place(property_value, property_schema);
191            }
192        }
193    }
194
195    if let Value::Array(items) = value
196        && let Some(items_schema) = schema_object.get("items")
197    {
198        for item in items {
199            changed |= coerce_string_to_schema_type_in_place(item, items_schema);
200            changed |= normalize_schema_aliases_in_place(item, items_schema);
201        }
202    }
203
204    for keyword in ["allOf", "anyOf", "oneOf"] {
205        if let Some(branches) = schema_object.get(keyword).and_then(Value::as_array) {
206            for branch in branches {
207                changed |= normalize_schema_aliases_in_place(value, branch);
208            }
209        }
210    }
211    for keyword in ["if", "then", "else"] {
212        if let Some(branch) = schema_object.get(keyword) {
213            changed |= normalize_schema_aliases_in_place(value, branch);
214        }
215    }
216
217    changed
218}
219
220/// Strict, schema-aware compatibility coercion of a string-encoded JSON value.
221///
222/// Some models occasionally emit a JSON-encoded *string* where the schema
223/// expects a structured or primitive value — e.g. `"[\"path\"]"` for an array
224/// field, or `"10"` for an integer field. Left unhandled the agent retries the
225/// same malformed call for many turns (six preflight failures were observed in
226/// a single checkpoint turn), wasting tool budget and context.
227///
228/// When `value` is a string and `schema` declares a single non-string `type`,
229/// this rewrites it in place to the parsed JSON value, but **only** when the
230/// parse is unambiguous and the parsed top-level type exactly matches the
231/// schema. It deliberately does not:
232/// - coerce free-form string fields (schema `type` is `string`, an array of
233///   types, or absent),
234/// - reinterpret malformed/non-JSON strings (they stay strings and fail strict
235///   validation with an actionable message),
236/// - coerce floats into integers (`"10.0"` for an integer field stays a string),
237/// - relax enum/bounds checks — strict `jsonschema` validation still runs after.
238fn coerce_string_to_schema_type_in_place(value: &mut Value, schema: &Value) -> bool {
239    let Some(raw) = value.as_str() else {
240        return false;
241    };
242    let Some(schema_type) = schema.get("type").and_then(Value::as_str) else {
243        return false;
244    };
245    let Some(coerced) = parse_string_as_schema_type(raw, schema_type) else {
246        return false;
247    };
248    *value = coerced;
249    true
250}
251
252/// Parse a JSON-encoded string into the schema-declared primitive/container type.
253///
254/// Returns `Some` only when `serde_json` accepts the string and the resulting
255/// top-level JSON type exactly matches `schema_type`. Anything else returns
256/// `None` so the caller leaves the value untouched and lets strict validation
257/// produce the error.
258fn parse_string_as_schema_type(raw: &str, schema_type: &str) -> Option<Value> {
259    let parsed = serde_json::from_str::<Value>(raw).ok()?;
260    match schema_type {
261        "array" => parsed.is_array().then_some(parsed),
262        "object" => parsed.is_object().then_some(parsed),
263        "integer" => parsed.as_i64().map(Value::from).or_else(|| parsed.as_u64().map(Value::from)),
264        "number" => parsed.is_number().then_some(parsed),
265        "boolean" => parsed.as_bool().map(Value::Bool),
266        _ => None,
267    }
268}
269
270fn normalize_details_aliases(args: &Value, parameter_schema: Option<&Value>) -> Option<Value> {
271    let schema = parameter_schema?;
272    let mut normalized = args.clone();
273    normalize_schema_aliases_in_place(&mut normalized, schema).then_some(normalized)
274}
275
276fn serialized_payload_size_bytes(args: &Value) -> usize {
277    serde_json::to_vec(args)
278        .map(|bytes| bytes.len())
279        .unwrap_or_else(|_| args.to_string().len())
280}
281
282fn file_operation_action_for_limit(normalized_tool_name: &str, args: &Value) -> Option<String> {
283    if normalized_tool_name == tool_names::UNIFIED_FILE {
284        return crate::tools::tool_intent::file_operation_action(args).map(|a| a.to_ascii_lowercase());
285    }
286    if normalized_tool_name == tool_names::APPLY_PATCH {
287        return Some("patch".to_string());
288    }
289    if normalized_tool_name == tool_names::EDIT_FILE {
290        return Some("edit".to_string());
291    }
292    None
293}
294
295/// Shared prefix for action-alias remappers: return the args object only when it
296/// is a JSON object that does not already declare an `action`. A caller that
297/// needs to reject non-matching tool names does that check before calling.
298fn args_object_without_action(args: &Value) -> Option<&Map<String, Value>> {
299    let obj = args.as_object()?;
300    if obj.contains_key("action") {
301        return None;
302    }
303    Some(obj)
304}
305
306/// Clone an args object and inject the resolved `action` field, producing the
307/// remapped payload. Single source for the clone-insert-wrap so the two
308/// action-alias remappers cannot drift in how the action is attached.
309fn with_action_inserted(obj: &Map<String, Value>, action: &str) -> Value {
310    let mut mapped = obj.clone();
311    mapped.insert("action".to_string(), Value::String(action.to_string()));
312    Value::Object(mapped)
313}
314
315pub(super) fn remap_public_file_operation_alias_args(
316    requested_name: &str,
317    normalized_tool_name: &str,
318    args: &Value,
319) -> Option<Value> {
320    if normalized_tool_name != tool_names::UNIFIED_FILE {
321        return None;
322    }
323
324    let obj = args_object_without_action(args)?;
325
326    let action = super::assembly::public_tool_name_candidates(requested_name)
327        .into_iter()
328        .find_map(|candidate| match candidate.as_str() {
329            tool_names::READ_FILE => Some("read"),
330            tool_names::WRITE_FILE => Some("write"),
331            tool_names::EDIT_FILE => Some("edit"),
332            tool_names::DELETE_FILE => Some("delete"),
333            tool_names::MOVE_FILE => Some("move"),
334            tool_names::COPY_FILE => Some("copy"),
335            tool_names::CREATE_FILE => Some("write"),
336            _ => None,
337        })?;
338
339    Some(with_action_inserted(obj, action))
340}
341
342pub(super) fn remap_consolidated_action_alias_args(
343    requested_name: &str,
344    normalized_tool_name: &str,
345    args: &Value,
346) -> Option<Value> {
347    let obj = args_object_without_action(args)?;
348
349    let action = super::assembly::public_tool_name_candidates(requested_name)
350        .into_iter()
351        .find_map(|candidate| match (normalized_tool_name, candidate.as_str()) {
352            (tool_names::MCP, tool_names::MCP_SEARCH_TOOLS) => Some("search_tools"),
353            (tool_names::MCP, tool_names::MCP_GET_TOOL_DETAILS) => Some("get_tool_details"),
354            (tool_names::MCP, tool_names::MCP_LIST_SERVERS) => Some("list_servers"),
355            (tool_names::MCP, tool_names::MCP_CONNECT_SERVER) => Some("connect"),
356            (tool_names::MCP, tool_names::MCP_DISCONNECT_SERVER) => Some("disconnect"),
357            (tool_names::CRON, tool_names::CRON_CREATE) => Some("create"),
358            (tool_names::CRON, tool_names::CRON_LIST) => Some("list"),
359            (tool_names::CRON, tool_names::CRON_DELETE) => Some("delete"),
360            (tool_names::AGENT, tool_names::SPAWN_AGENT) => Some("spawn"),
361            (tool_names::AGENT, tool_names::SPAWN_BACKGROUND_SUBPROCESS) => Some("spawn_subprocess"),
362            (tool_names::AGENT, tool_names::SEND_INPUT) => Some("send_input"),
363            (tool_names::AGENT, tool_names::RESUME_AGENT) => Some("resume"),
364            (tool_names::AGENT, tool_names::WAIT_AGENT) => Some("wait"),
365            (tool_names::AGENT, tool_names::CLOSE_AGENT) => Some("close"),
366            _ => None,
367        })?;
368
369    Some(with_action_inserted(obj, action))
370}
371
372fn enforce_file_operation_payload_limit(
373    normalized_tool_name: &str,
374    args: &Value,
375    max_payload_bytes: usize,
376    failures: &mut Vec<String>,
377) {
378    let Some(action) = file_operation_action_for_limit(normalized_tool_name, args) else {
379        return;
380    };
381    if action != "patch" && action != "edit" {
382        return;
383    }
384
385    let payload_bytes = serialized_payload_size_bytes(args);
386    if payload_bytes <= max_payload_bytes {
387        return;
388    }
389
390    tracing::warn!(
391        tool = %normalized_tool_name,
392        action = %action,
393        payload_bytes,
394        max_payload_bytes,
395        "Rejected oversized patch/edit payload during preflight"
396    );
397
398    failures.push(format!(
399        "Patch/edit payload too large for '{normalized_tool_name}': action='{action}', payload={payload_bytes} bytes exceeds {max_payload_bytes} bytes. \
400         Split the change into smaller patch/edit calls, or raise {UNIFIED_FILE_MAX_PAYLOAD_BYTES_ENV} for intentional large edits."
401    ));
402}
403
404pub(super) fn normalize_tool_args<'a>(
405    normalized_tool_name: &str,
406    args: &'a Value,
407    parameter_schema: Option<&Value>,
408) -> Result<std::borrow::Cow<'a, Value>> {
409    let mut normalized = std::borrow::Cow::Borrowed(args);
410
411    if normalized_tool_name == tool_names::APPLY_PATCH
412        && let Some(raw_patch) = normalized.as_ref().as_str()
413    {
414        normalized = std::borrow::Cow::Owned(json!({ "input": raw_patch }));
415    }
416
417    if matches!(
418        normalized_tool_name,
419        tool_names::RUN_PTY_CMD | tool_names::CREATE_PTY_SESSION | tool_names::UNIFIED_EXEC | tool_names::SHELL
420    ) {
421        let shell_args =
422            crate::tools::command_args::normalize_shell_args(normalized.as_ref()).map_err(|error| anyhow!(error))?;
423        if shell_args != *normalized.as_ref() {
424            normalized = std::borrow::Cow::Owned(shell_args);
425        }
426        // Share truthful-status normalization with gate accounting. Pure
427        // truncators are elided; static read-only filters retain their output
428        // under fail-closed pipefail. Safety and permissions still validate
429        // the resulting command. Preserve the original cmd as typed evidence.
430        if let std::borrow::Cow::Owned(executed) =
431            crate::tools::tool_intent::shell_args_as_executed(normalized_tool_name, normalized.as_ref())
432        {
433            normalized = std::borrow::Cow::Owned(executed);
434        }
435    }
436
437    if let Some(alias_args) = normalize_details_aliases(normalized.as_ref(), parameter_schema) {
438        normalized = std::borrow::Cow::Owned(alias_args);
439    }
440
441    Ok(normalized)
442}
443
444fn public_exec_validation_args(normalized_tool_name: &str, args: &Value) -> Result<Option<Value>> {
445    let write_stdin_dispatch = match normalized_tool_name {
446        tool_names::WRITE_STDIN => {
447            Some(crate::tools::command_args::write_stdin_dispatch(args).map_err(|error| anyhow!(error))?)
448        }
449        _ => None,
450    };
451    let action = match normalized_tool_name {
452        tool_names::EXEC_COMMAND => "run",
453        tool_names::WRITE_STDIN => write_stdin_dispatch
454            .map(crate::tools::command_args::WriteStdinDispatch::command_session_action)
455            .ok_or_else(|| anyhow!("write_stdin dispatch was not resolved"))?,
456        _ => return Ok(None),
457    };
458    let mut exec_args = crate::tools::command_args::normalize_shell_args(args).map_err(|error| anyhow!(error))?;
459    // Same truthful-status rewrite as `normalize_tool_args` (see above):
460    // `EXEC_COMMAND` normalizes here rather than there, so the hook must be
461    // repeated to keep validation and execution agreed on one command text.
462    if let std::borrow::Cow::Owned(executed) =
463        crate::tools::tool_intent::shell_args_as_executed(normalized_tool_name, &exec_args)
464    {
465        exec_args = executed;
466    }
467    let payload = exec_args
468        .as_object_mut()
469        .ok_or_else(|| anyhow!("{normalized_tool_name} requires a JSON object"))?;
470    if write_stdin_dispatch == Some(crate::tools::command_args::WriteStdinDispatch::Poll) {
471        payload.remove("input");
472    }
473    payload.insert("action".to_string(), Value::String(action.to_string()));
474    Ok(Some(exec_args))
475}
476
477pub(super) fn preflight_validate_call(
478    registry: &ToolRegistry,
479    name: &str,
480    args: &Value,
481) -> Result<ToolPreflightOutcome> {
482    preflight_validate_call_with_mode(registry, name, args, DispatchMode::ModelPublic)
483}
484
485/// Preflight-validate a tool call under the given [`DispatchMode`].
486///
487/// Resolution is delegated to [`resolve_dispatch_target`] so the preflight and
488/// execution paths agree on exactly which names are dispatchable in each mode.
489pub(super) fn preflight_validate_call_with_mode(
490    registry: &ToolRegistry,
491    name: &str,
492    args: &Value,
493    mode: DispatchMode,
494) -> Result<ToolPreflightOutcome> {
495    let normalized_tool_name = resolve_dispatch_target(registry, name, mode)?;
496
497    if let Some(remapped_args) = remap_public_file_operation_alias_args(name, &normalized_tool_name, args)
498        .or_else(|| remap_consolidated_action_alias_args(name, &normalized_tool_name, args))
499    {
500        preflight_validate_resolved_call(registry, &normalized_tool_name, &remapped_args)
501    } else {
502        preflight_validate_resolved_call(registry, &normalized_tool_name, args)
503    }
504}
505
506/// Resolve a requested tool name to its registration name for the given
507/// [`DispatchMode`].
508///
509/// This is the single source of truth shared by both the preflight
510/// ([`preflight_validate_call_with_mode`]) and execution
511/// (`execute_public_tool_ref_internal_with_mode`) paths, so they can never
512/// disagree on whether a tool is dispatchable.
513///
514/// - [`DispatchMode::ModelPublic`]: only the public route table resolves; any
515///   miss returns "Unknown tool".
516/// - [`DispatchMode::Harness`]: on a public-route miss, fall back to the
517///   internal builtin registration, but only for names in
518///   [`HARNESS_DISPATCHABLE_INTERNAL_TOOLS`].
519pub(super) fn resolve_dispatch_target(registry: &ToolRegistry, name: &str, mode: DispatchMode) -> Result<String> {
520    match registry.resolve_public_tool(name) {
521        Ok(resolution) => Ok(resolution.registration_name().to_string()),
522        Err(public_err) => {
523            if mode.allows_internal_dispatch() {
524                if let Some(internal_target) = resolve_internal_dispatch_tool(registry, name) {
525                    return Ok(internal_target);
526                }
527                if name.eq_ignore_ascii_case("mcp_proxy")
528                    && let Some(mcp_target) = resolve_mcp_proxy_dispatch_target(registry)
529                {
530                    return mcp_target;
531                }
532            }
533            Err(anyhow!("Unknown tool: {}: {public_err}", canonical_tool_name(name)))
534        }
535    }
536}
537
538/// Resolve the legacy `mcp_proxy` tool name used by some persisted model
539/// histories to a registered MCP proxy.
540///
541/// Returns `Ok(None)` when no MCP registration exists, and an explicit error
542/// naming the model-visible alternatives when the request is ambiguous. This
543/// keeps the fallback scoped to MCP tools instead of silently widening the
544/// harness dispatch surface.
545fn resolve_mcp_proxy_dispatch_target(registry: &ToolRegistry) -> Option<Result<String>> {
546    let mcp_registrations = registry
547        .inventory
548        .registrations_snapshot()
549        .into_iter()
550        .filter(|registration| registration.catalog_source() == ToolCatalogSource::Mcp)
551        .collect::<Vec<_>>();
552    if mcp_registrations.is_empty() {
553        return None;
554    }
555
556    if mcp_registrations.len() == 1 {
557        return Some(Ok(mcp_registrations[0].name().to_string()));
558    }
559
560    let visible_names = mcp_registrations
561        .iter()
562        .flat_map(|registration| {
563            registration
564                .metadata()
565                .aliases()
566                .iter()
567                .find(|alias| alias.starts_with(MCP_QUALIFIED_TOOL_PREFIX))
568                .map(|alias| alias.to_string())
569                .or_else(|| Some(registration.name().to_string()))
570        })
571        .collect::<Vec<_>>()
572        .join(", ");
573    Some(Err(anyhow!("Ambiguous MCP proxy call: specify one of the available MCP tools: {visible_names}")))
574}
575
576/// Resolve a requested name to a registered harness-dispatchable internal
577/// (model-hidden) tool.
578///
579/// Returns the registration name only when the canonicalized name is in
580/// [`HARNESS_DISPATCHABLE_INTERNAL_TOOLS`] AND actually registered in the
581/// inventory. This never exposes the tool publicly; it merely lets the harness
582/// dispatch the intentionally model-hidden file helpers. Any other
583/// `with_llm_visibility(false)` helper (e.g. diagnostics-only tools) is NOT
584/// reachable through this path, so registering a new hidden tool cannot silently
585/// widen the harness-dispatchable surface.
586fn resolve_internal_dispatch_tool(registry: &ToolRegistry, name: &str) -> Option<String> {
587    super::assembly::public_tool_name_candidates(name)
588        .iter()
589        .map(|candidate| candidate.trim())
590        .filter(|candidate| !candidate.is_empty())
591        .find(|candidate| {
592            HARNESS_DISPATCHABLE_INTERNAL_TOOLS
593                .iter()
594                .any(|allowed| allowed.eq_ignore_ascii_case(candidate))
595        })
596        .and_then(|candidate| {
597            let candidate = candidate.to_ascii_lowercase();
598            registry
599                .inventory
600                .registration_for(&candidate)
601                .map(|registration| registration.name().to_string())
602        })
603}
604
605pub(super) fn preflight_validate_resolved_call(
606    registry: &ToolRegistry,
607    normalized_tool_name: &str,
608    args: &Value,
609) -> Result<ToolPreflightOutcome> {
610    let mut routed_tool_name = normalized_tool_name.to_string();
611    let mut validation_tool_name = routed_tool_name.clone();
612    let parameter_schema = registry
613        .inventory
614        .registration_for(normalized_tool_name)
615        .and_then(|registration| registration.parameter_schema().cloned());
616    let mut validation_args = normalize_tool_args(normalized_tool_name, args, parameter_schema.as_ref())?;
617    let mut effective_args = None;
618    // Schema for the tool we ultimately validate against. Defaults to the
619    // originally-resolved tool's schema; each remap branch overwrites it with
620    // the exec schema it already fetches, so we never re-look-up the same
621    // registration (the previous form fetched the same schema up to 3x).
622    let mut effective_parameter_schema = parameter_schema;
623
624    crate::tools::output_limits::max_output_tokens(validation_args.as_ref())
625        .map_err(|error| anyhow!("Invalid arguments for tool '{routed_tool_name}': {error}"))?;
626
627    if let Some(exec_args) = public_exec_validation_args(normalized_tool_name, validation_args.as_ref())? {
628        validation_tool_name = tool_names::UNIFIED_EXEC.to_string();
629        validation_args = std::borrow::Cow::Owned(exec_args);
630        effective_args = Some(validation_args.as_ref().clone());
631        effective_parameter_schema = registry
632            .inventory
633            .registration_for(&validation_tool_name)
634            .and_then(|registration| registration.parameter_schema().cloned());
635    } else if normalized_tool_name == tool_names::UNIFIED_FILE
636        && let Some(remapped_args) =
637            crate::tools::tool_intent::remap_file_operation_command_args_to_command_session(validation_args.as_ref())
638    {
639        routed_tool_name = tool_names::UNIFIED_EXEC.to_string();
640        validation_tool_name = tool_names::UNIFIED_EXEC.to_string();
641        effective_parameter_schema = registry
642            .inventory
643            .registration_for(&validation_tool_name)
644            .and_then(|registration| registration.parameter_schema().cloned());
645        validation_args = std::borrow::Cow::Owned(
646            normalize_tool_args(&validation_tool_name, &remapped_args, effective_parameter_schema.as_ref())?
647                .into_owned(),
648        );
649        effective_args = Some(validation_args.as_ref().clone());
650    }
651
652    if validation_tool_name == tool_names::TASK_TRACKER {
653        effective_parameter_schema =
654            Some(crate::tools::handlers::task_tracker::task_tracker_parameter_schema_for_workflow(
655                registry.is_planning_active(),
656            ));
657        validation_args = std::borrow::Cow::Owned(
658            normalize_tool_args(&validation_tool_name, validation_args.as_ref(), effective_parameter_schema.as_ref())?
659                .into_owned(),
660        );
661        if effective_args.is_some() {
662            effective_args = Some(validation_args.as_ref().clone());
663        }
664    }
665
666    let required = required_args_for_tool(&validation_tool_name);
667    let mut failures = Vec::with_capacity(required.len());
668    for key in required {
669        if is_missing_required_arg(&validation_tool_name, validation_args.as_ref(), key) {
670            failures.push(if validation_tool_name == tool_names::APPLY_PATCH {
671                crate::tools::apply_patch::APPLY_PATCH_ARGUMENT_CORRECTION.to_string()
672            } else {
673                missing_required_arg_failure(key)
674            });
675        }
676    }
677    if validation_tool_name == tool_names::UNIFIED_EXEC {
678        failures.extend(
679            crate::tools::command_args::command_session_missing_required_args(validation_args.as_ref())
680                .into_iter()
681                .map(missing_required_arg_failure),
682        );
683    }
684
685    if let Some(path) = PATH_ALIAS_KEYS
686        .iter()
687        .find_map(|key| validation_args.as_ref().get(*key).and_then(Value::as_str))
688        && let Err(err) = paths::validate_path_safety(path)
689    {
690        failures.push(format!("Path security check failed: {err}"));
691    }
692
693    let should_validate_command = matches!(
694        validation_tool_name.as_str(),
695        tool_names::RUN_PTY_CMD | tool_names::CREATE_PTY_SESSION | tool_names::SHELL
696    ) || (validation_tool_name == tool_names::UNIFIED_EXEC
697        && crate::tools::command_args::command_session_requires_command_safety(validation_args.as_ref()));
698    if should_validate_command {
699        let command_value = crate::tools::command_args::normalized_command_value(validation_args.as_ref())
700            .ok()
701            .flatten();
702        let collision_command = match &command_value {
703            Some(Value::String(command)) => Some(command.as_str()),
704            Some(Value::Array(command)) => command.first().and_then(Value::as_str),
705            _ => None,
706        };
707        if collision_command.is_some_and(crate::tools::names::is_apply_patch_shell_collision_command) {
708            failures.push(format!(
709                "apply_patch is a tool, not a shell executable. {}",
710                crate::tools::apply_patch::APPLY_PATCH_ARGUMENT_CORRECTION
711            ));
712        }
713        let validation_result = match command_value {
714            Some(Value::Array(_)) => crate::tools::command_args::command_words(validation_args.as_ref())
715                .ok()
716                .flatten()
717                .map_or(Ok(()), |command| commands::validate_command_argv(&command)),
718            Some(Value::String(command)) => commands::validate_shell_script(&command),
719            _ => Ok(()),
720        };
721        if let Err(err) = validation_result {
722            failures.push(format!("Command security check failed: {err}"));
723        }
724    }
725    enforce_file_operation_payload_limit(
726        &validation_tool_name,
727        validation_args.as_ref(),
728        configured_file_operation_max_payload_bytes(),
729        &mut failures,
730    );
731
732    if !failures.is_empty() {
733        return Err(anyhow!("Tool preflight validation failed for '{}': {}", routed_tool_name, failures.join("; ")));
734    }
735
736    if validation_tool_name == tool_names::UNIFIED_EXEC
737        && crate::tools::tool_intent::command_session_action(validation_args.as_ref()).is_none()
738    {
739        return Err(anyhow!(
740            "Invalid arguments for tool '{routed_tool_name}': missing action; provide `action` or inferable exec arguments"
741        ));
742    }
743    let schema_validation_args = crate::tools::output_limits::args_without_output_metadata(validation_args.as_ref());
744    if let Some(schema) = effective_parameter_schema.as_ref() {
745        let error_msg = match jsonschema::validator_for(schema) {
746            Ok(validator) => validator
747                .iter_errors(&schema_validation_args)
748                .map(|error| crate::tools::validation::describe_jsonschema_error(&error))
749                .collect::<Vec<_>>()
750                .join("; "),
751            Err(schema_error) => crate::tools::validation::describe_jsonschema_error(&schema_error),
752        };
753        if !error_msg.is_empty() {
754            let hint_msg = condensed_schema_hint(schema)
755                .map(|hint| format!("\nExpected schema (required fields and types): {hint}"))
756                .unwrap_or_default();
757            let patch_hint = if validation_tool_name == tool_names::APPLY_PATCH {
758                crate::tools::apply_patch::APPLY_PATCH_ARGUMENT_CORRECTION
759            } else if validation_tool_name == tool_names::TASK_TRACKER
760                && crate::tools::handlers::task_tracker::is_task_tracker_shape_error(&error_msg)
761            {
762                crate::tools::handlers::task_tracker::TASK_TRACKER_ARGUMENT_CORRECTION
763            } else {
764                ""
765            };
766            return Err(anyhow!("Invalid arguments for tool '{routed_tool_name}': {error_msg}{hint_msg} {patch_hint}"));
767        }
768    }
769    if validation_tool_name == tool_names::CODE_SEARCH {
770        crate::tools::code_search::validate_args(&schema_validation_args)
771            .map_err(|error| anyhow!("Invalid arguments for tool '{routed_tool_name}': {error}"))?;
772    }
773
774    let intent = crate::tools::tool_intent::classify_tool_intent(&validation_tool_name, validation_args.as_ref());
775    let readonly_classification = !intent.mutating;
776    if registry.is_planning_active()
777        && !registry.is_planning_active_allowed_with_intent(&validation_tool_name, validation_args.as_ref(), &intent)
778    {
779        let msg = agent_execution::planning_workflow_denial_message(&routed_tool_name);
780        return Err(anyhow!(msg).context(agent_execution::PLANNING_DENIED_CONTEXT));
781    }
782
783    Ok(ToolPreflightOutcome {
784        normalized_tool_name: routed_tool_name.clone(),
785        readonly_classification,
786        parallel_safe_after_preflight: crate::tools::tool_intent::is_parallel_safe_call_with_intent(
787            &validation_tool_name,
788            validation_args.as_ref(),
789            &intent,
790        ),
791        effective_args: effective_args.unwrap_or_else(|| validation_args.into_owned()),
792        intent,
793    })
794}
795
796#[cfg(test)]
797mod tests;