@bismawy/pi-arnative 0.16.0 → 0.18.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.
Files changed (47) hide show
  1. package/README.md +25 -16
  2. package/extensions/index.ts +41 -0
  3. package/lib/active-clock.ts +91 -0
  4. package/lib/ansi.ts +40 -78
  5. package/lib/box.ts +49 -68
  6. package/lib/chatbox.ts +371 -0
  7. package/lib/color.ts +3 -31
  8. package/lib/compat.ts +0 -60
  9. package/lib/edit-diff.ts +199 -0
  10. package/{extensions → lib/features}/arnative.ts +96 -423
  11. package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -325
  12. package/{extensions → lib/features}/budget.ts +17 -242
  13. package/lib/features/footer.ts +2232 -0
  14. package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
  15. package/{extensions → lib/features}/model-heal.ts +5 -28
  16. package/lib/features/progress-group.ts +1557 -0
  17. package/{extensions → lib/features}/render-cache.ts +130 -158
  18. package/{extensions → lib/features}/timestamps.ts +132 -131
  19. package/{extensions → lib/features}/todos.ts +44 -158
  20. package/lib/features/tools.ts +1202 -0
  21. package/lib/features/ui-render-tweaks.ts +606 -0
  22. package/lib/features/usage.ts +786 -0
  23. package/lib/format.ts +22 -4
  24. package/lib/git-status.ts +164 -0
  25. package/lib/list-picker.ts +1 -127
  26. package/lib/model-heal.ts +70 -162
  27. package/lib/patch.ts +0 -63
  28. package/lib/preset-store.ts +17 -133
  29. package/lib/provider-store.ts +573 -0
  30. package/lib/provider.ts +52 -851
  31. package/lib/runtime.ts +209 -0
  32. package/lib/shortcuts.ts +8 -58
  33. package/lib/spinner.ts +88 -162
  34. package/lib/status-widget.ts +4 -30
  35. package/lib/todos.ts +20 -143
  36. package/lib/usage-pricing.ts +143 -0
  37. package/lib/usage-store.ts +44 -378
  38. package/package.json +5 -6
  39. package/CHANGELOG.md +0 -678
  40. package/extensions/animations.ts +0 -242
  41. package/extensions/footer.ts +0 -2580
  42. package/extensions/tools.ts +0 -2231
  43. package/extensions/ui-render-tweaks.ts +0 -1005
  44. package/extensions/usage-export.ts +0 -283
  45. package/extensions/usage.ts +0 -1406
  46. package/lib/check.ts +0 -21
  47. package/themes/gen-themes.mjs +0 -715
@@ -18,7 +18,7 @@
18
18
  * - When every child is known and settled, the container checks child state, then
19
19
  * reuses its cached `lines` and `mouseLayout` without rebuilding the combined array.
20
20
  * - Attaches at the `InteractiveMode.chatContainer` instance layer (not `Container.prototype`),
21
- * so it never collides with `section-headers.ts` or `ui-render-tweaks.ts` prototype
21
+ * so it never collides with `headers.ts` or `ui-render-tweaks.ts` prototype
22
22
  * patches and sits outside `tools.ts` / `timestamps.ts` render wrappers.
23
23
  */
