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.
- package/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +149 -0
- package/LICENSE +21 -0
- package/README.md +661 -2
- package/README.pt.md +617 -0
- package/bin/entry.d.mts +2 -0
- package/bin/entry.mjs +19 -0
- package/bin/t3sm.mjs +44 -0
- package/commands/import.md +30 -0
- package/commands/organize.md +36 -0
- package/commands/status.md +24 -0
- package/commands/sync.md +26 -0
- package/dist/apply/apply.js +993 -0
- package/dist/apply/claudeWriter.js +168 -0
- package/dist/apply/copyTranscript.js +116 -0
- package/dist/apply/importer.js +697 -0
- package/dist/apply/materialize.js +335 -0
- package/dist/apply/waves.js +108 -0
- package/dist/assistant/core.js +295 -0
- package/dist/assistant/deps.js +256 -0
- package/dist/assistant/i18n.js +38 -0
- package/dist/assistant/messages.en.js +494 -0
- package/dist/assistant/messages.pt.js +495 -0
- package/dist/assistant/runner.js +64 -0
- package/dist/assistant/steps.js +1362 -0
- package/dist/build.json +1 -0
- package/dist/cli/applyScreen.js +158 -0
- package/dist/cli/commands/accounts.js +254 -0
- package/dist/cli/commands/apply.js +199 -0
- package/dist/cli/commands/changelog.js +61 -0
- package/dist/cli/commands/doctor.js +105 -0
- package/dist/cli/commands/help.js +158 -0
- package/dist/cli/commands/merge.js +224 -0
- package/dist/cli/commands/organize.js +872 -0
- package/dist/cli/commands/plan.js +161 -0
- package/dist/cli/commands/projects.js +430 -0
- package/dist/cli/commands/quarantine.js +203 -0
- package/dist/cli/commands/recover.js +230 -0
- package/dist/cli/commands/rollback.js +281 -0
- package/dist/cli/commands/scan.js +117 -0
- package/dist/cli/commands/schedule.js +288 -0
- package/dist/cli/commands/start.js +45 -0
- package/dist/cli/commands/status.js +615 -0
- package/dist/cli/commands/sync.js +336 -0
- package/dist/cli/commands/update.js +398 -0
- package/dist/cli/errors.js +53 -0
- package/dist/cli/failures.js +91 -0
- package/dist/cli/flags.js +53 -0
- package/dist/cli/help.en.js +549 -0
- package/dist/cli/help.pt.js +566 -0
- package/dist/cli/helpText.js +211 -0
- package/dist/cli/helpTopics.js +58 -0
- package/dist/cli/locked.js +12 -0
- package/dist/cli/main.js +155 -0
- package/dist/cli/screen.js +3 -0
- package/dist/cli/ui.js +111 -0
- package/dist/cli/words.js +2 -0
- package/dist/core/activity.js +16 -0
- package/dist/core/changelog.js +47 -0
- package/dist/core/config.js +530 -0
- package/dist/core/fsutil.js +171 -0
- package/dist/core/glob.js +34 -0
- package/dist/core/history.js +32 -0
- package/dist/core/journal.js +274 -0
- package/dist/core/lock.js +337 -0
- package/dist/core/mask.js +31 -0
- package/dist/core/paths.js +68 -0
- package/dist/core/pid.js +101 -0
- package/dist/core/run.js +4 -0
- package/dist/core/semver.js +18 -0
- package/dist/core/sysroot.js +29 -0
- package/dist/core/types.js +229 -0
- package/dist/core/uuid.js +29 -0
- package/dist/core/version.js +72 -0
- package/dist/help/convert.md +20 -0
- package/dist/help/convert.pt.md +21 -0
- package/dist/help/cwd-unreadable.md +22 -0
- package/dist/help/cwd-unreadable.pt.md +22 -0
- package/dist/help/decisions.md +20 -0
- package/dist/help/decisions.pt.md +20 -0
- package/dist/help/errors.md +20 -0
- package/dist/help/errors.pt.md +22 -0
- package/dist/help/held.md +20 -0
- package/dist/help/held.pt.md +20 -0
- package/dist/help/history.md +27 -0
- package/dist/help/history.pt.md +29 -0
- package/dist/help/instance-unavailable.md +18 -0
- package/dist/help/instance-unavailable.pt.md +19 -0
- package/dist/help/not-imported.md +19 -0
- package/dist/help/not-imported.pt.md +19 -0
- package/dist/help/orphaned.md +19 -0
- package/dist/help/orphaned.pt.md +19 -0
- package/dist/help/project-mismatch.md +15 -0
- package/dist/help/project-mismatch.pt.md +15 -0
- package/dist/help/t3-native.md +22 -0
- package/dist/help/t3-native.pt.md +22 -0
- package/dist/help/updates.md +26 -0
- package/dist/help/updates.pt.md +28 -0
- package/dist/help/watcher.md +24 -0
- package/dist/help/watcher.pt.md +25 -0
- package/dist/i18n/cli.js +41 -0
- package/dist/i18n/lang.js +55 -0
- package/dist/i18n/messages.en.js +1140 -0
- package/dist/i18n/messages.pt.js +1149 -0
- package/dist/i18n/words.js +12 -0
- package/dist/ops/accounts.js +170 -0
- package/dist/ops/autoUpdate.js +223 -0
- package/dist/ops/discoverAccounts.js +343 -0
- package/dist/ops/doctor.js +811 -0
- package/dist/ops/elevated.js +47 -0
- package/dist/ops/errorCodes.js +39 -0
- package/dist/ops/follow.js +558 -0
- package/dist/ops/merge.js +830 -0
- package/dist/ops/notify.js +249 -0
- package/dist/ops/plugin.js +92 -0
- package/dist/ops/projects.js +113 -0
- package/dist/ops/quarantine.js +415 -0
- package/dist/ops/rollback.js +868 -0
- package/dist/ops/schedule.js +464 -0
- package/dist/ops/successor.js +51 -0
- package/dist/ops/sync.js +859 -0
- package/dist/ops/syncStatus.js +290 -0
- package/dist/ops/update.js +273 -0
- package/dist/ops/updater.js +641 -0
- package/dist/ops/watch.js +676 -0
- package/dist/ops/watcherControl.js +212 -0
- package/dist/ops/watcherHealth.js +75 -0
- package/dist/organize/apply.js +495 -0
- package/dist/organize/history.js +192 -0
- package/dist/organize/plan.js +283 -0
- package/dist/organize/rename.js +58 -0
- package/dist/organize/sessions.js +100 -0
- package/dist/organize/titles.js +70 -0
- package/dist/organize/types.js +73 -0
- package/dist/plan/categories.js +104 -0
- package/dist/plan/dedupe.js +137 -0
- package/dist/plan/folders.js +82 -0
- package/dist/plan/names.js +93 -0
- package/dist/plan/ownership.js +316 -0
- package/dist/plan/planner.js +669 -0
- package/dist/plan/rehome.js +101 -0
- package/dist/plan/report.js +407 -0
- package/dist/sources/cache.js +89 -0
- package/dist/sources/claude.js +297 -0
- package/dist/sources/codex.js +496 -0
- package/dist/sources/flatten.js +23 -0
- package/dist/sources/jsonl.js +169 -0
- package/dist/sources/native.js +233 -0
- package/dist/sources/opencode.js +483 -0
- package/dist/sources/pi.js +133 -0
- package/dist/sources/registry.js +67 -0
- package/dist/sources/types.js +80 -0
- package/dist/t3/api.js +492 -0
- package/dist/t3/auth.js +248 -0
- package/dist/t3/compat.js +62 -0
- package/dist/t3/decode.js +408 -0
- package/dist/t3/errors.js +185 -0
- package/dist/t3/rpc.js +185 -0
- package/dist/t3/runtime.js +80 -0
- package/dist/ui/caps.js +56 -0
- package/dist/ui/components.js +202 -0
- package/dist/ui/layout.js +205 -0
- package/dist/ui/list.js +423 -0
- package/dist/ui/locale.js +73 -0
- package/dist/ui/organizePreview.js +72 -0
- package/dist/ui/progress.js +237 -0
- package/dist/ui/style.js +32 -0
- package/dist/ui/terminal.js +126 -0
- package/dist/ui/text.js +186 -0
- package/package.json +58 -4
- package/skills/t3code-sessions/SKILL.md +183 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { mkdir, readFile } from "node:fs/promises";
|
|
3
|
+
import { spawn as nodeSpawn } from "node:child_process";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
import { stateDir } from "../../core/paths.js";
|
|
7
|
+
import { defaultConfig, loadConfig } from "../../core/config.js";
|
|
8
|
+
import { resolveAccounts } from "../../ops/discoverAccounts.js";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import { allAdapters } from "../../sources/registry.js";
|
|
12
|
+
|
|
13
|
+
import { appendSyncLog, detailLines, logLine, overLimitLine, runSyncCycle, summaryLines, } from "../../ops/sync.js";
|
|
14
|
+
import { T3NotRunning } from "../../t3/errors.js";
|
|
15
|
+
import { importProgress, resultPanel, waitingLines } from "../applyScreen.js";
|
|
16
|
+
import { FAILURES_SHOWN, failureTable } from "../failures.js";
|
|
17
|
+
import { fitLine, flowAtoms, richAtoms } from "../screen.js";
|
|
18
|
+
import { cliUi, unknownOption, usageLine } from "../ui.js";
|
|
19
|
+
import { UPDATE_INTERRUPTED_REASON, notifyOnce, systemNotifier, updateNotice, } from "../../ops/notify.js";
|
|
20
|
+
import { resolveLang, } from "../../assistant/i18n.js";
|
|
21
|
+
import { defaultRunner } from "../../t3/auth.js";
|
|
22
|
+
import { writeHeartbeatMerged } from "../../ops/syncStatus.js";
|
|
23
|
+
import { UnsafeSocketDirError, acquireWatcherSingleton, createDirWatch, readHeartbeat, realClock, startWatch, withStopRequest, } from "../../ops/watch.js";
|
|
24
|
+
import { LAUNCHD_LABEL, defaultBinPath, detectConhost } from "../../ops/schedule.js";
|
|
25
|
+
import { spawnSuccessor, } from "../../ops/successor.js";
|
|
26
|
+
import { installReady, installRoot, installStamp, packageVersion, } from "../../core/version.js";
|
|
27
|
+
import { Journal } from "../../core/journal.js";
|
|
28
|
+
import { acquireLock } from "../../core/lock.js";
|
|
29
|
+
import { readPendingUpdate, recoverInterruptedUpdate } from "../../ops/updater.js";
|
|
30
|
+
import { recordUpdateOutcome } from "../../ops/syncStatus.js";
|
|
31
|
+
import { watcherUpdateCheck } from "../../ops/autoUpdate.js";
|
|
32
|
+
import { describeError, oneLine, renderError } from "../errors.js";
|
|
33
|
+
import { makeEnv } from "./scan.js";
|
|
34
|
+
import { reportLockBusy } from "../locked.js";
|
|
35
|
+
import { FLAG_SPECS } from "../flags.js";
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
/** Exit code of a watcher that restarted on macOS: non-zero so launchd's KeepAlive relaunches the new install (EX_TEMPFAIL). */
|
|
71
|
+
export const RESTART_EXIT_LAUNCHD = 75;
|
|
72
|
+
|
|
73
|
+
export { spawnSuccessor, } from "../../ops/successor.js";
|
|
74
|
+
|
|
75
|
+
const FLAGS = new Set (FLAG_SPECS.sync.bool);
|
|
76
|
+
|
|
77
|
+
const processSignals = {
|
|
78
|
+
on: (sig, fn) => void process.on(sig , fn),
|
|
79
|
+
off: (sig, fn) => void process.off(sig , fn),
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export async function syncCommand(args , io , deps = {}) {
|
|
83
|
+
const bad = args.find((a) => !FLAGS.has(a));
|
|
84
|
+
if (bad !== undefined) { unknownOption(io, bad, "sync"); return 2; }
|
|
85
|
+
const quiet = args.includes("--quiet");
|
|
86
|
+
const watch = args.includes("--watch");
|
|
87
|
+
const dryRun = args.includes("--dry-run");
|
|
88
|
+
if (watch && dryRun) { usageLine(io, "usage.watchDryRun"); return 2; }
|
|
89
|
+
|
|
90
|
+
const home = deps.home ?? homedir();
|
|
91
|
+
const now = deps.now ?? Date.now;
|
|
92
|
+
const dir = stateDir(io.env, home);
|
|
93
|
+
// --watch: a stop request over the endpoint reaches the running cycle like SIGTERM (withStopRequest).
|
|
94
|
+
const stopper = watch ? withStopRequest(deps.signals ?? processSignals) : null;
|
|
95
|
+
const signals = stopper ?? deps.signals ?? processSignals;
|
|
96
|
+
const ui = cliUi(io, { now });
|
|
97
|
+
// One progress bar for a cycle's import; nothing at all with --quiet or --watch (the watcher prints its log lines).
|
|
98
|
+
const bar = importProgress(io, ui, now, { silent: quiet || watch, verbose: args.includes("--verbose") });
|
|
99
|
+
const cycleDeps = {
|
|
100
|
+
home, stateDir: dir, env: io.env, lang: io.langFlag, now, adapters: deps.adapters, connect: deps.connect, isT3Running: deps.isT3Running,
|
|
101
|
+
existsDir: deps.existsDir, allowUntested: args.includes("--allow-untested"), signals, afterApply: deps.afterApply,
|
|
102
|
+
notifier: deps.notifier ?? (deps.home === undefined ? systemNotifier(process.platform, defaultRunner) : undefined),
|
|
103
|
+
onNotice: (l) => { if (!quiet && !bar.note(l)) io.stderr(`${l}\n`); },
|
|
104
|
+
onProgress: (l) => bar.onProgress(l),
|
|
105
|
+
onImportStart: (n) => bar.onImportStart(n),
|
|
106
|
+
onWave: (p) => bar.onWave(p),
|
|
107
|
+
// Titles are read only for the failures the screen shows: never in a quiet or watcher cycle.
|
|
108
|
+
failureTitles: quiet || watch ? 0 : FAILURES_SHOWN,
|
|
109
|
+
};
|
|
110
|
+
const log = async (o , at ) => {
|
|
111
|
+
try { await appendSyncLog(dir, logLine(o, at)); } catch { /* a log failure never fails the cycle */ }
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
if (!watch) {
|
|
115
|
+
let o ;
|
|
116
|
+
try {
|
|
117
|
+
o = await runSyncCycle({ ...cycleDeps, dryRun });
|
|
118
|
+
bar.end();
|
|
119
|
+
} finally {
|
|
120
|
+
bar.dispose();
|
|
121
|
+
}
|
|
122
|
+
if (!dryRun) await log(o, now());
|
|
123
|
+
return report(o, io, quiet, ui, now());
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---------- --watch ----------
|
|
127
|
+
// Single instance: an OS endpoint (named pipe / unix socket) released when the process ends, crash included. The
|
|
128
|
+
// heartbeat only names the running watcher's pid for the message (and for `doctor`).
|
|
129
|
+
await mkdir(dir, { recursive: true });
|
|
130
|
+
// A stop request over the endpoint stops the running cycle after its current step (as Ctrl+C does) and then the loop
|
|
131
|
+
// (it may arrive before startWatch returns).
|
|
132
|
+
let stopLoop = null;
|
|
133
|
+
let stopAsked = false;
|
|
134
|
+
let single ;
|
|
135
|
+
try {
|
|
136
|
+
single = await (deps.singleton ?? acquireWatcherSingleton)(dir, process.platform, undefined, () => { stopAsked = true; stopper?.fire(); stopLoop?.(); });
|
|
137
|
+
} catch (e) {
|
|
138
|
+
if (!(e instanceof UnsafeSocketDirError)) throw e;
|
|
139
|
+
// Exit 0 on purpose: a non-zero exit would make launchd's KeepAlive restart the watcher every minute, forever.
|
|
140
|
+
// The reason is logged once (not on every keep-alive run) and shown to anyone watching.
|
|
141
|
+
await logOnce(dir, `socket-dir-unsafe ${e.dir}`, now);
|
|
142
|
+
if (!quiet) io.stderr(`t3sm: ${e.message}\n`);
|
|
143
|
+
return 0;
|
|
144
|
+
}
|
|
145
|
+
if (!single) {
|
|
146
|
+
// --quiet: the keep-alive run (cron every 15 min, Task Scheduler) must stay silent (no cron mail).
|
|
147
|
+
if (!quiet) io.stdout(`A t3sm watcher is already running (pid ${(await readHeartbeat(dir))?.pid ?? "?"}).\n`);
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
let released = false;
|
|
151
|
+
let exitCode = 0;
|
|
152
|
+
const platform = deps.platform ?? process.platform;
|
|
153
|
+
const release = async () => { if (released) return; released = true; await single.release(); };
|
|
154
|
+
try {
|
|
155
|
+
let config ;
|
|
156
|
+
let adapters ;
|
|
157
|
+
try {
|
|
158
|
+
config = (await loadConfig(dir, home)) ?? defaultConfig(await resolveAccounts(home, [], io.env));
|
|
159
|
+
adapters = deps.adapters ?? allAdapters();
|
|
160
|
+
} catch (e) {
|
|
161
|
+
// Started by the scheduler with nobody watching: the log is where this shows up (once per streak: the keep-alive
|
|
162
|
+
// retries every 15 minutes; `t3sm status` reads the line as "the background watcher cannot start").
|
|
163
|
+
await logOnce(dir, `startup-error exit=${describeError(e).code} ${(e )?.name ?? "Error"}`, now);
|
|
164
|
+
throw e;
|
|
165
|
+
}
|
|
166
|
+
const env = makeEnv(io, home, config, now);
|
|
167
|
+
let lastKey = null;
|
|
168
|
+
// The install this watcher runs: compared after every cycle, a newer one restarts the watcher into it.
|
|
169
|
+
const root = installRoot();
|
|
170
|
+
const readStamp = deps.installStamp ?? (() => installStamp(root));
|
|
171
|
+
let own = null;
|
|
172
|
+
try { own = readStamp(); } catch (e) {
|
|
173
|
+
try { await appendSyncLog(dir, `${new Date(now()).toISOString()} version-unknown ${(e )?.name ?? "Error"}`); } catch { /* ignore */ }
|
|
174
|
+
}
|
|
175
|
+
// An update interrupted earlier (t3sm killed mid-install, power loss) is journaled now, even when no newer release
|
|
176
|
+
// ever comes; under the lock, so an update that is running right now (it holds the lock) is never mistaken for one.
|
|
177
|
+
await recoverUpdateAtStart(dir, home, now, cycleDeps.notifier, io.env, io.langFlag);
|
|
178
|
+
const updateCheck = deps.updateCheck ?? (deps.home === undefined
|
|
179
|
+
? watcherUpdateCheck({ stateDir: dir, home, env: io.env, platform, now, notifier: cycleDeps.notifier }) : undefined);
|
|
180
|
+
const w = startWatch({
|
|
181
|
+
clock: deps.clock ?? realClock,
|
|
182
|
+
debounceMs: config.sync.watchDebounceMs,
|
|
183
|
+
everyMinutes: config.schedule.everyMinutes,
|
|
184
|
+
pid: process.pid,
|
|
185
|
+
...(process.platform !== "win32" && single.addr !== undefined ? { socket: single.addr } : {}),
|
|
186
|
+
signals,
|
|
187
|
+
dirs: async () => {
|
|
188
|
+
const out = [];
|
|
189
|
+
for (const a of adapters) {
|
|
190
|
+
if (a.watchRoots && (await a.isAvailable(env).catch(() => false))) out.push(...(await a.watchRoots(env).catch(() => [])));
|
|
191
|
+
}
|
|
192
|
+
return [...new Set(out)];
|
|
193
|
+
},
|
|
194
|
+
watch: deps.watchFactory ?? ((d, cb) => createDirWatch(d, cb)),
|
|
195
|
+
cycle: () => runSyncCycle(cycleDeps),
|
|
196
|
+
writeHeartbeat: (h) => writeHeartbeatMerged(dir, h), // keeps what the cycles recorded (needsAttention, undecided, ...)
|
|
197
|
+
install: own ? { root, stamp: own, read: readStamp, ready: deps.installReady ?? (() => installReady(root, now())) } : undefined,
|
|
198
|
+
version: packageVersion(),
|
|
199
|
+
onStampError: (e) => void appendSyncLog(dir, `${new Date(now()).toISOString()} version-unknown ${(e )?.name ?? "Error"}`).catch(() => {}),
|
|
200
|
+
...(updateCheck ? { afterCycle: async (stopped) => { await updateCheck(stopped); } } : {}),
|
|
201
|
+
restart: async (next) => {
|
|
202
|
+
try { await appendSyncLog(dir, `${new Date(now()).toISOString()} restart from=${own?.version ?? "?"} to=${next.version}`); } catch { /* ignore */ }
|
|
203
|
+
if (!quiet) io.stdout(`t3sm ${next.version} is installed: restarting the watcher into it.\n`);
|
|
204
|
+
await release(); // first: the successor must find the pipe free
|
|
205
|
+
// macOS under launchd (KeepAlive after a failed exit): a detached successor would run outside its job and nobody
|
|
206
|
+
// would bring back one that dies; a non-zero exit makes launchd start the newly installed bin itself.
|
|
207
|
+
// Only under launchd: a watcher started by hand on macOS has no KeepAlive behind it, so it spawns its successor.
|
|
208
|
+
if (platform === "darwin" && io.env["XPC_SERVICE_NAME"] === LAUNCHD_LABEL) { exitCode = RESTART_EXIT_LAUNCHD; return; }
|
|
209
|
+
spawnSuccessor({
|
|
210
|
+
platform, nodePath: process.execPath, binPath: defaultBinPath(), spawn: deps.spawn ?? (nodeSpawn ),
|
|
211
|
+
conhost: deps.conhost !== undefined ? deps.conhost : platform === "win32" ? detectConhost() : null,
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
onCycle: async (o, at) => {
|
|
215
|
+
// T3 closed for hours = one line, not one per minute; the over-limit line again only when the count changes.
|
|
216
|
+
const key = o.kind === "t3-down" || o.kind === "locked" ? o.kind : o.kind === "over-limit" ? `over-limit:${o.pending}` : null;
|
|
217
|
+
const repeat = key !== null && key === lastKey;
|
|
218
|
+
lastKey = key;
|
|
219
|
+
if (repeat) return;
|
|
220
|
+
await log(o, at);
|
|
221
|
+
if (o.kind === "over-limit") io.stdout(`${overLimitLine(o.pending)}\n`);
|
|
222
|
+
else if (!quiet) io.stdout(`${logLine(o, at)}\n`);
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
stopLoop = w.stop;
|
|
226
|
+
if (stopAsked) w.stop();
|
|
227
|
+
if (!quiet) io.stdout(`${ui.t("sync.watching")}\n`);
|
|
228
|
+
await w.done;
|
|
229
|
+
} finally {
|
|
230
|
+
await release();
|
|
231
|
+
}
|
|
232
|
+
return exitCode; // 0, or RESTART_EXIT_LAUNCHD after a restart on macOS
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Appends `what` to sync.log with the time, unless the log's last line already says exactly that: a watcher that
|
|
237
|
+
* stops at start-up is retried by the keep-alive every 15 minutes, and one line per streak is enough. Never throws.
|
|
238
|
+
*/
|
|
239
|
+
async function logOnce(dir , what , now ) {
|
|
240
|
+
try {
|
|
241
|
+
const last = (await readFile(join(dir, "logs", "sync.log"), "utf8").catch(() => "")).trimEnd().split(/\r?\n/).pop() ?? "";
|
|
242
|
+
if (!last.endsWith(` ${what}`)) await appendSyncLog(dir, `${new Date(now()).toISOString()} ${what}`);
|
|
243
|
+
} catch { /* ignore */ }
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* recoverInterruptedUpdate under the lock (busy: an update or a cycle runs; the next start looks again). A newly found
|
|
248
|
+
* interruption needs attention (update-failed) and is notified once per version. Never throws.
|
|
249
|
+
*/
|
|
250
|
+
async function recoverUpdateAtStart(dir , home , now , notifier , env , lang ) {
|
|
251
|
+
try {
|
|
252
|
+
const lock = await acquireLock(dir);
|
|
253
|
+
if (!lock) return;
|
|
254
|
+
let found = null;
|
|
255
|
+
try {
|
|
256
|
+
const seen = (await readPendingUpdate(dir))?.interrupted === true; // reported at an earlier start already
|
|
257
|
+
const p = await recoverInterruptedUpdate(dir, new Journal(join(dir, "journal.jsonl")));
|
|
258
|
+
if (p !== null && !seen) {
|
|
259
|
+
found = { from: p.previous.version, to: p.target };
|
|
260
|
+
await recordUpdateOutcome(dir, { outcome: "interrupted", ...found }, new Date(now()));
|
|
261
|
+
}
|
|
262
|
+
} finally {
|
|
263
|
+
await lock.release();
|
|
264
|
+
}
|
|
265
|
+
if (found !== null && notifier) {
|
|
266
|
+
const config = await loadConfig(dir, home).catch(() => null);
|
|
267
|
+
const n = updateNotice("interrupted", found, lang ?? resolveLang(null, env, config?.language ?? null));
|
|
268
|
+
await notifyOnce({
|
|
269
|
+
stateDir: dir, mode: config?.notify ?? "attention", notifier, now: now(), key: `${UPDATE_INTERRUPTED_REASON}:${found.to}`, title: n.title, body: n.body,
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
} catch { /* the journal or status file cannot be written: reported by doctor; never stops the watcher */ }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* A cycle's result on screen, in the user's language: "nothing to import" as one line, a dry run as its lines, an
|
|
277
|
+
* import as the result panel; then why held projects wait (each once), the other things the cycle did, and the
|
|
278
|
+
* failures as a table. Commands are shown whole and without backticks.
|
|
279
|
+
*/
|
|
280
|
+
export function renderSyncResult(ui , o , now , width = ui.width) {
|
|
281
|
+
const { s } = ui;
|
|
282
|
+
const w = Math.max(1, width);
|
|
283
|
+
const opts = { lang: ui.lang, dash: s.caps.unicode ? "—" : "-" };
|
|
284
|
+
const line = (lead , text ) => flowAtoms([...(lead ? [lead] : []), ...richAtoms(s, text.trim())], w, 2);
|
|
285
|
+
if (o.kind === "t3-down" || o.kind === "locked" || o.kind === "error") return [];
|
|
286
|
+
if (o.kind === "over-limit") return line("", overLimitLine(o.pending, ui.lang));
|
|
287
|
+
if (o.kind === "dry-run") return summaryLines(o, now, opts).flatMap((l, i) => line(i === 0 ? "" : s("warn", s.sym.warn), l));
|
|
288
|
+
const held = o.held ?? [];
|
|
289
|
+
// The held projects are said once: named under the panel (or counted in it), never again as a detail line.
|
|
290
|
+
const shownHeld = o.kind !== "noop" || held.length > 0;
|
|
291
|
+
const details = detailLines({ ...o, held: [], counts: shownHeld ? { ...o.counts, heldInUse: 0 } : o.counts }, now, opts);
|
|
292
|
+
const out = [];
|
|
293
|
+
if (o.kind === "noop") out.push(...fitLine(`${s("ok", s.sym.ok)} ${ui.t("sync.nothing")}`, w, 2));
|
|
294
|
+
else {
|
|
295
|
+
const c = o.counts;
|
|
296
|
+
out.push(...resultPanel(ui, {
|
|
297
|
+
status: o.kind === "aborted" ? "aborted" : o.kind === "partial" ? "partial" : "ok",
|
|
298
|
+
imported: c.imported, failed: c.failed, converted: 0, unverified: c.unverified, alreadyInT3: 0,
|
|
299
|
+
deferred: c.deferred - c.heldInUse, held: c.heldInUse, heldNamed: held.length > 0, runId: o.runId,
|
|
300
|
+
}, w));
|
|
301
|
+
}
|
|
302
|
+
const waiting = waitingLines(ui, held, w);
|
|
303
|
+
if (waiting.length > 0 || details.length > 0) out.push("", ...waiting, ...details.flatMap((d) => line(s("accent", s.sym.sep), d)));
|
|
304
|
+
if (o.failures && o.failures.length > 0) out.push("", ...failureTable(ui, o.failures, w));
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function report(o , io , quiet , ui , now ) {
|
|
309
|
+
if (o.kind === "t3-down") {
|
|
310
|
+
if (!quiet) io.stderr(renderError(new T3NotRunning(), ui).join("\n") + "\n");
|
|
311
|
+
return 3;
|
|
312
|
+
}
|
|
313
|
+
if (o.kind === "locked") {
|
|
314
|
+
reportLockBusy(io, quiet);
|
|
315
|
+
return 0;
|
|
316
|
+
}
|
|
317
|
+
if (o.kind === "over-limit") {
|
|
318
|
+
io.stdout(`${overLimitLine(o.pending, ui.lang)}\n`); // even under --quiet: the scheduler log shows it
|
|
319
|
+
return 0;
|
|
320
|
+
}
|
|
321
|
+
if (o.kind === "error") {
|
|
322
|
+
io.stderr(quiet ? `${oneLine(describeError(o.error).message)}\n` : renderError(o.error, ui).join("\n") + "\n");
|
|
323
|
+
return o.exitCode;
|
|
324
|
+
}
|
|
325
|
+
if (!quiet) io.stdout(renderSyncResult(ui, o, now).join("\n") + "\n");
|
|
326
|
+
if (o.kind === "aborted") {
|
|
327
|
+
io.stderr(`${ui.t("sync.interrupted")}\n`);
|
|
328
|
+
return 130;
|
|
329
|
+
}
|
|
330
|
+
if (o.kind === "partial") {
|
|
331
|
+
const c = o.counts;
|
|
332
|
+
io.stderr(`${ui.t("sync.partial", { failed: c.failed, unverified: c.unverified, runId: o.runId ?? "-" })}\n`);
|
|
333
|
+
return 5;
|
|
334
|
+
}
|
|
335
|
+
return 0;
|
|
336
|
+
}
|