| 1 | use super::canonical_action_test_support::*; |
| 2 | use codewhale_execpolicy::canonical_action::*; |
| 3 | use serde_json::json; |
| 4 | |
| 5 | #[test] |
| 6 | fn retired_names_are_not_model_visible() { |
| 7 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 8 | let registry = full_native_registry(tmp.path()); |
| 9 | for tool in registry.all() { |
| 10 | let name = tool.name().to_string(); |
| 11 | assert!( |
| 12 | !(tool.model_visible() && RETIRED_TOOL_NAMES.contains(&name.as_str())), |
| 13 | "`{name}` is model-visible, so it is live rather than retired" |
| 14 | ); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn shared_prompt_text_names_only_callable_tools() { |
| 20 | assert_text_names_only_callable_tools( |
| 21 | "GOAL_CONTINUATION_PROMPT", |
| 22 | crate::prompts::GOAL_CONTINUATION_PROMPT, |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | /// The catalog is re-sent on every request, so a retired name in it is a |
| 27 | /// per-turn lie to every model. `verifier.rs` already guarded one such |
| 28 | /// description by hand; this covers the whole advertised surface at once. |
| 29 | #[test] |
| 30 | fn no_advertised_tool_teaches_a_retired_name() { |
| 31 | use crate::tools::registry::ToolRegistryBuilder; |
| 32 | use crate::tools::spec::ToolContext; |
| 33 | |
| 34 | let tmp = tempfile::tempdir().expect("tempdir"); |
| 35 | let registry = ToolRegistryBuilder::new() |
| 36 | .with_file_tools() |
| 37 | .with_search_tools() |
| 38 | .with_git_tools() |
| 39 | .with_git_history_tools() |
| 40 | .with_test_runner_tool() |
| 41 | .with_web_tools() |
| 42 | .with_patch_tools() |
| 43 | .build(ToolContext::new(tmp.path().to_path_buf())); |
| 44 | |
| 45 | for tool in registry.to_api_tools() { |
| 46 | let advertised = format!("{} {}", tool.description, tool.input_schema); |
| 47 | for retired in RETIRED_TOOL_NAMES { |
| 48 | assert!( |
| 49 | !advertised.contains(retired), |
| 50 | "tool `{}` advertises the retired name `{retired}`; \ |
| 51 | name the canonical action form instead", |
| 52 | tool.name |
| 53 | ); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | fn every_canonical_action_resolves_to_its_legacy_semantic_alias() { |
| 60 | for (family, action, alias) in CANONICAL_ACTION_ALIASES { |
| 61 | assert_eq!( |
| 62 | canonical_action_alias(family, &json!({"action": action})), |
| 63 | *alias, |
| 64 | "{family}.{action}" |
| 65 | ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn lowercase_primitives_preserve_legacy_policy_names() { |
| 71 | assert_eq!(canonical_action_alias("read", &json!({})), "read_file"); |
| 72 | assert_eq!(canonical_action_alias("write", &json!({})), "write_file"); |
| 73 | assert_eq!(canonical_action_alias("edit", &json!({})), "edit_file"); |
| 74 | assert_eq!( |
| 75 | canonical_action_alias("bash", &json!({"command": "pwd"})), |
| 76 | "exec_shell" |
| 77 | ); |
| 78 | assert_eq!( |
| 79 | canonical_action_alias("bash", &json!({"action": "cancel"})), |
| 80 | "exec_shell_cancel" |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | /// Execution refuses an actionless call; policy still needs a label for |
| 85 | /// it, and that label must stay the family's most conservative action. |
| 86 | #[test] |
| 87 | fn actionless_calls_keep_a_conservative_policy_label() { |
| 88 | for (family, alias) in [ |
| 89 | ("Bash", "exec_shell"), |
| 90 | ("File", "read_file"), |
| 91 | ("Git", "git_status"), |
| 92 | ("Run", "run_tests"), |
| 93 | ("Web", "web_search"), |
| 94 | ] { |
| 95 | assert_eq!( |
| 96 | canonical_action_alias(family, &json!({})), |
| 97 | alias, |
| 98 | "{family}" |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn legacy_unknown_and_invalid_calls_keep_their_original_names() { |
| 105 | for name in ["exec_shell", "read_file", "future_tool"] { |
| 106 | assert_eq!(canonical_action_alias(name, &json!({})), name); |
| 107 | } |
| 108 | assert_eq!( |
| 109 | canonical_action_alias("File", &json!({"action": "delete"})), |
| 110 | "File" |
| 111 | ); |
| 112 | assert_eq!( |
| 113 | canonical_action_alias("Bash", &json!({"action": 42})), |
| 114 | "exec_shell" |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /// A family with no execution default still resolves an explicit action. |
| 119 | /// Without this, `rlm{action:"eval"}` resolves to `rlm` and slips past every |
| 120 | /// deny list entry that names `rlm_eval`. |
| 121 | #[test] |
| 122 | fn a_family_without_a_default_still_resolves_an_explicit_action() { |
| 123 | assert_eq!( |
| 124 | canonical_action_alias("rlm", &json!({"action": "eval"})), |
| 125 | "rlm_eval" |
| 126 | ); |
| 127 | assert_eq!( |
| 128 | canonical_action_alias("rlm", &json!({"action": "open", "url": "https://x.test/a"})), |
| 129 | "rlm_open" |
| 130 | ); |
| 131 | // No action, no default: nothing to resolve, and `RlmTool` will reject |
| 132 | // the call on its own terms. |
| 133 | assert_eq!(canonical_action_alias("rlm", &json!({})), "rlm"); |
| 134 | assert_eq!( |
| 135 | canonical_action_alias("rlm", &json!({"action": "nope"})), |
| 136 | "rlm" |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /// Every family named in the alias table must be recognised as a family, or |
| 141 | /// its actions are unprunable by the visibility filter. |
| 142 | #[test] |
| 143 | fn every_aliased_family_is_a_known_action_family() { |
| 144 | for (family, _, _) in CANONICAL_ACTION_ALIASES { |
| 145 | assert!( |
| 146 | is_action_family(family), |
| 147 | "{family} is aliased but not registered as an action family" |
| 148 | ); |
| 149 | } |
| 150 | assert!(!is_action_family("read_file")); |
| 151 | assert!(!is_action_family("future_tool")); |
| 152 | } |
| 153 |