repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

fix(decay): improve compaction progress display

c47d47f4147a1c35b78690805bf4dffe222c9878

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (3)

CHANGELOG.md

Modified
2 2 # Changelog
4 4 ## Unreleased
6 +- Improve Decay’s compaction display with stationary retained context, left-growing summaries, chunk-position progress, live memory selection, and recency markers
6 7 - Make Decay retry classification and summarization with the current session model when an explicit model is unavailable or fails, including quota and rate-limit errors
7 8 - Remove working-label updates from the Web fetch and search tools
8 9 - Remove the working-label update from git-safe cloning

extensions/decay/__tests__/ui.test.ts

Modified
102 102 it("uses one continuous style under each CTX label and block", () => {
103 103 const frame = renderDecayFrame(113, 30, state(), 2_000);
104 104 const row = frame[1].slice(13);
105 - const sourceLabel = plainFrame(frame)[1].indexOf("~94k SUMMARIZE");
105 + const sourceLabel = plainFrame(frame)[1].indexOf("~94k TO SUMMARY");
106 106 const keepLabel = plainFrame(frame)[1].indexOf("~24k KEEP");
107 107 expect(sourceLabel).toBeGreaterThanOrEqual(13);
108 108 expect(keepLabel).toBeGreaterThan(sourceLabel);
173 173 state({
174 174 phase: "merge",
175 - mergeStartedAt: 1_000,
176 175 candidates,
177 176 selectedKeys: ["keep me"],
183 182 expect(plainFrame(frame)[2]).toContain("DURABLE");
186 - it("uses one token scale and places KEEP directly after streamed summary", () => {
185 + it("keeps retained context stationary while the new summary grows leftward", () => {
187 186 const initial = renderDecayFrame(
191 190 inputTokens: 100_000,
192 191 keptTokens: 20_000,
193 192 phase: "merge",
194 - mergeStartedAt: 1_000,
195 193 summaryTokens: 1_000,
197 195 2_000,
203 201 inputTokens: 100_000,
204 202 keptTokens: 20_000,
205 203 phase: "merge",
206 - mergeStartedAt: 1_000,
207 204 summaryTokens: 4_000,
209 206 2_000,
208 + const initialKeep = initial[3].findIndex(
209 + (cell) => cell.style === "keepBlock",
210 + );
211 + const grownKeep = grown[3].findIndex((cell) => cell.style === "keepBlock");
211 212 expect(countStyle(initial[1], "keepBlock")).toBe(20);
212 213 expect(countStyle(initial[3], "keepBlock")).toBe(20);
213 214 expect(countStyle(initial[3], "selectedBlock")).toBe(1);
214 215 expect(countStyle(grown[3], "selectedBlock")).toBe(4);
215 - const firstKeep = initial[3].findIndex(
216 - (cell) => cell.style === "keepBlock",
217 - );
218 - expect(firstKeep).toBe(14);
216 + expect(initialKeep).toBe(93);
217 + expect(grownKeep).toBe(initialKeep);
219 218 expect(plainFrame(initial)[3]).toContain("NEW ~21k");
220 219 expect(plainFrame(grown)[3]).toContain("NEW ~24k");
222 + it("shows retained context before the first summary token arrives", () => {
223 + const frame = renderDecayFrame(
224 + 80,
225 + 30,
226 + state({ phase: "merge", summaryTokens: undefined }),
227 + 2_000,
228 + );
229 + const text = plainFrame(frame);
230 + const keepStart = frame[3].findIndex((cell) => cell.style === "keepBlock");
231 + expect(keepStart).toBeGreaterThan(13);
232 + expect(text[0]).toContain("WRITING SUMMARY");
233 + expect(text[3]).toContain("NEW ?");
234 + expect(text[3].slice(keepStart)).toContain("KEEP");
235 + });
237 + it("keeps out-of-order chunk progress in source order", () => {
238 + const frame = renderDecayFrame(
239 + 45,
240 + 30,
241 + state({ records: [record(8)] }),
242 + 2_000,
243 + );
244 + const row = frame[2];
245 + const firstCompleted = row.findIndex(
246 + (cell) => cell.style === "contextBlock",
247 + );
248 + expect(firstCompleted).toBeGreaterThan(13);
249 + expect(
250 + row
251 + .slice(13, firstCompleted)
252 + .every((cell) => cell.style === "pendingBlock"),
253 + ).toBe(true);
254 + expect(
255 + row.slice(firstCompleted).every((cell) => cell.style === "contextBlock"),
256 + ).toBe(true);
257 + });
259 + it("shows selected memory count and recency markers", () => {
260 + const candidates = [
261 + { ...atom("old", "context"), lastSeen: 1, seenCount: 1 },
262 + { ...atom("repeat", "decision"), lastSeen: 2, seenCount: 2 },
263 + { ...atom("new", "goal"), lastSeen: 3, seenCount: 1 },
264 + ];
265 + const frame = renderDecayFrame(
266 + 100,
267 + 30,
268 + state({
269 + phase: "merge",
270 + candidates,
271 + selectedKeys: ["old", "repeat"],
272 + }),
273 + 2_000,
274 + );
275 + expect(plainFrame(frame)[2]).toContain("MEM 2/3 │");
276 + expect(plainFrame(frame)[2]).toContain("◆");
277 + expect(plainFrame(frame)[4]).toContain("◆ recurring");
278 + });
223 280 it("never substitutes the configured target for unknown retained tokens", () => {
224 281 const frame = plainFrame(
225 282 renderDecayFrame(80, 30, state({ keptTokens: undefined }), 2_000),
235 292 const widget = component(
236 293 state({
237 294 phase: "merge",
238 - mergeStartedAt: 0,
239 295 candidates: [atom("one", "decision")],
240 296 selectedKeys: ["one"],
241 297 summaryTokens: 100,

extensions/decay/ui.ts

Modified
5 5 const FRAME_MS = 55;
6 6 const HEIGHT = 5;
7 7 const MAP_START = 13;
8 -const SELECTION_MS = 660;
9 8 const SPIN = ["◉", "◍", "◎", "◌"] as const;
10 9 const SUMMARY_HEAD = [" ", "░", "▒", "▓", "▒", "░"] as const;
41 40 phase: "classify" | "merge";
42 41 records: DisplayRecord[];
43 42 startedAt: number;
44 - mergeStartedAt?: number;
45 43 candidates?: MemoryAtom[];
46 44 selectedKeys?: string[];
47 45 summaryTokens?: number;
87 85 selected: readonly MemoryAtom[],
88 86 ): void {
89 87 this.state.phase = "merge";
90 - this.state.mergeStartedAt = this.now();
91 88 this.state.candidates = candidates.map((atom) => ({ ...atom }));
92 89 this.state.selectedKeys = selected.map((atom) => normalizeKey(atom.key));
93 90 this.tui.requestRender();
127 124 const elapsedMs = Math.max(0, now - state.startedAt);
128 125 const frame = Math.floor(elapsedMs / FRAME_MS);
129 126 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,
135 - )
136 - : 0;
137 127 const phase =
138 128 state.phase === "classify"
139 129 ? `CLASSIFYING ${completedChunks}/${state.totalChunks}`
140 - : selectionProgress < 1 && state.summaryTokens === undefined
141 - ? "SELECTING MEMORY"
142 - : "WRITING SUMMARY";
130 + : "WRITING SUMMARY";
144 132 putFragments(canvas, 0, 0, [
145 133 ["▣", "sourceText"],
156 144 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");
145 + renderFacts(canvas, state);
146 + renderNewContext(canvas, state, frame, completedChunks);
147 + put(
148 + canvas,
149 + 0,
150 + 4,
151 + state.phase === "merge"
152 + ? "↵ queues a message · Esc cancels · ◆ recurring · █ newer"
153 + : "↵ queues a message · Esc cancels",
154 + "dim",
155 + );
160 156 return canvas;
184 180 scaledWidth(summarized, scale, capacity),
186 182 fillBlock(canvas, MAP_START, 1, sourceWidth, "sourceBlock", [
187 - ` ~${compactTokens(summarized)} ${state.phase === "merge" ? "SUMMARIZED" : "SUMMARIZE"} `,
188 - state.phase === "merge" ? " SUMMARIZED " : " SUMMARIZE ",
183 + ` ~${compactTokens(summarized)} TO SUMMARY `,
184 + " TO SUMMARY ",
189 185 " S ",
191 187 fillBlock(canvas, MAP_START + sourceWidth, 1, keepWidth, "keepBlock", [
198 -function renderFacts(
199 - canvas: Cell[][],
200 - state: DecayDisplayState,
201 - selectionProgress: number,
202 -): void {
194 +function renderFacts(canvas: Cell[][], state: DecayDisplayState): void {
203 195 const capacity = Math.max(0, canvas[0].length - MAP_START);
204 196 const records = uniqueRecords(state.records);
205 - const observed = records.flatMap((record) => record.atoms);
197 + const observed = records.flatMap((record) =>
198 + record.atoms.map((atom) => ({ key: atom.key, kind: atom.kind })),
199 + );
206 200 const candidates =
207 201 state.phase === "merge" ? (state.candidates ?? []) : observed;
208 - renderMapPrefix(canvas, 2, "FACTS", compactCount(candidates.length));
202 + if (state.phase === "classify") {
203 + renderMapPrefix(canvas, 2, "FACTS", compactCount(observed.length));
204 + } else {
205 + const selected = new Set(state.selectedKeys ?? []);
206 + const selectedCount = candidates.filter((atom) =>
207 + selected.has(normalizeKey(atom.key)),
208 + ).length;
209 + renderMapPrefix(canvas, 2, "MEM", `${selectedCount}/${candidates.length}`);
209 211 if (capacity === 0) return;
211 213 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(
223 - canvas,
224 - MAP_START + completeWidth,
225 - 2,
226 - capacity - completeWidth,
227 - "pendingBlock",
228 - [],
229 - "░",
230 - );
214 + const byChunk = new Map(records.map((record) => [record.chunk, record]));
215 + for (let index = 0; index < state.totalChunks; index++) {
216 + const start = Math.floor((index * capacity) / state.totalChunks);
217 + const end = Math.floor(((index + 1) * capacity) / state.totalChunks);
218 + const width = end - start;
219 + const record = byChunk.get(index + 1);
220 + if (record) {
221 + renderClassifiedGroups(
222 + canvas,
223 + 2,
224 + record.atoms.map((atom) => ({ key: atom.key, kind: atom.kind })),
225 + width,
226 + undefined,
227 + MAP_START + start,
228 + );
229 + } else {
230 + fillBlock(canvas, MAP_START + start, 2, width, "pendingBlock", [], "░");
231 233 return;
234 236 const selected = new Set(state.selectedKeys ?? []);
235 - renderClassifiedGroups(
236 - canvas,
237 - 2,
238 - candidates,
239 - capacity,
240 - selected,
241 - selectionProgress,
242 - );
237 + renderClassifiedGroups(canvas, 2, candidates, capacity, selected);
240 +type AtomView = Pick<MemoryAtom, "key" | "kind"> &
241 + Partial<Pick<MemoryAtom, "lastSeen" | "seenCount">>;
245 243 function renderClassifiedGroups(
246 244 canvas: Cell[][],
247 245 y: number,
248 - atoms: readonly Pick<MemoryAtom, "key" | "kind">[],
246 + atoms: readonly AtomView[],
249 247 width: number,
250 248 selectedKeys: ReadonlySet<string> | undefined,
251 - selectionProgress: number,
249 + startX = MAP_START,
252 250 ): void {
253 251 if (width <= 0) return;
254 252 const durable = atoms.filter((atom) => isProtectedKind(atom.kind));
255 253 const context = atoms.filter((atom) => !isProtectedKind(atom.kind));
256 254 if (atoms.length === 0) {
257 - fillBlock(canvas, MAP_START, y, width, "completeBlock", []);
255 + fillBlock(canvas, startX, y, width, "completeBlock", []);
258 256 return;
260 258 const durableWidth = partitionWidth(durable.length, atoms.length, width);
261 259 const contextWidth = width - durableWidth;
262 260 renderAtomGroup(
263 261 canvas,
264 - MAP_START,
262 + startX,
266 264 durableWidth,
267 265 durable,
268 266 "durableBlock",
269 267 [` ◆ ${durable.length} DURABLE `, ` ${durable.length} DUR `, " ◆ "],
270 268 selectedKeys,
271 - selectionProgress,
273 270 renderAtomGroup(
274 271 canvas,
275 - MAP_START + durableWidth,
272 + startX + durableWidth,
277 274 contextWidth,
278 275 context,
279 276 "contextBlock",
280 277 [` ◇ ${context.length} CONTEXT `, ` ${context.length} CTX `, " ◇ "],
281 278 selectedKeys,
282 - selectionProgress,
288 284 x: number,
289 285 y: number,
290 286 width: number,
291 - atoms: readonly Pick<MemoryAtom, "key">[],
287 + atoms: readonly AtomView[],
292 288 baseStyle: CellStyle,
293 289 labels: readonly string[],
294 290 selectedKeys: ReadonlySet<string> | undefined,
295 - selectionProgress: number,
296 291 ): void {
297 292 if (width <= 0) return;
298 293 for (let offset = 0; offset < width; offset++) {
306 301 const selected = atom && selectedKeys?.has(normalizeKey(atom.key));
307 - const revealed = hashFraction(offset, x) <= selectionProgress;
308 - put(
309 - canvas,
310 - x + offset,
311 - y,
312 - " ",
313 - selected && revealed ? "selectedBlock" : baseStyle,
314 - );
302 + const glyph = selectedKeys ? atomGlyph(atom, atoms) : " ";
303 + put(canvas, x + offset, y, glyph, selected ? "selectedBlock" : baseStyle);
316 305 const label = fittingLabel(width, labels);
317 306 if (label) putPreservingStyle(canvas, x, y, label);
322 311 state: DecayDisplayState,
323 312 frame: number,
324 313 completedChunks: number,
325 - selectionProgress: number,
326 314 ): void {
327 315 const capacity = Math.max(0, canvas[0].length - MAP_START);
328 316 const summary = state.summaryTokens;
329 317 const kept = state.keptTokens;
330 - const known = summary !== undefined && summary > 0 && kept !== undefined;
318 + const knownKept =
319 + kept !== undefined && kept >= 0 && kept <= state.inputTokens;
331 320 renderMapPrefix(
332 321 canvas,
334 323 "NEW",
335 - known ? `~${compactTokens(summary + kept)}` : "?",
324 + knownKept && summary !== undefined
325 + ? `~${compactTokens(summary + kept)}`
326 + : "?",
337 328 if (capacity === 0) return;
338 - if (!known) {
329 + if (!knownKept) {
339 330 const waiting =
340 331 state.phase === "classify"
341 332 ? `○ waiting for ${Math.max(0, state.totalChunks - completedChunks)} chunks`
342 - : selectionProgress < 1
343 - ? "○ selecting facts"
344 - : "○ waiting for summary";
333 + : "○ retained size unavailable";
345 334 put(canvas, MAP_START, 3, waiting, "dim");
346 335 return;
348 - if (kept < 0 || kept > state.inputTokens) {
349 - put(canvas, MAP_START, 3, "○ retained size unavailable", "dim");
350 - return;
351 - }
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)
338 + const scale = Math.max(1, state.inputTokens, kept + (summary ?? 0));
339 + const keepWidth = scaledWidth(kept, scale, capacity);
340 + const keepStart = MAP_START + capacity - keepWidth;
341 + const summaryCapacity = Math.max(0, keepStart - MAP_START);
342 + const summaryWidth =
343 + summary === undefined
344 + ? 0
345 + : Math.min(summaryCapacity, scaledWidth(summary, scale, capacity));
346 + if (summaryWidth > 0) {
347 + const summaryStart = keepStart - summaryWidth;
348 + fillBlock(canvas, summaryStart, 3, summaryWidth, "selectedBlock", []);
361 350 canvas,
362 - MAP_START + summaryWidth - 1,
351 + summaryStart,
364 353 SUMMARY_HEAD[frame % SUMMARY_HEAD.length],
365 354 "selectedBlock",
367 - fillBlock(canvas, MAP_START + summaryWidth, 3, keepWidth, "keepBlock", [
356 + } else {
357 + const waiting =
358 + state.phase === "classify"
359 + ? `○ waiting for ${Math.max(0, state.totalChunks - completedChunks)} chunks`
360 + : "○ waiting for summary";
361 + put(canvas, MAP_START, 3, waiting.slice(0, summaryCapacity), "dim");
363 + fillBlock(canvas, keepStart, 3, keepWidth, "keepBlock", [
368 364 ` ~${compactTokens(kept)} KEEP `,
369 365 " KEEP ",
370 366 " K ",
426 422 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;
425 +function atomGlyph(
426 + atom: AtomView | undefined,
427 + atoms: readonly AtomView[],
428 +): string {
429 + if (!atom) return " ";
430 + if ((atom.seenCount ?? 0) > 1) return "◆";
431 + const seen = atom.lastSeen ?? 0;
432 + const newest = Math.max(...atoms.map((item) => item.lastSeen ?? 0), seen);
433 + const oldest = Math.min(...atoms.map((item) => item.lastSeen ?? 0), seen);
434 + const ratio = (seen - oldest) / Math.max(1, newest - oldest);
435 + return ratio < 0.25 ? "░" : ratio < 0.5 ? "▒" : ratio < 0.75 ? "▓" : "█";
433 438 function normalizeKey(key: string): string {