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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const METHODS = [
}
] as const

const DESTRUCTIVE_METHOD_NAMES = new Set(['session.tabs.close', 'session.tabs.closeLifecycle'])

describe('session tab structured capability mutations', () => {
for (const method of METHODS) {
it(`rejects ${method.name} when the structured row is hidden`, async () => {
Expand Down Expand Up @@ -88,6 +90,38 @@ describe('session tab structured capability mutations', () => {
})
}

for (const method of METHODS) {
const expectedToAllowPromptedRow = !DESTRUCTIVE_METHOD_NAMES.has(method.name)

it(`${expectedToAllowPromptedRow ? 'allows' : 'rejects'} ${method.name} on a row an old mobile client was prompted to update`, async () => {
const { calls, dispatch } = createFixture([], {
clientKind: 'mobile',
structuredNativeChatEnabled: true
})

const response = await dispatch(method.name, method.params('codex-session'))

expect(response.ok).toBe(expectedToAllowPromptedRow)
expect(calls[method.runtimeMethod as keyof typeof calls]).toHaveBeenCalledTimes(
expectedToAllowPromptedRow ? 1 : 0
)
})

it(`${expectedToAllowPromptedRow ? 'allows' : 'rejects'} ${method.name} on a prompted Claude row for a mobile client without the Claude capability`, async () => {
const { calls, dispatch } = createFixture([STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY], {
clientKind: 'mobile',
structuredNativeChatEnabled: true
})

const response = await dispatch(method.name, method.params('claude-session'))

expect(response.ok).toBe(expectedToAllowPromptedRow)
expect(calls[method.runtimeMethod as keyof typeof calls]).toHaveBeenCalledTimes(
expectedToAllowPromptedRow ? 1 : 0
)
})
}

it.each(['session.tabs.close', 'session.tabs.closeLifecycle'] as const)(
'allows capable mobile clients to close structured tabs when the experiment is enabled (%s)',
async (method) => {
Expand Down Expand Up @@ -131,7 +165,10 @@ describe('session tab structured capability mutations', () => {
)
})

function createFixture(capabilities: RuntimeCapability[]) {
function createFixture(
capabilities: RuntimeCapability[],
options: { clientKind?: 'mobile' | 'runtime'; structuredNativeChatEnabled?: boolean } = {}
) {
const snapshot = agentSnapshot()
const calls = {
closeMobileSessionTab: vi.fn().mockResolvedValue({ closed: true }),
Expand All @@ -142,11 +179,14 @@ function createFixture(capabilities: RuntimeCapability[]) {
const runtime = {
getRuntimeId: () => 'test-runtime',
listMobileSessionTabs: vi.fn().mockResolvedValue(snapshot),
getClientSettings: () => ({
experimentalStructuredNativeChat: options.structuredNativeChatEnabled === true
}),
...calls
} as unknown as OrcaRuntimeService
const dispatcher = new RpcDispatcher({ runtime, methods: SESSION_TAB_METHODS })
const context: RpcDispatchStreamingOptions = {
clientKind: 'runtime',
clientKind: options.clientKind ?? 'runtime',
pairedDeviceId: 'paired-client',
clientCapabilities: capabilities
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY
} from '../../../../shared/protocol-version'
import type { RuntimeMobileSessionTabsSnapshot } from '../../../../shared/runtime-types'
import { projectSessionTabAgentStatus } from './session-tab-agent-status-projection'
import {
CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE,
STRUCTURED_CHAT_UPDATE_REQUIRED_TAB_TITLE,
projectSessionTabAgentStatus
} from './session-tab-agent-status-projection'

function makeSnapshot(sessionBoundary: boolean): RuntimeMobileSessionTabsSnapshot {
return {
Expand Down Expand Up @@ -160,23 +164,96 @@ describe('projectSessionTabAgentStatus', () => {
CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY
]

it.each([
['mobile', 'mobile' as const, [STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY]],
['runtime', 'runtime' as const, [STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY]]
])(
'withholds Claude rows from a paired %s client that never negotiated them',
(_name, clientKind, capabilities) => {
const projected = projectSessionTabAgentStatus(claudeSnapshot, clientKind, capabilities, true)
it('withholds Claude rows from a paired runtime client that never negotiated them', () => {
const projected = projectSessionTabAgentStatus(
claudeSnapshot,
'runtime',
[STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY],
true
)

expect(projected.tabs.map((tab) => tab.id)).toEqual(['agent-session:codex'])
// A row pruned from `tabs` but left in the layout is its own dead tab.
expect(projected.tabGroups?.map((group) => group.id)).toEqual(['group-a'])
expect(projected.tabGroupLayout).toEqual({ type: 'leaf', groupId: 'group-a' })
expect(projected.activeGroupId).toBe('group-a')
expect(projected.activeTabId).toBe('agent-session:codex')
expect(projected.activeTabType).toBe('agent-session')
})

it('uses a desktop fallback for an unsupported Claude row instead of withholding it', () => {
const projected = projectSessionTabAgentStatus(
claudeSnapshot,
'mobile',
[STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY],
true
)

// The row survives so the chat the desktop shows is not simply absent on the phone.
expect(projected.tabs.map((tab) => tab.id)).toEqual([
'agent-session:codex',
'agent-session:claude'
])
expect(projected.tabs.map((tab) => tab.title)).toEqual([
'Codex Chat',
CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE
])
// Nothing is removed, so the layout it belonged to is untouched.
expect(projected.tabGroups?.map((group) => group.id)).toEqual(['group-a', 'group-b'])
expect(projected.tabGroupLayout).toEqual(claudeSnapshot.tabGroupLayout)
expect(projected.activeTabId).toBe('agent-session:codex')
})

it('projects agent-specific fallback titles for a mobile client with no capabilities', () => {
const projected = projectSessionTabAgentStatus(claudeSnapshot, 'mobile', [], true)

expect(projected.tabs.map((tab) => tab.id)).toEqual(['agent-session:codex'])
// A row pruned from `tabs` but left in the layout is its own dead tab.
expect(projected.tabGroups?.map((group) => group.id)).toEqual(['group-a'])
expect(projected.tabGroupLayout).toEqual({ type: 'leaf', groupId: 'group-a' })
expect(projected.activeGroupId).toBe('group-a')
expect(projected.activeTabId).toBe('agent-session:codex')
expect(projected.activeTabType).toBe('agent-session')
expect(projected.tabs.map((tab) => tab.title)).toEqual([
STRUCTURED_CHAT_UPDATE_REQUIRED_TAB_TITLE,
CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE
])
expect(projected.tabGroupLayout).toEqual(claudeSnapshot.tabGroupLayout)
})

it('does not treat the Claude capability as a substitute for the base structured capability', () => {
const projected = projectSessionTabAgentStatus(
claudeSnapshot,
'mobile',
[CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY],
true
)

expect(projected.tabs.map((tab) => tab.title)).toEqual([
STRUCTURED_CHAT_UPDATE_REQUIRED_TAB_TITLE,
CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE
])
})

it('shows both real titles once mobile negotiates Claude', () => {
expect(projectSessionTabAgentStatus(claudeSnapshot, 'mobile', structuredMobile, true)).toBe(
claudeSnapshot
)
})

// Why: updating cannot reveal a chat the desktop is not serving, so the prompt would lie.
it('withholds rather than prompts when the desktop experiment is off', () => {
for (const capabilities of [
[],
[STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY],
structuredMobile
]) {
const projected = projectSessionTabAgentStatus(claudeSnapshot, 'mobile', capabilities, false)
expect(projected.tabs).toEqual([])
}
)
})

it('never emits an empty structured tab title', () => {
for (const capabilities of [[], [STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY]]) {
const projected = projectSessionTabAgentStatus(claudeSnapshot, 'mobile', capabilities, true)
for (const tab of projected.tabs) {
expect(tab.title.length).toBeGreaterThan(0)
}
}
})

it.each([
['mobile', 'mobile' as const, structuredMobile],
Expand Down
110 changes: 100 additions & 10 deletions src/main/runtime/rpc/methods/session-tab-agent-status-projection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AGENT_SESSION_BOUNDARY_RUNTIME_CAPABILITY,
CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY,
STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY,
type RuntimeCapability
} from '../../../../shared/protocol-version'
import type {
Expand All @@ -13,6 +14,43 @@ import { structuredNativeChatProjectionEnabled } from './structured-agent-sessio

type SessionTabsPayload = RuntimeMobileSessionTabsResult | RuntimeMobileSessionTabsSnapshot

/** Capped at 128px / one line in every shipped mobile build, so ~15-18 characters render. */
export const STRUCTURED_CHAT_UPDATE_REQUIRED_TAB_TITLE = 'Update to view'
export const CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE = 'Open on desktop'

function clientCanRenderStructuredAgentSessionTab(
tab: RuntimeMobileSessionAgentTab,
clientCapabilities: readonly RuntimeCapability[] | undefined
): boolean {
if (!clientCapabilities?.includes(STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY)) {
return false
}
return (
tab.agent === 'codex' ||
clientCapabilities.includes(CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY)
)
}

function resolveMobileStructuredChatFallbackTitle(
tab: RuntimeMobileSessionAgentTab,
args: {
clientKind: 'mobile' | 'runtime' | undefined
clientCapabilities: readonly RuntimeCapability[] | undefined
structuredNativeChatEnabled?: boolean
}
): string | null {
if (
args.clientKind !== 'mobile' ||
args.structuredNativeChatEnabled !== true ||
clientCanRenderStructuredAgentSessionTab(tab, args.clientCapabilities)
) {
return null
}
return tab.agent === 'claude'
? CLAUDE_STRUCTURED_CHAT_DESKTOP_ONLY_TAB_TITLE
: STRUCTURED_CHAT_UPDATE_REQUIRED_TAB_TITLE
}

export function projectSessionTabAgentStatus<TPayload extends SessionTabsPayload>(
payload: TPayload,
clientKind: 'mobile' | 'runtime' | undefined,
Expand All @@ -24,16 +62,27 @@ export function projectSessionTabAgentStatus<TPayload extends SessionTabsPayload
clientCapabilities,
structuredNativeChatEnabled
})
let projected = structuredVisible ? payload : projectAgentSessionTabsOut(payload, () => true)
// Why: a paired client renders only codex structured tabs unless it says otherwise
// (mobile's resolveMobileNativeChat returns null for every other agent), so an
// ungated row would list and select into a pane that shows neither chat nor terminal.
if (
structuredVisible &&
clientKind !== undefined &&
!clientCapabilities?.includes(CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY)
) {
projected = projectAgentSessionTabsOut(projected, (tab) => tab.agent !== 'codex')
let projected: TPayload
if (clientKind === 'mobile' && structuredNativeChatEnabled === true) {
// Why: deleting the row left the user hunting for a chat the desktop says exists; the row
// survives with a title naming the fix. Nothing is removed, so no group/layout repair applies.
projected = projectUnsupportedAgentSessionTabTitles(payload, {
clientKind,
clientCapabilities,
structuredNativeChatEnabled
})
} else {
projected = structuredVisible ? payload : projectAgentSessionTabsOut(payload, () => true)
// Why: a paired client renders only codex structured tabs unless it says otherwise
// (mobile's resolveMobileNativeChat returns null for every other agent), so an
// ungated row would list and select into a pane that shows neither chat nor terminal.
if (
structuredVisible &&
clientKind !== undefined &&
!clientCapabilities?.includes(CLAUDE_STRUCTURED_AGENT_SESSION_RUNTIME_CAPABILITY)
) {
projected = projectAgentSessionTabsOut(projected, (tab) => tab.agent !== 'codex')
}
}
// Why: only paired runtimes have legacy `done` completion side effects; mobile must keep its row without changing the exact v2 auth shape.
if (
Expand All @@ -55,6 +104,47 @@ export function projectSessionTabAgentStatus<TPayload extends SessionTabsPayload
return changed ? ({ ...projected, tabs } as TPayload) : projected
}

function projectUnsupportedAgentSessionTabTitles<TPayload extends SessionTabsPayload>(
payload: TPayload,
args: {
clientKind: 'mobile'
clientCapabilities: readonly RuntimeCapability[] | undefined
structuredNativeChatEnabled: true
}
): TPayload {
let changed = false
const tabs = payload.tabs.map((tab) => {
if (tab.type !== 'agent-session') {
return tab
}
const title = resolveMobileStructuredChatFallbackTitle(tab, args)
if (title === null) {
return tab
}
changed = true
return { ...tab, title }
})
return changed ? ({ ...payload, tabs } as TPayload) : payload
}

export function assertAgentSessionTabDestructiveMutationSupported(
payload: SessionTabsPayload,
tabId: string,
clientKind: 'mobile' | 'runtime' | undefined,
clientCapabilities: readonly RuntimeCapability[] | undefined
): void {
if (clientKind === undefined) {
return
}
const tab = payload.tabs.find((candidate) => candidate.id === tabId)
if (
tab?.type === 'agent-session' &&
!clientCanRenderStructuredAgentSessionTab(tab, clientCapabilities)
) {
throw new Error('structured_agent_session_unsupported')
}
}

function projectAgentSessionTabsOut<TPayload extends SessionTabsPayload>(
payload: TPayload,
shouldHide: (tab: RuntimeMobileSessionAgentTab) => boolean
Expand Down
Loading