Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

92.1 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0

/**
 * The `openai-compatible` provider: any OpenAI-shaped `/chat/completions`
 * endpoint, configured on its own settings keys, with NVIDIA NIM and a handful
 * of other endpoints available as presets.
 *
 * See src/shared/openai-compat-presets.ts for why this is a provider of its own
 * rather than another special case inside the OpenRouter client. The short
 * version: OpenRouter's client sends openrouter.ai attribution headers and
 * carries the cmem.ai gateway's credential-pinning rules, and neither should
 * follow a user to NVIDIA.
 *
 * The body is model, messages, temperature, the output-token cap, the standard
 * OpenAI streaming fields, and `reasoning: { enabled: false }` by default
 * (CLAUDE_MEM_OPENAI_COMPAT_REASONING). A reasoning-by-default model otherwise
 * spends max_tokens thinking and returns empty content (#2995). A strict
 * endpoint that rejects the field (vLLM in particular) is sent the request
 * once more without it. One that refuses the streaming fields is sent the
 * plain body instead (streamed-chat-completion.ts). Token counts are used when
 * the endpoint reports them and are never estimated into the usage event,
 * matching the rule OpenRouter's `buildLastUsage` already enforces: real
 * numbers on both sides or none.
 */

import { getCredential } from '../../shared/EnvManager.js';
import { SettingsDefaultsManager, type SettingsDefaults } from '../../shared/SettingsDefaultsManager.js';
import {
  diagnoseHostModel,
  HOST_MODEL_STATUS_WORDS,
  type HostModelDiagnostic,
  type HostModelFallbackReason,
} from '../../shared/openai-compat-host-diagnosis.js';
import { USER_SETTINGS_PATH, paths } from '../../shared/paths.js';
import { resolveOpenRouterChatCompletionsUrl } from '../../shared/openrouter-base-url.js';
import { keysForEndpoint } from '../../shared/cmem-gateway.js';
import { parseOpenAICompatSessionEndpoints, redactUserinfo, type OpenAICompatSessionEndpoint } from '../../shared/openai-compat-session-endpoints.js';
import { describeNetworkFailure, isConnectionRefused, networkFailureSuffix } from '../../shared/network-failure.js';
import { resolveOpenAICompatPreset, resolvePresetDefaultModel, type OpenAICompatPreset } from '../../shared/openai-compat-presets.js';
import { buildKeyPool, resolvePoolKeys, retryPolicyForPool, withKeyPool, type KeyPoolId } from '../../shared/api-key-pool.js';
import { logger } from '../../utils/logger.js';
import type { ActiveSession, ConversationMessage, PendingMessageWithId } from '../worker-types.js';
import { buildObservationPrompt, renderObservationPrompt, type ObservationPromptParts } from '../../sdk/prompts.js';
import { observationFieldMaxChars } from './context-window.js';
import { boundedInteger } from './CodexAppServerPool.js';
import { sameObservationContext } from './codex-observation-batch.js';
import { DatabaseManager } from './DatabaseManager.js';
import { SessionManager } from './SessionManager.js';
import { createHash, randomUUID } from 'crypto';
import { ClassifiedProviderError, DEFAULT_MODEL_DISABLED_CODE, HOST_MODEL_NOT_SERVED_CODE, OWN_MODEL_NOT_SERVED_CODE, SESSION_ENDPOINT_REFUSED_CODE, isClassified, rateLimitUntilNextKey } from './provider-errors.js';
// Kept as re-exports: existing callers import the codes from this module.
export { DEFAULT_MODEL_DISABLED_CODE, HOST_MODEL_NOT_SERVED_CODE, OWN_MODEL_NOT_SERVED_CODE, SESSION_ENDPOINT_REFUSED_CODE } from './provider-errors.js';
import {
  claimReasoningBudgetRetry,
  combineBilledChatUsage,
  endpointRejectsReasoningField,
  isLengthTruncatedEmptyReply,
  isUnknownReasoningParameter,
  openAICompatReasoningBody,
  type CompatReasoningBody,
  requestDisablesReasoning,
  resolveOpenAICompatReasoningEffort,
  reasoningBudgetGiveUpMessage,
  reasoningBudgetRetryMessage,
  reasoningBudgetSkippedMessage,
  rememberEndpointRejectsReasoningField,
  resolveOpenAICompatReasoning,
  startReasoningRetryDeadline,
} from './reasoning-budget.js';
import type { PaidSendBudget } from './paid-send-budget.js';
import { isContextOverflowBody } from './OpenRouterProvider.js';
import { resolveSummaryTierModel } from './model-aliases.js';
import { namesPeriodRateLimit } from '../../shared/period-rate-limit.js';
import { withRetry, parseRetryAfterMs } from './retry.js';
import { resolveObserverMaxOutputTokens } from './context-window.js';
import {
  STREAMED_REQUEST_FIELDS,
  resolveStreamIdleTimeoutMs,
  resolveStreamLiveness,
  sendChatCompletion,
  streamsChatCompletion,
  type ChatCompletionExchange,
  type StreamLiveness,
} from './streamed-chat-completion.js';
import {
  OpenAICompatibleProvider,
  assistantText,
  type ObserverRequestLabel,
  type OpenAIChatMessage,
  type ProviderQueryResult,
} from './OpenAICompatibleProvider.js';
import {
  extractPromptCacheUsage,
  type OpenAICompatUsagePayload,
} from './observer-usage.js';

const CHARS_PER_TOKEN_ESTIMATE = 4;

interface ChatCompletionResponse {
  model?: string;
  choices?: Array<{
    message?: { role?: string; content?: unknown };
    finish_reason?: string;
  }>;
  usage?: OpenAICompatUsagePayload;
  error?: { message?: string; code?: string | number; type?: string };
}

/** Last prompt for context size; both requests' completion tokens were billed. */
function withCombinedCompatUsage(
  first: ChatCompletionResponse['usage'],
  retry: ChatCompletionResponse,
): ChatCompletionResponse {
  const combined = combineBilledChatUsage(
    {
      promptTokens: first?.prompt_tokens,
      completionTokens: first?.completion_tokens,
      totalTokens: first?.total_tokens,
    },
    {
      promptTokens: retry.usage?.prompt_tokens,
      completionTokens: retry.usage?.completion_tokens,
      totalTokens: retry.usage?.total_tokens,
    },
  );
  const usage: NonNullable<ChatCompletionResponse['usage']> = { ...retry.usage };
  if (combined.promptTokens !== undefined) usage.prompt_tokens = combined.promptTokens;
  if (combined.completionTokens !== undefined) usage.completion_tokens = combined.completionTokens;
  if (combined.totalTokens !== undefined) usage.total_tokens = combined.totalTokens;
  return { ...retry, usage };
}

export interface OpenAICompatConfig {
  apiKey: string;
  /** Rotation pool: `apiKey` followed by CLAUDE_MEM_OPENAI_COMPAT_API_KEYS. */
  apiKeys: string[];
  model: string;
  apiUrl: string;
  preset: OpenAICompatPreset;
  /**
   * Whether this endpoint needs a key, resolved from the endpoint actually
   * being called rather than from the preset's name.
   *
   * A preset carries two things that come apart the moment
   * CLAUDE_MEM_OPENAI_COMPAT_BASE_URL is set: where to send the request, and
   * whether that place wants a bearer token. Keeping the preset's answer after
   * its URL has been replaced breaks both directions — `nvidia-nim` pointed at
   * a local server refuses to run without a key it does not need, and `ollama`
   * pointed at a hosted gateway reports itself ready and then 401s on every
   * observation.
   */
  requiresApiKey: boolean;
  /**
   * Endpoint-ready small model taken from the host's OpenCode config (#6485).
   * Present only when the host gate is on and the host declares a small model;
   * the summary tier uses it after CLAUDE_MEM_TIER_SUMMARY_MODEL and before the
   * observer model.
   */
  summaryModel?: string;
  /**
   * Model ids in this config that the host chose, by the opt-in that took each:
   * the host session's model, and the model and small model of the host's
   * OpenCode config. A request whose `model` is one of them was sent on the
   * host's choice, so a 404 for it is the endpoint refusing that choice, not a
   * wrong configured model.
   */
  hostChosenModels?: Record<string, HostModelSetting>;
  /**
   * Internal: this endpoint was usable, but every model candidate it had was
   * an id it had already refused, so `model` is empty. Not set when nothing was
   * configured, nor when an explicit setting removed the preset default: those
   * are half-configured endpoints that legitimately fall through to Claude.
   * Dispatch keeps the provider selected on this flag alone, and `query()` stops
   * with the classified refusal instead of sending another doomed request.
   */
  allModelsRefused?: boolean;
  /**
   * Internal: the unusable id was an explicit `CLAUDE_MEM_OPENAI_COMPAT_MODEL`
   * this endpoint refused, not a host choice or a configured implicit default.
   * It only selects the refusal's error code and remedy; `allModelsRefused`
   * remains the one flag dispatch reads.
   */
  explicitModelRefused?: boolean;
  /**
   * Internal: the operator deliberately disabled the implicit default with
   * `CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL=none`, no other model is usable,
   * and the endpoint itself is configured. Dispatch keeps the provider
   * selected; `query()` stops before the socket with a classified error.
   */
  defaultDisabled?: boolean;
  /** Set for a Telegram wrap-up request (OpenAICompatibleProvider.formatTelegramWrapup). */
  plainText?: boolean;
  /**
   * Provider id whose CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS entry chose
   * this config's endpoint. Present only when a session mapping applied; it
   * names the provider in the action a refusal shows.
   */
  sessionEndpointProvider?: string;
  /**
   * Internal: the modelMap of the session endpoint that chose this config.
   * The summary tier resolves after config build and reads its own settings
   * first, so it needs the same translation the observer model received; the
   * map is ids only, never a credential.
   */
  sessionModelMap?: Readonly<Record<string, string>>;
  /**
   * Internal: this config pays for a summary turn whose model differs from the
   * observer model. A refusal of that id must name the tier setting, not
   * CLAUDE_MEM_OPENAI_COMPAT_MODEL.
   */
  summaryTurn?: true;
}

/** The opt-ins that take a model id from the host instead of from claude-mem's own settings. */
export type HostModelSetting = 'CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION' | 'CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST';

/** Where each opt-in's model id comes from, as the refusal's remedy says it. */
const HOST_MODEL_ORIGIN: Record<HostModelSetting, string> = {
  CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION: 'the model selected in the host session, which CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION sends as-is, or through its provider\'s CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS entry when mapped',
  CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST: "the model the host's OpenCode config names, which CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST reads",
};

/**
 * Where a refused id has to be served: its provider's mapped entry when one
 * chose this endpoint, otherwise the pinned base URL. The provider id is
 * user-written and may carry URL userinfo, so it is redacted before it reaches
 * an action (which can be logged like any other failure text).
 */
function serveRefusedModelAt(sessionEndpointProvider: string | undefined): string {
  return sessionEndpointProvider
    ? `the endpoint mapped for provider "${redactUserinfo(sessionEndpointProvider)}" in CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS`
    : 'CLAUDE_MEM_OPENAI_COMPAT_BASE_URL';
}

/** Where an id the observer chose itself came from, named on the refusal's WARN line. */
const OWN_MODEL_ORIGIN = 'CLAUDE_MEM_OPENAI_COMPAT_MODEL / CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL / preset default';

