pi-bro 0.33.0 → 0.35.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/CHANGELOG.md +19 -0
- package/README.md +2 -1
- package/docs/guided-review.md +25 -2
- package/docs/overthink.md +22 -4
- package/docs/reference/commands.md +2 -1
- package/package.json +2 -2
- package/src/backend.ts +247 -332
- package/src/bro.ts +9 -2
- package/src/btw.ts +4 -17
- package/src/doctor.ts +79 -234
- package/src/explain-modal.ts +5 -5
- package/src/grill.ts +64 -76
- package/src/overthink-questions-ui.ts +3 -3
- package/src/overthink-state.ts +80 -17
- package/src/overthink.ts +84 -33
- package/src/remember-store.ts +13 -7
- package/src/remember.ts +2 -8
- package/src/review-modal.ts +3 -0
- package/src/review-ui.ts +158 -30
- package/src/review.ts +9 -0
package/src/review-ui.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
replaceGuide,
|
|
15
15
|
guideEvidence,
|
|
16
16
|
buildReviewPrompt,
|
|
17
|
+
deleteReview,
|
|
17
18
|
listReviews,
|
|
18
19
|
loadReview,
|
|
19
20
|
parseGuide,
|
|
@@ -34,9 +35,74 @@ export const REVIEW_ACQUIRE_OVERLAY = {
|
|
|
34
35
|
margin: { top: 1, bottom: 1 },
|
|
35
36
|
} as const;
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
type ActiveReview = {
|
|
39
|
+
controller?: AbortController;
|
|
40
|
+
flush?: () => Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
const activeReviews = new Map<string, ActiveReview>();
|
|
43
|
+
|
|
44
|
+
// Pi awaits session_shutdown handlers before invalidating the outgoing session.
|
|
45
|
+
// Abort all review work first, then flush its existing serialized save queue.
|
|
46
|
+
export async function stopGuidedReviews(): Promise<void> {
|
|
47
|
+
const active = [...activeReviews.values()];
|
|
48
|
+
for (const review of active) review.controller?.abort();
|
|
49
|
+
await Promise.all(active.map((review) => review.flush?.()));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Same picker for resume and delete. Presentation and ordering have one owner. */
|
|
53
|
+
export function savedReviewLabel(item: ReviewRecord): string {
|
|
54
|
+
const target = item.snapshot.target;
|
|
55
|
+
const title = safeReviewText(target.title ?? "").replace(/[\r\n\t]/g, " ");
|
|
56
|
+
const readiness = item.guide ? (item.guide.assessment ? "review ready" : "guide ready") : "not prepared";
|
|
57
|
+
return `${target.repository} #${target.number} · ${title} · ${readiness} · ${target.head.slice(0, 8)}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async function savedReviews(ctx: ExtensionCommandContext, root: string): Promise<ReviewRecord[]> {
|
|
61
|
+
const records = await listReviews(root, (file, error) =>
|
|
62
|
+
ctx.ui.notify(`Cannot load ${file}: ${String(error)}. Original file left untouched.`, "warning"));
|
|
63
|
+
return records.sort((a, b) =>
|
|
64
|
+
a.snapshot.target.repository.localeCompare(b.snapshot.target.repository) ||
|
|
65
|
+
b.snapshot.target.number - a.snapshot.target.number);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function pickSavedReview(
|
|
69
|
+
ctx: ExtensionCommandContext, root: string, title: string, emptyMessage: string,
|
|
70
|
+
): Promise<ReviewRecord | undefined> {
|
|
71
|
+
const records = await savedReviews(ctx, root);
|
|
72
|
+
if (!records.length) {
|
|
73
|
+
ctx.ui.notify(emptyMessage, "info");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const labels = records.map(savedReviewLabel);
|
|
77
|
+
const picked = await ctx.ui.select(title, labels);
|
|
78
|
+
return picked ? records[labels.indexOf(picked)] : undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Resolve stored PR identity offline; a bare number is allowed only if unambiguous. */
|
|
82
|
+
export function matchSavedReview(
|
|
83
|
+
records: readonly ReviewRecord[], requested: string,
|
|
84
|
+
): { selected?: ReviewRecord; error?: string } {
|
|
85
|
+
const input = requested.trim();
|
|
86
|
+
if (/^[1-9]\d*$/.test(input)) {
|
|
87
|
+
const matches = records.filter(item => item.snapshot.target.number === Number(input));
|
|
88
|
+
if (matches.length > 1) return { error: "Multiple saved reviews have that PR number. Use its full PR URL." };
|
|
89
|
+
return { selected: matches[0] };
|
|
90
|
+
}
|
|
91
|
+
let url: URL;
|
|
92
|
+
try { url = new URL(input); }
|
|
93
|
+
catch { return { error: "Use a saved PR number or GitHub PR URL." }; }
|
|
94
|
+
const match = /^\/([^/]+)\/([^/]+)\/pull\/([1-9]\d*)(?:\/(?:files|commits|checks))?\/?$/.exec(url.pathname);
|
|
95
|
+
if (url.protocol !== "https:" || !match || url.username || url.password) {
|
|
96
|
+
return { error: "Use a saved PR number or GitHub PR URL." };
|
|
97
|
+
}
|
|
98
|
+
const [, owner, repo, number] = match;
|
|
99
|
+
const matches = records.filter(item => {
|
|
100
|
+
const target = item.snapshot.target;
|
|
101
|
+
return (target.host ?? "github.com").toLowerCase() === url.hostname.toLowerCase() &&
|
|
102
|
+
target.repository.toLowerCase() === `${owner}/${repo}`.toLowerCase() &&
|
|
103
|
+
target.number === Number(number);
|
|
104
|
+
});
|
|
105
|
+
return { selected: matches[0] };
|
|
40
106
|
}
|
|
41
107
|
|
|
42
108
|
export async function openGuidedReview(
|
|
@@ -49,31 +115,52 @@ export async function openGuidedReview(
|
|
|
49
115
|
return;
|
|
50
116
|
}
|
|
51
117
|
const root = join(getAgentDir(), "bro-reviews");
|
|
118
|
+
|
|
119
|
+
if (value === "delete" || value.startsWith("delete ")) {
|
|
120
|
+
const requested = value.slice("delete".length).trim();
|
|
121
|
+
let selected: ReviewRecord | undefined;
|
|
122
|
+
if (!requested) {
|
|
123
|
+
selected = await pickSavedReview(ctx, root, "Delete saved review", "There are no saved reviews to delete.");
|
|
124
|
+
if (!selected) return; // Esc is cancellation, not a failed target lookup.
|
|
125
|
+
} else {
|
|
126
|
+
const match = matchSavedReview(await savedReviews(ctx, root), requested);
|
|
127
|
+
if (match.error) {
|
|
128
|
+
ctx.ui.notify(match.error, "warning");
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
selected = match.selected;
|
|
132
|
+
}
|
|
133
|
+
if (!selected) {
|
|
134
|
+
ctx.ui.notify("No saved review matches that target.", "info");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const id = selected.id;
|
|
138
|
+
if (activeReviews.has(id)) {
|
|
139
|
+
ctx.ui.notify("That review is currently open or being prepared. Close it before deleting.", "warning");
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const title = `${selected.snapshot.target.repository} #${selected.snapshot.target.number}`;
|
|
143
|
+
const approved = await ctx.ui.confirm("Delete saved review?", `Permanently remove ${title}, its notes, unsent drafts and all captured Git source? This cannot be undone.`);
|
|
144
|
+
if (!approved) return;
|
|
145
|
+
if (activeReviews.has(id)) {
|
|
146
|
+
ctx.ui.notify("That review became active. Close it and retry deletion.", "warning");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
// Reserve this identity while deletion runs so a new review cannot start writing it.
|
|
150
|
+
activeReviews.set(id, {});
|
|
151
|
+
try {
|
|
152
|
+
await deleteReview(root, id);
|
|
153
|
+
ctx.ui.notify(`Deleted saved review ${title} and its captured source.`, "info");
|
|
154
|
+
} finally {
|
|
155
|
+
activeReviews.delete(id);
|
|
156
|
+
}
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
52
159
|
let record: ReviewRecord | undefined;
|
|
53
160
|
let fresh = false;
|
|
54
161
|
if (value === "resume" || !value) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
`Cannot load ${file}: ${String(error)}. Original file left untouched.`,
|
|
58
|
-
"warning",
|
|
59
|
-
),
|
|
60
|
-
);
|
|
61
|
-
if (!records.length) {
|
|
62
|
-
ctx.ui.notify("Start with /bro guided-review <PR number or URL>.", "info");
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
records.sort(
|
|
66
|
-
(a, b) =>
|
|
67
|
-
a.snapshot.target.repository.localeCompare(b.snapshot.target.repository) ||
|
|
68
|
-
b.snapshot.target.number - a.snapshot.target.number,
|
|
69
|
-
);
|
|
70
|
-
const labels = records.map(
|
|
71
|
-
(item) =>
|
|
72
|
-
`${item.snapshot.target.repository} #${item.snapshot.target.number} · ${safeReviewText(item.snapshot.target.title ?? "").replace(/[\r\n\t]/g, " ")} · ${item.guide ? (item.guide.assessment ? "review ready" : "guide ready") : "not prepared"} · ${item.snapshot.target.head.slice(0, 8)}`,
|
|
73
|
-
);
|
|
74
|
-
const picked = await ctx.ui.select("Resume guided review", labels);
|
|
75
|
-
if (!picked) return;
|
|
76
|
-
record = records[labels.indexOf(picked)];
|
|
162
|
+
record = await pickSavedReview(ctx, root, "Resume guided review", "Start with /bro guided-review <PR number or URL>.");
|
|
163
|
+
if (!record) return;
|
|
77
164
|
} else {
|
|
78
165
|
record = await openBroOverlay<ReviewRecord | undefined>(
|
|
79
166
|
ctx,
|
|
@@ -117,7 +204,7 @@ export async function openGuidedReview(
|
|
|
117
204
|
);
|
|
118
205
|
}
|
|
119
206
|
locked = id;
|
|
120
|
-
activeReviews.set(id, controller);
|
|
207
|
+
activeReviews.set(id, { controller });
|
|
121
208
|
const existing = await loadReview(root, id);
|
|
122
209
|
if (existing) {
|
|
123
210
|
if (
|
|
@@ -198,7 +285,7 @@ export async function openGuidedReview(
|
|
|
198
285
|
ctx.ui.notify("This review is already open in this Pi session.", "info");
|
|
199
286
|
return;
|
|
200
287
|
}
|
|
201
|
-
activeReviews.set(state.id,
|
|
288
|
+
activeReviews.set(state.id, {});
|
|
202
289
|
let overlayHandle: { setHidden(hidden: boolean): void } | undefined;
|
|
203
290
|
try {
|
|
204
291
|
await openBroOverlay<void>(
|
|
@@ -279,7 +366,7 @@ export async function openGuidedReview(
|
|
|
279
366
|
let generated = false;
|
|
280
367
|
const abort = new AbortController();
|
|
281
368
|
controller = abort;
|
|
282
|
-
activeReviews.
|
|
369
|
+
activeReviews.get(state.id)!.controller = abort;
|
|
283
370
|
panel.busy = true;
|
|
284
371
|
panel.operation = question ? "answer" : regenerate ? "regenerate" : "guide";
|
|
285
372
|
panel.startedAt = Date.now();
|
|
@@ -392,7 +479,7 @@ export async function openGuidedReview(
|
|
|
392
479
|
} finally {
|
|
393
480
|
clearInterval(heartbeat);
|
|
394
481
|
controller = undefined;
|
|
395
|
-
if (activeReviews.has(state.id)) activeReviews.
|
|
482
|
+
if (activeReviews.has(state.id)) activeReviews.get(state.id)!.controller = undefined;
|
|
396
483
|
panel.activeTurn = undefined;
|
|
397
484
|
panel.stage = undefined;
|
|
398
485
|
panel.busy = false;
|
|
@@ -417,6 +504,19 @@ export async function openGuidedReview(
|
|
|
417
504
|
tui.requestRender();
|
|
418
505
|
}
|
|
419
506
|
};
|
|
507
|
+
const active = activeReviews.get(state.id);
|
|
508
|
+
if (active) active.flush = async () => {
|
|
509
|
+
closing = true;
|
|
510
|
+
controller?.abort();
|
|
511
|
+
if (saveTimer) clearTimeout(saveTimer);
|
|
512
|
+
try {
|
|
513
|
+
await running;
|
|
514
|
+
} finally {
|
|
515
|
+
// A run can itself queue a save; this flush follows it in order.
|
|
516
|
+
await persist();
|
|
517
|
+
closed = true;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
420
520
|
const panel = new ReviewModal(state, tui, theme, {
|
|
421
521
|
ask(target, question) {
|
|
422
522
|
running = run(target, question);
|
|
@@ -427,6 +527,34 @@ export async function openGuidedReview(
|
|
|
427
527
|
retrySave() {
|
|
428
528
|
void persist().catch(() => {});
|
|
429
529
|
},
|
|
530
|
+
deleteReview() {
|
|
531
|
+
if (confirming || controller || closing || closed || panel.busy) return;
|
|
532
|
+
confirming = true;
|
|
533
|
+
overlayHandle?.setHidden(true);
|
|
534
|
+
const target = state.snapshot.target;
|
|
535
|
+
const title = `${target.repository} #${target.number}`;
|
|
536
|
+
void ctx.ui.confirm(
|
|
537
|
+
"Delete this saved review?",
|
|
538
|
+
`Permanently delete ${title}, all private questions and drafts, and its captured Git source? This cannot be undone.`,
|
|
539
|
+
).then(async (approved) => {
|
|
540
|
+
if (!approved || closed || closing || controller) return;
|
|
541
|
+
closing = true;
|
|
542
|
+
// Do not let a delayed save recreate the record after deletion.
|
|
543
|
+
if (saveTimer) { clearTimeout(saveTimer); saveTimer = undefined; }
|
|
544
|
+
await running;
|
|
545
|
+
await queue;
|
|
546
|
+
await deleteReview(root, state.id);
|
|
547
|
+
closed = true;
|
|
548
|
+
ctx.ui.notify(`Deleted saved review ${title} and its captured source.`, "info");
|
|
549
|
+
done();
|
|
550
|
+
}).catch((error) => {
|
|
551
|
+
closing = false;
|
|
552
|
+
panel.notice = `Deletion failed: ${String(error)}. Review remains open; retry when ready.`;
|
|
553
|
+
}).finally(() => {
|
|
554
|
+
confirming = false;
|
|
555
|
+
if (!closed) { overlayHandle?.setHidden(false); tui.requestRender(); }
|
|
556
|
+
});
|
|
557
|
+
},
|
|
430
558
|
regenerate() {
|
|
431
559
|
if (confirming || controller || closing || closed) return;
|
|
432
560
|
confirming = true;
|
|
@@ -523,7 +651,7 @@ export async function openGuidedReview(
|
|
|
523
651
|
},
|
|
524
652
|
);
|
|
525
653
|
} finally {
|
|
526
|
-
activeReviews.get(state.id)?.abort();
|
|
654
|
+
activeReviews.get(state.id)?.controller?.abort();
|
|
527
655
|
activeReviews.delete(state.id);
|
|
528
656
|
}
|
|
529
657
|
}
|
package/src/review.ts
CHANGED
|
@@ -150,6 +150,15 @@ function recordPath(root: string, id: string): string {
|
|
|
150
150
|
if (!/^[a-f0-9]{64}$/.test(id)) throw Error("Invalid review identity.");
|
|
151
151
|
return join(root, id + ".json");
|
|
152
152
|
}
|
|
153
|
+
/** Remove one validated saved record and its captured source, never other reviews.
|
|
154
|
+
* The controller owns the active-review lock; deletion is not atomic across files.
|
|
155
|
+
*/
|
|
156
|
+
export async function deleteReview(root: string, id: string): Promise<void> {
|
|
157
|
+
const record = recordPath(root, id); // validates the 64-character hex boundary
|
|
158
|
+
await rm(join(root, "source", id), { recursive: true, force: true });
|
|
159
|
+
await rm(record, { force: true });
|
|
160
|
+
}
|
|
161
|
+
|
|
153
162
|
export async function saveReview(root: string, record: ReviewRecord): Promise<void> {
|
|
154
163
|
const path = recordPath(root, record.id);
|
|
155
164
|
await mkdir(root, { recursive: true, mode: 0o700 });
|