nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] autoCommit leaves the edit staged when the commit fails, and can commit the user's own changes in the same file

Open addyCooks opened this issue 1 month ago • 2 comments

Description

maybeAutoCommit in source/services/auto-commit.ts runs git add -- <path> (line 133) and then git commit -m ... -- <path> (line 146). Two problems:

  1. If the commit fails (for example a failing pre-commit hook), nothing is undone. The catch (lines 89-92) only logs, so the agent's edit stays staged in the user's index. The comment above the function says the edit stays in the working tree "exactly as if auto-commit were off", which is not true after git add.
  2. git commit -- <path> commits the whole file, not just the agent's lines. If the user already had uncommitted changes in that file, they go into the agent's commit. This does not match docs/configuration/index.md:284: "Your own staged or unstaged changes are left exactly as they were".

Environment

  • OS: Windows 11 (10.0.26300)
  • Node version: 22.22.3
  • Nanocoder version: 1.31.0 (main at 5c2e693)
  • Git: 2.55.0.windows.3
  • Provider / Model: not relevant (reproduced by calling maybeAutoCommit directly)

Steps to Reproduce

Failed commit:

  1. Create a git repo with one committed file a.txt. Turn on nanocoder.autoCommit.
  2. Add .git/hooks/pre-commit that prints a message and exits 1.
  3. Let the agent edit a.txt.
  4. Run git status and git rev-list --count HEAD.

Same-file changes:

  1. Use a repo with no hook. Commit a.txt containing line1, line2, line3.
  2. As the user, add USER WIP DEBUG as the first line. Do not commit it.
  3. Let the agent add agent line at the bottom.
  4. Run git show HEAD -p.

Expected Behavior

  • After a rejected commit, the index is the same as before auto-commit ran.
  • The user's own changes are not committed under the agent's message (or the docs say clearly that they are).

Actual Behavior

  • Failed commit: the commit count is still 1, but git diff --cached --name-only prints a.txt. The next commit the user makes will include the agent's edit.
  • Same-file changes: the commit contains both +USER WIP DEBUG and +agent line.

Suggested Fix

  • Remember whether the path was staged before git add. If the commit fails, restore that state (for a file that was not staged before, git reset -q -- <path>).
  • For the same-file case, either skip files that already had uncommitted changes before the agent edited them, or update the docs to say the whole file is committed.
  • Add a test with a rejecting pre-commit hook in source/services/auto-commit.spec.ts (the failure branch at lines 89-92 has no coverage).

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

addyCooks avatar Oct 08 '26 16:10 addyCooks

Taking this issue will propose my plan soon!

rishu685 avatar Oct 08 '26 16:10 rishu685

Let me know what you think about this plan,suggestions are welcome!

Implementation Plan

  1. State Preservation & Rollback on Commit Failure (source/services/auto-commit.ts):

    • Before executing git add -- <pathspec>, inspect the current index state for that pathspec using git diff --cached --name-only -- <pathspec>.
    • Wrap the staging and commit sequence in a try...catch:
      • If git commit fails or throws, and the pathspec was not staged prior to auto-commit, automatically roll back the index state using git reset -q -- <pathspec>.
      • Ensure the file contents in the working tree remain untouched.
  2. Documentation & Behavior Clarification (docs/configuration/index.md):

    • Clarify the isolation scope in documentation: auto-commit isolates edits on a per-file basis (-- <pathspec>), meaning other untouched files in the working tree/index are untouched, but any pre-existing uncommitted changes in the same edited file will be included in that file's commit.
  3. Automated Unit Tests (source/services/auto-commit.spec.ts):

    • Add a test with a failing pre-commit hook (exiting 1) asserting that:
      • maybeAutoCommit returns null and logs a warning.
      • The target file is not left staged in the git index (git diff --cached --name-only is empty).
      • Working tree modifications remain intact.
    • Verify all existing git service and auto-commit tests pass.

rishu685 avatar Oct 08 '26 17:10 rishu685