/**
 * Model ids the observer endpoint refused, by endpoint URL and id: ids the host
 * chose (CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION / _FROM_HOST) and ids we
 * chose ourselves (an explicit setting, a preset-default override, the preset
 * default). A host id is only right when the host's provider is the same service
 * as the observer endpoint; when it is not, every request for it gets the same
 * refusal and the id comes back with each prompt or turn. An own id can be just
 * as stale. Without this, the first batch of every prompt would be refused and
 * the failure would name the wrong setting.
 *
 * Kept for the worker's life, not on the session: an unrecoverable failure ends
 * the session's live state (handleGeneratorExit), and the next prompt builds a
 * new one. It holds no inference input and sends nothing by itself. A restart
 * forgets it, so an endpoint that starts serving the model is used again then.
 *
 * Not capped. An entry takes one refusal from the user's own endpoint, a session
 * id is at most 200 characters (the session-init schema), and each one is logged
 * at WARN when it is added.
 */
const hostModelsRefusedByEndpoint = new Set<string>();

/** Any id this endpoint refused, whoever chose it. */
function isModelRefused(apiUrl: string, model: string): boolean {
  return hostModelsRefusedByEndpoint.has(`${apiUrl}\n${model}`);
}

/** PR2 alias: every existing caller asks about a host id, but the set also holds ours. */
function isHostModelRefused(apiUrl: string, model: string): boolean {
  return isModelRefused(apiUrl, model);
}

/** The model of this request and the opt-in that took it, when the host chose it; else undefined. */
function hostChosenModelOfRequest(config: OpenAICompatConfig): { id: string; setting: HostModelSetting } | undefined {
  const chosen = config.hostChosenModels;
  return chosen && Object.hasOwn(chosen, config.model) ? { id: config.model, setting: chosen[config.model] } : undefined;
}

/**
 * Stop sending a model id this endpoint refused. Says so once per endpoint and
 * id. `origin` names where the id came from; `log` lets the host wrapper retain
 * PR2's exact WARN wording and fields (which name the host opt-in).
 */
function rememberModelRefused(
  apiUrl: string,
  model: string,
  origin: string,
  log?: { message: string; fields: Record<string, unknown> },
): void {
  if (isModelRefused(apiUrl, model)) return;
  hostModelsRefusedByEndpoint.add(`${apiUrl}\n${model}`);
  const notice = log ?? {
    message: `OpenAI-compatible endpoint does not serve a model id it was given (${origin}); the observer stops sending that id`,
    fields: { model, url: apiUrl, origin },
  };
  logger.warn('SDK', notice.message, notice.fields);
}

/** PR2 wrapper: host refusals keep their own WARN, naming the opt-in that chose the id. */
function rememberHostModelRefused(config: OpenAICompatConfig, setting: HostModelSetting): void {
  rememberModelRefused(config.apiUrl, config.model, setting, {
    message: `${config.preset.label} does not serve a model the host chose; the observer goes back to the configured model for it`,
    fields: {
      model: config.model,
      url: config.apiUrl,
      setting,
      // Which map entry chose this endpoint, so the WARN names the setting to edit.
      ...(config.sessionEndpointProvider ? { provider: redactUserinfo(config.sessionEndpointProvider) } : {}),
    },
  });
}

/** Test seam: forget every refused id, host-chosen and ours. */
export function resetRefusedHostModelsForTesting(): void {
  hostModelsRefusedByEndpoint.clear();
}

/**
 * The last model each endpoint actually answered, by URL. Ids only: no
 * inference input, no disk, no retry store, no startup sweep. One entry per
 * endpoint and at most 64, so a worker that sees many URLs stays bounded.
 */
const MAX_SERVED_MODELS_BY_ENDPOINT = 64;
const servedModelsByEndpoint = new Map<string, string>();

/** Test seam: record the last model an endpoint answered, as a successful query does. */
export function rememberServedModel(apiUrl: string, model: string): void {
  if (!apiUrl || !model) return;
  // A Map keeps insertion order, so the first key is the least recently added
  // endpoint. Updating an existing endpoint does not move it.
  if (!servedModelsByEndpoint.has(apiUrl) && servedModelsByEndpoint.size >= MAX_SERVED_MODELS_BY_ENDPOINT) {
    const oldest = servedModelsByEndpoint.keys().next().value;
    if (oldest) servedModelsByEndpoint.delete(oldest);
  }
  servedModelsByEndpoint.set(apiUrl, model);
}

function lastServedModel(apiUrl: string): string | undefined {
  const model = servedModelsByEndpoint.get(apiUrl);
  // An id the endpoint began refusing cannot stay a fallback, even when it
  // answered successfully earlier.
  return model && !isHostModelRefused(apiUrl, model) ? model : undefined;
}

/** Test seam: forget the last served model per endpoint. */
export function resetServedModelsForTesting(): void {
  servedModelsByEndpoint.clear();
}

/**
 * Session provider → endpoint mappings already announced. An entry takes one
 * INFO line per provider, host model, endpoint model and URL; the Set is capped
 * by evicting the oldest entry, so a worker that resolves many sessions stays
 * bounded. Ids and URLs only: no credential, no request content.
 */
const MAX_SESSION_ENDPOINT_LOG_KEYS = 64;
const loggedSessionEndpoints = new Set<string>();

function logSessionEndpointOnce(provider: string, hostModel: string, endpointModel: string, apiUrl: string): void {
  // A provider id can be forged as a URL; the parser masks userinfo in its own
  // warnings, so this line masks it too, in the field and in the dedupe key.
  const redactedProvider = redactUserinfo(provider);
  const key = `${redactedProvider}\n${hostModel}\n${endpointModel}\n${apiUrl}`;
  if (loggedSessionEndpoints.has(key)) return;
  loggedSessionEndpoints.add(key);
  if (loggedSessionEndpoints.size > MAX_SESSION_ENDPOINT_LOG_KEYS) {
    const oldest = loggedSessionEndpoints.values().next().value;
    if (oldest !== undefined) loggedSessionEndpoints.delete(oldest);
  }
  logger.info('SDK', 'OpenAI-compatible observer follows the session provider mapping', {
    provider: redactedProvider,
    hostModel,
    endpointModel,
    endpoint: apiUrl,
  });
}

/**
 * The raw setting last warned about, so a malformed map is reported once per
 * distinct value rather than on every observation turn. The warning text comes
 * from the parser and names provider ids only.
 */
let lastSessionEndpointWarningRaw: string | null = null;

function warnSessionEndpointSettingOnce(raw: string, warning: string | undefined): void {
  if (!warning || lastSessionEndpointWarningRaw === raw) return;
  lastSessionEndpointWarningRaw = raw;
  logger.warn('SDK', warning);
}

/**
 * Mapped endpoints that refused a session's key or allowance, by provider id
 * and endpoint URL. A refusal there says nothing about the default endpoint,
 * so it is never booked as the provider-wide auth cooldown or quota breaker;
 * instead the mapping is set aside and that provider's sessions go back to the
 * pinned endpoint until the worker restarts. Like the refused model ids it
 * holds no inference input and sends nothing by itself. Bounded by the map
 * (at most MAX_SESSION_ENDPOINTS entries per value) and logged at WARN when
 * added.
 */
const refusedSessionEndpoints = new Set<string>();

/** Kinds that would arm a provider-wide breaker or cooldown in GeneratorRunner. */
const PROVIDER_WIDE_BREAKER_KINDS = new Set(['auth_invalid', 'quota_exhausted', 'rate_limit']);

function isSessionEndpointRefused(provider: string, apiUrl: string): boolean {
  return refusedSessionEndpoints.has(`${provider}\n${apiUrl}`);
}

/**
 * The key pool a request runs against. The default endpoint keeps the
 * `openai-compatible` pool, so existing cooldown files stay valid; a mapped
 * endpoint gets its own pool, keyed by a hash of its URL, so a key it refuses
 * never sits out for the default endpoint.
 */
function keyPoolIdFor(config: OpenAICompatConfig): KeyPoolId {
  if (!config.sessionEndpointProvider) return 'openai-compatible';
  return `openai-compatible:${createHash('sha256').update(config.apiUrl).digest('hex').slice(0, 16)}`;
}

/** Key-binding problems already reported, one line per provider id and reason. */
const warnedSessionEndpointKeyBindings = new Set<string>();

function warnSessionEndpointKeyBindingOnce(provider: string, reason: string): void {
  const redactedProvider = redactUserinfo(provider);
  const key = `${redactedProvider}\n${reason}`;
  if (warnedSessionEndpointKeyBindings.has(key)) return;
  warnedSessionEndpointKeyBindings.add(key);
  if (warnedSessionEndpointKeyBindings.size > MAX_SESSION_ENDPOINT_LOG_KEYS) {
    const oldest = warnedSessionEndpointKeyBindings.values().next().value;
    if (oldest !== undefined) warnedSessionEndpointKeyBindings.delete(oldest);
  }
  logger.warn('SDK', `CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS entry for provider "${redactedProvider}" ${reason}; that provider's sessions stay on the configured endpoint`);
}

/**
 * The entry, when the keys it may receive are settled. With `keyIndexes` the
 * endpoint gets only those pool positions, and at least one must exist. Without
 * it the entry is usable only while the pool holds at most one key: rotation
 * would otherwise send every vendor's key to this one.
 */
function usableSessionEndpoint(
  provider: string,
  endpoint: OpenAICompatSessionEndpoint,
  settings: SettingsDefaults,
): OpenAICompatSessionEndpoint | undefined {
  const poolSize = configuredOpenAICompatKeyPool(settings).length;
  if (endpoint.keyIndexes) {
    if (endpoint.keyIndexes.some(index => index < poolSize)) return endpoint;
    warnSessionEndpointKeyBindingOnce(provider, `names keyIndexes outside the configured key pool of ${poolSize}`);
    return undefined;
  }
  if (poolSize <= 1) return endpoint;
  warnSessionEndpointKeyBindingOnce(provider, `has no keyIndexes while the configured key pool holds ${poolSize} keys`);
  return undefined;
}

/** Test seam: forget the announced mappings, the warned setting value and refused mapped endpoints. */
export function resetSessionEndpointRoutingForTesting(): void {
  loggedSessionEndpoints.clear();
  lastSessionEndpointWarningRaw = null;
  refusedSessionEndpoints.clear();
  warnedSessionEndpointKeyBindings.clear();
}

/**
 * Where the observer's host-configured model may be read from (#6485). The cwd
 * is the observed project: when present, its own OpenCode configs (and its
 * parents') are read as well as the global ones.
 */
export interface OpenAICompatHostOptions {
  cwd?: string;
}

/**
 * True for an endpoint on this machine or this network, where an
 * OpenAI-compatible server (Ollama, LM Studio, a bare vLLM) legitimately
 * accepts any bearer token or none.
 *
 * A remote endpoint is assumed to want a key. That is the safe default: being
 * told to set one is a clear, fixable message, whereas the alternative is a
 * provider that reports itself available and fails every observation with a
 * 401. A remote server that genuinely needs no auth still works — set
 * CLAUDE_MEM_OPENAI_COMPAT_API_KEY to any non-empty value.
 */
export function isLocalEndpointUrl(rawUrl: string): boolean {
  let host: string;
  try {
    host = new URL(rawUrl).hostname.toLowerCase();
  } catch {
    return false;
  }
  // URL() keeps IPv6 hosts in brackets.
  const bare = host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
  if (bare === 'localhost' || bare.endsWith('.localhost') || bare.endsWith('.local')) return true;
  if (bare === '::1' || bare === '0.0.0.0' || bare === '::') return true;
  if (/^127\./.test(bare)) return true;
  if (/^10\./.test(bare)) return true;
  if (/^192\.168\./.test(bare)) return true;
  if (/^172\.(1[6-9]|2\d|3[01])\./.test(bare)) return true;
  return false;
}

