Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ and device. It backs up that app's plist and loads `io.sidepulse.bar`, which res
but stays stopped after Quit. Logs go to
`~/.local/state/sidepulse/agent-monitor/sidepulse-bar.out.log`.

To switch a Mac that runs the stock Python SidePulse, including one with no Rust toolchain, copy
the two release binaries over and run:

```sh
install -m 755 sidepulse-hook ~/.local/bin/sidepulse-hook
python3 scripts/switch-hooks.py # use the Python that has SidePulse installed
SIDEPULSE_BAR_BIN=./sidepulse-bar scripts/install-bar.sh
```

`switch-hooks.py` points the Claude, Codex and Grok hook commands at the native binary and backs up
each file first. For Codex it also re-trusts the changed hooks the way `sidepulse setup` does, using
`codex app-server`'s `hooks/list`, so Codex doesn't ask you to review them again.

For a side-by-side test that doesn't touch your real device or state:

```sh
Expand Down
11 changes: 8 additions & 3 deletions scripts/install-bar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Install sidepulse-bar as the login agent, replacing the Python SidePulse menu-bar app.
# scripts/install-bar.sh install (builds release first) and switch over
# scripts/install-bar.sh --revert stop sidepulse-bar and restore the Python app's agent
# SIDEPULSE_BAR_BIN=/path/to/sidepulse-bar scripts/install-bar.sh install a prebuilt binary (no cargo)
set -euo pipefail

LABEL=io.sidepulse.bar
Expand Down Expand Up @@ -44,10 +45,14 @@ if [[ "${1:-}" == "--revert" ]]; then
exit 0
fi

cd "$(dirname "$0")/.."
cargo build --release -p sidepulse-bar
SRC="${SIDEPULSE_BAR_BIN:-}"
if [[ -z "$SRC" ]]; then
cd "$(dirname "$0")/.."
cargo build --release -p sidepulse-bar
SRC=target/release/sidepulse-bar
fi
mkdir -p "$(dirname "$BIN")" "$STATE" "$AGENTS"
install -m 755 target/release/sidepulse-bar "$BIN"
install -m 755 "$SRC" "$BIN"

cat > "$PLIST" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
152 changes: 152 additions & 0 deletions scripts/switch-hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
"""Point SidePulse agent hooks at the native sidepulse-hook binary (run on the target Mac).

Usage: python3 scripts/switch-hooks.py (with the Python that has SidePulse installed)

Rewrites only commands that run SidePulse's Python hook_entry.py, keeping their --provider/--log
arguments, in ~/.claude/settings.json, ~/.codex/config.toml and ~/.grok/hooks/sidepulse.json.
Backs each file up as <file>.bak-sidepulse-rust first. For Codex, re-trusts the changed hooks the
same way `sidepulse setup` does: ask `codex app-server` (hooks/list) for each hook's currentHash and
store it as trusted_hash, using SidePulse's own update_codex_trusted_hashes TOML writer.
"""
import json
import os
import re
import select
import shutil
import subprocess
import sys
import time
from pathlib import Path

HOME = Path.home()
NATIVE = str(HOME / ".local/bin/sidepulse-hook")
PY_HOOK = re.compile(r"\S*python[0-9.]*\s+\S*sidepulse/hook_entry\.py(?=\s)")
CODEX_COMMAND = re.compile(r"(command\s*=\s*''')([^']*?)(''')")


def rewrite(command: str) -> str:
return PY_HOOK.sub(NATIVE, command, count=1)


def backup(path: Path) -> None:
dest = path.with_name(path.name + ".bak-sidepulse-rust")
if not dest.exists():
shutil.copy2(path, dest)


def switch_json(path: Path) -> int:
if not path.exists():
return 0
data = json.loads(path.read_text())
changed = 0
for entries in (data.get("hooks") or {}).values():
for entry in entries if isinstance(entries, list) else []:
for hook in entry.get("hooks", []) if isinstance(entry, dict) else []:
cmd = hook.get("command")
if isinstance(cmd, str) and PY_HOOK.search(cmd):
hook["command"] = rewrite(cmd)
changed += 1
if changed:
backup(path)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n")
os.replace(tmp, path)
return changed


def switch_codex(path: Path) -> tuple[int, int]:
if not path.exists():
return 0, 0
text = path.read_text()
changed = sum(1 for m in CODEX_COMMAND.finditer(text) if PY_HOOK.search(m.group(2)))
if not changed:
return 0, 0
new = CODEX_COMMAND.sub(lambda m: m.group(1) + rewrite(m.group(2)) + m.group(3), text)
backup(path)
path.write_text(new)
return changed, retrust_codex(path)


# Non-interactive shells (ssh, launchd) often lack the PATH entries where the Codex CLI and the
# `node` its npm/bun shim needs are installed.
EXTRA_PATH = [str(HOME / ".bun/bin"), "/opt/homebrew/bin", "/usr/local/bin", str(HOME / ".local/bin")]


def codex_env() -> dict:
env = dict(os.environ)
env["PATH"] = os.pathsep.join(EXTRA_PATH + [env.get("PATH", "/usr/bin:/bin")])
return env


def codex_cli():
for c in ("/Applications/ChatGPT.app/Contents/Resources/codex", "/Applications/Codex.app/Contents/Resources/codex"):
if Path(c).exists():
return c
return shutil.which("codex", path=codex_env()["PATH"])


def codex_hook_hashes(config: Path, timeout: float = 15.0) -> dict:
cli = codex_cli()
if not cli:
return {}
proc = subprocess.Popen([cli, "app-server", "--stdio"], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, text=True, bufsize=1, cwd=str(HOME),
env=codex_env())

def call(msg_id, method, params):
proc.stdin.write(json.dumps({"jsonrpc": "2.0", "id": msg_id, "method": method, "params": params}) + "\n")
proc.stdin.flush()
deadline = time.time() + timeout
while time.time() < deadline:
ready, _, _ = select.select([proc.stdout], [], [], 0.5)
if not ready:
continue
try:
msg = json.loads(proc.stdout.readline())
except json.JSONDecodeError:
continue
if msg.get("id") == msg_id:
return msg
return None

try:
call(1, "initialize", {"clientInfo": {"name": "sidepulse", "version": "0"}, "capabilities": None})
resp = call(2, "hooks/list", {"cwds": [str(HOME)]})
finally:
proc.terminate()
try:
hooks = resp["result"]["data"][0]["hooks"]
except (TypeError, KeyError, IndexError):
return {}
return {
h["key"]: h["currentHash"]
for h in hooks
if isinstance(h, dict)
and h.get("sourcePath") == str(config)
and "sidepulse-hook" in str(h.get("command"))
and h.get("key")
and h.get("currentHash")
}


def retrust_codex(path: Path) -> int:
# SidePulse's own TOML writer: from the installed package, or a source checkout given by
# SIDEPULSE_SRC (default ~/sidepulse/sidepulse/src).
sys.path.insert(0, os.environ.get("SIDEPULSE_SRC", str(HOME / "sidepulse/sidepulse/src")))
from sidepulse.install import update_codex_trusted_hashes

hashes = codex_hook_hashes(path)
if hashes:
path.write_text(update_codex_trusted_hashes(path.read_text(), hashes))
return len(hashes)


if __name__ == "__main__":
if not Path(NATIVE).exists():
raise SystemExit(f"{NATIVE} is missing; install the binary first")
claude = switch_json(HOME / ".claude/settings.json")
grok = switch_json(HOME / ".grok/hooks/sidepulse.json")
codex, trusted = switch_codex(HOME / ".codex/config.toml")
print(f"claude hooks switched: {claude}")
print(f"grok hooks switched: {grok}")
print(f"codex hooks switched: {codex}, re-trusted: {trusted}")
Loading