Files
HealthLog/src/lib/ai/codex-slug-cache.ts
T
Marc-André Bombeck fea1d1e0b5 chore: strip planning tags from comments and enable the rule
Output of `node scripts/strip-planning-tags.mjs --write`: 3,157 comment
lines in 1,308 files lose the work-item identifier at their head. Release
versions and issue references stay; nothing outside comment text
changes, which the script checks per file against a comment-free
printout. healthlog/no-planning-tags is enabled at error level with this
commit, so the tree it lands on is clean.
2026-10-20 09:08:40 +02:00

63 lines
1.8 KiB
TypeScript

/**
* Process-local positive cache for working Codex model slugs.
*
* (v1.4.15) — slug-drift defence per
* `docs/codex-protocol-spec.md` §7b. The ChatGPT-Codex backend rotates
* its allow-list silently; pinning a single `CODEX_MODEL` slug bricks
* the integration the moment upstream flips it. Instead of pinning,
* `CodexClient` walks an ordered fallback chain on every fresh
* request series and caches the first slug that returns 200 +
* `response.completed` for `CACHE_TTL_MS` so subsequent requests skip
* the chain walk.
*
* Lives in its own module so:
* - Tests can `clearCodexSlugCache()` between cases.
* - The shared Map state stays out of `CodexClient` itself (so
* instance creation in `resolveProvider` doesn't reset the cache).
* - Future telemetry (cache hit-rate emission) hooks here cleanly.
*/
const CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour per spec §7b
interface CachedEntry {
slug: string;
cachedAtMs: number;
}
/**
* Cache key — currently a single global slot ("codex"). Kept in a Map
* so future per-account / per-model-family scoping is a one-line
* change.
*/
const CACHE_KEY = "codex";
const cache = new Map<string, CachedEntry>();
export function getCachedCodexSlug(now: number = Date.now()): string | null {
const entry = cache.get(CACHE_KEY);
if (!entry) return null;
if (now - entry.cachedAtMs > CACHE_TTL_MS) {
cache.delete(CACHE_KEY);
return null;
}
return entry.slug;
}
export function setCachedCodexSlug(
slug: string,
now: number = Date.now(),
): void {
cache.set(CACHE_KEY, { slug, cachedAtMs: now });
}
export function invalidateCachedCodexSlug(): void {
cache.delete(CACHE_KEY);
}
/** Test-only: reset the cache between cases. */
export function clearCodexSlugCache(): void {
cache.clear();
}
export const CODEX_SLUG_CACHE_TTL_MS = CACHE_TTL_MS;