t3code-session-manager 0.0.0-stage → 0.4.1

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.
Files changed (172) hide show
  1. package/.claude-plugin/marketplace.json +21 -0
  2. package/.claude-plugin/plugin.json +19 -0
  3. package/CHANGELOG.md +149 -0
  4. package/LICENSE +21 -0
  5. package/README.md +661 -2
  6. package/README.pt.md +617 -0
  7. package/bin/entry.d.mts +2 -0
  8. package/bin/entry.mjs +19 -0
  9. package/bin/t3sm.mjs +44 -0
  10. package/commands/import.md +30 -0
  11. package/commands/organize.md +36 -0
  12. package/commands/status.md +24 -0
  13. package/commands/sync.md +26 -0
  14. package/dist/apply/apply.js +993 -0
  15. package/dist/apply/claudeWriter.js +168 -0
  16. package/dist/apply/copyTranscript.js +116 -0
  17. package/dist/apply/importer.js +697 -0
  18. package/dist/apply/materialize.js +335 -0
  19. package/dist/apply/waves.js +108 -0
  20. package/dist/assistant/core.js +295 -0
  21. package/dist/assistant/deps.js +256 -0
  22. package/dist/assistant/i18n.js +38 -0
  23. package/dist/assistant/messages.en.js +494 -0
  24. package/dist/assistant/messages.pt.js +495 -0
  25. package/dist/assistant/runner.js +64 -0
  26. package/dist/assistant/steps.js +1362 -0
  27. package/dist/build.json +1 -0
  28. package/dist/cli/applyScreen.js +158 -0
  29. package/dist/cli/commands/accounts.js +254 -0
  30. package/dist/cli/commands/apply.js +199 -0
  31. package/dist/cli/commands/changelog.js +61 -0
  32. package/dist/cli/commands/doctor.js +105 -0
  33. package/dist/cli/commands/help.js +158 -0
  34. package/dist/cli/commands/merge.js +224 -0
  35. package/dist/cli/commands/organize.js +872 -0
  36. package/dist/cli/commands/plan.js +161 -0
  37. package/dist/cli/commands/projects.js +430 -0
  38. package/dist/cli/commands/quarantine.js +203 -0
  39. package/dist/cli/commands/recover.js +230 -0
  40. package/dist/cli/commands/rollback.js +281 -0
  41. package/dist/cli/commands/scan.js +117 -0
  42. package/dist/cli/commands/schedule.js +288 -0
  43. package/dist/cli/commands/start.js +45 -0
  44. package/dist/cli/commands/status.js +615 -0
  45. package/dist/cli/commands/sync.js +336 -0
  46. package/dist/cli/commands/update.js +398 -0
  47. package/dist/cli/errors.js +53 -0
  48. package/dist/cli/failures.js +91 -0
  49. package/dist/cli/flags.js +53 -0
  50. package/dist/cli/help.en.js +549 -0
  51. package/dist/cli/help.pt.js +566 -0
  52. package/dist/cli/helpText.js +211 -0
  53. package/dist/cli/helpTopics.js +58 -0
  54. package/dist/cli/locked.js +12 -0
  55. package/dist/cli/main.js +155 -0
  56. package/dist/cli/screen.js +3 -0
  57. package/dist/cli/ui.js +111 -0
  58. package/dist/cli/words.js +2 -0
  59. package/dist/core/activity.js +16 -0
  60. package/dist/core/changelog.js +47 -0
  61. package/dist/core/config.js +530 -0
  62. package/dist/core/fsutil.js +171 -0
  63. package/dist/core/glob.js +34 -0
  64. package/dist/core/history.js +32 -0
  65. package/dist/core/journal.js +274 -0
  66. package/dist/core/lock.js +337 -0
  67. package/dist/core/mask.js +31 -0
  68. package/dist/core/paths.js +68 -0
  69. package/dist/core/pid.js +101 -0
  70. package/dist/core/run.js +4 -0
  71. package/dist/core/semver.js +18 -0
  72. package/dist/core/sysroot.js +29 -0
  73. package/dist/core/types.js +229 -0
  74. package/dist/core/uuid.js +29 -0
  75. package/dist/core/version.js +72 -0
  76. package/dist/help/convert.md +20 -0
  77. package/dist/help/convert.pt.md +21 -0
  78. package/dist/help/cwd-unreadable.md +22 -0
  79. package/dist/help/cwd-unreadable.pt.md +22 -0
  80. package/dist/help/decisions.md +20 -0
  81. package/dist/help/decisions.pt.md +20 -0
  82. package/dist/help/errors.md +20 -0
  83. package/dist/help/errors.pt.md +22 -0
  84. package/dist/help/held.md +20 -0
  85. package/dist/help/held.pt.md +20 -0
  86. package/dist/help/history.md +27 -0
  87. package/dist/help/history.pt.md +29 -0
  88. package/dist/help/instance-unavailable.md +18 -0
  89. package/dist/help/instance-unavailable.pt.md +19 -0
  90. package/dist/help/not-imported.md +19 -0
  91. package/dist/help/not-imported.pt.md +19 -0
  92. package/dist/help/orphaned.md +19 -0
  93. package/dist/help/orphaned.pt.md +19 -0
  94. package/dist/help/project-mismatch.md +15 -0
  95. package/dist/help/project-mismatch.pt.md +15 -0
  96. package/dist/help/t3-native.md +22 -0
  97. package/dist/help/t3-native.pt.md +22 -0
  98. package/dist/help/updates.md +26 -0
  99. package/dist/help/updates.pt.md +28 -0
  100. package/dist/help/watcher.md +24 -0
  101. package/dist/help/watcher.pt.md +25 -0
  102. package/dist/i18n/cli.js +41 -0
  103. package/dist/i18n/lang.js +55 -0
  104. package/dist/i18n/messages.en.js +1140 -0
  105. package/dist/i18n/messages.pt.js +1149 -0
  106. package/dist/i18n/words.js +12 -0
  107. package/dist/ops/accounts.js +170 -0
  108. package/dist/ops/autoUpdate.js +223 -0
  109. package/dist/ops/discoverAccounts.js +343 -0
  110. package/dist/ops/doctor.js +811 -0
  111. package/dist/ops/elevated.js +47 -0
  112. package/dist/ops/errorCodes.js +39 -0
  113. package/dist/ops/follow.js +558 -0
  114. package/dist/ops/merge.js +830 -0
  115. package/dist/ops/notify.js +249 -0
  116. package/dist/ops/plugin.js +92 -0
  117. package/dist/ops/projects.js +113 -0
  118. package/dist/ops/quarantine.js +415 -0
  119. package/dist/ops/rollback.js +868 -0
  120. package/dist/ops/schedule.js +464 -0
  121. package/dist/ops/successor.js +51 -0
  122. package/dist/ops/sync.js +859 -0
  123. package/dist/ops/syncStatus.js +290 -0
  124. package/dist/ops/update.js +273 -0
  125. package/dist/ops/updater.js +641 -0
  126. package/dist/ops/watch.js +676 -0
  127. package/dist/ops/watcherControl.js +212 -0
  128. package/dist/ops/watcherHealth.js +75 -0
  129. package/dist/organize/apply.js +495 -0
  130. package/dist/organize/history.js +192 -0
  131. package/dist/organize/plan.js +283 -0
  132. package/dist/organize/rename.js +58 -0
  133. package/dist/organize/sessions.js +100 -0
  134. package/dist/organize/titles.js +70 -0
  135. package/dist/organize/types.js +73 -0
  136. package/dist/plan/categories.js +104 -0
  137. package/dist/plan/dedupe.js +137 -0
  138. package/dist/plan/folders.js +82 -0
  139. package/dist/plan/names.js +93 -0
  140. package/dist/plan/ownership.js +316 -0
  141. package/dist/plan/planner.js +669 -0
  142. package/dist/plan/rehome.js +101 -0
  143. package/dist/plan/report.js +407 -0
  144. package/dist/sources/cache.js +89 -0
  145. package/dist/sources/claude.js +297 -0
  146. package/dist/sources/codex.js +496 -0
  147. package/dist/sources/flatten.js +23 -0
  148. package/dist/sources/jsonl.js +169 -0
  149. package/dist/sources/native.js +233 -0
  150. package/dist/sources/opencode.js +483 -0
  151. package/dist/sources/pi.js +133 -0
  152. package/dist/sources/registry.js +67 -0
  153. package/dist/sources/types.js +80 -0
  154. package/dist/t3/api.js +492 -0
  155. package/dist/t3/auth.js +248 -0
  156. package/dist/t3/compat.js +62 -0
  157. package/dist/t3/decode.js +408 -0
  158. package/dist/t3/errors.js +185 -0
  159. package/dist/t3/rpc.js +185 -0
  160. package/dist/t3/runtime.js +80 -0
  161. package/dist/ui/caps.js +56 -0
  162. package/dist/ui/components.js +202 -0
  163. package/dist/ui/layout.js +205 -0
  164. package/dist/ui/list.js +423 -0
  165. package/dist/ui/locale.js +73 -0
  166. package/dist/ui/organizePreview.js +72 -0
  167. package/dist/ui/progress.js +237 -0
  168. package/dist/ui/style.js +32 -0
  169. package/dist/ui/terminal.js +126 -0
  170. package/dist/ui/text.js +186 -0
  171. package/package.json +58 -4
  172. package/skills/t3code-sessions/SKILL.md +183 -0
