The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): bound the Chromium log while the shell is running, not only at launch
Austin Pickett782886f87dce
diff --git a/apps/desktop/electron/log-rotation.test.ts b/apps/desktop/electron/log-rotation.test.tsindex a94564cff4d..948a06e842d 100644--- a/apps/desktop/electron/log-rotation.test.ts+++ b/apps/desktop/electron/log-rotation.test.ts@@ -1,8 +1,17 @@import assert from 'node:assert/strict'+import fs from 'node:fs'+import os from 'node:os'+import path from 'node:path'import { test } from 'vitest'-import { LOG_DISCARD_BYTES, LOG_MAX_BYTES, logBackupPath, planLogRotation } from './log-rotation'+import {+ LOG_DISCARD_BYTES,+ LOG_MAX_BYTES,+ logBackupPath,+ planLogRotation,+ reclaimActiveLogIfOversized+} 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@@ -33,3 +42,55 @@ test('a boot-loop log past the discard ceiling is reclaimed, not stranded in .1'assert.ok(ops.every(([op]) => op === 'rm'))assert.ok(ops.some(([, src]) => src === base))})++test('a log a live process keeps appending to is reclaimed in place, not renamed', () => {+ const truncated: string[] = []+ const io = { size: () => LOG_MAX_BYTES, truncate: (f: string) => truncated.push(f) }++ // Chromium holds --log-file open in append mode for the life of the shell:+ // renaming it would leave the writer on the renamed inode and the cap would+ // silently stop applying, so the only reclamation is truncating in place.+ assert.equal(reclaimActiveLogIfOversized('/logs/desktop-chromium.log', io), true)+ assert.deepEqual(truncated, ['/logs/desktop-chromium.log'])+})++test('an under-cap or absent active log is left alone', () => {+ const touched: string[] = []+ const truncate = (f: string) => touched.push(f)++ assert.equal(+ reclaimActiveLogIfOversized('/logs/x.log', { size: () => LOG_MAX_BYTES - 1, truncate }),+ false+ )+ assert.equal(reclaimActiveLogIfOversized('/logs/x.log', { size: () => null, truncate }), false)+ assert.deepEqual(touched, [])+})++test('truncation really frees the file, and an append-mode writer restarts at 0', () => {+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-log-bound-'))+ const file = path.join(dir, 'desktop-chromium.log')++ try {+ // Stand in for Chromium: an O_APPEND handle held across the reclaim.+ const handle = fs.openSync(file, 'a')++ try {+ fs.ftruncateSync(handle, LOG_MAX_BYTES + 1) // Grow without writing GBs.++ assert.equal(+ reclaimActiveLogIfOversized(file, {+ size: f => fs.statSync(f).size,+ truncate: f => fs.truncateSync(f, 0)+ }),+ true+ )++ fs.writeSync(handle, 'FATAL:after\n')+ assert.equal(fs.readFileSync(file, 'utf8'), 'FATAL:after\n')+ } finally {+ fs.closeSync(handle)+ }+ } finally {+ fs.rmSync(dir, { recursive: true, force: true })+ }+})diff --git a/apps/desktop/electron/log-rotation.ts b/apps/desktop/electron/log-rotation.tsindex 3b0a86328fd..cb51309f357 100644--- a/apps/desktop/electron/log-rotation.ts+++ b/apps/desktop/electron/log-rotation.ts@@ -18,6 +18,32 @@ export const LOG_DISCARD_BYTES = LOG_MAX_BYTES * 4export 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.diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 845ce08f1e4..0ba2a1f9567 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -269,10 +269,14 @@ import { resolveHudWindowing } from './hud-windowing'import { createIntroRevealWindowController } from './intro-reveal-window'import { isAuthWall, resolveLinkTitle } from './link-title-wall'import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window'-import { enableLinuxCrashDiagnostics, linuxCrashDiagnostics } from './linux-crash-diagnostics'+import {+ CHROMIUM_LOG_FILENAME,+ enableLinuxCrashDiagnostics,+ linuxCrashDiagnostics+} from './linux-crash-diagnostics'import { notifyLauncherWindowRevealed } from './linux-launcher-ready'import { createLocalBackendLifecycle, waitForTeardown } from './local-backend-lifecycle'-import { planLogRotation } from './log-rotation'+import { ACTIVE_LOG_POLL_MS, planLogRotation, reclaimActiveLogIfOversized } from './log-rotation'import { ensureMainWindow } from './main-window-lifecycle'import {assertManagedUpdatePreflightClear,@@ -986,8 +990,11 @@ const DESKTOP_LOG_BUFFER_MAX_CHARS = 64 * 1024// way, and never let optional diagnostics fail the shell's startup.const CRASH_DIAGNOSTICS_LOGS_DIR = path.dirname(DESKTOP_LOG_PATH)+const CRASH_DIAGNOSTICS = linuxCrashDiagnostics(CRASH_DIAGNOSTICS_LOGS_DIR)+const CHROMIUM_LOG_PATH = path.join(CRASH_DIAGNOSTICS_LOGS_DIR, CHROMIUM_LOG_FILENAME)+enableLinuxCrashDiagnostics(- linuxCrashDiagnostics(CRASH_DIAGNOSTICS_LOGS_DIR),+ CRASH_DIAGNOSTICS,CRASH_DIAGNOSTICS_LOGS_DIR,{ensureLogsDir: dir => fs.mkdirSync(dir, { recursive: true }),@@ -1831,6 +1838,35 @@ let bootProgressState = {timestamp: Date.now()}+// Chromium owns its --log-file for the life of the process, so the startup+// reclaim above cannot bound a shell that stays up for days writing errors.+// Poll and truncate in place; renaming would leave Chromium appending to the+// renamed inode. Unref'd so it never holds the process open.+function startChromiumLogWatcher(file) {+ const io = {+ size: f => {+ try {+ return fs.statSync(f).size+ } catch {+ return null // Not created yet — nothing has been logged.+ }+ },+ truncate: f => fs.truncateSync(f, 0)+ }++ const timer = setInterval(() => {+ try {+ if (reclaimActiveLogIfOversized(file, io)) {+ rememberLog(`[diagnostics] truncated oversized Chromium log ${file}`)+ }+ } catch {+ // Best-effort — an unbounded log beats a crashed shell.+ }+ }, ACTIVE_LOG_POLL_MS)++ timer.unref?.()+}+function rotateLogIfNeededSync(base) {let size@@ -18535,6 +18571,10 @@ app.whenReady().then(() => {// before the backend start path awaits the same single-flight promise.void ensureLoginShellPath()+ if (CRASH_DIAGNOSTICS) {+ startChromiumLogWatcher(CHROMIUM_LOG_PATH)+ }+const systemCa = installWindowsSystemCaTrust(tls)if (systemCa.applied) {