fix(trace): refuse to install hooks when the root is a user-level directory
SA
1d ago+234+234+012345678901234567890123456789-0-0-0123456789
Fixes #3812
trace run outside a git repository uses the working directory as the root
and still installs the agent hooks there. Run from $HOME, that writes the
user-level configuration (~/.claude/settings.json, ~/.codex/hooks.json,
~/.cursor/hooks.json), so the hooks load in every session of that agent in
any project, and a killed run leaves them there. The same applies to a git
repository rooted at $HOME.
This adds trace.IsUserLevelRoot, which resolves the root with symlinks
evaluated and compares it against the home directory and the agent home
folders ($CODEX_HOME, $COPILOT_HOME, defaulting to ~/.codex and
~/.copilot). Both entry points refuse such a root before anything is
installed or asked:
trace runfails inTraceRun, before snapshotting, installing, or
running the wrapped command.trace initfails right after resolving the repository root, before any
prompt or write.
Test plan:
TestIsUserLevelRootinapp/cli/internal/trace/userlevel_test.go: home
itself, home through a symlink, a project below home, an unrelated
directory, and both agent home folders (explicit and default).TestTraceRunRefusesUserLevelRootin
app/cli/pkg/action/trace_run_test.go: both paths from the issue, no git
repository with the working directory at$HOMEand a git repository
rooted at$HOME. Both fail with the user-level error, and neither writes
agent hooks or trace state under home.- Ran
go test ./app/cli/internal/trace/ -run TestIsUserLevelRootand
go test ./app/cli/pkg/action/ -run 'TestTraceRun|TestCleanupTrace|TestSnapshotAndRestore|TestPiTraceRun', all passing.