@@ -0,0 +1,343 @@
1
+ // Account discovery: where on this machine do Claude Code accounts (config dirs) live? Read-only.
2
+ // Evidence: ~/.claude (default), ~/.claude-* folders, CLAUDE_CONFIG_DIR assignments in shell profiles (PowerShell,
3
+ // bash, zsh, fish) with the enclosing alias/function name, `.vscode/settings.json` pins of known project roots, the
4
+ // current process CLAUDE_CONFIG_DIR, and homes of existing T3 Claude instances. A candidate is an account only when the
5
+ // directory exists and holds `projects/` (~/.claude counts whenever it exists: T3's default instance always reads it).
6
+ // Profile parsing is line-based and tolerant: anything it cannot resolve to an absolute path is ignored.
7
+
8
+ import { readdir, readFile } from "node:fs/promises";
9
+ import { parseJsonc, pinFromSettings } from "../plan/ownership.js";
10
+ import { normalizeCwd } from "../core/paths.js";
11
+ import { detectAccounts } from "../core/config.js";
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+ const SOURCE_ORDER = ["default", "folder", "shell-profile", "vscode-pin", "env", "t3-instance"];
33
+ const VAR = "CLAUDE_CONFIG_DIR";
34
+
35
+
36
+
37
+ const PS_PROFILES = ["Microsoft.PowerShell_profile.ps1", "profile.ps1"];
38
+ const PS_DIRS = ["Documents/PowerShell", "Documents/WindowsPowerShell"];
39
+ const SH_PROFILES = [".bashrc", ".bash_profile", ".bash_aliases", ".profile", ".zshrc", ".zprofile", ".zshenv"];
40
+
41
+ function join2(a , b ) {
42
+ return a.replace(/[\\/]+$/, "") + "/" + b;
43
+ }
44
+
45
+ async function profileFiles(env ) {
46
+ const out = [];
47
+ // OneDrive redirects Documents (personal: "OneDrive", business: "OneDrive - <Org>").
48
+ const roots = [env.home, ...(await env.listDirs(env.home)).filter((n) => /^OneDrive\b/i.test(n)).sort().map((n) => join2(env.home, n))];
49
+ for (const r of roots) for (const d of PS_DIRS) for (const f of PS_PROFILES) out.push({ path: `${join2(r, d)}/${f}`, kind: "ps" });
50
+ for (const f of PS_PROFILES) out.push({ path: `${join2(env.home, ".config/powershell")}/${f}`, kind: "ps" });
51
+ for (const f of SH_PROFILES) out.push({ path: join2(env.home, f), kind: "sh" });
52
+ out.push({ path: join2(env.home, ".config/fish/config.fish"), kind: "fish" });
53
+ return out;
54
+ }
55
+
56
+ // ---------- line scanning ----------
57
+
58
+ /** Escape character inside "…": the backtick in PowerShell, the backslash in sh/fish. */
59
+ const escOf = (kind ) => (kind === "ps" ? "`" : "\\");
60
+
61
+ /** Removes a `#` comment that starts the line or follows whitespace, outside quotes. */
62
+ function stripComment(line , kind ) {
63
+ const esc = escOf(kind);
64
+ let q = null;
65
+ for (let i = 0; i < line.length; i++) {
66
+ const c = line[i] ;
67
+ if (q) { if (c === q) q = null; else if (c === esc && q === '"') i++; continue; }
68
+ if (c === '"' || c === "'") { q = c; continue; }
69
+ if (c === "#" && (i === 0 || /\s/.test(line[i - 1] ))) return line.slice(0, i);
70
+ }
71
+ return line;
72
+ }
73
+
74
+ /** Splits on `;` outside quotes (fish statements). */
75
+ function statements(line ) {
76
+ const out = [];
77
+ let q = null, start = 0;
78
+ for (let i = 0; i < line.length; i++) {
79
+ const c = line[i] ;
80
+ if (q) { if (c === q) q = null; else if (c === "\\" && q === '"') i++; continue; }
81
+ if (c === '"' || c === "'") q = c;
82
+ else if (c === ";") { out.push(line.slice(start, i)); start = i + 1; }
83
+ }
84
+ out.push(line.slice(start));
85
+ return out;
86
+ }
87
+
88
+ /** Net `{`/`}` events outside quotes and outside `${...}` expansions, in order. */
89
+ function braces(line , kind ) {
90
+ const esc = escOf(kind);
91
+ const out = [];
92
+ let q = null;
93
+ for (let i = 0; i < line.length; i++) {
94
+ const c = line[i] ;
95
+ if (q) { if (c === q) q = null; else if (c === esc && q === '"') i++; continue; }
96
+ if (c === '"' || c === "'") { q = c; continue; }
97
+ if (c === "$" && line[i + 1] === "{") { const e = line.indexOf("}", i + 2); if (e !== -1) { i = e; continue; } }
98
+ if (c === "{" || c === "}") out.push(c);
99
+ }
100
+ return out;
101
+ }
102
+
103
+ const VALUE = String.raw`("[^"]*"|'[^']*'|[^\s;&|"'\x60)]+)`;
104
+ const SH_ASSIGN = new RegExp(String.raw`(?:^|[\s;&|("'\x60])(?:export\s+|local\s+|declare\s+-x\s+)?${VAR}=` + VALUE, "g");
105
+ /** A PowerShell argument: "…" (backtick escapes), '…', or a bare token. */
106
+ const PS_DQ = String.raw`"(?:[^"\x60]|\x60.)*"`;
107
+ const PS_ARG = String.raw`(?:${PS_DQ}|'[^']*'|[^\s;)'"]+)`;
108
+ const PS_JOIN = String.raw`Join-Path\s+(?:-Path\s+)?(${PS_ARG})\s+(?:-ChildPath\s+)?(${PS_ARG})`;
109
+ const PS_ASSIGN = new RegExp(String.raw`\$env:${VAR}\s*=\s*\(?\s*(Join-Path\s+(?:-Path\s+)?${PS_ARG}\s+(?:-ChildPath\s+)?${PS_ARG}|${PS_DQ}|'[^']*'|[^\s;)]+)`, "gi");
110
+ const PS_SETENV = new RegExp(String.raw`(?:SetEnvironmentVariable\(\s*['"]${VAR}['"]\s*,\s*|Set-Item\s+(?:-Path\s+)?env:${VAR}\s+(?:-Value\s+)?)(${PS_DQ}|'[^']*'|[^\s;),]+)`, "gi");
111
+ const FISH_SET = new RegExp(String.raw`^\s*set\s+(?:-{1,2}[A-Za-z]+\s+)*${VAR}\s+` + VALUE);
112
+ const NAME = String.raw`([A-Za-z0-9_][A-Za-z0-9_.:+-]*)`;
113
+ /** Lines that only print text: an assignment shown there is not evidence. */
114
+ const PRINT_LINE = /^\s*(?:echo|printf|Write-Host|Write-Output|Write-Verbose|Write-Information|Write-Warning)(?![\w-])/i;
115
+ /** sh heredoc start (`<<EOF`, `<<-'END'`), not the `<<<` here-string. */
116
+ const HEREDOC = /(?<!<)<<(?!<)-?\s*(['"]?)([A-Za-z_][A-Za-z0-9_]*)\1/;
117
+
118
+ function unquote(v , kind = "sh") {
119
+ const t = v.trim();
120
+ if (t.length >= 2 && t[0] === '"' && t.at(-1) === '"') return kind === "ps" ? t.slice(1, -1).replace(/`(.)/g, "$1") : t.slice(1, -1);
121
+ if (t.length >= 2 && t[0] === "'" && t.at(-1) === "'") return t.slice(1, -1);
122
+ return t;
123
+ }
124
+
125
+ function psValue(v ) {
126
+ const jp = new RegExp(`^${PS_JOIN}$`, "i").exec(v.trim());
127
+ if (jp) return `${unquote(jp[1] , "ps")}/${unquote(jp[2] , "ps")}`;
128
+ return unquote(v, "ps");
129
+ }
130
+
131
+ /** Raw CLAUDE_CONFIG_DIR values with the alias/function that encloses each (exported for tests). */
132
+ export function scanProfile(text , kind ) {
133
+ const out = [];
134
+ const stack = []; // ps/sh: brace scopes; fish: block scopes
135
+ let depth = 0;
136
+ let pending = null; // function header seen, body brace not yet
137
+ let inBlock = false; // PowerShell <# ... #>
138
+ let bodyEnd = null; // inside a heredoc / here-string body: its terminator test
139
+ for (const raw of text.replace(/^\uFEFF/, "").split(/\r?\n/)) {
140
+ let line = raw;
141
+ if (bodyEnd) {
142
+ if (bodyEnd(line)) bodyEnd = null;
143
+ continue;
144
+ }
145
+ if (kind === "ps") {
146
+ if (inBlock) {
147
+ const e = line.indexOf("#>");
148
+ if (e === -1) continue;
149
+ line = line.slice(e + 2);
150
+ inBlock = false;
151
+ }
152
+ for (let s = line.indexOf("<#"); s !== -1; s = line.indexOf("<#")) {
153
+ const e = line.indexOf("#>", s + 2);
154
+ if (e === -1) { line = line.slice(0, s); inBlock = true; break; }
155
+ line = line.slice(0, s) + line.slice(e + 2);
156
+ }
157
+ }
158
+ line = stripComment(line, kind);
159
+ if (line.trim() === "") continue;
160
+ if (kind === "ps") {
161
+ const hs = /@(["'])\s*$/.exec(line);
162
+ if (hs) { const end = `${hs[1]}@`; bodyEnd = (l) => l.trimStart().startsWith(end); }
163
+ } else if (kind === "sh") {
164
+ const hd = HEREDOC.exec(line);
165
+ if (hd) { const word = hd[2] ; bodyEnd = (l) => l.trim() === word; }
166
+ }
167
+ const printing = PRINT_LINE.test(line);
168
+
169
+ if (kind === "fish") {
170
+ for (const st of statements(line)) {
171
+ const words = st.trim().split(/\s+/);
172
+ const w0 = words[0] ?? "";
173
+ if (PRINT_LINE.test(st)) continue;
174
+ const alias = new RegExp(String.raw`^\s*alias\s+${NAME}(?:\s*=|\s+)`).exec(st)?.[1] ?? null;
175
+ const current = alias ?? ([...stack].reverse().find((s) => s.name !== null)?.name ?? null);
176
+ const fm = FISH_SET.exec(st);
177
+ if (fm) out.push({ value: unquote(fm[1] ), alias: current });
178
+ for (const m of st.matchAll(SH_ASSIGN)) out.push({ value: unquote(m[1] ), alias: current });
179
+ if (w0 === "function") stack.push({ name: words[1] ?? null, depth: 0 });
180
+ else if (["if", "for", "while", "switch", "begin"].includes(w0)) stack.push({ name: null, depth: 0 });
181
+ else if (w0 === "end") stack.pop();
182
+ }
183
+ continue;
184
+ }
185
+
186
+ const header = new RegExp(String.raw`^\s*function\s+${NAME}`, "i").exec(line)
187
+ ?? (kind === "sh" ? new RegExp(String.raw`^\s*${NAME}\s*\(\s*\)`).exec(line) : null);
188
+ const isHeader = header !== null;
189
+ if (header) pending = header[1] ;
190
+ const alias = kind === "sh" ? new RegExp(String.raw`^\s*alias\s+${NAME}=`).exec(line)?.[1] ?? null : null;
191
+ const current = alias ?? pending ?? stack.at(-1)?.name ?? null;
192
+ const regs = kind === "ps" ? [PS_ASSIGN, PS_SETENV] : [SH_ASSIGN];
193
+ if (!printing) for (const re of regs) for (const m of line.matchAll(re)) out.push({ value: kind === "ps" ? psValue(m[1] ) : unquote(m[1] ), alias: current });
194
+ for (const b of braces(line, kind)) {
195
+ if (b === "{") {
196
+ if (pending !== null) { stack.push({ name: pending, depth }); pending = null; }
197
+ depth++;
198
+ } else {
199
+ depth = Math.max(0, depth - 1);
200
+ while (stack.length && stack.at(-1) .depth >= depth) stack.pop();
201
+ }
202
+ }
203
+ if (pending !== null && !isHeader) pending = null; // header without a body brace on the next line
204
+ }
205
+ return out;
206
+ }
207
+
208
+ // ---------- expansion ----------
209
+
210
+ const isAbsolute = (p , platform ) =>
211
+ platform === "win32" ? /^[A-Za-z]:\//.test(p) || p.startsWith("//") : p.startsWith("/");
212
+
213
+ /** `$HOME`, `${HOME}`, `~`, `$env:USERPROFILE`, `%USERPROFILE%`, `${userHome}` expanded; null if unresolvable or relative. */
214
+ export function expandProfilePath(raw , home , platform ) {
215
+ const h = home.replace(/\\/g, "/").replace(/\/+$/, "");
216
+ let v = raw.trim();
217
+ if (v === "") return null;
218
+ v = v
219
+ .replace(/\$\{(?:env:)?(?:USERPROFILE|HOME|userHome)\}/gi, h)
220
+ .replace(/\$env:(?:USERPROFILE|HOME)(?![A-Za-z0-9_])/gi, h)
221
+ .replace(/\$HOME(?![A-Za-z0-9_])/gi, h)
222
+ .replace(/%USERPROFILE%/gi, h)
223
+ .replace(/%HOMEDRIVE%%HOMEPATH%/gi, h);
224
+ if (v === "~" || v.startsWith("~/") || v.startsWith("~\\")) v = h + v.slice(1);
225
+ if (/[$%`]/.test(v)) return null;
226
+ v = v.replace(/\\/g, "/");
227
+ if (!isAbsolute(v, platform)) return null;
228
+ return normalizeCwd(v, platform);
229
+ }
230
+
231
+ // ---------- discovery ----------
232
+
233
+ const foldCase = (platform ) => platform === "win32" || platform === "darwin";
234
+
235
+ function claudeJsonRoots(text ) {
236
+ if (text === null) return [];
237
+ try {
238
+ const j = JSON.parse(text.replace(/^\uFEFF/, "")) ;
239
+ return j && typeof j.projects === "object" && j.projects !== null && !Array.isArray(j.projects) ? Object.keys(j.projects) : [];
240
+ } catch { return []; }
241
+ }
242
+
243
+ export async function discoverAccountHomes(env ) {
244
+ const { platform } = env;
245
+ const home = normalizeCwd(env.home.replace(/\\/g, "/"), platform);
246
+ const key = (p ) => (foldCase(platform) ? p.toLowerCase() : p);
247
+ const cands = new Map ();
248
+ const add = (p , source , alias = null) => {
249
+ if (p === null) return;
250
+ const n = normalizeCwd(p.replace(/\\/g, "/"), platform);
251
+ let c = cands.get(key(n));
252
+ if (!c) cands.set(key(n), (c = { home: n, sources: new Set(), aliases: [] }));
253
+ c.sources.add(source);
254
+ if (alias && !c.aliases.includes(alias)) c.aliases.push(alias);
255
+ };
256
+
257
+ const top = await env.listDirs(home);
258
+ const defaultHome = join2(home, ".claude");
259
+ if (top.includes(".claude")) add(defaultHome, "default");
260
+ for (const n of [...top].sort()) if (n.startsWith(".claude-") && n.length > ".claude-".length) add(join2(home, n), "folder");
261
+
262
+ for (const f of await profileFiles(env)) {
263
+ const text = await env.readFile(f.path);
264
+ if (text === null) continue;
265
+ for (const a of scanProfile(text, f.kind)) add(expandProfilePath(a.value, home, platform), "shell-profile", a.alias);
266
+ }
267
+
268
+ const cur = env.env?.[VAR];
269
+ if (cur) add(expandProfilePath(cur, home, platform), "env");
270
+ for (const h of env.instanceHomes ?? []) add(expandProfilePath(h, home, platform), "t3-instance");
271
+
272
+ // vscode pins of known project roots: Claude's own project lists (~/.claude.json and <account>/.claude.json) + extras.
273
+ const roots = new Map ();
274
+ const addRoot = (r ) => { const n = normalizeCwd(r.replace(/\\/g, "/"), platform); if (n && isAbsolute(n, platform)) roots.set(key(n), n); };
275
+ for (const r of claudeJsonRoots(await env.readFile(join2(home, ".claude.json")))) addRoot(r);
276
+ for (const c of [...cands.values()]) for (const r of claudeJsonRoots(await env.readFile(join2(c.home, ".claude.json")))) addRoot(r);
277
+ for (const r of env.projectRoots ?? []) addRoot(r);
278
+ const list = [...roots.values()];
279
+ for (let i = 0; i < list.length; i += 32) {
280
+ const pins = await Promise.all(list.slice(i, i + 32).map(async (r) => {
281
+ const text = await env.readFile(`${r}/.vscode/settings.json`);
282
+ if (text === null) return null;
283
+ try { return pinFromSettings(parseJsonc(text.replace(/^\uFEFF/, ""))); } catch { return null; }
284
+ }));
285
+ for (const p of pins) if (p) add(expandProfilePath(p, home, platform), "vscode-pin");
286
+ }
287
+
288
+ const out = [];
289
+ for (const c of cands.values()) {
290
+ const isDefault = key(c.home) === key(defaultHome);
291
+ if (isDefault) { if (!top.includes(".claude")) continue; c.sources.add("default"); }
292
+ else {
293
+ const subs = await env.listDirs(c.home);
294
+ if (!subs.some((s) => key(s) === "projects")) continue;
295
+ }
296
+ const alias = c.aliases.find((a) => /claude/i.test(a)) ?? c.aliases[0] ?? null;
297
+ out.push({ home: c.home, sources: SOURCE_ORDER.filter((s) => c.sources.has(s)), alias });
298
+ }
299
+ return out.sort((a, b) => {
300
+ const da = a.sources.includes("default") ? 0 : 1, db = b.sources.includes("default") ? 0 : 1;
301
+ if (da !== db) return da - db;
302
+ return key(a.home) < key(b.home) ? -1 : key(a.home) > key(b.home) ? 1 : 0;
303
+ });
304
+ }
305
+
306
+ /** "folder, shell-profile (claude-org)" */
307
+ export function formatSources(d ) {
308
+ return d.sources.map((s) => (s === "shell-profile" && d.alias ? `${s} (${d.alias})` : s)).join(", ");
309
+ }
310
+
311
+ /**
312
+ * The accounts to use when there is no config.json: discovery (folders, aliases, profiles, pins, the shell, T3 instance
313
+ * homes) fed into detectAccounts, so ids follow T3 instance -> claudeAgent -> alias -> folder name. Read-only.
314
+ */
315
+ export async function resolveAccounts(
316
+ home , instances , env , platform = process.platform,
317
+ ) {
318
+ return (await resolveAccountsWithSources(home, instances, env, platform)).accounts;
319
+ }
320
+
321
+ /** resolveAccounts plus the discovered homes and where each was found (for the first plan's notice). */
322
+ export async function resolveAccountsWithSources(
323
+ home , instances , env , platform = process.platform,
324
+ ) {
325
+ const found = await discoverAccountHomes(nodeDiscoveryEnv({ home, platform, env, instanceHomes: instances.map((i) => i.homePath) }));
326
+ return { accounts: await detectAccounts(home, instances, found.map((f) => ({ home: f.home, alias: f.alias }))), found };
327
+ }
328
+
329
+ export function nodeDiscoveryEnv(o ) {
330
+ return {
331
+ home: o.home,
332
+ platform: o.platform ?? process.platform,
333
+ env: o.env ?? {},
334
+ instanceHomes: o.instanceHomes,
335
+ projectRoots: o.projectRoots,
336
+ readFile: async (p) => { try { return await readFile(p, "utf8"); } catch { return null; } },
337
+ listDirs: async (p) => {
338
+ try {
339
+ return (await readdir(p, { withFileTypes: true })).filter((e) => e.isDirectory() || e.isSymbolicLink()).map((e) => e.name);
340
+ } catch { return []; }
341
+ },
342
+ };
343
+ }