xclaude-cli 0.1.4 → 0.2.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 +7 -4
- package/dist/xclaude.js +986 -473
- package/package.json +1 -1
package/dist/xclaude.js
CHANGED
|
@@ -98,6 +98,7 @@ function resolvePaths(env) {
|
|
|
98
98
|
cache: path.join(xhome, "cache"),
|
|
99
99
|
shell: path.join(xhome, "shell"),
|
|
100
100
|
locks: path.join(xhome, "locks"),
|
|
101
|
+
inherit: path.join(xhome, "inherit"),
|
|
101
102
|
accounts: path.join(xhome, "accounts")
|
|
102
103
|
};
|
|
103
104
|
}
|
|
@@ -338,7 +339,7 @@ function processCtx(io) {
|
|
|
338
339
|
}
|
|
339
340
|
|
|
340
341
|
// src/commands/add.ts
|
|
341
|
-
import
|
|
342
|
+
import fs18 from "node:fs";
|
|
342
343
|
|
|
343
344
|
// src/claude/auth.ts
|
|
344
345
|
import { execFile } from "node:child_process";
|
|
@@ -447,6 +448,15 @@ function writeFileAtomic(path_, data, opts = {}) {
|
|
|
447
448
|
throw e;
|
|
448
449
|
}
|
|
449
450
|
}
|
|
451
|
+
function stampOf(st) {
|
|
452
|
+
return st ? { mtimeMs: st.mtimeMs, size: st.size, ino: st.ino } : null;
|
|
453
|
+
}
|
|
454
|
+
function writeIfUnchanged(file, text, seen) {
|
|
455
|
+
const st = lstatOrNull(file);
|
|
456
|
+
const unchanged = seen ? st !== null && st.mtimeMs === seen.mtimeMs && st.size === seen.size && st.ino === seen.ino : st === null;
|
|
457
|
+
if (unchanged) writeFileAtomic(file, text);
|
|
458
|
+
return unchanged;
|
|
459
|
+
}
|
|
450
460
|
function removeTree(p) {
|
|
451
461
|
const st = lstatOrNull(p);
|
|
452
462
|
if (!st) return;
|
|
@@ -553,6 +563,7 @@ var isStringArray = (v) => Array.isArray(v) && v.every((x) => typeof x === "stri
|
|
|
553
563
|
var isEffortOrNull = (v) => v === null || typeof v === "string" && isEffort(v);
|
|
554
564
|
function parseDefaults(raw, where) {
|
|
555
565
|
if (!isObject(raw)) throw new ConfigProblem(`${where.replace(/\.$/, "")} must be an object`);
|
|
566
|
+
field(raw, "inherit", where, true, isBool, "true or false");
|
|
556
567
|
return {
|
|
557
568
|
...raw,
|
|
558
569
|
model: field(raw, "model", where, null, isStringOrNull, "a model name or null"),
|
|
@@ -637,6 +648,9 @@ function identities(config) {
|
|
|
637
648
|
if (config.main.enabled) names.push(MAIN);
|
|
638
649
|
return names;
|
|
639
650
|
}
|
|
651
|
+
function inherits(config, name) {
|
|
652
|
+
return name !== MAIN && Object.hasOwn(config.accounts, name) && config.accounts[name].inherit === true;
|
|
653
|
+
}
|
|
640
654
|
function defaultsOf(config, name) {
|
|
641
655
|
if (name === MAIN) return config.main.enabled ? config.main : null;
|
|
642
656
|
return Object.hasOwn(config.accounts, name) ? config.accounts[name] : null;
|
|
@@ -698,7 +712,83 @@ function identityEnv(ctx, name) {
|
|
|
698
712
|
return buildEnv(ctx.env, { name, configDir: name === MAIN ? null : accountDir(ctx.paths, name) });
|
|
699
713
|
}
|
|
700
714
|
|
|
701
|
-
// src/
|
|
715
|
+
// src/core/state.ts
|
|
716
|
+
var MAX_DIRS = 500;
|
|
717
|
+
function emptyState() {
|
|
718
|
+
return {
|
|
719
|
+
installedVersion: null,
|
|
720
|
+
lastAccountByDir: {},
|
|
721
|
+
lastUsedAccount: null,
|
|
722
|
+
seenUnknownEntries: {},
|
|
723
|
+
listedPlugins: {},
|
|
724
|
+
turnedOnPlugins: {},
|
|
725
|
+
inheritNoted: []
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
function isRecord(v) {
|
|
729
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
730
|
+
}
|
|
731
|
+
function loadState(paths) {
|
|
732
|
+
let raw;
|
|
733
|
+
try {
|
|
734
|
+
raw = JSON.parse(readFileOrNull(paths.state) ?? "{}");
|
|
735
|
+
} catch {
|
|
736
|
+
return emptyState();
|
|
737
|
+
}
|
|
738
|
+
const state = emptyState();
|
|
739
|
+
if (!isRecord(raw)) return state;
|
|
740
|
+
if (typeof raw.installedVersion === "string") state.installedVersion = raw.installedVersion;
|
|
741
|
+
if (typeof raw.lastUsedAccount === "string") state.lastUsedAccount = raw.lastUsedAccount;
|
|
742
|
+
if (isRecord(raw.lastAccountByDir)) {
|
|
743
|
+
for (const [dir, name] of Object.entries(raw.lastAccountByDir)) {
|
|
744
|
+
if (typeof name === "string") state.lastAccountByDir[dir] = name;
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
if (isRecord(raw.seenUnknownEntries)) {
|
|
748
|
+
for (const [name, entries] of Object.entries(raw.seenUnknownEntries)) {
|
|
749
|
+
if (Array.isArray(entries)) state.seenUnknownEntries[name] = entries.filter((e) => typeof e === "string");
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
for (const key of ["listedPlugins", "turnedOnPlugins"]) {
|
|
753
|
+
const lists = raw[key];
|
|
754
|
+
if (!isRecord(lists)) continue;
|
|
755
|
+
for (const [name, ids] of Object.entries(lists)) {
|
|
756
|
+
if (Array.isArray(ids)) state[key][name] = ids.filter((id) => typeof id === "string");
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
if (Array.isArray(raw.inheritNoted)) state.inheritNoted = raw.inheritNoted.filter((name) => typeof name === "string");
|
|
760
|
+
return state;
|
|
761
|
+
}
|
|
762
|
+
function saveState(paths, state) {
|
|
763
|
+
ensureXHome(paths);
|
|
764
|
+
writeFileAtomic(paths.state, `${JSON.stringify(state, null, 2)}
|
|
765
|
+
`, { mode: 384 });
|
|
766
|
+
}
|
|
767
|
+
function updateState(paths, change) {
|
|
768
|
+
const state = loadState(paths);
|
|
769
|
+
change(state);
|
|
770
|
+
saveState(paths, state);
|
|
771
|
+
return state;
|
|
772
|
+
}
|
|
773
|
+
function recordUse(state, dir, account) {
|
|
774
|
+
delete state.lastAccountByDir[dir];
|
|
775
|
+
state.lastAccountByDir[dir] = account;
|
|
776
|
+
const dirs = Object.keys(state.lastAccountByDir);
|
|
777
|
+
for (const old of dirs.slice(0, Math.max(0, dirs.length - MAX_DIRS))) delete state.lastAccountByDir[old];
|
|
778
|
+
state.lastUsedAccount = account;
|
|
779
|
+
}
|
|
780
|
+
function forgetAccount(state, account) {
|
|
781
|
+
for (const [dir, name] of Object.entries(state.lastAccountByDir)) {
|
|
782
|
+
if (name === account) delete state.lastAccountByDir[dir];
|
|
783
|
+
}
|
|
784
|
+
if (state.lastUsedAccount === account) state.lastUsedAccount = null;
|
|
785
|
+
delete state.seenUnknownEntries[account];
|
|
786
|
+
delete state.listedPlugins[account];
|
|
787
|
+
delete state.turnedOnPlugins[account];
|
|
788
|
+
state.inheritNoted = state.inheritNoted.filter((name) => name !== account);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// src/link/inherit.ts
|
|
702
792
|
import fs10 from "node:fs";
|
|
703
793
|
import path6 from "node:path";
|
|
704
794
|
|
|
@@ -785,79 +875,270 @@ function takeOverStale(lockPath) {
|
|
|
785
875
|
}
|
|
786
876
|
}
|
|
787
877
|
|
|
788
|
-
// src/
|
|
789
|
-
var
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
878
|
+
// src/link/inherit.ts
|
|
879
|
+
var PER_ACCOUNT_KEYS = [
|
|
880
|
+
"env",
|
|
881
|
+
"enabledPlugins",
|
|
882
|
+
"apiKeyHelper",
|
|
883
|
+
"awsAuthRefresh",
|
|
884
|
+
"awsCredentialExport",
|
|
885
|
+
"gcpAuthRefresh",
|
|
886
|
+
"otelHeadersHelper",
|
|
887
|
+
"forceLoginMethod",
|
|
888
|
+
"forceLoginOrgUUID"
|
|
889
|
+
];
|
|
890
|
+
function isObject2(v) {
|
|
794
891
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
795
892
|
}
|
|
796
|
-
function
|
|
797
|
-
|
|
893
|
+
function canon(v) {
|
|
894
|
+
if (v === void 0) return "~";
|
|
895
|
+
if (Array.isArray(v)) return `[${v.map(canon).join(",")}]`;
|
|
896
|
+
if (isObject2(v)) {
|
|
897
|
+
return `{${Object.keys(v).sort().map((k) => `${JSON.stringify(k)}:${canon(v[k])}`).join(",")}}`;
|
|
898
|
+
}
|
|
899
|
+
return JSON.stringify(v);
|
|
900
|
+
}
|
|
901
|
+
function same(a, b) {
|
|
902
|
+
return canon(a) === canon(b);
|
|
903
|
+
}
|
|
904
|
+
function each(values, test) {
|
|
905
|
+
return values.every((v) => v === void 0 || test(v));
|
|
906
|
+
}
|
|
907
|
+
function merge3(base, main2, account, at, kept) {
|
|
908
|
+
if (same(main2, base)) return account;
|
|
909
|
+
if (same(account, base)) return main2;
|
|
910
|
+
if (same(account, main2)) return account;
|
|
911
|
+
const values = [base, main2, account];
|
|
912
|
+
if (each(values, Array.isArray)) {
|
|
913
|
+
const was = new Set((base ?? []).map(canon));
|
|
914
|
+
const now = new Set((main2 ?? []).map(canon));
|
|
915
|
+
const out = (account ?? []).filter((x) => now.has(canon(x)) || !was.has(canon(x)));
|
|
916
|
+
const have = new Set(out.map(canon));
|
|
917
|
+
for (const x of main2 ?? []) if (!was.has(canon(x)) && !have.has(canon(x))) out.push(x);
|
|
918
|
+
return out.length || main2 !== void 0 && account !== void 0 ? out : void 0;
|
|
919
|
+
}
|
|
920
|
+
if (each(values, isObject2)) {
|
|
921
|
+
const b = base ?? {};
|
|
922
|
+
const m = main2 ?? {};
|
|
923
|
+
const a = account ?? {};
|
|
924
|
+
const out = {};
|
|
925
|
+
for (const k of /* @__PURE__ */ new Set([...Object.keys(a), ...Object.keys(m), ...Object.keys(b)])) {
|
|
926
|
+
const v = merge3(b[k], m[k], a[k], at ? `${at}.${k}` : k, kept);
|
|
927
|
+
if (v !== void 0) out[k] = v;
|
|
928
|
+
}
|
|
929
|
+
return Object.keys(out).length || main2 !== void 0 && account !== void 0 ? out : void 0;
|
|
930
|
+
}
|
|
931
|
+
kept.push(at);
|
|
932
|
+
return account;
|
|
933
|
+
}
|
|
934
|
+
function inheritable(settings) {
|
|
935
|
+
return Object.fromEntries(Object.entries(settings).filter(([k]) => !PER_ACCOUNT_KEYS.includes(k)));
|
|
936
|
+
}
|
|
937
|
+
function mergeSettings(base, main2, account) {
|
|
938
|
+
const picked = inheritable(main2);
|
|
939
|
+
const kept = [];
|
|
940
|
+
const next = { ...account };
|
|
941
|
+
for (const k of /* @__PURE__ */ new Set([...Object.keys(picked), ...Object.keys(base ?? {})])) {
|
|
942
|
+
if (PER_ACCOUNT_KEYS.includes(k)) continue;
|
|
943
|
+
const v = merge3(base?.[k], picked[k], account[k], k, kept);
|
|
944
|
+
if (v === void 0) delete next[k];
|
|
945
|
+
else next[k] = v;
|
|
946
|
+
}
|
|
947
|
+
return { next, base: picked, kept };
|
|
948
|
+
}
|
|
949
|
+
function basePath(paths, account) {
|
|
950
|
+
return path6.join(paths.inherit, `${account}.json`);
|
|
951
|
+
}
|
|
952
|
+
function readBase(paths, account) {
|
|
798
953
|
try {
|
|
799
|
-
|
|
954
|
+
const v = JSON.parse(readFileOrNull(basePath(paths, account)) ?? "null");
|
|
955
|
+
return isObject2(v) ? v : void 0;
|
|
800
956
|
} catch {
|
|
801
|
-
return
|
|
957
|
+
return void 0;
|
|
802
958
|
}
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
959
|
+
}
|
|
960
|
+
function mainSettings(paths) {
|
|
961
|
+
let text;
|
|
962
|
+
try {
|
|
963
|
+
text = readFileOrNull(path6.join(paths.store, "settings.json"));
|
|
964
|
+
} catch (e) {
|
|
965
|
+
return { problem: `~/.claude/settings.json can't be read (${e.message})` };
|
|
966
|
+
}
|
|
967
|
+
if (text === null) return { settings: {} };
|
|
968
|
+
let v;
|
|
969
|
+
try {
|
|
970
|
+
v = JSON.parse(text);
|
|
971
|
+
} catch {
|
|
972
|
+
return { problem: "~/.claude/settings.json isn't valid JSON" };
|
|
973
|
+
}
|
|
974
|
+
return isObject2(v) ? { settings: v } : { problem: "~/.claude/settings.json isn't a JSON object" };
|
|
975
|
+
}
|
|
976
|
+
function planInherit(paths, account, main2) {
|
|
977
|
+
const dir = accountDir(paths, account);
|
|
978
|
+
if (!lstatOrNull(dir)?.isDirectory()) return null;
|
|
979
|
+
let theirs = main2;
|
|
980
|
+
if (!theirs) {
|
|
981
|
+
const read = mainSettings(paths);
|
|
982
|
+
if ("problem" in read) return read;
|
|
983
|
+
theirs = read.settings;
|
|
984
|
+
}
|
|
985
|
+
const file = path6.join(dir, "settings.json");
|
|
986
|
+
const st = lstatOrNull(file);
|
|
987
|
+
if (st && !st.isFile()) return null;
|
|
988
|
+
const original = st ? fs10.readFileSync(file, "utf8") : null;
|
|
989
|
+
let ours = {};
|
|
990
|
+
if (original !== null) {
|
|
991
|
+
try {
|
|
992
|
+
ours = JSON.parse(original);
|
|
993
|
+
} catch {
|
|
994
|
+
return null;
|
|
810
995
|
}
|
|
811
996
|
}
|
|
812
|
-
if (
|
|
813
|
-
|
|
814
|
-
|
|
997
|
+
if (!isObject2(ours)) return null;
|
|
998
|
+
const current = ours;
|
|
999
|
+
const stored = original === null ? void 0 : readBase(paths, account);
|
|
1000
|
+
const { next, base, kept } = mergeSettings(stored, theirs, current);
|
|
1001
|
+
const changed = [.../* @__PURE__ */ new Set([...Object.keys(current), ...Object.keys(next)])].filter((k) => !same(current[k], next[k]));
|
|
1002
|
+
const ending = original === null || original.endsWith("\n") ? "\n" : "";
|
|
1003
|
+
return {
|
|
1004
|
+
file,
|
|
1005
|
+
next,
|
|
1006
|
+
text: changed.length ? `${JSON.stringify(next, null, 2)}${ending}` : null,
|
|
1007
|
+
changed,
|
|
1008
|
+
kept,
|
|
1009
|
+
base,
|
|
1010
|
+
baseChanged: !same(stored ?? {}, base),
|
|
1011
|
+
first: stored === void 0,
|
|
1012
|
+
seen: stampOf(st)
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
function saveBase(paths, account, base) {
|
|
1016
|
+
mkdirPrivate(paths.inherit);
|
|
1017
|
+
writeFileAtomic(basePath(paths, account), `${JSON.stringify(base, null, 2)}
|
|
1018
|
+
`, { mode: 384 });
|
|
1019
|
+
}
|
|
1020
|
+
function forgetInherit(paths, account) {
|
|
1021
|
+
removeTree(basePath(paths, account));
|
|
1022
|
+
}
|
|
1023
|
+
function listed(names) {
|
|
1024
|
+
return names.length > 6 ? `${names.slice(0, 6).join(", ")} and ${names.length - 6} more` : names.join(", ");
|
|
1025
|
+
}
|
|
1026
|
+
function logMerge(account, plan, log) {
|
|
1027
|
+
if (plan.changed.length) {
|
|
1028
|
+
if (plan.first) {
|
|
1029
|
+
log(`xclaude: ${account} takes ~/.claude/settings.json now (${listed(plan.changed)}); its own changes stay. Turn that off with: xclaude set ${account} --inherit off`);
|
|
1030
|
+
} else log(`xclaude: ${account} took ~/.claude/settings.json's changes to ${listed(plan.changed)}`);
|
|
1031
|
+
}
|
|
1032
|
+
if (plan.kept.length) log(`xclaude: ${account} keeps its own ${listed(plan.kept)}, which ~/.claude/settings.json sets differently`);
|
|
1033
|
+
}
|
|
1034
|
+
function inheritSettings(paths, account, opts) {
|
|
1035
|
+
const result = { changed: [], kept: [], deferred: false };
|
|
1036
|
+
try {
|
|
1037
|
+
const first = planInherit(paths, account);
|
|
1038
|
+
if (first && "problem" in first) {
|
|
1039
|
+
opts.log(`xclaude: ${first.problem}, so ${account} keeps its settings as they are`);
|
|
1040
|
+
return result;
|
|
815
1041
|
}
|
|
1042
|
+
if (!first || first.text === null && !first.baseChanged) return result;
|
|
1043
|
+
const lock = DirLock.acquire(path6.join(paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1044
|
+
if (!lock) return { ...result, deferred: true };
|
|
1045
|
+
try {
|
|
1046
|
+
const plan = planInherit(paths, account);
|
|
1047
|
+
if (!plan || "problem" in plan) return result;
|
|
1048
|
+
if (plan.text !== null && !writeIfUnchanged(plan.file, plan.text, plan.seen)) return { ...result, deferred: true };
|
|
1049
|
+
if (plan.text !== null || plan.baseChanged) saveBase(paths, account, plan.base);
|
|
1050
|
+
logMerge(account, plan, opts.log);
|
|
1051
|
+
return { changed: plan.changed, kept: plan.kept, deferred: false };
|
|
1052
|
+
} finally {
|
|
1053
|
+
lock.release();
|
|
1054
|
+
}
|
|
1055
|
+
} catch (e) {
|
|
1056
|
+
opts.log(`xclaude: couldn't merge ~/.claude/settings.json into ${account} (${e.message})`);
|
|
1057
|
+
return result;
|
|
816
1058
|
}
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
1059
|
+
}
|
|
1060
|
+
function stopInheriting(paths, account, opts) {
|
|
1061
|
+
const lock = DirLock.acquire(path6.join(paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1062
|
+
if (!lock) return false;
|
|
1063
|
+
try {
|
|
1064
|
+
const plan = planInherit(paths, account, {});
|
|
1065
|
+
if (plan && !("problem" in plan) && plan.text !== null) {
|
|
1066
|
+
if (!writeIfUnchanged(plan.file, plan.text, plan.seen)) return false;
|
|
1067
|
+
opts.log(`xclaude: took back what ${account} had from ~/.claude/settings.json: ${listed(plan.changed)}`);
|
|
820
1068
|
}
|
|
1069
|
+
forgetInherit(paths, account);
|
|
1070
|
+
return true;
|
|
1071
|
+
} finally {
|
|
1072
|
+
lock.release();
|
|
821
1073
|
}
|
|
822
|
-
return state;
|
|
823
1074
|
}
|
|
824
|
-
function
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
`, { mode: 384 });
|
|
1075
|
+
function resetInherit(paths, account, opts) {
|
|
1076
|
+
forgetInherit(paths, account);
|
|
1077
|
+
return inheritSettings(paths, account, opts);
|
|
828
1078
|
}
|
|
829
|
-
function
|
|
830
|
-
const
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
return
|
|
1079
|
+
function inheritStatus(paths, account, main2) {
|
|
1080
|
+
const plan = planInherit(paths, account, main2);
|
|
1081
|
+
if (!plan || "problem" in plan) return null;
|
|
1082
|
+
const picked = inheritable(main2);
|
|
1083
|
+
return { pending: plan.changed, own: Object.keys(picked).filter((k) => !same(plan.next[k], picked[k])) };
|
|
834
1084
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
1085
|
+
|
|
1086
|
+
// src/link/mcp.ts
|
|
1087
|
+
import fs11 from "node:fs";
|
|
1088
|
+
import path7 from "node:path";
|
|
1089
|
+
function isRecord2(v) {
|
|
1090
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
841
1091
|
}
|
|
842
|
-
function
|
|
843
|
-
|
|
844
|
-
|
|
1092
|
+
function serversIn(file) {
|
|
1093
|
+
try {
|
|
1094
|
+
const json = JSON.parse(readFileOrNull(file) ?? "{}");
|
|
1095
|
+
if (!isRecord2(json)) return null;
|
|
1096
|
+
return isRecord2(json.mcpServers) ? json.mcpServers : {};
|
|
1097
|
+
} catch {
|
|
1098
|
+
return null;
|
|
845
1099
|
}
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
1100
|
+
}
|
|
1101
|
+
function mcpFile(paths, account) {
|
|
1102
|
+
return path7.join(paths.inherit, `${account}.mcp.json`);
|
|
1103
|
+
}
|
|
1104
|
+
function mcpServersFor(paths, account) {
|
|
1105
|
+
const main2 = serversIn(path7.join(paths.home, ".claude.json"));
|
|
1106
|
+
const own = serversIn(path7.join(accountDir(paths, account), ".claude.json"));
|
|
1107
|
+
if (!main2 || !own) return null;
|
|
1108
|
+
return Object.fromEntries(Object.entries(main2).filter(([name]) => !Object.hasOwn(own, name)));
|
|
1109
|
+
}
|
|
1110
|
+
function prepareMcp(paths, account) {
|
|
1111
|
+
const file = mcpFile(paths, account);
|
|
1112
|
+
const servers = mcpServersFor(paths, account);
|
|
1113
|
+
if (!servers) return fs11.existsSync(file) ? file : null;
|
|
1114
|
+
if (!Object.keys(servers).length) {
|
|
1115
|
+
removeTree(file);
|
|
1116
|
+
return null;
|
|
1117
|
+
}
|
|
1118
|
+
const text = `${JSON.stringify({ mcpServers: servers }, null, 2)}
|
|
1119
|
+
`;
|
|
1120
|
+
if (readFileOrNull(file) !== text) {
|
|
1121
|
+
mkdirPrivate(paths.inherit);
|
|
1122
|
+
writeFileAtomic(file, text, { mode: 384 });
|
|
1123
|
+
}
|
|
1124
|
+
return file;
|
|
1125
|
+
}
|
|
1126
|
+
function forgetMcp(paths, account) {
|
|
1127
|
+
removeTree(mcpFile(paths, account));
|
|
849
1128
|
}
|
|
850
1129
|
|
|
851
1130
|
// src/link/plugins.ts
|
|
852
|
-
|
|
1131
|
+
import fs12 from "node:fs";
|
|
1132
|
+
import path8 from "node:path";
|
|
1133
|
+
function isRecord3(v) {
|
|
853
1134
|
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
854
1135
|
}
|
|
855
1136
|
function userPlugins(paths) {
|
|
856
|
-
const dir = realpathOrNull(
|
|
1137
|
+
const dir = realpathOrNull(path8.join(paths.store, "plugins"));
|
|
857
1138
|
if (!dir) return null;
|
|
858
1139
|
let text;
|
|
859
1140
|
try {
|
|
860
|
-
text = readFileOrNull(
|
|
1141
|
+
text = readFileOrNull(path8.join(dir, "installed_plugins.json"));
|
|
861
1142
|
} catch {
|
|
862
1143
|
return null;
|
|
863
1144
|
}
|
|
@@ -868,20 +1149,29 @@ function userPlugins(paths) {
|
|
|
868
1149
|
} catch {
|
|
869
1150
|
return null;
|
|
870
1151
|
}
|
|
871
|
-
if (!
|
|
872
|
-
return Object.entries(json.plugins).filter(([, records]) => Array.isArray(records) && records.some((r) =>
|
|
1152
|
+
if (!isRecord3(json) || !isRecord3(json.plugins)) return null;
|
|
1153
|
+
return Object.entries(json.plugins).filter(([, records]) => Array.isArray(records) && records.some((r) => isRecord3(r) && r.scope === "user")).map(([id]) => id).sort();
|
|
873
1154
|
}
|
|
874
|
-
function
|
|
1155
|
+
function mainPlugins(paths) {
|
|
1156
|
+
const read = mainSettings(paths);
|
|
1157
|
+
if ("problem" in read) return null;
|
|
1158
|
+
const enabled = read.settings.enabledPlugins;
|
|
1159
|
+
if (!isRecord3(enabled)) return /* @__PURE__ */ new Set();
|
|
1160
|
+
return new Set(
|
|
1161
|
+
Object.entries(enabled).filter(([, on]) => on === true).map(([id]) => id.toLowerCase())
|
|
1162
|
+
);
|
|
1163
|
+
}
|
|
1164
|
+
function planPlugins(paths, account, tracked, follow = null) {
|
|
875
1165
|
const dir = accountDir(paths, account);
|
|
876
1166
|
if (!lstatOrNull(dir)?.isDirectory()) return null;
|
|
877
|
-
const store = realpathOrNull(
|
|
878
|
-
if (!store || realpathOrNull(
|
|
1167
|
+
const store = realpathOrNull(path8.join(paths.store, "plugins"));
|
|
1168
|
+
if (!store || realpathOrNull(path8.join(dir, "plugins")) !== store) return null;
|
|
879
1169
|
const installed = userPlugins(paths);
|
|
880
1170
|
if (!installed) return null;
|
|
881
|
-
const file =
|
|
1171
|
+
const file = path8.join(dir, "settings.json");
|
|
882
1172
|
const st = lstatOrNull(file);
|
|
883
1173
|
if (st && !st.isFile()) return null;
|
|
884
|
-
const original = st ?
|
|
1174
|
+
const original = st ? fs12.readFileSync(file, "utf8") : null;
|
|
885
1175
|
let settings = {};
|
|
886
1176
|
if (original !== null) {
|
|
887
1177
|
try {
|
|
@@ -890,88 +1180,336 @@ function planPlugins(paths, account, tracked) {
|
|
|
890
1180
|
return null;
|
|
891
1181
|
}
|
|
892
1182
|
}
|
|
893
|
-
if (!
|
|
1183
|
+
if (!isRecord3(settings)) return null;
|
|
894
1184
|
const current = settings.enabledPlugins ?? {};
|
|
895
|
-
if (!
|
|
1185
|
+
if (!isRecord3(current)) return null;
|
|
1186
|
+
const wanted = (id) => follow?.has(id.toLowerCase()) === true;
|
|
896
1187
|
const named = new Set(Object.keys(current).map((id) => id.toLowerCase()));
|
|
897
1188
|
const add = installed.filter((id) => !named.has(id.toLowerCase()));
|
|
898
|
-
const
|
|
1189
|
+
const next = { ...current };
|
|
1190
|
+
const off = [];
|
|
1191
|
+
const on = [];
|
|
1192
|
+
const turnOn = [];
|
|
1193
|
+
const turnOff = [];
|
|
899
1194
|
const drop = [];
|
|
900
|
-
for (const
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
1195
|
+
for (const [ids, value] of [
|
|
1196
|
+
[tracked.off, false],
|
|
1197
|
+
[tracked.on, true]
|
|
1198
|
+
]) {
|
|
1199
|
+
for (const id of ids) {
|
|
1200
|
+
if (current[id] !== value) continue;
|
|
1201
|
+
if (!installed.includes(id)) {
|
|
1202
|
+
drop.push(id);
|
|
1203
|
+
delete next[id];
|
|
1204
|
+
} else if (follow && wanted(id) !== value) {
|
|
1205
|
+
next[id] = !value;
|
|
1206
|
+
(value ? turnOff : turnOn).push(id);
|
|
1207
|
+
(value ? off : on).push(id);
|
|
1208
|
+
} else (value ? on : off).push(id);
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
for (const id of add) {
|
|
1212
|
+
next[id] = wanted(id);
|
|
1213
|
+
if (wanted(id)) {
|
|
1214
|
+
on.push(id);
|
|
1215
|
+
turnOn.push(id);
|
|
1216
|
+
} else off.push(id);
|
|
1217
|
+
}
|
|
1218
|
+
let text = null;
|
|
1219
|
+
if (add.length || drop.length || turnOn.length || turnOff.length) {
|
|
1220
|
+
const ending = original === null || original.endsWith("\n") ? "\n" : "";
|
|
1221
|
+
text = `${JSON.stringify({ ...settings, enabledPlugins: next }, null, 2)}${ending}`;
|
|
1222
|
+
}
|
|
1223
|
+
return {
|
|
1224
|
+
file,
|
|
1225
|
+
add,
|
|
1226
|
+
turnOn,
|
|
1227
|
+
turnOff,
|
|
1228
|
+
drop,
|
|
1229
|
+
tracked: { off: off.sort(), on: on.sort() },
|
|
1230
|
+
text,
|
|
1231
|
+
seen: stampOf(st)
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
function writePlan(plan) {
|
|
1235
|
+
return plan.text === null || writeIfUnchanged(plan.file, plan.text, plan.seen);
|
|
1236
|
+
}
|
|
1237
|
+
function changesNothing(plan, tracked) {
|
|
1238
|
+
const same2 = (a, b) => a.join("\n") === [...b].sort().join("\n");
|
|
1239
|
+
return plan.text === null && same2(plan.tracked.off, tracked.off) && same2(plan.tracked.on, tracked.on);
|
|
1240
|
+
}
|
|
1241
|
+
function trackedOf(paths, account) {
|
|
1242
|
+
const state = loadState(paths);
|
|
1243
|
+
return { off: state.listedPlugins[account] ?? [], on: state.turnedOnPlugins[account] ?? [] };
|
|
1244
|
+
}
|
|
1245
|
+
function listPlugins(paths, account, opts) {
|
|
1246
|
+
const result = { added: [], dropped: [], deferred: false };
|
|
1247
|
+
try {
|
|
1248
|
+
const follow = opts.turnOffOwn ? /* @__PURE__ */ new Set() : opts.follow ? mainPlugins(paths) : null;
|
|
1249
|
+
const before = trackedOf(paths, account);
|
|
1250
|
+
const first = planPlugins(paths, account, before, follow);
|
|
1251
|
+
if (!first || changesNothing(first, before)) return result;
|
|
1252
|
+
const lock = DirLock.acquire(path8.join(paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1253
|
+
if (!lock) return { ...result, deferred: true };
|
|
1254
|
+
try {
|
|
1255
|
+
const tracked = trackedOf(paths, account);
|
|
1256
|
+
const plan = planPlugins(paths, account, tracked, follow);
|
|
1257
|
+
if (!plan || changesNothing(plan, tracked)) return result;
|
|
1258
|
+
if (!writePlan(plan)) return { ...result, deferred: true };
|
|
1259
|
+
updateState(paths, (state) => {
|
|
1260
|
+
for (const [key, ids] of [
|
|
1261
|
+
["listedPlugins", plan.tracked.off],
|
|
1262
|
+
["turnedOnPlugins", plan.tracked.on]
|
|
1263
|
+
]) {
|
|
1264
|
+
if (ids.length) state[key][account] = ids;
|
|
1265
|
+
else delete state[key][account];
|
|
1266
|
+
}
|
|
1267
|
+
});
|
|
1268
|
+
const off = plan.add.filter((id) => !plan.turnOn.includes(id));
|
|
1269
|
+
if (off.length === 1) {
|
|
1270
|
+
opts.log(`xclaude: ${off[0]} is installed under another login; ${account}'s /plugin lists it now, turned off`);
|
|
1271
|
+
} else if (off.length) {
|
|
1272
|
+
opts.log(`xclaude: ${off.length} plugins installed under other logins are in ${account}'s /plugin now, turned off: ${off.join(", ")}`);
|
|
1273
|
+
}
|
|
1274
|
+
if (plan.turnOn.length) opts.log(`xclaude: ${account} turned on what ~/.claude has on: ${plan.turnOn.join(", ")}`);
|
|
1275
|
+
if (plan.turnOff.length) opts.log(`xclaude: ${account} turned off ${plan.turnOff.join(", ")}, which ${opts.turnOffOwn ? "it had from ~/.claude" : "~/.claude has off now"}`);
|
|
1276
|
+
return { added: plan.add, dropped: plan.drop, deferred: false };
|
|
1277
|
+
} finally {
|
|
1278
|
+
lock.release();
|
|
1279
|
+
}
|
|
1280
|
+
} catch (e) {
|
|
1281
|
+
opts.log(`xclaude: couldn't list other logins' plugins for ${account} (${e.message})`);
|
|
1282
|
+
return result;
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
// src/launch/picker.ts
|
|
1287
|
+
import fs13 from "node:fs";
|
|
1288
|
+
import tty2 from "node:tty";
|
|
1289
|
+
function parseKeys(chunk) {
|
|
1290
|
+
const keys = [];
|
|
1291
|
+
let i = 0;
|
|
1292
|
+
while (i < chunk.length) {
|
|
1293
|
+
const rest = chunk.slice(i);
|
|
1294
|
+
if (rest.startsWith("\x1B[A") || rest.startsWith("\x1BOA")) {
|
|
1295
|
+
keys.push("up");
|
|
1296
|
+
i += 3;
|
|
1297
|
+
} else if (rest.startsWith("\x1B[B") || rest.startsWith("\x1BOB")) {
|
|
1298
|
+
keys.push("down");
|
|
1299
|
+
i += 3;
|
|
1300
|
+
} else if (rest.startsWith("\x1B[")) {
|
|
1301
|
+
let j = 2;
|
|
1302
|
+
while (j < rest.length && !/[@-~]/.test(rest[j])) j++;
|
|
1303
|
+
keys.push(null);
|
|
1304
|
+
i += j + 1;
|
|
1305
|
+
} else {
|
|
1306
|
+
const c = rest[0];
|
|
1307
|
+
if (c === "\x1B") keys.push(rest.length === 1 ? "cancel" : null);
|
|
1308
|
+
else if (c === "") keys.push("cancel");
|
|
1309
|
+
else if (c === "\r" || c === "\n") keys.push("enter");
|
|
1310
|
+
else if (c === "k") keys.push("up");
|
|
1311
|
+
else if (c === "j") keys.push("down");
|
|
1312
|
+
else if (c >= "1" && c <= "9") keys.push({ jump: Number(c) - 1 });
|
|
1313
|
+
else keys.push(null);
|
|
1314
|
+
i++;
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
return keys;
|
|
1318
|
+
}
|
|
1319
|
+
function step(index, count, key) {
|
|
1320
|
+
if (key === "up") return { index: (index - 1 + count) % count };
|
|
1321
|
+
if (key === "down") return { index: (index + 1) % count };
|
|
1322
|
+
if (key === "enter") return { index, done: "chosen" };
|
|
1323
|
+
if (key === "cancel") return { index, done: "cancelled" };
|
|
1324
|
+
if (key && typeof key === "object" && key.jump < count) return { index: key.jump };
|
|
1325
|
+
return { index };
|
|
1326
|
+
}
|
|
1327
|
+
var dim = (s) => `\x1B[2m${s}\x1B[22m`;
|
|
1328
|
+
var bold = (s) => `\x1B[1m${s}\x1B[22m`;
|
|
1329
|
+
function fit(text, width) {
|
|
1330
|
+
return width > 0 && text.length > width ? `${text.slice(0, Math.max(0, width - 1))}\u2026` : text;
|
|
1331
|
+
}
|
|
1332
|
+
function render(title, items, index, width = 0) {
|
|
1333
|
+
const labelWidth = Math.max(...items.map((it) => it.label.length));
|
|
1334
|
+
const lines = [bold(fit(title, width))];
|
|
1335
|
+
items.forEach((it, i) => {
|
|
1336
|
+
const marker = i === index ? "\u276F" : " ";
|
|
1337
|
+
const num = i < 9 ? String(i + 1) : " ";
|
|
1338
|
+
const label = it.label.padEnd(labelWidth);
|
|
1339
|
+
const text = fit(`${marker} ${num} ${label}${it.detail ? ` ${it.detail}` : ""}`, width);
|
|
1340
|
+
const head = text.slice(0, 5 + labelWidth);
|
|
1341
|
+
const tail = text.slice(5 + labelWidth);
|
|
1342
|
+
lines.push(i === index ? `${bold(head)}${dim(tail)}` : `${head}${dim(tail)}`);
|
|
1343
|
+
});
|
|
1344
|
+
lines.push(dim(fit("\u2191/\u2193 or j/k to move \xB7 1\u20139 to jump \xB7 Enter to choose \xB7 Esc to cancel", width)));
|
|
1345
|
+
return lines;
|
|
1346
|
+
}
|
|
1347
|
+
function preselect(names, state, cwd) {
|
|
1348
|
+
for (const candidate of [state.lastAccountByDir[cwd], state.lastUsedAccount]) {
|
|
1349
|
+
if (candidate && names.includes(candidate)) return names.indexOf(candidate);
|
|
1350
|
+
}
|
|
1351
|
+
return 0;
|
|
1352
|
+
}
|
|
1353
|
+
function openKeyboard() {
|
|
1354
|
+
let fd;
|
|
1355
|
+
try {
|
|
1356
|
+
fd = fs13.openSync("/dev/tty", "r");
|
|
1357
|
+
} catch {
|
|
1358
|
+
throw new UsageError("account required: there's no terminal to show the picker on (xclaude <account> [claude args\u2026])");
|
|
1359
|
+
}
|
|
1360
|
+
const input = new tty2.ReadStream(fd);
|
|
1361
|
+
let columns = 0;
|
|
1362
|
+
try {
|
|
1363
|
+
const size = [];
|
|
1364
|
+
const handle = input._handle;
|
|
1365
|
+
if (handle?.getWindowSize?.(size) === 0) columns = size[0] ?? 0;
|
|
1366
|
+
} catch {
|
|
1367
|
+
}
|
|
1368
|
+
return {
|
|
1369
|
+
input,
|
|
1370
|
+
columns,
|
|
1371
|
+
close: () => {
|
|
1372
|
+
input.pause();
|
|
1373
|
+
input.unref();
|
|
1374
|
+
}
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
function pick(title, items, initial) {
|
|
1378
|
+
const keyboard = openKeyboard();
|
|
1379
|
+
const { input } = keyboard;
|
|
1380
|
+
const write = (s) => fs13.writeSync(2, s);
|
|
1381
|
+
let index = Math.min(Math.max(initial, 0), items.length - 1);
|
|
1382
|
+
let drawn = 0;
|
|
1383
|
+
const draw = () => {
|
|
1384
|
+
const lines = render(title, items, index, keyboard.columns);
|
|
1385
|
+
write(`${drawn ? `\r\x1B[${drawn}A` : ""}\x1B[J${lines.join("\n")}
|
|
1386
|
+
`);
|
|
1387
|
+
drawn = lines.length;
|
|
1388
|
+
};
|
|
1389
|
+
return new Promise((resolve, reject) => {
|
|
1390
|
+
const finish = (result) => {
|
|
1391
|
+
input.off("data", onData);
|
|
1392
|
+
try {
|
|
1393
|
+
input.setRawMode(false);
|
|
1394
|
+
} finally {
|
|
1395
|
+
keyboard.close();
|
|
1396
|
+
write(`\r\x1B[${drawn}A\x1B[J\x1B[?25h`);
|
|
1397
|
+
}
|
|
1398
|
+
if (result === "chosen") resolve(index);
|
|
1399
|
+
else reject(new Cancelled());
|
|
1400
|
+
};
|
|
1401
|
+
const onData = (chunk) => {
|
|
1402
|
+
for (const key of parseKeys(chunk)) {
|
|
1403
|
+
const next = step(index, items.length, key);
|
|
1404
|
+
index = next.index;
|
|
1405
|
+
if (next.done) {
|
|
1406
|
+
finish(next.done);
|
|
1407
|
+
return;
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
draw();
|
|
1411
|
+
};
|
|
1412
|
+
input.setRawMode(true);
|
|
1413
|
+
input.setEncoding("utf8");
|
|
1414
|
+
input.on("data", onData);
|
|
1415
|
+
input.resume();
|
|
1416
|
+
write("\x1B[?25l");
|
|
1417
|
+
draw();
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// src/launch/inherit-question.ts
|
|
1422
|
+
function setupPreview(paths, account) {
|
|
1423
|
+
try {
|
|
1424
|
+
return previewLines(paths, account);
|
|
1425
|
+
} catch {
|
|
1426
|
+
return [];
|
|
904
1427
|
}
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
const
|
|
911
|
-
|
|
1428
|
+
}
|
|
1429
|
+
function previewLines(paths, account) {
|
|
1430
|
+
const lines = [];
|
|
1431
|
+
const main2 = mainSettings(paths);
|
|
1432
|
+
if ("settings" in main2) {
|
|
1433
|
+
const keys = inheritStatus(paths, account, main2.settings)?.pending ?? Object.keys(inheritable(main2.settings));
|
|
1434
|
+
if (keys.length) lines.push(` settings ${listed(keys)}`);
|
|
912
1435
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
};
|
|
1436
|
+
const on = mainPlugins(paths);
|
|
1437
|
+
if (on?.size) {
|
|
1438
|
+
const plan = planPlugins(paths, account, trackedOf(paths, account), on);
|
|
1439
|
+
const ids = plan ? plan.turnOn : (userPlugins(paths) ?? []).filter((id) => on.has(id.toLowerCase()));
|
|
1440
|
+
if (ids.length) lines.push(` plugins turns on ${listed(ids)}`);
|
|
1441
|
+
}
|
|
1442
|
+
const servers = Object.keys(mcpServersFor(paths, account) ?? {});
|
|
1443
|
+
if (servers.length) lines.push(` MCP ${listed(servers)}`);
|
|
1444
|
+
return lines;
|
|
921
1445
|
}
|
|
922
|
-
function
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
1446
|
+
function noteOnce(ctx, account) {
|
|
1447
|
+
try {
|
|
1448
|
+
if (loadState(ctx.paths).inheritNoted.includes(account)) return;
|
|
1449
|
+
updateState(ctx.paths, (state) => {
|
|
1450
|
+
state.inheritNoted.push(account);
|
|
1451
|
+
});
|
|
1452
|
+
} catch {
|
|
1453
|
+
}
|
|
1454
|
+
ctx.io.err(
|
|
1455
|
+
`xclaude: ${account} doesn't take ~/.claude's settings, plugins and MCP servers until you choose: launch it in a terminal, or run xclaude set ${account} --inherit on (or off)
|
|
1456
|
+
`
|
|
1457
|
+
);
|
|
928
1458
|
}
|
|
929
|
-
function
|
|
930
|
-
|
|
1459
|
+
async function askInherit(ctx, account) {
|
|
1460
|
+
const preview = setupPreview(ctx.paths, account);
|
|
1461
|
+
if (!preview.length) return void 0;
|
|
1462
|
+
if (!ctx.tty.stdin || !ctx.tty.stderr) {
|
|
1463
|
+
noteOnce(ctx, account);
|
|
1464
|
+
return void 0;
|
|
1465
|
+
}
|
|
1466
|
+
ctx.io.err(`xclaude: ${account} can take what you set up for plain claude in ~/.claude:
|
|
1467
|
+
${preview.join("\n")}
|
|
1468
|
+
`);
|
|
1469
|
+
const choice = await pick(
|
|
1470
|
+
`Take it into ${account}?`,
|
|
1471
|
+
[
|
|
1472
|
+
{ label: "Yes", detail: `now and at every launch; what ${account} changes itself stays` },
|
|
1473
|
+
{ label: "No", detail: `leave ${account} as it is (later: xclaude set ${account} --inherit on)` }
|
|
1474
|
+
],
|
|
1475
|
+
0
|
|
1476
|
+
);
|
|
1477
|
+
if (choice === 0) return true;
|
|
1478
|
+
ctx.io.err(`xclaude: ${account} stays as it is; take ~/.claude's setup later with: xclaude set ${account} --inherit on
|
|
1479
|
+
`);
|
|
1480
|
+
return false;
|
|
931
1481
|
}
|
|
932
|
-
function
|
|
933
|
-
|
|
1482
|
+
async function settleInherit(ctx, config, account) {
|
|
1483
|
+
if (config.accounts[account]?.inherit !== void 0) return config;
|
|
1484
|
+
const take = await askInherit(ctx, account);
|
|
1485
|
+
if (take === void 0) return config;
|
|
934
1486
|
try {
|
|
935
|
-
const
|
|
936
|
-
const
|
|
937
|
-
if (!
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
const tracked = loadState(paths).listedPlugins[account] ?? [];
|
|
942
|
-
const plan = planPlugins(paths, account, tracked);
|
|
943
|
-
if (!plan || changesNothing(plan, tracked)) return result;
|
|
944
|
-
if (!writePlan(plan)) return { ...result, deferred: true };
|
|
945
|
-
updateState(paths, (state) => {
|
|
946
|
-
if (plan.tracked.length) state.listedPlugins[account] = plan.tracked;
|
|
947
|
-
else delete state.listedPlugins[account];
|
|
948
|
-
});
|
|
949
|
-
if (plan.add.length === 1) {
|
|
950
|
-
opts.log(`xclaude: ${plan.add[0]} is installed under another login; ${account}'s /plugin lists it now, turned off`);
|
|
951
|
-
} else if (plan.add.length) {
|
|
952
|
-
opts.log(`xclaude: ${plan.add.length} plugins installed under other logins are in ${account}'s /plugin now, turned off: ${plan.add.join(", ")}`);
|
|
953
|
-
}
|
|
954
|
-
return { added: plan.add, dropped: plan.drop, deferred: false };
|
|
955
|
-
} finally {
|
|
956
|
-
lock.release();
|
|
957
|
-
}
|
|
1487
|
+
const fresh = loadConfig(ctx.paths, { create: false }).config;
|
|
1488
|
+
const defaults = fresh.accounts[account];
|
|
1489
|
+
if (!defaults) return config;
|
|
1490
|
+
defaults.inherit = take;
|
|
1491
|
+
saveConfig(ctx.paths, fresh);
|
|
1492
|
+
return fresh;
|
|
958
1493
|
} catch (e) {
|
|
959
|
-
|
|
960
|
-
|
|
1494
|
+
ctx.io.err(`xclaude: couldn't save ${account}'s answer (${e.message}); the next launch asks again
|
|
1495
|
+
`);
|
|
1496
|
+
const once = structuredClone(config);
|
|
1497
|
+
once.accounts[account].inherit = take;
|
|
1498
|
+
return once;
|
|
961
1499
|
}
|
|
962
1500
|
}
|
|
963
1501
|
|
|
964
1502
|
// src/link/repair.ts
|
|
965
|
-
import
|
|
966
|
-
import
|
|
1503
|
+
import fs17 from "node:fs";
|
|
1504
|
+
import path12 from "node:path";
|
|
967
1505
|
|
|
968
1506
|
// src/link/history.ts
|
|
969
|
-
import
|
|
970
|
-
import
|
|
1507
|
+
import fs15 from "node:fs";
|
|
1508
|
+
import path10 from "node:path";
|
|
971
1509
|
|
|
972
1510
|
// src/link/merge.ts
|
|
973
|
-
import
|
|
974
|
-
import
|
|
1511
|
+
import fs14 from "node:fs";
|
|
1512
|
+
import path9 from "node:path";
|
|
975
1513
|
var counter2 = 0;
|
|
976
1514
|
function asideName(entry) {
|
|
977
1515
|
return `.xclaude-merge-${entry}-${stamp()}-${process.pid}-${counter2++}`;
|
|
@@ -996,23 +1534,23 @@ function remember(rec, c) {
|
|
|
996
1534
|
rec.byPath.set(c.path, c);
|
|
997
1535
|
}
|
|
998
1536
|
function requireRealDir(dir) {
|
|
999
|
-
const st =
|
|
1537
|
+
const st = fs14.lstatSync(dir);
|
|
1000
1538
|
if (!st.isDirectory()) throw new Error(`${dir} isn't a real directory; it won't be read through`);
|
|
1001
1539
|
return st;
|
|
1002
1540
|
}
|
|
1003
1541
|
function linkText(src, root) {
|
|
1004
|
-
const text =
|
|
1005
|
-
if (
|
|
1006
|
-
const resolved =
|
|
1007
|
-
return resolved === root || resolved.startsWith(`${root}${
|
|
1542
|
+
const text = fs14.readlinkSync(src);
|
|
1543
|
+
if (path9.isAbsolute(text)) return text;
|
|
1544
|
+
const resolved = path9.resolve(path9.dirname(src), text);
|
|
1545
|
+
return resolved === root || resolved.startsWith(`${root}${path9.sep}`) ? text : resolved;
|
|
1008
1546
|
}
|
|
1009
1547
|
function identicalEntry(src, srcStat, dst, root) {
|
|
1010
1548
|
const dstStat = lstatOrNull(dst);
|
|
1011
1549
|
if (!dstStat) return false;
|
|
1012
1550
|
if (srcStat.isSymbolicLink()) {
|
|
1013
1551
|
if (!dstStat.isSymbolicLink()) return false;
|
|
1014
|
-
const there =
|
|
1015
|
-
return there === linkText(src, root) || there ===
|
|
1552
|
+
const there = fs14.readlinkSync(dst);
|
|
1553
|
+
return there === linkText(src, root) || there === fs14.readlinkSync(src);
|
|
1016
1554
|
}
|
|
1017
1555
|
return srcStat.isFile() && dstStat.isFile() && sameContent(src, dst);
|
|
1018
1556
|
}
|
|
@@ -1027,18 +1565,18 @@ function conflictName(dir, name, ctx) {
|
|
|
1027
1565
|
const base = `${name}.xclaude-conflict-${ctx.account}-${ctx.stamp}`;
|
|
1028
1566
|
for (let n = 1; ; n++) {
|
|
1029
1567
|
const candidate = n === 1 ? base : `${base}-${n}`;
|
|
1030
|
-
if (!lstatOrNull(
|
|
1568
|
+
if (!lstatOrNull(path9.join(dir, candidate))) return candidate;
|
|
1031
1569
|
}
|
|
1032
1570
|
}
|
|
1033
1571
|
var join = (rel, name) => rel ? `${rel}/${name}` : name;
|
|
1034
1572
|
function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
1035
1573
|
let fresh = 0;
|
|
1036
|
-
for (const d of
|
|
1574
|
+
for (const d of fs14.readdirSync(src, { withFileTypes: true })) {
|
|
1037
1575
|
ctx.refresh();
|
|
1038
|
-
const s =
|
|
1039
|
-
const t =
|
|
1576
|
+
const s = path9.join(src, d.name);
|
|
1577
|
+
const t = path9.join(dst, d.name);
|
|
1040
1578
|
const relName = join(rel, d.name);
|
|
1041
|
-
const st =
|
|
1579
|
+
const st = fs14.lstatSync(s);
|
|
1042
1580
|
const prior = rec.placed.get(relName);
|
|
1043
1581
|
if (st.isDirectory()) {
|
|
1044
1582
|
let target = t;
|
|
@@ -1046,14 +1584,14 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
1046
1584
|
target = prior.target;
|
|
1047
1585
|
} else {
|
|
1048
1586
|
try {
|
|
1049
|
-
|
|
1587
|
+
fs14.mkdirSync(t, { mode: st.mode & 4095 | 448 });
|
|
1050
1588
|
remember(rec, { path: t, kind: "dir" });
|
|
1051
1589
|
} catch (e) {
|
|
1052
1590
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1053
|
-
if (!
|
|
1591
|
+
if (!fs14.lstatSync(t).isDirectory()) {
|
|
1054
1592
|
const name2 = conflictName(dst, d.name, ctx);
|
|
1055
|
-
target =
|
|
1056
|
-
|
|
1593
|
+
target = path9.join(dst, name2);
|
|
1594
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
1057
1595
|
remember(rec, { path: target, kind: "dir" });
|
|
1058
1596
|
stats.conflicts.push(join(rel, name2));
|
|
1059
1597
|
}
|
|
@@ -1068,7 +1606,7 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
1068
1606
|
const place = (to) => {
|
|
1069
1607
|
if (st.isSymbolicLink()) {
|
|
1070
1608
|
const link = linkText(s, root);
|
|
1071
|
-
|
|
1609
|
+
fs14.symlinkSync(link, to);
|
|
1072
1610
|
remember(rec, { path: to, kind: "symlink", link });
|
|
1073
1611
|
} else {
|
|
1074
1612
|
fsops.link(s, to);
|
|
@@ -1094,8 +1632,8 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
1094
1632
|
continue;
|
|
1095
1633
|
}
|
|
1096
1634
|
const name = conflictName(dst, d.name, ctx);
|
|
1097
|
-
place(
|
|
1098
|
-
rec.placed.set(relName, { target:
|
|
1635
|
+
place(path9.join(dst, name));
|
|
1636
|
+
rec.placed.set(relName, { target: path9.join(dst, name) });
|
|
1099
1637
|
stats.moved++;
|
|
1100
1638
|
stats.conflicts.push(join(rel, name));
|
|
1101
1639
|
}
|
|
@@ -1106,29 +1644,29 @@ function undoLinks(rec) {
|
|
|
1106
1644
|
const st = lstatOrNull(c.path);
|
|
1107
1645
|
if (!st) continue;
|
|
1108
1646
|
try {
|
|
1109
|
-
if (c.kind === "dir" && st.isDirectory())
|
|
1110
|
-
else if (c.kind === "symlink" && st.isSymbolicLink() &&
|
|
1111
|
-
else if (c.kind === "file" && st.ino === c.ino && st.dev === c.dev)
|
|
1647
|
+
if (c.kind === "dir" && st.isDirectory()) fs14.rmdirSync(c.path);
|
|
1648
|
+
else if (c.kind === "symlink" && st.isSymbolicLink() && fs14.readlinkSync(c.path) === c.link) fs14.unlinkSync(c.path);
|
|
1649
|
+
else if (c.kind === "file" && st.ino === c.ino && st.dev === c.dev) fs14.unlinkSync(c.path);
|
|
1112
1650
|
} catch {
|
|
1113
1651
|
}
|
|
1114
1652
|
}
|
|
1115
1653
|
}
|
|
1116
1654
|
function openForEmptying(dir) {
|
|
1117
1655
|
const st = requireRealDir(dir);
|
|
1118
|
-
if ((st.mode & 448) !== 448)
|
|
1119
|
-
return
|
|
1656
|
+
if ((st.mode & 448) !== 448) fs14.chmodSync(dir, st.mode & 4095 | 448);
|
|
1657
|
+
return fs14.readdirSync(dir, { withFileTypes: true });
|
|
1120
1658
|
}
|
|
1121
1659
|
function takeOver(src, placed, rec, dst, ctx, stats, rel) {
|
|
1122
1660
|
const ours = rec.byPath.get(placed.target);
|
|
1123
1661
|
const there = lstatOrNull(placed.target);
|
|
1124
1662
|
if (!ours || ours.kind !== "file" || !there || there.ino !== ours.ino || there.dev !== ours.dev) return false;
|
|
1125
|
-
const base =
|
|
1663
|
+
const base = path9.basename(placed.target);
|
|
1126
1664
|
const old = conflictName(dst, base, ctx);
|
|
1127
|
-
fsops.link(placed.target,
|
|
1128
|
-
const tmp =
|
|
1665
|
+
fsops.link(placed.target, path9.join(dst, old));
|
|
1666
|
+
const tmp = path9.join(dst, `.xclaude-tmp-${base}-${process.pid}-${counter2++}`);
|
|
1129
1667
|
fsops.link(src, tmp);
|
|
1130
|
-
|
|
1131
|
-
|
|
1668
|
+
fs14.renameSync(tmp, placed.target);
|
|
1669
|
+
fs14.unlinkSync(src);
|
|
1132
1670
|
stats.conflicts.push(join(rel, old));
|
|
1133
1671
|
return true;
|
|
1134
1672
|
}
|
|
@@ -1138,34 +1676,34 @@ function moveTree(src, dst, ctx, stats, rec = null, root = src, rel = "") {
|
|
|
1138
1676
|
entries.sort((a, b) => Number(isPlaced(a.name)) - Number(isPlaced(b.name)));
|
|
1139
1677
|
for (const d of entries) {
|
|
1140
1678
|
ctx.refresh();
|
|
1141
|
-
const s =
|
|
1142
|
-
const st =
|
|
1679
|
+
const s = path9.join(src, d.name);
|
|
1680
|
+
const st = fs14.lstatSync(s);
|
|
1143
1681
|
const relName = join(rel, d.name);
|
|
1144
1682
|
const placed = rec?.placed.get(relName);
|
|
1145
1683
|
if (st.isDirectory()) {
|
|
1146
|
-
let target =
|
|
1684
|
+
let target = path9.join(dst, d.name);
|
|
1147
1685
|
if (placed && lstatOrNull(placed.target)?.isDirectory()) {
|
|
1148
1686
|
target = placed.target;
|
|
1149
1687
|
} else {
|
|
1150
1688
|
try {
|
|
1151
|
-
|
|
1689
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
1152
1690
|
} catch (e) {
|
|
1153
1691
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1154
|
-
if (!
|
|
1692
|
+
if (!fs14.lstatSync(target).isDirectory()) {
|
|
1155
1693
|
const name2 = conflictName(dst, d.name, ctx);
|
|
1156
|
-
target =
|
|
1157
|
-
|
|
1694
|
+
target = path9.join(dst, name2);
|
|
1695
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
1158
1696
|
stats.conflicts.push(join(rel, name2));
|
|
1159
1697
|
}
|
|
1160
1698
|
}
|
|
1161
1699
|
}
|
|
1162
1700
|
moveTree(s, target, ctx, stats, rec, root, relName);
|
|
1163
|
-
|
|
1701
|
+
fs14.rmdirSync(s);
|
|
1164
1702
|
continue;
|
|
1165
1703
|
}
|
|
1166
1704
|
if (placed) {
|
|
1167
1705
|
if (stillPlaced(placed, s, st, root)) {
|
|
1168
|
-
|
|
1706
|
+
fs14.unlinkSync(s);
|
|
1169
1707
|
continue;
|
|
1170
1708
|
}
|
|
1171
1709
|
if (st.isFile() && !placed.identical && rec && takeOver(s, placed, rec, dst, ctx, stats, rel)) {
|
|
@@ -1175,29 +1713,29 @@ function moveTree(src, dst, ctx, stats, rec = null, root = src, rel = "") {
|
|
|
1175
1713
|
}
|
|
1176
1714
|
if (st.isSymbolicLink()) {
|
|
1177
1715
|
const link = linkText(s, root);
|
|
1178
|
-
const to =
|
|
1716
|
+
const to = path9.join(dst, d.name);
|
|
1179
1717
|
try {
|
|
1180
|
-
|
|
1181
|
-
|
|
1718
|
+
fs14.symlinkSync(link, to);
|
|
1719
|
+
fs14.unlinkSync(s);
|
|
1182
1720
|
stats.moved++;
|
|
1183
1721
|
continue;
|
|
1184
1722
|
} catch (e) {
|
|
1185
1723
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1186
1724
|
}
|
|
1187
|
-
} else if (moveNoClobber(s,
|
|
1725
|
+
} else if (moveNoClobber(s, path9.join(dst, d.name), st) === "moved") {
|
|
1188
1726
|
stats.moved++;
|
|
1189
1727
|
continue;
|
|
1190
1728
|
}
|
|
1191
|
-
if (identicalEntry(s, st,
|
|
1192
|
-
|
|
1729
|
+
if (identicalEntry(s, st, path9.join(dst, d.name), root)) {
|
|
1730
|
+
fs14.unlinkSync(s);
|
|
1193
1731
|
stats.dropped++;
|
|
1194
1732
|
continue;
|
|
1195
1733
|
}
|
|
1196
1734
|
const name = conflictName(dst, d.name, ctx);
|
|
1197
1735
|
if (st.isSymbolicLink()) {
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
} else if (moveNoClobber(s,
|
|
1736
|
+
fs14.symlinkSync(linkText(s, root), path9.join(dst, name));
|
|
1737
|
+
fs14.unlinkSync(s);
|
|
1738
|
+
} else if (moveNoClobber(s, path9.join(dst, name), st) !== "moved") {
|
|
1201
1739
|
throw new Error(`can't place ${relName}: ${name} is taken`);
|
|
1202
1740
|
}
|
|
1203
1741
|
stats.moved++;
|
|
@@ -1328,6 +1866,7 @@ var APPENDIX_A = [
|
|
|
1328
1866
|
"local-settings",
|
|
1329
1867
|
"project-settings",
|
|
1330
1868
|
"remote",
|
|
1869
|
+
"remote-control",
|
|
1331
1870
|
"scratch",
|
|
1332
1871
|
"seed-admin",
|
|
1333
1872
|
"storage-v2",
|
|
@@ -1402,7 +1941,7 @@ function readLines(file) {
|
|
|
1402
1941
|
return (readFileOrNull(file) ?? "").split("\n");
|
|
1403
1942
|
}
|
|
1404
1943
|
function storeLockPath(S) {
|
|
1405
|
-
return `${realpathOrNull(
|
|
1944
|
+
return `${realpathOrNull(path10.join(S, HISTORY)) ?? path10.join(S, HISTORY)}.lock`;
|
|
1406
1945
|
}
|
|
1407
1946
|
function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
1408
1947
|
const refreshAll = () => {
|
|
@@ -1413,7 +1952,7 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1413
1952
|
const storeLock = DirLock.acquire(storeLockPath(ctx.S), HISTORY_LOCK, { waitMs, onWait: refreshAll });
|
|
1414
1953
|
if (!storeLock) return { ok: false, reason: "the shared history.jsonl is busy" };
|
|
1415
1954
|
try {
|
|
1416
|
-
const storeFile = realpathOrNull(
|
|
1955
|
+
const storeFile = realpathOrNull(path10.join(ctx.S, HISTORY));
|
|
1417
1956
|
if (!storeFile) return { ok: false, reason: "~/.claude/history.jsonl doesn't resolve" };
|
|
1418
1957
|
const storeLines = readLines(storeFile);
|
|
1419
1958
|
const merged = mergeHistoryLines(storeLines, readLines(aside));
|
|
@@ -1421,7 +1960,7 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1421
1960
|
if (!storeLock.refresh() || alsoHeld && !alsoHeld.refresh()) return { ok: false, reason: "a history lock was taken over" };
|
|
1422
1961
|
writeFileAtomic(storeFile, merged.length ? `${merged.join("\n")}
|
|
1423
1962
|
` : "", { mode: 384, fsync: true });
|
|
1424
|
-
|
|
1963
|
+
fs15.unlinkSync(aside);
|
|
1425
1964
|
const before = new Set(storeLines);
|
|
1426
1965
|
return { ok: true, added: merged.filter((line) => !before.has(line)).length };
|
|
1427
1966
|
} finally {
|
|
@@ -1429,37 +1968,37 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1429
1968
|
}
|
|
1430
1969
|
}
|
|
1431
1970
|
function mergeHistory(ctx) {
|
|
1432
|
-
const file =
|
|
1971
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1433
1972
|
const accountLock = DirLock.acquire(`${file}.lock`, HISTORY_LOCK, { waitMs: ctx.lockWaitMs, onWait: ctx.refreshStoreLock });
|
|
1434
1973
|
if (!accountLock) return { ok: false, reason: `this account's history.jsonl is busy` };
|
|
1435
1974
|
try {
|
|
1436
1975
|
const st = lstatOrNull(file);
|
|
1437
1976
|
if (!st?.isFile()) return { ok: true, added: 0 };
|
|
1438
|
-
const storeFile = realpathOrNull(
|
|
1977
|
+
const storeFile = realpathOrNull(path10.join(ctx.S, HISTORY));
|
|
1439
1978
|
if (!storeFile) return { ok: false, reason: "~/.claude/history.jsonl doesn't resolve" };
|
|
1440
|
-
const aside =
|
|
1441
|
-
|
|
1979
|
+
const aside = path10.join(ctx.A, asideName(HISTORY));
|
|
1980
|
+
fs15.linkSync(file, aside);
|
|
1442
1981
|
const tmp = siblingName(file, "link");
|
|
1443
|
-
|
|
1444
|
-
|
|
1982
|
+
fs15.symlinkSync(storeFile, tmp);
|
|
1983
|
+
fs15.renameSync(tmp, file);
|
|
1445
1984
|
return mergeAsideIntoStore(ctx, aside, accountLock);
|
|
1446
1985
|
} finally {
|
|
1447
1986
|
accountLock.release();
|
|
1448
1987
|
}
|
|
1449
1988
|
}
|
|
1450
1989
|
function resumeHistoryAside(ctx, aside, shared) {
|
|
1451
|
-
const file =
|
|
1990
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1452
1991
|
const st = lstatOrNull(file);
|
|
1453
|
-
const asideStat =
|
|
1992
|
+
const asideStat = fs15.lstatSync(aside);
|
|
1454
1993
|
if (st?.isFile() && st.ino === asideStat.ino && st.dev === asideStat.dev) {
|
|
1455
|
-
|
|
1994
|
+
fs15.unlinkSync(aside);
|
|
1456
1995
|
return { ok: true, added: 0 };
|
|
1457
1996
|
}
|
|
1458
1997
|
if (!shared) return mergeAsideIntoAccount(ctx, aside);
|
|
1459
1998
|
return mergeAsideIntoStore(ctx, aside, null);
|
|
1460
1999
|
}
|
|
1461
2000
|
function mergeAsideIntoAccount(ctx, aside) {
|
|
1462
|
-
const file =
|
|
2001
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1463
2002
|
const lock = DirLock.acquire(`${file}.lock`, HISTORY_LOCK, { waitMs: ctx.lockWaitMs, onWait: ctx.refreshStoreLock });
|
|
1464
2003
|
if (!lock) return { ok: false, reason: "this account's history.jsonl is busy" };
|
|
1465
2004
|
try {
|
|
@@ -1470,7 +2009,7 @@ function mergeAsideIntoAccount(ctx, aside) {
|
|
|
1470
2009
|
if (!lock.refresh()) return { ok: false, reason: "a history lock was taken over" };
|
|
1471
2010
|
writeFileAtomic(file, merged.length ? `${merged.join("\n")}
|
|
1472
2011
|
` : "", { mode: 384, fsync: true });
|
|
1473
|
-
|
|
2012
|
+
fs15.unlinkSync(aside);
|
|
1474
2013
|
const before = new Set(own);
|
|
1475
2014
|
return { ok: true, added: merged.filter((line) => !before.has(line)).length };
|
|
1476
2015
|
} finally {
|
|
@@ -1479,23 +2018,23 @@ function mergeAsideIntoAccount(ctx, aside) {
|
|
|
1479
2018
|
}
|
|
1480
2019
|
|
|
1481
2020
|
// src/link/stub.ts
|
|
1482
|
-
import
|
|
1483
|
-
import
|
|
2021
|
+
import fs16 from "node:fs";
|
|
2022
|
+
import path11 from "node:path";
|
|
1484
2023
|
var STUB_TEXT = "Shared instructions live in ~/.claude/CLAUDE.md, so edit that file.\n@~/.claude/CLAUDE.md\n";
|
|
1485
2024
|
function stubState(A, S, enabled, detail) {
|
|
1486
|
-
const storeHas = enabled && Boolean(statOrNull(
|
|
1487
|
-
const a = lstatOrNull(
|
|
2025
|
+
const storeHas = enabled && Boolean(statOrNull(path11.join(S, STUB))?.isFile());
|
|
2026
|
+
const a = lstatOrNull(path11.join(A, STUB));
|
|
1488
2027
|
if (!a) return storeHas ? "create" : "none";
|
|
1489
2028
|
if (storeHas && !detail) return "ok";
|
|
1490
|
-
const isStub = a.isFile() && readFileOrNull(
|
|
2029
|
+
const isStub = a.isFile() && readFileOrNull(path11.join(A, STUB)) === STUB_TEXT;
|
|
1491
2030
|
if (storeHas) return isStub ? "ok" : "differs";
|
|
1492
2031
|
return isStub ? "remove" : "differs";
|
|
1493
2032
|
}
|
|
1494
2033
|
function applyStub(A, state) {
|
|
1495
|
-
const file =
|
|
2034
|
+
const file = path11.join(A, STUB);
|
|
1496
2035
|
if (state === "create") {
|
|
1497
2036
|
try {
|
|
1498
|
-
|
|
2037
|
+
fs16.writeFileSync(file, STUB_TEXT, { flag: "wx", mode: 384 });
|
|
1499
2038
|
return "created";
|
|
1500
2039
|
} catch (e) {
|
|
1501
2040
|
if (errCode(e) === "EEXIST") return null;
|
|
@@ -1503,7 +2042,7 @@ function applyStub(A, state) {
|
|
|
1503
2042
|
}
|
|
1504
2043
|
}
|
|
1505
2044
|
if (state === "remove" && readFileOrNull(file) === STUB_TEXT) {
|
|
1506
|
-
|
|
2045
|
+
fs16.unlinkSync(file);
|
|
1507
2046
|
return "removed";
|
|
1508
2047
|
}
|
|
1509
2048
|
return null;
|
|
@@ -1521,8 +2060,8 @@ function storeKind(p) {
|
|
|
1521
2060
|
return st.isDirectory() ? "dir" : st.isFile() ? "file" : "other";
|
|
1522
2061
|
}
|
|
1523
2062
|
function classifyEntry(A, S, name, kind) {
|
|
1524
|
-
const a =
|
|
1525
|
-
const s =
|
|
2063
|
+
const a = path12.join(A, name);
|
|
2064
|
+
const s = path12.join(S, name);
|
|
1526
2065
|
const lst = lstatOrNull(a);
|
|
1527
2066
|
const optional = OPTIONAL_DIRS.has(name);
|
|
1528
2067
|
const sk = storeKind(s);
|
|
@@ -1553,7 +2092,7 @@ function inspectAccount(paths, account, table, detail = false) {
|
|
|
1553
2092
|
const S = paths.store;
|
|
1554
2093
|
let dirents;
|
|
1555
2094
|
try {
|
|
1556
|
-
dirents =
|
|
2095
|
+
dirents = fs17.readdirSync(A, { withFileTypes: true });
|
|
1557
2096
|
} catch (e) {
|
|
1558
2097
|
if (errCode(e) !== "ENOENT") throw e;
|
|
1559
2098
|
return { dir: A, exists: false, entries: [], leftovers: [], stub: "none", names: [] };
|
|
@@ -1564,8 +2103,8 @@ function inspectAccount(paths, account, table, detail = false) {
|
|
|
1564
2103
|
for (const d of dirents) {
|
|
1565
2104
|
const name = d.name;
|
|
1566
2105
|
if (!d.isSymbolicLink() || shared.has(name) || name === STUB || NEVER_SHARED.includes(name) || isIgnored(name)) continue;
|
|
1567
|
-
const target = readlinkOrNull(
|
|
1568
|
-
const s =
|
|
2106
|
+
const target = readlinkOrNull(path12.join(A, name));
|
|
2107
|
+
const s = path12.join(S, name);
|
|
1569
2108
|
if (target === null || target !== s && target !== realpathOrNull(s)) continue;
|
|
1570
2109
|
const kind = name === HISTORY || storeKind(s) === "file" ? "file" : "dir";
|
|
1571
2110
|
entries.push({ name, kind, state: "unshare", target });
|
|
@@ -1630,11 +2169,11 @@ function describeProblems(insp, paths) {
|
|
|
1630
2169
|
return problems;
|
|
1631
2170
|
}
|
|
1632
2171
|
function ensureStoreEntry(S, name, kind) {
|
|
1633
|
-
|
|
1634
|
-
const p =
|
|
2172
|
+
fs17.mkdirSync(S, { recursive: true, mode: 448 });
|
|
2173
|
+
const p = path12.join(S, name);
|
|
1635
2174
|
try {
|
|
1636
|
-
if (kind === "dir")
|
|
1637
|
-
else
|
|
2175
|
+
if (kind === "dir") fs17.mkdirSync(p, { mode: 448 });
|
|
2176
|
+
else fs17.closeSync(fs17.openSync(p, "wx", 384));
|
|
1638
2177
|
} catch (e) {
|
|
1639
2178
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1640
2179
|
}
|
|
@@ -1712,10 +2251,10 @@ var Repairer = class {
|
|
|
1712
2251
|
/** realpath(S/E), created first if missing. Never a path inside an account dir. */
|
|
1713
2252
|
linkTarget(info) {
|
|
1714
2253
|
ensureStoreEntry(this.S, info.name, info.kind);
|
|
1715
|
-
const target = realpathOrNull(
|
|
2254
|
+
const target = realpathOrNull(path12.join(this.S, info.name));
|
|
1716
2255
|
if (!target) throw new Error(`${this.storePath(info.name)} doesn't resolve`);
|
|
1717
2256
|
const accounts = realpathOrNull(this.opts.paths.accounts) ?? this.opts.paths.accounts;
|
|
1718
|
-
if (target === accounts || target.startsWith(`${accounts}${
|
|
2257
|
+
if (target === accounts || target.startsWith(`${accounts}${path12.sep}`)) {
|
|
1719
2258
|
throw new Error(`${this.storePath(info.name)} resolves into ${tildify(target, this.opts.paths.home)}, inside an account dir; make it a real ${info.kind === "dir" ? "directory" : "file"} again`);
|
|
1720
2259
|
}
|
|
1721
2260
|
return target;
|
|
@@ -1723,7 +2262,7 @@ var Repairer = class {
|
|
|
1723
2262
|
createLink(info, depth) {
|
|
1724
2263
|
const target = this.linkTarget(info);
|
|
1725
2264
|
try {
|
|
1726
|
-
|
|
2265
|
+
fs17.symlinkSync(target, path12.join(this.A, info.name));
|
|
1727
2266
|
} catch (e) {
|
|
1728
2267
|
if (errCode(e) !== "EEXIST" || depth > 0) throw e;
|
|
1729
2268
|
this.act(classifyEntry(this.A, this.S, info.name, info.kind), depth + 1);
|
|
@@ -1731,22 +2270,22 @@ var Repairer = class {
|
|
|
1731
2270
|
}
|
|
1732
2271
|
replaceLink(info) {
|
|
1733
2272
|
const target = this.linkTarget(info);
|
|
1734
|
-
const a =
|
|
2273
|
+
const a = path12.join(this.A, info.name);
|
|
1735
2274
|
const tmp = siblingName(a, "link");
|
|
1736
|
-
|
|
1737
|
-
|
|
2275
|
+
fs17.symlinkSync(target, tmp);
|
|
2276
|
+
fs17.renameSync(tmp, a);
|
|
1738
2277
|
this.opts.log(`xclaude: ${this.label}/${info.name} pointed to ${info.target}; relinked to ${this.storePath(info.name)}`);
|
|
1739
2278
|
}
|
|
1740
2279
|
unshare(info) {
|
|
1741
|
-
const a =
|
|
2280
|
+
const a = path12.join(this.A, info.name);
|
|
1742
2281
|
if (info.kind === "file") {
|
|
1743
2282
|
const tmp = siblingName(a, "unshare");
|
|
1744
|
-
|
|
1745
|
-
|
|
2283
|
+
fs17.closeSync(fs17.openSync(tmp, "wx", 384));
|
|
2284
|
+
fs17.renameSync(tmp, a);
|
|
1746
2285
|
} else {
|
|
1747
|
-
|
|
2286
|
+
fs17.unlinkSync(a);
|
|
1748
2287
|
try {
|
|
1749
|
-
|
|
2288
|
+
fs17.mkdirSync(a, { mode: 448 });
|
|
1750
2289
|
} catch (e) {
|
|
1751
2290
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1752
2291
|
}
|
|
@@ -1772,7 +2311,7 @@ var Repairer = class {
|
|
|
1772
2311
|
* was lost: then another process may already rely on those links.
|
|
1773
2312
|
*/
|
|
1774
2313
|
mergeDir(info) {
|
|
1775
|
-
const a =
|
|
2314
|
+
const a = path12.join(this.A, info.name);
|
|
1776
2315
|
const target = this.linkTarget(info);
|
|
1777
2316
|
const ctx = this.moveContext();
|
|
1778
2317
|
const stats = emptyStats();
|
|
@@ -1797,15 +2336,15 @@ var Repairer = class {
|
|
|
1797
2336
|
}
|
|
1798
2337
|
const asides = [];
|
|
1799
2338
|
for (let attempt = 0; ; attempt++) {
|
|
1800
|
-
const aside =
|
|
1801
|
-
|
|
2339
|
+
const aside = path12.join(this.A, asideName(info.name));
|
|
2340
|
+
fs17.renameSync(a, aside);
|
|
1802
2341
|
asides.push(aside);
|
|
1803
2342
|
try {
|
|
1804
|
-
|
|
2343
|
+
fs17.symlinkSync(target, a);
|
|
1805
2344
|
break;
|
|
1806
2345
|
} catch (e) {
|
|
1807
2346
|
if (errCode(e) !== "EEXIST") {
|
|
1808
|
-
|
|
2347
|
+
fs17.renameSync(asides[0], a);
|
|
1809
2348
|
if (asides.length === 1) undoLinks(rec);
|
|
1810
2349
|
throw e;
|
|
1811
2350
|
}
|
|
@@ -1816,7 +2355,7 @@ var Repairer = class {
|
|
|
1816
2355
|
}
|
|
1817
2356
|
asides.forEach((aside, i) => {
|
|
1818
2357
|
moveTree(aside, target, ctx, stats, i === 0 ? rec : null);
|
|
1819
|
-
|
|
2358
|
+
fs17.rmdirSync(aside);
|
|
1820
2359
|
});
|
|
1821
2360
|
const line = summarize(this.label, info.name, this.storePath(info.name), stats);
|
|
1822
2361
|
if (line) this.opts.log(line);
|
|
@@ -1852,11 +2391,11 @@ var Repairer = class {
|
|
|
1852
2391
|
}
|
|
1853
2392
|
}
|
|
1854
2393
|
resumeLeftover(name) {
|
|
1855
|
-
const p =
|
|
2394
|
+
const p = path12.join(this.A, name);
|
|
1856
2395
|
if (name.startsWith(".xclaude-link-") || name.startsWith(".xclaude-unshare-") || name.startsWith(".xclaude-tmp-")) {
|
|
1857
2396
|
const st2 = lstatOrNull(p);
|
|
1858
|
-
if (st2?.isDirectory())
|
|
1859
|
-
else if (st2)
|
|
2397
|
+
if (st2?.isDirectory()) fs17.rmdirSync(p);
|
|
2398
|
+
else if (st2) fs17.unlinkSync(p);
|
|
1860
2399
|
return;
|
|
1861
2400
|
}
|
|
1862
2401
|
const entry = parseAside(name);
|
|
@@ -1867,7 +2406,7 @@ var Repairer = class {
|
|
|
1867
2406
|
const st = lstatOrNull(p);
|
|
1868
2407
|
if (!st) return;
|
|
1869
2408
|
if (st.isSymbolicLink()) {
|
|
1870
|
-
|
|
2409
|
+
fs17.unlinkSync(p);
|
|
1871
2410
|
return;
|
|
1872
2411
|
}
|
|
1873
2412
|
if (entry === HISTORY ? !st.isFile() : !st.isDirectory()) {
|
|
@@ -1881,22 +2420,22 @@ var Repairer = class {
|
|
|
1881
2420
|
return;
|
|
1882
2421
|
}
|
|
1883
2422
|
if (!this.opts.table.dirs.includes(entry)) {
|
|
1884
|
-
const own =
|
|
1885
|
-
if (!lstatOrNull(own))
|
|
1886
|
-
const st2 =
|
|
2423
|
+
const own = path12.join(this.A, entry);
|
|
2424
|
+
if (!lstatOrNull(own)) fs17.mkdirSync(own, { mode: 448 });
|
|
2425
|
+
const st2 = fs17.lstatSync(own);
|
|
1887
2426
|
if (!st2.isDirectory() || st2.isSymbolicLink()) {
|
|
1888
2427
|
this.problem(name, `left by an interrupted merge of ${entry}; ${entry} isn't a directory, so move the contents back by hand`);
|
|
1889
2428
|
return;
|
|
1890
2429
|
}
|
|
1891
2430
|
moveTree(p, own, this.moveContext(), emptyStats());
|
|
1892
|
-
|
|
2431
|
+
fs17.rmdirSync(p);
|
|
1893
2432
|
return;
|
|
1894
2433
|
}
|
|
1895
2434
|
const target = this.linkTarget({ name: entry, kind: "dir", state: "ok" });
|
|
1896
2435
|
const stats = emptyStats();
|
|
1897
2436
|
try {
|
|
1898
2437
|
moveTree(p, target, this.moveContext(), stats);
|
|
1899
|
-
|
|
2438
|
+
fs17.rmdirSync(p);
|
|
1900
2439
|
} catch (e) {
|
|
1901
2440
|
this.cannotMerge(entry, e);
|
|
1902
2441
|
return;
|
|
@@ -1909,7 +2448,7 @@ function unmanageable(A, store) {
|
|
|
1909
2448
|
if (lstatOrNull(A)?.isSymbolicLink()) return "is a symlink; xclaude only manages real folders there, so it leaves this one alone";
|
|
1910
2449
|
const realA = realpathOrNull(A);
|
|
1911
2450
|
const realS = realpathOrNull(store);
|
|
1912
|
-
if (realA && realS && (realA === realS || realS.startsWith(`${realA}${
|
|
2451
|
+
if (realA && realS && (realA === realS || realS.startsWith(`${realA}${path12.sep}`))) {
|
|
1913
2452
|
return "is the shared store or holds it; xclaude leaves it alone";
|
|
1914
2453
|
}
|
|
1915
2454
|
return null;
|
|
@@ -1923,7 +2462,7 @@ function repairAccount(opts) {
|
|
|
1923
2462
|
const problems = describeProblems(insp, opts.paths);
|
|
1924
2463
|
return { clean: problems.length === 0, busy: false, problems, names: insp.names };
|
|
1925
2464
|
}
|
|
1926
|
-
const lock = DirLock.acquire(
|
|
2465
|
+
const lock = DirLock.acquire(path12.join(opts.paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1927
2466
|
if (!lock) {
|
|
1928
2467
|
const problems = describeProblems(insp, opts.paths);
|
|
1929
2468
|
return { clean: false, busy: true, problems, names: insp.names };
|
|
@@ -2036,19 +2575,21 @@ function applyDefaultOptions(p, home, into) {
|
|
|
2036
2575
|
const args = stringOption(p, "args");
|
|
2037
2576
|
if (args !== void 0) into.args = splitShellWords(args, home);
|
|
2038
2577
|
}
|
|
2039
|
-
function formatDefaults(d) {
|
|
2578
|
+
function formatDefaults(d, account = false) {
|
|
2579
|
+
const inherit = d.inherit === void 0 ? "not chosen yet" : d.inherit ? "on" : "off";
|
|
2040
2580
|
return [
|
|
2041
2581
|
` model ${d.model ?? DASH}`,
|
|
2042
2582
|
` effort ${d.effort ?? DASH}`,
|
|
2043
|
-
` args ${d.args.length ? quoteShellWords(d.args) : DASH}
|
|
2583
|
+
` args ${d.args.length ? quoteShellWords(d.args) : DASH}`,
|
|
2584
|
+
...account ? [` inherit ${inherit}`] : []
|
|
2044
2585
|
].map((l) => `${l}
|
|
2045
2586
|
`).join("");
|
|
2046
2587
|
}
|
|
2047
2588
|
|
|
2048
2589
|
// src/commands/add.ts
|
|
2049
|
-
var USAGE = 'usage: xclaude add <name> [--model M] [--effort E] [--args "\u2026"]';
|
|
2590
|
+
var USAGE = 'usage: xclaude add <name> [--model M] [--effort E] [--args "\u2026"] [--inherit on|off]';
|
|
2050
2591
|
async function addCommand(ctx, config, args) {
|
|
2051
|
-
const p = parseOptions(args, DEFAULT_OPTIONS, "add");
|
|
2592
|
+
const p = parseOptions(args, { ...DEFAULT_OPTIONS, inherit: { value: true } }, "add");
|
|
2052
2593
|
const [name, ...extra] = p.positionals;
|
|
2053
2594
|
if (!name || extra.length) throw new UsageError(USAGE);
|
|
2054
2595
|
const problem2 = accountNameProblem(name);
|
|
@@ -2056,13 +2597,17 @@ async function addCommand(ctx, config, args) {
|
|
|
2056
2597
|
if (Object.hasOwn(config.accounts, name)) throw new XError(`account "${name}" already exists; change its defaults with: xclaude set ${name}`);
|
|
2057
2598
|
const defaults = { model: null, effort: null, args: [] };
|
|
2058
2599
|
applyDefaultOptions(p, ctx.paths.home, defaults);
|
|
2600
|
+
const inherit = stringOption(p, "inherit");
|
|
2601
|
+
if (inherit !== void 0 && inherit !== "on" && inherit !== "off") throw new UsageError(`--inherit takes on or off, not "${inherit}"`);
|
|
2059
2602
|
const claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
2060
2603
|
const log = (line) => ctx.io.err(`${line}
|
|
2061
2604
|
`);
|
|
2062
2605
|
const dir = accountDir(ctx.paths, name);
|
|
2063
2606
|
const refusal = unmanageable(dir, ctx.paths.store);
|
|
2064
2607
|
if (refusal) throw new XError(`not adding ${name}: ${tildify(dir, ctx.paths.home)} ${refusal}. Replace it with a real folder, or remove it, then add ${name} again.`);
|
|
2065
|
-
|
|
2608
|
+
const take = inherit !== void 0 ? inherit === "on" : await askInherit(ctx, name);
|
|
2609
|
+
if (take !== void 0) defaults.inherit = take;
|
|
2610
|
+
if (fs18.existsSync(dir)) log(`xclaude: ${tildify(dir, ctx.paths.home)} already exists; reusing it`);
|
|
2066
2611
|
mkdirPrivate(dir);
|
|
2067
2612
|
const table = shareTable(config, ctx.switches);
|
|
2068
2613
|
const res = repairAccount({
|
|
@@ -2074,7 +2619,8 @@ async function addCommand(ctx, config, args) {
|
|
|
2074
2619
|
log
|
|
2075
2620
|
});
|
|
2076
2621
|
for (const pr of res.problems) log(`xclaude: ${problemLine(name, pr)}`);
|
|
2077
|
-
if (
|
|
2622
|
+
if (take) inheritSettings(ctx.paths, name, { lockWaitMs: Infinity, log });
|
|
2623
|
+
if (table.dirs.includes("plugins")) listPlugins(ctx.paths, name, { lockWaitMs: Infinity, log, follow: take === true });
|
|
2078
2624
|
config = loadConfig(ctx.paths, { create: false }).config;
|
|
2079
2625
|
config.accounts[name] = defaults;
|
|
2080
2626
|
saveConfig(ctx.paths, config);
|
|
@@ -2100,12 +2646,12 @@ async function addCommand(ctx, config, args) {
|
|
|
2100
2646
|
|
|
2101
2647
|
// src/commands/doctor.ts
|
|
2102
2648
|
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
2103
|
-
import
|
|
2104
|
-
import
|
|
2649
|
+
import fs23 from "node:fs";
|
|
2650
|
+
import path17 from "node:path";
|
|
2105
2651
|
|
|
2106
2652
|
// src/link/normalize.ts
|
|
2107
|
-
import
|
|
2108
|
-
import
|
|
2653
|
+
import fs19 from "node:fs";
|
|
2654
|
+
import path13 from "node:path";
|
|
2109
2655
|
var REGISTRY_FILES = ["plugins/installed_plugins.json", "plugins/known_marketplaces.json"];
|
|
2110
2656
|
function escapeRe(s) {
|
|
2111
2657
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -2125,17 +2671,17 @@ function rewrite(value, re, to, onChange) {
|
|
|
2125
2671
|
return value;
|
|
2126
2672
|
}
|
|
2127
2673
|
function planNormalization(paths, account) {
|
|
2128
|
-
const accountsDir =
|
|
2129
|
-
const to =
|
|
2674
|
+
const accountsDir = path13.join(paths.xhome, "accounts");
|
|
2675
|
+
const to = path13.join(paths.home, ".claude");
|
|
2130
2676
|
const name = account ? escapeRe(account) : "[a-z][a-z0-9-]{0,31}";
|
|
2131
2677
|
const re = new RegExp(`^${escapeRe(accountsDir)}/(${name})(/|$)`);
|
|
2132
2678
|
const plans = [];
|
|
2133
2679
|
for (const rel of REGISTRY_FILES) {
|
|
2134
|
-
const file = realpathOrNull(
|
|
2680
|
+
const file = realpathOrNull(path13.join(paths.store, rel));
|
|
2135
2681
|
if (!file) continue;
|
|
2136
2682
|
const st = lstatOrNull(file);
|
|
2137
2683
|
if (!st?.isFile()) continue;
|
|
2138
|
-
const original =
|
|
2684
|
+
const original = fs19.readFileSync(file, "utf8");
|
|
2139
2685
|
if (!original.includes(accountsDir)) continue;
|
|
2140
2686
|
let parsed;
|
|
2141
2687
|
try {
|
|
@@ -2168,7 +2714,7 @@ function applyNormalization(plans) {
|
|
|
2168
2714
|
function normalizePaths(paths, opts) {
|
|
2169
2715
|
const plans = planNormalization(paths, opts.account);
|
|
2170
2716
|
if (!plans.length) return { changed: [], skipped: [], busy: false };
|
|
2171
|
-
const lock = DirLock.acquire(
|
|
2717
|
+
const lock = DirLock.acquire(path13.join(paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
2172
2718
|
if (!lock) return { changed: [], skipped: plans.map((p) => p.file), busy: true };
|
|
2173
2719
|
try {
|
|
2174
2720
|
const result = applyNormalization(plans);
|
|
@@ -2182,12 +2728,12 @@ function normalizePaths(paths, opts) {
|
|
|
2182
2728
|
}
|
|
2183
2729
|
|
|
2184
2730
|
// src/link/leftover.ts
|
|
2185
|
-
import
|
|
2186
|
-
import
|
|
2731
|
+
import fs20 from "node:fs";
|
|
2732
|
+
import path14 from "node:path";
|
|
2187
2733
|
function strayFolders(paths, config) {
|
|
2188
2734
|
let names;
|
|
2189
2735
|
try {
|
|
2190
|
-
names =
|
|
2736
|
+
names = fs20.readdirSync(paths.accounts);
|
|
2191
2737
|
} catch {
|
|
2192
2738
|
return [];
|
|
2193
2739
|
}
|
|
@@ -2195,14 +2741,14 @@ function strayFolders(paths, config) {
|
|
|
2195
2741
|
const out = [];
|
|
2196
2742
|
for (const name of names.sort()) {
|
|
2197
2743
|
if (name.endsWith(".lock") || name === ".DS_Store" || Object.hasOwn(config.accounts, name)) continue;
|
|
2198
|
-
const dir =
|
|
2744
|
+
const dir = path14.join(paths.accounts, name);
|
|
2199
2745
|
const st = lstatOrNull(dir);
|
|
2200
2746
|
if (!st) continue;
|
|
2201
2747
|
if (st.isSymbolicLink()) out.push({ name, dir, kind: "link", entries: [] });
|
|
2202
2748
|
else if (!st.isDirectory() || accounts.some((a) => a.dev === st.dev && a.ino === st.ino)) continue;
|
|
2203
2749
|
else if (accountNameProblem(name)) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2204
2750
|
else {
|
|
2205
|
-
const entries =
|
|
2751
|
+
const entries = fs20.readdirSync(dir).filter((e) => e !== ".DS_Store" && !lstatOrNull(path14.join(dir, e))?.isSymbolicLink());
|
|
2206
2752
|
out.push({ name, dir, kind: entries.length ? "content" : "leftover", entries });
|
|
2207
2753
|
}
|
|
2208
2754
|
}
|
|
@@ -2217,27 +2763,31 @@ function linkNames(table) {
|
|
|
2217
2763
|
function hasContent(p) {
|
|
2218
2764
|
const st = lstatOrNull(p);
|
|
2219
2765
|
if (!st) return false;
|
|
2220
|
-
if (st.isDirectory()) return
|
|
2766
|
+
if (st.isDirectory()) return fs20.readdirSync(p).some((e) => e !== ".DS_Store");
|
|
2221
2767
|
return st.size > 0;
|
|
2222
2768
|
}
|
|
2769
|
+
function madeByUser(p, name, table) {
|
|
2770
|
+
if (name === STUB) return !(lstatOrNull(p)?.isFile() && readFileOrNull(p) === STUB_TEXT);
|
|
2771
|
+
return !isKnown(name, table) && !isIgnored(name);
|
|
2772
|
+
}
|
|
2223
2773
|
function keepOnlyLinks(dir, table) {
|
|
2224
2774
|
if (lstatOrNull(dir)?.isSymbolicLink()) throw new Error(`${dir} is a symlink`);
|
|
2225
2775
|
const shared = linkNames(table);
|
|
2226
2776
|
const content = [];
|
|
2227
2777
|
let links = 0;
|
|
2228
|
-
for (const name of
|
|
2229
|
-
const p =
|
|
2778
|
+
for (const name of fs20.readdirSync(dir).sort()) {
|
|
2779
|
+
const p = path14.join(dir, name);
|
|
2230
2780
|
if (shared.has(name) && lstatOrNull(p)?.isSymbolicLink()) links++;
|
|
2231
|
-
else if (shared.has(name) && hasContent(p)) content.push(name);
|
|
2781
|
+
else if ((shared.has(name) || madeByUser(p, name, table)) && hasContent(p)) content.push(name);
|
|
2232
2782
|
else removeTree(p);
|
|
2233
2783
|
}
|
|
2234
2784
|
if (links || content.length) return { kept: true, content };
|
|
2235
|
-
|
|
2785
|
+
fs20.rmdirSync(dir);
|
|
2236
2786
|
return { kept: false, content };
|
|
2237
2787
|
}
|
|
2238
2788
|
|
|
2239
2789
|
// src/link/unknown.ts
|
|
2240
|
-
import
|
|
2790
|
+
import fs21 from "node:fs";
|
|
2241
2791
|
function unknownEntries(names, table) {
|
|
2242
2792
|
return names.filter((n) => !isIgnored(n) && !isKnown(n, table)).sort();
|
|
2243
2793
|
}
|
|
@@ -2253,7 +2803,7 @@ function unknownNotice(account, entry, configLabel) {
|
|
|
2253
2803
|
function storeUnknownEntries(store, table) {
|
|
2254
2804
|
let names;
|
|
2255
2805
|
try {
|
|
2256
|
-
names =
|
|
2806
|
+
names = fs21.readdirSync(store);
|
|
2257
2807
|
} catch (e) {
|
|
2258
2808
|
if (errCode(e) === "ENOENT") return [];
|
|
2259
2809
|
throw e;
|
|
@@ -2262,8 +2812,8 @@ function storeUnknownEntries(store, table) {
|
|
|
2262
2812
|
}
|
|
2263
2813
|
|
|
2264
2814
|
// src/shell/rc.ts
|
|
2265
|
-
import
|
|
2266
|
-
import
|
|
2815
|
+
import fs22 from "node:fs";
|
|
2816
|
+
import path15 from "node:path";
|
|
2267
2817
|
var START = "# >>> xclaude >>>";
|
|
2268
2818
|
var END = "# <<< xclaude <<<";
|
|
2269
2819
|
function dquote(s) {
|
|
@@ -2312,14 +2862,14 @@ function sourcesBashrc(text) {
|
|
|
2312
2862
|
return text.split("\n").some((l) => !/^\s*#/.test(l) && /\.bashrc\b/.test(l) && /(^|[\s;&|({])(\.|source)\s/.test(l));
|
|
2313
2863
|
}
|
|
2314
2864
|
function rcTargets(env, home, platform, want) {
|
|
2315
|
-
const zshrc =
|
|
2316
|
-
const bashrc =
|
|
2865
|
+
const zshrc = path15.join(env.ZDOTDIR || home, ".zshrc");
|
|
2866
|
+
const bashrc = path15.join(home, ".bashrc");
|
|
2317
2867
|
const explicit = want.bash || want.zsh;
|
|
2318
2868
|
const targets = [];
|
|
2319
|
-
if (explicit ? want.zsh :
|
|
2320
|
-
if (explicit ? want.bash :
|
|
2869
|
+
if (explicit ? want.zsh : fs22.existsSync(zshrc)) targets.push({ shell: "zsh", file: zshrc });
|
|
2870
|
+
if (explicit ? want.bash : fs22.existsSync(bashrc)) {
|
|
2321
2871
|
targets.push({ shell: "bash", file: bashrc });
|
|
2322
|
-
const profile =
|
|
2872
|
+
const profile = path15.join(home, ".bash_profile");
|
|
2323
2873
|
const text = platform === "darwin" ? readFileOrNull(profile) : null;
|
|
2324
2874
|
if (text !== null && !sourcesBashrc(text)) targets.push({ shell: "bash", file: profile });
|
|
2325
2875
|
}
|
|
@@ -2328,11 +2878,11 @@ function rcTargets(env, home, platform, want) {
|
|
|
2328
2878
|
function writeRc(file, text, previous) {
|
|
2329
2879
|
if (text === previous) return { file, status: "unchanged" };
|
|
2330
2880
|
const real = realpathOrNull(file);
|
|
2331
|
-
if (real === null &&
|
|
2881
|
+
if (real === null && fs22.lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink()) return { file, status: "manual", text };
|
|
2332
2882
|
try {
|
|
2333
2883
|
const target = real ?? file;
|
|
2334
|
-
|
|
2335
|
-
if (previous !== null)
|
|
2884
|
+
fs22.accessSync(path15.dirname(target), fs22.constants.W_OK);
|
|
2885
|
+
if (previous !== null) fs22.accessSync(target, fs22.constants.W_OK);
|
|
2336
2886
|
writeFileAtomic(target, text, { mode: 420 });
|
|
2337
2887
|
return { file, status: "written" };
|
|
2338
2888
|
} catch (e) {
|
|
@@ -2342,9 +2892,9 @@ function writeRc(file, text, previous) {
|
|
|
2342
2892
|
}
|
|
2343
2893
|
function knownRcFiles(env, home) {
|
|
2344
2894
|
return [
|
|
2345
|
-
{ shell: "zsh", file:
|
|
2346
|
-
{ shell: "bash", file:
|
|
2347
|
-
{ shell: "bash", file:
|
|
2895
|
+
{ shell: "zsh", file: path15.join(env.ZDOTDIR || home, ".zshrc") },
|
|
2896
|
+
{ shell: "bash", file: path15.join(home, ".bashrc") },
|
|
2897
|
+
{ shell: "bash", file: path15.join(home, ".bash_profile") }
|
|
2348
2898
|
];
|
|
2349
2899
|
}
|
|
2350
2900
|
function installedIn(env, home) {
|
|
@@ -2353,7 +2903,7 @@ function installedIn(env, home) {
|
|
|
2353
2903
|
|
|
2354
2904
|
// src/tmux/client.ts
|
|
2355
2905
|
import { spawnSync } from "node:child_process";
|
|
2356
|
-
import
|
|
2906
|
+
import path16 from "node:path";
|
|
2357
2907
|
var STRIPPED_ENV = [
|
|
2358
2908
|
"CLAUDE_CONFIG_DIR",
|
|
2359
2909
|
"XCLAUDE_ACCOUNT",
|
|
@@ -2383,9 +2933,9 @@ function escapeArg(arg) {
|
|
|
2383
2933
|
}
|
|
2384
2934
|
var TMUX_INSTALL_HINT = process.platform === "darwin" ? "install it with: brew install tmux" : "install it with your package manager, e.g. sudo apt install tmux";
|
|
2385
2935
|
function findTmux(env) {
|
|
2386
|
-
for (const dir of (env.PATH ?? "").split(
|
|
2387
|
-
if (!dir || !
|
|
2388
|
-
const candidate =
|
|
2936
|
+
for (const dir of (env.PATH ?? "").split(path16.delimiter)) {
|
|
2937
|
+
if (!dir || !path16.isAbsolute(dir)) continue;
|
|
2938
|
+
const candidate = path16.join(dir, "tmux");
|
|
2389
2939
|
if (isExecutableFile(candidate)) return candidate;
|
|
2390
2940
|
}
|
|
2391
2941
|
return null;
|
|
@@ -2458,10 +3008,10 @@ function readForcedOrgs(ctx) {
|
|
|
2458
3008
|
}
|
|
2459
3009
|
const files = [];
|
|
2460
3010
|
for (const dir of dirs) {
|
|
2461
|
-
files.push(
|
|
3011
|
+
files.push(path17.join(dir, "managed-settings.json"));
|
|
2462
3012
|
try {
|
|
2463
|
-
for (const name of
|
|
2464
|
-
if (name.endsWith(".json")) files.push(
|
|
3013
|
+
for (const name of fs23.readdirSync(path17.join(dir, "managed-settings.d")).sort()) {
|
|
3014
|
+
if (name.endsWith(".json")) files.push(path17.join(dir, "managed-settings.d", name));
|
|
2465
3015
|
}
|
|
2466
3016
|
} catch {
|
|
2467
3017
|
}
|
|
@@ -2498,7 +3048,7 @@ function readForcedOrgs(ctx) {
|
|
|
2498
3048
|
}
|
|
2499
3049
|
function remoteCleanupDays(dir) {
|
|
2500
3050
|
try {
|
|
2501
|
-
const json = JSON.parse(readFileOrNull(
|
|
3051
|
+
const json = JSON.parse(readFileOrNull(path17.join(dir, "remote-settings.json")) ?? "null");
|
|
2502
3052
|
const value = json?.cleanupPeriodDays ?? json?.settings?.cleanupPeriodDays;
|
|
2503
3053
|
return typeof value === "number" ? value : null;
|
|
2504
3054
|
} catch {
|
|
@@ -2531,8 +3081,11 @@ async function doctorCommand(ctx, args) {
|
|
|
2531
3081
|
repairAccount({ paths: ctx.paths, account: name, table, lockWaitMs: Infinity, replaceWrongLinks: true, log });
|
|
2532
3082
|
}
|
|
2533
3083
|
if (ctx.switches.normalizePaths && table.dirs.includes("plugins")) normalizePaths(ctx.paths, { lockWaitMs: Infinity, log });
|
|
3084
|
+
for (const name of Object.keys(config.accounts)) {
|
|
3085
|
+
if (inherits(config, name)) inheritSettings(ctx.paths, name, { lockWaitMs: Infinity, log });
|
|
3086
|
+
}
|
|
2534
3087
|
if (table.dirs.includes("plugins")) {
|
|
2535
|
-
for (const name of Object.keys(config.accounts)) listPlugins(ctx.paths, name, { lockWaitMs: Infinity, log });
|
|
3088
|
+
for (const name of Object.keys(config.accounts)) listPlugins(ctx.paths, name, { lockWaitMs: Infinity, log, follow: inherits(config, name) });
|
|
2536
3089
|
}
|
|
2537
3090
|
}
|
|
2538
3091
|
report.section("Tools");
|
|
@@ -2554,7 +3107,7 @@ async function doctorCommand(ctx, args) {
|
|
|
2554
3107
|
report.add("ok", `Node ${process.versions.node}`);
|
|
2555
3108
|
report.section("Config");
|
|
2556
3109
|
if (configProblem) report.add("error", configProblem);
|
|
2557
|
-
else report.add("ok", `${t(ctx.paths.config)}${
|
|
3110
|
+
else report.add("ok", `${t(ctx.paths.config)}${fs23.existsSync(ctx.paths.config) ? "" : " (not created yet)"}`);
|
|
2558
3111
|
for (const name of table.refused) report.add("warn", `share.add "${name}" is ignored: never-shared entries and file names can't be shared`);
|
|
2559
3112
|
const names = identities(config);
|
|
2560
3113
|
const logins = /* @__PURE__ */ new Map();
|
|
@@ -2567,7 +3120,10 @@ async function doctorCommand(ctx, args) {
|
|
|
2567
3120
|
if (configProblem) report.add("info", "not checked: fix the config first (account dirs are left alone)");
|
|
2568
3121
|
else if (!names.length) report.add("info", "no accounts yet; add one with: xclaude add <name>");
|
|
2569
3122
|
const storeDev = statOrNull(ctx.paths.store)?.dev;
|
|
2570
|
-
const
|
|
3123
|
+
const pluginState = loadState(ctx.paths);
|
|
3124
|
+
const mainOn = mainPlugins(ctx.paths);
|
|
3125
|
+
const fromMain = mainSettings(ctx.paths);
|
|
3126
|
+
const shared = "settings" in fromMain && Object.keys(inheritable(fromMain.settings)).length ? fromMain.settings : null;
|
|
2571
3127
|
for (const name of names) {
|
|
2572
3128
|
const login = logins.get(name);
|
|
2573
3129
|
report.lines.push(` ${name}${login ? ` ${describeLogin(login)}` : ""}`);
|
|
@@ -2592,9 +3148,36 @@ async function doctorCommand(ctx, args) {
|
|
|
2592
3148
|
}
|
|
2593
3149
|
const unknown = unknownEntries(insp.names, table);
|
|
2594
3150
|
if (unknown.length) report.add("info", `per-account entries xclaude doesn't know: ${unknown.join(", ")}`, 2);
|
|
2595
|
-
const
|
|
2596
|
-
if (
|
|
2597
|
-
|
|
3151
|
+
const choice = config.accounts[name]?.inherit;
|
|
3152
|
+
if (choice === void 0) {
|
|
3153
|
+
if (setupPreview(ctx.paths, name).length) {
|
|
3154
|
+
report.add("info", `hasn't chosen whether to take ~/.claude's settings, plugins and MCP servers; its next launch in a terminal asks (or: xclaude set ${name} --inherit on, or off)`, 2);
|
|
3155
|
+
}
|
|
3156
|
+
} else if (shared && !choice) {
|
|
3157
|
+
report.add("info", `doesn't take ~/.claude/settings.json (turn that on with: xclaude set ${name} --inherit on)`, 2);
|
|
3158
|
+
} else if (shared) {
|
|
3159
|
+
const status = inheritStatus(ctx.paths, name, shared);
|
|
3160
|
+
if (!status) report.add("warn", "its settings.json is a link or isn't a valid JSON object, so ~/.claude/settings.json isn't merged into it", 2);
|
|
3161
|
+
else if (status.pending.length) {
|
|
3162
|
+
report.add("info", `~/.claude/settings.json changes not taken yet: ${status.pending.join(", ")} (the next launch, or --fix, takes them)`, 2);
|
|
3163
|
+
} else {
|
|
3164
|
+
report.add("ok", `takes ~/.claude/settings.json${status.own.length ? `, keeping its own ${status.own.join(", ")}` : ""}`, 2);
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
const servers = inherits(config, name) ? Object.keys(mcpServersFor(ctx.paths, name) ?? {}) : [];
|
|
3168
|
+
if (servers.length) report.add("ok", `gets ~/.claude.json's MCP servers at launch: ${servers.join(", ")}`, 2);
|
|
3169
|
+
const tracked = { off: pluginState.listedPlugins[name] ?? [], on: pluginState.turnedOnPlugins[name] ?? [] };
|
|
3170
|
+
const plan = table.dirs.includes("plugins") ? planPlugins(ctx.paths, name, tracked, inherits(config, name) ? mainOn : null) : null;
|
|
3171
|
+
const them = (ids) => ids.length === 1 ? "it" : "them";
|
|
3172
|
+
const plugins = plan ? plan.add.filter((id) => !plan.turnOn.includes(id)) : [];
|
|
3173
|
+
if (plugins.length) {
|
|
3174
|
+
report.add("info", `not in its /plugin yet, installed under another login: ${plugins.join(", ")} (the next launch, or --fix, lists ${them(plugins)} there, turned off)`, 2);
|
|
3175
|
+
}
|
|
3176
|
+
if (plan?.turnOn.length) {
|
|
3177
|
+
report.add("info", `on in ~/.claude but not here yet: ${plan.turnOn.join(", ")} (the next launch, or --fix, turns ${them(plan.turnOn)} on)`, 2);
|
|
3178
|
+
}
|
|
3179
|
+
if (plan?.turnOff.length) {
|
|
3180
|
+
report.add("info", `off in ~/.claude but still on here: ${plan.turnOff.join(", ")} (the next launch, or --fix, turns ${them(plan.turnOff)} off)`, 2);
|
|
2598
3181
|
}
|
|
2599
3182
|
const dev = statOrNull(dir)?.dev;
|
|
2600
3183
|
if (storeDev !== void 0 && dev !== void 0 && dev !== storeDev) {
|
|
@@ -2624,8 +3207,8 @@ async function doctorCommand(ctx, args) {
|
|
|
2624
3207
|
${s.orgId}`, [...byLogin.get(`${s.email}
|
|
2625
3208
|
${s.orgId}`) ?? [], n]);
|
|
2626
3209
|
}
|
|
2627
|
-
for (const [key,
|
|
2628
|
-
if (
|
|
3210
|
+
for (const [key, same2] of byLogin) {
|
|
3211
|
+
if (same2.length > 1) report.add("warn", `${same2.join(" and ")} use the same login (${key.split("\n")[0]})`);
|
|
2629
3212
|
}
|
|
2630
3213
|
for (const f of configProblem ? [] : strayFolders(ctx.paths, config)) {
|
|
2631
3214
|
const where = t(f.dir);
|
|
@@ -2641,18 +3224,19 @@ ${s.orgId}`) ?? [], n]);
|
|
|
2641
3224
|
}
|
|
2642
3225
|
report.section("Store");
|
|
2643
3226
|
const storeUnknown = storeUnknownEntries(ctx.paths.store, table);
|
|
2644
|
-
if (!
|
|
3227
|
+
if (!fs23.existsSync(ctx.paths.store)) report.add("info", `${t(ctx.paths.store)} doesn't exist yet`);
|
|
2645
3228
|
else if (storeUnknown.length) report.add("info", `entries in ${t(ctx.paths.store)} that no account links to: ${storeUnknown.join(", ")}`);
|
|
2646
3229
|
else report.add("ok", t(ctx.paths.store));
|
|
3230
|
+
if ("problem" in fromMain) report.add("warn", `${fromMain.problem}, so the accounts can't take it`);
|
|
2647
3231
|
const mainDays = remoteCleanupDays(ctx.paths.store);
|
|
2648
3232
|
if (mainDays !== null && mainDays < 30) {
|
|
2649
3233
|
report.add("warn", `the ~/.claude login's organization sets cleanupPeriodDays to ${mainDays}: plain claude deletes everyone's shared transcripts older than ${mainDays} days`);
|
|
2650
3234
|
}
|
|
2651
3235
|
for (const entry of SHARED_DIRS) {
|
|
2652
|
-
const p2 =
|
|
2653
|
-
if (!
|
|
3236
|
+
const p2 = path17.join(ctx.paths.store, entry);
|
|
3237
|
+
if (!fs23.existsSync(p2)) continue;
|
|
2654
3238
|
try {
|
|
2655
|
-
|
|
3239
|
+
fs23.accessSync(p2, fs23.constants.W_OK);
|
|
2656
3240
|
} catch {
|
|
2657
3241
|
report.add("warn", `${t(p2)} isn't writable, so sessions can't save ${entry} there`);
|
|
2658
3242
|
}
|
|
@@ -2682,8 +3266,8 @@ ${s.orgId}`) ?? [], n]);
|
|
|
2682
3266
|
}
|
|
2683
3267
|
|
|
2684
3268
|
// src/shell/init.ts
|
|
2685
|
-
import
|
|
2686
|
-
import
|
|
3269
|
+
import fs24 from "node:fs";
|
|
3270
|
+
import path18 from "node:path";
|
|
2687
3271
|
function header(shell, version) {
|
|
2688
3272
|
return `# xclaude ${version}: ${shell} integration. Written by \`xclaude shell install\`, \`xclaude guard on|off\`
|
|
2689
3273
|
# and after xclaude updates; don't edit, changes are overwritten.
|
|
@@ -2788,7 +3372,7 @@ ${guard ? `
|
|
|
2788
3372
|
${GUARD}` : ""}`;
|
|
2789
3373
|
}
|
|
2790
3374
|
function initFile(paths, shell) {
|
|
2791
|
-
return
|
|
3375
|
+
return path18.join(paths.shell, `init.${shell}`);
|
|
2792
3376
|
}
|
|
2793
3377
|
function writeInitFiles(paths, config, version) {
|
|
2794
3378
|
mkdirPrivate(paths.shell);
|
|
@@ -2796,11 +3380,11 @@ function writeInitFiles(paths, config, version) {
|
|
|
2796
3380
|
writeFileAtomic(initFile(paths, "zsh"), initZsh(version, config.guard), { mode: 420 });
|
|
2797
3381
|
}
|
|
2798
3382
|
function refreshInitFiles(paths, config, version) {
|
|
2799
|
-
if (
|
|
3383
|
+
if (fs24.existsSync(paths.shell)) writeInitFiles(paths, config, version);
|
|
2800
3384
|
}
|
|
2801
3385
|
|
|
2802
3386
|
// package.json
|
|
2803
|
-
var package_default = { version: "0.
|
|
3387
|
+
var package_default = { version: "0.2.0" };
|
|
2804
3388
|
|
|
2805
3389
|
// src/version.ts
|
|
2806
3390
|
var VERSION = package_default.version;
|
|
@@ -2856,19 +3440,30 @@ Commands:
|
|
|
2856
3440
|
Conversations, prompt history, memory and personal content (skills, agents,
|
|
2857
3441
|
commands, plugins\u2026) are shared by every account, so \`xclaude other -c\`
|
|
2858
3442
|
continues the last conversation in this folder, whichever account it ran on.
|
|
2859
|
-
|
|
2860
|
-
|
|
3443
|
+
An account can also take what ~/.claude/settings.json sets (rules, hooks, auto
|
|
3444
|
+
mode\u2026) at every launch, keeping its own changes, with the plugins ~/.claude
|
|
3445
|
+
has on and the MCP servers in ~/.claude.json. Its first launch in a terminal
|
|
3446
|
+
asks; set <name> --inherit on|off changes the answer. A plugin installed under
|
|
3447
|
+
another login shows in /plugin: on when the account takes ~/.claude's setup
|
|
3448
|
+
and ~/.claude has it on, otherwise off.
|
|
2861
3449
|
`;
|
|
2862
3450
|
var COMMAND_HELP = {
|
|
2863
3451
|
add: `Usage: xclaude add <name> [--model M] [--effort E] [--args "\u2026"]
|
|
3452
|
+
[--inherit on|off]
|
|
2864
3453
|
|
|
2865
3454
|
Creates the account, links it to the shared ~/.claude, then starts Claude Code
|
|
2866
3455
|
in it so you can go through the first-run screens and log in with /login.
|
|
3456
|
+
First, when ~/.claude has something to give, it asks whether the account takes
|
|
3457
|
+
~/.claude's setup: what ~/.claude/settings.json sets, the plugins ~/.claude has
|
|
3458
|
+
on and the MCP servers in ~/.claude.json.
|
|
2867
3459
|
|
|
2868
3460
|
--model M default model, e.g. opus or sonnet
|
|
2869
3461
|
--effort E default effort: low, medium, high, xhigh, max or ultracode
|
|
2870
3462
|
--args "\u2026" extra Claude Code arguments for every launch, split like a
|
|
2871
3463
|
shell line
|
|
3464
|
+
--inherit on|off
|
|
3465
|
+
answer that question up front: on takes ~/.claude's setup now
|
|
3466
|
+
and at every launch, off leaves the account as it is
|
|
2872
3467
|
|
|
2873
3468
|
Names use lowercase letters, digits and dashes, and start with a letter.
|
|
2874
3469
|
`,
|
|
@@ -2878,9 +3473,11 @@ Names use lowercase letters, digits and dashes, and start with a letter.
|
|
|
2878
3473
|
Removes an account: stops its background sessions, logs it out, and deletes its
|
|
2879
3474
|
login, settings and caches. Shared conversations and content stay in ~/.claude.
|
|
2880
3475
|
|
|
2881
|
-
Its folder keeps
|
|
2882
|
-
|
|
2883
|
-
|
|
3476
|
+
Its folder keeps its links into ~/.claude, so its old conversations can still
|
|
3477
|
+
open their saved long outputs, and files of its own, such as a CLAUDE.md you
|
|
3478
|
+
edited with /memory. Once it holds only links, run rm on the name again to
|
|
3479
|
+
delete that leftover folder, or --leftovers to delete every one, after a
|
|
3480
|
+
confirmation.
|
|
2884
3481
|
|
|
2885
3482
|
--keep-login don't log the account out
|
|
2886
3483
|
--leftovers delete the leftover folders of all removed accounts
|
|
@@ -2893,7 +3490,7 @@ defaults, and each one's config dir (the CLAUDE_CONFIG_DIR to use elsewhere,
|
|
|
2893
3490
|
e.g. in an editor's environment settings).
|
|
2894
3491
|
`,
|
|
2895
3492
|
set: `Usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"]
|
|
2896
|
-
[--unset model|effort|args]
|
|
3493
|
+
[--unset model|effort|args] [--inherit on|off|reset]
|
|
2897
3494
|
xclaude set main --enable|--disable
|
|
2898
3495
|
|
|
2899
3496
|
Changes an account's launch defaults; with no options, prints them.
|
|
@@ -2903,6 +3500,14 @@ Arguments typed at launch always win over these defaults.
|
|
|
2903
3500
|
--effort E default effort: low, medium, high, xhigh, max or ultracode
|
|
2904
3501
|
--args "\u2026" extra Claude Code arguments, split like a shell line
|
|
2905
3502
|
--unset KEY clear model, effort or args
|
|
3503
|
+
--inherit on|off|reset
|
|
3504
|
+
on: take what ~/.claude/settings.json sets at every launch,
|
|
3505
|
+
except login-bound settings such as env, with the plugins
|
|
3506
|
+
~/.claude has on and the MCP servers in ~/.claude.json; the
|
|
3507
|
+
account's own changes stay. off: stop, and take back what it
|
|
3508
|
+
took and hasn't changed since. reset: take everything again.
|
|
3509
|
+
Until one is set, the account's first launch in a terminal
|
|
3510
|
+
asks.
|
|
2906
3511
|
--enable (main only) use the login in ~/.claude as the account "main"
|
|
2907
3512
|
--disable (main only) turn it off again
|
|
2908
3513
|
`,
|
|
@@ -2948,7 +3553,8 @@ machine-managed settings and the environment. Exits non-zero if something is
|
|
|
2948
3553
|
wrong.
|
|
2949
3554
|
|
|
2950
3555
|
--fix repair links, merge left-behind directories, fix CLAUDE.md stubs,
|
|
2951
|
-
|
|
3556
|
+
merge ~/.claude/settings.json into the accounts that take it,
|
|
3557
|
+
list plugins installed under other logins
|
|
2952
3558
|
`,
|
|
2953
3559
|
help: `Usage: xclaude help [command]
|
|
2954
3560
|
`
|
|
@@ -2964,13 +3570,13 @@ function wantsHelp(args) {
|
|
|
2964
3570
|
}
|
|
2965
3571
|
|
|
2966
3572
|
// src/shell/complete.ts
|
|
2967
|
-
import
|
|
2968
|
-
import
|
|
3573
|
+
import fs26 from "node:fs";
|
|
3574
|
+
import path20 from "node:path";
|
|
2969
3575
|
|
|
2970
3576
|
// src/claude/help.ts
|
|
2971
3577
|
import { spawnSync as spawnSync3 } from "node:child_process";
|
|
2972
|
-
import
|
|
2973
|
-
import
|
|
3578
|
+
import fs25 from "node:fs";
|
|
3579
|
+
import path19 from "node:path";
|
|
2974
3580
|
var FALLBACK_SUBCOMMANDS = ["auth", "mcp", "plugin", "agents", "daemon", "doctor", "update", "install", "setup-token"];
|
|
2975
3581
|
var opt = (names, value, placeholder, choices) => ({
|
|
2976
3582
|
names,
|
|
@@ -3092,12 +3698,12 @@ function loadHelp(claude, cacheDir, env) {
|
|
|
3092
3698
|
const real = realpathOrNull(claude) ?? claude;
|
|
3093
3699
|
let st;
|
|
3094
3700
|
try {
|
|
3095
|
-
st =
|
|
3701
|
+
st = fs25.statSync(real);
|
|
3096
3702
|
} catch {
|
|
3097
3703
|
return FALLBACK_HELP;
|
|
3098
3704
|
}
|
|
3099
3705
|
const key = { path: real, mtimeMs: st.mtimeMs, size: st.size };
|
|
3100
|
-
const file =
|
|
3706
|
+
const file = path19.join(cacheDir, "claude-help.json");
|
|
3101
3707
|
try {
|
|
3102
3708
|
const cached = JSON.parse(readFileOrNull(file) ?? "null");
|
|
3103
3709
|
if (cached && cached.key.path === key.path && cached.key.mtimeMs === key.mtimeMs && cached.key.size === key.size) {
|
|
@@ -3190,14 +3796,14 @@ function logicalWords(shell, words2, cword) {
|
|
|
3190
3796
|
return { prior, cur };
|
|
3191
3797
|
}
|
|
3192
3798
|
function suggestLabel(cwd) {
|
|
3193
|
-
const label =
|
|
3799
|
+
const label = path20.basename(cwd).replace(/[^A-Za-z0-9_-]+/g, "-").replace(/^[^A-Za-z0-9]+/, "").slice(0, 64);
|
|
3194
3800
|
return label || null;
|
|
3195
3801
|
}
|
|
3196
3802
|
function resumeCandidates(store, cwd) {
|
|
3197
|
-
const dir =
|
|
3803
|
+
const dir = path20.join(store, "projects", cwd.replace(/[^a-zA-Z0-9]/g, "-"));
|
|
3198
3804
|
let files;
|
|
3199
3805
|
try {
|
|
3200
|
-
files =
|
|
3806
|
+
files = fs26.readdirSync(dir).filter((n) => n.endsWith(".jsonl")).map((n) => ({ id: n.slice(0, -".jsonl".length), file: path20.join(dir, n), mtime: fs26.statSync(path20.join(dir, n)).mtimeMs }));
|
|
3201
3807
|
} catch {
|
|
3202
3808
|
return [];
|
|
3203
3809
|
}
|
|
@@ -3209,12 +3815,12 @@ function resumeCandidates(store, cwd) {
|
|
|
3209
3815
|
function sessionTitle(file) {
|
|
3210
3816
|
let head;
|
|
3211
3817
|
try {
|
|
3212
|
-
const fd =
|
|
3818
|
+
const fd = fs26.openSync(file, "r");
|
|
3213
3819
|
try {
|
|
3214
3820
|
const buf = Buffer.alloc(16384);
|
|
3215
|
-
head = buf.subarray(0,
|
|
3821
|
+
head = buf.subarray(0, fs26.readSync(fd, buf, 0, buf.length, 0)).toString("utf8");
|
|
3216
3822
|
} finally {
|
|
3217
|
-
|
|
3823
|
+
fs26.closeSync(fd);
|
|
3218
3824
|
}
|
|
3219
3825
|
} catch {
|
|
3220
3826
|
return null;
|
|
@@ -3256,7 +3862,7 @@ var Completer = class {
|
|
|
3256
3862
|
claudeHelp() {
|
|
3257
3863
|
if (this.help) return this.help;
|
|
3258
3864
|
this.help = FALLBACK_HELP;
|
|
3259
|
-
if (
|
|
3865
|
+
if (fs26.existsSync(this.ctx.paths.xhome)) {
|
|
3260
3866
|
try {
|
|
3261
3867
|
const claude = findClaude(this.ctx.env, this.config, this.ctx.paths.home, this.ctx.selfPath);
|
|
3262
3868
|
this.help = loadHelp(claude, this.ctx.paths.cache, this.ctx.env);
|
|
@@ -3287,7 +3893,7 @@ var Completer = class {
|
|
|
3287
3893
|
if (first.startsWith("-")) return this.claudeArgs(args, cur);
|
|
3288
3894
|
switch (first) {
|
|
3289
3895
|
case "add":
|
|
3290
|
-
return this.own(rest, cur, { "--model": "model", "--effort": "effort", "--args": "text" }, 1, () => []);
|
|
3896
|
+
return this.own(rest, cur, { "--model": "model", "--effort": "effort", "--args": "text", "--inherit": "inherit-add" }, 1, () => []);
|
|
3291
3897
|
case "rm":
|
|
3292
3898
|
if (rest.includes("--leftovers")) return this.own(rest, cur, { "-y": null }, 0, () => []);
|
|
3293
3899
|
return this.own(
|
|
@@ -3302,7 +3908,7 @@ var Completer = class {
|
|
|
3302
3908
|
return this.own(
|
|
3303
3909
|
rest,
|
|
3304
3910
|
cur,
|
|
3305
|
-
{ "--model": "model", "--effort": "effort", "--args": "text", "--unset": "unset", "--enable": null, "--disable": null },
|
|
3911
|
+
{ "--model": "model", "--effort": "effort", "--args": "text", "--unset": "unset", "--inherit": "inherit", "--enable": null, "--disable": null },
|
|
3306
3912
|
1,
|
|
3307
3913
|
() => [...Object.keys(this.config.accounts), MAIN]
|
|
3308
3914
|
);
|
|
@@ -3340,7 +3946,7 @@ var Completer = class {
|
|
|
3340
3946
|
return words(positional(), describe);
|
|
3341
3947
|
}
|
|
3342
3948
|
ownValues(kind, prefix) {
|
|
3343
|
-
const values = kind === "model" ? MODEL_ALIASES : kind === "effort" ? EFFORTS : kind === "unset" ? ["model", "effort", "args"] : [];
|
|
3949
|
+
const values = kind === "model" ? MODEL_ALIASES : kind === "effort" ? EFFORTS : kind === "unset" ? ["model", "effort", "args"] : kind === "inherit" ? ["on", "off", "reset"] : kind === "inherit-add" ? ["on", "off"] : [];
|
|
3344
3950
|
return words(values.map((v) => `${prefix}${v}`));
|
|
3345
3951
|
}
|
|
3346
3952
|
claudeFlags() {
|
|
@@ -3443,14 +4049,14 @@ function formatCompletion(c, shell, cur) {
|
|
|
3443
4049
|
|
|
3444
4050
|
// src/update.ts
|
|
3445
4051
|
import { spawnSync as spawnSync4 } from "node:child_process";
|
|
3446
|
-
import
|
|
4052
|
+
import path21 from "node:path";
|
|
3447
4053
|
var PACKAGE = "xclaude-cli";
|
|
3448
4054
|
var REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE}/latest`;
|
|
3449
4055
|
var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
3450
4056
|
var FETCH_TIMEOUT_MS = 1e4;
|
|
3451
4057
|
var SEMVER = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
3452
4058
|
function checkFile(paths) {
|
|
3453
|
-
return
|
|
4059
|
+
return path21.join(paths.cache, "update-check.json");
|
|
3454
4060
|
}
|
|
3455
4061
|
function readUpdateCheck(paths) {
|
|
3456
4062
|
try {
|
|
@@ -3572,24 +4178,24 @@ async function lsCommand(ctx, config, args) {
|
|
|
3572
4178
|
}
|
|
3573
4179
|
|
|
3574
4180
|
// src/core/prompt.ts
|
|
3575
|
-
import
|
|
4181
|
+
import fs27 from "node:fs";
|
|
3576
4182
|
function confirm(question) {
|
|
3577
|
-
|
|
4183
|
+
fs27.writeSync(2, `${question} [y/N] `);
|
|
3578
4184
|
let fd;
|
|
3579
4185
|
try {
|
|
3580
|
-
fd =
|
|
4186
|
+
fd = fs27.openSync("/dev/tty", "r");
|
|
3581
4187
|
} catch {
|
|
3582
4188
|
return false;
|
|
3583
4189
|
}
|
|
3584
4190
|
try {
|
|
3585
4191
|
const buf = Buffer.alloc(256);
|
|
3586
|
-
const n =
|
|
4192
|
+
const n = fs27.readSync(fd, buf, 0, buf.length, null);
|
|
3587
4193
|
const answer = buf.subarray(0, n).toString("utf8").trim().toLowerCase();
|
|
3588
4194
|
return answer === "y" || answer === "yes";
|
|
3589
4195
|
} catch {
|
|
3590
4196
|
return false;
|
|
3591
4197
|
} finally {
|
|
3592
|
-
|
|
4198
|
+
fs27.closeSync(fd);
|
|
3593
4199
|
}
|
|
3594
4200
|
}
|
|
3595
4201
|
|
|
@@ -3647,7 +4253,7 @@ async function rmCommand(ctx, config, args, listSessions2 = () => []) {
|
|
|
3647
4253
|
}
|
|
3648
4254
|
if (!yes) {
|
|
3649
4255
|
if (!ctx.tty.stdin) throw new UsageError(`removing ${name} needs a confirmation: run it in a terminal, or pass -y`);
|
|
3650
|
-
const ok = confirm(`Remove account ${name}? Its login and
|
|
4256
|
+
const ok = confirm(`Remove account ${name}? Its login, settings.json, keybindings.json and caches go; files of its own stay in its folder, shared conversations and content in ~/.claude.`);
|
|
3651
4257
|
if (!ok) {
|
|
3652
4258
|
log("xclaude: nothing removed");
|
|
3653
4259
|
throw new Cancelled();
|
|
@@ -3700,6 +4306,8 @@ function forget(ctx, name) {
|
|
|
3700
4306
|
delete fresh.accounts[name];
|
|
3701
4307
|
saveConfig(ctx.paths, fresh);
|
|
3702
4308
|
updateState(ctx.paths, (s) => forgetAccount(s, name));
|
|
4309
|
+
forgetInherit(ctx.paths, name);
|
|
4310
|
+
forgetMcp(ctx.paths, name);
|
|
3703
4311
|
}
|
|
3704
4312
|
function whyNot(f, where) {
|
|
3705
4313
|
switch (f.kind) {
|
|
@@ -3773,9 +4381,9 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
3773
4381
|
log(`xclaude: not logging ${name} out: couldn't read the login of ${unknown.map((o) => o.label).join(", ")}, which may use the same email`);
|
|
3774
4382
|
return;
|
|
3775
4383
|
}
|
|
3776
|
-
const
|
|
3777
|
-
if (
|
|
3778
|
-
log(`xclaude: not logging ${name} out: ${
|
|
4384
|
+
const same2 = others.filter((_, i) => theirs[i].loggedIn && theirs[i].email === mine.email);
|
|
4385
|
+
if (same2.length) {
|
|
4386
|
+
log(`xclaude: not logging ${name} out: ${same2.map((o) => o.label).join(", ")} ${same2.length > 1 ? "are" : "is"} logged in with the same email (${mine.email}), and a logout could affect ${same2.length > 1 ? "them" : "it"}`);
|
|
3779
4387
|
return;
|
|
3780
4388
|
}
|
|
3781
4389
|
}
|
|
@@ -3784,10 +4392,11 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
3784
4392
|
}
|
|
3785
4393
|
|
|
3786
4394
|
// src/commands/set.ts
|
|
3787
|
-
var USAGE3 = 'usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"] [--unset model|effort|args], or xclaude set main --enable|--disable';
|
|
4395
|
+
var USAGE3 = 'usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"] [--unset model|effort|args] [--inherit on|off|reset], or xclaude set main --enable|--disable';
|
|
3788
4396
|
var KEYS = ["model", "effort", "args"];
|
|
4397
|
+
var INHERIT = ["on", "off", "reset"];
|
|
3789
4398
|
function setCommand(ctx, config, args) {
|
|
3790
|
-
const p = parseOptions(args, { ...DEFAULT_OPTIONS, unset: { value: true, repeat: true }, enable: {}, disable: {} }, "set");
|
|
4399
|
+
const p = parseOptions(args, { ...DEFAULT_OPTIONS, unset: { value: true, repeat: true }, inherit: { value: true }, enable: {}, disable: {} }, "set");
|
|
3791
4400
|
const [name, ...extra] = p.positionals;
|
|
3792
4401
|
if (!name || extra.length) throw new UsageError(USAGE3);
|
|
3793
4402
|
const isMain = name === MAIN;
|
|
@@ -3802,6 +4411,11 @@ function setCommand(ctx, config, args) {
|
|
|
3802
4411
|
if (!KEYS.includes(key)) throw new UsageError(`--unset takes model, effort or args, not "${key}"`);
|
|
3803
4412
|
if (p.values[key] !== void 0) throw new UsageError(`--${key} and --unset ${key} contradict each other`);
|
|
3804
4413
|
}
|
|
4414
|
+
const inherit = stringOption(p, "inherit");
|
|
4415
|
+
if (inherit !== void 0) {
|
|
4416
|
+
if (isMain) throw new UsageError("--inherit is for accounts; the main identity is ~/.claude itself");
|
|
4417
|
+
if (!INHERIT.includes(inherit)) throw new UsageError(`--inherit takes on, off or reset, not "${inherit}"`);
|
|
4418
|
+
}
|
|
3805
4419
|
if (Object.keys(p.values).length) {
|
|
3806
4420
|
applyDefaultOptions(p, ctx.paths.home, target);
|
|
3807
4421
|
for (const key of unset) {
|
|
@@ -3809,16 +4423,34 @@ function setCommand(ctx, config, args) {
|
|
|
3809
4423
|
else if (key === "model") target.model = null;
|
|
3810
4424
|
else target.effort = null;
|
|
3811
4425
|
}
|
|
4426
|
+
if (inherit !== void 0) target.inherit = inherit !== "off";
|
|
3812
4427
|
if (p.values.enable) config.main.enabled = true;
|
|
3813
4428
|
if (p.values.disable) config.main.enabled = false;
|
|
3814
4429
|
saveConfig(ctx.paths, config);
|
|
3815
4430
|
}
|
|
4431
|
+
if (inherit !== void 0) applyInherit(ctx, name, inherit);
|
|
3816
4432
|
let head = name;
|
|
3817
4433
|
if (isMain) head += config.main.enabled ? " (enabled: the login in ~/.claude)" : " (disabled; enable with: xclaude set main --enable)";
|
|
3818
4434
|
ctx.io.out(`${head}
|
|
3819
|
-
${formatDefaults(target)}`);
|
|
4435
|
+
${formatDefaults(target, !isMain)}`);
|
|
3820
4436
|
return EXIT_OK;
|
|
3821
4437
|
}
|
|
4438
|
+
function applyInherit(ctx, name, inherit) {
|
|
4439
|
+
const log = (line) => ctx.io.err(`${line}
|
|
4440
|
+
`);
|
|
4441
|
+
const opts = { lockWaitMs: Infinity, log };
|
|
4442
|
+
if (inherit === "off") {
|
|
4443
|
+
if (!stopInheriting(ctx.paths, name, opts)) {
|
|
4444
|
+
log(`xclaude: Claude Code wrote ${name}'s settings.json meanwhile, so what it took from ~/.claude/settings.json is still there; run this again`);
|
|
4445
|
+
}
|
|
4446
|
+
listPlugins(ctx.paths, name, { ...opts, turnOffOwn: true });
|
|
4447
|
+
forgetMcp(ctx.paths, name);
|
|
4448
|
+
return;
|
|
4449
|
+
}
|
|
4450
|
+
if (inherit === "reset") resetInherit(ctx.paths, name, opts);
|
|
4451
|
+
else inheritSettings(ctx.paths, name, opts);
|
|
4452
|
+
listPlugins(ctx.paths, name, { ...opts, follow: true });
|
|
4453
|
+
}
|
|
3822
4454
|
|
|
3823
4455
|
// src/commands/shell.ts
|
|
3824
4456
|
function install(ctx, config, args) {
|
|
@@ -3886,8 +4518,8 @@ function shellCommand(ctx, config, args) {
|
|
|
3886
4518
|
}
|
|
3887
4519
|
|
|
3888
4520
|
// src/commands/tmux.ts
|
|
3889
|
-
import
|
|
3890
|
-
import
|
|
4521
|
+
import fs29 from "node:fs";
|
|
4522
|
+
import path22 from "node:path";
|
|
3891
4523
|
|
|
3892
4524
|
// src/launch/args.ts
|
|
3893
4525
|
function hasFlag(args, flag) {
|
|
@@ -3961,141 +4593,6 @@ function needsHelp(defaults, userArgs) {
|
|
|
3961
4593
|
return defaults.args.length > 0 || first !== void 0 && !first.startsWith("-");
|
|
3962
4594
|
}
|
|
3963
4595
|
|
|
3964
|
-
// src/launch/picker.ts
|
|
3965
|
-
import fs25 from "node:fs";
|
|
3966
|
-
import tty2 from "node:tty";
|
|
3967
|
-
function parseKeys(chunk) {
|
|
3968
|
-
const keys = [];
|
|
3969
|
-
let i = 0;
|
|
3970
|
-
while (i < chunk.length) {
|
|
3971
|
-
const rest = chunk.slice(i);
|
|
3972
|
-
if (rest.startsWith("\x1B[A") || rest.startsWith("\x1BOA")) {
|
|
3973
|
-
keys.push("up");
|
|
3974
|
-
i += 3;
|
|
3975
|
-
} else if (rest.startsWith("\x1B[B") || rest.startsWith("\x1BOB")) {
|
|
3976
|
-
keys.push("down");
|
|
3977
|
-
i += 3;
|
|
3978
|
-
} else if (rest.startsWith("\x1B[")) {
|
|
3979
|
-
let j = 2;
|
|
3980
|
-
while (j < rest.length && !/[@-~]/.test(rest[j])) j++;
|
|
3981
|
-
keys.push(null);
|
|
3982
|
-
i += j + 1;
|
|
3983
|
-
} else {
|
|
3984
|
-
const c = rest[0];
|
|
3985
|
-
if (c === "\x1B") keys.push(rest.length === 1 ? "cancel" : null);
|
|
3986
|
-
else if (c === "") keys.push("cancel");
|
|
3987
|
-
else if (c === "\r" || c === "\n") keys.push("enter");
|
|
3988
|
-
else if (c === "k") keys.push("up");
|
|
3989
|
-
else if (c === "j") keys.push("down");
|
|
3990
|
-
else if (c >= "1" && c <= "9") keys.push({ jump: Number(c) - 1 });
|
|
3991
|
-
else keys.push(null);
|
|
3992
|
-
i++;
|
|
3993
|
-
}
|
|
3994
|
-
}
|
|
3995
|
-
return keys;
|
|
3996
|
-
}
|
|
3997
|
-
function step(index, count, key) {
|
|
3998
|
-
if (key === "up") return { index: (index - 1 + count) % count };
|
|
3999
|
-
if (key === "down") return { index: (index + 1) % count };
|
|
4000
|
-
if (key === "enter") return { index, done: "chosen" };
|
|
4001
|
-
if (key === "cancel") return { index, done: "cancelled" };
|
|
4002
|
-
if (key && typeof key === "object" && key.jump < count) return { index: key.jump };
|
|
4003
|
-
return { index };
|
|
4004
|
-
}
|
|
4005
|
-
var dim = (s) => `\x1B[2m${s}\x1B[22m`;
|
|
4006
|
-
var bold = (s) => `\x1B[1m${s}\x1B[22m`;
|
|
4007
|
-
function fit(text, width) {
|
|
4008
|
-
return width > 0 && text.length > width ? `${text.slice(0, Math.max(0, width - 1))}\u2026` : text;
|
|
4009
|
-
}
|
|
4010
|
-
function render(title, items, index, width = 0) {
|
|
4011
|
-
const labelWidth = Math.max(...items.map((it) => it.label.length));
|
|
4012
|
-
const lines = [bold(fit(title, width))];
|
|
4013
|
-
items.forEach((it, i) => {
|
|
4014
|
-
const marker = i === index ? "\u276F" : " ";
|
|
4015
|
-
const num = i < 9 ? String(i + 1) : " ";
|
|
4016
|
-
const label = it.label.padEnd(labelWidth);
|
|
4017
|
-
const text = fit(`${marker} ${num} ${label}${it.detail ? ` ${it.detail}` : ""}`, width);
|
|
4018
|
-
const head = text.slice(0, 5 + labelWidth);
|
|
4019
|
-
const tail = text.slice(5 + labelWidth);
|
|
4020
|
-
lines.push(i === index ? `${bold(head)}${dim(tail)}` : `${head}${dim(tail)}`);
|
|
4021
|
-
});
|
|
4022
|
-
lines.push(dim(fit("\u2191/\u2193 or j/k to move \xB7 1\u20139 to jump \xB7 Enter to choose \xB7 Esc to cancel", width)));
|
|
4023
|
-
return lines;
|
|
4024
|
-
}
|
|
4025
|
-
function preselect(names, state, cwd) {
|
|
4026
|
-
for (const candidate of [state.lastAccountByDir[cwd], state.lastUsedAccount]) {
|
|
4027
|
-
if (candidate && names.includes(candidate)) return names.indexOf(candidate);
|
|
4028
|
-
}
|
|
4029
|
-
return 0;
|
|
4030
|
-
}
|
|
4031
|
-
function openKeyboard() {
|
|
4032
|
-
let fd;
|
|
4033
|
-
try {
|
|
4034
|
-
fd = fs25.openSync("/dev/tty", "r");
|
|
4035
|
-
} catch {
|
|
4036
|
-
throw new UsageError("account required: there's no terminal to show the picker on (xclaude <account> [claude args\u2026])");
|
|
4037
|
-
}
|
|
4038
|
-
const input = new tty2.ReadStream(fd);
|
|
4039
|
-
let columns = 0;
|
|
4040
|
-
try {
|
|
4041
|
-
const size = [];
|
|
4042
|
-
const handle = input._handle;
|
|
4043
|
-
if (handle?.getWindowSize?.(size) === 0) columns = size[0] ?? 0;
|
|
4044
|
-
} catch {
|
|
4045
|
-
}
|
|
4046
|
-
return {
|
|
4047
|
-
input,
|
|
4048
|
-
columns,
|
|
4049
|
-
close: () => {
|
|
4050
|
-
input.pause();
|
|
4051
|
-
input.unref();
|
|
4052
|
-
}
|
|
4053
|
-
};
|
|
4054
|
-
}
|
|
4055
|
-
function pick(title, items, initial) {
|
|
4056
|
-
const keyboard = openKeyboard();
|
|
4057
|
-
const { input } = keyboard;
|
|
4058
|
-
const write = (s) => fs25.writeSync(2, s);
|
|
4059
|
-
let index = Math.min(Math.max(initial, 0), items.length - 1);
|
|
4060
|
-
let drawn = 0;
|
|
4061
|
-
const draw = () => {
|
|
4062
|
-
const lines = render(title, items, index, keyboard.columns);
|
|
4063
|
-
write(`${drawn ? `\r\x1B[${drawn}A` : ""}\x1B[J${lines.join("\n")}
|
|
4064
|
-
`);
|
|
4065
|
-
drawn = lines.length;
|
|
4066
|
-
};
|
|
4067
|
-
return new Promise((resolve, reject) => {
|
|
4068
|
-
const finish = (result) => {
|
|
4069
|
-
input.off("data", onData);
|
|
4070
|
-
try {
|
|
4071
|
-
input.setRawMode(false);
|
|
4072
|
-
} finally {
|
|
4073
|
-
keyboard.close();
|
|
4074
|
-
write(`\r\x1B[${drawn}A\x1B[J\x1B[?25h`);
|
|
4075
|
-
}
|
|
4076
|
-
if (result === "chosen") resolve(index);
|
|
4077
|
-
else reject(new Cancelled());
|
|
4078
|
-
};
|
|
4079
|
-
const onData = (chunk) => {
|
|
4080
|
-
for (const key of parseKeys(chunk)) {
|
|
4081
|
-
const next = step(index, items.length, key);
|
|
4082
|
-
index = next.index;
|
|
4083
|
-
if (next.done) {
|
|
4084
|
-
finish(next.done);
|
|
4085
|
-
return;
|
|
4086
|
-
}
|
|
4087
|
-
}
|
|
4088
|
-
draw();
|
|
4089
|
-
};
|
|
4090
|
-
input.setRawMode(true);
|
|
4091
|
-
input.setEncoding("utf8");
|
|
4092
|
-
input.on("data", onData);
|
|
4093
|
-
input.resume();
|
|
4094
|
-
write("\x1B[?25l");
|
|
4095
|
-
draw();
|
|
4096
|
-
});
|
|
4097
|
-
}
|
|
4098
|
-
|
|
4099
4596
|
// src/launch/launch.ts
|
|
4100
4597
|
function identityItems(config) {
|
|
4101
4598
|
return identities(config).map((name) => {
|
|
@@ -4136,17 +4633,33 @@ function repairForLaunch(ctx, config, account, log) {
|
|
|
4136
4633
|
if (ctx.switches.normalizePaths && table.dirs.includes("plugins")) {
|
|
4137
4634
|
normalizePaths(ctx.paths, { lockWaitMs: LAUNCH_LOCK_WAIT_MS, log });
|
|
4138
4635
|
}
|
|
4139
|
-
if (
|
|
4636
|
+
if (inherits(config, account) && !res.busy) inheritSettings(ctx.paths, account, { lockWaitMs: LAUNCH_LOCK_WAIT_MS, log });
|
|
4637
|
+
if (table.dirs.includes("plugins") && !res.busy) {
|
|
4638
|
+
listPlugins(ctx.paths, account, { lockWaitMs: LAUNCH_LOCK_WAIT_MS, log, follow: inherits(config, account) });
|
|
4639
|
+
}
|
|
4140
4640
|
return unknownEntries(res.names, table);
|
|
4141
4641
|
}
|
|
4642
|
+
function mcpForLaunch(ctx, config, account, defaults, userArgs, help) {
|
|
4643
|
+
if (!inherits(config, account) || isSubcommand(userArgs[0], help)) return null;
|
|
4644
|
+
if (hasFlag(userArgs, "--strict-mcp-config") || hasFlag(defaults.args, "--strict-mcp-config")) return null;
|
|
4645
|
+
try {
|
|
4646
|
+
return prepareMcp(ctx.paths, account);
|
|
4647
|
+
} catch (e) {
|
|
4648
|
+
ctx.io.err(`xclaude: couldn't pass ~/.claude.json's MCP servers to ${account} (${e.message}); launching without them
|
|
4649
|
+
`);
|
|
4650
|
+
return null;
|
|
4651
|
+
}
|
|
4652
|
+
}
|
|
4142
4653
|
async function launch(ctx, config, account, userArgs) {
|
|
4143
4654
|
const defaults = defaultsOf(config, account);
|
|
4144
4655
|
if (!defaults) throw unknownAccount(account, config);
|
|
4145
4656
|
const isMain = account === MAIN;
|
|
4146
|
-
const
|
|
4657
|
+
const settled = isMain ? config : await settleInherit(ctx, config, account);
|
|
4658
|
+
const unknown = isMain ? [] : prepareAccount(ctx, settled, account);
|
|
4147
4659
|
const claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
4148
4660
|
const help = needsHelp(defaults, userArgs) ? loadHelp(claude, ctx.paths.cache, ctx.env) : null;
|
|
4149
|
-
const
|
|
4661
|
+
const mcp = isMain ? null : mcpForLaunch(ctx, settled, account, defaults, userArgs, help);
|
|
4662
|
+
const args = [...mcp ? [`--mcp-config=${mcp}`] : [], ...buildArgs(defaults, userArgs, help)];
|
|
4150
4663
|
const env = buildEnv(ctx.env, { name: account, configDir: isMain ? null : accountDir(ctx.paths, account) });
|
|
4151
4664
|
try {
|
|
4152
4665
|
updateState(ctx.paths, (state) => {
|
|
@@ -4162,7 +4675,7 @@ async function launch(ctx, config, account, userArgs) {
|
|
|
4162
4675
|
|
|
4163
4676
|
// src/tmux/claude-state.ts
|
|
4164
4677
|
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
4165
|
-
import
|
|
4678
|
+
import fs28 from "node:fs";
|
|
4166
4679
|
function describeAgent(e) {
|
|
4167
4680
|
if (e.status === "waiting") return { text: e.waitingFor ? `needs input (${e.waitingFor})` : "needs input", rank: 3 };
|
|
4168
4681
|
if (e.status === "busy") return { text: "working", rank: 2 };
|
|
@@ -4170,10 +4683,10 @@ function describeAgent(e) {
|
|
|
4170
4683
|
return null;
|
|
4171
4684
|
}
|
|
4172
4685
|
function parentLookup() {
|
|
4173
|
-
if (process.platform === "linux" &&
|
|
4686
|
+
if (process.platform === "linux" && fs28.existsSync("/proc/self/stat")) {
|
|
4174
4687
|
return (pid) => {
|
|
4175
4688
|
try {
|
|
4176
|
-
const stat =
|
|
4689
|
+
const stat = fs28.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
4177
4690
|
const ppid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
4178
4691
|
return Number.isFinite(ppid) ? ppid : null;
|
|
4179
4692
|
} catch {
|
|
@@ -4299,8 +4812,8 @@ async function tmuxNew(ctx, config, args) {
|
|
|
4299
4812
|
account ??= await chooseAccount(ctx, config, `Run Claude Code in tmux session "${req.label}" as`);
|
|
4300
4813
|
if (!defaultsOf(config, account)) throw unknownAccount(account, config);
|
|
4301
4814
|
}
|
|
4302
|
-
const dir =
|
|
4303
|
-
if (!
|
|
4815
|
+
const dir = path22.resolve(ctx.cwd, req.dir ?? ".");
|
|
4816
|
+
if (!fs29.statSync(dir, { throwIfNoEntry: false })?.isDirectory()) throw new XError(`--dir ${req.dir}: not a directory`);
|
|
4304
4817
|
const existing = findByLabel(listSessions(tmux), req.label);
|
|
4305
4818
|
if (existing) {
|
|
4306
4819
|
throw new XError(`"${req.label}" is running (${describeSession(existing, ctx.paths.home)}): xclaude tmux attach ${req.label}, or kill it first`);
|