Skip to content

Commit 8d1eec8

Browse files
committed
perf(usage): memoize read-time boundary validation per raw-log revision; document the digest-window contract
1 parent 49cb22c commit 8d1eec8

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

‎src/usage/rollup.ts‎

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
currentUsageLogRevision,
2525
normalizeUsageEntry,
2626
usageLogPath,
27+
usageLogRevisionKey,
2728
type PersistedUsageEntry,
2829
type UsageStatus,
2930
} from "./log";
@@ -168,6 +169,8 @@ interface KeyAccumulator {
168169
}
169170

170171
let rollupFlight: Promise<void> | null = null;
172+
/** Memoized read-time boundary validation, keyed on the raw log's revision. */
173+
let boundaryValidationCache: { key: string; valid: boolean } | null = null;
171174

172175
function rollupPath(): string {
173176
return join(getConfigDir(), "usage-rollup.jsonl");
@@ -446,22 +449,39 @@ export function readRollupSnapshot(): RollupSnapshot | null {
446449
// Read-time boundary validity (review threads: truncated/rewritten raw log
447450
// within a throttle window must not serve a stale sidecar). The fold path
448451
// performs the same check before appending; readers need it too because a
449-
// truncate-then-regrow can happen entirely between folds. EVERY segment is
450-
// validated, not just the last: a same-size rewrite of an earlier folded
451-
// span would leave the final segment's tail intact and otherwise pass.
452+
// truncate-then-regrow can happen entirely between folds. EVERY segment's
453+
// boundary window is validated, not just the last — a same-size rewrite of
454+
// an earlier folded span would leave the final segment's tail intact.
455+
//
456+
// Scope, stated honestly: each boundary digest covers the trailing
457+
// BOUNDARY_DIGEST_BYTES of its segment, so a surgical same-size rewrite
458+
// strictly inside a segment's untailed span is not detected here. That is
459+
// the fold-time contract too (the sidecar trusts an append-only log); the
460+
// read-time check exists to catch the realistic failure — truncation,
461+
// rotation, regrowth — not an adversarial editor with byte-level care.
462+
//
463+
// Cost: the validation is memoized per raw-log revision (dev/ino/size/
464+
// mtime/ctime), so cached hot paths pay one fstat, not a re-hash of every
465+
// segment on every call.
452466
if (parsed.segments.length > 0) {
453-
let fd: number | undefined;
454-
try {
455-
fd = openSync(usageLogPath(), "r");
456-
const rawSize = Number(fstatSync(fd).size);
457-
if (rawSize < parsed.snapshot.cutlineOffset) return null;
458-
for (const segment of parsed.segments) {
459-
if (!boundaryMatches(fd, segment)) return null;
467+
const revisionKey = `${usageLogRevisionKey(currentUsageLogRevision())}|${parsed.snapshot.cutlineOffset}|${parsed.segments.length}`;
468+
if (boundaryValidationCache?.key === revisionKey) {
469+
if (!boundaryValidationCache.valid) return null;
470+
} else {
471+
let valid = false;
472+
let fd: number | undefined;
473+
try {
474+
fd = openSync(usageLogPath(), "r");
475+
const rawSize = Number(fstatSync(fd).size);
476+
valid = rawSize >= parsed.snapshot.cutlineOffset
477+
&& parsed.segments.every(segment => boundaryMatches(fd!, segment));
478+
} catch {
479+
valid = false;
480+
} finally {
481+
if (fd !== undefined) closeSync(fd);
460482
}
461-
} catch {
462-
return null;
463-
} finally {
464-
if (fd !== undefined) closeSync(fd);
483+
boundaryValidationCache = { key: revisionKey, valid };
484+
if (!valid) return null;
465485
}
466486
}
467487
return parsed.snapshot;
@@ -866,4 +886,5 @@ export async function ensureRollupCurrent(): Promise<void> {
866886

867887
export function resetRollupForTests(): void {
868888
rollupFlight = null;
889+
boundaryValidationCache = null;
869890
}

0 commit comments

Comments
 (0)