Skip to content

fix(app): no-output watchdog replaces fixed timeouts; graceful kills; cold floor everywhere; orphan serve reaping - #1096

Merged
iamtoruk merged 4 commits into
mainfrom
fix/app-timeout-watchdog
Aug 22, 2026
Merged

iamtoruk merged 4 commits into
mainfrom
fix/app-timeout-watchdog

Conversation

@iamtoruk

@iamtoruk iamtoruk commented Aug 22, 2026 •

Copy link
Copy Markdown
Member

The failure class

Desktop-app telemetry across 386 installs plus a local repro established a
timeout failure class that is not a slow-machine annoyance but a permanent
broken state.

app/electron/cli.ts capped every read at DEFAULT_TIMEOUT_MS = 45_000 of
total runtime, so any section command — optimize, yield, models,
sessions, spend, audit, act report, plan — that ran longer than 45s
was SIGKILLed mid-parse. On the repro machine a warm optimize measures
52.5s on 0.9.20 and 39.6s on main: over the cliff, and near it. The kill
surfaced as a red CliErrorPanel "timed out", usePolled re-polled 60s later,
and the whole thing repeated forever — the cache never persisted, so every retry
restarted the same doomed scan.

Three compounding pieces:

  1. WARMUP_TIMEOUT_MS (10 min) covered only the overview status while cold.
    The moment ready flipped — which an overview error also does — every
    section spawned with the plain 45s cap, straight into the queue behind the
    still-running cold parse.
  2. Both kill branches used SIGKILL, so a lock-holding parser's cache refresh
    lock was only ever reclaimed through the next parse's stale-pid takeover
    rather than released by the process that took it.
  3. Orphan codeburn serve --stdio children survive app crashes — 4 found alive
    on the dev machine, up to 4 days old.

What changed

The parse heartbeats (src/parser.ts). A watchdog is only as good as the
signal it watches, and the real CLI is not as chatty as it looks: a cold parse's
inter-provider cache save (src/parser.ts:4165) was measured at 31.6s of total
silence
, plus an ~8s tail. A 45s silence window survives that only until the
machine is 1.5x slower. So under CODEBURN_PROGRESS=1 a running parse now emits a
keepalive event every 10s regardless of phase, started and stopped around
runParse so it covers the throwing paths too, and — for served requests — around
runCaptured, the one seam that also covers the ~8s aggregation-and-serialization
tail landing right after the parse's own quiet stretches. (A one-shot spawn keeps
the parse-only wrap; an 8s tail alone sits comfortably inside the window.) Silence
now genuinely means stopped. The event carries nothing; the Splash reducer ignores it, and gained a
default arm so any future event kind from a newer CLI is ignored rather than
crashing an older app.

codeburn serve drains before exiting, but not forever (src/serve.ts).
runStdioServe returned as soon as the transport closed, so a request still in
flight lost its response frame — and because runCaptured monkeypatches
process.exit into a thrown ExitSignal, the new explicit exit turned that into
exit 1. The finally now awaits the request queue, bounded at 45s: generous
enough that nothing legitimate is truncated, short enough that an async-wedged
request releases the child rather than making it the orphan this whole section is
about. The ceiling is documented in place — a synchronously wedged parse never
yields to that timer, or to any other JS path; only a signal ends it.

No-output watchdog (app/electron/cli.ts). timeoutMs now bounds silence,
not total runtime: the window restarts on every byte the child writes on
stdout/stderr, and on every serve frame carrying that request's id. A 15-minute
absolute ceiling still reaps a livelocked child. Every read spawn sets
CODEBURN_PROGRESS=1 (the env already exists on main — src/parser.ts:2937) so
a multi-minute parse heartbeats instead of going silent; those heartbeat lines
are stripped from any stderr that becomes an error message.

Cold floor everywhere (app/electron/main.ts). While the first hydration is
still running, every read gets the 10-minute floor, not just the overview.

Graceful kill. SIGTERM first, SIGKILL after a 5s grace, on the spawn branch,
the serve branch, and the mutation-restart/destroy paths. To be precise about the
benefit: SIGTERM is catchable, so armSignalCleanup (src/session-cache.ts:1549)
unlinks the child's own cache refresh lock before it dies. Under SIGKILL that lock
is merely left behind and self-heals via the stale-pid takeover. Neither signal
publishes a partial cache — nothing does.
The grace is a clean release in place
of a takeover, not data preservation. Quit stays a hard kill: its flush budget is
1.5s, shorter than the grace, and a lock left there still self-heals.

