Files
openclaw/src/agents/context.ts
Peter Steinberger bb46b79d3c refactor: internalize OpenClaw agent runtime (#85341)
* refactor: extract agent core package

Introduce packages/agent-core as the OpenClaw-owned home for reusable agent loop, harness, session, prompt, and runtime dependency contracts.

* refactor: extract shared llm runtime

Move provider model registries, stream wrappers, OAuth helpers, and LLM utilities into src/llm with plugin-sdk barrels instead of depending on the old embedded runtime layout.

* refactor: remove pi runtime internals

Rename remaining Pi-shaped agent surfaces to OpenClaw agent runtime names, delete obsolete Pi docs and package graph checks, and add the third-party notice for incorporated code.

* refactor: tighten agent session runtime

Make agent-core/runtime dependencies explicit, consolidate compaction and session transcript helpers, and move model/session helpers behind OpenClaw-owned contracts.

* refactor: remove static model and pi auth paths

Drop static model catalogs and Pi auth bridges, move model/provider facts to manifest-owned runtime contracts, and harden internal embedded-agent utilities.

* refactor: remove legacy provider compat paths

* docs: remove agent parity notes

* fix: skip provider wildcard metadata parsing

* refactor: share session extension sdk loading

* refactor: inline acpx proxy error formatter

* refactor: fold edit recovery into edit tool

* fix: accept extension batch separator

* test: align startup provider plugin expectations

* fix: restore provider-scoped release discovery

* test: align static asset packaging expectations

* fix: run static provider catalogs during scoped discovery

* fix: add provider entry catalogs for scoped live discovery

* fix: load lightweight provider catalog entries

* fix: refresh provider-scoped plugin metadata

* fix: keep provider catalog entries on release live path

* fix: keep static manifest models in release live checks

* fix: harden release model discovery

* fix: reduce OpenAI live cache probe reasoning

* fix: disable OpenAI cache probe reasoning

* ci: extend OpenAI gateway live timeout

* fix: extend live gateway model budget

* fix: stabilize release validation regressions

* fix: honor provider aliases in model rows

* fix: stabilize release validation lanes

* fix: stabilize release memory qa

* ci: stabilize release validation lanes

* ci: prefer ipv4 for live docker node calls

* fix: restore shared tool-call stream wrapper

* ci: remove legacy pi test shard alias

* fix: clean up embedded agent test drift

* fix: stabilize runtime alias status

* fix: clean up embedded agent ci drift

* fix: restore release ci invariants

* fix: clean up post-rebase runtime drift

* fix: restore release ci checks

* fix: restore release ci after rebase

* fix: remove stale pi runtime path

* test: align compaction runtime expectations

* test: update plugin prerelease expectations

* fix: handle claude live tool approvals

* fix: stabilize release validation gates

* fix: finish agent runtime import

* test: finish post-rebase agent runtime mocks

* fix: keep codex compaction native

* fix: stabilize codex app-server hook tests

* test: isolate codex diagnostic active run

* test: remove codex diagnostic completion race

# Conflicts:
#	extensions/codex/src/app-server/run-attempt.test.ts

* ci: fix full release manifest performance run id

* refactor: narrow llm plugin sdk boundary

* chore: drop generated google boundary stamps

* fix: repair rebase fallout

* fix: clean up rebased runtime references

* fix: decode codex jwt payloads as base64url

* fix: preserve shipped pi runtime alias

* fix: add scoped sdk virtual modules

* fix: decode llm codex oauth jwt as base64url

* fix: avoid stale vertex adc negative cache

* fix: harden tool arg decoding and codeql path

* fix: keep vertex adc negative checks live

* refactor: consolidate codex jwt and edit helpers

* fix: await codex oauth node runtime imports

* fix: preserve sdk tool and notice contracts

* fix: preserve shipped compat config boundaries

* fix: align codex oauth callback host

* fix: terminate agent-core loop streams on failure

* fix: keep codex oauth callback alive during fallback

* ci: include session tools in critical codeql scans

* fix: keep Cloudflare Anthropic provider auth header

* docs: redirect legacy pi runtime pages

* fix: honor bundled web provider compat discovery

* fix: protect session output spill files

* fix: keep legacy agent dir env blocked

* fix: contain auto-discovered skill symlinks

* fix: harden agent core sdk proxy surfaces

* fix: restore approval reaction sdk compat

* fix: keep live docker runs bounded

* fix: keep codex oauth redirect host aligned

* fix: resolve post-rebase agent runtime drift

* fix: redact anthropic oauth parse failures

* fix: preserve responses strict tool shaping

* fix: repair agent runtime rebase cleanup

* docs: redirect retired parity pages

* fix: bound auto-discovered resources to roots

* fix: repair post-rebase agent test drift

* fix: preserve bundled provider allowlist migration

* fix: preserve manifest-owned provider aliases

* fix: declare photon image dependency

* fix: keep provider headers out of proxy body

* fix: preserve shipped env aliases

* fix: refresh control ui i18n generated state

* fix: quote read fallback paths

* fix: preview edits through configured backend

* test: satisfy core test typecheck

* fix: preserve ZAI usage auth fallback

* test: repair codex diagnostic test

* fix: repair agent runtime rebase drift

* test: finish embedded runner import rename

* fix: repair agent runtime rebase integrations

* test: align compaction oauth fallback expectations

* fix: allow sdk-auth session models

* fix: update doctor tool schema import

* fix: preserve bedrock plugin region

* fix: stream harmony-like prose immediately

* ci: include session runtime in codeql shards

* fix: repair latest rebase integrations

* fix: honor explicit codex websocket transport

* fix: keep openai-compatible credentials provider-scoped

* fix: refresh sdk api baseline after rebase

* fix: route cli runtime aliases through openclaw harness

* test: rename stale harness mock expectation

* test: rename embedded agent overflow calls

* test: clean embedded auth test wording

* test: use openclaw stream types in deepinfra cache test

* fix: refresh sdk api baseline on latest main

* fix: honor bundled discovery compat allowlists

* fix: refresh sdk api baseline after latest rebase

* fix: remove stale rebase imports

* test: rename stale model catalog mock

* test: mock renamed doctor runtime modules

* fix: map canonical kimi env auth

* fix: use internal model registry in bench script

* fix: migrate deepinfra provider catalog entry

* fix: enforce builtin tool suppression

* fix: route compaction auth and proxy payloads safely

* refactor: prune unused llm registry leftovers

* test: update codex hooks session import

* test: fix model picker ci coverage

* test: align model picker auth mock types
2026-05-27 19:24:04 +01:00

449 lines
16 KiB
TypeScript

// Load session runtime model metadata so we can infer context windows when the
// agent reports a model id. This includes custom models.json entries.
import { getRuntimeConfig } from "../config/config.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { computeBackoff, type BackoffPolicy } from "../infra/backoff.js";
import { normalizeLowercaseStringOrEmpty } from "../shared/string-coerce.js";
import { discoverAuthStorage, discoverModels } from "./agent-model-discovery.js";
import { resolveDefaultAgentDir } from "./agent-scope.js";
import { lookupCachedContextTokens, MODEL_CONTEXT_TOKEN_CACHE } from "./context-cache.js";
import { CONTEXT_WINDOW_RUNTIME_STATE } from "./context-runtime-state.js";
import { normalizeProviderId } from "./model-selection.js";
export { resetContextWindowCacheForTest } from "./context-runtime-state.js";
type ModelEntry = {
id: string;
provider?: string;
contextWindow?: number;
contextTokens?: number;
};
type ModelRegistryLike = {
getAvailable?: () => ModelEntry[];
getAll: () => ModelEntry[];
};
type ConfigModelEntry = { id?: string; contextWindow?: number; contextTokens?: number };
type ProviderConfigEntry = {
contextWindow?: number;
contextTokens?: number;
models?: ConfigModelEntry[];
};
type ModelsConfig = { providers?: Record<string, ProviderConfigEntry | undefined> };
const ANTHROPIC_GA_1M_MODEL_PREFIXES = [
"claude-opus-4-6",
"claude-opus-4.6",
"claude-opus-4-7",
"claude-opus-4.7",
"claude-sonnet-4-6",
"claude-sonnet-4.6",
] as const;
export const ANTHROPIC_CONTEXT_1M_TOKENS = 1_048_576;
const CONFIG_LOAD_RETRY_POLICY: BackoffPolicy = {
initialMs: 1_000,
maxMs: 60_000,
factor: 2,
jitter: 0,
};
export function applyDiscoveredContextWindows(params: {
cache: Map<string, number>;
models: ModelEntry[];
}) {
for (const model of params.models) {
if (!model?.id) {
continue;
}
const discoveredContextTokens =
typeof model.contextTokens === "number"
? Math.trunc(model.contextTokens)
: typeof model.contextWindow === "number"
? Math.trunc(model.contextWindow)
: undefined;
const contextTokens = shouldUseDiscoveredAnthropicGa1MContextWindow(model)
? ANTHROPIC_CONTEXT_1M_TOKENS
: discoveredContextTokens;
if (!contextTokens || contextTokens <= 0) {
continue;
}
const existing = params.cache.get(model.id);
// Cache the most conservative effective limit. Provider/runtime callers that
// know the active provider should still prefer qualified lookups first.
if (existing === undefined || contextTokens < existing) {
params.cache.set(model.id, contextTokens);
}
}
}
export function applyConfiguredContextWindows(params: {
cache: Map<string, number>;
modelsConfig: ModelsConfig | undefined;
}) {
const providers = params.modelsConfig?.providers;
if (!providers || typeof providers !== "object") {
return;
}
for (const provider of Object.values(providers)) {
if (!Array.isArray(provider?.models)) {
continue;
}
for (const model of provider.models) {
const modelId = typeof model?.id === "string" ? model.id : undefined;
const contextTokens =
typeof model?.contextTokens === "number"
? model.contextTokens
: typeof model?.contextWindow === "number"
? model.contextWindow
: typeof provider?.contextTokens === "number"
? provider.contextTokens
: typeof provider?.contextWindow === "number"
? provider.contextWindow
: undefined;
if (!modelId || !contextTokens || contextTokens <= 0) {
continue;
}
params.cache.set(modelId, contextTokens);
}
}
}
function loadModelsConfigRuntime() {
return CONTEXT_WINDOW_RUNTIME_STATE.modelsConfigRuntimeLoader.load();
}
function primeConfiguredContextWindows(): OpenClawConfig | undefined {
if (CONTEXT_WINDOW_RUNTIME_STATE.configuredConfig) {
applyConfiguredContextWindows({
cache: MODEL_CONTEXT_TOKEN_CACHE,
modelsConfig: CONTEXT_WINDOW_RUNTIME_STATE.configuredConfig.models as
| ModelsConfig
| undefined,
});
return CONTEXT_WINDOW_RUNTIME_STATE.configuredConfig;
}
if (Date.now() < CONTEXT_WINDOW_RUNTIME_STATE.nextConfigLoadAttemptAtMs) {
return undefined;
}
try {
const cfg = getRuntimeConfig();
applyConfiguredContextWindows({
cache: MODEL_CONTEXT_TOKEN_CACHE,
modelsConfig: cfg.models as ModelsConfig | undefined,
});
CONTEXT_WINDOW_RUNTIME_STATE.configuredConfig = cfg;
CONTEXT_WINDOW_RUNTIME_STATE.configLoadFailures = 0;
CONTEXT_WINDOW_RUNTIME_STATE.nextConfigLoadAttemptAtMs = 0;
return cfg;
} catch {
CONTEXT_WINDOW_RUNTIME_STATE.configLoadFailures += 1;
const backoffMs = computeBackoff(
CONFIG_LOAD_RETRY_POLICY,
CONTEXT_WINDOW_RUNTIME_STATE.configLoadFailures,
);
CONTEXT_WINDOW_RUNTIME_STATE.nextConfigLoadAttemptAtMs = Date.now() + backoffMs;
// If config can't be loaded, leave cache empty and retry after backoff.
return undefined;
}
}
export function ensureContextWindowCacheLoaded(): Promise<void> {
if (CONTEXT_WINDOW_RUNTIME_STATE.loadPromise) {
return CONTEXT_WINDOW_RUNTIME_STATE.loadPromise;
}
const cfg = primeConfiguredContextWindows();
if (!cfg) {
return Promise.resolve();
}
CONTEXT_WINDOW_RUNTIME_STATE.loadPromise = (async () => {
try {
await (await loadModelsConfigRuntime()).ensureOpenClawModelsJson(cfg);
} catch {
// Continue with best-effort discovery/overrides.
}
try {
const agentDir = resolveDefaultAgentDir(cfg);
const authStorage = discoverAuthStorage(agentDir);
const modelRegistry = discoverModels(authStorage, agentDir, {
normalizeModels: false,
}) as unknown as ModelRegistryLike;
const models =
typeof modelRegistry.getAvailable === "function"
? modelRegistry.getAvailable()
: modelRegistry.getAll();
applyDiscoveredContextWindows({
cache: MODEL_CONTEXT_TOKEN_CACHE,
models,
});
} catch {
// If model discovery fails, continue with config overrides only.
}
applyConfiguredContextWindows({
cache: MODEL_CONTEXT_TOKEN_CACHE,
modelsConfig: cfg.models as ModelsConfig | undefined,
});
})().catch(() => {
// Keep lookup best-effort.
});
return CONTEXT_WINDOW_RUNTIME_STATE.loadPromise;
}
export function lookupContextTokens(
modelId?: string,
options?: { allowAsyncLoad?: boolean; skipRuntimeConfigLoad?: boolean },
): number | undefined {
if (!modelId) {
return undefined;
}
if (options?.skipRuntimeConfigLoad) {
return lookupCachedContextTokens(modelId);
}
if (options?.allowAsyncLoad === false) {
// Read-only callers still need synchronous config-backed overrides, but they
// should not start background model discovery or models.json writes.
primeConfiguredContextWindows();
} else {
// Best-effort: kick off loading on demand, but don't block lookups.
void ensureContextWindowCacheLoaded();
}
return lookupCachedContextTokens(modelId);
}
function resolveProviderModelRef(params: {
provider?: string;
model?: string;
}): { provider: string; model: string } | undefined {
const modelRaw = params.model?.trim();
if (!modelRaw) {
return undefined;
}
const providerRaw = params.provider?.trim();
if (providerRaw) {
const provider = normalizeProviderId(providerRaw);
if (!provider) {
return undefined;
}
return { provider, model: modelRaw };
}
const slash = modelRaw.indexOf("/");
if (slash <= 0) {
return undefined;
}
const provider = normalizeProviderId(modelRaw.slice(0, slash));
const model = modelRaw.slice(slash + 1).trim();
if (!provider || !model) {
return undefined;
}
return { provider, model };
}
// Look up an explicit runtime context cap for a specific provider+model
// directly from config, without going through the shared discovery cache.
// This avoids the cache keyspace collision where "provider/model" synthetic
// keys overlap with raw slash-containing model IDs (e.g. OpenRouter's
// "google/gemini-2.5-pro" stored as a raw catalog entry).
function resolveConfiguredProviderContextTokens(
cfg: OpenClawConfig | undefined,
provider: string,
model: string,
): number | undefined {
const providers = (cfg?.models as ModelsConfig | undefined)?.providers;
if (!providers) {
return undefined;
}
// Mirror the lookup order in embedded-agent-runner/model.ts: exact key first,
// then normalized fallback. This prevents alias collisions from picking the
// wrong configured cap based on Object.entries iteration order.
function readProviderContextTokens(providerConfig: ProviderConfigEntry | undefined) {
return typeof providerConfig?.contextTokens === "number"
? providerConfig.contextTokens
: typeof providerConfig?.contextWindow === "number"
? providerConfig.contextWindow
: undefined;
}
function findContextTokens(matchProviderId: (id: string) => boolean): number | undefined {
for (const [providerId, providerConfig] of Object.entries(providers!)) {
if (!matchProviderId(providerId)) {
continue;
}
if (Array.isArray(providerConfig?.models)) {
for (const m of providerConfig.models) {
const contextTokens =
typeof m?.contextTokens === "number"
? m.contextTokens
: typeof m?.contextWindow === "number"
? m.contextWindow
: undefined;
if (
typeof m?.id === "string" &&
m.id === model &&
typeof contextTokens === "number" &&
contextTokens > 0
) {
return contextTokens;
}
}
}
const providerContextTokens = readProviderContextTokens(providerConfig);
if (typeof providerContextTokens === "number" && providerContextTokens > 0) {
return providerContextTokens;
}
}
return undefined;
}
// 1. Exact match (case-insensitive, no alias expansion).
const exactResult = findContextTokens(
(id) => normalizeLowercaseStringOrEmpty(id) === normalizeLowercaseStringOrEmpty(provider),
);
if (exactResult !== undefined) {
return exactResult;
}
// 2. Normalized fallback: covers case-only provider key differences.
const normalizedProvider = normalizeProviderId(provider);
return findContextTokens((id) => normalizeProviderId(id) === normalizedProvider);
}
function isAnthropic1MModel(provider: string, model: string): boolean {
if (provider !== "anthropic" && provider !== "claude-cli") {
return false;
}
const modelId = resolveModelFamilyId(model);
return ANTHROPIC_GA_1M_MODEL_PREFIXES.some((prefix) => modelId.startsWith(prefix));
}
function shouldUseAnthropicGa1MContextWindow(params: {
provider?: string;
model: string;
}): boolean {
const provider = params.provider ? normalizeProviderId(params.provider) : "";
return isAnthropic1MModel(provider, params.model);
}
function shouldUseDiscoveredAnthropicGa1MContextWindow(model: ModelEntry): boolean {
const provider =
typeof model.provider === "string" ? normalizeProviderId(model.provider) : undefined;
const modelId = model.id;
if (provider) {
return isAnthropic1MModel(provider, modelId);
}
const normalized = normalizeLowercaseStringOrEmpty(modelId);
const slash = normalized.indexOf("/");
if (slash < 0) {
return false;
}
const inferredProvider = normalizeProviderId(normalized.slice(0, slash));
const inferredModel = normalized.slice(slash + 1);
return inferredProvider === "claude-cli" && isAnthropic1MModel(inferredProvider, inferredModel);
}
function resolveModelFamilyId(modelId: string): string {
const normalized = normalizeLowercaseStringOrEmpty(modelId);
return normalized.includes("/") ? (normalized.split("/").at(-1) ?? normalized) : normalized;
}
export function resolveContextTokensForModel(params: {
cfg?: OpenClawConfig;
provider?: string;
model?: string;
contextTokensOverride?: number;
fallbackContextTokens?: number;
allowAsyncLoad?: boolean;
}): number | undefined {
if (typeof params.contextTokensOverride === "number" && params.contextTokensOverride > 0) {
return params.contextTokensOverride;
}
const ref = resolveProviderModelRef({
provider: params.provider,
model: params.model,
});
const explicitProvider = params.provider?.trim();
if (ref) {
if (explicitProvider && isAnthropic1MModel(ref.provider, ref.model)) {
return ANTHROPIC_CONTEXT_1M_TOKENS;
}
// Only do the config direct scan when the caller explicitly passed a
// provider. When provider is inferred from a slash in the model string
// (e.g. "google/gemini-2.5-pro" → ref.provider = "google"), the model ID
// may belong to a DIFFERENT provider (e.g. an OpenRouter session). Scanning
// cfg.models.providers.google in that case would return Google's configured
// window and misreport context limits for the OpenRouter session.
// See status.ts log-usage fallback which calls with only { model } set.
if (explicitProvider) {
const configuredWindow = resolveConfiguredProviderContextTokens(
params.cfg,
explicitProvider,
ref.model,
);
if (configuredWindow !== undefined) {
return configuredWindow;
}
}
}
if (explicitProvider && ref && shouldUseAnthropicGa1MContextWindow(ref)) {
return ANTHROPIC_CONTEXT_1M_TOKENS;
}
// When provider is explicitly given and the model ID is bare (no slash),
// try the provider-qualified cache key BEFORE the bare key. Discovery
// entries are stored under qualified IDs (e.g. "google-gemini-cli/
// gemini-3.1-pro-preview → 1M"), while the bare key may hold a cross-
// provider minimum (128k). Returning the qualified entry gives the correct
// provider-specific window for /status and session context-token persistence.
//
// Guard: only when params.provider is explicit (not inferred from a slash in
// the model string). For model-only callers (e.g. status.ts log-usage
// fallback with model="google/gemini-2.5-pro"), the inferred provider would
// construct "google/gemini-2.5-pro" as the qualified key which accidentally
// matches OpenRouter's raw discovery entry — the bare lookup is correct there.
if (params.provider && ref && !ref.model.includes("/")) {
const qualifiedResult = lookupContextTokens(
`${normalizeProviderId(ref.provider)}/${ref.model}`,
{
allowAsyncLoad: params.allowAsyncLoad,
skipRuntimeConfigLoad: Boolean(params.cfg),
},
);
if (qualifiedResult !== undefined) {
return qualifiedResult;
}
}
// Bare key fallback. For model-only calls with slash-containing IDs
// (e.g. "google/gemini-2.5-pro") this IS the raw discovery cache key.
const bareResult = lookupContextTokens(params.model, {
allowAsyncLoad: params.allowAsyncLoad,
skipRuntimeConfigLoad: Boolean(params.cfg),
});
if (bareResult !== undefined) {
return bareResult;
}
// When provider is implicit, try qualified as a last resort so inferred
// provider/model pairs (e.g. model="google-gemini-cli/gemini-3.1-pro")
// still find discovery entries stored under that qualified ID.
if (!params.provider && ref && !ref.model.includes("/")) {
const qualifiedResult = lookupContextTokens(
`${normalizeProviderId(ref.provider)}/${ref.model}`,
{
allowAsyncLoad: params.allowAsyncLoad,
skipRuntimeConfigLoad: Boolean(params.cfg),
},
);
if (qualifiedResult !== undefined) {
return qualifiedResult;
}
}
return params.fallbackContextTokens;
}