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
69 changes: 66 additions & 3 deletions src/main/browser/agent-browser-bridge-interaction-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import type {
} from '../../shared/runtime-types'
import { BrowserError } from './cdp-bridge'
import { WAIT_PROCESS_TIMEOUT_GRACE_MS } from './agent-browser-bridge-types'
import { acquireElectronDebugger } from './electron-debugger-lease'
import { parseCdpKeyEvent, imeFallbackKeyEvent } from './cdp-keyboard-us-layout'
import { AgentBrowserBridgeCaptureCommands } from './agent-browser-bridge-capture-commands'

export abstract class AgentBrowserBridgeInteractionCommands extends AgentBrowserBridgeCaptureCommands {
Expand Down Expand Up @@ -170,9 +172,70 @@ export abstract class AgentBrowserBridgeInteractionCommands extends AgentBrowser
worktreeId?: string,
browserPageId?: string
): Promise<BrowserKeypressResult> {
return this.enqueueTargetedCommand(worktreeId, browserPageId, async (sessionName) => {
return (await this.execAgentBrowser(sessionName, ['press', key])) as BrowserKeypressResult
})
return this.enqueueTargetedCommand(
worktreeId,
browserPageId,
async (sessionName, target) => {
const parsed = parseCdpKeyEvent(key) ?? imeFallbackKeyEvent(key)
if (!parsed) {
// Why: a key name the table cannot express must not dispatch keyCode 0 and
// report success — route it to the helper, creating its session only now so
// the direct path never pays for it.
await this.ensureSession(sessionName, target.browserPageId, target.webContentsId)
return (await this.execAgentBrowser(sessionName, ['press', key])) as BrowserKeypressResult
}
const wc = this.getWebContents(target.webContentsId)
if (!wc || wc.isDestroyed()) {
throw new BrowserError(
'browser_tab_not_found',
`Browser page ${target.browserPageId} is no longer available`
)
}
const event = {
windowsVirtualKeyCode: parsed.keyCode,
nativeVirtualKeyCode: parsed.keyCode,
key: parsed.key,
code: parsed.code,
modifiers: parsed.modifiers,
location: parsed.location
}
let releaseDebugger = (): void => {}
try {
releaseDebugger = acquireElectronDebugger(wc).release
await wc.debugger.sendCommand('Input.dispatchKeyEvent', {
// Why: rawKeyDown is the no-character form; sending keyDown without text
// makes Blink synthesize an empty input for editing keys.
type: parsed.text === null ? 'rawKeyDown' : 'keyDown',
...event,
...(parsed.text === null ? {} : { text: parsed.text, unmodifiedText: parsed.text })
})
await wc.debugger.sendCommand('Input.dispatchKeyEvent', {
type: 'keyUp',
...event,
// Why: the self bit is keydown-only -- Blink reports shiftKey false on the Shift keyup.
modifiers: parsed.modifiers & ~parsed.selfModifier
})
Comment on lines +212 to +217

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Release the key after a partial dispatch failure.

If keyDown succeeds and keyUp rejects while the page remains live, the catch block returns browser_error without another keyUp. The page has already observed keyDown, so it can retain a stuck key or modifier state.

Track whether keyDown completed. If keyUp fails on a live page, send a best-effort keyUp before rethrowing. Add a regression test where the first sendCommand resolves and the second rejects.

return { pressed: key }
} catch (error) {
// Why: attach/dispatch reject with plain Errors, which the RPC layer would report as
// runtime_error — the helper path this replaced always produced a browser_* code, and
// the pane only reclaims a dead page when it sees one.
if (error instanceof BrowserError) {
throw error
}
if (!this.getWebContents(target.webContentsId)) {
throw this.createPageUnavailableError(sessionName)
}
throw new BrowserError(
'browser_error',
`Failed to press ${key} in browser page ${target.browserPageId}: ${error instanceof Error ? error.message : String(error)}`
)
} finally {
releaseDebugger()
}
},
{ ensureSession: false }
)
}

