use std::sync::Arc;
use merry_core::PendingToolCall;
use merry_runtime::{
ToolActionPreflight, ToolActionProposalFuture, ToolExecutionContext, ToolExecutionError,
ToolExecutor, ToolExecutorFuture,
};
use crate::{
APPLY_PATCH_TOOL,
errors::{ERROR_INVALID_ARGUMENTS, failed_outcome},
state::WorkspaceToolState,
trace::{
WorkspaceTraceFinish, WorkspaceTraceTarget, invalid_arguments_outcome,
trace_workspace_tool_finish, trace_workspace_tool_start,
},
};
mod apply;
mod diagnostic;
pub(crate) mod envelope;
mod input;
pub(crate) use input::{ApplyPatchInput, spec};
mod parse;
mod plan;
mod types;
#[cfg(test)]
pub(crate) use plan::apply_patch_blocking;
pub(crate) use plan::{apply_patch_blocking_checked, propose_apply_patch_blocking_checked};
#[cfg(test)]
pub(crate) use types::stable_content_fingerprint;
#[derive(Debug)]
pub(crate) struct ApplyPatchExecutor {
pub(crate) state: Arc<WorkspaceToolState>,
}
impl ToolExecutor for ApplyPatchExecutor {
fn propose<'a>(
&'a self,
call: PendingToolCall,
context: ToolExecutionContext,
) -> ToolActionProposalFuture<'a> {
Box::pin(async move {
if context.cancellation_token().is_cancelled() {
return Err(ToolExecutionError::Cancelled);
}
let args = match input::parse(&call) {
Ok(args) => args,
Err(message) => {
return Ok(ToolActionPreflight::Outcome(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_INVALID_ARGUMENTS,
message,
None::<String>,
)));
}
};
let state = Arc::clone(&self.state);
let token = context.cancellation_token().clone();
let worker_token = token.clone();
let approved_path_grants = context.approved_path_grants().to_vec();
let handle = tokio::task::spawn_blocking(move || {
let is_cancelled = || worker_token.is_cancelled();
propose_apply_patch_blocking_checked(
&state,
args,
&call,
&approved_path_grants,
&is_cancelled,
)
});
tokio::select! {
biased;
() = token.cancelled() => Err(ToolExecutionError::Cancelled),
joined = handle => match joined {
Ok(Ok(proposal)) => {
if token.is_cancelled() {
Err(ToolExecutionError::Cancelled)
} else {
Ok(proposal)
}
}
Ok(Err(error)) => Err(error),
Err(error) => Err(ToolExecutionError::infrastructure(format!(
"workspace patch proposal task failed to join: {error}"
))),
},
}
})
}
fn execute<'a>(
&'a self,
call: PendingToolCall,
context: ToolExecutionContext,
) -> ToolExecutorFuture<'a> {
Box::pin(async move {
if context.cancellation_token().is_cancelled() {
return Err(ToolExecutionError::Cancelled);
}
let args = match input::parse(&call) {
Ok(args) => args,
Err(message) => {
return Ok(invalid_arguments_outcome(
APPLY_PATCH_TOOL,
call.id().as_str(),
message,
));
}
};
let trace_patch_bytes = args.patch.len();
trace_workspace_tool_start(
APPLY_PATCH_TOOL,
call.id().as_str(),
WorkspaceTraceTarget::Patch {
patch_bytes: trace_patch_bytes,
},
);
let state = Arc::clone(&self.state);
let token = context.cancellation_token().clone();
let worker_token = token.clone();
let approved_proposal = context.approved_apply_patch().cloned();
let approved_path_grants = context.approved_path_grants().to_vec();
let handle = tokio::task::spawn_blocking(move || {
let is_cancelled = || worker_token.is_cancelled();
apply_patch_blocking_checked(
&state,
args,
approved_proposal.as_ref(),
&approved_path_grants,
&is_cancelled,
)
});
tokio::select! {
biased;
joined = handle => match joined {
Ok(Ok(outcome)) => {
trace_workspace_tool_finish(
APPLY_PATCH_TOOL,
call.id().as_str(),
WorkspaceTraceTarget::Patch {
patch_bytes: trace_patch_bytes,
},
WorkspaceTraceFinish::Outcome(&outcome),
);
Ok(outcome)
}
Ok(Err(error)) => {
trace_workspace_tool_finish(
APPLY_PATCH_TOOL,
call.id().as_str(),
WorkspaceTraceTarget::Patch {
patch_bytes: trace_patch_bytes,
},
WorkspaceTraceFinish::from_error(&error),
);
Err(error)
}
Err(error) => {
trace_workspace_tool_finish(
APPLY_PATCH_TOOL,
call.id().as_str(),
WorkspaceTraceTarget::Patch {
patch_bytes: trace_patch_bytes,
},
WorkspaceTraceFinish::Error("workspace_infrastructure_error"),
);
Err(ToolExecutionError::infrastructure(format!(
"workspace patch task failed to join: {error}"
)))
}
},
}
})
}
}