Files
openclaw/scripts/lib/sqlite-reliability-wal-monitor.ts
Vincent Koc 4afcbea5c5 test(sqlite): harden compaction and restore reliability proof (#105811)
* test(sqlite): chain compaction and restore reliability proof

* test(sqlite): bound reliability stress storage

* test(sqlite): monitor transient WAL growth
2026-07-13 09:09:21 +08:00

44 lines
1.2 KiB
TypeScript

import fs from "node:fs";
const DEFAULT_POLL_INTERVAL_MS = 25;
function fileSize(pathname: string): number {
try {
return fs.statSync(pathname).size;
} catch {
return 0;
}
}
export async function monitorSqliteWalDuring<T>(params: {
maxWalBytes: number;
onLimitExceeded: () => void;
operation: () => Promise<T>;
pollIntervalMs?: number;
walPath: string;
}): Promise<{ peakWalBytes: number; result: T }> {
let peakWalBytes = fileSize(params.walPath);
let limitExceeded = false;
const sample = () => {
const currentWalBytes = fileSize(params.walPath);
peakWalBytes = Math.max(peakWalBytes, currentWalBytes);
if (!limitExceeded && currentWalBytes > params.maxWalBytes) {
limitExceeded = true;
params.onLimitExceeded();
}
};
const timer = setInterval(sample, params.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS);
try {
const result = await params.operation();
sample();
if (limitExceeded) {
throw new Error(
`SQLite reliability WAL exceeded the ${params.maxWalBytes}-byte profile limit: ${peakWalBytes} bytes`,
);
}
return { peakWalBytes, result };
} finally {
clearInterval(timer);
}
}