No red panel during provable cold — and it always ends. A timeout raised while
hydration is running is flagged cold on the error envelope (add-only, optional
field); the renderer keeps its indexing splash and keeps the sections gated rather
than latching ready. Only timeouts are flagged — a permission or nonzero
failure still surfaces normally. The claim is bounded by the cold window
itself
: overviewWarmed only flips on success, so without a bound an install
that can never hydrate would sit behind the splash forever, with no error and no
route to the "Locate the CLI" recovery. Past 10 minutes, a timeout is an error.

Orphan reaping, Windows included. The serve child's pid and full command
line
are recorded under userData; on boot a serve orphaned by a previous crash
is SIGTERMed, but only after the OS reports that pid running that exact argv.
A keyword sniff ("a cli.js running serve") matches plenty of unrelated tools,
and pids recycle.

Windows is the majority of the desktop fleet and previously had no orphan escape
— no ps, and no stdin-close recourse once the app has crashed — so it is covered
now. On the command-line lookup: tasklist never reports argv (image name and
window title only) and wmic is removed from current Windows, so this reads
Win32_Process.CommandLine via Get-CimInstance. Quoting is normalized on both
sides — we record a plain space-joined argv while Windows reports the real quoted
line — so C:\Program Files\… round-trips without loosening the match. src/serve.ts already
exits on stdin EOF and the app never detaches, so the remaining gap was a handle
outliving the transport — src/main.ts now exits explicitly once runStdioServe
returns.

Before / after, at production timings

Real spawnCli from origin/main vs this branch, production DEFAULT_TIMEOUT_MS
(45s), against a CLI that heartbeats every 5s and answers at 60s — the 0.9.20
warm-optimize shape with margin:

BEFORE (origin/main, fixed 45s total cap)
  RESULT timeout after 45.0s: codeburn optimize timed out after 45000ms

AFTER (this branch, 45s silence watchdog)
  RESULT ok after 60.1s: {"ok":1,"seconds":60}

And the lock half, against a silent child holding a lock it releases on SIGTERM
the way armSignalCleanup does:

BEFORE (SIGKILL)              RESULT timeout after 45.0s   refresh lock: LEFT BEHIND
AFTER  (SIGTERM then SIGKILL) RESULT timeout after 45.0s   refresh lock: UNLINKED BY OWNER

Read that as the modest thing it is: the lock is released by its owner instead of
being reclaimed later by a stale-pid takeover. Nothing is rescued from the parse.

Detection of a genuinely stuck child is unchanged: still 45s, still a timeout —
what changed is that a working child no longer looks stuck, on both sides (the
app resets on output, and the CLI now produces some).

Tests

