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
81 changes: 69 additions & 12 deletions .claude-config/bin/README.md

Large diffs are not rendered by default.

355 changes: 317 additions & 38 deletions .claude-config/bin/await-approval

Large diffs are not rendered by default.

216 changes: 200 additions & 16 deletions .claude-config/bin/mcp-http-call.py

Large diffs are not rendered by default.

442 changes: 440 additions & 2 deletions .claude-config/bin/tests/await-approval-test.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions .claude-config/bin/tests/guard-selftest-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
# command in GST_FLIP. One command per probe site that read through the pipe:
# a blocked rtk form, an allowed rtk form, and the plain project push (a few
# heredoc probes send that command too; they are counted either way).
# Exit 0 = all pass; 1 = a case fails; 77 = did not run (no jq: the suite proved nothing).
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
HOOKS="$(cd "$HERE/../../hooks" && pwd)"
BIN="$(cd "$HERE/.." && pwd)"
if ! command -v jq >/dev/null 2>&1; then
echo "SKIP: guard-selftest-test (jq not installed; guard-selftest's rtk probes build their JSON with it)"
exit 0
echo "guard-selftest-test: did not run: no jq (guard-selftest's rtk probes build their JSON with it)"
exit 77
fi
T="$(mktemp -d "${TMPDIR:-/tmp}/guard-selftest-test.XXXXXX")"; trap 'rm -rf "$T"' EXIT
PASS=0; FAIL=0
Expand Down
278 changes: 275 additions & 3 deletions .claude-config/bin/tests/mcp-http-call-test.sh

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions .claude-config/bin/tests/post-compact-resume-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,21 @@ run_hook "$(compact_payload "$T/t1.jsonl")"
&& ok "with no snapshot the no-snapshot notice prints and the block follows it" || bad "no-snapshot case" "$out"
printf '# Pre-compact snapshot\n' > "$SNAP"

# --- 5b. the resume text tells the agent to verify a DO NOT / HOLD line before trusting it ---
# A snapshot written before a compaction carries the previous session's DO NOT and HOLD
# lines, and an uncited one can be a guess that the next session would treat as a ruling.
# The text names the check (the cited operator message or doctrine, else the newest
# operator direction) and keeps the exceptions (standing instructions, real holds).
printf '# Pre-compact snapshot\n\nDO NOT touch the config.\n' > "$SNAP"
run_hook "$(compact_payload "$T/t1.jsonl")"
{ [ "$rc" -eq 0 ] && has 'verify a DO NOT' && has 'HOLD line against the operator message or doctrine it cites' \
&& has "the operator's newest direction" && has 'standing instructions and real operator holds'; } \
&& ok "with a snapshot the resume text asks for a DO NOT / HOLD line to be verified, and keeps standing instructions and real holds" \
|| bad "DO NOT / HOLD verification sentence missing" "$out"
run_hook "{\"source\":\"resume\",\"session_id\":\"s\",\"transcript_path\":\"$T/t1.jsonl\"}"
{ [ -z "$out" ] && [ "$rc" -eq 0 ]; } && ok "source=resume stays silent: no resume text, no guidance" || bad "resume source printed" "$out"
printf '# Pre-compact snapshot\n' > "$SNAP"

