t3code-session-manager 0.0.0-stage → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/.claude-plugin/marketplace.json +21 -0
  2. package/.claude-plugin/plugin.json +19 -0
  3. package/CHANGELOG.md +138 -0
  4. package/LICENSE +21 -0
  5. package/README.md +660 -2
  6. package/README.pt.md +616 -0
  7. package/bin/entry.d.mts +2 -0
  8. package/bin/entry.mjs +19 -0
  9. package/bin/t3sm.mjs +44 -0
  10. package/commands/import.md +30 -0
  11. package/commands/organize.md +36 -0
  12. package/commands/status.md +24 -0
  13. package/commands/sync.md +26 -0
  14. package/dist/apply/apply.js +993 -0
  15. package/dist/apply/claudeWriter.js +168 -0
  16. package/dist/apply/copyTranscript.js +116 -0
  17. package/dist/apply/importer.js +697 -0
  18. package/dist/apply/materialize.js +335 -0
  19. package/dist/apply/waves.js +108 -0
  20. package/dist/assistant/core.js +295 -0
  21. package/dist/assistant/deps.js +256 -0
  22. package/dist/assistant/i18n.js +38 -0
  23. package/dist/assistant/messages.en.js +494 -0
  24. package/dist/assistant/messages.pt.js +495 -0
  25. package/dist/assistant/runner.js +64 -0
  26. package/dist/assistant/steps.js +1362 -0
  27. package/dist/build.json +1 -0
  28. package/dist/cli/applyScreen.js +158 -0
  29. package/dist/cli/commands/accounts.js +254 -0
  30. package/dist/cli/commands/apply.js +199 -0
  31. package/dist/cli/commands/changelog.js +61 -0
  32. package/dist/cli/commands/doctor.js +105 -0
  33. package/dist/cli/commands/help.js +158 -0
  34. package/dist/cli/commands/merge.js +224 -0
  35. package/dist/cli/commands/organize.js +872 -0
  36. package/dist/cli/commands/plan.js +161 -0
  37. package/dist/cli/commands/projects.js +430 -0
  38. package/dist/cli/commands/quarantine.js +203 -0
  39. package/dist/cli/commands/recover.js +230 -0
  40. package/dist/cli/commands/rollback.js +281 -0
  41. package/dist/cli/commands/scan.js +117 -0
  42. package/dist/cli/commands/schedule.js +288 -0
  43. package/dist/cli/commands/start.js +45 -0
  44. package/dist/cli/commands/status.js +615 -0
  45. package/dist/cli/commands/sync.js +336 -0
  46. package/dist/cli/commands/update.js +398 -0
  47. package/dist/cli/errors.js +53 -0
  48. package/dist/cli/failures.js +91 -0
  49. package/dist/cli/flags.js +53 -0
  50. package/dist/cli/help.en.js +549 -0
  51. package/dist/cli/help.pt.js +566 -0
  52. package/dist/cli/helpText.js +211 -0
  53. package/dist/cli/helpTopics.js +58 -0
  54. package/dist/cli/locked.js +12 -0
  55. package/dist/cli/main.js +155 -0
  56. package/dist/cli/screen.js +3 -0
  57. package/dist/cli/ui.js +111 -0
  58. package/dist/cli/words.js +2 -0
  59. package/dist/core/activity.js +16 -0
  60. package/dist/core/changelog.js +47 -0
  61. package/dist/core/config.js +530 -0
  62. package/dist/core/fsutil.js +171 -0
  63. package/dist/core/glob.js +34 -0
  64. package/dist/core/history.js +32 -0
  65. package/dist/core/journal.js +274 -0
  66. package/dist/core/lock.js +337 -0
  67. package/dist/core/mask.js +31 -0
  68. package/dist/core/paths.js +68 -0
  69. package/dist/core/pid.js +101 -0
  70. package/dist/core/run.js +4 -0
  71. package/dist/core/semver.js +18 -0
  72. package/dist/core/sysroot.js +29 -0
  73. package/dist/core/types.js +229 -0
  74. package/dist/core/uuid.js +29 -0
  75. package/dist/core/version.js +72 -0
  76. package/dist/help/convert.md +20 -0
  77. package/dist/help/convert.pt.md +21 -0
  78. package/dist/help/cwd-unreadable.md +22 -0
  79. package/dist/help/cwd-unreadable.pt.md +22 -0
  80. package/dist/help/decisions.md +20 -0
  81. package/dist/help/decisions.pt.md +20 -0
  82. package/dist/help/errors.md +20 -0
  83. package/dist/help/errors.pt.md +22 -0
  84. package/dist/help/held.md +20 -0
  85. package/dist/help/held.pt.md +20 -0
  86. package/dist/help/history.md +27 -0
  87. package/dist/help/history.pt.md +29 -0
  88. package/dist/help/instance-unavailable.md +18 -0
  89. package/dist/help/instance-unavailable.pt.md +19 -0
  90. package/dist/help/not-imported.md +19 -0
  91. package/dist/help/not-imported.pt.md +19 -0
  92. package/dist/help/orphaned.md +19 -0
  93. package/dist/help/orphaned.pt.md +19 -0
  94. package/dist/help/project-mismatch.md +15 -0
  95. package/dist/help/project-mismatch.pt.md +15 -0
  96. package/dist/help/t3-native.md +22 -0
  97. package/dist/help/t3-native.pt.md +22 -0
  98. package/dist/help/updates.md +26 -0
  99. package/dist/help/updates.pt.md +28 -0
  100. package/dist/help/watcher.md +24 -0
  101. package/dist/help/watcher.pt.md +25 -0
  102. package/dist/i18n/cli.js +41 -0
  103. package/dist/i18n/lang.js +55 -0
  104. package/dist/i18n/messages.en.js +1140 -0
  105. package/dist/i18n/messages.pt.js +1149 -0
  106. package/dist/i18n/words.js +12 -0
  107. package/dist/ops/accounts.js +170 -0
  108. package/dist/ops/autoUpdate.js +223 -0
  109. package/dist/ops/discoverAccounts.js +343 -0
  110. package/dist/ops/doctor.js +811 -0
  111. package/dist/ops/elevated.js +47 -0
  112. package/dist/ops/errorCodes.js +39 -0
  113. package/dist/ops/follow.js +558 -0
  114. package/dist/ops/merge.js +830 -0
  115. package/dist/ops/notify.js +249 -0
  116. package/dist/ops/plugin.js +92 -0
  117. package/dist/ops/projects.js +113 -0
  118. package/dist/ops/quarantine.js +415 -0
  119. package/dist/ops/rollback.js +868 -0
  120. package/dist/ops/schedule.js +464 -0
  121. package/dist/ops/successor.js +51 -0
  122. package/dist/ops/sync.js +859 -0
  123. package/dist/ops/syncStatus.js +290 -0
  124. package/dist/ops/update.js +273 -0
  125. package/dist/ops/updater.js +641 -0
  126. package/dist/ops/watch.js +676 -0
  127. package/dist/ops/watcherControl.js +212 -0
  128. package/dist/ops/watcherHealth.js +75 -0
  129. package/dist/organize/apply.js +495 -0
  130. package/dist/organize/history.js +192 -0
  131. package/dist/organize/plan.js +283 -0
  132. package/dist/organize/rename.js +58 -0
  133. package/dist/organize/sessions.js +100 -0
  134. package/dist/organize/titles.js +70 -0
  135. package/dist/organize/types.js +73 -0
  136. package/dist/plan/categories.js +104 -0
  137. package/dist/plan/dedupe.js +137 -0
  138. package/dist/plan/folders.js +82 -0
  139. package/dist/plan/names.js +93 -0
  140. package/dist/plan/ownership.js +316 -0
  141. package/dist/plan/planner.js +669 -0
  142. package/dist/plan/rehome.js +101 -0
  143. package/dist/plan/report.js +407 -0
  144. package/dist/sources/cache.js +89 -0
  145. package/dist/sources/claude.js +297 -0
  146. package/dist/sources/codex.js +496 -0
  147. package/dist/sources/flatten.js +23 -0
  148. package/dist/sources/jsonl.js +169 -0
  149. package/dist/sources/native.js +233 -0
  150. package/dist/sources/opencode.js +483 -0
  151. package/dist/sources/pi.js +133 -0
  152. package/dist/sources/registry.js +67 -0
  153. package/dist/sources/types.js +80 -0
  154. package/dist/t3/api.js +492 -0
  155. package/dist/t3/auth.js +248 -0
  156. package/dist/t3/compat.js +62 -0
  157. package/dist/t3/decode.js +408 -0
  158. package/dist/t3/errors.js +185 -0
  159. package/dist/t3/rpc.js +185 -0
  160. package/dist/t3/runtime.js +80 -0
  161. package/dist/ui/caps.js +56 -0
  162. package/dist/ui/components.js +202 -0
  163. package/dist/ui/layout.js +205 -0
  164. package/dist/ui/list.js +423 -0
  165. package/dist/ui/locale.js +73 -0
  166. package/dist/ui/organizePreview.js +72 -0
  167. package/dist/ui/progress.js +237 -0
  168. package/dist/ui/style.js +32 -0
  169. package/dist/ui/terminal.js +126 -0
  170. package/dist/ui/text.js +186 -0
  171. package/package.json +58 -4
  172. package/skills/t3code-sessions/SKILL.md +183 -0
