Repository navigation
fix(agents): apply paired host preferences to launch menus #31971
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
src/renderer/src/components/settings/use-host-agent-launch-preferences.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| import { useEffect, useState } from 'react' | ||
| import type { AgentLaunchSettings } from '../../../../shared/agent-launch-settings' | ||
| import type { GlobalSettings } from '../../../../shared/global-settings-types' | ||
| import { parseExecutionHostId } from '../../../../shared/execution-host' | ||
| import { lastVerifiedRuntimeStatus } from '../../../../shared/runtime-host-status' | ||
| import type { AppState } from '@/store' | ||
| import { | ||
| buildAgentLaunchRouteInput, | ||
| workspaceKindForWorktreeId | ||
| } from '@/lib/agent-launch-route-input' | ||
| import { getRuntimeEnvironmentRevision } from '@/runtime/runtime-environment-revision' | ||
| import { | ||
| readHostAgentLaunchSettings, | ||
| subscribeHostAgentLaunchSettings | ||
| } from './agent-launch-settings-transport' | ||
|
|
||
| type Preferences = Pick<AgentLaunchSettings, 'defaultTuiAgent' | 'disabledTuiAgents'> | ||
| /** `desktop`: the host predates host launch settings or has not answered, so this client's own choices apply. */ | ||
| type Snapshot = Preferences | 'desktop' | ||
| type Observation = { key: string; snapshot: Snapshot | null } | ||
|
|
||
| // Why: a stalled host must not hold the menu on placeholders for the full RPC timeouts. | ||
| const FIRST_READ_DEADLINE_MS = 3_000 | ||
|
|
||
| // Why: menus mount on open; the last answer per host lets them render at once while revalidating. | ||
| const snapshotByEnvironment = new Map<string, { key: string; snapshot: Snapshot }>() | ||
| subscribeHostAgentLaunchSettings((owner) => { | ||
| snapshotByEnvironment.delete(owner.environmentId) | ||
| }) | ||
|
|
||
| export function clearHostAgentMenuSettingsForTests(): void { | ||
| snapshotByEnvironment.clear() | ||
| } | ||
|
|
||
| function snapshotKey( | ||
| environmentId: string | undefined, | ||
| pairingRevision: number | undefined, | ||
| runtimeId: string | null | undefined | ||
| ): string { | ||
| return JSON.stringify([environmentId, pairingRevision, runtimeId]) | ||
| } | ||
|
|
||
| function cachedSnapshot(environmentId: string | undefined, key: string): Snapshot | null { | ||
| const cached = environmentId ? snapshotByEnvironment.get(environmentId) : undefined | ||
| return cached?.key === key ? cached.snapshot : null | ||
| } | ||
|
|
||
| /** The launch preferences the worktree's menu last applied, so shortcuts launch what the menu shows. */ | ||
| export function readAgentLaunchPreferencesForWorktree( | ||
| state: AppState, | ||
| worktreeId: string | ||
| ): { [K in keyof Preferences]: Preferences[K] | undefined } { | ||
| const desktop = { | ||
| defaultTuiAgent: state.settings?.defaultTuiAgent, | ||
| disabledTuiAgents: state.settings?.disabledTuiAgents | ||
| } | ||
| const workspace = { kind: workspaceKindForWorktreeId(worktreeId), worktreeId } | ||
| const host = parseExecutionHostId( | ||
| buildAgentLaunchRouteInput(state, { agent: 'claude', workspace }).executionHostId | ||
| ) | ||
| if (host?.kind !== 'runtime') { | ||
| return desktop | ||
| } | ||
| const status = state.runtimeStatusByEnvironmentId.get(host.environmentId) | ||
| const key = snapshotKey( | ||
| host.environmentId, | ||
| getRuntimeEnvironmentRevision(host.environmentId), | ||
| lastVerifiedRuntimeStatus(status)?.runtimeId | ||
| ) | ||
| const snapshot = cachedSnapshot(host.environmentId, key) | ||
| return snapshot && snapshot !== 'desktop' ? snapshot : desktop | ||
| } | ||
|
|
||
| export function useHostAgentMenuSettings( | ||
| owner: { | ||
| executionHostId: string | ||
| pairingRevision: number | undefined | ||
| runtimeId: string | null | undefined | ||
| contact: string | ||
| } | null, | ||
| desktopSettings: GlobalSettings | null, | ||
| active: boolean, | ||
| open: boolean | ||
| ) { | ||
| const host = parseExecutionHostId(owner?.executionHostId) | ||
| const environmentId = host?.kind === 'runtime' ? host.environmentId : undefined | ||
| const { pairingRevision, runtimeId, contact } = owner ?? {} | ||
| const key = snapshotKey(environmentId, pairingRevision, runtimeId) | ||
| const [observation, setObservation] = useState<Observation>(() => ({ | ||
| key, | ||
| snapshot: cachedSnapshot(environmentId, key) | ||
| })) | ||
| useEffect(() => { | ||
| if (!environmentId || !active) { | ||
| return | ||
| } | ||
| const capturedOwner = { environmentId, pairingRevision, runtimeId: runtimeId ?? undefined } | ||
| let controller = new AbortController() | ||
| let live = true | ||
| const deadline = setTimeout(() => { | ||
| if (live) { | ||
| setObservation((prev) => | ||
| prev.key === key && prev.snapshot | ||
| ? prev | ||
| : { key, snapshot: cachedSnapshot(environmentId, key) ?? 'desktop' } | ||
| ) | ||
| } | ||
| }, FIRST_READ_DEADLINE_MS) | ||
| const refresh = (): void => { | ||
| controller.abort() | ||
| const readController = new AbortController() | ||
| controller = readController | ||
| void readHostAgentLaunchSettings(capturedOwner, readController.signal).then( | ||
| (settings) => { | ||
| if (live && !readController.signal.aborted) { | ||
| const snapshot = settings | ||
| ? { | ||
| defaultTuiAgent: settings.defaultTuiAgent, | ||
| disabledTuiAgents: settings.disabledTuiAgents | ||
| } | ||
| : 'desktop' | ||
| snapshotByEnvironment.set(environmentId, { key, snapshot }) | ||
| setObservation({ key, snapshot }) | ||
| } | ||
| }, | ||
| () => { | ||
| if (live && !readController.signal.aborted) { | ||
| // Why: a failed refresh keeps this runtime's last answer; with none, desktop choices apply as before. | ||
| setObservation({ key, snapshot: cachedSnapshot(environmentId, key) ?? 'desktop' }) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| const unsubscribe = subscribeHostAgentLaunchSettings((changedOwner) => { | ||
| if ( | ||
| changedOwner.environmentId === environmentId && | ||
| changedOwner.pairingRevision === pairingRevision | ||
| ) { | ||
| refresh() | ||
| } | ||
| }) | ||
| refresh() | ||
| return () => { | ||
| live = false | ||
| clearTimeout(deadline) | ||
| controller.abort() | ||
| unsubscribe() | ||
| } | ||
| }, [environmentId, pairingRevision, runtimeId, key, contact, active, open]) | ||
| if (!environmentId) { | ||
| return { settings: desktopSettings, pending: false } | ||
| } | ||
| const snapshot = | ||
| (observation.key === key ? observation.snapshot : null) ?? cachedSnapshot(environmentId, key) | ||
| if (snapshot === null) { | ||
| return { settings: desktopSettings, pending: true } | ||
| } | ||
| if (snapshot === 'desktop') { | ||
| return { settings: desktopSettings, pending: false } | ||
| } | ||
| return { | ||
| pending: false, | ||
| settings: { | ||
| ...desktopSettings, | ||
| defaultTuiAgent: snapshot.defaultTuiAgent, | ||
| disabledTuiAgents: snapshot.disabledTuiAgents | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: stablyai/orca
Length of output: 7199
Bound the combined host-settings read deadline.
When the paired host responds slowly,
readHostAgentLaunchSettingscan spend 15 seconds in the capability check and then another 15 seconds insettings.getAgentLaunch.useHostAgentMenuSettingskeepspendingtrue until both operations settle, so the new-tab menu can remain in its loading state for about 30 seconds whileinventoryReadyremains false.Use one 15-second deadline for both operations and pass the remaining time to the RPC.
♻️ Suggested fix
export async function readHostAgentLaunchSettings( owner: AgentLaunchSettingsOwner, signal?: AbortSignal ): Promise<AgentLaunchSettings | null> { + const deadline = Date.now() + 15_000 if ( !(await runtimeEnvironmentSupportsCapability( owner.environmentId, AGENT_LAUNCH_SETTINGS_CAPABILITY, - 15_000 + Math.max(1, deadline - Date.now()) )) ) { return null } ... - timeoutMs: 15_000 + timeoutMs: Math.max(1, deadline - Date.now())