Keep Tests out of the Real Profile Root¶
amplihack install refreshes the hooks in every profile's settings.json
under the profile root (issue #1661). Before #1699, a test install with a
temp HOME could inherit a real AMPLIHACK_CLAUDE_PROFILE_ROOT and rewrite
every real profile so that its hooks pointed at
/tmp/.tmpXXXX/home/.local/bin/amplihack-hooks, a binary deleted when the
test ended.
This guide is for contributors who write tests. It describes the three controls that keep a test run away from the real profile root, how to write a test that exercises the refresh, and what to do when the guard fails.
Contents¶
- The three controls
- Write a test that starts the binary
- Test the profile refresh in process
- Pass the profile variables to multitask launchers
- When the real-root guard fails
- Limits
The three controls¶
| Control | Where | What it does |
|---|---|---|
| Refusal | refresh_profile_settings in crates/amplihack-cli/src/commands/profile/settings_refresh.rs |
Refuses the refresh when the hooks binary is missing; when HOME is not the account's passwd home and the profile root is outside HOME, wherever the binary lies; and when the binary is under a temp directory, unless the profile root is under HOME and itself under a temp directory. See the profile reference. |
| Isolation | tests/support/isolated_amplihack.rs |
isolated(..) clears the environment and also calls .env_remove(PROFILE_ROOT_ENV), so no child inherits AMPLIHACK_CLAUDE_PROFILE_ROOT. |
| Guard | crates/amplihack-cli/tests/issue_1699_real_root_guard.rs |
Hashes the real profiles' settings.json files before main on Linux (when the guard test runs on other Unix systems) and again when the test process exits. It fails the run on any change, with two exceptions printed as a warning: a settings.json whose hooks path lies under another process's temp directory, and one naming only a real home's installed amplihack-hooks that was replaced during the run (another session's amplihack install). |
The refusal is the only one that protects users. It is enforced in
refresh_profile_settings, the single production entry point; refresh_in
is unchecked and is called directly only by unit tests.
env_clear() in isolated(..) remains the main isolation control. The
explicit env_remove is a second layer that still holds if a later change
adds the profile root to the list of variables isolated(..) keeps.
crates/amplihack-cli/tests/issue_1699_isolated_profile_root.rs checks it
in two ways. One test sets the variable and asserts that the built
Command gives the child no value for it; after env_clear() this only catches a kept list
that copies the variable. A source assertion checks that
tests/support/isolated_amplihack.rs calls env_remove(PROFILE_ROOT_ENV)
after the loop that copies the kept variables, so deleting that line fails
the test.
A test that calls run_install in process is outside both isolated(..)
and the library's cfg!(test) gate, so it must call
child_remove_var("AMPLIHACK_CLAUDE_PROFILE_ROOT") itself.
in_process_installs_remove_the_profile_root in the same file fails for any
file under crates/amplihack-cli/tests that calls run_install without
that line.
Write a test that starts the binary¶
Start the binary through isolated(..) and give it a profile root inside
the temp HOME. A root outside a HOME that is not the account's home is
exactly the case the refusal blocks, wherever the hooks binary lies, so such
a test sees Skipped for every profile:
use crate::isolated_amplihack::{isolated, PROFILE_ROOT_ENV};
let tmp = tempfile::tempdir().unwrap();
let home = tmp.path().join("home");
let mut cmd = isolated(env!("CARGO_BIN_EXE_amplihack"));
cmd.env("HOME", &home)
.env(PROFILE_ROOT_ENV, home.join("profiles"))
.args(["install"]);
let out = cmd.output().unwrap();
assert!(out.status.success());
Set the profile root only to a path under the test's own temp directory. Never copy the parent's value.
Test the profile refresh in process¶
Choose the call by what the test checks:
| Test checks | Call |
|---|---|
| The refusal rule itself | hook_path_refusal(hooks_bin, root, home, account_home, temp_bases) with injected account_home and temp_bases |
| The JSON edit on a fixture | refresh_in(&store, update) (unchecked) |
| The whole install path | refresh_profile_hooks under env_lock(), with HOME and the root both in one temp directory |
hook_path_refusal and refresh_in take all their inputs as arguments and
are called directly; they need no environment variables. In production the
two injected values come from amplihack_utils::secure_files::passwd_home()
and amplihack_utils::secure_files::temp_bases(). The real-root guard calls
the same two functions to decide which roots are real, so the guard and the
refusal cannot use different rules. In-crate calls to
refresh_profile_hooks reach the refresh only when
AMPLIHACK_TEST_PROFILE_REFRESH is set, and the test must hold env_lock()
while it changes process variables.
For the whole install path, create a real executable hooks file at
<home>/.local/bin/amplihack-hooks and put the root at <home>/profiles.
install_registers_this_builds_hooks_in_each_profile in
crates/amplihack-cli/src/commands/install/tests/profile_refresh_install.rs
uses this layout.
Pass the profile variables to multitask launchers¶
The multitask launchers write export lines for
AMPLIHACK_CLAUDE_PROFILE and AMPLIHACK_CLAUDE_PROFILE_ROOT into
run.sh. A test that calls write_recipe_launcher or
write_classic_launcher reads both variables from the process, and fails
whenever another test has set the root to a value that is not valid UTF-8
(issue #2014). Call the _with variants and pass the environment instead:
use super::command_builder_tests::{keep, no_env};
use super::launcher::write_recipe_launcher_with;
write_recipe_launcher_with(&ws, "amplihack claude", no_env, keep).unwrap();
no_env pins no profile, so the auto-switch substitution and the real
profile store are never reached. To test the exports themselves, pass
getter(&[(PROFILE_VAR, "work"), (ROOT_VAR, "/p")]) to
recipe_run_script or classic_run_script. getter is private to
command_builder_tests.rs, so write such tests in that file; only no_env
and keep are pub(super). Do not widen getter to import it elsewhere.
Do not set the variables in the process for these tests. The two-argument
launchers are tested only by issue_2014_launcher_env.rs, under with_env.
See Bug Fix #2014 for the seam and the regression tests.
When the real-root guard fails¶
Issue #1699 asks that a test run which changes any file under the real profile root fails, and the guard keeps that rule. It makes two exceptions, each only on positive evidence that another process made the change.
Hooks under another process's temp directory¶
An unfixed checkout's test run on the same host rewrites every profile so that
its hooks point at amplihack-hooks under that run's temp directory (on
2026-10-29 at 15:30:37 every real profile on the development host was
rewritten to /tmp/amplihack-workflow-2vXScw/...). A changed settings.json
is put down to another process, listed under a warning and does not fail the
run, only when all of these hold:
std::env::temp_dir()of this process is not/;- the file still exists and can be read;
- it names an
amplihack-hookspath under one of the temp bases (temp_dir(),$TMPDIR,/tmp); - it names nothing under this process's
temp_dir().
A real amplihack install from another session¶
amplihack install replaces ~/.local/bin/amplihack-hooks and then
rewrites every profile's hooks to point at it. On the development host this
happens every few minutes while other sessions work (on 2026-10-29 the
binary was replaced at 16:38:08 and all 17 profiles were rewritten at
16:38:13, in the middle of a 30-minute QA run). The guard records the
device, inode, length and modification time of
<home>/.local/bin/amplihack-hooks for $HOME and the passwd home (homes
under a temp directory are skipped) before the tests start, and again at
exit. A changed settings.json is put down to that install only when all of
these hold:
- the file still exists and can be read;
- one of those installed binaries was replaced (or first appeared) during the run;
- every
amplihack-hookspath the file names is such a replaced binary, and it names at least one; - it names nothing under any temp base.
The tests in these binaries install only under temp homes, so they cannot replace those binaries.
Either exception prints the path under a warning and does not fail the run:
issue_1699 real-root guard: warning: changed by another process (hooks under another temp directory with nothing under /home/user/.amplihack/work/tmp named, or only an installed hooks binary replaced during this run), not failing:
/home/user/.claude-profiles/work/settings.json
Every other change fails¶
Every other change fails the run: .auto-switch.d/rotate/root, a
settings.json without a temp hooks path or a replaced installed binary
(for example a profile add, seeding or status-line write leaked by a
test), a removed or unreadable file, and a file naming this process's temp
directory. The guard then prints
those paths and a hint to re-run, and exits the test process with status 1.
This happens after libtest prints test result: ok, and no test is named as
the failure:
issue_1699 real-root guard: real profile root changed during this test run:
/home/user/.claude-profiles/work/settings.json
issue_1699 real-root guard: another process can cause this (`amplihack install`, `profile add`, rotator status-line setup or a rotator restart during the run); re-run the tests before investigating
- Check whether another process changed the file during the run, for
example
amplihack install,profile add,/config, a rotator restart (record_rootrewrites<root>/.auto-switch.d/rotate/root), or another checkout's test run whileTMPDIRwas unset or/tmp. The guard compares content, not mtimes, so only a change of content trips it. If so, run the tests again, ideally with aTMPDIRof their own. - Otherwise a test wrote the file. List the tests that set
HOMEwithoutisolated(..):
Each match must either go through isolated(..) or remove
AMPLIHACK_CLAUDE_PROFILE_ROOT in the same scope.
3. Restore the profile's hooks by running your installed amplihack install
from a normal shell. It rewrites amplihack's hook entries and keeps
everything else.
The guard reads only <root>/<profile>/settings.json and
<root>/.auto-switch.d/rotate/root, for the resolved root,
~/.claude-profiles and ~/.amplihack/claude-profiles. It never writes
under those roots, never reads .credentials.json, does not follow
symlinks, hashes at most 1 MiB per file and prints only paths. Roots under a
temp directory are skipped, so on CI, where no real root exists, it checks
nothing and passes.
Limits¶
- The temp-directory exception needs a temp directory of its own. When
TMPDIRis unset or/tmp, a temp hooks path written by any process names this process's temp directory too, so every change fails the run. With aTMPDIRof its own, a change made by this run is still caught unless another process later rewrites the same file with hooks under its own temp directory; only the later content is judged. - Other changes made legitimately during the run by another process (a
profile add, a rotator restart, anamplihack installfrom a checkout whose hooks binary is not in~/.local/bin) fail the run, as the issue asks; re-run the tests. -
A test that ran a real install with the real
HOMEwould replace the installed binary too, and its profile rewrite would only warn. The guard cannot tell that apart from another session's install. Every test that runs an install must set a tempHOME; thegrepin step 2 of When the real-root guard fails lists the tests that set one. -
The guard takes its baseline in
.init_arraybeforemainon Linux, and when the guard test runs on other Unix systems, so it does not see changes made before that point. It does not see changes made by other test binaries; libtest has no hook that runs before or after the whole suite. - A
settings.jsonthat cannot be read both before and after the run is not compared. A change in readability is reported. - On failure the guard calls
_exit(1), which skips later exit handlers. A leftover temp directory is possible. - The throwaway-
HOMErefusal needs the account's passwd entry. Without one (some containers, and Windows), a scratchHOMEthat is not under a temp directory, with its hooks binary inside it, is not refused when the root is outside it. A scratchHOMEunder a temp directory still is.
Related¶
amplihack profilereference - The #1661 refresh and the #1699 refusal- Keep Tests from Leaking Rotator Daemons - The other stops
isolated(..)sets - Give Test Logins a Fake Account Identity - Fake logins and a loopback identity endpoint for profile tests (#1753)
- AMPLIHACK_* environment variables -
AMPLIHACK_CLAUDE_PROFILE_ROOT - Developing amplihack - Build and test setup
- Bug Fix #2014 - Why the multitask launcher tests pass the profile variables as arguments