Skip to content
Closed
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
108 changes: 108 additions & 0 deletions mobile/src/session/use-mobile-native-chat-answer-send-omp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Why: split from use-mobile-native-chat-answer-send.test.ts, which is at its max-lines cap.
// Takeover RPCs have their own send-site integration tests; these fixtures script PTY acknowledgements.
vi.mock('../terminal/worker-terminal-takeover-report', () => ({
reportWorkerTerminalUserInput: vi.fn()
}))

import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
buildNativeChatAskAnswerKeys,
formatAskAnswer,
type AskAnswerSelection,
type AskPrompt
} from '../../../src/shared/native-chat-ask'
import type { AgentType } from '../../../src/shared/native-chat-types'
import type { RpcClient } from '../transport/rpc-client'
import { MOBILE_NATIVE_CHAT_QUESTION_STEP_MS } from './mobile-native-chat-answer-stepping'
import { resetMobileNativeChatStaleInputForTests } from './mobile-native-chat-stale-input'
import { resetMobileNativeChatTerminalWritesForTests } from './mobile-native-chat-terminal-write-lock'
import {
useMobileNativeChatAnswerSend,
type MobileNativeChatAnswerSend
} from './use-mobile-native-chat-answer-send'

function acceptedResponse() {
return {
id: 'send',
ok: true as const,
result: { send: { accepted: true } },
_meta: { runtimeId: 'runtime' }
}
}