# --- 6. the cap, name hygiene, garbage lines --------------------------------------
names="$(python3 -I -c 'print(",".join("mcp__bulk__tool_%02d" % i for i in range(45)))')"
tx "$T/t6.jsonl" "boundary 2 $names"
Expand Down
161 changes: 161 additions & 0 deletions .claude-config/bin/tests/remote-claude-targets-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env bash
# remote-claude-targets-test.sh: the tmux targets the remote-session scripts pass are the
# right form for the command (remote-sessions/lib-remote-claude.sh, new-remote-claude.sh).
#
# Why: tmux resolves targets differently by command. A bare exact-session target "=NAME"
# is fine for has-session and kill-session, but send-keys with it fails ("can't find pane")
# on tmux 3.5a and 3.6, which dropped the launch line and left a bare shell that never became
# a Claude session, and display-message with it prints an empty value. Pane-level commands
# need "=NAME:" (exact session, current window). Both forms stay exact, so a name that is a
# prefix of a sibling's (Dev and Dev2) never reaches the sibling.
#
# Offline: `tmux` and `sleep` are shell functions here, so no tmux binary runs and nothing
# waits; each call's argv is logged and pinned. The real-server proof, with a prefix-sharing
# sibling, is remote-claude-tmux-test.sh.
#
# Pinned: for rc_launch (create and resume), rc_sweep, rc_archive and the already-running
# branch of new-remote-claude.sh, the exact argv of every tmux call; session-level commands
# take "=NAME", pane-level ones "=NAME:", and no -t target is ever bare.
#
# Every path (HOME, the registry, the archive, the env file) is a fixture under a temp dir.
# Usage: remote-claude-targets-test.sh (exit 0 all pass, 1 otherwise)
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd -P)"
RS="$HERE/../../remote-sessions"
[ -f "$RS/lib-remote-claude.sh" ] && [ -f "$RS/new-remote-claude.sh" ] || { echo "FAIL: remote-sessions scripts missing"; exit 1; }

T="$(mktemp -d)"; export T; trap 'rm -rf "$T"' EXIT
pass=0; fail=0
ok() { pass=$((pass+1)); echo "PASS: $1"; }
bad() { fail=$((fail+1)); echo "FAIL: $1"; [ $# -gt 1 ] && printf ' %s\n' "$2"; }

echo "remote-claude-targets-test:"
mkdir -p "$T/home/.claude/projects/p" "$T/work"
unset TMUX
export HOME="$T/home" TMUX_TMPDIR="$T/tmuxtmp" RC_ENVFILE="$T/env.sh" RC_REGISTRY="$T/registry.tsv" \
RC_ARCHIVE="$T/archive.tsv" RC_DEFAULT_WORKDIR="$T/work" TMUX_LOG="$T/tmux.log"
: > "$RC_ENVFILE"

# The stub: logs one line per call (arguments joined by a unit separator so a space inside an
# argument stays visible), then answers the way a running tmux would. TMUX_HAS decides
# has-session (0 running, 1 not); TMUX_ATTACHED is what session_attached returns.
tmux() {
( IFS=$'\037'; printf '%s\n' "$*" >> "$TMUX_LOG" )
case "$1" in
has-session) return "${TMUX_HAS:-1}" ;;
new-session) [ "${TMUX_FAIL:-}" != create ]; return ;;
list-panes) [ "${TMUX_FAIL:-}" != pane ]; return ;;
send-keys)
if [ "$#" -eq 5 ]; then [ "${TMUX_FAIL:-}" != launch ]
else [ "${TMUX_FAIL:-}" != enter ]; fi
return ;;
display-message) case "$*" in *session_attached*) printf '%s\n' "${TMUX_ATTACHED:-0}" ;; *) printf '%s\n' "$T/work" ;; esac ;;
esac
return 0
}
export -f tmux
sleep() { :; } # rc_launch waits 12 s for a real claude to start; nothing to wait for here
export -f sleep
# shellcheck source=/dev/null
. "$RS/lib-remote-claude.sh"
# the lib puts the system dirs first on PATH; a function still beats any binary
[ "$(type -t tmux)" = function ] || { echo "FAIL: the tmux stub is not in force; refusing to go on"; exit 1; }

