Skip to content

Make broker diagnostic logs opt-in#933

Merged
willwashburn merged 5 commits into
mainfrom
codex/quiet-broker-logs
May 21, 2026
Merged

Make broker diagnostic logs opt-in#933
willwashburn merged 5 commits into
mainfrom
codex/quiet-broker-logs

Conversation

@willwashburn
Copy link
Copy Markdown
Member

Summary

  • Default broker tracing to off unless AGENT_RELAY_BROKER_LOG=1 or RUST_LOG is set.
  • Correct PTY tracing target syntax so opt-in filters apply to agent_relay::worker::pty.
  • Add changelog and trajectory record.

Tests

  • cargo test --manifest-path crates/broker/Cargo.toml tracing_filter
  • cargo test --manifest-path crates/broker/Cargo.toml pty_worker::tests
  • cargo fmt --manifest-path crates/broker/Cargo.toml --all -- --check

@willwashburn willwashburn requested a review from khaliqgant as a code owner May 21, 2026 04:16
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 21, 2026

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 4936f48a-651b-4140-a1b6-0e132d87d22f

📥 Commits

Reviewing files that changed from the base of the PR and between 68439e0 and 0818b69.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • packages/sdk/src/index.ts

📝 Walkthrough

Walkthrough

Broker tracing is now opt-in via AGENT_RELAY_BROKER_LOG (or RUST_LOG); init_tracing accepts a broker_id and chooses Off/Stderr/File, PTY tracing targets/syntax were aligned, SDK helpers and CLI log commands were added, and trajectories/CHANGELOG were updated.

Changes

Broker Tracing Configuration