/**
 * Rate-limit markers from an OpenAI-shaped `error` envelope.
 *
 * The transport status cannot be trusted to carry this. Several compatible
 * gateways answer 200 OK with the failure in the body — the shape #3263 hit
 * through OpenRouter — so a status-only classifier calls a throttle
 * `unrecoverable`, which neither retries nor rotates the key. Read the
 * structured code/type as well, and let it decide regardless of status.
 */
const RATE_LIMIT_ERROR_CODES = new Set([
  'rate_limited',
  'rate_limit',
  'rate_limit_error',
  'rate_limit_exceeded',
  'too_many_requests',
  'requests_rate_limit_exceeded',
  'tokens_rate_limit_exceeded',
]);

/**
 * litellm (behind many compatible gateways) reports a failure to parse the
 * downstream model's reply as an error envelope, often inside a 200 (#3263).
 * The model ran and was billed; only its output was lost, so resending pays
 * for the same work again. An output failure, never retried.
 */
const UPSTREAM_OUTPUT_FAILURE_MARKERS = ['unable to get json', 'expecting value'];

interface ErrorEnvelope {
  code?: unknown;
  type?: unknown;
  message?: unknown;
}

/** The `error` object of a body that may not even be JSON. */
function parseErrorEnvelope(bodyText: string): ErrorEnvelope | null {
  if (!bodyText.trimStart().startsWith('{')) return null;
  try {
    const parsed = JSON.parse(bodyText) as { error?: unknown };
    return parsed?.error && typeof parsed.error === 'object' ? parsed.error as ErrorEnvelope : null;
  } catch {
    return null;
  }
}

/** `error.code` / `error.type`, lower-cased, for the rate-limit marker check. */
function structuredErrorMarkers(envelope: ErrorEnvelope | null): string[] {
  if (!envelope) return [];
  return [envelope.code, envelope.type]
    .filter((marker): marker is string | number => typeof marker === 'string' || typeof marker === 'number')
    .map(marker => String(marker).toLowerCase());
}

/**
 * The status to classify by. An error envelope inside a 2xx response often
 * carries the real HTTP status as a numeric `error.code` (a 429 or a 503
 * relayed by a gateway), and that is the one that says whether to retry.
 * Some gateways use a textual code ("bad_gateway") and name the status only in
 * the message, as "(HTTP 503)" (#5185). When the code is not numeric, that is
 * read from the message or from the textual code itself.
 */
function effectiveStatus(status: number | undefined, envelope: ErrorEnvelope | null): number | undefined {
  const code = envelope?.code;
  const numeric = typeof code === 'number' ? code
    : typeof code === 'string' && /^\d{3}$/.test(code) ? Number(code)
    : textualEnvelopeStatus(envelope);
  if (status !== undefined && status >= 200 && status < 300 && numeric >= 400 && numeric < 600) {
    return numeric;
  }
  return status;
}

/** Textual codes and types that name a server-side failure, as the HTTP status they stand for. */
const SERVER_FAILURE_ERROR_CODES: ReadonlyMap<string, number> = new Map([
  ['bad_gateway', 502],
  ['service_unavailable', 503],
  ['gateway_timeout', 504],
  ['server_error', 500],
  ['internal_server_error', 500],
]);

/**
 * The status an envelope with a non-numeric code reports, or NaN.
 *
 * Only a 429 or a 5xx counts, because those are the ones worth retrying. A
 * combo gateway's message lists every upstream's failure
 * ("a: auth (HTTP 401); b: ... (HTTP 503)"), and an upstream's 401 or 413 is not
 * this request's failure, so every marker is scanned for the first retryable one.
 * Only when the message names no status at all does a textual code or type that names a server failure
 * ("bad_gateway", type "server_error") stand for its status.
 *
 * A litellm output failure keeps its own classification: the model ran and was
 * billed, so promoting it to a 5xx would resend it and pay twice.
 */
function textualEnvelopeStatus(envelope: ErrorEnvelope | null): number {
  const message = typeof envelope?.message === 'string' ? envelope.message : '';
  const lowerMessage = message.toLowerCase();
  if (UPSTREAM_OUTPUT_FAILURE_MARKERS.some(marker => lowerMessage.includes(marker))) return NaN;
  const markers = [...message.matchAll(/\(HTTP (\d{3})\)/g)].map(match => Number(match[1]));
  const retryable = markers.find(fromMessage => fromMessage === 429 || (fromMessage >= 500 && fromMessage < 600));
  if (retryable !== undefined) return retryable;
  // The message named statuses and none is retryable (every upstream refused
  // the key, say): that is the answer, whatever the gateway's own code says.
  if (markers.length > 0) return NaN;
  for (const marker of structuredErrorMarkers(envelope)) {
    const mapped = SERVER_FAILURE_ERROR_CODES.get(marker);
    if (mapped !== undefined) return mapped;
  }
  return NaN;
}

/**
 * A model-refusal body vs. a credential/billing complaint. Both orders of
 * "revoked key" / "API key revoked" and any billing word belong to the second:
 * an auth_invalid is a real credential problem, not a bad model id.
 *
 * Only permanent wording counts. A refusal is remembered until the worker
 * restarts, and the own-model path is on for every OpenAI-compatible user, so
 * "unavailable" / "not available" stay out: providers also use them for
 * temporary capacity errors, and remembering one would stop capture. The
 * second branch is the 410 retirement shape ("no longer available" included):
 * a provider that retires an id says so permanently, as the model's own
 * predicate — an optional id token, then "has been"/"was"/"is" or the bare
 * participle. It deliberately cannot walk across another clause, so
 * "Model gpt-4o: the functions parameter is deprecated" is not a model
 * refusal and a valid id is not remembered as refused. An attribute noun
 * ("the model parameter/field/name/list is deprecated") describes the API
 * surface, not a model id, so the optional id token cannot start with one.
 */
