Skip to content

The agent that grows with you

fix(desktop): make the relaunch waiter's script log per-attempt, UTF-8 and bounded

ethernetddde33122d8c
diff --git a/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts b/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
index 74299210d73..47ffba13a51 100644
--- a/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
+++ b/apps/desktop/electron/updater/relaunch-waiter-lifecycle.test.ts
@@ -2,11 +2,18 @@ import assert from 'node:assert/strict'
import { type ChildProcess, spawn } from 'node:child_process'
import { once } from 'node:events'
import fs from 'node:fs'
+import os from 'node:os'
import path from 'node:path'
import { test, vi } from 'vitest'
-import { type RelaunchWaiterHandle, type SpawnWaiter, startRelaunchWaiter } from './relaunch-waiter'
+import {
+ RELAUNCH_WAITER_LOG_TAIL_BYTES,
+ type RelaunchWaiterHandle,
+ type SpawnWaiter,
+ startRelaunchWaiter,
+ waiterLogTail
+} from './relaunch-waiter'
const scriptPath: string = path.resolve(import.meta.dirname, '../../scripts/update-relaunch-waiter.ps1')
@@ -248,3 +255,29 @@ test('every failed start tells the log why it failed', async (): Promise<void> =
)
assert.match(logs.join('\n'), /no handshake within 200ms \(pid \d+\); stopping it/)
}, 20_000)
+
+test('the script log report keeps only this attempt, reads a bounded suffix, and decodes UTF-8', (): void => {
+ const dir: string = fs.mkdtempSync(path.join(os.tmpdir(), 'waiter-log-'))
+ const file: string = path.join(dir, 'waiter.log')
+
+ try {
+ // Older attempt's lines, then enough filler to push them past the bounded read.
+ const filler: string = `t [hermes-relaunch-old] pid=1 ${'x'.repeat(200)}\n`.repeat(
+ Math.ceil(RELAUNCH_WAITER_LOG_TAIL_BYTES / 200) + 5
+ )
+
+ fs.writeFileSync(
+ file,
+ `t [hermes-relaunch-gone] pid=2 stale line\n${filler}t [hermes-relaunch-me] pid=3 pfad C:\\Benutzer\\Jörg fehlgeschlagen\nt [hermes-relaunch-other] pid=4 not mine\n`,
+ 'utf8'
+ )
+
+ const report: string = waiterLogTail(file, 'hermes-relaunch-me')
+ assert.match(report, /Jörg fehlgeschlagen/)
+ assert.doesNotMatch(report, /not mine|stale line|\[hermes-relaunch-old\]/)
+ assert.equal(waiterLogTail(file, 'hermes-relaunch-gone'), `${file}:\nno lines from this attempt`)
+ assert.match(waiterLogTail(path.join(dir, 'absent.log'), 'x'), /not written/)
+ } finally {
+ fs.rmSync(dir, { recursive: true, force: true })
+ }
+})
diff --git a/apps/desktop/electron/updater/relaunch-waiter.ts b/apps/desktop/electron/updater/relaunch-waiter.ts
index 42cad88cfa9..e2191af9d07 100644
--- a/apps/desktop/electron/updater/relaunch-waiter.ts
+++ b/apps/desktop/electron/updater/relaunch-waiter.ts
@@ -72,16 +72,39 @@ function errorText(error: unknown): string {
return error instanceof Error ? `${error.name}: ${error.message}` : String(error)
}
-function waiterLogTail(maxChars: number = 1500): string {
- const file: string = path.join(os.tmpdir(), RELAUNCH_WAITER_LOG_FILENAME)
+/** Max bytes read from the shared script log: a failure report must not scale with its history. */
+export const RELAUNCH_WAITER_LOG_TAIL_BYTES = 16 * 1024
+
+/**
+ * The script log is shared by every attempt, so keep only this attempt's lines
+ * (the script tags each with its stage dir name) from a bounded suffix.
+ */
+export function waiterLogTail(
+ file: string,
+ attempt: string,
+ maxBytes: number = RELAUNCH_WAITER_LOG_TAIL_BYTES
+): string {
+ let text: string
try {
- const text: string = fs.readFileSync(file, 'utf8').trim()
+ const fd: number = fs.openSync(file, 'r')
- return `${file}:\n${text.slice(-maxChars)}`
+ try {
+ const size: number = fs.fstatSync(fd).size
+ const length: number = Math.min(size, maxBytes)
+ const buffer: Buffer = Buffer.alloc(length)
+ fs.readSync(fd, buffer, 0, length, size - length)
+ text = buffer.toString('utf8')
+ } finally {
+ fs.closeSync(fd)
+ }
} catch {
return `${file}: not written (the script never ran far enough to log)`
}
+
+ const lines: string[] = text.split(/\r?\n/).filter((line: string): boolean => line.includes(`[${attempt}]`))
+
+ return `${file}:\n${lines.length ? lines.join('\n') : 'no lines from this attempt'}`
}
/** Pure: the exact argv the waiter is spawned with. */
@@ -184,6 +207,8 @@ export async function startRelaunchWaiter(
let closed = false
const startedAt = Date.now()
+ const attempt: string = path.basename(staging.stageDir)
+ const waiterLogPath: string = path.join(os.tmpdir(), RELAUNCH_WAITER_LOG_FILENAME)
const closedPromise = new Promise<void>(resolve => {
child.once('close', () => {
@@ -248,7 +273,7 @@ export async function startRelaunchWaiter(
log(
`[updates] relaunch waiter: exited before its handshake after ${Date.now() - startedAt}ms ` +
`(exit code ${code ?? 'none'}, signal ${signal ?? 'none'}; 1 = script error, 2 = identity ` +
- `${options.identityName} not found or has no app id)\n${waiterLogTail()}`
+ `${options.identityName} not found or has no app id)\n${waiterLogTail(waiterLogPath, attempt)}`
)
}
@@ -276,7 +301,7 @@ export async function startRelaunchWaiter(
if (!closed) {
log(
`[updates] relaunch waiter: no handshake within ${handshakeTimeoutMs}ms ` +
- `(pid ${child.pid ?? 'none'}); stopping it\n${waiterLogTail()}`
+ `(pid ${child.pid ?? 'none'}); stopping it\n${waiterLogTail(waiterLogPath, attempt)}`
)
}
diff --git a/apps/desktop/scripts/update-relaunch-waiter.ps1 b/apps/desktop/scripts/update-relaunch-waiter.ps1
index 3633c7f22df..40b206ade6c 100644
--- a/apps/desktop/scripts/update-relaunch-waiter.ps1
+++ b/apps/desktop/scripts/update-relaunch-waiter.ps1
@@ -9,9 +9,14 @@ param(
)
# The parent runs this detached with no stdio, so this file is the only record of why it gave up.
+# It is shared across attempts: lines carry the stage dir name so the parent can pick out its own,
+# and it restarts when it passes 64KB so it cannot grow without bound.
$LogFile = Join-Path $env:TEMP 'hermes-relaunch-waiter.log'
+$Attempt = Split-Path -Leaf (Split-Path -Parent $ReadyFile)
+$Utf8NoBom = New-Object System.Text.UTF8Encoding($false)
+try { if ((Test-Path -LiteralPath $LogFile) -and (Get-Item -LiteralPath $LogFile).Length -gt 65536) { Remove-Item -LiteralPath $LogFile -Force } } catch {}
function Write-WaiterLog([string]$Message) {
- try { Add-Content -LiteralPath $LogFile -Value ('{0} [{1}] {2}' -f (Get-Date -Format o), $PID, $Message) } catch {}
+ try { [IO.File]::AppendAllText($LogFile, ('{0} [{1}] pid={2} {3}' -f (Get-Date -Format o), $Attempt, $PID, $Message) + "`n", $Utf8NoBom) } catch {}
}
try {