xclaude-cli 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/xclaude.js +468 -65
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -34,7 +34,8 @@ Update with `npm i -g xclaude-cli@latest`; xclaude checks npm once a day and pri
|
|
|
34
34
|
| `xclaude set main --enable\|--disable` | use the login in `~/.claude` (plain `claude`'s) as the account `main` |
|
|
35
35
|
| `xclaude ls` | accounts with their login, defaults and config dir |
|
|
36
36
|
| `xclaude rm <name> [--keep-login] [-y]` | remove an account and log it out; its folder keeps its links, so its old conversations still open their saved long outputs, and files of its own, such as a `CLAUDE.md` you edited with `/memory`. Run it on the name again to delete that folder (only once it holds nothing but links) |
|
|
37
|
-
| `xclaude rm --leftovers [-y]` | delete the leftover folders of all removed accounts, after a confirmation |
|
|
37
|
+
| `xclaude rm --leftovers [-y]` | delete the leftover folders of all removed and renamed accounts, after a confirmation |
|
|
38
|
+
| `xclaude rename <name> <new-name> [-y]` | rename an account: its folder, settings, defaults and tmux sessions follow, and on Linux its login. On macOS the login is tied to the folder's path, so the account is logged out first (after a confirmation) and you log in again. Quit the account's sessions first. Like `rm`, it leaves a folder of links under the old name for old conversations |
|
|
38
39
|
| `xclaude tmux new\|attach\|ls\|kill` | Claude Code in tmux sessions, by label |
|
|
39
40
|
| `xclaude shell install\|uninstall [--bash] [--zsh]` | completion (and the guard) in your shell |
|
|
40
41
|
| `xclaude shell completion bash\|zsh` | print the completion script, to load it some other way |
|
|
@@ -67,15 +68,17 @@ xclaude's options (`--dir`, `--detach`, `--empty`) come right after the label, a
|
|
|
67
68
|
|
|
68
69
|
- **Shared by all accounts:** conversations and their checkpoints (`projects`, `file-history`), prompt history, task lists, plans, running-session info, personal skills, agents, commands, rules, output styles, hooks and plugins, and `~/.claude/CLAUDE.md`.
|
|
69
70
|
- **Per account:** the login, `.claude.json` (MCP servers, folder trust), `settings.json` (so plugins and the statusline can differ per account), keybindings, background sessions and caches. The MCP servers you added to `~/.claude.json` with `claude mcp add --scope user` still reach every account that takes `~/.claude`'s setup: each launch passes them with `--mcp-config`, and an account's own server of the same name wins (`xclaude <name> mcp list` shows only its own).
|
|
71
|
+
- **Logins:** each account signs in with its own login. A claude.ai login in your shell (`CLAUDE_CODE_OAUTH_TOKEN` from `claude setup-token`, and the variables that provision one) is left out of accounts: Claude Code would use it in place of every account's login, so one subscription ending would stop them all. `main` and plain `claude` still get it, and a token meant for one account can go in that account's `settings.json` `env`. `ANTHROPIC_API_KEY`, `ANTHROPIC_AUTH_TOKEN`, a cloud provider (`CLAUDE_CODE_USE_BEDROCK`, `_VERTEX`, `_FOUNDRY`) and `ANTHROPIC_PROFILE` come before an account's login too; they're passed on, each account's launch says so once, and `xclaude ls` names the credential in use in place of the email.
|
|
70
72
|
- **Asked once:** an account's first launch in a terminal (or `xclaude add`) shows what `~/.claude` would give it (settings, the plugins `~/.claude` has on, MCP servers) and asks whether it takes them. The answer is kept; `xclaude set <name> --inherit on|off` changes it. A launch without a terminal takes nothing until then and says once how to choose.
|
|
71
73
|
- **Taken from `~/.claude/settings.json`:** once the answer is yes, at every launch the account's `settings.json` takes what changed in `~/.claude/settings.json` since the last time: permission rules, hooks, auto mode and the rest, apart from login-bound settings (`env`, `apiKeyHelper` and the other credential helpers, `forceLoginMethod`, `forceLoginOrgUUID`) and `enabledPlugins`. What the account changed itself stays, so a rule it added or removed stays that way, and one you remove from `~/.claude/settings.json` goes from the accounts too. `~/.claude/settings.json` itself is never written. `xclaude set <name> --inherit off` stops it and takes back what the account took and hasn't changed; `--inherit reset` takes everything again.
|
|
72
74
|
- **Plugins:** a plugin installed under another login shows in an account's `/plugin` from its next launch: on when `~/.claude` has it on (in an account that takes `~/.claude`'s setup), off otherwise. Until you change such an entry, it follows `~/.claude` turning the plugin on or off; once you do, it's yours. Uninstalling a plugin removes it for every login. For the `~/.claude` login, turn on a plugin an account installed with `claude plugin enable <plugin>`.
|
|
75
|
+
- **Memory:** an account saves Claude's auto memory into `~/.claude/projects/<project>/memory/`, the folder plain `claude` uses, without asking, in auto mode as in any other. xclaude points Claude Code there with `CLAUDE_CODE_REMOTE_MEMORY_DIR`: from Claude Code 2.1.280 on, a save through the account's link would wait for your yes every time. Plan mode in an account still asks before it saves a plan file, and subagents with `memory: local` keep that memory in `~/.claude/projects/<project>/agent-memory-local/` instead of the project's `.claude/agent-memory-local/`.
|
|
73
76
|
|
|
74
77
|
Sharing works by linking each account's directories into `~/.claude`, which plain `claude` keeps using as before. Don't open the same conversation in two terminals at once. Scripts can read `XCLAUDE_ACCOUNT` to show the account, for example in a statusline.
|
|
75
78
|
|
|
76
79
|
## Files
|
|
77
80
|
|
|
78
|
-
- `~/.xclaude/config.json` holds accounts, defaults and settings. Most of it is edited through the commands; by hand you can add `share.add` / `share.remove` (extra directories to share, or built-in ones to stop sharing), `claudePath` (a fixed `claude` binary) and `tmux.statusRight` ([docs/architecture.md](docs/architecture.md#2-layout) has the format). `~/.xclaude/accounts/<name>/` is each account's `CLAUDE_CONFIG_DIR` (`xclaude ls` prints it, e.g. for an editor's environment settings). `XCLAUDE_HOME` moves `~/.xclaude`; set it before adding accounts, since on macOS the login is tied to the exact directory path and moving it later means logging in again.
|
|
81
|
+
- `~/.xclaude/config.json` holds accounts, defaults and settings. Most of it is edited through the commands; by hand you can add `share.add` / `share.remove` (extra directories to share, or built-in ones to stop sharing), `claudePath` (a fixed `claude` binary) and `tmux.statusRight` ([docs/architecture.md](docs/architecture.md#2-layout) has the format). `~/.xclaude/accounts/<name>/` is each account's `CLAUDE_CONFIG_DIR` (`xclaude ls` prints it, e.g. for an editor's environment settings; after `xclaude rename`, use the new one there). `XCLAUDE_HOME` moves `~/.xclaude`; set it before adding accounts, since on macOS the login is tied to the exact directory path and moving it later means logging in again.
|
|
79
82
|
- `~/.claude` stays the shared store; nothing in it moves.
|
|
80
83
|
|
|
81
84
|
## Uninstall
|
package/dist/xclaude.js
CHANGED
|
@@ -310,7 +310,8 @@ var SWITCHES = {
|
|
|
310
310
|
spawnFallback: false,
|
|
311
311
|
linkIde: false,
|
|
312
312
|
normalizePaths: false,
|
|
313
|
-
keepSameEmailLogin: false
|
|
313
|
+
keepSameEmailLogin: false,
|
|
314
|
+
memoryInStore: true
|
|
314
315
|
};
|
|
315
316
|
function loadSwitches(env) {
|
|
316
317
|
const switches = { ...SWITCHES };
|
|
@@ -362,16 +363,29 @@ async function authStatus(claude, env) {
|
|
|
362
363
|
email: str(json.email),
|
|
363
364
|
orgId: str(json.orgId),
|
|
364
365
|
orgName: str(json.orgName),
|
|
366
|
+
method: str(json.authMethod),
|
|
365
367
|
error: null
|
|
366
368
|
};
|
|
367
369
|
} catch {
|
|
368
370
|
const why = (res.stderr || res.stdout).trim().split("\n")[0] || `exit code ${res.code}`;
|
|
369
|
-
return { loggedIn: false, email: null, orgId: null, orgName: null, error: why };
|
|
371
|
+
return { loggedIn: false, email: null, orgId: null, orgName: null, method: null, error: why };
|
|
370
372
|
}
|
|
371
373
|
}
|
|
374
|
+
var NOT_A_LOGIN = {
|
|
375
|
+
oauth_token: "a token",
|
|
376
|
+
api_key: "an API key",
|
|
377
|
+
api_key_helper: "apiKeyHelper",
|
|
378
|
+
third_party: "a cloud provider"
|
|
379
|
+
};
|
|
380
|
+
function otherCredential(s) {
|
|
381
|
+
if (s.error || !s.loggedIn || s.method === null) return null;
|
|
382
|
+
return NOT_A_LOGIN[s.method] ?? null;
|
|
383
|
+
}
|
|
372
384
|
function describeLogin(s) {
|
|
373
385
|
if (s.error) return `unknown (${s.error})`;
|
|
374
386
|
if (!s.loggedIn) return "not logged in";
|
|
387
|
+
const other = otherCredential(s);
|
|
388
|
+
if (other) return s.email ? `${other} (${s.email})` : other;
|
|
375
389
|
const who = s.email ?? "logged in";
|
|
376
390
|
return s.orgName ? `${who} (${s.orgName})` : who;
|
|
377
391
|
}
|
|
@@ -517,7 +531,7 @@ function stamp(now = /* @__PURE__ */ new Date()) {
|
|
|
517
531
|
|
|
518
532
|
// src/core/config.ts
|
|
519
533
|
var EFFORTS = ["low", "medium", "high", "xhigh", "max", "ultracode"];
|
|
520
|
-
var COMMANDS = ["add", "rm", "ls", "set", "tmux", "doctor", "shell", "guard", "help"];
|
|
534
|
+
var COMMANDS = ["add", "rm", "rename", "ls", "set", "tmux", "doctor", "shell", "guard", "help"];
|
|
521
535
|
var MAIN = "main";
|
|
522
536
|
var ACCOUNT_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
|
523
537
|
function defaultConfig() {
|
|
@@ -534,9 +548,9 @@ function defaultConfig() {
|
|
|
534
548
|
function isEffort(value) {
|
|
535
549
|
return EFFORTS.includes(value);
|
|
536
550
|
}
|
|
537
|
-
function accountNameProblem(name) {
|
|
551
|
+
function accountNameProblem(name, opts = {}) {
|
|
538
552
|
if (name === MAIN) return `"${MAIN}" is reserved for the main identity (xclaude set main --enable)`;
|
|
539
|
-
if (COMMANDS.includes(name)) return `"${name}" is reserved: it's an xclaude command`;
|
|
553
|
+
if (!opts.existing && COMMANDS.includes(name)) return `"${name}" is reserved: it's an xclaude command`;
|
|
540
554
|
if (name.startsWith("-") || name.startsWith("_")) return `"${name}" is reserved: names can't start with - or _`;
|
|
541
555
|
if (!ACCOUNT_NAME.test(name)) {
|
|
542
556
|
return `"${name}" isn't a valid account name: use lowercase letters, digits and dashes, start with a letter, at most 32 characters`;
|
|
@@ -582,7 +596,7 @@ function validateConfig(raw) {
|
|
|
582
596
|
if (!isObject(accountsRaw)) throw new ConfigProblem("accounts must be an object");
|
|
583
597
|
const accounts = {};
|
|
584
598
|
for (const [name, value] of Object.entries(accountsRaw)) {
|
|
585
|
-
const problem2 = accountNameProblem(name);
|
|
599
|
+
const problem2 = accountNameProblem(name, { existing: true });
|
|
586
600
|
if (problem2) throw new ConfigProblem(`accounts: ${problem2}`);
|
|
587
601
|
accounts[name] = parseDefaults(value, `accounts.${name}.`);
|
|
588
602
|
}
|
|
@@ -643,6 +657,11 @@ function saveConfig(paths, config) {
|
|
|
643
657
|
writeFileAtomic(paths.config, `${JSON.stringify(config, null, 2)}
|
|
644
658
|
`, { mode: 384, fsync: true });
|
|
645
659
|
}
|
|
660
|
+
function renameAccount(config, from, to) {
|
|
661
|
+
const accounts = {};
|
|
662
|
+
for (const [name, defaults] of Object.entries(config.accounts)) accounts[name === from ? to : name] = defaults;
|
|
663
|
+
config.accounts = accounts;
|
|
664
|
+
}
|
|
646
665
|
function identities(config) {
|
|
647
666
|
const names = Object.keys(config.accounts);
|
|
648
667
|
if (config.main.enabled) names.push(MAIN);
|
|
@@ -699,17 +718,66 @@ function stringOption(p, name) {
|
|
|
699
718
|
}
|
|
700
719
|
|
|
701
720
|
// src/launch/env.ts
|
|
721
|
+
function sharedMemoryDir(paths, table, switches) {
|
|
722
|
+
if (!switches.memoryInStore) return null;
|
|
723
|
+
return table.dirs.includes("projects") && table.dirs.includes("agent-memory") ? paths.store : null;
|
|
724
|
+
}
|
|
725
|
+
var LOGIN_VARS = [
|
|
726
|
+
"CLAUDE_CODE_OAUTH_TOKEN",
|
|
727
|
+
"CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR",
|
|
728
|
+
"CLAUDE_CODE_OAUTH_REFRESH_TOKEN",
|
|
729
|
+
"CLAUDE_CODE_OAUTH_SCOPES"
|
|
730
|
+
];
|
|
731
|
+
var CREDENTIAL_VARS = [
|
|
732
|
+
"ANTHROPIC_API_KEY",
|
|
733
|
+
"ANTHROPIC_AUTH_TOKEN",
|
|
734
|
+
"CLAUDE_CODE_USE_BEDROCK",
|
|
735
|
+
"CLAUDE_CODE_USE_VERTEX",
|
|
736
|
+
"CLAUDE_CODE_USE_FOUNDRY",
|
|
737
|
+
"ANTHROPIC_PROFILE"
|
|
738
|
+
];
|
|
739
|
+
function setIn(env, names) {
|
|
740
|
+
return names.filter((name) => {
|
|
741
|
+
const value = env[name];
|
|
742
|
+
return value !== void 0 && value !== "" && !/^(0|false|no|off)$/i.test(value);
|
|
743
|
+
});
|
|
744
|
+
}
|
|
702
745
|
function buildEnv(base, identity) {
|
|
703
746
|
const env = {};
|
|
704
747
|
for (const [k, v] of Object.entries(base)) if (v !== void 0) env[k] = v;
|
|
705
|
-
if (identity.configDir)
|
|
706
|
-
|
|
748
|
+
if (identity.configDir) {
|
|
749
|
+
env.CLAUDE_CONFIG_DIR = identity.configDir;
|
|
750
|
+
for (const name of LOGIN_VARS) delete env[name];
|
|
751
|
+
if (identity.memoryDir && !env.CLAUDE_CODE_REMOTE_MEMORY_DIR) env.CLAUDE_CODE_REMOTE_MEMORY_DIR = identity.memoryDir;
|
|
752
|
+
} else {
|
|
753
|
+
delete env.CLAUDE_CONFIG_DIR;
|
|
754
|
+
if (identity.memoryDir && env.CLAUDE_CODE_REMOTE_MEMORY_DIR === identity.memoryDir) delete env.CLAUDE_CODE_REMOTE_MEMORY_DIR;
|
|
755
|
+
}
|
|
707
756
|
delete env.CLAUDE_SECURESTORAGE_CONFIG_DIR;
|
|
708
757
|
env.XCLAUDE_ACCOUNT = identity.name;
|
|
709
758
|
return env;
|
|
710
759
|
}
|
|
711
760
|
function identityEnv(ctx, name) {
|
|
712
|
-
return buildEnv(ctx.env, { name, configDir: name === MAIN ? null : accountDir(ctx.paths, name) });
|
|
761
|
+
return buildEnv(ctx.env, { name, configDir: name === MAIN ? null : accountDir(ctx.paths, name), memoryDir: null });
|
|
762
|
+
}
|
|
763
|
+
function joined(names) {
|
|
764
|
+
return names.length > 1 ? `${names.slice(0, -1).join(", ")} and ${names.at(-1)}` : names.join("");
|
|
765
|
+
}
|
|
766
|
+
function takeCredentialNotes(state, account, env) {
|
|
767
|
+
const told = new Set(state.credentialNoted[account] ?? []);
|
|
768
|
+
const dropped = setIn(env, LOGIN_VARS).filter((name) => !told.has(name));
|
|
769
|
+
const others = setIn(env, CREDENTIAL_VARS).filter((name) => !told.has(name));
|
|
770
|
+
const lines = [];
|
|
771
|
+
if (dropped.length) {
|
|
772
|
+
const [is, it] = dropped.length === 1 ? ["is", "it"] : ["are", "them"];
|
|
773
|
+
lines.push(`xclaude: ${account} signs in with its own login, so ${joined(dropped)} from your shell ${is} left out (plain claude still gets ${it})`);
|
|
774
|
+
}
|
|
775
|
+
if (others.length) {
|
|
776
|
+
const [is, it] = others.length === 1 ? ["is", "it"] : ["are", "them"];
|
|
777
|
+
lines.push(`xclaude: ${joined(others)} ${is} set in your shell, so Claude Code may use ${it} instead of ${account}'s own login (/status shows which)`);
|
|
778
|
+
}
|
|
779
|
+
if (lines.length) state.credentialNoted[account] = [...told, ...dropped, ...others].sort();
|
|
780
|
+
return lines;
|
|
713
781
|
}
|
|
714
782
|
|
|
715
783
|
// src/core/state.ts
|
|
@@ -722,7 +790,8 @@ function emptyState() {
|
|
|
722
790
|
seenUnknownEntries: {},
|
|
723
791
|
listedPlugins: {},
|
|
724
792
|
turnedOnPlugins: {},
|
|
725
|
-
inheritNoted: []
|
|
793
|
+
inheritNoted: [],
|
|
794
|
+
credentialNoted: {}
|
|
726
795
|
};
|
|
727
796
|
}
|
|
728
797
|
function isRecord(v) {
|
|
@@ -749,7 +818,7 @@ function loadState(paths) {
|
|
|
749
818
|
if (Array.isArray(entries)) state.seenUnknownEntries[name] = entries.filter((e) => typeof e === "string");
|
|
750
819
|
}
|
|
751
820
|
}
|
|
752
|
-
for (const key of ["listedPlugins", "turnedOnPlugins"]) {
|
|
821
|
+
for (const key of ["listedPlugins", "turnedOnPlugins", "credentialNoted"]) {
|
|
753
822
|
const lists = raw[key];
|
|
754
823
|
if (!isRecord(lists)) continue;
|
|
755
824
|
for (const [name, ids] of Object.entries(lists)) {
|
|
@@ -786,6 +855,20 @@ function forgetAccount(state, account) {
|
|
|
786
855
|
delete state.listedPlugins[account];
|
|
787
856
|
delete state.turnedOnPlugins[account];
|
|
788
857
|
state.inheritNoted = state.inheritNoted.filter((name) => name !== account);
|
|
858
|
+
delete state.credentialNoted[account];
|
|
859
|
+
}
|
|
860
|
+
function renameInState(state, from, to) {
|
|
861
|
+
forgetAccount(state, to);
|
|
862
|
+
for (const [dir, name] of Object.entries(state.lastAccountByDir)) {
|
|
863
|
+
if (name === from) state.lastAccountByDir[dir] = to;
|
|
864
|
+
}
|
|
865
|
+
if (state.lastUsedAccount === from) state.lastUsedAccount = to;
|
|
866
|
+
for (const key of ["seenUnknownEntries", "listedPlugins", "turnedOnPlugins", "credentialNoted"]) {
|
|
867
|
+
if (!Object.hasOwn(state[key], from)) continue;
|
|
868
|
+
state[key][to] = state[key][from];
|
|
869
|
+
delete state[key][from];
|
|
870
|
+
}
|
|
871
|
+
state.inheritNoted = state.inheritNoted.map((name) => name === from ? to : name);
|
|
789
872
|
}
|
|
790
873
|
|
|
791
874
|
// src/link/inherit.ts
|
|
@@ -1825,6 +1908,7 @@ var APPENDIX_A = [
|
|
|
1825
1908
|
"policy-limits.json",
|
|
1826
1909
|
"remote-settings.json",
|
|
1827
1910
|
"hfi-auth.json",
|
|
1911
|
+
"config.json",
|
|
1828
1912
|
"daemon",
|
|
1829
1913
|
"jobs",
|
|
1830
1914
|
"teams",
|
|
@@ -2632,12 +2716,14 @@ async function addCommand(ctx, config, args) {
|
|
|
2632
2716
|
log(`xclaude: ${name} isn't logged in yet. Log in later with \`xclaude ${name} auth login\`, or /login inside \`xclaude ${name}\`.`);
|
|
2633
2717
|
return EXIT_OK;
|
|
2634
2718
|
}
|
|
2635
|
-
|
|
2719
|
+
const credential = otherCredential(status);
|
|
2720
|
+
if (credential) log(`xclaude: ${name} signs in with ${credential}, not a claude.ai login. Launch it with: xclaude ${name}`);
|
|
2721
|
+
else log(`xclaude: ${name} is logged in as ${describeLogin(status)}. Launch it with: xclaude ${name}`);
|
|
2636
2722
|
const others = identities(config).filter((n) => n !== name);
|
|
2637
2723
|
const theirs = await Promise.all(others.map((n) => authStatus(claude, identityEnv(ctx, n))));
|
|
2638
2724
|
others.forEach((other, i) => {
|
|
2639
2725
|
const s = theirs[i];
|
|
2640
|
-
if (s.loggedIn && s.email === status.email && s.orgId === status.orgId) {
|
|
2726
|
+
if (status.email && s.loggedIn && s.email === status.email && s.orgId === status.orgId) {
|
|
2641
2727
|
log(`xclaude: warning: ${name} and ${other} use the same login (${describeLogin(status)})`);
|
|
2642
2728
|
}
|
|
2643
2729
|
});
|
|
@@ -2649,6 +2735,35 @@ import { spawnSync as spawnSync2 } from "node:child_process";
|
|
|
2649
2735
|
import fs23 from "node:fs";
|
|
2650
2736
|
import path17 from "node:path";
|
|
2651
2737
|
|
|
2738
|
+
// src/grammar.ts
|
|
2739
|
+
var HIDDEN = ["__complete", "__names", "__update-check"];
|
|
2740
|
+
function isCommand(word) {
|
|
2741
|
+
return COMMANDS.includes(word);
|
|
2742
|
+
}
|
|
2743
|
+
function isHidden(word) {
|
|
2744
|
+
return HIDDEN.includes(word);
|
|
2745
|
+
}
|
|
2746
|
+
function classify(argv, config) {
|
|
2747
|
+
const [first, ...rest] = argv;
|
|
2748
|
+
if (first === void 0) return { kind: "launch", account: null, args: [] };
|
|
2749
|
+
if (first === "-v" || first === "--version") return { kind: "version" };
|
|
2750
|
+
if (first === "-h" || first === "--help") return { kind: "help" };
|
|
2751
|
+
if (first.startsWith("-")) return { kind: "launch", account: null, args: argv };
|
|
2752
|
+
if (isCommand(first)) return { kind: "command", name: first, args: rest };
|
|
2753
|
+
if (isHidden(first)) return { kind: "hidden", name: first, args: rest };
|
|
2754
|
+
if (first === MAIN) {
|
|
2755
|
+
if (!config.main.enabled) throw new XError(`the main identity is disabled; enable it with: xclaude set main --enable`);
|
|
2756
|
+
return { kind: "launch", account: MAIN, args: rest };
|
|
2757
|
+
}
|
|
2758
|
+
if (Object.hasOwn(config.accounts, first)) return { kind: "launch", account: first, args: rest };
|
|
2759
|
+
throw unknownAccount(first, config);
|
|
2760
|
+
}
|
|
2761
|
+
function unknownAccount(word, config) {
|
|
2762
|
+
const names = identities(config);
|
|
2763
|
+
const list = names.length ? `accounts: ${names.join(", ")}` : "no accounts yet; add one with: xclaude add <name>";
|
|
2764
|
+
return new UsageError(`unknown account or command "${word}" (${list})`);
|
|
2765
|
+
}
|
|
2766
|
+
|
|
2652
2767
|
// src/link/normalize.ts
|
|
2653
2768
|
import fs19 from "node:fs";
|
|
2654
2769
|
import path13 from "node:path";
|
|
@@ -2746,7 +2861,7 @@ function strayFolders(paths, config) {
|
|
|
2746
2861
|
if (!st) continue;
|
|
2747
2862
|
if (st.isSymbolicLink()) out.push({ name, dir, kind: "link", entries: [] });
|
|
2748
2863
|
else if (!st.isDirectory() || accounts.some((a) => a.dev === st.dev && a.ino === st.ino)) continue;
|
|
2749
|
-
else if (accountNameProblem(name)) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2864
|
+
else if (accountNameProblem(name, { existing: true })) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2750
2865
|
else {
|
|
2751
2866
|
const entries = fs20.readdirSync(dir).filter((e) => e !== ".DS_Store" && !lstatOrNull(path14.join(dir, e))?.isSymbolicLink());
|
|
2752
2867
|
out.push({ name, dir, kind: entries.length ? "content" : "leftover", entries });
|
|
@@ -2785,6 +2900,18 @@ function keepOnlyLinks(dir, table) {
|
|
|
2785
2900
|
fs20.rmdirSync(dir);
|
|
2786
2901
|
return { kept: false, content };
|
|
2787
2902
|
}
|
|
2903
|
+
function leaveLinks(dir, moved, table) {
|
|
2904
|
+
const links = [...linkNames(table)].filter((name) => lstatOrNull(path14.join(moved, name))?.isSymbolicLink());
|
|
2905
|
+
if (!links.length) return false;
|
|
2906
|
+
try {
|
|
2907
|
+
fs20.mkdirSync(dir, { mode: 448 });
|
|
2908
|
+
} catch (e) {
|
|
2909
|
+
if (errCode(e) === "EEXIST") return false;
|
|
2910
|
+
throw e;
|
|
2911
|
+
}
|
|
2912
|
+
for (const name of links) fs20.symlinkSync(fs20.readlinkSync(path14.join(moved, name)), path14.join(dir, name));
|
|
2913
|
+
return true;
|
|
2914
|
+
}
|
|
2788
2915
|
|
|
2789
2916
|
// src/link/unknown.ts
|
|
2790
2917
|
import fs21 from "node:fs";
|
|
@@ -2909,6 +3036,7 @@ var STRIPPED_ENV = [
|
|
|
2909
3036
|
"XCLAUDE_ACCOUNT",
|
|
2910
3037
|
"CLAUDE_CODE_CHILD_SESSION",
|
|
2911
3038
|
"CLAUDE_SECURESTORAGE_CONFIG_DIR",
|
|
3039
|
+
"CLAUDE_CODE_REMOTE_MEMORY_DIR",
|
|
2912
3040
|
"CLAUDECODE",
|
|
2913
3041
|
"CLAUDE_CODE_SESSION_ID",
|
|
2914
3042
|
"CLAUDE_CODE_SESSION_ATTENDED",
|
|
@@ -3130,6 +3258,7 @@ async function doctorCommand(ctx, args) {
|
|
|
3130
3258
|
if (name === MAIN) {
|
|
3131
3259
|
report.add("info", "the main identity: the login in ~/.claude, never linked", 2);
|
|
3132
3260
|
} else {
|
|
3261
|
+
if (isCommand(name)) report.add("warn", `"${name}" is also an xclaude command now, so launch it from the picker, or rename it: xclaude rename ${name} <new-name>`, 2);
|
|
3133
3262
|
const dir = accountDir(ctx.paths, name);
|
|
3134
3263
|
const insp = lstatOrNull(dir)?.isSymbolicLink() ? null : inspectAccount(ctx.paths, name, table, true);
|
|
3135
3264
|
if (!insp) {
|
|
@@ -3213,7 +3342,7 @@ ${s.orgId}`) ?? [], n]);
|
|
|
3213
3342
|
for (const f of configProblem ? [] : strayFolders(ctx.paths, config)) {
|
|
3214
3343
|
const where = t(f.dir);
|
|
3215
3344
|
if (f.kind === "leftover") {
|
|
3216
|
-
report.add("info", `${where}: links kept from the
|
|
3345
|
+
report.add("info", `${where}: links kept from the former account ${f.name}, for its old conversations; delete with: xclaude rm ${f.name}`);
|
|
3217
3346
|
} else if (f.kind === "content") {
|
|
3218
3347
|
report.add("warn", `${where} isn't an account but holds more than links (${f.entries.join(", ")}): \`xclaude add ${f.name}\` takes it back, or check it and delete it by hand`);
|
|
3219
3348
|
} else if (f.kind === "stray") {
|
|
@@ -3256,6 +3385,19 @@ ${s.orgId}`) ?? [], n]);
|
|
|
3256
3385
|
report.add("warn", `CLAUDE_CODE_EFFORT_LEVEL is set (${ctx.env.CLAUDE_CODE_EFFORT_LEVEL}); it overrides the effort level saved in every account's settings`);
|
|
3257
3386
|
envFindings++;
|
|
3258
3387
|
}
|
|
3388
|
+
const dropped = setIn(ctx.env, LOGIN_VARS);
|
|
3389
|
+
if (dropped.length) {
|
|
3390
|
+
const [is, it] = dropped.length === 1 ? ["is", "it"] : ["are", "them"];
|
|
3391
|
+
report.add("info", `${dropped.join(", ")} ${is} set: accounts don't get ${it}, so each signs in with its own login (plain claude still uses ${it})`);
|
|
3392
|
+
}
|
|
3393
|
+
const memory = ctx.env.CLAUDE_CODE_REMOTE_MEMORY_DIR;
|
|
3394
|
+
if (memory && memory !== ctx.paths.store) {
|
|
3395
|
+
report.add("info", `CLAUDE_CODE_REMOTE_MEMORY_DIR is set (${memory}): accounts keep it, so their memory goes there, as plain claude's does`);
|
|
3396
|
+
}
|
|
3397
|
+
for (const name of setIn(ctx.env, CREDENTIAL_VARS)) {
|
|
3398
|
+
report.add("warn", `${name} is set, so Claude Code may use it instead of each account's own login; unset it in your shell profile to stop that`);
|
|
3399
|
+
envFindings++;
|
|
3400
|
+
}
|
|
3259
3401
|
if (!envFindings) report.add("ok", "nothing that overrides accounts");
|
|
3260
3402
|
report.lines.push("");
|
|
3261
3403
|
const summary = report.errors || report.warnings ? `${report.errors} problem${report.errors === 1 ? "" : "s"}, ${report.warnings} warning${report.warnings === 1 ? "" : "s"}${report.errors && !p.values.fix ? ". Run `xclaude doctor --fix` to repair what can be repaired." : "."}` : "All good.";
|
|
@@ -3384,7 +3526,7 @@ function refreshInitFiles(paths, config, version) {
|
|
|
3384
3526
|
}
|
|
3385
3527
|
|
|
3386
3528
|
// package.json
|
|
3387
|
-
var package_default = { version: "0.
|
|
3529
|
+
var package_default = { version: "0.3.0" };
|
|
3388
3530
|
|
|
3389
3531
|
// src/version.ts
|
|
3390
3532
|
var VERSION = package_default.version;
|
|
@@ -3429,7 +3571,8 @@ Commands:
|
|
|
3429
3571
|
set <name> change an account's defaults
|
|
3430
3572
|
set main --enable|--disable use the login in ~/.claude as the account "main"
|
|
3431
3573
|
rm <name> remove an account and log it out
|
|
3432
|
-
rm --leftovers delete the folders
|
|
3574
|
+
rm --leftovers delete the folders former accounts left behind
|
|
3575
|
+
rename <name> <new-name> rename an account (on macOS, log in again)
|
|
3433
3576
|
tmux new|attach|ls|kill run Claude Code in tmux sessions, by label
|
|
3434
3577
|
shell install|uninstall completion (and the guard) in ~/.zshrc, ~/.bashrc
|
|
3435
3578
|
shell completion bash|zsh print the completion script to load elsewhere
|
|
@@ -3446,6 +3589,10 @@ has on and the MCP servers in ~/.claude.json. Its first launch in a terminal
|
|
|
3446
3589
|
asks; set <name> --inherit on|off changes the answer. A plugin installed under
|
|
3447
3590
|
another login shows in /plugin: on when the account takes ~/.claude's setup
|
|
3448
3591
|
and ~/.claude has it on, otherwise off.
|
|
3592
|
+
|
|
3593
|
+
Each account signs in with its own login: a CLAUDE_CODE_OAUTH_TOKEN in your
|
|
3594
|
+
shell is left out of accounts, and a launch says once when ANTHROPIC_API_KEY or
|
|
3595
|
+
another credential there may stand in for the account's login.
|
|
3449
3596
|
`;
|
|
3450
3597
|
var COMMAND_HELP = {
|
|
3451
3598
|
add: `Usage: xclaude add <name> [--model M] [--effort E] [--args "\u2026"]
|
|
@@ -3480,14 +3627,36 @@ delete that leftover folder, or --leftovers to delete every one, after a
|
|
|
3480
3627
|
confirmation.
|
|
3481
3628
|
|
|
3482
3629
|
--keep-login don't log the account out
|
|
3483
|
-
--leftovers delete the leftover folders of all removed accounts
|
|
3630
|
+
--leftovers delete the leftover folders of all removed and renamed accounts
|
|
3484
3631
|
-y don't ask for confirmation
|
|
3632
|
+
`,
|
|
3633
|
+
rename: `Usage: xclaude rename <name> <new-name> [-y]
|
|
3634
|
+
|
|
3635
|
+
Renames an account. Its folder becomes ~/.xclaude/accounts/<new-name>, with
|
|
3636
|
+
its settings, defaults and, on Linux, its login; its tmux sessions follow.
|
|
3637
|
+
Conversations and shared content stay where they are, in ~/.claude.
|
|
3638
|
+
|
|
3639
|
+
On macOS the login is tied to the folder's path, so after a confirmation the
|
|
3640
|
+
account is logged out first; log in again with xclaude <new-name> auth login.
|
|
3641
|
+
|
|
3642
|
+
The old name's folder keeps its links into ~/.claude, so conversations from
|
|
3643
|
+
before can still open their saved long outputs; xclaude rm <name> deletes it.
|
|
3644
|
+
The new name can take over such a leftover folder of a removed account.
|
|
3645
|
+
|
|
3646
|
+
Claude Code must not be running in the account: quit its sessions first,
|
|
3647
|
+
background ones too. Use the new folder wherever you set CLAUDE_CONFIG_DIR by
|
|
3648
|
+
hand, such as in an editor's settings.
|
|
3649
|
+
|
|
3650
|
+
-y don't ask before logging out (macOS), and go on when running
|
|
3651
|
+
sessions can't be checked for
|
|
3485
3652
|
`,
|
|
3486
3653
|
ls: `Usage: xclaude ls
|
|
3487
3654
|
|
|
3488
3655
|
Lists the accounts with their email, organization and login status, their
|
|
3489
3656
|
defaults, and each one's config dir (the CLAUDE_CONFIG_DIR to use elsewhere,
|
|
3490
|
-
e.g. in an editor's environment settings).
|
|
3657
|
+
e.g. in an editor's environment settings). When Claude Code signs an account
|
|
3658
|
+
in with something else, such as an ANTHROPIC_API_KEY in your shell, LOGIN
|
|
3659
|
+
names it.
|
|
3491
3660
|
`,
|
|
3492
3661
|
set: `Usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"]
|
|
3493
3662
|
[--unset model|effort|args] [--inherit on|off|reset]
|
|
@@ -3759,6 +3928,7 @@ var MODEL_ALIASES = ["default", "best", "fable", "opus", "sonnet", "haiku", "opu
|
|
|
3759
3928
|
var COMMAND_DESCRIPTIONS = {
|
|
3760
3929
|
add: "add an account and log in",
|
|
3761
3930
|
rm: "remove an account",
|
|
3931
|
+
rename: "rename an account",
|
|
3762
3932
|
ls: "list accounts and logins",
|
|
3763
3933
|
set: "change an account's defaults",
|
|
3764
3934
|
tmux: "Claude Code in tmux sessions",
|
|
@@ -3902,8 +4072,10 @@ var Completer = class {
|
|
|
3902
4072
|
{ "--keep-login": null, "--leftovers": null, "-y": null },
|
|
3903
4073
|
1,
|
|
3904
4074
|
() => [...Object.keys(this.config.accounts), ...leftoverNames(this.ctx.paths, this.config)],
|
|
3905
|
-
(n) => this.config.accounts[n] ? this.describeIdentity(n) : "leftover folder of a
|
|
4075
|
+
(n) => this.config.accounts[n] ? this.describeIdentity(n) : "leftover folder of a former account"
|
|
3906
4076
|
);
|
|
4077
|
+
case "rename":
|
|
4078
|
+
return this.own(rest, cur, { "-y": null }, 1, () => Object.keys(this.config.accounts));
|
|
3907
4079
|
case "set":
|
|
3908
4080
|
return this.own(
|
|
3909
4081
|
rest,
|
|
@@ -4177,68 +4349,270 @@ async function lsCommand(ctx, config, args) {
|
|
|
4177
4349
|
return EXIT_OK;
|
|
4178
4350
|
}
|
|
4179
4351
|
|
|
4180
|
-
// src/
|
|
4352
|
+
// src/commands/rename.ts
|
|
4353
|
+
import fs29 from "node:fs";
|
|
4354
|
+
import path23 from "node:path";
|
|
4355
|
+
|
|
4356
|
+
// src/claude/running.ts
|
|
4357
|
+
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
4181
4358
|
import fs27 from "node:fs";
|
|
4359
|
+
import path22 from "node:path";
|
|
4360
|
+
var VAR = "CLAUDE_CONFIG_DIR=";
|
|
4361
|
+
function namesDir(value, dirs) {
|
|
4362
|
+
if (!path22.isAbsolute(value)) return false;
|
|
4363
|
+
const plain = path22.normalize(value).replace(/(.)\/+$/, "$1");
|
|
4364
|
+
if (dirs.includes(plain)) return true;
|
|
4365
|
+
const real = realpathOrNull(value);
|
|
4366
|
+
return real !== null && dirs.some((d) => realpathOrNull(d) === real);
|
|
4367
|
+
}
|
|
4368
|
+
function procEnv(pid) {
|
|
4369
|
+
try {
|
|
4370
|
+
return fs27.readFileSync(`/proc/${pid}/environ`, "utf8").split("\0");
|
|
4371
|
+
} catch {
|
|
4372
|
+
return null;
|
|
4373
|
+
}
|
|
4374
|
+
}
|
|
4375
|
+
function fromProc(dirs, exclude) {
|
|
4376
|
+
let pids;
|
|
4377
|
+
try {
|
|
4378
|
+
pids = fs27.readdirSync("/proc").filter((n) => /^\d+$/.test(n));
|
|
4379
|
+
} catch {
|
|
4380
|
+
return null;
|
|
4381
|
+
}
|
|
4382
|
+
if (procEnv("self") === null) return null;
|
|
4383
|
+
const out = [];
|
|
4384
|
+
for (const pid of pids) {
|
|
4385
|
+
if (exclude.includes(Number(pid))) continue;
|
|
4386
|
+
const value = procEnv(pid)?.find((v) => v.startsWith(VAR));
|
|
4387
|
+
if (value === void 0 || !namesDir(value.slice(VAR.length), dirs)) continue;
|
|
4388
|
+
let name = "?";
|
|
4389
|
+
try {
|
|
4390
|
+
name = fs27.readFileSync(`/proc/${pid}/comm`, "utf8").trim() || name;
|
|
4391
|
+
} catch {
|
|
4392
|
+
}
|
|
4393
|
+
out.push({ pid: Number(pid), name });
|
|
4394
|
+
}
|
|
4395
|
+
return out;
|
|
4396
|
+
}
|
|
4397
|
+
function parsePs(text, dirs, exclude, self) {
|
|
4398
|
+
const out = [];
|
|
4399
|
+
let sawSelf = false;
|
|
4400
|
+
for (const line of text.split("\n")) {
|
|
4401
|
+
const m = /^\s*(\d+)\s+(.*)$/.exec(line);
|
|
4402
|
+
if (!m) continue;
|
|
4403
|
+
const pid = Number(m[1]);
|
|
4404
|
+
const rest = m[2];
|
|
4405
|
+
if (pid === self) sawSelf = / (?:PATH|HOME|USER)=/.test(rest);
|
|
4406
|
+
if (exclude.includes(pid) || pid === self) continue;
|
|
4407
|
+
for (const v of rest.matchAll(/ CLAUDE_CONFIG_DIR=(.*?)(?= [A-Za-z_][A-Za-z0-9_]*=|$)/g)) {
|
|
4408
|
+
if (namesDir(v[1], dirs)) {
|
|
4409
|
+
out.push({ pid, name: path22.basename(rest.split(" ")[0]) || "?" });
|
|
4410
|
+
break;
|
|
4411
|
+
}
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
return sawSelf ? out : null;
|
|
4415
|
+
}
|
|
4416
|
+
function fromPs(dirs, exclude) {
|
|
4417
|
+
const res = spawnSync5("ps", ["-axww", "-E", "-o", "pid=,command="], { encoding: "utf8", timeout: 1e4, maxBuffer: 64 * 1024 * 1024 });
|
|
4418
|
+
if (res.error || res.status !== 0) return null;
|
|
4419
|
+
return parsePs(res.stdout, dirs, [...exclude, res.pid], process.pid);
|
|
4420
|
+
}
|
|
4421
|
+
function processesUsing(dirs, platform = process.platform) {
|
|
4422
|
+
const exclude = [process.pid];
|
|
4423
|
+
if (platform === "linux") return fromProc(dirs, exclude);
|
|
4424
|
+
if (platform === "darwin") return fromPs(dirs, exclude);
|
|
4425
|
+
return null;
|
|
4426
|
+
}
|
|
4427
|
+
function describeProcs(procs) {
|
|
4428
|
+
const shown = procs.slice(0, 5).map((p) => `${p.pid} (${p.name})`);
|
|
4429
|
+
if (procs.length > 5) shown.push(`and ${procs.length - 5} more`);
|
|
4430
|
+
return shown.join(", ");
|
|
4431
|
+
}
|
|
4432
|
+
|
|
4433
|
+
// src/core/prompt.ts
|
|
4434
|
+
import fs28 from "node:fs";
|
|
4182
4435
|
function confirm(question) {
|
|
4183
|
-
|
|
4436
|
+
fs28.writeSync(2, `${question} [y/N] `);
|
|
4184
4437
|
let fd;
|
|
4185
4438
|
try {
|
|
4186
|
-
fd =
|
|
4439
|
+
fd = fs28.openSync("/dev/tty", "r");
|
|
4187
4440
|
} catch {
|
|
4188
4441
|
return false;
|
|
4189
4442
|
}
|
|
4190
4443
|
try {
|
|
4191
4444
|
const buf = Buffer.alloc(256);
|
|
4192
|
-
const n =
|
|
4445
|
+
const n = fs28.readSync(fd, buf, 0, buf.length, null);
|
|
4193
4446
|
const answer = buf.subarray(0, n).toString("utf8").trim().toLowerCase();
|
|
4194
4447
|
return answer === "y" || answer === "yes";
|
|
4195
4448
|
} catch {
|
|
4196
4449
|
return false;
|
|
4197
4450
|
} finally {
|
|
4198
|
-
|
|
4451
|
+
fs28.closeSync(fd);
|
|
4199
4452
|
}
|
|
4200
4453
|
}
|
|
4201
4454
|
|
|
4202
|
-
// src/
|
|
4203
|
-
var
|
|
4204
|
-
function
|
|
4205
|
-
|
|
4206
|
-
}
|
|
4207
|
-
|
|
4208
|
-
|
|
4209
|
-
|
|
4210
|
-
|
|
4211
|
-
|
|
4212
|
-
if (
|
|
4213
|
-
|
|
4214
|
-
if (
|
|
4215
|
-
if (
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4219
|
-
|
|
4220
|
-
|
|
4455
|
+
// src/commands/rename.ts
|
|
4456
|
+
var USAGE2 = "usage: xclaude rename <name> <new-name> [-y]";
|
|
4457
|
+
async function renameCommand(ctx, config, args, deps = {}) {
|
|
4458
|
+
const { platform = process.platform, processes = (dirs) => processesUsing(dirs), moveSessions = () => [] } = deps;
|
|
4459
|
+
const p = parseOptions(args, { yes: { short: "y" } }, "rename");
|
|
4460
|
+
const [from, to, ...extra] = p.positionals;
|
|
4461
|
+
if (!from || !to || extra.length) throw new UsageError(USAGE2);
|
|
4462
|
+
const yes = Boolean(p.values.yes);
|
|
4463
|
+
if (from === MAIN) throw new UsageError("the main identity is the login in ~/.claude, and its name is fixed");
|
|
4464
|
+
if (!Object.hasOwn(config.accounts, from)) throw unknownAccount(from, config);
|
|
4465
|
+
if (to === from) throw new UsageError(`${from} is already called that`);
|
|
4466
|
+
const problem2 = accountNameProblem(to);
|
|
4467
|
+
if (problem2) throw new UsageError(problem2);
|
|
4468
|
+
if (Object.hasOwn(config.accounts, to)) throw new XError(`account "${to}" already exists; pick another name`);
|
|
4469
|
+
const log = (line) => ctx.io.err(`${line}
|
|
4470
|
+
`);
|
|
4471
|
+
const t = (where) => tildify(where, ctx.paths.home);
|
|
4472
|
+
const dir = accountDir(ctx.paths, from);
|
|
4473
|
+
const target = accountDir(ctx.paths, to);
|
|
4474
|
+
const st = lstatOrNull(dir);
|
|
4475
|
+
if (st) {
|
|
4476
|
+
const refusal = unmanageable(dir, ctx.paths.store) ?? (st.isDirectory() ? null : "isn't a folder");
|
|
4477
|
+
if (refusal) throw new XError(`not renaming ${from}: ${t(dir)} ${refusal}`);
|
|
4478
|
+
}
|
|
4479
|
+
const replace = lstatOrNull(target) !== null;
|
|
4480
|
+
if (replace) {
|
|
4481
|
+
const f = strayFolders(ctx.paths, config).find((x) => x.name === to);
|
|
4482
|
+
if (f?.kind !== "leftover") {
|
|
4483
|
+
const what = f?.kind === "content" ? ` and holds more than links (${f.entries.join(", ")})` : "";
|
|
4484
|
+
throw new XError(`not renaming ${from}: ${t(target)} already exists${what}; move it away, or pick another name`);
|
|
4485
|
+
}
|
|
4221
4486
|
}
|
|
4222
|
-
|
|
4223
|
-
|
|
4487
|
+
const mine = processes([dir]);
|
|
4488
|
+
const theirs = replace ? processes([target]) : [];
|
|
4489
|
+
if (mine === null || theirs === null) {
|
|
4490
|
+
if (!yes) {
|
|
4491
|
+
throw new XError(`not renaming ${from}: couldn't check whether Claude Code still runs in it on this machine; quit its sessions, background ones too, then run this again with -y`);
|
|
4492
|
+
}
|
|
4493
|
+
log(`xclaude: couldn't check whether Claude Code still runs in ${from}; going on (-y)`);
|
|
4494
|
+
}
|
|
4495
|
+
const here = (dirs) => ctx.env.CLAUDE_CONFIG_DIR && namesDir(ctx.env.CLAUDE_CONFIG_DIR, dirs) ? " (this terminal is one of them)" : "";
|
|
4496
|
+
if (mine?.length) {
|
|
4497
|
+
throw new XError(
|
|
4498
|
+
`not renaming ${from}: these still run as ${from}: ${describeProcs(mine)}${here([dir])}. Quit its Claude Code sessions, background ones too, and what they started, then run this again.`
|
|
4499
|
+
);
|
|
4500
|
+
}
|
|
4501
|
+
if (theirs?.length) {
|
|
4502
|
+
throw new XError(`not renaming ${from}: these still run in ${t(target)}, the folder ${to} would take over: ${describeProcs(theirs)}${here([target])}. Quit them first.`);
|
|
4503
|
+
}
|
|
4504
|
+
let claude = null;
|
|
4505
|
+
let logOut = false;
|
|
4506
|
+
if (platform === "darwin" && st) {
|
|
4507
|
+
try {
|
|
4508
|
+
claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
4509
|
+
} catch (e) {
|
|
4510
|
+
if (!(e instanceof XError)) throw e;
|
|
4511
|
+
}
|
|
4512
|
+
const env = identityEnv(ctx, from);
|
|
4513
|
+
for (const name of CREDENTIAL_VARS) delete env[name];
|
|
4514
|
+
const status = claude ? await authStatus(claude, env) : null;
|
|
4515
|
+
logOut = status === null || status.loggedIn || status.error !== null;
|
|
4516
|
+
if (logOut && !yes) {
|
|
4517
|
+
if (!ctx.tty.stdin) throw new UsageError(`renaming ${from} on macOS logs it out: run it in a terminal, or pass -y`);
|
|
4518
|
+
const ok = confirm(`Rename ${from} to ${to}? On macOS the login is tied to the folder's path, so ${from} is logged out first, and ${to} logs in again.`);
|
|
4519
|
+
if (!ok) {
|
|
4520
|
+
log("xclaude: nothing renamed");
|
|
4521
|
+
throw new Cancelled();
|
|
4522
|
+
}
|
|
4523
|
+
}
|
|
4524
|
+
}
|
|
4525
|
+
const table = shareTable(config, ctx.switches);
|
|
4526
|
+
if (ctx.switches.normalizePaths && table.dirs.includes("plugins")) {
|
|
4527
|
+
normalizePaths(ctx.paths, { account: from, lockWaitMs: Infinity, log });
|
|
4528
|
+
}
|
|
4529
|
+
if (logOut) {
|
|
4530
|
+
const res = claude ? await runClaude(claude, ["auth", "logout"], identityEnv(ctx, from)) : null;
|
|
4531
|
+
if (res?.code !== 0) {
|
|
4532
|
+
const why = res ? (res.stderr || res.stdout).trim().split("\n")[0] : "claude wasn't found";
|
|
4533
|
+
log(`xclaude: logging ${from} out failed (${why}); its login may remain in the Keychain, under the old folder's path`);
|
|
4534
|
+
}
|
|
4535
|
+
}
|
|
4536
|
+
if (st) {
|
|
4537
|
+
try {
|
|
4538
|
+
if (replace) removeTree(target);
|
|
4539
|
+
fs29.renameSync(dir, target);
|
|
4540
|
+
} catch (e) {
|
|
4541
|
+
throw new XError(`not renaming ${from}: moving ${t(dir)} to ${t(target)} failed: ${e.message}`);
|
|
4542
|
+
}
|
|
4543
|
+
}
|
|
4544
|
+
try {
|
|
4545
|
+
const fresh = loadConfig(ctx.paths, { create: false }).config;
|
|
4546
|
+
if (!Object.hasOwn(fresh.accounts, from) || Object.hasOwn(fresh.accounts, to)) {
|
|
4547
|
+
throw new XError(`not renaming ${from}: the config changed meanwhile; run this again`);
|
|
4548
|
+
}
|
|
4549
|
+
renameAccount(fresh, from, to);
|
|
4550
|
+
saveConfig(ctx.paths, fresh);
|
|
4551
|
+
} catch (e) {
|
|
4552
|
+
if (st) {
|
|
4553
|
+
try {
|
|
4554
|
+
fs29.renameSync(target, dir);
|
|
4555
|
+
} catch {
|
|
4556
|
+
log(`xclaude: couldn't move ${t(target)} back to ${t(dir)}; move it by hand`);
|
|
4557
|
+
}
|
|
4558
|
+
}
|
|
4559
|
+
throw e;
|
|
4560
|
+
}
|
|
4561
|
+
const step2 = (what, run) => {
|
|
4562
|
+
try {
|
|
4563
|
+
run();
|
|
4564
|
+
} catch (e) {
|
|
4565
|
+
log(`xclaude: ${what} failed: ${e.message}`);
|
|
4566
|
+
}
|
|
4567
|
+
};
|
|
4568
|
+
let kept = false;
|
|
4569
|
+
if (st) {
|
|
4570
|
+
step2(`keeping links at ${t(dir)} for conversations from before`, () => {
|
|
4571
|
+
kept = leaveLinks(dir, target, table);
|
|
4572
|
+
});
|
|
4573
|
+
step2(`removing ${t(dir)}.lock`, () => removeTree(`${dir}.lock`));
|
|
4574
|
+
}
|
|
4575
|
+
step2(`moving what ${from} took from ~/.claude/settings.json`, () => {
|
|
4576
|
+
forgetInherit(ctx.paths, to);
|
|
4577
|
+
const base = basePath(ctx.paths, from);
|
|
4578
|
+
const baseStat = lstatOrNull(base);
|
|
4579
|
+
if (baseStat) moveNoClobber(base, basePath(ctx.paths, to), baseStat);
|
|
4580
|
+
});
|
|
4581
|
+
step2("removing the old --mcp-config file", () => {
|
|
4582
|
+
forgetMcp(ctx.paths, from);
|
|
4583
|
+
forgetMcp(ctx.paths, to);
|
|
4584
|
+
});
|
|
4585
|
+
step2("updating state.json", () => updateState(ctx.paths, (s) => renameInState(s, from, to)));
|
|
4586
|
+
let sessions = [];
|
|
4587
|
+
step2("moving its tmux sessions", () => {
|
|
4588
|
+
sessions = moveSessions(from, to);
|
|
4589
|
+
});
|
|
4590
|
+
log(`xclaude: renamed ${from} to ${to}; its config dir is now ${t(target)}, so use that wherever you set CLAUDE_CONFIG_DIR by hand (an editor's settings, say)`);
|
|
4591
|
+
if (kept) {
|
|
4592
|
+
log(`xclaude: ${t(dir)} keeps links into ~/.claude, so conversations from before can still open their saved long outputs; delete it with: xclaude rm ${from}`);
|
|
4593
|
+
}
|
|
4594
|
+
if (sessions.length) log(`xclaude: tmux sessions now on ${to}: ${sessions.join(", ")} (a command already typed in them still says ${from})`);
|
|
4595
|
+
const settings = readFileOrNull(path23.join(target, "settings.json"));
|
|
4596
|
+
if (settings && mentions(settings, [dir, t(dir)])) log(`xclaude: ${to}'s settings.json still names ${t(dir)}; change that to ${t(target)}`);
|
|
4597
|
+
if (logOut) log(`xclaude: log ${to} in again with \`xclaude ${to} auth login\`, or /login inside \`xclaude ${to}\``);
|
|
4598
|
+
return EXIT_OK;
|
|
4224
4599
|
}
|
|
4225
|
-
function
|
|
4226
|
-
const
|
|
4227
|
-
|
|
4228
|
-
return new UsageError(`unknown account or command "${word}" (${list})`);
|
|
4600
|
+
function mentions(text, paths) {
|
|
4601
|
+
const escaped = paths.map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
4602
|
+
return new RegExp(`(?:${escaped.join("|")})(?![A-Za-z0-9_-])`).test(text);
|
|
4229
4603
|
}
|
|
4230
4604
|
|
|
4231
4605
|
// src/commands/rm.ts
|
|
4232
|
-
var
|
|
4606
|
+
var USAGE3 = "usage: xclaude rm <name> [--keep-login] [-y], or xclaude rm --leftovers [-y]";
|
|
4233
4607
|
async function rmCommand(ctx, config, args, listSessions2 = () => []) {
|
|
4234
4608
|
const p = parseOptions(args, { "keep-login": {}, leftovers: {}, yes: { short: "y" } }, "rm");
|
|
4235
4609
|
const [name, ...extra] = p.positionals;
|
|
4236
4610
|
const yes = Boolean(p.values.yes);
|
|
4237
4611
|
if (p.values.leftovers) {
|
|
4238
|
-
if (name) throw new UsageError(
|
|
4612
|
+
if (name) throw new UsageError(USAGE3);
|
|
4239
4613
|
return removeLeftovers(ctx, config, null, yes, listSessions2);
|
|
4240
4614
|
}
|
|
4241
|
-
if (!name || extra.length) throw new UsageError(
|
|
4615
|
+
if (!name || extra.length) throw new UsageError(USAGE3);
|
|
4242
4616
|
if (name === MAIN) throw new UsageError("the main identity isn't removable; turn it off with: xclaude set main --disable");
|
|
4243
4617
|
if (!Object.hasOwn(config.accounts, name)) {
|
|
4244
4618
|
if (strayFolders(ctx.paths, config).some((f) => f.name === name)) return removeLeftovers(ctx, config, name, yes, listSessions2);
|
|
@@ -4392,13 +4766,13 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
4392
4766
|
}
|
|
4393
4767
|
|
|
4394
4768
|
// src/commands/set.ts
|
|
4395
|
-
var
|
|
4769
|
+
var USAGE4 = 'usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"] [--unset model|effort|args] [--inherit on|off|reset], or xclaude set main --enable|--disable';
|
|
4396
4770
|
var KEYS = ["model", "effort", "args"];
|
|
4397
4771
|
var INHERIT = ["on", "off", "reset"];
|
|
4398
4772
|
function setCommand(ctx, config, args) {
|
|
4399
4773
|
const p = parseOptions(args, { ...DEFAULT_OPTIONS, unset: { value: true, repeat: true }, inherit: { value: true }, enable: {}, disable: {} }, "set");
|
|
4400
4774
|
const [name, ...extra] = p.positionals;
|
|
4401
|
-
if (!name || extra.length) throw new UsageError(
|
|
4775
|
+
if (!name || extra.length) throw new UsageError(USAGE4);
|
|
4402
4776
|
const isMain = name === MAIN;
|
|
4403
4777
|
if (!isMain && !Object.hasOwn(config.accounts, name)) throw unknownAccount(name, config);
|
|
4404
4778
|
const target = isMain ? config.main : config.accounts[name];
|
|
@@ -4518,8 +4892,8 @@ function shellCommand(ctx, config, args) {
|
|
|
4518
4892
|
}
|
|
4519
4893
|
|
|
4520
4894
|
// src/commands/tmux.ts
|
|
4521
|
-
import
|
|
4522
|
-
import
|
|
4895
|
+
import fs31 from "node:fs";
|
|
4896
|
+
import path24 from "node:path";
|
|
4523
4897
|
|
|
4524
4898
|
// src/launch/args.ts
|
|
4525
4899
|
function hasFlag(args, flag) {
|
|
@@ -4660,12 +5034,15 @@ async function launch(ctx, config, account, userArgs) {
|
|
|
4660
5034
|
const help = needsHelp(defaults, userArgs) ? loadHelp(claude, ctx.paths.cache, ctx.env) : null;
|
|
4661
5035
|
const mcp = isMain ? null : mcpForLaunch(ctx, settled, account, defaults, userArgs, help);
|
|
4662
5036
|
const args = [...mcp ? [`--mcp-config=${mcp}`] : [], ...buildArgs(defaults, userArgs, help)];
|
|
4663
|
-
const
|
|
5037
|
+
const memoryDir = sharedMemoryDir(ctx.paths, shareTable(settled, ctx.switches), ctx.switches);
|
|
5038
|
+
const env = buildEnv(ctx.env, { name: account, configDir: isMain ? null : accountDir(ctx.paths, account), memoryDir });
|
|
4664
5039
|
try {
|
|
4665
5040
|
updateState(ctx.paths, (state) => {
|
|
4666
5041
|
recordUse(state, ctx.cwd, account);
|
|
4667
5042
|
const configLabel = tildify(ctx.paths.config, ctx.paths.home);
|
|
4668
5043
|
for (const entry of takeNewUnknown(state, account, unknown)) ctx.io.err(`${unknownNotice(account, entry, configLabel)}
|
|
5044
|
+
`);
|
|
5045
|
+
if (!isMain) for (const line of takeCredentialNotes(state, account, ctx.env)) ctx.io.err(`${line}
|
|
4669
5046
|
`);
|
|
4670
5047
|
});
|
|
4671
5048
|
} catch {
|
|
@@ -4674,8 +5051,8 @@ async function launch(ctx, config, account, userArgs) {
|
|
|
4674
5051
|
}
|
|
4675
5052
|
|
|
4676
5053
|
// src/tmux/claude-state.ts
|
|
4677
|
-
import { spawnSync as
|
|
4678
|
-
import
|
|
5054
|
+
import { spawnSync as spawnSync6 } from "node:child_process";
|
|
5055
|
+
import fs30 from "node:fs";
|
|
4679
5056
|
function describeAgent(e) {
|
|
4680
5057
|
if (e.status === "waiting") return { text: e.waitingFor ? `needs input (${e.waitingFor})` : "needs input", rank: 3 };
|
|
4681
5058
|
if (e.status === "busy") return { text: "working", rank: 2 };
|
|
@@ -4683,10 +5060,10 @@ function describeAgent(e) {
|
|
|
4683
5060
|
return null;
|
|
4684
5061
|
}
|
|
4685
5062
|
function parentLookup() {
|
|
4686
|
-
if (process.platform === "linux" &&
|
|
5063
|
+
if (process.platform === "linux" && fs30.existsSync("/proc/self/stat")) {
|
|
4687
5064
|
return (pid) => {
|
|
4688
5065
|
try {
|
|
4689
|
-
const stat =
|
|
5066
|
+
const stat = fs30.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
4690
5067
|
const ppid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
4691
5068
|
return Number.isFinite(ppid) ? ppid : null;
|
|
4692
5069
|
} catch {
|
|
@@ -4695,7 +5072,7 @@ function parentLookup() {
|
|
|
4695
5072
|
};
|
|
4696
5073
|
}
|
|
4697
5074
|
const map = /* @__PURE__ */ new Map();
|
|
4698
|
-
const res =
|
|
5075
|
+
const res = spawnSync6("ps", ["-axo", "pid=,ppid="], { encoding: "utf8", timeout: 1e4 });
|
|
4699
5076
|
for (const line of (res.stdout ?? "").split("\n")) {
|
|
4700
5077
|
const [pid, ppid] = line.trim().split(/\s+/).map(Number);
|
|
4701
5078
|
if (pid && ppid !== void 0 && Number.isFinite(ppid)) map.set(pid, ppid);
|
|
@@ -4812,8 +5189,8 @@ async function tmuxNew(ctx, config, args) {
|
|
|
4812
5189
|
account ??= await chooseAccount(ctx, config, `Run Claude Code in tmux session "${req.label}" as`);
|
|
4813
5190
|
if (!defaultsOf(config, account)) throw unknownAccount(account, config);
|
|
4814
5191
|
}
|
|
4815
|
-
const dir =
|
|
4816
|
-
if (!
|
|
5192
|
+
const dir = path24.resolve(ctx.cwd, req.dir ?? ".");
|
|
5193
|
+
if (!fs31.statSync(dir, { throwIfNoEntry: false })?.isDirectory()) throw new XError(`--dir ${req.dir}: not a directory`);
|
|
4817
5194
|
const existing = findByLabel(listSessions(tmux), req.label);
|
|
4818
5195
|
if (existing) {
|
|
4819
5196
|
throw new XError(`"${req.label}" is running (${describeSession(existing, ctx.paths.home)}): xclaude tmux attach ${req.label}, or kill it first`);
|
|
@@ -4922,6 +5299,30 @@ function accountSessionLabels(ctx, account) {
|
|
|
4922
5299
|
return [];
|
|
4923
5300
|
}
|
|
4924
5301
|
}
|
|
5302
|
+
function renameAccountSessions(ctx, from, to) {
|
|
5303
|
+
let tmux;
|
|
5304
|
+
let sessions;
|
|
5305
|
+
try {
|
|
5306
|
+
tmux = new Tmux(ctx.env);
|
|
5307
|
+
sessions = listSessions(tmux).filter((s) => s.account === from);
|
|
5308
|
+
} catch {
|
|
5309
|
+
return [];
|
|
5310
|
+
}
|
|
5311
|
+
const prefix = `[${from}] `;
|
|
5312
|
+
for (const s of sessions) {
|
|
5313
|
+
try {
|
|
5314
|
+
tmux.run(["set-option", "-t", s.id, "@xclaude_account", to]);
|
|
5315
|
+
tmux.run(["rename-session", "-t", s.id, sessionName(to, s.label)]);
|
|
5316
|
+
const right = tmux.run(["show-options", "-t", s.id, "-v", "status-right"]);
|
|
5317
|
+
if (right.code !== 0 || !right.stdout.startsWith(prefix)) continue;
|
|
5318
|
+
tmux.run(["set-option", "-t", s.id, "status-right", `[${to}] ${right.stdout.replace(/\n$/, "").slice(prefix.length)}`]);
|
|
5319
|
+
const length = Number(tmux.run(["show-options", "-t", s.id, "-v", "status-right-length"]).stdout.trim());
|
|
5320
|
+
if (length) tmux.run(["set-option", "-t", s.id, "status-right-length", String(length + to.length - from.length)]);
|
|
5321
|
+
} catch {
|
|
5322
|
+
}
|
|
5323
|
+
}
|
|
5324
|
+
return sessions.map((s) => s.label);
|
|
5325
|
+
}
|
|
4925
5326
|
|
|
4926
5327
|
// src/main.ts
|
|
4927
5328
|
async function main(argv, ctx) {
|
|
@@ -4982,6 +5383,8 @@ async function main(argv, ctx) {
|
|
|
4982
5383
|
return lsCommand(ctx, config, action.args);
|
|
4983
5384
|
case "rm":
|
|
4984
5385
|
return rmCommand(ctx, config, action.args, (account) => accountSessionLabels(ctx, account));
|
|
5386
|
+
case "rename":
|
|
5387
|
+
return renameCommand(ctx, config, action.args, { moveSessions: (from, to) => renameAccountSessions(ctx, from, to) });
|
|
4985
5388
|
case "tmux":
|
|
4986
5389
|
return tmuxCommand(ctx, config, action.args);
|
|
4987
5390
|
case "shell":
|