fix(server): atomic account-limits persist and deeper transcript scan

Concurrent persists (ingestion worker vs the RPC-path transcript seed)
could interleave writes and corrupt account-limits.json, silently dropping
the Claude snapshot on restart. Reuse writeFileStringAtomically. Also raise
the transcript scan cap from 8 to 32 files so a run of Spark-only or
abandoned sessions cannot mask an older main-meter snapshot.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Theo BrowneandClaude Fable 5 committed 2026-08-08 19:11:30 -07:00
1 parent 66b6fb4cf0
commit ba79f14d2d
2 files changed
+15 -5

No files matched your search

+10 -2
View File
@@ -28,6 +28,7 @@ import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { writeFileStringAtomically } from "../atomicWrite.ts";
import { ServerConfig } from "../config.ts";
import * as ServerSettings from "../serverSettings.ts";
import { resolveCodexHomeLayout } from "../provider/Drivers/CodexHomeLayout.ts";
@@ -114,10 +115,17 @@ export const make = Effect.gen(function* () {
}),
);
// A cache we cannot write is a colder next start, not a failure.
// A cache we cannot write is a colder next start, not a failure. Written
// atomically: the ingestion worker and the RPC-path transcript seed can
// persist concurrently, and an interleaved plain write would corrupt the
// file - which on restart silently drops the Claude snapshot.
const persist = Effect.fn("AccountLimitsService.persist")(function* () {
yield* encodeLimitsCache([...snapshots.values()]).pipe(
Effect.flatMap((serialized) => fileSystem.writeFileString(cachePath, serialized)),
Effect.flatMap((serialized) =>
writeFileStringAtomically({ filePath: cachePath, contents: serialized }),
),
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
Effect.catchCause(() => Effect.void),
);
});
@@ -25,10 +25,12 @@ import { listTranscriptFiles } from "./usageTranscriptReader.ts";
const TAIL_BYTES = 256 * 1024;
const SCAN_WINDOW_DAYS = 14;
/**
* Newest-first cutoff. The newest file almost always hits; the rest cover a
* newest file that happens to be a side-meter-only session (Spark).
* Newest-first cutoff bounding the scan on the RPC path. The newest file
* almost always hits; the margin covers runs of files without a main-meter
* snapshot (Spark-only sessions, sessions abandoned before any token count).
* If this many consecutive files lack one, the data is genuinely absent.
*/
const MAX_FILES = 8;
const MAX_FILES = 32;
export interface CodexTranscriptRateLimits {
readonly snapshot: CodexRateLimitsSnapshot;