From 7a29dc8e07d96a2f8ea174b10daaf877a2ed63da Mon Sep 17 00:00:00 2001 From: mysqto Date: Wed, 8 Jul 2026 18:46:43 +0800 Subject: [PATCH] legacy-fish: backport the daemon-stop-on-switch fix Keep the vendored legacy-fish mirror in sync with lei-wego/ccswitch-fish: stop the Claude Code daemon supervisor on switch so restored credentials take effect. Best-effort, background sessions kept. Co-Authored-By: Claude Opus 4.8 --- legacy-fish/README.md | 6 ++++++ legacy-fish/functions/ccswitch.fish | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/legacy-fish/README.md b/legacy-fish/README.md index 2022284..6fb586d 100644 --- a/legacy-fish/README.md +++ b/legacy-fish/README.md @@ -244,6 +244,12 @@ plaintext tokens to a shared or public repo. - Quit any running `claude` session before switching — an open session can rewrite `~/.claude.json` on exit and clobber the swap. `ccswitch` warns when it detects one. +- Recent Claude Code releases keep a background **daemon** that caches each + account's auth in memory. On every switch `ccswitch` stops its supervisor + (`claude daemon stop --any --keep-workers`) so the next session re-reads the + restored credential, while any detached background sessions keep running. It + is best-effort: on an older Claude Code, or when no daemon is running, it is a + harmless no-op and the switch proceeds regardless. - Access tokens expire, but the refresh token is restored too, so Claude Code re-refreshes automatically after a switch. - OAuth refresh tokens rotate on every use. To keep snapshots valid, `ccswitch` diff --git a/legacy-fish/functions/ccswitch.fish b/legacy-fish/functions/ccswitch.fish index 4548923..7a82987 100644 --- a/legacy-fish/functions/ccswitch.fish +++ b/legacy-fish/functions/ccswitch.fish @@ -147,6 +147,18 @@ function __ccswitch_add -d "log in a new account and save it as a profile" __ccswitch_save "$home" "$config" "$name" end +function __ccswitch_stop_daemon -d "stop the Claude Code daemon supervisor so a switch takes effect" + # Recent Claude Code releases keep a background daemon that caches each + # account's auth in memory, so a switch would not take effect until the + # daemon exited. Stop its supervisor so the next session re-reads the + # restored credential, while any detached background sessions keep running. + # Best-effort — an older Claude Code or no running daemon is a harmless + # no-op, and a failure never blocks the switch. + command -sq claude; or return 0 + command claude daemon stop --any --keep-workers >/dev/null 2>&1 + return 0 +end + function __ccswitch_sync_current -d "re-snapshot the live account into its matching profile" # OAuth refresh tokens rotate on every use, so a profile saved earlier goes # stale as its account keeps running. Before switching away, copy the live @@ -205,6 +217,10 @@ function __ccswitch_use -d "restore a profile as the active account" echo "ccswitch: warning — a running 'claude' may overwrite ~/.claude.json on exit; quit it first" >&2 end + # a running daemon caches this account's auth; stop its supervisor so the + # next session picks up the restored credential (background sessions kept) + __ccswitch_stop_daemon + # refresh the outgoing account's snapshot (its token may have rotated) __ccswitch_sync_current "$home" "$config"