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.
- package/.claude-plugin/marketplace.json +21 -0
- package/.claude-plugin/plugin.json +19 -0
- package/CHANGELOG.md +138 -0
- package/LICENSE +21 -0
- package/README.md +660 -2
- package/README.pt.md +616 -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
package/dist/ops/sync.js
ADDED
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
// `t3sm sync`: one unattended cycle = fresh plan + apply without asking, with the extra caution a scheduler needs.
|
|
2
|
+
// Safety rules (on top of everything applyPlan already does, including holding a project whose session is in use and
|
|
3
|
+
// parking every other in-window transcript of a project while it imports):
|
|
4
|
+
// - never creates T3 instances: sessions of an account without an instance wait for `t3sm accounts --apply`;
|
|
5
|
+
// - never quarantines or dedupes. Sessions continued in a terminal since t3sm imported them (Task 23) are only
|
|
6
|
+
// counted and reported ("run `t3sm merge`", once per change of the count) unless config.sync.autoMerge is true
|
|
7
|
+
// (default false): then, after an "ok" or no-op cycle, at most MERGES_PER_CYCLE are merged in their own
|
|
8
|
+
// `sync-merge` run, never retrying a revision whose import failed; a merge error never changes the cycle's result;
|
|
9
|
+
// - a session with a mirror copy inside T3's 30-day window waits for `t3sm dedupe` (the mirror itself is parked
|
|
10
|
+
// while its project imports, so the rest of the project goes on);
|
|
11
|
+
// - a session whose import failed after its last change waits for `t3sm apply` (no retry storm every cycle);
|
|
12
|
+
// - never overrides a per-project default the user chose in T3 (only new or default-less projects get one);
|
|
13
|
+
// - more than config.sync.maxUnattendedImports pending imports: nothing is written ("run `t3sm apply` once"), so the
|
|
14
|
+
// first big import always happens with someone watching;
|
|
15
|
+
// - a cycle with nothing to write writes nothing (no journal entry, no run folder, no unchanged discovery cache);
|
|
16
|
+
// - honours the organize decisions and never asks (spec 4.7, safety invariant 13): sessions of a category nobody has
|
|
17
|
+
// answered yet are not imported (counted per question, `needsAttention: undecided`); a copy from a folder T3 Code
|
|
18
|
+
// refuses is written only to a destination that already exists in T3 (the General folder is never created here,
|
|
19
|
+
// nor an inferred project that is not in T3 yet: held as `rehome-target-missing`); a new project folder is still
|
|
20
|
+
// created, with the clean name the planner gives it;
|
|
21
|
+
// - after a successful import, archives and retitles ONLY the threads that import just made (archive automated ones,
|
|
22
|
+
// clean titles), and applies the history rule (spec §5.2: the last `days` shown, at least `minPerProject` per
|
|
23
|
+
// project, older ones settled) to every imported thread of the projects that got new ones; once a day
|
|
24
|
+
// (HISTORY_INTERVAL_MS after sync-status.json's lastHistoryAt) it applies the rule to every project, also on a cycle
|
|
25
|
+
// with nothing to import. At most HISTORY_PER_CYCLE history changes per cycle (the first pass after an upgrade may
|
|
26
|
+
// have thousands): the rest continues on the next cycle. All of it inside the cycle's run and lock, so
|
|
27
|
+
// `t3sm rollback <run>` undoes it with the import; a thread changed by hand is never moved (the planner's
|
|
28
|
+
// user-touched rule). Never pins or unpins, never deletes a project, never changes a T3 setting. An organize failure
|
|
29
|
+
// (T3 refused) never undoes the import, is counted, and is not retried until the next cycle imports something new
|
|
30
|
+
// (or, for the history rule, the next daily pass);
|
|
31
|
+
// - then lets rehome copies follow their original (src/ops/follow.ts, spec §5.1.4), whatever sync.autoMerge says, in
|
|
32
|
+
// the same run and lock (at most FOLLOWS_PER_CYCLE; a copy continued itself is a conflict and is never written).
|
|
33
|
+
// One T3 session per cycle: the read-only connection that builds the plan is the one apply uses.
|
|
34
|
+
|
|
35
|
+
import { appendFile, mkdir, rename, rm, stat } from "node:fs/promises";
|
|
36
|
+
import { basename, join } from "node:path";
|
|
37
|
+
import { resolveLang, } from "../assistant/i18n.js";
|
|
38
|
+
|
|
39
|
+
import { defaultConfig, loadConfig } from "../core/config.js";
|
|
40
|
+
import { expandHome } from "../core/paths.js";
|
|
41
|
+
import { allAdapters, isConvertSession } from "../sources/registry.js";
|
|
42
|
+
import { discoverAll } from "../cli/commands/scan.js";
|
|
43
|
+
import { atomicWrite, statSafe } from "../core/fsutil.js";
|
|
44
|
+
import { Journal, newRunId, recoverPendingMtimes } from "../core/journal.js";
|
|
45
|
+
import { acquireLock } from "../core/lock.js";
|
|
46
|
+
import { STOP_SIGNALS, applyPlan, } from "../apply/apply.js";
|
|
47
|
+
|
|
48
|
+
import { cliT, } from "../i18n/cli.js";
|
|
49
|
+
import { inImportWindow } from "../core/activity.js";
|
|
50
|
+
import { rootKey } from "../apply/waves.js";
|
|
51
|
+
import { heldLine, renderPlanMarkdown } from "../plan/report.js";
|
|
52
|
+
import { connectT3, } from "../t3/api.js";
|
|
53
|
+
import { RpcTimeout } from "../t3/errors.js";
|
|
54
|
+
import { isT3Running } from "../t3/runtime.js";
|
|
55
|
+
|
|
56
|
+
import { defaultClaudeHome, planInstances } from "./accounts.js";
|
|
57
|
+
import { freshPlan } from "../cli/commands/plan.js";
|
|
58
|
+
import { buildOrganize } from "../cli/commands/organize.js";
|
|
59
|
+
|
|
60
|
+
import { JournalWriteError } from "./quarantine.js";
|
|
61
|
+
import { applyOrganize, failureText } from "../organize/apply.js";
|
|
62
|
+
import { resolveGeneralFolder } from "../plan/categories.js";
|
|
63
|
+
import { notifyAfterCycle, } from "./notify.js";
|
|
64
|
+
import { markHistoryPass, readLastHistoryAt, recordSyncStatus, } from "./syncStatus.js";
|
|
65
|
+
|
|
66
|
+
import { describeError } from "./errorCodes.js";
|
|
67
|
+
import { DEFERRED_REASONS, FAILED_REASONS, MERGES_PER_CYCLE, appendOrStop, executeMerges, findMergeCandidates, readMergePending, setMergePending, } from "./merge.js";
|
|
68
|
+
import { FOLLOWS_PER_CYCLE, followRehomed } from "./follow.js";
|
|
69
|
+
|
|
70
|
+
export const DEFAULT_MAX_UNATTENDED_IMPORTS = 200;
|
|
71
|
+
/** The daily pass of the history rule: once per 24 h (sync-status.json lastHistoryAt). */
|
|
72
|
+
export const HISTORY_INTERVAL_MS = 86_400_000;
|
|
73
|
+
/**
|
|
74
|
+
* History changes (settle / unsettle) per cycle, the daily pass and the one after an import together. One change is one
|
|
75
|
+
* T3 command, and applyOrganize re-reads the thread list every THREAD_BATCH (50) of them: 200 is a few seconds of T3's
|
|
76
|
+
* time per cycle, the same size as an unattended import (DEFAULT_MAX_UNATTENDED_IMPORTS). A first pass over thousands
|
|
77
|
+
* of old conversations takes several cycles instead of one long burst.
|
|
78
|
+
*/
|
|
79
|
+
export const HISTORY_PER_CYCLE = 200;
|
|
80
|
+
|
|
81
|
+
/** `plan` with at most `budget` history actions (the first ones: every unsettle comes before any settle); the rest counted. */
|
|
82
|
+
function capHistory(plan , budget ) {
|
|
83
|
+
let used = 0;
|
|
84
|
+
let deferred = 0;
|
|
85
|
+
const actions = plan.actions.filter((a) => {
|
|
86
|
+
if (a.kind !== "settle" && a.kind !== "unsettle") return true;
|
|
87
|
+
if (used < budget) { used++; return true; }
|
|
88
|
+
deferred++;
|
|
89
|
+
return false;
|
|
90
|
+
});
|
|
91
|
+
if (deferred === 0) return { plan, deferred };
|
|
92
|
+
const counts = { ...plan.counts };
|
|
93
|
+
for (const k of Object.keys(counts) ) counts[k] = actions.filter((a) => a.kind === k).length;
|
|
94
|
+
return { plan: { ...plan, actions, counts }, deferred };
|
|
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
|
+
/** A project held this cycle (named as T3 shows it) and the conversation holding it (spec 4.6; `t3sm status` shows it). Never logged. */
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
export const zeroCounts = () => ({
|
|
162
|
+
imported: 0, failed: 0, deferred: 0, unverified: 0, alreadyInT3: 0,
|
|
163
|
+
heldForDedupe: 0, heldForAccounts: 0, heldInUse: 0, heldFailed: 0, heldRehome: 0, wouldImport: 0, merged: 0, mergeDeferred: 0, mergeFailed: 0,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// ---------- the sync filter (pure) ----------
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
export function filterForSync(plan , o ) {
|
|
189
|
+
const platform = o.platform ?? process.platform;
|
|
190
|
+
const key = (p ) => rootKey(p, platform);
|
|
191
|
+
const newInstances = new Set(plan.instancesToCreate.map((s) => s.id));
|
|
192
|
+
const heldIds = new Set(plan.mirrorCopies.filter((m) => o.liveMirrorPaths.has(m.path)).map((m) => m.nativeId));
|
|
193
|
+
|
|
194
|
+
let heldForDedupe = 0;
|
|
195
|
+
let heldForAccounts = 0;
|
|
196
|
+
let heldFailed = 0;
|
|
197
|
+
let heldRehome = 0;
|
|
198
|
+
const existing = (root ) => o.projects.find((p) => !p.deleted && key(p.workspaceRoot) === key(root));
|
|
199
|
+
// Where a copy from a refused folder (spec 4.3) would go: the General folder or a project inferred from its paths.
|
|
200
|
+
const rehomeRoots = new Set(plan.items.flatMap((i) => (i.action.kind === "convert-import" && (i.action.rehomeFrom !== undefined || i.action.copy === "faithful") ? [key(i.action.projectRoot)] : [])));
|
|
201
|
+
const items = [];
|
|
202
|
+
for (const it of plan.items) {
|
|
203
|
+
const a = it.action;
|
|
204
|
+
if (a.kind === "skip") { items.push(it); continue; }
|
|
205
|
+
if (newInstances.has(a.instanceId)) { heldForAccounts++; continue; }
|
|
206
|
+
// Never creates a folder or project for a rehome copy: only where T3 already has the destination.
|
|
207
|
+
if (a.kind === "convert-import" && (a.rehomeFrom !== undefined || a.copy === "faithful" || rehomeRoots.has(key(a.projectRoot))) && !existing(a.projectRoot)) { heldRehome++; continue; }
|
|
208
|
+
if (heldIds.has(it.session.nativeId)) { heldForDedupe++; continue; }
|
|
209
|
+
const failedAt = o.failedBefore?.get(`${it.session.harness}:${it.session.nativeId}`);
|
|
210
|
+
// Changed since the failure? A convert-mode session is judged by its own activity (updatedAt): its source may be
|
|
211
|
+
// shared (OpenCode's db: any session's change moves its mtime) or rewritten unchanged (a Codex compressed rollout).
|
|
212
|
+
const changedAt = isConvertSession(it.session) ? Date.parse(it.session.updatedAt) : it.session.file.mtimeMs;
|
|
213
|
+
if (failedAt !== undefined && failedAt > changedAt) { heldFailed++; continue; }
|
|
214
|
+
items.push(it);
|
|
215
|
+
}
|
|
216
|
+
const importRoots = new Set(items.flatMap((i) => (i.action.kind === "skip" ? [] : [key(i.action.projectRoot)])));
|
|
217
|
+
const generalKey = o.generalFolder === undefined ? null : key(o.generalFolder);
|
|
218
|
+
return {
|
|
219
|
+
heldForDedupe,
|
|
220
|
+
heldForAccounts,
|
|
221
|
+
heldFailed,
|
|
222
|
+
heldRehome,
|
|
223
|
+
undecided: Object.fromEntries((plan.decisionsPending ?? []).map((d) => [d.key, d.sessions])),
|
|
224
|
+
plan: {
|
|
225
|
+
...plan,
|
|
226
|
+
items,
|
|
227
|
+
instancesToCreate: [],
|
|
228
|
+
projectsToCreate: plan.projectsToCreate.filter((p) => importRoots.has(key(p.root)) && !(generalKey !== null && key(p.root) === generalKey && !existing(p.root))),
|
|
229
|
+
projectDefaults: plan.projectDefaults.filter((d) => {
|
|
230
|
+
if (newInstances.has(d.instanceId)) return false;
|
|
231
|
+
const p = existing(d.root);
|
|
232
|
+
return p ? p.defaultModelSelection == null : importRoots.has(key(d.root));
|
|
233
|
+
}),
|
|
234
|
+
// The live ones are parked by the waves; apply would only warn about them again.
|
|
235
|
+
mirrorCopies: plan.mirrorCopies.filter((m) => !o.liveMirrorPaths.has(m.path)),
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** plan.mirrorCopies whose file is inside T3's 30-day import window now. */
|
|
241
|
+
export async function liveMirrorPaths(plan , now ) {
|
|
242
|
+
const out = new Set ();
|
|
243
|
+
for (const m of plan.mirrorCopies) {
|
|
244
|
+
const st = await statSafe(m.path).catch(() => null);
|
|
245
|
+
if (st && inImportWindow(st.mtimeMs, now)) out.add(m.path);
|
|
246
|
+
}
|
|
247
|
+
return out;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const RUN_ID_RE = /^(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})-/;
|
|
251
|
+
|
|
252
|
+
/** "harness:nativeId" -> time of its newest import-failed entry (from the run id, UTC). Unparseable run ids are ignored. */
|
|
253
|
+
export function failedImports(entries ) {
|
|
254
|
+
const out = new Map ();
|
|
255
|
+
for (const e of entries) {
|
|
256
|
+
if (e.t !== "import-failed") continue;
|
|
257
|
+
const m = RUN_ID_RE.exec(e.runId);
|
|
258
|
+
if (!m) continue;
|
|
259
|
+
const at = Date.UTC(+m[1] , +m[2] - 1, +m[3] , +m[4] , +m[5] , +m[6] );
|
|
260
|
+
const k = `${e.harness}:${e.nativeId}`;
|
|
261
|
+
if (at > (out.get(k) ?? -Infinity)) out.set(k, at);
|
|
262
|
+
}
|
|
263
|
+
return out;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
class OverLimit extends Error {
|
|
267
|
+
name = "OverLimit";
|
|
268
|
+
pending ;
|
|
269
|
+
limit ;
|
|
270
|
+
constructor(pending , limit ) {
|
|
271
|
+
super(`${pending} pending > ${limit}`);
|
|
272
|
+
this.pending = pending;
|
|
273
|
+
this.limit = limit;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const UNREACHABLE = new Set(["ECONNREFUSED", "ECONNRESET", "EHOSTUNREACH", "ENETUNREACH", "ETIMEDOUT", "EPIPE", "UND_ERR_SOCKET", "UND_ERR_CONNECT_TIMEOUT"]);
|
|
278
|
+
|
|
279
|
+
/** T3 went away or does not answer: retry like T3 down (60 s), not like an error. */
|
|
280
|
+
function unreachable(e ) {
|
|
281
|
+
if (e instanceof RpcTimeout) return true;
|
|
282
|
+
for (let c = e, i = 0; c && i < 4; c = (c ).cause, i++) {
|
|
283
|
+
const code = (c ).code;
|
|
284
|
+
if (typeof code === "string" && UNREACHABLE.has(code)) return true;
|
|
285
|
+
}
|
|
286
|
+
return e instanceof TypeError && /fetch failed/i.test(e.message);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// ---------- one cycle ----------
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
/** What a cycle saw besides its outcome, for sync-status.json (set by build). */
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
export async function runSyncCycle(deps ) {
|
|
333
|
+
const recovered = { restored: 0, failed: 0 };
|
|
334
|
+
const info = { undecided: {}, heldRehome: 0, signedOut: 0, planned: { projects: 0, newProjects: [] } };
|
|
335
|
+
const o = await cycle(deps, recovered, info);
|
|
336
|
+
if (!deps.dryRun) {
|
|
337
|
+
const obs = observe(o, info);
|
|
338
|
+
// A status file that cannot be written never fails the cycle.
|
|
339
|
+
const status = obs === null ? null : await recordSyncStatus(deps.stateDir, obs, new Date((deps.now ?? Date.now)())).catch(() => null);
|
|
340
|
+
if (status !== null && deps.notifier) {
|
|
341
|
+
const imported = o.kind === "ok" || o.kind === "partial" ? o.counts.imported : 0;
|
|
342
|
+
const summary = imported > 0 ? { imported, projects: Math.min(info.planned.projects, imported), newProjects: info.planned.newProjects } : null;
|
|
343
|
+
const config = await loadConfig(deps.stateDir, deps.home).catch(() => null);
|
|
344
|
+
// The watcher has no Io: the notification's language is resolved like the plan's (--lang, env, config.json).
|
|
345
|
+
const lang = deps.lang ?? resolveLang(null, deps.env ?? process.env, config?.language ?? null);
|
|
346
|
+
await notifyAfterCycle({ stateDir: deps.stateDir, mode: config?.notify ?? "attention", notifier: deps.notifier, now: (deps.now ?? Date.now)(), attention: status.needsAttention, summary, lang }).catch(() => []);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return recovered.restored > 0 || recovered.failed > 0 ? { ...o, recovered } : o;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The attention a cycle raised, by priority; null: nothing needs a person. Each carries its facts (the numbers it is
|
|
355
|
+
* about) next to the English detail, worded from the same catalogue entry, so any language can be told from them.
|
|
356
|
+
*/
|
|
357
|
+
export function attentionOf(o , info ) {
|
|
358
|
+
const said = (reason , key , facts , command ) =>
|
|
359
|
+
({ reason, detail: cliT("en", key, facts ), command, facts });
|
|
360
|
+
if (info.signedOut > 0) return said("signed-out", "notify.signedOut", { n: info.signedOut }, "t3sm accounts");
|
|
361
|
+
if (o.kind === "over-limit") return said("over-limit", "notify.overLimit", { n: o.pending, limit: o.limit }, "t3sm start");
|
|
362
|
+
const sessions = Object.values(info.undecided).reduce((a, n) => a + n, 0);
|
|
363
|
+
const questions = Object.keys(info.undecided).length;
|
|
364
|
+
if (questions > 0) return said("undecided", "notify.undecided", { n: sessions, q: questions }, "t3sm start");
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** A T3 Code version t3sm cannot talk to: needs a person (t3-incompatible). */
|
|
369
|
+
export function incompatibleAttention(version ) {
|
|
370
|
+
return { reason: "t3-incompatible", detail: cliT("en", "notify.incompatible", { version }), command: "t3sm doctor", facts: { version } };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** What sync-status.json should record for this outcome; null: the cycle did not look at anything (T3 closed, lock busy). */
|
|
374
|
+
function observe(o , info ) {
|
|
375
|
+
if (o.kind === "t3-down" || o.kind === "locked" || o.kind === "dry-run") return null;
|
|
376
|
+
if (o.kind === "error") {
|
|
377
|
+
const e = o.error ;
|
|
378
|
+
if (e?.name === "T3Incompatible") {
|
|
379
|
+
return { cycle: "failed", attention: incompatibleAttention(typeof e.version === "string" ? e.version : "?") };
|
|
380
|
+
}
|
|
381
|
+
return { cycle: "failed" };
|
|
382
|
+
}
|
|
383
|
+
const cycleKind = o.kind === "partial" ? "failed" : o.kind === "aborted" ? "unchanged" : "ok";
|
|
384
|
+
if (o.kind === "aborted") return { cycle: cycleKind };
|
|
385
|
+
return {
|
|
386
|
+
cycle: cycleKind, attention: attentionOf(o, info), undecided: info.undecided, heldRehome: info.heldRehome,
|
|
387
|
+
held: "held" in o ? (o.held ?? []).map((h) => ({ project: h.project, title: h.title, modifiedAt: h.modifiedAt })) : [],
|
|
388
|
+
...(info.followConflicts !== undefined ? { followConflicts: info.followConflicts } : {}),
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
async function cycle(deps , recovered , info ) {
|
|
393
|
+
const now = deps.now ?? Date.now;
|
|
394
|
+
const platform = deps.platform ?? process.platform;
|
|
395
|
+
const env = deps.env ?? process.env;
|
|
396
|
+
const notice = deps.onNotice ?? (() => {});
|
|
397
|
+
const connect = deps.connect ?? connectT3;
|
|
398
|
+
const counts = zeroCounts();
|
|
399
|
+
let api = null;
|
|
400
|
+
let runStarted = false;
|
|
401
|
+
const runId = newRunId(new Date(now()));
|
|
402
|
+
try {
|
|
403
|
+
// Recovery first, under the lock and BEFORE probing T3: after a crash mid-wave, file times parked or bumped by it
|
|
404
|
+
// are put back on the next scheduled cycle even if T3 Code stays closed. (Writes only when something is pending.)
|
|
405
|
+
if (!deps.dryRun) {
|
|
406
|
+
const lock = await acquireLock(deps.stateDir);
|
|
407
|
+
if (!lock) return { kind: "locked" };
|
|
408
|
+
try {
|
|
409
|
+
const rec = await recoverPendingMtimes(new Journal(join(deps.stateDir, "journal.jsonl")));
|
|
410
|
+
recovered.restored += rec.restored.length;
|
|
411
|
+
recovered.failed += rec.failed.length;
|
|
412
|
+
} finally { await lock.release(); }
|
|
413
|
+
}
|
|
414
|
+
// Then the cheap probe: with T3 closed, no connection, nothing else written.
|
|
415
|
+
if (!(await (deps.isT3Running ?? isT3Running)(deps.home))) return { kind: "t3-down" };
|
|
416
|
+
const config = await loadConfig(deps.stateDir, deps.home);
|
|
417
|
+
// The notices' language (stderr only: logs/sync.log stays English).
|
|
418
|
+
const noticeLang = deps.lang ?? resolveLang(null, { ...env, T3SM_HOME: deps.stateDir }, config?.language ?? null);
|
|
419
|
+
let names = {};
|
|
420
|
+
let fullItems = [];
|
|
421
|
+
let planConfig = null;
|
|
422
|
+
let overLimit = null;
|
|
423
|
+
|
|
424
|
+
// Runs under the lock (applyPlan's loader) except for --dry-run. Read-only against T3; the connection stays open
|
|
425
|
+
// for applyPlan (one T3 session per cycle) and is closed in this function's finally.
|
|
426
|
+
const build = async () => {
|
|
427
|
+
api = await connect(deps.home, { allowUntested: deps.allowUntested ?? false, manageProviders: false, onNotice: notice });
|
|
428
|
+
const fp = await freshPlan(api, {
|
|
429
|
+
home: deps.home, env: { ...env, T3SM_HOME: deps.stateDir }, now, runId, adapters: deps.adapters, existsDir: deps.existsDir,
|
|
430
|
+
missingConfig: "memory", readOnly: deps.dryRun ?? false, lang: deps.lang,
|
|
431
|
+
});
|
|
432
|
+
// sync never creates instances, but a drifted claudeAgent must still stop it (exit 2) before any write.
|
|
433
|
+
if (fp.plan.instancesToCreate.length > 0) await planInstances(api, fp.plan.instancesToCreate, defaultClaudeHome(deps.home));
|
|
434
|
+
const journal = await new Journal(join(deps.stateDir, "journal.jsonl")).readAll();
|
|
435
|
+
const f = filterForSync(fp.plan, {
|
|
436
|
+
liveMirrorPaths: await liveMirrorPaths(fp.plan, now()), projects: fp.projects, platform, failedBefore: failedImports(journal),
|
|
437
|
+
generalFolder: resolveGeneralFolder(fp.config, deps.lang ?? resolveLang(null, { ...env, T3SM_HOME: deps.stateDir }, fp.config.language), fp.projects.filter((p) => !p.deleted).map((p) => p.workspaceRoot), { platform, home: deps.home }),
|
|
438
|
+
});
|
|
439
|
+
counts.heldForDedupe = f.heldForDedupe;
|
|
440
|
+
counts.heldForAccounts = f.heldForAccounts;
|
|
441
|
+
counts.heldFailed = f.heldFailed;
|
|
442
|
+
counts.heldRehome = f.heldRehome;
|
|
443
|
+
if (Object.keys(f.undecided).length > 0) counts.undecided = f.undecided;
|
|
444
|
+
info.undecided = f.undecided;
|
|
445
|
+
info.heldRehome = f.heldRehome;
|
|
446
|
+
// An account signed out in T3 Code cannot import: its sessions wait, and a person has to sign it in.
|
|
447
|
+
const signedOut = new Set((await api.providerInstances()).filter((i) => i.authStatus === "unauthenticated").map((i) => i.instanceId));
|
|
448
|
+
info.signedOut = new Set(f.plan.items.flatMap((i) => (i.action.kind !== "skip" && signedOut.has(i.action.instanceId) ? [i.action.instanceId] : []))).size;
|
|
449
|
+
const importRoots = new Set(f.plan.items.flatMap((i) => (i.action.kind !== "skip" ? [rootKey(i.action.projectRoot, platform)] : [])));
|
|
450
|
+
info.planned = { projects: importRoots.size, newProjects: f.plan.projectsToCreate.filter((x) => importRoots.has(rootKey(x.root, platform))).map((x) => x.title) };
|
|
451
|
+
names = fp.names;
|
|
452
|
+
fullItems = fp.plan.items;
|
|
453
|
+
planConfig = fp.config;
|
|
454
|
+
const pending = f.plan.items.filter((i) => i.action.kind !== "skip").length;
|
|
455
|
+
const limit = fp.config.sync.maxUnattendedImports ?? DEFAULT_MAX_UNATTENDED_IMPORTS;
|
|
456
|
+
if (pending > limit) {
|
|
457
|
+
if (!deps.dryRun) throw new OverLimit(pending, limit);
|
|
458
|
+
overLimit = limit;
|
|
459
|
+
}
|
|
460
|
+
return f.plan;
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
// On a cycle with nothing to import, the history pass and the follow step journal their own run, with the cycle's run
|
|
464
|
+
// id, only once one of them is about to write (one run-start for both; run-end once both are done, in afterApply).
|
|
465
|
+
let ownRun = null;
|
|
466
|
+
let ownStatus = "ok";
|
|
467
|
+
const RANK = { ok: 0, partial: 1, aborted: 2 } ;
|
|
468
|
+
const ownEnds = (s ) => { if (RANK[s] > RANK[ownStatus]) ownStatus = s; };
|
|
469
|
+
const runOf = async (ctx ) => {
|
|
470
|
+
if (ctx.runId !== null) return ctx.runId;
|
|
471
|
+
if (ownRun === null) {
|
|
472
|
+
ownRun = runId;
|
|
473
|
+
runStarted = true;
|
|
474
|
+
await ctx.journal.append({ t: "run-start", runId, at: new Date(now()).toISOString(), command: `sync ${runId}` });
|
|
475
|
+
}
|
|
476
|
+
return ownRun;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// Organize (spec 4.7, §5.2): under the lock, T3 connected, inside the sync run (command null: applyPlan or runOf owns
|
|
480
|
+
// run-start / run-end), so one `t3sm rollback <run>` undoes the import and the organize changes together. Archive and
|
|
481
|
+
// title only for the threads of this run's `imported` entries; the history rule for every imported thread of their
|
|
482
|
+
// projects, or of all projects when the daily pass is due. No pins, no deletes, no auto-settle: invariant 13.
|
|
483
|
+
const organizeCycle = async (ctx ) => {
|
|
484
|
+
const imported = ctx.runId !== null && (ctx.result.status === "ok" || ctx.result.status === "partial");
|
|
485
|
+
if (!imported && ctx.result.status !== "noop") return;
|
|
486
|
+
const ids = imported
|
|
487
|
+
? new Set((await ctx.journal.readAll()).flatMap((e) => (e.t === "imported" && e.runId === ctx.runId ? [e.threadId] : [])))
|
|
488
|
+
: new Set ();
|
|
489
|
+
const last = await readLastHistoryAt(deps.stateDir).catch(() => null);
|
|
490
|
+
// A date in the future (the clock went back) counts as due.
|
|
491
|
+
const daily = last === null || last > now() || now() - last >= HISTORY_INTERVAL_MS;
|
|
492
|
+
if (ids.size === 0 && !daily) return;
|
|
493
|
+
let stop = false;
|
|
494
|
+
const onSignal = () => { stop = true; };
|
|
495
|
+
const sig = deps.signals ?? { on: (s , f ) => void process.on(s , f), off: (s , f ) => void process.off(s , f) };
|
|
496
|
+
for (const s of STOP_SIGNALS) sig.on(s, onSignal);
|
|
497
|
+
let status = "ok";
|
|
498
|
+
try {
|
|
499
|
+
const silent = { stdout: () => {}, stderr: () => {}, isTTY: false, prompt: async () => "", env: { ...env, T3SM_HOME: deps.stateDir } };
|
|
500
|
+
// dryRun here only means "read-only discovery": the cache was just written by the planning above.
|
|
501
|
+
const b = await buildOrganize(ctx.api, ctx.journal, planConfig ?? config ?? defaultConfig([]),
|
|
502
|
+
{ only: null, onlyThreads: ids, historyScope: daily ? "all" : "projects", dryRun: true }, silent, { adapters: deps.adapters, platform }, deps.home, now);
|
|
503
|
+
const { plan, deferred } = capHistory(b.plan, deps.historyPerCycle ?? HISTORY_PER_CYCLE);
|
|
504
|
+
if (deferred > 0) counts.historyDeferred = deferred;
|
|
505
|
+
if (plan.actions.length > 0) {
|
|
506
|
+
const r = await applyOrganize(plan, {
|
|
507
|
+
api: ctx.api, journal: ctx.journal, runId: await runOf(ctx), command: null, now, allowUntested: deps.allowUntested ?? false, onNotice: notice,
|
|
508
|
+
aborted: () => stop || ctx.aborted?.() === true,
|
|
509
|
+
});
|
|
510
|
+
counts.organized = r.done;
|
|
511
|
+
if (r.failed.length > 0) counts.organizeFailed = r.failed.length;
|
|
512
|
+
status = r.status === "aborted" ? "aborted" : r.failed.length > 0 ? "partial" : "ok";
|
|
513
|
+
}
|
|
514
|
+
// Done for today only when nothing is left; changes left over make the next cycle a daily pass.
|
|
515
|
+
if (status === "aborted") return;
|
|
516
|
+
if (deferred > 0) await markHistoryPass(deps.stateDir, null).catch(() => {});
|
|
517
|
+
else if (daily) await markHistoryPass(deps.stateDir, new Date(now())).catch(() => {});
|
|
518
|
+
} catch (e) {
|
|
519
|
+
// T3 changed but the journal did not hear of it: nothing could roll that back, so the cycle ends as an error.
|
|
520
|
+
if (e instanceof JournalWriteError) { status = "aborted"; throw e; }
|
|
521
|
+
counts.organizeFailed = (counts.organizeFailed ?? 0) + 1;
|
|
522
|
+
status = "partial";
|
|
523
|
+
const why = describeError(e).message.split("\n")[0] ?? "";
|
|
524
|
+
notice(cliT(noticeLang, "sync.notice.organizeStopped", { error: why }));
|
|
525
|
+
// Every failure carries its reason in the journal (spec §5.1.5): here the whole step, not one change.
|
|
526
|
+
await appendOrStop(ctx.journal, {
|
|
527
|
+
t: "organize-failed", runId: await runOf(ctx), kind: "organize", target: daily ? "history" : "*", projectId: null, error: failureText(why),
|
|
528
|
+
}, "nothing (organizing failed)");
|
|
529
|
+
} finally {
|
|
530
|
+
for (const s of STOP_SIGNALS) sig.off(s, onSignal);
|
|
531
|
+
if (ctx.runId === null && ownRun !== null) ownEnds(status);
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
// Rehome copies follow their original (spec §5.1.4, src/ops/follow.ts): under the lock, T3 connected, after the
|
|
536
|
+
// waves and the organize step (which must never retitle a revision thread), inside the cycle's own run, so one
|
|
537
|
+
// `t3sm rollback <run>` undoes it with the rest. On a cycle with nothing to import the run is journaled by runOf,
|
|
538
|
+
// with the cycle's run id, only once a follow is about to write. Runs whatever sync.autoMerge says; a follow error
|
|
539
|
+
// never changes the import's result.
|
|
540
|
+
const follow = async (ctx ) => {
|
|
541
|
+
if (ctx.result.status !== "ok" && ctx.result.status !== "partial" && ctx.result.status !== "noop") return;
|
|
542
|
+
if (ctx.aborted?.()) return; // asked to stop (during organize, say): nothing more is started
|
|
543
|
+
const cfg = planConfig ?? config;
|
|
544
|
+
let stop = false;
|
|
545
|
+
const onSignal = () => { stop = true; };
|
|
546
|
+
const sig = deps.signals ?? { on: (s , f ) => void process.on(s , f), off: (s , f ) => void process.off(s , f) };
|
|
547
|
+
for (const s of STOP_SIGNALS) sig.on(s, onSignal);
|
|
548
|
+
let status = "ok";
|
|
549
|
+
try {
|
|
550
|
+
const r = await followRehomed({
|
|
551
|
+
api: ctx.api, journal: ctx.journal, now, platform, limit: FOLLOWS_PER_CYCLE, exposureItems: fullItems, batch: cfg?.batch, stateDir: deps.stateDir,
|
|
552
|
+
accountHomes: Object.fromEntries(cfg?.accounts.map((a) => [a.id, expandHome(a.home, deps.home)]) ?? []),
|
|
553
|
+
aborted: () => stop || ctx.aborted?.() === true, onPendingFiles: ctx.onPendingFiles,
|
|
554
|
+
runId: ctx.runId ?? (() => runOf(ctx)),
|
|
555
|
+
});
|
|
556
|
+
if (r.followed) counts.followed = r.followed;
|
|
557
|
+
const id = (p ) => basename(p, ".jsonl");
|
|
558
|
+
const conflicts = r.conflicts.map((x) => ({ originalId: id(x.original), copyId: id(x.copy), originalTitle: x.originalTitle, copyTitle: x.copyTitle }));
|
|
559
|
+
info.followConflicts = conflicts;
|
|
560
|
+
if (conflicts.length) { counts.followConflicts = conflicts.length; counts.followConflictList = conflicts; }
|
|
561
|
+
if (r.deferred) counts.followDeferred = r.deferred;
|
|
562
|
+
if (r.failed.length) counts.followFailed = r.failed.length;
|
|
563
|
+
status = stop || ctx.aborted?.() ? "aborted" : r.failed.length > 0 ? "partial" : "ok";
|
|
564
|
+
} catch (e) {
|
|
565
|
+
// T3 changed but the journal did not hear of it: nothing could roll that back, so the cycle ends as an error.
|
|
566
|
+
if (e instanceof JournalWriteError) { status = "aborted"; throw e; }
|
|
567
|
+
counts.followFailed = (counts.followFailed ?? 0) + 1;
|
|
568
|
+
status = "partial";
|
|
569
|
+
notice(cliT(noticeLang, "sync.notice.followStopped", { error: describeError(e).message.split("\n")[0] ?? "" }));
|
|
570
|
+
} finally {
|
|
571
|
+
for (const s of STOP_SIGNALS) sig.off(s, onSignal);
|
|
572
|
+
if (ctx.runId === null && ownRun !== null) ownEnds(status);
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
// Merges (Task 23): under the lock, T3 connected, after the waves; always in their own `sync-merge` run (journaled
|
|
577
|
+
// only once a merge is about to write), so a merge never changes the cycle's own run or result.
|
|
578
|
+
let mergeRun = null;
|
|
579
|
+
const autoMerge = async (ctx ) => {
|
|
580
|
+
if (ctx.result.status !== "ok" && ctx.result.status !== "noop") return;
|
|
581
|
+
if (ctx.aborted?.()) return; // asked to stop: no merge is started
|
|
582
|
+
const cfg = planConfig ?? config;
|
|
583
|
+
const items = ctx.plan.items.filter((i) => i.notes.includes("continued-outside-t3"));
|
|
584
|
+
const mdeps = {
|
|
585
|
+
api: ctx.api, journal: ctx.journal, now, platform, stateDir: deps.stateDir, retryFailed: false, limit: MERGES_PER_CYCLE,
|
|
586
|
+
exposureItems: fullItems, batch: cfg?.batch, onPendingFiles: ctx.onPendingFiles,
|
|
587
|
+
accountHomes: Object.fromEntries(cfg?.accounts.map((a) => [a.id, expandHome(a.home, deps.home)]) ?? []),
|
|
588
|
+
runId: async () => {
|
|
589
|
+
mergeRun = newRunId(new Date(now()));
|
|
590
|
+
await ctx.journal.append({ t: "run-start", runId: mergeRun, at: new Date(now()).toISOString(), command: `sync-merge ${mergeRun}` });
|
|
591
|
+
return mergeRun;
|
|
592
|
+
},
|
|
593
|
+
};
|
|
594
|
+
// Counts reported once per change: sessions waiting for `t3sm merge`, and sessions whose terminal work sits on a
|
|
595
|
+
// side branch (never merged; `t3sm merge` lists them with the `claude --resume <id>` hint).
|
|
596
|
+
const before = await readMergePending(deps.stateDir).catch(() => null);
|
|
597
|
+
const report = (n , orphaned ) => {
|
|
598
|
+
if (n !== (before?.count ?? 0)) counts.mergePending = n;
|
|
599
|
+
if (orphaned !== (before?.orphaned ?? 0)) counts.mergeOrphaned = orphaned;
|
|
600
|
+
};
|
|
601
|
+
if (items.length === 0) {
|
|
602
|
+
await setMergePending(deps.stateDir, 0, new Date(now()), 0).catch(() => null);
|
|
603
|
+
if (cfg?.sync.autoMerge !== true) report(0, 0);
|
|
604
|
+
else if ((before?.orphaned ?? 0) !== 0) counts.mergeOrphaned = 0;
|
|
605
|
+
return;
|
|
606
|
+
}
|
|
607
|
+
if (cfg?.sync.autoMerge !== true) {
|
|
608
|
+
try {
|
|
609
|
+
const f = await findMergeCandidates(items, mdeps);
|
|
610
|
+
report(f.candidates.length, f.orphaned.length);
|
|
611
|
+
} catch { /* reporting only */ }
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
let stop = false;
|
|
615
|
+
const onSignal = () => { stop = true; };
|
|
616
|
+
const sig = deps.signals ?? { on: (s , f ) => void process.on(s , f), off: (s , f ) => void process.off(s , f) };
|
|
617
|
+
for (const s of STOP_SIGNALS) sig.on(s, onSignal);
|
|
618
|
+
let status = "aborted";
|
|
619
|
+
try {
|
|
620
|
+
const found = await findMergeCandidates(items, mdeps);
|
|
621
|
+
if (found.orphaned.length !== (before?.orphaned ?? 0)) counts.mergeOrphaned = found.orphaned.length;
|
|
622
|
+
const r = await executeMerges(found.candidates, { ...mdeps, aborted: () => stop || ctx.aborted?.() === true });
|
|
623
|
+
const skipped = [...found.skipped, ...r.skipped];
|
|
624
|
+
counts.merged = r.merged.length;
|
|
625
|
+
counts.mergeDeferred = skipped.filter((x) => DEFERRED_REASONS.has(x.reason)).length;
|
|
626
|
+
counts.mergeFailed = skipped.filter((x) => FAILED_REASONS.has(x.reason)).length;
|
|
627
|
+
if (r.merged.length > 0) await setMergePending(deps.stateDir, Math.max(0, found.candidates.length - r.merged.length), new Date(now()));
|
|
628
|
+
status = stop || ctx.aborted?.() ? "aborted" : (counts.mergeFailed ?? 0) > 0 ? "partial" : "ok";
|
|
629
|
+
} catch (e) {
|
|
630
|
+
// T3 changed but the journal did not hear of it: nothing could roll that back, so the cycle ends as an error.
|
|
631
|
+
if (e instanceof JournalWriteError) { status = "aborted"; throw e; }
|
|
632
|
+
counts.mergeFailed = (counts.mergeFailed ?? 0) + 1;
|
|
633
|
+
status = "partial";
|
|
634
|
+
notice(cliT(noticeLang, "sync.notice.mergeStopped", { error: describeError(e).message.split("\n")[0] ?? "" }));
|
|
635
|
+
} finally {
|
|
636
|
+
for (const s of STOP_SIGNALS) sig.off(s, onSignal);
|
|
637
|
+
if (mergeRun !== null) await ctx.journal.append({ t: "run-end", runId: mergeRun, at: new Date(now()).toISOString(), status }).catch(() => {});
|
|
638
|
+
}
|
|
639
|
+
};
|
|
640
|
+
|
|
641
|
+
if (deps.dryRun) {
|
|
642
|
+
const p = await build();
|
|
643
|
+
const byHarness = {};
|
|
644
|
+
const roots = new Set ();
|
|
645
|
+
for (const i of p.items) {
|
|
646
|
+
if (i.action.kind !== "skip") { // convert-import items are converted, then imported, in the same cycle
|
|
647
|
+
counts.wouldImport++;
|
|
648
|
+
byHarness[i.session.harness] = (byHarness[i.session.harness] ?? 0) + 1;
|
|
649
|
+
roots.add(rootKey(i.action.projectRoot, platform));
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return {
|
|
653
|
+
kind: "dry-run", runId: null, counts, byHarness, projects: roots.size, overLimit,
|
|
654
|
+
newProjects: p.projectsToCreate.filter((x) => roots.has(rootKey(x.root, platform))).length,
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const r = await applyPlan(build, {
|
|
659
|
+
stateDir: deps.stateDir, home: deps.home, now, platform, signals: deps.signals,
|
|
660
|
+
// The plan's connection, lent to applyPlan without its close: this function's finally closes it exactly once.
|
|
661
|
+
connect: async () => new Proxy(api , { get: (t, k) => (k === "close" ? async () => {} : Reflect.get(t, k)) }),
|
|
662
|
+
allowUntested: deps.allowUntested ?? false, batch: config?.batch,
|
|
663
|
+
runId, command: `sync ${runId}`, noopWithoutWrites: true,
|
|
664
|
+
pinDefaultInstance: false, // unattended: never changes T3's provider settings (a drifted default instance stops it in freshPlan)
|
|
665
|
+
exposure: () => fullItems, // held items are still on disk and still exposed to T3's importer
|
|
666
|
+
accountHomes: () => Object.fromEntries((planConfig ?? config)?.accounts.map((a) => [a.id, expandHome(a.home, deps.home)]) ?? []),
|
|
667
|
+
// Codex / Pi transcripts created after planning (cached discovery; Claude's are listed from the instance homes).
|
|
668
|
+
rediscover: async () => (await discoverAll((deps.adapters ?? allAdapters()).filter((a) => a.id !== "claude"), {
|
|
669
|
+
home: deps.home, platform, now, config: planConfig ?? config ?? defaultConfig([]), stateDir: deps.stateDir, env,
|
|
670
|
+
})).sessions,
|
|
671
|
+
beforeWrites: async (plan) => {
|
|
672
|
+
runStarted = true;
|
|
673
|
+
await atomicWrite(join(deps.stateDir, "runs", runId, "plan.md"), renderPlanMarkdown(plan, { names }));
|
|
674
|
+
},
|
|
675
|
+
afterApply: async (ctx) => {
|
|
676
|
+
try {
|
|
677
|
+
await organizeCycle(ctx);
|
|
678
|
+
await follow(ctx);
|
|
679
|
+
} finally {
|
|
680
|
+
if (ownRun !== null) await ctx.journal.append({ t: "run-end", runId: ownRun, at: new Date(now()).toISOString(), status: ownStatus }).catch(() => {});
|
|
681
|
+
}
|
|
682
|
+
await autoMerge(ctx);
|
|
683
|
+
await deps.afterApply?.(ctx);
|
|
684
|
+
},
|
|
685
|
+
onNotice: notice, lang: noticeLang, onProgress: deps.onProgress, onImportStart: deps.onImportStart, onWave: deps.onWave, failureTitles: deps.failureTitles,
|
|
686
|
+
});
|
|
687
|
+
if (r.status === "locked") return { kind: "locked" };
|
|
688
|
+
recovered.restored += r.recovered?.restored.length ?? 0;
|
|
689
|
+
recovered.failed += r.recovered?.failed.length ?? 0;
|
|
690
|
+
Object.assign(counts, {
|
|
691
|
+
imported: r.imported, failed: r.failed, deferred: r.deferred, unverified: r.unverified, alreadyInT3: r.alreadyInT3,
|
|
692
|
+
heldInUse: r.deferredInUse, parked: r.parked,
|
|
693
|
+
});
|
|
694
|
+
const extra = { ...(r.held.length > 0 ? { held: r.held } : {}), ...(r.failures.length > 0 ? { failures: r.failures } : {}) };
|
|
695
|
+
if ((r.status === "noop" || r.status === "declined") && mergeRun === null && ownRun === null) return { kind: "noop", runId: null, counts, ...extra };
|
|
696
|
+
if (r.status === "noop") return { kind: "ok", runId: ownRun ?? mergeRun, counts, ...extra };
|
|
697
|
+
const kind = r.status === "ok" && r.restoreErrors.length === 0 ? "ok" : r.status === "aborted" ? "aborted" : "partial";
|
|
698
|
+
return { kind, runId: r.runId, counts, ...extra };
|
|
699
|
+
} catch (e) {
|
|
700
|
+
if (e instanceof OverLimit) return { kind: "over-limit", pending: e.pending, limit: e.limit };
|
|
701
|
+
const { code } = describeError(e);
|
|
702
|
+
// Lost T3 mid-run: keep the run id so the log points at the (partial) run in the journal.
|
|
703
|
+
if (code === 3 || unreachable(e)) return runStarted ? { kind: "t3-down", runId } : { kind: "t3-down" };
|
|
704
|
+
return { kind: "error", exitCode: code, error: e };
|
|
705
|
+
} finally {
|
|
706
|
+
await (api )?.close();
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// ---------- reporting ----------
|
|
711
|
+
|
|
712
|
+
export function overLimitLine(pending , lang = "en") {
|
|
713
|
+
return cliT(lang, "sync.overLimit", { n: pending });
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** How the summary is told: the language (English by default) and the dash of a held line (ASCII consoles: "-"). */
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
/** Human summary (non-quiet), in `o.lang` (English by default). `now` dates the held conversations ("written 3 min ago"). */
|
|
720
|
+
export function summaryLines(o , now = Date.now(), opts = {}) {
|
|
721
|
+
const lang = opts.lang ?? "en";
|
|
722
|
+
const t = (k , v = {}) => cliT(lang, k, v);
|
|
723
|
+
if (o.kind === "t3-down" || o.kind === "locked" || o.kind === "error") return [];
|
|
724
|
+
if (o.kind === "over-limit") return [overLimitLine(o.pending, lang)];
|
|
725
|
+
const c = o.counts;
|
|
726
|
+
const out = [];
|
|
727
|
+
if (o.kind === "noop") out.push(t("sync.nothing"));
|
|
728
|
+
else if (o.kind === "dry-run") {
|
|
729
|
+
const h = Object.entries(o.byHarness).sort().map(([k, n]) => `${k} ${n}`).join(", ");
|
|
730
|
+
out.push(t("sync.dryRun", { n: c.wouldImport, harness: h ? ` (${h})` : "", p: o.projects, newP: o.newProjects }));
|
|
731
|
+
if (o.overLimit !== null) out.push(t("sync.dryRunOverLimit", { limit: o.overLimit }));
|
|
732
|
+
} else {
|
|
733
|
+
const parts = [t("sync.imported", { n: c.imported }), t("sync.failed", { n: c.failed })];
|
|
734
|
+
if (c.unverified) parts.push(t("sync.unverified", { n: c.unverified }));
|
|
735
|
+
const deferred = c.deferred - c.heldInUse;
|
|
736
|
+
if (deferred) parts.push(t("sync.deferred", { n: deferred }));
|
|
737
|
+
out.push(`${parts.join(", ")}.${o.runId ? t("sync.runId", { runId: o.runId }) : ""}`);
|
|
738
|
+
}
|
|
739
|
+
out.push(...detailLines(o, now, opts));
|
|
740
|
+
return out;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/** Everything summaryLines says after its first line (the result): what followed, merged, waits, organized. */
|
|
744
|
+
export function detailLines(o , now = Date.now(), opts = {}) {
|
|
745
|
+
const lang = opts.lang ?? "en";
|
|
746
|
+
const t = (k , v = {}) => cliT(lang, k, v);
|
|
747
|
+
if (o.kind === "t3-down" || o.kind === "locked" || o.kind === "error" || o.kind === "over-limit") return [];
|
|
748
|
+
const c = o.counts;
|
|
749
|
+
const out = [];
|
|
750
|
+
if (c.followed) out.push(t("sync.followed", { n: c.followed }));
|
|
751
|
+
if (c.followConflicts) {
|
|
752
|
+
out.push(t("sync.followConflicts", { n: c.followConflicts }));
|
|
753
|
+
const list = c.followConflictList ?? [];
|
|
754
|
+
for (const x of list.slice(0, 5)) out.push(t("sync.followConflict", { original: x.originalTitle || x.originalId, originalId: x.originalId, copy: x.copyTitle || x.copyId, copyId: x.copyId }));
|
|
755
|
+
if (list.length > 5) out.push(t("sync.andMore", { n: list.length - 5 }));
|
|
756
|
+
}
|
|
757
|
+
if (c.followDeferred) out.push(t("sync.followDeferred", { n: c.followDeferred, max: FOLLOWS_PER_CYCLE }));
|
|
758
|
+
if (c.followFailed) out.push(t("sync.followFailed", { n: c.followFailed }));
|
|
759
|
+
if (c.merged) out.push(t("sync.merged", { n: c.merged }));
|
|
760
|
+
if (c.mergeDeferred) out.push(t("sync.mergeDeferred", { n: c.mergeDeferred, max: MERGES_PER_CYCLE }));
|
|
761
|
+
if (c.mergeFailed) out.push(t("sync.mergeFailed", { n: c.mergeFailed }));
|
|
762
|
+
if (c.mergeOrphaned) out.push(t("sync.mergeOrphaned", { n: c.mergeOrphaned }));
|
|
763
|
+
if (c.mergePending) out.push(t("sync.mergePending", { n: c.mergePending }));
|
|
764
|
+
if (c.heldInUse) out.push(t("sync.heldInUse", { n: c.heldInUse }));
|
|
765
|
+
if ("held" in o) for (const h of o.held ?? []) out.push(heldLine(h, h.project, now, opts));
|
|
766
|
+
if (c.heldForDedupe) out.push(t("sync.heldDedupe", { n: c.heldForDedupe }));
|
|
767
|
+
if (c.heldForAccounts) out.push(t("sync.heldAccounts", { n: c.heldForAccounts }));
|
|
768
|
+
if (c.heldFailed) out.push(t("sync.heldFailed", { n: c.heldFailed }));
|
|
769
|
+
if (c.heldRehome) out.push(t("sync.heldRehome", { n: c.heldRehome }));
|
|
770
|
+
const undecided = Object.values(c.undecided ?? {}).reduce((a, n) => a + n, 0);
|
|
771
|
+
if (undecided) out.push(t("sync.undecided", { n: undecided }));
|
|
772
|
+
if (c.organized) out.push(t("sync.organized", { n: c.organized }));
|
|
773
|
+
if (c.historyDeferred) out.push(t("sync.historyDeferred", { n: c.historyDeferred, max: HISTORY_PER_CYCLE }));
|
|
774
|
+
if (c.organizeFailed) out.push(t("sync.organizeFailed", { n: c.organizeFailed }));
|
|
775
|
+
return out;
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/** One log line: timestamp, outcome, counts and run id. Never paths, titles or error messages (personal content). */
|
|
779
|
+
export function logLine(o , at ) {
|
|
780
|
+
const r = o.recovered;
|
|
781
|
+
const line = baseLogLine(o, at);
|
|
782
|
+
return r && (r.restored > 0 || r.failed > 0) ? `${line} recovered=${r.restored} failed=${r.failed}` : line;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
function baseLogLine(o , at ) {
|
|
786
|
+
const ts = new Date(at).toISOString();
|
|
787
|
+
if (o.kind === "t3-down") return `${ts} t3-down${o.runId ? ` run=${o.runId}` : ""}`;
|
|
788
|
+
if (o.kind === "locked") return `${ts} locked`;
|
|
789
|
+
if (o.kind === "over-limit") return `${ts} over-limit pending=${o.pending} limit=${o.limit}`;
|
|
790
|
+
if (o.kind === "error") return `${ts} error exit=${o.exitCode} ${(o.error )?.name ?? "Error"}`;
|
|
791
|
+
const c = o.counts;
|
|
792
|
+
// Held projects: a count only (their names and conversation titles stay out of the log).
|
|
793
|
+
const heldProjects = "held" in o ? o.held?.length ?? 0 : 0;
|
|
794
|
+
const held = `held-in-use=${c.heldInUse} held-dedupe=${c.heldForDedupe} held-accounts=${c.heldForAccounts} held-failed=${c.heldFailed}${c.heldRehome ? ` held-rehome=${c.heldRehome}` : ""}${heldProjects ? ` held-projects=${heldProjects}` : ""}`;
|
|
795
|
+
const anyHeld = c.heldInUse || c.heldForDedupe || c.heldForAccounts || c.heldFailed || c.heldRehome;
|
|
796
|
+
const undecided = Object.values(c.undecided ?? {}).reduce((a, n) => a + n, 0);
|
|
797
|
+
const merges = (c.merged || c.mergeDeferred || c.mergeFailed ? ` merged=${c.merged ?? 0} merge-deferred=${c.mergeDeferred ?? 0}${c.mergeFailed ? ` merge-failed=${c.mergeFailed}` : ""}` : "")
|
|
798
|
+
+ (c.mergePending !== undefined ? ` merge-pending=${c.mergePending}` : "")
|
|
799
|
+
+ (c.mergeOrphaned !== undefined ? ` merge-orphaned=${c.mergeOrphaned}` : "")
|
|
800
|
+
+ (undecided ? ` undecided=${undecided}` : "")
|
|
801
|
+
+ (c.organized ? ` organized=${c.organized}` : "")
|
|
802
|
+
+ (c.organizeFailed ? ` organize-failed=${c.organizeFailed}` : "")
|
|
803
|
+
+ (c.historyDeferred ? ` history-deferred=${c.historyDeferred}` : "")
|
|
804
|
+
+ (c.followed ? ` followed=${c.followed}` : "")
|
|
805
|
+
+ (c.followConflicts ? ` follow-conflicts=${c.followConflicts}` : "")
|
|
806
|
+
+ (c.followDeferred ? ` follow-deferred=${c.followDeferred}` : "")
|
|
807
|
+
+ (c.followFailed ? ` follow-failed=${c.followFailed}` : "");
|
|
808
|
+
if (o.kind === "noop") return `${ts} noop${anyHeld ? ` ${held}` : ""}${merges}`;
|
|
809
|
+
if (o.kind === "dry-run") return `${ts} dry-run would-import=${c.wouldImport} ${held}`;
|
|
810
|
+
return `${ts} ${o.kind} run=${o.runId ?? "-"} imported=${c.imported} failed=${c.failed} deferred=${c.deferred} parked=${c.parked ?? 0} ${held}${merges}`;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/** Compact result for the heartbeat file (sync-status.json `lastResult`): counts, the run id and held names; never a path. */
|
|
814
|
+
export function resultSummary(o ) {
|
|
815
|
+
if (o.kind === "t3-down" || o.kind === "locked") return { outcome: o.kind };
|
|
816
|
+
if (o.kind === "over-limit") return { outcome: o.kind, pending: o.pending, limit: o.limit };
|
|
817
|
+
if (o.kind === "error") return { outcome: "error", exitCode: o.exitCode };
|
|
818
|
+
const c = o.counts;
|
|
819
|
+
return {
|
|
820
|
+
outcome: o.kind, runId: o.runId, imported: c.imported, failed: c.failed, deferred: c.deferred,
|
|
821
|
+
heldInUse: c.heldInUse, heldForDedupe: c.heldForDedupe, heldForAccounts: c.heldForAccounts, heldFailed: c.heldFailed,
|
|
822
|
+
...(c.heldRehome ? { heldRehome: c.heldRehome } : {}),
|
|
823
|
+
...(c.undecided ? { undecided: c.undecided } : {}),
|
|
824
|
+
...(c.organized || c.organizeFailed ? { organized: c.organized ?? 0, organizeFailed: c.organizeFailed ?? 0 } : {}),
|
|
825
|
+
...(c.historyDeferred ? { historyDeferred: c.historyDeferred } : {}),
|
|
826
|
+
...(c.merged || c.mergeDeferred || c.mergeFailed ? { merged: c.merged ?? 0, mergeDeferred: c.mergeDeferred ?? 0, mergeFailed: c.mergeFailed ?? 0 } : {}),
|
|
827
|
+
...(c.followed || c.followConflicts || c.followDeferred || c.followFailed
|
|
828
|
+
? { followed: c.followed ?? 0, followConflicts: c.followConflicts ?? 0, followDeferred: c.followDeferred ?? 0, followFailed: c.followFailed ?? 0 } : {}),
|
|
829
|
+
// Titles and session ids only: never the transcripts' paths (SECURITY.md).
|
|
830
|
+
...(c.followConflictList?.length ? { followConflictList: c.followConflictList } : {}),
|
|
831
|
+
// Names and time only: the held project's folder and transcript path never reach the heartbeat (SECURITY.md).
|
|
832
|
+
...("held" in o && o.held?.length ? { held: o.held.map((h) => ({ project: h.project, title: h.title, modifiedAt: h.modifiedAt })) } : {}),
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
export const SYNC_LOG_MAX_BYTES = 1_048_576;
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Appends one line to <stateDir>/logs/sync.log; rotates to sync.log.1..keep when the next line would pass maxBytes.
|
|
840
|
+
* Race: two t3sm processes (a scheduled cycle and a watcher) may rotate at the same moment. Every rename tolerates a
|
|
841
|
+
* missing source, so the worst case is one rotation too many (a short sync.log.1) or a line landing in the file just
|
|
842
|
+
* renamed to sync.log.1; no line is lost and nothing throws. The lines are diagnostics, not state.
|
|
843
|
+
*/
|
|
844
|
+
export async function appendSyncLog(stateDir , line , o = {}) {
|
|
845
|
+
const max = o.maxBytes ?? SYNC_LOG_MAX_BYTES;
|
|
846
|
+
const keep = o.keep ?? 3;
|
|
847
|
+
const dir = join(stateDir, "logs");
|
|
848
|
+
const file = join(dir, "sync.log");
|
|
849
|
+
const text = `${line}\n`;
|
|
850
|
+
await mkdir(dir, { recursive: true });
|
|
851
|
+
let size = 0;
|
|
852
|
+
try { size = (await stat(file)).size; } catch { /* new log */ }
|
|
853
|
+
if (size > 0 && size + Buffer.byteLength(text) > max) {
|
|
854
|
+
await rm(`${file}.${keep}`, { force: true });
|
|
855
|
+
for (let i = keep - 1; i >= 1; i--) await rename(`${file}.${i}`, `${file}.${i + 1}`).catch(() => {});
|
|
856
|
+
await rename(file, `${file}.1`).catch(() => {});
|
|
857
|
+
}
|
|
858
|
+
await appendFile(file, text, "utf8");
|
|
859
|
+
}
|