async pdf(worktreeId?: string, browserPageId?: string): Promise<BrowserPdfResult> {
Expand Down
292 changes: 292 additions & 0 deletions src/main/browser/agent-browser-bridge-keypress-input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,292 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'

const { execFileMock, webContentsFromIdMock, existsSyncMock, readFileSyncMock, stdinWrites } =
vi.hoisted(() => {
const stdinWrites: string[] = []
return {
execFileMock: vi.fn(),
webContentsFromIdMock: vi.fn(),
existsSyncMock: vi.fn(() => false),
readFileSyncMock: vi.fn(() => Buffer.from('')),
stdinWrites
}
})

vi.mock('child_process', () => ({ execFile: execFileMock }))
vi.mock('fs', () => ({
existsSync: existsSyncMock,
readFileSync: readFileSyncMock,
accessSync: vi.fn(),
chmodSync: vi.fn(),
constants: { X_OK: 1 }
}))
vi.mock('os', () => ({ platform: () => 'darwin', arch: () => 'arm64' }))
vi.mock('electron', () => {
return {
app: { getPath: vi.fn(() => '/app'), getAppPath: vi.fn(() => '/project'), isPackaged: false },
webContents: { fromId: webContentsFromIdMock }
}
})
const { CdpWsProxyMock } = vi.hoisted(() => {
const instances: unknown[] = []
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const MockClass = vi.fn().mockImplementation(function (this: any, _wc: unknown) {
this._wc = _wc
this.start = vi.fn(async () => 'ws://127.0.0.1:9222')
this.stop = vi.fn(async () => {})
this.getPort = vi.fn(() => 9222)
instances.push(this)
})
return { CdpWsProxyMock: Object.assign(MockClass, { instances }) }
})

vi.mock('./cdp-ws-proxy', () => ({
CdpWsProxy: CdpWsProxyMock
}))

import { AgentBrowserBridge } from './agent-browser-bridge'
import {
createSucceedWith,
mockBrowserManager,
mockWebContents,
overrideBridgeWebContentsLookup,
resetAgentBrowserBridgeMocks,
type MockWebContents
} from './agent-browser-bridge-test-harness'

overrideBridgeWebContentsLookup(AgentBrowserBridge.prototype, webContentsFromIdMock)

const succeedWith = createSucceedWith(execFileMock, stdinWrites)

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}

function isStringArray(value: unknown): value is string[] {
return Array.isArray(value) && value.every((entry) => typeof entry === 'string')
}

function keyEventCalls(wc: MockWebContents): Record<string, unknown>[] {
return wc.debugger.sendCommand.mock.calls
.filter(([method]) => method === 'Input.dispatchKeyEvent')
.map(([, params]) => params)
.filter(isRecord)
}

describe('AgentBrowserBridge keypress input', () => {
let bridge: AgentBrowserBridge
let wc: MockWebContents

beforeEach(() => {
resetAgentBrowserBridgeMocks({
webContentsFromIdMock,
existsSyncMock,
readFileSyncMock,
stdinWrites,
cdpWsProxyInstances: CdpWsProxyMock.instances
})
bridge = new AgentBrowserBridge(mockBrowserManager())
bridge.setActiveTab(100)
wc = mockWebContents(100)
wc.debugger.sendCommand.mockResolvedValue({})
webContentsFromIdMock.mockImplementation((id: number) => (id === 100 ? wc : null))
})

it('dispatches a printable key over CDP without spawning agent-browser', async () => {
await expect(bridge.keypress('a', undefined, 'tab-1')).resolves.toEqual({ pressed: 'a' })

expect(execFileMock).not.toHaveBeenCalled()
expect(CdpWsProxyMock.instances).toHaveLength(0)
// Why: exactly two CDP calls, so the dispatch pair is the whole interaction.
expect(wc.debugger.sendCommand.mock.calls).toHaveLength(2)
expect(keyEventCalls(wc)).toEqual([
{
type: 'keyDown',
windowsVirtualKeyCode: 65,
nativeVirtualKeyCode: 65,
key: 'a',
code: 'KeyA',
modifiers: 0,
location: 0,
text: 'a',
unmodifiedText: 'a'
},
{
type: 'keyUp',
windowsVirtualKeyCode: 65,
nativeVirtualKeyCode: 65,
key: 'a',
code: 'KeyA',
modifiers: 0,
location: 0
}
])
})

it('types & as shifted 7 instead of colliding with the ArrowUp virtual key code', async () => {
await bridge.keypress('&', undefined, 'tab-1')

expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'keyDown',
windowsVirtualKeyCode: 55,
modifiers: 8,
text: '&'
})
})

it('dispatches editing and navigation keys as rawKeyDown with no text', async () => {
await bridge.keypress('ArrowDown', undefined, 'tab-1')

expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'rawKeyDown',
windowsVirtualKeyCode: 40,
key: 'ArrowDown'
})
expect(keyEventCalls(wc)[0]).not.toHaveProperty('text')
})

it('carries modifier masks for shortcuts', async () => {
await expect(bridge.keypress('Ctrl+Shift+K', undefined, 'tab-1')).resolves.toEqual({
pressed: 'Ctrl+Shift+K'
})

expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'rawKeyDown',
windowsVirtualKeyCode: 75,
modifiers: 10
})
})

