fix(trace): attribute commits made inside Bash calls
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
PostToolUsedoes 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
- #3698: advanced Git index semantics, including full temporary-index parity without the Git binary,
git commit --only, intent-to-add, conflict stages, and replacement refs - #3699: Git-clean worktree comparison, including attributes, line-ending normalization, and clean filters
- #3700: tracked paths below ignored directories, including safe symlink and special-file traversal
- #3701: provider and runtime expansion, including background Bash calls, competing input rewrites, narrow permissions, and non-Claude providers
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-enabledpkg/action,internal/trace/git,internal/trace/claude, andinternal/trace/statesuites 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-lintcannot load the repository config because the installed binary was built with Go 1.26 while the repository targets Go 1.27.3. The current-headlint (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.