Skip to content

bt-daemon: log through one rotating tracing subscriber - #288

Merged
Stephen Belanger (Qard) merged 7 commits into
mainfrom
design/rotate-serve-log
Nov 16, 2026
Merged

Stephen Belanger (Qard) merged 7 commits into
mainfrom
design/rotate-serve-log

Conversation

@Qard

@Qard Stephen Belanger (Qard) commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Part of the fix for SDK-830. In the reported case, serve.log had grown to 2.2 GB. Every spawned daemon appended its stderr there forever, and the daemon logged two info lines for every hook event.

Changes

  • One subscriber for the daemon. bt trace daemon and the standalone serve install a single tracing subscriber with two outputs:

    • daemon.log in the data directory: newline-delimited JSON at debug. Override with BT_TRACE_LOG_LEVEL.
    • stderr: plain text at warn, or debug with --verbose, coloured only on a terminal. Override with BT_TRACE_STDERR_LEVEL.

    Both overrides use RUST_LOG syntax and are read through env.rs. An unusable value falls back to the default and logs a warning. Panics are also logged through the subscriber.

  • Rotation via file-rotate. daemon.log is a FileRotate that rotates once it passes BT_TRACE_LOG_MAX_MB (default 10) and keeps one earlier generation as daemon.log.1. It is passed straight to the subscriber as its writer. file-rotate adds flate2, because its gzip support isn't optional, even though compression is off here.

  • serve.log only gets stderr now: warnings and anything printed before logging starts. Before spawning a daemon, the hook rotates it with the same file-rotate configuration. Copy-and-truncate and the once-a-minute rotation task are removed. That approach lost the start of every generation, and anything written between the copy and the truncate.

  • Quieter defaults. The per-event event received and event accepted logs move from info to debug.

The global tracing subscriber can only be set once per process. If bt installs its own subscriber later, it needs to leave the daemon command alone. Otherwise the daemon warns and logs through bt's subscriber.

During a daemon handover, two processes can briefly write daemon.log at the same time. If both rotate in that window, one generation can be replaced early.

Tests

  • daemon.log keeps exactly one rotated generation.
  • serve.log rotates before a spawn.
  • The size and filter settings fall back to their defaults when unusable.
  • An end-to-end subscriber test checks that the file records debug events as JSON with their fields, while stderr gets only plain-text warnings.
  • 720 passed, 14 ignored. Clippy is clean with all features and with default features, and cargo fmt --check passes.

🤖 Generated with Claude Code

A spawned daemon's standard streams are appended to serve.log forever, and
it reached gigabytes on long-lived installs. Once the log passes 10 MiB its
newest 10 MiB are kept as serve.log.1 and it is truncated in place, both
before a daemon is spawned and every minute while one runs. The running
daemon writes through an inherited append handle, so truncation (rather
than renaming the file away from that handle) works on every platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T16:56:17.277879Z c065883 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

BRAINTRUST_DAEMON_LOG_MAX_MB sets the limit in MiB (default 10). Both the
spawning hook and the running daemon read it, so it is an environment
variable rather than a serve flag the spawner cannot see. An unset, zero
or unparsable value keeps the default, so the log stays bounded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Daemon tuning variables use the BT_TRACE_ prefix. The env prefix test
now allows them by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Daemon tuning variables are declared in env.rs and read through
crate::env like every other variable. AGENTS.md describes the BT_TRACE_
prefix for them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AbhiPrasad

Copy link
Copy Markdown
Member

what happens when serve.log.1 is too big?

The daemon installs one subscriber with two outputs: newline-delimited JSON
to daemon.log at debug (BT_TRACE_LOG_LEVEL) and plain text to stderr at
warn, or debug with --verbose (BT_TRACE_STDERR_LEVEL). Panics are logged
through it too.

Logs rotate by rename and reopen instead of copy and truncate, which lost
the start of each generation and anything written between the copy and the
truncate. daemon.log rotates in its writer when the next line would pass
BT_TRACE_LOG_MAX_MB; serve.log, which now only receives stderr, is rotated
by the hook before it spawns a daemon. Each rotation replaces <name>.1, and
the two files together hold the latest log in order.

Per-event "event received" and "event accepted" logs move to debug: at info
they wrote two stderr lines for every hook event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard Stephen Belanger (Qard) changed the title bt-daemon: rotate serve.log by size bt-daemon: log through one rotating tracing subscriber Nov 16, 2026
Replace the hand-written rotating writer with file-rotate. daemon.log is a
size-limited FileRotate behind the subscriber's writer, and the hook rotates
serve.log with the same configuration before spawning a daemon. Each keeps
one earlier generation as <name>.1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard

Copy link
Copy Markdown
Collaborator Author

I rewrote the PR so it just uses the tracing crate for everything and does a proper log rotation. It'll only grow up to the size limit before rotating, and rotating will overwrite the old contents.

@Qard
Stephen Belanger (Qard) merged commit cfd2f09 into main Nov 16, 2026
29 checks passed
@Qard
Stephen Belanger (Qard) deleted the design/rotate-serve-log branch November 16, 2026 20:28
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.

2 participants