app/electron gains coverage for each rule; the two watchdog tests were verified
to fail against the old fixed-timeout logic (reset removed → both red), and
the renderer gating test fails when ready latches on a cold error.

  • watchdog does not kill a child producing output well past the window
  • a child that goes silent is killed, timed from its last byte (which lands
    mid-window, so a fixed-cap implementation fails this)
  • a running parse heartbeats through 90s of silence, stops when the parse ends,
    keeps beating until the outermost parse finishes, and stays quiet for a plain
    CLI run
  • codeburn serve answers an in-flight request in full and exits 0 when stdin
    closes on the same tick, and gives up on an async-wedged one at the drain bound
    (a fixture supplies its own never-settling buildProgram to the real
    runStdioServe, so no test hook exists in shipped code)
  • the win32 reap asks CIM, matches the Electron-shaped argv through both quoting
    conventions and a path with spaces, and refuses a recycled pid, an unavailable
    PowerShell, and an empty CommandLine
  • the cold claim expires with the cold window, so the error panel is reachable
  • a child that ignores SIGTERM is still reaped by a quit landing inside the grace
    (the child signals readiness with a byte, so the window is measured from that
    rather than from spawn — node's boot cost cannot flake it; 6/6 under 8 busy loops)
  • progress heartbeats never leak into the surfaced error message
  • every read spawn carries CODEBURN_PROGRESS=1
  • SIGTERM arrives first; a child ignoring it is SIGKILLed after the grace; a
    child handling it exits without waiting the grace out
  • the resident watchdog resets on every progress frame of a long serve request
  • the cold floor applies to act report / plan / optimize while cold, and
    reverts once the overview lands
  • a cold timeout is flagged, a cold nonzero is not
  • pidfile records pid + argv on serve start; orphan reaped on boot; a recycled pid
    is never signalled — including the lookalike shapes (cli.js serve --stdio, a
    binary literally named codeburn); missing/garbage/incomplete pidfiles are no-ops
  • the renderer keeps sections gated and the splash up on a cold timeout, and
    releases them on a real failure
app:  535 passed (41 files)                 tsc --noEmit  clean
root: 3353 passed, 0 failed (268 files)     tsc --noEmit  clean

(The root run also reports 26 collection errors from a missing jsdom in this
sandbox's borrowed node_modules; they are identical on the branch point and
unrelated to this change.)

Shipping

CODEBURN_PROGRESS and the new keepalive event exist in no shipped release —
both are post-0.9.20. Packaged builds spawn their own bundled, version-matched CLI,
so app and CLI ship together and this is a non-issue there. A dev repo build or a
pinned/persisted external codeburn that predates them simply emits no heartbeats,
and a long silent parse degrades to the old behaviour (a timeout at the window)
rather than misbehaving. The renderer's default reducer arm covers the opposite
skew, a newer CLI against an older app.

Contract

Payload shapes are add-only: the error envelope gains an optional cold?: true,
absent on older mains, and the renderer reads it defensively. No new CLI flag or
env — CODEBURN_PROGRESS already exists on main. The CLI-side changes (src/main.ts exiting after
runStdioServe, src/serve.ts draining first, the src/parser.ts keepalive) all
ship in this PR and are invisible to an older app.

… cold floor everywhere; orphan serve reaping

The desktop app capped every read at 45s of TOTAL runtime, so a slow machine's
warm `optimize` (52.5s on 0.9.20, 39.6s on main) was SIGKILLed mid-parse and the
panel painted a red "timed out" that a 60s poll then reproduced forever.

- cli.ts: the read timeout is now a no-output watchdog. The window restarts on
  every byte the child writes (serve: every frame carrying that request's id),
  so only a genuinely silent child times out. A 15-minute absolute ceiling still
  reaps a livelocked child. Every read spawn sets CODEBURN_PROGRESS=1 so a long
  parse heartbeats through it; progress lines are stripped from any surfaced
  stderr so they can never become the error message.
- cli.ts: a timed-out child gets SIGTERM first and SIGKILL only after a 5s
  grace, on both the spawn and the serve branch. That lets the CLI's signal
  cleanup publish its partial parse and release the cross-process refresh lock
  instead of leaving both stale.
- main.ts: the 10-minute cold floor now covers EVERY read while the first
  hydration is still running, not just the overview. Sections start polling the
  moment `ready` flips (which an overview error also does), and a 45s section
  spawn queued behind the cold parse was killed on arrival.
- main.ts/renderer: a timeout raised while the hydration is still running is
  flagged `cold`, and the renderer keeps its indexing splash (and keeps the
  sections gated) instead of latching ready and painting CliErrorPanel. Only
  timeouts are flagged; a permission or nonzero failure still surfaces.
- cli.ts/main.ts: the resident serve child's pid is recorded under userData and
  a serve orphaned by a previous crash is reaped at launch, after `ps` confirms
  the pid still belongs to a codeburn serve.
- src/main.ts: `codeburn serve` exits explicitly once its stdin closes, so a
  handle outliving the transport cannot keep the child alive as an orphan.
…M grace

A settings mutation replaces the resident serve child, which may be mid-write:
the same lock hazard a timed-out read has, so it gets the same treatment.
restartAfterMutation and destroy now go through killGracefully instead of
SIGKILL. reapAll's one-shot loop stays a hard kill and now says why - quit has a
1.5s flush budget, shorter than the grace, so waiting one out would wedge the
quit, and a one-shot read holds no lock worth releasing.
F1 - `codeburn serve` lost an in-flight request's response when stdin closed
mid-flight: runStdioServe returned before its queue drained, and the explicit
process.exit(0) then hit runCaptured's monkeypatched exit, throwing ExitSignal
and exiting 1 with the frame never written. The finally now awaits the queue.

F2 - the SIGTERM rationale was false. armSignalCleanup unlinks the refresh lock
and re-raises; it publishes no partial cache, and a lock left by SIGKILL already
self-heals through the stale-pid takeover. SIGTERM-first is kept for the real
(smaller) benefit - a clean lock release instead of a takeover - and every
comment plus the CHANGELOG now says only that.

F3 - the cold gate had no exit. overviewWarmed only flips on success, so an
install that can never hydrate sat behind an indexing splash forever with no
error and no route to the CLI recovery. The cold claim now expires with the cold
window itself.

F4 - the real CLI does not heartbeat the way the demo did: a cold parse's
inter-provider cache save measured 31.6s of total silence, which a 45s window
survives only until the machine is 1.5x slower. Under CODEBURN_PROGRESS a
running parse now emits a keepalive every 10s regardless of phase, so silence
genuinely means stopped. Consumers that do not know the event ignore it.

F5 - the orphan-reap identity check matched any `cli.js` running `serve`. The
pidfile now records the exact argv and `ps -ww` must match it exactly.

F6 - bump() re-armed the watchdog after settle, leaving a timer finish() never
clears when a killed child's buffered output landed.

F7 - kill paths dropped the child from activeChildren before SIGTERM and the
SIGKILL backstop was unref'd, so a quit inside the 5s grace orphaned a child
that ignores SIGTERM. It now stays registered until it actually dies.

N8 - the silence test wrote its only byte at t~0, so it passed without the
re-arm. The byte now lands mid-window and the kill is asserted from it.
N9 - documented why mutations keep a plain total cap.
… grace test

G1a - the stdin-EOF drain is now bounded (Promise.race against 45s, overridable
via CODEBURN_SERVE_DRAIN_MS for the test). Generous enough that no legitimate
request answered at EOF comes near it, and an async-wedged one releases the
child instead of turning it into the orphan the drain was added to prevent. The
comment names the ceiling: a SYNCHRONOUSLY wedged parse never yields to this
timer or to any other JS path, and only a signal can end that process.

G1b - Windows is the majority of the desktop fleet and had no orphan escape at
all: no ps, and no stdin-close recourse once the app has crashed. The reap now
works there. tasklist cannot report argv (image name and window title only) and
wmic is gone from current Windows, so the command line comes from
Get-CimInstance Win32_Process. Match discipline is unchanged - exact argv - with
quoting normalized on both sides, because we record a plain space-joined argv
while Windows reports the real quoted command line; a path with spaces still
round-trips. serveCommandMatches is extracted and tested directly.

G2 - the grace test raced node's cold boot (a 300ms window). The child now emits
one byte the moment it is ready, so the watchdog window is measured from
READINESS, not from spawn: boot cost cannot eat it at any machine speed. Timeout
raised on top of that. 6/6 green under 8 busy loops.

G3 - a served request heartbeats for its whole duration now, not just its parse:
the wrap moved to the one seam (runCaptured) that also covers aggregation and
payload serialization, whose ~8s tail lands back-to-back with the parse's own
quiet stretches. The one-shot path keeps the parse-only wrap, where an 8s tail
is comfortably inside the window on its own, and the CHANGELOG now says exactly
that instead of claiming every phase.

G4 - stillCold() anchors to coldStartBegan ?? bootedAt.

Text: the cold-timeout bound no longer claims to restore the "locate the CLI"
recovery (that is a not-found state, never cold-flagged), and the CHANGELOG
states the >15min case honestly - the ceiling still ends it, but partial cache
saves mean successive polls converge rather than each rescanning from nothing.
@iamtoruk
iamtoruk merged commit 9cfa957 into main Aug 22, 2026
15 checks passed
pull Bot pushed a commit to TheTechOddBug/codeburn that referenced this pull request Aug 24, 2026
…ver, serve orphan reaping

The 0.9.20 menubar never completed a fetch on a large corpus: the cache
version bump forced a full rehydration, DataClient's fixed 45s kill ended
it mid-transaction, cache-refresh-lock had no signal cleanup so the dead
holder's lock survived, and DEFAULT_WAIT_MS (30s) < DEFAULT_STALE_MS (90s)
meant no waiter could ever recover it.

A. Swift port of getagentseal#1096's watchdog. CLIWatchdog holds the constants and the
   pure verdict; spawns and the resident serve child set CODEBURN_PROGRESS=1;
   the window restarts on any stdout/stderr byte; 45s silence, 10min cold
   floor until the first payload, 15min ceiling, SIGTERM then SIGKILL after
   5s. ServeConnection's fixed 60s warm cap becomes the same silence window,
   re-armed by each progress frame, and a spent death budget is now a
   5-minute cooldown instead of disabling the resident for the app run.

B. cache-refresh-lock arms SIGINT/SIGTERM cleanup the way session-cache does
   for hydrating.lock, so a SIGTERMed holder unlinks its own lock.

C. Staleness also opens on a dead holder pid, and the waiter budget derives
   from staleMs so it can never expire before the gate it waits for. A live
   holder - fresh heartbeat, pid answers signal 0 - is never taken from.
   parser.ts heartbeats through the lock wait, the one silent stretch left.

D. The app closes its end of a retired child's stdin (dropping the handle
   left the pipe alive inside the Process), reaps every serve child
   synchronously at quit, and records pid+argv so a crash-orphaned child is
   reaped next launch. serve's final exit no longer runs through a
   monkeypatched process.exit, and its post-drain cleanup is bounded.

Closes getagentseal#1117
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant