use crate::{
ProcessActionError, ProcessActionIntent, ProcessExecutionEvidence,
tool::patch_evidence::{WorkspacePatchExecutionEvidence, WorkspacePatchProposal},
};
use merry_core::{PendingToolCall, ToolCallId, ToolName};
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ToolActionKind {
ReadOnly,
RuntimeControl,
WorkspaceWrite,
CommandExec,
Network,
TrustedExternal,
}
impl ToolActionKind {
#[must_use]
pub fn is_mutating(self) -> bool {
matches!(
self,
Self::WorkspaceWrite | Self::CommandExec | Self::Network
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ActionProposal {
pub(super) tool_call_id: ToolCallId,
pub(super) tool_name: ToolName,
pub(super) action_kind: ToolActionKind,
pub(super) label: String,
pub(super) subject: String,
pub(super) summary: String,
pub(super) evidence: ActionProposalEvidence,
}
impl ActionProposal {
pub fn new(
call: &PendingToolCall,
action_kind: ToolActionKind,
label: impl Into<String>,
subject: impl Into<String>,
summary: impl Into<String>,
evidence: ActionProposalEvidence,
) -> Result<Self, ActionProposalError> {
if action_kind == ToolActionKind::ReadOnly {
return Err(ActionProposalError::ReadOnlyAction);
}
validate_proposal_evidence_matches_action_kind(action_kind, &evidence)?;
Ok(Self {
tool_call_id: call.id().clone(),
tool_name: call.name().clone(),
action_kind,
label: validate_compact_proposal_text("label", label.into())?,
subject: validate_compact_proposal_text("subject", subject.into())?,
summary: validate_compact_proposal_text("summary", summary.into())?,
evidence,
})
}
#[must_use]
pub fn tool_call_id(&self) -> &ToolCallId {
&self.tool_call_id
}
#[must_use]
pub fn tool_name(&self) -> &ToolName {
&self.tool_name
}
#[must_use]
pub fn action_kind(&self) -> ToolActionKind {
self.action_kind
}
#[must_use]
pub fn label(&self) -> &str {
&self.label
}
#[must_use]
pub fn subject(&self) -> &str {
&self.subject
}
#[must_use]
pub fn summary(&self) -> &str {
&self.summary
}
#[must_use]
pub fn evidence(&self) -> &ActionProposalEvidence {
&self.evidence
}
pub(crate) fn validate_for_call(
&self,
call: &PendingToolCall,
action_kind: ToolActionKind,
) -> Result<(), &'static str> {
if self.tool_call_id != *call.id() {
return Err("proposal tool call id does not match pending call");
}
if self.tool_name != *call.name() {
return Err("proposal tool name does not match pending call");
}
if self.action_kind != action_kind {
return Err("proposal action kind does not match registered tool");
}
if !self.evidence.matches_action_kind(action_kind) {
return Err("proposal evidence does not match registered tool action kind");
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActionProposalEvidence {
WorkspacePatch(WorkspacePatchProposal),
ProcessAction(ProcessActionIntent),
}
impl ActionProposalEvidence {
pub(super) fn matches_action_kind(&self, action_kind: ToolActionKind) -> bool {
matches!(
(action_kind, self),
(ToolActionKind::WorkspaceWrite, Self::WorkspacePatch(_))
| (ToolActionKind::CommandExec, Self::ProcessAction(_))
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ActionExecutionEvidence {
WorkspacePatch(WorkspacePatchExecutionEvidence),
ProcessAction(ProcessExecutionEvidence),
}
impl ActionExecutionEvidence {
pub(crate) fn matches_action_kind(&self, action_kind: ToolActionKind) -> bool {
matches!(
(action_kind, self),
(ToolActionKind::WorkspaceWrite, Self::WorkspacePatch(_))
| (ToolActionKind::CommandExec, Self::ProcessAction(_))
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum ActionProposalError {
#[error("read-only actions do not need action proposals")]
ReadOnlyAction,
#[error("action proposal {field} {reason}")]
InvalidText {
field: &'static str,
reason: &'static str,
},
#[error("workspace patch proposal {field} {reason}")]
InvalidWorkspacePatch {
field: &'static str,
reason: &'static str,
},
#[error("action proposal evidence does not match action kind {action_kind:?}")]
EvidenceActionKindMismatch {
action_kind: ToolActionKind,
},
#[error("process action proposal metadata invalid: {source}")]
InvalidProcessAction {
#[from]
source: ProcessActionError,
},
}
pub(super) const MAX_ACTION_PROPOSAL_TEXT_BYTES: usize = 512;
pub(super) fn validate_compact_proposal_text(
field: &'static str,
value: String,
) -> Result<String, ActionProposalError> {
if value.trim().is_empty() {
return Err(ActionProposalError::InvalidText {
field,
reason: "must not be blank",
});
}
if value.len() > MAX_ACTION_PROPOSAL_TEXT_BYTES {
return Err(ActionProposalError::InvalidText {
field,
reason: "exceeds the byte limit",
});
}
if value.chars().any(char::is_control) {
return Err(ActionProposalError::InvalidText {
field,
reason: "must not contain control characters",
});
}
Ok(value)
}
pub(super) fn validate_proposal_evidence_matches_action_kind(
action_kind: ToolActionKind,
evidence: &ActionProposalEvidence,
) -> Result<(), ActionProposalError> {
if evidence.matches_action_kind(action_kind) {
Ok(())
} else {
Err(ActionProposalError::EvidenceActionKindMismatch { action_kind })
}
}