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

+28 -2
View File
@@ -216,6 +216,10 @@ export default function smartCompact(pi: any) {
let forceInFlight = false;
let lastForceFailureAt = 0;
let lastKnownTokens: number | null = null;
// Compactions observed by this process (session_compact). Panel fallback
// only; the panel prefers counting persisted compaction entries, which
// stays correct across resume/restart.
let sessionCompactions = 0;
/** min(absolute, fraction × window); absolute-only when window unknown. */
const effective = (absolute: number, fraction: number, window?: number | null): number =>
@@ -319,7 +323,10 @@ export default function smartCompact(pi: any) {
forceInFlight = false;
lastKnownTokens = null;
};
pi.on("session_compact", async () => resetCycle());
pi.on("session_compact", async () => {
sessionCompactions++;
resetCycle();
});
pi.on("session_compact_failed", async () => {
forceInFlight = false;
// Keep warnedThisCycle set — context is still large; don't re-nag.
@@ -327,7 +334,8 @@ export default function smartCompact(pi: any) {
// ── Debug/status command ─────────────────────────────────────────
// Shows a centered bordered overlay (the /dcp context look): current
// tokens, a usage bar, both effective thresholds, and cycle flags.
// tokens, a usage bar, both effective thresholds, cycle flags, and the
// session's compaction count.
// Dismissed with esc / enter / q / ctrl+c; notify() remains the
// fallback where ctx.ui.custom is unavailable (print/RPC mode).
pi.registerCommand?.("smart-compact", {
@@ -338,6 +346,23 @@ export default function smartCompact(pi: any) {
const forceAt = effective(cfg.forceTokens, cfg.forceFraction, window);
const fmt = (n: number): string => `${Math.round(n / 1000)}k`;
// Session-lifetime compaction count. Authoritative source: persisted
// `compaction` entries in the session tree (all branches — every
// compaction this session ever had, correct across resume/restart).
// Fallback: the in-memory counter (hosts without sessionManager;
// counts only this process's compactions).
const countCompactions = (): number => {
try {
const entries = ctx.sessionManager?.getEntries?.();
if (Array.isArray(entries)) {
return entries.filter((e: any) => e?.type === "compaction").length;
}
} catch {
/* unreadable tree — use the in-memory count */
}
return sessionCompactions;
};
const rows: StatusRow[] = [];
const add = (l: string, value: string, color?: string) =>
rows.push({ text: `${l} ${value}`, color });
@@ -372,6 +397,7 @@ export default function smartCompact(pi: any) {
);
add(pad("warned this cycle"), String(warnedThisCycle));
add(pad("force in flight"), String(forceInFlight));
add(pad("compactions"), String(countCompactions()));
await showStatusPanel(ctx, "smart-compact status", rows);
},