Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 131 additions & 0 deletions website/playwright/optimize-cancel-mobile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { test, expect, type Page, type Route } from '@playwright/test'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { json, stubDashboardApi } from '../scripts/lib/stub-dashboard-api.mjs'

/**
* On a phone, the "Optimizing prompt..." overlay shows a visible, tappable
* Cancel while the optimize runs, and a tap gives back the draft, editable.
*
* Pearce hit this on iPhone Safari (#20003, handoff 11bb0734): the composer
* showed only the status, with no way out. Touch screens get no title tooltip,
* so under a coarse pointer the X grows into a labelled "Cancel" chip with a
* 44px touch target. jsdom evaluates no media queries and computes no layout,
* so only a real browser can check that the label is shown and the target is
* big enough. ChatInput.optimizeCancel.test.tsx pins the abort and late-result
* behaviour; this spec pins what a finger can see and hit.
*
* The phone runs at 390x844 and again at 390x480, the height left above the
* soft keyboard, because the overlay must stay usable while the keyboard is up.
* A desktop control keeps the 28px icon-only X with its tooltip.
*/

const catalog = (name: string) => JSON.parse(readFileSync(fileURLToPath(new URL(`../src/i18n/locales/${name}`, import.meta.url)), 'utf8'))
const manual = catalog('en.manual.json')
const generated = catalog('en.json')
const label = (key: string): string => {
const value = manual.components.chatInput[key] ?? generated.components?.chatInput?.[key]
if (typeof value !== 'string') throw new Error(`Missing English fixture label: components.chatInput.${key}`)
return value
}
const CANCEL_NAME = label('cancel_optimizing')
const CANCEL_LABEL = label('cancel_optimizing_short')
const OPTIMIZING = label('optimizing_prompt')
const SLOT = 'optimize-cancel-fixture'
const DRAFT = 'look at the failing build and tell me what broke'
const TOUCH_TARGET_PX = 44

async function openComposer(page: Page, baseURL: string) {
const errors: string[] = []
page.on('pageerror', error => errors.push(error.message))
await page.route('**/*', route => new URL(route.request().url()).origin === new URL(baseURL).origin
? route.continue() : route.abort('blockedbyclient'))
const optimize = { requests: 0 }
await stubDashboardApi(page, {
localStorageEntries: { 'mc-lang': 'en', 'mc-active-slot': SLOT, 'mc-active-slot-chat': SLOT },
slots: [{ key: SLOT, title: 'Build triage', running: false, messages: 2,
agent: 'kirocrew', memory_mode: 'persistent', project: '', folder_id: '',
modified: 1788780000, source_links: [], source_links_total: 0 }],
extra: async (path: string, route: Route) => {
if (path === `/api/chat/slots/${SLOT}`) {
await json(route, { running: false, has_more: false, total: 2, queue: [], project: '', messages: [
{ role: 'user', ts: 1788780000, content: 'Hi' },
{ role: 'assistant', ts: 1788780030, content: 'Ready.' },
] })
} else if (path === `/api/chat/slots/${SLOT}/source-links`) {
await json(route, { links: [], total: 0 })
} else if (path.startsWith('/api/optimizer/optimize')) {
// Never answered: only the page's abort ends the request.
optimize.requests++
} else return false
return true
},
})
const response = await page.goto(`/?slot=${SLOT}`, { waitUntil: 'domcontentloaded' })
expect(response?.ok()).toBe(true)
// The composer is what this spec drives; the transcript is incidental.
await expect(page.locator('textarea[data-composer-input]')).toBeVisible()
return { errors, optimize }
}

async function startOptimize(page: Page, touch: boolean) {
const composer = page.locator('textarea[data-composer-input]')
await composer.fill(DRAFT)
const optimizeButton = page.getByRole('button', { name: 'Optimize prompt' }).first()
if (touch) await optimizeButton.tap()
else await optimizeButton.click()
await expect(page.getByText(OPTIMIZING)).toBeVisible()
return composer
}

