From c7f3dab2b527bb51662bea495127ebc433aead0a Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:04:17 -0800 Subject: [PATCH] Keep model discovery reads and waits in their own phases --- .../launch-settings/use-agent-model-menu.ts | 5 +- .../host-model-menu-catalog-phases.test.ts | 102 ++++++++++++++++++ .../model-catalogs/host-model-menu-catalog.ts | 90 +++++++++------- 3 files changed, 160 insertions(+), 37 deletions(-) create mode 100644 src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog-phases.test.ts diff --git a/src/renderer/src/components/agent/launch-settings/use-agent-model-menu.ts b/src/renderer/src/components/agent/launch-settings/use-agent-model-menu.ts index 8a1d6a36d17..ccfea032cbf 100644 --- a/src/renderer/src/components/agent/launch-settings/use-agent-model-menu.ts +++ b/src/renderer/src/components/agent/launch-settings/use-agent-model-menu.ts @@ -38,8 +38,11 @@ export function useAgentModelMenu( const snapshot = useAppViewExternalStore(subscribe, getSnapshot, () => absent) useEffect(() => { if (catalog && open && active) { - void catalog.read(source?.source === 'cli') + const controller = new AbortController() + void catalog.read(source?.source === 'cli', undefined, controller.signal) + return () => controller.abort() } + return undefined }, [catalog, open, active, snapshot.generation, source?.source]) const menu = modelMenuOptions(snapshot.answer, { agent, diff --git a/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog-phases.test.ts b/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog-phases.test.ts new file mode 100644 index 00000000000..8cf2bdd16af --- /dev/null +++ b/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog-phases.test.ts @@ -0,0 +1,102 @@ +import { beforeEach, expect, it, vi } from 'vitest' +import type { AgentSessionModelCatalogResult } from '../../../../../shared/agent-session/agent-session-wire' +import { hostModelMenuCatalog } from './host-model-menu-catalog' +import { resetHostModelCatalogSnapshotsForTests } from './host-model-catalog-snapshots' + +const target = { kind: 'local' } as const +const listing = { origin: 'unknown', listingInProgress: true } as const +const expired = { ...listing, waitExpired: true } as const +const models = (id: string): AgentSessionModelCatalogResult => ({ + origin: 'probe', + fetchedAt: 1, + listingNamesConfiguredModel: false, + models: [{ id, label: id, isDefault: false, efforts: [] }] +}) +beforeEach(() => resetHostModelCatalogSnapshotsForTests()) + +it('keeps the first timed-out list through failure without authorizing another refresh, then reopens', async () => { + const request = vi + .fn<(wait: boolean) => Promise>() + .mockResolvedValueOnce(listing) + .mockResolvedValueOnce({ ...models('gpt-6.1-sol'), listingInProgress: true, waitExpired: true }) + .mockRejectedValue(new Error('host offline')) + const scope = { target, agent: 'codex', worktree: 'id:expired-first-list', request } + const owner = hostModelMenuCatalog(scope) + const stop = owner.subscribe(() => {}) + try { + await owner.read(false, false) + let answer = await owner.read(false, true) + // One plain read, one genuinely expired wait, and one failed successor exhaust demand. + for (let attempts = 0; answer?.waitExpired && attempts < 2; attempts++) { + answer = await owner.read(false, true) + } + expect(request.mock.calls).toEqual([[false], [true], [true]]) + expect(answer).toMatchObject({ models: [{ id: 'gpt-6.1-sol' }] }) + expect(answer?.waitExpired).toBeUndefined() + expect(owner.snapshot.pending).toBe(false) + } finally { + stop() + } + request.mockResolvedValueOnce(models('reconnected')) + const successor = hostModelMenuCatalog(scope) + const stopSuccessor = successor.subscribe(() => {}) + try { + expect(await successor.read()).toMatchObject({ models: [{ id: 'reconnected' }] }) + expect(request.mock.calls).toEqual([[false], [true], [true], [false]]) + } finally { + stopSuccessor() + } +}) + +it('gives a waiting caller a waited result and follows expired waits only while the menu stays open', async () => { + const plain = Promise.withResolvers() + const wait = Promise.withResolvers() + const request = vi + .fn<(wait: boolean) => Promise>() + .mockReturnValueOnce(plain.promise) + .mockReturnValueOnce(wait.promise) + .mockResolvedValueOnce(models('finally-listed')) + const owner = hostModelMenuCatalog({ target, agent: 'codex', worktree: 'id:phases', request }) + const stop = owner.subscribe(() => {}) + const open = new AbortController() + try { + const plainRead = owner.read(false, false) + const waitingRead = owner.read(false, true) + const automaticRead = owner.read(false, undefined, open.signal) + plain.resolve(listing) + expect(await plainRead).toMatchObject(listing) + await expect.poll(() => request.mock.calls).toEqual([[false], [true]]) + wait.resolve(expired) + expect(await waitingRead).toMatchObject({ listingInProgress: true, waitExpired: true }) + expect(await automaticRead).toMatchObject({ models: [{ id: 'finally-listed' }] }) + expect(owner.snapshot.answer).toMatchObject({ models: [{ id: 'finally-listed' }] }) + expect(request.mock.calls).toEqual([[false], [true], [true]]) + } finally { + open.abort() + stop() + plain.resolve(listing) + wait.resolve(expired) + } + + const closingWait = Promise.withResolvers() + const closingRequest = vi + .fn<(wait: boolean) => Promise>() + .mockResolvedValueOnce(listing) + .mockReturnValueOnce(closingWait.promise) + .mockResolvedValue(models('must-not-request')) + const closing = hostModelMenuCatalog({ target, agent: 'claude', request: closingRequest }) + const stopClosing = closing.subscribe(() => {}) + const closed = new AbortController() + try { + const reading = closing.read(false, undefined, closed.signal) + await expect.poll(() => closingRequest.mock.calls).toEqual([[false], [true]]) + closed.abort() + closingWait.resolve(expired) + await reading + expect(closingRequest.mock.calls).toEqual([[false], [true]]) + } finally { + closed.abort() + stopClosing() + closingWait.resolve(expired) + } +}) diff --git a/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog.ts b/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog.ts index a535fb3dab5..bbf8fae80f8 100644 --- a/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog.ts +++ b/src/renderer/src/runtime/native-chat/model-catalogs/host-model-menu-catalog.ts @@ -24,20 +24,31 @@ export type HostModelMenuSnapshot = { generation: number } type Reader = (waitForListing: boolean) => Promise +type PhaseResult = { + answer?: AgentSessionModelCatalogResult + reply?: AgentSessionModelCatalogResult + waited?: boolean +} const owners = new Map() +function withoutWaitExpiry(answer: AgentSessionModelCatalogResult) { + if (!answer.waitExpired) { + return answer + } + const { waitExpired: _waitExpired, ...catalog } = answer + return catalog +} function retained(answer: AgentSessionModelCatalogResult | undefined) { return answer?.listingInProgress && (answer.origin === 'unknown' || !answer.models.length) ? undefined - : answer + : answer && withoutWaitExpiry(answer) } function withListingStatus( held: AgentSessionModelCatalogResult, answer: AgentSessionModelCatalogResult ): AgentSessionModelCatalogResult { - const { waitExpired: _waitExpired, ...catalog } = held return { - ...catalog, + ...withoutWaitExpiry(held), listingInProgress: true, ...(answer.waitExpired ? { waitExpired: true } : {}) } @@ -58,7 +69,7 @@ const CLI_REUSE_INTERVAL_MS = 10 * 60 * 1000 class HostModelMenuCatalog { listeners = new Set<() => void>() snapshot: HostModelMenuSnapshot - private pending?: Promise + private pending?: { waited: boolean; result: Promise } private unsubscribe?: () => void constructor(readonly scope: HostModelMenuScope) { this.snapshot = { @@ -121,12 +132,34 @@ class HostModelMenuCatalog { this.retire() } } - read( + async read( reuseHeld = false, - waitForListing?: boolean + waitForListing?: boolean, + signal?: AbortSignal ): Promise { + const generation = this.snapshot.generation + let result = await this.readPhase(reuseHeld, waitForListing ?? false) + while ( + waitForListing === undefined && + result.reply?.listingInProgress && + (!result.waited || result.reply.waitExpired) && + generation === this.snapshot.generation && + !signal?.aborted && + (signal !== undefined || this.listeners.size > 0) + ) { + result = await this.readPhase(false, true) + } + return result.answer + } + private readPhase(reuseHeld: boolean, waitForListing: boolean): Promise { + const { target, agent, worktree } = this.scope + const generation = hostModelCatalogGeneration(target, agent, this.scope.sourceKey) + const current = () => + generation === hostModelCatalogGeneration(target, agent, this.scope.sourceKey) if (this.pending) { - return this.pending + return waitForListing && !this.pending.waited + ? this.pending.result.then(() => (current() ? this.readPhase(false, true) : {})) + : this.pending.result } const completedAt = this.scope.sourceKey ? hostModelCatalogCompletedAttempt( @@ -144,12 +177,8 @@ class HostModelMenuCatalog { ) { this.publish({ ...this.snapshot, pending: false, attempted: true }) this.retire() - return Promise.resolve(this.snapshot.answer) + return Promise.resolve({ answer: retained(this.held() ?? this.snapshot.answer) }) } - const { target, agent, worktree } = this.scope - const generation = hostModelCatalogGeneration(target, agent, this.scope.sourceKey) - const current = () => - generation === hostModelCatalogGeneration(target, agent, this.scope.sourceKey) const request: Reader = this.scope.request ?? ((waitForListing) => @@ -158,31 +187,17 @@ class HostModelMenuCatalog { ...(worktree ? { worktree } : {}), ...(waitForListing ? { waitForListing: true } : {}) })) - this.publish({ ...this.snapshot, pending: true }) + this.publish({ ...this.snapshot, answer: retained(this.snapshot.answer), pending: true }) let contextUnresolved = false - const operation: Promise = Promise.resolve() - .then(() => request(waitForListing ?? false)) - .then(async (plain) => { - if (!current()) { - return this.pending !== operation ? this.pending : undefined - } - if (waitForListing === undefined && plain?.listingInProgress) { - this.publish({ - ...this.snapshot, - answer: withListingStatus(retained(this.snapshot.answer) ?? plain, plain), - pending: true - }) - return request(true) - } - return plain - }) + const operation: Promise = Promise.resolve() + .then(() => request(waitForListing)) .catch((error) => { contextUnresolved = error instanceof ModelMenuContextUnresolvedError return undefined }) .then((answer) => { if (!current()) { - return this.pending !== operation ? this.pending : undefined + return this.pending?.result !== operation ? (this.pending?.result ?? {}) : {} } if (this.scope.sourceKey && !contextUnresolved && !answer?.listingInProgress) { hostModelCatalogCompletedAttempt( @@ -198,25 +213,28 @@ class HostModelMenuCatalog { target, agent, worktree ?? null, - answer, + withoutWaitExpiry(answer), this.scope.sourceKey ) } const shown = answer?.listingInProgress - ? withListingStatus(this.held() ?? answer, answer) + ? withListingStatus(this.held() ?? answer, { + ...answer, + waitExpired: waitForListing && answer.waitExpired ? true : undefined + }) : answer ? this.held() - : (this.held() ?? retained(this.snapshot.answer)) + : retained(this.held() ?? this.snapshot.answer) this.publish({ answer: shown, pending: false, attempted: true }) - return shown + return { answer: shown, reply: answer, waited: waitForListing } }) .finally(() => { - if (this.pending === operation) { + if (this.pending?.result === operation) { this.pending = undefined } this.retire() }) - this.pending = operation + this.pending = { waited: waitForListing, result: operation } return operation } private retire() {