From 473585c4bb2fbc37a67b34f9519eadbc2b165556 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Sun, 8 Nov 2026 04:44:34 -0800 Subject: [PATCH] Require current host proof for inherited fork offers --- .../session/MobileNativeChatForkProvider.tsx | 3 + .../use-mobile-native-chat-fork.test.tsx | 67 ++++++++++++++++-- .../use-mobile-native-chat-fork.ts | 12 ++-- .../native-chat-side-chat-fork-point.test.ts | 69 ++++++++++++++++--- .../messages/actions/use-native-chat-fork.ts | 3 +- ...tructured-agent-session-host-capability.ts | 19 +++-- 6 files changed, 148 insertions(+), 25 deletions(-) diff --git a/mobile/src/session/MobileNativeChatForkProvider.tsx b/mobile/src/session/MobileNativeChatForkProvider.tsx index cd7c9b4f8e5..44f80dff88a 100644 --- a/mobile/src/session/MobileNativeChatForkProvider.tsx +++ b/mobile/src/session/MobileNativeChatForkProvider.tsx @@ -1,4 +1,5 @@ import { useCallback, useLayoutEffect, useMemo, useRef } from 'react' +import { useHostProtocolGates } from '../components/host-protocol-gates-context' import type { TuiAgent } from '../../../src/shared/agent-launch/catalog/tui-agent' import type { StructuredAgentSessionForkSource } from '../../../src/shared/agent-session/structured-agent-session-create' import { MobileNativeChatForkedFromContext } from './mobile-native-chat-notice-context' @@ -19,6 +20,7 @@ export function MobileNativeChatForkProvider({ children: React.ReactNode }): React.JSX.Element { const { client, nativeChatController: chat, openNativeSession, worktreeId } = controller + const { statusPending } = useHostProtocolGates() // The controller is rebuilt each render; a fork reads its session state at the press. const latestController = useRef(controller) useLayoutEffect(() => { @@ -32,6 +34,7 @@ export function MobileNativeChatForkProvider({ const fork = useMobileNativeChatFork({ client, hostCapabilities: controller.hostCapabilities, + hostStatusPending: statusPending || controller.connState !== 'connected', agent: chat.nativeChatAgent, sessionId: resolveMobileNativeChatSessionId(controller.activeSessionTab), messages: chat.nativeChatSession.messages, diff --git a/mobile/src/session/native-chat/use-mobile-native-chat-fork.test.tsx b/mobile/src/session/native-chat/use-mobile-native-chat-fork.test.tsx index cf829068007..7f1ce063132 100644 --- a/mobile/src/session/native-chat/use-mobile-native-chat-fork.test.tsx +++ b/mobile/src/session/native-chat/use-mobile-native-chat-fork.test.tsx @@ -78,6 +78,7 @@ function Probe({ latest = useMobileNativeChatFork({ client, hostCapabilities, + hostStatusPending: false, agent: 'claude', sessionId: 'session-1', messages: [message], @@ -204,28 +205,63 @@ it('offers an inherited OpenCode tip after the child advances and submits its di result: { agents: [{ agent: 'opencode', capabilities: { fork: 'tip' } }] } }) const createFork = vi.fn(async () => {}) - const copied = { ...message, id: 'legacy:opencode:source:reply-1' } + const copied = { + ...message, + id: 'legacy:opencode:source:reply-1', + journalPosition: { sequence: 2, index: 0 } + } + const copiedPrompt: NativeChatMessage = { + ...message, + id: 'legacy:opencode:source:user-1', + role: 'user', + journalPosition: { sequence: 1, index: 0 } + } + const ownPrompt: NativeChatMessage = { + ...message, + id: 'legacy:opencode:child:user-2', + role: 'user', + journalPosition: { sequence: 4, index: 0 } + } const own = { ...message, id: 'legacy:opencode:child:reply-2', - journalPosition: { sequence: 4, index: 0 } + journalPosition: { sequence: 5, index: 0 } } - function ForkProbe({ hostCapabilities }: { hostCapabilities: readonly string[] }) { + function ForkProbe({ + hostCapabilities, + hostStatusPending = false + }: { + hostCapabilities: readonly string[] + hostStatusPending?: boolean + }) { latest = useMobileNativeChatFork({ client, hostCapabilities, + hostStatusPending, agent: 'opencode', sessionId: 'child', - messages: [copied, own], + messages: [copiedPrompt, copied, ownPrompt, own], turnJournal: { items: [ - { ...reply, itemId: copied.id }, + { + ...reply, + itemId: copiedPrompt.id, + body: { kind: 'message', role: 'user', blocks: copiedPrompt.blocks } + }, + { ...reply, itemId: copied.id, sequence: 2 }, { ...reply, itemId: 'orca:forked-from', + sequence: 2, body: { kind: 'status', presentation: 'forked-from', text: 'Forked from source' } }, - { ...reply, itemId: own.id, sequence: 4 } + { + ...reply, + itemId: ownPrompt.id, + sequence: 4, + body: { kind: 'message', role: 'user', blocks: ownPrompt.blocks } + }, + { ...reply, itemId: own.id, sequence: 5 } ], submissions: [] }, @@ -249,6 +285,25 @@ it('offers an inherited OpenCode tip after the child advances and submits its di ) }) expect(latest?.offerFor(copied)).toBe('available') + await act(async () => { + renderer!.update( + createElement(ForkProbe, { + hostCapabilities: [...FORK_HOST, AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY], + hostStatusPending: true + }) + ) + }) + expect(latest?.offerFor(copied)).toBe('reserved') + expect(latest?.offerFor(own)).toBe('available') + await act(async () => { + renderer!.update( + createElement(ForkProbe, { + hostCapabilities: [...FORK_HOST, AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY], + hostStatusPending: false + }) + ) + }) + expect(latest?.offerFor(copied)).toBe('available') await act(async () => { latest?.fork(copied) }) diff --git a/mobile/src/session/native-chat/use-mobile-native-chat-fork.ts b/mobile/src/session/native-chat/use-mobile-native-chat-fork.ts index 72b807d6fc6..587620671e6 100644 --- a/mobile/src/session/native-chat/use-mobile-native-chat-fork.ts +++ b/mobile/src/session/native-chat/use-mobile-native-chat-fork.ts @@ -156,6 +156,7 @@ function useRegisteredAgent( export function useMobileNativeChatFork(input: { client: RpcClient | null hostCapabilities: readonly string[] | undefined + hostStatusPending: boolean agent: string | null sessionId: string | null messages: readonly NativeChatMessage[] @@ -170,11 +171,12 @@ export function useMobileNativeChatFork(input: { : input.hostCapabilities.includes(AGENT_SESSION_CREATE_FORK_RUNTIME_CAPABILITY) ? 'supported' : 'unsupported' - const inheritedOriginal: StructuredAgentSessionHostCapabilityState = !input.hostCapabilities - ? 'unknown' - : input.hostCapabilities.includes(AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY) - ? 'supported' - : 'unsupported' + const inheritedOriginal: StructuredAgentSessionHostCapabilityState = + !input.hostCapabilities || input.hostStatusPending + ? 'unknown' + : input.hostCapabilities.includes(AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY) + ? 'supported' + : 'unsupported' const registered = useRegisteredAgent(input.client, host, input.agent) const support = useMemo( () => nativeChatForkSupport(host, registered, input.sessionForkSupport, inheritedOriginal), diff --git a/src/renderer/src/components/native-chat/messages/actions/native-chat-side-chat-fork-point.test.ts b/src/renderer/src/components/native-chat/messages/actions/native-chat-side-chat-fork-point.test.ts index 2553be39edb..df5161f01c4 100644 --- a/src/renderer/src/components/native-chat/messages/actions/native-chat-side-chat-fork-point.test.ts +++ b/src/renderer/src/components/native-chat/messages/actions/native-chat-side-chat-fork-point.test.ts @@ -1,5 +1,5 @@ // @vitest-environment happy-dom -import { createElement } from 'react' +import { act, createElement } from 'react' import { renderHook } from '@testing-library/react' import { AGENT_SESSION_CREATE_FORK_RUNTIME_CAPABILITY, @@ -22,10 +22,28 @@ import type { NativeChatMessage } from '../../../../../../shared/native-chat/nat import type { NativeChatTranscriptSlotsInput } from '../../conversation/slots/native-chat-transcript-input' import { buildNativeChatTranscript } from '../../conversation/slots/native-chat-transcript-slots' -const host = vi.hoisted(() => ({ capabilities: new Set() })) -vi.mock('@/runtime/native-chat/host-capabilities/structured-agent-session-host-capability', () => ({ - useStructuredAgentSessionHostCapabilityState: (_target: unknown, capability: string) => - host.capabilities.has(capability) ? 'supported' : 'unsupported' +const host = vi.hoisted( + (): { + capabilities: Set + pendingOriginal: Promise | null + contacts: Set<() => void> + } => ({ + capabilities: new Set(), + pendingOriginal: null, + contacts: new Set<() => void>() + }) +) +vi.mock('@/runtime/rpc/compatibility/runtime-capability-proof', () => ({ + runtimeEnvironmentSupportsCapability: (_environmentId: string, capability: string) => + capability === AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY && host.pendingOriginal + ? host.pendingOriginal + : Promise.resolve(host.capabilities.has(capability)) +})) +vi.mock('@/runtime/hosts/connections/runtime-host-contact-regained', () => ({ + subscribeRuntimeHostContactRegained: (_environmentId: string, listener: () => void) => { + host.contacts.add(listener) + return () => host.contacts.delete(listener) + } })) vi.mock('@/runtime/native-chat/agent-catalogs/use-host-structured-agent', () => ({ useHostStructuredAgent: () => ({ capabilities: { fork: 'tip' } }) @@ -177,7 +195,7 @@ it('offers the inherited source tip while a later child response is running', () ).toEqual({ kind: 'reply-running' }) }) -it('carries target-host original-fork support into desktop response and side-chat offers', () => { +it('carries current target-host proof into desktop response and side-chat offers', async () => { const copied = row('legacy:opencode:source:reply-1', 'assistant', 1) const own = row('legacy:opencode:child:reply-2', 'assistant', 4) const { input, latestTurnKey } = conversation( @@ -224,6 +242,7 @@ it('carries target-host original-fork support into desktop response and side-cha sessionForkSupport: 'supported' }) ) + await act(async () => {}) const responseOffered = () => nativeChatRowOffersFork( { @@ -251,13 +270,47 @@ it('carries target-host original-fork support into desktop response and side-cha expect(sidePoint()).toEqual({ kind: 'reply-running' }) host.capabilities.add(AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY) - pane.rerender() + await act(async () => { + host.contacts.forEach((listener) => listener()) + }) + side.rerender() + expect(responseOffered()).toBe(true) + expect(sidePoint()).toEqual({ kind: 'ready', throughItemId: 'legacy:opencode:source:reply-1' }) + + let settleOriginal = (_supported: boolean) => {} + host.pendingOriginal = new Promise((resolve) => { + settleOriginal = resolve + }) + await act(async () => { + host.contacts.forEach((listener) => listener()) + }) + side.rerender() + expect(responseOffered()).toBe(false) + expect(sidePoint()).toEqual({ kind: 'reply-running' }) + expect( + nativeChatRowOffersFork( + { + message: own, + inSubagentSection: false, + turnWorking: false, + inLatestTurn: true + }, + pane.result.current! + ) + ).toBe(true) + + await act(async () => { + settleOriginal(true) + }) + host.pendingOriginal = null side.rerender() expect(responseOffered()).toBe(true) expect(sidePoint()).toEqual({ kind: 'ready', throughItemId: 'legacy:opencode:source:reply-1' }) host.capabilities.delete(AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY) - pane.rerender() + await act(async () => { + host.contacts.forEach((listener) => listener()) + }) side.rerender() expect(responseOffered()).toBe(false) expect(sidePoint()).toEqual({ kind: 'reply-running' }) diff --git a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-fork.ts b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-fork.ts index f86c04c1349..2fb4b7941da 100644 --- a/src/renderer/src/components/native-chat/messages/actions/use-native-chat-fork.ts +++ b/src/renderer/src/components/native-chat/messages/actions/use-native-chat-fork.ts @@ -81,7 +81,8 @@ export function useNativeChatFork(input: { ) const inheritedOriginalHost = useStructuredAgentSessionHostCapabilityState( input.target, - AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY + AGENT_SESSION_CREATE_FORK_ORIGINAL_RUNTIME_CAPABILITY, + true ) const registered = useHostStructuredAgent(input.target, input.agent) const support = nativeChatForkSupport( diff --git a/src/renderer/src/runtime/native-chat/host-capabilities/structured-agent-session-host-capability.ts b/src/renderer/src/runtime/native-chat/host-capabilities/structured-agent-session-host-capability.ts index 1d7ac7e3be7..6cdf78abe94 100644 --- a/src/renderer/src/runtime/native-chat/host-capabilities/structured-agent-session-host-capability.ts +++ b/src/renderer/src/runtime/native-chat/host-capabilities/structured-agent-session-host-capability.ts @@ -71,18 +71,21 @@ export function useStructuredAgentSessionOwnPermissionState( * host is asked again each time this client regains contact with it. */ export function useStructuredAgentSessionHostCapabilityState( target: RuntimeClientTarget | null, - capability: RuntimeCapability + capability: RuntimeCapability, + requireCurrentContact = false ): StructuredAgentSessionHostCapabilityState { const key = target ? structuredAgentSessionHostKey(target) : null + const [contact, setContact] = useState(0) const [answer, setAnswer] = useState<{ key: string | null + contact: number state: StructuredAgentSessionHostCapabilityState }>(() => ({ key, + contact, state: target?.kind === 'local' ? localCapabilityState(capability) : 'unknown' })) const environmentId = target?.kind === 'environment' ? target.environmentId : null - const [contact, setContact] = useState(0) useEffect(() => { if (environmentId === null) { return @@ -105,15 +108,21 @@ export function useStructuredAgentSessionHostCapabilityState( .then((state) => { if (!cancelled) { setAnswer((current) => - current.key === key && current.state === state ? current : { key, state } + current.key === key && + current.state === state && + (!requireCurrentContact || current.contact === contact) + ? current + : { key, contact, state } ) } }) return () => { cancelled = true } - }, [capability, contact, environmentId, key]) - return answer.key === key ? answer.state : 'unknown' + }, [capability, contact, environmentId, key, requireCurrentContact]) + return answer.key === key && (!requireCurrentContact || answer.contact === contact) + ? answer.state + : 'unknown' } function localCapabilityState(