Layer / File(s) Summary
Tracing filter and initialization
crates/broker/src/runtime/util.rs
Adds AGENT_RELAY_BROKER_LOG handling, TracingDestination, tracing_destination(), tracing_filter_directive(), platform log-dir and filename helpers, and changes init_tracing()init_tracing(broker_id) to select Off/Stderr/File writers. Unit tests added.
CLI integration for per-run identifier
crates/broker/src/cli/mod.rs
Adds Commands::log_identifier() and non_empty_name; parses CLI before calling runtime::init_tracing(&cli.command.log_identifier()).
PTY session tracing targets
crates/broker/src/pty.rs
Adds explicit target = "agent_relay::worker::pty" to tracing calls in PtySession::has_exited() and shutdown() so PTY traces are routed to the configured filter.
PTY worker logging syntax alignment
crates/broker/src/pty_worker.rs
Standardizes tracing macro key syntax from target = "..." to target: "..." across multiple PTY worker log sites; no behavioral changes.
SDK broker-log utilities
packages/sdk/src/broker-logs.ts, packages/sdk/src/__tests__/*, packages/sdk/src/index.ts
Adds SDK helpers to locate the broker log directory, list and parse rotated log filenames, tail file ends, prune by age with keep/current protection and dry-run, and clear files. Tests validate parsing, tailing, pruning, and clearing behavior; SDK index re-exports new APIs.
CLI commands and wiring
src/cli/commands/log.ts, src/cli/bootstrap.ts, src/cli/bootstrap.test.ts
Adds agent-relay log subcommands: path, list, view, rotate, clear; implements handlers using SDK helpers (with validation, dry-run, and force guards) and registers the commands in bootstrap; updates CLI snapshot tests.
Change documentation and trajectory
.trajectories/*, CHANGELOG.md, web/content/docs/*
Adds trajectory JSON/MD documenting the opt-in broker tracing decision, updates trajectories/index.json timestamp, updates CHANGELOG to document platform log paths and AGENT_RELAY_BROKER_LOG control values (off/stderr) and RUST_LOG usage, and adds CLI/SDK docs.

Sequence Diagram(s)

sequenceDiagram
  participant UserCLI as User CLI
  participant CLI as registerLogCommands
  participant SDK as broker-logs SDK
  participant FS as Filesystem
  participant Broker as runtime::init_tracing
  UserCLI->>CLI: run `log view|list|rotate|clear`
  CLI->>SDK: call listBrokerLogs / tailBrokerLog / pruneBrokerLogs / clearBrokerLogs
  SDK->>FS: read dir / stat / unlink / tail file
  CLI->>Broker: start broker with broker_id -> init_tracing(broker_id)
  Broker->>FS: resolve/create log dir when File destination selected
  Broker->>Broker: install tracing EnvFilter and writer
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • khaliqgant

🐰 I nudged the logs to hush and hide,
Quiet by default, tucked safe inside.
Wave a flag or set RUST_LOG bright,
And broker songs return at night.
Hop, trace, and tidy — carrot-approved delight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Make broker diagnostic logs opt-in' directly describes the main change: transitioning broker tracing from always-on to disabled-by-default with explicit opt-in via environment flags.
Description check ✅ Passed The description provides a clear summary of changes (default tracing off, PTY target fix, changelog/trajectory), lists three test commands, but lacks expected section structure from the template (Summary uses a bulleted list instead of prose, and Test Plan checkbox format differs).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/quiet-broker-logs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

Previously AGENT_RELAY_BROKER_LOG=off was the default. Move the default to
a per-broker log file under the user's home so diagnostics are captured
without contaminating PTY streams, and reshape the env var as a destination
switch: `off` to disable, `stderr`/`print` to keep the legacy on-terminal
behavior, anything else (including unset) routes to the file.

`RUST_LOG` continues to control the level filter and now defaults to `info`
when logging is enabled.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/broker/src/runtime/util.rs`:
- Around line 295-304: The test log_file_path_uses_agentworkforce_logs_dir is
asserting a Unix-style string slice against path.to_string_lossy(), which fails
on Windows; change the assertion to be platform-agnostic by constructing the
expected suffix with PathBuf joins or comparing Path components instead of a raw
string. Locate the test function log_file_path_uses_agentworkforce_logs_dir and
replace the contains(".agentworkforce/relay/logs/my-broker.log") check with a
comparison that builds
PathBuf::from(".agentworkforce").join("relay").join("logs").join("my-broker.log")
(or iterates and compares the tail components of the returned Path) so the test
passes on all OSes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ba8ed168-b062-44bd-995f-82ec7b3b6334

📥 Commits

Reviewing files that changed from the base of the PR and between 823749b and 1113527.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • crates/broker/src/cli/mod.rs
  • crates/broker/src/runtime/util.rs

Comment thread crates/broker/src/runtime/util.rs
The broker now writes its tracing log to a platform-standard directory
with daily rotation via tracing-appender:

- macOS:   ~/Library/Logs/agent-relay/{brokerId}.log.YYYY-MM-DD
- Linux:   ~/.local/state/agent-relay/logs/{brokerId}.log.YYYY-MM-DD
- Windows: %LOCALAPPDATA%\agent-relay\Logs\{brokerId}.log.YYYY-MM-DD

Rotation alone does not bound disk use, so add an `agent-relay log`
command group (path / list / view / rotate / clear) backed by new
@agent-relay/sdk helpers (`getBrokerLogDir`, `listBrokerLogs`,
`tailBrokerLog`, `pruneBrokerLogs`, `clearBrokerLogs`). `log rotate
--keep-days N` provides explicit retention; the active un-suffixed file
is always kept.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (3)
crates/broker/src/runtime/util.rs (1)

156-158: ⚡ Quick win

Consider logging or falling back to stderr when directory creation fails.

When create_dir_all fails at line 156, init_tracing returns without installing any subscriber, silently disabling all broker diagnostics. Users won't know why logs aren't appearing. Consider either:

  • Falling back to stderr when the file destination is unavailable, or
  • Writing a diagnostic to stderr before returning.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/broker/src/runtime/util.rs` around lines 156 - 158, The init_tracing
function currently returns silently if std::fs::create_dir_all(&log_dir) fails,
losing diagnostics; update init_tracing to handle that error by either falling
back to a stderr-based subscriber or at minimum writing a diagnostic message to
stderr (e.g., using eprintln! or
tracing_subscriber::fmt::writer::BoxMakeWriter::new(std::io::stderr)) before
returning; ensure the code references the existing log_dir and create_dir_all
call and installs a subscriber that logs to stderr as a fallback so broker
diagnostics remain visible when file-based logging cannot be created.
packages/sdk/src/broker-logs.ts (1)

213-213: 💤 Low value

Minor inefficiency in loop condition.

Line 213 uses newlines <= lines, meaning the loop reads one extra chunk even after collecting enough newlines. Changing to newlines < lines would stop as soon as sufficient newlines are found. Not a correctness issue, just a small performance optimization when tailing large files.

♻️ Suggested change
-    while (position > 0 && newlines <= lines) {
+    while (position > 0 && newlines < lines) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sdk/src/broker-logs.ts` at line 213, Update the loop condition in
the tailing logic to avoid reading one unnecessary chunk: in the function where
variables position, newlines, and lines are used (the while loop currently
written as "while (position > 0 && newlines <= lines)"), change the condition to
"newlines < lines" so the loop exits as soon as we've collected the requested
number of newlines; keep the rest of the logic (position decrement, buffer read,
and newlines counting) unchanged.
CHANGELOG.md (1)

78-79: ⚡ Quick win

Simplify changelog entries to be more concise and impact-first.

These entries include detailed implementation specifics (platform directory paths, filename suffixes, individual subcommand enumeration) which make them verbose. As per coding guidelines, changelog entries should be "concise and impact-first, with one short bullet per user-visible change" and "drop implementation backstory."

Consider condensing to:

  • Line 78: "Broker diagnostic logs now write to platform-standard directories with daily rotation. Set AGENT_RELAY_BROKER_LOG=off to disable or use RUST_LOG=... for filtering."
  • Line 79: "Added agent-relay log commands (path, list, view, rotate, clear) to inspect and manage broker logs."

As per coding guidelines: "Changelog entries should be concise and impact-first, with one short bullet per user-visible change. Name the command, API, or schema touched and the practical effect. Drop issue/PR links, internal review notes, implementation backstory, and 'foundation for...' phrasing unless that text clearly explains the shipped impact."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@CHANGELOG.md` around lines 78 - 79, The two CHANGELOG bullets are overly
detailed; replace the long platform/path-specific first bullet with a concise,
impact-first line stating that "Broker diagnostic logs now write to
platform-standard directories with daily rotation" and mention the environment
toggles `AGENT_RELAY_BROKER_LOG=off` and `RUST_LOG=...` for disabling and
filtering, and replace the second verbose line with a short note that "Added
`agent-relay log` commands to inspect and manage broker logs" (you may list the
command group name `agent-relay log` but drop the long subcommand enumeration
and implementation details).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@CHANGELOG.md`:
- Around line 78-79: The two CHANGELOG bullets are overly detailed; replace the
long platform/path-specific first bullet with a concise, impact-first line
stating that "Broker diagnostic logs now write to platform-standard directories
with daily rotation" and mention the environment toggles
`AGENT_RELAY_BROKER_LOG=off` and `RUST_LOG=...` for disabling and filtering, and
replace the second verbose line with a short note that "Added `agent-relay log`
commands to inspect and manage broker logs" (you may list the command group name
`agent-relay log` but drop the long subcommand enumeration and implementation
details).

In `@crates/broker/src/runtime/util.rs`:
- Around line 156-158: The init_tracing function currently returns silently if
std::fs::create_dir_all(&log_dir) fails, losing diagnostics; update init_tracing
to handle that error by either falling back to a stderr-based subscriber or at
minimum writing a diagnostic message to stderr (e.g., using eprintln! or
tracing_subscriber::fmt::writer::BoxMakeWriter::new(std::io::stderr)) before
returning; ensure the code references the existing log_dir and create_dir_all
call and installs a subscriber that logs to stderr as a fallback so broker
diagnostics remain visible when file-based logging cannot be created.

In `@packages/sdk/src/broker-logs.ts`:
- Line 213: Update the loop condition in the tailing logic to avoid reading one
unnecessary chunk: in the function where variables position, newlines, and lines
are used (the while loop currently written as "while (position > 0 && newlines
<= lines)"), change the condition to "newlines < lines" so the loop exits as
soon as we've collected the requested number of newlines; keep the rest of the
logic (position decrement, buffer read, and newlines counting) unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 591dee0c-717d-49b8-ad93-ab74d0932000

📥 Commits

Reviewing files that changed from the base of the PR and between 1113527 and 561db9d.

📒 Files selected for processing (8)
  • CHANGELOG.md
  • crates/broker/src/runtime/util.rs
  • packages/sdk/src/__tests__/broker-logs.test.ts
  • packages/sdk/src/broker-logs.ts
  • packages/sdk/src/index.ts
  • src/cli/bootstrap.test.ts
  • src/cli/bootstrap.ts
  • src/cli/commands/log.ts

willwashburn and others added 2 commits May 21, 2026 15:39
Add a Broker diagnostic logs section to the broker-lifecycle page covering
the platform-standard paths, the AGENT_RELAY_BROKER_LOG values, and the
new agent-relay log subcommands. Mirror the SDK surface
(getBrokerLogDir / listBrokerLogs / tailBrokerLog / pruneBrokerLogs /
clearBrokerLogs) in the TypeScript SDK reference, and add a `log`
section to the CLI reference plus a mention in the CLI overview.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@willwashburn willwashburn merged commit 37d030c into main May 21, 2026
25 of 28 checks passed
@willwashburn willwashburn deleted the codex/quiet-broker-logs branch May 21, 2026 20:39
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