diff --git a/src/main/acp/__fixtures__/session-new-auth-required.json b/src/main/acp/__fixtures__/session-new-auth-required.json new file mode 100644 index 000000000000..a3d956b0da64 --- /dev/null +++ b/src/main/acp/__fixtures__/session-new-auth-required.json @@ -0,0 +1,14 @@ +{ + "agent": "grok", + "version": "1.0.70", + "method": "session/new", + "reply": { + "jsonrpc": "2.0", + "id": 2, + "error": { + "code": -32000, + "message": "Authentication required", + "data": "no auth method id provided" + } + } +} diff --git a/src/main/acp/acp-model-catalog-probe.test.ts b/src/main/acp/acp-model-catalog-probe.test.ts index e0c357e57cd4..af24de5452b9 100644 --- a/src/main/acp/acp-model-catalog-probe.test.ts +++ b/src/main/acp/acp-model-catalog-probe.test.ts @@ -12,6 +12,7 @@ import { AcpScriptedAgent } from './acp-scripted-agent.test-support' import { AgentModelCatalogListingStoppedError } from '../native-chat/agent-model-catalog/agent-model-catalog-failures' import type { ProviderProcessLaunch } from '../provider-process/provider-process-launch' import { openCodeAcpAccountBinding } from '../opencode/opencode-structured-account-home' +import authRequired from './__fixtures__/session-new-auth-required.json' const PREFIX = ['run', '--', 'agent'] const launcherSettings = (agent: string) => ({ @@ -167,6 +168,20 @@ function scriptedOpenCode2(canDelete: boolean): { } describe('ACP model catalog probes', () => { + it('projects an agent-authored auth-required session/new error, retaining unknown for other failures', async () => { + const { agent, deps } = scriptedOpenCode2(true) + const { code, message, data } = authRequired.reply.error + agent.on('session/new', (frame) => agent.fail(frame, code, message, data)) + const list = () => + createAcpModelCatalogProbe(OPENCODE, deps)( + { kind: 'opencode', locator: { kind: 'unmanaged' } }, + { workspacePath: '/work/project' } + ) + await expect(list()).rejects.toMatchObject({ unavailable: { reason: 'notSignedIn' } }) + agent.on('session/new', (frame) => agent.fail(frame, -32603, 'Internal error')) + await expect(list()).rejects.toMatchObject({ code: -32603 }) + }) + it('lists Grok from initialize alone, under its pinned account, and closes the child', async () => { const { agent, deps, launches, closed } = scriptedGrok({ modelState: MODEL_STATE }) deps.resolveCommandSettings = () => launcherSettings('grok') diff --git a/src/main/acp/acp-model-catalog-probe.ts b/src/main/acp/acp-model-catalog-probe.ts index 361920e7134e..9786870d84fc 100644 --- a/src/main/acp/acp-model-catalog-probe.ts +++ b/src/main/acp/acp-model-catalog-probe.ts @@ -5,10 +5,7 @@ import { import { homedir } from 'node:os' import { join } from 'node:path' import type { AgentCliVersionReader } from '../agent-cli-version-probe' -import type { - AgentModelCatalogProbe, - AgentModelCatalogSuccess -} from '../native-chat/agent-model-catalog/agent-model-catalog-store' +import type { AgentModelCatalogProbe } from '../native-chat/agent-model-catalog/agent-model-catalog-store' import { runAgentModelCatalogListing, runAgentModelCatalogSession @@ -32,6 +29,8 @@ import { } from './acp-structured-launch-resolution' import { AcpStructuredOptions } from './acp-structured-options' import type { InitializeResponse } from './generated/acp-protocol.generated' +import { AcpAgentError } from './acp-errors' +import { AgentModelCatalogUnavailableError } from '../native-chat/agent-model-catalog/agent-model-catalog-unavailable' /** Session-free capability boundary: no session methods are exposed. */ export type AcpModelCatalogConnection = { @@ -60,7 +59,7 @@ export function createAcpModelCatalogProbe( spec: AcpLaunchSpec, deps: AcpModelCatalogProbeDeps ): AgentModelCatalogProbe { - return async (accountHome, options): Promise => { + const probe: AgentModelCatalogProbe = async (accountHome, options) => { const operation = options?.operation ?? { signal: options?.signal } assertStructuredAgentResolutionCurrent(operation) const discovery = spec.modelDiscovery @@ -156,6 +155,16 @@ export function createAcpModelCatalogProbe( } return { models, origin: 'probe' } } + return async (home, options) => { + try { + return await probe(home, options) + } catch (error) { + if (error instanceof AcpAgentError && error.code === -32000) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } + throw error + } + } } const folderlessListing = (spec: AcpLaunchSpec) => diff --git a/src/main/claude/__fixtures__/catalog-signin.json b/src/main/claude/__fixtures__/catalog-signin.json new file mode 100644 index 000000000000..2f2950710a0b --- /dev/null +++ b/src/main/claude/__fixtures__/catalog-signin.json @@ -0,0 +1,176 @@ +{ + "version": "2.1.310", + "accounts": { + "signedOut": { + "tokenSource": "none", + "apiProvider": "firstParty" + }, + "apiKey": { + "tokenSource": "none", + "apiKeySource": "ANTHROPIC_API_KEY", + "apiProvider": "firstParty" + }, + "bearer": { + "tokenSource": "ANTHROPIC_AUTH_TOKEN", + "apiProvider": "firstParty" + }, + "bedrock": { + "apiProvider": "bedrock" + } + }, + "models": { + "models": [ + { + "value": "default", + "resolvedModel": "claude-opus-5-5", + "displayName": "Default (recommended)", + "description": "Use the default model (currently Opus 5.5) · $4/$20 per Mtok", + "supportsEffort": true, + "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"], + "supportsAdaptiveThinking": true, + "supportsFastMode": true, + "supportsAutoMode": true, + "supportsThinkingOff": false + }, + { + "value": "opus", + "resolvedModel": "claude-opus-5-5", + "displayName": "Opus", + "description": "Opus 5.5 · Best for everyday, complex tasks · $4/$20 per Mtok", + "supportsEffort": true, + "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"], + "supportsAdaptiveThinking": true, + "supportsFastMode": true, + "supportsAutoMode": true, + "supportsThinkingOff": false + }, + { + "value": "fable", + "resolvedModel": "claude-fable-5-1", + "displayName": "Fable", + "description": "Fable 5.1 · Most capable for your hardest and longest-running tasks · $10/$50 per Mtok", + "supportsEffort": true, + "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"], + "supportsAdaptiveThinking": true, + "supportsAutoMode": true, + "supportsThinkingOff": false + }, + { + "value": "sonnet", + "resolvedModel": "claude-sonnet-5-5", + "displayName": "Sonnet", + "description": "Sonnet 5.5 · Efficient for routine tasks · $2/$10 per Mtok", + "supportsEffort": true, + "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"], + "supportsAdaptiveThinking": true, + "supportsAutoMode": true, + "supportsThinkingOff": false + }, + { + "value": "haiku", + "resolvedModel": "claude-haiku-5-5", + "displayName": "Haiku", + "description": "Haiku 5.5 · Fastest for quick answers · $0.10/$0.50 per Mtok ($0.50/$2.50 for prompts over 100k)", + "supportsEffort": true, + "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"], + "supportsAdaptiveThinking": true, + "supportsAutoMode": true, + "supportsThinkingOff": false + } + ] + }, + "settings": { + "effective": { + "disableAllHooks": true + }, + "sources": [ + { + "source": "flagSettings", + "settings": { + "disableAllHooks": true + } + } + ], + "applied": { + "model": "claude-opus-5-5", + "effort": "medium", + "advisor": null, + "ultracode": false, + "ultracodeRequested": false, + "ultracodeAvailable": true + } + }, + "endpointSettings": { + "effective": { + "env": { + "ANTHROPIC_BASE_URL": "https://gateway.example.invalid" + }, + "disableAllHooks": true + }, + "sources": [ + { + "source": "userSettings", + "settings": { + "env": { + "ANTHROPIC_BASE_URL": "https://gateway.example.invalid" + } + } + }, + { + "source": "projectSettings", + "settings": { + "env": { + "ANTHROPIC_BASE_URL": "https://gateway.example.invalid" + } + } + }, + { + "source": "flagSettings", + "settings": { + "disableAllHooks": true + } + } + ], + "applied": { + "model": "claude-opus-5-5", + "effort": "medium", + "advisor": null, + "ultracode": false, + "ultracodeRequested": false, + "ultracodeAvailable": true + } + }, + "commandFrames": { + "error": { + "type": "control_response", + "response": { + "subtype": "error", + "request_id": "orca-command-discovery", + "error": "Unsupported control request subtype: orca_unknown_subtype" + } + }, + "initialize": { + "type": "control_response", + "response": { + "subtype": "success", + "request_id": "orca-command-discovery", + "response": { + "commands": [ + { + "name": "deep-research", + "description": "Deep research harness — fan-out web searches, fetch sources, adversarially verify claims, synthesize a cited report. (dynamic workflow)", + "argumentHint": "", + "builtin": true + }, + { + "name": "design", + "description": "Grant or revoke Claude agent access to your Design projects", + "argumentHint": "consent | revoke", + "builtin": true + } + ] + } + } + } + } +} diff --git a/src/main/claude/claude-command-catalog-probe.ts b/src/main/claude/claude-command-catalog-probe.ts index 98bbdd892ec5..c13a4459e270 100644 --- a/src/main/claude/claude-command-catalog-probe.ts +++ b/src/main/claude/claude-command-catalog-probe.ts @@ -1,7 +1,7 @@ import type { AgentSessionSlashCommand } from '../../shared/agent-session/agent-session-wire' -import { assertJsonTextStructureWithinLimits } from '../../shared/json-text-structure-limit' import { runClaudeListing, type ClaudeModelCatalogProbeDeps } from './claude-model-catalog-probe' import { ClaudeSlashCommandCatalog } from './claude-slash-command-catalog' +import { readClaudeControlResponses } from './claude-control-responses' const REQUEST_ID = 'orca-command-discovery' @@ -12,8 +12,6 @@ const CLAUDE_COMMAND_LIST_STDIN = `${JSON.stringify({ request: { subtype: 'initialize' } })}\n` -const INITIALIZE_JSON_LIMITS = { structuralTokens: 64 * 1024, nestingDepth: 16 } as const - export type ClaudeCommandCatalogProbe = (request: { cwd: string accountHomePath: string @@ -42,31 +40,8 @@ export function createClaudeCommandCatalogProbe( } function readInitializeCommands(stdout: string): AgentSessionSlashCommand[] | undefined { - for (const rawLine of stdout.split(/\r?\n/)) { - const line = rawLine.trim() - if (!line.startsWith('{') || !line.includes(REQUEST_ID)) { - continue - } - let parsed: unknown - try { - assertJsonTextStructureWithinLimits(line, INITIALIZE_JSON_LIMITS) - parsed = JSON.parse(line) - } catch { - continue - } - const response = - parsed && typeof parsed === 'object' && 'response' in parsed ? parsed.response : undefined - if ( - response && - typeof response === 'object' && - 'subtype' in response && - response.subtype === 'success' && - 'request_id' in response && - response.request_id === REQUEST_ID && - 'response' in response - ) { - return new ClaudeSlashCommandCatalog(undefined, response.response).commands - } - } - return undefined + const initialization = readClaudeControlResponses(stdout).get(REQUEST_ID) + return initialization === undefined + ? undefined + : new ClaudeSlashCommandCatalog(undefined, initialization).commands } diff --git a/src/main/claude/claude-control-responses.ts b/src/main/claude/claude-control-responses.ts new file mode 100644 index 000000000000..498fde83c309 --- /dev/null +++ b/src/main/claude/claude-control-responses.ts @@ -0,0 +1,30 @@ +import { assertJsonTextStructureWithinLimits } from '../../shared/json-text-structure-limit' + +const CONTROL_JSON_LIMITS = { structuralTokens: 64 * 1024, nestingDepth: 16 } as const + +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null && !Array.isArray(value) +} + +export function readClaudeControlResponses(stdout: string): Map { + const responses = new Map() + for (const line of stdout.split(/\r?\n/)) { + if (!line.trimStart().startsWith('{') || !line.includes('request_id')) { + continue + } + try { + assertJsonTextStructureWithinLimits(line, CONTROL_JSON_LIMITS) + const frame: unknown = JSON.parse(line) + if (!isRecord(frame) || !isRecord(frame.response)) { + continue + } + const response = frame.response + if (response.subtype === 'success' && typeof response.request_id === 'string') { + responses.set(response.request_id, response.response) + } + } catch { + // Diagnostics and incomplete frames establish no control facts. + } + } + return responses +} diff --git a/src/main/claude/claude-model-catalog-probe.test.ts b/src/main/claude/claude-model-catalog-probe.test.ts index ab68544c4f4b..3fe97c784b8d 100644 --- a/src/main/claude/claude-model-catalog-probe.test.ts +++ b/src/main/claude/claude-model-catalog-probe.test.ts @@ -6,6 +6,8 @@ import { join } from 'node:path' import { describe, expect, it, vi } from 'vitest' import { PROVIDER_STDIN_END_GRACE_MS } from '../provider-process/provider-process-supervisor' import { createClaudeModelCatalogProbe } from './claude-model-catalog-probe' +import { createClaudeCommandCatalogProbe } from './claude-command-catalog-probe' +import signinCapture from './__fixtures__/catalog-signin.json' import { discoveredModel } from '../../shared/agent-session/options/structured-agent-session-discovered-model' import { resolveClaudeStructuredInvocation } from './structured/launch/claude-structured-launch-resolution' import { resolveStructuredAgentCommand } from '../native-chat/structured-agent-command-resolution' @@ -328,7 +330,7 @@ it.each(['high', 'low'])( ?.trim() .split('\n') .map((line) => JSON.parse(line)) - ).toHaveLength(2) + ).toHaveLength(3) } ) @@ -345,6 +347,7 @@ it('qualifies only a correlated valid settings observation', async () => { runListing: async () => LISTED + JSON.stringify({ + type: 'control_response', response: { subtype: 'success', request_id: requestId, @@ -355,3 +358,92 @@ it('qualifies only a correlated valid settings observation', async () => { expect((await probe(HOME('/account'))).complete).toBe(complete) } }) + +function controlReply(requestId: string, response: unknown): string { + return `${JSON.stringify({ type: 'control_response', response: { subtype: 'success', request_id: requestId, response } })}\n` +} + +it.each(['system', 'managed'] as const)( + 'keeps recorded models with the signed-out %s account advisory', + async (account) => { + const runListing = vi.fn( + async () => + controlReply('orca-model-discovery', signinCapture.models) + + controlReply('orca-model-settings', signinCapture.settings) + + controlReply('orca-signin-discovery', { account: signinCapture.accounts.signedOut }) + ) + const probe = createClaudeModelCatalogProbe({ + ...probeDeps(), + resolveAuthPolicy: () => ({ account }), + runListing + }) + const success = await probe(HOME('/account')) + expect(success).toMatchObject({ unavailable: { reason: 'notSignedIn', account } }) + expect(success.models.length).toBeGreaterThan(0) + const [launch, options] = runListing.mock.calls[0] ?? [] + expect( + launch?.stdin + ?.trim() + .split('\n') + .map((line: string) => JSON.parse(line).request.subtype) + ).toEqual(['list_models', 'get_settings', 'initialize']) + expect( + options?.partialOutput?.ready(controlReply('orca-model-discovery', signinCapture.models)) + ).toBe(false) + expect( + options?.partialOutput?.ready( + controlReply('orca-model-discovery', signinCapture.models) + + controlReply('orca-model-settings', signinCapture.settings) + ) + ).toBe(true) + } +) + +it('requires definitive first-party account facts, preserving inventory when auth is unknown', async () => { + const cases = [ + { account: signinCapture.accounts.apiKey }, + { account: signinCapture.accounts.bearer }, + { account: signinCapture.accounts.bedrock }, + { account: { tokenSource: 'none', email: 'user@example.test' } }, + { account: { tokenSource: 'none', subscriptionType: 'max' } }, + { account: {} }, + { account: undefined }, + { account: signinCapture.accounts.signedOut, settings: signinCapture.endpointSettings }, + { account: signinCapture.accounts.signedOut, endpoint: 'https://gateway.example.test' }, + { account: signinCapture.accounts.signedOut, settings: {} } + ] + for (const { account, settings = signinCapture.settings, endpoint } of cases) { + const probe = createClaudeModelCatalogProbe({ + ...probeDeps(), + resolveEnv: (): Record => (endpoint ? { ANTHROPIC_BASE_URL: endpoint } : {}), + runListing: async () => + controlReply('orca-model-discovery', signinCapture.models) + + controlReply('orca-model-settings', settings) + + (account === undefined ? '' : controlReply('orca-signin-discovery', { account })) + }) + const success = await probe(HOME('/account')) + expect(success.unavailable).toBeUndefined() + expect(success.models.length).toBeGreaterThan(0) + } +}) + +it('reads slash commands as main did: past error, diagnostic and oversized lines, with or without type', async () => { + const { error, initialize } = signinCapture.commandFrames + const nested = (depth: number): unknown => (depth === 0 ? {} : { response: nested(depth - 1) }) + const oversized = JSON.stringify({ + ...initialize, + response: { ...initialize.response, response: nested(20) } + }) + const list = (lines: string[]) => + createClaudeCommandCatalogProbe({ + ...probeDeps(), + runListing: async () => `${lines.join('\n')}\n` + })({ cwd: '/project', accountHomePath: '/homes/account-a' }) + const rejected = [JSON.stringify(error), 'orca-command-discovery request_id not ready', oversized] + await expect(list(rejected)).rejects.toThrow('claude listed no commands') + const { type: _type, ...untyped } = initialize + for (const frame of [initialize, untyped]) { + const commands = await list([...rejected, JSON.stringify(frame)]) + expect(commands.map((command) => command.name)).toEqual(['deep-research', 'design']) + } +}) diff --git a/src/main/claude/claude-model-catalog-probe.ts b/src/main/claude/claude-model-catalog-probe.ts index b53165a45ac0..eda5b39ccec6 100644 --- a/src/main/claude/claude-model-catalog-probe.ts +++ b/src/main/claude/claude-model-catalog-probe.ts @@ -12,14 +12,14 @@ import { runClaudeBackgroundAttempts } from '../../shared/claude-background-run- import { isCompleteClaudeModelListing, listedModels, - record, - type ListedModel + record } from './structured/launch/claude-structured-model-catalog' import { claudeFastModeSupport, wireClaudeModels } from './structured/launch/claude-structured-catalog-listing' import { readClaudeSettingsApplied } from './structured/launch/claude-structured-session-options' +import { readClaudeControlResponses } from './claude-control-responses' import { claudeStructuredLaunchArgs } from './structured/launch/claude-structured-launch-args' import { AgentModelCatalogUnavailableError } from '../native-chat/agent-model-catalog/agent-model-catalog-unavailable' import { claudeConfigDirEnvPatch } from './claude-config-dir-pin' @@ -142,7 +142,7 @@ export function createClaudeModelCatalogProbe( } const sources = await resolveClaudeChildEnvSources(launchDeps) assertStructuredAgentResolutionCurrent(operation) - const { command, prefixArgs, env } = await resolveClaudeStructuredInvocation( + const { command, prefixArgs, env, account } = await resolveClaudeStructuredInvocation( launchDeps, (base) => ({ ...base, @@ -174,9 +174,23 @@ export function createClaudeModelCatalogProbe( command, args: [...prefixArgs, ...argsFor(strictMcp)], ...(cwd ? { cwd } : {}), - stdin: `${CLAUDE_MODEL_LIST_STDIN}${JSON.stringify({ type: 'control_request', request_id: 'orca-model-settings', request: { subtype: 'get_settings' } })}\n` + stdin: `${CLAUDE_MODEL_LIST_STDIN}${JSON.stringify({ type: 'control_request', request_id: 'orca-model-settings', request: { subtype: 'get_settings' } })}\n${JSON.stringify({ type: 'control_request', request_id: 'orca-signin-discovery', request: { subtype: 'initialize' } })}\n` }, - { site: 'claude-model-catalog-probe', inheritedEnv: env, signal } + { + site: 'claude-model-catalog-probe', + inheritedEnv: env, + signal, + partialOutput: { + timeoutMs: 2_000, + ready: (stdout) => { + const replies = readClaudeControlResponses(stdout) + return ( + listedModels(replies.get('orca-model-discovery')).length > 0 && + replies.has('orca-model-settings') + ) + } + } + } ).catch((error: unknown) => { if (error instanceof AgentModelCatalogProbeError) { return error @@ -196,37 +210,18 @@ export function createClaudeModelCatalogProbe( throw outcome } const stdout = outcome - let models: ListedModel[] = [] - let complete = false - let configured: AgentModelCatalogSuccess['configured'] - for (const line of stdout.split(/\r?\n/)) { - try { - const response = record(record(JSON.parse(line))?.response) - if (response?.subtype !== 'success') { - continue - } - const body = response.response - const listed = response.request_id === 'orca-model-discovery' ? listedModels(body) : [] - if (listed.length > 0) { - models = listed - complete = isCompleteClaudeModelListing(body) - } - if (response.request_id === 'orca-model-settings') { - const applied = record(record(body)?.applied) - if ( - applied && - !Array.isArray(applied) && - ['model', 'effort'].every( - (key) => applied[key] == null || typeof applied[key] === 'string' - ) - ) { - configured = readClaudeSettingsApplied(body) ?? undefined - } - } - } catch { - /* Non-JSON diagnostic output carries no catalog facts. */ - } - } + const replies = readClaudeControlResponses(stdout) + const listing = replies.get('orca-model-discovery') + const models = listedModels(listing) + const settings = replies.get('orca-model-settings') + const applied = record(record(settings)?.applied) + const configured = + applied && + ['model', 'effort'].every( + (key) => applied[key] == null || typeof applied[key] === 'string' + ) + ? (readClaudeSettingsApplied(settings) ?? undefined) + : undefined if (models.length === 0) { throw new Error('claude listed no models') } @@ -234,9 +229,12 @@ export function createClaudeModelCatalogProbe( return { models: wireClaudeModels(models), origin: 'probe', - complete: complete && configured !== undefined, + complete: isCompleteClaudeModelListing(listing) && configured !== undefined, ...(fastModeSupport ? { fastModeSupport } : {}), - ...(configured ? { configured } : {}) + ...(configured ? { configured } : {}), + ...(claudeCatalogSignedOut(replies.get('orca-signin-discovery'), settings, env) + ? { unavailable: { reason: 'notSignedIn', account } } + : {}) } } } @@ -257,3 +255,34 @@ export function createClaudeModelCatalogProbe( { resolve, resolveLegacy: (home, operation) => resolve(home, undefined, 'recover', operation) } ) } + +function claudeCatalogSignedOut( + initialization: unknown, + settings: unknown, + env: Readonly> +): boolean { + const account = record(record(initialization)?.account) + const effective = record(record(settings)?.effective) + if ( + !account || + !effective || + account.tokenSource !== 'none' || + (account.apiProvider !== undefined && account.apiProvider !== 'firstParty') || + (account.apiKeySource && account.apiKeySource !== 'none') || + account.email || + account.subscriptionType + ) { + return false + } + const configuredEnv = record(effective.env) + const endpoint = configuredEnv?.ANTHROPIC_BASE_URL ?? env.ANTHROPIC_BASE_URL + if (typeof endpoint === 'string' && endpoint.trim()) { + try { + const url = new URL(endpoint) + return url.protocol === 'https:' && url.hostname === 'api.anthropic.com' + } catch { + return false + } + } + return true +} diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts index 79b78ee3e9e6..11c34b40566b 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.test.ts @@ -37,6 +37,41 @@ function isAlive(pid: number): boolean { const NODE_ENV = { ELECTRON_RUN_AS_NODE: '1' } describe('the shared catalog probe runner', () => { + it('returns required replies and stops the child when an optional response stalls', async () => { + const file = pidFile() + const stdout = await runAgentModelCatalogListing( + { + ...node( + `require('fs').writeFileSync(${JSON.stringify(file)}, String(process.pid));process.stdout.write('models\\nsettings\\n');setInterval(()=>{},1000)` + ), + env: NODE_ENV + }, + { + site: 'test-partial-listing', + timeoutMs: 5_000, + partialOutput: { ready: (output) => output.includes('settings\n'), timeoutMs: 50 } + } + ) + expect(stdout).toBe('models\nsettings\n') + const pid = Number(readFileSync(file, 'utf8')) + await vi.waitFor(() => expect(isAlive(pid)).toBe(false), { timeout: 5_000 }) + }) + + it('keeps required replies when the listing deadline lands inside the optional grace', async () => { + const stdout = await runAgentModelCatalogListing( + { + ...node(`process.stdout.write('models\\nsettings\\n');setInterval(()=>{},1000)`), + env: NODE_ENV + }, + { + site: 'test-partial-deadline', + timeoutMs: 3_000, + partialOutput: { ready: (output) => output.includes('settings\n'), timeoutMs: 60_000 } + } + ) + expect(stdout).toBe('models\nsettings\n') + }) + it('answers a listing’s stdout once it exits 0, with its request on stdin', async () => { const stdout = await runAgentModelCatalogListing( { diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts index be9f488cd58e..869eb3ca18cc 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-probe-runner.ts @@ -58,6 +58,8 @@ export type AgentModelCatalogRunnerOptions = { spawnImpl?: PipedProcessSpawner platform?: NodeJS.Platform inheritedEnv?: NodeJS.ProcessEnv + /** Finish required replies even when an optional control request never completes. */ + partialOutput?: { ready: (stdout: string) => boolean; timeoutMs: number } } /** Bound the listing and await child teardown before success, failure or cancellation settles. */ @@ -83,6 +85,7 @@ export async function runAgentModelCatalogListing( const stderrDetail = (): string => providerStderrForDisplay(managed.stderrTail()).trim().slice(0, STDERR_DETAIL_MAX_CHARS) let timer: ReturnType | undefined + let partialTimer: ReturnType | undefined let onAbort = (): void => {} try { return await new Promise((resolve, reject) => { @@ -99,16 +102,25 @@ export async function runAgentModelCatalogListing( failedExit ) ) + // Required replies already in hand outlive an optional reply that runs out of time or room. + const failUnlessReady = (message: string, reason: AgentModelCatalogProbeFailure): void => + partialTimer === undefined ? fail(message, reason) : resolve(output.text()) onAbort = () => fail(`${launch.command} listing stopped`, 'stopped') options.signal?.addEventListener('abort', onAbort, { once: true }) timer = setTimeout( - () => fail(`${launch.command} did not list models within ${timeoutMs}ms`, 'timeout'), + () => + failUnlessReady(`${launch.command} did not list models within ${timeoutMs}ms`, 'timeout'), timeoutMs ) child.stdout.on('data', (chunk: Buffer | string) => { output.write(chunk) if (output.truncated()) { - fail(`${launch.command} listed more than the model catalog reads`, 'output-overflow') + failUnlessReady( + `${launch.command} listed more than the model catalog reads`, + 'output-overflow' + ) + } else if (partialTimer === undefined && options.partialOutput?.ready(output.text())) { + partialTimer = setTimeout(() => resolve(output.text()), options.partialOutput.timeoutMs) } }) child.stdout.on('error', () => {}) @@ -142,6 +154,7 @@ export async function runAgentModelCatalogListing( }) } finally { clearTimeout(timer) + clearTimeout(partialTimer) options.signal?.removeEventListener('abort', onAbort) await managed.close() } diff --git a/src/main/native-chat/agent-model-catalog/agent-model-catalog-scaling.test.ts b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scaling.test.ts index 151e3fe0905b..905d9cb180e8 100644 --- a/src/main/native-chat/agent-model-catalog/agent-model-catalog-scaling.test.ts +++ b/src/main/native-chat/agent-model-catalog/agent-model-catalog-scaling.test.ts @@ -3,6 +3,97 @@ import { createAgentModelCatalogService } from './agent-model-catalog-service' import { AgentModelCatalogStore, type AgentModelCatalogSuccess } from './agent-model-catalog-store' import type { AgentModelCatalogProbe } from './agent-model-catalog-discovery' import type { AgentModelCatalogReadParams } from './agent-model-catalog-service-contract' +import { createClaudeModelCatalogProbe } from '../../claude/claude-model-catalog-probe' +import type { runAgentModelCatalogListing } from './agent-model-catalog-probe-runner' +import claudeCapture from '../../claude/__fixtures__/catalog-signin.json' +import { AGENT_MODEL_CATALOG_FAILURE_TTL_MS } from './agent-model-catalog-failures' +import { agentSessionRecordFixture } from '../agent-session-record-test-fixture' +import { agentSessionConversationView } from '../../../shared/agent-session-conversation-view' + +it('re-probes a signed-out Claude scope once per held-reason TTL for N and 10N chats', async () => { + const home = { variable: 'CLAUDE_CONFIG_DIR', path: '/account' } + // Distinct folders give distinct read claims; Claude's account-scoped catalog keeps one fingerprint. + const chat = (sessionId: string) => + agentSessionRecordFixture({ + sessionId, + provider: 'claude', + accountHome: home, + location: { workspaceId: `folder-${sessionId}`, workspaceKind: 'folder' } + }) + for (const chats of [10, 100]) { + let now = 1_000 + let listingGate: Promise | undefined + const runListing = vi.fn(async () => { + await listingGate + return [ + ['orca-model-discovery', claudeCapture.models], + ['orca-model-settings', claudeCapture.settings], + ['orca-signin-discovery', { account: claudeCapture.accounts.signedOut }] + ] + .map(([request_id, response]) => + JSON.stringify({ + type: 'control_response', + response: { subtype: 'success', request_id, response } + }) + ) + .join('\n') + }) + const store = new AgentModelCatalogStore({ now: () => now }) + const service = createAgentModelCatalogService({ + store, + getConversation: (id) => agentSessionConversationView(chat(id)), + getRecord: chat, + drivesRecord: () => true, + resolveAccountHome: async () => home, + probes: { + claude: createClaudeModelCatalogProbe({ + resolveInheritedEnv: async () => ({ PATH: '/usr/bin' }), + resolveEnv: () => ({}), + resolveCommand: () => ({ + command: process.execPath, + prefixArgs: ['run', '--', 'claude'] + }), + resolveAuthPolicy: () => ({ account: 'system' }), + runListing + }) + } + }) + const readAll = () => + Promise.all( + Array.from({ length: chats }, (_, index) => + service.read({ agent: 'claude', sessionId: `chat-${index}`, waitForListing: true }) + ) + ) + try { + await readAll() + now += AGENT_MODEL_CATALOG_FAILURE_TTL_MS - 1 + await readAll() + expect(runListing).toHaveBeenCalledTimes(1) + now += 1 + const opened = Promise.withResolvers() + listingGate = opened.promise + const decided = vi.spyOn(store, 'pendingListing') + const reading = readAll() + // Hold the re-probe until every chat's read has decided whether to start one. + await vi.waitFor(() => + expect(decided.mock.calls.filter(([, , terminal]) => terminal).length).toBe(chats) + ) + opened.resolve() + const due = await reading + expect(runListing).toHaveBeenCalledTimes(2) + for (const result of due) { + expect(result.unavailable).toMatchObject({ reason: 'notSignedIn' }) + expect(result.origin === 'unknown' ? 0 : result.models.length).toBeGreaterThan(0) + } + now += AGENT_MODEL_CATALOG_FAILURE_TTL_MS - 1 + await readAll() + expect(runListing).toHaveBeenCalledTimes(2) + expect(runListing.mock.calls[0]?.[0].stdin?.trim().split('\n')).toHaveLength(3) + } finally { + service.stop() + } + } +}) it('catalog-scale retires a many-scope frontier and releases actual listing waiters before recovery', async () => { for (const scopes of [5, 50]) { diff --git a/src/main/pi/rpc-model-catalog-probe.test.ts b/src/main/pi/rpc-model-catalog-probe.test.ts index 78d984bacb60..8aec026daaad 100644 --- a/src/main/pi/rpc-model-catalog-probe.test.ts +++ b/src/main/pi/rpc-model-catalog-probe.test.ts @@ -19,6 +19,8 @@ import { 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' +import { readFileSync } from 'node:fs' +import { join } from 'node:path' const AVAILABLE_MODELS = { models: [ @@ -77,6 +79,35 @@ function catalog(request: PiRpcRequester['request'], now = Date.now) { } describe('Pi model catalog', () => { + it('projects the recorded signed-out sentinel only when nothing is listable', async () => { + const frames = readFileSync(join(import.meta.dirname, '__fixtures__/signed-out.jsonl'), 'utf8') + .trim() + .split('\n') + .map((line) => JSON.parse(line)) + .filter((frame) => frame.dir === 'out') + .map((frame) => JSON.parse(frame.raw)) + const state = frames.find((frame) => frame.command === 'get_state').data + const empty = frames.find((frame) => frame.command === 'get_available_models').data + const closed = async () => { + throw new Error('rpc closed') + } + for (const read of [async () => empty, closed]) { + const { probe, record } = catalog(async (command) => + command === 'get_state' ? state : read() + ) + await expect(probe(record.accountHome)).rejects.toMatchObject({ + unavailable: { reason: 'notSignedIn' } + }) + } + const listed = catalog(async (command) => (command === 'get_state' ? state : AVAILABLE_MODELS)) + const result = await listed.probe(listed.record.accountHome) + expect(result.unavailable).toBeUndefined() + expect(result.models.length).toBeGreaterThan(0) + expect(result.configured).toBeUndefined() + const { probe, record } = catalog(async (command) => (command === 'get_state' ? {} : empty)) + await expect(probe(record.accountHome)).rejects.toThrow('pi listed no models') + }) + it.each(['', `"${process.execPath}" run -- pi`])( 'lists the native menu through sessionless RPC for Command %j', async (command) => { diff --git a/src/main/pi/rpc-model-catalog-probe.ts b/src/main/pi/rpc-model-catalog-probe.ts index 50ad681edc88..497c400397c7 100644 --- a/src/main/pi/rpc-model-catalog-probe.ts +++ b/src/main/pi/rpc-model-catalog-probe.ts @@ -13,6 +13,7 @@ import type { AgentModelCatalogSuccess } from '../native-chat/agent-model-catalog/agent-model-catalog-store' import { runAgentModelCatalogSession } from '../native-chat/agent-model-catalog/agent-model-catalog-probe-runner' +import { AgentModelCatalogUnavailableError } from '../native-chat/agent-model-catalog/agent-model-catalog-unavailable' import type { ProviderProcessLaunch } from '../provider-process/provider-process-launch' import { buildPiRpcLaunch, piRpcSessionlessEnvironment } from './rpc-launch' import { checkPiRpcVersion, resolvePiRpcInvocation } from './rpc-launch-resolution' @@ -89,9 +90,11 @@ export function createPiModelCatalogProbe(deps: PiModelCatalogProbeDeps): AgentM }), async (connection): Promise => { let configured: AgentModelCatalogSuccess['configured'] + let unknownProvider = false try { const state = discoveryStateSchema.safeParse(await connection.request('get_state')) - if (state.success && state.data.model) { + unknownProvider = state.success && state.data.model?.provider === 'unknown' + if (state.success && state.data.model && !unknownProvider) { configured = { model: piModelOptionId(state.data.model.provider, state.data.model.id), ...(typeof state.data.thinkingLevel === 'string' && state.data.thinkingLevel @@ -104,7 +107,16 @@ export function createPiModelCatalogProbe(deps: PiModelCatalogProbeDeps): AgentM } assertStructuredAgentResolutionCurrent(operation) options?.signal?.throwIfAborted() - const models = await readPiRpcModelOptions(connection) + // Pi's own "No models available" condition: no selected provider and nothing listable. + const models = await readPiRpcModelOptions(connection).catch((error: unknown) => { + if (unknownProvider) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } + throw error + }) + if (unknownProvider && models.length === 0) { + throw new AgentModelCatalogUnavailableError({ reason: 'notSignedIn' }) + } return { models, origin: 'probe', ...(configured ? { configured } : {}) } }, { label: 'pi', signal: options?.signal }