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,295 @@
1
+ // The guided assistant's shared types and prompt helpers. Steps never touch the world themselves: everything goes
2
+ // through AssistantDeps (reads, and t3sm's own commands for every write), so each step runs in tests with scripted
3
+ // answers and fakes.
4
+
5
+
6
+
7
+
8
+
9
+
10
+
11
+
12
+ import { makeCliUi, } from "../cli/ui.js";
13
+
14
+ import { stepHeader } from "../ui/components.js";
15
+
16
+
17
+ import { capsOf, } from "../ui/terminal.js";
18
+ import { wrapTo } from "../ui/text.js";
19
+ import { CATALOGUES, fmt, } from "./i18n.js";
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+ /** Where a returning user continues (steps.resumePoint). n: failures of the last full import; runId: the pilot's run. */
36
+
37
+
38
+ /** The steps of the assistant (steps.STEPS has exactly this many); `{total}` in a message is this number. */
39
+ export const TOTAL_STEPS = 11;
40
+
41
+ /** What organize would change in T3 Code, read-only (the dry run behind `t3sm organize`), with what the preview draws. */
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+ /** One configured Claude account and whether the t3sm plugin is installed in it (read from its installed_plugins.json). */
57
+
58
+
59
+
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+ /**
68
+ * A t3sm command run by the assistant: its exit code, what it printed (also shown to the user as it ran) and whether it
69
+ * found the lock taken and did nothing (Io.onLockBusy).
70
+ */
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
84
+
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
129
+
130
+
131
+
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+ /** q, Ctrl+C or closed input at a prompt. */
161
+ export class Quit extends Error { name = "Quit"; }
162
+ /** b at a prompt that allows it. */
163
+ export class Back extends Error { name = "Back"; }
164
+ /** The assistant ends here with this exit code (it has already said why). */
165
+ export class Stop extends Error {
166
+ name = "Stop";
167
+ code ;
168
+ constructor(code ) { super(`stop ${code}`); this.code = code; }
169
+ }
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ export function newCtx(d , o ) {
180
+ const base = o.caps ?? capsOf(d.term);
181
+ const caps = { ...base, color: o.color ? (base.color === 0 ? 1 : base.color) : 0 };
182
+ const ui = makeCliUi(caps, o.lang, o.now ?? Date.now);
183
+ return {
184
+ d, lang: o.lang, m: CATALOGUES[o.lang], color: caps.color > 0, ui, s: ui.s, f: ui.f, langFromFlag: o.langFromFlag,
185
+ defaultLang: o.defaultLang ?? "en", allowUntested: false, plan: null, done: [], worst: 0, resume: null, jumpTo: null, pilotVerify: null,
186
+ fullDone: false, syncOn: false, langOffered: false, summaryShown: false, returnTo: null, asked: new Set(),
187
+ };
188
+ }
189
+
190
+ /**
191
+ * The assistant's own glyphs as ASCII, for a console without Unicode: · « » “ ” ‘ ’ – — • … →. Only ever applied to
192
+ * catalogue text (before its placeholders are filled) and to the kit's words; never to a name, a folder or anything
193
+ * else that came from the user's data, which is shown as it is.
194
+ */
195
+ export function asciiText(s ) {
196
+ return s.replace(/·/g, "-").replace(/[«»“”]/g, '"').replace(/[‘’]/g, "'").replace(/[–—]/g, "-").replace(/•/g, "*")
197
+ .replace(/…/g, "...").replace(/→/g, "->");
198
+ }
199
+
200
+ /** The assistant's own text (a catalogue template, a kit word) as this terminal can show it. Never for data. */
201
+ export function glyphs(ctx , s ) {
202
+ return ctx.s.caps.unicode ? s : asciiText(s);
203
+ }
204
+
205
+ /** A catalogue message: its template's glyphs folded for the terminal, then its placeholders filled (data untouched). */
206
+ export function t(ctx , key , vars = {}) {
207
+ return fmt(glyphs(ctx, ctx.m[key]), { total: TOTAL_STEPS, ...vars });
208
+ }
209
+
210
+ /** The columns the assistant lays out for: the terminal's width, at most 80 (read each time: the window can be resized). */
211
+ export function width(ctx ) {
212
+ return Math.max(1, Math.min(80, Math.floor(ctx.d.term.columns) || 80));
213
+ }
214
+
215
+ /** Prints text wrapped to the terminal's width (at most 80 columns). */
216
+ export function say(ctx , text ) {
217
+ ctx.d.print(wrapTo(text, width(ctx)).join("\n") + "\n");
218
+ }
219
+
220
+ /** Prints lines already laid out (components). */
221
+ export function lines(ctx , ls ) {
222
+ if (ls.length === 0) return;
223
+ ctx.d.print(ls.join("\n") + "\n");
224
+ }
225
+
226
+ export function blank(ctx ) {
227
+ ctx.d.print("\n");
228
+ }
229
+
230
+ /** The step header: the rail of the 11 steps (every step before this one marked as behind) and "Step 3 of 11 · Accounts". */
231
+ export function heading(ctx , n ) {
232
+ const done = Array.from({ length: n - 1 }, (_, i) => i + 1);
233
+ blank(ctx);
234
+ lines(ctx, stepHeader(ctx.s, { n, total: TOTAL_STEPS, title: t(ctx, `title${n}` ), done, word: { step: t(ctx, "stepWord"), of: t(ctx, "ofWord") } }, width(ctx)));
235
+ }
236
+
237
+ /** The question's wrapped lines: all but the last are printed, the last one is the prompt itself. */
238
+ async function rawAsk(ctx , question ) {
239
+ // Room for the answer on the prompt line; a no-break space (ask) keeps the choices with the last word.
240
+ const ls = wrapTo(question, Math.max(1, width(ctx) - 4)).map((l) => l.replaceAll(NBSP, " "));
241
+ if (ls.length > 1) ctx.d.print(ls.slice(0, -1).join("\n") + "\n");
242
+ let a ;
243
+ try { a = await ctx.d.prompt(ls.at(-1) + " "); } catch { throw new Quit(); }
244
+ if (a.includes("\u0003")) throw new Quit();
245
+ return a.trim();
246
+ }
247
+
248
+ /** Joins the last word of a question to its choices so a wrap never leaves "[y/N/?/b/q]" alone on the prompt line. */
249
+ const NBSP = " ";
250
+
251
+ const YES = new Set(["y", "yes"]);
252
+ const NO = new Set(["n", "no"]);
253
+ const YES_PT = new Set(["s", "sim"]);
254
+ const NO_PT = new Set(["nao", "não"]);
255
+ const QUIT = new Set(["q", "quit", "sair", "exit"]);
256
+ const BACK = new Set(["b", "back", "voltar"]);
257
+ /** In Portuguese v (voltar) goes back too; b still does. */
258
+ const BACK_PT = new Set(["v"]);
259
+
260
+ /** The answer keys shown after a yes/no question: yes, no, back (the default in capitals). */
261
+ const KEYS = { en: { yes: "y", no: "n", back: "b" }, pt: { yes: "s", no: "n", back: "v" } };
262
+
263
+ /**
264
+ * A yes/no question with `[Y/n/?/b/q]` in English and `[S/n/?/v/q]` in Portuguese (the default in capitals; b / v only
265
+ * when going back is offered). `?` prints the longer explanation and asks again. Returns true for yes (y, yes; in
266
+ * Portuguese also s, sim); q throws Quit, b (in Portuguese also v, voltar) throws Back. Write questions default to no.
267
+ */
268
+ export async function ask(ctx , question , o ) {
269
+ const back = o.back !== false;
270
+ const k = KEYS[ctx.lang];
271
+ const keys = `[${o.def === "y" ? `${k.yes.toUpperCase()}/${k.no}` : `${k.yes}/${k.no.toUpperCase()}`}/?${back ? `/${k.back}` : ""}/q]`;
272
+ for (;;) {
273
+ const a = (await rawAsk(ctx, `${question}${NBSP}${keys}`)).toLowerCase();
274
+ if (a === "") return o.def === "y";
275
+ if (YES.has(a) || (ctx.lang === "pt" && YES_PT.has(a))) return true;
276
+ if (NO.has(a) || (ctx.lang === "pt" && NO_PT.has(a))) return false;
277
+ if (QUIT.has(a)) throw new Quit();
278
+ if (back && (BACK.has(a) || (ctx.lang === "pt" && BACK_PT.has(a)))) throw new Back();
279
+ if (a === "?") { say(ctx, o.help); continue; }
280
+ say(ctx, t(ctx, back ? "invalidYN" : "invalidYNNoBack"));
281
+ }
282
+ }
283
+
284
+ /** A free answer (a number, a name, a path). q quits, b goes back (when offered), ? explains. Returns it trimmed. */
285
+ export async function askText(ctx , question , o ) {
286
+ const back = o.back !== false;
287
+ for (;;) {
288
+ const a = await rawAsk(ctx, question);
289
+ const l = a.toLowerCase();
290
+ if (QUIT.has(l)) throw new Quit();
291
+ if (back && BACK.has(l)) throw new Back();
292
+ if (a === "?") { say(ctx, o.help); continue; }
293
+ return a;
294
+ }
295
+ }
@@ -0,0 +1,256 @@
1
+ // The guided assistant's real dependencies: thin wrappers over t3sm's own functions and commands. Reads call the
2
+ // functions behind doctor, accounts, plan, quarantine / dedupe (dry run), runs and schedule status; every write runs
3
+ // one of t3sm's commands (`run`), so each keeps its own lock, journal, checks and Ctrl+C handling.
4
+
5
+ import { readFile } from "node:fs/promises";
6
+ import { homedir, hostname } from "node:os";
7
+ import { join } from "node:path";
8
+ import { ConfigError, applyOrganizePatch, defaultConfig, detectAccounts, legacyHistory, loadConfig, saveConfig, } from "../core/config.js";
9
+ import { Journal } from "../core/journal.js";
10
+ import { acquireLock, lockInfo } from "../core/lock.js";
11
+ import { expandHome, sameCwd, stateDir, tildePath } from "../core/paths.js";
12
+ import { pidAlive } from "../core/pid.js";
13
+
14
+ import { discoverAccountHomes, nodeDiscoveryEnv, resolveAccounts } from "../ops/discoverAccounts.js";
15
+ import { defaultDoctorDeps, maskEmail, runDoctor } from "../ops/doctor.js";
16
+ import { dedupe, findBackupDirs, quarantine } from "../ops/quarantine.js";
17
+ import { listRuns } from "../ops/rollback.js";
18
+ import { connectT3, } from "../t3/api.js";
19
+ import { freshPlan, writeFreshPlan } from "../cli/commands/plan.js";
20
+ import { buildOrganize, connectForOrganize } from "../cli/commands/organize.js";
21
+
22
+ import { nodeTerm, } from "../ui/terminal.js";
23
+ import { accountsOf } from "../cli/commands/quarantine.js";
24
+ import { latestPlan } from "../cli/commands/apply.js";
25
+
26
+
27
+
28
+ import { installPlugin, packageVersion, readPluginState, } from "../ops/plugin.js";
29
+ import { defaultRunner } from "../t3/auth.js";
30
+ import { isLang, resolveLang, wrap, } from "./i18n.js";
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+ /** One child with the given environment, 2 minutes at most (a marketplace clone and an install). */
54
+ const spawnPluginChild = async (file, args, env) => {
55
+ const shell = process.platform === "win32" && /\.(cmd|bat)$/i.test(file);
56
+ if (shell && /["\r\n\0!%]/.test(file)) return { code: null, output: "unsafe path to claude" };
57
+ const r = await defaultRunner(file, args, { shell, timeoutMs: 120_000, env });
58
+ return { code: r.timedOut ? null : r.code, output: `${r.stdout}${r.stderr}` };
59
+ };
60
+
61
+ export function realAssistantDeps(io , o ) {
62
+ const home = o.home ?? homedir();
63
+ const now = o.now ?? Date.now;
64
+ const dir = stateDir(io.env, home);
65
+ const journal = new Journal(join(dir, "journal.jsonl"));
66
+ const childEnv = o.noColor ? { ...io.env, NO_COLOR: "1" } : io.env;
67
+ const readConfig = () => loadConfig(dir, home);
68
+ const connect = o.connect ?? connectT3;
69
+ // Notices from the read steps: each shown once, wrapped to 80 columns. The untested-version warning is left
70
+ // out: step 2 says it once, in the user's language.
71
+ const seen = new Set ();
72
+ const notice = (l ) => {
73
+ const s = l.replace(/\n+$/, "");
74
+ if (s === "" || seen.has(s) || /has not been tested with t3sm/.test(s)) return;
75
+ seen.add(s);
76
+ io.stderr(wrap(s).join("\n") + "\n");
77
+ };
78
+
79
+ // One terminal for the review lists. Without a TTY the assistant's own prompts read from it too, so piped answers are
80
+ // never split between two readers; on a TTY io.prompt is used (the lists read keys, the prompts read lines, never both).
81
+ const term = o.term ?? nodeTerm({ env: io.env });
82
+
83
+ return {
84
+ home,
85
+ print: (s) => io.stdout(s),
86
+ prompt: async (q) => (io.isTTY ? io.prompt(q) : (await term.readLine(q)) ?? "\u0003"),
87
+ term,
88
+
89
+ async lockHolder() {
90
+ const info = await lockInfo(dir).catch(() => null);
91
+ if (info === null) return null;
92
+ // A lock left by a process that is gone is not "held": the next command takes it over itself.
93
+ if (info.hostname === hostname() && !pidAlive(info.pid)) return null;
94
+ return { pid: info.pid };
95
+ },
96
+
97
+ async doctor() {
98
+ return (await runDoctor(defaultDoctorDeps({ home, env: io.env, repair: false }))).checks;
99
+ },
100
+
101
+ async config() {
102
+ const planFile = await latestPlan(dir);
103
+ const hasPlan = planFile !== null;
104
+ let cfg = null;
105
+ let language = null;
106
+ let configBroken = false;
107
+ try {
108
+ cfg = await readConfig();
109
+ language = cfg && isLang(cfg.language) ? cfg.language : null;
110
+ } catch (e) {
111
+ if (!(e instanceof ConfigError)) throw e;
112
+ configBroken = true; // doctor reports it
113
+ }
114
+ const exists = cfg !== null || configBroken;
115
+ // Questions still unanswered: from a fresh read-only plan when T3 Code answers (what the next plan will really ask),
116
+ // else from the newest saved plan (its own field; a damaged plan.json is apply's to report).
117
+ let decisionsPending = null;
118
+ if (hasPlan && cfg !== null) {
119
+ let api = null;
120
+ try {
121
+ api = await connect(home, { allowUntested: false, manageProviders: false, onNotice: notice });
122
+ const f = await freshPlan(api, { home, env: childEnv, now, adapters: o.adapters, missingConfig: "memory", readOnly: true, lang: io.lang ?? resolveLang(null, io.env, language), onNotice: notice });
123
+ decisionsPending = (f.plan.decisionsPending ?? []).length > 0;
124
+ } catch { decisionsPending = null; } finally { await api?.close().catch(() => undefined); }
125
+ }
126
+ decisionsPending ??= planFile === null ? false
127
+ : await readFile(planFile, "utf8").then((t) => ((JSON.parse(t) ).decisionsPending ?? []).length > 0, () => false);
128
+ return { exists, language, hasPlan, decisionsPending };
129
+ },
130
+
131
+ runs: async () => listRuns(await journal.readAll()),
132
+
133
+ async scheduleInstalled() {
134
+ try { return (await defaultDoctorDeps({ home, env: io.env, repair: false }).scheduleStatus()).installed; } catch { return false; }
135
+ },
136
+
137
+ async accounts(allowUntested) {
138
+ const api = await connect(home, { allowUntested, manageProviders: false, onNotice: notice });
139
+ try {
140
+ const instances = (await api.providerInstances()).filter((i) => i.driver === "claudeAgent");
141
+ const found = await discoverAccountHomes(nodeDiscoveryEnv({ home, env: io.env, instanceHomes: instances.flatMap((i) => (i.homePath === null ? [] : [i.homePath])) }));
142
+ const config = (await readConfig()) ?? defaultConfig(await detectAccounts(home, [], found.map((f) => ({ home: f.home, alias: f.alias }))));
143
+ return config.accounts.map((a) => {
144
+ const h = expandHome(a.home, home);
145
+ const inst = instances.find((i) => i.homePath !== null && sameCwd(i.homePath, h));
146
+ const f = found.find((x) => sameCwd(x.home, h));
147
+ return {
148
+ id: a.id, displayName: inst?.displayName ?? a.displayName, email: inst?.email ? maskEmail(inst.email) : null,
149
+ home: tildePath(h, home), foundVia: f ? [...f.sources] : ["config"], alias: f?.alias ?? null, hasInstance: inst !== undefined,
150
+ };
151
+ });
152
+ } finally {
153
+ await api.close();
154
+ }
155
+ },
156
+
157
+ async plan(allowUntested, lang) {
158
+ const r = await writeFreshPlan({ ...io, env: childEnv, stderr: notice }, { home, now, connect, adapters: o.adapters, lang }, allowUntested);
159
+ return { plan: r.plan, names: r.names, planPath: r.planPath, config: r.config };
160
+ },
161
+
162
+ async pluginAccounts() {
163
+ const config = (await readConfig()) ?? defaultConfig(await resolveAccounts(home, [], io.env));
164
+ const version = packageVersion();
165
+ return Promise.all(config.accounts.map(async (a) => {
166
+ const h = expandHome(a.home, home);
167
+ return { id: a.id, displayName: a.displayName, home: tildePath(h, home), state: await readPluginState(h, version) };
168
+ }));
169
+ },
170
+
171
+ // Two `claude plugin` children for this account only; the network is touched only inside them.
172
+ async installPlugin(accountId) {
173
+ const config = (await readConfig()) ?? defaultConfig(await resolveAccounts(home, [], io.env));
174
+ const a = config.accounts.find((x) => x.id === accountId);
175
+ if (!a) return { ok: false, step: "claude", output: `unknown account ${accountId}` };
176
+ return installPlugin(expandHome(a.home, home), { exec: o.pluginExec ?? spawnPluginChild, claudeBin: (o.claudeBin ?? (() => defaultDoctorDeps({ home, env: io.env, repair: false }).findBin("claude")))(), env: io.env });
177
+ },
178
+
179
+ async cleanupCounts(plan ) {
180
+ const accounts = await accountsOf(dir, home, io.env);
181
+ let backupFolders = 0;
182
+ for (const a of accounts) {
183
+ const dirs = await findBackupDirs(a.home);
184
+ if (dirs.length > 0) backupFolders += (await quarantine(dirs, a.home, journal, "preview", { dryRun: true, now })).moved.length;
185
+ }
186
+ const copies = new Set(plan.mirrorCopies.map((c) => c.path));
187
+ const mirrorCopies = plan.mirrorCopies.length === 0 ? 0
188
+ : (await dedupe(plan.mirrorCopies, accounts, journal, "preview", { dryRun: true, now, home })).moved.filter((m) => copies.has(m.from)).length;
189
+ return { backupFolders, mirrorCopies };
190
+ },
191
+
192
+ // config.json only, under the lock like every other config writer that can meet a running command.
193
+ async saveLanguage(lang ) {
194
+ const lock = await acquireLock(dir);
195
+ if (!lock) return false;
196
+ try {
197
+ const c = await readConfig();
198
+ if (c !== null) await saveConfig(dir, { ...c, language: lang }, home);
199
+ return true;
200
+ } finally {
201
+ await lock.release();
202
+ }
203
+ },
204
+
205
+ // The dry run behind `t3sm organize`: reads T3 Code and the transcripts, writes nothing (no lock, journal or cache).
206
+ // autoSettle: T3 Code's option as the run will set it (planned in this dry run, so its marks are counted).
207
+ async organizePreview(allowUntested, rule, autoSettle) {
208
+ const api = await connectForOrganize(connect, home, allowUntested, notice);
209
+ try {
210
+ const config = (await readConfig()) ?? defaultConfig(await resolveAccounts(home, [], io.env));
211
+ const b = await buildOrganize(api, journal, config, { only: null, dryRun: true, offerAll: true, ...(rule === undefined ? {} : { history: rule === null ? "off" : { days: rule.days, min: rule.minPerProject } }), ...(autoSettle === undefined ? {} : { autoSettle }) }, { ...io, env: childEnv, stderr: notice },
212
+ { home, now, connect, adapters: o.adapters, folderEnv: o.folderEnv, platform: o.folderEnv?.platform }, home, now);
213
+ if (!b.threads.some((t) => t.id.startsWith("import:"))) return null;
214
+ const legacy = legacyHistory(config);
215
+ return { plan: b.plan, projects: b.projects, threads: b.threads, autoSettleDays: b.autoSettleDays, history: b.history, off: b.off, ...(legacy !== null ? { legacyHistory: legacy } : {}) };
216
+ } finally {
217
+ await api.close();
218
+ }
219
+ },
220
+
221
+ // config.json only, under the lock (see saveLanguage).
222
+ async saveOrganize(patch ) {
223
+ const lock = await acquireLock(dir);
224
+ if (!lock) return false;
225
+ try {
226
+ const c = (await readConfig()) ?? defaultConfig(await resolveAccounts(home, [], io.env));
227
+ await saveConfig(dir, applyOrganizePatch(c, patch), home);
228
+ return true;
229
+ } finally {
230
+ await lock.release();
231
+ }
232
+ },
233
+
234
+ async about() {
235
+ const config = await readConfig().catch(() => null);
236
+ const accounts = config ? config.accounts.length : (await resolveAccounts(home, [], io.env).catch(() => [])).length;
237
+ return { version: packageVersion(), accounts };
238
+ },
239
+
240
+ // The child prints in the assistant's language and with the terminal's capabilities, on the same terminal: its progress
241
+ // bar is redrawn in place and its result, held projects and failures are shown as `t3sm apply` shows them.
242
+ async run(argv) {
243
+ let output = "";
244
+ let locked = false;
245
+ const caps = io.caps && o.noColor ? { ...io.caps, color: 0 } : io.caps;
246
+ const tee = {
247
+ ...io, env: childEnv, isTTY: io.isTTY, ...(io.lang ? { lang: io.lang } : {}), ...(caps ? { caps } : {}),
248
+ stdout: (s) => { output += s; io.stdout(s); },
249
+ stderr: (s) => { output += s; io.stderr(s); },
250
+ onLockBusy: () => { locked = true; },
251
+ };
252
+ const code = await o.main(argv, tee);
253
+ return { code, output, locked };
254
+ },
255
+ };
256
+ }
@@ -0,0 +1,38 @@
1
+ // The guided assistant's catalogue and the text helpers every step uses. The language itself (Lang, resolveLang, fmt)
2
+ // lives in src/i18n/lang.ts, shared with the commands' catalogues (src/i18n/cli.ts); re-exported here.
3
+
4
+ import { en } from "./messages.en.js";
5
+ import { pt } from "./messages.pt.js";
6
+
7
+ export { fmt, isLang, resolveLang, } from "../i18n/lang.js";
8
+
9
+
10
+
11
+
12
+
13
+ export const CATALOGUES = { en, pt };
14
+
15
+ export const WIDTH = 80;
16
+
17
+ /**
18
+ * Greedy word wrap of every line of `text` to `width` columns. A line keeps its leading spaces, and a bullet ("- ")
19
+ * hangs its continuation lines under the text. A single word longer than the width (a long path) keeps its own line.
20
+ */
21
+ export function wrap(text , width = WIDTH) {
22
+ const out = [];
23
+ for (const line of text.split("\n")) {
24
+ if (line.length <= width) { out.push(line.trimEnd()); continue; } // short lines keep their own spacing (aligned columns)
25
+ const lead = /^\s*/.exec(line) [0];
26
+ const bullet = /^\s*(?:- |\d+\. )/.exec(line);
27
+ const hang = bullet ? " ".repeat(bullet[0].length) : lead;
28
+ const words = line.slice(lead.length).split(/ +/).filter((w) => w !== "");
29
+ if (words.length === 0) { out.push(""); continue; }
30
+ let cur = lead + words[0];
31
+ for (const w of words.slice(1)) {
32
+ if (cur.length + 1 + w.length <= width) cur += " " + w;
33
+ else { out.push(cur); cur = hang + w; }
34
+ }
35
+ out.push(cur);
36
+ }
37
+ return out;
38
+ }