Skip to content

fix: correct after_line=-1 prepend position in append_file #16#20

Merged
four-bytes-robby merged 1 commit into
mainfrom
fix/16-append-file-after-line
Jun 14, 2026
Merged

fix: correct after_line=-1 prepend position in append_file #16#20
four-bytes-robby merged 1 commit into
mainfrom
fix/16-append-file-after-line

Conversation

@four-bytes-robby

@four-bytes-robby four-bytes-robby commented Jun 14, 2026

Copy link
Copy Markdown
Member

Closes #16

Bug

When mode=prepend and after_line=-1, content inserted at the wrong position (end of file instead of before the last line).

Root Causes

  1. split('\n') trailing newline inflation — Files ending with \n produce a trailing empty string in the split array, making lines.length one larger than the true line count
  2. Negative index formula — The old Math.max(0, lines.length - 2) only worked for files without trailing newlines, and only for -1 specifically

Fix

  • Strip the trailing empty string from split('\n') before computation, then restore it after
  • Use Python-style negative indexing: -1 = before last line, -2 = before second-to-last, etc.
  • insertPos = Math.max(0, lines.length + afterLine) for negative values

Tests Added

  • after_line=-1 on a 3-line file (before last line)
  • after_line=-1 on a 2-line file (edge case where old formula broke)

All 8 tests pass.


Summary by cubic

Fix incorrect insert position in append_file when mode=prepend and after_line=-1. Now inserts before the last line (and supports other negative indexes) while preserving trailing newlines.

  • Bug Fixes
    • Support Python-style negative after_line values (-1 before last line, -2 before second-to-last, etc.).
    • Normalize trailing newline: remove the extra empty split element before computing, then restore it.
    • Added tests for 3-line and 2-line files to cover after_line=-1.

Written for commit d55f68f. Summary will update on new commits.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Enhanced file prepend operations to correctly handle Python-style negative indexing and properly preserve file formatting.
  • Tests

    • Added test coverage for prepend mode with negative line indexing edge cases.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

Recent review info
Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b8733faf-f0c8-4c44-8424-af17bf7cf7e3

Commits

Reviewing files that changed from the base of the PR and between 993107a and d55f68f.

Files selected for processing (2)
  • src/tools/append-file.ts
  • tests/append-file.test.ts

Walkthrough

Walkthrough

The appendFileTool prepend-mode insertion is refactored to support Python-style negative indexing for the after_line parameter. The implementation now strips a trailing newline before computing the insert position, clamps the derived index, and restores the newline after insertion. Two new tests cover the -1 case.

Changes

Prepend Mode Negative Index Fix

Layer / File(s) Summary
Prepend insertion logic and argument description
src/tools/append-file.ts
after_line description is extended to document negative indexing semantics. Prepend-mode logic is rewritten to detect and strip a trailing newline, compute an insert position via Python-like negative index clamping, insert content between the derived slices, and restore the trailing newline.
Prepend mode after_line=-1 test cases
tests/append-file.test.ts
Two bun:test cases are added: one for a standard 3-line file and one for a 2-line edge case, each asserting the returned message contains prepend mode and that file contents reflect insertion before the last line.

Estimated code review effort

3 (Moderate) | ~15 minutes

Possibly related issues

Finishing Touches
Generate docstrings
  • Create stacked PR
  • Commit on current branch
Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/16-append-file-after-line
Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch fix/16-append-file-after-line

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Comment @coderabbitai help to get the list of available commands and usage tips.

@four-bytes-robby four-bytes-robby merged commit e73fe00 into main Jun 14, 2026
4 of 5 checks passed
@four-bytes-robby four-bytes-robby deleted the fix/16-append-file-after-line branch June 14, 2026 21:44
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.

fix: append_file after_line=-1 inserts at wrong position in prepend mode

1 participant