ai-devkit icon indicating copy to clipboard operation
ai-devkit copied to clipboard

investigate: worktree index corruption during pre-commit hooks — 6 occurrences, empty/stray commits injected

Open codeaholicguy opened this issue 3 weeks ago • 0 comments

Summary

Since 2026-10, AI-agent workers building PRs in isolated worktrees have hit a recurring failure: during the pre-commit hook window (~2-10 min, full lint+test suite), the worktree's git index is mutated by something other than the committing process — staged files get unstaged, and foreign content (another agent's in-progress edits) gets staged in. The resulting commit is empty (tree identical to base) or contains stray files. If undetected and pushed: a silently empty or wrong PR. All 6 known occurrences were caught locally by post-commit git show --stat verification and reset — nothing corrupted has merged — but each costs reset-retry cycles, forced --no-verify commits, and in two cases exhausted the agent's remaining context.

Occurrences (evidence in session ledgers + two preserved patches)

# When Where Signature
1 2026-10-03 memory-mcp PR #308 build commit lost staged files; repaired with --no-verify
2 2026-10-05 same feature, successor agent 3 staged paths → commit swept 69 files identical to main; index mtime mid-hook, ORIG_HEAD/FETCH_HEAD written seconds apart; no git-shelling test found
3 2026-10-16 console rename fix commit captured web-logo files not part of the fix (masked as 0-file PR because main had the same content)
4 2026-10-17 skill security PRs empty commit + stray node_modules symlink class
5 2026-11-07 skill-add-all (#425) two corrupted commits; foreign = old MCP generator hunks; patch preserved at agent-remote/foreign-mcp-commit-92027cf.patch
6 2026-11-09 release 0.73.1 empty commit 9d0513d; agent race-instrumented (watch-loop comparing staged vs worktree through a background full-suite run)

Leading theory (from occurrence 5-6 forensics)

Concurrent git operations from multiple agents crossing worktree boundaries via inherited environment/state. On this box, several AI agents operate on the same repo simultaneously; several have been observed working in the SHARED main checkout despite isolated-worktree instructions (panes show them on foreign branches in ~/code/ai-devkit). A pre-commit hook spawned from worktree A inherits GIT_DIR/GIT_INDEX_FILE/cwd — if any of these resolve into the shared checkout's index (or a hook child chdirs and runs git), you get exactly this signature: A's staged files vanish from its commit while B's in-flight edits appear. Occurrence 5's foreign content matched another agent's in-progress work; occurrences cluster exactly when multiple agents are active; single-agent periods show zero occurrences.

Investigation plan

  1. Reproduce deterministically: two shells, one committing in a worktree with a slow hook, one operating in the main checkout — instrument index mtime + git diff --cached snapshots
  2. Audit hook config (husky/lefthook interplay; note: an untracked lefthook.yml variant exists in the main checkout) for env leakage (GIT_INDEX_FILE, cwd, or tools that chdir)
  3. Determine whether nx/npm test children invoke git (session tracker? hooks?) with inherited env
  4. Fix candidates: hard isolation (agents never share the main checkout — orchestrator-exclusive), hook env sanitization (env -u GIT_INDEX_FILE...), or index-file pinning per commit

Immediate mitigations already in force

  • Post-commit git show --stat + pre-push git ls-tree verification in every agent brief (caught all 6)
  • Isolated-worktree-mandatory brief language (violated in practice — needs enforcement)

Acceptance criteria

  • Deterministic reproduction (or rigorous elimination of the theory with evidence)
  • Root-cause fix landed OR isolation enforcement proven sufficient
  • The verification gates stay (defense in depth)

codeaholicguy avatar Nov 09 '26 17:11 codeaholicguy