Skip to content

armgit / Orkas

Public Imported from GitHub
0

Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.

Preserve provider SDK and reasoning replay compatibility

wyt9e10b442571f
diff --git a/src/core-agent/src/config/schema.ts b/src/core-agent/src/config/schema.ts
index 1eb7233b..667da696 100644
--- a/src/core-agent/src/config/schema.ts
+++ b/src/core-agent/src/config/schema.ts
@@ -61,22 +61,22 @@ export const EvolutionConfigSchema = z.object({
/** Maximum SKILL.md content length in characters. */
maxSkillContentLength: z.number().int().positive().default(100_000),
/** Metacognition subsystem. */
- metacognition: MetacognitionConfigSchema.default({}),
+ metacognition: MetacognitionConfigSchema.prefault({}),
});
/** Top-level core-agent configuration schema. */
export const CoreAgentConfigSchema = z.object({
- agent: AgentConfigSchema.default({}),
+ agent: AgentConfigSchema.prefault({}),
models: z
.object({
- providers: z.record(z.string(), ProviderConfigSchema).default({}),
- catalog: z.record(z.string(), ModelConfigSchema).default({}),
+ providers: z.record(z.string(), ProviderConfigSchema).prefault({}),
+ catalog: z.record(z.string(), ModelConfigSchema).prefault({}),
})
- .default({}),
+ .prefault({}),
// The `memory` retrieval-engine section was removed with the unwired
// OpenClaw-ported engine (2026-08-16); the non-strict schema strips the key
// from older config files.
- evolution: EvolutionConfigSchema.default({}),
+ evolution: EvolutionConfigSchema.prefault({}),
});
export type ProviderConfig = z.infer<typeof ProviderConfigSchema>;
diff --git a/src/core-agent/src/providers/base.ts b/src/core-agent/src/providers/base.ts
index 447c276e..57876379 100644
--- a/src/core-agent/src/providers/base.ts
+++ b/src/core-agent/src/providers/base.ts
@@ -25,6 +25,17 @@ export type ToolDefinition = {
/** Parameters for an LLM completion request. */
export type CompletionParams = {
+ /** Request-local measurement from core preparation. Input references identify
+ * the unchanged request view; transformed inputs and foreign candidates must
+ * estimate afresh. Cloneable bookkeeping, never sent to an API or persisted. */
+ requestTokenEstimate?: {
+ model: string;
+ messages: Message[];
+ systemPrompt?: string;
+ tools?: ToolDefinition[];
+ tokens: number;
+ source: "anchored" | "estimated";
+ };
/** Best-effort metadata observer, including attempts recovered by wrappers. Never sent to the API. */
onRequestFailure?: (failure: import("./request-diagnostics.js").ProviderRequestFailure) => void;
model: string;
@@ -56,10 +67,10 @@ export type CompletionParams = {
* `"none"` disables caching. Provider-specific translation happens inside
* pi-ai; providers without cache support ignore it. */
cacheRetention?: "none" | "short" | "long";
- /** Stable identifier used as `prompt_cache_key` (OpenAI / Codex / Azure /
- * OpenRouter) and as cache-affinity hint (Mistral, others). Should be the
- * caller's session id so repeated turns on the same conversation hit the
- * same cache bucket. */
+ /** Conversation identifier forwarded to provider-specific cache metadata.
+ * The Codex SSE adapter separately scopes its HTTP cache-affinity header to
+ * the credential/endpoint/model; this id still owns its body cache key and
+ * request identity. Neither identifier guarantees a cache hit. */
sessionId?: string;
/** Opaque, in-memory identity of the active user turn. Transport adapters
* may associate private routing state with it; never serialize it into API
diff --git a/src/core-agent/src/providers/chat-reasoning-replay.ts b/src/core-agent/src/providers/chat-reasoning-replay.ts
new file mode 100644
index 00000000..a9a0dc90
--- /dev/null
+++ b/src/core-agent/src/providers/chat-reasoning-replay.ts
@@ -0,0 +1,60 @@
+import { randomUUID } from 'node:crypto';
+import type { Api, Context, Model } from '@earendil-works/pi-ai/compat';
+
+const PLAINTEXT_FIELDS = new Set(['reasoning_content', 'reasoning', 'reasoning_text']);
+
+/** pi-ai 1.3.0 drops an assistant with reasoning but no text/tool calls.
+ * Keep only already-projected portable Chat reasoning alive inside the SDK,
+ * then remove our request-local sentinel before payload hooks or transport.
+ * The SDK still owns reasoning fields, source checks and generic conversion.
+ * No sentinel enters model input, persisted history or visible answer text. */
+export function prepareChatReasoningReplay<T extends Context>(context: T, model: Model<Api>) {
+ let marker: string | undefined;
+ let expected = 0;
+ let messages: T['messages'] | undefined;
+ if (model.api === 'openai-completions') {
+ for (const [index, message] of context.messages.entries()) {
+ if (message.role !== 'assistant' || !message.content.length
+ || !message.content.every(block => block.type === 'text' ? !block.text.trim()
+ : block.type === 'thinking' && !block.redacted && PLAINTEXT_FIELDS.has(block.thinkingSignature ?? ''))
+ || !message.content.some(block => block.type === 'thinking' && block.thinking.trim())) continue;
+ marker ??= `orkas-chat-replay:${randomUUID()}`;
+ messages ??= context.messages.slice();
+ messages[index] = { ...message, content: [...message.content, { type: 'text', text: marker }] };
+ expected++;
+ }
+ }
+ return {
+ active: !!marker,
+ context: messages ? { ...context, messages } : context,
+ restore(payload: unknown): unknown {
+ if (!marker) return payload;
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)
+ || !Array.isArray((payload as { messages?: unknown }).messages)) {
+ throw new Error('Chat reasoning replay payload is unavailable');
+ }
+ const body = payload as { messages: Record<string, unknown>[] };
+ let restored = 0;
+ const projected = body.messages.flatMap(message => {
+ if (message.role !== 'assistant') return [message];
+ let content = message.content;
+ if (content === marker) content = null;
+ else if (Array.isArray(content) && content.some(block => block?.type === 'text' && block.text === marker)) {
+ // Preserve SDK-declared thinking-as-text compatibility, if enabled.
+ content = content.filter(block => block?.type !== 'text' || block.text !== marker);
+ } else return [message];
+ restored++;
+ const hasContent = typeof content === 'string' ? content.length > 0
+ : Array.isArray(content) && content.length > 0;
+ const hasReasoning = [...PLAINTEXT_FIELDS].some(field =>
+ typeof message[field] === 'string' && (message[field] as string).trim().length > 0);
+ // Never revive state the SDK itself omitted, or manufacture a body.
+ return hasContent || hasReasoning ? [{ ...message, content }] : [];
+ });
+ // Fail before transport if a future serializer changes the sentinel's
+ // shape; no internal text may escape under an unverified SDK contract.
+ if (restored !== expected) throw new Error('Chat reasoning replay serializer mismatch');
+ return { ...payload, messages: projected };
+ },
+ };
+}
diff --git a/src/core-agent/src/providers/pi-provider.ts b/src/core-agent/src/providers/pi-provider.ts
index 843ab5e8..c3ce2fa4 100644
--- a/src/core-agent/src/providers/pi-provider.ts
+++ b/src/core-agent/src/providers/pi-provider.ts
@@ -54,6 +54,7 @@ import { providerCredentialFailure, providerErrorFacts } from "../shared/provide
import type { CompletionParams, CompletionResult, LLMProvider, ToolDefinition } from "./base.js";
import { createResponsesTerminalReasoning } from './responses-terminal-reasoning.js';
+import { prepareChatReasoningReplay } from './chat-reasoning-replay.js';
import { createResponsesStatusDiagnostics } from './responses-status-diagnostics.js';
import { createRequestDiagnostics, type ProviderFailureSource } from "./request-diagnostics.js";
import { createProviderErrorCapture } from "./provider-error-capture.js";
@@ -73,16 +74,44 @@ type ProviderHttpResponse = { status: number; headers?: Headers | Record<string,
// turn owns this weak lifetime; neither session ids nor credentials retain it.
const codexTurnStates = new WeakMap<object, { owner: string; value?: string }>();
+/** Replace only SDK affinity headers after construction. Caller-owned headers
+ * (including managed login credentials) and per-chat request ids stay intact.
+ * Without an explicit host user scope, retain the SDK's per-chat identity. */
+function withUserCacheAffinity(
+ next: typeof globalThis.fetch, model: Model<Api>, key: string | undefined,
+ enabled: boolean, suppliedHeaders?: Record<string, string>,
+): typeof globalThis.fetch {
+ if (!enabled || !key) return next;
+ const name = model.api === 'openai-codex-responses' ? 'session-id'
+ : model.api === 'openai-responses' || model.api === 'openai-completions' ? 'session_id' : undefined;
+ if (!name || (model.api !== 'openai-codex-responses'
+ && new Headers(suppliedHeaders).has(name))) return next;
+ return (input, init) => {
+ const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
+ if (!headers.has(name)) return next(input, init);
+ headers.set(name, key);
+ return next(input, { ...init, headers });
+ };
+}
+
+/** Do not introduce cache extensions to an unknown protocol or gateway. */
+function withUserPromptCacheKey(payload: unknown, key: string | undefined): unknown {
+ if (!key || !payload || typeof payload !== 'object' || Array.isArray(payload)
+ || typeof (payload as Record<string, unknown>).prompt_cache_key !== 'string'
+ || !(payload as Record<string, unknown>).prompt_cache_key) return payload;
+ return { ...payload, prompt_cache_key: key };
+}
+
function codexTurnRouting(
model: Model<Api>, params: CompletionParams, apiKey: string | undefined,
- headers: Record<string, string> | undefined,
+ headers: Record<string, string> | undefined, userCacheKey?: string,
): { headers: Record<string, string>; observe: (response: ProviderHttpResponse) => void } | undefined {
const turn = params.providerTurnContext;
if (model.api !== "openai-codex-responses" || !turn) return undefined;
// A credential refresh, endpoint/model switch or session switch starts fresh.
// Never retain raw credentials or send another owner's routing state.
const owner = crypto.createHash("sha256").update(JSON.stringify([
- model.provider, model.baseUrl, model.id, apiKey, params.sessionId,
+ model.provider, model.baseUrl, model.id, apiKey, params.sessionId, userCacheKey,
])).digest("hex");
let state = codexTurnStates.get(turn);
if (!state || state.owner !== owner) {
@@ -173,7 +202,7 @@ type RuntimePiContext = PiContext & { hostMessageIndexes: number[]; foreignRespo
* ordering in `transform-messages`, one image trailer per contiguous tool-result
* batch in `openai-completions`). Bump only after re-checking those rules against
* the new release; `providers.test.ts` pins the installed package to this value. */
-export const MIRRORED_PI_AI_SERIALIZER_VERSION = "1.2.0";
+export const MIRRORED_PI_AI_SERIALIZER_VERSION = "1.3.0";
/** pi-ai's portable history has no developer role. Responses has one, so
* restore explicitly typed host messages at the native serialization boundary.
@@ -681,6 +710,10 @@ function mapContent(
atTextStart = false;
}
} else if (block.type === "toolCall") {
+ // pi-ai 1.3 marks invalid final JSON separately from its empty-object
+ // fallback. Never turn that fallback into an executable host call.
+ // The SDK error contains raw arguments, so keep it out of diagnostics.
+ if (block.argumentsError) throw new Error("Invalid tool call arguments");
result.push({
type: "tool_use",
id: block.id,
@@ -908,6 +941,8 @@ export function createPiProvider(config: {
apiKey?: string;
baseUrl?: string;
headers?: Record<string, string>;
+ /** Opaque host user mapping for cache affinity only; never local Session identity. */
+ userCacheKey?: string;
/** Optional per-call HTTP headers derived from Host-private metadata.
* Values are merged after static headers and are never added to the model
* payload. Managed Orkas requests use this for `X-Request-ID`. */
@@ -949,6 +984,7 @@ export function createPiProvider(config: {
normalizeLiteralLeadingThinkText?: boolean;
}): LLMProvider {
const providerId = config.provider as KnownProvider;
+ const userCacheKey = cacheSafeSessionId(config.userCacheKey);
// Resolve the model object. `customModel` shortcuts pi-ai's catalog
// lookup for providers pi-ai doesn't know about.
@@ -1009,7 +1045,8 @@ export function createPiProvider(config: {
const suppliedHeaders = requestHeaders(params);
const apiKey = config.apiKey ?? getEnvApiKey(model.provider);
const origin = replayOrigin(model, apiKey, suppliedHeaders);
- const context = buildPiContext(params.messages, params.systemPrompt, params.tools, model, statusDiagnostics, origin);
+ const chatReplay = prepareChatReasoningReplay(buildPiContext(params.messages, params.systemPrompt, params.tools, model, statusDiagnostics, origin), model);
+ const context = chatReplay.context;
const reasoning = effectiveReasoning(params.reasoning);
const sessionId = cacheSafeSessionId(params.sessionId);
const terminalReasoning = createResponsesTerminalReasoning(model.api, params.signal);
@@ -1017,19 +1054,20 @@ export function createPiProvider(config: {
&& ((params.reasoning === undefined && config.defaultReasoning === undefined)
|| (params.reasoning === "off" && model.thinkingLevelMap?.off === null));
const useProviderOutputDefault = params.requestMetadata?.outputLimitSource === "provider_default";
- const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders);
+ const cacheKey = sessionId && params.cacheRetention !== 'none' ? userCacheKey : undefined;
+ const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders, userCacheKey);
const headers = turnRouting?.headers ?? suppliedHeaders;
- const onPayload = (statusDiagnostics || useOfficialReasoningDefaults || useProviderOutputDefault || needsDeepSeekToolReasoning(model) || config.onPayload || context.hostMessageIndexes.length || context.foreignResponsesCalls.some(Boolean))
+ const onPayload = (cacheKey || chatReplay.active || statusDiagnostics || useOfficialReasoningDefaults || useProviderOutputDefault || needsDeepSeekToolReasoning(model) || config.onPayload || context.hostMessageIndexes.length || context.foreignResponsesCalls.some(Boolean))
? ((payload: unknown, hookModel: Model<Api>) => requestDiagnostics.observePayload(() => {
- payload = restoreHostMessageRoles(projectResponsesItemIds(payload, context.foreignResponsesCalls), context, hookModel);
+ payload = restoreHostMessageRoles(projectResponsesItemIds(chatReplay.restore(payload), context.foreignResponsesCalls), context, hookModel);
const reasoningNormalized = useOfficialReasoningDefaults
? restoreOfficialReasoningDefaults(payload, hookModel)
: payload;
- const normalized = useProviderDefaultOutputLimit(
+ const normalized = withUserPromptCacheKey(useProviderDefaultOutputLimit(
reasoningNormalized,
hookModel,
params.requestMetadata?.outputLimitSource,
- );
+ ), cacheKey);
const transformed = config.onPayload
? config.onPayload(normalized, hookModel, params.requestMetadata)
: normalized;
@@ -1037,7 +1075,7 @@ export function createPiProvider(config: {
// Preserve Host normalization when an observer-only hook returns
// nothing instead of accidentally restoring the generated cap.
const observe = (result: unknown) => {
- const value = projectDeepSeekToolReasoning(result === undefined ? normalized : result, hookModel);
+ const value = withUserPromptCacheKey(projectDeepSeekToolReasoning(result === undefined ? normalized : result, hookModel), cacheKey);
requestDiagnostics.observeBudget(value, hookModel.contextWindow);
statusDiagnostics?.payload(value);
return value;
@@ -1059,7 +1097,10 @@ export function createPiProvider(config: {
responseErrorStatus = response.status >= 400 ? response.status : undefined;
requestDiagnostics.onResponse(response);
};
- const transportFetch = errorCapture.wrapFetch(config.wrapFetch?.(requestDiagnostics.requestFetch) ?? requestDiagnostics.requestFetch);
+ const transportFetch = withUserCacheAffinity(
+ errorCapture.wrapFetch(config.wrapFetch?.(requestDiagnostics.requestFetch) ?? requestDiagnostics.requestFetch),
+ model, cacheKey, Boolean(cacheKey), suppliedHeaders,
+ );
const requestFetch: typeof globalThis.fetch = async (input, init) => {
const response = await transportFetch(input, init);
onResponse(response);
@@ -1155,14 +1196,16 @@ export function createPiProvider(config: {
const suppliedHeaders = requestHeaders(params);
const apiKey = config.apiKey ?? getEnvApiKey(model.provider);
const origin = replayOrigin(model, apiKey, suppliedHeaders);
- const context = buildPiContext(params.messages, params.systemPrompt, params.tools, model, statusDiagnostics, origin);
+ const chatReplay = prepareChatReasoningReplay(buildPiContext(params.messages, params.systemPrompt, params.tools, model, statusDiagnostics, origin), model);
+ const context = chatReplay.context;
const reasoning = effectiveReasoning(params.reasoning);
const sessionId = cacheSafeSessionId(params.sessionId);
const terminalReasoning = createResponsesTerminalReasoning(model.api, params.signal);
const useOfficialReasoningDefaults = model.reasoning === true
&& ((params.reasoning === undefined && config.defaultReasoning === undefined)
|| (params.reasoning === "off" && model.thinkingLevelMap?.off === null));
- const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders);
+ const cacheKey = sessionId && params.cacheRetention !== 'none' ? userCacheKey : undefined;
+ const turnRouting = codexTurnRouting(model, params, config.apiKey, suppliedHeaders, userCacheKey);
const headers = turnRouting?.headers ?? suppliedHeaders;
const errorCapture = createProviderErrorCapture(
model.api === 'openai-responses' ? 'openai-responses'
@@ -1173,7 +1216,10 @@ export function createPiProvider(config: {
responseErrorStatus = response.status >= 400 ? response.status : undefined;
requestDiagnostics.onResponse(response);
};
- const transportFetch = errorCapture.wrapFetch(config.wrapFetch?.(requestDiagnostics.requestFetch) ?? requestDiagnostics.requestFetch);
+ const transportFetch = withUserCacheAffinity(
+ errorCapture.wrapFetch(config.wrapFetch?.(requestDiagnostics.requestFetch) ?? requestDiagnostics.requestFetch),
+ model, cacheKey, Boolean(cacheKey), suppliedHeaders,
+ );
const requestFetch: typeof globalThis.fetch = async (input, init) => {
const response = await transportFetch(input, init);
onResponse(response);
@@ -1185,21 +1231,21 @@ export function createPiProvider(config: {
};
let effectiveMaxTokens: number | undefined;
const onPayload = (payload: unknown, hookModel: Model<Api>): Promise<unknown> => requestDiagnostics.observePayload(async () => {
- payload = restoreHostMessageRoles(projectResponsesItemIds(payload, context.foreignResponsesCalls), context, hookModel);
+ payload = restoreHostMessageRoles(projectResponsesItemIds(chatReplay.restore(payload), context.foreignResponsesCalls), context, hookModel);
const reasoningNormalized = useOfficialReasoningDefaults
? restoreOfficialReasoningDefaults(payload, hookModel)
: payload;
- const normalized = useProviderDefaultOutputLimit(
+ const normalized = withUserPromptCacheKey(useProviderDefaultOutputLimit(
reasoningNormalized,
hookModel,
params.requestMetadata?.outputLimitSource,
- );
+ ), cacheKey);
const transformed = config.onPayload
? await config.onPayload(normalized, hookModel, params.requestMetadata)
: normalized;
// pi-ai treats an undefined hook result as "keep the pre-hook
// payload". Observer-only hooks must not undo Host normalization.
- const finalPayload = projectDeepSeekToolReasoning(transformed === undefined ? normalized : transformed, hookModel);
+ const finalPayload = withUserPromptCacheKey(projectDeepSeekToolReasoning(transformed === undefined ? normalized : transformed, hookModel), cacheKey);
// Observe that exact final shape so this is an on-wire value, not a
// model-catalog guess made before context and adapter adjustments.
effectiveMaxTokens = effectiveMaxTokensFromPayload(finalPayload);
@@ -1552,7 +1598,9 @@ function wrapError(err: unknown, providerId: string): Error {
return err;
}
- if (facts.codes.some(code => code === 'context_length_exceeded' || code === 'CONTEXT_OVERFLOW')) {
+ // llama.cpp reports oversized input in error.type, alongside numeric code 400.
+ if (facts.codes.some(code => code === 'context_length_exceeded'
+ || code === 'exceed_context_size_error' || code === 'CONTEXT_OVERFLOW')) {
return new ContextOverflowError(`${providerId} context overflow: ${msg}`, original);
}
if (err instanceof ProviderError) return err;
diff --git a/src/core-agent/test/chat-reasoning-replay.test.ts b/src/core-agent/test/chat-reasoning-replay.test.ts
new file mode 100644
index 00000000..aa5fd42d
--- /dev/null
+++ b/src/core-agent/test/chat-reasoning-replay.test.ts
@@ -0,0 +1,60 @@
+import { expect, it } from 'vitest';
+import { prepareChatReasoningReplay } from '../src/providers/chat-reasoning-replay.js';
+import { buildPiContextForTest } from '../src/providers/pi-provider.js';
+import { replayOrigin } from '../src/providers/replay-compatibility.js';
+import type { MessageContent } from '../src/shared/types.js';
+
+const model = { api: 'openai-completions' as const, provider: 'custom', id: 'fixture', name: 'fixture',
+ baseUrl: 'https://fixture.invalid/v1', reasoning: true, input: ['text' as const], contextWindow: 10000,
+ maxTokens: 100, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } };
+const thought: MessageContent = { type: 'thinking', thinking: 'Original thought.', thinkingSignature: 'reasoning_content',
+ replayOrigin: replayOrigin(model) };
+const context = (content: MessageContent[]) => buildPiContextForTest([{ role: 'assistant', content }], undefined, undefined, model);
+
+it('fails before transport on missing or changed serialization rather than leaking internal text', () => {
+ const source = context([thought]);
+ const before = JSON.stringify(source);
+ const replay = prepareChatReasoningReplay(source, model);
+ expect(replay.active).toBe(true);
+ expect(() => replay.restore({})).toThrow('Chat reasoning replay payload is unavailable');
+ expect(() => replay.restore({ messages: [] })).toThrow('Chat reasoning replay serializer mismatch');
+ expect(JSON.stringify(source)).toBe(before);
+});
+
+it.each(([
+ [], [{ type: 'text', text: 'Answer.' }],
+ [{ ...thought, thinking: '' }], [{ ...thought, redacted: true }],
+ [{ ...thought, thinkingSignature: 'opaque-signature' }],
+ [thought, { type: 'tool_use', id: 'call_fixture', name: 'inspect', input: {} }],
+] as MessageContent[][]).map(content => ({ content })))('leaves noneligible history unchanged: %#', ({ content }) => {
+ const source = context(content);
+ const replay = prepareChatReasoningReplay(source, model);
+ const payload = { messages: [] };
+ expect(replay.active).toBe(false);
+ expect(replay.context).toBe(source);
+ expect(replay.restore(payload)).toBe(payload);
+});
+
+it('removes only its own sentinel, preserves native reasoning and never revives SDK-omitted state', () => {
+ const source = context([thought]);
+ const replay = prepareChatReasoningReplay(source, model);
+ const assistant = replay.context.messages[0];
+ if (assistant.role !== 'assistant') throw new Error('Missing assistant fixture');
+ const marker = assistant.content.at(-1);
+ if (marker?.type !== 'text') throw new Error('Missing internal fixture marker');
+ const user = { role: 'user', content: 'orkas-chat-replay:user-owned literal' };
+ expect(replay.restore({ messages: [user, { role: 'assistant', content: marker.text, reasoning_content: thought.thinking }] }))
+ .toEqual({ messages: [user, { role: 'assistant', content: null, reasoning_content: thought.thinking }] });
+ expect(replay.restore({ messages: [{ role: 'assistant', content: marker.text }] })).toEqual({ messages: [] });
+ // A declared SDK thinking-as-text format is retained without our sentinel.
+ expect(replay.restore({ messages: [{ role: 'assistant', content: [
+ { type: 'text', text: thought.thinking }, { type: 'text', text: marker.text },
+ ] }] })).toEqual({ messages: [{ role: 'assistant', content: [{ type: 'text', text: thought.thinking }] }] });
+});
+
+it('does not change another API transport', () => {
+ const source = context([thought]);
+ const replay = prepareChatReasoningReplay(source, { ...model, api: 'openai-responses' });
+ expect(replay.active).toBe(false);
+ expect(replay.context).toBe(source);
+});
diff --git a/src/core-agent/test/codex-cache-affinity.test.ts b/src/core-agent/test/codex-cache-affinity.test.ts
new file mode 100644
index 00000000..cb829730
--- /dev/null
+++ b/src/core-agent/test/codex-cache-affinity.test.ts
@@ -0,0 +1,246 @@
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { createPiProvider } from '../src/providers/pi-provider.js';
+import type { CompletionParams } from '../src/providers/base.js';
+import * as zlib from 'node:zlib';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import { AgentRunner } from '../src/agent/runner.js';
+import { PersistentSession } from '../src/agent/persistent-session.js';
+import { ProviderRegistry } from '../src/providers/registry.js';
+import { createConfig } from '../src/config/loader.js';
+import { defineTool } from '../src/tools/base.js';
+
+const token = (account: string) => `fixture.${Buffer.from(JSON.stringify({
+ 'https://api.openai.com/auth': { chatgpt_account_id: account },
+})).toString('base64')}.fixture`;
+const request = (sessionId: string): CompletionParams => ({
+ model: 'gpt-6.1-sol', sessionId, cacheRetention: 'long',
+ systemPrompt: 'Stable reference',
+ messages: [{ role: 'user', content: [{ type: 'text', text: `Private input for ${sessionId}` }] }],
+});
+function response() {
+ return new Response(`data: ${JSON.stringify({ type: 'response.completed', response: {
+ id: 'fixture', status: 'completed', output: [],
+ usage: { input_tokens: 100, output_tokens: 1, input_tokens_details: { cached_tokens: 0 } },
+ } })}\n\n`, { headers: { 'content-type': 'text/event-stream' } });
+}
+afterEach(() => vi.unstubAllGlobals());
+
+async function wirePayload(input: RequestInfo | URL, init?: RequestInit): Promise<any> {
+ const wire = new Request(input, init);
+ const bytes = Buffer.from(await wire.arrayBuffer());
+ return JSON.parse((wire.headers.get('content-encoding') === 'zstd'
+ ? zlib.zstdDecompressSync(bytes) : bytes).toString('utf8'));
+}
+
+describe('Codex cache affinity without shared conversation state', () => {
+ it.each(['complete', 'stream'] as const)('enforces user ownership after an async payload hook and on the actual wire (%s)', async method => {
+ const captures: Array<{ body: any; headers: Headers }> = [];
+ vi.stubGlobal('fetch', vi.fn(async (input, init) => {
+ captures.push({ body: await wirePayload(input, init), headers: new Headers(init?.headers) });
+ return response();
+ }));
+ const p = createPiProvider({ provider: 'openai-codex', model: 'gpt-6.1-sol', apiKey: token('shared'),
+ userCacheKey: 'opaque-user-a',
+ onPayload: async payload => ({ ...(payload as object), prompt_cache_key: 'foreign-hook-key' }),
+ });
+ const params = request('chat-a'), before = JSON.stringify(params);
+ if (method === 'complete') await p.complete(params);
+ else for await (const e of p.stream(params)) if (e.type === 'error') throw e.error;
+ expect(captures).toHaveLength(1);
+ expect(captures[0].body.prompt_cache_key).toBe('opaque-user-a');
+ expect(captures[0].headers.get('session-id')).toBe('opaque-user-a');
+ expect(captures[0].headers.get('x-client-request-id')).toBe('chat-a');
+ expect(JSON.stringify(captures[0].body)).not.toContain('foreign-hook-key');
+ expect(JSON.stringify(params)).toBe(before);
+ });
+
+ it('keeps two parallel tool sessions and their reloaded receipts isolated under one shared SID', async () => {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'user-affinity-isolation-'));
+ const traces: Array<{ chat: string; body: any; headers: Headers }> = [];
+ const rounds = new Map<string, number>(), executed: string[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (input, init) => {
+ const body = await wirePayload(input, init), headers = new Headers(init?.headers);
+ const chat = headers.get('x-client-request-id')!;
+ expect(['chat-a', 'chat-b']).toContain(chat);
+ const marker = chat === 'chat-a' ? 'MARKER_ALPHA' : 'MARKER_BETA';
+ const foreign = chat === 'chat-a' ? 'MARKER_BETA' : 'MARKER_ALPHA';
+ expect(JSON.stringify(body.input)).toContain(marker);
+ expect(JSON.stringify(body.input)).not.toContain(foreign);
+ const round = (rounds.get(chat) ?? 0) + 1;
+ rounds.set(chat, round); traces.push({ chat, body, headers });
+ const item = round === 1
+ ? { type: 'function_call', id: `fc_${chat}`, call_id: `call_${chat}`, name: 'record_fixture',
+ arguments: JSON.stringify({ marker }), status: 'completed' }
+ : { type: 'message', id: `msg_${chat}_${round}`, role: 'assistant', status: 'completed',
+ content: [{ type: 'output_text', text: 'Recorded.', annotations: [] }] };
+ const events: any[] = [{ type: 'response.created', response: { id: `resp_${chat}_${round}`, output: [], status: 'in_progress' } },
+ { type: 'response.output_item.added', output_index: 0, item: round === 1 ? { ...item, arguments: '' } : { ...item, content: [] } }];
+ if (round === 1) events.push({ type: 'response.function_call_arguments.delta', output_index: 0, delta: item.arguments });
+ else events.push({ type: 'response.content_part.added', output_index: 0, content_index: 0, part: { type: 'output_text', text: '', annotations: [] } },
+ { type: 'response.output_text.delta', output_index: 0, content_index: 0, delta: 'Recorded.' });
+ events.push({ type: 'response.output_item.done', output_index: 0, item },
+ { type: 'response.completed', response: { id: `resp_${chat}_${round}`, status: 'completed', output: [item],
+ usage: { input_tokens: 100, output_tokens: 5 } } });
+ return new Response(events.map(e => `event: ${e.type}\ndata: ${JSON.stringify(e)}\n\n`).join(''),
+ { headers: { 'content-type': 'text/event-stream', 'x-codex-turn-state': `route-${chat}` } });
+ }));
+ const build = (chat: string) => {
+ const session = new PersistentSession({ sessionFile: path.join(dir, `${chat}.jsonl`) });
+ const providers = new ProviderRegistry();
+ providers.registerFactory('openai-codex', () => createPiProvider({ provider: 'openai-codex', model: 'gpt-6.1-sol',
+ apiKey: token('shared'), userCacheKey: 'opaque-user-a' }));
+ return new AgentRunner({ config: createConfig({ agent: { defaultProvider: 'openai-codex', defaultModel: 'gpt-6.1-sol', maxRetries: 0 } }),
+ providers, session, evolution: { enabled: false }, tools: [defineTool({ name: 'record_fixture',
+ description: 'Record the supplied fixture marker.', inputSchema: { type: 'object', properties: { marker: { type: 'string' } }, required: ['marker'] },
+ execute: async args => { const marker = (args as { marker: string }).marker; executed.push(marker); return { content: `receipt:${marker}` }; },
+ })] });
+ };
+ try {
+ const first = await Promise.all([build('chat-a').run({ message: 'Record MARKER_ALPHA.' }),
+ build('chat-b').run({ message: 'Record MARKER_BETA.' })]);
+ expect(first.every(result => !result.meta.error)).toBe(true);
+ expect(executed.sort()).toEqual(['MARKER_ALPHA', 'MARKER_BETA']);
+ for (const [chat, marker, foreign] of [['chat-a', 'MARKER_ALPHA', 'MARKER_BETA'], ['chat-b', 'MARKER_BETA', 'MARKER_ALPHA']]) {
+ const stored = fs.readFileSync(path.join(dir, `${chat}.jsonl`), 'utf8');
+ expect(stored).toContain(`receipt:${marker}`);
+ expect(stored).not.toContain(foreign);
+ const resumed = await build(chat).run({ message: 'Continue after reopening.' });
+ expect(resumed.meta.error).toBeUndefined();
+ }
+ expect(executed).toHaveLength(2);
+ expect(traces).toHaveLength(6);
+ expect(traces.every(t => t.headers.get('session-id') === 'opaque-user-a'
+ && t.body.prompt_cache_key === 'opaque-user-a')).toBe(true);
+ for (const chat of ['chat-a', 'chat-b']) expect(traces.filter(t => t.chat === chat)
+ .map(t => t.headers.get('x-codex-turn-state'))).toEqual([null, `route-${chat}`, null]);
+ } finally { fs.rmSync(dir, { recursive: true, force: true }); }
+ });
+
+ it.each(['complete', 'stream'] as const)('keeps affinity across new chats while preserving private payloads and request identity (%s)', async method => {
+ const headers: Headers[] = [];
+ const payloads: any[] = [];
+ const signals: unknown[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => {
+ headers.push(new Headers(init.headers)); signals.push(init.signal);
+ return response();
+ }));
+ const config = { provider: 'openai-codex', model: 'gpt-6.1-sol', apiKey: token('a'), userCacheKey: 'opaque-user-a',
+ headers: { 'X-Fixture': 'preserved' },
+ onPayload: (value: unknown) => { payloads.push(structuredClone(value)); },
+ };
+ const build = () => createPiProvider(config);
+ const controller = new AbortController();
+ const a = { ...request('chat-a'), signal: controller.signal };
+ const b = { ...request('chat-b'), reasoning: 'low' as const, signal: controller.signal };
+ const before = JSON.stringify([a, b]);
+ for (const params of [a, b, a]) {
+ if (method === 'complete') await build().complete(params);
+ else for await (const event of build().stream(params)) if (event.type === 'error') throw event.error;
+ }
+ const affinity = headers[0].get('session-id');
+ expect(typeof affinity).toBe('string');
+ expect(affinity!.length).toBeGreaterThan(0);
+ expect(affinity!.length).toBeLessThanOrEqual(64);
+ expect(affinity).not.toBe('chat-a');
+ expect(headers.map(h => h.get('session-id'))).toEqual([affinity, affinity, affinity]);
+ expect(headers.map(h => h.get('x-client-request-id'))).toEqual(['chat-a', 'chat-b', 'chat-a']);
+ expect(headers.every(h => h.get('authorization') === `Bearer ${token('a')}`
+ && h.get('chatgpt-account-id') === 'a' && h.get('x-fixture') === 'preserved')).toBe(true);
+ expect(payloads.map(p => p.prompt_cache_key)).toEqual([affinity, affinity, affinity]);
+ expect(JSON.stringify(payloads[0])).not.toContain('chat-b');
+ expect(JSON.stringify(payloads[1])).not.toContain('chat-a');
+ expect(JSON.stringify([a, b])).toBe(before);
+ expect(signals.every(Boolean)).toBe(true);
+ });
+
+ it('keeps one user stable across credential refresh and model changes but isolates users sharing a credential', async () => {
+ const headers: Headers[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => { headers.push(new Headers(init.headers)); return response(); }));
+ for (const [userCacheKey, account, model, baseUrl] of [
+ ['opaque-user-a', 'shared', 'gpt-6.1-sol', undefined],
+ ['opaque-user-b', 'shared', 'gpt-6.1-sol', undefined],
+ ['opaque-user-a', 'refreshed', 'gpt-6-sol', undefined],
+ ['opaque-user-a', 'shared', 'gpt-6.1-sol', 'https://fixture.invalid/backend-api'],
+ ] as const) {
+ const config = { provider: 'openai-codex', model, apiKey: token(account), baseUrl, userCacheKey };
+ await createPiProvider(config).complete({ ...request('same-chat'), model });
+ }
+ const keys = headers.map(h => h.get('session-id'));
+ expect(keys).toEqual(['opaque-user-a', 'opaque-user-b', 'opaque-user-a', 'opaque-user-a']);
+ expect(keys.every(key => key && !key.includes('same-chat') && !key.includes(token('a')))).toBe(true);
+ });
+
+ it('retains per-chat SDK identity when no user mapping is available', async () => {
+ const headers: Headers[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => { headers.push(new Headers(init.headers)); return response(); }));
+ const p = createPiProvider({ provider: 'openai-codex', model: 'gpt-6.1-sol', apiKey: token('shared') });
+ await p.complete(request('chat-a'));
+ await p.complete(request('chat-b'));
+ expect(headers.map(h => h.get('session-id'))).toEqual(['chat-a', 'chat-b']);
+ });
+
+ it('cancels only its own concurrent chat and retries without changing the user mapping', async () => {
+ const headers: Headers[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => {
+ headers.push(new Headers(init.headers));
+ if (headers.length === 1) return new Promise<Response>((_resolve, reject) => {
+ init.signal.addEventListener('abort', () => reject(new DOMException('Fixture cancellation', 'AbortError')), { once: true });
+ });
+ return response();
+ }));
+ const config = { provider: 'openai-codex', model: 'gpt-6.1-sol', apiKey: token('a'), userCacheKey: 'opaque-user-a' };
+ const p = createPiProvider(config), abort = new AbortController();
+ const pending = p.complete({ ...request('chat-a'), signal: abort.signal, onRequestFailure() {} });
+ const rejected = expect(pending).rejects.toBeDefined();
+ await vi.waitFor(() => expect(headers).toHaveLength(1));
+ await p.complete(request('chat-b'));
+ abort.abort();
+ await rejected;
+ await p.complete(request('chat-a'));
+ expect(headers.map(h => h.get('session-id'))).toEqual(Array(3).fill('opaque-user-a'));
+ expect(headers.map(h => h.get('x-client-request-id'))).toEqual(['chat-a', 'chat-b', 'chat-a']);
+ });
+
+ it('does not add affinity to calls without a session or caching explicitly disabled', async () => {
+ const headers: Headers[] = [];
+ const bodies: any[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (input, init) => {
+ headers.push(new Headers(init?.headers)); bodies.push(await wirePayload(input, init)); return response();
+ }));
+ const config = { provider: 'openai-codex', model: 'gpt-6.1-sol', apiKey: token('a'), userCacheKey: 'opaque-user-a' };
+ const p = createPiProvider(config);
+ await p.complete({ ...request('unused'), sessionId: undefined });
+ await p.complete({ ...request('disabled'), cacheRetention: 'none' });
+ expect(headers.map(h => h.get('session-id'))).toEqual([null, null]);
+ expect(bodies.map(body => body.prompt_cache_key)).toEqual([undefined, undefined]);
+ });
+
+ it.each((['complete', 'stream'] as const).flatMap(method => (['static', 'dynamic'] as const).map(source => [method, source] as const)))
+ ('maps Responses cache identity without replacing mixed-case %s/%s authentication headers', async (method, source) => {
+ const headers: Headers[] = [], payloads: any[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => { headers.push(new Headers(init.headers)); return response(); }));
+ for (const sessionId of ['chat-a', 'chat-b']) {
+ const config = { provider: 'openai', model: 'gpt-6.1-sol', apiKey: 'fixture', userCacheKey: 'opaque-user-a',
+ ...(source === 'static' ? { headers: { SeSsIoN_Id: 'login-credential' } }
+ : { requestHeaders: () => ({ SeSsIoN_Id: 'login-credential' }) }),
+ onPayload: (value: unknown) => { payloads.push(structuredClone(value)); } };
+ const p = createPiProvider(config);
+ if (method === 'complete') await p.complete(request(sessionId));
+ else for await (const event of p.stream(request(sessionId))) if (event.type === 'error') throw event.error;
+ }
+ expect(payloads.map(p => p.prompt_cache_key)).toEqual(['opaque-user-a', 'opaque-user-a']);
+ expect(headers.map(h => h.get('session_id'))).toEqual(['login-credential', 'login-credential']);
+ expect(headers.map(h => h.get('x-client-request-id'))).toEqual(['chat-a', 'chat-b']);
+ });
+
+ it('preserves other protocols and their caller-supplied headers', async () => {
+ const headers: Headers[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => { headers.push(new Headers(init.headers)); return response(); }));
+ await createPiProvider({ provider: 'openai', model: 'gpt-6.1-sol', apiKey: 'fixture',
+ headers: { 'session-id': 'caller-owned' },
+ }).complete(request('openai-chat'));
+ expect(headers[0].get('session-id')).toBe('caller-owned');
+ });
+});
diff --git a/src/core-agent/test/config.test.ts b/src/core-agent/test/config.test.ts
index e0527a73..1fb56f6a 100644
--- a/src/core-agent/test/config.test.ts
+++ b/src/core-agent/test/config.test.ts
@@ -24,6 +24,18 @@ describe("Config", () => {
expect(config.agent.maxRetries).toBe(3);
});
+ it("fills nested defaults for omitted sections and older partial configuration files", () => {
+ for (const input of [{}, { agent: {}, models: {}, evolution: {} }, { evolution: { metacognition: {} } }]) {
+ const config = createConfig(input);
+ expect(config.models).toEqual({ providers: {}, catalog: {} });
+ expect(config.agent).toMatchObject({ maxRetries: 3, maxToolLoops: 500, toolIdleTimeoutMs: 1_800_000 });
+ expect(config.evolution).toEqual({
+ enabled: true, skillsDir: 'skills', maxSkills: 200, maxSkillContentLength: 100_000,
+ metacognition: { enabled: true, competenceCharLimit: 3000, strategiesCharLimit: 2500 },
+ });
+ }
+ });
+
it("strips a legacy memory-engine section from older config files", () => {
// The retrieval-engine config was removed with the unwired engine
// (2026-08-16); a config.json written by an older build must still load.
diff --git a/src/core-agent/test/providers.test.ts b/src/core-agent/test/providers.test.ts
index 71e29799..f8984c57 100644
--- a/src/core-agent/test/providers.test.ts
+++ b/src/core-agent/test/providers.test.ts
@@ -13,6 +13,43 @@ import { MIRRORED_PI_AI_SERIALIZER_VERSION, restoreHostMessageRoles } from "../s
import * as fs from "node:fs";
describe("Providers (pi-ai backed)", () => {
+ it.each(['complete', 'stream'] as const)('rejects malformed final tool arguments before returning an executable %s result', async (method) => {
+ const modelId = 'google/gemini-3.8-flash';
+ let rawArguments = '{"private_fixture":';
+ const fetchStub = vi.fn(async () => {
+ const frame = { id: 'fixture', model: modelId, choices: [{ index: 0,
+ delta: { role: 'assistant', tool_calls: [{ index: 0, id: 'call_bad', type: 'function', function: { name: 'lookup', arguments: rawArguments } }] },
+ finish_reason: 'tool_calls' }] };
+ return new Response(`data: ${JSON.stringify(frame)}\n\ndata: [DONE]\n\n`, { headers: { 'content-type': 'text/event-stream' } });
+ });
+ vi.stubGlobal('fetch', fetchStub);
+ try {
+ const provider = createPiProvider({ provider: 'openrouter', model: modelId, apiKey: 'fixture' });
+ const params: CompletionParams = { messages: [{ role: 'user', content: [{ type: 'text', text: 'probe' }] }],
+ tools: [{ name: 'lookup', description: 'Lookup', inputSchema: { type: 'object', properties: {} } }],
+ onRequestFailure() {},
+ };
+ if (method === 'complete') {
+ await expect(provider.complete(params)).rejects.toThrow('Invalid tool call arguments');
+ } else {
+ const events: StreamEvent[] = [];
+ let failure: unknown;
+ try { for await (const event of provider.stream(params)) events.push(event); } catch (error) { failure = error; }
+ expect(events.some(event => event.type === 'message_end')).toBe(false);
+ expect(failure).toBeInstanceOf(ProviderError);
+ expect(String(failure)).toContain('Invalid tool call arguments');
+ expect(String(failure)).not.toContain('private_fixture');
+ }
+ expect(fetchStub).toHaveBeenCalledTimes(1);
+ // An intentionally empty object remains a valid call; no silent retry.
+ rawArguments = '{}';
+ expect((await provider.complete(params)).content).toEqual([
+ expect.objectContaining({ type: 'tool_use', name: 'lookup', input: {} }),
+ ]);
+ expect(fetchStub).toHaveBeenCalledTimes(2);
+ } finally { vi.unstubAllGlobals(); }
+ });
+
it.each([
['complete', 'openai-completions'], ['stream', 'openai-completions'],
['complete', 'openai-responses'], ['stream', 'openai-responses'],
@@ -1027,9 +1064,11 @@ describe("Providers (pi-ai backed)", () => {
stream: true,
prompt_cache_key: "gpt6-payload-contract",
prompt_cache_options: { ttl: "30m" },
- reasoning: { effort: "low", summary: "auto" },
+ reasoning: { effort: "medium", summary: "auto" },
tools: [{ type: "function", name: "read_file" }],
});
+ // pi-ai keeps the prefix stable; the native update carries the requested effort.
+ expect(payloads[0].input.at(-1)).toMatchObject({ type: "configuration_update", reasoning: { effort: "low" } });
expect(payloads[0].prompt_cache_retention).toBeUndefined();
expect(JSON.stringify(payloads[0].input)).toContain("input_image");
@@ -1220,6 +1259,11 @@ describe("Providers (pi-ai backed)", () => {
["openrouter", "openai/gpt-6.1-sol", "openai"],
["openrouter", "anthropic/claude-opus-5", "anthropic"],
["google", "gemini-3.8-flash", "google"],
+ ["google", "gemini-4-argon", "google"],
+ ["kimi-coding", "k3", "openai"],
+ ["kimi-coding", "k3-256k", "openai"],
+ ["kimi-coding", "kimi-for-coding", "openai"],
+ ["openrouter", "google/gemini-4-argon", "openai"],
["openrouter", "google/gemini-3.8-flash", "openai"],
])("round-trips %s %s through a fragmented tool stream and its follow-up result", async (providerId, modelId, protocol) => {
const originalFetch = globalThis.fetch;
@@ -1272,7 +1316,9 @@ describe("Providers (pi-ai backed)", () => {
tools: [{ name: "lookup", description: "Look up an item", inputSchema: { type: "object", properties: { item: { type: "string" } }, required: ["item"] } }],
messages: [
{ role: "user", content: [{ type: "text", text: "Check this image" }, { type: "image", data: "aGVsbG8=", mediaType: "image/png" }] },
- { role: "assistant", content: [{ type: "tool_use", id: "call_lookup", name: "lookup", input: { item: "sample" } }] },
+ { role: "assistant", content: [{ type: "tool_use", id: "call_lookup", name: "lookup", input: { item: "sample" },
+ ...(providerId === "kimi-coding" ? { replayOrigin: replayOrigin({ api: "anthropic-messages", provider: providerId, id: modelId }), thoughtSignature: "legacy-signature" } : {}),
+ }] },
{ role: "user", content: [{ type: "tool_result", toolUseId: "call_lookup", content: "item found" }] },
],
};
@@ -1334,8 +1380,14 @@ describe("Providers (pi-ai backed)", () => {
expect.objectContaining({ functionResponse: expect.objectContaining({ name: "lookup", response: { output: "follow-up found" } }) }),
]));
} else {
- expect(url).toBe("https://openrouter.ai/api/v1/chat/completions");
- expect(body).toMatchObject({ model: modelId, reasoning: { effort: "low" } });
+ if (providerId === "kimi-coding") {
+ expect(url).toBe("https://api.kimi.com/coding/v1/chat/completions");
+ expect(body).toMatchObject({ model: modelId, reasoning_effort: "low" });
+ expect(JSON.stringify(body)).not.toContain("legacy-signature");
+ } else {
+ expect(url).toBe("https://openrouter.ai/api/v1/chat/completions");
+ expect(body).toMatchObject({ model: modelId, reasoning: { effort: "low" } });
+ }
expect(body.messages[0].content).toContainEqual({ type: "image_url", image_url: { url: "data:image/png;base64,aGVsbG8=" } });
expect(body.messages[1].tool_calls[0]).toMatchObject({ id: "call_lookup", function: { name: "lookup", arguments: '{"item":"sample"}' } });
expect(body.messages[2]).toMatchObject({ role: "tool", tool_call_id: "call_lookup", content: "item found" });
@@ -2125,9 +2177,10 @@ describe("Providers (pi-ai backed)", () => {
});
describe("pi-provider wrapError", () => {
- it('keeps embedded accepted output out of context-overflow request recovery', () => {
+ it.each(['context_length_exceeded', 'exceed_context_size_error'])(
+ 'keeps embedded accepted output out of context-overflow request recovery (%s)', code => {
const original = Object.assign(new Error('Provider rejected the response'), {
- status: 400, error: { code: 'context_length_exceeded' },
+ status: 400, error: { type: code },
cause: { code: 'PROVIDER_OUTPUT_UNCERTAIN' },
});
const wrapped = wrapErrorForTest(original, 'custom');
@@ -2135,6 +2188,46 @@ describe("pi-provider wrapError", () => {
expect(wrapped).not.toBeInstanceOf(ContextOverflowError);
expect(classifyRetryableError(wrapped)).toBeNull();
});
+
+ it.each([
+ ['complete', 400], ['stream', 400], ['complete', 200], ['stream', 200],
+ ] as const)('recognizes local context overflow through the real SDK (%s / HTTP %i)', async (method, status) => {
+ // llama.cpp's protocol uses a numeric code and a machine-readable type.
+ // The message deliberately contains no context-overflow wording.
+ const rejection = { code: 400, type: 'exceed_context_size_error', message: 'Request rejected',
+ n_prompt_tokens: 16764, n_ctx: 8192 };
+ const fetchStub = vi.fn(async () => new Response(status === 400
+ ? JSON.stringify({ error: rejection }) : `data: ${JSON.stringify({ error: rejection })}\n\n`, {
+ status, headers: { 'content-type': status === 400 ? 'application/json' : 'text/event-stream' },
+ }));
+ vi.stubGlobal('fetch', fetchStub);
+ try {
+ const provider = createPiProvider({ provider: 'custom', apiKey: 'fixture', customModel: {
+ id: 'fixture', name: 'fixture', provider: 'custom', api: 'openai-completions',
+ baseUrl: 'https://fixture.invalid/v1', reasoning: false, input: ['text'],
+ contextWindow: 131072, maxTokens: 32768,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ } });
+ const onRequestFailure = vi.fn();
+ const params: CompletionParams = { messages: [{ role: 'user', content: [{ type: 'text', text: 'probe' }] }],
+ onRequestFailure };
+ let delivered: unknown;
+ if (method === 'complete') {
+ delivered = await provider.complete(params).then(() => undefined, error => error);
+ } else {
+ const events: StreamEvent[] = [];
+ for await (const event of provider.stream(params)) events.push(event);
+ const errors = events.filter(event => event.type === 'error');
+ expect(errors).toHaveLength(1);
+ delivered = errors[0].error;
+ expect(events.some(event => event.type === 'message_end' || event.type === 'tool_use_start')).toBe(false);
+ }
+ expect(delivered).toBeInstanceOf(ContextOverflowError);
+ expect(classifyRetryableError(delivered)).toBeNull();
+ expect(fetchStub).toHaveBeenCalledTimes(1);
+ expect(onRequestFailure).toHaveBeenCalledTimes(1);
+ } finally { vi.unstubAllGlobals(); }
+ });
it.each(['complete', 'stream'] as const)('retains host budget errors before SDK flattening in %s', async method => {
const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});
const fetchStub = vi.fn(async () => { throw new Error('must not access network'); });
@@ -2199,6 +2292,10 @@ describe("pi-provider wrapError", () => {
});
it.each([
+ 'exceed_context_size_error',
+ '400: {"error":{"type":"exceed_context_size_error"}}',
+ 'Diagnostic example: exceed_context_size_error',
+ '400: {"error":{"type":"exceed_context_size_error"}',
'400: {"code":"invalid_request_error","message":"context_length_exceeded"}',
'400: {"message":"The request context is too long"}',
'400: {"code":"context_length_exceeded"',
@@ -2209,6 +2306,15 @@ describe("pi-provider wrapError", () => {
expect(wrapErrorForTest(original, "openai")).toBe(original);
});
+ it('does not infer local context overflow from an error message or an unrelated field', () => {
+ const original = Object.assign(new ProviderError('exceed_context_size_error', 'custom', 400), {
+ error: { type: 'invalid_request_error', message: 'exceed_context_size_error',
+ param: 'exceed_context_size_error' },
+ });
+ expect(wrapErrorForTest(original, 'custom')).toBe(original);
+ expect(classifyRetryableError(original)).toBeNull();
+ });
+
it("preserves a statusless authentication diagnostic without inventing its type", () => {
const streamError = new ProviderError("The authentication token is expired", "openai-codex");
const wrapped = wrapErrorForTest(streamError, "openai-codex");
diff --git a/src/main/features/client_config.ts b/src/main/features/client_config.ts
index 1a7c5b63..f3488029 100644
--- a/src/main/features/client_config.ts
+++ b/src/main/features/client_config.ts
@@ -12,7 +12,7 @@ import { getActiveUserId, hasActiveUser } from './users';
import { userRemoteConfigFile } from '../paths';
import { readJsonSync, writeJsonSync } from '../storage';
import { createLogger } from '../logger';
-import { fetchWithRetry } from '../util/retry';
+import { fetchAndReadWithRetry } from '../util/retry';
import { desktopPlatform } from '../system_info';
import { withCommonHeaders } from './api_common';
import { DEFAULT_RETRY_ERROR_POLICY, type RetryErrorPolicyConfig } from '../../core-agent/src/shared/errors';
@@ -54,6 +54,7 @@ type ElectronPowerMonitorLike = {
};
const log = createLogger('client-config');
+const CLIENT_CONFIG_REFRESH_TIMEOUT_MS = 60_000;
const REFRESH_INTERVAL_MS = 4 * 60 * 60 * 1000;
const PROD_DEFAULT = 'https://orkas.ai/api';
@@ -468,7 +469,19 @@ export async function refresh(
const { app } = runtimeApp ? { app: runtimeApp } : await loadElectronRuntime();
const headers: Record<string, string> = withCommonHeaders();
if (current.etag) headers['If-None-Match'] = current.etag;
- const res = await fetchWithRetry('client-config:refresh', buildUrl(app), { method: 'GET', headers });
+ const { response: res, body: parsed } = await fetchAndReadWithRetry(
+ 'client-config:refresh', buildUrl(app), { method: 'GET', headers },
+ async (response, signal) => {
+ if (!response.ok) return { ok: true, body: {} as Record<string, unknown> } as const;
+ try { return { ok: true, body: await response.json() as Record<string, unknown> } as const; }
+ catch (error) {
+ if (signal?.aborted) throw error;
+ // Keep malformed-body failures outside transport retries.
+ return { ok: false, error } as const;
+ }
+ },
+ { timeoutMs: CLIENT_CONFIG_REFRESH_TIMEOUT_MS },
+ );
const etag = res.headers.get('etag') || '';
if (activeConfigOwner() !== requestOwner) {
log.info('stale client config response ignored after account switch', { reason });
@@ -480,7 +493,8 @@ export async function refresh(
return { updated: false, notModified: true };
}
if (!res.ok) throw new Error(`HTTP ${res.status}`);
- const body = await res.json() as Record<string, unknown>;
+ if (!parsed.ok) throw parsed.error;
+ const body = parsed.body;
if (body.code !== 0) throw new Error(String(body.msg || body.code || 'client config error'));
return { updated: applyPayload(body, etag) };
} catch (err) {
diff --git a/src/main/model/core-agent/managed-connection-liveness.ts b/src/main/model/core-agent/managed-connection-liveness.ts
deleted file mode 100644
index 17982e87..00000000
--- a/src/main/model/core-agent/managed-connection-liveness.ts
+++ /dev/null
@@ -1,123 +0,0 @@
-/**
- * Connection liveness for Orkas managed-model requests.
- *
- * A half-open connection (proxy/VPN node switch, network change) delivers no
- * bytes and no close, which is indistinguishable from a model that is still
- * thinking when only parsed SDK events are observed. Orkas Server targets
- * headers or a controlled error with a 90s relay-opening budget, then
- * writes an SSE keepalive comment every 45s that the SDK discards. Watching raw
- * bytes here separates the two cases:
- *
- * - no response headers within MANAGED_HEADER_TIMEOUT_MS, or
- * - no byte at all (keepalives included) for MANAGED_BODY_IDLE_TIMEOUT_MS
- * on an event stream
- *
- * fails the request with an undici-style transport timeout code, so the
- * existing retry/rotation owner handles it exactly like any other dropped
- * connection. User cancellation keeps its own abort semantics. The Agent idle
- * budget and retry policy are unchanged.
- */
-import { createLogger } from '../../logger';
-
-const log = createLogger('managed-connection-liveness');
-
-/** Server opening budget (90s) plus nominal upload/preparation margin. */
-export const MANAGED_HEADER_TIMEOUT_MS = 105_000;
-/** Three Server keepalive intervals (45s). */
-export const MANAGED_BODY_IDLE_TIMEOUT_MS = 135_000;
-
-function transportTimeout(code: 'UND_ERR_HEADERS_TIMEOUT' | 'UND_ERR_BODY_TIMEOUT', message: string): Error {
- return Object.assign(new Error(message), { code });
-}
-
-export function withManagedConnectionLiveness(next: typeof globalThis.fetch): typeof globalThis.fetch {
- return async (input, init) => {
- const callerSignal = init?.signal ?? undefined;
- if (callerSignal?.aborted) return next(input, init);
- const controller = new AbortController();
- let failure: Error | undefined;
- const forwardAbort = () => controller.abort(callerSignal?.reason);
- callerSignal?.addEventListener('abort', forwardAbort, { once: true });
- const detach = () => callerSignal?.removeEventListener('abort', forwardAbort);
- const fail = (error: Error) => {
- if (failure || controller.signal.aborted) return;
- failure = error;
- log.warn('managed model connection considered lost', { error_code: (error as { code?: string }).code });
- controller.abort(error);
- };
-
- let headerTimer: ReturnType<typeof setTimeout> | undefined;
- let response: Response;
- try {
- response = await new Promise<Response>((resolve, reject) => {
- headerTimer = setTimeout(() => {
- fail(transportTimeout('UND_ERR_HEADERS_TIMEOUT', 'Orkas model response headers did not arrive'));
- reject(failure);
- }, MANAGED_HEADER_TIMEOUT_MS);
- next(input, { ...init, signal: controller.signal }).then(resolve, reject);
- });
- } catch (error) {
- detach();
- throw failure ?? error;
- } finally {
- clearTimeout(headerTimer);
- }
-
- const isEventStream = response.headers.get('content-type')?.toLowerCase().includes('text/event-stream');
- if (!response.body) {
- detach();
- return response;
- }
-
- const reader = response.body.getReader();
- let stream!: ReadableStreamDefaultController<Uint8Array>;
- let idleTimer: ReturnType<typeof setTimeout> | undefined;
- let settled = false;
- const finish = () => {
- settled = true;
- clearTimeout(idleTimer);
- detach();
- };
- const arm = () => {
- if (!isEventStream) return;
- clearTimeout(idleTimer);
- idleTimer = setTimeout(() => {
- if (settled) return;
- fail(transportTimeout('UND_ERR_BODY_TIMEOUT', 'Orkas model stream stopped sending data'));
- finish();
- stream.error(failure);
- void reader.cancel(failure).catch(() => {});
- }, MANAGED_BODY_IDLE_TIMEOUT_MS);
- };
- const body = new ReadableStream<Uint8Array>({
- start(controllerForStream) {
- stream = controllerForStream;
- arm();
- },
- async pull() {
- try {
- const part = await reader.read();
- if (settled) return;
- if (part.done) {
- finish();
- stream.close();
- return;
- }
- arm();
- stream.enqueue(part.value);
- } catch (error) {
- if (settled) return;
- finish();
- stream.error(failure ?? error);
- }
- },
- async cancel(reason) {
- finish();
- await reader.cancel(reason).catch(() => {});
- },
- }, { highWaterMark: 0 });
- const observed = new Response(body, { status: response.status, statusText: response.statusText, headers: response.headers });
- for (const name of ['url', 'redirected', 'type'] as const) Object.defineProperty(observed, name, { value: response[name] });
- return observed;
- };
-}
diff --git a/src/main/model/core-agent/rotating-provider.ts b/src/main/model/core-agent/rotating-provider.ts
index 4907bb7c..8a642ff5 100644
--- a/src/main/model/core-agent/rotating-provider.ts
+++ b/src/main/model/core-agent/rotating-provider.ts
@@ -81,10 +81,13 @@ export const PROVIDER_EMPTY_UNKNOWN_CODE = 'PROVIDER_EMPTY_UNKNOWN';
export const PROVIDER_CANDIDATES_UNAVAILABLE_CODE = 'PROVIDER_CANDIDATES_UNAVAILABLE';
class ProviderCandidatesUnavailableError extends Error {
- readonly code = PROVIDER_CANDIDATES_UNAVAILABLE_CODE;
+ readonly code: string;
- constructor() {
- super('No configured model candidate is currently available');
+ constructor(requireImageInput = false) {
+ super(requireImageInput
+ ? 'No available configured model can accept the required image input'
+ : 'No configured model candidate is currently available');
+ this.code = requireImageInput ? 'UNSUPPORTED_MODEL' : PROVIDER_CANDIDATES_UNAVAILABLE_CODE;
this.name = 'ProviderCandidatesUnavailableError';
}
}
@@ -222,6 +225,8 @@ export interface CreateRotatingProviderConfig {
/** Ordered list of candidates. First entry is the primary (matches
* `pickChatEntryGroup()[0]`). Further entries are fallbacks. */
candidates: RotatingCandidate[];
+ /** Library image understanding cannot proceed after dropping its pixels. */
+ requireImageInput?: boolean;
/** Called after the wrapper commits to a candidate (first content event
* yielded successfully). Used by callers to clear cooldown on the
* winning profile and bump `lastUsed`. */
@@ -293,8 +298,13 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
const rateLimitIdFor = (candidate: RotatingCandidate): string => (
JSON.stringify([candidate.profileId, candidate.providerId, candidate.modelId])
);
+ const lacksRequiredImages = (candidate: RotatingCandidate): boolean => (
+ config.requireImageInput === true
+ && (candidate.maxInputImages === 0 || candidate.imageSupport?.unsupported === true)
+ );
const isUnavailable = (candidate: RotatingCandidate): boolean => (
- unavailableProfiles.has(cooldownIdFor(candidate))
+ lacksRequiredImages(candidate)
+ || unavailableProfiles.has(cooldownIdFor(candidate))
|| rateLimitedCandidates.has(rateLimitIdFor(candidate))
);
const markUnavailable = (candidate: RotatingCandidate, kind: KeyFailureKind, reason: string): void => {
@@ -356,6 +366,7 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
* `provider_default`, omit a call-specific cap, and keep that omission
* across fallback. */
function paramsFor(cand: RotatingCandidate, params: CompletionParams): CompletionParams {
+ if (lacksRequiredImages(cand)) throw new ProviderCandidatesUnavailableError(config.requireImageInput);
const next = { ...params, model: cand.modelId,
...(config.onRequestFailure ? { onRequestFailure: config.onRequestFailure } : {}),
};
@@ -759,8 +770,11 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
name: providerId.charAt(0).toUpperCase() + providerId.slice(1),
async complete(params: CompletionParams): Promise<CompletionResult> {
+ if (config.requireImageInput && !params.messages.some(message => messageImageCount(message) > 0)) {
+ throw new ProviderCandidatesUnavailableError(true);
+ }
if (terminalRateLimit) throw terminalRateLimit;
- let lastErr: unknown = new ProviderCandidatesUnavailableError();
+ let lastErr: unknown = new ProviderCandidatesUnavailableError(config.requireImageInput);
let lastCand: RotatingCandidate | null = null;
let lastFailure: PreCommitFailure | null = null;
let exhaustedNetwork = false;
@@ -816,6 +830,7 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
}
observeFailure(cand, params, err);
lastErr = err;
+ if (lacksRequiredImages(cand)) break;
const failure = err instanceof ProviderEmptyResponseError && err.kind === 'normal_end_empty'
? null : classifyPreCommitFailure(err);
if (!failure) {
@@ -870,8 +885,11 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
},
async *stream(params: CompletionParams): AsyncIterable<StreamEvent> {
+ if (config.requireImageInput && !params.messages.some(message => messageImageCount(message) > 0)) {
+ throw new ProviderCandidatesUnavailableError(true);
+ }
if (terminalRateLimit) throw terminalRateLimit;
- let lastErr: unknown = new ProviderCandidatesUnavailableError();
+ let lastErr: unknown = new ProviderCandidatesUnavailableError(config.requireImageInput);
let lastCand: RotatingCandidate | null = null;
let lastFailure: PreCommitFailure | null = null;
let exhaustedNetwork = false;
@@ -903,6 +921,10 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
attempt = await streamOne(cand, params);
if (!('rotatable' in attempt)) break;
+ if (config.requireImageInput) {
+ observeFailure(cand, params, attempt.err);
+ if (lacksRequiredImages(cand)) break;
+ }
if (attempt.empty) {
yield {
@@ -982,6 +1004,7 @@ export function createRotatingProvider(config: CreateRotatingProviderConfig): LL
if ('rotatable' in attempt) {
observeFailure(cand, params, attempt.err);
lastErr = attempt.err;
+ if (lacksRequiredImages(cand)) continue;
if (!attempt.rotatable || (normalEmptyRecoveryActive && attempt.failure.kind !== 'rate_limit')) {
// Non-rotatable failure before any content event → this
// candidate is what the user sees in the surfaced error.
diff --git a/src/main/model/provider_catalog.ts b/src/main/model/provider_catalog.ts
index e0935cc0..bf87dbaa 100644
--- a/src/main/model/provider_catalog.ts
+++ b/src/main/model/provider_catalog.ts
@@ -102,7 +102,7 @@ export const CATALOG: readonly CatalogEntry[] = [
// Moonshot has two independently-billed endpoints:
// - `moonshot` → https://api.moonshot.cn/v1 (OpenAI-compatible,
// pay-as-you-go open platform).
- // - `kimi-coding` → https://api.kimi.com/coding (Anthropic protocol,
+ // - `kimi-coding` → https://api.kimi.com/coding/v1 (OpenAI-compatible,
// monthly subscription for Kimi Coding only).
// Both bind to the same Moonshot account but have separate auth and
// quota — a single key cannot be used on both. The UI shows them as
@@ -383,6 +383,23 @@ function cloneModelWithId(
return applyConfiguredModelOverrides(cloned, overrides);
}
+/** Temporary metadata for the official Flash id until pi-ai includes it.
+ * M3 supplies the regional transport and input limits, not Flash's pricing or
+ * thinking controls. Exact future SDK entries bypass this fallback.
+ * https://platform.minimax.io/docs/api-reference/text-anthropic-api
+ */
+function applyMinimaxFlashFallback(model: Model<Api>, providerId: string): Model<Api> {
+ if (!['minimax-cn', 'minimax-portal', 'minimax-portal-cn'].includes(providerId)
+ || model.id !== 'MiniMax-M3.1-Flash' || model.api !== 'anthropic-messages') return model;
+ return {
+ ...model,
+ reasoning: true,
+ cost: { input: 0.15, output: 0.6, cacheRead: 0.015, cacheWrite: 0 },
+ thinkingLevelMap: { off: null, minimal: 'low', low: 'low', medium: 'medium', high: 'high', xhigh: 'xhigh', max: 'max' },
+ compat: { ...model.compat, forceAdaptiveThinking: true, supportsTemperature: false },
+ };
+}
+
function hasConfiguredModelOverrides(configured: ProviderModelEntry | undefined): boolean {
return !!configured
&& (
@@ -489,7 +506,7 @@ export function resolveConfiguredPiModel(
|| PI_MODEL_TEMPLATE_ALIAS_LABEL[requestedProviderId]?.[requestedModelId]
|| requestedModelId;
return {
- model: cloneModelWithId(template, requestedModelId, name, configured),
+ model: applyMinimaxFlashFallback(cloneModelWithId(template, requestedModelId, name, configured), requestedProviderId),
requestedProviderId,
requestedModelId,
catalogProviderId,
diff --git a/src/main/model/public_model_catalog.ts b/src/main/model/public_model_catalog.ts
index 3dcfe0d2..255982c2 100644
--- a/src/main/model/public_model_catalog.ts
+++ b/src/main/model/public_model_catalog.ts
@@ -79,6 +79,7 @@ export const PUBLIC_PROVIDER_MODELS: Readonly<Record<string, readonly ProviderMo
],
google: [
{ id: 'gemini-3.8-flash', name: 'Gemini 3.8 Flash', maxInputImages: 20 },
+ { id: 'gemini-4-argon', name: 'Gemini 4 Argon', maxInputImages: 20 },
{ id: 'gemini-3.7-flash', name: 'Gemini 3.7 Flash', maxInputImages: 20 },
{ id: 'gemini-3.1-pro-preview', name: 'Gemini 3.1 Pro (preview)', maxInputImages: 20 },
{ id: 'gemini-3.5-flash-lite', name: 'Gemini 3.5 Flash Lite', maxInputImages: 20 },
@@ -98,16 +99,19 @@ export const PUBLIC_PROVIDER_MODELS: Readonly<Record<string, readonly ProviderMo
{ id: 'k2p7', name: 'Kimi K2.7 Code', hiddenFromPicker: true },
],
'minimax-cn': [
+ { id: 'MiniMax-M3.1-Flash', name: 'MiniMax M3.1 Flash', template: 'MiniMax-M3', contextWindow: 1_000_000, maxTokens: 131072, maxInputImages: 20 },
{ id: 'MiniMax-M3', name: 'MiniMax 3' },
- { id: 'MiniMax-M2.7', name: 'MiniMax 2.7' },
+ { id: 'MiniMax-M2.7', name: 'MiniMax 2.7', hiddenFromPicker: true },
],
'minimax-portal': [
+ { id: 'MiniMax-M3.1-Flash', name: 'MiniMax M3.1 Flash', template: 'MiniMax-M3', contextWindow: 1_000_000, maxTokens: 131072, maxInputImages: 20 },
{ id: 'MiniMax-M3', name: 'MiniMax 3' },
- { id: 'MiniMax-M2.7', name: 'MiniMax 2.7' },
+ { id: 'MiniMax-M2.7', name: 'MiniMax 2.7', hiddenFromPicker: true },
],
'minimax-portal-cn': [
+ { id: 'MiniMax-M3.1-Flash', name: 'MiniMax M3.1 Flash', template: 'MiniMax-M3', contextWindow: 1_000_000, maxTokens: 131072, maxInputImages: 20 },
{ id: 'MiniMax-M3', name: 'MiniMax 3' },
- { id: 'MiniMax-M2.7', name: 'MiniMax 2.7' },
+ { id: 'MiniMax-M2.7', name: 'MiniMax 2.7', hiddenFromPicker: true },
],
deepseek: [
{ id: 'deepseek-flash', name: 'DeepSeek V4.1 Flash', contextWindow: 1_000_000, maxTokens: 128_000, supportsVision: true, maxInputImages: 600 },
@@ -143,6 +147,7 @@ export const PUBLIC_PROVIDER_MODELS: Readonly<Record<string, readonly ProviderMo
{ id: 'openai/gpt-5.6-terra', name: 'GPT-5.6 Terra', template: 'openai/gpt-5.5', maxInputImages: 20 },
{ id: 'openai/gpt-5.6-luna', name: 'GPT-5.6 Luna', template: 'openai/gpt-5.5', maxInputImages: 20 },
{ id: 'google/gemini-3.8-flash', name: 'Gemini 3.8 Flash', maxInputImages: 20 },
+ { id: 'google/gemini-4-argon', name: 'Gemini 4 Argon', maxInputImages: 20 },
{ id: 'google/gemini-3.7-flash', name: 'Gemini 3.7 Flash', maxInputImages: 20 },
{ id: 'google/gemini-3.1-pro-preview', name: 'Gemini 3.1 Pro (preview)', maxInputImages: 20 },
{ id: 'google/gemini-3.5-flash-lite', name: 'Gemini 3.5 Flash Lite', maxInputImages: 20 },
diff --git a/test/main/features/client_config.test.ts b/test/main/features/client_config.test.ts
index e4890d67..3982aac0 100644
--- a/test/main/features/client_config.test.ts
+++ b/test/main/features/client_config.test.ts
@@ -22,6 +22,7 @@ vi.mock('electron', () => ({
import {
ClientConfigManager,
+ clientConfig,
DEFAULT_PROVIDER_MODELS,
clientConfigPlatform,
refresh,
@@ -498,3 +499,128 @@ describe('client_config', () => {
}
});
});
+
+
+describe('client config response ownership and deadline', () => {
+ afterEach(() => {
+ stop();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ });
+
+ it.each(['local-profile', 'legacy-anonymous'] as const)('discards a body completed after switching to %s and permits a fresh owner request', async destination => {
+ const users = await import('../../../src/main/features/users');
+ const paths = await import('../../../src/main/paths');
+ const oldUid = `clientconfigbodyold-${destination}`;
+ const newUid = destination === 'legacy-anonymous' ? users.ANONYMOUS_LOCAL_ID : 'clientconfigbodynew';
+ const manager = new ClientConfigManager();
+ users.activateUser(newUid);
+ manager.applyServerPayload({ immediate: { 'feature.owner': 'keep-new-owner' }, restart: {} }, 'new-owner-etag');
+ const newFile = paths.userRemoteConfigFile(newUid);
+ const before = fs.readFileSync(newFile);
+ users.activateUser(oldUid);
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const response = new Response(JSON.stringify({
+ code: 0, immediate: { 'feature.owner': 'obsolete-owner' }, restart: {}, config_hash: 'obsolete-owner-hash',
+ }), { headers: { etag: 'obsolete-owner-etag' } });
+ const read = response.json.bind(response);
+ const json = vi.spyOn(response, 'json').mockImplementation(async () => { await gate; return read(); });
+ const fetchMock = vi.fn(async () => response);
+ vi.stubGlobal('fetch', fetchMock);
+ const changed = vi.fn();
+ const unsubscribe = clientConfig.subscribeAll(changed);
+ let pending: ReturnType<typeof refresh> | undefined;
+ try {
+ pending = refresh('manual', { force: true });
+ await waitForCondition(() => json.mock.calls.length > 0);
+ expect(json).toHaveBeenCalledOnce();
+ users.activateUser(newUid);
+ release();
+ const result = await pending;
+ expect({ result, newOwnerCacheUnchanged: fs.readFileSync(newFile).equals(before), notifications: changed.mock.calls.length })
+ .toEqual({ result: { updated: false, skipped: true, stale: true }, newOwnerCacheUnchanged: true, notifications: 0 });
+ users.activateUser(oldUid);
+ expect(manager.get('feature.owner')).toBeUndefined();
+ users.activateUser(newUid);
+ fetchMock.mockImplementation(async () => new Response(JSON.stringify({
+ code: 0, immediate: { 'feature.owner': 'fresh-new-owner' }, restart: {}, config_hash: 'fresh-owner-hash',
+ }), { headers: { etag: 'fresh-owner-etag' } }));
+ await refresh('manual', { force: true });
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(manager.get('feature.owner')).toBe('fresh-new-owner');
+ expect(manager.readCache().etag).toBe('fresh-owner-etag');
+ } finally {
+ release(); await pending; unsubscribe();
+ for (const uid of [oldUid, newUid]) fs.rmSync(path.dirname(paths.userRemoteConfigFile(uid)), { recursive: true, force: true });
+ }
+ });
+
+ it('keeps the cached value after malformed JSON without introducing body-parse retries', async () => {
+ const users = await import('../../../src/main/features/users');
+ const paths = await import('../../../src/main/paths');
+ const uid = 'clientconfigbodymalformed';
+ users.activateUser(uid);
+ const manager = new ClientConfigManager();
+ manager.applyServerPayload({ immediate: { 'feature.owner': 'keep-cached' }, restart: {} }, 'cached-etag');
+ const fetchMock = vi.fn(async () => new Response('{broken'));
+ vi.stubGlobal('fetch', fetchMock);
+ try {
+ expect(await refresh('manual', { force: true })).toMatchObject({ updated: false, error: expect.any(String) });
+ expect(fetchMock).toHaveBeenCalledOnce();
+ expect(manager.get('feature.owner')).toBe('keep-cached');
+ expect(manager.readCache().etag).toBe('cached-etag');
+ } finally {
+ fs.rmSync(path.dirname(paths.userRemoteConfigFile(uid)), { recursive: true, force: true });
+ }
+ });
+
+ it('keeps the request deadline active while a response body stalls and recovers on its bounded retry', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
+ const users = await import('../../../src/main/features/users');
+ const paths = await import('../../../src/main/paths');
+ const uid = 'clientconfigbodydeadline';
+ users.activateUser(uid);
+ let bodyController: ReadableStreamDefaultController<Uint8Array> | undefined;
+ let firstSignal: AbortSignal | undefined;
+ let bodyAborts = 0;
+ const fetchMock = vi.fn()
+ .mockImplementationOnce(async (_input: unknown, init: RequestInit) => {
+ firstSignal = init.signal as AbortSignal;
+ return new Response(new ReadableStream<Uint8Array>({
+ start(controller) {
+ bodyController = controller;
+ firstSignal!.addEventListener('abort', () => {
+ bodyAborts += 1;
+ controller.error(firstSignal!.reason);
+ }, { once: true });
+ },
+ }));
+ })
+ .mockImplementation(async () => new Response(JSON.stringify({
+ code: 0, immediate: { 'feature.deadline': 'recovered' }, restart: {}, config_hash: 'deadline-recovered',
+ })));
+ vi.stubGlobal('fetch', fetchMock);
+ let pending: ReturnType<typeof refresh> | undefined;
+ try {
+ pending = refresh('manual', { force: true });
+ await waitForCondition(() => fetchMock.mock.calls.length > 0);
+ expect(fetchMock).toHaveBeenCalledOnce();
+ await vi.advanceTimersByTimeAsync(59_999);
+ expect(bodyAborts).toBe(0);
+ await vi.advanceTimersByTimeAsync(1);
+ expect(bodyAborts).toBe(1);
+ expect(firstSignal?.aborted).toBe(true);
+ await vi.advanceTimersByTimeAsync(500);
+ await expect(pending).resolves.toEqual({ updated: true });
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(new ClientConfigManager().get('feature.deadline')).toBe('recovered');
+ } finally {
+ bodyController?.error(new Error('fixture cleanup'));
+ await vi.advanceTimersByTimeAsync(10_000);
+ await pending;
+ vi.useRealTimers();
+ fs.rmSync(path.dirname(paths.userRemoteConfigFile(uid)), { recursive: true, force: true });
+ }
+ });
+});
diff --git a/test/main/model/core-agent/provider-cache-contract.test.ts b/test/main/model/core-agent/provider-cache-contract.test.ts
new file mode 100644
index 00000000..e6d8f673
--- /dev/null
+++ b/test/main/model/core-agent/provider-cache-contract.test.ts
@@ -0,0 +1,125 @@
+import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
+import fs from 'node:fs';
+import * as zlib from 'node:zlib';
+import { getBuiltinModel } from '@earendil-works/pi-ai/providers/all';
+import type { Api, Model } from '@earendil-works/pi-ai';
+import { createPiProvider } from '../../../../src/core-agent/src/providers/pi-provider.js';
+import { PUBLIC_PROVIDER_MODELS } from '../../../../src/main/model/public_model_catalog';
+import { CATALOG } from '../../../../src/main/model/provider_catalog';
+import type { CompletionParams } from '../../../../src/core-agent/src/providers/base.js';
+
+import { buildCustomAnthropicModel, buildCustomOpenAICompatibleModel,
+ buildDeepSeekModel, buildDoubaoModel, buildRuntimeMoonshotModel, buildOrkasApiModel,
+} from '../../../../src/main/model/core-agent/external-providers';
+
+const jwt = `fixture.${Buffer.from(JSON.stringify({ 'https://api.openai.com/auth': {
+ chatgpt_account_id: 'fixture-account',
+} })).toString('base64')}.fixture`;
+const config = { baseUrl: 'https://fixture.invalid/v1', contextWindow: 128000, maxTokens: 4096 };
+const builtin = (provider: string, id?: string): Model<Api> => {
+ const model = getBuiltinModel(provider as any, (id ?? PUBLIC_PROVIDER_MODELS[provider]?.[0]?.id) as any);
+ if (!model) throw new Error(`Missing fixture model: ${provider}`);
+ return model;
+};
+const scenarios: Array<[string, () => Model<Api> | Promise<Model<Api>>]> = [
+ ['codex', () => builtin('openai-codex', 'gpt-6.1-sol')],
+ ['openai', () => builtin('openai', 'gpt-6.1-sol')],
+ ['openrouter-openai', () => builtin('openrouter', 'openai/gpt-6.1-sol')],
+ ['openrouter-claude', () => builtin('openrouter', 'anthropic/claude-opus-5')],
+ ['claude', () => builtin('anthropic')],
+ ['gemini', () => builtin('google')],
+ ['glm', () => builtin('zai')],
+ ['kimi-coding', () => builtin('kimi-coding', 'kimi-for-coding')],
+ ['moonshot', () => buildRuntimeMoonshotModel('kimi-k3')],
+ ['doubao', () => buildDoubaoModel('doubao-seed-2-1-pro-260915')],
+ ['deepseek', () => buildDeepSeekModel('deepseek-v4-flash')],
+ ['minimax-cn', () => builtin('minimax-cn', 'MiniMax-M3')],
+ ['minimax-global-oauth', () => ({ ...builtin('minimax', 'MiniMax-M3'), provider: 'minimax-portal' })],
+ ['minimax-cn-oauth', () => ({ ...builtin('minimax-cn', 'MiniMax-M3'), provider: 'minimax-portal-cn' })],
+ ['orkas-api', () => buildOrkasApiModel('orkas-llm-1.5-pro')],
+ ['custom-chat', () => buildCustomOpenAICompatibleModel('fixture', config)],
+ ['custom-claude', () => buildCustomAnthropicModel('fixture', config)],
+];
+const report: unknown[] = [];
+afterEach(() => vi.unstubAllGlobals());
+afterAll(() => {
+ if (process.env.ORKAS_CACHE_AUDIT_OUTPUT) fs.writeFileSync(process.env.ORKAS_CACHE_AUDIT_OUTPUT,
+ JSON.stringify({ boundary: 'real-sdk-serialization-zero-network', scenarios: report }, null, 2));
+});
+
+describe('Provider cache and conversation isolation contracts', () => {
+ it('covers every built-in provider card and public custom routes', async () => {
+ const models = await Promise.all(scenarios.map(([, build]) => build()));
+ expect(new Set(models.map(model => model.provider))).toEqual(new Set([
+ ...CATALOG.map(entry => entry.id),
+ ]));
+ });
+ it.each(scenarios.flatMap(([id, build]) => (['short', 'long'] as const)
+ .flatMap(retention => (['complete', 'stream'] as const).map(method => [id, retention, method, build] as const))))
+ ('%s / %s / %s keeps final wire input independent of cache/session identity', async (id, retention, method, build) => {
+ const model = await build();
+ const captures: Array<{ payload: any; headers?: Headers }> = [];
+ let current: { payload: any; headers?: Headers };
+ // The real SDK serializes and reaches the transport; the explicit stop
+ // forbids paid traffic. This does not simulate cache hits or acceptance.
+ vi.stubGlobal('fetch', vi.fn(async (input, init) => {
+ const wire = new Request(input, init);
+ current.headers = wire.headers;
+ const bytes = Buffer.from(await wire.arrayBuffer());
+ const body = wire.headers.get('content-encoding') === 'zstd' ? zlib.zstdDecompressSync(bytes) : bytes;
+ current.payload = JSON.parse(body.toString('utf8'));
+ throw new Error('Expected zero-network transport stop');
+ }));
+ for (const sessionId of ['fixture-chat-a', 'fixture-chat-b']) {
+ current = { payload: undefined };
+ const providerConfig = { provider: model.provider, apiKey: model.api === 'openai-codex-responses' ? jwt : 'sk-fixture',
+ userCacheKey: 'opaque-user-fixture',
+ customModel: model,
+ };
+ const p = createPiProvider(providerConfig);
+ const params: CompletionParams = { model: model.id, sessionId, cacheRetention: retention, onRequestFailure() {},
+ systemPrompt: 'Stable reference', messages: [{ role: 'user', content: [{ type: 'text', text: 'Read the reference' }] }],
+ };
+ if (method === 'complete') await p.complete(params).catch(() => undefined);
+ else for await (const _event of p.stream(params)) { /* Expected transport stop. */ }
+ expect(current.payload, 'Decode the final wire body, including Codex compression').toBeDefined();
+ expect(current.headers, 'The outbound transport must be intercepted').toBeDefined();
+ captures.push(current);
+ }
+ expect(globalThis.fetch).toHaveBeenCalledTimes(2);
+ const semantic = (payload: any) => {
+ const copy = structuredClone(payload);
+ delete copy.prompt_cache_key;
+ return copy;
+ };
+ expect(semantic(captures[0].payload)).toEqual(semantic(captures[1].payload));
+ expect(JSON.stringify(semantic(captures[0].payload))).not.toContain('fixture-chat-');
+ const a = captures[0].headers!, b = captures[1].headers!;
+ if (model.api === 'openai-codex-responses') {
+ expect(a.get('session-id')).toBe(b.get('session-id'));
+ expect(a.get('session-id')).not.toBe('fixture-chat-a');
+ expect(a.get('x-client-request-id')).not.toBe(b.get('x-client-request-id'));
+ } else expect(a.has('session-id') || b.has('session-id')).toBe(false);
+ if (model.provider === 'openrouter') {
+ expect(a.get('x-session-id')).toBe('fixture-chat-a');
+ expect(b.get('x-session-id')).toBe('fixture-chat-b');
+ }
+ if (['custom-chat', 'deepseek'].includes(id)) {
+ expect(captures[0].payload.prompt_cache_key).toBeUndefined();
+ expect(captures[0].payload.prompt_cache_retention).toBeUndefined();
+ }
+ if (captures[0].payload.prompt_cache_key !== undefined) {
+ expect(captures.map(c => c.payload.prompt_cache_key)).toEqual(['opaque-user-fixture', 'opaque-user-fixture']);
+ }
+ report.push({ id, retention, method, provider: model.provider, model: model.id, api: model.api,
+ semanticInputStable: true,
+ bodyKeyChanges: captures[0].payload.prompt_cache_key !== captures[1].payload.prompt_cache_key,
+ legacyRetention: captures[0].payload.prompt_cache_retention ?? null,
+ cacheOptions: captures[0].payload.prompt_cache_options ?? null,
+ changedHeaders: ['session-id', 'session_id', 'x-session-id', 'x-client-request-id']
+ .filter(name => a.get(name) !== b.get(name)),
+ stableHeaders: ['session-id', 'session_id', 'x-session-id']
+ .filter(name => a.has(name) && a.get(name) === b.get(name)),
+ });
+ });
+});
diff --git a/test/main/model/core-agent/request-capacity-consistency.test.ts b/test/main/model/core-agent/request-capacity-consistency.test.ts
new file mode 100644
index 00000000..c942173e
--- /dev/null
+++ b/test/main/model/core-agent/request-capacity-consistency.test.ts
@@ -0,0 +1,224 @@
+import { describe, expect, it } from 'vitest';
+import {
+ AgentRunner, createConfig, defineTool, ProviderRegistry, Session,
+ estimateCompletionRequestInputTokens,
+ type CompletionParams, type CompletionResult, type LLMProvider, type StreamEvent,
+} from '#core-agent';
+import { createRotatingProvider } from '../../../../src/main/model/core-agent/rotating-provider';
+
+const window = 131072;
+const output = 32768;
+const ceiling = 80609;
+const done: CompletionResult = {
+ model: 'fixture', content: [{ type: 'text', text: 'Finished' }], stopReason: 'end_turn',
+ usage: { inputTokens: 50000, outputTokens: 1, totalTokens: 50001 },
+};
+async function drain<T>(stream: AsyncIterable<T>): Promise<T[]> {
+ const events: T[] = [];
+ for await (const event of stream) events.push(event);
+ return events;
+}
+function harness(session: Session, backend: LLMProvider, tools: ConstructorParameters<typeof AgentRunner>[0]['tools'] = []) {
+ const prepared: CompletionParams[] = [];
+ const failures: any[] = [];
+ const rotating = createRotatingProvider({
+ providerId: 'fixture', networkRetryAttempts: 0,
+ candidates: [{ profileId: 'fixture', providerId: 'fixture', modelId: 'fixture',
+ contextWindow: window, maxTokens: output, build: async () => backend }],
+ onRequestFailure: failure => failures.push(failure),
+ });
+ const providers = new ProviderRegistry();
+ providers.registerFactory('fixture', () => ({
+ ...rotating,
+ async *stream(params: CompletionParams) {
+ prepared.push(params);
+ yield* rotating.stream(params);
+ },
+ }));
+ const runner = new AgentRunner({
+ session, providers, tools,
+ config: createConfig({
+ agent: { defaultProvider: 'fixture', defaultModel: 'fixture', maxRetries: 0 },
+ evolution: { enabled: false },
+ models: { catalog: { fixture: { provider: 'fixture', model: 'fixture',
+ contextWindow: window, maxOutputTokens: output } } },
+ }),
+ });
+ return { runner, prepared, failures };
+}
+function backend(calls: CompletionParams[]): LLMProvider {
+ return {
+ id: 'fixture', name: 'Fixture',
+ async complete(params) { calls.push(params); return done; },
+ async *stream(params) {
+ calls.push(params);
+ yield { type: 'text_delta', text: 'Finished' };
+ yield { type: 'message_end', ...done };
+ },
+ async validateAuth() { return true; },
+ };
+}
+
+// A fitting task must survive preparation and public provider forwarding
+// without losing receipts or adding compaction/retry calls. Release-side
+// candidate capacity rejection is not enabled by the public provider policy.
+describe('request capacity consistency', () => {
+ it('remeasures changed thinking even when the request and block objects are reused', () => {
+ const thinking = { type: 'thinking' as const, thinking: 't'.repeat(80000) };
+ const request: CompletionParams = { model: 'fixture', messages: [{ role: 'assistant', content: [thinking] }] };
+ const before = estimateCompletionRequestInputTokens(request);
+ expect(before).toBeLessThan(ceiling);
+ expect(estimateCompletionRequestInputTokens(request)).toBe(before);
+ thinking.thinking = 't'.repeat(400000);
+ expect(estimateCompletionRequestInputTokens(request)).toBeGreaterThan(ceiling);
+ });
+
+ it('sends a calibrated fitting request instead of rejecting the same prepared input', async () => {
+ const session = new Session();
+ session.beginUserTurn([{ type: 'text', text: 'a'.repeat(332000) }]);
+ session.setEstimatorCalibration(3, 4);
+ const calls: CompletionParams[] = [];
+ const { runner, prepared, failures } = harness(session, backend(calls));
+ const events = await drain(runner.runStream({ message: 'Resume', resumeActiveTurn: true }));
+ expect(prepared[0].requestMetadata?.contextBudgetDiagnostics).toMatchObject({ emergency_result: 'not_needed' });
+ expect(events.find(event => event.type === 'done')?.result.meta.error).toBeUndefined();
+ expect(calls).toHaveLength(1);
+ expect(failures).toEqual([]);
+ expect(JSON.stringify(calls[0].messages)).toContain('a'.repeat(332000));
+ expect(events.some(event => event.type === 'retry' || event.type === 'compaction')).toBe(false);
+ expect((calls[0].requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens)
+ .toBe((prepared[0].requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens);
+ });
+
+ it('uses reported usage after a tool round while retaining original thinking and the tool receipt', async () => {
+ const calls: CompletionParams[] = [];
+ const receipt = 'r'.repeat(14000);
+ const thinking = 't'.repeat(60000);
+ const realBackend = backend(calls);
+ realBackend.stream = async function* (params): AsyncIterable<StreamEvent> {
+ calls.push(params);
+ if (calls.length === 1) {
+ yield { type: 'message_end', model: 'fixture', stopReason: 'tool_use',
+ usage: { inputTokens: 50000, outputTokens: 15000, totalTokens: 65000 },
+ content: [{ type: 'thinking', thinking },
+ { type: 'tool_use', id: 'read-once', name: 'inspect', input: {} }] };
+ } else {
+ yield { type: 'text_delta', text: 'Finished' };
+ yield { type: 'message_end', ...done };
+ }
+ };
+ let executions = 0;
+ const tool = defineTool({ name: 'inspect', description: 'Read the next record', inputSchema: { type: 'object' },
+ async execute() { executions++; return { content: receipt }; } });
+ const { runner, prepared, failures } = harness(new Session(), realBackend, [tool]);
+ const result = await runner.run({ message: 'a'.repeat(280000) });
+ expect(result.meta.error).toBeUndefined();
+ expect(result.text).toBe('Finished');
+ expect(calls).toHaveLength(2);
+ expect(executions).toBe(1);
+ expect(failures).toEqual([]);
+ expect(calls[1].messages.some(message => message.content.some(block => block.type === 'thinking' && block.thinking === thinking))).toBe(true);
+ expect(JSON.stringify(calls[1].messages)).toContain(receipt);
+ const preparedBudget = prepared[1].requestMetadata!.contextBudgetDiagnostics as any;
+ const sentBudget = calls[1].requestMetadata!.contextBudgetDiagnostics as any;
+ expect(preparedBudget.estimated_input_tokens).toBeLessThan(ceiling);
+ expect(sentBudget.estimated_input_tokens).toBe(preparedBudget.estimated_input_tokens);
+ expect(sentBudget.estimate_source).toBe('anchored');
+ });
+
+ it('includes retained thinking in preparation and forwards it intact when nothing can be dropped', async () => {
+ const session = new Session();
+ session.beginUserTurn([{ type: 'text', text: 'a'.repeat(248000) }]);
+ session.addAssistantMessage([{ type: 'thinking', thinking: 'b'.repeat(84000) }]);
+ const calls: CompletionParams[] = [];
+ const { runner, prepared, failures } = harness(session, backend(calls));
+ const result = await runner.run({ message: 'Resume', resumeActiveTurn: true });
+ // Public candidate rotation preserves its existing forwarding policy.
+ // Core preparation still prices the full retained thinking and reports
+ // that no allowed context can be dropped; it must not silently lose it.
+ expect(result.meta.error).toBeUndefined();
+ expect(result.text).toBe('Finished');
+ expect(calls).toHaveLength(1);
+ expect(failures).toEqual([]);
+ expect(calls[0].messages.some(message => message.content.some(block =>
+ block.type === 'thinking' && block.thinking === 'b'.repeat(84000)))).toBe(true);
+ const preparation = prepared[0].requestMetadata!.contextBudgetDiagnostics as any;
+ expect(preparation.estimated_input_tokens).toBeGreaterThan(ceiling);
+ expect(preparation.emergency_result).toBe('nothing_to_drop');
+ expect((calls[0].requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens)
+ .toBe(preparation.estimated_input_tokens);
+ });
+
+ it('prices the tools-off boundary control before deciding whether to reduce context', async () => {
+ const calls: CompletionParams[] = [];
+ const transport = backend(calls);
+ transport.stream = async function* (params) {
+ calls.push(params);
+ yield calls.length === 1
+ ? { type: 'message_end', model: 'fixture', stopReason: 'tool_use',
+ usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
+ content: [{ type: 'tool_use', id: 'boundary', name: 'check', input: {} }] }
+ : { type: 'message_end', ...done };
+ };
+ const check = defineTool({ name: 'check', description: 'Check required input', inputSchema: { type: 'object' },
+ async execute() { return { content: 'Awaiting required user input.', synthesizeAndEndTurn: true }; } });
+ const { runner, prepared } = harness(new Session(), transport, [check]);
+ expect((await runner.run({ message: 'Check and report the result', turnEphemeral: 'Current task constraints' })).meta.error).toBeUndefined();
+ expect(calls).toHaveLength(2);
+ expect(calls[1].tools).toBeUndefined();
+ expect(calls[1].messages.some(message => message.role === 'developer')).toBe(true);
+ const budget = prepared[1].requestMetadata!.contextBudgetDiagnostics as any;
+ expect(budget.emergency_before_tokens).toBe(Math.round(estimateCompletionRequestInputTokens(calls[1])));
+ expect(budget.estimated_input_tokens).toBe(budget.emergency_before_tokens);
+ });
+
+ it('recomputes the measured request after a checkpoint and preserves the latest tool receipt', async () => {
+ const session = new Session();
+ session.beginUserTurn([{ type: 'text', text: 'a'.repeat(140000) }]);
+ for (let i = 0; i < 8; i++) {
+ session.addAssistantMessage([{ type: 'tool_use', id: `old-${i}`, name: 'inspect', input: {} }]);
+ session.addToolResult(`old-${i}`, 'r'.repeat(30000));
+ }
+ session.setEstimatorCalibration(3, 4);
+ const calls: CompletionParams[] = [];
+ const { runner, prepared, failures } = harness(session, backend(calls));
+ const result = await runner.run({ message: 'Resume', resumeActiveTurn: true });
+ expect(result.meta.error).toBeUndefined();
+ const main = prepared.filter(request => request.requestMetadata?.contextBudgetDiagnostics);
+ expect(main).toHaveLength(1);
+ const before = main[0].requestMetadata!.contextBudgetDiagnostics as any;
+ expect(before.compaction_attempts).toBeGreaterThan(0);
+ expect(before.compaction_after_tokens).toBeLessThan(before.compaction_before_tokens);
+ // The final call is the main inference after the checkpoint completed;
+ // compare its prepared budget and retained receipt.
+ const sent = calls.at(-1)!;
+ expect((sent.requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens).toBe(before.emergency_after_tokens);
+ expect(JSON.stringify(sent.messages)).toContain('old-7');
+ expect(failures).toEqual([]);
+ });
+
+ it.each(['stream', 'complete'] as const)('preserves unchanged cloned prepared request calibration in %s', async mode => {
+ const session = new Session();
+ session.beginUserTurn([{ type: 'text', text: 'a'.repeat(332000) }]);
+ session.setEstimatorCalibration(3, 4);
+ const calls: CompletionParams[] = [];
+ const { runner } = harness(session, backend(calls));
+ expect((await runner.run({ message: 'Resume', resumeActiveTurn: true })).meta.error).toBeUndefined();
+ const request = calls[0];
+ // A request snapshot is data, so request-recording providers can clone it.
+ // Both public APIs must still forward this unchanged, calibrated request.
+ const { signal: _signal, onRequestFailure: _observer, ...cloneable } = request;
+ const cloned = structuredClone(cloneable);
+ const unchangedCalls: CompletionParams[] = [];
+ const unchanged = createRotatingProvider({ providerId: 'fixture', candidates: [{
+ profileId: 'fixture', providerId: 'fixture', modelId: 'fixture',
+ contextWindow: window, maxTokens: output, build: async () => backend(unchangedCalls),
+ }] });
+ await (mode === 'stream' ? drain(unchanged.stream(cloned)) : unchanged.complete(cloned));
+ expect(unchangedCalls).toHaveLength(1);
+ expect((unchangedCalls[0].requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens)
+ .toBe((request.requestMetadata!.contextBudgetDiagnostics as any).estimated_input_tokens);
+ // Changed-candidate capacity admission is a release provider policy;
+ // this public wrapper has not enabled that local rejection strategy.
+ });
+});
diff --git a/test/main/model/core-agent/rotating-provider.test.ts b/test/main/model/core-agent/rotating-provider.test.ts
index afd72b69..f9d90b96 100644
--- a/test/main/model/core-agent/rotating-provider.test.ts
+++ b/test/main/model/core-agent/rotating-provider.test.ts
@@ -1,6 +1,6 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
-import { classifyTransientNetworkError } from '../../../../src/core-agent/src/shared/errors';
+import { classifyRetryableError, classifyTransientNetworkError } from '../../../../src/core-agent/src/shared/errors';
import {
boundMessagesForImageLimit,
createRotatingProvider,
@@ -10,7 +10,7 @@ import {
type RotatingCandidate,
} from '../../../../src/main/model/core-agent/rotating-provider';
import type { LLMProvider, StreamEvent, CompletionParams, CompletionResult } from '#core-agent';
-import { createPiProvider, AgentRunner, createConfig, defineTool, ProviderError, ProviderRegistry } from '#core-agent';
+import { createPiProvider, AgentRunner, createConfig, defineTool, ContextOverflowError, ProviderError, ProviderRegistry } from '#core-agent';
import { _clearAll, getCooldown } from '../../../../src/main/model/core-agent/profile-cooldown';
import { normalizeElectronNetFetchError } from '../../../../src/main/util/proxy-dispatcher';
@@ -71,6 +71,82 @@ const PARAMS: CompletionParams = {
messages: [{ role: 'user', content: [{ type: 'text', text: 'ping' }] }],
};
+describe('recovery of actual upstream rejections', () => {
+ it.each(['complete', 'stream'] as const)('leaves local context overflow to Runner without rotating or cooling down (%s)', async method => {
+ _clearAll();
+ const fetchStub = vi.fn(async () => new Response(JSON.stringify({ error: {
+ code: 400, type: 'exceed_context_size_error', message: 'Request rejected',
+ n_prompt_tokens: 16764, n_ctx: 8192,
+ } }), { status: 400, headers: { 'content-type': 'application/json' } }));
+ vi.stubGlobal('fetch', fetchStub);
+ const fallbackBuild = vi.fn(async () => fakeProvider('fallback', {}));
+ const onCandidateChosen = vi.fn();
+ try {
+ const provider = createRotatingProvider({ providerId: 'custom', networkRetryDelayMs: () => 0, onCandidateChosen,
+ candidates: [{ profileId: 'primary', providerId: 'custom', modelId: 'fixture',
+ build: async () => createPiProvider({ provider: 'custom', apiKey: 'fixture', customModel: {
+ id: 'fixture', name: 'fixture', api: 'openai-completions', provider: 'custom',
+ baseUrl: 'https://primary.invalid/v1', reasoning: false, input: ['text'],
+ contextWindow: 131072, maxTokens: 32768,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ } }),
+ }, { profileId: 'fallback', providerId: 'custom', modelId: 'fixture', build: fallbackBuild }],
+ });
+ if (method === 'complete') await expect(provider.complete(PARAMS)).rejects.toBeInstanceOf(ContextOverflowError);
+ else await expect(collect(provider.stream(PARAMS))).rejects.toBeInstanceOf(ContextOverflowError);
+ expect(fetchStub).toHaveBeenCalledTimes(1);
+ expect(fallbackBuild).not.toHaveBeenCalled();
+ expect(onCandidateChosen).toHaveBeenCalledExactlyOnceWith({
+ profileId: 'primary', providerId: 'custom', modelId: 'fixture',
+ });
+ expect(getCooldown('primary')).toBeUndefined();
+ } finally { vi.unstubAllGlobals(); _clearAll(); }
+ });
+});
+
+describe('HTTP 451 terminal handling', () => {
+ it.each([
+ ['complete', 'openai-completions'], ['stream', 'openai-completions'],
+ ['complete', 'openai-responses'], ['stream', 'openai-responses'],
+ ] as const)('stops %s via %s without retry, rotation or cooldown and allows a fresh request', async (method, api) => {
+ _clearAll();
+ const warning = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ const requests: string[] = [];
+ const recoveryEvents: StreamEvent[] = [];
+ vi.stubGlobal('fetch', vi.fn(async (input) => {
+ requests.push(String(input).includes('primary.invalid') ? 'primary' : 'fallback');
+ return new Response(null, { status: 451 });
+ }));
+ const create = () => createRotatingProvider({ providerId: 'custom', networkRetryDelayMs: () => 0,
+ candidates: ['primary', 'fallback'].map(name => ({ profileId: name, providerId: 'custom', modelId: 'fixture',
+ build: async () => createPiProvider({ provider: 'custom', apiKey: 'fixture', customModel: {
+ id: 'fixture', name: 'fixture', api, provider: 'custom', baseUrl: `https://${name}.invalid/v1`,
+ reasoning: false, input: ['text'], contextWindow: 128000, maxTokens: 4096,
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ } }),
+ })),
+ });
+ const invoke = async () => {
+ const provider = create();
+ if (method === 'complete') return provider.complete(PARAMS);
+ for await (const event of provider.stream(PARAMS)) {
+ if (event.type === 'retry' || event.type === 'provider_fallback') recoveryEvents.push(event);
+ }
+ };
+ try {
+ await expect(invoke()).rejects.toMatchObject({ statusCode: 451 });
+ expect(requests).toEqual(['primary']);
+ expect(recoveryEvents).toEqual([]);
+ expect(getCooldown('primary')).toBeUndefined();
+ expect(getCooldown('fallback')).toBeUndefined();
+ await expect(invoke()).rejects.toMatchObject({ statusCode: 451 });
+ expect(requests).toEqual(['primary', 'primary']);
+ expect(warning.mock.calls).toHaveLength(2);
+ expect(warning.mock.calls.every(([, , facts]) => facts?.httpStatus === 451)).toBe(true);
+ } finally { vi.unstubAllGlobals(); warning.mockRestore(); _clearAll(); }
+ });
+});
+
describe('rotating-provider › successful streams', () => {
beforeEach(() => _clearAll());
@@ -1439,6 +1515,84 @@ describe('rotating-provider › exhausted stream candidates', () => {
describe('rotating-provider › cross-provider fallback', () => {
beforeEach(() => _clearAll());
+ it.each(['complete', 'stream'] as const)('requires image input in %s without calling text-only candidates', async method => {
+ const textBuild = vi.fn(async () => fakeProvider('text', {
+ completeResult: { content: [{ type: 'text', text: 'blind answer' }] }, streamEvents: [{ type: 'text_delta', text: 'blind answer' } as any],
+ }));
+ const received: CompletionParams[] = [];
+ const vision: LLMProvider = {
+ id: 'vision', name: 'vision', validateAuth: async () => true,
+ complete: async params => { received.push(params); return { content: [{ type: 'text', text: 'red chart' }] } as any; },
+ async *stream(params) { received.push(params); yield { type: 'text_delta', text: 'red chart' } as any; },
+ };
+ const provider = createRotatingProvider({
+ providerId: 'test', requireImageInput: true,
+ candidates: [
+ { ...candidate('text', {}), maxInputImages: 0, build: textBuild },
+ { ...candidate('vision', {}), maxInputImages: 1, build: async () => vision },
+ { ...candidate('unused', {}), maxInputImages: 1, build: textBuild },
+ ],
+ });
+ const params: CompletionParams = { ...PARAMS, messages: [{ role: 'user', content: [
+ { type: 'image', data: 'red-pixels', mediaType: 'image/png' },
+ ] }] };
+ if (method === 'complete') expect((await provider.complete(params)).content).toEqual([{ type: 'text', text: 'red chart' }]);
+ else expect(await collect(provider.stream(params))).toContainEqual({ type: 'text_delta', text: 'red chart' });
+ expect(textBuild).not.toHaveBeenCalled();
+ expect(received).toHaveLength(1);
+ expect(received[0].messages).toEqual(params.messages);
+ expect(getCooldown('text')).toBeUndefined();
+ });
+
+ it.each(['complete', 'stream'] as const)('fails %s without requests when every configured candidate lacks vision', async method => {
+ const build = vi.fn(async () => fakeProvider('blind', {}));
+ const provider = createRotatingProvider({
+ providerId: 'test', requireImageInput: true,
+ candidates: [
+ { ...candidate('text', {}), maxInputImages: 0, build },
+ { ...candidate('observed', {}), imageSupport: { unsupported: true, observeRejection() {} }, build },
+ ],
+ });
+ const params: CompletionParams = { ...PARAMS, messages: [{ role: 'user', content: [
+ { type: 'image', data: 'red-pixels', mediaType: 'image/png' },
+ ] }] };
+ const error = await (method === 'complete' ? provider.complete(params) : collect(provider.stream(params)))
+ .catch(error => error);
+ expect(error).toMatchObject({ code: 'UNSUPPORTED_MODEL' });
+ expect(classifyRetryableError(error)).toBeNull();
+ expect(build).not.toHaveBeenCalled();
+ });
+
+ it.each(['complete', 'stream'] as const)('keeps pixels when an unknown custom model rejects images during %s', async method => {
+ const { customModelImageSupport } = await import('../../../../src/main/model/core-agent/custom-model-image-support');
+ const imageSupport = customModelImageSupport({ userId: 'vision-test', profileId: method,
+ modelId: 'unknown', apiKey: 'test-only', config: {} });
+ const rejection = Object.assign(new Error('This model does not support image inputs.'), { status: 400 });
+ const primary = vi.fn(async () => fakeProvider('unknown', { throwBefore: rejection, completeError: rejection }));
+ const received: CompletionParams[] = [];
+ const provider = createRotatingProvider({
+ providerId: 'custom', requireImageInput: true,
+ candidates: [
+ { ...candidate('unknown', {}, 'custom'), imageSupport, build: primary },
+ { ...candidate('vision', {}), maxInputImages: 1, build: async () => ({
+ id: 'vision', name: 'vision', validateAuth: async () => true,
+ async complete(params) { received.push(params); return { content: [{ type: 'text', text: 'red' }] } as any; },
+ async *stream(params) { received.push(params); yield { type: 'text_delta', text: 'red' } as any; },
+ }) },
+ ],
+ });
+ const params: CompletionParams = { ...PARAMS, messages: [{ role: 'user', content: [
+ { type: 'image', data: 'red-pixels', mediaType: 'image/png' },
+ ] }] };
+ if (method === 'complete') await provider.complete(params);
+ else await collect(provider.stream(params));
+ expect(primary).toHaveBeenCalledTimes(1);
+ expect(received).toHaveLength(1);
+ expect(received[0].messages).toEqual(params.messages);
+ expect(imageSupport.unsupported).toBe(true);
+ expect(getCooldown('unknown')).toBeUndefined();
+ });
+
it('applies each fallback candidate image limit without mutating caller history', async () => {
const authErr = Object.assign(new Error('401 primary rejected'), { status: 401 });
const received: Array<{ provider: string; images: string[] }> = [];
diff --git a/test/main/model/provider_catalog.test.ts b/test/main/model/provider_catalog.test.ts
index e9aebede..e706250d 100644
--- a/test/main/model/provider_catalog.test.ts
+++ b/test/main/model/provider_catalog.test.ts
@@ -142,7 +142,7 @@ describe('provider_catalog › CURATED_MODELS', () => {
for (const row of rows.filter(m => m.id !== 'k2p7')) {
const resolved = resolveConfiguredPiModel({ getPiModel: getBuiltinModel as any }, 'kimi-coding', row.id);
expect(resolved).toMatchObject({ templateModelId: row.id, isConfiguredFallback: false,
- model: { id: row.id, api: 'anthropic-messages', compat: { forceAdaptiveThinking: true } } });
+ model: { id: row.id, api: 'openai-completions', baseUrl: 'https://api.kimi.com/coding/v1', compat: { requiresReasoningContentOnAssistantMessages: true } } });
}
expect(isSelectableModel('kimi-coding', 'k2p7')).toBe(true);
expect(resolveModelUpgrade('kimi-coding', 'k2p7')).toMatchObject({ modelId: 'k2p7', upgraded: false });
@@ -162,13 +162,14 @@ describe('provider_catalog › CURATED_MODELS', () => {
isConfiguredFallback: false,
needsCustomModel: true,
model: {
- api: 'anthropic-messages',
+ api: 'openai-completions',
+ baseUrl: 'https://api.kimi.com/coding/v1',
reasoning: true,
contextWindow: 1_000_000,
maxTokens: 131_072,
compat: {
- allowEmptySignature: true,
- forceAdaptiveThinking: true,
+ requiresReasoningContentOnAssistantMessages: true,
+ supportsReasoningEffort: true,
},
},
});
@@ -177,6 +178,7 @@ describe('provider_catalog › CURATED_MODELS', () => {
it('minimax catalog exposes M3 before the M2.7 generation', () => {
const directIds = (CURATED_MODELS['minimax-cn'] || []).map((m) => m.id);
expect(directIds).toEqual([
+ 'MiniMax-M3.1-Flash',
'MiniMax-M3',
'MiniMax-M2.7',
]);
@@ -186,6 +188,44 @@ describe('provider_catalog › CURATED_MODELS', () => {
const portalIds = (CURATED_MODELS['minimax-portal-cn'] || []).map((m) => m.id);
expect(portalIds).toEqual(directIds);
+ for (const provider of ['minimax-cn', 'minimax-portal', 'minimax-portal-cn']) {
+ expect(curatedModelsFor(provider).filter(m => !m.hiddenFromPicker).map(m => m.id))
+ .toEqual(['MiniMax-M3.1-Flash', 'MiniMax-M3']);
+ expect(resolveModelUpgrade(provider, 'MiniMax-M2.7').modelId).toBe('MiniMax-M2.7');
+ expect(isSelectableModel(provider, 'MiniMax-M2.7')).toBe(true);
+ }
+ });
+
+ it.each(['minimax-cn', 'minimax-portal', 'minimax-portal-cn'])('runs new Flash through %s with its own protocol metadata', async (providerId) => {
+ const catalog = { getPiModel: (provider: string, id: string) => id === 'MiniMax-M3.1-Flash' ? undefined : (getBuiltinModel as any)(provider, id) };
+ const original = structuredClone(catalog.getPiModel(providerId === 'minimax-portal' ? 'minimax' : 'minimax-cn', 'MiniMax-M3'));
+ const resolved = resolveConfiguredPiModel(catalog, providerId, 'MiniMax-M3.1-Flash');
+ expect(resolved).not.toBeNull();
+ expect(resolved!.model).toMatchObject({ id: 'MiniMax-M3.1-Flash', api: 'anthropic-messages',
+ baseUrl: providerId === 'minimax-portal' ? 'https://api.minimax.io/anthropic' : 'https://api.minimaxi.com/anthropic',
+ contextWindow: 1_000_000, maxTokens: 131072, reasoning: true, input: ['text', 'image'],
+ cost: { input: 0.15, output: 0.6, cacheRead: 0.015, cacheWrite: 0 },
+ });
+ expect(resolved!.model.cost).not.toHaveProperty('tiers');
+ const { createPiProvider } = await import('#core-agent');
+ for (const reasoning of [undefined, 'off', 'low', 'high'] as const) {
+ let payload: any;
+ const provider = createPiProvider({ provider: providerId, apiKey: 'fixture-only', customModel: resolved!.model,
+ onPayload(value) { payload = value; throw new Error('captured-before-transport'); } });
+ await expect(provider.complete({ model: 'MiniMax-M3.1-Flash', reasoning, temperature: 0.25,
+ messages: [{ role: 'user', content: [{ type: 'text', text: 'Answer' }] }], onRequestFailure() {},
+ })).rejects.toThrow('captured-before-transport');
+ expect(payload.model).toBe('MiniMax-M3.1-Flash');
+ expect(payload.thinking?.type).not.toBe('disabled');
+ expect(payload).not.toHaveProperty('temperature');
+ expect(payload).not.toHaveProperty('service_tier');
+ if (reasoning && reasoning !== 'off') {
+ expect(payload.thinking).toMatchObject({ type: 'adaptive' });
+ expect(payload.thinking).not.toHaveProperty('budget_tokens');
+ expect(payload.output_config.effort).toBe(reasoning);
+ }
+ }
+ expect(catalog.getPiModel(providerId === 'minimax-portal' ? 'minimax' : 'minimax-cn', 'MiniMax-M3')).toEqual(original);
});
it('exposes GLM 5.3 and 5.2 as native runnable Z.AI models', () => {
diff --git a/test/main/model/public_model_catalog.test.ts b/test/main/model/public_model_catalog.test.ts
index b77b27b6..af317350 100644
--- a/test/main/model/public_model_catalog.test.ts
+++ b/test/main/model/public_model_catalog.test.ts
@@ -45,6 +45,7 @@ describe('public model catalog', () => {
]);
expect(PUBLIC_PROVIDER_MODELS.google?.map((model) => model.id)).toEqual([
'gemini-3.8-flash',
+ 'gemini-4-argon',
'gemini-3.7-flash',
'gemini-3.1-pro-preview',
'gemini-3.5-flash-lite',
@@ -117,6 +118,7 @@ describe('public model catalog', () => {
'openai/gpt-5.6-terra',
'openai/gpt-5.6-luna',
'google/gemini-3.8-flash',
+ 'google/gemini-4-argon',
'google/gemini-3.7-flash',
'google/gemini-3.1-pro-preview',
'google/gemini-3.5-flash-lite',