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
40 changes: 39 additions & 1 deletion mobile/src/session/MobileNativeChatOverlay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ type Tick = {
/** The host says a person's Stop is ending the turn. */
stopping?: boolean
acceptsImages?: boolean
agent?: string
structured?: boolean
activeTabId?: string | null
setTabView?: (tabId: string, view: 'terminal') => void
}

function overlayElement(tick: Tick): ReturnType<typeof createElement> {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the overlay reads only these controller members; the rest of the controller is unreachable from it.
const controller = {
showNativeChat: tick.show ?? true,
nativeChatSession: { messages: tick.messages ?? [], status: 'ready' },
nativeChatAgent: 'claude',
nativeChatAgent: tick.agent ?? 'claude',
nativeChatStructured: tick.structured ?? false,
setTabView: tick.setTabView ?? vi.fn(),
nativeChatAcceptsImages: tick.acceptsImages ?? true,
nativeChatAgentWorking: tick.streamLive ?? false,
nativeChatTurnIndicator: {
Expand All @@ -70,6 +76,7 @@ function overlayElement(tick: Tick): ReturnType<typeof createElement> {
} as unknown as MobileNativeChatController
return createElement(MobileNativeChatOverlay, {
controller,
activeSessionTabId: tick.activeTabId,
images: {} as never,
onMicPress: vi.fn(),
micActive: false,
Expand Down Expand Up @@ -238,6 +245,37 @@ describe('MobileNativeChatOverlay attach button', () => {
renderer = null
})

it('opens the exact active terminal tab for an OMP bridge question', async () => {
const setTabView = vi.fn()
await act(async () => {
renderer = create(overlayElement({ agent: 'omp', activeTabId: 'tab-omp', setTabView }))
})
const view = renderer!.root.find((node) => node.type === 'ChatView')
expect(view.props.askRequiresTerminal).toBe(true)
act(() => view.props.onAnswerInTerminal())
expect(setTabView).toHaveBeenCalledExactlyOnceWith('tab-omp', 'terminal')
})

it.each(['structured', 'claude', 'missing tab'])(
'keeps the terminal-only gate scoped correctly: %s',
async (scenario) => {
await act(async () => {
renderer = create(
overlayElement({
agent: scenario === 'claude' ? 'claude' : 'omp',
structured: scenario === 'structured',
activeTabId: scenario === 'missing tab' ? null : 'tab-omp'
})
)
})
const view = renderer!.root.find((node) => node.type === 'ChatView')
expect(view.props.askRequiresTerminal).toBe(scenario === 'missing tab')
if (scenario === 'missing tab') {
expect(view.props.onAnswerInTerminal).toBeUndefined()
}
}
)

it.each([true, false])('offers attach only when the agent takes images (%s)', async (accepts) => {
await act(async () => {
renderer = create(overlayElement({ acceptsImages: accepts }))
Expand Down
11 changes: 11 additions & 0 deletions mobile/src/session/MobileNativeChatOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import {
useMobileNativeChatReplyReveals
} from './mobile-native-chat-reply-reveals'
import { MobileNativeChatRowOverlaysHost } from './MobileNativeChatRowOverlaysHost'
import { nativeChatAskRequiresTerminal } from '../../../src/shared/native-chat-agent-support'

type Props = {
controller: MobileNativeChatController
activeSessionTabId?: string | null
/** Opens a tapped file reference (worktree-relative or absolute, optional
* :line(:col) suffix) through the shared tap-to-open flow. */
onOpenFile: (pathText: string) => void
Expand Down Expand Up @@ -53,6 +55,7 @@ type Props = {
* the chat list below it does not. */
export function MobileNativeChatOverlay({
controller,
activeSessionTabId,
onOpenFile,
images,
structuredComposer = null,
Expand Down Expand Up @@ -146,6 +149,14 @@ export function MobileNativeChatOverlay({
promptKey={controller.nativeChatPromptKey}
onDismissAsk={controller.dismissNativeChatAsk}
onAnswerAsk={controller.handleNativeChatAnswerAsk}
askRequiresTerminal={
!controller.nativeChatStructured && nativeChatAskRequiresTerminal(agent)
}
onAnswerInTerminal={
activeSessionTabId
? () => controller.setTabView(activeSessionTabId, 'terminal')
: undefined
}
onCancelAsk={controller.handleNativeChatCancelAsk}
onCancelPrompt={controller.handleNativeChatCancelPrompt}
onCollapseAsk={controller.collapseNativeChatAsk}
Expand Down
37 changes: 37 additions & 0 deletions mobile/src/session/MobileNativeChatPromptCard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createElement } from 'react'
import { act, create, type ReactTestInstance, type ReactTestRenderer } from 'react-test-renderer'
import { Pressable, TextInput } from 'react-native'
import { describe, expect, it, vi } from 'vitest'
import { MobileNativeChatPromptCard } from './MobileNativeChatPromptCard'

Expand Down Expand Up @@ -27,6 +28,42 @@ const textInput = (root: ReactTestInstance) =>
const ask = { questions: [{ question: 'Pick?', options: [{ label: 'East' }], multiSelect: false }] }

describe('MobileNativeChatPromptCard collapse', () => {
it.each([true, false])(
'shows only terminal navigation when answering requires it (%s)',
async (available) => {
const onAnswerInTerminal = vi.fn()
const onAnswerAsk = vi.fn(async () => true)
const onCancelAsk = vi.fn(async () => true)
const onDismissAsk = vi.fn()
let tree: ReactTestRenderer | null = null
await act(async () => {
tree = create(
createElement(MobileNativeChatPromptCard, {
ask,
requiresTerminal: true,
onAnswerAsk,
onCancelAsk,
onDismissAsk,
onAnswerInTerminal: available ? onAnswerInTerminal : undefined
})
)
})
const buttons = tree!.root.findAllByType(Pressable)
expect(buttons).toHaveLength(1)
expect(buttons[0]!.props.accessibilityLabel).toBe('Answer in terminal')
expect(buttons[0]!.props.disabled).toBe(!available)
expect(tree!.root.findAllByType(TextInput)).toHaveLength(0)
if (available) {
act(() => buttons[0]!.props.onPress())
}
expect(onAnswerInTerminal).toHaveBeenCalledTimes(available ? 1 : 0)
expect(onAnswerAsk).not.toHaveBeenCalled()
expect(onCancelAsk).not.toHaveBeenCalled()
expect(onDismissAsk).not.toHaveBeenCalled()
act(() => tree!.unmount())
Comment on lines +37 to +62

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

Fix the narrowed tree variable. It breaks typecheck:tests and fails the "Mobile verify" job.

Line 37 declares let tree: ReactTestRenderer | null = null. The code assigns tree only inside the act callback. TypeScript narrows tree to null after the callback. At Lines 50 and 54, tree! therefore has type never, and .root is a type error. This matches the pipeline failure "Test file no longer typechecks".

Store the renderer on an object property. Property reads are not narrowed by the initializer. The repo already uses this pattern in agent-history-current-host-capability.test.tsx (rendered.tree = create(...)).

💚 Proposed fix
--- "a/mobile/src/session/MobileNativeChatPromptCard.test.tsx"
+++ "b/mobile/src/session/MobileNativeChatPromptCard.test.tsx"
@@ -34,32 +34,32 @@
       const onAnswerAsk = vi.fn(async () => true)
       const onCancelAsk = vi.fn(async () => true)
       const onDismissAsk = vi.fn()
-      let tree: ReactTestRenderer | null = null
+      const rendered: { tree: ReactTestRenderer | null } = { tree: null }
       await act(async () => {
-        tree = create(
+        rendered.tree = create(
           createElement(MobileNativeChatPromptCard, {
             ask,
             requiresTerminal: true,
             onAnswerAsk,
             onCancelAsk,
             onDismissAsk,
             onAnswerInTerminal: available ? onAnswerInTerminal : undefined
           })
         )
       })
-      const buttons = tree!.root.findAllByType('Pressable')
+      const buttons = rendered.tree!.root.findAllByType('Pressable')
       expect(buttons).toHaveLength(1)
       expect(buttons[0]!.props.accessibilityLabel).toBe('Answer in terminal')
       expect(buttons[0]!.props.disabled).toBe(!available)
-      expect(tree!.root.findAllByType('TextInput')).toHaveLength(0)
+      expect(rendered.tree!.root.findAllByType('TextInput')).toHaveLength(0)
       if (available) {
         act(() => buttons[0]!.props.onPress())
       }
       expect(onAnswerInTerminal).toHaveBeenCalledTimes(available ? 1 : 0)
       expect(onAnswerAsk).not.toHaveBeenCalled()
       expect(onCancelAsk).not.toHaveBeenCalled()
       expect(onDismissAsk).not.toHaveBeenCalled()
-      act(() => tree!.unmount())
+      act(() => rendered.tree!.unmount())
     }
   )
 
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let tree: ReactTestRenderer | null = null
await act(async () => {
tree = create(
createElement(MobileNativeChatPromptCard, {
ask,
requiresTerminal: true,
onAnswerAsk,
onCancelAsk,
onDismissAsk,
onAnswerInTerminal: available ? onAnswerInTerminal : undefined
})
)
})
const buttons = tree!.root.findAllByType('Pressable')
expect(buttons).toHaveLength(1)
expect(buttons[0]!.props.accessibilityLabel).toBe('Answer in terminal')
expect(buttons[0]!.props.disabled).toBe(!available)
expect(tree!.root.findAllByType('TextInput')).toHaveLength(0)
if (available) {
act(() => buttons[0]!.props.onPress())
}
expect(onAnswerInTerminal).toHaveBeenCalledTimes(available ? 1 : 0)
expect(onAnswerAsk).not.toHaveBeenCalled()
expect(onCancelAsk).not.toHaveBeenCalled()
expect(onDismissAsk).not.toHaveBeenCalled()
act(() => tree!.unmount())
const rendered: { tree: ReactTestRenderer | null } = { tree: null }
await act(async () => {
rendered.tree = create(
createElement(MobileNativeChatPromptCard, {
ask,
requiresTerminal: true,
onAnswerAsk,
onCancelAsk,
onDismissAsk,
onAnswerInTerminal: available ? onAnswerInTerminal : undefined
})
)
})
const buttons = rendered.tree!.root.findAllByType('Pressable')
expect(buttons).toHaveLength(1)
expect(buttons[0]!.props.accessibilityLabel).toBe('Answer in terminal')
expect(buttons[0]!.props.disabled).toBe(!available)
expect(rendered.tree!.root.findAllByType('TextInput')).toHaveLength(0)
if (available) {
act(() => buttons[0]!.props.onPress())
}
expect(onAnswerInTerminal).toHaveBeenCalledTimes(available ? 1 : 0)
expect(onAnswerAsk).not.toHaveBeenCalled()
expect(onCancelAsk).not.toHaveBeenCalled()
expect(onDismissAsk).not.toHaveBeenCalled()
act(() => rendered.tree!.unmount())

Source: Pipeline failures

}
)

it('keeps a partly answered Ask mounted but hidden while collapsed', async () => {
let tree: ReactTestRenderer | null = null
const render = (collapsed: boolean) =>
Expand Down
121 changes: 82 additions & 39 deletions mobile/src/session/MobileNativeChatPromptCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StyleSheet, View } from 'react-native'
import { Pressable, StyleSheet, Text, View } from 'react-native'
import { colors, radii, spacing, typography } from '../theme/mobile-theme'
import type { AskAnswerSelection, AskPrompt } from '../../../src/shared/native-chat-ask'
import { MobileNativeChatAsk } from './MobileNativeChatAsk'
import { MobileNativeChatPermission } from './MobileNativeChatPermission'
Expand All @@ -16,6 +17,8 @@ export function MobileNativeChatPromptCard({
askKey,
onDismissAsk,
onAnswerAsk,
requiresTerminal = false,
onAnswerInTerminal,
onCancelAsk,
onCancelPrompt,
onCollapseAsk,
Expand All @@ -30,6 +33,8 @@ export function MobileNativeChatPromptCard({
askKey?: string | null
onDismissAsk?: () => void
onAnswerAsk?: (prompt: AskPrompt, selections: AskAnswerSelection[]) => Promise<boolean>
requiresTerminal?: boolean
onAnswerInTerminal?: () => void
onCancelAsk?: () => Promise<boolean>
onCancelPrompt?: (prompt?: NonNullable<MobileChatPermission['prompt']>) => Promise<boolean>
onCollapseAsk?: () => void
Expand All @@ -42,43 +47,58 @@ export function MobileNativeChatPromptCard({
question?: MobileChatQuestion | null
onAnswerQuestion?: (text: string) => Promise<boolean>
}): React.JSX.Element | null {
const card = ask ? (
<MobileNativeChatAsk
key={askKey ?? 'ask'}
prompt={ask}
onAnswer={async (selections) => {
const accepted = (await onAnswerAsk?.(ask, selections)) ?? false
if (accepted) {
onDismissAsk?.()
}
return accepted
}}
onCancel={async () => {
const accepted = (await onCancelAsk?.()) ?? false
if (accepted) {
onDismissAsk?.()
}
return accepted
}}
onCollapse={onCollapseAsk}
/>
) : permission ? (
<MobileNativeChatPermission
key={JSON.stringify(permission)}
permission={permission}
onRespond={async (send) => (await onRespondPermission?.(send)) ?? false}
onCancel={onCancelPrompt}
onCollapse={onCollapsePrompt}
/>
) : question ? (
<MobileNativeChatQuestion
key={mobileChatQuestionKey(question)}
question={question}
onAnswer={async (text) => (await onAnswerQuestion?.(text)) ?? false}
onCancel={onCancelPrompt}
onCollapse={onCollapsePrompt}
/>
) : null
const card =
ask && requiresTerminal ? (
<View style={styles.terminalCard}>
<Text style={styles.explanation}>Answer this OMP question in the terminal.</Text>
<Pressable
accessibilityRole="button"
accessibilityLabel="Answer in terminal"
accessibilityState={{ disabled: !onAnswerInTerminal }}
disabled={!onAnswerInTerminal}
onPress={onAnswerInTerminal}
style={styles.terminalButton}
>
<Text style={styles.terminalButtonText}>Answer in terminal</Text>
</Pressable>
</View>
) : ask ? (
<MobileNativeChatAsk
key={askKey ?? 'ask'}
prompt={ask}
onAnswer={async (selections) => {
const accepted = (await onAnswerAsk?.(ask, selections)) ?? false
if (accepted) {
onDismissAsk?.()
}
return accepted
}}
onCancel={async () => {
const accepted = (await onCancelAsk?.()) ?? false
if (accepted) {
onDismissAsk?.()
}
return accepted
}}
onCollapse={onCollapseAsk}
/>
) : permission ? (
<MobileNativeChatPermission
key={JSON.stringify(permission)}
permission={permission}
onRespond={async (send) => (await onRespondPermission?.(send)) ?? false}
onCancel={onCancelPrompt}
onCollapse={onCollapsePrompt}
/>
) : question ? (
<MobileNativeChatQuestion
key={mobileChatQuestionKey(question)}
question={question}
onAnswer={async (text) => (await onAnswerQuestion?.(text)) ?? false}
onCancel={onCancelPrompt}
onCollapse={onCollapsePrompt}
/>
) : null
// Why one stable wrapper: collapsing hides the card without remounting it, so a partly answered
// card keeps its selections.
return (
Expand All @@ -94,4 +114,27 @@ export function MobileNativeChatPromptCard({
)
}

const styles = StyleSheet.create({ collapsed: { display: 'none' } })
const styles = StyleSheet.create({
collapsed: { display: 'none' },
terminalCard: {
backgroundColor: colors.bgPanel,
borderTopWidth: StyleSheet.hairlineWidth,
borderTopColor: colors.borderSubtle,
padding: spacing.md,
gap: spacing.sm
},
explanation: { color: colors.textPrimary, fontSize: typography.bodySize },
terminalButton: {
alignSelf: 'flex-start',
minHeight: 44,
justifyContent: 'center',
backgroundColor: colors.surfaceBright,
borderRadius: radii.button,
paddingHorizontal: spacing.md
},
terminalButtonText: {
color: colors.bgBase,
fontSize: typography.bodySize,
fontWeight: '600'
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { Pressable } from 'react-native'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { MobileNativeChatView } from './MobileNativeChatView'
import { MobileNativeChatAsk } from './MobileNativeChatAsk'
import { useMobileNativeChatPromptPresentation } from './use-mobile-native-chat-prompt-presentation'
import type { MobileChatPermission } from './mobile-native-chat-permission'
vi.mock('react-native', async () => {
Expand Down Expand Up @@ -134,6 +135,25 @@ afterEach(() => {
vi.unstubAllGlobals()
})
describe('terminal prompt presentation with the production view, card and composer', () => {
it('forwards terminal navigation without answering or cancelling the question', async () => {
const onAnswerInTerminal = vi.fn()
const onAnswerAsk = vi.fn(async () => true)
const onCancelAsk = vi.fn(async () => true)
await render({
ask: { questions: [{ question: 'Pick', options: [{ label: 'A' }], multiSelect: false }] },
askRequiresTerminal: true,
onAnswerInTerminal,
onAnswerAsk,
onCancelAsk
})
const button = tree!.root.findByProps({ accessibilityLabel: 'Answer in terminal' })
act(() => button.props.onPress())
expect(onAnswerInTerminal).toHaveBeenCalledOnce()
expect(onAnswerAsk).not.toHaveBeenCalled()
expect(onCancelAsk).not.toHaveBeenCalled()
expect(tree!.root.findAllByType(MobileNativeChatAsk)).toHaveLength(0)
})

it('accepted Deny hides exactly that occurrence and restores Send across reconnect', async () => {
vi.stubGlobal('requestAnimationFrame', (callback: () => void) => setTimeout(callback, 0))
const onRespondPermission = vi.fn(async () => true)
Expand Down
6 changes: 6 additions & 0 deletions mobile/src/session/MobileNativeChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ type Props = ComposerTrayProps & {
/** Deliver the ask answer as per-question selections; the send hook turns them
* into selector keystrokes (Claude) or pasted label text (other agents). */
onAnswerAsk?: (prompt: AskPrompt, selections: AskAnswerSelection[]) => Promise<boolean>
askRequiresTerminal?: boolean
onAnswerInTerminal?: () => void
onCancelAsk?: () => Promise<boolean>
/** Cancel a structured approval/question with exact item identity when supported. */
onCancelPrompt?: (prompt?: { itemId: string; expectedRevision: number }) => Promise<boolean>
Expand Down Expand Up @@ -213,6 +215,8 @@ export function MobileNativeChatView({
askKey,
onDismissAsk,
onAnswerAsk,
askRequiresTerminal = false,
onAnswerInTerminal,
onCancelAsk,
onCancelPrompt,
onCollapseAsk,
Expand Down Expand Up @@ -389,6 +393,8 @@ export function MobileNativeChatView({
<MobileNativeChatPromptCard
key={promptKey ?? undefined}
{...{ ask, askKey, onDismissAsk, onAnswerAsk, onCancelAsk, onCancelPrompt, onCollapseAsk }}
requiresTerminal={askRequiresTerminal}
onAnswerInTerminal={onAnswerInTerminal}
{...{ permission, onRespondPermission, question, onAnswerQuestion, onCollapsePrompt }}
collapsedPrompt={collapsedPrompt}
/>
Expand Down
1 change: 1 addition & 0 deletions mobile/src/session/MobileSessionActiveContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ export function MobileSessionActiveContent({
))}
<MobileNativeChatOverlay
controller={nativeChatController}
activeSessionTabId={controller.activeSessionTab?.id}
onOpenFile={handleNativeChatFileTap}
images={nativeChatImages}
structuredComposer={nativeChatStructuredComposer}
Expand Down
1 change: 1 addition & 0 deletions mobile/src/session/use-mobile-bridge-chat-prompt-writes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function useMobileBridgeChatPromptWrites(args: {
enabled,
handleRef,
deviceTokenRef,
agentRef: args.agentRef,
cancelPending,
onSendError
})
Expand Down
Loading
Loading