Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(ok): apply agent settings menu picks instead of stealing focus (#3093)

sarah06d64b3742fd
diff --git a/.changeset/acp-settings-menu-pick-lost.md b/.changeset/acp-settings-menu-pick-lost.md
new file mode 100644
index 000000000..cb46d0740
--- /dev/null
+++ b/.changeset/acp-settings-menu-pick-lost.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Picking a value in an agent thread's settings menu now applies. Choosing a model, permission mode, or reasoning effort from one of the menu's submenus closed the submenu and jumped focus to the message field without ever sending the change to the agent, so the setting silently stayed where it was. The composer card focuses its message field when you press its whitespace; because React delivers a portaled menu's events through the component that opened it, that handler was also claiming presses inside the menu. It now acts only on presses that land in the card itself.
diff --git a/packages/app/src/components/acp/ThreadView.dom.test.tsx b/packages/app/src/components/acp/ThreadView.dom.test.tsx
index 2766a1076..867e14c04 100644
--- a/packages/app/src/components/acp/ThreadView.dom.test.tsx
+++ b/packages/app/src/components/acp/ThreadView.dom.test.tsx
@@ -254,15 +254,49 @@ describe('ThreadView agent settings', () => {
await userEvent.click(fastRow);
expect(setConfigOption).toHaveBeenCalledWith('thread-1', 'fast', true);
- // Open the Model submenu and pick Opus. Radix menu items select on the
- // `click` event (handleSelect), which fireEvent dispatches directly;
- // userEvent's pointer sequence instead trips Radix's submenu grace logic
- // and never fires the RadioGroup's onValueChange.
+ // Open the Model submenu and pick Opus.
await userEvent.click(modelRow);
- fireEvent.click(await screen.findByTestId('agent-thread-config-option-opus'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-opus'));
expect(setConfigOption).toHaveBeenCalledWith('thread-1', 'model', 'opus');
});
+ test('a pick lands on a real pointer press, not just a synthetic click', async () => {
+ // The settings trigger lives inside the composer card, and that card's
+ // `mousedown` handler focuses the textarea when a press lands on its
+ // whitespace. React portals bubble synthetic events along the REACT tree,
+ // so presses inside the portaled menu reach that handler too. It used to
+ // claim them: the submenu closed, focus jumped to the composer, and the
+ // pick never reached the agent. Only a full pointer sequence shows it — a
+ // bare `fireEvent.click` dispatches no `mousedown` at all.
+ render(
+ <ThreadView
+ info={makeInfo({
+ status: 'ready',
+ configOptions: [
+ {
+ id: 'model',
+ name: 'Model',
+ category: 'model',
+ type: 'select',
+ currentValue: 'sonnet',
+ options: [
+ { value: 'sonnet', name: 'Sonnet' },
+ { value: 'opus', name: 'Opus' },
+ ],
+ },
+ ],
+ })}
+ />,
+ );
+
+ await userEvent.click(screen.getByRole('button', { name: 'Agent settings' }));
+ await userEvent.click(screen.getByTestId('agent-thread-config-model'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-opus'));
+
+ expect(setConfigOption).toHaveBeenCalledWith('thread-1', 'model', 'opus');
+ expect(document.activeElement).not.toBe(screen.getByTestId('agent-thread-composer'));
+ });
+
test('remembers every pick for this agent, modes included', async () => {
const key = agentSettingsKey({ source: 'registry', id: 'claude' });
render(
@@ -301,7 +335,7 @@ describe('ThreadView agent settings', () => {
// A model pick is remembered, so the next thread with this agent opens on it.
await userEvent.click(screen.getByTestId('agent-thread-config-model'));
- fireEvent.click(await screen.findByTestId('agent-thread-config-option-opus'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-opus'));
expect(setConfigOption).toHaveBeenCalledWith('thread-1', 'model', 'opus');
expect(getRememberedAgentConfig(key)).toEqual({ model: 'opus' });
@@ -311,7 +345,7 @@ describe('ThreadView agent settings', () => {
// reopen it.)
await userEvent.click(screen.getByRole('button', { name: 'Agent settings' }));
await userEvent.click(screen.getByTestId('agent-thread-config-permission'));
- fireEvent.click(await screen.findByTestId('agent-thread-config-option-bypass'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-bypass'));
expect(setConfigOption).toHaveBeenCalledWith('thread-1', 'permission', 'bypass');
expect(getRememberedAgentConfig(key)).toEqual({ model: 'opus', permission: 'bypass' });
});
@@ -402,10 +436,9 @@ describe('ThreadView agent settings (modes)', () => {
await userEvent.click(screen.getByRole('button', { name: 'Agent settings' }));
// Legacy modes render as an "Agent mode" submenu (synthetic id 'legacy-mode');
- // open it and pick Ask. fireEvent for the radio select — see the Model
- // submenu note above on why userEvent doesn't fire onValueChange.
+ // open it and pick Ask.
await userEvent.click(screen.getByTestId('agent-thread-config-legacy-mode'));
- fireEvent.click(await screen.findByTestId('agent-thread-config-option-ask'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-ask'));
expect(setMode).toHaveBeenCalledWith('thread-1', 'ask');
// The legacy surface has no config option to ride, so it persists under its
// own key — but it persists just the same.
@@ -432,7 +465,7 @@ describe('ThreadView agent settings (modes)', () => {
await userEvent.click(screen.getByRole('button', { name: 'Agent settings' }));
await userEvent.click(screen.getByTestId('agent-thread-config-legacy-mode'));
- fireEvent.click(await screen.findByTestId('agent-thread-config-option-yolo'));
+ await userEvent.click(await screen.findByTestId('agent-thread-config-option-yolo'));
expect(setMode).toHaveBeenCalledWith('thread-1', 'yolo');
// Guards against a relapse into gating persistence on permissiveness: the
// accent is what makes a permissive mode legible, not refusing to keep it.
diff --git a/packages/app/src/components/focus-composer-on-card-pointer.dom.test.ts b/packages/app/src/components/focus-composer-on-card-pointer.dom.test.ts
new file mode 100644
index 000000000..8949c330a
--- /dev/null
+++ b/packages/app/src/components/focus-composer-on-card-pointer.dom.test.ts
@@ -0,0 +1,72 @@
+/**
+ * The composer card's "press the whitespace, focus the field" handler.
+ *
+ * The three branches it has to keep apart, since two of them look identical
+ * from inside the handler: a press on the card's own padding (act), a press on
+ * a control the card owns (leave it alone), and a press that only *bubbles*
+ * through the card from a portaled floater opened by one of those controls
+ * (leave it alone). React portals bubble synthetic events along the React
+ * tree, so the third arrives here with the card as `currentTarget` even though
+ * the floater renders under `document.body`.
+ */
+
+import { afterEach, describe, expect, test } from 'vitest';
+import { focusComposerInputOnCardPointer } from './focus-composer-on-card-pointer';
+
+function harness() {
+ const card = document.createElement('div');
+ const padding = document.createElement('div');
+ const control = document.createElement('button');
+ card.append(padding, control);
+ // A Radix menu row: rendered under `document.body`, outside the card, but
+ // reached by the card's React `onMouseDown` all the same.
+ const portaledRow = document.createElement('div');
+ portaledRow.setAttribute('role', 'menuitemradio');
+ document.body.append(card, portaledRow);
+
+ let prevented = 0;
+ let focused = 0;
+ const inputRef = { current: { focus: () => (focused += 1) } };
+ const press = (target: HTMLElement) => {
+ focusComposerInputOnCardPointer(
+ { target, currentTarget: card, preventDefault: () => (prevented += 1) },
+ inputRef,
+ );
+ };
+
+ return {
+ padding,
+ control,
+ portaledRow,
+ press,
+ counts: () => ({ prevented, focused }),
+ };
+}
+
+afterEach(() => {
+ document.body.replaceChildren();
+});
+
+describe('the composer card pointer affordance', () => {
+ test('a press on the card whitespace focuses the field', () => {
+ // Without `preventDefault` focus visibly bounces to the card first and a
+ // text-selection drag starts on the padding.
+ const { padding, press, counts } = harness();
+ press(padding);
+ expect(counts()).toEqual({ prevented: 1, focused: 1 });
+ });
+
+ test("a press on a control the card owns is the control's", () => {
+ const { control, press, counts } = harness();
+ press(control);
+ expect(counts()).toEqual({ prevented: 0, focused: 0 });
+ });
+
+ test('a press inside a portaled floater is left alone', () => {
+ // Claiming it stole focus out of the open menu, which dismissed the
+ // submenu layer before the row's `click` landed — so the pick was lost.
+ const { portaledRow, press, counts } = harness();
+ press(portaledRow);
+ expect(counts()).toEqual({ prevented: 0, focused: 0 });
+ });
+});
diff --git a/packages/app/src/components/focus-composer-on-card-pointer.ts b/packages/app/src/components/focus-composer-on-card-pointer.ts
index 53c0a6a1f..e243af8b6 100644
--- a/packages/app/src/components/focus-composer-on-card-pointer.ts
+++ b/packages/app/src/components/focus-composer-on-card-pointer.ts
@@ -35,15 +35,32 @@ const INTERACTIVE_TARGET_SELECTOR =
* bounces to the card first, and no text-selection drag starts on the padding.
*/
export function focusComposerInputOnCardPointer(
- event: { target: EventTarget | null; preventDefault: () => void },
+ event: {
+ target: EventTarget | null;
+ currentTarget: EventTarget | null;
+ preventDefault: () => void;
+ },
// Any focusable input handle — the ProseMirror `ComposerMentionInputHandle`
// (Ask AI composers) or a native `HTMLTextAreaElement` (the agent-thread
// composer). Both expose `focus()`.
inputRef: RefObject<{ focus: () => void } | null>,
): void {
- if (!(event.target instanceof HTMLElement) || event.target.closest(INTERACTIVE_TARGET_SELECTOR)) {
+ if (!(event.target instanceof HTMLElement)) return;
+ // React portals bubble synthetic events along the REACT tree, not the DOM
+ // tree, so a press inside a menu/popover opened from a control on this card
+ // arrives here even though the floater renders under `document.body`. Only a
+ // press that physically lands in the card is the card's to handle: claiming
+ // the others stole focus out of the open menu, dismissing it before the row's
+ // `click` landed. The role list below cannot stand in for this — menu rows
+ // carry `menuitemradio` / `menuitemcheckbox`, and each new floater brings
+ // roles of its own.
+ if (
+ !(event.currentTarget instanceof HTMLElement) ||
+ !event.currentTarget.contains(event.target)
+ ) {
return;
}
+ if (event.target.closest(INTERACTIVE_TARGET_SELECTOR)) return;
event.preventDefault();
inputRef.current?.focus();
}