for (const height of [844, 480]) {
test.describe(`Phone ${390}x${height}: optimize overlay Cancel`, () => {
test.use({ viewport: { width: 390, height }, hasTouch: true, isMobile: true, reducedMotion: 'reduce' })

test('shows a labelled 44px Cancel and a tap returns the draft, editable', async ({ page, baseURL }) => {
const { errors, optimize } = await openComposer(page, baseURL!)
const composer = await startOptimize(page, true)
// The overlay paints on the mutation's start; the fetch can follow it.
await expect.poll(() => optimize.requests).toBe(1)

const cancel = page.getByRole('button', { name: CANCEL_NAME })
await expect(cancel).toBeVisible()
await expect(cancel.getByText(CANCEL_LABEL, { exact: true })).toBeVisible()
await expect(cancel).toBeInViewport({ ratio: 1 })
const box = (await cancel.boundingBox())!
expect(box.height).toBeGreaterThanOrEqual(TOUCH_TARGET_PX)
expect(box.width).toBeGreaterThanOrEqual(TOUCH_TARGET_PX)
// A hit test at the button's centre lands on the button, not the overlay.
const hit = await page.evaluate(([x, y]) => {
const el = document.elementFromPoint(x, y)
return !!el?.closest('.optimize-overlay-cancel')
}, [box.x + box.width / 2, box.y + box.height / 2])
expect(hit).toBe(true)

await cancel.tap()
await expect(cancel).toHaveCount(0)
await expect(composer).not.toHaveAttribute('readonly')
await expect(composer).toHaveValue(DRAFT)
await composer.press('End')
await composer.pressSequentially(' please')
await expect(composer).toHaveValue(`${DRAFT} please`)
expect(errors).toEqual([])
})
})
}

