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,281 @@
|
|
|
1
|
+
// t3sm rollback / t3sm runs.
|
|
2
|
+
// rollback flow: --dry-run, or no --yes on a non-terminal: list (read-only, no lock) and exit 0 / 2.
|
|
3
|
+
// Otherwise: lock -> recoverPendingMtimes -> list -> confirm -> rollbackRun. T3 Code is needed only for the T3 part
|
|
4
|
+
// (defaults, instances, threads); without it the file-level rollback still runs (exit 5, reported).
|
|
5
|
+
|
|
6
|
+
import { homedir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { stateDir } from "../../core/paths.js";
|
|
9
|
+
import { homeAsTilde } from "../screen.js";
|
|
10
|
+
import { Journal, recoverPendingMtimes } from "../../core/journal.js";
|
|
11
|
+
import { acquireLock } from "../../core/lock.js";
|
|
12
|
+
import { connectT3, } from "../../t3/api.js";
|
|
13
|
+
import { changesOf, namesOf, undoLines, } from "./organize.js";
|
|
14
|
+
import { isRollbackRun, latestRunId, listRuns, organizeOnlyRun, rollbackRun, } from "../../ops/rollback.js";
|
|
15
|
+
import { accountsOf, parseFlags, } from "./quarantine.js";
|
|
16
|
+
import { cliUi, confirm, } from "../ui.js";
|
|
17
|
+
import { reportLockBusy } from "../locked.js";
|
|
18
|
+
import { FLAG_SPECS, flagsOf } from "../flags.js";
|
|
19
|
+
import { panel } from "../../ui/components.js";
|
|
20
|
+
import { noteLines, sectionLines, snugWidth, tableOrBlocks, textLines } from "../screen.js";
|
|
21
|
+
import { shownAccountName, shownText } from "../../plan/names.js";
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
/** Anything in the report that rollbackRun would change (as opposed to only list). */
|
|
25
|
+
function actionable(r ) {
|
|
26
|
+
return r.filesRemoved.length + r.foldersRemoved.length + r.unquarantined.length + r.defaultsRestored.length + r.threadsArchived.length + r.threadsUnarchived.length
|
|
27
|
+
+ r.instanceChanges.filter((c) => c.status === "restored").length + r.organize.filter((x) => x.status === "reverted").length;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** How organize changes are named: organize's sentences (`undoLines`), titles as T3 Code showed them before the rollback. */
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
/** An instance's journaled appearance with its display name masked (shownAccountName); anything else as it is. */
|
|
34
|
+
function maskedNames(v ) {
|
|
35
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) return v;
|
|
36
|
+
const o = v ;
|
|
37
|
+
return typeof o["displayName"] === "string" ? { ...o, displayName: shownAccountName(o["displayName"], 200) } : o;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A path or id from the journal, safe to show. */
|
|
41
|
+
const shown = (x ) => shownText(x, 200);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Why rollback kept or skipped something, in the user's language. The engine's reasons are English (the report is
|
|
45
|
+
* also --json); each known one has a catalogue line, and an error's own text stays as it is. `show` makes every value
|
|
46
|
+
* it carries safe to show (the rollback screen passes its own: the home folder as ~).
|
|
47
|
+
*/
|
|
48
|
+
export function rollbackWhy(ui , reason , show = shown) {
|
|
49
|
+
const { t } = ui;
|
|
50
|
+
const fixed = {
|
|
51
|
+
"revision continued": "rollback.why.revisionContinued",
|
|
52
|
+
"revision in use": "rollback.why.revisionInUse",
|
|
53
|
+
"revision rewritten": "rollback.why.revisionRewritten",
|
|
54
|
+
"T3 Code unreachable": "rollback.why.t3Unreachable",
|
|
55
|
+
"T3 Code not connected": "rollback.why.t3NotConnected",
|
|
56
|
+
"T3 Code not running": "rollback.why.t3NotRunning",
|
|
57
|
+
"project no longer exists in T3 Code": "rollback.why.projectGone",
|
|
58
|
+
"left active: it is the session's only active thread (its original thread is archived and could not be brought back)": "rollback.why.onlyActive",
|
|
59
|
+
};
|
|
60
|
+
const k = fixed[reason];
|
|
61
|
+
if (k) return t(k);
|
|
62
|
+
let m ;
|
|
63
|
+
if ((m = /^followed again by run (\S+); roll back \S+ first$/.exec(reason))) return t("rollback.why.followedAgain", { runId: show(m[1] ) });
|
|
64
|
+
if ((m = /^changed since \(now (.*)\)$/.exec(reason))) return t("rollback.why.changedSince", { now: m[1] === "none" ? t("rollback.why.none") : show(m[1] ) });
|
|
65
|
+
if ((m = /^T3 Code not reachable \((.*)\)$/s.exec(reason))) return t("rollback.why.t3NotReachable", { error: show(m[1] ) });
|
|
66
|
+
if ((m = /^failed: (.*)$/s.exec(reason))) return t("rollback.why.failed", { error: show(m[1] ) });
|
|
67
|
+
if ((m = /^left active: the session's other thread \((.*)\) no longer exists in T3 Code, so this is its only thread$/.exec(reason))) return t("rollback.why.otherThreadGone", { threads: show(m[1] ) });
|
|
68
|
+
if ((m = /^could not read T3 projects \((.*)\)$/s.exec(reason))) return t("rollback.why.projectsUnreadable", { error: show(m[1] ) });
|
|
69
|
+
if ((m = /^left active: its original thread (\S+) could not be unarchived$/.exec(reason))) return t("rollback.why.originalNotUnarchived", { thread: show(m[1] ) });
|
|
70
|
+
if ((m = /^could not move back \((.*)\)$/s.exec(reason))) return t("rollback.why.moveBack", { error: show(m[1] ) });
|
|
71
|
+
return show(reason);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What a rollback did (or, `dry`, would do), in the user's language: a panel naming the run, then one titled list per
|
|
76
|
+
* kind of change (at most 50 items each), what was kept and why, and what could not be done.
|
|
77
|
+
*/
|
|
78
|
+
export function renderRollback(ui , r , dry , words , width = ui.width, home ) {
|
|
79
|
+
const { s, t } = ui;
|
|
80
|
+
// Paths with the home folder as ~ (shorter, and never a user name on screen).
|
|
81
|
+
const shown = (x ) => shownText(home ? homeAsTilde(x, home) : x, 300);
|
|
82
|
+
const w = Math.max(1, width);
|
|
83
|
+
const head = t(r.command ? "rollback.runCommand" : "rollback.run", { runId: shown(r.targetRunId), command: shown(r.command ?? "") });
|
|
84
|
+
const out = [];
|
|
85
|
+
const list = (key , xs , vars = {}) => {
|
|
86
|
+
const lines = sectionLines(s, t(key, vars), xs.map(shown), w, { more: (n) => t("common.andMore", { n }) });
|
|
87
|
+
if (lines.length > 0) out.push("", ...lines);
|
|
88
|
+
};
|
|
89
|
+
const did = (done , will ) => (dry ? will : done);
|
|
90
|
+
list(did("rollback.unquarantined", "rollback.dry.unquarantined"), r.unquarantined);
|
|
91
|
+
list(did("rollback.filesRemoved", "rollback.dry.filesRemoved"), r.filesRemoved);
|
|
92
|
+
list("rollback.keptModified", r.filesKeptModified);
|
|
93
|
+
list("rollback.keptRewritten", r.filesKeptRewritten);
|
|
94
|
+
list("rollback.keptRefreshed", r.filesKeptRefreshed);
|
|
95
|
+
list("rollback.keptActive", r.filesKeptActive);
|
|
96
|
+
list(did("rollback.foldersRemoved", "rollback.dry.foldersRemoved"), r.foldersRemoved);
|
|
97
|
+
list("rollback.foldersKept", r.foldersKept);
|
|
98
|
+
list("rollback.conflicts", r.unquarantineConflicts.map((c) => `${c.from} -> ${c.to}`));
|
|
99
|
+
list("rollback.alreadyRestored", r.alreadyRestored);
|
|
100
|
+
list("rollback.missing", r.unquarantineMissing);
|
|
101
|
+
list("rollback.moveFailed", r.unquarantineFailed.map((f) => `${f.path}: ${rollbackWhy(ui, f.reason, shown)}`));
|
|
102
|
+
list(did("rollback.defaultsRestored", "rollback.dry.defaultsRestored"), r.defaultsRestored);
|
|
103
|
+
list("rollback.defaultsSkipped", r.defaultsSkipped.map((d) => `${d.projectId}: ${rollbackWhy(ui, d.reason, shown)}`));
|
|
104
|
+
list("rollback.instanceChanges", r.instanceChanges.map((c) => `${c.kind} ${c.instanceId}: ${JSON.stringify(maskedNames(c.previous))} <- ${JSON.stringify(maskedNames(c.next))} [${c.status === "restored" && dry ? t("rollback.wouldRestore") : c.status}]${c.note ? ` (${c.note.replace(/"([^"]*)"/g, (_m, n ) => JSON.stringify(shownAccountName(n, 200)))})` : ""}`));
|
|
105
|
+
if (r.instanceChanges.some((c) => c.status === "listed")) out.push(...textLines(s, t("rollback.passInstances"), w, 2));
|
|
106
|
+
list("rollback.instancesCreated", r.instancesCreated);
|
|
107
|
+
list("rollback.projectsCreated", r.projectsCreated.map((p) => `${p.projectId} ${p.root}`));
|
|
108
|
+
list(did("rollback.unarchived", "rollback.dry.unarchived"), r.threadsUnarchived);
|
|
109
|
+
list("rollback.mergesKept", r.mergesKept.map((m) => `${m.nativeId}: ${rollbackWhy(ui, m.reason, shown)} (${m.revisionFile})`));
|
|
110
|
+
list(did("rollback.archived", "rollback.dry.archived"), r.threadsArchived);
|
|
111
|
+
list("rollback.needsReview", r.needsReview.map((x) => `${x.threadId}: ${rollbackWhy(ui, x.reason, shown)}`));
|
|
112
|
+
const review = r.threadsToReview.filter((x) => !r.threadsArchived.includes(x));
|
|
113
|
+
list(r.threadsVerified ? "rollback.imported" : "rollback.importedUnchecked", review);
|
|
114
|
+
if (review.length > 0 && r.threadsArchived.length === 0) out.push(...textLines(s, t("rollback.reviewHint"), w, 2));
|
|
115
|
+
const org = undoLines(r.organize, words.changes, words.names);
|
|
116
|
+
list(did("rollback.reverted", "rollback.dry.reverted"), org.reverted);
|
|
117
|
+
list("rollback.orgUserChanged", org.userChanged);
|
|
118
|
+
list("organize.undo.keptTitle", org.kept);
|
|
119
|
+
list("rollback.orgFailed", org.failed);
|
|
120
|
+
list("rollback.parked", r.parkedThreads);
|
|
121
|
+
if (r.t3Unavailable) out.push("", ...noteLines(s, "warn", t("rollback.t3Part", { why: rollbackWhy(ui, r.t3Unavailable, shown) }), w));
|
|
122
|
+
for (const e of r.errors) out.push(...noteLines(s, "err", t("rollback.error", { error: shown(e) }), w));
|
|
123
|
+
const body = textLines(s, t(dry ? "rollback.dry.lead" : "rollback.lead"), Math.max(1, w - 4));
|
|
124
|
+
return [...panel(s, { title: head, body }, snugWidth(head, body, w)), ...out];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function rollbackCommand(args , io , deps = {}) {
|
|
128
|
+
const spec = flagsOf("rollback");
|
|
129
|
+
const p = parseFlags(args, io, "rollback", spec.bool, spec.valued);
|
|
130
|
+
if (!p) return 2;
|
|
131
|
+
const yes = p.flags.has("--yes");
|
|
132
|
+
const dry = p.flags.has("--dry-run");
|
|
133
|
+
const home = deps.home ?? homedir();
|
|
134
|
+
const now = deps.now ?? Date.now;
|
|
135
|
+
const ui = cliUi(io, { now });
|
|
136
|
+
const say = (l ) => io.stdout(l.join("\n") + "\n");
|
|
137
|
+
const dir = stateDir(io.env, home);
|
|
138
|
+
const j = new Journal(join(dir, "journal.jsonl"));
|
|
139
|
+
const entries = await j.readAll();
|
|
140
|
+
const target = p.values.get("--run") ?? latestRunId(entries);
|
|
141
|
+
if (target === null) { say(noteLines(ui.s, "ok", ui.t("rollback.noRuns"), ui.width)); return 0; }
|
|
142
|
+
if (!entries.some((e) => e.runId === target)) { io.stderr(`${ui.t("rollback.noSuchRun", { runId: shown(target) })}\n`); return 2; }
|
|
143
|
+
if (isRollbackRun(entries, target)) { io.stderr(`${ui.t("rollback.ofRollback")}\n`); return 2; }
|
|
144
|
+
// Without --run the target is the newest run, which may be an unattended `sync`: never chosen silently.
|
|
145
|
+
if (yes && !p.values.has("--run")) {
|
|
146
|
+
io.stderr(`${ui.t("rollback.needsRun", { runId: shown(target) })}\n`);
|
|
147
|
+
return 2;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const instances = p.flags.has("--instances");
|
|
151
|
+
const archiveThreads = p.flags.has("--archive-threads");
|
|
152
|
+
const connectFn = deps.connect ?? connectT3;
|
|
153
|
+
const allowUntested = p.flags.has("--allow-untested");
|
|
154
|
+
// Each notice once per command, although the interactive path runs a preview and then the rollback (two connects).
|
|
155
|
+
const seen = new Set ();
|
|
156
|
+
const onNotice = (l ) => {
|
|
157
|
+
if (seen.has(l)) return;
|
|
158
|
+
seen.add(l);
|
|
159
|
+
io.stderr(`${l}\n`);
|
|
160
|
+
};
|
|
161
|
+
// A rollback that touches T3 only through organize's commands runs on any version (ruling R18): the connection's own
|
|
162
|
+
// version gate is passed (allowUntested) and its notice, the only one connecting gives, is dropped; organizeGate in
|
|
163
|
+
// rollbackRun gives the one warning. Notices after the connection (a failed revoke at close) still show.
|
|
164
|
+
const organizeOnly = organizeOnlyRun(entries, target);
|
|
165
|
+
// providers:manage only for the run that may restore instances; a listing never asks for it.
|
|
166
|
+
// Organize changes are listed by title: T3 Code's threads and projects are read once, right after connecting and
|
|
167
|
+
// before anything is reverted (the titles the user knows them by now).
|
|
168
|
+
const changes = changesOf(entries, target);
|
|
169
|
+
const words = { changes, names: namesOf([], [], home, ui.lang) };
|
|
170
|
+
let named = false;
|
|
171
|
+
const nameFrom = async (api ) => {
|
|
172
|
+
if (changes.size === 0 || named) return api;
|
|
173
|
+
named = true;
|
|
174
|
+
try { words.names = namesOf(await api.listThreads(), await api.listProjects(), home, ui.lang); } catch { /* the rollback reports what it could not read */ }
|
|
175
|
+
return api;
|
|
176
|
+
};
|
|
177
|
+
// settings:write only for the rollback that may put T3 Code's own auto-settle option back (an organize change).
|
|
178
|
+
const settingBack = [...changes.values()].some((c) => c.kind === "auto-settle");
|
|
179
|
+
const connectWith = (manageProviders , writeSettings = false) => async () => {
|
|
180
|
+
const extra = writeSettings ? { writeSettings } : {};
|
|
181
|
+
if (!organizeOnly) return nameFrom(await connectFn(home, { allowUntested, manageProviders, ...extra, onNotice }));
|
|
182
|
+
let connecting = true;
|
|
183
|
+
try {
|
|
184
|
+
return await nameFrom(await connectFn(home, { allowUntested: true, manageProviders, ...extra, onNotice: (l) => { if (!connecting) onNotice(l); } }));
|
|
185
|
+
} finally {
|
|
186
|
+
connecting = false;
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const accountHomes = (await accountsOf(dir, home, io.env)).map((a) => a.home);
|
|
190
|
+
const opts = { journal: j, accountHomes, connect: connectWith(instances, settingBack), now, instances, archiveThreads, allowUntested, onNotice };
|
|
191
|
+
const dryOpts = { ...opts, connect: connectWith(false), dryRun: true };
|
|
192
|
+
const nothing = () => say(noteLines(ui.s, "ok", ui.t("rollback.nothing", { runId: shown(target) }), ui.width));
|
|
193
|
+
|
|
194
|
+
if (dry || (!yes && !io.isTTY)) {
|
|
195
|
+
const r = await rollbackRun(target, dryOpts);
|
|
196
|
+
say(renderRollback(ui, r, true, words, ui.width, home));
|
|
197
|
+
if (actionable(r) === 0) { nothing(); return 0; }
|
|
198
|
+
if (dry) { say(["", ...noteLines(ui.s, "info", ui.t("rollback.dryDone", { runId: shown(target) }), ui.width)]); return 0; }
|
|
199
|
+
io.stderr(`${ui.t("rollback.needsYes")}\n`);
|
|
200
|
+
return 2;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const lock = await acquireLock(dir);
|
|
204
|
+
if (!lock) { reportLockBusy(io); return 0; }
|
|
205
|
+
try {
|
|
206
|
+
const rec = await recoverPendingMtimes(j);
|
|
207
|
+
if (rec.restored.length > 0) io.stderr(`${ui.t("apply.recovered", { n: rec.restored.length })}\n`);
|
|
208
|
+
if (!yes) {
|
|
209
|
+
const preview = await rollbackRun(target, dryOpts);
|
|
210
|
+
say(renderRollback(ui, preview, true, words, ui.width, home));
|
|
211
|
+
if (actionable(preview) === 0) { nothing(); return 0; }
|
|
212
|
+
io.stdout("\n");
|
|
213
|
+
if (!(await confirm(io, preview.command ? ui.t("rollback.confirmCommand", { runId: target, command: preview.command }) : ui.t("rollback.confirm", { runId: target })))) {
|
|
214
|
+
io.stdout(`${ui.t("common.nothingChanged")}\n`);
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const r = await rollbackRun(target, opts);
|
|
219
|
+
say(renderRollback(ui, r, false, words, ui.width, home));
|
|
220
|
+
if (r.runId === null) nothing();
|
|
221
|
+
else say(["", ...noteLines(ui.s, "ok", ui.t("rollback.done", { runId: shown(target), journal: r.runId }), ui.width)]);
|
|
222
|
+
const incomplete = r.unquarantineConflicts.length + r.unquarantineFailed.length + r.defaultsSkipped.length + r.errors.length + r.needsReview.length
|
|
223
|
+
+ r.instanceChanges.filter((c) => c.status === "failed").length + r.organize.filter((x) => x.status === "failed").length;
|
|
224
|
+
return incomplete > 0 ? 5 : 0;
|
|
225
|
+
} finally {
|
|
226
|
+
await lock.release();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const CHANGE_WORDS = [
|
|
231
|
+
["imported", "runs.change.imported"], ["importFailed", "runs.change.failed"], ["filesWritten", "runs.change.files"],
|
|
232
|
+
["quarantined", "runs.change.quarantined"], ["unquarantined", "runs.change.unquarantined"], ["defaults", "runs.change.defaults"],
|
|
233
|
+
["instances", "runs.change.instances"], ["projects", "runs.change.projects"], ["archived", "runs.change.archived"],
|
|
234
|
+
["merged", "runs.change.merged"], ["organized", "runs.change.organized"], ["organizeReverted", "runs.change.reverted"],
|
|
235
|
+
];
|
|
236
|
+
|
|
237
|
+
const STATUS_WORDS = { ok: "runs.status.ok", partial: "runs.status.partial", aborted: "runs.status.aborted", incomplete: "runs.status.incomplete" };
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The runs in the journal, newest last: a table (run, started, command, status, changes) when it fits, else each run
|
|
243
|
+
* as a short block, so nothing is cut. Counts and statuses in words; the run ids and commands as they are.
|
|
244
|
+
*/
|
|
245
|
+
export function renderRuns(ui , runs , width = ui.width) {
|
|
246
|
+
const { s, t, f } = ui;
|
|
247
|
+
if (runs.length === 0) return noteLines(s, "ok", t("runs.none"), width);
|
|
248
|
+
const rows = runs.map((r) => {
|
|
249
|
+
const status = STATUS_WORDS[r.status] ? t(STATUS_WORDS[r.status] ) : r.status;
|
|
250
|
+
const changes = CHANGE_WORDS.filter(([k]) => r.counts[k] > 0).map(([k, w]) => t(w, { n: f.n(r.counts[k]) })).join(", ");
|
|
251
|
+
return {
|
|
252
|
+
id: shown(r.runId), at: r.at ? f.date(r.at) : "-", command: shown(r.command ?? "-"),
|
|
253
|
+
status: r.rolledBackBy ? t("runs.rolledBack", { status, runId: shown(r.rolledBackBy) }) : status, changes: changes || "-",
|
|
254
|
+
};
|
|
255
|
+
});
|
|
256
|
+
// Never cut: a run id, a count or a status that lost its end would say something else. A table when every cell fits
|
|
257
|
+
// whole, else one block per run (every value whole, wrapped under its label).
|
|
258
|
+
const col = (header , get ) => ({ header, get: (r ) => get(r ) });
|
|
259
|
+
const cols = [
|
|
260
|
+
col(t("runs.col.run"), (r) => r.id),
|
|
261
|
+
col(t("runs.col.started"), (r) => r.at),
|
|
262
|
+
col(t("runs.col.command"), (r) => r.command),
|
|
263
|
+
col(t("runs.col.status"), (r) => r.status),
|
|
264
|
+
col(t("runs.col.changes"), (r) => r.changes),
|
|
265
|
+
];
|
|
266
|
+
return tableOrBlocks(s, cols, rows, width);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export async function runsCommand(args , io , deps = {}) {
|
|
270
|
+
const p = parseFlags(args, io, "runs", [...FLAG_SPECS.runs.bool]);
|
|
271
|
+
if (!p) return 2;
|
|
272
|
+
const dir = stateDir(io.env, deps.home ?? homedir());
|
|
273
|
+
const runs = listRuns(await new Journal(join(dir, "journal.jsonl")).readAll());
|
|
274
|
+
if (p.flags.has("--json")) { io.stdout(JSON.stringify(runs, null, 2) + "\n"); return 0; }
|
|
275
|
+
io.stdout(renderRuns(cliUi(io), runs).join("\n") + "\n");
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** The options `t3sm rollback` accepts: the run is only ever named with --run (a positional is refused). */
|
|
280
|
+
export const ROLLBACK_FLAGS = [...FLAG_SPECS.rollback.bool];
|
|
281
|
+
export const ROLLBACK_VALUED = [...FLAG_SPECS.rollback.valued];
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { Journal, pendingMtimePaths } from "../../core/journal.js";
|
|
4
|
+
import { lockInfo } from "../../core/lock.js";
|
|
5
|
+
|
|
6
|
+
import { stateDir } from "../../core/paths.js";
|
|
7
|
+
import { loadConfig, defaultConfig } from "../../core/config.js";
|
|
8
|
+
import { resolveAccounts } from "../../ops/discoverAccounts.js";
|
|
9
|
+
import { allAdapters } from "../../sources/registry.js";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import { cliUi, unknownOption } from "../ui.js";
|
|
13
|
+
import { table } from "../../ui/components.js";
|
|
14
|
+
import { textLines } from "../screen.js";
|
|
15
|
+
import { shownAccountName, shownText } from "../../plan/names.js";
|
|
16
|
+
import { FLAG_SPECS } from "../flags.js";
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
/** One line for scan / plan when a run holds the lock or left file times bumped: bumped files read as recent. */
|
|
27
|
+
export const RUN_IN_PROGRESS_NOTE = "Note: a t3sm run is in progress or was interrupted; results may show sessions as active — run `t3sm doctor`.";
|
|
28
|
+
|
|
29
|
+
/** True when another run holds (or left) the lock, or the journal has file times not restored yet. Read-only. */
|
|
30
|
+
export async function runInProgress(dir ) {
|
|
31
|
+
if ((await lockInfo(dir).catch(() => null)) !== null) return true;
|
|
32
|
+
const entries = await new Journal(join(dir, "journal.jsonl")).readAll().catch(() => []);
|
|
33
|
+
return pendingMtimePaths(entries).length > 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function makeEnv(io , home , config , now ) {
|
|
37
|
+
return { home, platform: process.platform, now, config, stateDir: stateDir(io.env, home), env: io.env };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Runs every available adapter and collects sessions plus the number of per-file errors they recorded. */
|
|
41
|
+
export async function discoverAll(adapters , env ) {
|
|
42
|
+
const sessions = [];
|
|
43
|
+
const unavailable = [];
|
|
44
|
+
let errors = 0;
|
|
45
|
+
for (const a of adapters) {
|
|
46
|
+
if (!(await a.isAvailable(env))) {
|
|
47
|
+
unavailable.push(a.id);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
for await (const s of a.discover(env)) sessions.push(s);
|
|
51
|
+
errors += (a ).lastErrors?.length ?? 0;
|
|
52
|
+
}
|
|
53
|
+
return { sessions, errors, unavailable };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const AGENT_NAME = { claude: "Claude Code", codex: "Codex", pi: "Pi", opencode: "OpenCode" };
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The sessions per agent and account as a table (sessions and flagged ones right-aligned), then the totals and the
|
|
62
|
+
* agents that are not installed, in the user's language. An account shows its display name from config.json
|
|
63
|
+
* (`names`, by account id); one without a name shows its id. --json keeps the ids.
|
|
64
|
+
*/
|
|
65
|
+
export function renderScan(ui , d , width = ui.width, names = {}) {
|
|
66
|
+
const { s, t, f } = ui;
|
|
67
|
+
const groups = new Map ();
|
|
68
|
+
for (const x of d.sessions) {
|
|
69
|
+
const k = `${x.harness}\u0000${x.accountId ?? "-"}`;
|
|
70
|
+
const g = groups.get(k) ?? groups.set(k, { harness: x.harness, agent: AGENT_NAME[x.harness] ?? shownText(x.harness, 30), account: shownText(x.accountId === null ? "-" : names[x.accountId] ? shownAccountName(names[x.accountId] ) : x.accountId, 60), n: 0, flagged: 0 }).get(k) ;
|
|
71
|
+
g.n++;
|
|
72
|
+
if (x.flags.length > 0) g.flagged++;
|
|
73
|
+
}
|
|
74
|
+
// Two accounts of one agent with the same name are told apart by their id.
|
|
75
|
+
const ids = new Map ();
|
|
76
|
+
for (const x of d.sessions) if (x.accountId !== null) {
|
|
77
|
+
const name = `${x.harness}\u0000${groups.get(`${x.harness}\u0000${x.accountId}`) .account}`;
|
|
78
|
+
(ids.get(name) ?? ids.set(name, new Set()).get(name) ).add(x.accountId);
|
|
79
|
+
}
|
|
80
|
+
for (const [k, g] of groups) {
|
|
81
|
+
const id = k.split("\u0000")[1] ;
|
|
82
|
+
if (id !== "-" && g.account !== id && (ids.get(`${g.harness}\u0000${g.account}`)?.size ?? 0) > 1) g.account = `${g.account} (${shownText(id, 40)})`;
|
|
83
|
+
}
|
|
84
|
+
const rows = [...groups.values()].sort((a, b) => a.harness.localeCompare(b.harness) || a.account.localeCompare(b.account));
|
|
85
|
+
const out = rows.length === 0 ? textLines(s, t("scan.none"), width) : table(s, [
|
|
86
|
+
{ header: t("scan.col.agent"), get: (r) => (r ).agent, priority: 8 },
|
|
87
|
+
{ header: t("scan.col.account"), get: (r) => (r ).account, priority: 6, truncate: "middle" },
|
|
88
|
+
{ header: t("scan.col.sessions"), get: (r) => f.n((r ).n), align: "right", priority: 9 },
|
|
89
|
+
{ header: t("scan.col.flagged"), get: (r) => f.n((r ).flagged), align: "right", priority: 5 },
|
|
90
|
+
], rows, width);
|
|
91
|
+
out.push("", ...textLines(s, t("scan.total", { n: d.sessions.length, count: f.n(d.sessions.length), f: rows.reduce((n, r) => n + r.flagged, 0), e: d.errors }), width));
|
|
92
|
+
if (d.unavailable.length > 0) out.push(...textLines(s, t("scan.notInstalled", { list: d.unavailable.map((u) => AGENT_NAME[u] ?? u).join(", ") }), width));
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function scanCommand(args , io , deps = {}) {
|
|
97
|
+
const json = args.includes("--json");
|
|
98
|
+
const bad = args.find((a) => !(FLAG_SPECS.scan.bool ).includes(a));
|
|
99
|
+
if (bad !== undefined) {
|
|
100
|
+
unknownOption(io, bad, "scan");
|
|
101
|
+
return 2;
|
|
102
|
+
}
|
|
103
|
+
const home = deps.home ?? homedir();
|
|
104
|
+
const now = deps.now ?? Date.now;
|
|
105
|
+
// scan needs no T3: without a config, the detected accounts stand in (nothing is saved).
|
|
106
|
+
const config = (await loadConfig(stateDir(io.env, home), home)) ?? defaultConfig(await resolveAccounts(home, [], io.env));
|
|
107
|
+
if (await runInProgress(stateDir(io.env, home))) io.stderr(`${cliUi(io).t("scan.runInProgress")}\n`);
|
|
108
|
+
const adapters = deps.adapters ?? allAdapters();
|
|
109
|
+
const d = await discoverAll(adapters, makeEnv(io, home, config, now));
|
|
110
|
+
|
|
111
|
+
if (json) {
|
|
112
|
+
io.stdout(JSON.stringify(d.sessions, null, 2) + "\n");
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
io.stdout(renderScan(cliUi(io), d, undefined, Object.fromEntries(config.accounts.map((a) => [a.id, a.displayName]))).join("\n") + "\n");
|
|
116
|
+
return 0;
|
|
117
|
+
}
|