Skip to main content

merry_coding/runtime/
permission_policy.rs

1//! Permission mode selection and its mapping onto runtime permission review.
2//!
3//! The product surface names [`CodingPermissionMode`]. This module turns that
4//! choice into the [`CodingPermissionPolicy`] both parent and child coding
5//! runtimes apply. A denial is a tool result for the working model; it does
6//! not end the turn.
7
8use merry_runtime::{PermissionAdmissionSource, PermissionReviewMode, RuntimeBuilder};
9use std::sync::Arc;
10use thiserror::Error;
11
12/// What happens when `auto` review denies an action or cannot decide.
13#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
14pub enum CodingAutoFallback {
15    /// Ask the person at the terminal.
16    #[default]
17    Ask,
18    /// Return the denial to the working model and let that model end the turn.
19    Deny,
20}
21
22/// Who reviews permission requests, as selected by the product surface.
23#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
24pub enum CodingPermissionMode {
25    /// The approval-review model decides first.
26    #[default]
27    Auto,
28    /// The person at the terminal decides.
29    Ask,
30    /// Configured actions run without a review round.
31    AlwaysApprove,
32    /// Refuse every permission request without consulting a reviewer.
33    Deny,
34}
35
36/// Failure to construct a permission policy for a permission mode.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
38pub enum CodingPermissionPolicyError {
39    /// A selected mode needs a host admission source, but the surface did not provide one.
40    #[error("{mode:?} permission review requires a host admission source")]
41    HostAdmissionUnavailable { mode: CodingPermissionMode },
42}
43
44/// Permission configuration shared by parent and child coding runtimes.
45///
46/// This is the only coding-layer representation of permission mode and host
47/// admission. Host-dependent variants carry their source directly, so an
48/// incomplete host policy cannot be constructed by callers.
49#[derive(Clone, Default)]
50pub enum CodingPermissionPolicy {
51    /// Use the runtime's trust-level default.
52    #[default]
53    Default,
54    /// Route permission requests through model-backed review without a host fallback.
55    Required,
56    /// Route permission requests through the supplied host admission source.
57    HostDecisionOnly {
58        /// Host-owned admission source used by the runtime.
59        source: Arc<dyn PermissionAdmissionSource>,
60    },
61    /// Try model-backed review first and use the supplied host source as a fallback.
62    ModelThenHostFallback {
63        /// Host-owned admission source used by the runtime.
64        source: Arc<dyn PermissionAdmissionSource>,
65    },
66    /// Admit configured registered tools without an approval round.
67    FullyTrusted,
68    /// Reject every permission request without an approval round.
69    DenyAll,
70}
71
72impl CodingPermissionPolicy {
73    /// Route permission requests through model-backed review without a host fallback.
74    #[must_use]
75    pub const fn model_only() -> Self {
76        Self::Required
77    }
78
79    /// Route permission requests through the supplied host admission source.
80    #[must_use]
81    pub fn host_decision_only(source: Arc<dyn PermissionAdmissionSource>) -> Self {
82        Self::HostDecisionOnly { source }
83    }
84
85    /// Try model-backed review first and use the supplied host source as a fallback.
86    #[must_use]
87    pub fn model_then_host_fallback(source: Arc<dyn PermissionAdmissionSource>) -> Self {
88        Self::ModelThenHostFallback { source }
89    }
90
91    /// Admit configured registered tools without an approval round.
92    #[must_use]
93    pub const fn fully_trusted() -> Self {
94        Self::FullyTrusted
95    }
96
97    /// Reject every permission request without an approval round.
98    #[must_use]
99    pub const fn deny_all() -> Self {
100        Self::DenyAll
101    }
102
103    /// Selects the runtime policy for one product permission mode.
104    ///
105    /// `fallback` is read only for [`CodingPermissionMode::Auto`]. A mode that
106    /// asks a person is constructed only when the caller supplies a host
107    /// admission source.
108    pub fn for_permission_mode(
109        mode: CodingPermissionMode,
110        fallback: CodingAutoFallback,
111        host_source: Option<Arc<dyn PermissionAdmissionSource>>,
112    ) -> Result<Self, CodingPermissionPolicyError> {
113        let host_reviewed = |reviewed: fn(Arc<dyn PermissionAdmissionSource>) -> Self| {
114            host_source
115                .map(reviewed)
116                .ok_or(CodingPermissionPolicyError::HostAdmissionUnavailable { mode })
117        };
118        match mode {
119            CodingPermissionMode::AlwaysApprove => Ok(Self::fully_trusted()),
120            CodingPermissionMode::Deny => Ok(Self::deny_all()),
121            CodingPermissionMode::Ask => host_reviewed(Self::host_decision_only),
122            CodingPermissionMode::Auto => match fallback {
123                CodingAutoFallback::Deny => Ok(Self::model_only()),
124                CodingAutoFallback::Ask => host_reviewed(Self::model_then_host_fallback),
125            },
126        }
127    }
128
129    pub(super) fn apply_to(&self, mut builder: RuntimeBuilder) -> RuntimeBuilder {
130        match self {
131            Self::Default => {}
132            Self::Required => {
133                builder = builder.permission_review_mode(PermissionReviewMode::Required);
134            }
135            Self::HostDecisionOnly { source } => {
136                builder = builder
137                    .permission_review_mode(PermissionReviewMode::HostDecisionOnly)
138                    .permission_admission_source(Arc::clone(source));
139            }
140            Self::ModelThenHostFallback { source } => {
141                builder = builder
142                    .permission_review_mode(PermissionReviewMode::ModelThenHostFallback)
143                    .permission_admission_source(Arc::clone(source));
144            }
145            Self::FullyTrusted => {
146                builder = builder.permission_review_mode(PermissionReviewMode::FullyTrusted);
147            }
148            Self::DenyAll => {
149                builder = builder.permission_review_mode(PermissionReviewMode::DenyAll);
150            }
151        }
152        builder
153    }
154}