πŸ› fix: P-17 agent sessions fail closed

Merged
AT
1mo ago
+787-133

Slice: #45

Closes the ways the first client found an agent session reaching GitHub as the owner, short of the SSH route (P-18): a SessionStart hook that left no trace (a), variables that never reached the session's shells after compaction (b), and onboard's Verified check dying on a dropped connection (d).

Claude Code hooks other than SessionStart cannot see the variables session-start writes to CLAUDE_ENV_FILE (hooks reference: "Other hook events don't have access to this variable"), so no hook can check PROCESS_RULES_AGENT before a command. So there are three layers:

  • The lock in settings env. onboard puts the lock's variables (a GH_TOKEN GitHub refuses, no credential helper, GitHub's SSH URLs on HTTPS, other SSH refused) in the client's .claude/settings.json env. Claude Code gives them to every session before any hook runs. A session whose hook never ran or failed reaches GitHub as nobody, and so does one whose shells lost session-start's variables. session-start's file still overrides them with the agent's identity.
  • A PreToolUse guard. onboard adds a Bash PreToolUse hook running the new process-rules guard. It refuses gh and git's remote commands (push, fetch, pull, clone, ls-remote, remote, submodule) with exit 2 and a reason, unless session-start recorded that it gave this session id the agent identity. session-start now records each outcome in ~/.config/process-rules/sessions/<session id>.json, so a refusal repeats why the session was locked. A command naming neither gh nor git goes ahead without starting the CLI. When the CLI cannot run at all, every command naming either is refused.
  • No node from the hook's PATH. machine writes a launcher, ~/.config/process-rules/bin/process-rules. It names the node that ran machine by its real path (fnm's per-shell fnm_multishells/<pid> path resolves to the installed version) and runs the package manager's command for the CLI, which stays put across upgrades. The hooks run the launcher, else process-rules from PATH. doctor checks that the launcher runs with a bare PATH.

Also:

  • session-start's own failures reach the session: the hook prints the STOP line with what the CLI printed (for example /bin/sh: process-rules: command not found).
  • onboard's Verified check asks GitHub up to five times, waiting 2, 4, 6 and 8 seconds, when the request throws or GitHub does not answer 200. It prints the commit link first. On a lasting failure it names the last error instead of dying with "fetch failed".
  • onboard upgrades a client's .claude/settings.json in place: the v0.1–v0.4 SessionStart commands are swapped where they stand, and the guard and env are added or corrected. One question lists every change, and the file keeps its layout (new setMember in jsonedit).

Proof against the sandbox (athya-hq/process-rules-sandbox, cloned locally; its .claude/settings.json upgraded by this build's claudeSettings from the v0.1.0 hook). These were real headless Claude Code 2.1.312 sessions run with PATH=/usr/bin:/bin:… and no node:

  • No launcher, no CLI on PATH: the session received process-rules: STOP. process-rules session-start did not run here (/bin/sh: process-rules: command not found), …. echo $GH_TOKEN $GIT_SSH_COMMAND showed process-rules-no-agent-token and the SSH refusal, which is the lock from env. gh api user, git ls-remote git@github.com:… and git status were refused by the guard. This run also caught a bug, fixed here: the fallback first matched gh/git anywhere in the hook's JSON input, not just the command.
  • Launcher pointing at this build: PROCESS_RULES_AGENT=AthyaBot, gh api user β†’ AthyaBot, git ls-remote git@github.com:… went over HTTPS as the agent, and the guard let them through.

The sandbox change was not pushed. This repository's own .claude/settings.json should move to the new hooks only after the release, because an older CLI does not know guard and would refuse every gh/git command.

docs/decisions.md records the design and the owner's decision on #22. pnpm check passes.

πŸ€– Generated with Claude Code