use std::{
fs,
io::{self, Seek, SeekFrom, Write},
};
use merry_runtime::{
ActionExecutionEvidence, ToolExecutionError, ToolExecutionOutcome,
WorkspacePatchChangeEvidence, WorkspacePatchExecutionEvidence,
};
#[cfg(test)]
use crate::trace::{
maybe_run_patch_test_after_write_hook, maybe_run_patch_test_before_mutation_hook,
};
use crate::{
APPLY_PATCH_TOOL,
errors::{BlockingToolError, DomainError, ERROR_WRITE_FAILED, failed_outcome},
file::read_bounded,
path::{open_file_for_patch, open_file_for_patch_create_new, open_file_for_read},
};
use super::{
envelope::{WorkspacePatchSuccess, WorkspacePatchSuccessChange},
plan::{
WorkspacePatchFileMode, WorkspacePatchFilePlan, WorkspacePatchPlan,
read_patch_preimage_for_path,
},
types::{count_file_lines, stable_content_fingerprint},
};
pub(super) fn execute_apply_patch_plan(
plan: WorkspacePatchPlan,
is_cancelled: &dyn Fn() -> bool,
) -> Result<ToolExecutionOutcome, ToolExecutionError> {
if is_cancelled() {
return Err(ToolExecutionError::Cancelled);
}
let mut written_changes = Vec::with_capacity(plan.changes.len());
let mut evidence_changes = Vec::with_capacity(plan.changes.len());
for change in plan.changes {
if is_cancelled() {
return Err(ToolExecutionError::Cancelled);
}
let relative_display = change.relative.display.clone();
let operation = change.mode.operation_kind();
let ignored_context_hunks = change.ignored_context_hunks;
let lines_before = change.lines_before;
let content_after = match execute_apply_patch_file_plan(&change, is_cancelled) {
Ok(content_after) => content_after,
Err(PatchFileWriteError::Outcome(outcome)) => return Ok(*outcome),
Err(PatchFileWriteError::Cancelled) => return Err(ToolExecutionError::Cancelled),
};
let lines_after = count_file_lines(&content_after);
evidence_changes.push(
WorkspacePatchChangeEvidence::new(
relative_display.clone(),
change.preimage_bytes,
change.replacement_bytes,
change.bytes_before,
content_after.len(),
stable_content_fingerprint(change.content_before.as_bytes()),
stable_content_fingerprint(content_after.as_bytes()),
)
.map_err(|error| {
ToolExecutionError::infrastructure(format!(
"workspace patch execution evidence was invalid: {error}"
))
})?,
);
written_changes.push(WorkspacePatchSuccessChange {
path: relative_display,
op: Some(operation),
hunks: change.hunks,
lines_before: Some(lines_before),
lines_after: Some(lines_after),
bytes_before: change.bytes_before,
bytes_after: content_after.len(),
ignored_context_hunks,
lines: change.lines,
});
}
let evidence =
WorkspacePatchExecutionEvidence::from_changes(evidence_changes).map_err(|error| {
ToolExecutionError::infrastructure(format!(
"workspace patch execution evidence was invalid: {error}"
))
})?;
let payload = WorkspacePatchSuccess {
ok: true,
tool: APPLY_PATCH_TOOL.to_owned(),
changes: written_changes,
};
Ok(ToolExecutionOutcome::succeeded_json(
serde_json::to_string(&payload).expect("workspace patch success envelope serializes"),
)
.with_model_text(payload.model_text())
.with_execution_evidence(ActionExecutionEvidence::WorkspacePatch(evidence)))
}
enum PatchFileWriteError {
Outcome(Box<ToolExecutionOutcome>),
Cancelled,
}
fn open_planned_patch_file(plan: &WorkspacePatchFilePlan) -> Result<fs::File, DomainError> {
match plan.mode {
WorkspacePatchFileMode::CreateNew => open_file_for_patch_create_new(&plan.path),
WorkspacePatchFileMode::UpdateExisting => open_file_for_patch(&plan.path),
WorkspacePatchFileMode::DeleteExisting => open_file_for_read(&plan.path),
}
}
fn execute_apply_patch_file_plan(
plan: &WorkspacePatchFilePlan,
is_cancelled: &dyn Fn() -> bool,
) -> Result<String, PatchFileWriteError> {
let relative_display = plan.relative.display.clone();
if is_cancelled() {
return Err(PatchFileWriteError::Cancelled);
}
#[cfg(test)]
maybe_run_patch_test_before_mutation_hook(&plan.path);
let mut file = match open_planned_patch_file(plan) {
Ok(file) => file,
Err(error) => {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
error.code,
error.message,
Some(relative_display),
))));
}
};
if plan.mode != WorkspacePatchFileMode::CreateNew {
match read_bounded(&mut file, plan.max_read_bytes, is_cancelled) {
Ok(bytes) if bytes == plan.content_before.as_bytes() => {}
Ok(_) => {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"workspace file changed after the patch was planned; re-read the target file and submit a fresh patch",
Some(relative_display),
))));
}
Err(BlockingToolError::Domain(error)) => {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
error.code,
error.message,
Some(relative_display),
))));
}
Err(BlockingToolError::Cancelled) => return Err(PatchFileWriteError::Cancelled),
}
}
if plan.mode == WorkspacePatchFileMode::DeleteExisting {
drop(file);
return delete_apply_patch_file(plan, relative_display);
}
if file.seek(SeekFrom::Start(0)).is_err() {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"could not seek workspace file",
Some(relative_display),
))));
}
if file.write_all(plan.replacement.as_bytes()).is_err() {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"could not write workspace file",
Some(relative_display),
))));
}
if file.set_len(plan.replacement.len() as u64).is_err() {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"could not truncate workspace file",
Some(relative_display),
))));
}
if file.sync_all().is_err() {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"could not sync workspace file",
Some(relative_display),
))));
}
drop(file);
#[cfg(test)]
maybe_run_patch_test_after_write_hook(&plan.path);
let content_after =
match read_patch_preimage_for_path(&plan.path, plan.replacement.len(), &|| false) {
Ok(content) => content,
Err(BlockingToolError::Domain(error)) => {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
error.code,
error.message,
Some(relative_display),
))));
}
Err(BlockingToolError::Cancelled) => {
unreachable!("post-write readback is not cancellable")
}
};
if content_after != plan.replacement {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"workspace patch verification failed after write",
Some(relative_display),
))));
}
Ok(content_after)
}
fn delete_apply_patch_file(
plan: &WorkspacePatchFilePlan,
relative_display: String,
) -> Result<String, PatchFileWriteError> {
if fs::remove_file(&plan.path).is_err() {
return Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"could not delete workspace file",
Some(relative_display),
))));
}
#[cfg(test)]
maybe_run_patch_test_after_write_hook(&plan.path);
match fs::symlink_metadata(&plan.path) {
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(String::new()),
_ => Err(PatchFileWriteError::Outcome(Box::new(failed_outcome(
APPLY_PATCH_TOOL,
ERROR_WRITE_FAILED,
"workspace file still exists after delete",
Some(relative_display),
)))),
}
}