@@ -0,0 +1,192 @@
1
+ // The per-project history rule (spec §5.2), owner's words: recent conversations stay visible; otherwise each project
2
+ // keeps at least `minPerProject` visible; older ones settle. Settled conversations stay in their project (T3's
3
+ // "Settled" shelf). Pure: no I/O, no clock (`now` is passed in).
4
+ //
5
+ // Per project:
6
+ // 1. visible = not archived and not settled (isVisibleInProject); settled = on the Settled shelf.
7
+ // 2. recent = activityAt >= now - days (every thread when days is "all").
8
+ // 3. Unsettle every movable recent settled thread.
9
+ // 4. While the projected visible count is below the minimum, unsettle the most recent movable settled threads.
10
+ // 5. Settle every movable visible thread that is not recent, except the keep set: the `minPerProject` most recent
11
+ // threads of the projected visible ones, native and pinned threads included (they count, but never move).
12
+ // 6. While T3 Code's own auto-settle is on (HistoryOptions.t3AutoSettleDays), mark the ones the rule keeps for
13
+ // the minimum per project (recent or not): movable, shown without an override, with a date t3sm can read, and
14
+ // within MARK_AHEAD_DAYS of that option's days. A mark is an unsettle (previous SHOWN_NO_OVERRIDE): it gives the thread
15
+ // the "active" override, which T3 Code's own auto-settle never touches (S6: `settledOverride !== null` is never
16
+ // auto-settled). T3 Code 0.1.10's `thread.unsettle` on a thread not archived returns it with `settledOverride:
17
+ // "active"`, `settledAt: null`, and `unsettledAt` set to now unless it was "active" already: that only moves it in
18
+ // the sidebar's sort. Without the mark, T3 Code's option would put it away once it is older than its days, and
19
+ // the project would drop below its minimum (spec §5.2: the kept imported threads are protected from it by
20
+ // settledOverride "active"; owner-approved). A thread without a date is not marked: no evidence.
21
+ // Actions come oldest activity first, unsettle and settle alike; every unsettle before any settle, so a project never
22
+ // dips below its minimum while the plan is applied.
23
+ //
24
+ // Journal values (src/organize/types.ts): unsettle "settled"|null -> "active"; settle "active"|null -> "settled", or
25
+ // `{ settledAt }` when the settle is backdated. `settleShown` maps them to the settledOverride T3 shows.
26
+
27
+
28
+
29
+
30
+ export const DAY_MS = 86_400_000;
31
+
32
+ /**
33
+ * The one definition of "shown inside the project" (T3's rule, docs/spikes/S6-organize.md, ruling R20): not archived and
34
+ * `settledAt == null`. An unsettled thread (override "active") has settledAt null; an auto-settled native thread has
35
+ * override null and settledAt set, so it is settled.
36
+ */
37
+ export function isVisibleInProject(t ) {
38
+ return !t.archived && t.settledAt == null;
39
+ }
40
+
41
+ /** Not archived and settled (`settledAt` set): the "Settled" shelf. */
42
+ export function isOnSettledShelf(t ) {
43
+ return !t.archived && t.settledAt != null;
44
+ }
45
+
46
+ /** An ISO date as epoch ms; null when absent or unparseable. */
47
+ export function ms(iso ) {
48
+ if (typeof iso !== "string") return null;
49
+ const v = Date.parse(iso);
50
+ return Number.isFinite(v) ? v : null;
51
+ }
52
+
53
+ /**
54
+ * When the thread was last active (epoch ms): the latest of its user message and its newest run's requested / started /
55
+ * completed times; when none is known, its createdAt; when that is unknown too, 0 (older than anything). A date in the
56
+ * future counts as `now`.
57
+ */
58
+ export function activityAt(t , now ) {
59
+ let at = null;
60
+ for (const v of [t.latestUserMessageAt, t.latestRunRequestedAt, t.latestRunStartedAt, t.latestRunCompletedAt]) {
61
+ const x = ms(v);
62
+ if (x !== null && (at === null || x > at)) at = x;
63
+ }
64
+ at ??= ms(t.createdAt);
65
+ if (at === null) return 0;
66
+ return at > now ? now : at;
67
+ }
68
+
69
+ export const isObj = (u ) => typeof u === "object" && u !== null && !Array.isArray(u);
70
+
71
+ /**
72
+ * The kind a journaled change really is. Task 6's follow (before `settle` existed) journaled a carried settled state as
73
+ * kind `unsettle` with `next: "settled"`: that is a settle, and its null `previous` means "shown" (see settleShown).
74
+ */
75
+ export function changeKind(kind , next ) {
76
+ return kind === "unsettle" && settleShown("settle", next) === "settled" ? "settle" : kind;
77
+ }
78
+
79
+ /**
80
+ * The settledOverride T3 shows once t3sm wrote `value` for a change of `kind` (settle or unsettle), as compared with the
81
+ * current state: a backdated `{ settledAt }` is "settled"; null (a `previous` from a thread without an override, which T3
82
+ * cannot restore) is the side the change left: "active" for settle, "settled" for unsettle. Other kinds: unchanged.
83
+ */
84
+ export function settleShown(kind , value ) {
85
+ if (kind !== "settle" && kind !== "unsettle") return value;
86
+ if (kind === "unsettle" && value === SHOWN_NO_OVERRIDE) return null;
87
+ if (isObj(value) && typeof value.settledAt === "string") return "settled";
88
+ if (value === null) return kind === "settle" ? "active" : "settled";
89
+ return value;
90
+ }
91
+
92
+ /**
93
+ * `previous` of an unsettle that only marks a thread already shown without an override (step 6): the thread shows the
94
+ * same before and after; undoing it leaves it shown (T3 Code cannot remove an override). The override T3 shows before it
95
+ * is none (settleShown).
96
+ */
97
+ export const SHOWN_NO_OVERRIDE = "shown";
98
+
99
+ /** An unsettle that marks a thread already shown (step 6), not one that brings a thread back from the Settled shelf. */
100
+ export function isShownMark(a ) {
101
+ return a.kind === "unsettle" && a.previous === SHOWN_NO_OVERRIDE;
102
+ }
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+ /** How long before T3 Code's own auto-settle could reach a kept thread the mark is planned (the daily pass's margin). */
117
+ export const MARK_AHEAD_DAYS = 7;
118
+
119
+
120
+
121
+ const newestFirst = (a , b ) => b.at - a.at || (a.t.id < b.t.id ? -1 : a.t.id > b.t.id ? 1 : 0);
122
+ const oldestFirst = (a , b ) => a.at - b.at || (a.t.id < b.t.id ? -1 : a.t.id > b.t.id ? 1 : 0);
123
+
124
+ /** The unsettle and settle actions of the history rule over `threads` (all of T3's threads; archived ones are ignored). */
125
+ export function historyActions(threads , rule , now , o ) {
126
+ const all = rule.days === "all";
127
+ const days = rule.days === "all" ? 0 : rule.days;
128
+ if (!all && (!Number.isFinite(days) || days <= 0)) return [];
129
+ const since = all ? null : now - days * DAY_MS;
130
+ const min = Math.max(0, Math.floor(rule.minPerProject));
131
+ const recentWhy = all ? "history: all" : `history: last ${days} days`;
132
+ const minWhy = `history: at least ${min} per project`;
133
+ const settleWhy = `history: older than ${days} days`;
134
+ const keepWhy = `history: kept visible, at least ${min} per project`;
135
+
136
+ const byProject = new Map ();
137
+ for (const t of threads) {
138
+ if (t.archived) continue;
139
+ const rows = byProject.get(t.projectId) ?? [];
140
+ rows.push({ t, at: activityAt(t, now) });
141
+ byProject.set(t.projectId, rows);
142
+ }
143
+
144
+ const unsettle = [];
145
+ const settle = [];
146
+ for (const rows of byProject.values()) {
147
+ const recent = (r ) => since === null || r.at >= since;
148
+ const shown = new Set (rows.filter((r) => isVisibleInProject(r.t)));
149
+ // 3. every recent conversation is shown.
150
+ const shelf = rows.filter((r) => isOnSettledShelf(r.t) && o.movable(r.t)).sort(newestFirst);
151
+ const rest = [];
152
+ for (const r of shelf) {
153
+ if (recent(r)) { unsettle.push({ row: r, reason: recentWhy }); shown.add(r); } else rest.push(r);
154
+ }
155
+ // 4. the minimum, from the most recent settled ones.
156
+ for (const r of rest) {
157
+ if (shown.size >= min) break;
158
+ unsettle.push({ row: r, reason: minWhy });
159
+ shown.add(r);
160
+ }
161
+ // 5. older ones settle, except the `min` most recent of what is shown.
162
+ if (since === null) continue;
163
+ const keep = new Set([...shown].sort(newestFirst).slice(0, min));
164
+ for (const r of shown) {
165
+ if (keep.has(r) || recent(r) || !isVisibleInProject(r.t) || !o.movable(r.t)) continue;
166
+ settle.push(r);
167
+ }
168
+ // 6. mark the kept ones shown without an override, so T3 Code's own auto-settle leaves them shown; only
169
+ // while that option is on, and only once it could reach the thread within MARK_AHEAD_DAYS.
170
+ const t3 = o.t3AutoSettleDays;
171
+ for (const r of t3 == null ? [] : keep) {
172
+ if (now - r.at < (t3 - MARK_AHEAD_DAYS) * DAY_MS) continue;
173
+ // Only on evidence: a thread with no date t3sm can read (activity 0) may not be old to T3 Code either.
174
+ // A kept one is marked even while recent: with T3 Code's option at the rule's days (the defaults), it reaches a
175
+ // kept conversation a week after the mark, while the rule still counts it as recent.
176
+ if (r.at === 0 || !isVisibleInProject(r.t) || !o.movable(r.t) || r.t.settledOverride != null) continue;
177
+ unsettle.push({ row: r, reason: keepWhy, mark: true });
178
+ }
179
+ }
180
+
181
+ unsettle.sort((a, b) => oldestFirst(a.row, b.row));
182
+ settle.sort(oldestFirst);
183
+ return [
184
+ ...unsettle.map(({ row: { t }, reason, mark }) => ({
185
+ kind: "unsettle", target: t.id, projectId: t.projectId, previous: mark ? SHOWN_NO_OVERRIDE : t.settledOverride ?? null, next: "active", reason,
186
+ })),
187
+ ...settle.map(({ t, at }) => ({
188
+ kind: "settle", target: t.id, projectId: t.projectId, previous: t.settledOverride ?? null,
189
+ next: o.backdate && at > 0 ? { settledAt: new Date(at).toISOString() } : "settled", reason: settleWhy,
190
+ })),
191
+ ];
192
+ }
@@ -0,0 +1,283 @@
1
+ // What `t3sm organize` would change in T3 Code (spec §5), decided from a T3 snapshot, the journal and the config.
2
+ // Pure: no I/O, no clock (input.now). Task 12 applies the plan; organize, sync and the assistant build it.
3
+ //
4
+ // Rules (spec §5, controller rulings):
5
+ // - Thread actions only for `import:*` threads (and scope.onlyThreads when given; the history rule follows
6
+ // scope.historyThreads instead when given); a native T3 thread is never in it.
7
+ // - User-touched: for each target t3sm changed (journal `organize-change`), the latest change of that family
8
+ // (archive | title | settle | pin; settle covers settle and unsettle) is compared with the current state; a difference
9
+ // means the user changed it, and no action of that family is planned on that target again, the opposite one included.
10
+ // In the settle family a thread t3sm never changed is user-touched too when only a person makes its state: shown
11
+ // (override "active": imports arrive settled), or settled again after an unsettle (`unsettledAt` set). A thread back
12
+ // at t3sm's value is user-touched when T3 dated a settle or unsettle after t3sm's run (away and back by hand):
13
+ // `unsettledAt`, or `settledAt` while settled, later than the run's bound + OWN_WRITE_MARGIN_MS. The bound is the
14
+ // run's end; for a run killed before its run-end, the next run's start (runs are serialised by the lock), else `now`
15
+ // (the planner runs under the lock: no earlier run can still be writing). A follow's revision thread (a standing
16
+ // `followed` entry names it as newThread) is user-touched in the settle family when its old thread is: follow carries
17
+ // the old thread's settled state, and a state the user chose there stays theirs.
18
+ // - History (spec §5.2, src/organize/history.ts): per project, recent conversations shown, at least `minPerProject`
19
+ // shown, older ones settled (they stay on the project's Settled shelf). Native, pinned and automated threads and
20
+ // those of an unknown settle state count toward the minimum but never move; archived and to-be-archived threads
21
+ // neither count nor move.
22
+ // - project-delete only for projects t3sm created with zero threads (any state): T3 refuses to delete a project that
23
+ // still holds a thread unless `force`, which t3sm never sends. Never one created in the last 24 h, nor one with
24
+ // sessions waiting to import (input.keepProjects): apply creates a held root's project empty and imports later.
25
+ import { normalizeHistory } from "../core/history.js";
26
+
27
+ import { SETTLE_BACKDATE } from "../t3/compat.js";
28
+
29
+ import { DAY_MS, historyActions, isOnSettledShelf, isVisibleInProject, ms, settleShown } from "./history.js";
30
+ import { isTechnicalTitle } from "./titles.js";
31
+
32
+
33
+ /**
34
+ * T3 dates t3sm's own settle / unsettle when it applies it, inside t3sm's run: a date up to this long after the run's
35
+ * bound is t3sm's own (clock rounding between the two processes on one machine).
36
+ */
37
+ const OWN_WRITE_MARGIN_MS = 60_000;
38
+
39
+ export const AUTO_SETTLE_TARGET = "sidebarAutoSettleAfterDays";
40
+
41
+
42
+
43
+ const FAMILY = {
44
+ archive: "archive", title: "title", unsettle: "settle", settle: "settle", pin: "pin", unpin: "pin",
45
+ };
46
+
47
+ /** Latest `next` t3sm recorded per family and target ("family\u0000target"), and the run that recorded it. */
48
+ function lastChanges(journal ) {
49
+ const next = new Map ();
50
+ const runOf = new Map ();
51
+ for (const e of journal) {
52
+ if (e.t !== "organize-change") continue;
53
+ const f = FAMILY[e.kind];
54
+ if (!f) continue;
55
+ const k = `${f}\u0000${e.target}`;
56
+ next.set(k, settleShown(e.kind, e.next));
57
+ runOf.set(k, e.runId);
58
+ }
59
+ return { next, runOf };
60
+ }
61
+
62
+ export { isOnSettledShelf, isVisibleInProject };
63
+
64
+ const KINDS = ["archive", "title", "unsettle", "settle", "pin", "unpin", "project-delete", "project-title", "auto-settle"];
65
+
66
+ export function buildOrganizePlan(i ) {
67
+ const cfg = i.config;
68
+ const { next: last, runOf } = lastChanges(i.journal);
69
+ /** t3sm's latest change of `family` on `target` is not the current state: the user changed it since. */
70
+ const touched = (family , target , current ) => {
71
+ const k = `${family}\u0000${target}`;
72
+ return last.has(k) && last.get(k) !== current;
73
+ };
74
+ const isTechnical = i.isTechnicalTitle ?? isTechnicalTitle;
75
+ const category = (projectId ) => i.projectCategory?.get(projectId) ?? "project";
76
+
77
+ const created = new Set ();
78
+ /** project id -> when t3sm created it (its run's start, else the time in its run id); absent when unknown. */
79
+ const createdAt = new Map ();
80
+ const runStart = new Map ();
81
+ /** run id -> the latest time it can have written: its run-end, else the next run's start, else now. */
82
+ const runBound = new Map ();
83
+ {
84
+ const unended = [];
85
+ for (const e of i.journal) {
86
+ if (e.t !== "run-start" && e.t !== "run-end") continue;
87
+ const at = Date.parse(e.at);
88
+ if (!Number.isFinite(at)) continue;
89
+ if (e.t === "run-end") { runBound.set(e.runId, at); continue; }
90
+ runStart.set(e.runId, at);
91
+ // A run still open when the next one starts was killed (the lock serialises runs): bounded by that start.
92
+ for (const r of unended.splice(0)) if (!runBound.has(r)) runBound.set(r, at);
93
+ unended.push(e.runId);
94
+ }
95
+ for (const r of unended) if (!runBound.has(r)) runBound.set(r, i.now);
96
+ }
97
+ for (const e of i.journal) {
98
+ if (e.t !== "project-created") continue;
99
+ created.add(e.projectId);
100
+ const at = runStart.get(e.runId) ?? runIdTime(e.runId);
101
+ if (at !== null) createdAt.set(e.projectId, at);
102
+ }
103
+
104
+ const inScope = (t ) =>
105
+ t.id.startsWith("import:") && (i.scope.onlyThreads === undefined || i.scope.onlyThreads.has(t.id));
106
+
107
+ const archive = [];
108
+ const title = [];
109
+ const archived = new Set ();
110
+
111
+ for (const t of i.threads) {
112
+ if (!inScope(t)) continue;
113
+ // A settle state this t3sm does not know (a newer T3 Code): nothing is planned for that thread.
114
+ if (t.settledOverride === "unknown") continue;
115
+ const s = i.sessions.get(t.id);
116
+ const automated = s?.origin === "automated";
117
+
118
+ // 5.1 / 5.5: archive automated sessions, and threads of a t3sm project in a temp/system folder (unless the user
119
+ // chose to import that folder kind).
120
+ if (!t.archived && !touched("archive", t.id, t.archived)) {
121
+ const cat = category(t.projectId);
122
+ let reason = null;
123
+ if (cfg.archiveAutomated && automated) reason = "automated session (Agent SDK)";
124
+ else if (i.archiveFolders !== false && (cat === "temp" || cat === "system") && created.has(t.projectId) && cfg.folders[cat] !== "import") {
125
+ reason = `${cat} folder`;
126
+ }
127
+ if (reason !== null) {
128
+ archive.push({ kind: "archive", target: t.id, projectId: t.projectId, previous: false, next: true, reason });
129
+ archived.add(t.id);
130
+ }
131
+ }
132
+
133
+ // 5.2: a new title when T3's title is technical, or still the one t3sm set. Not for archived or to-be-archived
134
+ // threads (R16).
135
+ const proposal = s?.title ?? null;
136
+ if (cfg.titles && !t.archived && !archived.has(t.id) && proposal !== null && proposal !== t.title && !touched("title", t.id, t.title)) {
137
+ const k = `title\u0000${t.id}`;
138
+ const ours = last.has(k) && last.get(k) === t.title;
139
+ if (ours || isTechnical(t.title)) {
140
+ title.push({
141
+ kind: "title", target: t.id, projectId: t.projectId, previous: t.title, next: proposal,
142
+ reason: ours ? "title set by t3sm earlier" : "technical title",
143
+ });
144
+ }
145
+ }
146
+ }
147
+
148
+ // History (spec §5.2, §5.3): the rule, per project, over every thread (native and pinned ones count toward the minimum).
149
+ /** A settle state only a person makes, or one changed since t3sm's last change of the settle family. */
150
+ const settleTouched = (t , seen = new Set()) => {
151
+ if (ownSettleTouched(t)) return true;
152
+ // A follow's revision thread (src/ops/follow.ts) continues its old thread and carries its settled state: a state the
153
+ // user chose there stays theirs here (spec §5.2: manual choices win), whether the carry had to be sent or not.
154
+ const from = followedFrom.get(t.id);
155
+ const old = from === undefined || seen.has(from) ? undefined : threadById.get(from);
156
+ return old !== undefined && settleTouched(old, seen.add(t.id));
157
+ };
158
+ /** newThread -> oldThread of every standing revision `followed` entry (a rolled-back run counts for nothing). */
159
+ const followedFrom = new Map ();
160
+ {
161
+ const rolledBack = new Set(i.journal.flatMap((e) => (e.t === "rolled-back" ? [e.targetRunId] : [])));
162
+ for (const e of i.journal) if (e.t === "followed" && typeof e.newThread === "string" && !rolledBack.has(e.runId)) followedFrom.set(e.newThread, e.oldThread);
163
+ }
164
+ const threadById = new Map(i.threads.map((t) => [t.id, t] ));
165
+ function ownSettleTouched(t ) {
166
+ const cur = t.settledOverride ?? null;
167
+ const k = `settle\u0000${t.id}`;
168
+ if (!last.has(k)) return cur === "active" || (cur === "settled" && t.unsettledAt != null);
169
+ if (touched("settle", t.id, cur)) return true;
170
+ // Back at t3sm's value: by hand when T3 dated a settle or unsettle after t3sm's run (A -> B -> A). The run's bound
171
+ // (runBound) is the latest time t3sm's own write can carry. A run with no run-start nor run-end: not checked.
172
+ const ref = runBound.get(runOf.get(k) );
173
+ if (ref === undefined) return false;
174
+ const after = (v ) => { const x = ms(v); return x !== null && x > ref + OWN_WRITE_MARGIN_MS; };
175
+ return after(t.unsettledAt) || (cur === "settled" && after(t.settledAt));
176
+ }
177
+ const historyScope = i.scope.historyThreads;
178
+ const inHistory = (t ) => (historyScope === undefined ? inScope(t) : t.id.startsWith("import:") && historyScope.has(t.id));
179
+ const movable = (t ) =>
180
+ inHistory(t) && !t.archived && !archived.has(t.id) && t.pinnedAt == null && t.settledOverride !== "unknown" &&
181
+ i.sessions.get(t.id)?.origin !== "automated" && !settleTouched(t);
182
+ const rule = normalizeHistory(cfg.history);
183
+ const history = rule === null ? [] : historyActions(
184
+ i.threads.map((t) => (archived.has(t.id) ? { ...t, archived: true } : t)), rule, i.now,
185
+ // T3 Code's own auto-settle as it will be after this run (this run's change, else its current value): the marks.
186
+ { movable, backdate: i.backdate ?? SETTLE_BACKDATE, t3AutoSettleDays: i.autoSettle ? i.autoSettle.want : i.t3AutoSettleDays },
187
+ );
188
+ const unsettle = history.filter((a) => a.kind === "unsettle");
189
+ const settle = history.filter((a) => a.kind === "settle");
190
+ const unsettled = new Set(unsettle.map((a) => a.target));
191
+ const settled = new Set(settle.map((a) => a.target));
192
+
193
+ // 5.4: pin the most recently active projects.
194
+ const pin = [];
195
+ const unpin = [];
196
+ if (i.scope.allowPins && cfg.pinRecent.enabled) {
197
+ const count = cfg.pinRecent.count;
198
+ const newest = new Map ();
199
+ for (const t of i.threads) {
200
+ if (t.archived || archived.has(t.id)) continue; // noise does not make a project recent
201
+ const at = ms(t.latestUserMessageAt);
202
+ if (at === null) continue;
203
+ const prev = newest.get(t.projectId);
204
+ if (prev === undefined || at > prev) newest.set(t.projectId, at);
205
+ }
206
+ const ranked = i.projects
207
+ .filter((p) => !p.deleted && !p.archived && newest.has(p.id))
208
+ .sort((a, b) => (newest.get(b.id) - newest.get(a.id) ) || cmp(a.title, b.title) || cmp(a.id, b.id));
209
+ const top = ranked.slice(0, count);
210
+ const topIds = new Set(top.map((p) => p.id));
211
+ const reason = `top ${count} recent`;
212
+ for (const p of top) {
213
+ if (p.pinned || touched("pin", p.id, p.pinned)) continue;
214
+ pin.push({ kind: "pin", target: p.id, projectId: p.id, previous: false, next: true, reason });
215
+ }
216
+ for (const p of i.projects) {
217
+ if (p.deleted || !p.pinned || topIds.has(p.id)) continue;
218
+ const k = `pin\u0000${p.id}`;
219
+ if (!last.has(k) || last.get(k) !== true) continue; // only pins t3sm made and the user left alone
220
+ unpin.push({ kind: "unpin", target: p.id, projectId: p.id, previous: true, next: false, reason: `left the ${reason}` });
221
+ }
222
+ }
223
+
224
+ // 5.5: delete empty projects t3sm created.
225
+ const del = [];
226
+ if (i.scope.allowDeletes) {
227
+ const withThreads = new Set(i.threads.map((t) => t.projectId));
228
+ for (const p of i.projects) {
229
+ if (p.deleted || !created.has(p.id) || withThreads.has(p.id)) continue;
230
+ // Its sessions may still be on their way (held while a conversation was in use): kept for now.
231
+ if (i.keepProjects?.has(p.id)) continue;
232
+ const at = createdAt.get(p.id);
233
+ if (at !== undefined && i.now - at < DAY_MS) continue;
234
+ del.push({
235
+ kind: "project-delete", target: p.id, projectId: p.id,
236
+ previous: { title: p.title, workspaceRoot: p.workspaceRoot }, next: null, reason: "created by t3sm, no threads",
237
+ });
238
+ }
239
+ }
240
+
241
+ const auto = [];
242
+ if (i.autoSettle && i.autoSettle.want !== i.autoSettle.current) {
243
+ auto.push({
244
+ kind: "auto-settle", target: AUTO_SETTLE_TARGET, projectId: null,
245
+ previous: i.autoSettle.current, next: i.autoSettle.want, reason: "global auto-settle",
246
+ });
247
+ }
248
+
249
+ const actions = [...archive, ...title, ...unsettle, ...settle, ...pin, ...unpin, ...del, ...auto];
250
+ const counts = Object.fromEntries(KINDS.map((k) => [k, 0])) ;
251
+ for (const a of actions) counts[a.kind]++;
252
+
253
+ return { runId: i.runId, actions, counts, byProject: byProject(i.projects, i.threads, archived, unsettled, settled) };
254
+ }
255
+
256
+ /** A run's start time from its id (`YYYYMMDD-HHMMSS-…`, UTC), or null. */
257
+ function runIdTime(runId ) {
258
+ const m = /^(\d{4})(\d{2})(\d{2})-(\d{2})(\d{2})(\d{2})-/.exec(runId);
259
+ return m ? Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]) : null;
260
+ }
261
+
262
+ function cmp(a , b ) {
263
+ return a < b ? -1 : a > b ? 1 : 0;
264
+ }
265
+
266
+ function byProject(projects , threads , archived , unsettled , settled ) {
267
+ const rows = new Map ();
268
+ for (const t of threads) {
269
+ const r = rows.get(t.projectId) ?? { before: 0, after: 0 };
270
+ const before = isVisibleInProject(t);
271
+ const after = archived.has(t.id) || settled.has(t.id) ? false : unsettled.has(t.id) ? true : before;
272
+ if (before) r.before++;
273
+ if (after) r.after++;
274
+ rows.set(t.projectId, r);
275
+ }
276
+ const out = [];
277
+ for (const p of projects) {
278
+ const r = rows.get(p.id);
279
+ if (p.deleted || !r) continue;
280
+ out.push({ projectId: p.id, title: p.title, visibleBefore: r.before, visibleAfter: r.after });
281
+ }
282
+ return out.sort((a, b) => (b.visibleAfter - a.visibleAfter) || cmp(a.title, b.title) || cmp(a.projectId, b.projectId));
283
+ }
@@ -0,0 +1,58 @@
1
+ // `t3sm projects rename --suggest` (spec §4.5.5): clean titles for the projects t3sm created whose title is still the
2
+ // one t3sm gave them. Pure: no I/O. Applied with applyOrganize (kind `project-title`), so `t3sm organize --undo` and
3
+ // `t3sm rollback` revert it.
4
+ //
5
+ // The title t3sm gave a project: the latest `organize-change` project-title `next` when t3sm renamed it before, else
6
+ // the `project-created` title, else (entries written before the title was journaled) the folder's last segment, which
7
+ // is what t3sm named projects then. A project whose current title differs was renamed by the user: never touched.
8
+
9
+
10
+ import { sameCwd } from "../core/paths.js";
11
+ import { projectTitle } from "../plan/names.js";
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+ const lastSegment = (root ) => root.replace(/[\\/]+$/, "").split(/[\\/]/).pop() || root;
23
+
24
+ function cmp(a , b ) {
25
+ return a < b ? -1 : a > b ? 1 : 0;
26
+ }
27
+
28
+ export function suggestProjectTitles(projects , journal , o ) {
29
+ /** projectId -> the title t3sm created it with (undefined: an entry from before the title was journaled). */
30
+ const created = new Map ();
31
+ const renamed = new Map ();
32
+ for (const e of journal) {
33
+ if (e.t === "project-created") created.set(e.projectId, typeof e.title === "string" ? e.title : undefined);
34
+ else if (e.t === "organize-change" && e.kind === "project-title") renamed.set(e.target, e.next);
35
+ }
36
+ const live = projects.filter((p) => !p.deleted);
37
+ // Titles in use (lower case) and how many projects carry each: T3 allows the same title twice.
38
+ const taken = new Map ();
39
+ const use = (t , d ) => {
40
+ const k = t.toLowerCase();
41
+ const n = (taken.get(k) ?? 0) + d;
42
+ if (n > 0) taken.set(k, n); else taken.delete(k);
43
+ };
44
+ for (const p of live) use(p.title, 1);
45
+ const out = [];
46
+ for (const p of [...live].sort((a, b) => cmp(a.workspaceRoot, b.workspaceRoot) || cmp(a.id, b.id))) {
47
+ if (!created.has(p.id)) continue;
48
+ if (o.generalFolder !== undefined && sameCwd(p.workspaceRoot, o.generalFolder, o.platform)) continue;
49
+ const ours = renamed.has(p.id) ? renamed.get(p.id) : created.get(p.id) ?? lastSegment(p.workspaceRoot);
50
+ if (p.title !== ours) continue;
51
+ use(p.title, -1);
52
+ const next = projectTitle(p.workspaceRoot, new Set(taken.keys()), { maxLength: o.maxLength, platform: o.platform });
53
+ use(next, 1);
54
+ if (next === p.title) continue;
55
+ out.push({ kind: "project-title", target: p.id, projectId: p.id, previous: p.title, next, reason: "created by t3sm, title not changed since" });
56
+ }
57
+ return out;
58
+ }
@@ -0,0 +1,100 @@
1
+ // The `sessions` input of buildOrganizePlan (ruling R13-4): for every `import:*` thread, its transcript's origin
2
+ // (human / automated) and a title proposal. Shared by `t3sm organize`, the assistant and sync.
3
+ //
4
+ // A thread `import:<instance>:<nativeId>` belongs to the Claude account whose home is that instance's home (no home
5
+ // reported, as for an implicit instance T3 has not resolved: the account with the instance's id); its
6
+ // transcript is the discovered Claude session of that account with that id (a backup copy only when nothing else has
7
+ // it), else the file the journal's `imported` entry names. A thread of another driver's instance (Codex, ...) is left
8
+ // out silently (no Claude transcript to read: human, no title). A Claude thread whose transcript is not found counts
9
+ // in `missing` (human, no title).
10
+
11
+
12
+ import { expandHome, pathKey, sameCwd } from "../core/paths.js";
13
+
14
+ import { isTechnicalTitle } from "./titles.js";
15
+
16
+
17
+ const IMPORT_RE = /^import:([^:]+):(.+)$/s;
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+ /**
41
+ * Threads buildOrganizePlan may retitle: not archived, and the title is technical or still the one t3sm set (the
42
+ * latest `organize-change` title). A superset of the planner's title rule, so only these transcripts are read for a
43
+ * title; the others get `title: null`, which the planner treats the same way for them.
44
+ */
45
+ export function mayRetitle(journal ) {
46
+ const ours = new Map ();
47
+ for (const e of journal) if (e.t === "organize-change" && e.kind === "title") ours.set(e.target, e.next);
48
+ return (t) => !t.archived && (isTechnicalTitle(t.title) || (ours.has(t.id) && ours.get(t.id) === t.title));
49
+ }
50
+
51
+ export async function matchSessions(i ) {
52
+ const key = (p ) => pathKey(p, i.platform);
53
+ const better = (a , b ) => {
54
+ const ab = a.flags.includes("backup-copy"), bb = b.flags.includes("backup-copy");
55
+ if (ab !== bb) return !ab;
56
+ return a.updatedAt > b.updatedAt;
57
+ };
58
+ const byId = new Map ();
59
+ const byPath = new Map ();
60
+ for (const s of i.discovered) {
61
+ if (s.harness !== "claude") continue;
62
+ byPath.set(key(s.file.path), s);
63
+ const k = `${s.accountId ?? ""}\u0000${s.nativeId}`;
64
+ const prev = byId.get(k);
65
+ if (!prev || better(s, prev)) byId.set(k, s);
66
+ }
67
+ const importedPath = new Map ();
68
+ for (const e of i.journal) if (e.t === "imported" && typeof e.source?.path === "string") importedPath.set(e.threadId, e.source.path);
69
+ const accounts = i.accounts.map((a) => ({ id: a.id, home: i.userHome !== undefined ? expandHome(a.home, i.userHome) : a.home }));
70
+ /**
71
+ * The config account of an instance: the one whose home is the instance's home. T3 reports no home for an implicit
72
+ * instance it has not resolved (apply and doctor fall back the same way): then the account with the instance's id.
73
+ */
74
+ const accountOf = (instanceId ) => {
75
+ const home = i.instances.find((x) => x.instanceId === instanceId)?.homePath ?? null;
76
+ if (home !== null) return accounts.find((a) => sameCwd(a.home, home, i.platform))?.id ?? null;
77
+ return accounts.find((a) => a.id === instanceId)?.id ?? null;
78
+ };
79
+ const retitle = mayRetitle(i.journal);
80
+
81
+ const sessions = new Map();
82
+ let missing = 0;
83
+ for (const t of i.threads) {
84
+ const m = IMPORT_RE.exec(t.id);
85
+ if (!m) continue;
86
+ const inst = i.instances.find((x) => x.instanceId === m[1]);
87
+ if (inst && inst.driver !== "claudeAgent") continue;
88
+ const account = accountOf(m[1] );
89
+ const path = importedPath.get(t.id);
90
+ const s = (account !== null ? byId.get(`${account}\u0000${m[2]}`) : undefined) ?? (path !== undefined ? byPath.get(key(path)) : undefined);
91
+ if (!s) {
92
+ missing++;
93
+ sessions.set(t.id, { origin: "human", title: null });
94
+ continue;
95
+ }
96
+ const title = i.title !== null && retitle(t) ? await i.title(s.file.path) : null;
97
+ sessions.set(t.id, { origin: s.origin ?? "human", title });
98
+ }
99
+ return { sessions, missing };
100
+ }