From 20d8d6d07515166abebe3647500b212787698220 Mon Sep 17 00:00:00 2001 From: Badcuban <108198679+badcuban@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:19:14 -0500 Subject: [PATCH 1/2] fix(web): a long press on an agent pill keeps its menu open on touch On a touch screen, a long press on an agent pill in the model menu opened its menu, and lifting the finger closed it again, so there was no way to rename or remove an agent on a phone. The menu comes up under the finger that is still holding the pill. When the finger lifts, the browser follows it with a mouse press at that spot, which now lands outside the menu, and the menu took it for a tap elsewhere. The pill now cancels that close while the holding finger is down and for 400ms after it lifts. Later taps outside still close the menu. A new press on the open menu ends the hold, so a slow tap on Rename or Remove is still not timed as another hold on the pill. The new browser test drives real touches through the devtools protocol: faked pointer events don't send the mouse press that follows a lifted finger, which is how this passed before. --- .../components/ChatView.agents.browser.tsx | 107 +++++++++++++++++- .../web/src/components/chat/RoomAgentsBar.tsx | 55 +++++++-- 2 files changed, 151 insertions(+), 11 deletions(-) diff --git a/apps/web/src/components/ChatView.agents.browser.tsx b/apps/web/src/components/ChatView.agents.browser.tsx index 0c57bc3af..4317177d6 100644 --- a/apps/web/src/components/ChatView.agents.browser.tsx +++ b/apps/web/src/components/ChatView.agents.browser.tsx @@ -28,7 +28,7 @@ import { createModelCapabilities } from "@threadlines/shared/model"; import { secretAnswerMessageId } from "@threadlines/shared/secretRequests"; import * as Effect from "effect/Effect"; import { scopedThreadKey, scopeThreadRef } from "@threadlines/client-runtime"; -import { page, userEvent } from "vite-plus/test/browser"; +import { cdp, page, userEvent } from "vite-plus/test/browser"; import { describe, expect, it, vi } from "vite-plus/test"; import { render } from "vitest-browser-react"; @@ -1823,6 +1823,111 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { } }); + it("opens a pill's menu on a long press in the phone drawer, and keeps it open when the finger lifts", async () => { + const astraId = "7a0b1c2d-3e4f-4a5b-8c6d-7e8f9a0b1c2d" as ThreadParticipantId; + const base = createSnapshotForTargetUser({ + targetMessageId: "msg-user-room-hold" as MessageId, + targetText: "room hold target", + }); + // Real touches, so the browser follows a lifted finger with the mouse + // press and click a phone sends: those land on whatever is under the + // finger by then, which is the menu's backdrop. + const session = cdp() as unknown as { + send: (method: string, params?: object) => Promise; + }; + await session.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 5 }); + const frame = window.frameElement?.getBoundingClientRect(); + const touch = async (element: Element, holdMs: number) => { + const box = element.getBoundingClientRect(); + const point = { + x: box.left + box.width / 2 + (frame?.left ?? 0), + y: box.top + box.height / 2 + (frame?.top ?? 0), + }; + await session.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [point] }); + await new Promise((resolve) => setTimeout(resolve, holdMs)); + await session.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] }); + // The mouse press and click that follow a lifted finger. + await new Promise((resolve) => setTimeout(resolve, 100)); + }; + const mounted = await mountChatView({ + viewport: PHONE_VIEWPORT, + configureFixture: withCodexModels, + snapshot: { + ...base, + threads: base.threads.map((thread) => + thread.id === THREAD_ID + ? { + ...thread, + participants: [ + { + id: astraId, + handle: "agent-2", + modelSelection: { + instanceId: ProviderInstanceId.make("codex"), + model: "gpt-6-astra", + }, + joinedAt: NOW_ISO, + leftAt: null, + }, + ], + } + : thread, + ), + }, + }); + + try { + const pill = (id: string) => + document.querySelector(`[data-room-agent-row="${id}"]`); + const sheet = () => document.querySelector('[data-model-picker-sheet="true"]'); + const menuItem = (text: string) => + [...document.querySelectorAll('[role="menuitem"]')].find( + (item) => item.textContent === text, + ) ?? null; + const picker = await waitForElement( + () => document.querySelector('[data-chat-provider-model-picker="true"]'), + "Unable to find the composer's model picker.", + ); + await touch(picker, 40); + await waitForElement(sheet, "A tap should open the picker as a drawer."); + const astra = await waitForElement(() => pill(astraId), "Unable to find astra's pill."); + // The drawer slides in: a touch is aimed at where the pill comes to rest. + await vi.waitFor(async () => { + const top = astra.getBoundingClientRect().top; + await nextFrames(2); + expect(astra.getBoundingClientRect().top).toBe(top); + }); + + // The hold opens the menu, and lifting the finger leaves it open, + // without picking the pill or closing the drawer. + await touch(astra, 700); + expect(menuItem("Rename…")).not.toBeNull(); + expect(pill(astraId)?.getAttribute("aria-checked")).toBe("false"); + expect(sheet()).not.toBeNull(); + + // A tap outside the menu still closes it, and only it. + await new Promise((resolve) => setTimeout(resolve, 500)); + await touch(document.querySelector('[data-slot="menu-backdrop"]')!, 40); + await vi.waitFor(() => expect(menuItem("Rename…")).toBeNull()); + expect(sheet()).not.toBeNull(); + + // A tap on Rename, even a slow one, opens the name box, focused, in the + // open drawer: it is not taken for another hold on the pill. + await touch(astra, 700); + await touch(menuItem("Rename…")!, 600); + const nameBox = await waitForElement( + () => pill(astraId)!.querySelector("input"), + "Rename should open the pill's name box.", + ); + await vi.waitFor(() => expect(document.activeElement).toBe(nameBox)); + expect(sheet()).not.toBeNull(); + } finally { + await session.send("Emulation.setTouchEmulationEnabled", { enabled: false }); + useRoomRecipientStore.getState().choose(THREAD_REF, null); + await mounted.cleanup(); + } + }); + it("stacks a room's agents on a phone's model button, and drops idle ones first as it narrows", async () => { const agentIds = [ "7a0b1c2d-3e4f-4a5b-8c6d-7e8f9a0b1c2d", diff --git a/apps/web/src/components/chat/RoomAgentsBar.tsx b/apps/web/src/components/chat/RoomAgentsBar.tsx index 8b74ddecb..f0fcd619f 100644 --- a/apps/web/src/components/chat/RoomAgentsBar.tsx +++ b/apps/web/src/components/chat/RoomAgentsBar.tsx @@ -209,25 +209,50 @@ function AgentPill(props: { const onPill = (event: React.SyntheticEvent) => event.currentTarget.contains(event.target as Node); - const openMenu = useCallback(() => setMenuOpen(true), []); + // A held finger opened the menu, which came up under it: when it lifts, the + // browser's mouse press at that spot lands outside the menu and would close + // it. Set from the hold until the finger has been up for a moment. + const hold = useRef<{ liftedAt: number | null } | null>(null); + const holdStillDown = () => { + const liftedAt = hold.current?.liftedAt; + return liftedAt === null || (liftedAt !== undefined && performance.now() - liftedAt < 400); + }; + const openFromHold = useCallback(() => { + hold.current = { liftedAt: null }; + setMenuOpen(true); + }, []); const onContextMenu = useCallback( (event: React.MouseEvent) => { if (!event.currentTarget.contains(event.target as Node)) return; event.preventDefault(); - if (canEdit) openMenu(); + if (!canEdit) return; + // Android sends this for a held finger, ahead of the hold's own timer. + hold.current = + (event.nativeEvent as PointerEvent).pointerType === "touch" ? { liftedAt: null } : null; + setMenuOpen(true); }, - [canEdit, openMenu], + [canEdit], ); - const touchHold = useTouchHoldMenu({ open: openMenu, onContextMenu }); - // A press on the open menu is the menu's: as a hold on the pill it would - // swallow the tap on Rename or Remove. + const touchHold = useTouchHoldMenu({ open: openFromHold, onContextMenu }); + const lift = () => { + if (hold.current?.liftedAt === null) hold.current.liftedAt = performance.now(); + }; const pillTouchHold = { ...touchHold, + // A new press ends the hold. One on the open menu is the menu's own: timed + // as another hold on the pill, it would swallow the tap on Rename or Remove. onPointerDownCapture: (event: React.PointerEvent) => { - if (onPill(event)) touchHold.onPointerDownCapture(event); + hold.current = null; + touchHold.onPointerDownCapture(event); + if (!onPill(event)) touchHold.onPointerCancel(); + }, + onPointerUp: () => { + lift(); + touchHold.onPointerUp(); }, - onClickCapture: (event: React.MouseEvent) => { - if (onPill(event)) touchHold.onClickCapture(event); + onPointerCancel: () => { + lift(); + touchHold.onPointerCancel(); }, }; @@ -305,7 +330,17 @@ function AgentPill(props: { {/* Mounted while the name box is open too: a menu that unmounted as Rename opened it would pull focus back from the box. */} {hasMenu ? ( - + { + // The finger that held the pill, lifting. + if (!open && details.reason === "outside-press" && holdStillDown()) { + details.cancel(); + return; + } + setMenuOpen(open); + }} + > Date: Mon, 23 Nov 2026 10:34:27 -0500 Subject: [PATCH 2/2] test(web): the long-press pill test replays its touch events by hand The test drove real touches through the devtools protocol, which puts the whole test browser in touch mode for a while. On the PR's CI run a hover test in another file (MessagesTimeline, "exposes a branch action on completed assistant messages") failed a minute later in the same shard. It passes locally in the same tab right after this test, so the link is not proven, but that test has not failed in other recent runs. The test now dispatches the same sequence itself: touch pointer events on the held element, then the mouse press and click on whatever is under the finger by then. It still fails on the old code and leaves the browser's input mode alone. --- .../components/ChatView.agents.browser.tsx | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/apps/web/src/components/ChatView.agents.browser.tsx b/apps/web/src/components/ChatView.agents.browser.tsx index 4317177d6..d5aed0e21 100644 --- a/apps/web/src/components/ChatView.agents.browser.tsx +++ b/apps/web/src/components/ChatView.agents.browser.tsx @@ -28,7 +28,7 @@ import { createModelCapabilities } from "@threadlines/shared/model"; import { secretAnswerMessageId } from "@threadlines/shared/secretRequests"; import * as Effect from "effect/Effect"; import { scopedThreadKey, scopeThreadRef } from "@threadlines/client-runtime"; -import { cdp, page, userEvent } from "vite-plus/test/browser"; +import { page, userEvent } from "vite-plus/test/browser"; import { describe, expect, it, vi } from "vite-plus/test"; import { render } from "vitest-browser-react"; @@ -1829,24 +1829,29 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { targetMessageId: "msg-user-room-hold" as MessageId, targetText: "room hold target", }); - // Real touches, so the browser follows a lifted finger with the mouse - // press and click a phone sends: those land on whatever is under the - // finger by then, which is the menu's backdrop. - const session = cdp() as unknown as { - send: (method: string, params?: object) => Promise; - }; - await session.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 5 }); - const frame = window.frameElement?.getBoundingClientRect(); + // What a phone sends for a finger held on an element and lifted: touch + // pointer events on it, then a mouse press and a click on whatever is + // under the finger by then. For a held pill that is the menu's backdrop, + // which came up under the finger. (Replayed by hand: real touches would + // put the whole test browser in touch mode.) const touch = async (element: Element, holdMs: number) => { const box = element.getBoundingClientRect(); - const point = { - x: box.left + box.width / 2 + (frame?.left ?? 0), - y: box.top + box.height / 2 + (frame?.top ?? 0), + const at = { + clientX: box.left + box.width / 2, + clientY: box.top + box.height / 2, + bubbles: true, + cancelable: true, + composed: true, }; - await session.send("Input.dispatchTouchEvent", { type: "touchStart", touchPoints: [point] }); + const finger = { ...at, pointerId: 7, pointerType: "touch", isPrimary: true }; + element.dispatchEvent(new PointerEvent("pointerdown", finger)); await new Promise((resolve) => setTimeout(resolve, holdMs)); - await session.send("Input.dispatchTouchEvent", { type: "touchEnd", touchPoints: [] }); - // The mouse press and click that follow a lifted finger. + element.dispatchEvent(new PointerEvent("pointerup", finger)); + const under = document.elementFromPoint(at.clientX, at.clientY) ?? element; + for (const type of ["mousedown", "mouseup", "click"]) { + under.dispatchEvent(new MouseEvent(type, at)); + } + // Let what those did reach the page. await new Promise((resolve) => setTimeout(resolve, 100)); }; const mounted = await mountChatView({ @@ -1888,8 +1893,8 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { () => document.querySelector('[data-chat-provider-model-picker="true"]'), "Unable to find the composer's model picker.", ); - await touch(picker, 40); - await waitForElement(sheet, "A tap should open the picker as a drawer."); + picker.click(); + await waitForElement(sheet, "A phone should open the picker as a drawer."); const astra = await waitForElement(() => pill(astraId), "Unable to find astra's pill."); // The drawer slides in: a touch is aimed at where the pill comes to rest. await vi.waitFor(async () => { @@ -1901,6 +1906,7 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { // The hold opens the menu, and lifting the finger leaves it open, // without picking the pill or closing the drawer. await touch(astra, 700); + expect(astra.hasAttribute("data-menu-open")).toBe(true); expect(menuItem("Rename…")).not.toBeNull(); expect(pill(astraId)?.getAttribute("aria-checked")).toBe("false"); expect(sheet()).not.toBeNull(); @@ -1908,6 +1914,7 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { // A tap outside the menu still closes it, and only it. await new Promise((resolve) => setTimeout(resolve, 500)); await touch(document.querySelector('[data-slot="menu-backdrop"]')!, 40); + expect(astra.hasAttribute("data-menu-open")).toBe(false); await vi.waitFor(() => expect(menuItem("Rename…")).toBeNull()); expect(sheet()).not.toBeNull(); @@ -1922,7 +1929,6 @@ describe("ChatView (full app): agent browser tabs and rooms", () => { await vi.waitFor(() => expect(document.activeElement).toBe(nameBox)); expect(sheet()).not.toBeNull(); } finally { - await session.send("Emulation.setTouchEmulationEnabled", { enabled: false }); useRoomRecipientStore.getState().choose(THREAD_REF, null); await mounted.cleanup(); }