fix: rehydrate context token cache after module reload

This commit is contained in:
Shakker
2026-04-03 14:22:36 +01:00
parent ba7297ff21
commit 8fabfa5d1c
2 changed files with 38 additions and 0 deletions

View File

@@ -116,6 +116,38 @@ describe("lookupContextTokens", () => {
);
});
it("rehydrates config-backed cache entries after module reload when runtime config survives", async () => {
const firstLoadConfigMock = vi.fn(() => ({
models: {
providers: {
openrouter: {
models: [{ id: "openrouter/claude-sonnet", contextWindow: 321_000 }],
},
},
},
}));
mockContextModuleDeps(firstLoadConfigMock);
let { lookupContextTokens } = await importContextModule();
expect(lookupContextTokens("openrouter/claude-sonnet", { allowAsyncLoad: false })).toBe(
321_000,
);
expect(firstLoadConfigMock).toHaveBeenCalledTimes(1);
vi.resetModules();
const secondLoadConfigMock = vi.fn(() => {
throw new Error("config should come from shared runtime state");
});
mockContextModuleDeps(secondLoadConfigMock);
({ lookupContextTokens } = await importContextModule());
expect(lookupContextTokens("openrouter/claude-sonnet", { allowAsyncLoad: false })).toBe(
321_000,
);
expect(secondLoadConfigMock).not.toHaveBeenCalled();
});
it("only warms eagerly for real openclaw startup commands that need model metadata", async () => {
const argvSnapshot = process.argv;
try {

View File

@@ -158,6 +158,12 @@ function shouldEagerWarmContextWindowCache(argv: string[] = process.argv): boole
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) {