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).
()
| 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). */ |
| 113 | function 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. |
no test coverage detected