Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(pi): use discovery for shared model menus and defaults (#36194)

Brennan Benson46595532b9a8
diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry.ts
index e8f805eb1ee..3d39e93223e 100644
--- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry.ts
+++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-entry.ts
@@ -265,7 +265,15 @@ export function agentModelCatalogEntryWithSuccess(
at
}
const { agent, fingerprint } = identity
- const configured = previous?.configured ?? null
+ let configured = previous?.configured ?? null
+ if (agent === 'pi' && source === 'discovery') {
+ const { model, effort } = success.configured ?? {}
+ if (model) {
+ const facts = configuredDefaultFacts({ modelId: model, effort }, configured)
+ configured = facts && { ...facts, at }
+ }
+ return agentModelCatalogEntry(agent, fingerprint, listing, null, configured)
+ }
return source === 'discovery'
? agentModelCatalogEntry(agent, fingerprint, listing, previous?.live ?? null, configured)
: agentModelCatalogEntry(agent, fingerprint, previous?.discovered ?? null, listing, configured)
@@ -304,13 +312,8 @@ export function entryWithConfiguredDefault(
if (JSON.stringify(next) === JSON.stringify(configuredDefaultFacts(entry.configured))) {
return null
}
- return agentModelCatalogEntry(
- entry.agent,
- entry.fingerprint,
- entry.discovered,
- entry.live,
- next && { ...next, at }
- )
+ const { agent, fingerprint, discovered, live } = entry
+ return agentModelCatalogEntry(agent, fingerprint, discovered, live, next && { ...next, at })
}
/** Exclude clocks so unchanged catalog facts do not trigger disk writes. */
diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-live-listings.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-live-listings.ts
index b1382f3d575..8fc95016174 100644
--- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-live-listings.ts
+++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-live-listings.ts
@@ -141,7 +141,12 @@ export function createAgentModelCatalogLiveListings(
source?: () => boolean
) {
const record = deps.getRecord(sessionId)
- if (!record || !deps.drivesRecord(record) || independentAgentModelCatalog(record.provider)) {
+ if (
+ !record ||
+ !deps.drivesRecord(record) ||
+ record.provider === 'pi' ||
+ independentAgentModelCatalog(record.provider)
+ ) {
return
}
const receipt = leaseWorkspace(record.location.workspaceId)
diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.test.ts
index 12744810878..8fb1401b66d 100644
--- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.test.ts
+++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-persistence.test.ts
@@ -2,13 +2,13 @@ import { mkdtempSync } from 'node:fs'
import * as fsPromises from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
-import { afterEach, expect, it, vi } from 'vitest'
+import { afterEach, expect, it, onTestFinished, vi } from 'vitest'
import { createAgentModelCatalogFilePersistence } from './agent-model-catalog-persistence'
import {
AgentModelCatalogStore,
AGENT_MODEL_CATALOG_MAX_ENTRIES
} from './agent-model-catalog-store'
-import type { AgentModelCatalogEntry } from './agent-model-catalog-entry'
+import { agentModelCatalogEntry, type AgentModelCatalogEntry } from './agent-model-catalog-entry'
vi.mock('node:fs/promises', async (original) => {
const actual = await original<typeof fsPromises>()
@@ -166,3 +166,95 @@ it('keeps optional cache write failure from poisoning subsequent serialized writ
await store.flushPersistence()
expect((await persistence.load())[0]?.models[0]?.id).toBe('B')
})
+
+it.each([
+ [false, 10, ['low']],
+ [true, 10, ['low']],
+ [false, 1, []],
+ [true, 1, []]
+] as const)(
+ 'replaces historical Pi live capabilities only after discovery succeeds (mixed=%s, at=%s)',
+ async (mixed, at, levels) => {
+ const directory = mkdtempSync(join(tmpdir(), 'pr-36194-pi-cache-'))
+ onTestFinished(() => actual.rm(directory, { recursive: true, force: true }))
+ const oldModels = [
+ {
+ id: 'openai/gpt-6',
+ label: 'GPT-6',
+ isDefault: false,
+ efforts: [{ value: 'high', label: 'high' }],
+ defaultEffort: 'high'
+ }
+ ]
+ const legacy = agentModelCatalogEntry(
+ 'pi',
+ 'pi-fp',
+ mixed
+ ? {
+ origin: 'probe',
+ at: 5,
+ models: oldModels
+ }
+ : null,
+ {
+ origin: 'live-session',
+ at: 10,
+ models: [
+ ...oldModels,
+ {
+ id: 'project/only',
+ label: 'Project',
+ isDefault: false,
+ efforts: []
+ }
+ ],
+ fastModeSupport: { supported: true }
+ },
+ { modelId: 'openai/gpt-6', effort: 'high', at: 3 }
+ )!
+ const other = { ...legacy, agent: 'grok', fingerprint: 'other-host-account' }
+ await actual.writeFile(
+ join(directory, 'agent-model-catalog.json'),
+ JSON.stringify({
+ version: 3,
+ entries: [legacy, other]
+ })
+ )
+ const store = new AgentModelCatalogStore({ now: () => at })
+ await store.attachPersistence(createAgentModelCatalogFilePersistence(directory))
+ const hydrated = store.get('pi-fp')!
+ expect(hydrated.models).toEqual(legacy.models)
+ const configured = hydrated.configured
+ const otherBefore = store.get('other-host-account')
+ const fail = async () => {
+ throw new Error('RPC failed')
+ }
+ await store.refresh('pi-fp', 'pi', fail)
+ expect(store.get('pi-fp')).toBe(hydrated)
+ await store.refresh('pi-fp', 'pi', async () => ({ origin: 'probe', models: [] }))
+ expect(store.get('pi-fp')).toBe(hydrated)
+ const discovered = [
+ {
+ id: 'openai/gpt-6',
+ label: 'GPT-6',
+ isDefault: false,
+ efforts: levels.map((value) => ({ value, label: value }))
+ }
+ ]
+ await store.refresh('pi-fp', 'pi', async () => ({ origin: 'probe', models: discovered }))
+ const fresh = store.get('pi-fp')!
+ expect(fresh.live).toBeNull()
+ expect(fresh.configured).toBe(configured)
+ expect(fresh.models).toEqual([{ ...discovered[0], isDefault: true }])
+ expect(fresh).not.toHaveProperty('fastModeSupport')
+ expect(store.get('other-host-account')).toBe(otherBefore)
+ await store.flushPersistence()
+ const reloaded = new AgentModelCatalogStore({ now: () => at + 1 })
+ await reloaded.attachPersistence(createAgentModelCatalogFilePersistence(directory))
+ expect(reloaded.get('pi-fp')).toEqual(fresh)
+ await reloaded.refresh('pi-fp', 'pi', fail)
+ expect(reloaded.get('pi-fp')?.models).toEqual(fresh.models)
+ expect(reloaded.get('pi-fp')?.live).toBeNull()
+ expect(reloaded.get('other-host-account')).toEqual(otherBefore)
+ }
+)
diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-prewarm.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-prewarm.test.ts
index 6897bd32e1f..7d904d1c1ec 100644
--- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-prewarm.test.ts
+++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-prewarm.test.ts
@@ -538,7 +538,7 @@ it('keeps old producers from publishing rows, defaults or failure expiry after r
expect(save).not.toHaveBeenCalled()
})
-it('withholds a known retired Pi invocation until a current live producer lists', async () => {
+it('withholds a known retired Pi invocation until current discovery succeeds', async () => {
const store = new AgentModelCatalogStore()
const record = agentSessionRecordFixture({
sessionId: 'pi-chat',
@@ -562,7 +562,7 @@ it('withholds a known retired Pi invocation until a current live producer lists'
})
const old = service.captureSource(record)
store.expireAgent('pi')
- void service.demand({ agent: 'pi', sessionId: record.sessionId })
+ const demand = service.demand({ agent: 'pi', sessionId: record.sessionId })
await vi.waitFor(() => expect(probe).toHaveBeenCalledTimes(1))
expect(service.snapshot({ agent: 'pi', sessionId: record.sessionId, savedOnly: true })).toEqual({
origin: 'unknown'
@@ -576,9 +576,15 @@ it('withholds a known retired Pi invocation until a current live producer lists'
listing('current-child'),
service.captureSource(record)
)
- expect(
- service.snapshot({ agent: 'pi', sessionId: record.sessionId, savedOnly: true })
- ).toMatchObject({ models: [{ id: 'current-child' }] })
+ expect(service.snapshot({ agent: 'pi', sessionId: record.sessionId, savedOnly: true })).toEqual({
+ origin: 'unknown'
+ })
+ listingPending.resolve(listing('current-discovery'))
+ await demand
+ const recovered = service.snapshot({ agent: 'pi', sessionId: record.sessionId, savedOnly: true })
+ expect(recovered.origin).toBe('probe')
+ expect(recovered.origin !== 'unknown' && recovered.models.map((model) => model.id)).toEqual([
+ 'current-discovery'
+ ])
service.stop()
- listingPending.resolve(listing('stopped'))
})
diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.test.ts
index 4407813c130..cb304f08e72 100644
--- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.test.ts
+++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-service.test.ts
@@ -5,7 +5,7 @@ import {
isHostModelListingWaitInFlight
} from '../../../shared/host-model-listing-waits'
import type { AgentSessionAccountHome } from '../../../shared/agent-session-account-home'
-import { describe, expect, it, vi } from 'vitest'
+import { describe, expect, it, onTestFinished, vi } from 'vitest'
import type { AgentSessionRecord } from '../../../shared/agent-session-record'
import {
agentModelCatalogFingerprint,
@@ -694,3 +694,68 @@ it('shares account rows while keeping workspace default provenance separate', as
service.stop()
}
})
+
+it.each(['pi', 'grok'])(
+ '%s applies live publication policy to both inventory and configured defaults',
+ async (agent) => {
+ const saved = agentSessionRecordFixture({
+ ...record('/same-account'),
+ provider: agent,
+ accountHome: {
+ variable: agent === 'pi' ? 'PI_CODING_AGENT_DIR' : 'GROK_HOME',
+ path: '/same-account'
+ }
+ })
+ const store = new AgentModelCatalogStore()
+ const fingerprint = agentModelCatalogFingerprintForRecord(saved)
+ const service = createAgentModelCatalogService({
+ store,
+ getRecord: () => saved,
+ getConversation: () => agentSessionConversationView(saved),
+ drivesRecord: () => true,
+ resolveAccountHome: async () => saved.accountHome,
+ recordWorkspacePath: async () => '/workspace',
+ workspaceMayOverrideDefaultModel: async () => false
+ })
+ onTestFinished(() => service.stop())
+ const defaultWrite = vi.spyOn(store, 'recordConfiguredDefault')
+ const inventoryWrite = vi.spyOn(store, 'recordSuccess')
+ const publish = (id: string) =>
+ service.recordLiveListing(saved.sessionId, {
+ models: listing(id).models,
+ configuredDefault: { modelId: id, effort: 'high' }
+ })
+ publish('first')
+ if (agent === 'pi') {
+ await Promise.resolve()
+ expect(inventoryWrite).not.toHaveBeenCalled()
+ expect(defaultWrite).not.toHaveBeenCalled()
+ expect(store.get(fingerprint)).toBeNull()
+ } else {
+ await vi.waitFor(() => expect(store.get(fingerprint)?.configured?.modelId).toBe('first'))
+ }
+ store.recordSuccess(fingerprint, agent, listing('account'), 'discovery')
+ const good = store.get(fingerprint)
+ publish('late-project')
+ service.recordLiveListing(saved.sessionId, {
+ models: listing('late-project').models,
+ configuredDefault: null
+ })
+ if (agent === 'pi') {
+ await Promise.resolve()
+ expect(store.get(fingerprint)).toBe(good)
+ expect(defaultWrite).not.toHaveBeenCalled()
+ expect(inventoryWrite).toHaveBeenCalledOnce()
+ } else {
+ await vi.waitFor(() => expect(store.get(fingerprint)?.configured).toBeNull())
+ expect(store.get(fingerprint)?.models[0]?.id).toBe('late-project')
+ expect(defaultWrite).toHaveBeenCalledTimes(3)
+ }
+ await store.refresh(fingerprint, agent, async () => {
+ throw new Error('probe failed')
+ })
+ expect(store.hasActiveFailure(fingerprint)).toBe(true)
+ service.providerStarted(saved)
+ await vi.waitFor(() => expect(store.hasActiveFailure(fingerprint)).toBe(false))
+ }
+)
diff --git a/src/main/pi/rpc-model-catalog-probe.test.ts b/src/main/pi/rpc-model-catalog-probe.test.ts
index d692e93c628..d16f7d3ae6c 100644
--- a/src/main/pi/rpc-model-catalog-probe.test.ts
+++ b/src/main/pi/rpc-model-catalog-probe.test.ts
@@ -1,7 +1,23 @@
-import { describe, expect, it, vi } from 'vitest'
+import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest'
import type { ProviderProcessLaunch } from '../provider-process/provider-process-launch'
import { createPiModelCatalogProbe } from './rpc-model-catalog-probe'
-import { readPiRpcSessionOptions } from './rpc-options'
+import { readPiRpcSessionOptions, type PiRpcRequester } from './rpc-options'
+import { agentSessionConversationView } from '../../shared/agent-session-conversation-view'
+import {
+ applyStructuredAgentSessionModelCatalog,
+ createStructuredAgentSessionOptionState,
+ structuredAgentSessionOptionSnapshot
+} from '../../shared/structured-agent-session-options'
+import { structuredAgentSessionMessageSelection } from '../../shared/structured-agent-session-option-view'
+import { structuredAgentSessionSeedCatalog } from '../../shared/structured-agent-session-seed-catalog'
+import { agentSessionRecordFixture } from '../native-chat/agent-session-record-test-fixture'
+import { agentModelCatalogFingerprintForRecord } from '../native-chat/agent-model-catalog/agent-model-catalog-fingerprint'
+import { createAgentModelCatalogService } from '../native-chat/agent-model-catalog/agent-model-catalog-service'
+import {
+ AGENT_MODEL_CATALOG_FAILURE_TTL_MS,
+ AgentModelCatalogStore
+} from '../native-chat/agent-model-catalog/agent-model-catalog-store'
+import * as probeRunner from '../native-chat/agent-model-catalog/agent-model-catalog-probe-runner'
const AVAILABLE_MODELS = {
models: [
@@ -16,11 +32,55 @@ const AVAILABLE_MODELS = {
]
}
+afterEach(() => {
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+})
+
+function catalog(request: PiRpcRequester['request'], now = Date.now) {
+ const record = agentSessionRecordFixture({
+ sessionId: 'pi-chat',
+ provider: 'pi',
+ accountHome: { variable: 'PI_CODING_AGENT_DIR', path: '/homes/pi' }
+ })
+ const close = vi.fn(async (): Promise<void> => undefined)
+ const runListing = vi
+ .spyOn(probeRunner, 'runAgentModelCatalogListing')
+ .mockRejectedValue(new Error('unexpected text listing'))
+ const probe = createPiModelCatalogProbe({
+ resolveEnvironment: async () => ({ PATH: '/usr/bin' }),
+ resolveCommand: () => '/opt/pi/bin/pi',
+ probeVersion: async () => ({ version: '1.1.0' }),
+ homePath: '/home/user',
+ connect: () => ({ request, close })
+ })
+ const store = new AgentModelCatalogStore({ now })
+ const service = createAgentModelCatalogService({
+ store,
+ getConversation: () => agentSessionConversationView(record),
+ getRecord: () => record,
+ drivesRecord: () => true,
+ resolveAccountHome: async () => record.accountHome,
+ probes: { pi: probe }
+ })
+ onTestFinished(() => service.stop())
+ return {
+ store,
+ service,
+ probe,
+ record,
+ close,
+ runListing,
+ fingerprint: agentModelCatalogFingerprintForRecord(record)
+ }
+}
+
describe('Pi model catalog', () => {
it.each(['', `"${process.execPath}" run -- pi`])(
'lists the native menu through sessionless RPC for Command %j',
async (command) => {
const launches: ProviderProcessLaunch[] = []
+ const requests: string[] = []
const close = vi.fn(async () => undefined)
const probeVersion = vi.fn(async () => ({ version: '0.84.0' }))
const probe = createPiModelCatalogProbe({
@@ -37,14 +97,18 @@ describe('Pi model catalog', () => {
launches.push(launch)
return {
request: async (command) => {
- expect(command).toBe('get_available_models')
- return AVAILABLE_MODELS
+ requests.push(command)
+ return command === 'get_state'
+ ? { model: { provider: 'openai', id: 'gpt-6' }, thinkingLevel: 'xhigh' }
+ : AVAILABLE_MODELS
},
close
}
}
})
const success = await probe({ variable: 'PI_CODING_AGENT_DIR', path: '/homes/pi-a' })
+ expect(requests).toEqual(['get_state', 'get_available_models'])
+ expect(success.configured).toEqual({ model: 'openai/gpt-6', effort: 'xhigh' })
const levels = (values: string[]) => values.map((value) => ({ value, label: value }))
expect(success.models).toEqual([
{
@@ -82,63 +146,304 @@ describe('Pi model catalog', () => {
}
)
- it('falls back to the text listing when Pi cannot answer over RPC', async () => {
- const runListing = vi.fn(async () =>
- [
- 'provider model context max-out thinking images',
- 'anthropic claude-sonnet-4 200K 64K yes yes',
- 'openai gpt-4o-mini 128K 16K no yes'
- ].join('\n')
+ it.each(['error', 'timeout', 'early exit', 'empty'] as const)(
+ 'keeps the last good RPC menu on %s, records failure, and closes without a text listing',
+ async (failure) => {
+ const request = vi.fn<PiRpcRequester['request']>().mockResolvedValue(AVAILABLE_MODELS)
+ const { store, service, probe, record, fingerprint, close, runListing } = catalog(request)
+ await store.refresh(fingerprint, 'pi', () => probe(record.accountHome))
+ const good = store.get(fingerprint)
+ const detail = failure === 'empty' ? 'pi listed no models' : `pi RPC ${failure}`
+ if (failure === 'timeout') {
+ vi.useFakeTimers()
+ request.mockImplementation(() => new Promise(() => {}))
+ } else if (failure === 'empty') {
+ request.mockResolvedValue({ models: [] })
+ } else {
+ request.mockRejectedValue(new Error(detail))
+ }
+ const refresh = store.refresh(fingerprint, 'pi', () => probe(record.accountHome))
+ if (failure === 'timeout') {
+ await vi.advanceTimersByTimeAsync(probeRunner.AGENT_MODEL_CATALOG_PROBE_TIMEOUT_MS)
+ }
+ expect(await refresh).toBeNull()
+ expect(store.get(fingerprint)).toBe(good)
+ expect(store.failureDetail(fingerprint)).toBe(
+ failure === 'timeout' ? 'pi did not list models within 15000ms' : detail
+ )
+ expect(await service.prepareSaved({ agent: 'pi' })).toMatchObject({
+ origin: 'probe',
+ models: good!.models
+ })
+ expect(close).toHaveBeenCalledTimes(2)
+ expect(runListing).not.toHaveBeenCalled()
+ }
+ )
+
+ it('leaves a first failure unknown with an unpickable model, and lists after the failure TTL', async () => {
+ let at = 1_000
+ const request = vi.fn<PiRpcRequester['request']>().mockRejectedValue(new Error('RPC failed'))
+ const { store, service, fingerprint, runListing } = catalog(request, () => at)
+ await service.demand({ agent: 'pi' })
+ const failed = await service.read({ agent: 'pi' })
+ expect(failed).toEqual({ origin: 'unknown' })
+ expect(store.failure(fingerprint)).toMatchObject({ detail: 'RPC failed' })
+ expect(failed).not.toHaveProperty('unavailable')
+ const seed = structuredAgentSessionSeedCatalog('pi')
+ const state = applyStructuredAgentSessionModelCatalog(
+ createStructuredAgentSessionOptionState('pi', seed),
+ seed,
+ failed,
+ { newLaunch: true }
)
- const probe = createPiModelCatalogProbe({
- resolveEnvironment: async () => ({ PATH: '/usr/bin' }),
- resolveCommandSettings: () => ({
- agentCmdOverrides: { pi: `"${process.execPath}" run -- pi` }
- }),
- resolveCommand: () => '/opt/pi/bin/pi',
- probeVersion: async () => ({ version: '0.84.0' }),
- homePath: '/home/user',
- connect: () => ({
- request: async () => {
- throw new Error('pi exited before answering')
- },
- close: async () => undefined
- }),
- runListing
- })
- const success = await probe({ variable: 'PI_CODING_AGENT_DIR', path: '/homes/pi-a' })
- expect(success.models.map((model) => [model.id, model.efforts.length])).toEqual([
- ['anthropic/claude-sonnet-4', 5],
- ['openai/gpt-4o-mini', 0]
- ])
- expect(runListing).toHaveBeenCalledWith(
- expect.objectContaining({ args: ['run', '--', 'pi', '--list-models'] }),
- expect.objectContaining({ site: 'pi-model-catalog-probe' })
+ expect(structuredAgentSessionOptionSnapshot(state)).toContainEqual(
+ expect.objectContaining({
+ id: 'model',
+ label: 'Model',
+ settable: false,
+ disabledReason: 'available-after-session-start'
+ })
)
+ expect(structuredAgentSessionMessageSelection(state)).toEqual({})
+ await service.demand({ agent: 'pi' })
+ expect(request).toHaveBeenCalledTimes(2)
+ at += AGENT_MODEL_CATALOG_FAILURE_TTL_MS
+ expect(store.hasActiveFailure(fingerprint)).toBe(false)
+ request.mockResolvedValue(AVAILABLE_MODELS)
+ await service.demand({ agent: 'pi' })
+ expect(await service.read({ agent: 'pi' })).toMatchObject({
+ origin: 'probe',
+ models: [{ id: 'openai/gpt-6' }, { id: 'openai/gpt-4o-mini' }]
+ })
+ expect(store.failure(fingerprint)).toBeNull()
+ expect(runListing).not.toHaveBeenCalled()
})
- it('does not fall back when stopped during RPC teardown', async () => {
- const stop = new AbortController()
- const runListing = vi.fn()
- const probe = createPiModelCatalogProbe({
- resolveEnvironment: async () => ({ PATH: '/usr/bin' }),
- resolveCommand: () => '/opt/pi/bin/pi',
- probeVersion: async () => ({ version: '1.1.0' }),
- homePath: '/home/user',
- connect: () => ({
- request: async () => {
+ it.each(['during RPC', 'during teardown'] as const)(
+ 'records no failure when cancelled %s',
+ async (when) => {
+ const stop = new AbortController()
+ const pending = Promise.withResolvers<unknown>()
+ const started = Promise.withResolvers<void>()
+ const request = async () => {
+ started.resolve()
+ if (when === 'during teardown') {
throw new Error('RPC interrupted')
- },
- close: async () => stop.abort()
- }),
- runListing
+ }
+ return pending.promise
+ }
+ const { store, probe, record, fingerprint, close, runListing } = catalog(request)
+ if (when === 'during teardown') {
+ close.mockImplementation(async () => stop.abort())
+ }
+ const refresh = store.refresh(
+ fingerprint,
+ 'pi',
+ () => probe(record.accountHome, { signal: stop.signal }),
+ undefined,
+ stop.signal
+ )
+ await started.promise
+ if (when === 'during RPC') {
+ stop.abort()
+ }
+ expect(await refresh).toBeNull()
+ await vi.waitFor(() => expect(close).toHaveBeenCalledOnce())
+ expect(store.failure(fingerprint)).toBeNull()
+ expect(runListing).not.toHaveBeenCalled()
+ }
+ )
+
+ it('keeps live chat menus local during failed discovery and preserves historical last-good data', async () => {
+ const pending = Promise.withResolvers<unknown>()
+ const started = Promise.withResolvers<void>()
+ let attempts = 0
+ const { store, service, probe, record, fingerprint, close, runListing } = catalog(
+ async (command) => {
+ if (command === 'get_state') {
+ return {}
+ }
+ if (++attempts === 1) {
+ throw new Error('first RPC failed')
+ }
+ started.resolve()
+ return pending.promise
+ }
+ )
+ await service.demand({ agent: 'pi' })
+ expect(await service.read({ agent: 'pi' })).toEqual({ origin: 'unknown' })
+ const discovery = store.refresh(fingerprint, 'pi', () => probe(record.accountHome))
+ await started.promise
+ const live = await readPiRpcSessionOptions({
+ request: async (command) =>
+ command === 'get_available_models'
+ ? AVAILABLE_MODELS
+ : { sessionFile: '/s.jsonl', isStreaming: false, isCompacting: false }
})
- await expect(
- probe({ variable: 'PI_CODING_AGENT_DIR', path: '/homes/pi' }, { signal: stop.signal })
- ).rejects.toThrow()
+ service.recordLiveListing(record.sessionId, { models: live.models })
+ expect(live.models).toHaveLength(2)
+ expect(store.get(fingerprint)).toBeNull()
+ // Older persisted live data remains last-good until a discovery actually succeeds.
+ const historical = store.recordSuccess(
+ fingerprint,
+ 'pi',
+ {
+ models: [live.models[0]!],
+ origin: 'live-session'
+ },
+ 'live'
+ )
+ service.recordLiveListing(record.sessionId, { models: live.models })
+ expect(store.get(fingerprint)).toBe(historical)
+ pending.reject(new Error('RPC exited'))
+ await discovery
+ expect(store.failureDetail(fingerprint)).toBe('RPC exited')
+ expect(await service.read({ agent: 'pi' })).toMatchObject({
+ origin: 'live-session',
+ models: historical!.models
+ })
+ expect(close).toHaveBeenCalledTimes(2)
expect(runListing).not.toHaveBeenCalled()
})
+ it.each([
+ [
+ 'positive',
+ { model: { provider: 'openai', id: 'gpt-6' }, thinkingLevel: 'xhigh' },
+ 'openai/gpt-6',
+ 'xhigh'
+ ],
+ [
+ 'same model missing level',
+ { model: { provider: 'openai', id: 'gpt-6' } },
+ 'openai/gpt-6',
+ 'high'
+ ],
+ [
+ 'invalid unrelated fields and level',
+ { model: { provider: 'openai', id: 'gpt-6', reasoning: 'bad' }, thinkingLevel: 4 },
+ 'openai/gpt-6',
+ 'high'
+ ],
+ [
+ 'empty level',
+ { model: { provider: 'openai', id: 'gpt-6' }, thinkingLevel: '' },
+ 'openai/gpt-6',
+ 'high'
+ ],
+ [
+ 'different model missing level',
+ { model: { provider: 'openai', id: 'gpt-4o-mini' } },
+ 'openai/gpt-4o-mini',
+ undefined
+ ],
+ [
+ 'unlisted identity',
+ { model: { provider: 'custom', id: 'project-only' }, thinkingLevel: 'max' },
+ 'custom/project-only',
+ 'max'
+ ],
+ [
+ 'unsupported literal level',
+ { model: { provider: 'openai', id: 'gpt-6' }, thinkingLevel: 'future-level' },
+ 'openai/gpt-6',
+ 'future-level'
+ ],
+ [
+ 'nonreasoning level',
+ { model: { provider: 'openai', id: 'gpt-4o-mini' }, thinkingLevel: 'high' },
+ 'openai/gpt-4o-mini',
+ 'high'
+ ],
+ ['missing model', { thinkingLevel: 'low' }, undefined, undefined],
+ ['null model', { model: null, thinkingLevel: 'low' }, undefined, undefined],
+ ['empty identity', { model: { provider: '', id: 'gpt-6' } }, undefined, undefined],
+ ['invalid provider', { model: { provider: 'open/ai', id: 'gpt-6' } }, undefined, undefined],
+ ['malformed model', { model: 4 }, undefined, undefined],
+ ['malformed payload', null, undefined, undefined],
+ ['rejected state', new Error('state unavailable'), undefined, undefined]
+ ] as const)(
+ 'learns only positive discovery defaults: %s',
+ async (_name, state, model, effort) => {
+ const request = vi.fn<PiRpcRequester['request']>(async (command) => {
+ if (command === 'get_available_models') {
+ return AVAILABLE_MODELS
+ }
+ if (state instanceof Error) {
+ throw state
+ }
+ return state
+ })
+ const { store, probe, record, fingerprint, close } = catalog(request, () => 200)
+ store.recordSuccess(
+ fingerprint,
+ 'pi',
+ {
+ models: [{ id: 'openai/gpt-6', label: 'old', isDefault: false, efforts: [] }],
+ configured: { model: 'openai/gpt-6', effort: 'high' },
+ origin: 'probe'
+ },
+ 'discovery'
+ )
+ const previous = store.get(fingerprint)!.configured
+ const success = await store.refresh(fingerprint, 'pi', () => probe(record.accountHome))
+ expect(success?.configured).toEqual(
+ model ? { modelId: model, ...(effort ? { effort } : {}), at: 200 } : previous
+ )
+ if (!model) {
+ expect(success?.configured).toBe(previous)
+ }
+ expect(success?.models.map((row) => row.id)).toEqual(['openai/gpt-6', 'openai/gpt-4o-mini'])
+ expect(success?.models.filter((row) => row.isDefault).map((row) => row.id)).toEqual(
+ model === 'custom/project-only' ? [] : [model ?? 'openai/gpt-6']
+ )
+ expect(success?.models[1]!.efforts).toEqual([])
+ if (effort === 'future-level' || model === 'openai/gpt-4o-mini') {
+ expect(success?.models.find((row) => row.id === model)).not.toHaveProperty('defaultEffort')
+ }
+ expect(request.mock.calls.map(([command]) => command)).toEqual([
+ 'get_state',
+ 'get_available_models'
+ ])
+ expect(close).toHaveBeenCalledOnce()
+ }
+ )
+
+ it.each(['abort', 'retirement'] as const)(
+ 'does not continue optional state acquisition after %s',
+ async (ending) => {
+ const stop = new AbortController()
+ let current = true
+ const request = vi.fn<PiRpcRequester['request']>(async () => {
+ if (ending === 'abort') {
+ stop.abort()
+ }
+ current = false
+ throw new Error('state ended')
+ })
+ const { probe, record, close } = catalog(request)
+ await expect(
+ probe(record.accountHome, {
+ signal: stop.signal,
+ operation: { signal: stop.signal, current: () => current }
+ })
+ ).rejects.toThrow()
+ expect(request.mock.calls.map(([command]) => command)).toEqual(['get_state'])
+ expect(close).toHaveBeenCalledOnce()
+ }
+ )
+
+ it('publishes no partial defaults when inventory is malformed', async () => {
+ const { store, probe, record, fingerprint } = catalog(async (command) =>
+ command === 'get_state'
+ ? { model: { provider: 'custom', id: 'unlisted' }, thinkingLevel: 'high' }
+ : { models: 'invalid' }
+ )
+ expect(await store.refresh(fingerprint, 'pi', () => probe(record.accountHome))).toBeNull()
+ expect(store.get(fingerprint)).toBeNull()
+ expect(store.failure(fingerprint)).not.toBeNull()
+ })
+
it('lists nothing from a Pi that runs no structured chat', async () => {
const connect = vi.fn()
const probe = createPiModelCatalogProbe({
diff --git a/src/main/pi/rpc-model-catalog-probe.ts b/src/main/pi/rpc-model-catalog-probe.ts
index 53ddd3a6722..52bd804eed8 100644
--- a/src/main/pi/rpc-model-catalog-probe.ts
+++ b/src/main/pi/rpc-model-catalog-probe.ts
@@ -1,7 +1,7 @@
import { assertStructuredAgentResolutionCurrent } from '../native-chat/structured-agent-command-resolution'
import { homedir } from 'node:os'
+import { z } from 'zod'
import { requireLegacyAgentSessionAccountHome } from '../../shared/agent-session-account-home'
-import { parsePiModels } from '../../shared/commit-message-model-parsers'
import type { resolveCliCommand } from '../../shared/node-cli-command-resolution'
import type { AgentCliVersionReader } from '../agent-cli-version-probe'
import { JsonlRpcAgentConnection } from '../jsonl-rpc/agent-connection'
@@ -9,18 +9,21 @@ import type {
AgentModelCatalogProbe,
AgentModelCatalogSuccess
} from '../native-chat/agent-model-catalog/agent-model-catalog-store'
-import {
- runAgentModelCatalogListing,
- runAgentModelCatalogSession
-} from '../native-chat/agent-model-catalog/agent-model-catalog-probe-runner'
+import { runAgentModelCatalogSession } from '../native-chat/agent-model-catalog/agent-model-catalog-probe-runner'
import type { StructuredAgentCommandSettings } from '../native-chat/structured-agent-command-resolution'
import type { ProviderProcessLaunch } from '../provider-process/provider-process-launch'
import { buildPiRpcLaunch, piRpcSessionlessEnvironment } from './rpc-launch'
import { checkPiRpcVersion, resolvePiRpcInvocation } from './rpc-launch-resolution'
-import { readPiRpcModelOptions, type PiRpcRequester } from './rpc-options'
+import { piModelOptionId, readPiRpcModelOptions, type PiRpcRequester } from './rpc-options'
+import { piRpcModelSchema } from './rpc-protocol'
type PiModelCatalogConnection = PiRpcRequester & { close(): Promise<unknown> }
+const discoveryStateSchema = z.object({
+ model: piRpcModelSchema.pick({ provider: true, id: true }).nullish(),
+ thinkingLevel: z.unknown().optional()
+})
+
export type PiModelCatalogProbeDeps = {
resolveEnvironment: () => Promise<NodeJS.ProcessEnv>
/** Refresh once on a missing Pi binary; absent disables retry. */
@@ -29,25 +32,12 @@ export type PiModelCatalogProbeDeps = {
resolveCommand?: typeof resolveCliCommand
probeVersion?: AgentCliVersionReader
connect?: (launch: ProviderProcessLaunch) => PiModelCatalogConnection
- runListing?: typeof runAgentModelCatalogListing
homePath?: string
}
-/** The text listing has no per-model thinking map; offer the levels every reasoning model keeps. */
-export function piModelCatalogFromListing(stdout: string): AgentModelCatalogSuccess['models'] {
- return parsePiModels(stdout).map((model) => ({
- id: model.id,
- label: model.label,
- isDefault: false,
- efforts: model.thinkingLevels
- ? ['off', 'minimal', 'low', 'medium', 'high'].map((level) => ({ value: level, label: level }))
- : []
- }))
-}
-
function connectPiModelCatalog(launch: ProviderProcessLaunch): PiModelCatalogConnection {
const connection: JsonlRpcAgentConnection = new JsonlRpcAgentConnection(launch, {
- // Nobody can answer an extension's dialog during discovery; cancelling lets Pi continue.
+ // Cancel dialogs after binding; Pi cannot read replies to session_start dialogs before binding.
onRecord: (frame) => {
if (
frame.type === 'extension_ui_request' &&
@@ -89,31 +79,37 @@ export function createPiModelCatalogProbe(deps: PiModelCatalogProbeDeps): AgentM
...piRpcSessionlessEnvironment(env),
fullAccess: true
})
- let models: AgentModelCatalogSuccess['models']
- try {
- models = await runAgentModelCatalogSession(
- () =>
- (deps.connect ?? connectPiModelCatalog)({
- ...launch,
- args: [...launch.args, '--no-session']
- }),
- readPiRpcModelOptions,
- { label: 'pi', signal: options?.signal }
- )
- } catch (error) {
- if (options?.signal?.aborted) {
- throw error
- }
- // Pi reads RPC input only after extensions start, so a startup dialog can block it; the listing exits first.
- const stdout = await (deps.runListing ?? runAgentModelCatalogListing)(
- { ...launch, args: [...prefixArgs, '--list-models'] },
- { site: 'pi-model-catalog-probe', signal: options?.signal }
- )
- models = piModelCatalogFromListing(stdout)
- }
- if (models.length === 0) {
+ const success = await runAgentModelCatalogSession(
+ () =>
+ (deps.connect ?? connectPiModelCatalog)({
+ ...launch,
+ args: [...launch.args, '--no-session']
+ }),
+ async (connection): Promise<AgentModelCatalogSuccess> => {
+ let configured: AgentModelCatalogSuccess['configured']
+ try {
+ const state = discoveryStateSchema.safeParse(await connection.request('get_state'))
+ if (state.success && state.data.model) {
+ configured = {
+ model: piModelOptionId(state.data.model.provider, state.data.model.id),
+ ...(typeof state.data.thinkingLevel === 'string' && state.data.thinkingLevel
+ ? { effort: state.data.thinkingLevel }
+ : {})
+ }
+ }
+ } catch {
+ // Optional default facts cannot prevent a usable inventory from being discovered.
+ }
+ assertStructuredAgentResolutionCurrent(operation)
+ options?.signal?.throwIfAborted()
+ const models = await readPiRpcModelOptions(connection)
+ return { models, origin: 'probe', ...(configured ? { configured } : {}) }
+ },
+ { label: 'pi', signal: options?.signal }
+ )
+ if (success.models.length === 0) {
throw new Error('pi listed no models')
}
- return { models, origin: 'probe' }
+ return success
}
}
diff --git a/src/main/runtime/structured-agent-new-chat-effort-at-rest.test.ts b/src/main/runtime/structured-agent-new-chat-effort-at-rest.test.ts
index 3a68caad212..3674e00965f 100644
--- a/src/main/runtime/structured-agent-new-chat-effort-at-rest.test.ts
+++ b/src/main/runtime/structured-agent-new-chat-effort-at-rest.test.ts
@@ -33,7 +33,6 @@ import type { StructuredAgentSessionMutationContext } from '../native-chat/agent
import { readStructuredAgentSessionOptions } from '../native-chat/agent-session-wire/structured-agent-session-options-read'
import { StructuredAgentRegistry } from '../native-chat/agent-session-wire/structured-agent-registry'
import { PI_RPC_AGENT } from '../pi/rpc-agent-definition'
-import { piModelCatalogFromListing } from '../pi/rpc-model-catalog-probe'
// A chat whose agent offers permission modes also names its own, which a new chat leaves at Ask.
const modeOf = (agent: string) => (agentChatPermissionModes(agent) ? { permissionMode: 'ask' } : {})
@@ -108,13 +107,14 @@ const GROK_LISTING = grokModelCatalogFromState({
]
})
-const PI_LISTING = piModelCatalogFromListing(
- [
- 'provider model context max-out thinking images',
- 'anthropic claude-sonnet-4 200K 64K yes yes',
- 'openai gpt-6 400K 128K yes yes'
- ].join('\n')
-)
+const PI_LISTING: AgentSessionModelOption[] = [
+ { id: 'anthropic/claude-sonnet-4', label: 'Claude Sonnet 4' },
+ { id: 'openai/gpt-6', label: 'GPT-6' }
+].map((model) => ({
+ ...model,
+ isDefault: false,
+ efforts: ['off', 'minimal', 'low', 'medium', 'high'].map((value) => ({ value, label: value }))
+}))
type Account = {
models: AgentSessionModelOption[]
diff --git a/src/main/runtime/structured-agent-session-pi-startup.test.ts b/src/main/runtime/structured-agent-session-pi-startup.test.ts
index 5253c980206..cc3539ea677 100644
--- a/src/main/runtime/structured-agent-session-pi-startup.test.ts
+++ b/src/main/runtime/structured-agent-session-pi-startup.test.ts
@@ -25,6 +25,7 @@ import {
structuredAgentSessionMessageSelection
} from '../../shared/structured-agent-session-option-view'
import { readPiRpcModelOptions } from '../pi/rpc-options'
+import type { AgentModelCatalogLiveListing } from '../native-chat/agent-model-catalog/agent-model-catalog-entry'
import { piScriptedChild } from '../pi/pi-scripted-child.test-fixture'
import { peekOpenedAgentSessionRecordStore } from './agent-session-record-store-slot'
import {
@@ -52,6 +53,7 @@ async function setup(
catalog?: AgentSessionModelOption[]
create?: true
firstMessage?: string
+ initialCatalogListing?: AgentModelCatalogLiveListing
} = {}
) {
directory = await mkdtemp(join(tmpdir(), 'orca-pi-startup-'))
@@ -76,6 +78,16 @@ async function setup(
: {}),
...script.deps
})
+ const publication = host.deps.modelCatalog
+ ? vi.spyOn(host.deps.modelCatalog, 'recordLiveListing')
+ : null
+ if (options.initialCatalogListing) {
+ const open = host.deps.adapter.open
+ vi.spyOn(host.deps.adapter, 'open').mockImplementation(async (input) => ({
+ ...(await open(input)),
+ catalogListing: options.initialCatalogListing
+ }))
+ }
if (options.catalog) {
vi.spyOn(host.deps.modelCatalog!, 'read').mockResolvedValue({
origin: 'probe',
@@ -136,7 +148,7 @@ async function setup(
if (!attached.ok) {
throw new Error(JSON.stringify(attached.refusal))
}
- return { script, host, caller, params: preparedParams, fence: attached.value.fence }
+ return { script, host, caller, publication, params: preparedParams, fence: attached.value.fence }
}
function envelope(method: string, fields: Record<string, unknown>) {
@@ -545,3 +557,72 @@ it('displays native gapped support and dispatches raw medium for seed and existi
stateInputs(h.script).filter(({ command }) => command === 'set_thinking_level')
).toHaveLength(1)
})
+
+it.each([false, true])(
+ 'keeps project-only native menus editable without initial/delayed account promotion (initial=%s)',
+ async (initial) => {
+ const native = {
+ provider: 'project',
+ id: 'only',
+ reasoning: true,
+ thinkingLevelMap: { medium: null, high: null, xhigh: 'xhigh' }
+ }
+ const models = await readPiRpcModelOptions({ request: async () => ({ models: [native] }) })
+ const home = [
+ {
+ id: 'home/model',
+ label: 'Home',
+ isDefault: false,
+ efforts: [{ value: 'high', label: 'High' }]
+ }
+ ]
+ const h = await setup({
+ seed: { model: 'project/only', effort: 'medium' },
+ catalog: home,
+ native: { model: native, appliedEfforts: { medium: 'xhigh' } },
+ ...(initial
+ ? {
+ initialCatalogListing: {
+ models,
+ configuredDefault: {
+ modelId: 'project/only',
+ effort: 'xhigh'
+ }
+ }
+ }
+ : {})
+ })
+ h.script.releaseHandshake()
+ await vi.waitFor(() =>
+ expect(h.publication!.mock.calls.length).toBeGreaterThanOrEqual(initial ? 2 : 1)
+ )
+ expect(h.host.deps.modelCatalog!.peek(HOST_TEST_SESSION)).toBeNull()
+ const answer = await h.host.readOptions(HOST_TEST_SESSION)
+ expect(answer.models).toEqual(models)
+ expect(displayed(answer)).toMatchObject({ settable: true, kind: { currentValue: 'xhigh' } })
+ const seed = structuredAgentSessionSeedCatalog('pi')
+ const state = applyStructuredAgentSessionOptions(
+ createStructuredAgentSessionOptionState('pi', seed),
+ seed,
+ answer
+ )
+ expect(structuredAgentSessionMessageSelection(state)).toEqual({
+ model: 'project/only',
+ effort: 'medium'
+ })
+ const requests = h.script
+ .wire()
+ .filter((command) => command === 'get_state' || command === 'get_available_models')
+ await h.host.readOptions(HOST_TEST_SESSION)
+ await h.host.readOptions(HOST_TEST_SESSION)
+ expect(
+ h.script
+ .wire()
+ .filter((command) => command === 'get_state' || command === 'get_available_models')
+ ).toEqual(requests)
+ expect(peekOpenedAgentSessionRecordStore()!.getRecord(HOST_TEST_SESSION)?.options).toEqual({
+ model: 'project/only',
+ effort: 'medium'
+ })
+ }
+)