Repository navigation
perf(trace): cut the CPU cost of the shell hook snapshots - #3860
Conversation
The pre and post hooks of every agent shell call took a snapshot of the checkout that hashed every file, for each checkout on the session record. - Read the ignore rules with go-git's gitignore.Scope as the walk goes down, so excluded folders such as node_modules and nested worktrees are never read. The deprecated ReadPatterns walked all of them. - Keep a hash cache per checkout. A file is read again only when its size, mtime, ctime or inode changed; files changed in the last two seconds are never cached. - Do not add a repository to the session when a shell command or a file tool works inside its .git folder. Closes #3858 Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev> Chainloop-Trace-Sessions: dfa0c5da-5e77-4fac-bbb3-6be405e8b3a0
PR validation — ✅ 3 passing
AI Session Checks — 🟢 87% · ✅ 0 failing
|
| Status | Attribution | File | Lines |
|---|---|---|---|
| created | ai | app/cli/internal/trace/git/snapshot_test.go |
+269 / -0 |
| created | ai | app/cli/internal/trace/git/snapshot.go |
+229 / -0 |
| modified | ai | app/cli/internal/trace/git/gogit.go |
+0 / -108 |
| created | ai | app/cli/internal/trace/state/hashcache_test.go |
+80 / -0 |
| modified | ai | app/cli/pkg/action/trace_agent_hook.go |
+54 / -12 |
| modified | ai | app/cli/pkg/action/trace_shell_checkout_test.go |
+60 / -0 |
| created | ai | app/cli/internal/trace/state/hashcache.go |
+59 / -0 |
| created | ai | app/cli/internal/trace/git/snapshot_stat_darwin.go |
+33 / -0 |
| created | ai | app/cli/internal/trace/git/snapshot_stat_linux.go |
+33 / -0 |
| created | ai | app/cli/internal/trace/git/snapshot_stat_other.go |
+26 / -0 |
| modified | ai | app/cli/internal/trace/git/gogit_test.go |
+5 / -6 |
| modified | ai | app/cli/internal/trace/git/exec.go |
+0 / -7 |
| modified | ai | app/cli/internal/trace/git/git.go |
+0 / -5 |
| modified | ai | app/cli/pkg/action/trace_hook_handler_test.go |
+0 / -3 |
Policies (4)
| Status | Policy | Material | Messages |
|---|---|---|---|
| ✅ Passed | ai-config-ai-agents-allowed |
ai-coding-session-dfa0c5 |
- |
| ✅ Passed | ai-config-no-dangerous-commands |
ai-coding-session-dfa0c5 |
- |
| ✅ Passed | ai-config-no-secrets |
ai-coding-session-dfa0c5 |
- |
| ✅ Passed | ai-config-mcp-servers-allowed |
ai-coding-session-dfa0c5 |
- |
Security Checks — ⚠️ 1 failing
⚠️ AI Security Analysis
The per-PR audit did not finish: 2 concerns went unreviewed (spend ceiling or time). No introduced risk was found in what it reviewed, but treat this as incomplete, not clean.
View in Chainloop ↗ · How this works ↗
✅ Secret Scan
| Status | Policy | Messages |
|---|---|---|
| ✅ Passed | secrets-detection |
- |
✅ SAST Scan
| Status | Policy | Messages |
|---|---|---|
| ✅ Passed | owasp-top10-2025 |
- |
| ✅ Passed | sast |
- |
| ✅ Passed | cwe-top25 |
- |
| ✅ Passed | cwe-top26-40-cusp |
- |
Scans not applied (3)
| Scan | Reason |
|---|---|
| Vulnerability Scan | no manifest/lockfile changed |
| GitHub Actions Scan | no workflow files changed |
| IaC Scan | no IaC files changed |
Powered by Chainloop and Chainloop Trace
There was a problem hiding this comment.
All reported issues were addressed across 14 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
- Snapshot the tracked files below an excluded folder, as git status does, so shell edits of a committed file in a folder that a later .gitignore rule excludes are attributed. The folder is walked only when the index tracks files below it, and no ignore rules are read there. - Do not read a cached hash for a file with a recent stat, as well as not writing one. - Check the path of a file tool write itself for .git, so a write to the .git file of a linked worktree does not join its repository to the session. Refs #3858 Assisted-by: Claude Code Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev> Chainloop-Trace-Sessions: dfa0c5da-5e77-4fac-bbb3-6be405e8b3a0
|
Merged by eng-autopilot: approved by jiparis; no change request; CI passed (16 check runs, 3 workflow runs); GitHub merge state clean; no Greptile review. 🤖 Posted by Maximus bot (Claude Code) on behalf of @migmartri using the eng-autopilot skill |
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
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.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..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_modulesand.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.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