US=$'\037'
SID="11111111-2222-3333-4444-555555555555"
calls() { cat "$TMUX_LOG" 2>/dev/null; }
reset() { : > "$TMUX_LOG"; TMUX_HAS=1; TMUX_ATTACHED=0; }
line_is() { # <label> <expected argv, joined by the separator>
if calls | grep -qxF -- "$2"; then ok "$1"; else bad "$1 (no call '${2//$US/ }')" "$(calls | tr "$US" ' ')"; fi
}
no_bare() { # <label>: every -t target is an exact one, and pane-level commands end with a colon
local label="$1" badline=""
while IFS= read -r l; do
IFS="$US" read -r -a a <<< "$l"
local cmd="${a[0]}" i tgt=""
for i in "${!a[@]}"; do [ "${a[$i]}" = "-t" ] && tgt="${a[$((i+1))]}"; done
[ -n "$tgt" ] || continue
case "$tgt" in =*) ;; *) badline="$l (not an exact target)" ;; esac
case "$cmd" in
has-session|kill-session) case "$tgt" in *:) badline="$l (session-level target has a colon)" ;; esac ;;
send-keys|display-message|list-panes) case "$tgt" in *:) ;; *) badline="$l (pane-level target has no colon)" ;; esac ;;
esac
done < <(calls)
[ -z "$badline" ] && ok "$label" || bad "$label" "${badline//$US/ }"
}

# --- rc_launch, create ---------------------------------------------------------------------
reset
out="$(rc_launch Dev "$T/work" "$SID" 2>&1)"
line_is "launch: has-session uses =NAME" "has-session${US}-t${US}=Dev"
line_is "launch: new-session names the session and its dir" "new-session${US}-d${US}-s${US}Dev${US}-c${US}$T/work"
line_is "launch: the readiness wait asks list-panes for =NAME:" "list-panes${US}-t${US}=Dev:"
LAUNCH="set -a; source '$RC_ENVFILE' 2>/dev/null; set +a; claude --remote-control 'Dev' --session-id '$SID'"
line_is "launch: send-keys delivers the launch line to =NAME:" "send-keys${US}-t${US}=Dev:${US}$LAUNCH${US}Enter"
line_is "launch: send-keys sends the first-run Enter to =NAME:" "send-keys${US}-t${US}=Dev:${US}Enter"
line_is "launch: display-message reads the pane dir from =NAME:" "display-message${US}-p${US}-t${US}=Dev:${US}-F${US}#{pane_current_path}"
no_bare "launch: no -t target is bare, pane-level ones end in a colon, session-level ones do not"
case "$out" in *"OK name=Dev cwd=$T/work session_id=$SID mode=create"*) ok "launch: the status line reports the pane dir and mode=create" ;; *) bad "launch: status line" "$out" ;; esac

# --- rc_launch, failed tmux steps -----------------------------------------------------------
for stage in create pane launch enter; do
reset
out="$(TMUX_FAIL="$stage" rc_launch Dev "$T/work" "$SID" 2>&1)"; rc=$?
if [ "$rc" -ne 0 ] && [[ "$out" == *"ERR_TMUX name=Dev"* && "$out" != *"OK name=Dev"* ]]; then
ok "launch: a failed $stage step returns an error"
else
bad "launch: a failed $stage step was reported as success" "rc=$rc $out"
fi
done
reset
TMUX_FAIL=pane rc_launch Dev "$T/work" "$SID" >/dev/null 2>&1
if calls | grep -q "^send-keys$US"; then
bad "launch: attempted to send input after the pane wait expired"
else
ok "launch: no input is sent after the pane wait expires"
fi

# --- rc_launch, resume under a secondary profile --------------------------------------------
mkdir -p "$T/prof/projects/p"; : > "$T/prof/projects/p/$SID.jsonl"
reset
out="$(rc_launch Dev "$T/work" "$SID" "$T/prof" 2>&1)"
line_is "resume: the launch line (resume flag, profile prefix) goes to =NAME:" "send-keys${US}-t${US}=Dev:${US}set -a; source '$RC_ENVFILE' 2>/dev/null; set +a; CLAUDE_CONFIG_DIR='$T/prof' claude --remote-control 'Dev' -r '$SID'${US}Enter"
no_bare "resume: no -t target is bare"

