repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

feat(decay): redesign compaction progress

01f499e17f286da7e09cceeeacad4df146bddbc8

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (6)

CHANGELOG.md

Modified
6 6 - Shorten model labels consistently in model status displays, including family-specific speed and release qualifiers
7 7 - Remove the cmprss, Leaks, and Verboten extensions and the dedicated Leaks fuzz task
8 8 - Remove the always-on Terse extension; configure personal response style through Pi's `APPEND_SYSTEM.md` instead
9 -- Improve Decay progress with neutral pending history, a combined history and retained-tail row, readable labels, and continuous merge animation with atom and streamed-token counts
9 +- Redesign Decay progress as a borderless five-row widget with aligned CTX, FACTS, and NEW maps, theme-safe semantic blocks, live durable/context classification, selected-memory highlighting, and summary growth on a shared retained-token scale
10 10 - Fix Decay retained-token estimates to measure the actual preserved suffix instead of the configured target, and show unknown counts when unavailable
11 11 - Show Ultra workflow phases and live prompt tails while arguments stream, hand off to the execution board when agents start, and retain full received prompts on Ctrl+O
12 12 - Fix Ultra failed tool calls to show an error summary instead of an empty progress board, with full diagnostics on Ctrl+O

extensions/decay/README.md

Modified
24 24 Repeated compactions provide the classifier a bounded prior-atom identity catalog so paraphrased facts can reuse exact recurrence keys without counting catalog entries as new occurrences.
26 -Interactive progress uses a passive fixed-height widget above Pi's editor, preserving native steering submission and Escape cancellation during compaction.
27 -History and the retained tail share one row, with contrasting labels over the block cells.
28 -Pending history is neutral; accepted records acquire source-age colors, and recurring keys are marked only after recurrence is observed across records.
29 -During merge, a looping summary blend indicates activity, not percentage complete.
30 -The widget reports the allocated atom count and an estimated output-token count driven by streamed summary text.
26 +Interactive progress uses a passive, borderless five-row widget above Pi's editor, preserving native steering submission and Escape cancellation during compaction.
27 +The aligned `CTX`, `FACTS`, and `NEW` maps use compact human-readable counts and reversed semantic colors so overlaid labels remain legible across themes.
28 +Classifier records replace muted pending cells with durable and context regions as they arrive.
29 +After reconciliation, selected canonical facts turn to the accent color before streamed summary cells grow at the start of `NEW`.
30 +`CTX` and `NEW` share a token scale, so the retained suffix keeps the same approximate width and follows the growing summary in the resulting context.
32 32 `keepRecentTokens` is Pi's configured target, not an exact count or minimum guarantee.
33 33 Pi chooses whole-entry boundaries, which can retain more or fewer tokens than the target.
36 36 `~` marks estimates; unavailable boundaries display `?`, never the configured target as a measured count.
38 38 The [original Decay plan](../../docs/super/plans/2026-08-22-decay-compaction.md) contains earlier visual artifacts.
39 -The current widget uses seven normal lines or four short-terminal lines; the single-row history and evidence-driven rendering replace that plan's eight-line design.
39 +The current widget uses five contiguous lines at every terminal height; aligned evidence-driven maps replace that plan's eight-line design.

extensions/decay/__tests__/integration.test.ts

