Skip to content

The agent that grows with you

refactor(desktop): a shared, path-parameterized log rotation planner

Austin Pickett5c57bfdcaa01
diff --git a/apps/desktop/electron/log-rotation.test.ts b/apps/desktop/electron/log-rotation.test.ts
new file mode 100644
index 00000000000..a94564cff4d
--- /dev/null
+++ b/apps/desktop/electron/log-rotation.test.ts
@@ -0,0 +1,35 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { LOG_DISCARD_BYTES, LOG_MAX_BYTES, logBackupPath, planLogRotation } from './log-rotation'
+
+// Regression for #100573 follow-up: the Chromium diagnostic log added for that
+// issue is opened with APPEND_TO_OLD_LOG_FILE, so it grows across launches the
+// same way desktop.log did before it was bounded (~326 GB, disk exhausted).
+// The bound is one shared planner, so any log the shell keeps gets it.
+
+test('a log under the cap is left alone, whatever its path', () => {
+ assert.deepEqual(planLogRotation(LOG_MAX_BYTES - 1, '/logs/desktop-chromium.log'), [])
+})
+
+test('an oversized log cascades to backups instead of growing forever', () => {
+ const base = '/logs/desktop-chromium.log'
+ const ops = planLogRotation(LOG_MAX_BYTES, base)
+
+ // The live file is moved aside, so the next launch starts from zero.
+ assert.ok(ops.some(([op, src, dst]) => op === 'mv' && src === base && dst === logBackupPath(base, 1)))
+ // The chain is bounded: the oldest backup is dropped, never accumulated.
+ assert.deepEqual(ops[0], ['rm', logBackupPath(base, 3)])
+ assert.ok(ops.every(([, src, dst]) => [src, dst].every(p => p === undefined || p.startsWith(base))))
+})
+
+test('a boot-loop log past the discard ceiling is reclaimed, not stranded in .1', () => {
+ const base = '/logs/desktop-chromium.log'
+ const ops = planLogRotation(LOG_DISCARD_BYTES + 1, base)
+
+ // Renaming a multi-GB file keeps the disk full for a cycle a healthy app may
+ // never reach, so every generation is deleted outright.
+ assert.ok(ops.every(([op]) => op === 'rm'))
+ assert.ok(ops.some(([, src]) => src === base))
+})
diff --git a/apps/desktop/electron/log-rotation.ts b/apps/desktop/electron/log-rotation.ts
new file mode 100644
index 00000000000..3b0a86328fd
--- /dev/null
+++ b/apps/desktop/electron/log-rotation.ts
@@ -0,0 +1,48 @@
+// 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}`
+
+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
+}