Skip to content

fix(patch): reject a malformed hunk instead of half-applying it - #776

Closed
ANIRUDDHA ADAK (aniruddhaadak80) wants to merge 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/patch-reject-malformed-hunk
Closed

ANIRUDDHA ADAK (aniruddhaadak80) wants to merge 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/patch-reject-malformed-hunk

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

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 a
line beginning @@, and anything else is skipped:

while (i < lines.length && !lines[i].startsWith("***")) {
  if (lines[i].startsWith("@@")) { /* …make a chunk… */ } else { i++ }
}

So an update section with no @@ parses to chunks: [],
deriveNewContentsFromChunks computes replacements = [], and the "new" content
is the file's own bytes. The hunks.length === 0 guard does not catch it,
because the *** Update File: header already produced one hunk. The tool then
prints Success. Updated the following files: M a.py with an empty diff.

A hunk line that lost its prefix. The change-line loop handles " ", "-"
and "+" and has no else, so a line with no leading space or sign is dropped
from both old_lines and new_lines. That line is usually the only thing
distinguishing one block from another, and seekSequence then tries four
progressively 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 x from a lost-prefix
x. 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 parse
    itself refuses it, which is where the defect is
  • a blank line inside a hunk is still accepted
  • an indented hunk line is still a context line, with its indentation kept

On unmodified 3e94875c the first two fail:

error: expect(received).toThrow(expected)
Expected substring: "expected a leading ' ', '-' or '+'"
Received function did not throw

error: Expected promise that rejects
Received promise that resolved: Promise { <resolved> }

Commands run:

  • bun test --timeout 30000 ./test/tool/apply_patch.test.ts → 42 pass, 0 fail
  • bun run --cwd backend/cli typecheck → exit 0

The 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 the
test 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:check cannot 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/workspace fails because
    frontend/workspace/src/custom-elements.d.ts is a symlink (mode 120000) that
    Windows checked out as a text file, which TypeScript parses as source →
    TS1128.

Checklist

  • bun run check is 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 green
  • bun run --cwd frontend/workspace build succeeds if I touched frontend/workspace or frontend/ui — not touched
  • ./tooling/repo/generate.ts was run and the tooling/sdk output committed if I changed backend/cli/src/server — not touched
  • CHANGELOG.md has an Unreleased entry if the change is user-visible
  • The matching docs page under frontend/docs/src/content/openscience/ is updated if behavior changed — no doc change needed: docs/notes/adding-a-tool.md documents the patch format, and this only makes its grammar enforced
  • Screenshots or a short video are attached for UI changes — not a UI change
  • No version bumps (package.json versions and tags are written by the release workflow)
  • install and frontend/landing/public/install are still byte-identical if I touched either — not touched

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.
@vercel

vercel Bot commented Sep 28, 2026

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.)

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Closing in favour of #826, which is the same fix reworked against current main and is green.

The objection raised here is addressed there rather than here: #826 rejects a malformed hunk by refusing a change line that appears before any @@ header, instead of refusing an empty chunk list. That means a Move to with no hunk below it — no change line to trip on — passes through and still renames the file, and renames a file when the update carries no hunk now pins exactly that. I checked it against the chunks.length === 0 guard this PR used: the test fails with it and passes on #826.

The two also land in different files: the parser lives in backend/cli/src/patch/index.ts in #826, so the check sits next to the parsing it validates rather than in the tool wrapper.

Both malformed shapes from this PR are covered on #826: the change-line-before-@@ case, and a hunk line that lost its leading space or sign.

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

Superseded by #826, which carries the same fix with the rename-only case preserved and is green on current main.

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.

A malformed patch is reported as applied when nothing was changed

2 participants