subagent: fix a real test race on the process-wide OTEL_RESOURCE_ATTRIBUTES env var

This commit is contained in:
damocles 2026-09-09 23:47:21 +02:00
commit d26b754701

View file

@ -443,10 +443,21 @@ mod tests {
); );
} }
// One test, not two: `subagent_otel_attrs` reads the real process-wide
// `OTEL_RESOURCE_ATTRIBUTES` env var, and cargo runs tests in parallel
// threads by default — two separate tests each mutating that global
// raced each other (and, in this container, lost to the agent's own
// real ambient value). Sequencing both assertions in one test removes
// the race instead of papering over it with a mutex.
#[test] #[test]
fn otel_attrs_appends_when_ambient_var_is_set() { fn otel_attrs_append_ambient_value_or_stand_alone() {
// SAFETY: test-only env mutation, single-threaded within this fn's // SAFETY: test-only env mutation; sequenced within this one test so
// scope (no other test in this crate touches this var — checked). // no other test's concurrent read/write of the same var can race it.
unsafe {
std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES");
}
assert_eq!(subagent_otel_attrs("batch-1"), "subagent=batch-1");
unsafe { unsafe {
std::env::set_var("OTEL_RESOURCE_ATTRIBUTES", "agent=damocles"); std::env::set_var("OTEL_RESOURCE_ATTRIBUTES", "agent=damocles");
} }
@ -459,14 +470,6 @@ mod tests {
} }
} }
#[test]
fn otel_attrs_stands_alone_when_ambient_var_is_unset() {
unsafe {
std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES");
}
assert_eq!(subagent_otel_attrs("batch-1"), "subagent=batch-1");
}
#[test] #[test]
fn build_config_only_appends_system_prompt_when_given() { fn build_config_only_appends_system_prompt_when_given() {
let with = build_config("n", None, Some("/tmp/p.md")); let with = build_config("n", None, Some("/tmp/p.md"));