Repository navigation
fix(patch): reject a malformed hunk instead of half-applying it - #776
ANIRUDDHA ADAK (aniruddhaadak80) wants to merge 1 commit into
Conversation
An update section with no @@ header produced no chunk, so the file was rewritten with its own bytes and still reported as edited, and a hunk line with no leading space or sign was dropped, removing the only disambiguator the model supplied. Refuse both with the reason. A blank line inside a hunk stays legal, and an indented line is still a context line.
|
ANIRUDDHA ADAK (@aniruddhaadak80) is attempting to deploy a commit to the InkVell Team on Vercel. A member of the Team first needs to authorize it. |
Ishaan Gangwani (ishaan1124)
left a comment
There was a problem hiding this comment.
Thanks, the two cases are real and the test catches them. One regression before this can merge.
The new guard in backend/cli/src/tool/apply_patch.ts, if (hunk.chunks.length === 0) throw, also rejects a rename-only update:
*** Begin Patch
*** Update File: a.txt
*** Move to: b.txt
*** End Patch
On main that renames the file and reports success; with this PR it's refused. The tool description allows an update "optionally with a rename", so this is valid input. Suggested fix:
if (hunk.chunks.length === 0 && !hunk.move_path) throw ...Please add a test that a rename-only update still renames the file. (It will also need a trivial rebase: #779 appends to the same test file.)
|
Closing in favour of #826, which is the same fix reworked against current The objection raised here is addressed there rather than here: #826 rejects a malformed hunk by refusing a change line that appears before any The two also land in different files: the parser lives in Both malformed shapes from this PR are covered on #826: the change-line-before- |
|
Superseded by #826, which carries the same fix with the rename-only case preserved and is green on current main. |
What does this PR do, and why?
Two malformed patches are accepted, and both are reported to the model as applied.
An update section with no
@@header. The parser only starts a chunk on aline beginning
@@, and anything else is skipped:So an update section with no
@@parses tochunks: [],deriveNewContentsFromChunkscomputesreplacements = [], and the "new" contentis the file's own bytes. The
hunks.length === 0guard does not catch it,because the
*** Update File:header already produced one hunk. The tool thenprints
Success. Updated the following files: M a.pywith an empty diff.A hunk line that lost its prefix. The change-line loop handles
" ","-"and
"+"and has noelse, so a line with no leading space or sign is droppedfrom both
old_linesandnew_lines. That line is usually the only thingdistinguishing one block from another, and
seekSequencethen tries fourprogressively looser passes (rstrip, both-end trim, punctuation folding) to find
somewhere to apply the hunk — so what the model asked for in one function can
land in another.
Both are now refused with the reason. A blank line inside a hunk stays legal,
because a hunk is allowed to end before the next header with one.
One case I deliberately left alone
A line that is indented is a legitimate context line whose first space is the
prefix, and that is not fixable: no parser can tell
xfrom a lost-prefixx. Only the unambiguously malformed case — no prefix at all — is rejected,and there is a test pinning that an indented line is still a context line with
its indentation kept.
Linked issue
Fixes #775
How did you verify it?
Four cases in
backend/cli/test/tool/apply_patch.test.ts:an update section with no @@ hunk is rejected instead of reported as edited— drives the real tool and asserts both the rejection and that the file on
disk is byte-identical afterwards
a hunk line with no prefix is rejected instead of dropped— asserts the parseitself refuses it, which is where the defect is
a blank line inside a hunk is still acceptedan indented hunk line is still a context line, with its indentation keptOn unmodified
3e94875cthe first two fail:Commands run:
bun test --timeout 30000 ./test/tool/apply_patch.test.ts→ 42 pass, 0 failbun run --cwd backend/cli typecheck→ exit 0The full file matters more than the new cases: this makes the parser stricter,
and 38 pre-existing tests — including the insert-only, multi-hunk, rollback and
"rejects update with missing context" cases — still pass, so nothing that used to
parse is now refused.
One thing I checked while verifying, because it changes what the fix can promise:
I first wrote the unprefixed-line test with an indented line and it passed on
main. The surrounding verification was already failing that patch closed, so thetest was passing for the wrong reason and proved nothing. The locked case is the
one with no leading whitespace at all.
Pre-existing reds on this Windows checkout, not from this diff:
bun run format:checkcannot pass here: git materializes the LF blobs as CRLF,so Prettier flags hundreds of untouched files. I verified all four changed
files are formatted per the repo config with line endings normalized.
bun run typecheck: backend is clean;@synsci/workspacefails becausefrontend/workspace/src/custom-elements.d.tsis a symlink (mode120000) thatWindows checked out as a text file, which TypeScript parses as source →
TS1128.Checklist
bun run checkis green (format, typecheck, backend + frontend/ui + SDK tests) — blocked on this Windows checkout by the CRLF and symlink artifacts above; backend typecheck is clean and the full apply_patch suite is greenbun run --cwd frontend/workspace buildsucceeds if I touchedfrontend/workspaceorfrontend/ui— not touched./tooling/repo/generate.tswas run and thetooling/sdkoutput committed if I changedbackend/cli/src/server— not touchedfrontend/docs/src/content/openscience/is updated if behavior changed — no doc change needed:docs/notes/adding-a-tool.mddocuments the patch format, and this only makes its grammar enforcedpackage.jsonversions and tags are written by the release workflow)installandfrontend/landing/public/installare still byte-identical if I touched either — not touched