Skip to content

fix(apply-edits): make batch failures actionable - #100

Merged
INK-Q merged 2 commits into
INK-Q:devfrom
iazrael:codex/apply-edits-recovery
Nov 29, 2026
Merged

INK-Q merged 2 commits into
INK-Q:devfrom
iazrael:codex/apply-edits-recovery

Conversation

@iazrael

@iazrael iazrael commented Nov 27, 2026

Copy link
Copy Markdown

Summary

  • Give agents a two-file example and recommend one edit entry per path, using exact current target text. Use line bounds to disambiguate repeated text.
  • Report the file, hunk number, and actual line count for out-of-range starts. When a unique target has moved, report its exact bounds for a corrected retry.
  • Explain which edits to retry after an atomic abort or a partial write. Limit the built-in tool fallback for serialization errors to the affected batch.

Reproduction

A four-file edit arrived with all four hunks under a single changelog path. The original error showed empty nearby-line ranges. The regression test checks that a wrong-path request is rejected even when it contains a target that happens to match the wrong file, then verifies that the corrected batch applies.

Verification

  • npm run typecheck and npm run validate passed.
  • npm test passed all 185 tests and rebuilt the checked-in MCP bundle.
  • node scripts/stress-benchmark.mjs passed all 11 scenarios.

Guide callers to group hunks by file and use exact current target text. Report invalid and stale bounds with specific recovery steps, and distinguish retries after zero writes, partial writes, and argument corruption.

@INK-Q INK-Q left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @iazrael for investigating misgrouped multi-file hunks and improving the recovery wording and EOF line-count accuracy!

A few P0/P1 regressions need to be addressed before merging:

P0 / P1 (Must Fix)

  1. Throwing on sLine > totalLines in getLineWindowOffsets breaks existing line-drift detection (src/tools/apply-edits.ts:151-154):
    • Previously, sLine > totalLines returned { windowStart: text.length, windowEnd: text.length }, allowing planHunksForContent to scan the file for an exact match outside the requested window (Exact match found outside range at L...) or emit a closest-region snippet when a file shrank slightly and startLine drifted past EOF.
    • Throwing immediately inside getLineWindowOffsets bypasses drift detection entirely (and would block upcoming small-drift auto-recovery on dev).
    • Instead, keep getLineWindowOffsets returning an empty window when sLine > totalLines, and report startLine N exceeds file length (M lines) only inside the foundOffsets.length === 0 path after confirming no global exact match exists (while still skipping empty Nearby lines).
  2. Empty-file edge case when popping EOF lineStarts (src/tools/apply-edits.ts:371):
    • For a 0-byte file (""), computeLineStarts("") returns [0], so lineStarts.at(-1) === normalizedContent.length (0 === 0) pops [0] to [] (totalLines = 0), causing startLine: 1 to fail with exceeds file length (0 lines) instead of normal target matching. Guard with if (normalizedContent.length > 0 && lineStarts.at(-1) === normalizedContent.length) lineStarts.pop();.
  3. Regressions & bloat in rules/AGENTS.md (+768 B) and src/mcp-server-core.ts:
    • Rewriting Rule 5 in rules/AGENTS.md adds +768 B to the always-injected system prompt and removes core repository routing rules (consistent usage across Vue/HTML/CSS/docs/scripts/scratch, defaulting to bestEffort: true for >=3 files with <=6-8-file batches) while reversing bestEffort guidance ("Enable bestEffort: true only when partial writes are acceptable"). Please preserve the existing Rule 5 directives and keep rules/AGENTS.md concise.
    • In src/mcp-server-core.ts, the updated apply_edits description drops verify timeout/budget constraints (60s/4000 chars, 150s max) and (Desktop 2.0 Hub). Please keep those operational limits in the tool description (within the <= 1280 B schema budget).

Checklist

  • Preserve global exact-match drift detection when startLine > totalLines before falling back to the out-of-bounds length error
  • Guard lineStarts.pop() for 0-byte empty files
  • Restore the removed rules/AGENTS.md Rule 5 directives & keep delta minimal, and restore verify timeout/char limits in apply_edits description
  • Rebuild dist/mcp-server.mjs and re-run npm run typecheck && npm test && npm run validate

Address review P0/P1 feedback on batch-failure recovery:

- getLineWindowOffsets returns an empty window when startLine exceeds the
  file length, so global exact-match drift detection still runs; the
  out-of-bounds length error is reported only after confirming no exact
  match exists, skipping empty Nearby lines.
- Guard lineStarts.pop() for 0-byte files so startLine 1 matches normally.
- Restore removed rules/AGENTS.md Rule 5 directives (repo routing across
  Vue/HTML/CSS/docs/scripts/scratch, bestEffort: true for >=3 files,
  serialization-corruption circuit break) and fold the grouping/recovery
  guidance in compactly (+505B vs +768B).
- Restore verify timeout/char limits and (Desktop 2.0 Hub) in the
  apply_edits description within the 1280B schema budget (array 1265B,
  string 1119B).

Rebuild dist/mcp-server.mjs; typecheck, 187 tests, and validate pass.

@INK-Q INK-Q left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks @iazrael for the clean follow-up commit! All previous P0/P1 items are verified resolved:

  • getLineWindowOffsets now rejects inverted ranges (requestedEnd < sLine) first while still allowing sLine > totalLines to fall through to exact-match drift detection (and emits the concise EOF guidance only when no global exact match exists).
  • lineStarts.pop() is properly guarded for 0-byte empty files and covered by a dedicated unit test.
  • apply_edits schema description restores the verify timeout/char limits (1,265 B array / 1,119 B string, safely within the 1,280 B cap), and Rule 5 restores multi-directory & bestEffort guidance.
  • Verified clean 3-way merge onto latest origin/dev (3675109) with byte-for-byte dist/mcp-server.mjs esbuild parity.

Approving now — once the GitHub Actions CI checks finish green, we'll merge this into dev.

@INK-Q
INK-Q merged commit 3241213 into INK-Q:dev Nov 29, 2026
8 checks passed
@INK-Q INK-Q mentioned this pull request Nov 29, 2026
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