# --- rc_launch, already running: skipped before any pane command ------------------------------
reset; TMUX_HAS=0
out="$(rc_launch Dev "$T/work" "$SID" 2>&1)"
case "$out" in "SKIP name=Dev (already running)") ok "already running: SKIP" ;; *) bad "already running status" "$out" ;; esac
[ "$(calls | wc -l | tr -d ' ')" = 1 ] && line_is "already running: only has-session =NAME was asked" "has-session${US}-t${US}=Dev" || bad "already running: more than has-session ran" "$(calls | tr "$US" ' ')"

# --- rc_sweep ------------------------------------------------------------------------------------
printf 'Dev\t%s\t%s\n' "$T/work" "$SID" > "$RC_REGISTRY"
reset; TMUX_HAS=0; TMUX_ATTACHED=1
out="$(rc_sweep 2>&1)"
line_is "sweep: has-session uses =NAME" "has-session${US}-t${US}=Dev"
line_is "sweep: the attached probe asks display-message on =NAME:" "display-message${US}-p${US}-t${US}=Dev:${US}#{session_attached}"
printf '%s\n' "$out" | grep -qE '^ +OK +Dev .*live\*' && ok "sweep: an attached session reads as OK, live*" || bad "sweep: attached row" "$out"
no_bare "sweep: no -t target is bare"
reset; TMUX_HAS=0; TMUX_ATTACHED=0
out="$(rc_sweep 2>&1)"
printf '%s\n' "$out" | grep -qE '^ +IDLE +Dev ' && ok "sweep: a detached session reads as IDLE" || bad "sweep: detached row" "$out"

# --- rc_archive (kills the session by its exact name) ----------------------------------------------
printf 'Dev\t%s\t%s\n' "$T/work" "$SID" > "$RC_REGISTRY"
reset; TMUX_HAS=0
out="$(rc_archive Dev 2>&1)"
line_is "archive: has-session uses =NAME" "has-session${US}-t${US}=Dev"
line_is "archive: kill-session uses =NAME (no colon)" "kill-session${US}-t${US}=Dev"
no_bare "archive: no -t target is bare"

# --- new-remote-claude.sh, the already-running branch -------------------------------------------------
reset; TMUX_HAS=0
out="$(TMUX_HAS=0 bash "$RS/new-remote-claude.sh" Dev "$T/work" 2>&1)"; rc=$? # the stub reads TMUX_HAS from its environment
{ [ "$rc" = 3 ] && case "$out" in *"ERR_EXISTS: a tmux session named 'Dev' is already running (cwd: $T/work)"*) true ;; *) false ;; esac; } \
&& ok "new-remote-claude: an already-running name exits 3 and reports the pane's dir" || bad "new-remote-claude: rc=$rc" "$out"
line_is "new-remote-claude: has-session uses =NAME" "has-session${US}-t${US}=Dev"
line_is "new-remote-claude: display-message reads the dir from =NAME:" "display-message${US}-p${US}-t${US}=Dev:${US}-F${US}#{pane_current_path}"
no_bare "new-remote-claude: no -t target is bare"

