Keep model discovery reads and waits in their own phases

This commit is contained in:
Brennan Benson
2026-11-09 18:28:46 -08:00
parent 6babe36b69
commit c7f3dab2b5
3 changed files with 160 additions and 37 deletions
@@ -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,
@@ -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<AgentSessionModelCatalogResult>>()
.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<AgentSessionModelCatalogResult>()
const wait = Promise.withResolvers<AgentSessionModelCatalogResult>()
const request = vi
.fn<(wait: boolean) => Promise<AgentSessionModelCatalogResult>>()
.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<AgentSessionModelCatalogResult>()
const closingRequest = vi
.fn<(wait: boolean) => Promise<AgentSessionModelCatalogResult>>()
.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)
}
})
@@ -24,20 +24,31 @@ export type HostModelMenuSnapshot = {
generation: number
}
type Reader = (waitForListing: boolean) => Promise<AgentSessionModelCatalogResult | undefined>
type PhaseResult = {
answer?: AgentSessionModelCatalogResult
reply?: AgentSessionModelCatalogResult
waited?: boolean
}
const owners = new Map<string, HostModelMenuCatalog>()
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<AgentSessionModelCatalogResult | undefined>
private pending?: { waited: boolean; result: Promise<PhaseResult> }
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<AgentSessionModelCatalogResult | undefined> {
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<PhaseResult> {
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<AgentSessionModelCatalogResult | undefined> = 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<PhaseResult> = 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() {