24
24
  import {
@@ -32,30 +32,32 @@ import {
32
32
  type ExtensionAPI,
33
33
  } from "@earendil-works/pi-coding-agent";
34
34
  import { Text } from "@earendil-works/pi-tui";
35
- import { mkdtempSync, rmSync } from "node:fs";
36
- import { tmpdir } from "node:os";
37
- import { join } from "node:path";
38
- import { assert, isMain } from "../lib/check.ts";
39
- import { patchOnce } from "../lib/patch.ts";
40
- import { RENDER_CACHE_KEY, choiceSetting, loadChoice, resetPresetCache } from "../lib/preset-store.ts";
35
+ import { patchOnce } from "../patch.ts";
36
+ import { RENDER_CACHE_KEY, choiceSetting } from "../preset-store.ts";
37
+ import {
38
+ TOOL_BOX_SCROLL_HOLD_KEY as SCROLL_HOLD_KEY,
39
+ TOOL_BOX_SCROLL_OFFSET_KEY as SCROLL_OFFSET_KEY,
40
+ getRuntime,
41
+ } from "../runtime.ts";
42
+ import {
43
+ activeToolLayout,
44
+ isProgressBox,
45
+ onToolLayoutChange,
46
+ projectTranscriptChildren,
47
+ } from "./progress-group.ts";
41
48
 
42
49
  /** Stored exactly as the picker shows them; `"On"` is the default. */
43
50
  export const RENDER_CACHE_CHOICES = ["On", "Off"] as const;
44
51
  export type RenderCacheChoice = (typeof RENDER_CACHE_CHOICES)[number];
45
- export const DEFAULT_RENDER_CACHE: RenderCacheChoice = "On";
52
+ const DEFAULT_RENDER_CACHE: RenderCacheChoice = "On";
46
53
 
47
54
  const RENDER_CACHE_CONFIG_KEY = Symbol.for("pi-arnative.renderCache");
48
- const RENDER_CACHE_STATE_KEY = Symbol.for("pi-arnative.renderCacheState");
49
55
  const INSTANCE_WRAP_KEY = Symbol.for("pi-arnative.renderCacheWrapped");
50
56
  const INSTANCE_ORIG_KEY = Symbol.for("pi-arnative.renderCacheOriginals");
51
57
 
52
- // Shared symbols from sibling extensions so active animations / scroll-holds bypass cache.
53
- const THINKING_STATE_KEY = Symbol.for("pi-arnative.thinkingLabelState");
54
- const SCROLL_OFFSET_KEY = Symbol.for("pi-arnative.toolBoxScrollOffset");
55
- const SCROLL_HOLD_KEY = Symbol.for("pi-arnative.toolBoxScrollHold");
56
-
57
58
  interface ChildMeta {
58
59
  version: number;
60
+ interactionVersion: number;
59
61
  scrollOffset: number;
60
62
  expanded: boolean;
61
63
  }
@@ -65,6 +67,7 @@ interface ChildCacheEntry {
65
67
  rows: number;
66
68
  epoch: number;
67
69
  version: number;
70
+ interactionVersion: number;
68
71
  scrollOffset: number;
69
72
  expanded: boolean;
70
73
  lines: string[];
@@ -78,6 +81,7 @@ interface ContainerCacheEntry {
78
81
  hasExpanded: boolean;
79
82
  children: unknown[];
80
83
  childMeta: ChildMeta[];
84
+ parts: Array<readonly string[]>;
81
85
  lines: string[];
82
86
  mouseChildren: Array<{ component: unknown; height: number }>;
83
87
  }
@@ -96,7 +100,9 @@ const CACHEABLE_CHILD_PROTOTYPES = new Set<object>([
96
100
  ]);
97
101
 
98
102
  function isCacheableChild(child: unknown): boolean {
99
- return !!child && typeof child === "object" && CACHEABLE_CHILD_PROTOTYPES.has(Object.getPrototypeOf(child));
103
+ if (!child || typeof child !== "object") return false;
104
+ if (isProgressBox(child)) return true;
105
+ return CACHEABLE_CHILD_PROTOTYPES.has(Object.getPrototypeOf(child));
100
106
  }
101
107
 
102
108
  interface RenderCacheState {
@@ -109,9 +115,9 @@ interface RenderCacheState {
109
115
  }
110
116
 
111
117
  function getCacheState(): RenderCacheState {
112
- const g = globalThis as Record<symbol, unknown>;
113
- if (!g[RENDER_CACHE_STATE_KEY]) {
114
- g[RENDER_CACHE_STATE_KEY] = {
118
+ const rt = getRuntime();
119
+ if (!rt.renderCacheState) {
120
+ rt.renderCacheState = {
115
121
  epoch: 0,
116
122
  versions: new WeakMap(),
117
123
  childCache: new WeakMap(),
@@ -119,7 +125,7 @@ function getCacheState(): RenderCacheState {
119
125
  stats: { containerHits: 0, childHits: 0, childRenders: 0 },
120
126
  } satisfies RenderCacheState;
121
127
  }
122
- return g[RENDER_CACHE_STATE_KEY] as RenderCacheState;
128
+ return rt.renderCacheState as RenderCacheState;
123
129
  }
124
130
 
125
131
  const renderCacheSetting = choiceSetting(RENDER_CACHE_KEY, RENDER_CACHE_CHOICES, DEFAULT_RENDER_CACHE);
@@ -185,14 +191,13 @@ function terminalRows(): number {
185
191
  /** True when a transcript component is currently animating, streaming, or hold-scrolling. */
186
192
  export function isDynamicChild(child: unknown): boolean {
187
193
  if (!child || typeof child !== "object") return false;
194
+ if (isProgressBox(child)) return child.isDynamic();
188
195
  const c = child as Record<string | symbol, unknown>;
189
196
  if (c.isStreaming === true) return true;
190
197
  if (c.isPartial === true || c.argsPending === true) return true;
191
198
  if (c.status === "running") return true;
192
199
  if (c[SCROLL_HOLD_KEY] !== undefined) return true;
193
- const thinking = (globalThis as Record<symbol, unknown>)[THINKING_STATE_KEY] as
194
- | { active?: Set<object> }
195
- | undefined;
200
+ const thinking = getRuntime().thinkingLabelState;
196
201
  if (thinking?.active?.has(child)) return true;
197
202
  return false;
198
203
  }
@@ -202,7 +207,7 @@ type MarkedWrap = AnyFn & { [INSTANCE_WRAP_KEY]?: true };
202
207
 
203
208
  /**
204
209
  * Wrap an instance method without touching the prototype chain, so prototype-level
205
- * patches from sibling extensions (`tools.ts`, `timestamps.ts`, `section-headers.ts`,
210
+ * patches from sibling extensions (`tools.ts`, `timestamps.ts`, `headers.ts`,
206
211
  * `ui-render-tweaks.ts`) continue to run and reinstall cleanly across `/reload`.
207
212
  */
208
213
  function wrapInstanceMethod(
@@ -282,8 +287,11 @@ function attachChildHooks(child: unknown): void {
282
287
 
283
288
  export interface ChatContainerLike {
284
289
  children: unknown[];
290
+ /** Pi < 1.6.0 mouse layout cache. */
285
291
  mouseLayout?: { width: number; children: Array<{ component: unknown; height: number }> };
286
- render(width: number): string[];
292
+ /** Pi >= 1.6.0 child line + mouse layout cache (`renderChildLines`). */
293
+ childLines?: { width: number; children: unknown[]; parts: Array<readonly string[]>; lines: readonly string[] };
294
+ render(width: number): readonly string[];
287
295
  invalidate?: () => void;
288
296
  addChild?: (component: unknown) => void;
289
297
  removeChild?: (component: unknown) => void;
@@ -291,17 +299,78 @@ export interface ChatContainerLike {
291
299
  handleMouse?: (event: unknown) => unknown;
292
300
  }
293
301
 
302
+ /** 32-bit FNV-1a mix (odd multiplier = bijective mod 2^32, so early values never shift out). */
303
+ function mixHash(hash: number, value: number): number {
304
+ return Math.imul(hash ^ (value | 0), 0x01000193) | 0;
305
+ }
306
+
307
+ function childMetaOf(child: unknown, state: RenderCacheState): ChildMeta {
308
+ if (!child || typeof child !== "object") {
309
+ return { version: 0, interactionVersion: 0, scrollOffset: 0, expanded: false };
310
+ }
311
+ if (isProgressBox(child)) {
312
+ const items = child.getItems();
313
+ let hash = mixHash(0x811c9dc5 | 0, items.length);
314
+ let expanded = false;
315
+ for (const item of items) {
316
+ if (item && typeof item === "object") {
317
+ attachChildHooks(item);
318
+ const itemVer = state.versions.get(item) ?? 0;
319
+ const itemExp = Boolean((item as { expanded?: boolean }).expanded);
320
+ if (itemExp) expanded = true;
321
+ hash = mixHash(hash, itemVer);
322
+ hash = mixHash(hash, itemExp ? 1 : 0);
323
+ }
324
+ }
325
+ return {
326
+ version: hash,
327
+ interactionVersion: child.getInteractionVersion(),
328
+ scrollOffset: child.getScrollSignature(),
329
+ expanded: expanded || items.length > 6,
330
+ };
331
+ }
332
+ const cObj = child as Record<string | symbol, unknown>;
333
+ return {
334
+ version: state.versions.get(cObj) ?? 0,
335
+ interactionVersion: 0,
336
+ scrollOffset: Number(cObj[SCROLL_OFFSET_KEY] ?? 0),
337
+ expanded: Boolean(cObj.expanded),
338
+ };
339
+ }
340
+
341
+ function renderProjectedDirect(container: ChatContainerLike, children: readonly unknown[], width: number): string[] {
342
+ const lines: string[] = [];
343
+ const mouseChildren: Array<{ component: unknown; height: number }> = [];
344
+ const snapshot: unknown[] = new Array(children.length);
345
+ const parts: Array<readonly string[]> = new Array(children.length);
346
+ for (let i = 0; i < children.length; i++) {
347
+ const child = children[i] as { render?: (w: number) => readonly string[] } | null | undefined;
348
+ snapshot[i] = child;
349
+ const childLines = child && typeof child.render === "function" ? child.render(width) : [];
350
+ parts[i] = childLines;
351
+ mouseChildren.push({ component: child, height: childLines.length });
352
+ for (let j = 0; j < childLines.length; j++) lines.push(childLines[j]!);
353
+ }
354
+ container.mouseLayout = { width, children: mouseChildren };
355
+ container.childLines = { width, children: snapshot, parts, lines };
356
+ return lines;
357
+ }
358
+
294
359
  function renderCachedChatContainer(
295
360
  container: ChatContainerLike,
296
361
  width: number,
297
362
  callNextRender: (self: any, args: unknown[]) => unknown,
298
363
  ): string[] {
364
+ const rawChildren = Array.isArray(container.children) ? container.children : [];
365
+ const grouped = activeToolLayout() === "Grouped";
366
+ const children = grouped ? projectTranscriptChildren(rawChildren) : rawChildren;
367
+
299
368
  if (activeRenderCache() !== "On") {
300
- return callNextRender(container, [width]) as string[];
369
+ if (!grouped) return callNextRender(container, [width]) as string[];
370
+ return renderProjectedDirect(container, children, width);
301
371
  }
302
372
 
303
373
  const state = getCacheState();
304
- const children = Array.isArray(container.children) ? container.children : [];
305
374
  const rows = terminalRows();
306
375
  const containerVer = state.versions.get(container) ?? 0;
307
376
 
@@ -322,12 +391,13 @@ function renderCachedChatContainer(
322
391
  allClean = false;
323
392
  break;
324
393
  }
325
- const cObj = child && typeof child === "object" ? (child as Record<string | symbol, unknown>) : null;
394
+ const cur = childMetaOf(child, state);
326
395
  const meta = cached.childMeta[i]!;
327
396
  if (
328
- meta.version !== (cObj ? (state.versions.get(cObj) ?? 0) : 0) ||
329
- meta.scrollOffset !== (cObj ? Number(cObj[SCROLL_OFFSET_KEY] ?? 0) : 0) ||
330
- meta.expanded !== Boolean(cObj?.expanded)
397
+ meta.version !== cur.version ||
398
+ meta.interactionVersion !== cur.interactionVersion ||
399
+ meta.scrollOffset !== cur.scrollOffset ||
400
+ meta.expanded !== cur.expanded
331
401
  ) {
332
402
  allClean = false;
333
403
  break;
@@ -336,6 +406,7 @@ function renderCachedChatContainer(
336
406
  if (allClean) {
337
407
  state.stats.containerHits += 1;
338
408
  container.mouseLayout = { width, children: cached.mouseChildren };
409
+ container.childLines = { width, children: cached.children, parts: cached.parts, lines: cached.lines };
339
410
  return cached.lines;
340
411
  }
341
412
  }
@@ -343,27 +414,28 @@ function renderCachedChatContainer(
343
414
  const lines: string[] = [];
344
415
  const mouseChildren: Array<{ component: unknown; height: number }> = [];
345
416
  const snapshot: unknown[] = new Array(children.length);
417
+ const parts: Array<readonly string[]> = new Array(children.length);
346
418
  const childMeta: ChildMeta[] = new Array(children.length);
347
419
  let allChildrenCacheable = true;
348
420
  let hasExpanded = false;
349
421
 
350
422
  for (let i = 0; i < children.length; i++) {
351
- const child = children[i] as { render?: (w: number) => string[] } & Record<string | symbol, unknown>;
423
+ const child = children[i] as { render?: (w: number) => readonly string[] } & Record<string | symbol, unknown>;
352
424
  snapshot[i] = child;
353
425
  if (!child || typeof child.render !== "function") {
354
- childMeta[i] = { version: 0, scrollOffset: 0, expanded: false };
426
+ parts[i] = [];
427
+ childMeta[i] = { version: 0, interactionVersion: 0, scrollOffset: 0, expanded: false };
355
428
  continue;
356
429
  }
357
- attachChildHooks(child);
430
+ if (!isProgressBox(child)) attachChildHooks(child);
358
431
 
359
432
  const isObj = typeof child === "object";
360
- const ver = isObj ? (state.versions.get(child) ?? 0) : 0;
361
- const scrollOffset = isObj ? Number(child[SCROLL_OFFSET_KEY] ?? 0) : 0;
362
- const expanded = Boolean(isObj && child.expanded);
433
+ const curMeta = childMetaOf(child, state);
434
+ const { version: ver, interactionVersion, scrollOffset, expanded } = curMeta;
363
435
  if (expanded) hasExpanded = true;
364
- childMeta[i] = { version: ver, scrollOffset, expanded };
436
+ childMeta[i] = curMeta;
365
437
 
366
- let childLines: string[];
438
+ let childLines: readonly string[];
367
439
  if (!isCacheableChild(child) || isDynamicChild(child)) {
368
440
  allChildrenCacheable = false;
369
441
  state.stats.childRenders += 1;
@@ -376,6 +448,7 @@ function renderCachedChatContainer(
376
448
  (entry) =>
377
449
  entry.epoch === state.epoch &&
378
450
  entry.version === ver &&
451
+ entry.interactionVersion === interactionVersion &&
379
452
  entry.scrollOffset === scrollOffset &&
380
453
  entry.expanded === expanded &&
381
454
  (!expanded || entry.rows === rows),
@@ -396,13 +469,15 @@ function renderCachedChatContainer(
396
469
  rows,
397
470
  epoch: state.epoch,
398
471
  version: ver,
472
+ interactionVersion,
399
473
  scrollOffset,
400
474
  expanded,
401
- lines: childLines,
475
+ lines: [...childLines],
402
476
  },
403
477
  (entry) =>
404
478
  entry.epoch === state.epoch &&
405
479
  entry.version === ver &&
480
+ entry.interactionVersion === interactionVersion &&
406
481
  entry.scrollOffset === scrollOffset &&
407
482
  entry.expanded === expanded,
408
483
  );
@@ -410,6 +485,7 @@ function renderCachedChatContainer(
410
485
  }
411
486
  }
412
487
 
488
+ parts[i] = childLines;
413
489
  mouseChildren.push({ component: child, height: childLines.length });
414
490
  for (let j = 0; j < childLines.length; j++) {
415
491
  lines.push(childLines[j]!);
@@ -417,6 +493,7 @@ function renderCachedChatContainer(
417
493
  }
418
494
 
419
495
  container.mouseLayout = { width, children: mouseChildren };
496
+ container.childLines = { width, children: snapshot, parts, lines };
420
497
  if (allChildrenCacheable) {
421
498
  putWidthEntry(
422
499
  state.containerCache,
@@ -429,6 +506,7 @@ function renderCachedChatContainer(
429
506
  hasExpanded,
430
507
  children: snapshot,
431
508
  childMeta,
509
+ parts,
432
510
  lines,
433
511
  mouseChildren,
434
512
  },
@@ -470,6 +548,10 @@ export function attachChatContainer(container: unknown): void {
470
548
  }
471
549
  if (typeof obj.handleMouse === "function") {
472
550
  wrapInstanceMethod(obj, "handleMouse", (callNext) => function (this: any, ...args: unknown[]) {
551
+ const ev = args[0] as { width?: number } | undefined;
552
+ if (ev && typeof ev.width === "number" && this.childLines?.width !== ev.width && typeof this.render === "function") {
553
+ this.render(ev.width);
554
+ }
473
555
  const res = callNext(this, args);
474
556
  if (res !== undefined) bumpVersion(this);
475
557
  return res;
@@ -477,6 +559,8 @@ export function attachChatContainer(container: unknown): void {
477
559
  }
478
560
  }
479
561
 
562
+ onToolLayoutChange(clearRenderCache);
563
+
480
564
  // Install InteractiveMode hooks so `this.chatContainer` is attached on startup,
481
565
  // `/reload`, session switch, and any incoming chat/tool event.
482
566
  if (InteractiveMode?.prototype) {
@@ -485,6 +569,9 @@ if (InteractiveMode?.prototype) {
485
569
  "init",
486
570
  "handleReloadCommand",
487
571
  "renderSessionContext",
572
+ "renderSessionEntries",
573
+ "renderSessionItems",
574
+ "renderCurrentSessionState",
488
575
  "rebuildChatFromMessages",
489
576
  "addMessageToChat",
490
577
  "handleEvent",
@@ -511,122 +598,7 @@ export default function registerRenderCache(pi: ExtensionAPI): void {
511
598
  });
512
599
  }
513
600
 
514
- // Self-check: `node extensions/render-cache.ts`
515
- if (isMain(import.meta.url)) {
516
- const fixture = mkdtempSync(join(tmpdir(), "arnative-rendercache-"));
517
- process.env.PI_CODING_AGENT_DIR = fixture;
518
- delete (globalThis as Record<symbol, unknown>)[RENDER_CACHE_CONFIG_KEY];
519
- resetPresetCache();
520
-
521
- // Choice persistence & default.
522
- assert(activeRenderCache() === "On", "the default is On");
523
- setRenderCache("Off");
524
- assert(activeRenderCache() === "Off", "Off persists immediately");
525
- assert(loadChoice(RENDER_CACHE_KEY, RENDER_CACHE_CHOICES) === "Off", "Off survives reload from disk");
526
- setRenderCache("On");
527
- assert(activeRenderCache() === "On", "switching back to On restores caching");
528
-
529
- // Use a known Text prototype to exercise cache policy without loading a theme.
530
- function makeChild(text: string) {
531
- const child = Object.assign(new Text(text, 0, 0), { renders: 0, isStreaming: false, isPartial: false, expanded: false });
532
- const setText = child.setText.bind(child);
533
- child.setText = (next: string): void => { text = next; setText(next); };
534
- const result = Object.assign(child, {
535
- handleInput(key: string): void { child.setText(`${text}:${key}`); },
536
- });
537
- result.render = (width: number): string[] => { result.renders += 1; return [`${text}@${width}`]; };
538
- return result as typeof result & { [key: symbol]: unknown };
539
- }
540
- type FakeChild = ReturnType<typeof makeChild>;
541
- class FakeContainer implements ChatContainerLike {
542
- children: FakeChild[] = [];
543
- mouseLayout?: { width: number; children: Array<{ component: unknown; height: number }> };
544
- addChild(c: FakeChild): void {
545
- this.children.push(c);
546
- }
547
- invalidate(): void {
548
- for (const c of this.children) c.invalidate();
549
- }
550
- render(width: number): string[] {
551
- const lines: string[] = [];
552
- const mouseChildren: Array<{ component: unknown; height: number }> = [];
553
- for (const c of this.children) {
554
- const out = c.render(width);
555
- mouseChildren.push({ component: c, height: out.length });
556
- lines.push(...out);
557
- }
558
- this.mouseLayout = { width, children: mouseChildren };
559
- return lines;
560
- }
561
- }
562
-
563
- const container = new FakeContainer();
564
- const c1 = makeChild("msg1");
565
- const c2 = makeChild("msg2");
566
- container.addChild(c1);
567
- container.addChild(c2);
568
- attachChatContainer(container);
569
-
570
- const out1 = container.render(80);
571
- assert(out1.join(",") === "msg1@80,msg2@80", "first render outputs both children");
572
- assert(c1.renders === 1 && c2.renders === 1, "first render calls each child once");
573
-
574
- // Second render at same width hits whole container cache: zero child.render calls.
575
- const out2 = container.render(80);
576
- assert(out2 === out1, "whole-container hit returns cached array reference");
577
- assert(c1.renders === 1 && c2.renders === 1, "container hit skips child renders");
578
- assert(container.mouseLayout?.children.length === 2, "mouseLayout preserved on container hit");
579
-
580
- // Streaming child bypasses cache while settled sibling stays cached.
581
- c2.isStreaming = true;
582
- c2.setText("streaming");
583
- const out3 = container.render(80);
584
- assert(out3.join(",") === "msg1@80,streaming@80", "streaming child updates live");
585
- assert(c1.renders === 1 && c2.renders === 2, "settled sibling stayed cached while streaming child re-rendered");
586
-
587
- c2.isStreaming = false;
588
- const out4 = container.render(80);
589
- assert(out4.join(",") === "msg1@80,streaming@80", "post-stream render caches final state");
590
- assert(c1.renders === 1 && c2.renders === 3, "settled after stream renders once then caches");
591
- container.render(80);
592
- assert(c1.renders === 1 && c2.renders === 3, "steady state hits container cache again");
593
-
594
- // Width change renders unseen width; toggling back to previous width hits 2-width LRU in O(1).
595
- const out5 = container.render(100);
596
- assert(out5.join(",") === "msg1@100,streaming@100", "width change re-renders at new width");
597
- assert(c1.renders === 2 && c2.renders === 4, "both re-rendered for new width");
598
- const out5Back = container.render(80);
599
- assert(out5Back === out4, "toggling back to prior width hits 2-width container LRU");
600
- assert(c1.renders === 2 && c2.renders === 4, "toggling back skips child renders");
601
- container.render(100);
602
-
603
- // Mutation on one child invalidates only that child.
604
- c1.setText("edited");
605
- const out6 = container.render(100);
606
- assert(out6.join(",") === "edited@100,streaming@100", "mutated child re-renders");
607
- assert(c1.renders === 3 && c2.renders === 4, "unchanged sibling stayed cached");
608
-
609
- // handleInput mutation invalidates that child without needing explicit invalidate().
610
- c1.handleInput("x");
611
- const out6Input = container.render(100);
612
- assert(out6Input.join(",") === "edited:x@100,streaming@100", "handleInput invalidates mutated child");
613
- assert(c1.renders === 4 && c2.renders === 4, "handleInput did not invalidate sibling");
614
-
615
- // Tool box scroll-offset change invalidates without explicit invalidate().
616
- c2[SCROLL_OFFSET_KEY] = 5;
617
- container.render(100);
618
- assert(c1.renders === 4 && c2.renders === 5, "scrollOffset change invalidates that tool box");
619
-
620
- // Off mode disables caching completely.
621
- setRenderCache("Off");
622
- container.render(100);
623
- container.render(100);
624
- assert(c1.renders === 6 && c2.renders === 7, "Off mode renders directly every frame");
625
- setRenderCache("On");
626
-
627
- rmSync(fixture, { recursive: true, force: true });
628
- delete process.env.PI_CODING_AGENT_DIR;
629
- delete (globalThis as Record<symbol, unknown>)[RENDER_CACHE_CONFIG_KEY];
630
- resetPresetCache();
631
- console.log("render-cache.ts self-check OK");
632
- }
601
+ export const __test = {
602
+ RENDER_CACHE_CONFIG_KEY,
603
+ SCROLL_OFFSET_KEY,
604
+ };