Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 33 additions & 19 deletions tests/test_doctor_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,21 +511,17 @@ def test_managed_hooks_are_inits_and_only_root_can_change_them(self):
self.assertEqual(got["D-HOOKS-PRESENT"]["detail"], f"v2 hooks in {self.managed}")
bad = self.rows(trusted="/etc/claude-code is group- or world-writable")["D-HOOKS-MANAGED"]
self.assertEqual((bad["status"], bad["detail"]), ("fail", "/etc/claude-code is group- or world-writable"))
self.assertTrue(bad["fix"].endswith(" --managed"), bad["fix"])
self.assertTrue(bad["fix"].endswith("; or " + doctor.REINIT), bad["fix"]) # collect() adds --managed
bad = self.rows(readable=False)["D-HOOKS-MANAGED"]
self.assertEqual(bad["status"], "fail")
self.assertIn(f"agent can't read {self.managed}, so its Claude Code starts without it", bad["detail"])

def test_the_fix_keeps_managed_only_and_runs_on_macos(self):
"""Following the fix after init --managed-only turned allowManagedHooksOnly off: init without --managed-only
removes the setting an earlier init turned on. And `chown root:root` fails on macOS, which has no root group."""
for only, want in ((True, " --managed --managed-only"), (False, " --managed")):
with self.subTest(managed_only=only), contextlib.redirect_stdout(io.StringIO()):
install.install_managed(None, only, self.managed, install.OPT_PYTHON, module=install.V2_HOOK,
signer="/run/s.sock")
fix = self.rows(trusted="/etc/claude-code is group- or world-writable")["D-HOOKS-MANAGED"]["fix"]
self.assertTrue(fix.endswith(want), fix)
self.assertTrue(fix.startswith(f"sudo chown 0:0 and sudo chmod 644 {self.managed}, "), fix)
def test_the_fix_runs_on_macos(self):
"""`chown root:root` fails on macOS, which has no root group. (collect() adds --managed and --managed-only to
the re-init: NoHooksChosen.)"""
fix = self.rows(trusted="/etc/claude-code is group- or world-writable")["D-HOOKS-MANAGED"]["fix"]
self.assertEqual(fix, f"sudo chown 0:0 and sudo chmod 644 {self.managed}, and sudo chmod go-w the directories "
f"above it; or {doctor.REINIT}")

def test_managed_file_check_claims_only_what_it_checked(self):
"""D-HOOKS-MANAGED said the file was readable by every user, though doctor probes only the agent's user, and
Expand Down Expand Up @@ -554,7 +550,7 @@ def test_inits_hooks_also_in_the_agents_own_settings_are_decided_twice(self):
self.assertEqual(got["status"], "warn")
self.assertEqual(got["detail"], f"v2 hooks in {self.managed}, and in {own} too: Claude Code runs both, so "
"every call is decided twice")
self.assertTrue(got["fix"].endswith(" --managed"), got["fix"])
self.assertEqual(got["fix"], doctor.REINIT) # collect() adds --managed

def test_own_hooks_allow_managed_hooks_only_stops_are_not_decided_twice(self):
"""With allowManagedHooksOnly on, Claude Code runs none of the agent's own hooks, yet doctor warned that init's
Expand All @@ -576,7 +572,7 @@ def test_the_fix_for_missing_managed_hooks_keeps_them_managed(self):
install.install_managed(None, False, self.managed, uninstall=True)
got = self.rows()["D-HOOKS-PRESENT"]
self.assertEqual(got["status"], "fail")
self.assertTrue(got["fix"].endswith(" --managed"), got["fix"])
self.assertEqual(got["fix"], doctor.REINIT) # collect() adds --managed


