pi-bro 0.34.0 → 0.35.1

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
  }
package/src/settings.ts CHANGED
@@ -583,34 +583,39 @@ export async function writeSettings(settings: BroSettings): Promise<void> {
583
583
  }
584
584
  }
585
585
 
586
- export const PREFERENCES_FILE = join(getAgentDir(), "bro-preferences.md");
586
+ export function preferencesFile(): string {
587
+ return join(getAgentDir(), "bro-preferences.md");
588
+ }
589
+ export const PREFERENCES_FILE = preferencesFile();
587
590
  // Larger files are rejected before reading, even by the editor; the prompt limit is MAX_PREFERENCES_CHARS.
588
591
  export const MAX_PREFERENCES_FILE_BYTES = 64 * 1024;
589
592
 
590
593
  // Raw file text for the editor, undefined when there is no file: never rejects for length, so an
591
594
  // oversize file can be opened and trimmed.
592
595
  export async function readPreferencesRaw(): Promise<string | undefined> {
596
+ const file = preferencesFile();
593
597
  let size: number;
594
598
  try {
595
- size = (await stat(PREFERENCES_FILE)).size;
599
+ size = (await stat(file)).size;
596
600
  } catch (error) {
597
601
  if ((error as NodeJS.ErrnoException).code === "ENOENT") return undefined;
598
- throw new Error(`Cannot read ${PREFERENCES_FILE}: ${errorMessage(error)}`);
602
+ throw new Error(`Cannot read ${file}: ${errorMessage(error)}`);
599
603
  }
600
- if (size > MAX_PREFERENCES_FILE_BYTES) throw new Error(`${PREFERENCES_FILE} is larger than 64 KB. Edit or delete it directly.`);
604
+ if (size > MAX_PREFERENCES_FILE_BYTES) throw new Error(`${file} is larger than 64 KB. Edit or delete it directly.`);
601
605
  try {
602
- return (await readFile(PREFERENCES_FILE, "utf8")).replace(/^\uFEFF/, "");
606
+ return (await readFile(file, "utf8")).replace(/^\uFEFF/, "");
603
607
  } catch (error) {
604
- throw new Error(`Cannot read ${PREFERENCES_FILE}: ${errorMessage(error)}`);
608
+ throw new Error(`Cannot read ${file}: ${errorMessage(error)}`);
605
609
  }
606
610
  }
607
611
 
608
612
  // Preferences for prompts: "" when absent or blank. Re-read on every request. An oversize file stops
609
613
  // the request before any backend call instead of being silently truncated.
610
614
  export async function readPreferences(): Promise<string> {
615
+ const file = preferencesFile();
611
616
  const text = ((await readPreferencesRaw()) ?? "").trim();
612
617
  if (text.length > MAX_PREFERENCES_CHARS) {
613
- throw new Error(`${PREFERENCES_FILE} is ${text.length.toLocaleString("en-US")} characters; keep it under ${MAX_PREFERENCES_CHARS.toLocaleString("en-US")} (it's sent with every request). Use /bro preferences to trim it.`);
618
+ throw new Error(`${file} is ${text.length.toLocaleString("en-US")} characters; keep it under ${MAX_PREFERENCES_CHARS.toLocaleString("en-US")} (it's sent with every request). Use /bro preferences to trim it.`);
614
619
  }
615
620
  return text;
616
621
  }