const MODEL_REFUSAL = /model[_\s-]*(?:access[_\s-]+is[_\s-]+disabled|not[_\s-]+(?:served|supported|found|enabled)|disabled|does[_\s-]+not[_\s-]+exist)|model(?![-_\s]+(?:name|field|parameter|list)[-_\s]+(?:is|was|has[-_\s]+been|deprecated|retired|decommissioned|sunset|no[-_\s]+longer)\b)(?:[_\s-]+[`"']?[\w./:@-]+[`"']?)?[_\s-]*(?:(?:has[_\s-]+been|was|is)[_\s-]+(?:deprecated|retired|decommissioned|sunset)|(?:deprecated|retired|decommissioned|sunset)|(?:is[_\s-]+)?no[_\s-]+longer[_\s-]+(?:available|served|supported))/i;
/** A 404 that names a model ("model_not_found", "model `x` does not exist") rather than a missing route. */
const MODEL_SPECIFIC_404 = /model[^\n]{0,120}(?:not[_\s-]+found|does[_\s-]+not[_\s-]+exist)/i;
const CREDENTIAL_REFUSAL = /(?:invalid|revoked|expired|bad)[_\s-]+(?:api[_\s-]+)?key|(?:api[_\s-]+)?key[_\s-]+(?:is[_\s-]+)?(?:invalid|revoked|expired|bad)|unauthori[sz]ed|billing|credit|quota|insufficient/i;

/**
 * Classify a failure from an arbitrary OpenAI-compatible endpoint.
 *
 * Kept separate from `classifyOpenRouterError` because the inputs differ in
 * kind, not just in wording. OpenRouter and the cmem gateway send a known
 * taxonomy envelope; a self-hosted vLLM or a vendor gateway sends whatever it
 * sends. So this leans on status codes first — which are the one thing the
 * OpenAI shape actually standardizes — and consults the body only for the
 * quota-vs-rate-limit distinction that status alone cannot make.
 *
 * The distinction matters more here than elsewhere: `quota_exhausted` retires a
 * key for 30 minutes and arms the provider breaker, while `rate_limit` retires
 * it for a minute. Calling a per-minute throttle "exhausted" would idle a
 * perfectly good key for half an hour.
 */
export function classifyOpenAICompatError(input: {
  status?: number;
  bodyText?: string;
  headers?: Headers | { get(name: string): string | null };
  cause: unknown;
  endpointLabel?: string;
  /** The URL a request with no response was sent to, named in the network-error message. */
  requestUrl?: string;
  /** The request's model when the host chose it, with the opt-in that took it; unset for a configured model. */
  hostChosenModel?: { id: string; setting: HostModelSetting };
  /**
   * Provider id whose CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS entry chose
   * this request's endpoint. The refusal's action names that entry instead of
   * the pinned base URL, which would be the wrong setting to fix.
   */
  sessionEndpointProvider?: string;
  /**
   * True when this request pays for the summary turn rather than the observer
   * model. The refusal action then names the tier setting instead of the
   * observer one.
   */
  summaryTurn?: true;
}): ClassifiedProviderError {
  const body = input.bodyText ?? '';
  const lower = body.toLowerCase();
  const envelope = parseErrorEnvelope(body);
  const status = effectiveStatus(input.status, envelope);
  const retryAfterMs = input.headers ? parseRetryAfterMs(input.headers.get('retry-after')) : undefined;
  const label = input.endpointLabel ?? 'OpenAI-compatible endpoint';
  const excerpt = typeof envelope?.message === 'string' && envelope.message
    ? envelope.message.substring(0, 300)
    : body.substring(0, 300);
  const describe = (cls: string): string =>
    `${label} ${cls}${status !== undefined ? ` (status ${status})` : ''}${excerpt ? `: ${excerpt}` : ''}`;

  /** The endpoint does not serve an id the host chose: name that opt-in, never the API key. */
  const hostModelNotServedError = (
    source: { cause: unknown },
    status: number,
    id: string,
    setting: HostModelSetting,
  ): ClassifiedProviderError => new ClassifiedProviderError(describe(status === 410 ? 'model refused' : 'not found'), {
    kind: 'unrecoverable',
    cause: source.cause,
    code: HOST_MODEL_NOT_SERVED_CODE,
    action: `The endpoint answered ${status} for "${id}", ${HOST_MODEL_ORIGIN[setting]}. The observer stops using that id and goes back to the configured model. To use it, serve it on ${serveRefusedModelAt(input.sessionEndpointProvider)} and restart the worker; otherwise set ${setting} to false.`,
  });

  // Read the body once. A credential complaint is not a model refusal: the
  // pool has to rotate to the next healthy key even when the request happened
  // to carry an id the host chose. C1's "Model access is disabled" has no
  // credential word, so it stays a refusal of the host's choice.
  const envelopeMessage = typeof envelope?.message === 'string' ? envelope.message : '';
  // The same refusal rule reads the structured code/type too. A named message
  // ("The model 'old-id' does not exist") need not match the textual regex,
  // and a non-empty message must not hide a `model_not_found` envelope code.
  // No second body parser: structuredErrorMarkers() already reads code/type.
  const structuredModelRefusal = structuredErrorMarkers(envelope).some(marker => MODEL_REFUSAL.test(marker));
  const refusedModel = MODEL_REFUSAL.test(envelopeMessage || body) || structuredModelRefusal;
  const credentialOrBilling = CREDENTIAL_REFUSAL.test(body);
  const modelNamedBy404 = status === 404 && (refusedModel || MODEL_SPECIFIC_404.test(body));

  // A request on a host-chosen id that the endpoint refuses is a refusal of
  // that choice, not a bad credential. Check before the 401/403 branch: a 401
  // here would otherwise arm a long auth cooldown and tell the user to rotate a
  // key that works for the configured model. 404 keeps its existing answer
  // (a route that is simply missing says nothing about the key either). A
  // model-specific 400/422 is the same permanent refusal; a plain one stays on
  // its old bad-request / context-overflow path. A 410 is "this model id is
  // gone": it joins the family only when the body names a model, so a bare
  // "Gone" stays the generic API error.
  const hostRefusalStatus = status === 401 || status === 403 || status === 404 || status === 410
    || ((status === 400 || status === 422) && refusedModel);
  if (input.hostChosenModel && hostRefusalStatus) {
    if (status === 404 || (refusedModel && !credentialOrBilling)) {
      const { id, setting } = input.hostChosenModel;
      return hostModelNotServedError(input, status, id, setting);
    }
  }

  // An id we chose ourselves (explicit setting, override or preset default)
  // can be refused too. Same family as the host refusal — unrecoverable,
  // discarded, no key rotation — but a different code and a different action.
  // A 404 only joins it when the body names the model; without that marker it
  // is the base URL / route that is wrong, and keeps its old answer below.
  // A 410 with a model marker is the retirement shape, for the same reason.
  if (!input.hostChosenModel && !credentialOrBilling
    && ((refusedModel && (status === 400 || status === 403 || status === 410 || status === 422)) || modelNamedBy404)) {
    return new ClassifiedProviderError(describe('model refused'), {
      kind: 'unrecoverable',
      code: OWN_MODEL_NOT_SERVED_CODE,
      cause: input.cause,
      action: input.summaryTurn
        ? `The endpoint does not serve the summary model in this request. Set CLAUDE_MEM_TIER_SUMMARY_MODEL to an id this endpoint serves on ${serveRefusedModelAt(input.sessionEndpointProvider)}, or turn off CLAUDE_MEM_TIER_ROUTING_ENABLED. The API key was not changed.`
        : `The endpoint does not serve the model id in this request. Set CLAUDE_MEM_OPENAI_COMPAT_MODEL to an id this endpoint serves on ${serveRefusedModelAt(input.sessionEndpointProvider)}, or use CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL to replace or disable the preset default. The API key was not changed.`,
    });
  }

  // A 410 is a permanent retirement, never a credential or allowance problem.
  // The two branches above already handled a named model id; every other 410
  // stays a generic unrecoverable error. Without this stop, a retirement note
  // that also says "update your billing" or "insufficient_quota" would reach
  // the quota scan below and retire a healthy key for 30 minutes.
  if (status === 410) {
    return new ClassifiedProviderError(describe('API error'), {
      kind: 'unrecoverable',
      cause: input.cause,
    });
  }

  // Authentication first. The quota scan below matches loose body words like
  // "billing", and a 401/403 very often explains itself in billing terms ("key
  // revoked, update your billing details"). Reading that as a spent allowance
  // retires the key for 30 minutes and can arm the provider breaker, when the
  // actual fix is a new key — so status wins here, before any body markers.
  if (status === 401 || status === 403) {
    return new ClassifiedProviderError(describe('auth error'), {
      kind: 'auth_invalid',
      cause: input.cause,
      action: 'Check the API key for this endpoint (CLAUDE_MEM_OPENAI_COMPAT_API_KEY).',
    });
  }

  // The request did not fit the model's context window (vLLM, llama.cpp, the
  // OpenAI shape), or the server refused its size outright (413). Retiring the
  // conversation fixes both, so neither may finalize the session as a bad
  // request (#3625). Local models have small windows, so this is common here.
  if (status === 413 || (status === 400 && isContextOverflowBody(body))) {
    return new ClassifiedProviderError(describe('context overflow'), {
      kind: 'context_overflow',
      cause: input.cause,
    });
  }

  // A structured rate-limit marker outranks the transport status: an endpoint
  // that reports the throttle in a 200 body must still retry and rotate.
  const rateLimitCoded = structuredErrorMarkers(envelope).some(marker => RATE_LIMIT_ERROR_CODES.has(marker));

  // Quota / credit exhaustion. Body markers win over status, because a 429 is
  // used for both a per-minute throttle and a spent allowance, and 402 is not
  // universal. The markers below are the ones actually emitted by NVIDIA NIM,
  // DeepSeek, Groq, Together and the OpenAI shape itself. "billing" alone is
  // weaker: Groq and OpenAI end a per-minute throttle with a link to their
  // billing page, so it never outranks a 429 or a rate-limit code.
  if (
    lower.includes('insufficient_quota')
    || lower.includes('insufficient quota')
    || lower.includes('quota exceeded')
    || lower.includes('exceeded your current quota')
    || lower.includes('insufficient credit')
    || lower.includes('insufficient balance')
    || lower.includes('out of credits')
    || lower.includes('credit limit')
    || (lower.includes('billing') && status !== 429 && !rateLimitCoded)
    // A 429 naming a daily cap is spent until the day turns over; as a rate
    // limit it would be re-probed every 90 seconds (shared with the worker's
    // OpenRouter classifier and the server runtime).
    || (status === 429 && namesPeriodRateLimit(lower))
    || status === 402
  ) {
    return new ClassifiedProviderError(describe('quota exhausted'), {
      kind: 'quota_exhausted',
      cause: input.cause,
    });
  }

  if (status === 429 || rateLimitCoded) {
    return new ClassifiedProviderError(describe('rate limit'), {
      kind: 'rate_limit',
      cause: input.cause,
      ...(retryAfterMs !== undefined ? { retryAfterMs } : {}),
    });
  }

  // 404 on a self-configured endpoint is very often a wrong base URL or a model
  // id the endpoint does not serve, so say so instead of just "bad request".
  if (status === 404) {
    // A host-chosen id was handled above; this one came from our own settings,
    // so it is the base URL or _MODEL that is wrong.
    return new ClassifiedProviderError(describe('not found'), {
      kind: 'unrecoverable',
      cause: input.cause,
      action: 'Verify CLAUDE_MEM_OPENAI_COMPAT_BASE_URL and _MODEL — a 404 here usually means the base URL is wrong or the endpoint does not serve that model.',
    });
  }

  if (status === 400 || status === 422) {
    return new ClassifiedProviderError(describe('bad request'), {
      kind: 'unrecoverable',
      cause: input.cause,
    });
  }

  if (status !== undefined && status >= 500 && status < 600) {
    return new ClassifiedProviderError(describe('upstream error'), {
      kind: 'transient',
      cause: input.cause,
    });
  }

  if (status === undefined) {
    // No status means the request never completed. For a localhost preset this
    // is nearly always "the server is not running", which is worth saying.
    const message = input.cause instanceof Error ? input.cause.message : String(input.cause);
    const network = describeNetworkFailure(input.cause, input.requestUrl);
    return new ClassifiedProviderError(`${label} network error: ${message}${networkFailureSuffix(network)}`, {
      kind: 'transient',
      cause: input.cause,
      // A refused connection never sent the request, so it is pre-send: it
      // must not spend the batch's paid-send budget (#4604). Resets and
      // timeouts stay ambiguous — they can land after the server took it.
      ...(isConnectionRefused(network) ? { paidSendOutcome: 'refused_before_work' as const } : {}),
      ...(network.localNetworkHint ? { action: network.localNetworkHint } : {}),
    });
  }

  if (UPSTREAM_OUTPUT_FAILURE_MARKERS.some(marker => lower.includes(marker))) {
    return new ClassifiedProviderError(describe('upstream output failure'), {
      kind: 'unrecoverable',
      paidSendOutcome: 'output_failure',
      cause: input.cause,
    });
  }

  return new ClassifiedProviderError(describe('API error'), {
    kind: 'unrecoverable',
    cause: input.cause,
  });
}

/**
 * The reply without a leading `<think>…</think>` block. Reasoning models on an
 * OpenAI-compatible endpoint (MiniMax's M2 family, DeepSeek R1 or Qwen3 on
 * Ollama and vLLM) can put their thinking inline at the start of `content`.
 * It is not the answer: kept, it lands in the conversation and is re-sent with
 * every later request. Only a closed block at the very start is removed, so an
 * answer that merely mentions the tag is left alone.
 */
export function stripLeadingThinkBlock(content: string): string {
  return content.replace(/^\s*<think>[\s\S]*?<\/think>\s*/i, '');
}

/** Endpoint a key was last withheld from, so a status poll logs it once, not per read. */
let lastWithheldKeyUrl: string | null = null;

/**
 * Unmapped host ids already warned about, so alternating ids still warn once
 * each. The ids come from OpenCode config files, a checked-out repository's own
 * among them, so the set is capped against a repository that names many: past
 * the cap the oldest id is forgotten, which costs one more warning line if it
 * is read again and nothing else. The number itself has no measurement behind it.
 */
const MAX_WARNED_UNMAPPED_HOST_MODEL_IDS = 64;
const warnedUnmappedHostModelIds = new Set<string>();

/**
 * The other statuses that are worth one notice — masked, none and identity —
 * deduplicated by the id they concern (or by endpoint when there is no id).
 * Separate from `warnedUnmappedHostModelIds`: an id that is sent, or a model
 * deliberately hidden by an explicit setting, must not inherit the
 * untranslatable model's message. Same bounded eviction, for the same reason.
 */
const MAX_WARNED_IDENTITY_HOST_MODEL_IDS = 64;
const warnedIdentityHostModelIds = new Set<string>();

/**
 * Unmapped small-model ids, kept apart from the main model's set: a main id
 * that was sent, or a small id that was sent, must not silence the other's
 * notice. Same cap and insertion-order eviction, for the same reason.
 */
const MAX_WARNED_UNMAPPED_SMALL_MODEL_IDS = 64;
const warnedUnmappedSmallModelIds = new Set<string>();

/** One notice per key, bounded by evicting the oldest entry. */
function warnOnce(warned: Set<string>, cap: number, key: string, line: string): void {
  if (warned.has(key)) return;
  warned.add(key);
  // A Set keeps insertion order, so evicting the first entry drops the oldest
  // warning and bounds memory even if a config lists many unknown ids.
  if (warned.size > cap) {
    const oldest = warned.values().next().value;
    if (oldest !== undefined) warned.delete(oldest);
  }
  logger.warn('SDK', line);
}

/** Print the shared diagnosis once per id; translation itself lives in the helper. */
function warnHostModelDiagnostic(diagnostic: HostModelDiagnostic, apiUrl: string): void {
  const status = diagnostic.status;
  if (status !== 'resolved' && status !== 'off') {
    const key = `${HOST_MODEL_STATUS_WORDS[status].label}\n${diagnostic.hostId ?? apiUrl}`;
    warnOnce(
      status === 'unmapped' ? warnedUnmappedHostModelIds : warnedIdentityHostModelIds,
      status === 'unmapped' ? MAX_WARNED_UNMAPPED_HOST_MODEL_IDS : MAX_WARNED_IDENTITY_HOST_MODEL_IDS,
      key,
      diagnostic.line,
    );
  }
  // A resolved main model still hides an untranslatable small model: the old
  // translateHostModel() warned for either id, so keep that notice here. It is
  // a separate line, not another HostModelStatus value.
  const small = diagnostic.smallModelNotice;
  if (small) {
    warnOnce(
      warnedUnmappedSmallModelIds,
      MAX_WARNED_UNMAPPED_SMALL_MODEL_IDS,
      `${HOST_MODEL_STATUS_WORDS.unmapped.label}\nsmall\n${small.hostId}`,
      small.line,
    );
  }
}

/**
 * Translate one id through a modelMap. An id the map does not name is already
 * endpoint-ready and passes through verbatim; the caller still checks the
 * refusal record, because a translation may have been tried and refused on an
 * earlier turn.
 */
function mapModelMap(modelMap: Readonly<Record<string, string>> | undefined, id: string): string {
  if (!modelMap || !Object.hasOwn(modelMap, id)) return id;
  return modelMap[id];
}

/** Translate one id through a mapped endpoint's modelMap. */
function mapEndpointModel(endpoint: OpenAICompatSessionEndpoint | undefined, id: string): string {
  return mapModelMap(endpoint?.modelMap, id);
}

/**
 * The host's observer and summary models, endpoint-ready (#6485).
 *
 * Read only when the operator opted in and the caller supplied host options.
 * Status, availability and wrap-up callers pass none and keep the pinned
 * config. The shared diagnosis reads only model, smallModel and provider; no
 * tool, plugin, MCP or permission setting crosses into the worker. The fourth
 * parameter only words the diagnosis line; it never changes which id wins.
 */
function hostObserverModels(
  settings: SettingsDefaults,
  hostOptions: OpenAICompatHostOptions | undefined,
  apiUrl: string,
  fallbackModel: string,
  fallbackReason?: HostModelFallbackReason,
): { model?: string; summaryModel?: string } {
  if (!hostOptions) return {};
  const diagnostic = diagnoseHostModel({
    settings,
    fallbackModel,
    fallbackReason,
    hostOptions,
    apiUrl,
    isRefused: id => isHostModelRefused(apiUrl, id),
  });
  warnHostModelDiagnostic(diagnostic, apiUrl);
  // A translated id this endpoint refused is left out, so the explicit
  // setting, last served id or preset serves instead.
  const servedHere = (id: string | undefined) => (id && !isHostModelRefused(apiUrl, id) ? id : undefined);
  const model = servedHere(diagnostic.endpointId);
  const summaryModel = servedHere(diagnostic.summaryId);
  return {
    ...(model ? { model } : {}),
    ...(summaryModel ? { summaryModel } : {}),
  };
}

/**
 * Resolve the endpoint tuple.
 *
 * Precedence, per field: the explicit setting, then the preset, then nothing.
 * The preset only ever fills a blank — so switching preset never silently
 * moves a user who pinned a base URL or a model, and the two settings remain
 * the source of truth for what is actually sent.
 *
 * Every key goes through the cmem key lock (#4276): an account-owned cm_pro_
 * key is never sent to a third-party endpoint, and no other key is sent to the
 * cmem gateway. A withheld key leaves the endpoint keyless, which a hosted
 * endpoint reports as unconfigured.
 */
/**
 * The configured key pool, in the order `keyIndexes` counts. One credential
 * name for every endpoint, on purpose: the .env whitelist in EnvManager is a
 * security boundary (#2375), and a per-vendor name would mean widening it for
 * each preset added.
 */
function configuredOpenAICompatKeyPool(settings: SettingsDefaults): string[] {
  const primaryKey = (settings.CLAUDE_MEM_OPENAI_COMPAT_API_KEY ?? '').trim()
    || getCredential('OPENAI_COMPAT_API_KEY')
    || '';
  return buildKeyPool(
    primaryKey,
    settings.CLAUDE_MEM_OPENAI_COMPAT_API_KEYS || getCredential('OPENAI_COMPAT_API_KEYS') || '',
  );
}

export function resolveOpenAICompatConfig(
  settingsPath: string = USER_SETTINGS_PATH,
  hostOptions?: OpenAICompatHostOptions,
  endpointOverride?: Pick<OpenAICompatSessionEndpoint, 'baseUrl' | 'keyIndexes'>,
): OpenAICompatConfig {
  const settings = SettingsDefaultsManager.loadFromFile(settingsPath);
  const preset = resolveOpenAICompatPreset(settings.CLAUDE_MEM_OPENAI_COMPAT_PRESET);

  const configuredBaseUrl = (settings.CLAUDE_MEM_OPENAI_COMPAT_BASE_URL ?? '').trim();
  // A mapped session endpoint replaces the configured base URL only when it
  // carries one. A blank override is ignored rather than falling through to the
  // preset, so a malformed map entry never moves the observer silently.
  const overrideBaseUrl = endpointOverride?.baseUrl.trim() ?? '';
  const useOverride = overrideBaseUrl.length > 0;
  const baseUrl = useOverride ? overrideBaseUrl : configuredBaseUrl || preset.baseUrl;
  // Reuses the OpenRouter base-URL normalizer: same job (tolerate a trailing
  // slash, tolerate a base that already names /chat/completions), and one copy
  // of that rule is better than two.
  const apiUrl = baseUrl ? resolveOpenRouterChatCompletionsUrl(baseUrl) : '';

  const configuredModel = (settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL ?? '').trim();
  // Precedence: explicit setting, then the host's translated model (opt-in),
  // then the implicit default: the operator's override, or the last id this
  // endpoint answered while a host opt-in is active, then the preset. A host id
  // never replaces an explicit observer model. An id this endpoint refused is
  // never reintroduced; '' is a real model value here, and query() stops before
  // the network when it is all we have.
  const hostModelOptedIn = settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST === 'true'
    || settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION === 'true';
  // Only a host-model path may reuse a remembered id: with every opt-in off,
  // the remembered id would let a summary turn change the observer model.
  const lastServed = hostModelOptedIn ? lastServedModel(apiUrl) : undefined;
  const implicitCandidate = resolvePresetDefaultModel(
    settings.CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL,
    lastServed || preset.defaultModel,
  );
  const implicitDefault = implicitCandidate && !isHostModelRefused(apiUrl, implicitCandidate) ? implicitCandidate : '';
  // A refused explicit id is no more reusable than a refused default: without
  // this check it won precedence on every later turn and earned the same
  // refusal again, discarding more pending inputs. A translated host id equal
  // to the preset default is still the host's choice, so only an explicit
  // setting that survived this filter is "ours".
  const ownModel = configuredModel && !isModelRefused(apiUrl, configuredModel) ? configuredModel : '';

  // A mapped endpoint receives only the pool positions its entry names.
  const configuredPool = configuredOpenAICompatKeyPool(settings);
  const configuredKeys = useOverride && endpointOverride?.keyIndexes
    ? endpointOverride.keyIndexes.flatMap(index => (index < configuredPool.length ? [configuredPool[index]] : []))
    : configuredPool;
  const apiKeys = keysForEndpoint(apiUrl, configuredKeys);
  if (apiKeys.length < configuredKeys.length && lastWithheldKeyUrl !== apiUrl) {
    lastWithheldKeyUrl = apiUrl;
    logger.warn('SDK', 'Withholding openai-compatible keys: the cmem gateway takes one cmem.ai memory key (cm_pro_) and never a pool, and a cm_pro_ key never goes to any other host. Set CLAUDE_MEM_OPENAI_COMPAT_API_KEY to the key this endpoint issued.');
  }

  // The preset's answer only stands while the preset's endpoint does. Once the
  // base URL is overridden — by a setting or by a session mapping — ask the
  // endpoint being called.
  const requiresApiKey = (configuredBaseUrl || useOverride)
    ? !isLocalEndpointUrl(baseUrl)
    : preset.requiresApiKey;

  const fallbackModel = ownModel || implicitDefault;
  // No fallback has two very different causes. `none` is the operator's
  // setting; only a candidate the endpoint actually refused may be blamed on
  // it. A bare endpoint without a default gets neutral wording too.
  // Ask the one resolver whether this is the disable sentinel, with a non-empty
  // stand-in default so an endpoint that merely has no default is not counted.
  const defaultDisabledBySetting = resolvePresetDefaultModel(
    settings.CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL,
    'sentinel-default',
  ) === '';
  const fallbackReason: HostModelFallbackReason | undefined = fallbackModel
    ? undefined
    : defaultDisabledBySetting
      ? 'none'
      : // Same test as allModelsRefused below: no key means no answer was had.
        !!implicitCandidate && !implicitDefault && !!apiUrl && (!requiresApiKey || apiKeys.length > 0)
        ? 'refused'
        : undefined;
  const hostModels = hostObserverModels(settings, hostOptions, apiUrl, fallbackModel, fallbackReason);
  const model = ownModel || hostModels.model || implicitDefault;
  // Only an id that claude-mem's own settings do not also name is the host's choice.
  const hostChosenModels: Record<string, HostModelSetting> = {};
  for (const id of [ownModel ? undefined : hostModels.model, hostModels.summaryModel]) {
    if (id && id !== ownModel) hostChosenModels[id] = 'CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST';
  }

  // An explicit id is a different candidate from the implicit one. A preset
  // without a default (`custom`, `api-route`, `modelgo`) leaves the implicit
  // branch empty even when the endpoint was fully configured and its pinned
  // model was refused. Count that refusal separately so dispatch stays here
  // instead of silently moving the work to Claude. A withheld key does not.
  const explicitModelRefused = !!configuredModel && !ownModel && !model
    && !!apiUrl && (!requiresApiKey || apiKeys.length > 0);
  // `none` is a different state from an unconfigured endpoint: the operator
  // disabled the default on purpose, so dispatch stays here and the stop has
  // its own code. A refused explicit id keeps its own (earlier) classification.
  const defaultDisabled = defaultDisabledBySetting && !configuredModel && !model
    && !!apiUrl && (!requiresApiKey || apiKeys.length > 0);
  // The effective implicit candidate is a model this provider actually chose,
  // unlike a bare endpoint's absent default or a host session id. When that
  // candidate was refused and nothing else is left, the endpoint is not
  // unconfigured: it answered, and there is simply no model left to send. Keep
  // that state distinct so dispatch cannot mistake it for "fall through to
  // Claude" and switch paid accounts. `none` is handled by `defaultDisabled`
  // below: it is a configured endpoint with no default, not an unconfigured
  // one. A withheld key does stay unconfigured.
  const allModelsRefused = (!model && !!implicitCandidate && !implicitDefault
    && !!apiUrl && (!requiresApiKey || apiKeys.length > 0)) || explicitModelRefused;

  return {
    apiKey: apiKeys[0] ?? '',
    apiKeys,
    requiresApiKey,
    model,
    apiUrl,
    preset,
    ...(allModelsRefused ? { allModelsRefused: true } : {}),
    ...(explicitModelRefused ? { explicitModelRefused: true } : {}),
    ...(defaultDisabled ? { defaultDisabled: true } : {}),
    ...(hostModels.summaryModel ? { summaryModel: hostModels.summaryModel } : {}),
    ...(Object.keys(hostChosenModels).length > 0 ? { hostChosenModels } : {}),
  };
}

function errorText(error: unknown): string {
  if (!(error instanceof Error)) return String(error);
  const cause = error.cause instanceof Error ? error.cause.message : '';
  return `${error.message}\n${cause}`;
}

export class OpenAICompatProvider extends OpenAICompatibleProvider<OpenAICompatConfig> {
  protected readonly providerName = 'OpenAI-compatible';
  protected readonly syntheticIdPrefix = 'openai-compat';
  // Match OpenRouter: forward the empty batch so the parser/recovery path sees
  // it, rather than dropping it the way Gemini does.
  protected readonly forwardEmptyMessageResponse = true;
  protected override readonly rejectAbortedObservation = true;

  constructor(dbManager: DatabaseManager, sessionManager: SessionManager) {
    super(dbManager, sessionManager);
  }

  protected getConfig(): OpenAICompatConfig {
    return resolveOpenAICompatConfig(paths.settings());
  }

  /**
   * The summary tier reads CLAUDE_MEM_TIER_SUMMARY_MODEL after config
   * resolution, so it would otherwise bypass the shared refusal record. Narrow
   * each candidate with that record, then let the shared resolver keep its one
   * precedence rule for everyone.
   *
   * On a mapped session endpoint the tier setting is read here, after
   * configForSession built the config, so it must be translated through the
   * same map as the observer id. config.model and config.summaryModel are
   * already endpoint ids; mapping them again could follow a second entry for
   * the translated id.
   */
  protected override resolveSummaryModel(config: OpenAICompatConfig, settings: SettingsDefaults): string {
    const usable = (id: string | undefined): string => (id && !isModelRefused(config.apiUrl, id) ? id : '');
    const usableTier = (id: string | undefined): string =>
      (id ? usable(mapModelMap(config.sessionModelMap, id)) : '');
    return resolveSummaryTierModel(
      usable(config.model),
      { ...settings, CLAUDE_MEM_TIER_SUMMARY_MODEL: usableTier(settings.CLAUDE_MEM_TIER_SUMMARY_MODEL) },
      usable(config.summaryModel),
    );
  }

  /**
   * The observer config for one live session, composing two independent
   * opt-ins (#6485 + #6486) and the per-provider endpoint map
   * (CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS).
   *
   * First resolve the base config with `host`: when
   * CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST='true' this reads the host's own
   * OpenCode config and may replace the preset model with its translated
   * endpoint id. Then, only when
   * CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION='true', the endpoint-ready id
   * parked on the session is applied last, so it wins over both the host model
   * and the explicit setting. Turning the session opt-in off must not disable
   * host mode.
   *
   * The same session opt-in lets a provider id choose its own endpoint from
   * CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS. The map is read only when the
   * session carries both a model and a provider: without the model there is
   * nothing to send there, and without the provider no entry can apply. A
   * provider with no entry keeps the pinned endpoint. The map translates the
   * host model, the host small model used by the summary tier, an explicit tier
   * summary id, and, when the session id is refused, the fallback id it names;
   * it is never a source of availability or credentials.
   *
   * Only model and endpoint fields are touched. Init, availability, wrap-up,
   * WorkView and api-agent resolve without host options and keep the pinned
   * config.
   */
  protected override configForSession(session: ActiveSession, host?: OpenAICompatHostOptions): OpenAICompatConfig {
    const settings = SettingsDefaultsManager.loadFromFile(paths.settings());
    const fromSession = settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION === 'true';
    // Defense in depth: the init schema already rejects a blank id, but any
    // other writer must not replace a valid configured model with whitespace.
    const sessionModel = session.observerModel?.trim();
    const sessionProvider = session.observerProvider?.trim();
    let endpoint: OpenAICompatSessionEndpoint | undefined;
    if (fromSession && sessionModel && sessionProvider) {
      const raw = settings.CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS ?? '';
      const parsed = parseOpenAICompatSessionEndpoints(raw);
      warnSessionEndpointSettingOnce(raw, parsed.warning);
      const entry = parsed.endpoints.get(sessionProvider);
      endpoint = entry ? usableSessionEndpoint(sessionProvider, entry, settings) : undefined;
    }
    // Keep getConfig() as the pinned path so provider subclasses and the init
    // turn retain their existing config. Host mode opts into the full resolver;
    // a mapped endpoint always does, because the session named its base URL.
    const resolveFor = (mapped: OpenAICompatSessionEndpoint | undefined): OpenAICompatConfig =>
      host && settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST === 'true'
        ? resolveOpenAICompatConfig(paths.settings(), host, mapped)
        : mapped
          ? resolveOpenAICompatConfig(paths.settings(), undefined, mapped)
          : this.getConfig();
    let config = resolveFor(endpoint);
    // A mapped endpoint that refused this provider's key or allowance stays set
    // aside: the session goes back to the pinned endpoint, as if unmapped.
    if (endpoint && sessionProvider && isSessionEndpointRefused(sessionProvider, config.apiUrl)) {
      endpoint = undefined;
      config = resolveFor(undefined);
    }
    if (!fromSession || !sessionModel) return config;
    // The mapping translates the host id into what this endpoint serves. An id
    // the map does not name is already endpoint-ready and is sent verbatim.
    const endpointModel = mapEndpointModel(endpoint, sessionModel);
    // Every return that carries the mapped config names the provider that chose
    // it, so a refusal's action can say which entry to fix. The map travels too:
    // the summary tier resolves after this build and translates an explicit
    // tier setting through the same entries.
    const endpointMarker = endpoint
      ? {
        sessionEndpointProvider: sessionProvider,
        ...(endpoint.modelMap ? { sessionModelMap: endpoint.modelMap } : {}),
      }
      : {};
    // The host's small model is translated for the host's own endpoint. On a
    // mapped endpoint it needs the same translation, or the first summary sends
    // an id that endpoint never served. Re-key its host marker onto the id
    // actually sent, so a refusal names the host choice and the map entry.
    const withMappedSummary = (base: OpenAICompatConfig): OpenAICompatConfig => {
      const sourceSummaryModel = base.summaryModel;
      if (!sourceSummaryModel) return base;
      const summaryModel = mapEndpointModel(endpoint, sourceSummaryModel);
      if (!summaryModel || summaryModel === sourceSummaryModel) return base;
      let hostChosenModels = base.hostChosenModels;
      if (hostChosenModels && Object.hasOwn(hostChosenModels, sourceSummaryModel)) {
        hostChosenModels = { ...hostChosenModels, [summaryModel]: hostChosenModels[sourceSummaryModel] };
        // The source id is no longer the summary; keep its marker only while
        // the observer model still sends it.
        if (base.model !== sourceSummaryModel) delete hostChosenModels[sourceSummaryModel];
      }
      return { ...base, summaryModel, ...(hostChosenModels ? { hostChosenModels } : {}) };
    };
    // An id this endpoint answered 404 for is not sent again; a different
    // selection later in the session applies as usual. The configured fallback
    // is translated by the same map: the fallback is often another vendor's
    // global id, which this endpoint would refuse too. An id the map does not
    // name is sent verbatim, as before, and must be served by this endpoint.
    if (isHostModelRefused(config.apiUrl, endpointModel)) {
      const mappedFallback = mapEndpointModel(endpoint, config.model);
      // A translation this endpoint already refused is no better than the id it
      // came from. Send the configured id verbatim instead: the next refusal is
      // then attributed to the setting that actually chose it, not to a map
      // entry the remedy would not name.
      const fallbackModel = !isHostModelRefused(config.apiUrl, mappedFallback) ? mappedFallback : config.model;
      if (fallbackModel === config.model) return withMappedSummary({ ...config, ...endpointMarker });
      // Re-key the origin marker on the id actually sent: the marker names the
      // choice that took the id, so a later refusal of the translated fallback
      // stays classified as that choice, not as a wrong configured model. A
      // fallback the config did not mark (an explicit setting) stays unmarked.
      const hostChosenModels = config.hostChosenModels && Object.hasOwn(config.hostChosenModels, config.model)
        ? Object.fromEntries(Object.entries(config.hostChosenModels).map(([id, setting]) => [
          id === config.model ? fallbackModel : id,
          setting,
        ]))
        : config.hostChosenModels;
      return withMappedSummary({
        ...config,
        model: fallbackModel,
        ...(hostChosenModels ? { hostChosenModels } : {}),
        ...endpointMarker,
      });
    }
    // One line per mapping, and only when the entry's model is the one this
    // session sends. An absent provider never logs.
    if (endpoint && sessionProvider) {
      logSessionEndpointOnce(sessionProvider, sessionModel, endpointModel, config.apiUrl);
    }
    // An explicit setting naming the same id stays ours: its 404 is a bad
    // base URL or model, not a refusal of the host's choice.
    if (settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL?.trim() === endpointModel) {
      return withMappedSummary({ ...config, ...endpointMarker });
    }
    // At equality the id is already the one being sent, but it may have been
    // supplied by lastServed, which records no origin. Restore the session
    // marker so a later refusal is classified as a host choice rather than
    // arming the credential cooldown. An existing marker (the host opt-in)
    // keeps its own setting; only a missing one is added.
    const hostChosenModels = config.hostChosenModels && Object.hasOwn(config.hostChosenModels, endpointModel)
      ? config.hostChosenModels
      : { ...config.hostChosenModels, [endpointModel]: 'CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION' as const };
    return withMappedSummary({
      ...config,
      model: endpointModel,
      hostChosenModels,
      ...endpointMarker,
      // A live id this endpoint has not refused is a model to send, so neither
      // "nothing left" stop applies to this session: the disabled default and
      // the refused explicit setting both lose to the session's own choice.
      ...(config.allModelsRefused ? { allModelsRefused: false } : {}),
      ...(config.defaultDisabled ? { defaultDisabled: false } : {}),
      ...(config.explicitModelRefused ? { explicitModelRefused: false } : {}),
    });
  }

  /** Extend this turn with already-queued events; normal confirm/reset covers every claim. */
  protected override observationTurnPrompt(session: ActiveSession, first: PendingMessageWithId, prompt: ObservationPromptParts): string {
    const settings = SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH);
    const count = boundedInteger(settings.CLAUDE_MEM_OPENAI_COMPAT_OBSERVATION_BATCH_SIZE, 1, 32);
    const fieldMaxChars = observationFieldMaxChars(session.observerContextWindowTokens);
    // Like the Claude observer, add events only within two window-aware field
    // caps. The first event keeps its normal condensation/rendering path.
    const maxChars = 2 * fieldMaxChars;
    let combined = renderObservationPrompt(prompt);
    for (let size = 1; size < count; size++) {
      let addition = '';
      const next = this.sessionManager.claimNextObservation(session.sessionDbId, candidate => {
        if (!sameObservationContext(first, candidate)) return false;
        // Oversized fields need their own asynchronous condensation pass.
        if ([candidate.tool_input, candidate.tool_response].some(field =>
          (JSON.stringify(field, null, 2) ?? '').length > fieldMaxChars)) return false;
        addition = buildObservationPrompt({
          id: candidate._persistentId,
          tool_name: candidate.tool_name!,
          tool_input: JSON.stringify(candidate.tool_input),
          tool_output: JSON.stringify(candidate.tool_response),
          created_at_epoch: candidate._originalTimestamp,
          cwd: candidate.cwd,
        }, fieldMaxChars);
        return combined.length + 2 + addition.length <= maxChars;
      });
      if (!next) break;
      combined += '\n\n' + addition;
    }
    return combined;
  }

  protected missingApiKeyError(): Error {
    const config = this.getConfig();
    if (!config.apiUrl) {
      return new ClassifiedProviderError(
        'No OpenAI-compatible endpoint configured. Set CLAUDE_MEM_OPENAI_COMPAT_PRESET (e.g. nvidia-nim) or CLAUDE_MEM_OPENAI_COMPAT_BASE_URL.',
        { kind: 'setup_required', cause: new Error('missing base URL') },
      );
    }
    if (!config.model) {
      return new ClassifiedProviderError(
        `No model configured for ${config.preset.label}. Set CLAUDE_MEM_OPENAI_COMPAT_MODEL to a model id the endpoint serves.`,
        { kind: 'setup_required', cause: new Error('missing model') },
      );
    }
    return new ClassifiedProviderError(
      `API key not configured for ${config.preset.label}. Set CLAUDE_MEM_OPENAI_COMPAT_API_KEY in settings, or OPENAI_COMPAT_API_KEY in ~/.claude-mem/.env.`,
      { kind: 'setup_required', cause: new Error('missing api key') },
    );
  }

  /** Local servers (Ollama, LM Studio, bare vLLM) legitimately have no key. */
  protected requiresApiKey(config: OpenAICompatConfig): boolean {
    return config.requiresApiKey;
  }

  /**
   * With `none` and host mode on, the pinned init config has no model while
   * the first observation can resolve the host id with the session's cwd.
   * Defer only the init request: the prompt stays the generation's opening
   * turn, and `configForSession(session)` remains unpinned by host options.
   */
  protected override deferInitRequest(config: OpenAICompatConfig): boolean {
    if (!config.defaultDisabled) return false;
    const settings = SettingsDefaultsManager.loadFromFile(paths.settings());
    return settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_HOST === 'true';
  }

  protected prepareSessionExtras(session: ActiveSession, _config: OpenAICompatConfig): void {
    // Telemetry already segments openrouter.ai from 'custom'; every endpoint
    // reached through this provider is by definition the latter.
    session.endpointClass = 'custom';
  }

  protected estimateTokens(text: string): number {
    return Math.ceil(text.length / CHARS_PER_TOKEN_ESTIMATE);
  }

  /** Real usage only, both sides or nothing — see OpenRouterProvider. */
  protected buildLastUsage(result: ProviderQueryResult): ActiveSession['lastUsage'] {
    if (typeof result.inputTokens !== 'number' || typeof result.outputTokens !== 'number') {
      return null;
    }
    return { input: result.inputTokens, output: result.outputTokens };
  }

  protected async query(
    history: ConversationMessage[],
    config: OpenAICompatConfig,
    signal?: AbortSignal,
    perAttemptTimeoutMs?: number,
    paidSendBudget?: PaidSendBudget,
    label?: ObserverRequestLabel,
  ): Promise<ProviderQueryResult> {
    // Every model this endpoint had was a host choice it refused. Stop before
    // opening a socket or reserving a paid send: with the provider still
    // selected, a setup_required error here would just fail the observation.
    // The classified code also lets GeneratorRunner discard the pending inputs
    // without a credential cooldown or a rotation to another key/provider.
    if ((config.allModelsRefused || config.defaultDisabled) && !config.model) {
      const ownRefusal = config.explicitModelRefused === true;
      const disabledDefault = config.defaultDisabled === true && !ownRefusal;
      throw new ClassifiedProviderError(
        `${config.preset.label} has no model left for the observer; no request was sent.`,
        {
          kind: 'unrecoverable',
          cause: new Error(disabledDefault ? 'the implicit default was deliberately disabled' : 'all model candidates were refused by the endpoint'),
          // All three codes end the generation the same way, but the remedy
          // must name the setting that actually left no model to send.
          code: ownRefusal ? OWN_MODEL_NOT_SERVED_CODE
            : disabledDefault ? DEFAULT_MODEL_DISABLED_CODE : HOST_MODEL_NOT_SERVED_CODE,
          action: ownRefusal
            ? `The endpoint refused the model id set in CLAUDE_MEM_OPENAI_COMPAT_MODEL. Set that setting to an id this endpoint serves on ${serveRefusedModelAt(config.sessionEndpointProvider)}; the observer will not switch providers on its own.`
            : disabledDefault
              ? 'CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL=none disables the implicit default. Set CLAUDE_MEM_OPENAI_COMPAT_MODEL to an id this endpoint serves, or remove the override; the provider stays selected and sends nothing until then.'
              : `The endpoint refused every model the host chose for it. Set CLAUDE_MEM_OPENAI_COMPAT_MODEL to an id this endpoint serves on ${serveRefusedModelAt(config.sessionEndpointProvider)}, or turn off the host model opt-in; the observer will not switch providers on its own.`,
        },
      );
    }
    // Resolution drops refused ids, but a config held across turns (a summary
    // tier's own setting, or a session id) can still name one. Check the final
    // model against the same record before opening a socket, so a later turn
    // cannot pay to earn the refusal again.
    if (config.model && isModelRefused(config.apiUrl, config.model)) {
      const hostChosen = hostChosenModelOfRequest(config);
      throw new ClassifiedProviderError(
        `${config.preset.label} does not serve the model id in this request; no request was sent.`,
        {
          kind: 'unrecoverable',
          cause: new Error('the endpoint refused this model id earlier'),
          code: hostChosen ? HOST_MODEL_NOT_SERVED_CODE : OWN_MODEL_NOT_SERVED_CODE,
          action: hostChosen
            ? `The endpoint refused "${config.model}", ${HOST_MODEL_ORIGIN[hostChosen.setting]}. The observer does not send that id again; serve it on ${serveRefusedModelAt(config.sessionEndpointProvider)} and restart the worker, or turn off the host model opt-in.`
            : config.summaryTurn
              ? `The endpoint does not serve the summary model in this request. Set CLAUDE_MEM_TIER_SUMMARY_MODEL to an id this endpoint serves on ${serveRefusedModelAt(config.sessionEndpointProvider)}, or turn off CLAUDE_MEM_TIER_ROUTING_ENABLED. The API key was not changed.`
              : `The endpoint does not serve the model id in this request. Set CLAUDE_MEM_OPENAI_COMPAT_MODEL to an id this endpoint serves on ${serveRefusedModelAt(config.sessionEndpointProvider)}, or use CLAUDE_MEM_OPENAI_COMPAT_DEFAULT_MODEL to replace or disable the preset default. The API key was not changed.`,
        },
      );
    }
    if (!config.apiUrl || !config.model) {
      throw this.missingApiKeyError();
    }
    try {
      const result = await withKeyPool(
        { poolId: keyPoolIdFor(config), keys: resolvePoolKeys(config), label: config.preset.label, rateLimitUntilNextKey },
        ({ key, poolSize }) => this.queryChatCompletions(history, key, poolSize, config, signal, perAttemptTimeoutMs, paidSendBudget, label?.sessionId),
      );
      // Only a completed request proves the endpoint serves this id. Keep the
      // id, never the prompt: a later turn may reuse it when the host and
      // preset choices are both unavailable, without replaying old input.
      // Only the observer's own turns name its model: summaries, field
      // condensation and the Telegram wrap-up run on a different tier's id,
      // which must never replace the observer model's fallback. An unlabelled
      // call is an observer turn (the superclass always supplies a label).
      const kind = label?.kind ?? 'observation';
      if (config.model && (kind === 'init' || kind === 'observation')) {
        rememberServedModel(config.apiUrl, config.model);
      }
      return result;
    } catch (error) {
      // The failed batch is not resent: the error ends this generation as any
      // unrecoverable one does. Only later requests change model.
      const hostChosen = hostChosenModelOfRequest(config);
      if (hostChosen && isClassified(error) && error.code === HOST_MODEL_NOT_SERVED_CODE) rememberHostModelRefused(config, hostChosen.setting);
      // Sibling branch: an id we chose ourselves that the endpoint refused is
      // just as stale. Without this the implicit default is resolved again on
      // every turn and keeps earning the same refusal.
      if (isClassified(error) && error.code === OWN_MODEL_NOT_SERVED_CODE) {
        rememberModelRefused(config.apiUrl, config.model, OWN_MODEL_ORIGIN);
      }
      // A mapped endpoint has no credential of its own, so its auth, quota or
      // rate-limit answer must not reach GeneratorRunner's provider-wide
      // breakers, which would stop sessions on the default endpoint too. Set
      // the mapping aside and end this generation: the batch is discarded like
      // any other unrecoverable stop and never resent.
      const sessionProvider = config.sessionEndpointProvider;
      if (sessionProvider && isClassified(error) && PROVIDER_WIDE_BREAKER_KINDS.has(error.kind)) {
        const provider = redactUserinfo(sessionProvider);
        if (!isSessionEndpointRefused(sessionProvider, config.apiUrl)) {
          refusedSessionEndpoints.add(`${sessionProvider}\n${config.apiUrl}`);
          logger.warn('SDK', `${config.preset.label} refused the endpoint mapped for a session provider (${error.kind}); that provider's sessions go back to the configured endpoint until the worker restarts`, {
            provider,
            url: config.apiUrl,
            kind: error.kind,
          });
        }
        throw new ClassifiedProviderError(`${error.message} (mapped endpoint set aside)`, {
          kind: 'unrecoverable',
          cause: error,
          code: SESSION_ENDPOINT_REFUSED_CODE,
          action: `The endpoint mapped for provider "${provider}" in CLAUDE_MEM_OPENAI_COMPAT_SESSION_ENDPOINTS answered ${error.kind}. That provider's sessions use CLAUDE_MEM_OPENAI_COMPAT_BASE_URL until the worker restarts. Point the entry's keyIndexes at a key this endpoint issued, then restart the worker.`,
          ...(error.url !== undefined ? { url: error.url } : {}),
          ...(error.requestId !== undefined ? { requestId: error.requestId } : {}),
          ...(error.clientAttemptId !== undefined ? { clientAttemptId: error.clientAttemptId } : {}),
          ...(error.paidSendOutcome !== undefined ? { paidSendOutcome: error.paidSendOutcome } : {}),
          ...(error.failedBeforeOutput !== undefined ? { failedBeforeOutput: error.failedBeforeOutput } : {}),
        });
      }
      throw error;
    }
  }

  /**
   * Overrides the streamed request's idle window when set — a test hook, the
   * only reason this is a field. Unset, every request resolves
   * CLAUDE_MEM_LLM_STREAM_IDLE_TIMEOUT_MS (#4737), so a settings change takes
   * effect without a restart.
   */
  protected streamIdleTimeoutMs: number | undefined;

  /**
   * POST the request and read its (streamed) reply. A model that only takes
   * max_completion_tokens gets the #4003 resend inside sendChatCompletion.
   */
  private async requestChatCompletion(
    config: OpenAICompatConfig,
    apiKey: string,
    messages: OpenAIChatMessage[],
    maxOutputTokens: number,
    clientAttemptId: string,
    attemptSignal: AbortSignal,
    liveness: StreamLiveness | null,
    sessionId: string | undefined,
    reasoning: CompatReasoningBody | undefined,
  ): Promise<ChatCompletionExchange> {
    const label = config.preset.label;
    const sessionHeader = config.preset.sessionHeader;
    const includeReasoning = reasoning !== undefined && !endpointRejectsReasoningField(config.apiUrl, reasoning);
    const send = (withReasoning: boolean) => sendChatCompletion({
      url: config.apiUrl,
      headers: {
        'Content-Type': 'application/json',
        // Tracing only; never treated as server-side idempotency.
        'x-client-request-id': clientAttemptId,
        // Local servers accept any token or none; sending an empty bearer to
        // them is worse than sending no header at all.
        ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
        // Per request, from the observed session: one provider instance serves
        // every session, so this can't be a field on it.
        ...(sessionHeader && sessionId ? { [sessionHeader]: sessionId } : {}),
      },
      body: {
        model: config.model,
        messages,
        temperature: 0.3,
        ...(liveness ? STREAMED_REQUEST_FIELDS : {}),
        ...(withReasoning ? reasoning : {}),
      },
      maxOutputTokens,
      signal: attemptSignal,
      liveness,
      label,
      classify: (input) => classifyOpenAICompatError({ ...input, endpointLabel: label, requestUrl: config.apiUrl, hostChosenModel: hostChosenModelOfRequest(config), sessionEndpointProvider: config.sessionEndpointProvider, summaryTurn: config.summaryTurn }),
    });
    try {
      return await send(includeReasoning);
    } catch (error) {
      // Strict schemas (vLLM) 400 on the unknown field. Remember the URL and
      // resend once without it. A model that says reasoning is mandatory is
      // not this: stripping the field would hide that refusal.
      const text = errorText(error);
      if (!includeReasoning || !isClassified(error) || error.kind !== 'unrecoverable' || !isUnknownReasoningParameter(text)) {
        throw error;
      }
      if (rememberEndpointRejectsReasoningField(config.apiUrl, reasoning)) {
        logger.warn('SDK', `${label} rejected the reasoning field; resending without it`, { url: config.apiUrl });
      }
      return send(false);
    }
  }

  private async queryChatCompletions(
    history: ConversationMessage[],
    apiKey: string,
    /** Size of the rotation pool this attempt belongs to; 1 means no rotation. */
    poolSize: number,
    config: OpenAICompatConfig,
    signal?: AbortSignal,
    perAttemptTimeoutMs?: number,
    paidSendBudget?: PaidSendBudget,
    /** anonymousSessionId() of the observed session, for preset.sessionHeader. */
    sessionId?: string,
  ): Promise<ProviderQueryResult> {
    const messages = this.conversationToOpenAIMessages(history);
    const label = config.preset.label;
    const clientAttemptId = paidSendBudget?.clientAttemptId ?? randomUUID();
    const maxOutputTokens = resolveObserverMaxOutputTokens();
    const settings = SettingsDefaultsManager.loadFromFile(USER_SETTINGS_PATH);
    const effort = resolveOpenAICompatReasoningEffort(settings.CLAUDE_MEM_OPENAI_COMPAT_REASONING_EFFORT);
    const usePreset = resolveOpenAICompatReasoning(settings.CLAUDE_MEM_OPENAI_COMPAT_REASONING) === 'off';
    const reasoning = (effort || usePreset) ? openAICompatReasoningBody(config.preset.reasoningOff, effort) : undefined;
    // The budget retry asks for no thinking in the same wire format as the initial request.
    const reasoningOff = openAICompatReasoningBody(effort ? 'effort-none' : config.preset.reasoningOff);

    logger.debug('SDK', `Querying ${label} multi-turn (${config.model})`, {
      turns: history.length,
      totalChars: history.reduce((sum, m) => sum + m.content.length, 0),
      maxOutputTokens,
    });

    // Decided once, so every attempt is bounded the way withRetry was told.
    const streamed = streamsChatCompletion(config.apiUrl);
    const liveness = streamed
      ? resolveStreamLiveness(perAttemptTimeoutMs, this.streamIdleTimeoutMs ?? resolveStreamIdleTimeoutMs())
      : null;
    let data = await withRetry<ChatCompletionResponse>(async (attemptSignal) => {
      const exchange = await this.requestChatCompletion(config, apiKey, messages, maxOutputTokens, clientAttemptId, attemptSignal, liveness, sessionId, reasoning);
      const responseData = exchange.body as ChatCompletionResponse;

      // Some gateways report failure in a 200 body (the case #3263 hit through
      // OpenRouter). Treat it exactly like the equivalent HTTP status.
      if (responseData.error) {
        throw classifyOpenAICompatError({
          status: exchange.status,
          bodyText: JSON.stringify(responseData),
          headers: exchange.headers,
          cause: new Error(`${label} API error: ${responseData.error.code} - ${responseData.error.message}`),
          endpointLabel: label,
          hostChosenModel: hostChosenModelOfRequest(config),
          sessionEndpointProvider: config.sessionEndpointProvider,
        });
      }

      return responseData;
    }, {
      label: `${label} ${config.model}`,
      abortSignal: signal,
      perAttemptTimeoutMs,
      paidSendBudget,
      clientAttemptId,
      // A streamed request is bounded by its idle timeout and absolute cap, and
      // may be resent once if it fails before any output.
      ...(streamed ? { attemptDeadlineOwnedByCaller: true, retryBeforeOutput: true } : {}),
      ...(signal ? { maxRetries: 0 } : {}),
      ...retryPolicyForPool(poolSize),
    });

    let choice = data.choices?.[0];
    // Text blocks only: reasoning and tool-call arguments are never the answer,
    // and neither is a leading <think> block in the text itself.
    let content = stripLeadingThinkBlock(assistantText(choice?.message?.content));
    let finishReason = typeof choice?.finish_reason === 'string' ? choice.finish_reason : undefined;
    const warnIfCutOff = () => {
      if (finishReason !== 'length') return;
      logger.warn('SDK', `${label} reply was cut off at the output-token limit`, {
        model: config.model,
        maxTokens: maxOutputTokens,
        outputTokens: data.usage?.completion_tokens,
        contentChars: content.length,
        clientAttemptId,
      });
    };
    warnIfCutOff();

    const canDisableReasoning = !requestDisablesReasoning(reasoning ?? {})
      && !endpointRejectsReasoningField(config.apiUrl, reasoningOff);
    if (isLengthTruncatedEmptyReply(content, finishReason) && canDisableReasoning) {
      if (!claimReasoningBudgetRetry(paidSendBudget)) {
        logger.error('SDK', reasoningBudgetSkippedMessage(label, paidSendBudget?.spentPaidSends ?? 0), {
          model: config.model,
          finishReason,
          maxTokens: maxOutputTokens,
        });
      } else {
        const firstUsage = data.usage;
        logger.error('SDK', reasoningBudgetRetryMessage(label), { model: config.model, finishReason, maxTokens: maxOutputTokens });
        const deadline = liveness ? undefined : startReasoningRetryDeadline(signal, perAttemptTimeoutMs);
        try {
          const retrySignal = deadline?.signal ?? signal ?? new AbortController().signal;
          const exchange = await this.requestChatCompletion(
            config, apiKey, messages, maxOutputTokens, clientAttemptId, retrySignal, liveness, sessionId, reasoningOff,
          );
          const retryData = exchange.body as ChatCompletionResponse;
          if (retryData.error) {
            throw classifyOpenAICompatError({
              status: exchange.status,
              bodyText: JSON.stringify(retryData),
              headers: exchange.headers,
              cause: new Error(`${label} API error: ${retryData.error.code} - ${retryData.error.message}`),
              endpointLabel: label,
              hostChosenModel: hostChosenModelOfRequest(config),
              sessionEndpointProvider: config.sessionEndpointProvider,
            });
          }
          data = withCombinedCompatUsage(firstUsage, retryData);
          choice = data.choices?.[0];
          content = stripLeadingThinkBlock(assistantText(choice?.message?.content));
          finishReason = typeof choice?.finish_reason === 'string' ? choice.finish_reason : undefined;
          warnIfCutOff();
          if (isLengthTruncatedEmptyReply(content, finishReason)) {
            logger.error('SDK', reasoningBudgetGiveUpMessage(label, finishReason), { model: config.model });
          }
        } catch (error) {
          signal?.throwIfAborted();
          logger.error('SDK', reasoningBudgetGiveUpMessage(label, finishReason), {
            model: config.model,
          }, error instanceof Error ? error : new Error(String(error)));
          // This retry runs outside withRetry, so its catch was the only place
          // that saw a model refusal and it used to swallow it. Let the two
          // refusal codes reach query()'s one remembering catch; every other
          // class stays logged and swallowed, with no second retry or replay.
          if (isClassified(error)
            && (error.code === HOST_MODEL_NOT_SERVED_CODE || error.code === OWN_MODEL_NOT_SERVED_CODE)) {
            throw error;
          }
        } finally {
          deadline?.finish();
        }
      }
    } else if (isLengthTruncatedEmptyReply(content, finishReason)) {
      logger.error('SDK', reasoningBudgetGiveUpMessage(label, finishReason), {
        model: config.model,
        maxTokens: maxOutputTokens,
      });
    } else if (!content) {
      logger.error('SDK', `Empty response from ${label}`, { model: config.model, finishReason });
    }

    const inputTokens = data.usage?.prompt_tokens;
    const outputTokens = data.usage?.completion_tokens;
    const tokensUsed = data.usage?.total_tokens;
    const { cachedInputTokens, cacheWriteInputTokens, uncachedInputTokens } =
      extractPromptCacheUsage(data.usage);
    const servedModel = typeof data.model === 'string' && data.model ? data.model : undefined;

    if (tokensUsed) {
      logger.info('SDK', `${label} API usage`, {
        model: servedModel ?? config.model,
        inputTokens: inputTokens ?? 0,
        ...(cachedInputTokens !== undefined ? { cachedInputTokens } : {}),
        ...(cacheWriteInputTokens !== undefined ? { cacheWriteInputTokens } : {}),
        ...(uncachedInputTokens !== undefined ? { uncachedInputTokens } : {}),
        outputTokens: outputTokens ?? 0,
        totalTokens: tokensUsed,
        messagesInContext: history.length,
        clientAttemptId,
      });
    }

    return {
      content,
      ...(tokensUsed !== undefined ? { tokensUsed } : {}),
      ...(inputTokens !== undefined ? { inputTokens } : {}),
      ...(outputTokens !== undefined ? { outputTokens } : {}),
      ...(cachedInputTokens !== undefined ? { cachedInputTokens } : {}),
      ...(cacheWriteInputTokens !== undefined ? { cacheWriteInputTokens } : {}),
      ...(servedModel ? { servedModel } : {}),
      ...(finishReason ? { finishReason } : {}),
    };
  }
}

/**
 * Usable when there is an endpoint and a model, and a key unless the preset is
 * a local server that does not need one. Availability is deliberately strict:
 * dispatch falls through to Claude when it is false, and a half-configured
 * endpoint should fall through rather than fail every observation.
 *
 * A session-selected model can supply the missing model only when the operator
 * opted in with CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION='true'. Without the
 * gate or without an id, the strict global-only rule stands. The endpoint and
 * key requirements are never relaxed by a session id.
 */
export function isOpenAICompatAvailable(
  settingsPath: string = paths.settings(),
  sessionModel?: string,
): boolean {
  const config = resolveOpenAICompatConfig(settingsPath);
  const settings = SettingsDefaultsManager.loadFromFile(settingsPath);
  const fromSession = settings.CLAUDE_MEM_OPENAI_COMPAT_MODEL_FROM_SESSION === 'true'
    && typeof sessionModel === 'string'
    ? sessionModel.trim()
    : '';
  // An id the endpoint answered 404 for supplies no model: with none configured
  // either, dispatch falls through as it does for any half-configured endpoint.
  const usableFromSession = fromSession && !isHostModelRefused(config.apiUrl, fromSession) ? fromSession : '';
  const model = usableFromSession || config.model;
  if (!config.apiUrl) return false;
  // All models refused is a configured endpoint with nothing left to send, not
  // an unconfigured one: keep dispatch here so query() can stop with the real
  // refusal instead of silently moving the work to Claude.
  if (!model && !config.allModelsRefused && !config.defaultDisabled) return false;
  if (!config.requiresApiKey) return true;
  return config.apiKeys.length > 0;
}

export function isOpenAICompatSelected(settingsPath: string = paths.settings()): boolean {
  const settings = SettingsDefaultsManager.loadFromFile(settingsPath);
  return settings.CLAUDE_MEM_PROVIDER === 'openai-compatible';
}