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
2 changes: 1 addition & 1 deletion mobile/src/components/NewWorktreeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function NewWorktreeModalContent(props: NewWorktreeModalProps) {
const navigation = useNewWorktreeDrawerNavigation(visible)
const [note, setNote] = useState('')
const [error, setError] = useState('')
const runtime = useNewWorkspaceRuntimeContext(client, visible, hostId)
const runtime = useNewWorkspaceRuntimeContext(client, visible, hostId, repoServer)
const repoRuntime = useNewWorktreeRuntimeCapabilities(client, visible)
const { tasksSupported, getWorktreeCreateCutoverSupport, getAgentLaunchSupport } = repoRuntime
// The desktop's own run target is named for the desktop, whichever host the picked repo is on.
Expand Down
69 changes: 64 additions & 5 deletions mobile/src/components/use-new-workspace-runtime-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type PublishedState = Pick<
// Use the persisted per-repo, per-hook approval shape; a content hash directly under the repo key is unreadable.
const TRUSTED_HOOKS = { 'repo-1': { setup: { contentHash: 'sha-1', approvedAt: 1700000000000 } } }
const UI_WITH_TRUST = { ui: { trustedOrcaHooks: TRUSTED_HOOKS } }
const SETTINGS = { defaultTuiAgent: 'codex', visibleTaskProviders: ['github', 'linear'] }
const SETTINGS = { defaultTuiAgent: 'codex' as const, visibleTaskProviders: ['github', 'linear'] }

function reply(result: unknown): RpcResponse {
return { id: 'r', ok: true, result, _meta: { runtimeId: 'runtime-1' } }
Expand All @@ -41,6 +41,7 @@ function clientAnswering(settingsResult: unknown, uiResult: unknown): RpcClient
describe('useNewWorkspaceRuntimeContext', () => {
let renderer: ReactTestRenderer | null = null
let context: RuntimeContext | null = null
let servers = 0

afterEach(() => {
act(() => renderer?.unmount())
Expand All @@ -55,14 +56,21 @@ describe('useNewWorkspaceRuntimeContext', () => {
return context
}

function Harness({ client }: { client: RpcClient }): null {
context = useNewWorkspaceRuntimeContext(client, true)
function Harness({ client, serverId }: { client: RpcClient; serverId?: string }): null {
context = useNewWorkspaceRuntimeContext(client, true, 'host-1', serverId)
return null
}

// Reuse the mounted harness so the second answer represents a host swap.
async function answer(settingsResult: unknown, uiResult: unknown): Promise<PublishedState> {
const element = createElement(Harness, { client: clientAnswering(settingsResult, uiResult) })
async function answer(
settingsResult: unknown,
uiResult: unknown,
serverId = `runtime:${++servers}`
): Promise<PublishedState> {
const element = createElement(Harness, {
client: clientAnswering(settingsResult, uiResult),
serverId
})
await act(async () => {
if (renderer) {
renderer.update(element)
Expand Down Expand Up @@ -138,4 +146,55 @@ describe('useNewWorkspaceRuntimeContext', () => {
)
expect((await answer({ settings: SETTINGS }, null)).trustedOrcaHooks).toEqual({})
})

async function swapToUnansweredClient(serverId: string): Promise<void> {
const next = new FakeSession('connected')
next.sendRequest.mockImplementation(() => new Promise<RpcResponse>(() => {}))
await act(async () => renderer?.update(createElement(Harness, { client: next, serverId })))
}

it('does not show the previous host settings or approvals while the new host has not answered', async () => {
await answer({ settings: SETTINGS }, UI_WITH_TRUST, 'runtime:a')
await swapToUnansweredClient('runtime:b')
expect(currentContext().runtimeSettings).toBeNull()
expect(currentContext().trustedOrcaHooks).toEqual({})
expect(currentContext().availableProviders).toEqual([])
})

// Any server's relay change rebuilds every scoped client, so the client object is not the host.
it('keeps the selected host values when its client is rebuilt', async () => {
const published = await answer({ settings: SETTINGS }, UI_WITH_TRUST, 'runtime:a')
await swapToUnansweredClient('runtime:a')
expect(currentContext().runtimeSettings).toEqual(published.runtimeSettings)
expect(currentContext().trustedOrcaHooks).toEqual(TRUSTED_HOOKS)
})

// A -> B -> A: the first A's Save callbacks must stay retired when A is picked again.
it('ignores publishers from an earlier selection, including of the same host', async () => {
let ui: unknown = UI_WITH_TRUST
const first = new FakeSession('connected')
first.sendRequest.mockImplementation(async (method: string) =>
reply(method === 'ui.get' ? ui : method === 'settings.get' ? { settings: SETTINGS } : {})
)
await act(async () => {
renderer = create(createElement(Harness, { client: first, serverId: 'runtime:a' }))
})
const retired = currentContext()
expect(retired.trustedOrcaHooks).toEqual(TRUSTED_HOOKS)

await answer({ settings: { defaultTuiAgent: 'claude' } }, { ui: {} }, 'runtime:b')
act(() => {
retired.setRuntimeSettings(SETTINGS)
retired.setTrustedOrcaHooks(TRUSTED_HOOKS)
})
expect(currentContext().runtimeSettings).toEqual({ defaultTuiAgent: 'claude' })
expect(currentContext().trustedOrcaHooks).toEqual({})

ui = { ui: {} }
await act(async () =>
renderer?.update(createElement(Harness, { client: first, serverId: 'runtime:a' }))
)
act(() => retired.setTrustedOrcaHooks(TRUSTED_HOOKS))
expect(currentContext().trustedOrcaHooks).toEqual({})
})
})
63 changes: 51 additions & 12 deletions mobile/src/components/use-new-workspace-runtime-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { optionalSettingsRead } from '../transport/settings-read-operations'
import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import type { PersistedTrustedOrcaHooks } from '../../../src/shared/orca-yaml-hook-types'
import type { RpcAcceptedResult } from '../transport/rpc-accepted-result'
import type { RpcClient } from '../transport/rpc-client'
Expand Down Expand Up @@ -34,10 +34,18 @@ function settledValue(
return verdict.accepted ? verdict.value : undefined
}

type OwnedRuntimeContext = {
owner: { hostId: string | undefined; serverId: string | undefined }
runtimeSettings: NewWorktreeRuntimeSettings | null
trustedOrcaHooks: PersistedTrustedOrcaHooks
availableProviders: TaskProvider[]
}

export function useNewWorkspaceRuntimeContext(
client: RpcClient | null,
visible: boolean,
hostId?: string
hostId?: string,
serverId?: string
): {
runtimeSettings: NewWorktreeRuntimeSettings | null
setRuntimeSettings: (settings: NewWorktreeRuntimeSettings) => void
Expand All @@ -46,9 +54,41 @@ export function useNewWorkspaceRuntimeContext(
availableProviders: TaskProvider[]
jiraConnection: MobileJiraConnection
} {
const [runtimeSettings, setRuntimeSettings] = useState<NewWorktreeRuntimeSettings | null>(null)
const [trustedOrcaHooks, setTrustedOrcaHooks] = useState<PersistedTrustedOrcaHooks>({})
const [availableProviders, setAvailableProviders] = useState<TaskProvider[]>([])
// Owned by the selected host, not the client object, which is rebuilt when any server's relay
// changes. A fresh owner per selection keeps A -> B -> A from reviving the first A's callbacks.
const [context, setContext] = useState<OwnedRuntimeContext>(() => ({
owner: { hostId, serverId },
runtimeSettings: null,
trustedOrcaHooks: {},
availableProviders: []
}))
const { owner } = context
const matchesOwner = owner.hostId === hostId && owner.serverId === serverId
if (!matchesOwner) {
setContext({
owner: { hostId, serverId },
runtimeSettings: null,
trustedOrcaHooks: {},
availableProviders: []
})
}
const runtimeSettings = matchesOwner ? context.runtimeSettings : null
const trustedOrcaHooks = matchesOwner ? context.trustedOrcaHooks : {}
const availableProviders = matchesOwner ? context.availableProviders : []
const publish = useCallback(
(patch: Partial<Omit<OwnedRuntimeContext, 'owner'>>) => {
setContext((current) => (current.owner === owner ? { ...current, ...patch } : current))
},
[owner]
)
const setRuntimeSettings = useCallback(
(settings: NewWorktreeRuntimeSettings) => publish({ runtimeSettings: settings }),
[publish]
)
const setTrustedOrcaHooks = useCallback(
(trust: PersistedTrustedOrcaHooks) => publish({ trustedOrcaHooks: trust }),
[publish]
)
// Tasks can offer Jira setup; the composer requires a connected site.
const [jiraConnection, setJiraConnection] = useMobileJiraConnectionState(
client,
Expand Down Expand Up @@ -105,12 +145,11 @@ export function useNewWorkspaceRuntimeContext(
readProbeMember(settledValue(linearRes, taskLinearStatusRead.interpret), 'connected') ===
true
const visibleProviders = normalizeVisibleTaskProviders(settingsValue?.visibleTaskProviders)
setAvailableProviders(
filterAvailableTaskProviders(visibleProviders, {
gitlabInstalled: glabInstalled,
linearConnected
}).filter((provider) => visibleProviders.includes(provider))
)
const availableProviders = filterAvailableTaskProviders(visibleProviders, {
gitlabInstalled: glabInstalled,
linearConnected
}).filter((provider) => visibleProviders.includes(provider))
publish({ availableProviders })
const [jiraRes] = await jiraProbe
if (!stale) {
setJiraConnection(
Expand All @@ -121,7 +160,7 @@ export function useNewWorkspaceRuntimeContext(
return () => {
stale = true
}
}, [visible, client, hostId])
}, [visible, client, hostId, publish, setRuntimeSettings, setTrustedOrcaHooks])

return {
runtimeSettings,
Expand Down
Loading