Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions cli/src/services/structured_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ fn build_write_create_patch(payload: &Map<String, Value>) -> PatchBuildResult {
return skipped_build(ClaudeStructuredPatchSkipReason::UnsupportedWritePayload);
};

if value_field(tool_response, &["originalFile", "original_file"]) != Some(&Value::Null) {
return skipped_build(ClaudeStructuredPatchSkipReason::UnsupportedWritePayload);
}
let structured_patch = value_field(tool_response, &["structuredPatch", "structured_patch"]);

let file_path = normalize_patch_path(
string_field(tool_input, &["file_path", "filePath"])
Expand All @@ -162,7 +160,15 @@ fn build_write_create_patch(payload: &Map<String, Value>) -> PatchBuildResult {
return skipped_build(ClaudeStructuredPatchSkipReason::MissingFilePath);
};

let Some(content) = string_value_field(tool_input, &["content", "newFile", "new_file"]) else {
if let Some(structured_patch) = structured_patch.filter(|patch| !patch.is_null()) {
if let Some(patch) =
modified_patch_from_structured_hunks(file_path.clone(), structured_patch)
{
return PatchBuildResult::Built(patch);
}
}

let Some(content) = string_value_field(tool_input, &["content"]) else {
return skipped_build(ClaudeStructuredPatchSkipReason::MissingFileContent);
};

Expand All @@ -187,29 +193,30 @@ fn build_edit_structured_patch(payload: &Map<String, Value>) -> PatchBuildResult
}

let file_path = normalize_patch_path(
string_field(tool_input, &["file_path", "filePath"])
.or_else(|| {
structured_patch
.as_object()
.and_then(|patch| string_field(patch, &["file_path", "filePath", "path"]))
})
.as_deref(),
string_field(tool_input, &["file_path", "filePath"]).as_deref(),
string_field(payload, &["cwd"]).as_deref(),
);
let Some(file_path) = file_path else {
return skipped_build(ClaudeStructuredPatchSkipReason::MissingFilePath);
};

let Some(patch) = modified_patch_from_structured_hunks(file_path, structured_patch) else {
return skipped_build(ClaudeStructuredPatchSkipReason::UnsupportedEditPayload);
};

PatchBuildResult::Built(patch)
}

fn modified_patch_from_structured_hunks(
file_path: String,
structured_patch: &Value,
) -> Option<ParsedPatch> {
let hunks: Vec<PatchHunk> = structured_patch_hunks(structured_patch)
.into_iter()
.filter_map(parse_structured_patch_hunk)
.collect();

if hunks.is_empty() {
return skipped_build(ClaudeStructuredPatchSkipReason::UnsupportedEditPayload);
}

PatchBuildResult::Built(ParsedPatch {
(!hunks.is_empty()).then_some(ParsedPatch {
files: vec![PatchFileChange {
old_path: file_path.clone(),
new_path: file_path,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
{
"hook_event_name": "PostToolUse",
"session_id": "test-write-create-simple",
"session_id": "test-write-update-structured-patch",
"tool_name": "Write",
"tool_input": {
"file_path": "hunks/hello.ts",
"content": "function helloWorld(): void {\n console.log(\"Hello World\");\n}\n\nhelloWorld();\n"
"file_path": "docs/status.md",
"content": "# Status\n\nThis full content fallback must not be used.\n"
},
"tool_response": {
"originalFile": null
"originalFile": "# Status\n\nThe old state is pending.\n",
"structuredPatch": {
"hunks": [
{
"oldStart": 1,
"oldCount": 3,
"newStart": 1,
"newCount": 3,
"lines": [
" # Status",
" ",
"-The old state is pending.",
"+The new state is complete."
]
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
diff --git a/hunks/hello.ts b/hunks/hello.ts
new file mode 100644
--- /dev/null
+++ b/hunks/hello.ts
@@ -0,0 +1,5 @@
+function helloWorld(): void {
+ console.log("Hello World");
+}
+
+helloWorld();
Index: docs/status.md
===================================================================
--- a/docs/status.md
+++ b/docs/status.md
@@ -1,3 +1,3 @@
# Status

-The old state is pending.
+The new state is complete.
2 changes: 1 addition & 1 deletion context/architecture.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion context/cli/patch-service.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions context/cli/structured-patch-service.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions context/context-map.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading