Skip to content

doctor: a re-init fix on a --managed host keeps --managed (follow-up to #845) - #858

Merged
LoopGlitch26 merged 1 commit into
mainfrom
agent/doctor-managed-reinit
Nov 15, 2026
Merged

LoopGlitch26 merged 1 commit into
mainfrom
agent/doctor-managed-reinit

Conversation

@LoopGlitch26

Copy link
Copy Markdown
Member

Follow-up to #845.

What went wrong

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 ended without it, including those for D-PROCESS-BOUNDARY, D-CODE-TRUST, D-UNIT-HARDENING and D-TAILER-CONFIG. Someone who followed one of them ran init without --managed, and then:

  • init wrote its hooks into the agent's own ~/.claude/settings.json, which the agent can edit (tracekit/install.py, init_system_v2);
  • _remove_v2_hooks took them out of the recorded managed file, which is root-owned;
  • with the Tracekit plugin on, every call was decided twice;
  • after --managed-only, init without that flag also turned allowManagedHooksOnly back off.

#845 fixed the same kind of bug for --no-hooks and --agent.

The fix

collect() builds the flags that every re-init fix gets. It now adds --managed when init's hooks are in Claude Code's managed settings (install.MANAGED_SETTINGS). It also adds --managed-only when init turned allowManagedHooksOnly on: init records that with the _tracekit_allowManagedHooksOnly marker in that file. If an admin turned allowManagedHooksOnly on, there is no marker, and init leaves the setting alone, so the fix doesn't add the flag. v2_checks' hook fix no longer adds --managed itself, so D-HOOKS-MANAGED's fix names it once. The --no-hooks and --agent rules from #845 are unchanged.

Tests

tests/test_doctor_v2.py:

  • NoHooksChosen.test_the_reinit_fix_keeps_inits_agent_and_no_hooks: the managed case used to expect an empty suffix. New subtests:
    • managed, with and without the plugin: --managed, and no --no-hooks;
    • managed-only, with and without the plugin (the file written by install.install_managed(managed_only=True)): --managed --managed-only;
    • the admin's own allowManagedHooksOnly (no init marker): --managed only;
    • a codex host with its hooks file recorded: --agent codex, unchanged.
  • ManagedHooks: the hook-check fixes now end at the plain re-init, because collect() adds --managed. This pins that D-HOOKS-MANAGED doesn't name it twice. The managed-only half of test_the_fix_keeps_managed_only_and_runs_on_macos moved into the collect test above, and the macOS half is now test_the_fix_runs_on_macos.

Fail-first: I ran the new test file against origin/main's tracekit/doctor.py (138950f), copied into a scratch tree. All 5 managed subtests fail. The 6 subtests without managed settings pass, so their behaviour is unchanged. The 4 updated ManagedHooks assertions also fail on main, where v2_checks still adds its own --managed.

Runs on this branch:

  • tests/test_doctor_v2.py and tests/test_docs.py: 140 passed, 220 subtests passed.
  • make lint: clean.
  • E16 (eval/e16_doctor.py): 57/57 flagged, gate PASS.
  • The worktree gates (make test-ts lint build test):
    • test-ts: 155 pass, 0 fail;
    • lint and build: pass;
    • make test: a session restart stopped it at 93%, with 5011 passed, 445 skipped and 0 failed. I re-ran the remaining files, tests/test_view_ui.py through tests/test_yamlmini.py: 420 passed.

Before and after, run as a user would

I ran real doctor.collect() on scratch system-mode hosts, once against origin/main and once against this branch, with the import path checked each time. Only client.json and the agent user are mocked; the plugin check, v2_checks and the fix rewrite run as shipped. In the output below, <REINIT> stands for from a root-owned clone of the release (https://github.com/Cygnux-Labs/Tracekit/wiki/Deploy#laptop-system-mode), sudo /usr/bin/python3 -m tracekit init --v2 --user <agent-user> --experimental-macos.

Host (a), init --managed with the plugin on. Before:

FAIL  <scratch>/etc/data: No such file or directory  (D-PROCESS-BOUNDARY)
      fix: run the signer as its own user: <REINIT>
FAIL  /opt/tracekit is missing  (D-CODE-TRUST)
      fix: re-run init to reinstall the venv: <REINIT>
WARN  no service file: /Library/LaunchDaemons/dev.tracekit.signer.plist: No such file or directory  (D-UNIT-HARDENING)
      fix: <REINIT>
FAIL  /Library/Application Support/ClaudeCode/managed-settings.json: No such file or directory  (D-HOOKS-MANAGED)
      fix: sudo chown 0:0 and sudo chmod 644 /Library/Application Support/ClaudeCode/managed-settings.json, and sudo chmod go-w the directories above it; or <REINIT> --managed
WARN  <scratch>/etc/client-a.json names no tailer: each Claude Code session records a tailer_lost gap instead of its transcript  (D-TAILER-CONFIG)
      fix: <REINIT>
re-init fixes: 4 distinct, in 5 rows; what follows REINIT: ['', ' --managed']

After:

FAIL  <scratch>/etc/data: No such file or directory  (D-PROCESS-BOUNDARY)
      fix: run the signer as its own user: <REINIT> --managed
FAIL  /opt/tracekit is missing  (D-CODE-TRUST)
      fix: re-run init to reinstall the venv: <REINIT> --managed
WARN  no service file: /Library/LaunchDaemons/dev.tracekit.signer.plist: No such file or directory  (D-UNIT-HARDENING)
      fix: <REINIT> --managed
FAIL  /Library/Application Support/ClaudeCode/managed-settings.json: No such file or directory  (D-HOOKS-MANAGED)
      fix: sudo chown 0:0 and sudo chmod 644 /Library/Application Support/ClaudeCode/managed-settings.json, and sudo chmod go-w the directories above it; or <REINIT> --managed
WARN  <scratch>/etc/client-a.json names no tailer: each Claude Code session records a tailer_lost gap instead of its transcript  (D-TAILER-CONFIG)
      fix: <REINIT> --managed
re-init fixes: 4 distinct, in 5 rows; what follows REINIT: [' --managed']

Host (d), init --managed --managed-only with the plugin on. Its managed file was written by install.install_managed(managed_only=True) and holds the marker. Before, the suffixes were ['', ' --managed --managed-only'], and only D-HOOKS-MANAGED and D-HOOKS-VENV had both flags. After, all six re-init rows end in --managed --managed-only.

The other hosts are unchanged: (b) init --no-hooks with the plugin on gives [' --no-hooks'], and (c) init --agent codex --no-hooks gives [' --agent codex --no-hooks']. No fix names --managed twice.

Not checked: a real root init --managed on Linux, then following the new fix. The scratch run checks the advice doctor prints, not what init does with it.

#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>
@LoopGlitch26
LoopGlitch26 enabled auto-merge (squash) November 15, 2026 01:57
@LoopGlitch26
LoopGlitch26 merged commit f32c2fe into main Nov 15, 2026
34 checks passed
@LoopGlitch26
LoopGlitch26 deleted the agent/doctor-managed-reinit branch November 15, 2026 04:30
LoopGlitch26 added a commit that referenced this pull request Nov 15, 2026
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 added a commit that referenced this pull request Nov 15, 2026
* doctor: a re-init fix on a --managed host keeps --managed (follow-up to #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>

* doctor: re-init advice keeps init's own flags (follow-up to #858)

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>

* doctor: re-init fixes run from the clone; init takes --project DIR (review 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>

* doctor: keep --harness as given only where root alone can repoint it (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>

* cli: --project [DIR] resolves against os.getcwd() as init runs, on every 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: HarnessSpec skips on Windows, as this file's other system-mode 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>

---------

Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant