返回 CodeWhale
canonical_action.rs
根目录 / crates / execpolicy / src / canonical_action.rs
1 //! Semantic aliases for the model-facing action tools.
2 //!
3 //! `Bash`, `File`, `Git`, `Run`, `Web`, and `rlm` deliberately keep their
4 //! canonical names at the execution and audit boundaries. Presentation and
5 //! policy consumers, however, still understand the older per-action names.
6 //! Resolve that semantic name in one place so live calls and saved legacy
7 //! transcripts receive identical downstream behavior without rewriting the
8 //! original call.
9 //!
10 //! This table is not documentation — it is the **action-policy seam**. A
11 //! permission check that denies `fetch_url` only reaches `Web{action:"fetch"}`
12 //! because the pair is listed here. A family that is missing from the table is
13 //! a family whose actions no deny list can see, which is why `rlm` was added:
14 //! `rlm{action:"open", url:...}` calls `FetchUrlTool` *inside the process*,
15 //! under its own name, and a name-keyed deny list never sees that call.
16
17 use serde_json::Value;
18
19 pub const CANONICAL_ACTION_ALIASES: &[(&str, &str, &str)] = &[
20 ("bash", "run", "exec_shell"),
21 ("bash", "wait", "exec_shell_wait"),
22 ("bash", "interact", "exec_shell_interact"),
23 ("bash", "cancel", "exec_shell_cancel"),
24 ("Bash", "run", "exec_shell"),
25 ("Bash", "wait", "exec_shell_wait"),
26 ("Bash", "interact", "exec_shell_interact"),
27 ("Bash", "cancel", "exec_shell_cancel"),
28 ("File", "read", "read_file"),
29 ("File", "list", "list_dir"),
30 ("File", "search_name", "file_search"),
31 ("File", "search_content", "grep_files"),
32 ("File", "write", "write_file"),
33 ("File", "edit", "edit_file"),
34 ("File", "patch", "apply_patch"),
35 ("Git", "status", "git_status"),
36 ("Git", "diff", "git_diff"),
37 ("Git", "log", "git_log"),
38 ("Git", "show", "git_show"),
39 ("Git", "blame", "git_blame"),
40 ("Git", "commit_plan", "git_commit_plan"),
41 ("Git", "fetch", "git_fetch"),
42 ("Git", "merge_tree", "git_merge_tree"),
43 ("Git", "review_scope", "git_review_scope"),
44 ("Run", "tests", "run_tests"),
45 ("Run", "verifiers", "run_verifiers"),
46 ("Web", "search", "web_search"),
47 ("Web", "fetch", "fetch_url"),
48 ("Web", "wait", "wait_for_dev_server"),
49 // The RLM session family. `open` reaches the network (it fetches a `url`
50 // through `FetchUrlTool` in-process) and `eval` runs operator-supplied
51 // Python against a live kernel — sockets and filesystem both. The other
52 // three actions are bounded local metadata. Listing every pair is what lets
53 // a deny list keep the local half and remove the reaching half, instead of
54 // having to choose between the whole family and nothing.
55 ("rlm", "session_objects", "rlm_session_objects"),
56 ("rlm", "open", "rlm_open"),
57 ("rlm", "eval", "rlm_eval"),
58 ("rlm", "configure", "rlm_configure"),
59 ("rlm", "close", "rlm_close"),
60 // The durable-work families. These were absent for the same reason `rlm`
61 // was: they are model-visible under one canonical name (`tasks`,
62 // `automation`, `github`) and their per-action legacy names are registered
63 // as *hidden* aliases. A deny list naming `task_gate_run` therefore never
64 // saw `tasks{action:"gate_run"}`, and the action-enum pruner never saw the
65 // family at all — so an operator ceiling could not express "durable task
66 // bookkeeping, yes; running a gate command, no".
67 //
68 // `gate_run` runs an operator-supplied command, `automation.run` executes a
69 // stored automation, and the mutating `automation.*` actions schedule agent
70 // runs with their own cwd. All three are execution primitives spelled as
71 // bookkeeping, which is exactly the shape
72 // `execution_envelope` classifies from capabilities.
73 ("tasks", "create", "task_create"),
74 ("tasks", "list", "task_list"),
75 ("tasks", "read", "task_read"),
76 ("tasks", "cancel", "task_cancel"),
77 ("tasks", "gate_run", "task_gate_run"),
78 ("tasks", "pr_attempt_record", "pr_attempt_record"),
79 ("tasks", "pr_attempt_list", "pr_attempt_list"),
80 ("tasks", "pr_attempt_read", "pr_attempt_read"),
81 ("tasks", "pr_attempt_preflight", "pr_attempt_preflight"),
82 ("automation", "create", "automation_create"),
83 ("automation", "list", "automation_list"),
84 ("automation", "read", "automation_read"),
85 ("automation", "update", "automation_update"),
86 ("automation", "pause", "automation_pause"),
87 ("automation", "resume", "automation_resume"),
88 ("automation", "adopt", "automation_adopt"),
89 ("automation", "delete", "automation_delete"),
90 ("automation", "run", "automation_run"),
91 ("github", "issue_context", "github_issue_context"),
92 ("github", "pr_context", "github_pr_context"),
93 ("github", "comment", "github_comment"),
94 ("github", "close_issue", "github_close_issue"),
95 ("github", "close_pr", "github_close_pr"),
96 ("github", "report_draft", "github_report_draft"),
97 ("github", "report_read", "github_report_read"),
98 ];
99
100 /// The conservative action label policy uses when the model omits `action`.
101 ///
102 /// This is a *policy* fallback only. Execution rejects an actionless call in
103 /// every family (see `required_action`); approval and parallel-safety
104 /// predicates cannot return an error, so they still need a label, and it must
105 /// be the family's least dangerous action.
106 ///
107 /// `None` means the family never had even a policy default — `rlm`'s contract:
108 /// `RlmTool::resolve_action` errors rather than guessing.
109 /// Policy still resolves an *explicit* action for such a family — see
110 /// [`canonical_action_alias`].
111 #[must_use]
112 pub fn action_family_default(tool_name: &str) -> Option<Option<&'static str>> {
113 match tool_name {
114 "bash" | "Bash" => Some(Some("run")),
115 "File" => Some(Some("read")),
116 "Git" => Some(Some("status")),
117 "Run" => Some(Some("tests")),
118 "Web" => Some(Some("search")),
119 // Families whose wrappers reject an actionless call rather than
120 // guessing. Policy still resolves an *explicit* action for them.
121 "rlm" | "tasks" | "automation" | "github" => Some(None),
122 _ => None,
123 }
124 }
125
126 /// Whether `tool_name` is a model-facing action family whose `action` enum
127 /// policy may prune.
128 ///
129 /// Derived from [`action_family_default`] rather than spelled out at each call
130 /// site: a hard-coded family list that falls behind the alias table is a family
131 /// whose actions stay visible after policy removed them.
132 #[must_use]
133 pub fn is_action_family(tool_name: &str) -> bool {
134 action_family_default(tool_name).is_some()
135 }
136
137 /// Resolve a canonical action tool to the legacy name for that exact action.
138 ///
139 /// A missing action falls back to the family's conservative default so the
140 /// *policy* label is never absent; execution itself refuses the call (see
141 /// `required_action`). Unknown actions stay canonical so policy remains
142 /// conservative and the eventual tool error is attributed to the call the
143 /// model actually made.
144 ///
145 /// A family with no default (`rlm`) still resolves an **explicit** action. The
146 /// earlier shape returned the family name for any such call, which meant
147 /// `rlm{action:"eval"}` never resolved to `rlm_eval` and therefore never met a
148 /// deny list entry naming it.
149 #[must_use]
150 pub fn canonical_action_alias<'a>(tool_name: &'a str, input: &Value) -> &'a str {
151 // The new model-facing file primitives deliberately reuse the old
152 // semantic policy names. This keeps permissions.toml, repo law, resource
153 // envelopes, approval caches, audit aggregation, and saved policy state
154 // compatible across the presentation change.
155 match tool_name {
156 "read" => return "read_file",
157 "write" => return "write_file",
158 "edit" => return "edit_file",
159 _ => {}
160 }
161 let Some(default_action) = action_family_default(tool_name) else {
162 return tool_name;
163 };
164 let Some(action) = input
165 .get("action")
166 .and_then(Value::as_str)
167 .or(default_action)
168 else {
169 return tool_name;
170 };
171
172 CANONICAL_ACTION_ALIASES
173 .iter()
174 .find_map(|(family, candidate_action, alias)| {
175 (*family == tool_name && *candidate_action == action).then_some(*alias)
176 })
177 .unwrap_or(tool_name)
178 }
179
179 lines RUST