feat(status): render state as centered overlay
Replace the /smart-compact notify() text with a ctx.ui.custom overlay panel (the /dcp context look): usage bar, threshold rows with crossed indicators, cycle flags, esc/enter/q/ctrl+c dismissal (keybindings-aware with a raw kitty/legacy fallback set), and notify() fallback for print/RPC mode. Hardened by a fresh-context reviewer fanout (ponytail/security/general): kitty explicit-modifier close forms added, render() now honors the requested width exactly with a degenerate-narrow fallback, label width simplified. Harness gains a ctx.ui.custom mock; 22 tests green.
This commit is contained in:
1 parent
692c9c6c37
commit
ea51a574c1
6 files changed
+424
-20
No files matched your search
+205
-7
@@ -51,13 +51,37 @@ function load(settings: Record<string, unknown>) {
|
||||
}
|
||||
|
||||
function makeCtx({ tokens, window, onCompact, idle = true }: CtxOverrides) {
|
||||
const calls: { compact: number; notified: string[] } = { compact: 0, notified: [] };
|
||||
const calls: {
|
||||
compact: number;
|
||||
notified: string[];
|
||||
overlays: { overlay?: boolean; overlayOptions?: Record<string, unknown> }[];
|
||||
components: any[];
|
||||
renders: number;
|
||||
} = { compact: 0, notified: [], overlays: [], components: [], renders: 0 };
|
||||
return {
|
||||
calls,
|
||||
ctx: {
|
||||
isIdle: () => idle,
|
||||
getContextUsage: () => ({ tokens, contextWindow: window }),
|
||||
ui: { notify: (msg: string) => calls.notified.push(msg) },
|
||||
ui: {
|
||||
notify: (msg: string) => calls.notified.push(msg),
|
||||
// Minimal ctx.ui.custom: captures overlay options, instantiates the
|
||||
// component, resolves the promise when done() fires.
|
||||
custom: (factory: any, opts?: any) => {
|
||||
calls.overlays.push(opts ?? {});
|
||||
return new Promise((resolve) => {
|
||||
calls.components.push(
|
||||
factory(
|
||||
{ requestRender: () => calls.renders++ },
|
||||
// Identity theme: styling is a no-op so assertions see plain text.
|
||||
{ fg: (_c: string, s: string) => s, bg: (_c: string, s: string) => s, bold: (s: string) => s },
|
||||
{},
|
||||
(result: unknown) => resolve(result),
|
||||
),
|
||||
);
|
||||
});
|
||||
},
|
||||
},
|
||||
compact: (opts: any) => {
|
||||
calls.compact++;
|
||||
(onCompact ?? ((o: any) => o?.onComplete?.({})))(opts);
|
||||
@@ -185,15 +209,189 @@ test("enabled: false registers nothing", () => {
|
||||
assert.equal(Object.keys(pi.commands).length, 0);
|
||||
});
|
||||
|
||||
test("status command reports thresholds and crossed indicators", async () => {
|
||||
test("status command opens a centered overlay panel with thresholds", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r)); // let the component factory run
|
||||
assert.equal(calls.overlays.length, 1);
|
||||
assert.equal(calls.overlays[0].overlay, true);
|
||||
assert.deepEqual(calls.overlays[0].overlayOptions, {
|
||||
width: "70%",
|
||||
minWidth: 50,
|
||||
maxHeight: "85%",
|
||||
anchor: "center",
|
||||
margin: 2,
|
||||
});
|
||||
assert.equal(calls.notified.length, 0); // overlay mode, not notify
|
||||
const comp = calls.components[0];
|
||||
const text = comp.render(80).join("\n");
|
||||
assert.match(text, /smart-compact status/);
|
||||
assert.match(text, /tokens now\s+56k/);
|
||||
assert.match(text, /window\s+100k/);
|
||||
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, /esc\/enter\/q to close/);
|
||||
comp.handleInput("\x1b");
|
||||
await pending; // resolves once done() fires
|
||||
assert.ok(calls.renders >= 1);
|
||||
});
|
||||
|
||||
test("overlay renders a bordered box of constant line width", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
const lines = calls.components[0].render(80) as string[];
|
||||
assert.ok(lines.length >= 5);
|
||||
for (const line of lines) {
|
||||
assert.equal(stripAnsi(line).length, 80); // borders and padding align
|
||||
}
|
||||
assert.match(stripAnsi(lines[0]!), /^┌─+┐$/);
|
||||
assert.match(stripAnsi(lines.at(-1)!), /^└─+┘$/);
|
||||
calls.components[0].handleInput("\r");
|
||||
await pending;
|
||||
});
|
||||
|
||||
test("overlay honors requested width, including degenerate narrow", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
const comp = calls.components[0];
|
||||
const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
for (const line of comp.render(10) as string[]) {
|
||||
assert.equal(stripAnsi(line).length, 10); // never exceeds the given width
|
||||
}
|
||||
const tiny = comp.render(4) as string[]; // below border floor: plain truncation
|
||||
assert.ok(tiny.length >= 5);
|
||||
for (const line of tiny) {
|
||||
assert.ok(stripAnsi(line).length <= 4);
|
||||
assert.ok(!line.includes("┌")); // no border glyphs at degenerate widths
|
||||
}
|
||||
comp.handleInput("\x1b");
|
||||
await pending;
|
||||
});
|
||||
|
||||
test("render cache: same-width hit, width-change and invalidate rebuild", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
const comp = calls.components[0];
|
||||
const stripAnsi = (s: string) => s.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
const a = comp.render(80) as string[];
|
||||
assert.ok(comp.render(80) === a); // same width → cached instance
|
||||
const c = comp.render(60) as string[]; // width change → rebuild at 60
|
||||
assert.notEqual(c, a);
|
||||
assert.equal(stripAnsi(c[0]!).length, 60);
|
||||
comp.invalidate(); // theme change → rebuild even at the same width
|
||||
const d = comp.render(60) as string[];
|
||||
assert.notEqual(d, c);
|
||||
comp.handleInput("\x1b");
|
||||
await pending;
|
||||
});
|
||||
|
||||
test("overlay closes on esc, enter, q, ctrl+c (legacy and kitty forms) — not on other keys", async () => {
|
||||
for (const key of ["\x1b", "\r", "\n", "q", "\x03", "\x1b[27u", "\x1b[27;1u", "\x1b[13u", "\x1b[13;1u", "\x1b[113u", "\x1b[113;1u", "\x1b[99;5u"]) {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
calls.components[0].handleInput(key);
|
||||
await pending; // must resolve
|
||||
}
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
let closed = false;
|
||||
void pending.then(() => {
|
||||
closed = true;
|
||||
});
|
||||
calls.components[0].handleInput("x");
|
||||
calls.components[0].handleInput("\x1b[A"); // arrow-up sequence ≠ bare esc
|
||||
await new Promise((r) => setImmediate(r));
|
||||
assert.equal(closed, false); // still open
|
||||
calls.components[0].handleInput("\x1b");
|
||||
await pending;
|
||||
});
|
||||
|
||||
// Primary close path in the real TUI: keybindings.matches normalizes kitty
|
||||
// CSI-u sequences and honors user-remapped cancel/confirm bindings.
|
||||
test("overlay close prefers keybindings.matches (remapped bindings honored)", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
// Swap in a custom() mock whose keybindings only cancel on "ZZ".
|
||||
(ctx.ui as any).custom = (factory: any, opts: any) => {
|
||||
calls.overlays.push(opts ?? {});
|
||||
return new Promise((resolve) => {
|
||||
calls.components.push(
|
||||
factory(
|
||||
{ requestRender: () => calls.renders++ },
|
||||
{ fg: (_c: string, s: string) => s, bg: (_c: string, s: string) => s, bold: (s: string) => s },
|
||||
{ matches: (data: string, id: string) => id === "tui.select.cancel" && data === "ZZ" },
|
||||
(result: unknown) => resolve(result),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
let closed = false;
|
||||
void pending.then(() => {
|
||||
closed = true;
|
||||
});
|
||||
calls.components[0].handleInput("\x1b[27u"); // kitty esc — still closes via the CLOSE_KEYS fallback
|
||||
await pending;
|
||||
assert.equal(closed, true);
|
||||
|
||||
// And the remapped binding closes too, even though "ZZ" is in no set.
|
||||
const pi2 = load(FULL);
|
||||
const { ctx: ctx2, calls: calls2 } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
(ctx2.ui as any).custom = (factory: any, opts: any) => {
|
||||
calls2.overlays.push(opts ?? {});
|
||||
return new Promise((resolve) => {
|
||||
calls2.components.push(
|
||||
factory(
|
||||
{ requestRender: () => calls2.renders++ },
|
||||
{ fg: (_c: string, s: string) => s, bg: (_c: string, s: string) => s, bold: (s: string) => s },
|
||||
{ matches: (data: string, id: string) => id === "tui.select.cancel" && data === "ZZ" },
|
||||
(result: unknown) => resolve(result),
|
||||
),
|
||||
);
|
||||
});
|
||||
};
|
||||
const pending2 = pi2.commands["smart-compact"].handler(undefined, ctx2);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
calls2.components[0].handleInput("ZZ");
|
||||
await pending2;
|
||||
});
|
||||
|
||||
test("status falls back to notify when overlay UI is unavailable", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 100_000 });
|
||||
delete (ctx.ui as any).custom;
|
||||
await pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
assert.equal(calls.overlays.length, 0);
|
||||
const text = calls.notified.join("\n");
|
||||
assert.match(text, /smart-compact enabled/);
|
||||
assert.match(text, /warn threshold:\s+55k \(crossed\)/);
|
||||
assert.match(text, /force threshold:\s+80k/);
|
||||
assert.doesNotMatch(text, /force threshold:.*crossed/);
|
||||
assert.match(text, /smart-compact status/);
|
||||
assert.match(text, /56k/);
|
||||
assert.doesNotMatch(text, /to close/); // hint is overlay-mode only
|
||||
});
|
||||
|
||||
test("usage bar hidden when window is unknown", async () => {
|
||||
const pi = load(FULL);
|
||||
const { ctx, calls } = makeCtx({ tokens: 56_000, window: 0 });
|
||||
const pending = pi.commands["smart-compact"].handler(undefined, ctx);
|
||||
await new Promise((r) => setImmediate(r));
|
||||
const text = calls.components[0].render(80).join("\n");
|
||||
assert.match(text, /window\s+unknown/);
|
||||
assert.doesNotMatch(text, /% of window/);
|
||||
calls.components[0].handleInput("\x1b");
|
||||
await pending;
|
||||
});
|
||||
|
||||
test("force does not fire when settled reports not idle", async () => {
|
||||
|
||||
Reference in new issue
Block a user