plandex-ai/plandex · error

invalid line number for first line: %d

Error message

invalid line number for first line: %d

What it means

When only a first line number is given (lastLineNum==0, single-line replacement), the number must be within the original file (1..len(originalFileLines)). This error is returned when the extracted first line number is 0 or exceeds the file length.

Source

Thrown at app/server/model/plan/build_validate_and_fix.go:420

		firstLineNum, err := shared.ExtractLineNumberWithPrefix(firstLine, "pdx-")
		if err != nil {
			log.Printf("Error extracting line number from first line: %v", err)
			return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("error extracting line number from first line: %v", err)
		}

		if lastLine != "" {
			lastLineNum, err = shared.ExtractLineNumberWithPrefix(lastLine, "pdx-")
			if err != nil {
				log.Printf("Error extracting line number from last line: %v", err)
				return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("error extracting line number from last line: %v", err)
			}
		}

		if lastLineNum == 0 {
			if !(firstLineNum > 0 && firstLineNum <= len(originalFileLines)) {
				log.Printf("Invalid line number for first line: %d", firstLineNum)
				return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("invalid line number for first line: %d", firstLineNum)
			}
			old = originalFileLines[firstLineNum-1]
		} else {
			if !(firstLineNum > 0 && firstLineNum <= len(originalFileLines) && lastLineNum > firstLineNum && lastLineNum <= len(originalFileLines)) {
				log.Printf("Invalid line numbers for first and last lines: %d-%d", firstLineNum, lastLineNum)
				return buildValidateResult{valid: false, updated: updated}, fmt.Errorf("invalid line numbers: %d-%d", firstLineNum, lastLineNum)
			}
			old = strings.Join(originalFileLines[firstLineNum-1:lastLineNum], "\n")
		}

		// log.Printf("Applying replacement.\n\nOld:\n\n%s\n\nNew:\n\n%s", old, new)

		incremental = shared.LineNumberedTextType(strings.Replace(string(incremental), old, new, 1))

		// log.Printf("Updated content:\n\n%s", string(incremental))
	}

	var problem string

View on GitHub (pinned to e2d772072e)

Solutions

  1. Refresh the file content/line numbering sent to the model and retry
  2. Re-send the file with line numbers so the model can pick a valid target
  3. Add a pre-check that clamps or rejects out-of-range line numbers with a retry prompt
Defensive patterns

Strategy: validation

Validate before calling

if !(firstLineNum > 0 && firstLineNum <= len(originalFileLines)) {
    return fmt.Errorf("first line number %d out of range (1..%d)", firstLineNum, len(originalFileLines))
}

Type guard

func inRange(n, max int) bool { return n > 0 && n <= max }

Prevention

When it happens

Trigger: Model references a line number outside the current file — typically 0, negative after parse issues, or greater than the file's line count (stale numbering).

Common situations: File changed since the model last saw it (context drift), model hallucinating line numbers on large files, or off-by-one confusion.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/1357099569bceb9f. Report an issue: GitHub.