Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(host-core): clarify Edit parse recovery

Linuxdazhaof8ecd7c680e3
diff --git a/crates/host-core/src/tools/hashline/parse.rs b/crates/host-core/src/tools/hashline/parse.rs
index 7f07f29d7..adf06e999 100644
--- a/crates/host-core/src/tools/hashline/parse.rs
+++ b/crates/host-core/src/tools/hashline/parse.rs
@@ -5,6 +5,7 @@ use std::path::PathBuf;
pub const MAX_OPS_BYTES: usize = 256 * 1024;
pub const MAX_OPS_PER_CALL: usize = 200;
pub const MAX_REGISTER_LINES: usize = 4096;
+const EDIT_OP_GUIDANCE: &str = "use `PUT N.=M:` with `+TEXT` rows to replace, `CUT N.=M` to delete, or `PUT <N:` / `PUT >N:` to insert";
pub const MAX_REGISTER_BYTES: usize = 1024 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -89,7 +90,7 @@ pub fn parse_ops(ops: &str) -> Result<ParsedOps, ParseError> {
return Err(ParseError::new(
"EDIT_PARSE_FAILED",
format!(
- "body or context row without a header at op line {}: {raw}",
+ "body or context row without a header at op line {}: {raw}; put `+TEXT` rows after `PUT N.=M:`; a lone `+` writes a blank line",
idx + 1
),
));
@@ -225,7 +226,7 @@ fn parse_header(raw: &str) -> Result<(ParsedOp, bool), ParseError> {
if colon {
return Err(ParseError::new(
"EDIT_PARSE_FAILED",
- "CUT takes no body; drop the colon",
+ "CUT takes no body; use `CUT N.=M` to delete or `PUT N.=M:` with `+TEXT` rows to replace",
));
}
let locator_raw = parts
@@ -276,10 +277,20 @@ fn parse_header(raw: &str) -> Result<(ParsedOp, bool), ParseError> {
let dest = parse_dest(rest)?;
Ok((ParsedOp::Mv { dest }, false))
}
- other => Err(ParseError::new(
- "EDIT_PARSE_FAILED",
- format!("unknown operation {other}; expected PUT, CUT, REM, or MV"),
- )),
+ other => {
+ if other == "@@" {
+ return Err(ParseError::new(
+ "EDIT_PARSE_FAILED",
+ format!("unified-diff hunk header is not valid Edit ops; {EDIT_OP_GUIDANCE}"),
+ ));
+ }
+ Err(ParseError::new(
+ "EDIT_PARSE_FAILED",
+ format!(
+ "unknown operation {other}; expected PUT, CUT, REM, or MV; {EDIT_OP_GUIDANCE}"
+ ),
+ ))
+ }
}
}
@@ -316,7 +327,7 @@ fn parse_locator(raw: &str) -> Result<Locator, ParseError> {
}
Err(ParseError::new(
"EDIT_PARSE_FAILED",
- format!("invalid locator {raw}; expected N.=M, N*, <N, >N, >$, or >N*"),
+ format!("invalid locator {raw}; expected N.=M, N*, <N, >N, >$, or >N*; {EDIT_OP_GUIDANCE}"),
))
}
@@ -433,6 +444,35 @@ mod tests {
);
}
+ #[test]
+ fn parse_errors_name_the_corrective_operation() {
+ for (ops, expected, corrective) in [
+ (
+ "PUT 2.=2:\n",
+ "PUT with a colon requires",
+ "use CUT to delete",
+ ),
+ ("@@ -1,2 +1,2 @@\n", "unified-diff hunk header", "PUT N.=M:"),
+ ("### Heading\n", "unknown operation ###", "CUT N.=M"),
+ ("CUT 2.=2:\n", "CUT N.=M", "PUT N.=M:"),
+ ("+orphan\n", "PUT N.=M:", "a lone `+`"),
+ ("PUT 2:\n+new\n", "PUT N.=M:", "CUT N.=M"),
+ ] {
+ let error = parse_ops(ops).unwrap_err();
+ assert_eq!(error.code, "EDIT_PARSE_FAILED");
+ assert!(
+ error.message.contains(expected),
+ "expected {expected:?} in {:?}",
+ error.message
+ );
+ assert!(
+ error.message.contains(corrective),
+ "expected {corrective:?} in {:?}",
+ error.message
+ );
+ }
+ }
+
#[test]
fn parses_cut_rem_mv_and_optional_header() {
let parsed = parse_ops("[src/a.rs#AB12]\nCUT 4.=5\nMV dest.rs\n").unwrap();
diff --git a/crates/host-core/src/tools/mod.rs b/crates/host-core/src/tools/mod.rs
index 0408f54a3..7e7854bbb 100644
--- a/crates/host-core/src/tools/mod.rs
+++ b/crates/host-core/src/tools/mod.rs
@@ -3362,7 +3362,7 @@ pub fn builtin_tool_defs() -> Value {
"name": "Edit",
"description": "Replace, insert, or delete lines in an existing file. Names positions and supplies new content only — never old_string. Required args: path, tag (4 hex from the latest Read/Grep/Write/Edit), ops. \
Ops: `PUT N.=M:` replace inclusive lines N–M (body rows required); `PUT <N:` insert before N; `PUT >N:` insert after N; `PUT >$:` append; `CUT N.=M` delete; `REM` delete the file; `MV DEST` rename after other ops. \
- Body rows are `+` plus the final line text; a bare `+` is an empty line. Every PUT with body rows must include the trailing colon, for example `PUT 48.=48:` followed by a `+replacement` row; `PUT 48.=48` followed by `+` rows is invalid. A colonless PUT is only for a register paste such as `PUT <1 @name`. No `-old` or context rows. Ranges name only the lines being changed — a pure insert uses a gap locator, not a widened PUT that restates survivors. \
+ Delete with `CUT`, not an empty `PUT`. Do not paste unified-diff or Markdown wrappers into ops. Body rows are `+` plus the final line text; a bare `+` is an empty line. Every PUT with body rows must include the trailing colon, for example `PUT 48.=48:` followed by a `+replacement` row; `PUT 48.=48` followed by `+` rows is invalid. A colonless PUT is only for a register paste such as `PUT <1 @name`. No `-old` or context rows. Ranges name only the lines being changed — a pure insert uses a gap locator, not a widened PUT that restates survivors. \"
All line numbers refer to the tagged snapshot and are 1-indexed. Re-ground on the tag returned by every successful write. After one failed Edit, classify the error: Read the live file for a stale tag or unseen lines (or retry unchanged on a complete EDIT_LINES_UNSEEN reveal), but correct syntax or range errors directly; do not guess.",
"risk": "high",
"parameters": {
@@ -4922,6 +4922,14 @@ mod tests {
.as_str()
.unwrap()
.contains("PUT 48.=48` followed by `+` rows is invalid"));
+ assert!(edit["description"]
+ .as_str()
+ .unwrap()
+ .contains("Delete with `CUT`, not an empty `PUT`"));
+ assert!(edit["description"]
+ .as_str()
+ .unwrap()
+ .contains("unified-diff or Markdown wrappers"));
assert!(edit["description"]
.as_str()
.unwrap()
diff --git a/docs/spec/03-runtime/18-line-anchored-edit-contract.md b/docs/spec/03-runtime/18-line-anchored-edit-contract.md
index 54315d755..48cccc45d 100644
--- a/docs/spec/03-runtime/18-line-anchored-edit-contract.md
+++ b/docs/spec/03-runtime/18-line-anchored-edit-contract.md
@@ -596,6 +596,12 @@ the turn (§9.3, [08-error-codes](08-error-codes.md) §3.3).
Every error message names one concrete next step. "Re-read and retry" without a
range is not an acceptable message when the host knows the range.
+Malformed-op diagnostics name a concrete replacement form: range `PUT` with
+`+TEXT` rows, `CUT`, or a gap `PUT`. Unified-diff hunk headers and Markdown/diff
+wrappers remain invalid input; the parser rejects them with this guidance rather
+than silently discarding lines. An empty `PUT` also remains invalid and directs
+the caller to `CUT`.
+
## 12. Limits
| Limit | Value | Behavior at the limit |
diff --git a/docs/spec/06-delivery/e2e/tools-and-workspace.md b/docs/spec/06-delivery/e2e/tools-and-workspace.md
index 690ac46be..5d5d02594 100644
--- a/docs/spec/06-delivery/e2e/tools-and-workspace.md
+++ b/docs/spec/06-delivery/e2e/tools-and-workspace.md
@@ -2428,6 +2428,21 @@ are withdrawn with ADR 0165.
- Automated coverage: `read_powershell_utf16le_log`,
`read_and_edit_utf16be_chinese_text`, and the existing binary/CRLF tool tests.
+## Edit parse diagnostics name corrective operations
+
+- Submit an empty `PUT`, a unified-diff hunk, a Markdown heading, a colon-
+ terminated `CUT`, an orphan body row, and an invalid `PUT` locator against a
+ read file. Follow them with a valid range `PUT` as a control.
+- Expected: every malformed payload fails before writing with
+ `EDIT_PARSE_FAILED`; the result names a concrete replacement `PUT`, `CUT`, or
+ gap-insert form. A unified-diff hunk is identified explicitly, and foreign
+ patch/Markdown wrappers are never silently stripped. The valid control edit
+ succeeds. The tool description says to delete with `CUT`, not an empty `PUT`,
+ and not to paste unified-diff or Markdown wrappers into `ops`.
+- Automated coverage: `cargo test -p host-core
+ tools::hashline::parse::tests` and `cargo test -p host-core
+ tool_defs_advertise_the_scoping_parameters`; no real provider is used.
+
## Delegate mutation recovery isolation
- Start two Task delegates editing the same file. Delegate A produces text and