Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | 1x 1x 1x 1x 1x 1x 12x 7x 12x 12x 5x 5x 1x 12x 12x 12x 12x 15x 12x 12x 12x 12x 15x 12x 12x 12x 12x 13x 13x 13x 5x 12x 15x 12x 12x 12x 12x 12x 1x 18x 3x 15x 1x 14x 2x 12x 1x 19x 1x 18x 18x 18x 18x 5x 8x 18x 1x 4x 4x 4x 2x 2x 2x 1x 2x 4x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x | /**
* progress-tracker.ts
*
* Extension: Progress Tracker
* Triggers: on_session_end
*
* Tracks weekly activity across skills and sessions.
* Surfaces burnout prevention nudges and weekly summaries.
*/
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { getWorkspaceState } from "../workspace-detector";
// ─── Types ─────────────────────────────────────────────────────────────────
export interface SessionRecord {
date: string; // ISO date string
durationMinutes: number;
skillsUsed: string[];
topicsWorked: string[];
}
export interface WeeklyStats {
totalMinutes: number;
sessionCount: number;
skillBreakdown: Record<string, number>;
longestSession: number;
streak: number; // days in a row with activity
}
// ─── Storage ───────────────────────────────────────────────────────────────
const TRACKER_DIR = path.join(os.homedir(), ".pisces");
const SESSIONS_FILE = path.join(TRACKER_DIR, "sessions.json");
function ensureTrackerDir(): void {
if (!fs.existsSync(TRACKER_DIR)) {
fs.mkdirSync(TRACKER_DIR, { recursive: true });
}
}
function loadSessions(): SessionRecord[] {
try {
if (!fs.existsSync(SESSIONS_FILE)) return [];
const raw = fs.readFileSync(SESSIONS_FILE, "utf-8");
return JSON.parse(raw) as SessionRecord[];
} catch {
return [];
}
}
function saveSessions(sessions: SessionRecord[]): void {
try {
ensureTrackerDir();
// Keep only last 90 days of sessions
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - 90);
const recent = sessions.filter((s) => new Date(s.date) > cutoff);
fs.writeFileSync(SESSIONS_FILE, JSON.stringify(recent, null, 2));
} catch {
// Fail silently — tracker is a nice-to-have, not critical
}
}
// ─── Stats ─────────────────────────────────────────────────────────────────
function getWeeklyStats(sessions: SessionRecord[]): WeeklyStats {
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
const weekSessions = sessions.filter(
(s) => new Date(s.date) > oneWeekAgo
);
const skillBreakdown: Record<string, number> = {};
let totalMinutes = 0;
let longestSession = 0;
for (const session of weekSessions) {
totalMinutes += session.durationMinutes;
longestSession = Math.max(longestSession, session.durationMinutes);
for (const skill of session.skillsUsed) {
skillBreakdown[skill] = (skillBreakdown[skill] ?? 0) + 1;
}
}
// Calculate streak
let streak = 0;
const sessionDays = new Set(sessions.map((s) => new Date(s.date).toDateString()));
const checkDate = new Date();
while (sessionDays.has(checkDate.toDateString())) {
streak++;
checkDate.setDate(checkDate.getDate() - 1);
}
return {
totalMinutes,
sessionCount: weekSessions.length,
skillBreakdown,
longestSession,
streak,
};
}
// ─── Nudges ─────────────────────────────────────────────────────────────────
export function getBurnoutNudge(
sessionDurationMinutes: number,
stats: WeeklyStats
): string | null {
// Long single session
if (sessionDurationMinutes >= 180) {
return "🐠 You've been coding for 3+ hours. Seriously — take a 15-minute break. Your brain consolidates learning during rest, not during grinding.";
}
// Very heavy week
if (stats.totalMinutes > 40 * 60) {
return "📊 You've put in 40+ hours of study this week. That's impressive, but rest is part of learning. Consistent rest is part of the process.";
}
// Long streak — positive reinforcement
if (stats.streak >= 7) {
return `🔥 ${stats.streak}-day streak! Consistent daily practice beats marathon sessions. Keep this up.`;
}
return null;
}
// ─── Weekly Summary ────────────────────────────────────────────────────────
export function buildWeeklySummary(stats: WeeklyStats): string {
if (stats.sessionCount === 0) {
return "📅 No activity recorded this week. When you're ready to start, just ask!";
}
const hours = Math.floor(stats.totalMinutes / 60);
const minutes = stats.totalMinutes % 60;
const timeStr = hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
const topSkills = Object.entries(stats.skillBreakdown)
.sort(([, a], [, b]) => b - a)
.slice(0, 3)
.map(([skill, count]) => ` - /${skill}: ${count} session${count > 1 ? "s" : ""}`)
.join("\n");
return [
`📊 This Week's Summary`,
``,
`⏱ Total study time: ${timeStr}`,
`📅 Sessions: ${stats.sessionCount}`,
`🔥 Current streak: ${stats.streak} day${stats.streak !== 1 ? "s" : ""}`,
topSkills ? `\n🛠 Most used skills:\n${topSkills}` : "",
``,
stats.totalMinutes < 5 * 60
? `💡 Tip: Even 30 minutes a day compounds significantly over a semester.`
: `✅ Solid week. Keep the consistency going.`,
]
.filter(Boolean)
.join("\n");
}
// ─── Pi Extension Factory ──────────────────────────────────────────────────
export default function (pi: ExtensionAPI) {
const sessionStart = Date.now();
const skillsUsed: string[] = [];
pi.on("input", async (event) => {
Iif (!getWorkspaceState().isActive) return { action: "continue" as const };
const match = event.text.match(/^\/skill:(\w+)/);
if (match && !skillsUsed.includes(match[1])) {
skillsUsed.push(match[1]);
}
return { action: "continue" as const };
});
pi.on("before_agent_start", async (_event, ctx) => {
Iif (!getWorkspaceState().isActive) return;
const elapsed = Math.round((Date.now() - sessionStart) / 60_000);
Iif (elapsed > 0 && elapsed % 180 === 0) {
ctx.ui.notify(
`🐠 You've been at this for ${elapsed} minutes. Take a short break — your brain consolidates learning during rest, not during grinding.`,
"info"
);
}
});
pi.on("session_shutdown", async (_event, ctx) => {
Iif (!getWorkspaceState().isActive) return;
const durationMinutes = Math.round((Date.now() - sessionStart) / 60_000);
const { nudge, weeklySummary } = run({ sessionDurationMinutes: durationMinutes, skillsUsed });
Iif (nudge) ctx.ui.notify(nudge, "info");
if (weeklySummary) ctx.ui.notify(weeklySummary, "info");
});
}
// ─── Pi Extension Entry Point ──────────────────────────────────────────────
export function run(context: {
sessionDurationMinutes: number;
skillsUsed: string[];
topicsWorked?: string[];
}): { nudge: string | null; weeklySummary: string } {
const sessions = loadSessions();
// Record this session
const record: SessionRecord = {
date: new Date().toISOString(),
durationMinutes: context.sessionDurationMinutes,
skillsUsed: context.skillsUsed,
topicsWorked: context.topicsWorked ?? [],
};
sessions.push(record);
saveSessions(sessions);
const stats = getWeeklyStats(sessions);
const nudge = getBurnoutNudge(context.sessionDurationMinutes, stats);
const weeklySummary = buildWeeklySummary(stats);
return { nudge, weeklySummary };
} |