test.describe('Desktop: optimize overlay Cancel stays icon-only', () => {
test.use({ viewport: { width: 1280, height: 800 }, reducedMotion: 'reduce' })

test('shows the 28px X without the touch label', async ({ page, baseURL }) => {
const { errors } = await openComposer(page, baseURL!)
await startOptimize(page, false)
const cancel = page.getByRole('button', { name: CANCEL_NAME })
await expect(cancel).toBeVisible()
await expect(cancel.getByText(CANCEL_LABEL, { exact: true })).toBeHidden()
const box = (await cancel.boundingBox())!
expect(Math.round(box.width)).toBe(28)
expect(Math.round(box.height)).toBe(28)
expect(errors).toEqual([])
})
})
119 changes: 119 additions & 0 deletions website/scripts/capture-optimize-cancel.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Evidence capture: the "Optimizing prompt…" overlay on a phone, with its
// Cancel button, before and after the tap.
//
// Runs the REAL built SPA (website/dist) with the dashboard API stubbed and the
// optimizer request held open, so the overlay stays up until Cancel aborts it.
// Not a test: ChatInput.optimizeCancel.test.tsx pins the behaviour; this shows it.
//
// node scripts/capture-optimize-cancel.mjs [outDir]
import { chromium } from 'playwright'
import { mkdirSync, readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { serveDist } from './lib/serve-dist.mjs'
import { logPageProblems, stubDashboardApi, json } from './lib/stub-dashboard-api.mjs'

const OUT = process.argv[2] || '../temp-screenshots/optimize-cancel'
mkdirSync(OUT, { recursive: true })

const LOCALES = fileURLToPath(new URL('../src/i18n/locales/', import.meta.url))
const manual = JSON.parse(readFileSync(LOCALES + 'en.manual.json', 'utf-8'))
const CANCEL = manual.components.chatInput.cancel_optimizing
if (!CANCEL) throw new Error('cancel_optimizing missing from en.manual.json')

const SLOT = 'optimize-demo'
const DRAFT = 'look at the failing build and tell me what broke'
const now = Math.floor(Date.now() / 1000)
const slots = [{
key: SLOT, title: 'Build triage', running: false, stop_state: 'idle',
last_message: 'Ready.', messages: 2, agent: 'kirocrew', memory_mode: 'persistent',
modified: now, source_links: [], source_links_total: 0,
}]
const detail = {
running: false, has_more: false, total: 2, queue: [],
messages: [
{ role: 'user', ts: now - 120, content: 'Hi' },
{ role: 'assistant', ts: now - 110, content: 'Ready.' },
],
}

async function main() {
const { srv, base } = await serveDist()
const browser = await chromium.launch()
const context = await browser.newContext({
viewport: { width: 390, height: 844 }, deviceScaleFactor: 2, hasTouch: true, isMobile: true,
})
const page = await context.newPage()
logPageProblems(page)
let optimizeRequests = 0
await stubDashboardApi(page, {
slots,
extra: async (path, route) => {
if (path.startsWith('/api/chat/slots/')) { await json(route, detail); return true }
// Never answered: the page's abort is the only thing that ends it.
if (path.startsWith('/api/optimizer/optimize')) { optimizeRequests++; return true }
return false
},
})
await page.goto(base + `/chat?sid=${encodeURIComponent(SLOT)}`, { waitUntil: 'domcontentloaded' })
const box = page.getByRole('textbox', { name: /message/i }).first()
await box.waitFor({ timeout: 15000 })
await box.fill(DRAFT)
await page.getByRole('button', { name: 'Optimize prompt' }).first().tap()
const cancel = page.getByRole('button', { name: CANCEL })
await cancel.waitFor({ timeout: 5000 })
if (optimizeRequests !== 1) throw new Error(`expected 1 optimize request, saw ${optimizeRequests}`)
await page.waitForTimeout(400)
const size = await cancel.boundingBox()
console.log(`cancel target ${Math.round(size.width)}x${Math.round(size.height)} css px`)
await page.screenshot({ path: `${OUT}/mobile-optimizing.png` })

await cancel.tap()
await cancel.waitFor({ state: 'detached', timeout: 5000 })
if (await box.getAttribute('readonly') !== null) throw new Error('composer still read-only after Cancel')
await box.press('End')
await box.type(' please')
const value = await box.inputValue()
if (value !== DRAFT + ' please') throw new Error(`draft not editable after Cancel: ${JSON.stringify(value)}`)
await page.waitForTimeout(300)
await page.screenshot({ path: `${OUT}/mobile-after-cancel.png` })
await captureDesktop(browser, base)
await browser.close()
srv.close()
console.log(`wrote ${OUT}/{mobile-optimizing,mobile-after-cancel,desktop-optimizing}.png`)
}

// The mouse-pointer size of the same button (28 px) inside the overlay pill.
async function captureDesktop(browser, base) {
const context = await browser.newContext({ viewport: { width: 1280, height: 800 }, deviceScaleFactor: 2 })
const page = await context.newPage()
logPageProblems(page)
await stubDashboardApi(page, {
slots,
extra: async (path, route) => {
if (path.startsWith('/api/chat/slots/')) { await json(route, detail); return true }
return path.startsWith('/api/optimizer/optimize')
},
})
await page.goto(base + `/chat?sid=${encodeURIComponent(SLOT)}`, { waitUntil: 'domcontentloaded' })
const box = page.getByRole('textbox', { name: /message/i }).first()
await box.waitFor({ timeout: 15000 })
await box.fill(DRAFT)
await page.getByRole('button', { name: 'Optimize prompt' }).first().click()
const cancel = page.getByRole('button', { name: CANCEL })
await cancel.waitFor({ timeout: 5000 })
await page.waitForTimeout(400)
const size = await cancel.boundingBox()
console.log(`desktop cancel target ${Math.round(size.width)}x${Math.round(size.height)} css px`)
const pill = await cancel.evaluate(el => {
const r = el.parentElement.getBoundingClientRect()
return { x: r.x, y: r.y, width: r.width, height: r.height }
})
const pad = 120
await page.screenshot({
path: `${OUT}/desktop-optimizing.png`,
clip: { x: Math.max(0, pill.x - pad * 3), y: Math.max(0, pill.y - pad), width: pill.width + pad * 6, height: pill.height + pad * 2 },
})
await context.close()
}

main().catch(err => { console.error(err); process.exit(1) })
16 changes: 13 additions & 3 deletions website/src/components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useRef, useEffect, useLayoutEffect, useMemo, useCallback, useId, useSta
import { GUIDE_KEEP_CLEAR } from '../guide/trustRoot'
import { useGuidePredicate } from '../guide/guidePredicates'
import { markComposerResize } from '../utils/composerResize'
import { ArrowUp, Loader2, RotateCw, Sparkles, Target, CheckCircle, Lock, FolderOpen, ClipboardList, PenLine, MoreHorizontal, Terminal, Clock } from 'lucide-react'
import { ArrowUp, Loader2, RotateCw, Sparkles, Target, CheckCircle, Lock, FolderOpen, ClipboardList, PenLine, MoreHorizontal, Terminal, Clock, X } from 'lucide-react'
import SketchDialog from './SketchDialog'
import QuoteCard from '../pages/chat/QuoteCard'
import CopyBranchButton from './CopyBranchButton'
Expand Down Expand Up @@ -655,9 +655,10 @@ function ChatInput({
setTextareaRef(textarea)
attachListContinuation(textarea)
}, [setTextareaRef, attachListContinuation])
const { optimizeError, setOptimizeError, optimizePending, optimizing, optimizePrompt } = usePromptOptimizer({
const { optimizeError, setOptimizeError, optimizePending, optimizing, optimizePrompt, cancelOptimize } = usePromptOptimizer({
slotId, chatStore, valueRef, pasteBlocks, onChange, onOptimizeResult, lexicalComposer, lexicalLoadFailed, composerControl, inputRef,
valueFromUserRef, optimizingRef, appendUndoBoundary: appendBoundary, terminalCommands, optimizerMentionGuard,
composerDisabled: disabled,
})
useEffect(() => { onOptimizingChange?.(optimizing) }, [optimizing, onOptimizingChange])
// A locked composer (a memory-mode switch is moving its draft) must not
Expand Down Expand Up @@ -1289,7 +1290,16 @@ function ChatInput({
micHeldElsewhere={micHeldElsewhere} micHeldElsewhereLabel={micHeldElsewhereLabel} micHeldElsewhereAction={micHeldElsewhereAction} voiceHeldLanded={voiceHeldLanded}
/>

{optimizing && <span className="optimize-overlay absolute inset-0 flex items-center justify-center backdrop-blur-md pointer-events-none z-10 rounded-2xl"><span className="optimize-overlay-pill inline-flex items-center gap-2 px-4 py-2 rounded-full text-sm font-medium text-text"><Sparkles size={15} className="text-accent animate-pulse shrink-0" /> {i18nT('components.chatInput.optimizing_prompt')}</span></span>}
{/* The overlay itself lets taps through; only the pill's Cancel takes
them. Without Cancel a mis-tap on Optimize (it sits beside Send on
a phone) leaves the draft read-only until the request settles. */}
{optimizing && <span className="optimize-overlay absolute inset-0 flex items-center justify-center backdrop-blur-md pointer-events-none z-10 rounded-2xl"><span className="optimize-overlay-pill inline-flex items-center gap-2 pl-4 pr-1.5 py-1.5 rounded-full text-sm font-medium text-text"><Sparkles size={15} className="text-accent animate-pulse shrink-0" /> {i18nT('components.chatInput.optimizing_prompt')}<button
type="button"
className="optimize-overlay-cancel pointer-events-auto w-7 h-7 shrink-0 rounded-full border-none flex items-center justify-center cursor-pointer bg-transparent text-muted hover:text-text hover:bg-text/10 focus-visible:outline-2 focus-visible:outline-accent"
onClick={(e) => { e.stopPropagation(); e.preventDefault(); cancelOptimize() }}
aria-label={i18nT('components.chatInput.cancel_optimizing')}
title={i18nT('components.chatInput.cancel_optimizing')}
><X size={15} className="shrink-0" /><span className="optimize-overlay-cancel-label">{i18nT('components.chatInput.cancel_optimizing_short')}</span></button></span></span>}
{/* The textarea fallback is seamless for typing (draft intact), but the
failure itself must be user-visible, not only a console line: the
person who opted into the editor should know they are no longer in
Expand Down
Loading
Loading