can1357/oh-my-pi · error · ApplyPatchError

Line hint ${lineHintForInsertion} is out of range for insert

Error message

Line hint ${lineHintForInsertion} is out of range for insertion in ${path} (line numbers start at 1)

What it means

For a pure-insertion hunk (no removed lines) positioned by a line hint rather than a @@ anchor, the hint must fall in the valid 1-indexed insertion range. If oldStartLine ?? newStartLine is less than 1, the hint is invalid and an ApplyPatchError is thrown before any insertion occurs. Valid insertion points are 1 through file length + 1.

Source

Thrown at packages/coding-agent/src/edit/modes/patch.ts:1181

				} else {
					lineIndex = idx + 1;
				}
			}
		}

		if (hunk.oldLines.length === 0) {
			// Pure addition - prefer changeContext position, then line hint, then end of file
			let insertionIdx: number;
			if (hunk.changeContext !== undefined) {
				// changeContext was processed above; lineIndex is set to the context line or after it
				insertionIdx = lineIndex;
			} else {
				const lineHintForInsertion = hunk.oldStartLine ?? hunk.newStartLine;
				if (lineHintForInsertion !== undefined) {
					// Reject if line hint is out of range for insertion
					// Valid insertion points are 1 to (file length + 1) for 1-indexed hints
					if (lineHintForInsertion < 1) {
						throw new ApplyPatchError(
							`Line hint ${lineHintForInsertion} is out of range for insertion in ${path} ` +
								`(line numbers start at 1)`,
						);
					}
					if (lineHintForInsertion > originalLines.length + 1) {
						throw new ApplyPatchError(
							`Line hint ${lineHintForInsertion} is out of range for insertion in ${path} ` +
								`(file has ${originalLines.length} lines)`,
						);
					}
					insertionIdx = Math.max(0, lineHintForInsertion - 1);
				} else {
					insertionIdx =
						originalLines.length > 0 && originalLines[originalLines.length - 1] === ""
							? originalLines.length - 1
							: originalLines.length;
				}
			}

View on GitHub (pinned to 9690622007)

Solutions

  1. Set the hunk's start line hint to 1 or greater (1 inserts at the top of the file, length+1 at the end).
  2. Remove the line hint so the insertion defaults to end-of-file.
  3. Use a @@ changeContext anchor instead of a line hint to position the insertion.
  4. Fix the generator that emits 0-indexed insertion coordinates.

Example fix

// before
@@ -0 +0,2 @@
+new line

// after
@@ -1,0 +1,2 @@
+new line
Defensive patterns

Strategy: validation

Validate before calling

if (hunk.oldLines.length === 0 && lineHint !== undefined && lineHint < 1) {
  // insertion hints are 1-indexed: 1 = top of file
  lineHint = Math.max(1, lineHint);
}

Type guard

function isValidInsertionHint(hint: number, fileLineCount: number): boolean {
  return hint >= 1 && hint <= fileLineCount + 1;
}

Try / catch

try {
  applyPatch(patch);
} catch (err) {
  if (err instanceof ApplyPatchError && err.message.includes('out of range for insertion')) {
    // clamp the hint to [1, lineCount + 1] and retry
  } else throw err;
}

Prevention

When it happens

Trigger: An addition-only hunk with a header like `@@ -0` or negative start line that carries no changeContext, so the insertion-index branch validates lineHintForInsertion.

Common situations: Patch generators emitting 0-indexed coordinates for pure additions; hand-written diffs that treat the position before line 1 as line 0; template-generated insert blocks with unfilled hint fields.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/7c562cd25e3cf1ad. Report an issue: GitHub.