1use std::cell::RefCell;
2use std::collections::{BTreeMap, BTreeSet};
3use std::future::Future;
4use std::pin::Pin;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value as JsonValue;
8use uuid::Uuid;
9
10use crate::event_log::{active_event_log, EventLog, LogEvent, Topic};
11use crate::stdlib::hitl::append_approval_request_on;
12use crate::triggers::dispatcher::current_dispatch_context;
13use crate::trust_graph::{append_trust_record, AutonomyTier, TrustOutcome, TrustRecord};
14use crate::value::{categorized_error, ErrorCategory, VmError, VmValue};
15
16pub const HARN_AUT_NEEDS_HUMAN_CODE: &str = "HARN-AUT-NEEDS-HUMAN";
20
21pub const NEEDS_HUMAN_AUTONOMY_CLASS: &str = "needs-human";
25
26thread_local! {
27 static AUTONOMY_POLICY_STACK: RefCell<Vec<AutonomyPolicy>> = const { RefCell::new(Vec::new()) };
28}
29
30#[derive(Clone, Debug, Default, Deserialize, Serialize)]
31#[serde(default)]
32pub struct AutonomyPolicy {
33 pub agent_id: Option<String>,
34 pub autonomy_tier: Option<AutonomyTier>,
35 pub tier: Option<AutonomyTier>,
36 pub action_tiers: BTreeMap<String, AutonomyTier>,
37 pub agent_tiers: BTreeMap<String, AutonomyTier>,
38 pub agent_action_tiers: BTreeMap<String, BTreeMap<String, AutonomyTier>>,
39 pub reviewers: Vec<String>,
40 #[serde(default)]
44 pub requires_human: bool,
45 #[serde(default, alias = "action_requires_human")]
50 pub requires_human_actions: BTreeSet<String>,
51 #[serde(default)]
54 pub requires_human_agents: BTreeSet<String>,
55}
56
57impl AutonomyPolicy {
58 fn effective_tier_for(
59 &self,
60 agent_id: &str,
61 action: &SideEffectAction,
62 ) -> Option<AutonomyTier> {
63 self.agent_action_tiers
64 .get(agent_id)
65 .and_then(|tiers| {
66 tiers
67 .get(action.builtin)
68 .or_else(|| tiers.get(action.class))
69 .copied()
70 })
71 .or_else(|| self.agent_tiers.get(agent_id).copied())
72 .or_else(|| {
73 self.action_tiers
74 .get(action.builtin)
75 .or_else(|| self.action_tiers.get(action.class))
76 .copied()
77 })
78 .or(self.autonomy_tier)
79 .or(self.tier)
80 }
81
82 fn is_needs_human(&self, agent_id: &str, action: &SideEffectAction) -> bool {
87 if self.requires_human {
88 return true;
89 }
90 if self.requires_human_agents.contains(agent_id) {
91 return true;
92 }
93 if self.requires_human_actions.contains(action.builtin)
94 || self.requires_human_actions.contains(action.class)
95 {
96 return true;
97 }
98 false
99 }
100}
101
102fn action(
103 builtin: &'static str,
104 class: &'static str,
105 capability: &'static str,
106) -> SideEffectAction {
107 SideEffectAction {
108 builtin,
109 class,
110 capability,
111 }
112}
113
114fn workspace_write_action(builtin: &'static str, class: &'static str) -> SideEffectAction {
115 action(builtin, class, "workspace.write_text")
116}
117
118fn first_matching_action(
119 name: &str,
120 builtins: &[&'static str],
121 class: &'static str,
122 capability: &'static str,
123) -> Option<SideEffectAction> {
124 builtins
125 .iter()
126 .find(|builtin| **builtin == name)
127 .map(|builtin| action(builtin, class, capability))
128}
129
130fn first_workspace_write_action(
131 name: &str,
132 builtins: &[&'static str],
133 class: &'static str,
134) -> Option<SideEffectAction> {
135 builtins
136 .iter()
137 .find(|builtin| **builtin == name)
138 .map(|builtin| workspace_write_action(builtin, class))
139}
140
141#[derive(Clone, Copy, Debug, PartialEq, Eq)]
142pub struct SideEffectAction {
143 pub builtin: &'static str,
144 pub class: &'static str,
145 pub capability: &'static str,
146}
147
148#[derive(Clone, Debug)]
149struct AutonomyIdentity {
150 agent_id: String,
151 trace_id: String,
152 tier: AutonomyTier,
153 reviewers: Vec<String>,
154 requires_human: bool,
158}
159
160#[derive(Clone, Debug)]
161pub enum AutonomyDecision {
162 Skip(VmValue),
163 AllowApproved,
164}
165
166pub struct AutonomyPolicyGuard;
167
168impl Drop for AutonomyPolicyGuard {
169 fn drop(&mut self) {
170 AUTONOMY_POLICY_STACK.with(|stack| {
171 stack.borrow_mut().pop();
172 });
173 }
174}
175
176pub fn push_autonomy_policy(policy: AutonomyPolicy) -> AutonomyPolicyGuard {
177 AUTONOMY_POLICY_STACK.with(|stack| stack.borrow_mut().push(policy));
178 AutonomyPolicyGuard
179}
180
181pub fn current_autonomy_policy() -> Option<AutonomyPolicy> {
182 AUTONOMY_POLICY_STACK.with(|stack| stack.borrow().last().cloned())
183}
184
185pub(crate) fn swap_autonomy_policy_stack(next: Vec<AutonomyPolicy>) -> Vec<AutonomyPolicy> {
190 AUTONOMY_POLICY_STACK.with(|stack| std::mem::replace(&mut *stack.borrow_mut(), next))
191}
192
193pub fn is_side_effecting_builtin(name: &str) -> bool {
194 side_effect_action_for_builtin(name).is_some()
195}
196
197pub fn needs_async_side_effect_enforcement(name: &str) -> bool {
198 let Some(action) = side_effect_action_for_builtin(name) else {
199 return false;
200 };
201 current_identity(&action)
202 .is_some_and(|identity| identity.requires_human || identity.tier != AutonomyTier::ActAuto)
206}
207
208pub fn enforce_builtin_side_effect_boxed<'a>(
209 name: &'a str,
210 args: &'a [VmValue],
211) -> Pin<Box<dyn Future<Output = Result<Option<AutonomyDecision>, VmError>> + Send + 'a>> {
212 Box::pin(enforce_builtin_side_effect(name, args))
213}
214
215pub fn side_effect_action_for_builtin(name: &str) -> Option<SideEffectAction> {
216 first_workspace_write_action(
217 name,
218 &["write_file", "write_file_bytes", "append_file"],
219 "fs.write",
220 )
221 .or_else(|| first_workspace_write_action(name, &["mkdir"], "fs.mkdir"))
222 .or_else(|| first_workspace_write_action(name, &["mkdtemp"], "fs.mkdtemp"))
223 .or_else(|| first_workspace_write_action(name, &["copy_file"], "fs.copy"))
224 .or_else(|| first_matching_action(name, &["delete_file"], "fs.delete", "workspace.delete"))
225 .or_else(|| first_workspace_write_action(name, &["move_file"], "fs.move"))
226 .or_else(|| {
227 first_matching_action(
228 name,
229 &["exec", "exec_at", "shell", "shell_at"],
230 "process.exec",
231 "process.exec",
232 )
233 })
234 .or_else(|| first_matching_action(name, &["host_call"], "host.call", "host.call"))
235 .or_else(|| {
236 first_matching_action(
237 name,
238 &["store_set", "store_delete", "store_save", "store_clear"],
239 "store.write",
240 "store.write",
241 )
242 })
243 .or_else(|| {
244 first_matching_action(
245 name,
246 &[
247 "metadata_set",
248 "metadata_save",
249 "metadata_refresh_hashes",
250 "invalidate_facts",
251 "path_metadata_set",
252 ],
253 "metadata.write",
254 "metadata.write",
255 )
256 })
257 .or_else(|| {
258 first_matching_action(
259 name,
260 &["checkpoint", "checkpoint_delete", "checkpoint_clear"],
261 "checkpoint.write",
262 "checkpoint.write",
263 )
264 })
265 .or_else(|| {
266 first_matching_action(
267 name,
268 &[
269 "sse_server_response",
270 "sse_server_send",
271 "sse_server_heartbeat",
272 "sse_server_flush",
273 "sse_server_close",
274 "sse_server_cancel",
275 "sse_server_mock_receive",
276 "sse_server_mock_disconnect",
277 ],
278 "network.sse.write",
279 "network.sse",
280 )
281 })
282 .or_else(|| {
283 first_matching_action(
284 name,
285 &[
286 "__agent_state_write",
287 "__agent_state_delete",
288 "__agent_state_handoff",
289 ],
290 "agent_state.write",
291 "agent_state.write",
292 )
293 })
294 .or_else(|| first_matching_action(name, &["mcp_release"], "mcp.release", "mcp.release"))
295 .or_else(|| {
296 first_matching_action(
297 name,
298 &[
299 "git.worktree.create",
300 "git.worktree.remove",
301 "git.fetch",
302 "git.rebase",
303 "git.push",
304 ],
305 "git.write",
306 "git.write",
307 )
308 })
309}
310
311pub async fn enforce_builtin_side_effect(
312 name: &str,
313 args: &[VmValue],
314) -> Result<Option<AutonomyDecision>, VmError> {
315 let Some(action) = side_effect_action_for_builtin(name) else {
316 return Ok(None);
317 };
318 let Some(identity) = current_identity(&action) else {
319 return Ok(None);
320 };
321 if identity.requires_human {
325 emit_proposal_event(identity.tier, action, args).await?;
326 let request_id = append_needs_human_approval_request(&identity, action, args).await?;
327 append_enforcement_record(
328 &identity,
329 action,
330 args,
331 TrustOutcome::Denied,
332 Some(request_id.clone()),
333 )
334 .await?;
335 return Err(needs_human_deny_error(&identity, action, &request_id));
336 }
337 match identity.tier {
338 AutonomyTier::ActAuto => Ok(None),
339 AutonomyTier::Shadow => {
340 emit_proposal_event(identity.tier, action, args).await?;
341 append_enforcement_record(&identity, action, args, TrustOutcome::Denied, None).await?;
342 Ok(Some(AutonomyDecision::Skip(VmValue::Nil)))
343 }
344 AutonomyTier::Suggest => {
345 emit_proposal_event(identity.tier, action, args).await?;
346 let request_id = append_nonblocking_approval_request(&identity, action, args).await?;
347 append_enforcement_record(
348 &identity,
349 action,
350 args,
351 TrustOutcome::Denied,
352 Some(request_id),
353 )
354 .await?;
355 Ok(Some(AutonomyDecision::Skip(VmValue::Nil)))
356 }
357 AutonomyTier::ActWithApproval => {
358 let approval = request_approval_before_effect(&identity, action, args).await?;
359 append_enforcement_record(
360 &identity,
361 action,
362 args,
363 TrustOutcome::Success,
364 approval.request_id,
365 )
366 .await?;
367 Ok(Some(AutonomyDecision::AllowApproved))
368 }
369 }
370}
371
372fn current_identity(action: &SideEffectAction) -> Option<AutonomyIdentity> {
373 let scoped = current_autonomy_policy();
374 let dispatch = current_dispatch_context();
375 let agent_id = scoped
376 .as_ref()
377 .and_then(|policy| policy.agent_id.clone())
378 .or_else(|| dispatch.as_ref().map(|context| context.agent_id.clone()))
379 .unwrap_or_else(|| "runtime".to_string());
380 let tier = scoped
381 .as_ref()
382 .and_then(|policy| policy.effective_tier_for(&agent_id, action))
383 .or_else(|| dispatch.as_ref().map(|context| context.autonomy_tier))?;
384 let trace_id = dispatch
385 .as_ref()
386 .map(|context| context.trigger_event.trace_id.0.clone())
387 .unwrap_or_else(|| format!("trace-{}", Uuid::now_v7()));
388 let reviewers = scoped
389 .as_ref()
390 .map(|policy| policy.reviewers.clone())
391 .filter(|reviewers| !reviewers.is_empty())
392 .unwrap_or_default();
393 let requires_human = scoped
394 .as_ref()
395 .map(|policy| policy.is_needs_human(&agent_id, action))
396 .unwrap_or(false);
397 Some(AutonomyIdentity {
398 agent_id,
399 trace_id,
400 tier,
401 reviewers,
402 requires_human,
403 })
404}
405
406fn detail_for(action: SideEffectAction, args: &[VmValue]) -> JsonValue {
407 serde_json::json!({
408 "builtin": action.builtin,
409 "action_class": action.class,
410 "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
411 })
412}
413
414fn needs_human_detail(action: SideEffectAction, args: &[VmValue]) -> JsonValue {
415 let mut detail = detail_for(action, args);
416 if let Some(obj) = detail.as_object_mut() {
417 obj.insert(
418 "autonomy_class".to_string(),
419 JsonValue::String(NEEDS_HUMAN_AUTONOMY_CLASS.to_string()),
420 );
421 obj.insert("requires_human".to_string(), JsonValue::Bool(true));
422 obj.insert(
423 "deny_code".to_string(),
424 JsonValue::String(HARN_AUT_NEEDS_HUMAN_CODE.to_string()),
425 );
426 }
427 detail
428}
429
430async fn emit_proposal_event(
431 tier: AutonomyTier,
432 action: SideEffectAction,
433 args: &[VmValue],
434) -> Result<(), VmError> {
435 let Some(context) = current_dispatch_context() else {
436 return Ok(());
437 };
438 let Some(log) = active_event_log() else {
439 return Ok(());
440 };
441 let topic = Topic::new(crate::TRIGGER_OUTBOX_TOPIC)
442 .map_err(|error| VmError::Runtime(format!("autonomy proposal topic error: {error}")))?;
443 let mut headers = BTreeMap::new();
444 headers.insert(
445 "trace_id".to_string(),
446 context.trigger_event.trace_id.0.clone(),
447 );
448 headers.insert("agent".to_string(), context.agent_id.clone());
449 headers.insert("autonomy_tier".to_string(), tier.as_str().to_string());
450 let payload = serde_json::json!({
451 "agent": context.agent_id,
452 "action": context.action,
453 "builtin": action.builtin,
454 "action_class": action.class,
455 "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
456 "trace_id": context.trigger_event.trace_id.0,
457 "replay_of_event_id": context.replay_of_event_id,
458 "autonomy_tier": tier,
459 "proposal": true,
460 });
461 log.append(
462 &topic,
463 LogEvent::new("dispatch_proposed", payload).with_headers(headers),
464 )
465 .await
466 .map(|_| ())
467 .map_err(|error| VmError::Runtime(format!("failed to append autonomy proposal: {error}")))
468}
469
470async fn append_nonblocking_approval_request(
471 identity: &AutonomyIdentity,
472 action: SideEffectAction,
473 args: &[VmValue],
474) -> Result<String, VmError> {
475 let log = active_event_log().ok_or_else(|| {
476 categorized_error(
477 "autonomy approval requires an active event log",
478 ErrorCategory::ToolRejected,
479 )
480 })?;
481 append_approval_request_on(
482 &log,
483 identity.agent_id.clone(),
484 identity.trace_id.clone(),
485 action.class.to_string(),
486 detail_for(action, args),
487 identity.reviewers.clone(),
488 )
489 .await
490}
491
492async fn append_needs_human_approval_request(
497 identity: &AutonomyIdentity,
498 action: SideEffectAction,
499 args: &[VmValue],
500) -> Result<String, VmError> {
501 let log = active_event_log().ok_or_else(|| {
502 categorized_error(
503 "needs-human autonomy class requires an active event log",
504 ErrorCategory::ToolRejected,
505 )
506 })?;
507 append_approval_request_on(
508 &log,
509 identity.agent_id.clone(),
510 identity.trace_id.clone(),
511 format!("{}#needs-human", action.class),
512 needs_human_detail(action, args),
513 identity.reviewers.clone(),
514 )
515 .await
516}
517
518fn needs_human_deny_error(
523 identity: &AutonomyIdentity,
524 action: SideEffectAction,
525 request_id: &str,
526) -> VmError {
527 categorized_error(
528 format!(
529 "{code}: side effect `{builtin}` ({class}) is tagged `needs-human` for agent `{agent}`; \
530 auto-apply is forbidden regardless of autonomy tier `{tier}`. \
531 Approval request `{request_id}` was queued.",
532 code = HARN_AUT_NEEDS_HUMAN_CODE,
533 builtin = action.builtin,
534 class = action.class,
535 agent = identity.agent_id,
536 tier = identity.tier.as_str(),
537 request_id = request_id,
538 ),
539 ErrorCategory::ToolRejected,
540 )
541}
542
543struct ApprovalOutcome {
544 request_id: Option<String>,
545}
546
547async fn request_approval_before_effect(
548 identity: &AutonomyIdentity,
549 action: SideEffectAction,
550 args: &[VmValue],
551) -> Result<ApprovalOutcome, VmError> {
552 active_event_log().ok_or_else(|| {
553 categorized_error(
554 "act_with_approval requires an active event log",
555 ErrorCategory::ToolRejected,
556 )
557 })?;
558 let detail = detail_for(action, args);
559 let approval = crate::stdlib::hitl::request_approval_for_side_effect(
560 action.class,
561 detail,
562 identity.agent_id.clone(),
563 identity.reviewers.clone(),
564 vec![action.capability.to_string()],
565 )
566 .await?;
567 let request_id = approval
568 .as_dict()
569 .and_then(|dict| dict.get("request_id"))
570 .map(VmValue::display);
571 Ok(ApprovalOutcome { request_id })
572}
573
574async fn append_enforcement_record(
575 identity: &AutonomyIdentity,
576 action: SideEffectAction,
577 args: &[VmValue],
578 outcome: TrustOutcome,
579 request_id: Option<String>,
580) -> Result<(), VmError> {
581 let Some(log) = active_event_log() else {
582 return Ok(());
583 };
584 let mut record = TrustRecord::new(
585 identity.agent_id.clone(),
586 action.class.to_string(),
587 None,
588 outcome,
589 identity.trace_id.clone(),
590 identity.tier,
591 );
592 let enforcement = if identity.requires_human {
593 "needs_human_denied"
597 } else {
598 match identity.tier {
599 AutonomyTier::Shadow => "shadow_noop",
600 AutonomyTier::Suggest => "suggest_approval_request",
601 AutonomyTier::ActWithApproval => "approval_granted",
602 AutonomyTier::ActAuto => "auto",
603 }
604 };
605 record.metadata.insert(
606 "autonomy.enforcement".to_string(),
607 serde_json::json!(enforcement),
608 );
609 record
610 .metadata
611 .insert("builtin".to_string(), serde_json::json!(action.builtin));
612 record
613 .metadata
614 .insert("action_class".to_string(), serde_json::json!(action.class));
615 let autonomy_class = if identity.requires_human {
620 NEEDS_HUMAN_AUTONOMY_CLASS.to_string()
621 } else {
622 identity.tier.as_str().to_string()
623 };
624 record.metadata.insert(
625 "autonomy_class".to_string(),
626 serde_json::json!(autonomy_class),
627 );
628 record.metadata.insert(
629 "requires_human".to_string(),
630 serde_json::json!(identity.requires_human),
631 );
632 if identity.requires_human {
633 record.metadata.insert(
634 "deny_code".to_string(),
635 serde_json::json!(HARN_AUT_NEEDS_HUMAN_CODE),
636 );
637 }
638 record.metadata.insert(
639 "args".to_string(),
640 serde_json::json!(args
641 .iter()
642 .map(crate::llm::vm_value_to_json)
643 .collect::<Vec<_>>()),
644 );
645 if let Some(request_id) = request_id {
646 record.metadata.insert(
647 "approval_request_id".to_string(),
648 serde_json::json!(request_id),
649 );
650 }
651 append_trust_record(&log, &record)
652 .await
653 .map(|_| ())
654 .map_err(|error| VmError::Runtime(format!("autonomy trust graph append: {error}")))
655}