echo "remote-claude-targets-test: $pass passed, $fail failed"
[ "$fail" -eq 0 ]
115 changes: 115 additions & 0 deletions .claude-config/bin/tests/remote-claude-tmux-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env bash
# remote-claude-tmux-test.sh: rc_launch delivers its launch line to the exact pane on a REAL
# tmux server, with a sibling session whose name starts with the same letters
# (remote-sessions/lib-remote-claude.sh). remote-claude-targets-test.sh pins the argv of
# every call offline; this proves the argv does what it claims, because the defect was
# tmux-version behavior no stub reproduces: on tmux 3.5a and 3.6 `send-keys -t =NAME`
# fails with "can't find pane" (the launch line was silently lost and the session was a
# bare shell), and `display-message -t =NAME` prints an empty value.
#
# Everything runs on a private tmux server reached only through `tmux -L <unique socket>`
# (with TMUX_TMPDIR and TMUX unset or fixture), so the operator's own tmux is never touched.
# A stub `claude` on the fixture's PATH records that it was started and with which
# arguments; the env file, registry and archive are synthetic. The server is stopped, by its
# socket only, on exit.
#
# Pinned: rc_launch of "Dev" while "Dev2" is running starts the stub claude once, in the
# workdir, with the remote-control name and session id; the launch line appears in Dev's pane
# and never in Dev2's; the status line carries the pane's dir (an empty dir reads "unknown");
# rc_archive of Dev kills Dev and leaves Dev2 running. Also recorded, not asserted (the
# behavior is tmux-version dependent): whether the old bare target fails on this tmux.
#
# Usage: remote-claude-tmux-test.sh (exit 0 all pass, 1 otherwise, 77 did not run: no tmux)
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd -P)"
RS="$HERE/../../remote-sessions"
command -v tmux >/dev/null 2>&1 || { echo "remote-claude-tmux-test: did not run: no tmux"; exit 77; }
command -v bash >/dev/null 2>&1 || { echo "remote-claude-tmux-test: did not run: no bash"; exit 77; }
[ -f "$RS/lib-remote-claude.sh" ] || { echo "FAIL: lib-remote-claude.sh missing"; exit 1; }

