hermes-agent/apps/desktop/electron/log-rotation.ts
Austin Pickett 782886f87d fix(desktop): bound the Chromium log while the shell is running, not only at launch
The startup reclaim caps the log a previous run left behind, but a shell
that stays up for days writing Chromium ERRORs is unbounded until it
restarts — the case @ehz0ah flagged in review.

Chromium owns that descriptor in append mode for the life of the
process, so rotation is the wrong primitive: renaming leaves the writer
on the renamed inode and the cap silently stops applying. Poll the live
size and truncate in place instead; an O_APPEND writer resumes at offset
0, so a noisy process stays bounded at ~cap plus one interval's output.
The timer is unref'd and every tick is best-effort.

Co-authored-by: ehz0ah <[email protected]>

Refs #100573
2026-09-21 10:23:04 -04:00

74 lines
2.9 KiB
TypeScript

// Any log the shell keeps across launches needs a size bound — desktop.log has
// been seen at ~326 GB, which exhausts the disk and then breaks update/install
// (no room for git/venv/npm temp files).
//
// Mirror the Python logs (hermes_logging.py RotatingFileHandler, maxBytes x
// backupCount): cascade live -> .1 -> .2 -> .3, drop the oldest. Steady-state
// stays bounded at ~(backupCount + 1) x cap however hard the app loops.
//
// Bounding alone never RECLAIMS an already-huge file: a plain rotation just
// renames the monster to .1 and strands it for a cycle a healthy app may never
// reach. A multi-GB boot-loop transcript has no diagnostic value, so anything
// past the discard ceiling is deleted outright — the updated app self-heals a
// disk a stale build filled, on the next launch.
export const LOG_MAX_BYTES = 10 * 1024 * 1024
export const LOG_BACKUP_COUNT = 3
export const LOG_DISCARD_BYTES = LOG_MAX_BYTES * 4
export const logBackupPath = (base: string, n: number): string => `${base}.${n}`
// A log another PROCESS owns (Chromium's --log-file) cannot be rotated: it
// holds the descriptor open in append mode, so renaming the file just moves
// the growth to the renamed inode and the cap silently stops applying. The
// only reclamation that works from outside is truncating in place — an
// O_APPEND writer resumes at offset 0 — so a long-lived noisy process is
// bounded at ~cap plus one poll interval's output instead of the whole disk.
export const ACTIVE_LOG_POLL_MS = 5 * 60 * 1000
export interface ActiveLogIo {
/** Live size, or null when the file does not exist yet. */
size(file: string): number | null
truncate(file: string): void
}
export function reclaimActiveLogIfOversized(file: string, io: ActiveLogIo): boolean {
const size = io.size(file)
if (size === null || size < LOG_MAX_BYTES) {
return false
}
io.truncate(file)
return true
}
export type LogRotationOp = ['rm', string] | ['mv', string, string]
// Pure planner: ordered fs ops to bound the live log at `base`. [] = nothing.
// Each step is ['rm', path] or ['mv', src, dst]; executed best-effort so a
// missing chain link never aborts the rest.
export function planLogRotation(size: number, base: string): LogRotationOp[] {
if (size < LOG_MAX_BYTES) {
return []
}
const backups = (n: number) => Array.from({ length: n }, (_, i) => logBackupPath(base, i + 1))
// Pathological boot-loop log: reclaim live + every backup outright.
if (size > LOG_DISCARD_BYTES) {
return [base, ...backups(LOG_BACKUP_COUNT)].map(p => ['rm', p] as LogRotationOp)
}
// Cascade: drop oldest, shift each up, live -> .1.
const ops: LogRotationOp[] = [['rm', logBackupPath(base, LOG_BACKUP_COUNT)]]
for (let i = LOG_BACKUP_COUNT - 1; i >= 1; i--) {
ops.push(['mv', logBackupPath(base, i), logBackupPath(base, i + 1)])
}
ops.push(['mv', base, logBackupPath(base, 1)])
return ops
}