t3code-session-manager 0.0.0-stage → 0.4.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.
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 +138 -0
  4. package/LICENSE +21 -0
  5. package/README.md +660 -2
  6. package/README.pt.md +616 -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,288 @@
1
+ import { homedir } from "node:os";
2
+ import { readFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { stateDir } from "../../core/paths.js";
5
+ import { ConfigError, DEFAULT_SCHEDULE_MINUTES, loadConfig } from "../../core/config.js";
6
+ import {
7
+ ScheduleError, defaultBinPath, installSchedule, realRunner, removeSchedule, scheduleSpec, scheduleStatus,
8
+ } from "../../ops/schedule.js";
9
+ import { pidAlive, readHeartbeat, realClock } from "../../ops/watch.js";
10
+ import { markGoneWatcher, stopWatcher as realStopWatcher, } from "../../ops/watcherControl.js";
11
+ import { acquireLock, } from "../../core/lock.js";
12
+ import { reportLockBusy } from "../locked.js";
13
+ import { spawn as nodeSpawn } from "node:child_process";
14
+ import { detectConhost } from "../../ops/schedule.js";
15
+ import { cliUi, confirm, isCliKey, } from "../ui.js";
16
+ import { FLAG_SPECS } from "../flags.js";
17
+ import { callout, kv } from "../../ui/components.js";
18
+ import { noteLines, textLines } from "../screen.js";
19
+ import { shownText } from "../../plan/names.js";
20
+ import { spawnSuccessor, } from "../../ops/successor.js";
21
+ import { realIsElevated, warnIfElevated } from "../../ops/elevated.js";
22
+
23
+ import { systemRoot } from "../../core/sysroot.js";
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+ /** What the scheduler entry runs, in words ("t3sm sync --quiet every 15 min while you are logged on, ..."). */
48
+ function describe(ui , spec , every ) {
49
+ const what = spec.watch ? "t3sm sync --watch --quiet" : "t3sm sync --quiet";
50
+ const key = spec.kind === "schtasks" ? (spec.watch ? "schedule.desc.schtasksWatch" : "schedule.desc.schtasks")
51
+ : spec.kind === "launchd" ? (spec.watch ? "schedule.desc.launchdWatch" : "schedule.desc.launchd")
52
+ : spec.watch ? "schedule.desc.cronWatch" : "schedule.desc.cron";
53
+ return ui.t(key, { what, every });
54
+ }
55
+
56
+ /** What `t3sm schedule status` found, as aligned rows (and a warning when the entry points at paths that are gone). */
57
+
58
+
59
+
60
+
61
+
62
+
63
+ /** The scheduler's own state for a healthy entry (schtasks "Ready"/"Running", launchd "running"): not worth a word. */
64
+ const NORMAL_STATE = /^(ready|running|waiting)$/i;
65
+ /** A scheduler field that says nothing (schtasks "N/A"). */
66
+ const EMPTY_FIELD = /^(n\/a|-|)$/i;
67
+
68
+ /** A cycle's outcome (the heartbeat's, a sync.log line's) in words; an unknown one as it is. */
69
+ function outcomeWord(ui , kind ) {
70
+ const k = `schedule.st.outcome.${kind.replace(/-([a-z])/g, (_m, c ) => c.toUpperCase())}`;
71
+ return isCliKey(k) ? ui.t(k) : shownText(kind, 40);
72
+ }
73
+
74
+ /**
75
+ * A sync.log line that is not a cycle (the watcher's restart, an update, the daily check, a start-up stop) in words,
76
+ * or null for a cycle line. The versions and the error's name come from t3sm's own log, shown sanitized.
77
+ */
78
+ function eventWords(ui , kind , rest ) {
79
+ const field = (name ) => shownText(new RegExp(`\\b${name}=(\\S+)`).exec(rest)?.[1] ?? "?", 40);
80
+ const sub = /^\s*(\S+)/.exec(rest)?.[1] ?? "";
81
+ if (kind === "restart") return ui.t("schedule.st.event.restart", { to: field("to") });
82
+ if (kind === "update") {
83
+ const k = ({ installed: "schedule.st.event.updateInstalled", "rolled-back": "schedule.st.event.updateRolledBack", failed: "schedule.st.event.updateFailed", busy: "schedule.st.event.updateBusy" } )[sub];
84
+ return k ? ui.t(k, { to: field("to"), from: field("from") }) : null;
85
+ }
86
+ if (kind === "update-check") {
87
+ const k = ({ unavailable: "schedule.st.event.checkUnavailable", available: "schedule.st.event.checkAvailable", start: "schedule.st.event.checkStart" } )[sub];
88
+ return k ? ui.t(k, { to: field("to") }) : null;
89
+ }
90
+ if (kind === "version-unknown") return ui.t("schedule.st.event.versionUnknown");
91
+ if (kind === "startup-error") {
92
+ const name = /^\s*exit=\S+\s+(\S+)/.exec(rest)?.[1];
93
+ return ui.t("schedule.st.event.startupError", { name: name ? ` (${shownText(name, 40)})` : "" });
94
+ }
95
+ if (kind === "socket-dir-unsafe") return ui.t("schedule.st.event.socketDirUnsafe");
96
+ return null;
97
+ }
98
+
99
+ /** The last sync.log line in words: when, how it ended, how many it imported and its run; an unknown line as it is. */
100
+ export function loggedLine(ui , line ) {
101
+ const m = /^(\S+)\s+(\S+)(.*)$/.exec(line.trim());
102
+ if (!m || Number.isNaN(Date.parse(m[1] ))) return shownText(line, 400);
103
+ const at = m[1] ;
104
+ const event = eventWords(ui, m[2] , m[3] );
105
+ if (event !== null) return ui.t("schedule.st.loggedLine", { at: ui.f.date(at), ago: ui.f.ago(at), outcome: event });
106
+ const imported = /\bimported=(\d+)/.exec(m[3] )?.[1];
107
+ const run = /\brun=(\S+)/.exec(m[3] )?.[1];
108
+ const parts = [ui.t("schedule.st.loggedLine", { at: ui.f.date(at), ago: ui.f.ago(at), outcome: outcomeWord(ui, m[2] ) })];
109
+ if (imported !== undefined && Number(imported) > 0) parts.push(ui.t("schedule.st.loggedImported", { n: Number(imported) }));
110
+ if (run !== undefined && run !== "-") parts.push(ui.t("schedule.st.loggedRun", { runId: shownText(run, 60) }));
111
+ return parts.join(", ");
112
+ }
113
+
114
+ export function renderScheduleStatus(ui , v , width = ui.width) {
115
+ const { s, t } = ui;
116
+ const next = v.nextRun !== null && !EMPTY_FIELD.test(v.nextRun.trim()) ? v.nextRun : null;
117
+ const state = v.detail !== null && v.kind !== "cron" && !NORMAL_STATE.test(v.detail.trim()) && !EMPTY_FIELD.test(v.detail.trim()) ? v.detail : null;
118
+ const entry = !v.installed ? t("schedule.st.notInstalled")
119
+ : [t("schedule.st.installed", { kind: v.kind }), ...(next ? [t("schedule.st.next", { next: shownText(next, 60) })] : []), ...(state ? [t("schedule.st.state", { state: shownText(state, 60) })] : [])].join(", ");
120
+ const rows = [[t("schedule.st.entry"), entry]];
121
+ if (v.installed && v.kind === "cron" && v.detail) rows.push(["", v.detail]);
122
+ if (v.installed) rows.push([t("schedule.st.command"), v.command ? shownText(v.command, 400) : t("schedule.st.unreadable")]);
123
+ const w = v.watcher;
124
+ const last = w === null ? "" : w.lastCycleAt ? t("schedule.st.lastCycle", { at: ui.f.date(w.lastCycleAt), outcome: w.outcome ? ` (${outcomeWord(ui, w.outcome)})` : "" }) : t("schedule.st.noCycle");
125
+ rows.push([t("schedule.st.watcher"), w === null ? t("schedule.st.noWatcher")
126
+ : w.running ? t("schedule.st.running", { pid: w.pid, last, next: w.nextCycleAt ? ui.f.date(w.nextCycleAt) : "-" }) : t("schedule.st.notRunning", { last })]);
127
+ if (v.lastLogged) rows.push([t("schedule.st.logged"), loggedLine(ui, v.lastLogged)]);
128
+ const out = kv(s, rows, width);
129
+ if (v.installed && v.missing.length > 0) {
130
+ out.push("", ...callout(s, { kind: "warn", title: t("schedule.st.missingTitle"), body: textLines(s, t("schedule.st.missing", { paths: v.missing.map((m) => shownText(m, 300)).join(", "), n: v.missing.length }), Math.max(1, width - 2)), command: `t3sm schedule install${v.watch ? " --watch" : ""}` }, width));
131
+ }
132
+ return out;
133
+ }
134
+
135
+ export async function scheduleCommand(args , io , deps = {}) {
136
+ const [sub, ...rest] = args;
137
+ const ui = cliUi(io);
138
+ const say = (l ) => io.stdout(l.join("\n") + "\n");
139
+ const note = (tone , text ) => noteLines(ui.s, tone, text, ui.width);
140
+ const okFlags = sub === "install" ? [...FLAG_SPECS.schedule.bool] : sub === "remove" || sub === "restart" ? ["--dry-run", "--yes"] : [];
141
+ if ((sub !== "install" && sub !== "remove" && sub !== "restart" && sub !== "status") || rest.some((a) => !okFlags.includes(a))) {
142
+ io.stderr(`${ui.t("schedule.usage")}\n`);
143
+ return 2;
144
+ }
145
+ const dryRun = rest.includes("--dry-run");
146
+ if (dryRun && rest.includes("--yes")) { io.stderr(`${ui.t("common.eitherDryRunOrYes")}\n`); return 2; }
147
+ // Before any question: what this installs belongs to the user running it.
148
+ await warnIfElevated(io, deps.elevated ?? (deps.home === undefined ? realIsElevated : async () => false), "schedule");
149
+ if (sub !== "status" && !dryRun && !rest.includes("--yes")) {
150
+ if (!io.isTTY) { io.stderr(`${ui.t("schedule.needsYes")}\n`); return 2; }
151
+ const what = sub === "install" ? (rest.includes("--watch") ? "schedule.confirmInstallWatch" : "schedule.confirmInstall") : sub === "remove" ? "schedule.confirmRemove" : "schedule.confirmRestart";
152
+ if (!(await confirm(io, ui.t(what)))) { io.stdout(`${ui.t("common.nothingChanged")}\n`); return 0; }
153
+ }
154
+ const home = deps.home ?? homedir();
155
+ const dir = stateDir(io.env, home);
156
+ const run = deps.runner ?? realRunner;
157
+ const platform = deps.platform ?? process.platform;
158
+ const every = (await loadConfig(dir, home))?.schedule.everyMinutes ?? DEFAULT_SCHEDULE_MINUTES;
159
+ const stop = deps.stopWatcher ?? ((o ) => realStopWatcher({
160
+ stateDir: dir, platform, kill: (pid) => process.kill(pid), clock: realClock, ...(o?.restart ? { intent: "restart" } : {}),
161
+ }));
162
+ const alive = deps.isAlive ?? pidAlive;
163
+ /**
164
+ * Runs `fn` under the state lock, as `t3sm update` does: while a run holds it (the watcher's cycle in the middle of an
165
+ * import, say), nothing is stopped and nothing changes (the busy line, exit 0 like every writing command), so a stop
166
+ * never ends in a hard kill of a watcher that is writing.
167
+ */
168
+ const locked = async (fn ) => {
169
+ const lock = await (deps.lock ?? acquireLock)(dir);
170
+ if (!lock) { reportLockBusy(io); return 0; }
171
+ try { return await fn(); } finally { await lock.release(); }
172
+ };
173
+ const nodePath = deps.nodePath ?? process.execPath;
174
+ const binPath = deps.binPath ?? defaultBinPath();
175
+ const stillRunning = (r ) => ui.t(r.pid !== null ? "schedule.stillRunningPid" : "schedule.stillRunning", { pid: r.pid ?? 0 });
176
+ const started = (r ) => (r.how === "none" ? ui.t("schedule.startedNew") : ui.t(r.pid !== null ? "schedule.restartedPid" : "schedule.restarted", { pid: r.pid ?? 0 }));
177
+ /** The detached watcher (`sync --watch --quiet`), started outside the scheduler. */
178
+ const successor = () => spawnSuccessor({
179
+ platform, nodePath, binPath, spawn: deps.spawn ?? (nodeSpawn ),
180
+ conhost: deps.conhost !== undefined ? deps.conhost : platform === "win32" ? detectConhost() : null,
181
+ });
182
+ /**
183
+ * Starts the scheduled watcher now: schtasks /Run, launchd bootstrap (after a bootout) or, for cron, the detached spawn.
184
+ * win32: the detached spawn follows /Run as well. Task Scheduler may still count the stopped watcher's task instance as
185
+ * running and drop /Run (IgnoreNew, still exit 0); whichever of the two starts second finds the pipe taken and exits 0.
186
+ */
187
+ const startWatcher = async (spec ) => {
188
+ if (spec.kind === "cron") { successor(); return; }
189
+ if (spec.kind === "launchd") for (const c of spec.remove) await run(c[0] , c.slice(1));
190
+ const c = spec.kind === "launchd" ? spec.install[0] : spec.install[1] ;
191
+ const r = await run(c[0] , c.slice(1));
192
+ if (spec.kind === "schtasks") { successor(); return; }
193
+ if (r.code !== 0) throw new ScheduleError(`Could not start the watcher: ${c.join(" ")} failed${r.stderr.trim() ? ` (${r.stderr.trim()})` : ""}.`);
194
+ };
195
+ try {
196
+ const spec = scheduleSpec(platform, nodePath, binPath, every, {
197
+ watch: rest.includes("--watch") || sub === "restart", home, uid: deps.uid, conhost: deps.conhost, stateDir: dir,
198
+ systemRoot: systemRoot(io.env),
199
+ });
200
+ if (sub === "install") {
201
+ if (dryRun) {
202
+ say([...note("info", ui.t(spec.watch ? "schedule.wouldInstallWatch" : "schedule.wouldInstall", { kind: spec.kind, desc: describe(ui, spec, every) })), ...note("ok", ui.t("common.nothingChanged"))]);
203
+ return 0;
204
+ }
205
+ const done = (r , extra ) => {
206
+ const out = note("ok", ui.t("schedule.installed", { kind: spec.kind, desc: describe(ui, spec, every) }));
207
+ if (r !== null) out.push(...note("ok", started(r)));
208
+ out.push(...textLines(ui.s, ui.t("schedule.logs", { log: join(dir, "logs", "sync.log") }), ui.width, 2), ...extra);
209
+ say(out);
210
+ return 0;
211
+ };
212
+ if (!spec.watch) {
213
+ await installSchedule(spec, run, deps.fs);
214
+ const hb = await readHeartbeat(dir);
215
+ if (hb && !hb.stoppedAt && alive(hb.pid)) return done(null, note("warn", ui.t("schedule.watcherStillRuns", { pid: hb.pid })));
216
+ // A watcher that crashed (pid gone, no clean stop) is not coming back with an interval job: its heartbeat says so.
217
+ await markGoneWatcher(dir, { pidAlive: alive, now: Date.now() }).catch(() => false);
218
+ return done(null, []);
219
+ }
220
+ return locked(async () => {
221
+ const r = await stop({ restart: true });
222
+ if (r.running) { io.stderr(`${stillRunning(r)} ${ui.t("common.nothingChanged")}\n`); return 1; }
223
+ await installSchedule(spec, run, deps.fs); // win32: /Create, then /Run
224
+ if (spec.kind === "cron") await startWatcher(spec); // cron @reboot would wait for a reboot
225
+ else if (spec.kind === "schtasks") successor(); // /Run may have been dropped (see startWatcher)
226
+ return done(r, []);
227
+ });
228
+ }
229
+ if (sub === "remove") {
230
+ if (dryRun) {
231
+ say([...note("info", ui.t("schedule.wouldRemove")), ...note("ok", ui.t("common.nothingChanged"))]);
232
+ return 0;
233
+ }
234
+ return locked(async () => {
235
+ // The entry goes first: a keep-alive trigger firing between the stop and the removal would start a watcher
236
+ // that outlives the removal.
237
+ const gone = await removeSchedule(spec, run, deps.fs);
238
+ const r = await stop();
239
+ if (r.running) {
240
+ io.stderr(`${ui.t(gone.removed ? "schedule.removed" : "schedule.notInstalledRemove")} ${stillRunning(r)}\n`);
241
+ return 1;
242
+ }
243
+ say([
244
+ ...(r.how === "none" ? [] : note("ok", ui.t(r.pid !== null ? "schedule.stoppedPid" : "schedule.stopped", { pid: r.pid ?? 0 }))),
245
+ ...note("ok", ui.t(gone.removed ? "schedule.removed" : "schedule.notInstalledRemove")),
246
+ ]);
247
+ return 0;
248
+ });
249
+ }
250
+ if (sub === "restart") {
251
+ const st = await scheduleStatus(spec, run);
252
+ if (!st.installed || (st.command !== null && st.command !== undefined && !st.command.includes("--watch"))) {
253
+ // Nothing to restart. A watcher started by hand that crashed is not coming back either: its heartbeat says so,
254
+ // so `t3sm status` stops pointing here.
255
+ if (!dryRun) await markGoneWatcher(dir, { pidAlive: alive, now: Date.now() }).catch(() => false);
256
+ io.stderr(`${ui.t("schedule.noWatcherInstalled")}\n`);
257
+ return 2;
258
+ }
259
+ if (dryRun) {
260
+ say([...note("info", ui.t("schedule.wouldRestart")), ...note("ok", ui.t("common.nothingChanged"))]);
261
+ return 0;
262
+ }
263
+ return locked(async () => {
264
+ const r = await stop({ restart: true });
265
+ if (r.running) { io.stderr(`${stillRunning(r)} ${ui.t("schedule.nothingStarted")}\n`); return 1; }
266
+ await startWatcher(spec);
267
+ say(note("ok", started(r)));
268
+ return 0;
269
+ });
270
+ }
271
+ const st = await scheduleStatus(spec, run);
272
+ const hb = await readHeartbeat(dir);
273
+ let lastLogged = null;
274
+ try { lastLogged = (await readFile(join(dir, "logs", "sync.log"), "utf8")).trimEnd().split("\n").at(-1) || null; } catch { /* no log yet */ }
275
+ say(renderScheduleStatus(ui, {
276
+ kind: spec.kind, installed: st.installed, nextRun: st.nextRun, detail: st.detail, command: st.command, missing: st.missing, watch: spec.watch,
277
+ watcher: hb ? { running: !hb.stoppedAt && alive(hb.pid), pid: hb.pid, lastCycleAt: hb.lastCycleAt ?? null, outcome: typeof hb.lastResult?.["outcome"] === "string" ? hb.lastResult["outcome"] : null, nextCycleAt: hb.nextCycleAt ?? null } : null,
278
+ lastLogged,
279
+ }));
280
+ return 0;
281
+ } catch (e) {
282
+ if (e instanceof ScheduleError || e instanceof ConfigError) {
283
+ io.stderr(`${e.message}\n`);
284
+ return e instanceof ConfigError ? 2 : 1;
285
+ }
286
+ throw e;
287
+ }
288
+ }
@@ -0,0 +1,45 @@
1
+ // t3sm start (and bare `t3sm` on a terminal): the guided assistant. Not on a terminal: prints the quick start, exit 0.
2
+
3
+ import { homedir } from "node:os";
4
+ import { stateDir } from "../../core/paths.js";
5
+ import { loadConfig } from "../../core/config.js";
6
+
7
+ import { realAssistantDeps } from "../../assistant/deps.js";
8
+ import { CATALOGUES, resolveLang, wrap, } from "../../assistant/i18n.js";
9
+ import { parseGlobalFlags, unknownOption } from "../ui.js";
10
+ import { runAssistant } from "../../assistant/runner.js";
11
+ import { realIsElevated, warnIfElevated } from "../../ops/elevated.js";
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+ export async function startCommand(args , io , deps ) {
25
+ // main has already taken --lang / --no-color out of argv (Io.langFlag, Io.caps); a direct call passes them here, read
26
+ // by the same parser.
27
+ const g = parseGlobalFlags(args);
28
+ if ("error" in g) { io.stderr(g.error); return 2; }
29
+ if (g.rest.length > 0) { unknownOption(io, g.rest[0] , "start"); return 2; }
30
+ const flagLang = g.lang ?? io.langFlag ?? null;
31
+ const home = deps.home ?? homedir();
32
+ // An invalid config.json is doctor's to report (step 2): the language falls back to the environment's meanwhile.
33
+ const configLang = await loadConfig(stateDir(io.env, home), home).then((c) => c?.language ?? null, () => null);
34
+ const lang = resolveLang(flagLang, io.env, configLang);
35
+ if (!io.isTTY) {
36
+ io.stdout(wrap(CATALOGUES[lang].quickStart).join("\n") + "\n");
37
+ return 0;
38
+ }
39
+ await warnIfElevated({ stderr: (s) => io.stderr(s), lang }, deps.elevated ?? (deps.assistant === undefined && deps.home === undefined ? realIsElevated : async () => false), "start");
40
+ const noColor = g.noColor || io.caps?.color === 0 || Boolean(io.env["NO_COLOR"]);
41
+ // The commands the assistant runs print in its language.
42
+ const child = { ...io, lang };
43
+ const d = deps.assistant ?? realAssistantDeps(child, { main: deps.main, home, noColor });
44
+ return runAssistant(d, { lang, color: !noColor, langFromFlag: flagLang !== null, defaultLang: resolveLang(null, io.env, null), ...(io.caps ? { caps: io.caps } : {}) });
45
+ }