← Commit history

Publish 1.3.11

Colby Knox ·095120d601 · ·parent b6eb2e4
5 files changed +222−11
claude-rc+77−7
@@ -244,6 +244,9 @@ launch() {   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_del offline_since   st_set launches "${kept:+$kept,}$t"   st_set last_launch "$t"   st_set state enabled@@ -251,12 +254,44 @@ launch() {   return 0 } # A RUNNING server holds the account it started with. Sign Claude Code into a different-# account (or refresh an expired login) and the old process keeps serving the OLD identity:-# `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.-server_identity_stale() {+# account and the old process keeps serving the OLD identity: `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.+# 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 that alone dropped the server,+# and every live remote session with it, about every 8 h (Mac mini 2026-10-21: 10:20, 18:16).+# So a restart needs the credentials to be newer than the process AND either the account to+# have changed or the server to have stayed off "Connected" for 5 minutes.++# The signed-in account: accountUuid/organizationUuid from ~/.claude.json, empty when unknown+# (an API-key login, no python3).+account_id() {+  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+}++# The newest status line the server drew ("·✔︎· Connected · project · HEAD"). Connected, or no+# status line to read, counts as connected; anything else ("Reconnecting · retrying in ...")+# does not. The line is anchored at its start: the "Capacity: ... · ..." line under it also+# carries a middle dot.+server_connected() {+  local line+  line="$(tail -c 4000 "$LOG" 2>/dev/null | tr -d '\r' | sed 's/\x1b\[[0-9;?]*[A-Za-z]//g' | grep -a '^·' | tail -1)"+  [ -z "$line" ] && return 0+  case "$line" in *" Connected"*) return 0 ;; esac+  return 1+}++creds_newer_than_server() {   local pid="$1" creds="$HOME/.claude/.credentials.json" age started cmtime   [ -n "$pid" ] || return 1   [ -s "$creds" ] || return 1@@ -269,6 +304,41 @@ server_identity_stale() {   [ "$cmtime" -gt $(( started + 60 )) ] } +server_identity_stale() {+  local acct rec t since+  rec="$(st_get account)"+  if ! creds_newer_than_server "$1"; then+    # The credentials are the ones this server started with, so their account is the+    # server's: record it for a server started by a keeper that did not (1.3.10 and older).+    if [ -z "$rec" ]; then+      acct="$(account_id)"+      [ -n "$acct" ] && st_set account "$acct"+    fi+    st_del offline_since+    return 1+  fi+  acct="$(account_id)"+  if [ -n "$acct" ]; then+    # Never saw which account this server started with: fall back to restarting (launch+    # records it, so this happens at most once).+    [ -z "$rec" ] && return 0+    [ "$acct" != "$rec" ] && return 0+  fi+  # Same account, so a token refresh. Leave the server alone unless it has stayed off+  # Connected for 5 minutes: a reconnect after a network blip settles in seconds.+  if server_connected; then+    st_del offline_since+    return 1+  fi+  t="$(now)"+  since="$(st_get offline_since)"+  if ! [[ "$since" =~ ^[0-9]+$ ]] || [ "$since" -gt "$t" ]; then+    st_set offline_since "$t"+    return 1+  fi+  [ $(( t - since )) -ge 300 ]+}+ restart_server() {   local pid="$1"   [ -n "$pid" ] || return 0@@ -288,7 +358,7 @@ check() {   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: Claude was signed in to another account (or this server stopped connecting) after it started; restarting it so remote control uses the current login."     restart_server "$pid"     return $?   fi
install.sh

  
package.json+2−2
@@ -1,6 +1,6 @@ {   "slug": "hook",-  "version": "1.3.10",+  "version": "1.3.11",   "type": "app",   "org": "adom",   "bundled": true,@@ -56,7 +56,7 @@       "mode": "0755"     }   ],-  "changelog": "1.3.10: each machine runs the headless update at its own two minutes of the hour (a hash of its name and HOME) instead of */30, so the fleet no longer checks the wiki all at once at :00 and :30. ADOM_HOOK_CRON_MINUTE overrides the minute.",+  "changelog": "1.3.11: claude-rc no longer restarts remote control on every OAuth token refresh (about every 8 h, dropping live sessions). A newer credentials file restarts the server only when the signed-in account changed, or when the server has stayed off Connected for 5 minutes (hook#6).",   "discovery_pitch": "Use when adom packages are behind or an update failed, and when the user asks about Claude Remote Control -- opening AI threads on this container from their phone or claude.ai/code.",   "sample_prompts": [     {
page.json+2−2
@@ -1,6 +1,6 @@ {   "slug": "hook",-  "version": "1.3.10",+  "version": "1.3.11",   "type": "app",   "org": "adom",   "bundled": true,@@ -50,7 +50,7 @@     "name": "Kyle Bergstedt",     "email": "[email protected]"   },-  "changelog": "1.3.10: each machine runs the headless update at its own two minutes of the hour (a hash of its name and HOME) instead of */30, so the fleet no longer checks the wiki all at once at :00 and :30. ADOM_HOOK_CRON_MINUTE overrides the minute.",+  "changelog": "1.3.11: claude-rc no longer restarts remote control on every OAuth token refresh (about every 8 h, dropping live sessions). A newer credentials file restarts the server only when the signed-in account changed, or when the server has stayed off Connected for 5 minutes (hook#6).",   "discovery_pitch": "Use when adom packages are behind or an update failed, and when the user asks about Claude Remote Control -- opening AI threads on this container from their phone or claude.ai/code.",   "sample_prompts": [     {
tests/test_claude_rc.py+141
@@ -212,5 +212,146 @@ main check                         pass  +IDENTITY_HARNESS = HARNESS.replace("main \"$@\"", """+creds_newer_than_server() { [ -f "$STATE_DIR/creds_newer" ]; }+sleep() { :; }+main "$@"+""")+++class IdentityTest(unittest.TestCase):+    """When a running server is restarted after its credentials file changes (hook#6)."""++    A = "aaaa-1111/org-1"+    B = "bbbb-2222/org-1"+    CONNECTED = "\u00b7\u2714\ufe0e\u00b7 Connected \u00b7 project \u00b7 HEAD\n    Capacity: 0/32 \u00b7 New sessions\n"+    RECONNECTING = "\u00b7|\u00b7 Reconnecting \u00b7 retrying in 591ms \u00b7 disconnected 0s\n"++    def setUp(self):+        self.temp = tempfile.TemporaryDirectory(prefix="claude-rc-id-")+        self.addCleanup(self.temp.cleanup)+        self.root = Path(self.temp.name)+        self.env = dict(os.environ, RC_TEST_SOURCE=str(SOURCE), RC_TEST_DIR=str(self.root), HOME=str(self.root))+        self.env.pop("CLAUDE_RC_DISABLE", None)+        (self.root / "running").touch()+        (self.root / "prereqs").touch()+        self.at(10000)+        self.log(self.CONNECTED)++    def at(self, stamp):+        (self.root / "time").write_text(str(stamp))++    def write_state(self, **state):+        (self.root / "state").write_text("".join(f"{k}={v}\n" for k, v in state.items()))++    def state(self):+        return dict(line.split("=", 1) for line in (self.root / "state").read_text().splitlines())++    def signed_in(self, account):+        uuid, org = account.split("/")+        (self.root / ".claude.json").write_text('{"oauthAccount": {"accountUuid": "%s", "organizationUuid": "%s"}}' % (uuid, org))++    def log(self, text):+        (self.root / "log").write_text(text, encoding="utf-8")++    def creds_newer(self, yes=True):+        f = self.root / "creds_newer"+        f.touch() if yes else f.unlink(missing_ok=True)++    def check(self):+        p = subprocess.run(["bash", "-c", IDENTITY_HARNESS, "test-keeper", "check"],+                           env=self.env, capture_output=True, text=True, timeout=15)+        self.assertEqual(p.returncode, 0, p.stdout + p.stderr)+        return p.stdout++    def starts(self):+        p = self.root / "starts"+        return p.read_text().splitlines() if p.exists() else []++    def test_token_refresh_on_the_same_account_keeps_the_server(self):+        self.write_state(state="enabled", account=self.A)+        self.signed_in(self.A)+        self.creds_newer()+        self.check()+        self.assertEqual(self.starts(), [])++    def test_a_different_account_restarts_and_records_it(self):+        self.write_state(state="enabled", account=self.A)+        self.signed_in(self.B)+        self.creds_newer()+        self.assertIn("another account", self.check())+        self.assertEqual(len(self.starts()), 1)+        self.assertEqual(self.state()["account"], self.B)++    def test_an_older_keepers_server_learns_its_account_while_creds_are_unchanged(self):+        self.write_state(state="enabled")+        self.signed_in(self.A)+        self.check()+        self.assertEqual(self.starts(), [])+        self.assertEqual(self.state()["account"], self.A)+        # a sign-in to another account afterwards is still caught+        self.signed_in(self.B)+        self.creds_newer()+        self.check()+        self.assertEqual(len(self.starts()), 1)++    def test_newer_creds_with_no_recorded_account_restart_once(self):+        self.write_state(state="enabled")+        self.signed_in(self.A)+        self.creds_newer()+        self.check()+        self.assertEqual(len(self.starts()), 1)+        self.assertEqual(self.state()["account"], self.A)++    def test_a_brief_reconnect_after_a_refresh_does_not_restart(self):+        self.write_state(state="enabled", account=self.A)+        self.signed_in(self.A)+        self.creds_newer()+        self.log(self.CONNECTED + self.RECONNECTING)+        self.check()+        self.assertEqual(self.starts(), [])+        self.assertEqual(self.state()["offline_since"], "10000")+        self.at(10120)+        self.log(self.RECONNECTING + self.CONNECTED)+        self.check()+        self.assertEqual(self.starts(), [])+        self.assertNotIn("offline_since", self.state())++    def test_a_server_stuck_off_connected_for_5_minutes_restarts(self):+        self.write_state(state="enabled", account=self.A)+        self.signed_in(self.A)+        self.creds_newer()+        self.log(self.RECONNECTING)+        self.check()+        self.at(10240)+        self.check()+        self.assertEqual(self.starts(), [])+        self.at(10300)+        self.check()+        self.assertEqual(len(self.starts()), 1)+        self.assertNotIn("offline_since", self.state())++    def test_no_status_line_counts_as_connected(self):+        self.write_state(state="enabled", account=self.A)+        self.signed_in(self.A)+        self.creds_newer()+        self.log("Continue coding in the Claude mobile app\n")+        self.check()+        self.at(11000)+        self.check()+        self.assertEqual(self.starts(), [])++    def test_unknown_account_relies_on_the_connection_only(self):+        self.write_state(state="enabled")+        self.creds_newer()+        self.check()+        self.assertEqual(self.starts(), [])+        self.log(self.RECONNECTING)+        self.check()+        self.at(10300)+        self.check()+        self.assertEqual(len(self.starts()), 1)++ if __name__ == "__main__":     unittest.main()