From 0916618b7ba0a2f5704a73e70f5cf5005bfbf0d5 Mon Sep 17 00:00:00 2001 From: mysqto Date: Sun, 5 Jul 2026 21:30:11 +0800 Subject: [PATCH] isolate: concurrent sessions with shared memory via CLAUDE_CONFIG_DIR Adds 'ccswitch isolate ': runs a session under a per-profile CLAUDE_CONFIG_DIR (own credential + identity) while symlinking projects/, history.jsonl and CLAUDE.md to a shared dir, so memory and history are shared across profiles. Centralized under ~/.claude/profiles (override with CCSWITCH_ISOLATE_HOME). Existing non-symlink files are never clobbered. Co-Authored-By: Claude Opus 4.8 (1M context) --- README.md | 44 ++++++++++++++++++++++++ completions/ccswitch.fish | 18 ++++++++++ functions/ccswitch.fish | 70 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec96d5d..42ac95a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Requires [`jq`](https://jqlang.github.io/jq/). | `ccswitch [args...]` | Switch to ``, then start `claude` (extra args passed through) | | `ccswitch use ` | Switch without launching | | `ccswitch add ` | Sign in to a new account (`claude auth login`) and save it as `` | +| `ccswitch isolate [args...]` | Run a **concurrent** session isolated to `` (own credential) with memory/history shared across profiles | | `ccswitch save ` | Save the current account as `` | | `ccswitch list` / `ls` | List profiles (`*` marks the active one) | | `ccswitch current` / `whoami` | Show the active account | @@ -74,6 +75,49 @@ ccswitch save org-b `ccswitch list` distinguishes them by account **and** organization, so the same email can appear more than once with only the active org starred. +## Concurrent sessions (shared memory, separate accounts) + +`ccswitch ` swaps a single machine-global account, so it is sequential — +one active account at a time. To run **two accounts at once** (two terminals), +use `ccswitch isolate`, which gives each profile its own +[`CLAUDE_CONFIG_DIR`](https://code.claude.com/docs/en/settings): + +```fish +ccswitch isolate work # terminal 1 — signs in once, then reuses +ccswitch isolate personal # terminal 2 — a different account, at the same time +``` + +Each profile keeps its **own credential and identity**, but memory and history +are **shared** — `isolate` symlinks `projects/` (session transcripts + the +`memory/` files), `history.jsonl`, and `CLAUDE.md` from each profile to a common +`shared/` directory. So both accounts see the same project memory and past +sessions while staying logged in as different users. + +Layout (centralized under `~/.claude`, override with `$CCSWITCH_ISOLATE_HOME`): + +``` +~/.claude/profiles/ + ├── shared/ # one real copy of the shared memory/history + │ ├── projects/ + │ ├── history.jsonl + │ └── CLAUDE.md + ├── work/ # CLAUDE_CONFIG_DIR for "work" + │ ├── projects -> ../shared/projects + │ ├── history.jsonl -> ../shared/history.jsonl + │ ├── CLAUDE.md -> ../shared/CLAUDE.md + │ ├── .claude.json # own account identity (NOT shared) + │ └── settings.json # own settings + └── personal/ # CLAUDE_CONFIG_DIR for "personal" (same shared links) +``` + +`ccswitch isolate` with no name lists existing isolated profiles. Existing +non-symlink files in a profile dir are left untouched (never clobbered). + +> Note: this pattern relies on `CLAUDE_CONFIG_DIR` (supported) plus symlinking of +> account-agnostic paths (a community pattern, not officially documented). Auth +> stays isolated; only memory/history is shared. Two sessions writing the same +> project's memory simultaneously can race — low risk in practice. + ## Profile storage Profiles live in `$CCSWITCH_HOME` (default `~/.claude/accounts`), one directory diff --git a/completions/ccswitch.fish b/completions/ccswitch.fish index 0ad5ed3..b3faf17 100644 --- a/completions/ccswitch.fish +++ b/completions/ccswitch.fish @@ -15,6 +15,7 @@ complete -c ccswitch -f # subcommands and bare profile names (first token only) complete -c ccswitch -n __fish_use_subcommand -a add -d "log in a new account and save it" +complete -c ccswitch -n __fish_use_subcommand -a isolate -d "concurrent isolated session (shared memory)" complete -c ccswitch -n __fish_use_subcommand -a save -d "save current account" complete -c ccswitch -n __fish_use_subcommand -a use -d "switch without launching" complete -c ccswitch -n __fish_use_subcommand -a list -d "list profiles" @@ -25,3 +26,20 @@ complete -c ccswitch -n __fish_use_subcommand -a "(__ccswitch_profiles)" -d "swi # profile names as the argument to use/rm complete -c ccswitch -n "__fish_seen_subcommand_from use rm remove delete" -a "(__ccswitch_profiles)" -d profile + +function __ccswitch_iso_profiles -d "list isolated ccswitch profile names" + set -l base + if set -q CCSWITCH_ISOLATE_HOME + set base $CCSWITCH_ISOLATE_HOME + else + set base "$HOME/.claude/profiles" + end + test -d "$base"; or return + for d in "$base"/*/ + set -l n (basename "$d") + test "$n" = shared; and continue + test -L "$d/projects"; and echo $n + end +end + +complete -c ccswitch -n "__fish_seen_subcommand_from isolate iso" -a "(__ccswitch_iso_profiles)" -d "isolated profile" diff --git a/functions/ccswitch.fish b/functions/ccswitch.fish index d211f49..88e38bf 100644 --- a/functions/ccswitch.fish +++ b/functions/ccswitch.fish @@ -8,6 +8,8 @@ function ccswitch -d "switch between multiple Claude Code accounts" __ccswitch_save "$home" "$config" $argv[2] case add __ccswitch_add "$home" "$config" $argv[2] + case isolate iso + __ccswitch_isolate $argv[2..-1] case list ls __ccswitch_list "$home" "$config" case current whoami @@ -81,7 +83,7 @@ function __ccswitch_save -d "snapshot the current account into a profile" echo "usage: ccswitch save " >&2 return 1 end - if contains -- "$name" save add list ls current whoami use rm remove delete help + if contains -- "$name" save add isolate iso shared list ls current whoami use rm remove delete help echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2 return 1 end @@ -121,7 +123,7 @@ function __ccswitch_add -d "log in a new account and save it as a profile" echo "usage: ccswitch add " >&2 return 1 end - if contains -- "$name" save add list ls current whoami use rm remove delete help + if contains -- "$name" save add isolate iso shared list ls current whoami use rm remove delete help echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2 return 1 end @@ -289,6 +291,67 @@ function __ccswitch_rm -d "delete a saved profile" echo "removed profile '$name'" end +function __ccswitch_isolate_home -d "resolve the isolated-profile base directory" + if set -q CCSWITCH_ISOLATE_HOME + echo $CCSWITCH_ISOLATE_HOME + else + echo "$HOME/.claude/profiles" + end +end + +function __ccswitch_link -d "symlink a shared path into a profile dir, non-destructively" + set -l target $argv[1] + set -l link $argv[2] + if test -L "$link" + ln -sfn "$target" "$link" + else if test -e "$link" + echo "ccswitch: $link exists and is not a symlink — leaving it as-is" >&2 + else + ln -s "$target" "$link" + end +end + +function __ccswitch_isolate -d "launch a concurrent session isolated to a profile, memory shared" + set -l base (__ccswitch_isolate_home) + set -l shared "$base/shared" + set -l name $argv[1] + + if test -z "$name" + echo "isolated profiles in $base:" + set -l any 0 + if test -d "$base" + for d in "$base"/*/ + set -l n (basename "$d") + test "$n" = shared; and continue + test -L "$d/projects"; or continue + set any 1 + echo " $n" + end + end + test $any -eq 0; and echo " (none yet)" + echo "usage: ccswitch isolate [claude args...]" + return 0 + end + if contains -- "$name" shared save add isolate iso list ls current whoami use rm remove delete help + echo "ccswitch: '$name' is a reserved word, pick another profile name" >&2 + return 1 + end + + set -l dir "$base/$name" + mkdir -p "$shared/projects" "$dir" + touch "$shared/history.jsonl" + test -e "$shared/CLAUDE.md"; or touch "$shared/CLAUDE.md" + + # share memory/history across profiles, keep auth (.claude.json + creds) per-dir + __ccswitch_link "$shared/projects" "$dir/projects" + __ccswitch_link "$shared/history.jsonl" "$dir/history.jsonl" + __ccswitch_link "$shared/CLAUDE.md" "$dir/CLAUDE.md" + + echo "launching isolated '$name' — CLAUDE_CONFIG_DIR=$dir (memory shared via $shared)" + echo "(first run for a profile will ask you to sign in)" + env CLAUDE_CONFIG_DIR="$dir" claude $argv[2..-1] +end + function __ccswitch_help -d "show ccswitch usage" echo "ccswitch — switch between multiple Claude Code accounts" echo @@ -297,10 +360,13 @@ function __ccswitch_help -d "show ccswitch usage" echo " ccswitch use switch to without launching claude" echo " ccswitch add sign in to a new account and save it as " echo " ccswitch save save the current account as " + echo " ccswitch isolate run a concurrent session isolated to ," + echo " with memory/history shared across profiles" echo " ccswitch list | ls list saved profiles (* marks the active one)" echo " ccswitch current | whoami show the active account" echo " ccswitch rm delete a saved profile" echo " ccswitch help show this help" echo echo "profiles live in \$CCSWITCH_HOME (default ~/.claude/accounts)." + echo "isolated profiles live in \$CCSWITCH_ISOLATE_HOME (default ~/.claude/profiles)." end