pi-bro 0.34.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/src/review-ui.ts CHANGED
@@ -35,9 +35,18 @@ export const REVIEW_ACQUIRE_OVERLAY = {
35
35
  margin: { top: 1, bottom: 1 },
36
36
  } as const;
37
37
 
38
- const activeReviews = new Map<string, AbortController | undefined>();
39
- export function stopGuidedReviews(): void {
40
- for (const controller of activeReviews.values()) controller?.abort();
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?.()));
41
50
  }
42
51
 
43
52
  /** Same picker for resume and delete. Presentation and ordering have one owner. */
@@ -138,7 +147,7 @@ export async function openGuidedReview(
138
147
  return;
139
148
  }
140
149
  // Reserve this identity while deletion runs so a new review cannot start writing it.
141
- activeReviews.set(id, undefined);
150
+ activeReviews.set(id, {});
142
151
  try {
143
152
  await deleteReview(root, id);
144
153
  ctx.ui.notify(`Deleted saved review ${title} and its captured source.`, "info");
@@ -195,7 +204,7 @@ export async function openGuidedReview(
195
204
  );
196
205
  }
197
206
  locked = id;
198
- activeReviews.set(id, controller);
207
+ activeReviews.set(id, { controller });
199
208
  const existing = await loadReview(root, id);
200
209
  if (existing) {
201
210
  if (
@@ -276,7 +285,7 @@ export async function openGuidedReview(
276
285
  ctx.ui.notify("This review is already open in this Pi session.", "info");
277
286
  return;
278
287
  }
279
- activeReviews.set(state.id, undefined);
288
+ activeReviews.set(state.id, {});
280
289
  let overlayHandle: { setHidden(hidden: boolean): void } | undefined;
281
290
  try {
282
291
  await openBroOverlay<void>(
@@ -357,7 +366,7 @@ export async function openGuidedReview(
357
366
  let generated = false;
358
367
  const abort = new AbortController();
359
368
  controller = abort;
360
- activeReviews.set(state.id, abort);
369
+ activeReviews.get(state.id)!.controller = abort;
361
370
  panel.busy = true;
362
371
  panel.operation = question ? "answer" : regenerate ? "regenerate" : "guide";
363
372
  panel.startedAt = Date.now();
@@ -470,7 +479,7 @@ export async function openGuidedReview(
470
479
  } finally {
471
480
  clearInterval(heartbeat);
472
481
  controller = undefined;
473
- if (activeReviews.has(state.id)) activeReviews.set(state.id, undefined);
482
+ if (activeReviews.has(state.id)) activeReviews.get(state.id)!.controller = undefined;
474
483
  panel.activeTurn = undefined;
475
484
  panel.stage = undefined;
476
485
  panel.busy = false;
@@ -495,6 +504,19 @@ export async function openGuidedReview(
495
504
  tui.requestRender();
496
505
  }
497
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
+ };
498
520
  const panel = new ReviewModal(state, tui, theme, {
499
521
  ask(target, question) {
500
522
  running = run(target, question);
@@ -629,7 +651,7 @@ export async function openGuidedReview(
629
651
  },
630
652
  );
631
653
  } finally {
632
- activeReviews.get(state.id)?.abort();
654
+ activeReviews.get(state.id)?.controller?.abort();
633
655
  activeReviews.delete(state.id);
634
656
  }
635
657
  }