A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Windows/Linux: weekly recap aggregation in local time
Claude43c13ed2de9a
diff --git a/windows/src/recap/summary.ts b/windows/src/recap/summary.tsnew file mode 100644index 00000000..b62cc8d0--- /dev/null+++ b/windows/src/recap/summary.ts@@ -0,0 +1,251 @@+// Weekly recap — pure aggregation, port of RecapStore.weeklySummary.+// Rust (src-tauri/src/recap.rs) records the turns; this file turns them into+// one week's numbers. It lives on the JavaScript side because weeks, days and+// "Monday 8 am" are local time, and the webview knows the user's time zone.+// No DOM, no Tauri: tests/recap.test.mjs runs it as it is.++/** One finished turn, as recap.rs stores it. Times are Unix seconds. */+export interface RecapTurn {+ agent: string;+ project: string;+ start: number;+ end: number;+ filesChanged: number;+ linesAdded: number;+ linesRemoved: number;+ commandsRun: number;+ questions: number;+}++export interface RecapDecision {+ agent: string;+ date: number;+ decision: string;+}++export interface RecapPrefs {+ enabled: boolean;+ hideProjects: boolean;+ /** Monday (YYYY-MM-DD) of the week the recap last opened on its own. */+ lastShownWeek: string;+}++export interface RecapHistory {+ turns: RecapTurn[];+ decisions: RecapDecision[];+ prefs: RecapPrefs;+}++export interface WeeklySummary {+ weekStart: Date;+ /** Last second of the week (Sunday 23:59:59). */+ weekEnd: Date;+ totalMinutes: number;+ sessionCount: number;+ filesChanged: number;+ linesAdded: number;+ linesRemoved: number;+ commandsRun: number;+ questions: number;+ permissionsAllowed: number;+ permissionsDenied: number;+ /** Display name. */+ topAgent: string | null;+ topProject: string | null;+ busiestDay: string | null;+ longestSessionMinutes: number;+}++export const DEFAULT_PREFS: RecapPrefs = { enabled: true, hideProjects: false, lastShownWeek: "" };++// ── Weeks (ISO 8601: Monday to Sunday, local time) ────────────────────────────++/** Local midnight `n` days after `d`'s date — DST-safe, unlike adding ms. */+export function addDays(d: Date, n: number): Date {+ return new Date(d.getFullYear(), d.getMonth(), d.getDate() + n);+}++/** Monday 00:00 (local) of the week holding `d`. */+export function mondayOf(d: Date): Date {+ const sinceMonday = (d.getDay() + 6) % 7;+ return addDays(d, -sinceMonday);+}++/** Monday 00:00 of the last completed week. */+export function previousWeekStart(now: Date): Date {+ return addDays(mondayOf(now), -7);+}++/** YYYY-MM-DD of a local date. */+export function dayKey(d: Date): string {+ const p = (n: number) => String(n).padStart(2, "0");+ return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;+}++/** The Monday recap opens on its own once per week, from 8 am on. */+export const RECAP_HOUR = 8;++export function shouldAutoShow(now: Date, lastShownWeek: string): boolean {+ return now.getDay() === 1 && now.getHours() >= RECAP_HOUR && dayKey(mondayOf(now)) !== lastShownWeek;+}++// ── Aggregation ───────────────────────────────────────────────────────────────++const DAY_NAMES = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];++/** Pill IDs → names, from PillCatalog.swift. Claude Code hooks come from any+ * terminal here, so its pill reads "Claude Code" rather than "VS Code". */+const AGENT_NAMES: Record<string, string> = {+ integration_claude: "Claude Code",+ agent_cursor: "Cursor",+ agent_antigravity: "Antigravity",+ agent_codex: "Codex",+ agent_gemini: "Gemini CLI",+ agent_copilot: "Copilot CLI",+ agent_muse: "Muse Code",+ agent_opencode: "OpenCode",+ agent_amp: "Amp",+ agent_hermes: "Hermes",+};++export function agentName(id: string): string {+ const known = AGENT_NAMES[id];+ if (known) return known;+ const raw = id.replace(/^agent_/, "");+ return raw ? raw.charAt(0).toUpperCase() + raw.slice(1) : id;+}++/** The most frequent key; ties go to whatever `order` ranks first. */+function topKey<K>(counts: Map<K, number>, order: (a: K, b: K) => number): K | null {+ let best: K | null = null;+ let bestCount = 0;+ for (const [key, n] of counts) {+ if (n > bestCount || (n === bestCount && best !== null && order(key, best) < 0)) {+ best = key;+ bestCount = n;+ }+ }+ return best;+}++/** Wall time covered by the turns, overlapping ones counted once. Seconds. */+export function mergedSeconds(turns: RecapTurn[]): number {+ const sorted = turns.filter((t) => t.end > t.start).sort((a, b) => a.start - b.start);+ let total = 0;+ let segStart = 0;+ let segEnd = -Infinity;+ for (const t of sorted) {+ if (t.start <= segEnd) {+ segEnd = Math.max(segEnd, t.end);+ } else {+ if (segEnd > segStart) total += segEnd - segStart;+ segStart = t.start;+ segEnd = t.end;+ }+ }+ if (segEnd > segStart) total += segEnd - segStart;+ return total;+}++/** The week starting at `weekStart` (a local Monday 00:00), or null when nothing ran. */+export function summarize(history: RecapHistory, weekStart: Date): WeeklySummary | null {+ const end = addDays(weekStart, 7);+ const from = weekStart.getTime() / 1000;+ const to = end.getTime() / 1000;+ const turns = history.turns.filter((t) => t.start >= from && t.start < to);+ if (turns.length === 0) return null;+ const decisions = history.decisions.filter((d) => d.date >= from && d.date < to);+ const sum = (f: (t: RecapTurn) => number) => turns.reduce((n, t) => n + f(t), 0);++ const byAgent = new Map<string, number>();+ const byProject = new Map<string, number>();+ const byDay = new Map<number, number>();+ for (const t of turns) {+ byAgent.set(t.agent, (byAgent.get(t.agent) ?? 0) + 1);+ if (t.project) byProject.set(t.project, (byProject.get(t.project) ?? 0) + 1);+ const day = new Date(t.start * 1000).getDay();+ byDay.set(day, (byDay.get(day) ?? 0) + 1);+ }+ const byName = (a: string, b: string) => a.localeCompare(b);+ // Ties go to the earlier day of the (Monday-first) week.+ const byWeekOrder = (a: number, b: number) => ((a + 6) % 7) - ((b + 6) % 7);+ const topAgent = topKey(byAgent, byName);+ const busiest = topKey(byDay, byWeekOrder);+ const longest = Math.max(0, ...turns.map((t) => t.end - t.start));++ return {+ weekStart,+ weekEnd: new Date(end.getTime() - 1000),+ totalMinutes: Math.floor(mergedSeconds(turns) / 60),+ sessionCount: turns.length,+ filesChanged: sum((t) => t.filesChanged),+ linesAdded: sum((t) => t.linesAdded),+ linesRemoved: sum((t) => t.linesRemoved),+ commandsRun: sum((t) => t.commandsRun),+ questions: sum((t) => t.questions),+ permissionsAllowed: decisions.filter((d) => d.decision === "allow" || d.decision === "always").length,+ permissionsDenied: decisions.filter((d) => d.decision === "deny").length,+ topAgent: topAgent === null ? null : agentName(topAgent),+ topProject: topKey(byProject, byName),+ busiestDay: busiest === null ? null : DAY_NAMES[busiest],+ longestSessionMinutes: Math.floor(longest / 60),+ };+}++// ── Formatting ────────────────────────────────────────────────────────────────++export function formatDuration(minutes: number): string {+ if (minutes < 60) return `${minutes}m`;+ const h = Math.floor(minutes / 60);+ const m = minutes % 60;+ return m === 0 ? `${h}h` : `${h}h ${m}m`;+}++/** 1234 → "1,234"; 12345 → "12.3k" — keeps a stat chip narrow. */+export function formatCount(n: number): string {+ if (n >= 10_000) return `${(n / 1000).toFixed(n >= 100_000 ? 0 : 1)}k`;+ return n.toLocaleString("en-US");+}++const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];++/** "Sep 28 – Oct 4". */+export function weekRangeLabel(s: Pick<WeeklySummary, "weekStart" | "weekEnd">): string {+ const f = (d: Date) => `${MONTHS[d.getMonth()]} ${d.getDate()}`;+ return `${f(s.weekStart)} – ${f(s.weekEnd)}`;+}++// ── Sample week ───────────────────────────────────────────────────────────────++/**+ * The fake week of scripts/test-weekly-recap.swift, starting on `monday`.+ * Used by the tests and by `npm run dev` in a plain browser, where there is+ * no Rust side to read a history from.+ */+export function sampleHistory(monday: Date): RecapHistory {+ const at = (day: number, hour: number, minute = 0) =>+ Math.floor(new Date(monday.getFullYear(), monday.getMonth(), monday.getDate() + day, hour, minute).getTime() / 1000);+ const turn = (+ agent: string, project: string, start: number, end: number,+ filesChanged: number, linesAdded: number, linesRemoved: number, commandsRun: number, questions: number,+ ): RecapTurn => ({ agent, project, start, end, filesChanged, linesAdded, linesRemoved, commandsRun, questions });+ const claude = "integration_claude";+ return {+ turns: [+ turn(claude, "coucou", at(0, 9), at(0, 11, 30), 8, 312, 87, 14, 2),+ turn(claude, "coucou", at(0, 14), at(0, 15, 45), 3, 95, 20, 5, 0),+ turn("agent_gemini", "side-project", at(1, 10), at(1, 11), 2, 50, 10, 3, 1),+ turn(claude, "coucou", at(2, 9, 30), at(2, 12), 5, 180, 60, 8, 3),+ turn(claude, "coucou", at(3, 16), at(3, 17), 1, 40, 5, 2, 0),+ turn(claude, "coucou", at(4, 8), at(4, 13), 12, 540, 130, 22, 5),+ ],+ decisions: [+ { agent: claude, date: at(0, 9, 30), decision: "allow" },+ { agent: claude, date: at(0, 10), decision: "allow" },+ { agent: claude, date: at(2, 10), decision: "deny" },+ { agent: claude, date: at(4, 9), decision: "always" },+ { agent: claude, date: at(4, 10), decision: "allow" },+ ],+ prefs: { ...DEFAULT_PREFS },+ };+}diff --git a/windows/tests/recap.test.mjs b/windows/tests/recap.test.mjsnew file mode 100644index 00000000..1570ffb7--- /dev/null+++ b/windows/tests/recap.test.mjs@@ -0,0 +1,168 @@+// Weekly recap aggregation (src/recap/summary.ts) — the numbers the card and+// the shared image show. Mirrors the fake week of scripts/test-weekly-recap.swift.+// Dates are built with local constructors, so the tests hold in any time zone.++import { test } from "node:test";+import assert from "node:assert/strict";+import {+ addDays, dayKey, formatCount, formatDuration, mergedSeconds, mondayOf,+ previousWeekStart, sampleHistory, shouldAutoShow, summarize, weekRangeLabel,+} from "../src/recap/summary.ts";++const secs = (d) => Math.floor(d.getTime() / 1000);+const turn = (start, end, extra = {}) => ({+ agent: "integration_claude", project: "coucou",+ start: secs(start), end: secs(end),+ filesChanged: 0, linesAdded: 0, linesRemoved: 0, commandsRun: 0, questions: 0,+ ...extra,+});+const history = (turns, decisions = []) => ({+ turns, decisions, prefs: { enabled: true, hideProjects: false, lastShownWeek: "" },+});++// Monday 28 September 2026 → Sunday 4 October; "now" is Monday 5 October.+const LAST_MONDAY = new Date(2026, 8, 28);+const NOW = new Date(2026, 9, 5, 9, 15);++// ── Week boundaries ───────────────────────────────────────────────────────────++test("the last completed week runs from the previous Monday to Sunday, local time", () => {+ assert.equal(dayKey(previousWeekStart(NOW)), "2026-09-28");+ // Any moment of the current week points at the same previous week.+ assert.equal(dayKey(previousWeekStart(new Date(2026, 9, 5, 0, 0, 1))), "2026-09-28");+ assert.equal(dayKey(previousWeekStart(new Date(2026, 9, 11, 23, 59))), "2026-09-28");+ // Sunday still belongs to the week that began six days earlier.+ assert.equal(dayKey(mondayOf(new Date(2026, 9, 4, 22))), "2026-09-28");+ // Across a month and a year.+ assert.equal(dayKey(mondayOf(new Date(2027, 0, 1))), "2026-12-28");+ assert.equal(previousWeekStart(NOW).getHours(), 0);+});++test("only turns that start inside the week count", () => {+ const s = summarize(history([+ turn(new Date(2026, 8, 27, 23, 0), new Date(2026, 8, 27, 23, 59)), // Sunday before+ turn(new Date(2026, 8, 28, 0, 0), new Date(2026, 8, 28, 0, 30)), // Monday 00:00+ turn(new Date(2026, 9, 4, 23, 50), new Date(2026, 9, 5, 0, 40)), // Sunday night, ends Monday+ turn(new Date(2026, 9, 5, 0, 0), new Date(2026, 9, 5, 1, 0)), // this Monday+ ]), LAST_MONDAY);+ assert.equal(s.sessionCount, 2);+ assert.equal(s.totalMinutes, 30 + 50);+ assert.equal(dayKey(s.weekStart), "2026-09-28");+ assert.equal(dayKey(s.weekEnd), "2026-10-04");+ assert.equal(weekRangeLabel(s), "Sep 28 – Oct 4");+});++test("a week with nothing in it has no summary", () => {+ assert.equal(summarize(history([]), LAST_MONDAY), null);+ const lastMonth = turn(new Date(2026, 8, 1, 9), new Date(2026, 8, 1, 10));+ assert.equal(summarize(history([lastMonth]), LAST_MONDAY), null);+});++// ── The Mac's fake week ───────────────────────────────────────────────────────++test("the sample week adds up like the Mac's", () => {+ const s = summarize(sampleHistory(LAST_MONDAY), LAST_MONDAY);+ assert.equal(s.totalMinutes, 150 + 105 + 60 + 150 + 60 + 300);+ assert.equal(formatDuration(s.totalMinutes), "13h 45m");+ assert.equal(s.sessionCount, 6);+ assert.equal(s.filesChanged, 31);+ assert.equal(s.linesAdded, 1217);+ assert.equal(s.linesRemoved, 312);+ assert.equal(s.commandsRun, 54);+ assert.equal(s.questions, 11);+ assert.equal(s.permissionsAllowed, 4);+ assert.equal(s.permissionsDenied, 1);+ assert.equal(s.topAgent, "Claude Code");+ assert.equal(s.topProject, "coucou");+ assert.equal(s.busiestDay, "Monday");+ assert.equal(s.longestSessionMinutes, 300);+});++// ── Details ───────────────────────────────────────────────────────────────────++test("parallel sessions are not counted twice in the time spent", () => {+ const d = (h, m = 0) => new Date(2026, 8, 29, h, m);+ const turns = [+ turn(d(9), d(10)),+ turn(d(9, 30), d(11)), // overlaps the first+ turn(d(10, 15), d(10, 45)), // inside both+ turn(d(14), d(14, 20)),+ ];+ assert.equal(mergedSeconds(turns), (120 + 20) * 60);+ const s = summarize(history(turns), LAST_MONDAY);+ assert.equal(s.totalMinutes, 140);+ assert.equal(s.longestSessionMinutes, 90);+ assert.equal(s.sessionCount, 4);+});++test("top agent and top project go by number of sessions", () => {+ const d = (day, h) => new Date(2026, 8, 28 + day, h);+ const s = summarize(history([+ turn(d(0, 9), d(0, 15), { agent: "integration_claude", project: "big-but-once" }),+ turn(d(1, 9), d(1, 10), { agent: "agent_gemini", project: "korus" }),+ turn(d(1, 11), d(1, 12), { agent: "agent_gemini", project: "korus" }),+ turn(d(2, 9), d(2, 10), { agent: "agent_my-tool", project: "" }),+ turn(d(2, 11), d(2, 12), { agent: "agent_my-tool", project: "" }),+ turn(d(2, 13), d(2, 14), { agent: "agent_my-tool", project: "" }),+ ]), LAST_MONDAY);+ assert.equal(s.topAgent, "My-tool");+ // Sessions without a folder never make a top project.+ assert.equal(s.topProject, "korus");+ assert.equal(s.busiestDay, "Wednesday");+ assert.equal(s.longestSessionMinutes, 360);+});++test("ties are settled the same way every time", () => {+ const d = (day, h) => new Date(2026, 8, 28 + day, h);+ const s = summarize(history([+ turn(d(6, 9), d(6, 10), { agent: "agent_codex", project: "zeta" }), // Sunday+ turn(d(3, 9), d(3, 10), { agent: "integration_claude", project: "alpha" }), // Thursday+ ]), LAST_MONDAY);+ assert.equal(s.topAgent, "Codex"); // agent_codex < integration_claude+ assert.equal(s.topProject, "alpha");+ assert.equal(s.busiestDay, "Thursday"); // earlier in the week than Sunday+});++test("decisions outside the week do not count", () => {+ const inWeek = secs(new Date(2026, 8, 30, 10));+ const s = summarize(history(+ [turn(new Date(2026, 8, 30, 9), new Date(2026, 8, 30, 11))],+ [+ { agent: "integration_claude", date: inWeek, decision: "allow" },+ { agent: "integration_claude", date: inWeek, decision: "deny" },+ { agent: "integration_claude", date: secs(new Date(2026, 9, 5, 10)), decision: "allow" },+ { agent: "integration_claude", date: secs(new Date(2026, 8, 20, 10)), decision: "deny" },+ ],+ ), LAST_MONDAY);+ assert.equal(s.permissionsAllowed, 1);+ assert.equal(s.permissionsDenied, 1);+});++test("a history older than the window shows nothing for last week", () => {+ // Rust prunes past 12 weeks; whatever it keeps from earlier weeks stays out.+ const twelveWeeksAgo = addDays(LAST_MONDAY, -7 * 12);+ const s = summarize(history([turn(twelveWeeksAgo, addDays(twelveWeeksAgo, 0.01))]), LAST_MONDAY);+ assert.equal(s, null);+ const old = summarize(history([turn(addDays(twelveWeeksAgo, 1), new Date(addDays(twelveWeeksAgo, 1).getTime() + 3600_000))]), twelveWeeksAgo);+ assert.equal(old.sessionCount, 1);+});++// ── When it opens on its own ──────────────────────────────────────────────────++test("the recap opens on Monday from 8 am, once a week", () => {+ assert.equal(shouldAutoShow(new Date(2026, 9, 5, 7, 59), ""), false);+ assert.equal(shouldAutoShow(new Date(2026, 9, 5, 8, 0), ""), true);+ assert.equal(shouldAutoShow(new Date(2026, 9, 5, 18, 0), "2026-09-28"), true);+ assert.equal(shouldAutoShow(new Date(2026, 9, 5, 18, 0), "2026-10-05"), false);+ assert.equal(shouldAutoShow(new Date(2026, 9, 6, 9, 0), ""), false); // Tuesday+ assert.equal(shouldAutoShow(new Date(2026, 9, 4, 9, 0), ""), false); // Sunday+});++test("numbers stay short enough for a stat chip", () => {+ assert.equal(formatDuration(0), "0m");+ assert.equal(formatDuration(59), "59m");+ assert.equal(formatDuration(120), "2h");+ assert.equal(formatCount(1217), "1,217");+ assert.equal(formatCount(12_345), "12.3k");+ assert.equal(formatCount(123_456), "123k");+});