Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat: carry the catalog context window to subscription tasks (#731)

Alfredo Lopezedf6e0877fbf
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index 8364d87ea..930aee47e 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -600,8 +600,28 @@ export const CATALOG_LIMITS = {
maxBodyBytes: 8 * 1_048_576,
maxModels: 256,
maxModelIdLength: 128,
+ // control-api stores the window in a Postgres INTEGER column; a larger value
+ // would fail the whole catalog sync, so it is omitted here instead.
+ maxContextWindowTokens: 2_147_483_647,
} as const
+type CatalogModel = { model: string; displayName?: string; contextWindowTokens?: number }
+
+/**
+ * The upstream row's `context_window`, when it is a positive integer the
+ * catalog store can hold. `max_context_window` is an opt-in extension, not the
+ * window a request gets, so it is not read.
+ */
+function contextWindowOf(row: Record<string, unknown>): number | undefined {
+ const value = row.context_window
+ return typeof value === 'number' &&
+ Number.isInteger(value) &&
+ value > 0 &&
+ value <= CATALOG_LIMITS.maxContextWindowTokens
+ ? value
+ : undefined
+}
+
export async function listCodexModels(input: {
accessToken: string
fetchFn: typeof fetch
@@ -610,7 +630,7 @@ export async function listCodexModels(input: {
timeoutMs?: number
}): Promise<{
outcome: 'ready' | 'auth-rejected' | 'unavailable'
- models: Array<{ model: string; displayName?: string }>
+ models: CatalogModel[]
}> {
const url = assertAllowedUpstreamUrl(CODEX_CATALOG_ORIGIN, 'catalog')
const headers = chatgptUpstreamHeaders(input.accessToken, { accept: 'application/json' })
@@ -659,7 +679,7 @@ export async function testCodexConnection(input: {
return { outcome: listed.outcome }
}
-function normalizeModels(body: unknown): Array<{ model: string; displayName?: string }> {
+function normalizeModels(body: unknown): CatalogModel[] {
const rows = Array.isArray(body)
? body
: isPlainObject(body) && Array.isArray(body.models)
@@ -667,7 +687,7 @@ function normalizeModels(body: unknown): Array<{ model: string; displayName?: st
: isPlainObject(body) && Array.isArray(body.data)
? body.data
: []
- const models: Array<{ model: string; displayName?: string }> = []
+ const models: CatalogModel[] = []
let droppedOverlongIds = 0
let droppedOverLimit = 0
for (const row of rows) {
@@ -688,7 +708,12 @@ function normalizeModels(body: unknown): Array<{ model: string; displayName?: st
: typeof row.title === 'string'
? row.title
: undefined
- models.push(displayName ? { model, displayName } : { model })
+ const contextWindowTokens = contextWindowOf(row)
+ models.push({
+ model,
+ ...(displayName ? { displayName } : {}),
+ ...(contextWindowTokens !== undefined ? { contextWindowTokens } : {}),
+ })
}
if (droppedOverlongIds > 0 || droppedOverLimit > 0) {
logger.warn(
diff --git a/codex-llm-proxy/test/catalogBounds.test.ts b/codex-llm-proxy/test/catalogBounds.test.ts
index 475f434c7..fa15dd5cb 100644
--- a/codex-llm-proxy/test/catalogBounds.test.ts
+++ b/codex-llm-proxy/test/catalogBounds.test.ts
@@ -28,6 +28,7 @@ describe('Codex catalog upstream bounds', () => {
maxBodyBytes: 8 * 1_048_576,
maxModels: 256,
maxModelIdLength: 128,
+ maxContextWindowTokens: 2_147_483_647,
})
})
diff --git a/control-api/package-lock.json b/control-api/package-lock.json
index 026eff3ee..0056e37ad 100644
--- a/control-api/package-lock.json
+++ b/control-api/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-control-api",
- "version": "0.1.602",
+ "version": "0.1.603",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-control-api",
- "version": "0.1.602",
+ "version": "0.1.603",
"dependencies": {
"@clerum/display-field": "file:../packages/display-field",
"@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",
diff --git a/control-api/package.json b/control-api/package.json
index 8ec051e38..4bcfb5ef1 100644
--- a/control-api/package.json
+++ b/control-api/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-control-api",
- "version": "0.1.602",
+ "version": "0.1.603",
"description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets",
"main": "dist/main.js",
"engines": {
diff --git a/control-api/src/services/codexSubscriptionCatalog.ts b/control-api/src/services/codexSubscriptionCatalog.ts
index 8d3c4d565..d3258f0ad 100644
--- a/control-api/src/services/codexSubscriptionCatalog.ts
+++ b/control-api/src/services/codexSubscriptionCatalog.ts
@@ -89,7 +89,8 @@ export type CodexCatalogRow = {
export type CodexCatalogPlan = {
inserts: CodexDiscoveredModel[]
- refresh: string[]
+ /** Existing discovery rows seen again, carrying what the catalog now reports. */
+ refresh: CodexDiscoveredModel[]
stale: string[]
catalogStatus: CodexCatalogOutcome
connectionStatus?: Extract<CodexSubscriptionConnectionStatus, 'reauth_required'>
@@ -122,7 +123,7 @@ export function planCodexCatalogReconcile(
const discovered = new Map(result.models.map(model => [model.model, model]))
const inserts: CodexDiscoveredModel[] = []
- const refresh: string[] = []
+ const refresh: CodexDiscoveredModel[] = []
const stale: string[] = []
for (const model of discovered.values()) {
const row = existing.find(candidate => candidate.model === model.model)
@@ -131,7 +132,7 @@ export function planCodexCatalogReconcile(
continue
}
if (row.source === 'manual') continue
- refresh.push(row.model)
+ refresh.push(model)
}
for (const row of existing) {
if (row.source !== 'discovery') continue
@@ -347,6 +348,10 @@ export async function rebuildLiveCodexUnionAllowlist(db: DbClient): Promise<void
ON CONFLICT (provider, model) DO UPDATE
SET enabled = true,
stale = false,
+ context_window_tokens = COALESCE(
+ EXCLUDED.context_window_tokens,
+ llm_allowed_models.context_window_tokens
+ ),
last_seen_at = NOW()`,
[PROVIDER]
)
@@ -568,21 +573,28 @@ async function insertDiscovered(
return added
}
+// A catalog that omits the window keeps the stored one; it never clears it.
async function refreshDiscovered(
db: DbClient,
connectionId: string,
- models: string[]
+ models: CodexDiscoveredModel[]
): Promise<number> {
if (models.length === 0) return 0
const result = await db.query(
- `UPDATE codex_catalog_models
+ `UPDATE codex_catalog_models AS m
SET last_seen_at = NOW(),
stale = false,
- updated_at = NOW()
- WHERE connection_id = $1
- AND source = 'discovery'
- AND model = ANY($2::text[])`,
- [connectionId, models]
+ updated_at = NOW(),
+ context_window_tokens = COALESCE(seen.context_window_tokens, m.context_window_tokens)
+ FROM unnest($2::text[], $3::integer[]) AS seen(model, context_window_tokens)
+ WHERE m.connection_id = $1
+ AND m.source = 'discovery'
+ AND m.model = seen.model`,
+ [
+ connectionId,
+ models.map(model => model.model),
+ models.map(model => model.contextWindowTokens ?? null),
+ ]
)
return result.rowCount ?? 0
}
diff --git a/control-api/src/services/grokSubscriptionCatalog.ts b/control-api/src/services/grokSubscriptionCatalog.ts
index 48a8634a2..2be17efe9 100644
--- a/control-api/src/services/grokSubscriptionCatalog.ts
+++ b/control-api/src/services/grokSubscriptionCatalog.ts
@@ -55,7 +55,8 @@ export type GrokCatalogRow = {
export type GrokCatalogPlan = {
inserts: GrokDiscoveredModel[]
- refresh: string[]
+ /** Existing discovery rows seen again, carrying what the catalog now reports. */
+ refresh: GrokDiscoveredModel[]
stale: string[]
catalogStatus: GrokCatalogOutcome
connectionStatus?: Extract<GrokSubscriptionConnectionStatus, 'reauth_required'>
@@ -88,7 +89,7 @@ export function planGrokCatalogReconcile(
const discovered = new Map(result.models.map(model => [model.model, model]))
const inserts: GrokDiscoveredModel[] = []
- const refresh: string[] = []
+ const refresh: GrokDiscoveredModel[] = []
const stale: string[] = []
for (const model of discovered.values()) {
const row = existing.find(candidate => candidate.model === model.model)
@@ -97,7 +98,7 @@ export function planGrokCatalogReconcile(
continue
}
if (row.source === 'manual') continue
- refresh.push(row.model)
+ refresh.push(model)
}
for (const row of existing) {
if (row.source !== 'discovery') continue
@@ -267,6 +268,10 @@ export async function rebuildLiveGrokUnionAllowlist(db: DbClient): Promise<void>
ON CONFLICT (provider, model) DO UPDATE
SET enabled = true,
stale = false,
+ context_window_tokens = COALESCE(
+ EXCLUDED.context_window_tokens,
+ llm_allowed_models.context_window_tokens
+ ),
last_seen_at = NOW()`,
[PROVIDER]
)
@@ -484,21 +489,28 @@ async function insertDiscovered(
return added
}
+// A catalog that omits the window keeps the stored one; it never clears it.
async function refreshDiscovered(
db: DbClient,
connectionId: string,
- models: string[]
+ models: GrokDiscoveredModel[]
): Promise<number> {
if (models.length === 0) return 0
const result = await db.query(
- `UPDATE grok_catalog_models
+ `UPDATE grok_catalog_models AS m
SET last_seen_at = NOW(),
stale = false,
- updated_at = NOW()
- WHERE connection_id = $1
- AND source = 'discovery'
- AND model = ANY($2::text[])`,
- [connectionId, models]
+ updated_at = NOW(),
+ context_window_tokens = COALESCE(seen.context_window_tokens, m.context_window_tokens)
+ FROM unnest($2::text[], $3::integer[]) AS seen(model, context_window_tokens)
+ WHERE m.connection_id = $1
+ AND m.source = 'discovery'
+ AND m.model = seen.model`,
+ [
+ connectionId,
+ models.map(model => model.model),
+ models.map(model => model.contextWindowTokens ?? null),
+ ]
)
return result.rowCount ?? 0
}
diff --git a/control-api/test/services.codexSubscriptionCatalog.test.ts b/control-api/test/services.codexSubscriptionCatalog.test.ts
index 339c21787..7d1728b05 100644
--- a/control-api/test/services.codexSubscriptionCatalog.test.ts
+++ b/control-api/test/services.codexSubscriptionCatalog.test.ts
@@ -56,7 +56,7 @@ describe('planCodexCatalogReconcile', () => {
)
expect(plan.mutateRows).toBe(true)
expect(plan.inserts.map(model => model.model)).toEqual(['gpt-5.3-codex'])
- expect(plan.refresh).toEqual(['gpt-5'])
+ expect(plan.refresh.map(model => model.model)).toEqual(['gpt-5'])
expect(plan.stale).toEqual(['legacy-codex'])
expect(plan.catalogStatus).toBe('ready')
expect(plan.connectionStatus).toBeUndefined()
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index 2664809e3..8969bd458 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -683,8 +683,28 @@ export const CATALOG_LIMITS = {
maxBodyBytes: 1_048_576,
maxModels: 256,
maxModelIdLength: 128,
+ // control-api stores the window in a Postgres INTEGER column; a larger value
+ // would fail the whole catalog sync, so it is omitted here instead.
+ maxContextWindowTokens: 2_147_483_647,
} as const
+type CatalogModel = { model: string; displayName?: string; contextWindowTokens?: number }
+
+/**
+ * The upstream row's `context_window`, when it is a positive integer the
+ * catalog store can hold. The field name matches the Grok CLI's model cache;
+ * the raw catalog envelope is confirmed by the live probe on #739.
+ */
+function contextWindowOf(row: Record<string, unknown>): number | undefined {
+ const value = row.context_window
+ return typeof value === 'number' &&
+ Number.isInteger(value) &&
+ value > 0 &&
+ value <= CATALOG_LIMITS.maxContextWindowTokens
+ ? value
+ : undefined
+}
+
export async function listGrokModels(input: {
accessToken: string
fetchFn: typeof fetch
@@ -693,7 +713,7 @@ export async function listGrokModels(input: {
timeoutMs?: number
}): Promise<{
outcome: 'ready' | 'auth-rejected' | 'unavailable'
- models: Array<{ model: string; displayName?: string }>
+ models: CatalogModel[]
}> {
const url = assertAllowedUpstreamUrl(GROK_CATALOG_ORIGIN, 'catalog')
const headers = grokUpstreamHeaders(input.accessToken, { accept: 'application/json' })
@@ -737,7 +757,7 @@ export async function testGrokConnection(input: {
return { outcome: listed.outcome }
}
-function normalizeModels(body: unknown): Array<{ model: string; displayName?: string }> {
+function normalizeModels(body: unknown): CatalogModel[] {
const rows = Array.isArray(body)
? body
: isPlainObject(body) && Array.isArray(body.models)
@@ -745,7 +765,7 @@ function normalizeModels(body: unknown): Array<{ model: string; displayName?: st
: isPlainObject(body) && Array.isArray(body.data)
? body.data
: []
- const models: Array<{ model: string; displayName?: string }> = []
+ const models: CatalogModel[] = []
let droppedOverlongIds = 0
let droppedOverLimit = 0
for (const row of rows) {
@@ -766,7 +786,12 @@ function normalizeModels(body: unknown): Array<{ model: string; displayName?: st
: typeof row.title === 'string'
? row.title
: undefined
- models.push(displayName ? { model, displayName } : { model })
+ const contextWindowTokens = contextWindowOf(row)
+ models.push({
+ model,
+ ...(displayName ? { displayName } : {}),
+ ...(contextWindowTokens !== undefined ? { contextWindowTokens } : {}),
+ })
}
if (droppedOverlongIds > 0 || droppedOverLimit > 0) {
logger.warn(
diff --git a/grok-llm-proxy/test/catalogBounds.test.ts b/grok-llm-proxy/test/catalogBounds.test.ts
index 413461c30..b1dae5c6d 100644
--- a/grok-llm-proxy/test/catalogBounds.test.ts
+++ b/grok-llm-proxy/test/catalogBounds.test.ts
@@ -28,6 +28,7 @@ describe('Grok catalog upstream bounds', () => {
maxBodyBytes: 1_048_576,
maxModels: 256,
maxModelIdLength: 128,
+ maxContextWindowTokens: 2_147_483_647,
})
})
diff --git a/mcp-host/package-lock.json b/mcp-host/package-lock.json
index ef8dead3d..0af82dab6 100644
--- a/mcp-host/package-lock.json
+++ b/mcp-host/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.397",
+ "version": "0.1.398",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "clerum-mcp-host",
- "version": "0.1.397",
+ "version": "0.1.398",
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@anthropic-ai/sdk": "^0.32.0",
diff --git a/mcp-host/package.json b/mcp-host/package.json
index a20e48f3c..6ed0b1bff 100644
--- a/mcp-host/package.json
+++ b/mcp-host/package.json
@@ -1,6 +1,6 @@
{
"name": "clerum-mcp-host",
- "version": "0.1.397",
+ "version": "0.1.398",
"description": "MCP Host - reads Host CRD and provides LLM access via OpenAI/Claude",
"main": "dist/main.js",
"engines": {
diff --git a/mcp-host/src/agent/stateMachine.ts b/mcp-host/src/agent/stateMachine.ts
index 018210c48..1ddf99329 100644
--- a/mcp-host/src/agent/stateMachine.ts
+++ b/mcp-host/src/agent/stateMachine.ts
@@ -42,6 +42,7 @@ import { isTerminal } from '../lifecycle/types'
import { SingleTurnProvider } from '../llm'
import type { ImageInputResolver } from '../llm/imageInput'
import type { PromptCache } from '../llm/promptCache'
+import { resolveContextWindow } from '../llm/registryCore'
import { logger } from '../logger'
import { McpManager } from '../mcp'
import { ensureReporter } from '../progress/sseProgressReporter'
@@ -900,7 +901,11 @@ export class AgentStateMachine extends EventEmitter {
// summary call meters the pair really served (adapter-per-attempt).
const compactPort = this.wrapCompactFailover(llmPort, compactProvider, compactModel, conv)
const manager = new PressureContextManager(
- compactContextWindow ?? appConfig.contextMaxTokens,
+ resolveContextWindow(
+ compactProvider.getProviderType(),
+ compactContextWindow,
+ appConfig.contextMaxTokens
+ ).contextWindowTokens,
this.workspaceProvider?.forSessionKey(opts.sessionKey),
compactPort,
tokenCounter,
diff --git a/mcp-host/src/agent/taskExecutor.ts b/mcp-host/src/agent/taskExecutor.ts
index d2a9c757e..c259d58ca 100644
--- a/mcp-host/src/agent/taskExecutor.ts
+++ b/mcp-host/src/agent/taskExecutor.ts
@@ -94,7 +94,7 @@ import type { SingleTurnProvider } from '../llm'
import type { ImageInputResolver } from '../llm/imageInput'
import type { PromptCache } from '../llm/promptCache'
import { stampStableHashGauge } from '../llm/promptCacheMetrics'
-import { descriptorFor, isLlmProvider } from '../llm/registryCore'
+import { descriptorFor, isLlmProvider, resolveContextWindow } from '../llm/registryCore'
import { logger } from '../logger'
import type { McpManager } from '../mcp'
import { getDisplayName, sanitizeError } from '../progress/intentExtraction.js'
@@ -279,6 +279,8 @@ export class TaskExecutor {
* accumulates across iterations of the same task.
*/
private tokenCounter: TokenCounter | null = null
+ /** #731 — the task's context window, resolved and logged once. */
+ private contextWindow: number | null = null
/**
* P2 token budgets (§5.2) — snapshot of the conversation's lifetime token
* counters captured at task start, used as the per-task brake baseline.
@@ -983,11 +985,34 @@ export class TaskExecutor {
/**
* R2.6 — model-aware context window. The allowlist entry for the effective
- * model wins; falls back to the fixed `CLERUM_CONTEXT_MAX_TOKENS` env when the
- * allowlist carries no `contextWindowTokens` (or is unavailable/degraded).
+ * model wins. Without one, a subscription provider uses its 256k default and
+ * every other provider the fixed `CLERUM_CONTEXT_MAX_TOKENS` env (#731).
+ * Subscription tasks log the window and its source once.
*/
private contextMaxTokens(): number {
- return this.deps.contextWindowTokens ?? appConfig.contextMaxTokens
+ if (this.contextWindow !== null) return this.contextWindow
+ const provider = this.deps.llmProvider.getProviderType()
+ const { contextWindowTokens, source } = resolveContextWindow(
+ provider,
+ this.deps.contextWindowTokens,
+ appConfig.contextMaxTokens
+ )
+ if (isLlmProvider(provider) && descriptorFor(provider).defaultContextWindowTokens) {
+ logger.info(
+ {
+ event: 'context_window_resolved',
+ component: 'TaskExecutor',
+ taskId: this.taskId,
+ provider,
+ model: this.deps.modelName,
+ contextWindowTokens,
+ source,
+ },
+ 'Context window resolved for the task'
+ )
+ }
+ this.contextWindow = contextWindowTokens
+ return contextWindowTokens
}
/**
diff --git a/mcp-host/src/llm/registryCore.ts b/mcp-host/src/llm/registryCore.ts
index e986a56e0..5a169014e 100644
--- a/mcp-host/src/llm/registryCore.ts
+++ b/mcp-host/src/llm/registryCore.ts
@@ -88,8 +88,20 @@ export interface CoreProviderDescriptor {
* without an attempt contract.
*/
maxMessages?: number
+ /**
+ * The context window used when the provider's model catalog names none
+ * (#731). Absent for providers whose window comes from
+ * `CLERUM_CONTEXT_MAX_TOKENS`.
+ */
+ defaultContextWindowTokens?: number
}
+/**
+ * The window a subscription model gets when its upstream catalog reports none.
+ * Every Codex and Grok model listed in September 2026 has at least 256k.
+ */
+export const SUBSCRIPTION_DEFAULT_CONTEXT_WINDOW_TOKENS = 256_000
+
/** The `LlmProvider` union is the shared canonical set (data-only leaf). */
export type LlmProvider = LlmProviderId
@@ -105,6 +117,7 @@ interface RuntimeProviderFields {
baseURL?: string
tokenizer: CoreProviderDescriptor['tokenizer']
maxMessages?: number
+ defaultContextWindowTokens?: number
}
const RUNTIME_FIELDS: Record<LlmProvider, RuntimeProviderFields> = {
@@ -212,8 +225,16 @@ const RUNTIME_FIELDS: Record<LlmProvider, RuntimeProviderFields> = {
azure: { defaultModel: 'gpt-4.1', tokenizer: 'openai' },
// Broker: explicit model required later; no Secret slot and no default. The
// message bound is read from each contract, never written as a literal.
- 'codex-subscription': { tokenizer: 'fallback', maxMessages: CODEX_CONTRACT_LIMITS.maxMessages },
- 'grok-subscription': { tokenizer: 'fallback', maxMessages: GROK_CONTRACT_LIMITS.maxMessages },
+ 'codex-subscription': {
+ tokenizer: 'fallback',
+ maxMessages: CODEX_CONTRACT_LIMITS.maxMessages,
+ defaultContextWindowTokens: SUBSCRIPTION_DEFAULT_CONTEXT_WINDOW_TOKENS,
+ },
+ 'grok-subscription': {
+ tokenizer: 'fallback',
+ maxMessages: GROK_CONTRACT_LIMITS.maxMessages,
+ defaultContextWindowTokens: SUBSCRIPTION_DEFAULT_CONTEXT_WINDOW_TOKENS,
+ },
}
// Order = dev auto-detection priority (first present key wins), inherited from
@@ -273,3 +294,25 @@ export const ALL_PROVIDER_SLOT_ENV_NAMES: ReadonlySet<string> = new Set(
export const isLlmProvider = isLlmProviderId
export const descriptorFor = (p: LlmProvider): CoreProviderDescriptor => PROVIDERS[p]
+
+export type ContextWindowSource = 'catalog' | 'default' | 'env'
+
+/**
+ * The context window a task runs with: the model catalog's value, else the
+ * provider's default, else `envDefault` (`CLERUM_CONTEXT_MAX_TOKENS`, passed in
+ * because this module cannot import the config).
+ */
+export function resolveContextWindow(
+ providerType: string,
+ catalogWindow: number | null | undefined,
+ envDefault: number
+): { contextWindowTokens: number; source: ContextWindowSource } {
+ if (catalogWindow != null) return { contextWindowTokens: catalogWindow, source: 'catalog' }
+ const providerDefault = isLlmProvider(providerType)
+ ? PROVIDERS[providerType].defaultContextWindowTokens
+ : undefined
+ if (providerDefault !== undefined) {
+ return { contextWindowTokens: providerDefault, source: 'default' }
+ }
+ return { contextWindowTokens: envDefault, source: 'env' }
+}