From 66dc9949edbc80cc0b59b08bd61f53fb8feac609 Mon Sep 17 00:00:00 2001 From: dtzp555 Date: Thu, 25 Jun 2026 11:13:18 +1000 Subject: [PATCH] fix(ocp): macOS restart uses bootout+bootstrap so plist env is re-read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The macOS restart path used `launchctl kickstart -k gui/$uid/dev.ocp.proxy`, which only re-execs the process and reuses launchd's CACHED environment — so a plist EnvironmentVariables edit (CLAUDE_BIND, CLAUDE_CODE_OAUTH_TOKEN, etc.) was silently ignored until a full unload/reload. This is the documented pit-index footgun. `ocp restart` (macOS) now does a full `launchctl bootout` + `bootstrap` of the agent via a new `_launchd_reload` helper, which re-reads the plist EnvironmentVariables so env changes take effect. Success is keyed on the bootstrap (the env-reloading load), not the bootout (which may legitimately fail if the agent is not currently loaded). A missing plist returns failure so the `elif` chain falls through to the legacy label and then to the Linux `systemctl --user restart` path unchanged (systemctl already re-reads its EnvironmentFile). Updates `ocp restart` help text and adds a README Troubleshooting subsection ("Env var change doesn't take effect after restart") with the manual bootout+bootstrap commands and a ps-based verification one-liner. Verified `_launchd_reload` with a stubbed launchctl: missing plist → rc=1 (falls through, no launchctl call); bootout-fail + bootstrap-ok → rc=0; bootstrap-fail → rc=1; call order is bootout then bootstrap. `bash -n ocp` clean; npm test 247/0 (server.mjs untouched). ALIGNMENT.md Rule 2 justification: this changes ONLY the `ocp` CLI wrapper's local service-restart mechanism (launchctl invocation). It does not touch server.mjs, the wire path, any endpoint/header, or any API token — so there is no cli.js function to cite. Co-Authored-By: Claude Opus 4.8 (1M context) --- README.md | 23 +++++++++++++++++++++++ ocp | 27 ++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c90abe5..d3a4c8b 100644 --- a/README.md +++ b/README.md @@ -844,6 +844,29 @@ ocp restart openclaw gateway restart ``` +### Env var change (e.g. `CLAUDE_BIND`, `CLAUDE_CODE_OAUTH_TOKEN`) doesn't take effect after restart + +On **macOS**, `ocp restart` does a full `launchctl bootout` + `bootstrap` of the agent, which **re-reads the plist `EnvironmentVariables`** — so an env change you made (in `~/Library/LaunchAgents/dev.ocp.proxy.plist`) actually takes effect: + +```bash +ocp restart +``` + +This is deliberate: the older `launchctl kickstart -k` only re-execs the process and **reuses launchd's cached environment**, so plist env edits would be silently ignored. If you ever restart the agent by hand, use bootout+bootstrap, not `kickstart -k`: + +```bash +launchctl bootout gui/$(id -u)/dev.ocp.proxy 2>/dev/null +launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/dev.ocp.proxy.plist +``` + +Verify the new value reached the running process: + +```bash +ps -E -p "$(launchctl print gui/$(id -u)/dev.ocp.proxy 2>/dev/null | awk '/pid =/{print $3}')" | tr ' ' '\n' | grep CLAUDE_ +``` + +On **Linux**, `systemctl --user restart` already re-reads the unit's `EnvironmentFile`, so no special handling is needed. + ### Usage shows "unknown" Usually caused by an expired Claude CLI session. Fix: diff --git a/ocp b/ocp index dbf4f48..a30e889 100755 --- a/ocp +++ b/ocp @@ -573,21 +573,42 @@ Usage: ocp restart Restart the Claude proxy service ocp restart gateway Restart the OpenClaw gateway (briefly disconnects all Telegram/Discord bots) + +Note (macOS): restart does a full launchctl bootout + bootstrap, NOT +`kickstart -k`. bootout+bootstrap re-reads the plist's EnvironmentVariables, +so an env change you made (e.g. CLAUDE_BIND, CLAUDE_CODE_OAUTH_TOKEN) actually +takes effect. `kickstart -k` only re-execs the process and reuses launchd's +cached env, so env edits would be silently ignored. (Linux systemctl already +re-reads its EnvironmentFile on restart.) EOF } +# macOS only: reload a launchd agent via bootout + bootstrap so plist +# EnvironmentVariables are re-read (kickstart -k would reuse the cached env). +# Args: