π fix: P-17 agent sessions fail closed
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.onboardputs the lock's variables (aGH_TOKENGitHub refuses, no credential helper, GitHub's SSH URLs on HTTPS, other SSH refused) in the client's.claude/settings.jsonenv. 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.
onboardadds a Bash PreToolUse hook running the newprocess-rules guard. It refusesghand 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 neitherghnorgitgoes 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.
machinewrites a launcher,~/.config/process-rules/bin/process-rules. It names the node that ranmachineby its real path (fnm's per-shellfnm_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, elseprocess-rulesfrom PATH.doctorchecks 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".
onboardupgrades a client's.claude/settings.jsonin place: the v0.1βv0.4 SessionStart commands are swapped where they stand, and the guard andenvare added or corrected. One question lists every change, and the file keeps its layout (newsetMemberinjsonedit).
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_COMMANDshowedprocess-rules-no-agent-tokenand the SSH refusal, which is the lock fromenv.gh api user,git ls-remote git@github.com:β¦andgit statuswere refused by the guard. This run also caught a bug, fixed here: the fallback first matchedgh/gitanywhere 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