Fix ⌘B: dispatch/listen event-name mismatch; thinking pill → "Thinking (⇧⇥)"

Root cause (interlude, owner 2026-09-25): keyboard-shortcuts dispatches
registry actions on the lh:kbd-<action> convention, but the sidebar's
listener sat on the pre-convention name "lh:toggle-sidebar" — a name
nothing ever dispatches, so ⌘B silently did nothing while its tooltip
and cheat-sheet row kept claiming the binding. A sweep of every lh:
listener confirmed the sidebar was the lone stray (chat-renamed and
graph-revision are deliberate non-kbd events).

Fix, structural: kbdEventForAction(action) exported from
src/core/keyboard.ts — the ONE spelling — and both sides compose
through it (dispatcher's default branch, sidebar's toggle listener);
keyboard-shortcuts re-exports the helper so the contract pin can assert
no second spelling exists. The mismatch is now impossible by
construction.

Thinking pill: label trimmed to "Thinking" with its real shortcut via
the registry-composition path — "Thinking (⇧⇥)" (cycle-thinking,
rebind-live like every Hint suffix).

Pins (tests/keyboard-toggle-sidebar.test.ts): the full chain — mod+b →
actionForBinding → toggle-sidebar → kbdEventForAction →
lh:kbd-toggle-sidebar; formatBinding("shift+tab") → ⇧⇥; the
dispatcher-imports-the-same-helper contract.

