1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! Tracing and logging initialisation for tcode.
//!
//! Why: All tcode binaries and the library's test harness need consistent
//! tracing initialisation that writes to stderr (never stdout — stdout is the
//! API transport channel). Centralising it here prevents duplicated setup and
//! ensures the `try_init` pattern is used everywhere so test binaries remain
//! idempotent.
//! What: `init_tracing` initialises the global tracing subscriber from the
//! `RUST_LOG` env var. `init_tracing_for_test` is a lightweight variant for
//! test binaries that silently drops duplicate init errors.
//! Test: `init_tracing_for_test_is_idempotent` calls the function twice and
//! asserts no panic.
//!
//! ## Log-level convention (#2857)
//!
//! trusty-code makes control-flow decisions that change a run's outcome —
//! caps tripping, retries, delegation refusals, fallbacks, truncations,
//! skips, gate interceptions. Every such decision point MUST log at the
//! level below when it fires; the organising principle is:
//!
//! - **`warn!`** — the decision silently changes the run's outcome (a cap
//! trips, a retry budget is exhausted, a gate refuses/intercepts an
//! action, a floor is exceeded). Minimum level for anything that could
//! otherwise only be diagnosed via cross-run forensics (the #2852 class
//! of bug this ticket exists to prevent).
//! - **`info!`** — a degradation the model or user acts on, but the run
//! proceeds normally (a fallback lane serves results, a redundant
//! re-run is suppressed, a budget truncation drops lowest-scored
//! content, cadence compression fires on schedule).
//! - **`debug!`** — detail useful for tracing a run's mechanics but not
//! itself a decision that changes the outcome (byte-level truncation of
//! a single tool's output, already surfaced in-band to the model).
//! - **`error!`** — reserved for regression signals: a safeguard fired
//! under conditions where it should have been structurally unreachable
//! (e.g. threshold compaction firing while the cadence compressor is
//! supposed to keep the transcript under budget at all times).
//!
//! All logs go to **stderr** via `tracing` (never stdout — see the module
//! doc above) and are filtered by `RUST_LOG`, falling back to `info` when it
//! is unset or invalid.
//!
//! **Logs vs. events:** structured [`crate::events`] are telemetry for the
//! UI (a typed, machine-consumed fact stream); `tracing` logs here are for
//! an operator reading stderr while debugging a run. Both may describe the
//! same underlying fact, but one must never substitute for the other — an
//! event emission does not excuse a silent decision point from also logging.
use EnvFilter;
/// Build the tracing filter, falling back to [`DEFAULT_LOG_LEVEL`].
///
/// Why: `EnvFilter::from_default_env` yields an EMPTY directive set when
/// `RUST_LOG` is unset or malformed, which filters everything below `error`
/// and silences the `warn!` decision points this module's convention requires.
/// What: parses `RUST_LOG`; on any parse failure builds a filter from
/// [`DEFAULT_LOG_LEVEL`] instead.
/// Test: `tests/logging_e2e.rs`.
/// Initialise the global tracing subscriber.
///
/// Why: All daemons and CLI entry points call this once at startup to ensure
/// log output is consistently routed to stderr with the `RUST_LOG` filter.
/// What: Installs a stderr-bound fmt subscriber with [`env_filter`].
/// Panics if called twice (use `init_tracing_for_test` in test binaries).
/// Test: Called in `main.rs`; correctness is verified by observing log output.
/// Initialise the global tracing subscriber, silently ignoring duplicate inits.
///
/// Why: Test binaries may link multiple test modules that all call setup code.
/// `try_init` returns an error on the second call instead of panicking, so
/// test runs remain stable regardless of execution order.
/// What: Calls `try_init()`; swallows the error if already initialised.
/// Test: `init_tracing_for_test_is_idempotent`.
/// Log level used when no `RUST_LOG` env var is set.
///
/// Why: Documents and centralises the default so operators know what to expect
/// without reading source.
/// What: A static string literal used by [`env_filter`] as its real fallback.
/// Test: `tests/logging_e2e.rs` covers unset and invalid `RUST_LOG` values.
pub const DEFAULT_LOG_LEVEL: &str = "info";
// ── Tests ─────────────────────────────────────────────────────────────────────