{"record":{"id":"9987d42a493b5e94","repo":"can1357/oh-my-pi","slug":"line-hint-hunk-newstartline-is-out-of-range-for","errorCode":null,"errorMessage":"Line hint ${hunk.newStartLine} is out of range for ${path} (line numbers start at 1)","messagePattern":"Line hint (.+?) is out of range for (.+?) \\(line numbers start at 1\\)","errorType":"validation","errorClass":"ApplyPatchError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/modes/patch.ts","lineNumber":1106,"sourceCode":"function computeReplacements(\n\toriginalLines: string[],\n\tpath: string,\n\thunks: DiffHunk[],\n\tallowFuzzy: boolean,\n): { replacements: Replacement[]; warnings: string[] } {\n\tconst replacements: Replacement[] = [];\n\tconst warnings: string[] = [];\n\tlet lineIndex = 0;\n\n\tfor (const hunk of hunks) {\n\t\tlet contextIndex: number | undefined;\n\t\tif (hunk.oldStartLine !== undefined && hunk.oldStartLine < 1) {\n\t\t\tthrow new ApplyPatchError(\n\t\t\t\t`Line hint ${hunk.oldStartLine} is out of range for ${path} (line numbers start at 1)`,\n\t\t\t);\n\t\t}\n\t\tif (hunk.newStartLine !== undefined && hunk.newStartLine < 1) {\n\t\t\tthrow new ApplyPatchError(\n\t\t\t\t`Line hint ${hunk.newStartLine} is out of range for ${path} (line numbers start at 1)`,\n\t\t\t);\n\t\t}\n\t\tconst lineHint = hunk.oldStartLine;\n\t\tconst allowAggressiveFallbacks = hunk.changeContext !== undefined || lineHint !== undefined || hunk.isEndOfFile;\n\t\tconst fallbackVariants = filterFallbackVariants(buildFallbackVariants(hunk), allowAggressiveFallbacks);\n\t\tif (lineHint !== undefined && hunk.changeContext === undefined && !hunk.hasContextLines) {\n\t\t\tlineIndex = Math.max(0, Math.min(lineHint - 1, originalLines.length - 1));\n\t\t}\n\n\t\t// If hunk has a changeContext, find it and adjust lineIndex\n\t\tif (hunk.changeContext !== undefined) {\n\t\t\t// Use hierarchical context matching for nested @@ anchors and space-separated contexts\n\t\t\tconst result = findHierarchicalContext(originalLines, hunk.changeContext, lineIndex, lineHint, allowFuzzy);\n\t\t\tconst idx = result.index;\n\t\t\tcontextIndex = idx;\n\n\t\t\tif (idx === undefined || (result.matchCount !== undefined && result.matchCount > 1)) {","sourceCodeStart":1088,"sourceCodeEnd":1124,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/modes/patch.ts#L1088-L1124","documentation":"Identical validation to error 1182 but for the new-side hunk header: hunk.newStartLine is defined and less than 1. The patcher requires 1-indexed positive line hints on both sides of the @@ header. Thrown before any file matching happens.","triggerScenarios":"A hunk header `@@ -1,5 +0,3 @@` (or negative new start) is parsed and passed to computeReplacements; 0-indexed new-side line numbers produced by a patch generator.","commonSituations":"Custom diff writers that emit 0-indexed new-line coordinates; pure-insertion hunks mislabeled with start line 0; diffs converted between tools that disagree on indexing.","solutions":["Correct the new-side start line in the @@ header to be >= 1.","Regenerate the patch with a standard diff tool (git diff) to get valid headers.","Remove the line hints and rely on context lines for locating the change.","If the patch creates a new file, switch to *** Add File semantics."],"exampleFix":"// before\n@@ -10,4 +0,2 @@\n\n// after\n@@ -10,4 +11,2 @@","handlingStrategy":"validation","validationCode":"if (hunk.newStartLine !== undefined && hunk.newStartLine < 1) {\n  throw new Error(`newStartLine must be >= 1, got ${hunk.newStartLine}`);\n}","typeGuard":"function hasValidNewHint(hunk: DiffHunk): boolean {\n  return hunk.newStartLine === undefined || hunk.newStartLine >= 1;\n}","tryCatchPattern":"try {\n  applyPatch(patch);\n} catch (err) {\n  if (err instanceof ApplyPatchError && err.message.includes('Line hint') && err.message.includes('start at 1')) {\n    // sanitize headers to 1-indexed values, then retry\n  } else throw err;\n}","preventionTips":["Validate both sides of the @@ header before applying.","Clamp generated newStartLine values to at least 1.","Prefer context-based location over new-side line hints.","Test patch generators against git-applied reference diffs."],"tags":["patch","validation","line-numbers"],"backgroundTag":"patch-line-hint-out-of-range","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}