Live-verified on :3001: ⌘B visually toggles on home, /chat/[id], and
/chat/new (sidebar 0↔288px, hamburger slot 0↔36); with the rebind
lh:keymap {"toggle-sidebar":"mod+j"}: ⌘J toggles and ⌘B is inert
(rebind-live); thinking pill renders "Thinking (⇧⇥)"; keymap restored,
sidebar left open.
This commit is contained in:
amosblomqvist
2026-09-25 02:39:11 +03:00
parent 6b83d3248b
commit b3c07f89f4
5 changed files with 85 additions and 7 deletions
+7 -1
View File
@@ -31,11 +31,17 @@ import {
eventBinding, eventBinding,
formatBinding, formatBinding,
isTypingSurface, isTypingSurface,
kbdEventForAction,
KEYBOARD_ACTIONS, KEYBOARD_ACTIONS,
} from "@/src/core"; } from "@/src/core";
import { isDictationActive } from "@/components/dictation"; import { isDictationActive } from "@/components/dictation";
import { useSettingsDialog } from "@/components/settings/settings-dialog.tsx"; import { useSettingsDialog } from "@/components/settings/settings-dialog.tsx";
// Re-exported for the contract pin (tests/keyboard-toggle-sidebar.test.ts):
// the dispatcher composes event names through the ONE helper — a second
// spelling here is how the ⌘B bug happened.
export { kbdEventForAction };
export function KeyboardShortcuts() { export function KeyboardShortcuts() {
const router = useRouter(); const router = useRouter();
const [cheatOpen, setCheatOpen] = useState(false); const [cheatOpen, setCheatOpen] = useState(false);
@@ -109,7 +115,7 @@ export function KeyboardShortcuts() {
handlersRef.current.setCheatOpen(true); handlersRef.current.setCheatOpen(true);
break; break;
default: default:
window.dispatchEvent(new CustomEvent(`lh:kbd-${action}`)); window.dispatchEvent(new CustomEvent(kbdEventForAction(action)));
} }
}; };
window.addEventListener("keydown", onKeyDown); window.addEventListener("keydown", onKeyDown);
+5 -2
View File
@@ -277,8 +277,11 @@ export function ModelControl() {
</button> </button>
</Hint> </Hint>
<Hint <Hint
// Trimmed (owner, subtlety round 2026-09-25). // "Thinking" + its real shortcut via the registry (owner interlude
label="Thinking level" // 2026-09-25): ⇧⇥ cycles the level, so "Thinking (⇧⇥)" —
// rebind-live like every Hint suffix.
label="Thinking"
action="cycle-thinking"
side="bottom" side="bottom"
> >
<button <button
+10 -4
View File
@@ -18,6 +18,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import type { ChatListItem } from "@/src/core"; import type { ChatListItem } from "@/src/core";
import { kbdEventForAction } from "@/src/core";
/** /**
* Toggleable sidebar — NOT a modal drawer: no backdrop, no focus theft. It * Toggleable sidebar — NOT a modal drawer: no backdrop, no focus theft. It
@@ -118,12 +119,17 @@ export function SidebarProvider({ children }: { children: React.ReactNode }) {
}); });
}, [narrow, open]); }, [narrow, open]);
// ⌘B (keyboard wave A): the global shortcut dispatches lh:toggle-sidebar; // ⌘B: the global shortcut dispatches kbdEventForAction("toggle-sidebar")
// the context is the one place that owns open state, so it listens here. // and the context answers on exactly that name — the shared spelling
// is the fix for the interlude bug (the listener sat on the pre-
// convention name "lh:toggle-sidebar" while the dispatcher sent
// "lh:kbd-toggle-sidebar", so ⌘B silently did nothing; owner
// 2026-09-25).
useEffect(() => { useEffect(() => {
const event = kbdEventForAction("toggle-sidebar");
const onToggle = () => toggle(); const onToggle = () => toggle();
window.addEventListener("lh:toggle-sidebar", onToggle); window.addEventListener(event, onToggle);
return () => window.removeEventListener("lh:toggle-sidebar", onToggle); return () => window.removeEventListener(event, onToggle);
}, [toggle]); }, [toggle]);
return ( return (
+12
View File
@@ -163,6 +163,18 @@ export function eventBinding(e: KeyboardEvent): string {
return [...mods, e.key.toLowerCase()].join("+"); return [...mods, e.key.toLowerCase()].join("+");
} }
/** The window event name a registry action dispatches/answers on —
* the ONE spelling shared by the dispatcher (keyboard-shortcuts) and
* every surface listener. Born from the ⌘B bug (owner interlude
* 2026-09-25): the sidebar listened on the pre-convention name
* "lh:toggle-sidebar" while the dispatcher sent "lh:kbd-toggle-sidebar",
* so ⌘B silently did nothing. With both sides composing through this
* helper the mismatch is structurally impossible.
*/
export function kbdEventForAction(action: KeyAction): string {
return `lh:kbd-${action}`;
}
/** True when the event lands in a typing surface (input, textarea, or a /** True when the event lands in a typing surface (input, textarea, or a
* contentEditable). In one, only mod/alt combos (and Tab, which types * contentEditable). In one, only mod/alt combos (and Tab, which types
* nothing) may fire — bare keys belong to the field (typing, caret). */ * nothing) may fire — bare keys belong to the field (typing, caret). */
+51
View File
@@ -0,0 +1,51 @@
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import {
actionForBinding,
bindingForAction,
formatBinding,
kbdEventForAction,
} from "../src/core/keyboard.ts";
import { kbdEventForAction as kbdEventForActionFromUi } from "../components/keyboard-shortcuts.tsx";
/**
* The ⌘B sidebar bug interlude (owner, 2026-09-25): ⌘B did nothing because
* the sidebar listened on the pre-convention "lh:toggle-sidebar" while the
* dispatcher sent "lh:kbd-toggle-sidebar". The fix is structural — both
* sides compose the event name through the ONE helper — and these pins
* hold the chain together: the registry maps the keypress to the action,
* the helper spells the event, and the UI dispatcher imports the same
* helper (not its own copy).
*/
describe("kbdEventForAction — the one spelling for action events", () => {
it("dispatch and listen names are the helper's output", () => {
assert.equal(kbdEventForAction("toggle-sidebar"), "lh:kbd-toggle-sidebar");
assert.equal(kbdEventForAction("sessions-search"), "lh:kbd-sessions-search");
});
it("the dispatcher imports the same helper (no second spelling exists)", () => {
assert.equal(kbdEventForActionFromUi, kbdEventForAction);
});
});
describe("the ⌘B chain — keypress → binding → action → event", () => {
it("mod+b is the toggle-sidebar action", () => {
assert.equal(actionForBinding("mod+b"), "toggle-sidebar");
assert.equal(bindingForAction("toggle-sidebar"), "mod+b");
assert.equal(formatBinding("mod+b"), "⌘B");
});
it("the event the chain ends in is the one the sidebar answers on", () => {
const action = actionForBinding("mod+b");
assert.equal(action, "toggle-sidebar");
assert.equal(kbdEventForAction(action), "lh:kbd-toggle-sidebar");
});
});
describe("the thinking pill suffix — ⇧⇥ via the registry", () => {
it("shift+tab formats as ⇧⇥ and maps to cycle-thinking", () => {
assert.equal(actionForBinding("shift+tab"), "cycle-thinking");
assert.equal(formatBinding("shift+tab"), "⇧⇥");
});
});