Modified
254 254 render(width: number): string[];
255 255 dispose?(): void;
256 - state: { mergeAtoms: number; summaryTokens: number };
256 + state: {
257 + candidates: unknown[];
258 + selectedKeys: string[];
259 + summaryTokens: number;
260 + };
258 262 | undefined;
259 263 runner.createContext = () => ({
284 288 fg: (_color: string, text: string) => text,
285 289 bg: (_color: string, text: string) => text,
286 290 bold: (text: string) => text,
291 + inverse: (text: string) => text,
296 301 expect(result.compaction.summary).toContain("Widget path");
297 302 expect(widgetOptions).toEqual({ placement: "aboveEditor" });
298 303 expect(widgetCleared).toBe(true);
299 - expect(rendered).toHaveLength(7);
300 - expect(component?.state.mergeAtoms).toBe(1);
304 + expect(rendered).toHaveLength(5);
305 + expect(component?.state.candidates).toHaveLength(1);
306 + expect(component?.state.selectedKeys).toHaveLength(1);
301 307 expect(component?.state.summaryTokens).toBeGreaterThan(0);
302 - expect(rendered.join("\n")).toContain("DECAY.EXE");
303 - expect(rendered.join("\n")).toContain("Enter queues steer");
308 + expect(rendered.join("\n")).toContain("DECAY");
309 + expect(rendered.join("\n")).toContain("↵ queues a message");
304 310 expect(custom).not.toHaveBeenCalled();
305 311 expect(setEditorText).not.toHaveBeenCalled();
306 312 expect(terminalWrite).not.toHaveBeenCalled();

extensions/decay/__tests__/ui.test.ts

Modified
1 1 import { describe, expect, it, vi } from "vitest";
2 +import type { MemoryAtom, RecordChunk } from "../core";
2 3 import {
3 4 DecayComponent,
4 5 type DecayDisplayState,
13 14 keptTokens: 24_000,
14 15 totalChunks: 8,
15 16 phase: "classify",
16 - transfers: [],
17 + records: [],
17 18 startedAt: 1_000,
18 19 ...overrides,
22 -const completedTransfers: DecayDisplayState["transfers"] = Array.from(
23 - { length: 8 },
24 - (_, index) => ({
25 - chunk: index + 1,
26 - kind: (["old", "established", "recent", "newest"] as const)[
27 - Math.floor(index / 2)
28 - ],
29 - recurring: index === 5,
30 - acceptedAt: 1_000,
31 - }),
32 -);
23 +function record(
24 + chunk: number,
25 + atoms: RecordChunk["atoms"] = [
26 + { key: `fact-${chunk}`, text: "fact", kind: "context", status: "active" },
27 + ],
28 + of = 8,
29 +): RecordChunk {
30 + return { chunk, of, atoms };
33 +function atom(
34 + key: string,
35 + kind: MemoryAtom["kind"] = "context",
36 + status: MemoryAtom["status"] = "active",
37 +): MemoryAtom {
38 + return {
39 + key,
40 + text: key,
41 + kind,
42 + status,
43 + firstSeen: 1,
44 + lastSeen: 1,
45 + seenCount: 1,
46 + sourcePosition: 1,
47 + };
34 50 const theme = {
35 - fg: (_color: string, text: string) => text,
51 + fg: vi.fn((_color: string, text: string) => text),
36 52 bg: vi.fn((_color: string, text: string) => text),
37 53 bold: (text: string) => text,
54 + inverse: vi.fn((text: string) => text),
40 -function component(displayState = state()) {
57 +function component(displayState = state(), now: () => number = () => 1_000) {
41 58 return new DecayComponent(
43 60 terminal: { rows: 30 },
45 62 } as never,
46 63 theme as never,
47 64 displayState,
48 - () => 1_000,
65 + now,
69 +function countStyle(
70 + row: ReturnType<typeof renderDecayFrame>[number],
71 + style: string,
72 +): number {
73 + return row.filter((cell) => cell.style === style).length;
52 76 describe("Decay terminal rendering", () => {
53 - it.each([
54 - [1, 15, 4],
55 - [20, 24, 7],
56 - [40, 15, 4],
57 - [40, 24, 7],
58 - [59, 23, 4],
59 - [80, 30, 7],
60 - [120, 40, 7],
61 - [140, 45, 7],
62 - ])(
63 - "keeps %ix%i frames bounded at %i lines across phases",
64 - (width, height, lines) => {
77 + it.each([1, 12, 20, 40, 80, 120, 160])(
78 + "keeps five borderless rows bounded at width %i",
79 + (width) => {
65 80 for (const phase of ["classify", "merge"] as const) {
66 81 const frame = plainFrame(
67 - renderDecayFrame(width, height, state({ phase }), 5_000),
82 + renderDecayFrame(width, 15, state({ phase }), 5_000),
69 - expect(frame).toHaveLength(lines);
84 + expect(frame).toHaveLength(5);
70 85 for (const line of frame) expect([...line]).toHaveLength(width);
71 - if (width >= 40) {
72 - expect(frame.join("\n")).toContain("DECAY");
73 - expect(frame[0]).toContain(
74 - phase === "merge" ? "MERGE" : "CLASSIFY 0/8",
75 - );
76 - expect(frame[1]).toContain("KEPT");
77 - }
86 + expect(frame.join("\n")).not.toMatch(/[╭╮╰╯┌┐└┘]/);
82 - it("starts with neutral history, no inferred age or fake recurrence", () => {
83 - const frame = renderDecayFrame(80, 30, state(), 2_000);
84 - const separator = frame[1].findIndex((cell) => cell.char === "║");
85 - const history = frame[1].slice(2, separator);
86 - expect(history.filter((cell) => cell.char === "░").length).toBeGreaterThan(
87 - 10,
88 - );
89 - expect(history.every((cell) => ["dim", "label"].includes(cell.color))).toBe(
90 - true,
91 - );
92 - expect(history.some((cell) => cell.color === "recurring")).toBe(false);
93 - expect(plainFrame(frame)[1]).toContain("HISTORY ~94K");
94 - expect(plainFrame(frame)[1]).toContain("~24K KEPT");
91 + it("uses human-readable counts and aligns every map origin", () => {
92 + const frame = plainFrame(renderDecayFrame(120, 30, state(), 2_000));
93 + expect(frame[0]).toContain("CLASSIFYING 0/8");
94 + expect(frame[1]).toContain("CTX 118k │");
95 + expect(frame[2]).toContain("FACTS 0 │");
96 + expect(frame[3]).toContain("NEW ? │");
97 + expect(frame.slice(1, 4).map((line) => line.indexOf("│"))).toEqual([
98 + 12, 12, 12,
99 + ]);
97 - it("vacates source-order history and lands only completed records, preserving the tail", () => {
98 - const initial = renderDecayFrame(80, 30, state(), 2_000);
99 - const completed = renderDecayFrame(
100 - 80,
102 + it("uses one continuous style under each CTX label and block", () => {
103 + const frame = renderDecayFrame(113, 30, state(), 2_000);
104 + const row = frame[1].slice(13);
105 + const sourceLabel = plainFrame(frame)[1].indexOf("~94k SUMMARIZE");
106 + const keepLabel = plainFrame(frame)[1].indexOf("~24k KEEP");
107 + expect(sourceLabel).toBeGreaterThanOrEqual(13);
108 + expect(keepLabel).toBeGreaterThan(sourceLabel);
109 + expect(frame[1][sourceLabel]?.style).toBe("sourceBlock");
110 + expect(frame[1][keepLabel]?.style).toBe("keepBlock");
111 + expect(row.every((cell) => cell.style !== "text")).toBe(true);
112 + });
114 + it("replaces pending blocks from an out-of-order completed chunk", () => {
115 + const frame = renderDecayFrame(
116 + 45,
102 - state({ transfers: completedTransfers }),
118 + state({ records: [record(8)] }),
103 119 2_000,
105 - const separator = initial[1].findIndex((cell) => cell.char === "║");
121 + expect(plainFrame(frame)[0]).toContain("CLASSIFYING 1/8");
122 + expect(countStyle(frame[2], "contextBlock")).toBeGreaterThan(0);
123 + expect(countStyle(frame[2], "pendingBlock")).toBeGreaterThan(0);
106 124 expect(
107 - completed[1].slice(2, separator).some((cell) => cell.char === "░"),
108 - ).toBe(false);
109 - expect(completed[1].slice(separator)).toEqual(initial[1].slice(separator));
110 - expect(completed[3].filter((cell) => cell.color === "recurring")).toEqual([
111 - { char: "◆", color: "recurring" },
112 - ]);
113 - expect(plainFrame(completed)[3]).toContain("SUMMARY");
125 + frame[2]
126 + .filter((cell) => cell.style === "pendingBlock")
127 + .every((cell) => cell.char === "░"),
128 + ).toBe(true);
116 - it("shows every in-flight record, not only the newest transfer", () => {
131 + it("classifies only context as context and all other kinds as durable", () => {
132 + const kinds = [
133 + "constraint",
134 + "decision",
135 + "goal",
136 + "state",
137 + "blocker",
138 + "exact",
139 + "context",
140 + ] as const;
141 + const frame = renderDecayFrame(
142 + 113,
143 + 30,
144 + state({
145 + totalChunks: 1,
146 + records: [
147 + record(
148 + 1,
149 + kinds.map((kind) => ({
150 + key: kind,
151 + text: kind,
152 + kind,
153 + status: "active",
154 + })),
155 + 1,
156 + ),
157 + ],
158 + }),
159 + 2_000,
160 + );
161 + expect(plainFrame(frame)[2]).toContain("6 DURABLE");
162 + expect(plainFrame(frame)[2]).toContain("1 CONTEXT");
163 + });
165 + it("highlights selected canonical atoms by normalized key", () => {
166 + const candidates = [
167 + atom(" Keep Me ", "decision"),
168 + atom("drop", "context"),
169 + ];
117 170 const frame = renderDecayFrame(
120 173 state({
121 - transfers: [
122 - { ...completedTransfers[0], acceptedAt: 1_000 },
123 - { ...completedTransfers[1], acceptedAt: 1_200 },
124 - ],
174 + phase: "merge",
175 + mergeStartedAt: 1_000,
176 + candidates,
177 + selectedKeys: ["keep me"],
126 - 1_400,
179 + 2_000,
128 - expect(frame[2].filter((cell) => /[▖▘▝▗]/.test(cell.char))).toHaveLength(2);
181 + expect(countStyle(frame[2], "selectedBlock")).toBeGreaterThan(0);
182 + expect(countStyle(frame[2], "contextBlock")).toBeGreaterThan(0);
183 + expect(plainFrame(frame)[2]).toContain("DURABLE");
131 - it.each([15, 30])(
132 - "keeps summary blending animated long after the last transfer at height %i",
133 - (height) => {
134 - const display = state({
186 + it("uses one token scale and places KEEP directly after streamed summary", () => {
187 + const initial = renderDecayFrame(
188 + 113,
189 + 30,
190 + state({
191 + inputTokens: 100_000,
192 + keptTokens: 20_000,
135 193 phase: "merge",
136 - transfers: completedTransfers,
137 - mergeAtoms: 72,
138 - summaryTokens: 1_200,
139 - });
140 - const summaryRow = height < 24 ? 2 : 3;
141 - const first = renderDecayFrame(80, height, display, 20_000);
142 - const second = renderDecayFrame(80, height, display, 20_110);
143 - expect(second[summaryRow]).not.toEqual(first[summaryRow]);
144 - expect(second[1]).toEqual(first[1]);
145 - expect(plainFrame(second).join("\n")).not.toContain("%");
146 - if (height >= 24) {
147 - expect(plainFrame(second)[2]).toContain("72 atoms");
148 - expect(plainFrame(second)[2]).toContain("tokens emitted");
149 - }
150 - },
151 - );
194 + mergeStartedAt: 1_000,
195 + summaryTokens: 1_000,
196 + }),
197 + 2_000,
198 + );
199 + const grown = renderDecayFrame(
200 + 113,
201 + 30,
202 + state({
203 + inputTokens: 100_000,
204 + keptTokens: 20_000,
205 + phase: "merge",
206 + mergeStartedAt: 1_000,
207 + summaryTokens: 4_000,
208 + }),
209 + 2_000,
210 + );
211 + expect(countStyle(initial[1], "keepBlock")).toBe(20);
212 + expect(countStyle(initial[3], "keepBlock")).toBe(20);
213 + expect(countStyle(initial[3], "selectedBlock")).toBe(1);
214 + expect(countStyle(grown[3], "selectedBlock")).toBe(4);
215 + const firstKeep = initial[3].findIndex(
216 + (cell) => cell.style === "keepBlock",
217 + );
218 + expect(firstKeep).toBe(14);
219 + expect(plainFrame(initial)[3]).toContain("NEW ~21k");
220 + expect(plainFrame(grown)[3]).toContain("NEW ~24k");
221 + });
153 - it("does not label an unknown retained count as the configured 20k target", () => {
223 + it("never substitutes the configured target for unknown retained tokens", () => {
154 224 const frame = plainFrame(
155 225 renderDecayFrame(80, 30, state({ keptTokens: undefined }), 2_000),
157 - expect(frame[1]).toContain("? KEPT");
158 - expect(frame[1]).not.toContain("20K");
227 + expect(frame[1]).not.toContain("24k KEEP");
228 + expect(frame[3]).toContain("NEW ?");
161 - it("overlays readable labels on block rows and remains passive", () => {
162 - const widget = component();
231 + it("uses inverse semantic colors instead of selectedBg", () => {
232 + theme.fg.mockClear();
233 + theme.bg.mockClear();
234 + theme.inverse.mockClear();
235 + const widget = component(
236 + state({
237 + phase: "merge",
238 + mergeStartedAt: 0,
239 + candidates: [atom("one", "decision")],
240 + selectedKeys: ["one"],
241 + summaryTokens: 100,
242 + }),
243 + );
164 245 expect("handleInput" in widget).toBe(false);
165 - expect(widget.render(80)).toHaveLength(7);
166 - expect(theme.bg).toHaveBeenCalledWith(
167 - "selectedBg",
168 - expect.stringContaining("HISTORY"),
169 - );
246 + expect(widget.render(80)).toHaveLength(5);
247 + expect(theme.inverse).toHaveBeenCalled();
248 + expect(theme.fg).toHaveBeenCalledWith("accent", expect.any(String));
249 + expect(theme.fg).toHaveBeenCalledWith("mdLink", expect.any(String));
250 + expect(theme.bg).not.toHaveBeenCalled();
170 251 } finally {
171 252 widget.dispose();
175 - it("counts recurrence across records, not duplicate keys in one record", () => {
256 + it("deduplicates records and ignores records outside the display run", () => {
176 257 const widget = component();
177 - const atom = {
178 - key: " Same Key ",
179 - text: "fact",
180 - kind: "context" as const,
181 - status: "active" as const,
182 - };
184 - widget.record({ chunk: 1, of: 8, atoms: [atom, atom] });
185 - widget.record({ chunk: 2, of: 8, atoms: [{ ...atom, key: "same key" }] });
186 - expect(
187 - widget.state.transfers.map((transfer) => transfer.recurring),
188 - ).toEqual([false, true]);
259 + widget.record(record(1));
260 + widget.record(record(1, []));
261 + widget.record(record(9));
262 + widget.record(record(2, [], 9));
263 + expect(widget.state.records).toEqual([{ chunk: 1, of: 8, atoms: [] }]);
264 + } finally {
265 + widget.dispose();
267 + });
269 + it("requests immediate renders for merge and summary stream updates", () => {
270 + const requestRender = vi.fn();
271 + const widget = new DecayComponent(
272 + { terminal: { rows: 30 }, requestRender } as never,
273 + theme as never,
274 + state(),
275 + () => 2_000,
276 + );
277 + try {
278 + widget.merge([atom("one", "decision")], [atom("one", "decision")]);
279 + widget.setSummary(12);
280 + expect(requestRender).toHaveBeenCalledTimes(2);
281 + expect(widget.state.summaryTokens).toBe(12);
282 + expect(widget.state.selectedKeys).toEqual(["one"]);
189 283 } finally {
190 284 widget.dispose();

extensions/decay/index.ts

Modified
59 59 interface RunCallbacks {
60 60 onRecord(record: RecordChunk): void;
61 - onMerge(atomCount: number): void;
61 + onMerge(atoms: readonly MemoryAtom[], selected: readonly MemoryAtom[]): void;
62 62 onSummary?(tokens: number): void;
230 230 keptTokens: estimateKeptTokens(event),
231 231 totalChunks: previewChunkCount(event),
232 232 phase: "classify",
233 - transfers: [],
233 + records: [],
234 234 startedAt: Date.now(),
252 252 signal,
254 254 onRecord: (record) => component?.record(record),
255 - onMerge: (atomCount) => {
256 - state.mergeAtoms = atomCount;
255 + onMerge: (atoms, selectedAtoms) => {
257 256 state.model = `${summarizerModel.provider}/${summarizerModel.id}`;
258 - component?.setPhase("merge");
259 - },
260 - onSummary: (tokens) => {
261 - state.summaryTokens = tokens;
257 + component?.merge(atoms, selectedAtoms);
259 + onSummary: (tokens) => component?.setSummary(tokens),
265 262 return { type: "success", result };
328 325 event.preparation.settings.reserveTokens,
330 327 const allocated = allocateAtoms(atoms, outputBudget);
331 - callbacks.onMerge(allocated.length);
328 + callbacks.onMerge(atoms, allocated);
332 329 stage = "summarizer";
333 330 const auth = await ctx.modelRegistry.getApiKeyAndHeaders(summarizerModel);
334 331 if (!auth.ok)

extensions/decay/ui.ts

Modified
1 1 import type { Theme } from "@earendil-works/pi-coding-agent";
2 2 import type { Component, TUI } from "@earendil-works/pi-tui";
3 -import type { RecordChunk } from "./core.js";
3 +import { isProtectedKind, type MemoryAtom, type RecordChunk } from "./core.js";
5 5 const FRAME_MS = 55;
6 -const TRANSFER_MS = 660;
7 -const NORMAL_HEIGHT = 7;
8 -const SHORT_HEIGHT = 4;
9 -const SPIN = ["▖", "▘", "▝", "▗"] as const;
6 +const HEIGHT = 5;
7 +const MAP_START = 13;
8 +const SELECTION_MS = 660;
9 +const SPIN = ["◉", "◍", "◎", "◌"] as const;
10 +const SUMMARY_HEAD = [" ", "░", "▒", "▓", "▒", "░"] as const;
11 -type Recency = "old" | "established" | "recent" | "newest";
12 -type CellColor = Recency | "recurring" | "title" | "text" | "dim" | "label";
12 +type CellStyle =
13 + | "title"
14 + | "text"
15 + | "dim"
16 + | "sourceText"
17 + | "sourceBlock"
18 + | "keepBlock"
19 + | "durableBlock"
20 + | "contextBlock"
21 + | "pendingBlock"
22 + | "completeBlock"
23 + | "selectedBlock";
14 25 interface Cell {
15 26 char: string;
16 - color: CellColor;
27 + style: CellStyle;
19 -export interface DisplayTransfer {
30 +export interface DisplayRecord {
20 31 chunk: number;
21 - kind: Recency;
22 - recurring: boolean;
23 - acceptedAt: number;
32 + of: number;
33 + atoms: RecordChunk["atoms"];
26 36 export interface DecayDisplayState {
29 39 keptTokens: number | undefined;
30 40 totalChunks: number;
31 41 phase: "classify" | "merge";
32 - transfers: DisplayTransfer[];
42 + records: DisplayRecord[];
33 43 startedAt: number;
34 - mergeAtoms?: number;
44 + mergeStartedAt?: number;
45 + candidates?: MemoryAtom[];
46 + selectedKeys?: string[];
35 47 summaryTokens?: number;
38 50 export class DecayComponent implements Component {
39 - private readonly seenKeys = new Set<string>();
40 51 private readonly timer: ReturnType<typeof setInterval>;
41 52 private disposed = false;
53 64 record(record: RecordChunk): void {
54 - const keys = new Set(
55 - record.atoms.map((atom) =>
56 - atom.key.trim().toLowerCase().replace(/\s+/g, " "),
57 - ),
58 - );
59 - const recurring = [...keys].some((key) => this.seenKeys.has(key));
60 - for (const key of keys) this.seenKeys.add(key);
61 - this.state.transfers.push({
65 + if (
66 + record.of !== this.state.totalChunks ||
67 + record.chunk < 1 ||
68 + record.chunk > this.state.totalChunks
69 + )
70 + return;
71 + const displayRecord: DisplayRecord = {
62 72 chunk: record.chunk,
63 - kind: recencyFor(record.chunk, record.of),
64 - recurring,
65 - acceptedAt: this.now(),
66 - });
73 + of: record.of,
74 + atoms: record.atoms,
75 + };
76 + const index = this.state.records.findIndex(
77 + (existing) => existing.chunk === record.chunk,
78 + );
79 + if (index >= 0) this.state.records[index] = displayRecord;
80 + else this.state.records.push(displayRecord);
81 + this.state.records.sort((left, right) => left.chunk - right.chunk);
67 82 this.tui.requestRender();
70 - setPhase(phase: DecayDisplayState["phase"]): void {
71 - this.state.phase = phase;
85 + merge(
86 + candidates: readonly MemoryAtom[],
87 + selected: readonly MemoryAtom[],
88 + ): void {
89 + this.state.phase = "merge";
90 + this.state.mergeStartedAt = this.now();
91 + this.state.candidates = candidates.map((atom) => ({ ...atom }));
92 + this.state.selectedKeys = selected.map((atom) => normalizeKey(atom.key));
93 + this.tui.requestRender();
94 + }
96 + setSummary(tokens: number): void {
97 + this.state.summaryTokens = Math.max(0, tokens);
72 98 this.tui.requestRender();
93 119 export function renderDecayFrame(
94 120 viewportWidth: number,
95 - viewportHeight: number,
121 + _viewportHeight: number,
96 122 state: DecayDisplayState,
97 123 now: number,
98 124 ): Cell[][] {
99 125 const width = Math.max(1, viewportWidth);
100 - const height = viewportHeight < 24 ? SHORT_HEIGHT : NORMAL_HEIGHT;
101 - const canvas = blankCanvas(width, height);
126 + const canvas = blankCanvas(width, HEIGHT);
102 127 const elapsedMs = Math.max(0, now - state.startedAt);
103 128 const frame = Math.floor(elapsedMs / FRAME_MS);
104 - const short = height === SHORT_HEIGHT;
129 + const completedChunks = completedChunkCount(state);
130 + const selectionProgress =
131 + state.phase === "merge"
132 + ? Math.min(
133 + 1,
134 + Math.max(0, now - (state.mergeStartedAt ?? now)) / SELECTION_MS,
136 + : 0;
105 137 const phase =
106 138 state.phase === "classify"
107 - ? `CLASSIFY ${state.transfers.length}/${state.totalChunks}`
108 - : "MERGE";
109 - const header = `${short ? "DECAY" : "DECAY.EXE"} ${SPIN[frame % SPIN.length]} · ${phase} · ${(elapsedMs / 1_000).toFixed(1)}s · ${modelLabel(state.model)}`;
110 - put(canvas, 0, 0, "─".repeat(width), "dim");
111 - put(canvas, 2, 0, header.slice(0, Math.max(0, width - 4)), "title");
112 - renderHistory(canvas, state);
113 - if (short) {
114 - const controls = width >= 48 ? " ↵ queue · Esc cancel" : " ↵ queue";
115 - renderSummary(canvas, 2, width - controls.length - 2, state, now);
116 - put(canvas, width - controls.length - 1, 2, controls, "dim");
117 - } else {
118 - renderTransfer(canvas, 2, state, now);
119 - renderSummary(canvas, 3, width - 2, state, now);
120 - renderLegend(canvas, 4);
121 - const controls = "Enter queues steer · Esc cancels";
122 - const leftWidth = Math.max(0, width - controls.length - 5);
123 - put(
124 - canvas,
125 - 2,
126 - 5,
127 - `INPUT ${state.inputTokens.toLocaleString("en-US")}`.slice(0, leftWidth),
128 - "text",
129 - );
130 - put(canvas, Math.max(2, width - controls.length - 2), 5, controls, "dim");
131 - }
132 - // Draw borders last so even tiny viewports cannot overwrite them.
133 - for (let y = 1; y < height - 1; y++) {
134 - put(canvas, 0, y, "│", "dim");
135 - put(canvas, width - 1, y, "│", "dim");
136 - }
137 - for (let x = 0; x < width; x++) {
138 - put(canvas, x, height - 1, "─", "dim");
139 - }
140 - put(canvas, 0, 0, "╭", "dim");
141 - put(canvas, width - 1, 0, "╮", "dim");
142 - put(canvas, 0, height - 1, "╰", "dim");
143 - put(canvas, width - 1, height - 1, "╯", "dim");
139 + ? `CLASSIFYING ${completedChunks}/${state.totalChunks}`
140 + : selectionProgress < 1 && state.summaryTokens === undefined
141 + ? "SELECTING MEMORY"
142 + : "WRITING SUMMARY";
144 + putFragments(canvas, 0, 0, [
145 + ["▣", "sourceText"],
146 + [" DECAY ", "title"],
147 + ["· ", "dim"],
148 + [SPIN[frame % SPIN.length], "sourceText"],
149 + [` ${phase}`, "title"],
151 + ` · ${Math.floor(elapsedMs / 1_000)}s · ${modelLabel(state.model)}`,
152 + "dim",
153 + ],
154 + ]);
156 + renderContext(canvas, state);
157 + renderFacts(canvas, state, selectionProgress);
158 + renderNewContext(canvas, state, frame, completedChunks, selectionProgress);
159 + put(canvas, 0, 4, "↵ queues a message · Esc cancels", "dim");
144 160 return canvas;
147 -function renderHistory(canvas: Cell[][], state: DecayDisplayState): void {
148 - const width = canvas[0].length;
149 - const tailWidth = Math.min(
150 - Math.max(12, Math.round(width * 0.25)),
151 - Math.max(0, width - 6),
152 - );
153 - const separator = width - tailWidth - 3;
154 - const historyStart = 2;
155 - const historyEnd = Math.max(historyStart, separator - 1);
156 - const capacity = historyEnd - historyStart;
157 - const accepted = new Set(state.transfers.map((transfer) => transfer.chunk));
158 - for (let x = historyStart; x < historyEnd; x++) {
159 - // Contiguous source-order bins, never decorative pseudo-random classifications.
160 - const first =
161 - Math.floor(
162 - ((x - historyStart) * state.totalChunks) / Math.max(1, capacity),
163 - ) + 1;
164 - const last = Math.max(
165 - first,
166 - Math.ceil(
167 - ((x - historyStart + 1) * state.totalChunks) / Math.max(1, capacity),
168 - ),
169 - );
170 - let complete = state.totalChunks > 0;
171 - for (let chunk = first; chunk <= last; chunk++)
172 - complete &&= accepted.has(chunk);
173 - put(canvas, x, 1, complete ? " " : "░", "dim");
163 +function renderContext(canvas: Cell[][], state: DecayDisplayState): void {
164 + const capacity = Math.max(0, canvas[0].length - MAP_START);
165 + renderMapPrefix(canvas, 1, "CTX", compactTokens(state.inputTokens));
166 + if (capacity === 0) return;
167 + const kept = state.keptTokens;
168 + if (kept === undefined || kept < 0 || kept > state.inputTokens) {
169 + fillBlock(canvas, MAP_START, 1, capacity, "sourceBlock", [
170 + ` ${compactTokens(state.inputTokens)} CTX `,
171 + " CTX ",
172 + ]);
173 + return;
175 - put(canvas, separator, 1, "║", "dim");
176 - for (let x = separator + 2; x < width - 2; x++)
177 - put(canvas, x, 1, "▓", "recent");
178 - const history =
179 - state.keptTokens === undefined
180 - ? "?"
181 - : compactTokens(Math.max(0, state.inputTokens - state.keptTokens));
182 - put(
183 - canvas,
184 - historyStart,
185 - 1,
186 - ` HISTORY ${history} `.slice(0, capacity),
187 - "label",
188 - );
189 - put(
190 - canvas,
191 - separator + 2,
192 - 1,
193 - ` ${compactTokens(state.keptTokens)} KEPT `.slice(0, tailWidth - 1),
194 - "label",
195 - );
175 + const summary = state.summaryTokens ?? 0;
176 + const scale = Math.max(1, state.inputTokens, kept + summary);
177 + const keepWidth = scaledWidth(kept, scale, capacity);
178 + const summarized = Math.max(0, state.inputTokens - kept);
179 + const sourceWidth =
180 + state.inputTokens === scale
181 + ? Math.max(0, capacity - keepWidth)
182 + : Math.min(
183 + capacity - keepWidth,
184 + scaledWidth(summarized, scale, capacity),
185 + );
186 + fillBlock(canvas, MAP_START, 1, sourceWidth, "sourceBlock", [
187 + ` ~${compactTokens(summarized)} ${state.phase === "merge" ? "SUMMARIZED" : "SUMMARIZE"} `,
188 + state.phase === "merge" ? " SUMMARIZED " : " SUMMARIZE ",
189 + " S ",
190 + ]);
191 + fillBlock(canvas, MAP_START + sourceWidth, 1, keepWidth, "keepBlock", [
192 + ` ~${compactTokens(kept)} KEEP `,
193 + " KEEP ",
194 + " K ",
195 + ]);
198 -function renderTransfer(
198 +function renderFacts(
199 199 canvas: Cell[][],
200 - y: number,
201 200 state: DecayDisplayState,
202 - now: number,
201 + selectionProgress: number,
203 202 ): void {
204 - const end = canvas[0].length - 3;
205 - if (state.phase === "merge") {
206 - const output = state.summaryTokens
207 - ? `${compactTokens(state.summaryTokens)} tokens emitted`
208 - : "awaiting summary text";
209 - put(
203 + const capacity = Math.max(0, canvas[0].length - MAP_START);
204 + const records = uniqueRecords(state.records);
205 + const observed = records.flatMap((record) => record.atoms);
206 + const candidates =
207 + state.phase === "merge" ? (state.candidates ?? []) : observed;
208 + renderMapPrefix(canvas, 2, "FACTS", compactCount(candidates.length));
209 + if (capacity === 0) return;
211 + if (state.phase === "classify") {
212 + const completed = records.length;
213 + let completeWidth =
214 + state.totalChunks > 0
215 + ? Math.round((completed / state.totalChunks) * capacity)
216 + : 0;
217 + if (completed > 0) completeWidth = Math.max(1, completeWidth);
218 + if (completed < state.totalChunks && capacity > 1)
219 + completeWidth = Math.min(capacity - 1, completeWidth);
220 + completeWidth = Math.min(capacity, completeWidth);
221 + renderClassifiedGroups(canvas, 2, observed, completeWidth, undefined, 0);
222 + fillBlock(
210 223 canvas,
224 + MAP_START + completeWidth,
212 - y,
213 - `MERGING ${state.mergeAtoms ?? 0} atoms → summary · ${output}`,
214 - "text",
226 + capacity - completeWidth,
227 + "pendingBlock",
228 + [],
229 + "░",
216 231 return;
218 - for (let x = 2; x < end; x++) put(canvas, x, y, "·", "dim");
219 - put(canvas, end, y, "▶", "recent");
220 - const frame = Math.floor(Math.max(0, now - state.startedAt) / FRAME_MS);
221 - for (const transfer of state.transfers) {
222 - const progress = Math.max(0, (now - transfer.acceptedAt) / TRANSFER_MS);
223 - if (progress >= 1) continue;
224 - const x = Math.round(2 + (end - 2) * progress);
225 - put(
226 - canvas,
227 - x,
228 - y,
229 - SPIN[frame % SPIN.length],
230 - transfer.recurring ? "recurring" : transfer.kind,
231 - );
232 - }
233 - put(canvas, 2, y, " TRANSFER ", "label");
234 + const selected = new Set(state.selectedKeys ?? []);
235 + renderClassifiedGroups(
236 + canvas,
237 + 2,
238 + candidates,
239 + capacity,
240 + selected,
241 + selectionProgress,
242 + );
236 -function renderSummary(
245 +function renderClassifiedGroups(
237 246 canvas: Cell[][],
238 247 y: number,
239 - end: number,
240 - state: DecayDisplayState,
241 - now: number,
248 + atoms: readonly Pick<MemoryAtom, "key" | "kind">[],
249 + width: number,
250 + selectedKeys: ReadonlySet<string> | undefined,
251 + selectionProgress: number,
242 252 ): void {
243 - const start = 2;
244 - const capacity = Math.max(0, end - start);
245 - for (let x = start; x < end; x++) put(canvas, x, y, "·", "dim");
246 - for (const transfer of state.transfers) {
247 - if (state.phase !== "merge" && now - transfer.acceptedAt < TRANSFER_MS)
248 - continue;
249 - const x =
250 - start +
251 - Math.floor(
252 - ((transfer.chunk - 1) / Math.max(1, state.totalChunks)) * capacity,
253 - );
253 + if (width <= 0) return;
254 + const durable = atoms.filter((atom) => isProtectedKind(atom.kind));
255 + const context = atoms.filter((atom) => !isProtectedKind(atom.kind));
256 + if (atoms.length === 0) {
257 + fillBlock(canvas, MAP_START, y, width, "completeBlock", []);
258 + return;
260 + const durableWidth = partitionWidth(durable.length, atoms.length, width);
261 + const contextWidth = width - durableWidth;
262 + renderAtomGroup(
263 + canvas,
264 + MAP_START,
265 + y,
266 + durableWidth,
267 + durable,
268 + "durableBlock",
269 + [` ◆ ${durable.length} DURABLE `, ` ${durable.length} DUR `, " ◆ "],
270 + selectedKeys,
271 + selectionProgress,
272 + );
273 + renderAtomGroup(
274 + canvas,
275 + MAP_START + durableWidth,
276 + y,
277 + contextWidth,
278 + context,
279 + "contextBlock",
280 + [` ◇ ${context.length} CONTEXT `, ` ${context.length} CTX `, " ◇ "],
281 + selectedKeys,
282 + selectionProgress,
283 + );
286 +function renderAtomGroup(
287 + canvas: Cell[][],
288 + x: number,
289 + y: number,
290 + width: number,
291 + atoms: readonly Pick<MemoryAtom, "key">[],
292 + baseStyle: CellStyle,
293 + labels: readonly string[],
294 + selectedKeys: ReadonlySet<string> | undefined,
295 + selectionProgress: number,
296 +): void {
297 + if (width <= 0) return;
298 + for (let offset = 0; offset < width; offset++) {
299 + const atom =
300 + atoms[
301 + Math.min(
302 + atoms.length - 1,
303 + Math.floor((offset * atoms.length) / Math.max(1, width)),
305 + ];
306 + const selected = atom && selectedKeys?.has(normalizeKey(atom.key));
307 + const revealed = hashFraction(offset, x) <= selectionProgress;
255 309 canvas,
256 - x,
310 + x + offset,
258 - transfer.recurring ? "◆" : historyGlyph(transfer.kind),
259 - transfer.recurring ? "recurring" : transfer.kind,
312 + " ",
313 + selected && revealed ? "selectedBlock" : baseStyle,
262 - const short = canvas.length === SHORT_HEIGHT;
263 - const label = short
264 - ? " S "
265 - : state.phase === "merge"
266 - ? " SUMMARY · blending "
267 - : " SUMMARY ";
268 - const blendStart = start + label.length;
269 - const blendWidth = end - blendStart;
270 - if (state.phase === "merge" && blendWidth > 0) {
271 - // A looping blend indicates synthesis activity, not progress toward an unknown total.
272 - const frame = Math.floor(Math.max(0, now - state.startedAt) / 110);
273 - for (let offset = 0; offset < Math.min(5, blendWidth); offset++) {
274 - put(
275 - canvas,
276 - blendStart + ((frame + offset) % blendWidth),
277 - y,
278 - ["░", "▒", "▓", "▒", "░"][offset],
279 - "recent",
280 - );
281 - }
316 + const label = fittingLabel(width, labels);
317 + if (label) putPreservingStyle(canvas, x, y, label);
320 +function renderNewContext(
321 + canvas: Cell[][],
322 + state: DecayDisplayState,
323 + frame: number,
324 + completedChunks: number,
325 + selectionProgress: number,
326 +): void {
327 + const capacity = Math.max(0, canvas[0].length - MAP_START);
328 + const summary = state.summaryTokens;
329 + const kept = state.keptTokens;
330 + const known = summary !== undefined && summary > 0 && kept !== undefined;
331 + renderMapPrefix(
332 + canvas,
333 + 3,
334 + "NEW",
335 + known ? `~${compactTokens(summary + kept)}` : "?",
336 + );
337 + if (capacity === 0) return;
338 + if (!known) {
339 + const waiting =
340 + state.phase === "classify"
341 + ? `○ waiting for ${Math.max(0, state.totalChunks - completedChunks)} chunks`
342 + : selectionProgress < 1
343 + ? "○ selecting facts"
344 + : "○ waiting for summary";
345 + put(canvas, MAP_START, 3, waiting, "dim");
346 + return;
283 - put(canvas, start, y, label.slice(0, capacity), "label");
348 + if (kept < 0 || kept > state.inputTokens) {
349 + put(canvas, MAP_START, 3, "○ retained size unavailable", "dim");
350 + return;
352 + const scale = Math.max(1, state.inputTokens, kept + summary);
353 + const summaryWidth = scaledWidth(summary, scale, capacity);
354 + const keepWidth = Math.min(
355 + capacity - summaryWidth,
356 + scaledWidth(kept, scale, capacity),
357 + );
358 + fillBlock(canvas, MAP_START, 3, summaryWidth, "selectedBlock", []);
359 + if (summaryWidth > 0)
360 + put(
361 + canvas,
362 + MAP_START + summaryWidth - 1,
363 + 3,
364 + SUMMARY_HEAD[frame % SUMMARY_HEAD.length],
365 + "selectedBlock",
366 + );
367 + fillBlock(canvas, MAP_START + summaryWidth, 3, keepWidth, "keepBlock", [
368 + ` ~${compactTokens(kept)} KEEP `,
369 + " KEEP ",
370 + " K ",
371 + ]);
286 -function renderLegend(canvas: Cell[][], y: number): void {
287 - const width = canvas[0].length;
288 - const legend =
289 - width < 68
290 - ? "░ pending · age: old → new · ◆ repeat"
291 - : "░ pending · colors: source age, old → new · ◆ recurring";
292 - put(canvas, 2, y, legend, "dim");
293 - const index = legend.indexOf("◆");
294 - put(canvas, 2 + index, y, "◆", "recurring");
374 +function renderMapPrefix(
375 + canvas: Cell[][],
376 + y: number,
377 + label: string,
378 + value: string,
379 +): void {
380 + put(canvas, 0, y, label.slice(0, 6).padEnd(6), "text");
381 + put(canvas, 6, y, value.slice(-5).padStart(5), "title");
382 + put(canvas, 11, y, " │", "dim");
297 -function recencyFor(chunk: number, total: number): Recency {
298 - const ratio = chunk / Math.max(1, total);
299 - if (ratio <= 0.25) return "old";
300 - if (ratio <= 0.5) return "established";
301 - if (ratio <= 0.75) return "recent";
302 - return "newest";
385 +function fillBlock(
386 + canvas: Cell[][],
387 + x: number,
388 + y: number,
389 + width: number,
390 + style: CellStyle,
391 + labels: readonly string[],
392 + fill = " ",
393 +): void {
394 + for (let offset = 0; offset < width; offset++)
395 + put(canvas, x + offset, y, fill, style);
396 + const label = fittingLabel(width, labels);
397 + if (label) put(canvas, x, y, label, style);
400 +function fittingLabel(width: number, labels: readonly string[]): string {
401 + return labels.find((label) => label.length <= width) ?? "";
404 +function partitionWidth(part: number, total: number, width: number): number {
405 + if (part <= 0) return 0;
406 + if (part >= total) return width;
407 + if (width <= 1) return Math.round(part / total);
408 + return Math.min(width - 1, Math.max(1, Math.round((part / total) * width)));
411 +function scaledWidth(value: number, scale: number, capacity: number): number {
412 + if (value <= 0 || capacity <= 0) return 0;
413 + return Math.min(
414 + capacity,
415 + Math.max(1, Math.round((value / Math.max(1, scale)) * capacity)),
416 + );
419 +function completedChunkCount(state: DecayDisplayState): number {
420 + return uniqueRecords(state.records).length;
423 +function uniqueRecords(records: readonly DisplayRecord[]): DisplayRecord[] {
424 + const unique = new Map<number, DisplayRecord>();
425 + for (const record of records) unique.set(record.chunk, record);
426 + return [...unique.values()].sort((left, right) => left.chunk - right.chunk);
429 +function hashFraction(index: number, salt: number): number {
430 + return ((index * 37 + salt * 17) % 101) / 100;
433 +function normalizeKey(key: string): string {
434 + return key.trim().toLowerCase().replace(/\s+/g, " ");
305 437 function blankCanvas(width: number, height: number): Cell[][] {
306 438 return Array.from({ length: height }, () =>
307 - Array.from({ length: width }, () => ({ char: " ", color: "dim" })),
439 + Array.from({ length: width }, () => ({ char: " ", style: "dim" })),
443 +function putFragments(
444 + canvas: Cell[][],
445 + x: number,
446 + y: number,
447 + parts: readonly (readonly [string, CellStyle])[],
448 +): void {
449 + let column = x;
450 + for (const [text, style] of parts) {
451 + put(canvas, column, y, text, style);
452 + column += [...text].length;
311 456 function put(
312 457 canvas: Cell[][],
313 458 x: number,
314 459 y: number,
315 460 text: string,
316 - color: CellColor,
461 + style: CellStyle,
317 462 ): void {
318 463 if (y < 0 || y >= canvas.length) return;
319 464 let column = x;
320 465 for (const char of text) {
321 466 if (column >= 0 && column < canvas[y].length)
322 - canvas[y][column] = { char, color };
467 + canvas[y][column] = { char, style };
323 468 column++;
327 -function historyGlyph(kind: Recency): string {
328 - return { old: "░", established: "▒", recent: "▓", newest: "█" }[kind];
472 +function putPreservingStyle(
473 + canvas: Cell[][],
474 + x: number,
475 + y: number,
476 + text: string,
477 +): void {
478 + if (y < 0 || y >= canvas.length) return;
479 + let column = x;
480 + for (const char of text) {
481 + if (column >= 0 && column < canvas[y].length) canvas[y][column].char = char;
482 + column++;
331 486 function modelLabel(reference: string): string {
333 488 return separator >= 0 ? reference.slice(separator + 1) : reference;
336 -function compactTokens(tokens: number | undefined): string {
337 - if (tokens === undefined) return "?";
338 - return tokens >= 1_000 ? `~${Math.round(tokens / 1_000)}K` : `~${tokens}`;
491 +function compactTokens(tokens: number): string {
492 + const value = Math.max(0, tokens);
493 + if (value >= 1_000_000) {
494 + const millions = value / 1_000_000;
495 + return `${millions >= 10 ? Math.round(millions) : millions.toFixed(1).replace(/\.0$/, "")}m`;
497 + if (value >= 1_000) {
498 + const thousands = value / 1_000;
499 + return `${thousands >= 10 ? Math.round(thousands) : thousands.toFixed(1).replace(/\.0$/, "")}k`;
501 + return Math.round(value).toLocaleString("en-US");
504 +function compactCount(value: number): string {
505 + return value >= 1_000 ? compactTokens(value) : value.toLocaleString("en-US");
341 508 function styleFrame(frame: Cell[][], theme: Theme): string[] {
342 - const color = (name: CellColor, text: string): string => {
509 + const style = (name: CellStyle, text: string): string => {
343 510 switch (name) {
344 511 case "title":
345 - return theme.bold(theme.fg("success", text));
346 - case "label":
347 - return theme.bg("selectedBg", theme.bold(theme.fg("text", text)));
512 + return theme.bold(theme.fg("text", text));
348 513 case "text":
349 - case "newest":
350 514 return theme.fg("text", text);
351 - case "recurring":
352 - return theme.fg("warning", text);
353 - case "recent":
515 + case "sourceText":
354 516 return theme.fg("success", text);
355 - case "established":
356 - return theme.fg("borderAccent", text);
357 - case "old":
358 - return theme.fg("muted", text);
517 + case "sourceBlock":
518 + return theme.inverse(theme.fg("success", text));
519 + case "selectedBlock":
520 + return theme.inverse(theme.fg("accent", text));
521 + case "keepBlock":
522 + return theme.inverse(theme.fg("mdLink", text));
523 + case "durableBlock":
524 + return theme.inverse(theme.fg("warning", text));
525 + case "contextBlock":
526 + return theme.inverse(theme.fg("borderAccent", text));
527 + case "pendingBlock":
528 + return theme.inverse(theme.fg("muted", text));
529 + case "completeBlock":
530 + return theme.inverse(theme.fg("border", text));
359 531 case "dim":
360 532 return theme.fg("dim", text);
363 535 return frame.map((row) => {
364 536 let result = "";
365 - let active = row[0]?.color ?? "dim";
537 + let active = row[0]?.style ?? "dim";
366 538 let text = "";
367 539 for (const cell of row) {
368 - if (cell.color !== active) {
369 - result += color(active, text);
370 - active = cell.color;
540 + if (cell.style !== active) {
541 + result += style(active, text);
542 + active = cell.style;
371 543 text = "";
373 545 text += cell.char;
375 - return result + color(active, text);
547 + return result + style(active, text);