xclaude-cli 0.1.3 → 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 +1059 -409
- 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,9 +712,85 @@ 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/
|
|
702
|
-
|
|
703
|
-
|
|
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
|
|
792
|
+
import fs10 from "node:fs";
|
|
793
|
+
import path6 from "node:path";
|
|
704
794
|
|
|
705
795
|
// src/core/lock.ts
|
|
706
796
|
import fs9 from "node:fs";
|
|
@@ -785,13 +875,641 @@ function takeOverStale(lockPath) {
|
|
|
785
875
|
}
|
|
786
876
|
}
|
|
787
877
|
|
|
788
|
-
// src/link/
|
|
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) {
|
|
891
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
892
|
+
}
|
|
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) {
|
|
953
|
+
try {
|
|
954
|
+
const v = JSON.parse(readFileOrNull(basePath(paths, account)) ?? "null");
|
|
955
|
+
return isObject2(v) ? v : void 0;
|
|
956
|
+
} catch {
|
|
957
|
+
return void 0;
|
|
958
|
+
}
|
|
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;
|
|
995
|
+
}
|
|
996
|
+
}
|
|
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;
|
|
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;
|
|
1058
|
+
}
|
|
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)}`);
|
|
1068
|
+
}
|
|
1069
|
+
forgetInherit(paths, account);
|
|
1070
|
+
return true;
|
|
1071
|
+
} finally {
|
|
1072
|
+
lock.release();
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
function resetInherit(paths, account, opts) {
|
|
1076
|
+
forgetInherit(paths, account);
|
|
1077
|
+
return inheritSettings(paths, account, opts);
|
|
1078
|
+
}
|
|
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])) };
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// src/link/mcp.ts
|
|
789
1087
|
import fs11 from "node:fs";
|
|
790
1088
|
import path7 from "node:path";
|
|
1089
|
+
function isRecord2(v) {
|
|
1090
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1091
|
+
}
|
|
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;
|
|
1099
|
+
}
|
|
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));
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
// src/link/plugins.ts
|
|
1131
|
+
import fs12 from "node:fs";
|
|
1132
|
+
import path8 from "node:path";
|
|
1133
|
+
function isRecord3(v) {
|
|
1134
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1135
|
+
}
|
|
1136
|
+
function userPlugins(paths) {
|
|
1137
|
+
const dir = realpathOrNull(path8.join(paths.store, "plugins"));
|
|
1138
|
+
if (!dir) return null;
|
|
1139
|
+
let text;
|
|
1140
|
+
try {
|
|
1141
|
+
text = readFileOrNull(path8.join(dir, "installed_plugins.json"));
|
|
1142
|
+
} catch {
|
|
1143
|
+
return null;
|
|
1144
|
+
}
|
|
1145
|
+
if (text === null) return [];
|
|
1146
|
+
let json;
|
|
1147
|
+
try {
|
|
1148
|
+
json = JSON.parse(text);
|
|
1149
|
+
} catch {
|
|
1150
|
+
return null;
|
|
1151
|
+
}
|
|
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();
|
|
1154
|
+
}
|
|
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) {
|
|
1165
|
+
const dir = accountDir(paths, account);
|
|
1166
|
+
if (!lstatOrNull(dir)?.isDirectory()) return null;
|
|
1167
|
+
const store = realpathOrNull(path8.join(paths.store, "plugins"));
|
|
1168
|
+
if (!store || realpathOrNull(path8.join(dir, "plugins")) !== store) return null;
|
|
1169
|
+
const installed = userPlugins(paths);
|
|
1170
|
+
if (!installed) return null;
|
|
1171
|
+
const file = path8.join(dir, "settings.json");
|
|
1172
|
+
const st = lstatOrNull(file);
|
|
1173
|
+
if (st && !st.isFile()) return null;
|
|
1174
|
+
const original = st ? fs12.readFileSync(file, "utf8") : null;
|
|
1175
|
+
let settings = {};
|
|
1176
|
+
if (original !== null) {
|
|
1177
|
+
try {
|
|
1178
|
+
settings = JSON.parse(original);
|
|
1179
|
+
} catch {
|
|
1180
|
+
return null;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
if (!isRecord3(settings)) return null;
|
|
1184
|
+
const current = settings.enabledPlugins ?? {};
|
|
1185
|
+
if (!isRecord3(current)) return null;
|
|
1186
|
+
const wanted = (id) => follow?.has(id.toLowerCase()) === true;
|
|
1187
|
+
const named = new Set(Object.keys(current).map((id) => id.toLowerCase()));
|
|
1188
|
+
const add = installed.filter((id) => !named.has(id.toLowerCase()));
|
|
1189
|
+
const next = { ...current };
|
|
1190
|
+
const off = [];
|
|
1191
|
+
const on = [];
|
|
1192
|
+
const turnOn = [];
|
|
1193
|
+
const turnOff = [];
|
|
1194
|
+
const drop = [];
|
|
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 [];
|
|
1427
|
+
}
|
|
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)}`);
|
|
1435
|
+
}
|
|
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;
|
|
1445
|
+
}
|
|
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
|
+
);
|
|
1458
|
+
}
|
|
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;
|
|
1481
|
+
}
|
|
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;
|
|
1486
|
+
try {
|
|
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;
|
|
1493
|
+
} catch (e) {
|
|
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;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
// src/link/repair.ts
|
|
1503
|
+
import fs17 from "node:fs";
|
|
1504
|
+
import path12 from "node:path";
|
|
1505
|
+
|
|
1506
|
+
// src/link/history.ts
|
|
1507
|
+
import fs15 from "node:fs";
|
|
1508
|
+
import path10 from "node:path";
|
|
791
1509
|
|
|
792
1510
|
// src/link/merge.ts
|
|
793
|
-
import
|
|
794
|
-
import
|
|
1511
|
+
import fs14 from "node:fs";
|
|
1512
|
+
import path9 from "node:path";
|
|
795
1513
|
var counter2 = 0;
|
|
796
1514
|
function asideName(entry) {
|
|
797
1515
|
return `.xclaude-merge-${entry}-${stamp()}-${process.pid}-${counter2++}`;
|
|
@@ -816,23 +1534,23 @@ function remember(rec, c) {
|
|
|
816
1534
|
rec.byPath.set(c.path, c);
|
|
817
1535
|
}
|
|
818
1536
|
function requireRealDir(dir) {
|
|
819
|
-
const st =
|
|
1537
|
+
const st = fs14.lstatSync(dir);
|
|
820
1538
|
if (!st.isDirectory()) throw new Error(`${dir} isn't a real directory; it won't be read through`);
|
|
821
1539
|
return st;
|
|
822
1540
|
}
|
|
823
1541
|
function linkText(src, root) {
|
|
824
|
-
const text =
|
|
825
|
-
if (
|
|
826
|
-
const resolved =
|
|
827
|
-
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;
|
|
828
1546
|
}
|
|
829
1547
|
function identicalEntry(src, srcStat, dst, root) {
|
|
830
1548
|
const dstStat = lstatOrNull(dst);
|
|
831
1549
|
if (!dstStat) return false;
|
|
832
1550
|
if (srcStat.isSymbolicLink()) {
|
|
833
1551
|
if (!dstStat.isSymbolicLink()) return false;
|
|
834
|
-
const there =
|
|
835
|
-
return there === linkText(src, root) || there ===
|
|
1552
|
+
const there = fs14.readlinkSync(dst);
|
|
1553
|
+
return there === linkText(src, root) || there === fs14.readlinkSync(src);
|
|
836
1554
|
}
|
|
837
1555
|
return srcStat.isFile() && dstStat.isFile() && sameContent(src, dst);
|
|
838
1556
|
}
|
|
@@ -847,18 +1565,18 @@ function conflictName(dir, name, ctx) {
|
|
|
847
1565
|
const base = `${name}.xclaude-conflict-${ctx.account}-${ctx.stamp}`;
|
|
848
1566
|
for (let n = 1; ; n++) {
|
|
849
1567
|
const candidate = n === 1 ? base : `${base}-${n}`;
|
|
850
|
-
if (!lstatOrNull(
|
|
1568
|
+
if (!lstatOrNull(path9.join(dir, candidate))) return candidate;
|
|
851
1569
|
}
|
|
852
1570
|
}
|
|
853
1571
|
var join = (rel, name) => rel ? `${rel}/${name}` : name;
|
|
854
1572
|
function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
855
1573
|
let fresh = 0;
|
|
856
|
-
for (const d of
|
|
1574
|
+
for (const d of fs14.readdirSync(src, { withFileTypes: true })) {
|
|
857
1575
|
ctx.refresh();
|
|
858
|
-
const s =
|
|
859
|
-
const t =
|
|
1576
|
+
const s = path9.join(src, d.name);
|
|
1577
|
+
const t = path9.join(dst, d.name);
|
|
860
1578
|
const relName = join(rel, d.name);
|
|
861
|
-
const st =
|
|
1579
|
+
const st = fs14.lstatSync(s);
|
|
862
1580
|
const prior = rec.placed.get(relName);
|
|
863
1581
|
if (st.isDirectory()) {
|
|
864
1582
|
let target = t;
|
|
@@ -866,14 +1584,14 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
866
1584
|
target = prior.target;
|
|
867
1585
|
} else {
|
|
868
1586
|
try {
|
|
869
|
-
|
|
1587
|
+
fs14.mkdirSync(t, { mode: st.mode & 4095 | 448 });
|
|
870
1588
|
remember(rec, { path: t, kind: "dir" });
|
|
871
1589
|
} catch (e) {
|
|
872
1590
|
if (errCode(e) !== "EEXIST") throw e;
|
|
873
|
-
if (!
|
|
1591
|
+
if (!fs14.lstatSync(t).isDirectory()) {
|
|
874
1592
|
const name2 = conflictName(dst, d.name, ctx);
|
|
875
|
-
target =
|
|
876
|
-
|
|
1593
|
+
target = path9.join(dst, name2);
|
|
1594
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
877
1595
|
remember(rec, { path: target, kind: "dir" });
|
|
878
1596
|
stats.conflicts.push(join(rel, name2));
|
|
879
1597
|
}
|
|
@@ -888,7 +1606,7 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
888
1606
|
const place = (to) => {
|
|
889
1607
|
if (st.isSymbolicLink()) {
|
|
890
1608
|
const link = linkText(s, root);
|
|
891
|
-
|
|
1609
|
+
fs14.symlinkSync(link, to);
|
|
892
1610
|
remember(rec, { path: to, kind: "symlink", link });
|
|
893
1611
|
} else {
|
|
894
1612
|
fsops.link(s, to);
|
|
@@ -914,8 +1632,8 @@ function linkTree(src, dst, ctx, stats, rec, root = src, rel = "") {
|
|
|
914
1632
|
continue;
|
|
915
1633
|
}
|
|
916
1634
|
const name = conflictName(dst, d.name, ctx);
|
|
917
|
-
place(
|
|
918
|
-
rec.placed.set(relName, { target:
|
|
1635
|
+
place(path9.join(dst, name));
|
|
1636
|
+
rec.placed.set(relName, { target: path9.join(dst, name) });
|
|
919
1637
|
stats.moved++;
|
|
920
1638
|
stats.conflicts.push(join(rel, name));
|
|
921
1639
|
}
|
|
@@ -926,29 +1644,29 @@ function undoLinks(rec) {
|
|
|
926
1644
|
const st = lstatOrNull(c.path);
|
|
927
1645
|
if (!st) continue;
|
|
928
1646
|
try {
|
|
929
|
-
if (c.kind === "dir" && st.isDirectory())
|
|
930
|
-
else if (c.kind === "symlink" && st.isSymbolicLink() &&
|
|
931
|
-
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);
|
|
932
1650
|
} catch {
|
|
933
1651
|
}
|
|
934
1652
|
}
|
|
935
1653
|
}
|
|
936
1654
|
function openForEmptying(dir) {
|
|
937
1655
|
const st = requireRealDir(dir);
|
|
938
|
-
if ((st.mode & 448) !== 448)
|
|
939
|
-
return
|
|
1656
|
+
if ((st.mode & 448) !== 448) fs14.chmodSync(dir, st.mode & 4095 | 448);
|
|
1657
|
+
return fs14.readdirSync(dir, { withFileTypes: true });
|
|
940
1658
|
}
|
|
941
1659
|
function takeOver(src, placed, rec, dst, ctx, stats, rel) {
|
|
942
1660
|
const ours = rec.byPath.get(placed.target);
|
|
943
1661
|
const there = lstatOrNull(placed.target);
|
|
944
1662
|
if (!ours || ours.kind !== "file" || !there || there.ino !== ours.ino || there.dev !== ours.dev) return false;
|
|
945
|
-
const base =
|
|
1663
|
+
const base = path9.basename(placed.target);
|
|
946
1664
|
const old = conflictName(dst, base, ctx);
|
|
947
|
-
fsops.link(placed.target,
|
|
948
|
-
const tmp =
|
|
1665
|
+
fsops.link(placed.target, path9.join(dst, old));
|
|
1666
|
+
const tmp = path9.join(dst, `.xclaude-tmp-${base}-${process.pid}-${counter2++}`);
|
|
949
1667
|
fsops.link(src, tmp);
|
|
950
|
-
|
|
951
|
-
|
|
1668
|
+
fs14.renameSync(tmp, placed.target);
|
|
1669
|
+
fs14.unlinkSync(src);
|
|
952
1670
|
stats.conflicts.push(join(rel, old));
|
|
953
1671
|
return true;
|
|
954
1672
|
}
|
|
@@ -958,34 +1676,34 @@ function moveTree(src, dst, ctx, stats, rec = null, root = src, rel = "") {
|
|
|
958
1676
|
entries.sort((a, b) => Number(isPlaced(a.name)) - Number(isPlaced(b.name)));
|
|
959
1677
|
for (const d of entries) {
|
|
960
1678
|
ctx.refresh();
|
|
961
|
-
const s =
|
|
962
|
-
const st =
|
|
1679
|
+
const s = path9.join(src, d.name);
|
|
1680
|
+
const st = fs14.lstatSync(s);
|
|
963
1681
|
const relName = join(rel, d.name);
|
|
964
1682
|
const placed = rec?.placed.get(relName);
|
|
965
1683
|
if (st.isDirectory()) {
|
|
966
|
-
let target =
|
|
1684
|
+
let target = path9.join(dst, d.name);
|
|
967
1685
|
if (placed && lstatOrNull(placed.target)?.isDirectory()) {
|
|
968
1686
|
target = placed.target;
|
|
969
1687
|
} else {
|
|
970
1688
|
try {
|
|
971
|
-
|
|
1689
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
972
1690
|
} catch (e) {
|
|
973
1691
|
if (errCode(e) !== "EEXIST") throw e;
|
|
974
|
-
if (!
|
|
1692
|
+
if (!fs14.lstatSync(target).isDirectory()) {
|
|
975
1693
|
const name2 = conflictName(dst, d.name, ctx);
|
|
976
|
-
target =
|
|
977
|
-
|
|
1694
|
+
target = path9.join(dst, name2);
|
|
1695
|
+
fs14.mkdirSync(target, { mode: st.mode & 4095 | 448 });
|
|
978
1696
|
stats.conflicts.push(join(rel, name2));
|
|
979
1697
|
}
|
|
980
1698
|
}
|
|
981
1699
|
}
|
|
982
1700
|
moveTree(s, target, ctx, stats, rec, root, relName);
|
|
983
|
-
|
|
1701
|
+
fs14.rmdirSync(s);
|
|
984
1702
|
continue;
|
|
985
1703
|
}
|
|
986
1704
|
if (placed) {
|
|
987
1705
|
if (stillPlaced(placed, s, st, root)) {
|
|
988
|
-
|
|
1706
|
+
fs14.unlinkSync(s);
|
|
989
1707
|
continue;
|
|
990
1708
|
}
|
|
991
1709
|
if (st.isFile() && !placed.identical && rec && takeOver(s, placed, rec, dst, ctx, stats, rel)) {
|
|
@@ -995,29 +1713,29 @@ function moveTree(src, dst, ctx, stats, rec = null, root = src, rel = "") {
|
|
|
995
1713
|
}
|
|
996
1714
|
if (st.isSymbolicLink()) {
|
|
997
1715
|
const link = linkText(s, root);
|
|
998
|
-
const to =
|
|
1716
|
+
const to = path9.join(dst, d.name);
|
|
999
1717
|
try {
|
|
1000
|
-
|
|
1001
|
-
|
|
1718
|
+
fs14.symlinkSync(link, to);
|
|
1719
|
+
fs14.unlinkSync(s);
|
|
1002
1720
|
stats.moved++;
|
|
1003
1721
|
continue;
|
|
1004
1722
|
} catch (e) {
|
|
1005
1723
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1006
1724
|
}
|
|
1007
|
-
} else if (moveNoClobber(s,
|
|
1725
|
+
} else if (moveNoClobber(s, path9.join(dst, d.name), st) === "moved") {
|
|
1008
1726
|
stats.moved++;
|
|
1009
1727
|
continue;
|
|
1010
1728
|
}
|
|
1011
|
-
if (identicalEntry(s, st,
|
|
1012
|
-
|
|
1729
|
+
if (identicalEntry(s, st, path9.join(dst, d.name), root)) {
|
|
1730
|
+
fs14.unlinkSync(s);
|
|
1013
1731
|
stats.dropped++;
|
|
1014
1732
|
continue;
|
|
1015
1733
|
}
|
|
1016
1734
|
const name = conflictName(dst, d.name, ctx);
|
|
1017
1735
|
if (st.isSymbolicLink()) {
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
} 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") {
|
|
1021
1739
|
throw new Error(`can't place ${relName}: ${name} is taken`);
|
|
1022
1740
|
}
|
|
1023
1741
|
stats.moved++;
|
|
@@ -1148,6 +1866,7 @@ var APPENDIX_A = [
|
|
|
1148
1866
|
"local-settings",
|
|
1149
1867
|
"project-settings",
|
|
1150
1868
|
"remote",
|
|
1869
|
+
"remote-control",
|
|
1151
1870
|
"scratch",
|
|
1152
1871
|
"seed-admin",
|
|
1153
1872
|
"storage-v2",
|
|
@@ -1222,7 +1941,7 @@ function readLines(file) {
|
|
|
1222
1941
|
return (readFileOrNull(file) ?? "").split("\n");
|
|
1223
1942
|
}
|
|
1224
1943
|
function storeLockPath(S) {
|
|
1225
|
-
return `${realpathOrNull(
|
|
1944
|
+
return `${realpathOrNull(path10.join(S, HISTORY)) ?? path10.join(S, HISTORY)}.lock`;
|
|
1226
1945
|
}
|
|
1227
1946
|
function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
1228
1947
|
const refreshAll = () => {
|
|
@@ -1233,7 +1952,7 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1233
1952
|
const storeLock = DirLock.acquire(storeLockPath(ctx.S), HISTORY_LOCK, { waitMs, onWait: refreshAll });
|
|
1234
1953
|
if (!storeLock) return { ok: false, reason: "the shared history.jsonl is busy" };
|
|
1235
1954
|
try {
|
|
1236
|
-
const storeFile = realpathOrNull(
|
|
1955
|
+
const storeFile = realpathOrNull(path10.join(ctx.S, HISTORY));
|
|
1237
1956
|
if (!storeFile) return { ok: false, reason: "~/.claude/history.jsonl doesn't resolve" };
|
|
1238
1957
|
const storeLines = readLines(storeFile);
|
|
1239
1958
|
const merged = mergeHistoryLines(storeLines, readLines(aside));
|
|
@@ -1241,7 +1960,7 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1241
1960
|
if (!storeLock.refresh() || alsoHeld && !alsoHeld.refresh()) return { ok: false, reason: "a history lock was taken over" };
|
|
1242
1961
|
writeFileAtomic(storeFile, merged.length ? `${merged.join("\n")}
|
|
1243
1962
|
` : "", { mode: 384, fsync: true });
|
|
1244
|
-
|
|
1963
|
+
fs15.unlinkSync(aside);
|
|
1245
1964
|
const before = new Set(storeLines);
|
|
1246
1965
|
return { ok: true, added: merged.filter((line) => !before.has(line)).length };
|
|
1247
1966
|
} finally {
|
|
@@ -1249,37 +1968,37 @@ function mergeAsideIntoStore(ctx, aside, alsoHeld) {
|
|
|
1249
1968
|
}
|
|
1250
1969
|
}
|
|
1251
1970
|
function mergeHistory(ctx) {
|
|
1252
|
-
const file =
|
|
1971
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1253
1972
|
const accountLock = DirLock.acquire(`${file}.lock`, HISTORY_LOCK, { waitMs: ctx.lockWaitMs, onWait: ctx.refreshStoreLock });
|
|
1254
1973
|
if (!accountLock) return { ok: false, reason: `this account's history.jsonl is busy` };
|
|
1255
1974
|
try {
|
|
1256
1975
|
const st = lstatOrNull(file);
|
|
1257
1976
|
if (!st?.isFile()) return { ok: true, added: 0 };
|
|
1258
|
-
const storeFile = realpathOrNull(
|
|
1977
|
+
const storeFile = realpathOrNull(path10.join(ctx.S, HISTORY));
|
|
1259
1978
|
if (!storeFile) return { ok: false, reason: "~/.claude/history.jsonl doesn't resolve" };
|
|
1260
|
-
const aside =
|
|
1261
|
-
|
|
1979
|
+
const aside = path10.join(ctx.A, asideName(HISTORY));
|
|
1980
|
+
fs15.linkSync(file, aside);
|
|
1262
1981
|
const tmp = siblingName(file, "link");
|
|
1263
|
-
|
|
1264
|
-
|
|
1982
|
+
fs15.symlinkSync(storeFile, tmp);
|
|
1983
|
+
fs15.renameSync(tmp, file);
|
|
1265
1984
|
return mergeAsideIntoStore(ctx, aside, accountLock);
|
|
1266
1985
|
} finally {
|
|
1267
1986
|
accountLock.release();
|
|
1268
1987
|
}
|
|
1269
1988
|
}
|
|
1270
1989
|
function resumeHistoryAside(ctx, aside, shared) {
|
|
1271
|
-
const file =
|
|
1990
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1272
1991
|
const st = lstatOrNull(file);
|
|
1273
|
-
const asideStat =
|
|
1992
|
+
const asideStat = fs15.lstatSync(aside);
|
|
1274
1993
|
if (st?.isFile() && st.ino === asideStat.ino && st.dev === asideStat.dev) {
|
|
1275
|
-
|
|
1994
|
+
fs15.unlinkSync(aside);
|
|
1276
1995
|
return { ok: true, added: 0 };
|
|
1277
1996
|
}
|
|
1278
1997
|
if (!shared) return mergeAsideIntoAccount(ctx, aside);
|
|
1279
1998
|
return mergeAsideIntoStore(ctx, aside, null);
|
|
1280
1999
|
}
|
|
1281
2000
|
function mergeAsideIntoAccount(ctx, aside) {
|
|
1282
|
-
const file =
|
|
2001
|
+
const file = path10.join(ctx.A, HISTORY);
|
|
1283
2002
|
const lock = DirLock.acquire(`${file}.lock`, HISTORY_LOCK, { waitMs: ctx.lockWaitMs, onWait: ctx.refreshStoreLock });
|
|
1284
2003
|
if (!lock) return { ok: false, reason: "this account's history.jsonl is busy" };
|
|
1285
2004
|
try {
|
|
@@ -1290,7 +2009,7 @@ function mergeAsideIntoAccount(ctx, aside) {
|
|
|
1290
2009
|
if (!lock.refresh()) return { ok: false, reason: "a history lock was taken over" };
|
|
1291
2010
|
writeFileAtomic(file, merged.length ? `${merged.join("\n")}
|
|
1292
2011
|
` : "", { mode: 384, fsync: true });
|
|
1293
|
-
|
|
2012
|
+
fs15.unlinkSync(aside);
|
|
1294
2013
|
const before = new Set(own);
|
|
1295
2014
|
return { ok: true, added: merged.filter((line) => !before.has(line)).length };
|
|
1296
2015
|
} finally {
|
|
@@ -1299,23 +2018,23 @@ function mergeAsideIntoAccount(ctx, aside) {
|
|
|
1299
2018
|
}
|
|
1300
2019
|
|
|
1301
2020
|
// src/link/stub.ts
|
|
1302
|
-
import
|
|
1303
|
-
import
|
|
2021
|
+
import fs16 from "node:fs";
|
|
2022
|
+
import path11 from "node:path";
|
|
1304
2023
|
var STUB_TEXT = "Shared instructions live in ~/.claude/CLAUDE.md, so edit that file.\n@~/.claude/CLAUDE.md\n";
|
|
1305
2024
|
function stubState(A, S, enabled, detail) {
|
|
1306
|
-
const storeHas = enabled && Boolean(statOrNull(
|
|
1307
|
-
const a = lstatOrNull(
|
|
2025
|
+
const storeHas = enabled && Boolean(statOrNull(path11.join(S, STUB))?.isFile());
|
|
2026
|
+
const a = lstatOrNull(path11.join(A, STUB));
|
|
1308
2027
|
if (!a) return storeHas ? "create" : "none";
|
|
1309
2028
|
if (storeHas && !detail) return "ok";
|
|
1310
|
-
const isStub = a.isFile() && readFileOrNull(
|
|
2029
|
+
const isStub = a.isFile() && readFileOrNull(path11.join(A, STUB)) === STUB_TEXT;
|
|
1311
2030
|
if (storeHas) return isStub ? "ok" : "differs";
|
|
1312
2031
|
return isStub ? "remove" : "differs";
|
|
1313
2032
|
}
|
|
1314
2033
|
function applyStub(A, state) {
|
|
1315
|
-
const file =
|
|
2034
|
+
const file = path11.join(A, STUB);
|
|
1316
2035
|
if (state === "create") {
|
|
1317
2036
|
try {
|
|
1318
|
-
|
|
2037
|
+
fs16.writeFileSync(file, STUB_TEXT, { flag: "wx", mode: 384 });
|
|
1319
2038
|
return "created";
|
|
1320
2039
|
} catch (e) {
|
|
1321
2040
|
if (errCode(e) === "EEXIST") return null;
|
|
@@ -1323,7 +2042,7 @@ function applyStub(A, state) {
|
|
|
1323
2042
|
}
|
|
1324
2043
|
}
|
|
1325
2044
|
if (state === "remove" && readFileOrNull(file) === STUB_TEXT) {
|
|
1326
|
-
|
|
2045
|
+
fs16.unlinkSync(file);
|
|
1327
2046
|
return "removed";
|
|
1328
2047
|
}
|
|
1329
2048
|
return null;
|
|
@@ -1341,8 +2060,8 @@ function storeKind(p) {
|
|
|
1341
2060
|
return st.isDirectory() ? "dir" : st.isFile() ? "file" : "other";
|
|
1342
2061
|
}
|
|
1343
2062
|
function classifyEntry(A, S, name, kind) {
|
|
1344
|
-
const a =
|
|
1345
|
-
const s =
|
|
2063
|
+
const a = path12.join(A, name);
|
|
2064
|
+
const s = path12.join(S, name);
|
|
1346
2065
|
const lst = lstatOrNull(a);
|
|
1347
2066
|
const optional = OPTIONAL_DIRS.has(name);
|
|
1348
2067
|
const sk = storeKind(s);
|
|
@@ -1373,7 +2092,7 @@ function inspectAccount(paths, account, table, detail = false) {
|
|
|
1373
2092
|
const S = paths.store;
|
|
1374
2093
|
let dirents;
|
|
1375
2094
|
try {
|
|
1376
|
-
dirents =
|
|
2095
|
+
dirents = fs17.readdirSync(A, { withFileTypes: true });
|
|
1377
2096
|
} catch (e) {
|
|
1378
2097
|
if (errCode(e) !== "ENOENT") throw e;
|
|
1379
2098
|
return { dir: A, exists: false, entries: [], leftovers: [], stub: "none", names: [] };
|
|
@@ -1384,8 +2103,8 @@ function inspectAccount(paths, account, table, detail = false) {
|
|
|
1384
2103
|
for (const d of dirents) {
|
|
1385
2104
|
const name = d.name;
|
|
1386
2105
|
if (!d.isSymbolicLink() || shared.has(name) || name === STUB || NEVER_SHARED.includes(name) || isIgnored(name)) continue;
|
|
1387
|
-
const target = readlinkOrNull(
|
|
1388
|
-
const s =
|
|
2106
|
+
const target = readlinkOrNull(path12.join(A, name));
|
|
2107
|
+
const s = path12.join(S, name);
|
|
1389
2108
|
if (target === null || target !== s && target !== realpathOrNull(s)) continue;
|
|
1390
2109
|
const kind = name === HISTORY || storeKind(s) === "file" ? "file" : "dir";
|
|
1391
2110
|
entries.push({ name, kind, state: "unshare", target });
|
|
@@ -1450,11 +2169,11 @@ function describeProblems(insp, paths) {
|
|
|
1450
2169
|
return problems;
|
|
1451
2170
|
}
|
|
1452
2171
|
function ensureStoreEntry(S, name, kind) {
|
|
1453
|
-
|
|
1454
|
-
const p =
|
|
2172
|
+
fs17.mkdirSync(S, { recursive: true, mode: 448 });
|
|
2173
|
+
const p = path12.join(S, name);
|
|
1455
2174
|
try {
|
|
1456
|
-
if (kind === "dir")
|
|
1457
|
-
else
|
|
2175
|
+
if (kind === "dir") fs17.mkdirSync(p, { mode: 448 });
|
|
2176
|
+
else fs17.closeSync(fs17.openSync(p, "wx", 384));
|
|
1458
2177
|
} catch (e) {
|
|
1459
2178
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1460
2179
|
}
|
|
@@ -1532,10 +2251,10 @@ var Repairer = class {
|
|
|
1532
2251
|
/** realpath(S/E), created first if missing. Never a path inside an account dir. */
|
|
1533
2252
|
linkTarget(info) {
|
|
1534
2253
|
ensureStoreEntry(this.S, info.name, info.kind);
|
|
1535
|
-
const target = realpathOrNull(
|
|
2254
|
+
const target = realpathOrNull(path12.join(this.S, info.name));
|
|
1536
2255
|
if (!target) throw new Error(`${this.storePath(info.name)} doesn't resolve`);
|
|
1537
2256
|
const accounts = realpathOrNull(this.opts.paths.accounts) ?? this.opts.paths.accounts;
|
|
1538
|
-
if (target === accounts || target.startsWith(`${accounts}${
|
|
2257
|
+
if (target === accounts || target.startsWith(`${accounts}${path12.sep}`)) {
|
|
1539
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`);
|
|
1540
2259
|
}
|
|
1541
2260
|
return target;
|
|
@@ -1543,7 +2262,7 @@ var Repairer = class {
|
|
|
1543
2262
|
createLink(info, depth) {
|
|
1544
2263
|
const target = this.linkTarget(info);
|
|
1545
2264
|
try {
|
|
1546
|
-
|
|
2265
|
+
fs17.symlinkSync(target, path12.join(this.A, info.name));
|
|
1547
2266
|
} catch (e) {
|
|
1548
2267
|
if (errCode(e) !== "EEXIST" || depth > 0) throw e;
|
|
1549
2268
|
this.act(classifyEntry(this.A, this.S, info.name, info.kind), depth + 1);
|
|
@@ -1551,22 +2270,22 @@ var Repairer = class {
|
|
|
1551
2270
|
}
|
|
1552
2271
|
replaceLink(info) {
|
|
1553
2272
|
const target = this.linkTarget(info);
|
|
1554
|
-
const a =
|
|
2273
|
+
const a = path12.join(this.A, info.name);
|
|
1555
2274
|
const tmp = siblingName(a, "link");
|
|
1556
|
-
|
|
1557
|
-
|
|
2275
|
+
fs17.symlinkSync(target, tmp);
|
|
2276
|
+
fs17.renameSync(tmp, a);
|
|
1558
2277
|
this.opts.log(`xclaude: ${this.label}/${info.name} pointed to ${info.target}; relinked to ${this.storePath(info.name)}`);
|
|
1559
2278
|
}
|
|
1560
2279
|
unshare(info) {
|
|
1561
|
-
const a =
|
|
2280
|
+
const a = path12.join(this.A, info.name);
|
|
1562
2281
|
if (info.kind === "file") {
|
|
1563
2282
|
const tmp = siblingName(a, "unshare");
|
|
1564
|
-
|
|
1565
|
-
|
|
2283
|
+
fs17.closeSync(fs17.openSync(tmp, "wx", 384));
|
|
2284
|
+
fs17.renameSync(tmp, a);
|
|
1566
2285
|
} else {
|
|
1567
|
-
|
|
2286
|
+
fs17.unlinkSync(a);
|
|
1568
2287
|
try {
|
|
1569
|
-
|
|
2288
|
+
fs17.mkdirSync(a, { mode: 448 });
|
|
1570
2289
|
} catch (e) {
|
|
1571
2290
|
if (errCode(e) !== "EEXIST") throw e;
|
|
1572
2291
|
}
|
|
@@ -1592,7 +2311,7 @@ var Repairer = class {
|
|
|
1592
2311
|
* was lost: then another process may already rely on those links.
|
|
1593
2312
|
*/
|
|
1594
2313
|
mergeDir(info) {
|
|
1595
|
-
const a =
|
|
2314
|
+
const a = path12.join(this.A, info.name);
|
|
1596
2315
|
const target = this.linkTarget(info);
|
|
1597
2316
|
const ctx = this.moveContext();
|
|
1598
2317
|
const stats = emptyStats();
|
|
@@ -1617,15 +2336,15 @@ var Repairer = class {
|
|
|
1617
2336
|
}
|
|
1618
2337
|
const asides = [];
|
|
1619
2338
|
for (let attempt = 0; ; attempt++) {
|
|
1620
|
-
const aside =
|
|
1621
|
-
|
|
2339
|
+
const aside = path12.join(this.A, asideName(info.name));
|
|
2340
|
+
fs17.renameSync(a, aside);
|
|
1622
2341
|
asides.push(aside);
|
|
1623
2342
|
try {
|
|
1624
|
-
|
|
2343
|
+
fs17.symlinkSync(target, a);
|
|
1625
2344
|
break;
|
|
1626
2345
|
} catch (e) {
|
|
1627
2346
|
if (errCode(e) !== "EEXIST") {
|
|
1628
|
-
|
|
2347
|
+
fs17.renameSync(asides[0], a);
|
|
1629
2348
|
if (asides.length === 1) undoLinks(rec);
|
|
1630
2349
|
throw e;
|
|
1631
2350
|
}
|
|
@@ -1636,7 +2355,7 @@ var Repairer = class {
|
|
|
1636
2355
|
}
|
|
1637
2356
|
asides.forEach((aside, i) => {
|
|
1638
2357
|
moveTree(aside, target, ctx, stats, i === 0 ? rec : null);
|
|
1639
|
-
|
|
2358
|
+
fs17.rmdirSync(aside);
|
|
1640
2359
|
});
|
|
1641
2360
|
const line = summarize(this.label, info.name, this.storePath(info.name), stats);
|
|
1642
2361
|
if (line) this.opts.log(line);
|
|
@@ -1672,11 +2391,11 @@ var Repairer = class {
|
|
|
1672
2391
|
}
|
|
1673
2392
|
}
|
|
1674
2393
|
resumeLeftover(name) {
|
|
1675
|
-
const p =
|
|
2394
|
+
const p = path12.join(this.A, name);
|
|
1676
2395
|
if (name.startsWith(".xclaude-link-") || name.startsWith(".xclaude-unshare-") || name.startsWith(".xclaude-tmp-")) {
|
|
1677
2396
|
const st2 = lstatOrNull(p);
|
|
1678
|
-
if (st2?.isDirectory())
|
|
1679
|
-
else if (st2)
|
|
2397
|
+
if (st2?.isDirectory()) fs17.rmdirSync(p);
|
|
2398
|
+
else if (st2) fs17.unlinkSync(p);
|
|
1680
2399
|
return;
|
|
1681
2400
|
}
|
|
1682
2401
|
const entry = parseAside(name);
|
|
@@ -1687,7 +2406,7 @@ var Repairer = class {
|
|
|
1687
2406
|
const st = lstatOrNull(p);
|
|
1688
2407
|
if (!st) return;
|
|
1689
2408
|
if (st.isSymbolicLink()) {
|
|
1690
|
-
|
|
2409
|
+
fs17.unlinkSync(p);
|
|
1691
2410
|
return;
|
|
1692
2411
|
}
|
|
1693
2412
|
if (entry === HISTORY ? !st.isFile() : !st.isDirectory()) {
|
|
@@ -1701,22 +2420,22 @@ var Repairer = class {
|
|
|
1701
2420
|
return;
|
|
1702
2421
|
}
|
|
1703
2422
|
if (!this.opts.table.dirs.includes(entry)) {
|
|
1704
|
-
const own =
|
|
1705
|
-
if (!lstatOrNull(own))
|
|
1706
|
-
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);
|
|
1707
2426
|
if (!st2.isDirectory() || st2.isSymbolicLink()) {
|
|
1708
2427
|
this.problem(name, `left by an interrupted merge of ${entry}; ${entry} isn't a directory, so move the contents back by hand`);
|
|
1709
2428
|
return;
|
|
1710
2429
|
}
|
|
1711
2430
|
moveTree(p, own, this.moveContext(), emptyStats());
|
|
1712
|
-
|
|
2431
|
+
fs17.rmdirSync(p);
|
|
1713
2432
|
return;
|
|
1714
2433
|
}
|
|
1715
2434
|
const target = this.linkTarget({ name: entry, kind: "dir", state: "ok" });
|
|
1716
2435
|
const stats = emptyStats();
|
|
1717
2436
|
try {
|
|
1718
2437
|
moveTree(p, target, this.moveContext(), stats);
|
|
1719
|
-
|
|
2438
|
+
fs17.rmdirSync(p);
|
|
1720
2439
|
} catch (e) {
|
|
1721
2440
|
this.cannotMerge(entry, e);
|
|
1722
2441
|
return;
|
|
@@ -1729,7 +2448,7 @@ function unmanageable(A, store) {
|
|
|
1729
2448
|
if (lstatOrNull(A)?.isSymbolicLink()) return "is a symlink; xclaude only manages real folders there, so it leaves this one alone";
|
|
1730
2449
|
const realA = realpathOrNull(A);
|
|
1731
2450
|
const realS = realpathOrNull(store);
|
|
1732
|
-
if (realA && realS && (realA === realS || realS.startsWith(`${realA}${
|
|
2451
|
+
if (realA && realS && (realA === realS || realS.startsWith(`${realA}${path12.sep}`))) {
|
|
1733
2452
|
return "is the shared store or holds it; xclaude leaves it alone";
|
|
1734
2453
|
}
|
|
1735
2454
|
return null;
|
|
@@ -1743,7 +2462,7 @@ function repairAccount(opts) {
|
|
|
1743
2462
|
const problems = describeProblems(insp, opts.paths);
|
|
1744
2463
|
return { clean: problems.length === 0, busy: false, problems, names: insp.names };
|
|
1745
2464
|
}
|
|
1746
|
-
const lock = DirLock.acquire(
|
|
2465
|
+
const lock = DirLock.acquire(path12.join(opts.paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1747
2466
|
if (!lock) {
|
|
1748
2467
|
const problems = describeProblems(insp, opts.paths);
|
|
1749
2468
|
return { clean: false, busy: true, problems, names: insp.names };
|
|
@@ -1856,19 +2575,21 @@ function applyDefaultOptions(p, home, into) {
|
|
|
1856
2575
|
const args = stringOption(p, "args");
|
|
1857
2576
|
if (args !== void 0) into.args = splitShellWords(args, home);
|
|
1858
2577
|
}
|
|
1859
|
-
function formatDefaults(d) {
|
|
2578
|
+
function formatDefaults(d, account = false) {
|
|
2579
|
+
const inherit = d.inherit === void 0 ? "not chosen yet" : d.inherit ? "on" : "off";
|
|
1860
2580
|
return [
|
|
1861
2581
|
` model ${d.model ?? DASH}`,
|
|
1862
2582
|
` effort ${d.effort ?? DASH}`,
|
|
1863
|
-
` args ${d.args.length ? quoteShellWords(d.args) : DASH}
|
|
2583
|
+
` args ${d.args.length ? quoteShellWords(d.args) : DASH}`,
|
|
2584
|
+
...account ? [` inherit ${inherit}`] : []
|
|
1864
2585
|
].map((l) => `${l}
|
|
1865
2586
|
`).join("");
|
|
1866
2587
|
}
|
|
1867
2588
|
|
|
1868
2589
|
// src/commands/add.ts
|
|
1869
|
-
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]';
|
|
1870
2591
|
async function addCommand(ctx, config, args) {
|
|
1871
|
-
const p = parseOptions(args, DEFAULT_OPTIONS, "add");
|
|
2592
|
+
const p = parseOptions(args, { ...DEFAULT_OPTIONS, inherit: { value: true } }, "add");
|
|
1872
2593
|
const [name, ...extra] = p.positionals;
|
|
1873
2594
|
if (!name || extra.length) throw new UsageError(USAGE);
|
|
1874
2595
|
const problem2 = accountNameProblem(name);
|
|
@@ -1876,23 +2597,30 @@ async function addCommand(ctx, config, args) {
|
|
|
1876
2597
|
if (Object.hasOwn(config.accounts, name)) throw new XError(`account "${name}" already exists; change its defaults with: xclaude set ${name}`);
|
|
1877
2598
|
const defaults = { model: null, effort: null, args: [] };
|
|
1878
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}"`);
|
|
1879
2602
|
const claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
1880
2603
|
const log = (line) => ctx.io.err(`${line}
|
|
1881
2604
|
`);
|
|
1882
2605
|
const dir = accountDir(ctx.paths, name);
|
|
1883
2606
|
const refusal = unmanageable(dir, ctx.paths.store);
|
|
1884
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.`);
|
|
1885
|
-
|
|
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`);
|
|
1886
2611
|
mkdirPrivate(dir);
|
|
2612
|
+
const table = shareTable(config, ctx.switches);
|
|
1887
2613
|
const res = repairAccount({
|
|
1888
2614
|
paths: ctx.paths,
|
|
1889
2615
|
account: name,
|
|
1890
|
-
table
|
|
2616
|
+
table,
|
|
1891
2617
|
lockWaitMs: Infinity,
|
|
1892
2618
|
replaceWrongLinks: false,
|
|
1893
2619
|
log
|
|
1894
2620
|
});
|
|
1895
2621
|
for (const pr of res.problems) log(`xclaude: ${problemLine(name, pr)}`);
|
|
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 });
|
|
1896
2624
|
config = loadConfig(ctx.paths, { create: false }).config;
|
|
1897
2625
|
config.accounts[name] = defaults;
|
|
1898
2626
|
saveConfig(ctx.paths, config);
|
|
@@ -1918,12 +2646,12 @@ async function addCommand(ctx, config, args) {
|
|
|
1918
2646
|
|
|
1919
2647
|
// src/commands/doctor.ts
|
|
1920
2648
|
import { spawnSync as spawnSync2 } from "node:child_process";
|
|
1921
|
-
import
|
|
1922
|
-
import
|
|
2649
|
+
import fs23 from "node:fs";
|
|
2650
|
+
import path17 from "node:path";
|
|
1923
2651
|
|
|
1924
2652
|
// src/link/normalize.ts
|
|
1925
|
-
import
|
|
1926
|
-
import
|
|
2653
|
+
import fs19 from "node:fs";
|
|
2654
|
+
import path13 from "node:path";
|
|
1927
2655
|
var REGISTRY_FILES = ["plugins/installed_plugins.json", "plugins/known_marketplaces.json"];
|
|
1928
2656
|
function escapeRe(s) {
|
|
1929
2657
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -1943,17 +2671,17 @@ function rewrite(value, re, to, onChange) {
|
|
|
1943
2671
|
return value;
|
|
1944
2672
|
}
|
|
1945
2673
|
function planNormalization(paths, account) {
|
|
1946
|
-
const accountsDir =
|
|
1947
|
-
const to =
|
|
2674
|
+
const accountsDir = path13.join(paths.xhome, "accounts");
|
|
2675
|
+
const to = path13.join(paths.home, ".claude");
|
|
1948
2676
|
const name = account ? escapeRe(account) : "[a-z][a-z0-9-]{0,31}";
|
|
1949
2677
|
const re = new RegExp(`^${escapeRe(accountsDir)}/(${name})(/|$)`);
|
|
1950
2678
|
const plans = [];
|
|
1951
2679
|
for (const rel of REGISTRY_FILES) {
|
|
1952
|
-
const file = realpathOrNull(
|
|
2680
|
+
const file = realpathOrNull(path13.join(paths.store, rel));
|
|
1953
2681
|
if (!file) continue;
|
|
1954
2682
|
const st = lstatOrNull(file);
|
|
1955
2683
|
if (!st?.isFile()) continue;
|
|
1956
|
-
const original =
|
|
2684
|
+
const original = fs19.readFileSync(file, "utf8");
|
|
1957
2685
|
if (!original.includes(accountsDir)) continue;
|
|
1958
2686
|
let parsed;
|
|
1959
2687
|
try {
|
|
@@ -1986,7 +2714,7 @@ function applyNormalization(plans) {
|
|
|
1986
2714
|
function normalizePaths(paths, opts) {
|
|
1987
2715
|
const plans = planNormalization(paths, opts.account);
|
|
1988
2716
|
if (!plans.length) return { changed: [], skipped: [], busy: false };
|
|
1989
|
-
const lock = DirLock.acquire(
|
|
2717
|
+
const lock = DirLock.acquire(path13.join(paths.locks, "store"), STORE_LOCK, { waitMs: opts.lockWaitMs });
|
|
1990
2718
|
if (!lock) return { changed: [], skipped: plans.map((p) => p.file), busy: true };
|
|
1991
2719
|
try {
|
|
1992
2720
|
const result = applyNormalization(plans);
|
|
@@ -2000,12 +2728,12 @@ function normalizePaths(paths, opts) {
|
|
|
2000
2728
|
}
|
|
2001
2729
|
|
|
2002
2730
|
// src/link/leftover.ts
|
|
2003
|
-
import
|
|
2004
|
-
import
|
|
2731
|
+
import fs20 from "node:fs";
|
|
2732
|
+
import path14 from "node:path";
|
|
2005
2733
|
function strayFolders(paths, config) {
|
|
2006
2734
|
let names;
|
|
2007
2735
|
try {
|
|
2008
|
-
names =
|
|
2736
|
+
names = fs20.readdirSync(paths.accounts);
|
|
2009
2737
|
} catch {
|
|
2010
2738
|
return [];
|
|
2011
2739
|
}
|
|
@@ -2013,14 +2741,14 @@ function strayFolders(paths, config) {
|
|
|
2013
2741
|
const out = [];
|
|
2014
2742
|
for (const name of names.sort()) {
|
|
2015
2743
|
if (name.endsWith(".lock") || name === ".DS_Store" || Object.hasOwn(config.accounts, name)) continue;
|
|
2016
|
-
const dir =
|
|
2744
|
+
const dir = path14.join(paths.accounts, name);
|
|
2017
2745
|
const st = lstatOrNull(dir);
|
|
2018
2746
|
if (!st) continue;
|
|
2019
2747
|
if (st.isSymbolicLink()) out.push({ name, dir, kind: "link", entries: [] });
|
|
2020
2748
|
else if (!st.isDirectory() || accounts.some((a) => a.dev === st.dev && a.ino === st.ino)) continue;
|
|
2021
2749
|
else if (accountNameProblem(name)) out.push({ name, dir, kind: "stray", entries: [] });
|
|
2022
2750
|
else {
|
|
2023
|
-
const entries =
|
|
2751
|
+
const entries = fs20.readdirSync(dir).filter((e) => e !== ".DS_Store" && !lstatOrNull(path14.join(dir, e))?.isSymbolicLink());
|
|
2024
2752
|
out.push({ name, dir, kind: entries.length ? "content" : "leftover", entries });
|
|
2025
2753
|
}
|
|
2026
2754
|
}
|
|
@@ -2035,27 +2763,31 @@ function linkNames(table) {
|
|
|
2035
2763
|
function hasContent(p) {
|
|
2036
2764
|
const st = lstatOrNull(p);
|
|
2037
2765
|
if (!st) return false;
|
|
2038
|
-
if (st.isDirectory()) return
|
|
2766
|
+
if (st.isDirectory()) return fs20.readdirSync(p).some((e) => e !== ".DS_Store");
|
|
2039
2767
|
return st.size > 0;
|
|
2040
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
|
+
}
|
|
2041
2773
|
function keepOnlyLinks(dir, table) {
|
|
2042
2774
|
if (lstatOrNull(dir)?.isSymbolicLink()) throw new Error(`${dir} is a symlink`);
|
|
2043
2775
|
const shared = linkNames(table);
|
|
2044
2776
|
const content = [];
|
|
2045
2777
|
let links = 0;
|
|
2046
|
-
for (const name of
|
|
2047
|
-
const p =
|
|
2778
|
+
for (const name of fs20.readdirSync(dir).sort()) {
|
|
2779
|
+
const p = path14.join(dir, name);
|
|
2048
2780
|
if (shared.has(name) && lstatOrNull(p)?.isSymbolicLink()) links++;
|
|
2049
|
-
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);
|
|
2050
2782
|
else removeTree(p);
|
|
2051
2783
|
}
|
|
2052
2784
|
if (links || content.length) return { kept: true, content };
|
|
2053
|
-
|
|
2785
|
+
fs20.rmdirSync(dir);
|
|
2054
2786
|
return { kept: false, content };
|
|
2055
2787
|
}
|
|
2056
2788
|
|
|
2057
2789
|
// src/link/unknown.ts
|
|
2058
|
-
import
|
|
2790
|
+
import fs21 from "node:fs";
|
|
2059
2791
|
function unknownEntries(names, table) {
|
|
2060
2792
|
return names.filter((n) => !isIgnored(n) && !isKnown(n, table)).sort();
|
|
2061
2793
|
}
|
|
@@ -2071,7 +2803,7 @@ function unknownNotice(account, entry, configLabel) {
|
|
|
2071
2803
|
function storeUnknownEntries(store, table) {
|
|
2072
2804
|
let names;
|
|
2073
2805
|
try {
|
|
2074
|
-
names =
|
|
2806
|
+
names = fs21.readdirSync(store);
|
|
2075
2807
|
} catch (e) {
|
|
2076
2808
|
if (errCode(e) === "ENOENT") return [];
|
|
2077
2809
|
throw e;
|
|
@@ -2080,8 +2812,8 @@ function storeUnknownEntries(store, table) {
|
|
|
2080
2812
|
}
|
|
2081
2813
|
|
|
2082
2814
|
// src/shell/rc.ts
|
|
2083
|
-
import
|
|
2084
|
-
import
|
|
2815
|
+
import fs22 from "node:fs";
|
|
2816
|
+
import path15 from "node:path";
|
|
2085
2817
|
var START = "# >>> xclaude >>>";
|
|
2086
2818
|
var END = "# <<< xclaude <<<";
|
|
2087
2819
|
function dquote(s) {
|
|
@@ -2130,14 +2862,14 @@ function sourcesBashrc(text) {
|
|
|
2130
2862
|
return text.split("\n").some((l) => !/^\s*#/.test(l) && /\.bashrc\b/.test(l) && /(^|[\s;&|({])(\.|source)\s/.test(l));
|
|
2131
2863
|
}
|
|
2132
2864
|
function rcTargets(env, home, platform, want) {
|
|
2133
|
-
const zshrc =
|
|
2134
|
-
const bashrc =
|
|
2865
|
+
const zshrc = path15.join(env.ZDOTDIR || home, ".zshrc");
|
|
2866
|
+
const bashrc = path15.join(home, ".bashrc");
|
|
2135
2867
|
const explicit = want.bash || want.zsh;
|
|
2136
2868
|
const targets = [];
|
|
2137
|
-
if (explicit ? want.zsh :
|
|
2138
|
-
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)) {
|
|
2139
2871
|
targets.push({ shell: "bash", file: bashrc });
|
|
2140
|
-
const profile =
|
|
2872
|
+
const profile = path15.join(home, ".bash_profile");
|
|
2141
2873
|
const text = platform === "darwin" ? readFileOrNull(profile) : null;
|
|
2142
2874
|
if (text !== null && !sourcesBashrc(text)) targets.push({ shell: "bash", file: profile });
|
|
2143
2875
|
}
|
|
@@ -2146,11 +2878,11 @@ function rcTargets(env, home, platform, want) {
|
|
|
2146
2878
|
function writeRc(file, text, previous) {
|
|
2147
2879
|
if (text === previous) return { file, status: "unchanged" };
|
|
2148
2880
|
const real = realpathOrNull(file);
|
|
2149
|
-
if (real === null &&
|
|
2881
|
+
if (real === null && fs22.lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink()) return { file, status: "manual", text };
|
|
2150
2882
|
try {
|
|
2151
2883
|
const target = real ?? file;
|
|
2152
|
-
|
|
2153
|
-
if (previous !== null)
|
|
2884
|
+
fs22.accessSync(path15.dirname(target), fs22.constants.W_OK);
|
|
2885
|
+
if (previous !== null) fs22.accessSync(target, fs22.constants.W_OK);
|
|
2154
2886
|
writeFileAtomic(target, text, { mode: 420 });
|
|
2155
2887
|
return { file, status: "written" };
|
|
2156
2888
|
} catch (e) {
|
|
@@ -2160,9 +2892,9 @@ function writeRc(file, text, previous) {
|
|
|
2160
2892
|
}
|
|
2161
2893
|
function knownRcFiles(env, home) {
|
|
2162
2894
|
return [
|
|
2163
|
-
{ shell: "zsh", file:
|
|
2164
|
-
{ shell: "bash", file:
|
|
2165
|
-
{ 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") }
|
|
2166
2898
|
];
|
|
2167
2899
|
}
|
|
2168
2900
|
function installedIn(env, home) {
|
|
@@ -2171,7 +2903,7 @@ function installedIn(env, home) {
|
|
|
2171
2903
|
|
|
2172
2904
|
// src/tmux/client.ts
|
|
2173
2905
|
import { spawnSync } from "node:child_process";
|
|
2174
|
-
import
|
|
2906
|
+
import path16 from "node:path";
|
|
2175
2907
|
var STRIPPED_ENV = [
|
|
2176
2908
|
"CLAUDE_CONFIG_DIR",
|
|
2177
2909
|
"XCLAUDE_ACCOUNT",
|
|
@@ -2201,9 +2933,9 @@ function escapeArg(arg) {
|
|
|
2201
2933
|
}
|
|
2202
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";
|
|
2203
2935
|
function findTmux(env) {
|
|
2204
|
-
for (const dir of (env.PATH ?? "").split(
|
|
2205
|
-
if (!dir || !
|
|
2206
|
-
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");
|
|
2207
2939
|
if (isExecutableFile(candidate)) return candidate;
|
|
2208
2940
|
}
|
|
2209
2941
|
return null;
|
|
@@ -2276,10 +3008,10 @@ function readForcedOrgs(ctx) {
|
|
|
2276
3008
|
}
|
|
2277
3009
|
const files = [];
|
|
2278
3010
|
for (const dir of dirs) {
|
|
2279
|
-
files.push(
|
|
3011
|
+
files.push(path17.join(dir, "managed-settings.json"));
|
|
2280
3012
|
try {
|
|
2281
|
-
for (const name of
|
|
2282
|
-
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));
|
|
2283
3015
|
}
|
|
2284
3016
|
} catch {
|
|
2285
3017
|
}
|
|
@@ -2316,7 +3048,7 @@ function readForcedOrgs(ctx) {
|
|
|
2316
3048
|
}
|
|
2317
3049
|
function remoteCleanupDays(dir) {
|
|
2318
3050
|
try {
|
|
2319
|
-
const json = JSON.parse(readFileOrNull(
|
|
3051
|
+
const json = JSON.parse(readFileOrNull(path17.join(dir, "remote-settings.json")) ?? "null");
|
|
2320
3052
|
const value = json?.cleanupPeriodDays ?? json?.settings?.cleanupPeriodDays;
|
|
2321
3053
|
return typeof value === "number" ? value : null;
|
|
2322
3054
|
} catch {
|
|
@@ -2349,6 +3081,12 @@ async function doctorCommand(ctx, args) {
|
|
|
2349
3081
|
repairAccount({ paths: ctx.paths, account: name, table, lockWaitMs: Infinity, replaceWrongLinks: true, log });
|
|
2350
3082
|
}
|
|
2351
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
|
+
}
|
|
3087
|
+
if (table.dirs.includes("plugins")) {
|
|
3088
|
+
for (const name of Object.keys(config.accounts)) listPlugins(ctx.paths, name, { lockWaitMs: Infinity, log, follow: inherits(config, name) });
|
|
3089
|
+
}
|
|
2352
3090
|
}
|
|
2353
3091
|
report.section("Tools");
|
|
2354
3092
|
let claude = null;
|
|
@@ -2369,7 +3107,7 @@ async function doctorCommand(ctx, args) {
|
|
|
2369
3107
|
report.add("ok", `Node ${process.versions.node}`);
|
|
2370
3108
|
report.section("Config");
|
|
2371
3109
|
if (configProblem) report.add("error", configProblem);
|
|
2372
|
-
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)"}`);
|
|
2373
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`);
|
|
2374
3112
|
const names = identities(config);
|
|
2375
3113
|
const logins = /* @__PURE__ */ new Map();
|
|
@@ -2382,6 +3120,10 @@ async function doctorCommand(ctx, args) {
|
|
|
2382
3120
|
if (configProblem) report.add("info", "not checked: fix the config first (account dirs are left alone)");
|
|
2383
3121
|
else if (!names.length) report.add("info", "no accounts yet; add one with: xclaude add <name>");
|
|
2384
3122
|
const storeDev = statOrNull(ctx.paths.store)?.dev;
|
|
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;
|
|
2385
3127
|
for (const name of names) {
|
|
2386
3128
|
const login = logins.get(name);
|
|
2387
3129
|
report.lines.push(` ${name}${login ? ` ${describeLogin(login)}` : ""}`);
|
|
@@ -2406,6 +3148,37 @@ async function doctorCommand(ctx, args) {
|
|
|
2406
3148
|
}
|
|
2407
3149
|
const unknown = unknownEntries(insp.names, table);
|
|
2408
3150
|
if (unknown.length) report.add("info", `per-account entries xclaude doesn't know: ${unknown.join(", ")}`, 2);
|
|
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);
|
|
3181
|
+
}
|
|
2409
3182
|
const dev = statOrNull(dir)?.dev;
|
|
2410
3183
|
if (storeDev !== void 0 && dev !== void 0 && dev !== storeDev) {
|
|
2411
3184
|
report.add(
|
|
@@ -2434,8 +3207,8 @@ async function doctorCommand(ctx, args) {
|
|
|
2434
3207
|
${s.orgId}`, [...byLogin.get(`${s.email}
|
|
2435
3208
|
${s.orgId}`) ?? [], n]);
|
|
2436
3209
|
}
|
|
2437
|
-
for (const [key,
|
|
2438
|
-
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]})`);
|
|
2439
3212
|
}
|
|
2440
3213
|
for (const f of configProblem ? [] : strayFolders(ctx.paths, config)) {
|
|
2441
3214
|
const where = t(f.dir);
|
|
@@ -2451,18 +3224,19 @@ ${s.orgId}`) ?? [], n]);
|
|
|
2451
3224
|
}
|
|
2452
3225
|
report.section("Store");
|
|
2453
3226
|
const storeUnknown = storeUnknownEntries(ctx.paths.store, table);
|
|
2454
|
-
if (!
|
|
3227
|
+
if (!fs23.existsSync(ctx.paths.store)) report.add("info", `${t(ctx.paths.store)} doesn't exist yet`);
|
|
2455
3228
|
else if (storeUnknown.length) report.add("info", `entries in ${t(ctx.paths.store)} that no account links to: ${storeUnknown.join(", ")}`);
|
|
2456
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`);
|
|
2457
3231
|
const mainDays = remoteCleanupDays(ctx.paths.store);
|
|
2458
3232
|
if (mainDays !== null && mainDays < 30) {
|
|
2459
3233
|
report.add("warn", `the ~/.claude login's organization sets cleanupPeriodDays to ${mainDays}: plain claude deletes everyone's shared transcripts older than ${mainDays} days`);
|
|
2460
3234
|
}
|
|
2461
3235
|
for (const entry of SHARED_DIRS) {
|
|
2462
|
-
const p2 =
|
|
2463
|
-
if (!
|
|
3236
|
+
const p2 = path17.join(ctx.paths.store, entry);
|
|
3237
|
+
if (!fs23.existsSync(p2)) continue;
|
|
2464
3238
|
try {
|
|
2465
|
-
|
|
3239
|
+
fs23.accessSync(p2, fs23.constants.W_OK);
|
|
2466
3240
|
} catch {
|
|
2467
3241
|
report.add("warn", `${t(p2)} isn't writable, so sessions can't save ${entry} there`);
|
|
2468
3242
|
}
|
|
@@ -2492,8 +3266,8 @@ ${s.orgId}`) ?? [], n]);
|
|
|
2492
3266
|
}
|
|
2493
3267
|
|
|
2494
3268
|
// src/shell/init.ts
|
|
2495
|
-
import
|
|
2496
|
-
import
|
|
3269
|
+
import fs24 from "node:fs";
|
|
3270
|
+
import path18 from "node:path";
|
|
2497
3271
|
function header(shell, version) {
|
|
2498
3272
|
return `# xclaude ${version}: ${shell} integration. Written by \`xclaude shell install\`, \`xclaude guard on|off\`
|
|
2499
3273
|
# and after xclaude updates; don't edit, changes are overwritten.
|
|
@@ -2598,7 +3372,7 @@ ${guard ? `
|
|
|
2598
3372
|
${GUARD}` : ""}`;
|
|
2599
3373
|
}
|
|
2600
3374
|
function initFile(paths, shell) {
|
|
2601
|
-
return
|
|
3375
|
+
return path18.join(paths.shell, `init.${shell}`);
|
|
2602
3376
|
}
|
|
2603
3377
|
function writeInitFiles(paths, config, version) {
|
|
2604
3378
|
mkdirPrivate(paths.shell);
|
|
@@ -2606,11 +3380,11 @@ function writeInitFiles(paths, config, version) {
|
|
|
2606
3380
|
writeFileAtomic(initFile(paths, "zsh"), initZsh(version, config.guard), { mode: 420 });
|
|
2607
3381
|
}
|
|
2608
3382
|
function refreshInitFiles(paths, config, version) {
|
|
2609
|
-
if (
|
|
3383
|
+
if (fs24.existsSync(paths.shell)) writeInitFiles(paths, config, version);
|
|
2610
3384
|
}
|
|
2611
3385
|
|
|
2612
3386
|
// package.json
|
|
2613
|
-
var package_default = { version: "0.
|
|
3387
|
+
var package_default = { version: "0.2.0" };
|
|
2614
3388
|
|
|
2615
3389
|
// src/version.ts
|
|
2616
3390
|
var VERSION = package_default.version;
|
|
@@ -2666,17 +3440,30 @@ Commands:
|
|
|
2666
3440
|
Conversations, prompt history, memory and personal content (skills, agents,
|
|
2667
3441
|
commands, plugins\u2026) are shared by every account, so \`xclaude other -c\`
|
|
2668
3442
|
continues the last conversation in this folder, whichever account it ran on.
|
|
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.
|
|
2669
3449
|
`;
|
|
2670
3450
|
var COMMAND_HELP = {
|
|
2671
3451
|
add: `Usage: xclaude add <name> [--model M] [--effort E] [--args "\u2026"]
|
|
3452
|
+
[--inherit on|off]
|
|
2672
3453
|
|
|
2673
3454
|
Creates the account, links it to the shared ~/.claude, then starts Claude Code
|
|
2674
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.
|
|
2675
3459
|
|
|
2676
3460
|
--model M default model, e.g. opus or sonnet
|
|
2677
3461
|
--effort E default effort: low, medium, high, xhigh, max or ultracode
|
|
2678
3462
|
--args "\u2026" extra Claude Code arguments for every launch, split like a
|
|
2679
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
|
|
2680
3467
|
|
|
2681
3468
|
Names use lowercase letters, digits and dashes, and start with a letter.
|
|
2682
3469
|
`,
|
|
@@ -2686,9 +3473,11 @@ Names use lowercase letters, digits and dashes, and start with a letter.
|
|
|
2686
3473
|
Removes an account: stops its background sessions, logs it out, and deletes its
|
|
2687
3474
|
login, settings and caches. Shared conversations and content stay in ~/.claude.
|
|
2688
3475
|
|
|
2689
|
-
Its folder keeps
|
|
2690
|
-
|
|
2691
|
-
|
|
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.
|
|
2692
3481
|
|
|
2693
3482
|
--keep-login don't log the account out
|
|
2694
3483
|
--leftovers delete the leftover folders of all removed accounts
|
|
@@ -2701,7 +3490,7 @@ defaults, and each one's config dir (the CLAUDE_CONFIG_DIR to use elsewhere,
|
|
|
2701
3490
|
e.g. in an editor's environment settings).
|
|
2702
3491
|
`,
|
|
2703
3492
|
set: `Usage: xclaude set <name> [--model M] [--effort E] [--args "\u2026"]
|
|
2704
|
-
[--unset model|effort|args]
|
|
3493
|
+
[--unset model|effort|args] [--inherit on|off|reset]
|
|
2705
3494
|
xclaude set main --enable|--disable
|
|
2706
3495
|
|
|
2707
3496
|
Changes an account's launch defaults; with no options, prints them.
|
|
@@ -2711,6 +3500,14 @@ Arguments typed at launch always win over these defaults.
|
|
|
2711
3500
|
--effort E default effort: low, medium, high, xhigh, max or ultracode
|
|
2712
3501
|
--args "\u2026" extra Claude Code arguments, split like a shell line
|
|
2713
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.
|
|
2714
3511
|
--enable (main only) use the login in ~/.claude as the account "main"
|
|
2715
3512
|
--disable (main only) turn it off again
|
|
2716
3513
|
`,
|
|
@@ -2755,7 +3552,9 @@ Checks tools, config, every account's links and login, the shell setup,
|
|
|
2755
3552
|
machine-managed settings and the environment. Exits non-zero if something is
|
|
2756
3553
|
wrong.
|
|
2757
3554
|
|
|
2758
|
-
--fix repair links, merge left-behind directories, fix CLAUDE.md stubs
|
|
3555
|
+
--fix repair links, merge left-behind directories, fix CLAUDE.md stubs,
|
|
3556
|
+
merge ~/.claude/settings.json into the accounts that take it,
|
|
3557
|
+
list plugins installed under other logins
|
|
2759
3558
|
`,
|
|
2760
3559
|
help: `Usage: xclaude help [command]
|
|
2761
3560
|
`
|
|
@@ -2771,13 +3570,13 @@ function wantsHelp(args) {
|
|
|
2771
3570
|
}
|
|
2772
3571
|
|
|
2773
3572
|
// src/shell/complete.ts
|
|
2774
|
-
import
|
|
2775
|
-
import
|
|
3573
|
+
import fs26 from "node:fs";
|
|
3574
|
+
import path20 from "node:path";
|
|
2776
3575
|
|
|
2777
3576
|
// src/claude/help.ts
|
|
2778
3577
|
import { spawnSync as spawnSync3 } from "node:child_process";
|
|
2779
|
-
import
|
|
2780
|
-
import
|
|
3578
|
+
import fs25 from "node:fs";
|
|
3579
|
+
import path19 from "node:path";
|
|
2781
3580
|
var FALLBACK_SUBCOMMANDS = ["auth", "mcp", "plugin", "agents", "daemon", "doctor", "update", "install", "setup-token"];
|
|
2782
3581
|
var opt = (names, value, placeholder, choices) => ({
|
|
2783
3582
|
names,
|
|
@@ -2899,12 +3698,12 @@ function loadHelp(claude, cacheDir, env) {
|
|
|
2899
3698
|
const real = realpathOrNull(claude) ?? claude;
|
|
2900
3699
|
let st;
|
|
2901
3700
|
try {
|
|
2902
|
-
st =
|
|
3701
|
+
st = fs25.statSync(real);
|
|
2903
3702
|
} catch {
|
|
2904
3703
|
return FALLBACK_HELP;
|
|
2905
3704
|
}
|
|
2906
3705
|
const key = { path: real, mtimeMs: st.mtimeMs, size: st.size };
|
|
2907
|
-
const file =
|
|
3706
|
+
const file = path19.join(cacheDir, "claude-help.json");
|
|
2908
3707
|
try {
|
|
2909
3708
|
const cached = JSON.parse(readFileOrNull(file) ?? "null");
|
|
2910
3709
|
if (cached && cached.key.path === key.path && cached.key.mtimeMs === key.mtimeMs && cached.key.size === key.size) {
|
|
@@ -2997,14 +3796,14 @@ function logicalWords(shell, words2, cword) {
|
|
|
2997
3796
|
return { prior, cur };
|
|
2998
3797
|
}
|
|
2999
3798
|
function suggestLabel(cwd) {
|
|
3000
|
-
const label =
|
|
3799
|
+
const label = path20.basename(cwd).replace(/[^A-Za-z0-9_-]+/g, "-").replace(/^[^A-Za-z0-9]+/, "").slice(0, 64);
|
|
3001
3800
|
return label || null;
|
|
3002
3801
|
}
|
|
3003
3802
|
function resumeCandidates(store, cwd) {
|
|
3004
|
-
const dir =
|
|
3803
|
+
const dir = path20.join(store, "projects", cwd.replace(/[^a-zA-Z0-9]/g, "-"));
|
|
3005
3804
|
let files;
|
|
3006
3805
|
try {
|
|
3007
|
-
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 }));
|
|
3008
3807
|
} catch {
|
|
3009
3808
|
return [];
|
|
3010
3809
|
}
|
|
@@ -3016,12 +3815,12 @@ function resumeCandidates(store, cwd) {
|
|
|
3016
3815
|
function sessionTitle(file) {
|
|
3017
3816
|
let head;
|
|
3018
3817
|
try {
|
|
3019
|
-
const fd =
|
|
3818
|
+
const fd = fs26.openSync(file, "r");
|
|
3020
3819
|
try {
|
|
3021
3820
|
const buf = Buffer.alloc(16384);
|
|
3022
|
-
head = buf.subarray(0,
|
|
3821
|
+
head = buf.subarray(0, fs26.readSync(fd, buf, 0, buf.length, 0)).toString("utf8");
|
|
3023
3822
|
} finally {
|
|
3024
|
-
|
|
3823
|
+
fs26.closeSync(fd);
|
|
3025
3824
|
}
|
|
3026
3825
|
} catch {
|
|
3027
3826
|
return null;
|
|
@@ -3063,7 +3862,7 @@ var Completer = class {
|
|
|
3063
3862
|
claudeHelp() {
|
|
3064
3863
|
if (this.help) return this.help;
|
|
3065
3864
|
this.help = FALLBACK_HELP;
|
|
3066
|
-
if (
|
|
3865
|
+
if (fs26.existsSync(this.ctx.paths.xhome)) {
|
|
3067
3866
|
try {
|
|
3068
3867
|
const claude = findClaude(this.ctx.env, this.config, this.ctx.paths.home, this.ctx.selfPath);
|
|
3069
3868
|
this.help = loadHelp(claude, this.ctx.paths.cache, this.ctx.env);
|
|
@@ -3094,7 +3893,7 @@ var Completer = class {
|
|
|
3094
3893
|
if (first.startsWith("-")) return this.claudeArgs(args, cur);
|
|
3095
3894
|
switch (first) {
|
|
3096
3895
|
case "add":
|
|
3097
|
-
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, () => []);
|
|
3098
3897
|
case "rm":
|
|
3099
3898
|
if (rest.includes("--leftovers")) return this.own(rest, cur, { "-y": null }, 0, () => []);
|
|
3100
3899
|
return this.own(
|
|
@@ -3109,7 +3908,7 @@ var Completer = class {
|
|
|
3109
3908
|
return this.own(
|
|
3110
3909
|
rest,
|
|
3111
3910
|
cur,
|
|
3112
|
-
{ "--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 },
|
|
3113
3912
|
1,
|
|
3114
3913
|
() => [...Object.keys(this.config.accounts), MAIN]
|
|
3115
3914
|
);
|
|
@@ -3147,7 +3946,7 @@ var Completer = class {
|
|
|
3147
3946
|
return words(positional(), describe);
|
|
3148
3947
|
}
|
|
3149
3948
|
ownValues(kind, prefix) {
|
|
3150
|
-
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"] : [];
|
|
3151
3950
|
return words(values.map((v) => `${prefix}${v}`));
|
|
3152
3951
|
}
|
|
3153
3952
|
claudeFlags() {
|
|
@@ -3250,14 +4049,14 @@ function formatCompletion(c, shell, cur) {
|
|
|
3250
4049
|
|
|
3251
4050
|
// src/update.ts
|
|
3252
4051
|
import { spawnSync as spawnSync4 } from "node:child_process";
|
|
3253
|
-
import
|
|
4052
|
+
import path21 from "node:path";
|
|
3254
4053
|
var PACKAGE = "xclaude-cli";
|
|
3255
4054
|
var REGISTRY_URL = `https://registry.npmjs.org/${PACKAGE}/latest`;
|
|
3256
4055
|
var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
3257
4056
|
var FETCH_TIMEOUT_MS = 1e4;
|
|
3258
4057
|
var SEMVER = /^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?(?:\+[0-9A-Za-z.-]+)?$/;
|
|
3259
4058
|
function checkFile(paths) {
|
|
3260
|
-
return
|
|
4059
|
+
return path21.join(paths.cache, "update-check.json");
|
|
3261
4060
|
}
|
|
3262
4061
|
function readUpdateCheck(paths) {
|
|
3263
4062
|
try {
|
|
@@ -3379,82 +4178,25 @@ async function lsCommand(ctx, config, args) {
|
|
|
3379
4178
|
}
|
|
3380
4179
|
|
|
3381
4180
|
// src/core/prompt.ts
|
|
3382
|
-
import
|
|
4181
|
+
import fs27 from "node:fs";
|
|
3383
4182
|
function confirm(question) {
|
|
3384
|
-
|
|
4183
|
+
fs27.writeSync(2, `${question} [y/N] `);
|
|
3385
4184
|
let fd;
|
|
3386
4185
|
try {
|
|
3387
|
-
fd =
|
|
4186
|
+
fd = fs27.openSync("/dev/tty", "r");
|
|
3388
4187
|
} catch {
|
|
3389
4188
|
return false;
|
|
3390
4189
|
}
|
|
3391
4190
|
try {
|
|
3392
4191
|
const buf = Buffer.alloc(256);
|
|
3393
|
-
const n =
|
|
4192
|
+
const n = fs27.readSync(fd, buf, 0, buf.length, null);
|
|
3394
4193
|
const answer = buf.subarray(0, n).toString("utf8").trim().toLowerCase();
|
|
3395
4194
|
return answer === "y" || answer === "yes";
|
|
3396
4195
|
} catch {
|
|
3397
4196
|
return false;
|
|
3398
4197
|
} finally {
|
|
3399
|
-
|
|
3400
|
-
}
|
|
3401
|
-
}
|
|
3402
|
-
|
|
3403
|
-
// src/core/state.ts
|
|
3404
|
-
var MAX_DIRS = 500;
|
|
3405
|
-
function emptyState() {
|
|
3406
|
-
return { installedVersion: null, lastAccountByDir: {}, lastUsedAccount: null, seenUnknownEntries: {} };
|
|
3407
|
-
}
|
|
3408
|
-
function isRecord(v) {
|
|
3409
|
-
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
3410
|
-
}
|
|
3411
|
-
function loadState(paths) {
|
|
3412
|
-
let raw;
|
|
3413
|
-
try {
|
|
3414
|
-
raw = JSON.parse(readFileOrNull(paths.state) ?? "{}");
|
|
3415
|
-
} catch {
|
|
3416
|
-
return emptyState();
|
|
3417
|
-
}
|
|
3418
|
-
const state = emptyState();
|
|
3419
|
-
if (!isRecord(raw)) return state;
|
|
3420
|
-
if (typeof raw.installedVersion === "string") state.installedVersion = raw.installedVersion;
|
|
3421
|
-
if (typeof raw.lastUsedAccount === "string") state.lastUsedAccount = raw.lastUsedAccount;
|
|
3422
|
-
if (isRecord(raw.lastAccountByDir)) {
|
|
3423
|
-
for (const [dir, name] of Object.entries(raw.lastAccountByDir)) {
|
|
3424
|
-
if (typeof name === "string") state.lastAccountByDir[dir] = name;
|
|
3425
|
-
}
|
|
3426
|
-
}
|
|
3427
|
-
if (isRecord(raw.seenUnknownEntries)) {
|
|
3428
|
-
for (const [name, entries] of Object.entries(raw.seenUnknownEntries)) {
|
|
3429
|
-
if (Array.isArray(entries)) state.seenUnknownEntries[name] = entries.filter((e) => typeof e === "string");
|
|
3430
|
-
}
|
|
3431
|
-
}
|
|
3432
|
-
return state;
|
|
3433
|
-
}
|
|
3434
|
-
function saveState(paths, state) {
|
|
3435
|
-
ensureXHome(paths);
|
|
3436
|
-
writeFileAtomic(paths.state, `${JSON.stringify(state, null, 2)}
|
|
3437
|
-
`, { mode: 384 });
|
|
3438
|
-
}
|
|
3439
|
-
function updateState(paths, change) {
|
|
3440
|
-
const state = loadState(paths);
|
|
3441
|
-
change(state);
|
|
3442
|
-
saveState(paths, state);
|
|
3443
|
-
return state;
|
|
3444
|
-
}
|
|
3445
|
-
function recordUse(state, dir, account) {
|
|
3446
|
-
delete state.lastAccountByDir[dir];
|
|
3447
|
-
state.lastAccountByDir[dir] = account;
|
|
3448
|
-
const dirs = Object.keys(state.lastAccountByDir);
|
|
3449
|
-
for (const old of dirs.slice(0, Math.max(0, dirs.length - MAX_DIRS))) delete state.lastAccountByDir[old];
|
|
3450
|
-
state.lastUsedAccount = account;
|
|
3451
|
-
}
|
|
3452
|
-
function forgetAccount(state, account) {
|
|
3453
|
-
for (const [dir, name] of Object.entries(state.lastAccountByDir)) {
|
|
3454
|
-
if (name === account) delete state.lastAccountByDir[dir];
|
|
4198
|
+
fs27.closeSync(fd);
|
|
3455
4199
|
}
|
|
3456
|
-
if (state.lastUsedAccount === account) state.lastUsedAccount = null;
|
|
3457
|
-
delete state.seenUnknownEntries[account];
|
|
3458
4200
|
}
|
|
3459
4201
|
|
|
3460
4202
|
// src/grammar.ts
|
|
@@ -3511,7 +4253,7 @@ async function rmCommand(ctx, config, args, listSessions2 = () => []) {
|
|
|
3511
4253
|
}
|
|
3512
4254
|
if (!yes) {
|
|
3513
4255
|
if (!ctx.tty.stdin) throw new UsageError(`removing ${name} needs a confirmation: run it in a terminal, or pass -y`);
|
|
3514
|
-
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.`);
|
|
3515
4257
|
if (!ok) {
|
|
3516
4258
|
log("xclaude: nothing removed");
|
|
3517
4259
|
throw new Cancelled();
|
|
@@ -3564,6 +4306,8 @@ function forget(ctx, name) {
|
|
|
3564
4306
|
delete fresh.accounts[name];
|
|
3565
4307
|
saveConfig(ctx.paths, fresh);
|
|
3566
4308
|
updateState(ctx.paths, (s) => forgetAccount(s, name));
|
|
4309
|
+
forgetInherit(ctx.paths, name);
|
|
4310
|
+
forgetMcp(ctx.paths, name);
|
|
3567
4311
|
}
|
|
3568
4312
|
function whyNot(f, where) {
|
|
3569
4313
|
switch (f.kind) {
|
|
@@ -3637,9 +4381,9 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
3637
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`);
|
|
3638
4382
|
return;
|
|
3639
4383
|
}
|
|
3640
|
-
const
|
|
3641
|
-
if (
|
|
3642
|
-
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"}`);
|
|
3643
4387
|
return;
|
|
3644
4388
|
}
|
|
3645
4389
|
}
|
|
@@ -3648,10 +4392,11 @@ async function logout(ctx, config, claude, name, keep, log) {
|
|
|
3648
4392
|
}
|
|
3649
4393
|
|
|
3650
4394
|
// src/commands/set.ts
|
|
3651
|
-
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';
|
|
3652
4396
|
var KEYS = ["model", "effort", "args"];
|
|
4397
|
+
var INHERIT = ["on", "off", "reset"];
|
|
3653
4398
|
function setCommand(ctx, config, args) {
|
|
3654
|
-
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");
|
|
3655
4400
|
const [name, ...extra] = p.positionals;
|
|
3656
4401
|
if (!name || extra.length) throw new UsageError(USAGE3);
|
|
3657
4402
|
const isMain = name === MAIN;
|
|
@@ -3666,6 +4411,11 @@ function setCommand(ctx, config, args) {
|
|
|
3666
4411
|
if (!KEYS.includes(key)) throw new UsageError(`--unset takes model, effort or args, not "${key}"`);
|
|
3667
4412
|
if (p.values[key] !== void 0) throw new UsageError(`--${key} and --unset ${key} contradict each other`);
|
|
3668
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
|
+
}
|
|
3669
4419
|
if (Object.keys(p.values).length) {
|
|
3670
4420
|
applyDefaultOptions(p, ctx.paths.home, target);
|
|
3671
4421
|
for (const key of unset) {
|
|
@@ -3673,16 +4423,34 @@ function setCommand(ctx, config, args) {
|
|
|
3673
4423
|
else if (key === "model") target.model = null;
|
|
3674
4424
|
else target.effort = null;
|
|
3675
4425
|
}
|
|
4426
|
+
if (inherit !== void 0) target.inherit = inherit !== "off";
|
|
3676
4427
|
if (p.values.enable) config.main.enabled = true;
|
|
3677
4428
|
if (p.values.disable) config.main.enabled = false;
|
|
3678
4429
|
saveConfig(ctx.paths, config);
|
|
3679
4430
|
}
|
|
4431
|
+
if (inherit !== void 0) applyInherit(ctx, name, inherit);
|
|
3680
4432
|
let head = name;
|
|
3681
4433
|
if (isMain) head += config.main.enabled ? " (enabled: the login in ~/.claude)" : " (disabled; enable with: xclaude set main --enable)";
|
|
3682
4434
|
ctx.io.out(`${head}
|
|
3683
|
-
${formatDefaults(target)}`);
|
|
4435
|
+
${formatDefaults(target, !isMain)}`);
|
|
3684
4436
|
return EXIT_OK;
|
|
3685
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
|
+
}
|
|
3686
4454
|
|
|
3687
4455
|
// src/commands/shell.ts
|
|
3688
4456
|
function install(ctx, config, args) {
|
|
@@ -3750,8 +4518,8 @@ function shellCommand(ctx, config, args) {
|
|
|
3750
4518
|
}
|
|
3751
4519
|
|
|
3752
4520
|
// src/commands/tmux.ts
|
|
3753
|
-
import
|
|
3754
|
-
import
|
|
4521
|
+
import fs29 from "node:fs";
|
|
4522
|
+
import path22 from "node:path";
|
|
3755
4523
|
|
|
3756
4524
|
// src/launch/args.ts
|
|
3757
4525
|
function hasFlag(args, flag) {
|
|
@@ -3825,141 +4593,6 @@ function needsHelp(defaults, userArgs) {
|
|
|
3825
4593
|
return defaults.args.length > 0 || first !== void 0 && !first.startsWith("-");
|
|
3826
4594
|
}
|
|
3827
4595
|
|
|
3828
|
-
// src/launch/picker.ts
|
|
3829
|
-
import fs24 from "node:fs";
|
|
3830
|
-
import tty2 from "node:tty";
|
|
3831
|
-
function parseKeys(chunk) {
|
|
3832
|
-
const keys = [];
|
|
3833
|
-
let i = 0;
|
|
3834
|
-
while (i < chunk.length) {
|
|
3835
|
-
const rest = chunk.slice(i);
|
|
3836
|
-
if (rest.startsWith("\x1B[A") || rest.startsWith("\x1BOA")) {
|
|
3837
|
-
keys.push("up");
|
|
3838
|
-
i += 3;
|
|
3839
|
-
} else if (rest.startsWith("\x1B[B") || rest.startsWith("\x1BOB")) {
|
|
3840
|
-
keys.push("down");
|
|
3841
|
-
i += 3;
|
|
3842
|
-
} else if (rest.startsWith("\x1B[")) {
|
|
3843
|
-
let j = 2;
|
|
3844
|
-
while (j < rest.length && !/[@-~]/.test(rest[j])) j++;
|
|
3845
|
-
keys.push(null);
|
|
3846
|
-
i += j + 1;
|
|
3847
|
-
} else {
|
|
3848
|
-
const c = rest[0];
|
|
3849
|
-
if (c === "\x1B") keys.push(rest.length === 1 ? "cancel" : null);
|
|
3850
|
-
else if (c === "") keys.push("cancel");
|
|
3851
|
-
else if (c === "\r" || c === "\n") keys.push("enter");
|
|
3852
|
-
else if (c === "k") keys.push("up");
|
|
3853
|
-
else if (c === "j") keys.push("down");
|
|
3854
|
-
else if (c >= "1" && c <= "9") keys.push({ jump: Number(c) - 1 });
|
|
3855
|
-
else keys.push(null);
|
|
3856
|
-
i++;
|
|
3857
|
-
}
|
|
3858
|
-
}
|
|
3859
|
-
return keys;
|
|
3860
|
-
}
|
|
3861
|
-
function step(index, count, key) {
|
|
3862
|
-
if (key === "up") return { index: (index - 1 + count) % count };
|
|
3863
|
-
if (key === "down") return { index: (index + 1) % count };
|
|
3864
|
-
if (key === "enter") return { index, done: "chosen" };
|
|
3865
|
-
if (key === "cancel") return { index, done: "cancelled" };
|
|
3866
|
-
if (key && typeof key === "object" && key.jump < count) return { index: key.jump };
|
|
3867
|
-
return { index };
|
|
3868
|
-
}
|
|
3869
|
-
var dim = (s) => `\x1B[2m${s}\x1B[22m`;
|
|
3870
|
-
var bold = (s) => `\x1B[1m${s}\x1B[22m`;
|
|
3871
|
-
function fit(text, width) {
|
|
3872
|
-
return width > 0 && text.length > width ? `${text.slice(0, Math.max(0, width - 1))}\u2026` : text;
|
|
3873
|
-
}
|
|
3874
|
-
function render(title, items, index, width = 0) {
|
|
3875
|
-
const labelWidth = Math.max(...items.map((it) => it.label.length));
|
|
3876
|
-
const lines = [bold(fit(title, width))];
|
|
3877
|
-
items.forEach((it, i) => {
|
|
3878
|
-
const marker = i === index ? "\u276F" : " ";
|
|
3879
|
-
const num = i < 9 ? String(i + 1) : " ";
|
|
3880
|
-
const label = it.label.padEnd(labelWidth);
|
|
3881
|
-
const text = fit(`${marker} ${num} ${label}${it.detail ? ` ${it.detail}` : ""}`, width);
|
|
3882
|
-
const head = text.slice(0, 5 + labelWidth);
|
|
3883
|
-
const tail = text.slice(5 + labelWidth);
|
|
3884
|
-
lines.push(i === index ? `${bold(head)}${dim(tail)}` : `${head}${dim(tail)}`);
|
|
3885
|
-
});
|
|
3886
|
-
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)));
|
|
3887
|
-
return lines;
|
|
3888
|
-
}
|
|
3889
|
-
function preselect(names, state, cwd) {
|
|
3890
|
-
for (const candidate of [state.lastAccountByDir[cwd], state.lastUsedAccount]) {
|
|
3891
|
-
if (candidate && names.includes(candidate)) return names.indexOf(candidate);
|
|
3892
|
-
}
|
|
3893
|
-
return 0;
|
|
3894
|
-
}
|
|
3895
|
-
function openKeyboard() {
|
|
3896
|
-
let fd;
|
|
3897
|
-
try {
|
|
3898
|
-
fd = fs24.openSync("/dev/tty", "r");
|
|
3899
|
-
} catch {
|
|
3900
|
-
throw new UsageError("account required: there's no terminal to show the picker on (xclaude <account> [claude args\u2026])");
|
|
3901
|
-
}
|
|
3902
|
-
const input = new tty2.ReadStream(fd);
|
|
3903
|
-
let columns = 0;
|
|
3904
|
-
try {
|
|
3905
|
-
const size = [];
|
|
3906
|
-
const handle = input._handle;
|
|
3907
|
-
if (handle?.getWindowSize?.(size) === 0) columns = size[0] ?? 0;
|
|
3908
|
-
} catch {
|
|
3909
|
-
}
|
|
3910
|
-
return {
|
|
3911
|
-
input,
|
|
3912
|
-
columns,
|
|
3913
|
-
close: () => {
|
|
3914
|
-
input.pause();
|
|
3915
|
-
input.unref();
|
|
3916
|
-
}
|
|
3917
|
-
};
|
|
3918
|
-
}
|
|
3919
|
-
function pick(title, items, initial) {
|
|
3920
|
-
const keyboard = openKeyboard();
|
|
3921
|
-
const { input } = keyboard;
|
|
3922
|
-
const write = (s) => fs24.writeSync(2, s);
|
|
3923
|
-
let index = Math.min(Math.max(initial, 0), items.length - 1);
|
|
3924
|
-
let drawn = 0;
|
|
3925
|
-
const draw = () => {
|
|
3926
|
-
const lines = render(title, items, index, keyboard.columns);
|
|
3927
|
-
write(`${drawn ? `\r\x1B[${drawn}A` : ""}\x1B[J${lines.join("\n")}
|
|
3928
|
-
`);
|
|
3929
|
-
drawn = lines.length;
|
|
3930
|
-
};
|
|
3931
|
-
return new Promise((resolve, reject) => {
|
|
3932
|
-
const finish = (result) => {
|
|
3933
|
-
input.off("data", onData);
|
|
3934
|
-
try {
|
|
3935
|
-
input.setRawMode(false);
|
|
3936
|
-
} finally {
|
|
3937
|
-
keyboard.close();
|
|
3938
|
-
write(`\r\x1B[${drawn}A\x1B[J\x1B[?25h`);
|
|
3939
|
-
}
|
|
3940
|
-
if (result === "chosen") resolve(index);
|
|
3941
|
-
else reject(new Cancelled());
|
|
3942
|
-
};
|
|
3943
|
-
const onData = (chunk) => {
|
|
3944
|
-
for (const key of parseKeys(chunk)) {
|
|
3945
|
-
const next = step(index, items.length, key);
|
|
3946
|
-
index = next.index;
|
|
3947
|
-
if (next.done) {
|
|
3948
|
-
finish(next.done);
|
|
3949
|
-
return;
|
|
3950
|
-
}
|
|
3951
|
-
}
|
|
3952
|
-
draw();
|
|
3953
|
-
};
|
|
3954
|
-
input.setRawMode(true);
|
|
3955
|
-
input.setEncoding("utf8");
|
|
3956
|
-
input.on("data", onData);
|
|
3957
|
-
input.resume();
|
|
3958
|
-
write("\x1B[?25l");
|
|
3959
|
-
draw();
|
|
3960
|
-
});
|
|
3961
|
-
}
|
|
3962
|
-
|
|
3963
4596
|
// src/launch/launch.ts
|
|
3964
4597
|
function identityItems(config) {
|
|
3965
4598
|
return identities(config).map((name) => {
|
|
@@ -4000,16 +4633,33 @@ function repairForLaunch(ctx, config, account, log) {
|
|
|
4000
4633
|
if (ctx.switches.normalizePaths && table.dirs.includes("plugins")) {
|
|
4001
4634
|
normalizePaths(ctx.paths, { lockWaitMs: LAUNCH_LOCK_WAIT_MS, log });
|
|
4002
4635
|
}
|
|
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
|
+
}
|
|
4003
4640
|
return unknownEntries(res.names, table);
|
|
4004
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
|
+
}
|
|
4005
4653
|
async function launch(ctx, config, account, userArgs) {
|
|
4006
4654
|
const defaults = defaultsOf(config, account);
|
|
4007
4655
|
if (!defaults) throw unknownAccount(account, config);
|
|
4008
4656
|
const isMain = account === MAIN;
|
|
4009
|
-
const
|
|
4657
|
+
const settled = isMain ? config : await settleInherit(ctx, config, account);
|
|
4658
|
+
const unknown = isMain ? [] : prepareAccount(ctx, settled, account);
|
|
4010
4659
|
const claude = findClaude(ctx.env, config, ctx.paths.home, ctx.selfPath);
|
|
4011
4660
|
const help = needsHelp(defaults, userArgs) ? loadHelp(claude, ctx.paths.cache, ctx.env) : null;
|
|
4012
|
-
const
|
|
4661
|
+
const mcp = isMain ? null : mcpForLaunch(ctx, settled, account, defaults, userArgs, help);
|
|
4662
|
+
const args = [...mcp ? [`--mcp-config=${mcp}`] : [], ...buildArgs(defaults, userArgs, help)];
|
|
4013
4663
|
const env = buildEnv(ctx.env, { name: account, configDir: isMain ? null : accountDir(ctx.paths, account) });
|
|
4014
4664
|
try {
|
|
4015
4665
|
updateState(ctx.paths, (state) => {
|
|
@@ -4025,7 +4675,7 @@ async function launch(ctx, config, account, userArgs) {
|
|
|
4025
4675
|
|
|
4026
4676
|
// src/tmux/claude-state.ts
|
|
4027
4677
|
import { spawnSync as spawnSync5 } from "node:child_process";
|
|
4028
|
-
import
|
|
4678
|
+
import fs28 from "node:fs";
|
|
4029
4679
|
function describeAgent(e) {
|
|
4030
4680
|
if (e.status === "waiting") return { text: e.waitingFor ? `needs input (${e.waitingFor})` : "needs input", rank: 3 };
|
|
4031
4681
|
if (e.status === "busy") return { text: "working", rank: 2 };
|
|
@@ -4033,10 +4683,10 @@ function describeAgent(e) {
|
|
|
4033
4683
|
return null;
|
|
4034
4684
|
}
|
|
4035
4685
|
function parentLookup() {
|
|
4036
|
-
if (process.platform === "linux" &&
|
|
4686
|
+
if (process.platform === "linux" && fs28.existsSync("/proc/self/stat")) {
|
|
4037
4687
|
return (pid) => {
|
|
4038
4688
|
try {
|
|
4039
|
-
const stat =
|
|
4689
|
+
const stat = fs28.readFileSync(`/proc/${pid}/stat`, "utf8");
|
|
4040
4690
|
const ppid = Number(stat.slice(stat.lastIndexOf(")") + 2).split(" ")[1]);
|
|
4041
4691
|
return Number.isFinite(ppid) ? ppid : null;
|
|
4042
4692
|
} catch {
|
|
@@ -4162,8 +4812,8 @@ async function tmuxNew(ctx, config, args) {
|
|
|
4162
4812
|
account ??= await chooseAccount(ctx, config, `Run Claude Code in tmux session "${req.label}" as`);
|
|
4163
4813
|
if (!defaultsOf(config, account)) throw unknownAccount(account, config);
|
|
4164
4814
|
}
|
|
4165
|
-
const dir =
|
|
4166
|
-
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`);
|
|
4167
4817
|
const existing = findByLabel(listSessions(tmux), req.label);
|
|
4168
4818
|
if (existing) {
|
|
4169
4819
|
throw new XError(`"${req.label}" is running (${describeSession(existing, ctx.paths.home)}): xclaude tmux attach ${req.label}, or kill it first`);
|