nanocoder
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
Description
maybeAutoCommit in source/services/auto-commit.ts runs git add -- <path> (line 133) and then git commit -m ... -- <path> (line 146). Two problems:
- If the commit fails (for example a failing
pre-commithook), nothing is undone. Thecatch(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 aftergit add. 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 matchdocs/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
maybeAutoCommitdirectly)
Steps to Reproduce
Failed commit:
- Create a git repo with one committed file
a.txt. Turn onnanocoder.autoCommit. - Add
.git/hooks/pre-committhat prints a message and exits 1. - Let the agent edit
a.txt. - Run
git statusandgit rev-list --count HEAD.
Same-file changes:
- Use a repo with no hook. Commit
a.txtcontainingline1,line2,line3. - As the user, add
USER WIP DEBUGas the first line. Do not commit it. - Let the agent add
agent lineat the bottom. - 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-onlyprintsa.txt. The next commit the user makes will include the agent's edit. - Same-file changes: the commit contains both
+USER WIP DEBUGand+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-commithook insource/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
Taking this issue will propose my plan soon!
Let me know what you think about this plan,suggestions are welcome!
Implementation Plan
-
State Preservation & Rollback on Commit Failure (
source/services/auto-commit.ts):- Before executing
git add -- <pathspec>, inspect the current index state for that pathspec usinggit diff --cached --name-only -- <pathspec>. - Wrap the staging and commit sequence in a
try...catch:- If
git commitfails or throws, and the pathspec was not staged prior to auto-commit, automatically roll back the index state usinggit reset -q -- <pathspec>. - Ensure the file contents in the working tree remain untouched.
- If
- Before executing
-
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.
- Clarify the isolation scope in documentation: auto-commit isolates edits on a per-file basis (
-
Automated Unit Tests (
source/services/auto-commit.spec.ts):- Add a test with a failing
pre-commithook (exiting1) asserting that:maybeAutoCommitreturnsnulland logs a warning.- The target file is not left staged in the git index (
git diff --cached --name-onlyis empty). - Working tree modifications remain intact.
- Verify all existing git service and auto-commit tests pass.
- Add a test with a failing