Skip to main content

harn_vm/
autonomy.rs

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
16/// Stable diagnostic prefix for a deny driven by the `needs-human` autonomy
17/// class. Approval surfaces (Slack, IDE, portal) match on this code to render
18/// the deny distinctly from a normal tier-based block.
19pub const HARN_AUT_NEEDS_HUMAN_CODE: &str = "HARN-AUT-NEEDS-HUMAN";
20
21/// Canonical string value of the `needs-human` autonomy class. Mirrors
22/// `RepairSafety::NeedsHuman.as_str()` from `harn-parser` so the autonomy
23/// surface and the repair-safety surface stay in lockstep.
24pub 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    /// Mark the whole policy as `needs-human`: every side-effecting builtin
41    /// covered by this policy raises a structured `HARN-AUT-NEEDS-HUMAN`
42    /// deny, regardless of the resolved `autonomy_tier`.
43    #[serde(default)]
44    pub requires_human: bool,
45    /// Per-action or per-class needs-human tags. Entries match either the
46    /// builtin name (`write_file`) or the action class (`fs.write`).
47    /// Mutually exclusive with auto-apply: an entry here always wins over
48    /// any tier resolution.
49    #[serde(default, alias = "action_requires_human")]
50    pub requires_human_actions: BTreeSet<String>,
51    /// Per-agent needs-human tags. If an agent is listed here, every side
52    /// effect it attempts is treated as needs-human.
53    #[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    /// Resolve whether a given (agent, action) is tagged `needs-human` under
83    /// this policy. Any positive signal — blanket `requires_human`, a
84    /// per-agent tag, or a per-builtin/per-class tag — flips the action into
85    /// the needs-human discipline.
86    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    /// Whether this (agent, action) is tagged `needs-human` by the
155    /// currently-active autonomy policy. When true the dispatcher MUST
156    /// deny auto-apply regardless of `tier`.
157    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
185/// Per-task ambient-scope swap of the autonomy-policy stack. See
186/// `orchestration::ambient_scope`: a worker inherits its parent's autonomy
187/// tier, and `push_autonomy_policy` guards are held across `.await`, so the
188/// stack must follow the task rather than leak to interleaved siblings.
189pub(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        // `needs-human` always needs the async enforcement path so the
203        // dispatcher can emit the structured deny + approval-request,
204        // even when the resolved tier is `ActAuto`.
205        .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                "verification_profiles_set",
253                "verification_profile_record_run",
254            ],
255            "metadata.write",
256            "metadata.write",
257        )
258    })
259    .or_else(|| {
260        first_matching_action(
261            name,
262            &["checkpoint", "checkpoint_delete", "checkpoint_clear"],
263            "checkpoint.write",
264            "checkpoint.write",
265        )
266    })
267    .or_else(|| {
268        first_matching_action(
269            name,
270            &[
271                "sse_server_response",
272                "sse_server_send",
273                "sse_server_heartbeat",
274                "sse_server_flush",
275                "sse_server_close",
276                "sse_server_cancel",
277                "sse_server_mock_receive",
278                "sse_server_mock_disconnect",
279            ],
280            "network.sse.write",
281            "network.sse",
282        )
283    })
284    .or_else(|| {
285        first_matching_action(
286            name,
287            &[
288                "__agent_state_write",
289                "__agent_state_delete",
290                "__agent_state_handoff",
291            ],
292            "agent_state.write",
293            "agent_state.write",
294        )
295    })
296    .or_else(|| first_matching_action(name, &["mcp_release"], "mcp.release", "mcp.release"))
297    .or_else(|| {
298        first_matching_action(
299            name,
300            &[
301                "git.worktree.create",
302                "git.worktree.remove",
303                "git.fetch",
304                "git.rebase",
305                "git.push",
306            ],
307            "git.write",
308            "git.write",
309        )
310    })
311}
312
313pub async fn enforce_builtin_side_effect(
314    name: &str,
315    args: &[VmValue],
316) -> Result<Option<AutonomyDecision>, VmError> {
317    let Some(action) = side_effect_action_for_builtin(name) else {
318        return Ok(None);
319    };
320    let Some(identity) = current_identity(&action) else {
321        return Ok(None);
322    };
323    // `needs-human` is a transverse discipline: it forbids auto-apply even
324    // when the resolved tier is `ActAuto`. Check this *before* tier
325    // dispatch so no tier can ever override it.
326    if identity.requires_human {
327        emit_proposal_event(identity.tier, action, args).await?;
328        let request_id = append_needs_human_approval_request(&identity, action, args).await?;
329        append_enforcement_record(
330            &identity,
331            action,
332            args,
333            TrustOutcome::Denied,
334            Some(request_id.clone()),
335        )
336        .await?;
337        return Err(needs_human_deny_error(&identity, action, &request_id));
338    }
339    match identity.tier {
340        AutonomyTier::ActAuto => Ok(None),
341        AutonomyTier::Shadow => {
342            emit_proposal_event(identity.tier, action, args).await?;
343            append_enforcement_record(&identity, action, args, TrustOutcome::Denied, None).await?;
344            Ok(Some(AutonomyDecision::Skip(VmValue::Nil)))
345        }
346        AutonomyTier::Suggest => {
347            emit_proposal_event(identity.tier, action, args).await?;
348            let request_id = append_nonblocking_approval_request(&identity, action, args).await?;
349            append_enforcement_record(
350                &identity,
351                action,
352                args,
353                TrustOutcome::Denied,
354                Some(request_id),
355            )
356            .await?;
357            Ok(Some(AutonomyDecision::Skip(VmValue::Nil)))
358        }
359        AutonomyTier::ActWithApproval => {
360            let approval = request_approval_before_effect(&identity, action, args).await?;
361            append_enforcement_record(
362                &identity,
363                action,
364                args,
365                TrustOutcome::Success,
366                approval.request_id,
367            )
368            .await?;
369            Ok(Some(AutonomyDecision::AllowApproved))
370        }
371    }
372}
373
374fn current_identity(action: &SideEffectAction) -> Option<AutonomyIdentity> {
375    let scoped = current_autonomy_policy();
376    let dispatch = current_dispatch_context();
377    let agent_id = scoped
378        .as_ref()
379        .and_then(|policy| policy.agent_id.clone())
380        .or_else(|| dispatch.as_ref().map(|context| context.agent_id.clone()))
381        .unwrap_or_else(|| "runtime".to_string());
382    let tier = scoped
383        .as_ref()
384        .and_then(|policy| policy.effective_tier_for(&agent_id, action))
385        .or_else(|| dispatch.as_ref().map(|context| context.autonomy_tier))?;
386    let trace_id = dispatch
387        .as_ref()
388        .map(|context| context.trigger_event.trace_id.0.clone())
389        .unwrap_or_else(|| format!("trace-{}", Uuid::now_v7()));
390    let reviewers = scoped
391        .as_ref()
392        .map(|policy| policy.reviewers.clone())
393        .filter(|reviewers| !reviewers.is_empty())
394        .unwrap_or_default();
395    let requires_human = scoped
396        .as_ref()
397        .map(|policy| policy.is_needs_human(&agent_id, action))
398        .unwrap_or(false);
399    Some(AutonomyIdentity {
400        agent_id,
401        trace_id,
402        tier,
403        reviewers,
404        requires_human,
405    })
406}
407
408fn detail_for(action: SideEffectAction, args: &[VmValue]) -> JsonValue {
409    serde_json::json!({
410        "builtin": action.builtin,
411        "action_class": action.class,
412        "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
413    })
414}
415
416fn needs_human_detail(action: SideEffectAction, args: &[VmValue]) -> JsonValue {
417    let mut detail = detail_for(action, args);
418    if let Some(obj) = detail.as_object_mut() {
419        obj.insert(
420            "autonomy_class".to_string(),
421            JsonValue::String(NEEDS_HUMAN_AUTONOMY_CLASS.to_string()),
422        );
423        obj.insert("requires_human".to_string(), JsonValue::Bool(true));
424        obj.insert(
425            "deny_code".to_string(),
426            JsonValue::String(HARN_AUT_NEEDS_HUMAN_CODE.to_string()),
427        );
428    }
429    detail
430}
431
432async fn emit_proposal_event(
433    tier: AutonomyTier,
434    action: SideEffectAction,
435    args: &[VmValue],
436) -> Result<(), VmError> {
437    let Some(context) = current_dispatch_context() else {
438        return Ok(());
439    };
440    let Some(log) = active_event_log() else {
441        return Ok(());
442    };
443    let topic = Topic::new(crate::TRIGGER_OUTBOX_TOPIC)
444        .map_err(|error| VmError::Runtime(format!("autonomy proposal topic error: {error}")))?;
445    let mut headers = BTreeMap::new();
446    headers.insert(
447        "trace_id".to_string(),
448        context.trigger_event.trace_id.0.clone(),
449    );
450    headers.insert("agent".to_string(), context.agent_id.clone());
451    headers.insert("autonomy_tier".to_string(), tier.as_str().to_string());
452    let payload = serde_json::json!({
453        "agent": context.agent_id,
454        "action": context.action,
455        "builtin": action.builtin,
456        "action_class": action.class,
457        "args": args.iter().map(crate::llm::vm_value_to_json).collect::<Vec<_>>(),
458        "trace_id": context.trigger_event.trace_id.0,
459        "replay_of_event_id": context.replay_of_event_id,
460        "autonomy_tier": tier,
461        "proposal": true,
462    });
463    log.append(
464        &topic,
465        LogEvent::new("dispatch_proposed", payload).with_headers(headers),
466    )
467    .await
468    .map(|_| ())
469    .map_err(|error| VmError::Runtime(format!("failed to append autonomy proposal: {error}")))
470}
471
472async fn append_nonblocking_approval_request(
473    identity: &AutonomyIdentity,
474    action: SideEffectAction,
475    args: &[VmValue],
476) -> Result<String, VmError> {
477    let log = active_event_log().ok_or_else(|| {
478        categorized_error(
479            "autonomy approval requires an active event log",
480            ErrorCategory::ToolRejected,
481        )
482    })?;
483    append_approval_request_on(
484        &log,
485        identity.agent_id.clone(),
486        identity.trace_id.clone(),
487        action.class.to_string(),
488        detail_for(action, args),
489        identity.reviewers.clone(),
490    )
491    .await
492}
493
494/// Emit a non-blocking approval request tagged with the `needs-human`
495/// autonomy class. Surfaces (Slack-approval, IDE, portal) match on the
496/// `autonomy_class` field in the request payload's `detail` to render the
497/// pending row distinctly from a normal tier-driven approval ask.
498async fn append_needs_human_approval_request(
499    identity: &AutonomyIdentity,
500    action: SideEffectAction,
501    args: &[VmValue],
502) -> Result<String, VmError> {
503    let log = active_event_log().ok_or_else(|| {
504        categorized_error(
505            "needs-human autonomy class requires an active event log",
506            ErrorCategory::ToolRejected,
507        )
508    })?;
509    append_approval_request_on(
510        &log,
511        identity.agent_id.clone(),
512        identity.trace_id.clone(),
513        format!("{}#needs-human", action.class),
514        needs_human_detail(action, args),
515        identity.reviewers.clone(),
516    )
517    .await
518}
519
520/// Build the structured deny returned when a `needs-human`-tagged side
521/// effect is attempted. The message is prefixed with [`HARN_AUT_NEEDS_HUMAN_CODE`]
522/// so approval surfaces and structured-error consumers can match on a stable
523/// token rather than substring-matching the human-readable text.
524fn needs_human_deny_error(
525    identity: &AutonomyIdentity,
526    action: SideEffectAction,
527    request_id: &str,
528) -> VmError {
529    categorized_error(
530        format!(
531            "{code}: side effect `{builtin}` ({class}) is tagged `needs-human` for agent `{agent}`; \
532             auto-apply is forbidden regardless of autonomy tier `{tier}`. \
533             Approval request `{request_id}` was queued.",
534            code = HARN_AUT_NEEDS_HUMAN_CODE,
535            builtin = action.builtin,
536            class = action.class,
537            agent = identity.agent_id,
538            tier = identity.tier.as_str(),
539            request_id = request_id,
540        ),
541        ErrorCategory::ToolRejected,
542    )
543}
544
545struct ApprovalOutcome {
546    request_id: Option<String>,
547}
548
549async fn request_approval_before_effect(
550    identity: &AutonomyIdentity,
551    action: SideEffectAction,
552    args: &[VmValue],
553) -> Result<ApprovalOutcome, VmError> {
554    active_event_log().ok_or_else(|| {
555        categorized_error(
556            "act_with_approval requires an active event log",
557            ErrorCategory::ToolRejected,
558        )
559    })?;
560    let detail = detail_for(action, args);
561    let approval = crate::stdlib::hitl::request_approval_for_side_effect(
562        action.class,
563        detail,
564        identity.agent_id.clone(),
565        identity.reviewers.clone(),
566        vec![action.capability.to_string()],
567    )
568    .await?;
569    let request_id = approval
570        .as_dict()
571        .and_then(|dict| dict.get("request_id"))
572        .map(VmValue::display);
573    Ok(ApprovalOutcome { request_id })
574}
575
576async fn append_enforcement_record(
577    identity: &AutonomyIdentity,
578    action: SideEffectAction,
579    args: &[VmValue],
580    outcome: TrustOutcome,
581    request_id: Option<String>,
582) -> Result<(), VmError> {
583    let Some(log) = active_event_log() else {
584        return Ok(());
585    };
586    let mut record = TrustRecord::new(
587        identity.agent_id.clone(),
588        action.class.to_string(),
589        None,
590        outcome,
591        identity.trace_id.clone(),
592        identity.tier,
593    );
594    let enforcement = if identity.requires_human {
595        // `needs-human` always denies regardless of tier — record the
596        // distinct enforcement label so audit consumers can filter on it
597        // without re-deriving the discipline from policy snapshots.
598        "needs_human_denied"
599    } else {
600        match identity.tier {
601            AutonomyTier::Shadow => "shadow_noop",
602            AutonomyTier::Suggest => "suggest_approval_request",
603            AutonomyTier::ActWithApproval => "approval_granted",
604            AutonomyTier::ActAuto => "auto",
605        }
606    };
607    record.metadata.insert(
608        "autonomy.enforcement".to_string(),
609        serde_json::json!(enforcement),
610    );
611    record
612        .metadata
613        .insert("builtin".to_string(), serde_json::json!(action.builtin));
614    record
615        .metadata
616        .insert("action_class".to_string(), serde_json::json!(action.class));
617    // Every record carries an explicit autonomy class so the trust-graph
618    // record (`TrustRecord.metadata.autonomy_class`) flows downstream into
619    // approval surfaces and receipt envelopes. `needs-human` is mutually
620    // exclusive with the tier-based labels.
621    let autonomy_class = if identity.requires_human {
622        NEEDS_HUMAN_AUTONOMY_CLASS.to_string()
623    } else {
624        identity.tier.as_str().to_string()
625    };
626    record.metadata.insert(
627        "autonomy_class".to_string(),
628        serde_json::json!(autonomy_class),
629    );
630    record.metadata.insert(
631        "requires_human".to_string(),
632        serde_json::json!(identity.requires_human),
633    );
634    if identity.requires_human {
635        record.metadata.insert(
636            "deny_code".to_string(),
637            serde_json::json!(HARN_AUT_NEEDS_HUMAN_CODE),
638        );
639    }
640    record.metadata.insert(
641        "args".to_string(),
642        serde_json::json!(args
643            .iter()
644            .map(crate::llm::vm_value_to_json)
645            .collect::<Vec<_>>()),
646    );
647    if let Some(request_id) = request_id {
648        record.metadata.insert(
649            "approval_request_id".to_string(),
650            serde_json::json!(request_id),
651        );
652    }
653    append_trust_record(&log, &record)
654        .await
655        .map(|_| ())
656        .map_err(|error| VmError::Runtime(format!("autonomy trust graph append: {error}")))
657}