Skip to content

Commit f1115ce

Browse files
committed
perf(tui): move context cache warmup from module top-level to embedded backend
agents/context.ts fired ensureContextWindowCacheLoaded() unconditionally at module-eval time for non-skip-listed CLI commands. The TUI transitively imports this module, so the warmup ran on every TUI startup including remote-mode, cascading into ensureOpenClawModelsJson -> resolveImplicitProviders -> runProviderCatalog and dominating the cold-start freeze (CPU profile showed ~55s of resolveProviderSyntheticAuthWithPlugin, lstat, open, etc.). It also pre-emptively called getRuntimeConfig() without skipPluginValidation, pinning the full snapshot and nullifying the skip flag added on this branch. Remove the top-level side effect and trigger the warmup explicitly from EmbeddedTuiBackend.start(), which only runs when an in-process agent runtime actually needs the cache.
1 parent 2463656 commit f1115ce

2 files changed

Lines changed: 3 additions & 7 deletions

File tree

src/agents/context.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ function primeConfiguredContextWindows(): OpenClawConfig | undefined {
219219
}
220220
}
221221

222-
function ensureContextWindowCacheLoaded(): Promise<void> {
222+
export function ensureContextWindowCacheLoaded(): Promise<void> {
223223
if (CONTEXT_WINDOW_RUNTIME_STATE.loadPromise) {
224224
return CONTEXT_WINDOW_RUNTIME_STATE.loadPromise;
225225
}
@@ -287,12 +287,6 @@ export function lookupContextTokens(
287287
return lookupCachedContextTokens(modelId);
288288
}
289289

290-
if (shouldEagerWarmContextWindowCache()) {
291-
// Keep startup warmth for the real CLI, but avoid import-time side effects
292-
// when this module is pulled in through library/plugin-sdk surfaces.
293-
void ensureContextWindowCacheLoaded();
294-
}
295-
296290
function resolveProviderModelRef(params: {
297291
provider?: string;
298292
model?: string;

src/tui/embedded-backend.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { randomUUID } from "node:crypto";
22
import { agentCommandFromIngress } from "../agents/agent-command.js";
33
import { resolveSessionAgentId } from "../agents/agent-scope.js";
4+
import { ensureContextWindowCacheLoaded } from "../agents/context.js";
45
import { DEFAULT_PROVIDER } from "../agents/defaults.js";
56
import { buildAllowedModelSet, resolveThinkingDefault } from "../agents/model-selection.js";
67
import { createDefaultDeps } from "../cli/deps.js";
@@ -138,6 +139,7 @@ export class EmbeddedTuiBackend implements TuiBackend {
138139
return;
139140
}
140141
setEmbeddedMode(true);
142+
void ensureContextWindowCacheLoaded();
141143
// Suppress console output from logError/logInfo that would pollute the TUI.
142144
// File logger (getLogger()) still captures everything via logger.ts:35.
143145
this.previousRuntimeLog = defaultRuntime.log;

0 commit comments

Comments
 (0)