it('reports the modifier bit on a bare Shift keydown but not on its keyup', async () => {
await bridge.keypress('Shift', undefined, 'tab-1')

expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'rawKeyDown',
windowsVirtualKeyCode: 16,
code: 'ShiftLeft',
modifiers: 8,
location: 1
})
expect(keyEventCalls(wc)[1]).toMatchObject({ type: 'keyUp', modifiers: 0, location: 1 })
})

it('keeps held modifiers on the keyup of a non-modifier shortcut key', async () => {
await bridge.keypress('Ctrl+Shift+K', undefined, 'tab-1')

expect(keyEventCalls(wc)[1]).toMatchObject({ type: 'keyUp', modifiers: 10 })
})

it('presses Enter with its carriage-return text so fields submit', async () => {
await bridge.keypress('Enter', undefined, 'tab-1')

expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'keyDown',
windowsVirtualKeyCode: 13,
text: '\r'
})
})

it('dispatches a non-US printable character as an IME-style event in process', async () => {
await expect(bridge.keypress('é', undefined, 'tab-1')).resolves.toEqual({ pressed: 'é' })

expect(execFileMock).not.toHaveBeenCalled()
expect(keyEventCalls(wc)[0]).toMatchObject({
type: 'keyDown',
windowsVirtualKeyCode: 229,
key: 'é',
code: '',
text: 'é',
unmodifiedText: 'é'
})
expect(keyEventCalls(wc)[1]).toMatchObject({ type: 'keyUp', windowsVirtualKeyCode: 229 })
})

it('keeps the helper for a surrogate-pair character', async () => {
succeedWith({ pressed: '👍' })

await expect(bridge.keypress('👍', undefined, 'tab-1')).resolves.toEqual({ pressed: '👍' })

expect(keyEventCalls(wc)).toHaveLength(0)
})

it('falls back to agent-browser for a key name the table cannot express', async () => {
succeedWith({ pressed: 'MediaPlayPause' })

await expect(bridge.keypress('MediaPlayPause', undefined, 'tab-1')).resolves.toEqual({
pressed: 'MediaPlayPause'
})

expect(keyEventCalls(wc)).toHaveLength(0)
const pressCall = execFileMock.mock.calls
.map(([, commandArgs]) => commandArgs)
.filter(isStringArray)
.find((commandArgs) => commandArgs.includes('press'))
expect(pressCall).toBeDefined()
const args = pressCall ?? []
expect(args[args.indexOf('press') + 1]).toBe('MediaPlayPause')
})

it('rejects with tab not found when the page is gone', async () => {
webContentsFromIdMock.mockReturnValue(null)

await expect(bridge.keypress('a', undefined, 'tab-1')).rejects.toMatchObject({
code: 'browser_tab_not_found'
})
})

// Why: one keypress looks the page up three times — the queued target, the
// automation-visibility refresh, then the dispatch guard. Serving the first N keeps the
// later ones on the guard; the trailing assertions fail loudly if that count ever moves.
function killPageAfterLookups(lookups: number): () => number {
let remaining = lookups
webContentsFromIdMock.mockImplementation((id: number) => {
if (id !== 100 || remaining === 0) {
return null
}
remaining -= 1
return wc
})
return () => remaining
}

it('rejects with tab not found when the page dies after its target is resolved', async () => {
const remaining = killPageAfterLookups(2)

await expect(bridge.keypress('a', undefined, 'tab-1')).rejects.toMatchObject({
code: 'browser_tab_not_found'
})
expect(remaining()).toBe(0)
expect(keyEventCalls(wc)).toHaveLength(0)
})

it('rejects with tab not found when the page dies mid-dispatch', async () => {
const remaining = killPageAfterLookups(3)
wc.debugger.sendCommand.mockRejectedValue(new Error('Inspected target navigated or closed'))

await expect(bridge.keypress('a', undefined, 'tab-1')).rejects.toMatchObject({
code: 'browser_tab_not_found'
})
expect(remaining()).toBe(0)
})

it('reports a dispatch failure on a live page as a browser error', async () => {
wc.debugger.sendCommand.mockRejectedValue(new Error('Debugger is not attached to the target'))

await expect(bridge.keypress('a', undefined, 'tab-1')).rejects.toMatchObject({
code: 'browser_error',
message: expect.stringContaining('Debugger is not attached to the target')
})
})

it('reports a debugger attach failure as a browser error', async () => {
wc.debugger.isAttached.mockReturnValue(false)
wc.debugger.attach.mockImplementation(() => {
throw new Error('Another debugger is already attached to the debug target')
})

await expect(bridge.keypress('a', undefined, 'tab-1')).rejects.toMatchObject({
code: 'browser_error'
})
expect(keyEventCalls(wc)).toHaveLength(0)
})
})
Loading