MCPcopy Create free account
hub / github.com/cortexkit/magic-context / persistApiCache

Function persistApiCache

packages/plugin/src/shared/models-dev-cache.ts:113–129  ·  view source on GitHub ↗

Atomically persist the current (sane-filtered) apiCache so the next process * cold-starts with the real limits. Temp-write + rename so a concurrent reader * never sees a torn file (the exact failure mode we're eliminating).

()

Source from the content-addressed store, hash-verified

111 * cold-starts with the real limits. Temp-write + rename so a concurrent reader
112 * never sees a torn file (the exact failure mode we're eliminating). */
113function persistApiCache(): void {
114 if (!apiCache) return;
115 const obj: Record<string, number> = {};
116 for (const [key, value] of apiCache) {
117 if (isSaneLimit(value.limit)) obj[key] = value.limit;
118 }
119 try {
120 const dir = getMagicContextStorageDir();
121 mkdirSync(dir, { recursive: true });
122 const target = persistFilePath();
123 const tmp = `${target}.${process.pid}.tmp`;
124 writeFileSync(tmp, JSON.stringify(obj), { encoding: "utf-8", mode: 0o600 });
125 renameSync(tmp, target);
126 } catch {
127 // best-effort — a failed persist only loses cold-start warmth, not correctness
128 }
129}
130
131/**
132 * Resolve the effective pressure limit for a model's `limit` object.

Callers 1

refreshModelLimitsOnceFunction · 0.85

Calls 3

isSaneLimitFunction · 0.85
persistFilePathFunction · 0.85

Tested by

no test coverage detected