Self-trim update.log

Capped at 256 KB, trimmed back to the newest 500 lines. Done once per run
before anything is written: the no-change path logs nothing, so that is the
only point at which the file can have grown since the last run. Failures are
swallowed - housekeeping must never be the reason an update does not land.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-08-30 18:40:19 -05:00
co-authored by Claude Opus 5
parent fbe2c47ed4
commit be8c628778
+20 -1
View File
@@ -12,7 +12,7 @@
* node scripts/update-check.mjs --dry-run # report only, change nothing
*/
import { spawnSync } from 'node:child_process';
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { ensurePythonDeps } from './ensure-python-deps.mjs';
@@ -25,6 +25,24 @@ const PORT = process.env.AUTOFIRMER_PORT ?? '3000';
const BASE = `http://127.0.0.1:${PORT}`;
const DRY_RUN = process.argv.includes('--dry-run');
const STALE_LOCK_MS = 60 * 60 * 1000;
const LOG_MAX_BYTES = 256 * 1024;
const LOG_KEEP_LINES = 500;
/**
* Keep update.log from growing without bound.
*
* Trimmed once per run, before anything is written, rather than on every line:
* the no-change path writes nothing at all, so this is the only moment the file
* can have grown since last time. Housekeeping must never break an update, so
* any failure here is swallowed.
*/
function trimLog() {
try {
if (!existsSync(LOG_FILE) || statSync(LOG_FILE).size <= LOG_MAX_BYTES) return;
const kept = readFileSync(LOG_FILE, 'utf8').split('\n').filter(Boolean).slice(-LOG_KEEP_LINES);
writeFileSync(LOG_FILE, kept.join('\n') + '\n');
} catch { /* ignore */ }
}
function log(msg) {
const line = `${new Date().toISOString()} ${msg}`;
@@ -63,6 +81,7 @@ const releaseLock = () => { try { rmSync(LOCK_FILE, { force: true }); } catch {
async function main() {
mkdirSync(path.join(ROOT, 'scripts'), { recursive: true });
trimLog();
if (!DRY_RUN && !acquireLock()) {
console.log('another update is already running — exiting');