fix(daemon): rotate non-empty log on start to preserve previous crashes (#991)
Summary
In src/browser_harness/daemon.py, the daemon entry point called open(LOG, "w").close() on every startup, truncating the log file to 0 bytes. When ensure_daemon() respawned a dead or crashed daemon, the prior daemon's terminal output (WebSocket close code, fatal exceptions, or exit diagnostics) was destroyed before operators or debuggers could inspect it.
Changes
- Added
_rotate_log(log_path=None)to rotate any existing non-empty log file to{LOG}.1usingos.replace. - Replaced
open(LOG, "w").close()indaemon.py:__main__with_rotate_log(LOG)so previous run diagnostics are retained in{LOG}.1and the active daemon starts fresh. - Added unit tests:
test_rotate_log_preserves_previous_run_outputasserting non-empty log rotation to.1.test_rotate_log_ignores_empty_or_missing_fileasserting empty or missing log files do not produce spurious.1backup files.
Closes #991.
Signed-off-by: Yunus Ardaguney yunusarda-sudo@users.noreply.github.com
Summary by cubic
Fixes the daemon truncating its log on startup so previous crash diagnostics survive respawns.
ensure_daemon() now rotates any non-empty log before opening the child's stderr sink instead of wiping it to 0 bytes, keeping three generations (.1–.3) so all start attempts in the retry loop survive. Remote spawns are now serialized under the same spawn lock as local ones, and rotation is skipped while another daemon is still starting so a concurrent caller cannot move a live child's log.
- Reports rotation failures to stderr and the log instead of hiding them.
- Adds tests for rotation, three-generation retention, empty or missing log files, failed-spawn rotation, and remote spawns with a starting sibling.