--- /home/adom/scratch_overflow/claude-rc-patch/claude-rc.orig 2026-10-20 11:00:08.909931441 -0700 +++ /home/adom/.local/bin/claude-rc 2026-10-21 19:53:59.637432971 -0700 @@ -244,6 +244,8 @@ rotate_log echo "[$(date -Is)] claude-rc: launching (name=$(rc_name), env=$(env_name))" >> "$LOG" spawn_server + local acct; acct="$(account_id)" + if [ -n "$acct" ]; then st_set account "$acct"; else st_del account; fi st_set launches "${kept:+$kept,}$t" st_set last_launch "$t" st_set state enabled @@ -255,9 +257,31 @@ # `status` says running, the log says Ready, and the box simply never appears in the new # account's device list. ConfRoomROG 2026-09-23: the server had been up since Sep 19 while # the credentials were rewritten that morning, and John could not see the box from his phone. -# So: credentials newer than the process = stale identity, restart it. +# But a newer credentials file alone is NOT a new identity: Claude Code rewrites it on every +# access-token refresh (8 h tokens), same account. Restarting on mtime alone killed the server, +# and every live remote session with it, about every 8 h (Mac mini 2026-10-21: 10:20 and 18:16). +# So: credentials newer than the process AND (the account changed OR the server is not +# connected) = stale identity, restart it. +account_id() { + # accountUuid/organizationUuid from ~/.claude.json; empty when unknown (API key, no python3). + command -v python3 >/dev/null 2>&1 || return 0 + python3 - "$HOME/.claude.json" <<'PY' 2>/dev/null +import json, sys +try: + a = json.load(open(sys.argv[1])).get("oauthAccount") or {} +except Exception: + sys.exit(0) +if a.get("accountUuid"): + print("%s/%s" % (a["accountUuid"], a.get("organizationUuid", ""))) +PY +} +server_connected() { + # The TUI redraws its status line ("·✔︎· Connected · project · HEAD"); read the newest one. + tail -c 4000 "$LOG" 2>/dev/null | tr -d '\r' | sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' \ + | grep -a '^·' | tail -1 | grep -q 'Connected' +} server_identity_stale() { - local pid="$1" creds="$HOME/.claude/.credentials.json" age started cmtime + local pid="$1" creds="$HOME/.claude/.credentials.json" age started cmtime acct rec [ -n "$pid" ] || return 1 [ -s "$creds" ] || return 1 age="$(ps -o etimes= -p "$pid" 2>/dev/null | tr -d ' ')" @@ -266,7 +290,15 @@ cmtime="$(stat -c %Y "$creds" 2>/dev/null || echo 0)" # 60s of slack: the server writes nothing to the creds file, but a login that happens a # moment before the launch must not look newer than it. - [ "$cmtime" -gt $(( started + 60 )) ] + [ "$cmtime" -gt $(( started + 60 )) ] || return 1 + acct="$(account_id)" + rec="$(st_get account)" + if [ -n "$acct" ] && [ -z "$rec" ]; then + st_set account "$acct" # server started by an older keeper: adopt the current account + rec="$acct" + fi + [ -n "$acct" ] && [ "$acct" != "$rec" ] && return 0 + ! server_connected } restart_server() { @@ -288,7 +320,7 @@ case "$(state)" in enabled|failed) ;; *) return 0 ;; esac local pid; pid="$(running_pid)" if [ -n "$pid" ] && server_identity_stale "$pid"; then - echo "claude-rc: Claude was signed in again after this server started; restarting it so remote control uses the current account." + echo "claude-rc: the signed-in account changed (or the server lost its login) after this server started; restarting it so remote control uses the current account." restart_server "$pid" return $? fi