difflore_core/cloud/capture.rs
1//! Telemetry capture kill-switch.
2//!
3//! `DIFFLORE_CAPTURE=false` stops local fire-and-forget rows from
4//! entering any cloud outbox. Every capture-emitting entry point checks
5//! this gate before opening pools or building payloads.
6//!
7//! Both cloud queues must honor the gate:
8//! - [`crate::cloud::outbox::OutboxQueue::enqueue`] — the original
9//! `cloud_outbox` SQLite queue.
10//! - [`crate::cloud::observations::storage::ObservationEmitter::enqueue`]
11//! — the second `observation_events` queue added for PostToolUse
12//! observation capture.
13//!
14//! When disabled, no row enters either queue and later drain passes have
15//! nothing to upload.
16
17/// Env var documented as the telemetry capture kill-switch.
18pub const DIFFLORE_CAPTURE_ENV: &str = "DIFFLORE_CAPTURE";
19
20/// Whether telemetry capture is enabled.
21///
22/// Only the exact lowercase string `"false"` disables capture; unset
23/// and every other value leave it enabled.
24#[must_use]
25pub fn capture_enabled() -> bool {
26 std::env::var(DIFFLORE_CAPTURE_ENV).as_deref() != Ok("false")
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn capture_enabled_returns_true_when_unset() {
35 temp_env::with_var(DIFFLORE_CAPTURE_ENV, None::<&str>, || {
36 assert!(capture_enabled(), "unset env must leave capture enabled");
37 });
38 }
39
40 #[test]
41 fn capture_enabled_returns_true_when_set_to_other_values() {
42 // A typo must not silently disable capture.
43 for value in ["true", "1", "", "FALSE", "False", "no", "off", " false"] {
44 temp_env::with_var(DIFFLORE_CAPTURE_ENV, Some(value), || {
45 assert!(
46 capture_enabled(),
47 "value {value:?} must not disable capture (only exact lowercase \"false\" does)",
48 );
49 });
50 }
51 }
52
53 #[test]
54 fn capture_enabled_returns_false_only_for_exact_lowercase_false() {
55 temp_env::with_var(DIFFLORE_CAPTURE_ENV, Some("false"), || {
56 assert!(
57 !capture_enabled(),
58 "exact lowercase \"false\" must disable capture",
59 );
60 });
61 }
62}