openclaw/src/config/cache-utils.ts
Josh Lehman 1212328c8d fix: refresh session-store cache when file size changes within same mtime tick
The session-store cache used only mtime for invalidation. In fast CI
runs (especially under bun), test writes to the session store can
complete within the same filesystem mtime granularity (~1s on HFS+/ext4),
so the cache returns stale data. This caused non-deterministic failures
in model precedence tests where a session override written to disk was
not observed by the next loadSessionStore() call.

Fix: add file size as a secondary cache invalidation signal. The cache
now checks both mtimeMs and sizeBytes — if either differs from the
cached values, it reloads from disk.

Changes:
- cache-utils.ts: add getFileSizeBytes() helper
- sessions/store.ts: extend SessionStoreCacheEntry with sizeBytes field,
  check size in cache-hit path, populate size on cache writes
- sessions.cache.test.ts: add regression test for same-mtime rewrite
2026-03-02 22:09:36 +00:00

36 lines
762 B
TypeScript

import fs from "node:fs";
export function resolveCacheTtlMs(params: {
envValue: string | undefined;
defaultTtlMs: number;
}): number {
const { envValue, defaultTtlMs } = params;
if (envValue) {
const parsed = Number.parseInt(envValue, 10);
if (Number.isFinite(parsed) && parsed >= 0) {
return parsed;
}
}
return defaultTtlMs;
}
export function isCacheEnabled(ttlMs: number): boolean {
return ttlMs > 0;
}
export function getFileMtimeMs(filePath: string): number | undefined {
try {
return fs.statSync(filePath).mtimeMs;
} catch {
return undefined;
}
}
export function getFileSizeBytes(filePath: string): number | undefined {
try {
return fs.statSync(filePath).size;
} catch {
return undefined;
}
}