Repository navigation
doctor: re-init advice keeps init's own flags (follow-up to #858) - #860
Merged
Merged
Conversation
#845) On a host set up with init --managed, only the hook checks' fixes (D-HOOKS-MANAGED, D-HOOKS-PRESENT, D-HOOKS-VENV) named --managed. The other re-init fixes, D-PROCESS-BOUNDARY, D-CODE-TRUST, D-UNIT-HARDENING, D-TAILER-CONFIG among them, ended without it, so a person who followed one ran init without --managed: init wrote its hooks into the agent's own ~/.claude/settings.json, which the agent can edit, and took them out of the root-owned managed file. With the Tracekit plugin on, each call was then decided twice. Without --managed-only, init also turned off the allowManagedHooksOnly an init with it had turned on. collect() now adds --managed to every re-init fix when init's hooks are in Claude Code's managed settings, and --managed-only when init recorded it there (its _tracekit_allowManagedHooksOnly marker; an admin's own allowManagedHooksOnly has no marker, and init leaves it). The hook checks' fix no longer adds its own, so it names --managed once. --no-hooks and --agent stay as #845 left them. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
A fix that re-ran system init gave back init's agent, --no-hooks and --managed only. Following one on a host set up with --policy, --harness, --approver, --egress and the like rewrote signer.yaml with init's defaults (its own policy, no harness binding), and doctor didn't say. - init --v2 records the rest of its flags in client.json (hooks.init_flags, and the directory of a --project init); v1 init records its own at the top level. Doctor reads them from that root-owned file only and gives them back shell-quoted. A host whose client.json predates the record gets a fix that says doctor can't tell init's flags, instead of a re-init that drops them. - v1: V1_REINIT never named --managed; doctor now gives v1 init's flags too, and --managed where the managed settings hold v1 hooks on an older host. - D-HOOKS-PRESENT no longer fails "every call is decided twice" on an init --managed host with allowManagedHooksOnly on when only the agent's own settings enable the plugin (Claude Code doesn't run it then). On a managed host its fix no longer names tracekit uninstall, which never takes managed hooks out. - D-SYSTEM-CONFIG keeps --managed when the managed settings hold init's v2 hooks. - --managed-only is given again only while init's marker is there and allowManagedHooksOnly is still on, init's own rule. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…eview of #860) - init --project [DIR]: a re-init fix for a --project host named the directory and had the person run `sudo python3 -m tracekit init` there, in the agent's own repo, where -m imports a planted tracekit/ before the release's, as root. init now takes the directory (no DIR keeps the working directory), init records it, and the fix runs from the root-owned clone like every other re-init. - --harness is recorded as given (absolute, symlinks kept), not as its resolved real path, which a package upgrade removes or leaves pointing at the old binary. The signer still binds the resolved program. - --managed-only is recorded (v1 and v2). When allowManagedHooksOnly is off now (an admin, or MDM replacing the managed file), the fix keeps it and says to drop it only if it was turned off on purpose; a fix's own --managed-only isn't given twice. - --i-understand-agent-is-privileged, --no-witness and --public-witness aren't recorded or replayed. - migrate --system keeps v1's init_flags. - install no longer imports doctor's private helpers: install.reinit_flags and install.setting are shared. doctor --config with no client.json says there is none. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…(re-review of #860) - harness_spec kept the given path, links and all, but trusted_file checks only the real path's chain: a link in a directory the agent's user can write could be repointed before a re-init, which then bound any root-owned program. The given path is now kept only when every directory its lookup reads, its links' targets included, is root-owned and not group- or world-writable; else init records the real path. - The no-record note names --project DIR: a bare --project run from the release's clone puts the hooks in the clone. - uninstall takes --project [DIR] as init does, and dev init's plugin check reads DIR's .claude (and doesn't take a named project's .claude for a user config root, as it didn't the working directory's). Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
Conflicts: #858's squash (f32c2fe) against this branch's later edits of the same hunks, kept as this branch has them (a superset of #858's), and main's jcode and Kiro CLI changes: cli.py keeps both --project [DIR] and jcode's user-config refusal; docs/doctor.md keeps this branch's D-HOOKS-PRESENT and D-HOOKS-MANAGED rows and main's D-HOOKS-EVENT and D-HOOKS-ORDER rows. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
LoopGlitch26
enabled auto-merge (squash)
November 15, 2026 06:13
LoopGlitch26
disabled auto-merge
November 15, 2026 07:24
Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
…ery OS
CI's py3.9 Windows job failed five init --dev --v2 --project tests: the
hooks went into the checkout, not the test's working directory. --project
with no DIR went through os.path.abspath(".") at run time, and on Windows
abspath asks the OS for the working directory (nt._getfullpathname)
instead of calling os.getcwd(), which those tests patch to set it and
which the code read before --project took a DIR. DIR is now joined to
os.getcwd() and normalized, as POSIX abspath does. A test stands in an
abspath that ignores a patched os.getcwd, as Windows' does, and checks a
parser built before a chdir. The two CliProject tests compare against
os.path.abspath of the input (C:\srv\my repo on Windows).
Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
… tests do CI's py3.13 windows-latest job failed it: resolve_harness couldn't take the temp script's "#!/bin/sh" interpreter, as Python 3.13's ntpath.isabs no longer counts a drive-less rooted path. Harness binding is system mode's, which _require_system refuses on Windows before any harness code runs. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #858, which is merged now; the base is
main. #858 was squash-merged, so I mergedmaininto this branch (no rebase, no force-push). The conflicts were #858's own hunks, which this branch extends and which I kept as the branch has them, andmain's jcode and Kiro CLI changes, which I kept:cli.pyhas both--project [DIR]and jcode's user-config refusal, anddocs/doctor.mdhas this branch's D-HOOKS-PRESENT and D-HOOKS-MANAGED rows next to main's D-HOOKS-EVENT and D-HOOKS-ORDER rows.Fixes the re-init advice gaps found in the review of #858. The second and third commits fix what the independent review and re-review of this PR found.
What was wrong
--agent,--no-hooksand--managed, and nothing else. On a host set up with--policy X --harness …, following D-CODE-TRUST (or any re-init fix) rewrote signer.yaml with init's default policy and noharness_binding. Doctor didn't say.V1_REINITnever named--managed, so following it on a v1--managedhost wrote user hooks beside the managed ones.init --managedhost withallowManagedHooksOnlyon, D-HOOKS-PRESENT failed "every call is decided twice" when only the agent's own settings enabled the plugin. Claude Code doesn't run that plugin then, and the agent could cause the FAIL. The fix it gave,tracekit uninstall, never removes managed hooks.--managed-onlycame only from init's marker in the managed settings file. If an MDM policy replaced that file, the marker went with it, and the fix re-inited without--managed-only, which is weaker.The review of this PR found these in the first commit:
--projectinit's fix told the person to runsudo python3 -m tracekit initin the project, which is the agent's own directory.-mputs the working directory first onsys.path, so a plantedtracekit/would run as root.--harnesswas recorded as its resolved real path, which a package upgrade removes or leaves pointing at the old binary.--i-understand-agent-is-privileged,--no-witnessand--public-witness.migrate --systemerased the v1 record.install.pyimported doctor's private helpers.doctor --configon a host with no client.json said client.json "doesn't record" the flags.The re-review found these in the second commit:
--harnesskept the given path even when the link sat in a directory the agent's user can write. That user could repoint the link before a re-init, and the re-init would bind any root-owned program they chose.trusted_filechecks only the real path's chain.--project. Run from the clone, that puts the hooks in the clone.uninstall --projecttook only the working directory. Dev init's plugin check read the working directory's.claude, not DIR's.What changes
init --v2records its flags in/etc/tracekit/client.jsonashooks.init_flags:--managed,--managed-only,--approver(always, the resolved one),--policy,--project DIR,--harness NAME=PATH,--egress,--no-thinking-summariesand--no-service.--harnessis recorded as given (absolute, links kept) only when root alone can repoint it. That means every directory its lookup reads, including the ones its links' targets pass through, is root-owned and not group- or world-writable. Otherwise init records the real path. A package upgrade that moves a root-owned link's target is followed; a link the agent's user could repoint is not. The signer still binds the resolved program, a script's real path plus its interpreter.--managed,--managed-only,--no-hooks,--no-service,--proxy --experimental [--proxy-port N],--fail-closed,--experimental-macos,--project DIRand--agent.--i-understand-agent-is-privileged(init's refusal names it, for a user that has gained a privilege since),--no-witness(the no-witness file already keeps it) and--public-witness(replaying it can only make init fail). Witnesses, the checkpoint interval, v1 harnesses and the external signer aren't recorded either, because the signer config keeps them across inits.migrate --systemkeeps the v1 record.init --project [DIR]anduninstall --project [DIR]. Without DIR it's the working directory, as before. A re-init fix gives--project DIRand runs from the root-owned clone, like every re-init. It never tells the person to run init from a directory the agent can write. Dev init's plugin check reads DIR's.claude. As with the working directory's, it doesn't take that project's.claudefor a user config root, so a repo can't vouch for a plugin key with planted install state.collect()already reads client.json, and gives them back shell-quoted. The shared code isinstall.reinit_flagsandinstall.setting; install no longer imports doctor's private helpers.--managed-onlystays when init was run with it, from the record or, on an older host, from the marker. IfallowManagedHooksOnlyis off now, the fix keeps it and adds: "init turned on allowManagedHooksOnly and it is off now; drop --managed-only only if you turned it off on purpose". The safe default wins. A fix that names--managed-onlyitself (D-HOOKS-PRESENT's) doesn't give it twice.doctor --configwith no client.json. The fix gives the flags doctor can see, then says plainly: "add each flag this host's init was run with that this lacks (…, --project DIR, …): client.json doesn't record them / records them, and it can't be read / there's no client.json here to record them, so doctor can't tell them, and init without them goes back to its defaults".--no-hooksgets the rebuilt flags, without--no-hooks.--harness, not a--harness [NAME=]PATHplaceholder.allowManagedHooksOnlyon counts a plugin as running only when the managed settings enable it. Its fix is "take X out of enabledPlugins in " or "/plugin disable X in the agent's Claude Code, or --managed-only, which stops every user's own Claude Code hooks and plugins on this machine".--managed(and--managed-onlyby the marker) when the managed settings hold Tracekit's v2 hooks. v1's D-SYSTEM-CONFIG does the same for v1 hooks.docs/doctor.md: a paragraph on re-init flags, plus the D-HOOKS-PRESENT and D-HOOKS-MANAGED rows and the v1 fix.Fail-first
I checked this on scratch copies of each head's code (
git archive) with the new tests laid over them. Nogit stash.On #858's head
3fe1871a7a(first commit, 11 failures):3fe1871a7aKeyError: 'init_flags'KeyError;… init --user <agent-user>with no--managed('fail', "…decided twice…")whereokis right--managedOn the first commit
9d0b2ee39b(review fixes, 12 failures):9d0b2ee39b--project DIRinit --project /srv/my repoexits 2; the record is--projectwith no directory--harnessas givenclaude=/private/var/…(the real path), not the path given--managed-onlyrecorded--managed --managed-only --managed --managed-only; D-HOOKS-PRESENT's fix gives it twice--i-understand-agent-is-privileged,--no-witness,--public-witnessmigrate --systemNone != ['--managed', '--proxy', '--experimental']--configwordingOn the second commit
f34f40d24c(re-review fixes, 8 failures):f34f40d24c--harnesslink in a writable directoryclaude=<the link>; once it's repointed to/bin/sh,resolve_harnessbinds/bin/sh(noscript)--project DIRin the note--project(v2 and v1)uninstall --project DIRplugin_enabledtakes no projectMutation check
Each mutant undoes one fix in a scratch copy of the branch. The second commit's 16 were all killed:
--projectuses the cwd again; v2 or v1 record has--projectwithout its directoryCliProject, bothtest_init_records_the_flags_doctor_gives_again--harnessrecorded as its real path; M10: recorded as the interpreter--managed-only; the fix drops a recorded one silently; a fix's own--managed-onlygiven twicetest_managed_only_stays_and_says_so_when_the_setting_is_off_now,test_a_fixs_own_managed_only_is_not_given_twice--i-understand-agent-is-privilegedmigrate --systemdrops the recordtest_migrate_keeps_a_policy_path_and_inits_record--no-witness/--public-witness; M14: D-HARNESS-HELPER's placeholder back;--configsays "doesn't record them"test_the_harness_helpers_fix_is_the_re_init_with_inits_harness,test_without_a_record_…The third commit's 10 were all killed:
_fixedchecks no directoryHarnessSpec(a link in a temp dir is recorded by its real path and survives a repoint; a root-owned link chain such as macOS's/etc/hostsor merged-usr/bin/shis kept)--projecttest_without_a_record_…, v1Doctortestuninstall --projectnot made absolute; uninstall (Claude Code or--agent) uses the cwd; the dev plugin check reads the cwd; a named project's.claudetaken for a user config rootCliProjecttestsBefore / after, as a person sees it
This ran the real
tracekit initandtracekit doctorCLIs. Only the system was mocked: root, the OS users, systemctl, the venv build, and root-owned writes, which went into a scratch tree.<REINIT>stands forfrom a root-owned clone of the release (…), sudo /usr/bin/python3 -m tracekit init --v2 --user <agent-user> --experimental-macos.init --v2 --user tkagent --approver tkadmin --policy "$S/etc/corp policy.yaml" --harness claude=$S/bin/claude --no-thinking-summaries --i-understand-agent-is-privileged, where$S/bin/claudeis a symlink to/bin/sh.$Sis a scratch directory this user owns. D-CODE-TRUST:nowis the real path because a user other than root can repoint$S/bin/claude. A link in root-only directories, such as/usr/local/bin/claudeon a root-owned Homebrew prefix, is kept as given.init --v2 … --project "$S/agent repo":init --managed --managed-only, then an admin turnsallowManagedHooksOnlyoff, or an MDM policy replaces the file:The same host while the setting is on, with the agent enabling the plugin in its own settings (D-HOOKS-PRESENT):
A host from an older init (no
init_flags):client.json can't be read, and the managed file holds init's v2 hooks (D-SYSTEM-CONFIG):
v1
init --managedhost (D-V1-RUNTIME):Limits
--user <agent-user>is still a placeholder, as before. client.json does record the user, so filling it in would be a small follow-up.--no-hooksthat doctor adds when the agent's own settings enable the plugin (from System mode: hints, messages and help text that don't match what init does #464) still reads the agent's settings. I didn't change that here.install.status()still imports doctor's_dev_harnesses. That import was already there before this PR.--harnesscheck's walk through a root-owned link whose target passes through a writable directory, because building one needs root. The unit tests cover a link in a writable directory, a link to a root-only directory placed in a writable one, and a root-owned link chain that stays as given./proctests skip here, and CI's Linux run is the merge gate.Windows CI fix (
ba8f577362)3dc73c9382failed CI'spy3.9 on windows-latestjob with 7 failures. Fiveinit --dev --v2 --projecttests (test_hook_v2.pyInitV2 ×3,test_harness_hooks_v2.pyInstaller and CodexReasoningSummary) found the hooks in the checkout, not in their temp working directory. TwoCliProjectassertions compared'C:\srv\my repo'with'/srv/my repo'.The cause isn't a default fixed when the parser is built. A parser built in one directory and run in another resolves to the second, on that head too. The cause is that
--projectwith no DIR went throughos.path.abspath("."). On POSIX that callsos.getcwd(), but on Windows it asks the OS (nt._getfullpathname). Those tests set the directory by patchingos.getcwd, which is also what the code read before--projecttook a DIR.DIR is now joined to
os.getcwd()and normalized, which is what POSIXabspathdoes. The newCliProject::test_the_default_is_the_working_directory_as_init_runsstands in anabspaththat ignores a patchedos.getcwd, as Windows' does, and also checks a parser built before a chdir. It fails on3dc73c9382for bothinitanduninstall. The twoCliProjecttests now compare againstos.path.abspathof the input, which is still an exact comparison.I reproduced CI's failure locally by running the five failing tests under that Windows-style
abspath(as_windows_abspath.py): 20 failures on3dc73c9382, with the hooks written into the checkout, and 10 of 10 passing after the fix. I also mergedmain(#840) first; it merged cleanly.Tests
tests/test_doctor_v2.py,tests/test_system_install.py,tests/test_install_files.py,tests/test_system_v2.py,tests/test_docs.pyandtests/test_hook_v2.py(whose plugin checksplugin_enabledshares): 376 passed, 7 skipped on the merged head.make lintis clean. Full gate in the worktree (make test-ts lint build test): onf4883767ff, exit 0, 5278 passed, 446 skipped, 10 xfailed; on the merge withmain(3dc73c9382), exit 0, 5324 passed, 447 skipped, 10 xfailed; on the Windows fix (ba8f577362), exit 0, 5335 passed, 447 skipped, 10 xfailed. On the Windows fix, the affected files,test_harness_hooks_v2.pyandtest_grants.py(from #840) included, gave 551 passed and 10 skipped. E16 onf34f40d24c: 57/57 flagged, gate PASS.