feat(status): show session compaction count

Add a compactions row to the /smart-compact overlay counting the
session's persisted compaction entries via ctx.sessionManager, so the
number stays correct across resume and restart. An in-memory
session_compact counter remains as the fallback where sessionManager
is unavailable.
This commit is contained in:
AlexKucera committed 2026-11-16 17:20:24 +01:00
1 parent 8599ae8176
commit dc75ef8a7b
7 files changed
+113 -5

No files matched your search

+25 -1
View File
@@ -20,6 +20,7 @@ interface CtxOverrides {
window: number;
onCompact?: (opts: any) => void;
idle?: boolean;
entries?: any[];
}
function load(settings: Record<string, unknown>) {
@@ -50,7 +51,7 @@ function load(settings: Record<string, unknown>) {
return pi;
}
function makeCtx({ tokens, window, onCompact, idle = true }: CtxOverrides) {
function makeCtx({ tokens, window, onCompact, idle = true, entries = [] }: CtxOverrides) {
const calls: {
compact: number;
notified: string[];
@@ -63,6 +64,7 @@ function makeCtx({ tokens, window, onCompact, idle = true }: CtxOverrides) {
ctx: {
isIdle: () => idle,
getContextUsage: () => ({ tokens, contextWindow: window }),
sessionManager: { getEntries: () => entries },
ui: {
notify: (msg: string) => calls.notified.push(msg),
// Minimal ctx.ui.custom: captures overlay options, instantiates the
@@ -232,6 +234,7 @@ test("status command opens a centered overlay panel with thresholds", async () =
assert.match(text, /56% of window/);
assert.match(text, /warn threshold\s+55k \(crossed\)/);
assert.match(text, /force threshold\s+80k(?!\s*\(crossed\))/);
assert.match(text, /compactions\s+0/);
assert.match(text, /esc\/enter\/q to close/);
comp.handleInput("\x1b");
await pending; // resolves once done() fires
@@ -382,6 +385,27 @@ test("status falls back to notify when overlay UI is unavailable", async () => {
assert.doesNotMatch(text, /to close/); // hint is overlay-mode only
});
test("status counts compactions from the session tree; in-memory fallback without sessionManager", async () => {
const pi = load(FULL);
const entries = [{ type: "message" }, { type: "compaction" }, { type: "message" }, { type: "compaction" }, { type: "compaction" }];
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000, entries });
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
await new Promise((r) => setImmediate(r));
assert.match(calls.components[0].render(80).join("\n"), /compactions\s+3/);
calls.components[0].handleInput("\x1b");
await pending;
// No sessionManager: fall back to compactions this process has seen.
const { ctx: bareCtx, calls: bareCalls } = makeCtx({ tokens: 56_000, window: 100_000 });
delete (bareCtx as any).sessionManager;
await fire(pi, "session_compact", bareCtx);
const pending2 = pi.commands["smart-compact"].handler(undefined, bareCtx);
await new Promise((r) => setImmediate(r));
assert.match(bareCalls.components[0].render(80).join("\n"), /compactions\s+1/);
bareCalls.components[0].handleInput("\x1b");
await pending2;
});
test("usage bar hidden when window is unknown", async () => {
const pi = load(FULL);
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 0 });