fix(trace): refuse to install hooks when the root is a user-level directory

SA
1d ago
+234-0

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 run fails in TraceRun, before snapshotting, installing, or
    running the wrapped command.
  • trace init fails right after resolving the repository root, before any
    prompt or write.

Test plan:

  • TestIsUserLevelRoot in app/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).
  • TestTraceRunRefusesUserLevelRoot in
    app/cli/pkg/action/trace_run_test.go: both paths from the issue, no git
    repository with the working directory at $HOME and 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 TestIsUserLevelRoot and
    go test ./app/cli/pkg/action/ -run 'TestTraceRun|TestCleanupTrace|TestSnapshotAndRestore|TestPiTraceRun', all passing.