返回 CodeWhale
telemetry_notice.rs
根目录 / crates / engine / src / telemetry_notice.rs
1 //! Eligibility and persistence for the interactive telemetry disclosure.
2 //!
3 //! Rendering belongs to the native TUI event loop.
4 //! This module owns only the privacy-sensitive state transitions: deciding
5 //! whether disclosure is owed and applying the explicit Settings preference.
6 //! Drawing a notice never records acceptance or arms collection.
7
8 use std::io::IsTerminal;
9 use std::path::{Path, PathBuf};
10
11 use anyhow::{Result, anyhow};
12 use codewhale_config::{SetupState, TELEMETRY_NOTICE_VERSION};
13 use codewhale_telemetry::SessionSource;
14
15 use codewhale_localization::{Locale, MessageId, tr};
16
17 /// Marker for a nonblocking disclosure of default-on usage and Settings opt-out.
18 #[derive(Debug, Clone)]
19 pub struct PendingTelemetryNotice;
20
21 /// Whether an interactive launch owes the native notice, may arm immediately,
22 /// or must stay unarmed because the durable privacy state could not be read.
23 #[derive(Debug)]
24 pub enum TelemetryNoticePlan {
25 Due(PendingTelemetryNotice),
26 NotDue,
27 SuppressArming,
28 }
29
30 impl TelemetryNoticePlan {
31 pub fn should_arm_before_tui(&self) -> bool {
32 !matches!(self, Self::SuppressArming)
33 }
34
35 pub fn into_pending(self) -> Option<PendingTelemetryNotice> {
36 match self {
37 Self::Due(pending) => Some(pending),
38 Self::NotDue | Self::SuppressArming => None,
39 }
40 }
41 }
42
43 /// Typed result of changing the durable telemetry preference from `/settings`.
44 #[derive(Debug, Clone, PartialEq, Eq)]
45 pub struct AppliedTelemetryPreference {
46 outcome: TelemetryPreferenceOutcome,
47 }
48
49 #[derive(Debug, Clone, PartialEq, Eq)]
50 enum TelemetryPreferenceOutcome {
51 EnabledNextLaunch,
52 Disabled,
53 DisabledWithWarning(String),
54 DisabledForSession(String),
55 SaveFailed(String),
56 }
57
58 impl AppliedTelemetryPreference {
59 pub fn is_error(&self) -> bool {
60 matches!(
61 self.outcome,
62 TelemetryPreferenceOutcome::DisabledWithWarning(_)
63 | TelemetryPreferenceOutcome::DisabledForSession(_)
64 | TelemetryPreferenceOutcome::SaveFailed(_)
65 )
66 }
67
68 pub fn is_save_failed(&self) -> bool {
69 matches!(self.outcome, TelemetryPreferenceOutcome::SaveFailed(_))
70 }
71
72 pub fn is_persisted(&self) -> bool {
73 matches!(
74 self.outcome,
75 TelemetryPreferenceOutcome::EnabledNextLaunch
76 | TelemetryPreferenceOutcome::Disabled
77 | TelemetryPreferenceOutcome::DisabledWithWarning(_)
78 )
79 }
80
81 pub fn message(&self, locale: Locale) -> String {
82 let (id, detail) = match &self.outcome {
83 TelemetryPreferenceOutcome::EnabledNextLaunch => {
84 (MessageId::TelemetryPreferenceEnabledNextLaunch, None)
85 }
86 TelemetryPreferenceOutcome::Disabled => (MessageId::TelemetryPreferenceDisabled, None),
87 TelemetryPreferenceOutcome::DisabledWithWarning(detail) => (
88 MessageId::TelemetryPreferenceDisabledWithWarning,
89 Some(detail),
90 ),
91 TelemetryPreferenceOutcome::DisabledForSession(detail) => (
92 MessageId::TelemetryPreferenceDisabledForSession,
93 Some(detail),
94 ),
95 TelemetryPreferenceOutcome::SaveFailed(detail) => {
96 (MessageId::TelemetryPreferenceSaveFailed, Some(detail))
97 }
98 };
99 let mut message = tr(locale, id).into_owned();
100 if let Some(detail) = detail {
101 message = message.replace("{detail}", detail);
102 }
103 message
104 }
105 }
106
107 /// Return a native-notice plan when this interactive launch owes disclosure.
108 ///
109 /// This is read-only. It never prints, blocks on a line read, creates telemetry
110 /// state, or records a fictional answer. `--skip-onboarding` intentionally has
111 /// no bearing on a privacy disclosure.
112 pub fn plan_if_due(
113 config_path: Option<PathBuf>,
114 session_source: SessionSource,
115 ) -> TelemetryNoticePlan {
116 if !(std::io::stdin().is_terminal() && std::io::stdout().is_terminal()) {
117 return TelemetryNoticePlan::NotDue;
118 }
119
120 let store = match codewhale_config::ConfigStore::load(config_path) {
121 Ok(store) => store,
122 Err(error) => {
123 // A config we cannot read is a config we must not write.
124 tracing::warn!("telemetry stays unarmed; config unreadable: {error}");
125 return TelemetryNoticePlan::SuppressArming;
126 }
127 };
128 let setup_state_path = match SetupState::path() {
129 Ok(path) => path,
130 Err(error) => {
131 tracing::warn!("telemetry stays unarmed; setup-state path unavailable: {error}");
132 return TelemetryNoticePlan::SuppressArming;
133 }
134 };
135 plan_for_store_and_state(store, setup_state_path, session_source)
136 }
137
138 fn plan_for_store_and_state(
139 store: codewhale_config::ConfigStore,
140 setup_state_path: PathBuf,
141 _session_source: SessionSource,
142 ) -> TelemetryNoticePlan {
143 let resolved = store
144 .config
145 .resolve_runtime_options(&codewhale_config::CliRuntimeOverrides::default());
146 let state = match load_notice_state_at(&setup_state_path) {
147 Ok(state) => state,
148 Err(error) => {
149 // Never replace a corrupt constitution/setup sidecar with a fresh
150 // telemetry-only record. The next successful setup repair can
151 // make this notice eligible again.
152 tracing::warn!("telemetry stays unarmed; setup state unreadable: {error}");
153 return TelemetryNoticePlan::SuppressArming;
154 }
155 };
156 let gate = NoticeGate {
157 needs_notice: state.needs_telemetry_notice(TELEMETRY_NOTICE_VERSION),
158 persisted_off: resolved.telemetry_explicit_off,
159 recorded_opt_out: state.telemetry_opted_out(),
160 floor_in_force: codewhale_config::telemetry_floor_in_force(),
161 };
162 if gate.may_ask() {
163 TelemetryNoticePlan::Due(PendingTelemetryNotice)
164 } else {
165 TelemetryNoticePlan::NotDue
166 }
167 }
168
169 /// Record that the interactive disclosure was drawn for this policy version.
170 ///
171 /// Display bookkeeping only: it never touches the preference registers, and a
172 /// failed write merely repeats the notice on a later launch.
173 pub fn record_presented() {
174 let Ok(path) = SetupState::path() else {
175 return;
176 };
177 let _ = SetupState::update_at(&path, |state| {
178 state.record_telemetry_notice_shown(TELEMETRY_NOTICE_VERSION);
179 });
180 }
181
182 /// Return the saved telemetry preference shown by `/settings`.
183 ///
184 /// A missing preference defaults on. An existing
185 /// unreadable record may contain an opt-out, so the Settings row fails closed
186 /// and shows Off rather than guessing.
187 pub fn saved_preference_enabled(config: &codewhale_runtime::config::Config) -> bool {
188 let Ok(path) = SetupState::path() else {
189 return false;
190 };
191 saved_preference_enabled_at(config, &path)
192 }
193
194 fn saved_preference_enabled_at(
195 config: &codewhale_runtime::config::Config,
196 setup_state_path: &Path,
197 ) -> bool {
198 if config.telemetry == Some(false) {
199 return false;
200 }
201 match codewhale_telemetry::load_setup_state_for_decision_at(setup_state_path) {
202 // The telemetry owner returns a fresh default state for a genuinely
203 // absent sidecar. `None` therefore means an existing record was
204 // unreadable (or the path could not be inspected) and must fail closed.
205 Some(state) => !state.telemetry_opted_out(),
206 None => false,
207 }
208 }
209
210 /// Persist the `/settings` telemetry toggle through the same two privacy
211 /// registers as the CLI preference command.
212 ///
213 /// Turning off is successful when either durable register records the opt-out;
214 /// existing local telemetry is then wiped under the telemetry ordering lock.
215 /// The wipe writes its tombstone first, so a partial erase still fails closed.
216 /// Turning on is stricter: both registers must agree before the UI reports the
217 /// preference as enabled. Re-enabling takes effect for new sessions so a
218 /// process that was disabled never starts collecting again behind the user's
219 /// back.
220 pub fn apply_persistent_preference(
221 config_path: Option<PathBuf>,
222 enabled: bool,
223 ) -> AppliedTelemetryPreference {
224 let setup_state_path = match SetupState::path() {
225 Ok(path) => path,
226 Err(error) => {
227 return AppliedTelemetryPreference {
228 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(
229 error.to_string(),
230 )),
231 };
232 }
233 };
234 let telemetry_root = codewhale_config::codewhale_home()
235 .ok()
236 .map(|home| home.join(codewhale_telemetry::TELEMETRY_DIR));
237 apply_persistent_preference_at(config_path, setup_state_path, telemetry_root, enabled)
238 }
239
240 fn apply_persistent_preference_at(
241 config_path: Option<PathBuf>,
242 setup_state_path: PathBuf,
243 telemetry_root: Option<PathBuf>,
244 enabled: bool,
245 ) -> AppliedTelemetryPreference {
246 if enabled {
247 // Keep a durable Off floor in place until both privacy registers have
248 // accepted the explicit re-enable. This makes every partial failure
249 // resolve Off on the next launch.
250 match load_notice_state_at(&setup_state_path) {
251 Ok(_) => {}
252 Err(error) => {
253 return AppliedTelemetryPreference {
254 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(
255 format!("privacy record: {error}"),
256 )),
257 };
258 }
259 };
260 if let Err(error) = write_config_preference(config_path.clone(), false) {
261 return AppliedTelemetryPreference {
262 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(format!(
263 "config: {error}"
264 ))),
265 };
266 }
267 if let Err(error) = SetupState::update_at(&setup_state_path, |state| {
268 state.record_telemetry_notice(TELEMETRY_NOTICE_VERSION, true);
269 }) {
270 return AppliedTelemetryPreference {
271 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(format!(
272 "privacy record: {error}"
273 ))),
274 };
275 }
276 if let Err(error) = write_config_preference(config_path.clone(), true) {
277 let mut failures = vec![format!("config: {error}")];
278 if let Err(rollback) = write_config_preference(config_path, false) {
279 failures.push(format!("restoring the config Off floor: {rollback}"));
280 }
281 // The config Off floor is authoritative, but put the privacy
282 // sidecar back in the same fail-closed state as well. Otherwise a
283 // later manual edit could expose the partial enable as consent.
284 if let Err(rollback) = SetupState::update_at(&setup_state_path, |state| {
285 state.record_telemetry_notice(TELEMETRY_NOTICE_VERSION, false);
286 }) {
287 failures.push(format!("restoring the privacy opt-out: {rollback}"));
288 }
289 return AppliedTelemetryPreference {
290 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(
291 failures.join("; "),
292 )),
293 };
294 }
295 return AppliedTelemetryPreference {
296 outcome: TelemetryPreferenceOutcome::EnabledNextLaunch,
297 };
298 }
299
300 let config_result = write_config_preference(config_path, false);
301 let state_result = SetupState::update_at(&setup_state_path, |state| {
302 state.record_telemetry_notice(TELEMETRY_NOTICE_VERSION, false);
303 });
304 // Wipe even if both durable writes fail: a successful tombstone stops the
305 // already-armed process for the remainder of this session.
306 let wipe_result = match telemetry_root.as_deref().filter(|root| root.is_dir()) {
307 Some(root) => codewhale_telemetry::buffer::wipe(root),
308 None => Ok(()),
309 };
310 let durable = config_result.is_ok() || state_result.is_ok();
311 if !durable {
312 let mut failures = vec![
313 format!("config: {}", config_result.expect_err("failed result")),
314 format!(
315 "privacy record: {}",
316 state_result.expect_err("failed result")
317 ),
318 ];
319 if let Err(error) = wipe_result {
320 failures.push(format!("local erase: {error}"));
321 return AppliedTelemetryPreference {
322 outcome: TelemetryPreferenceOutcome::SaveFailed(bounded_failure_detail(
323 failures.join("; "),
324 )),
325 };
326 }
327 let detail = bounded_failure_detail(failures.join("; "));
328 return AppliedTelemetryPreference {
329 outcome: TelemetryPreferenceOutcome::DisabledForSession(detail),
330 };
331 }
332
333 let mut warnings = Vec::new();
334 if let Err(error) = config_result {
335 warnings.push(format!("config: {error}"));
336 }
337 if let Err(error) = state_result {
338 warnings.push(format!("privacy record: {error}"));
339 }
340 if let Err(error) = wipe_result {
341 warnings.push(format!("local erase: {error}"));
342 }
343 if warnings.is_empty() {
344 AppliedTelemetryPreference {
345 outcome: TelemetryPreferenceOutcome::Disabled,
346 }
347 } else {
348 AppliedTelemetryPreference {
349 outcome: TelemetryPreferenceOutcome::DisabledWithWarning(bounded_failure_detail(
350 warnings.join("; "),
351 )),
352 }
353 }
354 }
355
356 fn bounded_failure_detail(detail: String) -> String {
357 const MAX_CHARS: usize = 240;
358 let single_line = detail.split_whitespace().collect::<Vec<_>>().join(" ");
359 let mut chars = single_line.chars();
360 let bounded = chars.by_ref().take(MAX_CHARS).collect::<String>();
361 if chars.next().is_some() {
362 format!("{bounded}…")
363 } else {
364 bounded
365 }
366 }
367
368 fn write_config_preference(config_path: Option<PathBuf>, enabled: bool) -> Result<()> {
369 let mut store = codewhale_config::ConfigStore::load(config_path)?;
370 store
371 .config
372 .set_value("telemetry", if enabled { "true" } else { "false" })?;
373 store.save()
374 }
375
376 /// Load a missing sidecar as a fresh state, but distinguish it from an
377 /// existing unreadable/corrupt sidecar so the notice can never overwrite the
378 /// latter with defaults.
379 fn load_notice_state_at(path: &Path) -> Result<SetupState> {
380 if !path
381 .try_exists()
382 .map_err(|error| anyhow!("could not inspect {}: {error}", path.display()))?
383 {
384 return Ok(SetupState::default());
385 }
386 SetupState::load_from(path)
387 .ok_or_else(|| anyhow!("{} could not be read as setup state", path.display()))
388 }
389
390 /// Everything that decides whether the disclosure may be shown.
391 struct NoticeGate {
392 needs_notice: bool,
393 persisted_off: bool,
394 recorded_opt_out: bool,
395 floor_in_force: bool,
396 }
397
398 impl NoticeGate {
399 fn may_ask(&self) -> bool {
400 self.needs_notice && !self.persisted_off && !self.recorded_opt_out && !self.floor_in_force
401 }
402 }
403
404 #[cfg(test)]
405 mod tests {
406 use super::*;
407
408 fn gate(
409 needs_notice: bool,
410 persisted_off: bool,
411 recorded_opt_out: bool,
412 floor_in_force: bool,
413 ) -> NoticeGate {
414 NoticeGate {
415 needs_notice,
416 persisted_off,
417 recorded_opt_out,
418 floor_in_force,
419 }
420 }
421
422 #[test]
423 fn the_notice_is_not_put_to_someone_who_already_answered_durably() {
424 assert!(gate(true, false, false, false).may_ask());
425 assert!(!gate(true, true, false, false).may_ask());
426 assert!(!gate(true, false, true, false).may_ask());
427 assert!(!gate(true, false, false, true).may_ask());
428 assert!(!gate(false, false, false, false).may_ask());
429 }
430
431 #[test]
432 fn a_due_disclosure_does_not_gate_default_on_collection() {
433 let dir = tempfile::tempdir().unwrap();
434 let config_path = dir.path().join("config.toml");
435 std::fs::write(&config_path, "").unwrap();
436 let store = codewhale_config::ConfigStore::load(Some(config_path)).unwrap();
437 let plan = plan_for_store_and_state(
438 store,
439 dir.path().join("setup_state.json"),
440 SessionSource::Interactive,
441 );
442 assert!(matches!(plan, TelemetryNoticePlan::Due(_)));
443 assert!(plan.should_arm_before_tui());
444 assert!(
445 !dir.path().join("setup_state.json").exists(),
446 "planning is not presentation or acceptance"
447 );
448 }
449
450 #[test]
451 fn corrupt_setup_state_is_never_replaced_with_telemetry_defaults() {
452 let dir = tempfile::tempdir().expect("tempdir");
453 let config_path = dir.path().join("config.toml");
454 let state_path = dir.path().join("setup_state.json");
455 std::fs::write(&config_path, "").expect("seed config");
456 std::fs::write(&state_path, "not-json").expect("seed corrupt state");
457
458 assert!(load_notice_state_at(&state_path).is_err());
459 let store = codewhale_config::ConfigStore::load(Some(config_path)).expect("load config");
460 let plan = plan_for_store_and_state(store, state_path.clone(), SessionSource::Interactive);
461 assert!(matches!(&plan, TelemetryNoticePlan::SuppressArming));
462 assert!(
463 !plan.should_arm_before_tui(),
464 "unreadable privacy state must fail closed instead of arming by default"
465 );
466 assert_eq!(
467 std::fs::read_to_string(&state_path).expect("read corrupt state"),
468 "not-json"
469 );
470 }
471
472 #[test]
473 fn settings_preference_defaults_on_and_preserves_old_declines() {
474 let dir = tempfile::tempdir().expect("tempdir");
475 let state_path = dir.path().join("setup_state.json");
476 let config = {
477 let __cw_config_init_0_0 = Some(true);
478 let mut __cw_config_init_0_value: codewhale_runtime::config::Config =
479 codewhale_runtime::config::Config::default();
480 __cw_config_init_0_value.telemetry = __cw_config_init_0_0;
481 __cw_config_init_0_value
482 };
483
484 assert!(
485 saved_preference_enabled_at(&config, &state_path),
486 "a missing privacy record uses default-on"
487 );
488
489 let mut state = SetupState::default();
490 state.record_telemetry_notice("3", true);
491 state.save_to(&state_path).expect("seed old acceptance");
492 assert!(saved_preference_enabled_at(&config, &state_path));
493 state.record_telemetry_notice("4", false);
494 state.save_to(&state_path).expect("seed historical decline");
495 assert!(!saved_preference_enabled_at(&config, &state_path));
496 state.record_telemetry_notice(TELEMETRY_NOTICE_VERSION, true);
497 state.save_to(&state_path).expect("seed current acceptance");
498 assert!(saved_preference_enabled_at(&config, &state_path));
499 assert!(saved_preference_enabled_at(
500 &codewhale_runtime::config::Config::default(),
501 &state_path
502 ));
503
504 std::fs::write(&state_path, "not-json").expect("seed corrupt state");
505 assert!(
506 !saved_preference_enabled_at(&config, &state_path),
507 "an unreadable privacy record may contain an opt-out"
508 );
509
510 let explicitly_off = {
511 let __cw_config_init_1_0 = Some(false);
512 let mut __cw_config_init_1_value: codewhale_runtime::config::Config =
513 codewhale_runtime::config::Config::default();
514 __cw_config_init_1_value.telemetry = __cw_config_init_1_0;
515 __cw_config_init_1_value
516 };
517 assert!(!saved_preference_enabled_at(&explicitly_off, &state_path));
518 }
519
520 #[test]
521 fn settings_off_persists_both_registers_and_stops_the_armed_buffer() {
522 let dir = tempfile::tempdir().expect("tempdir");
523 let config_path = dir.path().join("config.toml");
524 let state_path = dir.path().join("setup_state.json");
525 let telemetry_root = dir.path().join("telemetry");
526 std::fs::write(&config_path, "telemetry = true\n").expect("seed config");
527 std::fs::create_dir_all(&telemetry_root).expect("seed telemetry root");
528 std::fs::write(
529 codewhale_telemetry::buffer::buffer_path(&telemetry_root),
530 "queued-event\n",
531 )
532 .expect("seed buffer");
533
534 let applied = apply_persistent_preference_at(
535 Some(config_path.clone()),
536 state_path.clone(),
537 Some(telemetry_root.clone()),
538 false,
539 );
540
541 assert_eq!(applied.outcome, TelemetryPreferenceOutcome::Disabled);
542 assert!(
543 std::fs::read_to_string(&config_path)
544 .expect("read config")
545 .contains("telemetry = false")
546 );
547 assert!(
548 SetupState::load_from(&state_path)
549 .expect("saved state")
550 .telemetry_opted_out()
551 );
552 assert!(codewhale_telemetry::buffer::tombstone_present(
553 &telemetry_root
554 ));
555 assert_eq!(
556 std::fs::read_to_string(codewhale_telemetry::buffer::buffer_path(&telemetry_root))
557 .expect("read wiped buffer"),
558 ""
559 );
560 }
561
562 #[test]
563 fn settings_on_is_saved_for_next_launch_without_clearing_the_tombstone() {
564 let dir = tempfile::tempdir().expect("tempdir");
565 let config_path = dir.path().join("config.toml");
566 let state_path = dir.path().join("setup_state.json");
567 let telemetry_root = dir.path().join("telemetry");
568 std::fs::write(&config_path, "telemetry = true\n").expect("seed config");
569 std::fs::create_dir_all(&telemetry_root).expect("seed telemetry root");
570 let disabled = apply_persistent_preference_at(
571 Some(config_path.clone()),
572 state_path.clone(),
573 Some(telemetry_root.clone()),
574 false,
575 );
576 assert_eq!(disabled.outcome, TelemetryPreferenceOutcome::Disabled);
577
578 let enabled = apply_persistent_preference_at(
579 Some(config_path.clone()),
580 state_path.clone(),
581 Some(telemetry_root.clone()),
582 true,
583 );
584
585 assert_eq!(
586 enabled.outcome,
587 TelemetryPreferenceOutcome::EnabledNextLaunch
588 );
589 assert!(
590 std::fs::read_to_string(&config_path)
591 .expect("read config")
592 .contains("telemetry = true")
593 );
594 assert!(
595 SetupState::load_from(&state_path)
596 .expect("saved state")
597 .telemetry_accepted(TELEMETRY_NOTICE_VERSION)
598 );
599 assert!(
600 codewhale_telemetry::buffer::tombstone_present(&telemetry_root),
601 "the already-running process stays off; the next launch clears this after a fresh permission check"
602 );
603
604 let store = codewhale_config::ConfigStore::load(Some(config_path))
605 .expect("reload enabled config for the next launch");
606 let resolved = store
607 .config
608 .resolve_runtime_options(&codewhale_config::CliRuntimeOverrides::default());
609 let reloaded_state = SetupState::load_from(&state_path).expect("reload enabled state");
610 assert!(
611 codewhale_telemetry::decide_in_home(
612 Some(dir.path()),
613 &resolved,
614 &reloaded_state,
615 codewhale_telemetry::Surface::Tui,
616 )
617 .is_enabled(),
618 "a fresh launch may clear the prior tombstone only after both saved registers resolve enabled"
619 );
620 }
621
622 #[test]
623 fn failed_settings_enable_preserves_the_existing_opt_out() {
624 let dir = tempfile::tempdir().expect("tempdir");
625 let config_path = dir.path().to_path_buf();
626 let state_path = dir.path().join("setup_state.json");
627 let mut state = SetupState::default();
628 state.record_telemetry_notice(TELEMETRY_NOTICE_VERSION, false);
629 state.save_to(&state_path).expect("seed opt-out");
630
631 let applied =
632 apply_persistent_preference_at(Some(config_path), state_path.clone(), None, true);
633
634 assert!(matches!(
635 applied.outcome,
636 TelemetryPreferenceOutcome::SaveFailed(_)
637 ));
638 assert!(
639 SetupState::load_from(&state_path)
640 .expect("saved state")
641 .telemetry_opted_out()
642 );
643 }
644
645 #[test]
646 fn unsaved_settings_off_still_suppresses_the_current_session() {
647 let dir = tempfile::tempdir().expect("tempdir");
648 let config_path = dir.path().to_path_buf();
649 let state_path = dir.path().join("setup_state.json");
650 let telemetry_root = dir.path().join("telemetry");
651 std::fs::write(&state_path, "not-json").expect("seed corrupt state");
652 std::fs::create_dir_all(&telemetry_root).expect("seed telemetry root");
653
654 let applied = apply_persistent_preference_at(
655 Some(config_path),
656 state_path,
657 Some(telemetry_root.clone()),
658 false,
659 );
660
661 assert!(matches!(
662 applied.outcome,
663 TelemetryPreferenceOutcome::DisabledForSession(_)
664 ));
665 assert!(codewhale_telemetry::buffer::tombstone_present(
666 &telemetry_root
667 ));
668 }
669
670 #[test]
671 fn a_non_tty_test_surface_cannot_schedule_the_native_notice() {
672 assert!(matches!(
673 plan_if_due(None, SessionSource::Interactive),
674 TelemetryNoticePlan::NotDue
675 ));
676 }
677 }
678
678 lines RUST