| 1 | use std::borrow::Cow; |
| 2 | |
| 3 | use serde_json::Value; |
| 4 | |
| 5 | use crate::ToolError; |
| 6 | |
| 7 | /// Rewrite well-known parameter spellings from other coding harnesses onto the |
| 8 | /// names this tool actually implements. |
| 9 | /// |
| 10 | /// Every mainstream harness names the same three file-edit arguments |
| 11 | /// differently — `old_string`/`new_string`, `old_str`/`new_str`, |
| 12 | /// `oldText`/`newText` — and models carry whichever spelling their training |
| 13 | /// saw most. CodeWhale's canonical `search`/`replace` is the odd one out, so a |
| 14 | /// model reaching for its prior used to burn a full turn on a rejection |
| 15 | /// (#5209) and then guess again. Translating an unambiguous synonym is |
| 16 | /// strictly better than refusing it: the edit the model asked for is the edit |
| 17 | /// that happens, and the schema still advertises exactly one canonical name so |
| 18 | /// there is no new ambiguity to learn. |
| 19 | /// |
| 20 | /// This is deliberately *not* a silent-acceptance path. Only exact synonyms |
| 21 | /// are mapped, a synonym that disagrees with an explicitly supplied canonical |
| 22 | /// value is an error rather than a coin flip, and any parameter that is not a |
| 23 | /// known synonym still fails validation. The #5209 guarantee — no fabricated |
| 24 | /// "Replaced 1 occurrence" for an edit that never landed — is unchanged. |
| 25 | pub struct ParamAlias { |
| 26 | /// Spelling a model might emit. |
| 27 | alias: &'static str, |
| 28 | /// Parameter this tool implements. |
| 29 | canonical: &'static str, |
| 30 | } |
| 31 | |
| 32 | pub const fn alias(alias: &'static str, canonical: &'static str) -> ParamAlias { |
| 33 | ParamAlias { alias, canonical } |
| 34 | } |
| 35 | |
| 36 | /// Path spellings shared by every file action. `path` is CodeWhale's |
| 37 | /// canonical name and the most common one in the field, but `file_path` is |
| 38 | /// widespread enough in training data to be worth accepting everywhere. |
| 39 | pub const PATH_ALIASES: &[ParamAlias] = &[alias("file_path", "path"), alias("filePath", "path")]; |
| 40 | |
| 41 | /// `input` with [`PATH_ALIASES`] folded onto `path`, exactly as the file |
| 42 | /// tools' `execute` does before touching disk. |
| 43 | /// |
| 44 | /// Policy gates (typed file rules, the workspace-write carve-out, repo law, |
| 45 | /// Auto-Review) must judge the path the tool will act on. Reading only the |
| 46 | /// raw `path` key misses accepted `file_path`/`filePath` spellings. A |
| 47 | /// conflicting pair, which `execute` refuses, is returned unchanged. |
| 48 | pub fn with_canonical_path_argument(input: &Value) -> Cow<'_, Value> { |
| 49 | if !PATH_ALIASES |
| 50 | .iter() |
| 51 | .any(|ParamAlias { alias, .. }| input.get(*alias).is_some()) |
| 52 | { |
| 53 | return Cow::Borrowed(input); |
| 54 | } |
| 55 | let mut folded = input.clone(); |
| 56 | match apply_param_aliases(&mut folded, PATH_ALIASES, "path") { |
| 57 | Ok(()) => Cow::Owned(folded), |
| 58 | Err(_) => Cow::Borrowed(input), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /// `path` and every spelling [`PATH_ALIASES`] folds onto it, for gates that |
| 63 | /// deliberately over-collect candidate targets. |
| 64 | pub fn path_argument_keys() -> impl Iterator<Item = &'static str> { |
| 65 | std::iter::once("path").chain(PATH_ALIASES.iter().map(|ParamAlias { alias, .. }| *alias)) |
| 66 | } |
| 67 | |
| 68 | /// Apply `aliases` to `input`, in place. |
| 69 | /// |
| 70 | /// An alias is consumed only when the canonical key is absent. When both are |
| 71 | /// present and *equal* the alias is dropped as a harmless duplicate; when both |
| 72 | /// are present and disagree the call fails, because guessing which one the |
| 73 | /// model meant is exactly the fabrication this path exists to prevent. |
| 74 | pub fn apply_param_aliases( |
| 75 | input: &mut Value, |
| 76 | aliases: &[ParamAlias], |
| 77 | tool_label: &str, |
| 78 | ) -> Result<(), ToolError> { |
| 79 | let Some(obj) = input.as_object_mut() else { |
| 80 | return Ok(()); |
| 81 | }; |
| 82 | |
| 83 | for ParamAlias { alias, canonical } in aliases { |
| 84 | let Some(alias_value) = obj.remove(*alias) else { |
| 85 | continue; |
| 86 | }; |
| 87 | match obj.get(*canonical) { |
| 88 | None => { |
| 89 | obj.insert((*canonical).to_string(), alias_value); |
| 90 | } |
| 91 | Some(existing) if existing == &alias_value => {} |
| 92 | Some(_) => { |
| 93 | return Err(ToolError::invalid_input(format!( |
| 94 | "{tool_label} received both `{canonical}` and its alias `{alias}` with different values, so the intended argument is ambiguous; nothing was changed. Pass only `{canonical}`." |
| 95 | ))); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | Ok(()) |
| 101 | } |
| 102 |