REAL_TMUX="$(command -v tmux)" # the binary itself: env(1) cannot run a shell builtin or function
T="$(mktemp -d)"
SOCK="rc-targets-test-$$-$RANDOM"
SERVER_UP=0
# shellcheck disable=SC2329 # Invoked by the EXIT trap below.
cleanup() {
# Stop only the server this test started, by its own socket name.
[ "$SERVER_UP" = 1 ] && "$REAL_TMUX" -L "$SOCK" kill-server >/dev/null 2>&1
rm -rf "$T"
}
trap cleanup EXIT
pass=0; fail=0
ok() { pass=$((pass+1)); echo "PASS: $1"; }
bad() { fail=$((fail+1)); echo "FAIL: $1"; [ $# -gt 1 ] && printf ' %s\n' "$2"; }

echo "remote-claude-tmux-test:"
unset TMUX
export TMUX_TMPDIR="$T/tmuxtmp"; mkdir -p "$TMUX_TMPDIR"
mkdir -p "$T/home/.local/bin" "$T/home/.claude/projects" "$T/work"
export HOME="$T/home" RC_ENVFILE="$T/env.sh" RC_REGISTRY="$T/registry.tsv" RC_ARCHIVE="$T/archive.tsv" \
RC_DEFAULT_WORKDIR="$T/work" CLAUDE_STUB_LOG="$T/claude.log"
printf 'export CLAUDE_STUB_LOG=%q\n' "$CLAUDE_STUB_LOG" > "$RC_ENVFILE"
# The stub claude: record the arguments and the directory, then hold the pane.
cat > "$HOME/.local/bin/claude" <<'EOF'
#!/bin/bash
printf 'args=%s pwd=%s\n' "$*" "$PWD" >> "${CLAUDE_STUB_LOG:-/dev/null}"
exec sleep 120
EOF
chmod +x "$HOME/.local/bin/claude"

# Every tmux call in this test, the lib's included, goes to the private server.
tmux() { "$REAL_TMUX" -L "$SOCK" "$@"; }
export REAL_TMUX SOCK
export -f tmux
# shellcheck source=/dev/null
. "$RS/lib-remote-claude.sh" # puts the system dirs first on PATH; $HOME/.local/bin (the stub) leads
[ "$(type -t tmux)" = function ] || { echo "FAIL: the private-socket tmux wrapper is not in force; refusing to go on"; exit 1; }
# rc_launch waits 12 s for a real claude; poll for the stub instead.
sleep() { command sleep 0.2; }

# Start the private server with a plain bash in every pane (no user shell rc), HOME and PATH
# from the fixture, then a sibling session whose name Dev is a prefix of.
env -i HOME="$HOME" PATH="$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin" TERM=xterm TMUX_TMPDIR="$TMUX_TMPDIR" \
"$REAL_TMUX" -L "$SOCK" -f /dev/null new-session -d -s Anchor -c "$T/work" "sleep 300" 2>"$T/server.err"
if ! tmux has-session -t "=Anchor" 2>/dev/null; then
echo "remote-claude-tmux-test: did not run: tmux cannot start a private server here ($(tr '\n' ' ' < "$T/server.err"))"
exit 77
fi
SERVER_UP=1
tmux set -g default-command "bash --norc --noprofile"
tmux new-session -d -s Dev2 -c "$T/work"
echo "tmux $("$REAL_TMUX" -V | awk '{print $2}'), private socket $SOCK"

# What the old bare target does on this tmux (recorded, not asserted).
tmux new-session -d -s Probe -c "$T/work"
if tmux send-keys -t "=Probe" "true" Enter 2>"$T/old.err"; then
echo "NOTE: this tmux accepts send-keys -t =NAME; the missed delivery is not reproducible here"
else
echo "NOTE: this tmux rejects send-keys -t =NAME ($(tr -d '\n' < "$T/old.err")): the defect this test guards is live here"
fi
[ -z "$(tmux display-message -p -t '=Probe' '#{pane_current_path}' 2>/dev/null)" ] \
&& echo "NOTE: display-message -t =NAME prints an empty value here"
tmux kill-session -t "=Probe"

# --- rc_launch of Dev while Dev2 runs -------------------------------------------------------------
SID="11111111-2222-3333-4444-555555555555"
out="$(rc_launch Dev "$T/work" "$SID" 2>&1)"
# the stub writes its record when the pane shell runs the launch line; wait for it (bounded)
for _ in $(seq 50); do [ -s "$CLAUDE_STUB_LOG" ] && break; command sleep 0.1; done
[ "$(grep -c . "$CLAUDE_STUB_LOG" 2>/dev/null)" = 1 ] && ok "launch: the stub claude was started exactly once" || bad "launch: stub claude starts: $(grep -c . "$CLAUDE_STUB_LOG" 2>/dev/null || echo 0)" "$(cat "$CLAUDE_STUB_LOG" 2>/dev/null)"
grep -q "args=--remote-control Dev --session-id $SID pwd=$T/work\$" "$CLAUDE_STUB_LOG" 2>/dev/null \
&& ok "launch: it ran in the workdir with the remote-control name and the new session id" || bad "launch: stub arguments" "$(cat "$CLAUDE_STUB_LOG" 2>/dev/null)"
dev_pane="$(tmux capture-pane -p -J -t '=Dev:' 2>/dev/null)"
dev2_pane="$(tmux capture-pane -p -J -t '=Dev2:' 2>/dev/null)"
case "$dev_pane" in *"claude --remote-control 'Dev'"*) ok "launch: the launch line is in Dev's pane" ;; *) bad "launch: Dev's pane has no launch line" "$dev_pane" ;; esac
case "$dev2_pane" in *"claude"*|*"remote-control"*) bad "launch: the launch line reached the prefix-sharing sibling Dev2" "$dev2_pane" ;; *) ok "launch: nothing reached the prefix-sharing sibling Dev2's pane" ;; esac
case "$out" in *"OK name=Dev cwd=$T/work session_id=$SID mode=create"*) ok "launch: the status line carries the pane's dir (display-message resolved)" ;; *) bad "launch: status line" "$out" ;; esac

# --- rc_archive of Dev kills Dev only ---------------------------------------------------------------
rc_register Dev "$T/work" "$SID" # rc_launch does not register; new-remote-claude.sh does, and archive reads the registry
out="$(rc_archive Dev 2>&1)"
tmux has-session -t "=Dev" 2>/dev/null && bad "archive: Dev is still running" "$out" || ok "archive: Dev was killed"
tmux has-session -t "=Dev2" 2>/dev/null && ok "archive: the prefix-sharing sibling Dev2 is still running" || bad "archive: Dev2 was killed too" "$out"

echo "remote-claude-tmux-test: $pass passed, $fail failed"
[ "$fail" -eq 0 ]
Loading