Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions bt-daemon/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bt-daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ braintrust-sdk-rust = { git = "https://github.com/braintrustdata/braintrust-sdk-
braintrust-api-client = { git = "https://github.com/braintrustdata/braintrust-sdk-rust", rev = "b29025a725125f56d97232464500f065d98dfe5d" }
base64 = "0.22"
mime_guess = "2"
notify = "8.2"
anyhow = "1"
muse-code-sdk = "=1.4.12"
parking_lot = "0.12"
Expand Down Expand Up @@ -59,8 +60,11 @@ weak-table = "0.4"
file-rotate = "0.8"
humantime = "2"

[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_System_Threading"] }
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_System_Threading", "Win32_System_Com", "Win32_UI_Shell"] }

[dev-dependencies]
bt-daemon = { path = ".", features = ["test-support"] }
Expand Down
45 changes: 37 additions & 8 deletions bt-daemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ user-level plugins, and their paths are relative to `.braintrust.json`:
bt trace install claude --scope project --plugin ./scripts/redact.mjs
```

Use `--no-inherit-plugins` to run only the project's plugins; use
`--inherit-plugins` to restore the default. A project configuration can execute
Use `--no-inherit-user-plugins` to omit the user-level plugins; use
`--inherit-user-plugins` to restore the default. The project setting is
`inherit_user_plugins`; the earlier `inherit_plugins` spelling remains supported.
If both are present, `inherit_user_plugins` takes precedence. Setup writes both
names so older installations honor the same choice. The earlier
`--no-inherit-plugins` and `--inherit-plugins` flags are also aliases.
A project configuration can execute
the JavaScript plugins it names when tracing runs, so review them before using
tracing in a repository.

Managed `bt trace run` takes precedence over both scopes. For example,
Managed `bt trace run` takes precedence over user and project settings. For example,
`bt trace run codex --inherit` starts from user and project settings before
applying invocation options; `--no-inherit-plugins` replaces the inherited
plugin chain for that invocation. `bt trace doctor <agent>` shows the current
Expand Down Expand Up @@ -133,13 +138,37 @@ destination, flush mode, and metadata. Setup saves a stable `route.auth.profile_
production resolves and refreshes them through `bt`. `bt trace run` supplies a
process-local settings overlay and never changes any of these files.

### Global span plugins

Administrators can configure global span plugins for all users, agents, projects,
and invocations in `/etc/braintrust/trace.json` (macOS and Linux) or
`%ProgramData%\Braintrust\trace.json` (Windows):

```json
{
"span_plugins": ["/opt/company/braintrust/redact.mjs"]
}
```

Paths may be absolute or relative to this file. Global plugins run first,
followed by route plugins; project and invocation options cannot remove them.
Protect the configuration and plugin files with system permissions. Configuration
changes are loaded automatically; deliveries already in progress may finish
under the previous configuration. `bt trace doctor <agent>` shows the configured
global plugins. An invalid or unreadable configuration or a plugin failure holds
delivery until it is fixed.
An absent file leaves existing behavior unchanged. This protects delivery through
Braintrust tracing, not against a user who can bypass or replace the software.
Raw events and translated operations are stored locally before plugins run.

### JavaScript span plugins

`--plugin PATH` registers a synchronous ES module that transforms each
sink-neutral span row after translation and immediately before delivery. Repeat
the flag to compose plugins from left to right. `enable` persists its ordered list
for ordinary agent sessions. Managed runs and imports are isolated from that
list and use only the `--plugin` flags passed to their command. Each path is
list and use the `--plugin` flags passed to their command as route plugins.
Global plugins still run first. Each path is
canonicalized to an absolute path before it is validated or stored.

Each module must default-export a synchronous function. It receives a span and
Expand Down Expand Up @@ -200,10 +229,10 @@ bt trace install codex --plugin ./redact.mjs --plugin ./tag-ci.mjs
merge with, plugins saved by `enable`:

```bash
# Only local.mjs runs; redact.mjs and tag-ci.mjs remain global enable behavior.
# local.mjs replaces the user-level redact.mjs and tag-ci.mjs for this run.
bt trace run --plugin ./local.mjs codex -- "summarize this change"

# Only sanitize-history.mjs transforms spans produced by this import.
# sanitize-history.mjs runs after any global plugins for this import.
bt trace import codex SESSION_ID --plugin ./sanitize-history.mjs
```

Expand All @@ -221,8 +250,8 @@ portable.
The environment map is captured from the daemon process when each worker-local
span processor is constructed. Plugins execute in bounded, thread-local
QuickJS runtimes with no filesystem or network host APIs. Modules must be
self-contained and transforms must be stateless: module globals belong to a
worker thread, not a session. Every configured plugin is mandatory. A plugin
self-contained and transforms must be stateless: each plugin has its own JavaScript
context, and module globals belong to a worker thread, not a session. Every configured plugin is mandatory. A plugin
failure holds the affected route at its earliest unresolved SpanOp; other routes
continue independently. Calls have a one-second total budget, including input
conversion, synchronous execution, and output conversion. Timeout diagnostics
Expand Down
15 changes: 15 additions & 0 deletions bt-daemon/src/command_output/doctor_command_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub struct DoctorCommandOutput {
pub source: String,
pub display_name: String,
pub settings_path: PathBuf,
pub system_settings_path: PathBuf,
pub global_span_plugins: Option<Vec<PathBuf>>,
pub settings_present: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_settings_path: Option<PathBuf>,
Expand Down Expand Up @@ -40,6 +42,19 @@ impl DoctorCommandOutput {
self.route_source,
route,
);
rendered.push_str(&format!(
"\nSystem tracing settings: {}\nGlobal span plugins (configured): {}",
self.system_settings_path.display(),
match &self.global_span_plugins {
None => "(unavailable)".into(),
Some(paths) if paths.is_empty() => "(none)".into(),
Some(paths) => paths
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", "),
}
));
if let Some(path) = &self.project_settings_path {
rendered.push_str(&format!("\nProject settings: {}", path.display()));
if !self.project_settings_fields.is_empty() {
Expand Down
2 changes: 2 additions & 0 deletions bt-daemon/src/command_output/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ fn doctor_json_is_structured_and_contains_no_credentials() {
source: "codex".into(),
display_name: "Codex".into(),
settings_path: PathBuf::from("/tmp/braintrust.json"),
system_settings_path: PathBuf::from("/etc/braintrust/trace.json"),
global_span_plugins: Some(Vec::new()),
settings_present: true,
project_settings_path: None,
project_settings_fields: Vec::new(),
Expand Down
2 changes: 2 additions & 0 deletions bt-daemon/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub(crate) struct SessionOptions {
pub config: crate::wire::SessionConfig,
pub route: crate::wire::SessionRoute,
pub data_dir: PathBuf,
pub system_policy: tokio::sync::watch::Receiver<crate::system_trace_config::Policy>,
pub translation: Arc<Translation>,
pub input: crate::pipeline::cursor::Cursor,
pub auth_provider: Option<Arc<dyn crate::AuthProvider>>,
Expand Down Expand Up @@ -120,6 +121,7 @@ impl Session {
session_id: crate::ids::native_session_id(&options.session_id).to_owned(),
version: options.bt_version,
data_dir: options.data_dir,
system_policy: options.system_policy,
config,
route: options.route,
counters: counters.clone(),
Expand Down
2 changes: 2 additions & 0 deletions bt-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ mod muse;
mod pipeline;
mod plugin_diagnostics;
pub(crate) mod process;
mod regular_file;
mod route;
mod server;
mod settings;
mod setup;
mod sink;
mod span_processor;
mod subprocess;
mod system_trace_config;
mod trace_command;
mod trace_runtime;
mod transcript_import;
Expand Down
Loading
Loading