uv-suite 0.26.5 → 0.28.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.
@@ -0,0 +1,100 @@
1
+ ---
2
+ name: session-end
3
+ description: >
4
+ Cleanly close the current UV Suite session: write a final manual checkpoint
5
+ (full conversation context), then mark the session as terminated so the
6
+ Watchtower dashboard flips its lifecycle badge to Terminated. Use before
7
+ closing the terminal when you want a deliberate end-of-session beat.
8
+ argument-hint: "[optional-label]"
9
+ user-invocable: true
10
+ allowed-tools:
11
+ - Write(*)
12
+ - Read(*)
13
+ - Bash(git status *)
14
+ - Bash(git diff *)
15
+ - Bash(git log *)
16
+ - Bash(git branch *)
17
+ - Bash(git rev-parse *)
18
+ - Bash(date *)
19
+ - Bash(ls *)
20
+ - Bash(mkdir *)
21
+ - Bash(cat *)
22
+ - Bash(echo *)
23
+ - Bash("$CLAUDE_PROJECT_DIR"/.claude/hooks/checkpoint-helper.sh *)
24
+ - Bash("$CLAUDE_PROJECT_DIR"/.claude/hooks/session-end-helper.sh *)
25
+ ---
26
+
27
+ ## Resolve checkpoint directory + session metadata
28
+
29
+ !`"$CLAUDE_PROJECT_DIR"/.claude/hooks/checkpoint-helper.sh dir`
30
+
31
+ !`"$CLAUDE_PROJECT_DIR"/.claude/hooks/checkpoint-helper.sh frontmatter`
32
+
33
+ ## Git state
34
+
35
+ !`git branch --show-current 2>/dev/null || echo "(not a git repo)"`
36
+
37
+ !`git status --short 2>/dev/null | head -20 || echo "(no git)"`
38
+
39
+ !`git log --oneline -5 2>/dev/null || echo "(no git history)"`
40
+
41
+ ## Step 1 — Write the final checkpoint
42
+
43
+ Write a file at `<checkpoint-dir>/final-YYYY-MM-DD-HHMM.md` using the current
44
+ timestamp. **Override** the frontmatter line `checkpoint_kind: auto-mechanical`
45
+ to read `checkpoint_kind: final-manual` so it's distinguishable from the
46
+ auto-checkpoints. Also write/overwrite `<checkpoint-dir>/latest.md` with the
47
+ same content so `/restore` picks it up.
48
+
49
+ If a label was provided, include it in the filename:
50
+ `<checkpoint-dir>/final-YYYY-MM-DD-HHMM-[label].md`
51
+
52
+ ### Label
53
+
54
+ $ARGUMENTS
55
+
56
+ ### Body structure (after the frontmatter)
57
+
58
+ ```markdown
59
+ # Final checkpoint: [date] [time] [label if provided]
60
+
61
+ ## What was accomplished
62
+ - Concrete things done across the whole session — files, commits, decisions
63
+ - One bullet per significant artifact; don't pad
64
+
65
+ ## Key decisions made
66
+ - Decision: Why — what was considered, what was rejected
67
+
68
+ ## Current state
69
+ - Branch / uncommitted changes / tests status / blockers
70
+
71
+ ## Open threads
72
+ - Anything left in flight that the next session needs to pick up
73
+ - Pending reviews, awaiting input, deferred work
74
+
75
+ ## Context the next session needs
76
+ - Non-obvious facts, workarounds, "this looks wrong but it's intentional because…"
77
+ ```
78
+
79
+ Be specific. This is the last record before the session closes — anything
80
+ not written here is lost unless it's in code or the auto-checkpoints.
81
+
82
+ ## Step 2 — Mark the session terminated
83
+
84
+ Once the file is written, run:
85
+
86
+ !`"$CLAUDE_PROJECT_DIR"/.claude/hooks/session-end-helper.sh`
87
+
88
+ Show the user the two-line output. Don't add commentary — the file write
89
+ and the termination signal together speak for themselves.
90
+
91
+ ## Notes
92
+
93
+ - `/session-end` does **not** close the terminal or exit Claude Code. After
94
+ it runs, the badge flips to Terminated in the Watchtower; the user
95
+ closes the terminal manually.
96
+ - The final checkpoint uses the live session's full conversation context —
97
+ higher fidelity than the auto-checkpoint summaries because Claude has
98
+ everything in working memory at this moment.
99
+ - If you only want to terminate without a checkpoint, run
100
+ `hooks/session-end-helper.sh` directly from bash and skip this skill.
@@ -0,0 +1,42 @@
1
+ You are writing a one-paragraph summary for the UV Suite auto-checkpoint
2
+ system. The actual conversation transcript is provided below — base the
3
+ summary on what was actually said and done, not on inference.
4
+
5
+ ## Session
6
+
7
+ - name: {{name}}
8
+ - kind: {{kind}}
9
+ - priority: {{priority}}
10
+ - persona: {{persona}}
11
+ - purpose: {{purpose}}
12
+ - elapsed since last semantic checkpoint: {{elapsed_min}} min
13
+
14
+ ## Conversation (verbatim, last {{interval_min}} min)
15
+
16
+ {{conversation}}
17
+
18
+ ## Mechanical (from the dashboard event log)
19
+
20
+ {{mechanical}}
21
+
22
+ ## Git
23
+
24
+ Branch: {{git_branch}}
25
+ Status: {{git_status}}
26
+ Recent commits: {{git_log}}
27
+
28
+ ## Write the summary
29
+
30
+ Output **only** a single paragraph, 3-6 sentences, no headers, no bullets.
31
+ The paragraph should answer, in order:
32
+
33
+ 1. What was the user trying to do this window?
34
+ 2. What did Claude actually do — concrete files / commands / decisions?
35
+ 3. Where does the session stand right now (in progress / blocked / awaiting input)?
36
+
37
+ Rules:
38
+ - Use the user's own words for the topic when possible ("user asked about X")
39
+ - Name the artifacts (file paths, command names, function names) — no vague verbs
40
+ - If the conversation is sparse, say "Quiet window — only X tool calls, no
41
+ substantive exchange" and stop. Don't invent activity.
42
+ - Don't restate the session metadata; it's already in the frontmatter.
@@ -0,0 +1,505 @@
1
+ // UV Suite — Tier B auto-checkpoint runner.
2
+ // Called from watchtower/server.js on a setInterval. For each active session
3
+ // whose interval has elapsed, reads the Claude Code transcript JSONL at
4
+ // ~/.claude/projects/<encoded-cwd>/<session_id>.jsonl, extracts the
5
+ // conversation in the window, and writes a self-contained checkpoint:
6
+ //
7
+ // ## Summary — one paragraph from `claude -p --bare --model haiku`
8
+ // using the transcript as input
9
+ // ## Conversation — raw extract: user prompts verbatim + assistant
10
+ // response openings (~250 chars each) + tool calls
11
+ // ## Mechanical — git state + tool counts + files touched
12
+ //
13
+ // The transcript is copied into our checkpoint, so the file stands alone
14
+ // even if Claude Code later deletes its source JSONL.
15
+
16
+ const fs = require("fs");
17
+ const path = require("path");
18
+ const os = require("os");
19
+ const { spawn } = require("child_process");
20
+
21
+ const PROMPT_TEMPLATE_PATH = path.join(__dirname, "auto-checkpoint-prompt.md");
22
+ const DEFAULT_INTERVAL_MIN = 10;
23
+ const POLL_INTERVAL_MS = 60 * 1000;
24
+ const MAX_BUDGET_USD = "0.05";
25
+ const MODEL = "haiku";
26
+ const MAX_ASSISTANT_PREVIEW_CHARS = 250;
27
+ const MAX_CONVERSATION_LINES = 200;
28
+
29
+ function readJsonSafe(p) {
30
+ try {
31
+ return JSON.parse(fs.readFileSync(p, "utf-8"));
32
+ } catch {
33
+ return null;
34
+ }
35
+ }
36
+
37
+ function readStringSafe(p) {
38
+ try {
39
+ return fs.readFileSync(p, "utf-8").trim();
40
+ } catch {
41
+ return "";
42
+ }
43
+ }
44
+
45
+ // Returns { mode, interval_minutes } — defaults if state file missing.
46
+ function readAutoCheckpointState(projectDir) {
47
+ const f = path.join(projectDir, ".uv-suite-state", "auto-checkpoint.json");
48
+ const d = readJsonSafe(f);
49
+ return {
50
+ mode: d?.mode ?? "on",
51
+ interval_minutes: d?.interval_minutes ?? DEFAULT_INTERVAL_MIN,
52
+ };
53
+ }
54
+
55
+ // Group events by session, keep the most recent set per session.
56
+ function groupActiveSessions(events, windowMs) {
57
+ const cutoff = Date.now() - windowMs;
58
+ const bySession = new Map();
59
+ for (const ev of events) {
60
+ const sid = ev.uvs_session_id || ev.session_id;
61
+ if (!sid) continue;
62
+ if ((ev._ts || 0) < cutoff) continue;
63
+ if (!bySession.has(sid)) {
64
+ bySession.set(sid, {
65
+ sid,
66
+ cwd: ev.cwd,
67
+ session_name: ev.session_name || "",
68
+ session_kind: ev.session_kind || "",
69
+ session_priority: ev.session_priority || "",
70
+ session_purpose: ev.session_purpose || "",
71
+ persona: ev.persona || "",
72
+ events: [],
73
+ });
74
+ }
75
+ bySession.get(sid).events.push(ev);
76
+ }
77
+ return [...bySession.values()];
78
+ }
79
+
80
+ // Claude Code stores transcripts at ~/.claude/projects/<encoded>/<sid>.jsonl
81
+ // where <encoded> is the project path with "/" replaced by "-".
82
+ function transcriptPathFor(cwd, ccSessionId) {
83
+ if (!ccSessionId) return null;
84
+ const encoded = cwd.replace(/\//g, "-");
85
+ return path.join(
86
+ os.homedir(),
87
+ ".claude",
88
+ "projects",
89
+ encoded,
90
+ `${ccSessionId}.jsonl`,
91
+ );
92
+ }
93
+
94
+ // Defensive parser: Claude Code's JSONL format is internal and may change.
95
+ // We pull out user prompts, assistant responses, and tool calls — skipping
96
+ // anything we can't interpret rather than blowing up.
97
+ function extractTextFromContent(content) {
98
+ if (typeof content === "string") return content;
99
+ if (Array.isArray(content)) {
100
+ return content
101
+ .map((block) => {
102
+ if (typeof block === "string") return block;
103
+ if (block && block.type === "text" && typeof block.text === "string")
104
+ return block.text;
105
+ return "";
106
+ })
107
+ .filter(Boolean)
108
+ .join("\n");
109
+ }
110
+ return "";
111
+ }
112
+
113
+ // Read transcript messages whose timestamp falls in [sinceMs, +inf).
114
+ // Returns a flat array of { role, text, ts, tool? } records, oldest first.
115
+ function readTranscriptMessages(transcriptPath, sinceMs) {
116
+ if (!transcriptPath || !fs.existsSync(transcriptPath)) {
117
+ if (transcriptPath) {
118
+ console.warn(`[auto-checkpoint] transcript not found: ${transcriptPath}`);
119
+ }
120
+ return null;
121
+ }
122
+ let raw;
123
+ try {
124
+ raw = fs.readFileSync(transcriptPath, "utf-8");
125
+ } catch (err) {
126
+ console.warn(`[auto-checkpoint] failed to read transcript: ${err.message}`);
127
+ return null;
128
+ }
129
+ const out = [];
130
+ let totalLines = 0;
131
+ let parseFailures = 0;
132
+ let recognizedShapes = 0;
133
+ for (const line of raw.split("\n")) {
134
+ if (!line.trim()) continue;
135
+ totalLines++;
136
+ let msg;
137
+ try {
138
+ msg = JSON.parse(line);
139
+ } catch {
140
+ parseFailures++;
141
+ continue;
142
+ }
143
+ const tsStr = msg.timestamp || msg.ts || msg.createdAt || msg.created_at;
144
+ const ts = tsStr ? Date.parse(tsStr) : NaN;
145
+ if (Number.isFinite(ts) && ts < sinceMs) continue;
146
+
147
+ // Tolerate several shapes Claude Code has used:
148
+ // { type: "user"|"assistant", message: { role, content } }
149
+ // { role, content }
150
+ // { sender: "user"|"assistant", text } (older)
151
+ // { event: "message", role, content } (variant)
152
+ const role =
153
+ msg.role ||
154
+ msg.message?.role ||
155
+ msg.sender ||
156
+ (msg.type === "user" || msg.type === "assistant" ? msg.type : null);
157
+ const content = msg.message?.content ?? msg.content ?? msg.text ?? msg.body;
158
+ const text = extractTextFromContent(content).trim();
159
+
160
+ if (role === "user" && text) {
161
+ out.push({ role: "user", text, ts });
162
+ recognizedShapes++;
163
+ } else if (role === "assistant" && text) {
164
+ out.push({ role: "assistant", text, ts });
165
+ recognizedShapes++;
166
+ }
167
+ }
168
+ // Surface format drift loudly: if the file has content but nothing parsed
169
+ // as a recognizable message, the Claude Code schema has probably changed.
170
+ if (totalLines > 0 && recognizedShapes === 0) {
171
+ console.warn(
172
+ `[auto-checkpoint] transcript at ${transcriptPath}: ${totalLines} lines, ` +
173
+ `${parseFailures} JSON-parse failures, 0 recognized user/assistant messages. ` +
174
+ `Claude Code's transcript format may have changed — please file an issue.`,
175
+ );
176
+ }
177
+ return out;
178
+ }
179
+
180
+ // Build the ## Conversation extract markdown block. Trims long assistant
181
+ // turns; caps total lines.
182
+ function buildConversationExtract(messages) {
183
+ if (!messages || messages.length === 0) return "";
184
+ const lines = [];
185
+ for (const m of messages) {
186
+ if (m.role === "user") {
187
+ lines.push(`**You:**`);
188
+ for (const ln of m.text.split("\n")) lines.push(`> ${ln}`);
189
+ } else {
190
+ let preview = m.text;
191
+ if (preview.length > MAX_ASSISTANT_PREVIEW_CHARS) {
192
+ preview =
193
+ preview.slice(0, MAX_ASSISTANT_PREVIEW_CHARS).trimEnd() + " …";
194
+ }
195
+ lines.push(`**Claude:** ${preview.replace(/\n+/g, " ")}`);
196
+ }
197
+ lines.push("");
198
+ }
199
+ if (lines.length > MAX_CONVERSATION_LINES) {
200
+ const trimmed = lines.slice(-MAX_CONVERSATION_LINES);
201
+ trimmed.unshift(
202
+ `_(earlier turns truncated; showing last ${MAX_CONVERSATION_LINES} lines)_`,
203
+ "",
204
+ );
205
+ return trimmed.join("\n");
206
+ }
207
+ return lines.join("\n");
208
+ }
209
+
210
+ function eventToCompactLine(ev) {
211
+ const t = ev.event_type || ev.hook_event_name || "?";
212
+ const tool = ev.tool_name || "";
213
+ const input = ev.tool_input || {};
214
+ const target =
215
+ input.file_path ||
216
+ input.command ||
217
+ input.pattern ||
218
+ input.url ||
219
+ input.description ||
220
+ "";
221
+ const ts = new Date(ev._ts || Date.now()).toISOString().slice(11, 19);
222
+ let label = t;
223
+ if (tool) label += ` ${tool}`;
224
+ if (target) label += ` ${String(target).slice(0, 80)}`;
225
+ return ` ${ts} ${label}`;
226
+ }
227
+
228
+ function gitState(cwd) {
229
+ return new Promise((resolve) => {
230
+ const out = { branch: "", status: "", log: "" };
231
+ let pending = 3;
232
+ const done = () => {
233
+ if (--pending === 0) resolve(out);
234
+ };
235
+ const run = (args, key) => {
236
+ const child = spawn("git", args, { cwd });
237
+ let buf = "";
238
+ child.stdout.on("data", (d) => (buf += d));
239
+ child.on("close", () => {
240
+ out[key] = buf.trim();
241
+ done();
242
+ });
243
+ child.on("error", () => done());
244
+ };
245
+ run(["branch", "--show-current"], "branch");
246
+ run(["status", "--short"], "status");
247
+ run(["log", "--oneline", "-5"], "log");
248
+ });
249
+ }
250
+
251
+ function buildPrompt(template, ctx) {
252
+ let out = template;
253
+ for (const [k, v] of Object.entries(ctx)) {
254
+ out = out.split(`{{${k}}}`).join(v ?? "");
255
+ }
256
+ return out;
257
+ }
258
+
259
+ function runClaudeP(prompt) {
260
+ return new Promise((resolve) => {
261
+ const child = spawn(
262
+ "claude",
263
+ ["-p", "--bare", "--model", MODEL, "--max-budget-usd", MAX_BUDGET_USD],
264
+ { stdio: ["pipe", "pipe", "pipe"] },
265
+ );
266
+ let stdout = "";
267
+ let stderr = "";
268
+ child.stdout.on("data", (d) => (stdout += d));
269
+ child.stderr.on("data", (d) => (stderr += d));
270
+ child.on("error", (err) =>
271
+ resolve({ ok: false, stdout, stderr, error: err.message }),
272
+ );
273
+ child.on("close", (code) =>
274
+ resolve({ ok: code === 0, stdout, stderr, code }),
275
+ );
276
+ child.stdin.write(prompt);
277
+ child.stdin.end();
278
+ });
279
+ }
280
+
281
+ let promptTemplate = null;
282
+ function loadPromptTemplate() {
283
+ if (promptTemplate) return promptTemplate;
284
+ try {
285
+ promptTemplate = fs.readFileSync(PROMPT_TEMPLATE_PATH, "utf-8");
286
+ } catch {
287
+ promptTemplate = null;
288
+ }
289
+ return promptTemplate;
290
+ }
291
+
292
+ async function processSession(session, broadcast) {
293
+ const { sid, cwd, events } = session;
294
+ if (!cwd || !sid) return;
295
+
296
+ const state = readAutoCheckpointState(cwd);
297
+ if (state.mode !== "on") return;
298
+ const intervalMs = state.interval_minutes * 60 * 1000;
299
+
300
+ const lastFile = path.join(
301
+ cwd,
302
+ ".uv-suite-state",
303
+ "sessions",
304
+ `${sid}.last-semantic-checkpoint.txt`,
305
+ );
306
+ const lastTs = parseInt(readStringSafe(lastFile) || "0", 10);
307
+ const now = Date.now();
308
+ if (now - lastTs * 1000 < intervalMs) return;
309
+
310
+ // Activity since last checkpoint
311
+ const recent = events
312
+ .filter((e) => (e._ts || 0) > lastTs * 1000)
313
+ .sort((a, b) => (a._ts || 0) - (b._ts || 0));
314
+ if (recent.length === 0) return;
315
+
316
+ // Find the Claude Code session id from the most recent event (it differs
317
+ // from uvs_session_id) and read the conversation transcript.
318
+ const ccSessionId =
319
+ [...recent].reverse().find((e) => e.session_id)?.session_id || null;
320
+ const transcriptPath = transcriptPathFor(cwd, ccSessionId);
321
+ const sinceMs = lastTs > 0 ? lastTs * 1000 : now - intervalMs;
322
+ const transcriptMessages = readTranscriptMessages(transcriptPath, sinceMs);
323
+
324
+ const conversationExtract =
325
+ buildConversationExtract(transcriptMessages) ||
326
+ "_(no transcript content found; only mechanical activity captured below)_";
327
+
328
+ // Mechanical breakdown — tool counts and files touched.
329
+ const toolCounts = {};
330
+ const fileCounts = {};
331
+ for (const e of recent) {
332
+ const t = e.tool_name;
333
+ if (t) toolCounts[t] = (toolCounts[t] || 0) + 1;
334
+ const fp = e.tool_input?.file_path;
335
+ if (fp && (t === "Edit" || t === "Write" || t === "Read")) {
336
+ fileCounts[fp] = (fileCounts[fp] || 0) + 1;
337
+ }
338
+ }
339
+ const mechanicalLines = [];
340
+ mechanicalLines.push("### Tool calls");
341
+ Object.entries(toolCounts)
342
+ .sort((a, b) => b[1] - a[1])
343
+ .slice(0, 8)
344
+ .forEach(([t, n]) => mechanicalLines.push(`- ${n}× ${t}`));
345
+ if (Object.keys(fileCounts).length) {
346
+ mechanicalLines.push("", "### Files touched");
347
+ Object.entries(fileCounts)
348
+ .sort((a, b) => b[1] - a[1])
349
+ .slice(0, 8)
350
+ .forEach(([f, n]) => mechanicalLines.push(`- ${f} (${n})`));
351
+ }
352
+ const mechanicalBlock = mechanicalLines.join("\n");
353
+
354
+ const git = await gitState(cwd);
355
+ const gitBlock = [
356
+ git.branch ? `**Branch:** ${git.branch}` : "_(not a git repo)_",
357
+ git.status
358
+ ? "**Status:**\n```\n" + git.status + "\n```"
359
+ : "**Status:** clean",
360
+ git.log ? "**Recent commits:**\n```\n" + git.log + "\n```" : "",
361
+ ]
362
+ .filter(Boolean)
363
+ .join("\n\n");
364
+
365
+ // Summary via claude -p, fed the actual conversation extract instead of
366
+ // just the event log. Falls back to a one-line stub if the call fails or
367
+ // the transcript is empty.
368
+ let summary = "";
369
+ const template = loadPromptTemplate();
370
+ if (template && transcriptMessages && transcriptMessages.length > 0) {
371
+ const elapsedMin =
372
+ lastTs === 0
373
+ ? state.interval_minutes
374
+ : Math.round((now - lastTs * 1000) / 60000);
375
+ const prompt = buildPrompt(template, {
376
+ name: session.session_name || "(unset)",
377
+ kind: session.session_kind || "(unset)",
378
+ priority: session.session_priority || "(unset)",
379
+ persona: session.persona || "(unset)",
380
+ purpose: session.session_purpose || "(unset)",
381
+ elapsed_min: String(elapsedMin),
382
+ interval_min: String(state.interval_minutes),
383
+ conversation: conversationExtract,
384
+ mechanical: mechanicalBlock,
385
+ git_branch: git.branch || "(not a git repo)",
386
+ git_status: git.status || "(no changes)",
387
+ git_log: git.log || "",
388
+ timestamp: new Date(now).toISOString(),
389
+ });
390
+ const result = await runClaudeP(prompt);
391
+ if (result.ok && result.stdout.trim()) {
392
+ summary = result.stdout.trim();
393
+ } else {
394
+ console.warn(
395
+ `[auto-checkpoint] summary call failed for ${sid.slice(0, 8)}:`,
396
+ result.error || result.stderr?.slice(0, 200) || `exit ${result.code}`,
397
+ );
398
+ }
399
+ }
400
+ if (!summary) {
401
+ summary = transcriptMessages
402
+ ? "_(summary generation failed; raw conversation below)_"
403
+ : "_(no conversation transcript available; only mechanical activity below)_";
404
+ }
405
+
406
+ // Write the checkpoint file
407
+ const cpDir = path.join(cwd, "uv-out", "checkpoints", sid);
408
+ fs.mkdirSync(cpDir, { recursive: true });
409
+ const tsFile = new Date(now)
410
+ .toISOString()
411
+ .slice(0, 16)
412
+ .replace(/[T:]/g, "-")
413
+ .replace(/-(\d\d)$/, "$1");
414
+ const cpFile = path.join(cpDir, `auto-${tsFile}-semantic.md`);
415
+
416
+ const frontmatter = [
417
+ "---",
418
+ `uvs_session_id: ${sid}`,
419
+ `session_name: ${session.session_name || ""}`,
420
+ `session_kind: ${session.session_kind || ""}`,
421
+ `session_purpose: ${session.session_purpose || ""}`,
422
+ `session_priority: ${session.session_priority || ""}`,
423
+ `persona: ${session.persona || ""}`,
424
+ `checkpoint_at: ${new Date(now).toISOString()}`,
425
+ `checkpoint_kind: auto-semantic`,
426
+ `transcript_messages: ${transcriptMessages ? transcriptMessages.length : 0}`,
427
+ `tool_calls_in_window: ${recent.length}`,
428
+ "---",
429
+ "",
430
+ ].join("\n");
431
+
432
+ const body = [
433
+ `# Auto-checkpoint (semantic): ${new Date(now).toISOString()}`,
434
+ "",
435
+ "## Summary",
436
+ "",
437
+ summary,
438
+ "",
439
+ "## Conversation",
440
+ "",
441
+ conversationExtract,
442
+ "",
443
+ "## Mechanical",
444
+ "",
445
+ mechanicalBlock,
446
+ "",
447
+ "## Git",
448
+ "",
449
+ gitBlock,
450
+ "",
451
+ ].join("\n");
452
+
453
+ fs.writeFileSync(cpFile, frontmatter + body);
454
+ fs.writeFileSync(lastFile, String(Math.floor(now / 1000)));
455
+
456
+ // Broadcast — the dashboard's expand-on-click body uses the summary.
457
+ const event = {
458
+ event_type: "AutoCheckpoint",
459
+ source_app: path.basename(cwd),
460
+ cwd,
461
+ uvs_session_id: sid,
462
+ session_id: sid,
463
+ session_name: session.session_name,
464
+ session_kind: session.session_kind,
465
+ session_priority: session.session_priority,
466
+ persona: session.persona,
467
+ checkpoint_kind: "auto-semantic",
468
+ checkpoint_path: cpFile,
469
+ checkpoint_summary: summary,
470
+ checkpoint_preview: (frontmatter + body).slice(0, 2000),
471
+ interval_minutes: state.interval_minutes,
472
+ tool_calls_in_window: recent.length,
473
+ transcript_messages: transcriptMessages ? transcriptMessages.length : 0,
474
+ _ts: now,
475
+ };
476
+ broadcast(event);
477
+ }
478
+
479
+ // One pass over all sessions with recent activity. Exposed so tests (and
480
+ // any future "force a checkpoint now" command) can drive a single tick.
481
+ async function tick({ getEvents, broadcast }) {
482
+ try {
483
+ const events = getEvents();
484
+ const window = 60 * 60 * 1000; // 1h lookback for active sessions
485
+ const sessions = groupActiveSessions(events, window);
486
+ for (const s of sessions) {
487
+ await processSession(s, broadcast);
488
+ }
489
+ } catch (err) {
490
+ console.warn("[auto-checkpoint] tick error:", err.message);
491
+ }
492
+ }
493
+
494
+ // Public API: start the runner. `getEvents` returns the watchtower's event
495
+ // store; `broadcast` injects an AutoCheckpoint event into the SSE stream.
496
+ // First tick after POLL_INTERVAL_MS; subsequent ticks every POLL_INTERVAL_MS.
497
+ function start({ getEvents, broadcast }) {
498
+ const handle = setInterval(
499
+ () => tick({ getEvents, broadcast }),
500
+ POLL_INTERVAL_MS,
501
+ );
502
+ return () => clearInterval(handle);
503
+ }
504
+
505
+ module.exports = { start, tick };