perf(trace): cut the CPU cost of the shell hook snapshots
Closes #3858.
The trace hooks run before and after every agent shell call. Each one took a snapshot of the checkout that SHA-256 hashed every file, and did it again for every checkout on the session record. On a large repository this made each Bash hook cost 0.5–1.3 s of CPU. With parallel tool calls, the hooks used more CPU than the agent itself. The root cause analysis is in the issue.
Changes
- Ignore rules are read with go-git's
gitignore.Scope, folder by folder as the walk goes down. Folders that a rule excludes, such asnode_modulesand worktrees nested under the main checkout, are never read. The deprecatedReadPatternswalked all of them. The behavior now matches git: a rule below an excluded folder cannot include a file again. Files that the index tracks below an excluded folder are now snapshotted, asgit statusdoes: the walk goes into such a folder only for those files. The old walk missed them. - Hash cache per checkout (
chainloop-trace/worktree-hashes.json). A file is read and hashed again only when its size, mtime, ctime or inode changed. A file changed in the last two seconds never uses the cache and is never written to it, the same safeguard git uses for racily clean index entries. A push does not wipe the cache. - A repository is not added to the session when a shell command or a file tool works inside its
.gitfolder, or writes the.gitfile of a linked worktree. For example,cd <repo>/.git/worktrees/<name>registered the main checkout. From then on, every shell call of the session snapshotted it. Client.SnapshotWorktreeis removed, because no production code calls it now. The hooks callSnapshotWorktreeCached.
This also explains the worktree slowdown a customer reported. A session in a main checkout with worktrees nested inside it, or a session that edited files in other worktrees, paid for walking or hashing all of them on every shell call.
End-to-end cost
I ran the real CLI binary the way Claude Code runs it: one process per hook, with the payload on stdin. Both binaries were built from the same base commit. The data was isolated clones of this repository with real content: 3,429 files per checkout, the real docs/node_modules and .opencode/node_modules, and 11 linked worktrees under .claude/worktrees, which give 10,971 folders in the main checkout. Warm figures are the median of calls 2–10 of a session, in CPU seconds (user + sys) per hook.
| Session layout | Before | After (warm) | After (first call) |
|---|---|---|---|
| Session in a linked worktree | 0.50 | 0.21 | 0.55 |
| Session in the main checkout (node_modules + 11 worktrees) | 0.95 | 0.20–0.21 | 0.51 |
| Worktree session with the main checkout on its record | 1.31 | 0.27–0.29 | 0.87 |
| 8 parallel shell calls in that layout, total CPU (pre / post) | 14.0 s / 13.8 s | 2.9 s / 2.8 s | |
| 8 parallel shell calls, batch wall time (pre / post) | 1.94 s / 1.93 s | 0.43 s / 0.42 s |
- Max RSS per hook went from 113–125 MB to 101–111 MB.
- About 0.14 s of each warm hook is process startup, which every hook pays even with nothing to do. The snapshot work itself went from 0.36–1.2 s to about 0.07 s.
- The cache is about 740 KB for this repository and is written only when it changes.
- In both binaries, a shell call that changed a tracked file and created a new one in each checkout recorded the same files in both ledgers, with the cache warm.
Out of scope: the fixed startup cost of each hook process, and snapshot files left behind when a shell call is denied.
This PR was written with the assistance of Claude Code.
🤖 Posted by Maximus bot (Claude Code) on behalf of @migmartri