pi-conversation-retro 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Claudio Reiter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # pi-conversation-retro
2
+
3
+ A [pi](https://github.com/badlogic/pi-mono) extension that runs automated postmortem reviews on your coding agent conversations. It identifies mistakes, analyzes root causes, and generates weekly improvement reports.
4
+
5
+ ## What it does
6
+
7
+ 1. **Discovers** recent pi session files related to the current repo (via `git rev-parse --show-toplevel`)
8
+ 2. **Skips** sessions that already have a summary markdown file
9
+ 3. **Spawns** one reviewer subagent per remaining session to analyze mistakes
10
+ 4. **Writes** one markdown summary per session
11
+ 5. **Synthesizes** all in-scope summaries into a workflow improvement report
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ pi install npm:pi-conversation-retro
17
+ # or
18
+ pi install git:github.com/c-reiter/pi-conversation-retro
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ In any pi session, run:
24
+
25
+ ```
26
+ /conversation-retro
27
+ ```
28
+
29
+ ### Options
30
+
31
+ | Flag | Short | Default | Description |
32
+ |------|-------|---------|-------------|
33
+ | `--days <n>` | `-d` | `7` | Number of days to look back |
34
+ | `--concurrency <n>` | `-c` | `10` | Max concurrent reviewer subagents |
35
+ | `--timeout <minutes>` | `-t` | `12` | Timeout per subagent (minutes) |
36
+ | `--output <path>` | `-o` | `.pi/reports/conversation-retro` | Output directory (absolute or repo-relative) |
37
+ | `--limit <n>` | `-l` | — | Cap newly analyzed conversations per run |
38
+ | `--dry-run` | — | — | Discover and count only, no subagents |
39
+
40
+ ### Examples
41
+
42
+ ```
43
+ /conversation-retro --days 14 --concurrency 4
44
+ /conversation-retro --dry-run
45
+ /conversation-retro --limit 5 --output reports/retro
46
+ ```
47
+
48
+ ## Output
49
+
50
+ All output goes to `.pi/reports/conversation-retro/` by default:
51
+
52
+ - **Per-conversation summaries:** `<session-file-name>.md` — mistake analysis for each session
53
+ - **Improvement report:** `workflow-improvement-report-<timestamp>.md` — synthesized patterns and action items
54
+ - **Latest report:** `workflow-improvement-report-latest.md` — always points to the most recent report
55
+
56
+ ### Per-conversation summary sections
57
+
58
+ - Snapshot
59
+ - What went wrong
60
+ - Root causes
61
+ - Recommended fixes
62
+ - Quick prevention checklist
63
+
64
+ ### Improvement report sections
65
+
66
+ - Executive summary
67
+ - Recurring failure patterns
68
+ - Process improvements
69
+ - Documentation/instruction improvements
70
+ - Repo/tooling structure improvements
71
+ - Prioritized action plan (next 7 days)
72
+ - Metrics to track
73
+
74
+ ## How it works
75
+
76
+ The extension registers a `/conversation-retro` slash command. When invoked, it:
77
+
78
+ 1. Finds all `.jsonl` session files under `~/.pi/agent/sessions/` whose `cwd` header points inside the current git repo
79
+ 2. Filters to sessions created within the `--days` window
80
+ 3. Skips sessions that already have a corresponding `.md` summary in the output directory
81
+ 4. Spawns pi subagents in print mode (`pi -p --no-session`) with read-only tools to analyze each session
82
+ 5. Runs the analyses concurrently (up to `--concurrency`) with per-agent timeouts
83
+ 6. Collects all summaries (including previously generated ones) and spawns a final reviewer subagent
84
+ 7. The reviewer synthesizes recurring patterns into an actionable improvement report
85
+
86
+ Progress is shown via a TUI widget and status bar during execution.
87
+
88
+ ## License
89
+
90
+ MIT
@@ -0,0 +1,693 @@
1
+ import type { ExtensionAPI, ExtensionCommandContext } from "@mariozechner/pi-coding-agent";
2
+ import { spawn } from "node:child_process";
3
+ import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
4
+ import * as os from "node:os";
5
+ import * as path from "node:path";
6
+
7
+ const COMMAND_NAME = "conversation-retro";
8
+ const STATUS_KEY = "conversation-retro-status";
9
+ const WIDGET_KEY = "conversation-retro-widget";
10
+
11
+ const DEFAULT_DAYS = 7;
12
+ const DEFAULT_CONCURRENCY = 10;
13
+ const DEFAULT_TIMEOUT_MINUTES = 12;
14
+ const DEFAULT_OUTPUT_DIR = ".pi/reports/conversation-retro";
15
+
16
+ type Phase = "discovering" | "analyzing" | "reviewing" | "done";
17
+
18
+ interface CommandOptions {
19
+ days: number;
20
+ concurrency: number;
21
+ timeoutMinutes: number;
22
+ outputDir: string;
23
+ limit?: number;
24
+ dryRun: boolean;
25
+ }
26
+
27
+ interface SessionHeader {
28
+ type?: string;
29
+ cwd?: string;
30
+ timestamp?: string;
31
+ }
32
+
33
+ interface ConversationCandidate {
34
+ sessionPath: string;
35
+ sessionFileName: string;
36
+ sessionCreatedAt: Date;
37
+ sessionCwd: string;
38
+ summaryPath: string;
39
+ }
40
+
41
+ interface AnalysisResult {
42
+ candidate: ConversationCandidate;
43
+ success: boolean;
44
+ error?: string;
45
+ }
46
+
47
+ interface ProgressState {
48
+ phase: Phase;
49
+ totalInScope: number;
50
+ totalToAnalyze: number;
51
+ totalSkippedExisting: number;
52
+ running: number;
53
+ finished: number;
54
+ succeeded: number;
55
+ failed: number;
56
+ reviewerDone: boolean;
57
+ reportPath?: string;
58
+ runningItems: string[];
59
+ outputDir: string;
60
+ }
61
+
62
+ interface RunPiResult {
63
+ stdout: string;
64
+ stderr: string;
65
+ exitCode: number;
66
+ killed: boolean;
67
+ }
68
+
69
+ function parseArgs(rawArgs: string | undefined): CommandOptions {
70
+ const options: CommandOptions = {
71
+ days: DEFAULT_DAYS,
72
+ concurrency: DEFAULT_CONCURRENCY,
73
+ timeoutMinutes: DEFAULT_TIMEOUT_MINUTES,
74
+ outputDir: DEFAULT_OUTPUT_DIR,
75
+ dryRun: false,
76
+ };
77
+
78
+ if (!rawArgs?.trim()) return options;
79
+
80
+ const parts = rawArgs.trim().split(/\s+/);
81
+ for (let i = 0; i < parts.length; i++) {
82
+ const part = parts[i];
83
+ const next = parts[i + 1];
84
+
85
+ if ((part === "--days" || part === "-d") && next) {
86
+ const parsed = Number.parseInt(next, 10);
87
+ if (Number.isFinite(parsed) && parsed > 0 && parsed <= 90) {
88
+ options.days = parsed;
89
+ }
90
+ i++;
91
+ continue;
92
+ }
93
+
94
+ if ((part === "--concurrency" || part === "-c") && next) {
95
+ const parsed = Number.parseInt(next, 10);
96
+ if (Number.isFinite(parsed) && parsed > 0 && parsed <= 16) {
97
+ options.concurrency = parsed;
98
+ }
99
+ i++;
100
+ continue;
101
+ }
102
+
103
+ if ((part === "--timeout" || part === "-t") && next) {
104
+ const parsed = Number.parseInt(next, 10);
105
+ if (Number.isFinite(parsed) && parsed > 0 && parsed <= 60) {
106
+ options.timeoutMinutes = parsed;
107
+ }
108
+ i++;
109
+ continue;
110
+ }
111
+
112
+ if ((part === "--output" || part === "-o") && next) {
113
+ options.outputDir = next;
114
+ i++;
115
+ continue;
116
+ }
117
+
118
+ if ((part === "--limit" || part === "-l") && next) {
119
+ const parsed = Number.parseInt(next, 10);
120
+ if (Number.isFinite(parsed) && parsed > 0) {
121
+ options.limit = parsed;
122
+ }
123
+ i++;
124
+ continue;
125
+ }
126
+
127
+ if (part === "--dry-run") {
128
+ options.dryRun = true;
129
+ continue;
130
+ }
131
+ }
132
+
133
+ return options;
134
+ }
135
+
136
+ function getAgentDir(): string {
137
+ const fromEnv = process.env.PI_CODING_AGENT_DIR?.trim();
138
+ if (fromEnv) return fromEnv;
139
+ return path.join(os.homedir(), ".pi", "agent");
140
+ }
141
+
142
+ function getSessionsBaseDir(): string {
143
+ return path.join(getAgentDir(), "sessions");
144
+ }
145
+
146
+ function isPathInside(child: string, parent: string): boolean {
147
+ const rel = path.relative(path.resolve(parent), path.resolve(child));
148
+ return rel === "" || (!rel.startsWith("..") && !path.isAbsolute(rel));
149
+ }
150
+
151
+ function collectSessionFilesRecursively(rootDir: string): string[] {
152
+ if (!existsSync(rootDir)) return [];
153
+
154
+ const out: string[] = [];
155
+ const stack = [rootDir];
156
+
157
+ while (stack.length > 0) {
158
+ const current = stack.pop()!;
159
+ let entries: ReturnType<typeof readdirSync>;
160
+ try {
161
+ entries = readdirSync(current, { withFileTypes: true });
162
+ } catch {
163
+ continue;
164
+ }
165
+
166
+ for (const entry of entries) {
167
+ const fullPath = path.join(current, entry.name);
168
+ if (entry.isDirectory()) {
169
+ if (entry.name === "subagent-artifacts") continue;
170
+ stack.push(fullPath);
171
+ continue;
172
+ }
173
+
174
+ if (entry.isFile() && entry.name.endsWith(".jsonl")) {
175
+ out.push(fullPath);
176
+ }
177
+ }
178
+ }
179
+
180
+ return out;
181
+ }
182
+
183
+ function parseCreatedAtFromSessionFileName(filePath: string): Date | undefined {
184
+ const base = path.basename(filePath, ".jsonl");
185
+ const timestampPart = base.split("_")[0];
186
+ const match = timestampPart.match(/^(\d{4}-\d{2}-\d{2})T(\d{2})-(\d{2})-(\d{2})-(\d{3})Z$/);
187
+ if (!match) return undefined;
188
+
189
+ const iso = `${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`;
190
+ const ms = Date.parse(iso);
191
+ if (!Number.isFinite(ms)) return undefined;
192
+ return new Date(ms);
193
+ }
194
+
195
+ function readSessionHeader(filePath: string): SessionHeader | null {
196
+ try {
197
+ const raw = readFileSync(filePath, "utf8");
198
+ const firstLine = raw.split(/\r?\n/, 1)[0];
199
+ if (!firstLine) return null;
200
+ const parsed = JSON.parse(firstLine) as SessionHeader;
201
+ if (parsed?.type !== "session" || typeof parsed.cwd !== "string") return null;
202
+ return parsed;
203
+ } catch {
204
+ return null;
205
+ }
206
+ }
207
+
208
+ function toOutputDir(repoRoot: string, outputArg: string): string {
209
+ if (path.isAbsolute(outputArg)) return outputArg;
210
+ return path.join(repoRoot, outputArg);
211
+ }
212
+
213
+ function ensureDir(dir: string): void {
214
+ if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
215
+ }
216
+
217
+ function truncateMiddle(input: string, max = 600): string {
218
+ if (input.length <= max) return input;
219
+ const half = Math.floor((max - 20) / 2);
220
+ return `${input.slice(0, half)}\n\n...[truncated]...\n\n${input.slice(-half)}`;
221
+ }
222
+
223
+ function buildAnalysisPrompt(candidate: ConversationCandidate): string {
224
+ return [
225
+ "You are a strict postmortem reviewer for Pi coding-agent conversations.",
226
+ "",
227
+ `Analyze this session JSONL file: ${candidate.sessionPath}`,
228
+ "",
229
+ "Goal: identify what went wrong and what concrete mistakes the agent made in this conversation.",
230
+ "Focus on misses, root causes, and improvements. Use evidence from the session (quotes/tool actions).",
231
+ "",
232
+ "Output ONLY markdown with these sections:",
233
+ "# Conversation Mistake Review",
234
+ "## Snapshot",
235
+ "## What went wrong",
236
+ "## Root causes",
237
+ "## Recommended fixes",
238
+ "## Quick prevention checklist",
239
+ "",
240
+ "Keep it practical and concise (max ~700 words).",
241
+ "Do not execute destructive commands. Read-only investigation only.",
242
+ ].join("\n");
243
+ }
244
+
245
+ function buildSummaryFileContent(candidate: ConversationCandidate, analysis: string): string {
246
+ const generatedAt = new Date().toISOString();
247
+ const header = [
248
+ `<!-- source_session: ${candidate.sessionPath} -->`,
249
+ `<!-- session_created_at: ${candidate.sessionCreatedAt.toISOString()} -->`,
250
+ `<!-- generated_at: ${generatedAt} -->`,
251
+ "",
252
+ ];
253
+ return `${header.join("\n")}${analysis.trim()}\n`;
254
+ }
255
+
256
+ function buildReviewerPrompt(summaryCount: number): string {
257
+ return [
258
+ "You are the reviewer agent for coding-workflow improvement.",
259
+ "",
260
+ `You are given ${summaryCount} conversation mistake summaries from the last week.`,
261
+
262
+ "Synthesize recurring problems and produce a concrete improvement report.",
263
+ "",
264
+ "Output ONLY markdown with these sections:",
265
+ "# Agentic Workflow Improvement Report",
266
+ "## Executive summary",
267
+ "## Recurring failure patterns",
268
+ "## Process improvements (team workflow)",
269
+ "## Documentation/instruction improvements",
270
+ "## Repo/tooling structure improvements",
271
+ "## Prioritized action plan (next 7 days)",
272
+ "## Metrics to track",
273
+ "",
274
+ "Be specific and opinionated. Avoid generic advice.",
275
+ ].join("\n");
276
+ }
277
+
278
+ function buildReviewerInputBundle(summaryPaths: string[]): string {
279
+ const lines: string[] = [];
280
+ lines.push("# Weekly conversation mistake summaries");
281
+ lines.push("");
282
+ lines.push(`Total summaries: ${summaryPaths.length}`);
283
+ lines.push(`Generated: ${new Date().toISOString()}`);
284
+ lines.push("");
285
+
286
+ for (const summaryPath of summaryPaths) {
287
+ let content = "";
288
+ try {
289
+ content = readFileSync(summaryPath, "utf8").trim();
290
+ } catch {
291
+ content = "[Could not read summary file]";
292
+ }
293
+
294
+ lines.push("---");
295
+ lines.push(`## ${path.basename(summaryPath)}`);
296
+ lines.push(`Path: ${summaryPath}`);
297
+ lines.push("");
298
+ lines.push(content.length > 0 ? content : "[Empty summary]");
299
+ lines.push("");
300
+ }
301
+
302
+ return `${lines.join("\n")}\n`;
303
+ }
304
+
305
+ function getTimestampTag(date = new Date()): string {
306
+ const pad = (n: number) => n.toString().padStart(2, "0");
307
+ return `${date.getUTCFullYear()}${pad(date.getUTCMonth() + 1)}${pad(date.getUTCDate())}-${pad(date.getUTCHours())}${pad(
308
+ date.getUTCMinutes(),
309
+ )}${pad(date.getUTCSeconds())}`;
310
+ }
311
+
312
+ function buildProgressLines(state: ProgressState): string[] {
313
+ const remaining = Math.max(0, state.totalToAnalyze - state.finished - state.running);
314
+ const phaseLabel =
315
+ state.phase === "discovering"
316
+ ? "discovering sessions"
317
+ : state.phase === "analyzing"
318
+ ? "running conversation reviewers"
319
+ : state.phase === "reviewing"
320
+ ? "running final reviewer"
321
+ : "complete";
322
+
323
+ const lines = [
324
+ "Conversation Retro",
325
+ `phase: ${phaseLabel}`,
326
+ `in scope: ${state.totalInScope} • existing summaries: ${state.totalSkippedExisting}`,
327
+ `finished: ${state.finished}/${state.totalToAnalyze} • running: ${state.running} • remaining: ${remaining}`,
328
+ `success: ${state.succeeded} • failed: ${state.failed}`,
329
+ `output: ${state.outputDir}`,
330
+ ];
331
+
332
+ if (state.runningItems.length > 0) {
333
+ const runningPreview = state.runningItems.slice(0, 3).join(", ");
334
+ lines.push(`active: ${runningPreview}${state.runningItems.length > 3 ? ` (+${state.runningItems.length - 3} more)` : ""}`);
335
+ }
336
+
337
+ if (state.phase === "done" && state.reportPath) {
338
+ lines.push(`report: ${state.reportPath}`);
339
+ }
340
+
341
+ return lines;
342
+ }
343
+
344
+ function renderProgress(ctx: ExtensionCommandContext, state: ProgressState): void {
345
+ if (!ctx.hasUI) return;
346
+ const lines = buildProgressLines(state);
347
+ const short = `retro ${state.finished}/${state.totalToAnalyze} done • ${state.running} running`;
348
+ ctx.ui.setStatus(STATUS_KEY, short);
349
+ ctx.ui.setWidget(WIDGET_KEY, lines);
350
+ }
351
+
352
+ function clearProgress(ctx: ExtensionCommandContext): void {
353
+ if (!ctx.hasUI) return;
354
+ ctx.ui.setStatus(STATUS_KEY, undefined);
355
+ }
356
+
357
+ async function runPiCommand(
358
+ args: string[],
359
+ cwd: string,
360
+ timeoutMs: number,
361
+ envAdditions?: Record<string, string>,
362
+ ): Promise<RunPiResult> {
363
+ return new Promise((resolve) => {
364
+ const env = {
365
+ ...process.env,
366
+ PI_SKIP_VERSION_CHECK: "1",
367
+ ...(envAdditions ?? {}),
368
+ };
369
+
370
+ const proc = spawn("pi", args, {
371
+ cwd,
372
+ shell: false,
373
+ stdio: ["ignore", "pipe", "pipe"],
374
+ env,
375
+ });
376
+
377
+ let stdout = "";
378
+ let stderr = "";
379
+ let killed = false;
380
+
381
+ const timeoutId = setTimeout(() => {
382
+ killed = true;
383
+ proc.kill("SIGTERM");
384
+ setTimeout(() => {
385
+ if (!proc.killed) proc.kill("SIGKILL");
386
+ }, 4000);
387
+ }, timeoutMs);
388
+
389
+ proc.stdout.on("data", (chunk) => {
390
+ stdout += chunk.toString();
391
+ });
392
+
393
+ proc.stderr.on("data", (chunk) => {
394
+ stderr += chunk.toString();
395
+ });
396
+
397
+ proc.on("close", (code) => {
398
+ clearTimeout(timeoutId);
399
+ resolve({ stdout, stderr, exitCode: code ?? 0, killed });
400
+ });
401
+
402
+ proc.on("error", (error) => {
403
+ clearTimeout(timeoutId);
404
+ resolve({ stdout, stderr: `${stderr}\n${String(error)}`.trim(), exitCode: 1, killed: true });
405
+ });
406
+ });
407
+ }
408
+
409
+ async function resolveRepoRoot(pi: ExtensionAPI, cwd: string): Promise<string> {
410
+ const result = await pi.exec("git", ["rev-parse", "--show-toplevel"], { cwd, timeout: 5000 });
411
+ if (result.code === 0 && result.stdout.trim()) return result.stdout.trim();
412
+ return cwd;
413
+ }
414
+
415
+ function getConversationCandidates(
416
+ repoRoot: string,
417
+ outputDir: string,
418
+ cutoffMs: number,
419
+ ): { candidates: ConversationCandidate[]; skippedExisting: number } {
420
+ const sessionsBase = getSessionsBaseDir();
421
+ const files = collectSessionFilesRecursively(sessionsBase);
422
+
423
+ const candidates: ConversationCandidate[] = [];
424
+ let skippedExisting = 0;
425
+
426
+ for (const filePath of files) {
427
+ const createdAt = parseCreatedAtFromSessionFileName(filePath) ?? new Date(statSync(filePath).mtimeMs);
428
+ if (createdAt.getTime() < cutoffMs) continue;
429
+
430
+ const header = readSessionHeader(filePath);
431
+ if (!header?.cwd) continue;
432
+ if (!isPathInside(header.cwd, repoRoot)) continue;
433
+
434
+ const sessionFileName = path.basename(filePath, ".jsonl");
435
+ const summaryPath = path.join(outputDir, `${sessionFileName}.md`);
436
+
437
+ if (existsSync(summaryPath)) {
438
+ skippedExisting++;
439
+ continue;
440
+ }
441
+
442
+ candidates.push({
443
+ sessionPath: filePath,
444
+ sessionFileName,
445
+ sessionCreatedAt: createdAt,
446
+ sessionCwd: header.cwd,
447
+ summaryPath,
448
+ });
449
+ }
450
+
451
+ candidates.sort((a, b) => a.sessionCreatedAt.getTime() - b.sessionCreatedAt.getTime());
452
+ return { candidates, skippedExisting };
453
+ }
454
+
455
+ async function analyzeConversation(
456
+ candidate: ConversationCandidate,
457
+ repoRoot: string,
458
+ timeoutMs: number,
459
+ ): Promise<AnalysisResult> {
460
+ const prompt = buildAnalysisPrompt(candidate);
461
+ const args = [
462
+ "-p",
463
+ "--no-session",
464
+ "--no-extensions",
465
+ "--no-skills",
466
+ "--no-prompt-templates",
467
+ "--tools",
468
+ "read,bash,grep,find,ls",
469
+ prompt,
470
+ ];
471
+
472
+ const result = await runPiCommand(args, repoRoot, timeoutMs);
473
+ if (result.exitCode !== 0 || result.killed) {
474
+ return {
475
+ candidate,
476
+ success: false,
477
+ error: truncateMiddle(result.stderr || result.stdout || `pi exited with code ${result.exitCode}`),
478
+ };
479
+ }
480
+
481
+ const text = result.stdout.trim();
482
+ if (!text) {
483
+ return {
484
+ candidate,
485
+ success: false,
486
+ error: "Subagent returned empty output",
487
+ };
488
+ }
489
+
490
+ try {
491
+ writeFileSync(candidate.summaryPath, buildSummaryFileContent(candidate, text), "utf8");
492
+ } catch (error) {
493
+ return {
494
+ candidate,
495
+ success: false,
496
+ error: `Failed writing summary: ${String(error)}`,
497
+ };
498
+ }
499
+
500
+ return { candidate, success: true };
501
+ }
502
+
503
+ async function runWithConcurrency<T, R>(
504
+ items: T[],
505
+ concurrency: number,
506
+ onItemStart: (item: T, index: number) => void,
507
+ onItemDone: (item: T, index: number, result: R) => void,
508
+ worker: (item: T, index: number) => Promise<R>,
509
+ ): Promise<R[]> {
510
+ if (items.length === 0) return [];
511
+
512
+ const safeConcurrency = Math.max(1, Math.min(concurrency, items.length));
513
+ const results = new Array<R>(items.length);
514
+ let cursor = 0;
515
+
516
+ const loops = new Array(safeConcurrency).fill(null).map(async () => {
517
+ while (true) {
518
+ const index = cursor++;
519
+ if (index >= items.length) return;
520
+ const item = items[index];
521
+ onItemStart(item, index);
522
+ const result = await worker(item, index);
523
+ results[index] = result;
524
+ onItemDone(item, index, result);
525
+ }
526
+ });
527
+
528
+ await Promise.all(loops);
529
+ return results;
530
+ }
531
+
532
+ export default function (pi: ExtensionAPI) {
533
+ pi.registerCommand(COMMAND_NAME, {
534
+ description:
535
+ "Spawn one reviewer subagent per recent repo conversation, write per-conversation mistake summaries, then generate an improvement report",
536
+ handler: async (args, ctx) => {
537
+ const options = parseArgs(args);
538
+ const repoRoot = await resolveRepoRoot(pi, ctx.cwd);
539
+ const outputDir = toOutputDir(repoRoot, options.outputDir);
540
+ ensureDir(outputDir);
541
+
542
+ const progress: ProgressState = {
543
+ phase: "discovering",
544
+ totalInScope: 0,
545
+ totalToAnalyze: 0,
546
+ totalSkippedExisting: 0,
547
+ running: 0,
548
+ finished: 0,
549
+ succeeded: 0,
550
+ failed: 0,
551
+ reviewerDone: false,
552
+ runningItems: [],
553
+ outputDir,
554
+ };
555
+
556
+ renderProgress(ctx, progress);
557
+
558
+ const cutoffMs = Date.now() - options.days * 24 * 60 * 60 * 1000;
559
+ const sessionsBase = getSessionsBaseDir();
560
+ const allRecentInRepo = collectSessionFilesRecursively(sessionsBase)
561
+ .map((filePath) => {
562
+ const created = parseCreatedAtFromSessionFileName(filePath) ?? new Date(statSync(filePath).mtimeMs);
563
+ const header = readSessionHeader(filePath);
564
+ return { filePath, created, header };
565
+ })
566
+ .filter((row) => row.created.getTime() >= cutoffMs && row.header?.cwd && isPathInside(row.header.cwd, repoRoot));
567
+
568
+ const { candidates, skippedExisting } = getConversationCandidates(repoRoot, outputDir, cutoffMs);
569
+ const limitedCandidates = options.limit ? candidates.slice(0, options.limit) : candidates;
570
+
571
+ progress.totalInScope = allRecentInRepo.length;
572
+ progress.totalSkippedExisting = skippedExisting;
573
+ progress.totalToAnalyze = limitedCandidates.length;
574
+ progress.phase = "analyzing";
575
+ renderProgress(ctx, progress);
576
+
577
+ if (ctx.hasUI) {
578
+ const limitSuffix = options.limit ? ` (limit: ${options.limit})` : "";
579
+ ctx.ui.notify(
580
+ `conversation retro: ${progress.totalInScope} in scope, ${progress.totalToAnalyze} to analyze, ${progress.totalSkippedExisting} already summarized${limitSuffix}`,
581
+ "info",
582
+ );
583
+ }
584
+
585
+ if (options.dryRun) {
586
+ progress.phase = "done";
587
+ renderProgress(ctx, progress);
588
+ clearProgress(ctx);
589
+ if (ctx.hasUI) {
590
+ ctx.ui.notify("conversation retro dry run complete (no subagents were started)", "info");
591
+ }
592
+ return;
593
+ }
594
+
595
+ const timeoutMs = options.timeoutMinutes * 60 * 1000;
596
+
597
+ const results = await runWithConcurrency(
598
+ limitedCandidates,
599
+ options.concurrency,
600
+ (item) => {
601
+ progress.running++;
602
+ progress.runningItems.push(item.sessionFileName);
603
+ renderProgress(ctx, progress);
604
+ },
605
+ (item, _index, result) => {
606
+ progress.running = Math.max(0, progress.running - 1);
607
+ progress.finished++;
608
+ progress.runningItems = progress.runningItems.filter((name) => name !== item.sessionFileName);
609
+ if (result.success) progress.succeeded++;
610
+ else progress.failed++;
611
+ renderProgress(ctx, progress);
612
+ },
613
+ (item) => analyzeConversation(item, repoRoot, timeoutMs),
614
+ );
615
+
616
+ const failed = results.filter((r) => !r.success);
617
+ if (failed.length > 0 && ctx.hasUI) {
618
+ ctx.ui.notify(`conversation retro: ${failed.length} subagents failed`, "warning");
619
+ }
620
+
621
+ const summaryPathsInScope = allRecentInRepo
622
+ .map((row) => path.join(outputDir, `${path.basename(row.filePath, ".jsonl")}.md`))
623
+ .filter((summaryPath) => existsSync(summaryPath));
624
+
625
+ if (summaryPathsInScope.length === 0) {
626
+ progress.phase = "done";
627
+ renderProgress(ctx, progress);
628
+ if (ctx.hasUI) ctx.ui.notify("conversation retro: no summaries available for review", "warning");
629
+ clearProgress(ctx);
630
+ return;
631
+ }
632
+
633
+ progress.phase = "reviewing";
634
+ renderProgress(ctx, progress);
635
+
636
+ const tempBundlePath = path.join(os.tmpdir(), `pi-conversation-retro-${Date.now()}-${Math.random().toString(36).slice(2)}.md`);
637
+ writeFileSync(tempBundlePath, buildReviewerInputBundle(summaryPathsInScope), "utf8");
638
+
639
+ const reviewerPrompt = buildReviewerPrompt(summaryPathsInScope.length);
640
+ const reviewerArgs = [
641
+ "-p",
642
+ "--no-session",
643
+ "--no-extensions",
644
+ "--no-skills",
645
+ "--no-prompt-templates",
646
+ "--no-tools",
647
+ `@${tempBundlePath}`,
648
+ reviewerPrompt,
649
+ ];
650
+
651
+ const reviewerResult = await runPiCommand(reviewerArgs, repoRoot, timeoutMs);
652
+ if (reviewerResult.exitCode !== 0 || reviewerResult.killed || !reviewerResult.stdout.trim()) {
653
+ const reason = truncateMiddle(
654
+ reviewerResult.stderr || reviewerResult.stdout || `Reviewer subagent exited with code ${reviewerResult.exitCode}`,
655
+ );
656
+ if (ctx.hasUI) ctx.ui.notify(`conversation retro reviewer failed: ${reason}`, "error");
657
+ progress.phase = "done";
658
+ renderProgress(ctx, progress);
659
+ clearProgress(ctx);
660
+ return;
661
+ }
662
+
663
+ const reportTag = getTimestampTag();
664
+ const reportPath = path.join(outputDir, `workflow-improvement-report-${reportTag}.md`);
665
+ const latestReportPath = path.join(outputDir, "workflow-improvement-report-latest.md");
666
+ writeFileSync(reportPath, reviewerResult.stdout.trim() + "\n", "utf8");
667
+ writeFileSync(latestReportPath, reviewerResult.stdout.trim() + "\n", "utf8");
668
+
669
+ progress.phase = "done";
670
+ progress.reviewerDone = true;
671
+ progress.reportPath = reportPath;
672
+ renderProgress(ctx, progress);
673
+ clearProgress(ctx);
674
+
675
+ if (ctx.hasUI) {
676
+ const failPreview = failed.slice(0, 3).map((f) => `${f.candidate.sessionFileName}: ${f.error ?? "unknown error"}`);
677
+ const failSuffix = failed.length > 3 ? `\n... +${failed.length - 3} more failures` : "";
678
+ ctx.ui.notify(
679
+ [
680
+ `conversation retro complete`,
681
+ `summaries analyzed this run: ${progress.succeeded}/${progress.totalToAnalyze}`,
682
+ `summaries considered by reviewer: ${summaryPathsInScope.length}`,
683
+ `report: ${reportPath}`,
684
+ failed.length > 0 ? `failures:\n${failPreview.join("\n")}${failSuffix}` : undefined,
685
+ ]
686
+ .filter(Boolean)
687
+ .join("\n"),
688
+ failed.length > 0 ? "warning" : "info",
689
+ );
690
+ }
691
+ },
692
+ });
693
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "pi-conversation-retro",
3
+ "version": "0.1.0",
4
+ "description": "Pi extension that runs automated postmortem reviews on your coding agent conversations, identifying mistakes and generating weekly improvement reports",
5
+ "type": "module",
6
+ "keywords": [
7
+ "pi",
8
+ "pi-package",
9
+ "coding-agent",
10
+ "extensions",
11
+ "retrospective",
12
+ "postmortem",
13
+ "conversation-review",
14
+ "workflow-improvement"
15
+ ],
16
+ "pi": {
17
+ "extensions": [
18
+ "./extensions/index.ts"
19
+ ]
20
+ },
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/c-reiter/pi-conversation-retro.git"
24
+ },
25
+ "bugs": {
26
+ "url": "https://github.com/c-reiter/pi-conversation-retro/issues"
27
+ },
28
+ "homepage": "https://github.com/c-reiter/pi-conversation-retro#readme",
29
+ "license": "MIT",
30
+ "author": "Claudio Reiter",
31
+ "peerDependencies": {
32
+ "@mariozechner/pi-coding-agent": "*",
33
+ "@mariozechner/pi-ai": "*",
34
+ "@mariozechner/pi-tui": "*",
35
+ "@sinclair/typebox": "*"
36
+ },
37
+ "files": [
38
+ "extensions/",
39
+ "README.md",
40
+ "LICENSE"
41
+ ]
42
+ }