Skip to content

fix(patch): insert an added-only hunk where its header says - #779

Merged
Ishaan Gangwani (ishaan1124) merged 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/patch-insert-hunk-anchor
Sep 29, 2026
Merged

Ishaan Gangwani (ishaan1124) merged 1 commit into
synthetic-sciences:mainfrom
aniruddhaadak80:fix/patch-insert-hunk-anchor

Conversation

@aniruddhaadak80

Copy link
Copy Markdown
Contributor

What does this PR do, and why?

computeReplacements resolves the @@ header into a line index and then throws
it away for a hunk that only adds lines:

if (chunk.change_context) {
  const contextIdx = seekSequence(originalLines, [chunk.change_context], lineIndex)
  if (contextIdx === -1) throw new UpdateChunkMismatch(…)
  lineIndex = contextIdx + 1          // ← where the hunk belongs
}

// Handle pure addition (no old lines)
if (chunk.old_lines.length === 0) {
  const insertionIdx =
    originalLines.length > 0 && originalLines[originalLines.length - 1] === ""
      ? originalLines.length - 1
      : originalLines.length           // ← always end of file
  replacements.push([insertionIdx, 0, chunk.new_lines])
  continue
}

So @@ def main(): followed by only + lines puts the line at end of file, not
after the function it named.

The failure mode is quiet rather than loud, which is what makes it worth fixing.
In Python, YAML or LaTeX a misplaced statement is usually not a syntax error:
a line relocates from inside a function to module scope, or a YAML key lands
under the wrong parent, and the file keeps working while doing something else.
The tool reports Success. Updated the following files: M m.py.

The fix uses the position the header already resolved, keeping end-of-file as the
fallback for a hunk with no header:

const insertionIdx = chunk.change_context
  ? lineIndex
  : originalLines.length > 0 && originalLines[originalLines.length - 1] === ""
    ? originalLines.length - 1
    : originalLines.length

Linked issue

Addresses the same class as #775 (a patch that is not what it appears to be),
filed there. This hunk header is resolved — it is just ignored on this one path,
so there is no separate report to link.

How did you verify it?

Two cases in backend/cli/test/tool/apply_patch.test.ts:

  • an insert-only hunk lands after the context it names, not at end of file —
    drives the real tool on a file with main() and cleanup(); on 3e94875c the
    line lands after the top-level main() call
  • an insert-only hunk with no context still appends — the fallback is unchanged,
    so this is the guard that the fix is not "always insert at the anchor"

On unmodified 3e94875c the first fails with the line in the wrong place; the
second passes on both.

Commands run:

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

Two notes on how I ran that suite, because the numbers are worth trusting:

  • This file is genuinely slow on this machine: 892 s for 40 tests. My first
    full run used the default 30 s per-test timeout and reported 3 failures — all
    three at exactly 30 s. Re-run individually they pass in 14 s, 25 s and 16 s,
    and the whole file is green at a realistic timeout, so those were load-induced
    timeouts, not failures. I am reporting this rather than quietly quoting a green
    number, because a reader running the default timeout will see reds.
  • The pre-existing inserts lines with insert-only hunk case carries context
    lines, so old_lines.length > 0 and it never reached the branch I changed. That
    is exactly why the bug survived, and it is why the new test omits context lines.

Relationship to #776

Disjoint hunks in the same file. #776 changes the change-line parser (rejecting an
unprefixed line) and adds a guard in apply_patch.ts; this changes only the
pure-addition branch in computeReplacements. Neither conflicts with the other,
and both are in the same test file at different locations.

Checklist

  • bun run check is green (format, typecheck, backend + frontend/ui + SDK tests) — blocked on this Windows checkout by the CRLF and symlink artifacts in my other pull requests; backend typecheck is clean and the full apply_patch suite is green at a realistic timeout
  • 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: the @@ header is already documented as naming the change context
  • 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

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

computeReplacements resolved the @@ header into a line index and then ignored it
for a hunk with no old lines, so the line was appended after the last line of the
file rather than after the block the model named. Use the resolved index, and
keep end-of-file for a hunk with no header.
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.

2 participants