class DroidHooks(unittest.TestCase):
Expand Down Expand Up @@ -910,19 +906,37 @@ def test_the_reinit_fix_keeps_inits_agent_and_no_hooks(self):
followed the fix got init's hooks beside the plugin's (D-HOOKS-PRESENT then warned), and an SDK agent's
host got Claude Code hooks it had declined (#464). Not over init --managed's hooks: --no-hooks there removed
them, leaving the plugin's, which the agent's user can turn off. Nor did it name init's agent, so another
coding agent's host was set up again for Claude Code."""
coding agent's host was set up again for Claude Code. And on an init --managed host only D-HOOKS-MANAGED's fix
had --managed: following any other moved the hooks from the root-owned managed file into the agent's own
settings, which the agent can edit (with the plugin on, each call was decided twice), and one without
--managed-only turned off the allowManagedHooksOnly an init with it had turned on."""
row = doctor.result("D-TAILER-CONFIG", "warn", "detail", doctor.REINIT)
plugin = ("/home/a/.claude/settings.json", install.PLUGIN)
for hooks, enabled, want in (({"no_hooks": True}, None, " --no-hooks"), ({}, plugin, " --no-hooks"),
({"agent": "codex"}, plugin, " --agent codex"), ({}, None, ""),
({"settings": install.MANAGED_SETTINGS["linux"]}, plugin, ""),
({"agent": "codex", "no_hooks": True}, None, " --agent codex --no-hooks")):
d = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, d, True)
managed = os.path.join(d, "managed-settings.json")
for hooks, enabled, only, want in (
({"no_hooks": True}, None, None, " --no-hooks"), ({}, plugin, None, " --no-hooks"),
({"agent": "codex"}, plugin, None, " --agent codex"), ({}, None, None, ""),
({"agent": "codex", "settings": "/home/a/.codex/hooks.json"}, plugin, None, " --agent codex"),
({"agent": "codex", "no_hooks": True}, None, None, " --agent codex --no-hooks"),
({"settings": managed}, plugin, False, " --managed"), ({"settings": managed}, None, False, " --managed"),
({"settings": managed}, plugin, True, " --managed --managed-only"),
({"settings": managed}, None, True, " --managed --managed-only"),
({"settings": managed}, None, "admin", " --managed")): # the admin's own setting: init leaves it
sc = {"signer": "/run/s.sock", "hooks": {"user": "a", **hooks}}
with self.subTest(hooks=hooks, plugin=enabled), \
with self.subTest(hooks=hooks, plugin=enabled, managed_only=only), \
mock.patch.dict(install.MANAGED_SETTINGS, linux=managed, darwin=managed), \
mock.patch.object(doctor.client, "system_config", return_value=sc), \
mock.patch.object(doctor, "_user", return_value=types.SimpleNamespace(pw_dir="/home/a")), \
mock.patch.object(install, "plugin_enabled", return_value=enabled), \
mock.patch.object(doctor, "v2_checks", return_value=[row]):
if only is not None: # init --managed [--managed-only] writes the file, over an admin's setting
with open(managed, "w") as f:
json.dump({"allowManagedHooksOnly": True} if only == "admin" else {}, f)
with contextlib.redirect_stdout(io.StringIO()):
install.install_managed(None, only is True, managed, install.OPT_PYTHON,
module=install.V2_HOOK, signer="/run/s.sock")
self.assertEqual(doctor.collect()[-1]["fix"], doctor.REINIT + want)


Expand Down
15 changes: 8 additions & 7 deletions tracekit/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,14 @@ def collect(config=None, issuer_key=None):
else None, no_hooks=h.get("no_hooks") is True)
# the re-init a fix names keeps init's agent, and --no-hooks where init was told it or the Claude Code plugin's
# hooks are in place (else it writes init's hooks beside the plugin's, which decides each call twice); not over
# init --managed's hooks: it would drop them for the plugin's, which the agent's user can turn off
# init --managed's hooks: it would drop them for the plugin's, which the agent's user can turn off. It keeps
# --managed, else init moves the hooks from the root-owned file into the agent's own settings, and
# --managed-only where init turned allowManagedHooksOnly on, else init turns it off
managed = settings in install.MANAGED_SETTINGS.values()
again = (f" --agent {agent_name}" if agent_name != "claude" else "") + (
" --no-hooks" if h.get("no_hooks") is True or plugin and agent_name == "claude"
and settings not in install.MANAGED_SETTINGS.values() else "")
" --no-hooks" if h.get("no_hooks") is True or plugin and agent_name == "claude" and not managed else "") + (
" --managed" + (" --managed-only" if _setting(settings, "_tracekit_allowManagedHooksOnly") is True else "")
if managed else "")
return out + [dict(r, fix=r["fix"].replace(REINIT, REINIT + again)) for r in rows]
if sc:
return out
Expand Down Expand Up @@ -1259,10 +1263,7 @@ def add(id, ok, detail, fix, bad=FAIL):
else install.V2_HELPER + ".service"), REINIT + " --harness [NAME=]PATH"))

managed = settings in install.MANAGED_SETTINGS.values() # init --managed: Claude Code's admin-managed settings
# an init without --managed-only turns off the allowManagedHooksOnly an init with it turned on
only = managed and _setting(settings, "_tracekit_allowManagedHooksOnly") is True
hook_fix = (REINIT + " --managed" + (" --managed-only" if only else "") if managed else REINIT) if prod else \
"tracekit init --dev --v2"
hook_fix = REINIT if prod else "tracekit init --dev --v2" # collect() adds --managed, as to every re-init
if managed: # the hooks are for the agent: whether other users' Claude Code reads the file doesn't count
read = agent and agent_access(agent, [[settings, "read"]]) # None: not probed (doctor isn't root)
bad = trusted_file(settings) or (f"{agent.pw_name} can't read {settings}, so its Claude Code starts without "
Expand Down
Loading