From 6f75114695f47409e741d55d75b73195e8e8df5a Mon Sep 17 00:00:00 2001 From: Dylan Date: Tue, 2 Sep 2025 17:38:19 -0700 Subject: [PATCH] [apply-patch] Fix lark grammar (#2651) ## Summary Fixes an issue with the lark grammar definition for the apply_patch freeform tool. This does NOT change the defaults, merely patches the root cause of the issue we were seeing with empty lines, and an issue with config flowing through correctly. Specifically, the following requires that a line is non-empty: ``` add_line: "+" /(.+)/ LF -> line ``` but many changes _should_ involve creating/updating empty lines. The new definition is: ``` add_line: "+" /(.*)/ LF -> line ``` ## Testing - [x] Tested locally, reproduced the issue without the update and confirmed that the model will produce empty lines wiht the new lark grammar --- codex-rs/core/src/tool_apply_patch.lark | 19 +++++++++++++++++++ codex-rs/core/src/tool_apply_patch.rs | 24 +++--------------------- 2 files changed, 22 insertions(+), 21 deletions(-) create mode 100644 codex-rs/core/src/tool_apply_patch.lark diff --git a/codex-rs/core/src/tool_apply_patch.lark b/codex-rs/core/src/tool_apply_patch.lark new file mode 100644 index 00000000..5aa41b0a --- /dev/null +++ b/codex-rs/core/src/tool_apply_patch.lark @@ -0,0 +1,19 @@ +start: begin_patch hunk+ end_patch +begin_patch: "*** Begin Patch" LF +end_patch: "*** End Patch" LF? + +hunk: add_hunk | delete_hunk | update_hunk +add_hunk: "*** Add File: " filename LF add_line+ +delete_hunk: "*** Delete File: " filename LF +update_hunk: "*** Update File: " filename LF change_move? change? + +filename: /(.+)/ +add_line: "+" /(.*)/ LF -> line + +change_move: "*** Move to: " filename LF +change: (change_context | change_line)+ eof_line? +change_context: ("@@" | "@@ " /(.+)/) LF +change_line: ("+" | "-" | " ") /(.*)/ LF +eof_line: "*** End of File" LF + +%import common.LF diff --git a/codex-rs/core/src/tool_apply_patch.rs b/codex-rs/core/src/tool_apply_patch.rs index 3868cf0e..d4039fd1 100644 --- a/codex-rs/core/src/tool_apply_patch.rs +++ b/codex-rs/core/src/tool_apply_patch.rs @@ -8,6 +8,8 @@ use crate::openai_tools::JsonSchema; use crate::openai_tools::OpenAiTool; use crate::openai_tools::ResponsesApiTool; +const APPLY_PATCH_LARK_GRAMMAR: &str = include_str!("tool_apply_patch.lark"); + #[derive(Serialize, Deserialize)] pub(crate) struct ApplyPatchToolArgs { pub(crate) input: String, @@ -29,27 +31,7 @@ pub(crate) fn create_apply_patch_freeform_tool() -> OpenAiTool { format: FreeformToolFormat { r#type: "grammar".to_string(), syntax: "lark".to_string(), - definition: r#"start: begin_patch hunk+ end_patch -begin_patch: "*** Begin Patch" LF -end_patch: "*** End Patch" LF? - -hunk: add_hunk | delete_hunk | update_hunk -add_hunk: "*** Add File: " filename LF add_line+ -delete_hunk: "*** Delete File: " filename LF -update_hunk: "*** Update File: " filename LF change_move? change? - -filename: /(.+)/ -add_line: "+" /(.+)/ LF -> line - -change_move: "*** Move to: " filename LF -change: (change_context | change_line)+ eof_line? -change_context: ("@@" | "@@ " /(.+)/) LF -change_line: ("+" | "-" | " ") /(.+)/ LF -eof_line: "*** End of File" LF - -%import common.LF -"# - .to_string(), + definition: APPLY_PATCH_LARK_GRAMMAR.to_string(), }, }) }