describe('useMobileNativeChatAnswerSend for OMP asks', () => {
let renderer: ReactTestRenderer | null = null
let answerSend: MobileNativeChatAnswerSend | null = null

beforeEach(() => {
vi.useFakeTimers()
resetMobileNativeChatStaleInputForTests()
resetMobileNativeChatTerminalWritesForTests()
})

afterEach(() => {
act(() => renderer?.unmount())
renderer = null
answerSend = null
vi.useRealTimers()
})

async function mount(client: RpcClient, agent: AgentType): Promise<void> {
function Harness(): null {
answerSend = useMobileNativeChatAnswerSend({
client,
enabled: true,
handleRef: { current: 'terminal' },
deviceTokenRef: { current: 'device' },
agentRef: { current: agent },
sessionId: 'session',
streamIdentity: 'host\0worktree\0tab\0session',
onSendError: vi.fn()
})
return null
}
await act(async () => {
renderer = create(createElement(Harness))
})
}

it('writes OMP groups from the shared key plan, paced, never as pasted labels', async () => {
const sendRequest = vi.fn().mockResolvedValue(acceptedResponse())
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This fixture exercises only the scripted sendRequest port.
await mount({ sendRequest } as unknown as RpcClient, 'omp')
const prompt: AskPrompt = {
questions: [
{ question: 'q1', multiSelect: false, options: [{ label: 'A' }, { label: 'B' }] },
{ question: 'q2', multiSelect: false, options: [{ label: 'C' }, { label: 'D' }] }
]
}
const selections: AskAnswerSelection[] = [{ indices: [1] }, { indices: [0] }]
// Why: the key bytes belong to the shared builder, so the expectation derives from it.
const expected = buildNativeChatAskAnswerKeys('omp', prompt, selections).map((group) =>
'raw' in group ? group.raw : group.text.replace(/[\r\n]+/g, ' ')
)
expect(expected.length).toBeGreaterThan(1)

let result: Promise<boolean> | undefined
await act(async () => {
result = answerSend?.answerAsk(prompt, selections)
})
expect(sendRequest).toHaveBeenCalledTimes(1)
for (let index = 1; index < expected.length; index += 1) {
await act(async () => {
await vi.advanceTimersByTimeAsync(MOBILE_NATIVE_CHAT_QUESTION_STEP_MS)
})
expect(sendRequest).toHaveBeenCalledTimes(index + 1)
}
await expect(result).resolves.toBe(true)
expect(sendRequest.mock.calls.map((call) => call[1])).toEqual(
expected.map((text) => expect.objectContaining({ text, enter: false }))
)
// Why: a pasted label answer is one Enter-terminated write; OMP must never send it.
expect(sendRequest.mock.calls.map((call) => call[1])).not.toContainEqual(
expect.objectContaining({ text: formatAskAnswer(prompt, selections), enter: true })
)
})
})
17 changes: 5 additions & 12 deletions mobile/src/session/use-mobile-native-chat-answer-send.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useCallback, useEffect, useRef, type MutableRefObject } from 'react'
import {
buildAskAnswerKeys,
buildCodexAskAnswerKeys,
buildNativeChatAskAnswerKeys,
formatAskAnswer,
hasAskAnswer,
type AskAnswerSelection,
Expand All @@ -18,12 +17,9 @@ import {
acquireMobileNativeChatTerminalWrite,
releaseMobileNativeChatTerminalWrite
} from './mobile-native-chat-terminal-write-lock'
import {
resolveNativeChatTranscriptAgent,
shouldStepNativeChatAskAnswer
} from '../../../src/shared/native-chat-agent-support'
import { shouldStepNativeChatAskAnswer } from '../../../src/shared/native-chat-agent-support'

/** Sends an ask-user answer to the active chat pane. Claude and Codex selectors
/** Sends an ask-user answer to the active chat pane. Claude, Codex and OMP selectors
* use their agent-specific keystrokes; other agents get pasted label text.
* Extracted from the session route to keep that file under its line cap and to
* own the pending-timer lifecycle in one place. */
Expand Down Expand Up @@ -224,7 +220,7 @@ export function useMobileNativeChatAnswerSend(args: {
}
return false
}
// Grok commits pasted labels; Claude and Codex need their selector-specific
// Grok commits pasted labels; Claude, Codex and OMP need their selector-specific
// keystrokes paced so each step renders before the next lands.
if (!shouldStepNativeChatAskAnswer(agentRef.current)) {
// This shape pastes the label into the composer and commits it, so an
Expand Down Expand Up @@ -259,10 +255,7 @@ export function useMobileNativeChatAnswerSend(args: {
const sent = (await sendTerminal(formatAskAnswer(prompt, selections), true)) || fail()
return sent && writeTurnsRef.current.get(handle) === turn
}
const groups =
resolveNativeChatTranscriptAgent(agentRef.current) === 'codex'
? buildCodexAskAnswerKeys(prompt, selections)
: buildAskAnswerKeys(prompt, selections)
const groups = buildNativeChatAskAnswerKeys(agentRef.current, prompt, selections)
for (let index = 0; index < groups.length; index += 1) {
if (generationRef.current !== generation) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { describe, expect, it } from 'vitest'
import {
buildAskAnswerKeys,
buildCodexAskAnswerKeys,
buildNativeChatAskAnswerKeys,
buildOmpAskAnswerKeys,
formatAskAnswer,
hasAskAnswer,
parseApprovalFromStatus,
parseAskFromStatus,
parseInteractivePrompt,
type AskPrompt
} from './native-chat-interactive-prompt'
import { AGENT_TUI_CLEAR_INPUT_MAX } from '../../../../shared/agent-tui-input-clear'

const ESC = String.fromCharCode(27)

Expand Down Expand Up @@ -302,3 +305,122 @@ for (const agent of ['opencode', 'opencode2']) {
expect(card?.options[0].send).toBe('\r')
})
}

describe('buildOmpAskAnswerKeys', () => {
const UP = '\x1b[A'
const DOWN = '\x1b[B'
const ENTER = '\r'
const RIGHT = '\x1b[C'
const SPACE = ' '
const CLEAR = { raw: AGENT_TUI_CLEAR_INPUT_MAX }

it('homes to row 0 then moves down to the picked option before Enter', () => {
expect(buildOmpAskAnswerKeys(single(['A', 'B', 'C']), [{ indices: [1] }])).toEqual([
CLEAR,
{ raw: `${UP}${UP}${UP}${DOWN}` },
{ raw: ENTER }
])
})

it('homes only when the first option is picked', () => {
expect(buildOmpAskAnswerKeys(single(['A', 'B', 'C']), [{ indices: [0] }])).toEqual([
CLEAR,
{ raw: `${UP}${UP}${UP}` },
{ raw: ENTER }
])
})

it('routes free text through the Other row and submits the editor', () => {
expect(
buildOmpAskAnswerKeys(single(['A', 'B', 'C']), [{ indices: [], other: 'Custom' }])
).toEqual([
CLEAR,
{ raw: `${UP}${UP}${UP}${DOWN}${DOWN}${DOWN}` },
{ raw: ENTER },
{ text: 'Custom' },
{ raw: ENTER }
])
})

it('toggles multi-select picks with Space, then Right to Review and Enter to submit', () => {
// OMP >= 18.2: Enter on a multi-select row confirms and advances; only Space toggles.
expect(buildOmpAskAnswerKeys(single(['A', 'B', 'C'], true), [{ indices: [0, 2] }])).toEqual([
CLEAR,
{ raw: `${UP}${UP}${UP}` },
{ raw: SPACE },
{ raw: `${UP}${UP}${UP}${DOWN}${DOWN}` },
{ raw: SPACE },
{ raw: RIGHT },
{ raw: ENTER }
])
})

it('does not step again after multi-select free text, which advances on its own', () => {
expect(
buildOmpAskAnswerKeys(single(['A', 'B', 'C'], true), [{ indices: [1], other: 'D' }])
).toEqual([
CLEAR,
{ raw: `${UP}${UP}${UP}${DOWN}` },
{ raw: SPACE },
{ raw: `${UP}${UP}${UP}${DOWN}${DOWN}${DOWN}` },
{ raw: ENTER },
{ text: 'D' },
{ raw: ENTER },
{ raw: ENTER }
])
const prompt: AskPrompt = {
questions: [
{ question: 'q1', multiSelect: true, options: [{ label: 'A' }, { label: 'B' }] },
{ question: 'q2', multiSelect: false, options: [{ label: 'C' }, { label: 'D' }] }
]
}
expect(buildOmpAskAnswerKeys(prompt, [{ indices: [], other: 'x' }, { indices: [1] }])).toEqual([
CLEAR,
{ raw: `${UP}${UP}${DOWN}${DOWN}` },
{ raw: ENTER },
{ text: 'x' },
{ raw: ENTER },
{ raw: `${UP}${UP}${DOWN}` },
{ raw: ENTER },
{ raw: ENTER }
])
})

it('steps past an unanswered question in a multi-question prompt', () => {
const prompt: AskPrompt = {
questions: [
{ question: 'q1', multiSelect: false, options: [{ label: 'A' }, { label: 'B' }] },
{ question: 'q2', multiSelect: false, options: [{ label: 'C' }, { label: 'D' }] }
]
}

expect(buildOmpAskAnswerKeys(prompt, [{ indices: [1] }, { indices: [] }])).toEqual([
CLEAR,
{ raw: `${UP}${UP}${DOWN}` },
{ raw: ENTER },
{ raw: RIGHT },
{ raw: ENTER }
])
})

it('returns no keys when nothing is answered', () => {
expect(buildOmpAskAnswerKeys(single(['A', 'B', 'C']), [{ indices: [] }])).toEqual([])
expect(buildOmpAskAnswerKeys(single(['A', 'B', 'C']), [])).toEqual([])
})
})

describe('buildNativeChatAskAnswerKeys', () => {
it('dispatches by agent to the matching builder', () => {
const prompt = single(['A', 'B', 'C'])
const selections = [{ indices: [1] }]
expect(buildNativeChatAskAnswerKeys('omp', prompt, selections)).toEqual(
buildOmpAskAnswerKeys(prompt, selections)
)
expect(buildNativeChatAskAnswerKeys('codex', prompt, selections)).toEqual(
buildCodexAskAnswerKeys(prompt, selections)
)
expect(buildNativeChatAskAnswerKeys('claude', prompt, selections)).toEqual(
buildAskAnswerKeys(prompt, selections)
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
import {
buildAskAnswerKeys,
buildCodexAskAnswerKeys,
buildNativeChatAskAnswerKeys,
buildOmpAskAnswerKeys,
formatAskAnswer,
hasAskAnswer,
parseAskFromStatus,
Expand All @@ -22,6 +24,8 @@ import {
export {
buildAskAnswerKeys,
buildCodexAskAnswerKeys,
buildNativeChatAskAnswerKeys,
buildOmpAskAnswerKeys,
formatAskAnswer,
hasAskAnswer,
parseAskFromStatus,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
shouldStepNativeChatAskAnswer
} from '../../../../shared/native-chat-agent-support'
import {
buildAskAnswerKeys,
buildCodexAskAnswerKeys,
buildNativeChatAskAnswerKeys,
formatAskAnswer,
hasAskAnswer,
type AskAnswerSelection,
Expand Down Expand Up @@ -118,9 +117,8 @@ export function useNativeChatInteractiveSend(
// Cancel any prior in-flight answer before starting a new one.
cancelInFlight()
const settings = getSettingsForAgentTabRuntimeOwner(terminalTabId)
// Selector TUIs ignore pasted labels; Codex uses a different key sequence.
// Selector TUIs ignore pasted labels; each agent's selector takes its own key sequence.
const stepsAnswer = shouldStepNativeChatAskAnswer(agent)
const buildsCodexAnswer = resolveNativeChatTranscriptAgent(agent) === 'codex'
// Why: pin the answered question's baseline BEFORE delivery. A late settle
// callback (paced writes + remote acceptance can span seconds on SSH) must
// not read the live status and mint a fresh baseline for a replacement
Expand Down Expand Up @@ -154,9 +152,7 @@ export function useNativeChatInteractiveSend(
? sendNativeChatAskAnswer(
settings,
targetPtyId,
buildsCodexAnswer
? buildCodexAskAnswerKeys(prompt, selections)
: buildAskAnswerKeys(prompt, selections),
buildNativeChatAskAnswerKeys(agent, prompt, selections),
onSettled
)
: sendNativeChatMessage(settings, targetPtyId, formatAskAnswer(prompt, selections), {
Expand Down
4 changes: 2 additions & 2 deletions src/shared/native-chat-agent-support.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ describe('nativeChatRequiresLocalTranscript', () => {
})

describe('shouldStepNativeChatAskAnswer', () => {
it('steps the digit-commit selector agents (Claude, OpenClaude, Codex)', () => {
it('steps the selector agents (Claude, OpenClaude, Codex, OpenCode, OMP)', () => {
expect(shouldStepNativeChatAskAnswer('claude')).toBe(true)
expect(shouldStepNativeChatAskAnswer('openclaude')).toBe(true)
// Codex 0.145's request_user_input card ignores typed labels and commits on
// the highlighted row, so pasted answers misdeliver like STA-1860.
expect(shouldStepNativeChatAskAnswer('codex')).toBe(true)
expect(shouldStepNativeChatAskAnswer('opencode')).toBe(true)
expect(shouldStepNativeChatAskAnswer('opencode2')).toBe(true)
expect(shouldStepNativeChatAskAnswer('omp')).toBe(true)
})

it('does not step other or unknown agents', () => {
expect(shouldStepNativeChatAskAnswer('grok')).toBe(false)
expect(shouldStepNativeChatAskAnswer('omp')).toBe(false)
expect(shouldStepNativeChatAskAnswer('cursor')).toBe(false)
expect(shouldStepNativeChatAskAnswer(null)).toBe(false)
expect(shouldStepNativeChatAskAnswer(undefined)).toBe(false)
Expand Down
5 changes: 4 additions & 1 deletion src/shared/native-chat-agent-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export function nativeChatRequiresLocalTranscript(agent: string | null | undefin
export function shouldStepNativeChatAskAnswer(agent: string | null | undefined): boolean {
const transcriptAgent = resolveNativeChatTranscriptAgent(agent)
return (
transcriptAgent === 'claude' || transcriptAgent === 'codex' || transcriptAgent === 'opencode'
transcriptAgent === 'claude' ||
transcriptAgent === 'codex' ||
transcriptAgent === 'opencode' ||
transcriptAgent === 'omp'
)
}

Expand Down
Loading