fix(trace): attribute commits made inside Bash calls

Merged
WA
25d ago
+1629-55

Closes #3625

Problem

Claude Code can edit, stage, commit, and push within one foreground Bash tool call. The commit-msg hook runs before Claude's PostToolUse hook, so the staged files previously had no recorded AI attribution and the commit lost its Chainloop-Trace-Sessions trailer.

Narrowed scope

This change now implements only the foreground Claude Bash path requested in the maintainer scope review:

  • tags the exact Bash call with an opaque token while preserving the rest of tool_input;
  • snapshots the index through NewGitClient: Git handles split, sparse, and temporary indexes when available; GoGit handles ordinary indexes otherwise;
  • compares the pre-call index with the staged index at commit-msg, so content staged before the call is not claimed;
  • reads committed content from the staged blob rather than the worktree, and preserves worktree/HEAD snapshots for ordinary shell attribution when the index cannot be decoded;
  • checkpoints the worktree against the staged blob's raw-byte hash, so different, missing, recreated, or restored-pre-call worktree content remains pending;
  • advances the open call's HEAD after commit so PostToolUse does not requeue committed attribution; and
  • keeps the existing commit, trailer, session-record, and pre-push evidence pipeline.

StagedFiles and SnapshotWorktree follow main. Broader Git-clean and index-edge-case parity remains in the follow-up issues below.

Deferred behavior

Validation

  • focused race regressions cover the same-call path, pre-staged safety, split-index fallback, split and sparse Git index reads, staged-only content, raw worktree divergence, restored pre-call bytes, recreated deletion, stale calls, and Claude command tagging;
  • after merging current main, the race-enabled pkg/action, internal/trace/git, internal/trace/claude, and internal/trace/state suites pass;
  • CLI build and go vet ./... pass;
  • earlier CLI-level QA in a temporary repository verified help, Trace initialization, command tagging, a same-call commit trailer, CommitRecord persistence, HEAD advancement, attribution consumption, and silent invalid-input handling; and
  • local golangci-lint cannot load the repository config because the installed binary was built with Go 1.26 while the repository targets Go 1.27.3. The current-head lint (cli) CI job is the authoritative lint result.

The earlier live headless Claude Code testing in this thread was performed by @jiparis. The contributor validation above is automated Go testing plus CLI-level hook QA, not an independent live Claude Code run.

Assisted-by: Vibhav's Pi

Remaining policy gate

This PR still needs an approving review and AI-session evidence or a skip-ai-session label. The available Chainloop login cannot upload evidence to the project, and the repository label API returned HTTP 403 when asked to apply this label to #3719 (admin rights required). A maintainer with admin access must add it here; code, test, DCO, and signing checks remain separate.