Skip to content

feat(tools): read large files in part and tolerate common edit mismatches - #299

Merged
GoDiao merged 2 commits into
mainfrom
feat/harness-exec-layer
Nov 21, 2026
Merged

GoDiao merged 2 commits into
mainfrom
feat/harness-exec-layer

Conversation

@GoDiao

@GoDiao GoDiao commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Part of #261 (tool execution: editing, reading, and recovery from errors).

What changed

Read

  • A file over the token or byte limit returns the first lines that fit, with a note saying which lines they are (lines 1-N of M), instead of an error that forces a second, ranged read.
  • A file attached with @ is cut by the same rule instead of at 2,000 lines; the old 2,000-line attachment path and its note are removed.
  • After such a partial read, Edit is allowed but Write refuses to overwrite the file. The partial state survives an Edit and a CLI restart (--resume rebuilds it from the transcript). After compaction, a file over the limit is referred to instead of being restored in part.

Edit

  • One matchEdit function (src/tools/FileEditTool/matchEdit.ts) replaces the scattered matching code. After an exact match it tries, in order: desanitized tags, curly quotes, line-number prefixes copied from Read, trailing whitespace, indentation (with new_string re-indented by the same mapping), and written-out escapes. Each strategy must find exactly one candidate; the line-block strategies also count substring occurrences so they never replace a different spot.
  • The match runs at execution time against the file as it is then. The approval prompt and PreToolUse hooks see the rewritten old_string/new_string, i.e. the text that will actually be written; the transcript and tool cards keep the model's text. If the approved input is unchanged, call() gets the model's original input and matches again; if a hook or the user changed anything, call() uses the approved input as is.
  • Receipt-time rewriting is reduced to the existing trailing-whitespace strip of new_string.

Success text

  • Edit and Write results end with a note that the file does not need to be read back. Edit leaves it out when the written text differs from new_string or the user modified the edit.

Sidecar

  • tool_use_complete is built only from the CLI's assistant message, so a tool card shows the same input live and after a refresh.

Other

  • Adds @types/lodash-es as a dev dependency: lodash-es ships no types, so its 108 importers in src were typed as any.
  • Harness docs and tutorials 00/02/04/06 updated; CHANGELOG entry under Unreleased.

Why

Third-party models often quote old_string with tabs instead of spaces, missing trailing spaces, Read's line-number prefixes or escaped quotes. Each of these failed the edit and cost one or two extra round trips with the whole old_string resent. A Read over the limit cost a guaranteed extra round trip. Without a read-back note, models tend to Read the file again after editing it.

Numbers

Measured on one large file (direct measurement, no benchmark set):

  • Before: Read returns a 230-237 character error (about 58 tokens), then the model needs a second, ranged Read.
  • After: one Read returning 108,853 characters (about 27,213 tokens, rough estimate).

How tested

  • bun test: 3414 pass, 0 fail (before the rebase onto current main).
  • bun test --randomize --seed=1 on the touched test files: pass.
  • New tests: matchEdit.test.ts (every strategy, ambiguity, prefixes, desanitize), FileEditTool.test.ts (end-to-end edits, the approved input, an Edit run through runToolUse with approval as shown, a fresh copy, and a user-changed old_string/new_string), FileReadTool.test.ts (partial read, Write refusal, restore from transcript, @ attachments), postCompactFileRestore.test.ts, sidecar ws-memory-events.test.ts. Each fix was checked by reverting it and seeing its test fail.
  • No new type errors in the root project compared with main, except that typing lodash-es surfaces 10 errors in untouched code (QueryEngine.ts, queryHelpers.ts:66-68, pluginService.ts, client.ts, mcp.test.ts) that any used to hide. The root project is not type-checked in CI.
  • Desktop code is not touched.

Known gaps

  • Bash sed -i writes do not clear the partial-read state.
  • The escape strategy unescapes the whole new_string, so a \n the code needs there becomes a real newline.
  • When a hook changes the input, the read-back note is still added (same as before this PR).

@GoDiao
GoDiao merged commit 8967886 into main Nov 21, 2026
4 checks passed
@GoDiao
GoDiao deleted the feat/harness-exec-layer branch November 21, 2026 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant