wavekat-cli 0.0.25

Command-line client for the WaveKat platform (wk)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Crash and error reporting.
//!
//! The public API (`init`, `report_error`, `is_enabled`) is uniform
//! whether the `telemetry` cargo feature is on or off — when it's off,
//! every function is a no-op so `main.rs` doesn't have to `#[cfg]`-gate
//! call sites.
//!
//! Design rationale and scope live in
//! `docs/01-crash-and-error-reporting.md`. The short version:
//!
//! * Sentry envelope protocol via the official `sentry` crate; backend
//!   is Sentry SaaS for now (DSN baked in at build time via the
//!   `WK_SENTRY_DSN` env var). No DSN → no-op even with the feature on.
//! * Scrubbing happens in `before_send` below. It is the load-bearing
//!   privacy layer; assume the SDK will try to capture more than we
//!   want and treat scrubbing as the policy point of record.
//! * Off-switches: env var `WK_TELEMETRY=0` and persisted
//!   `auth.json:telemetry=false`. Either disables for the run.

// Several helpers are referenced only from `cfg(feature = "telemetry")`
// blocks; without the feature they're effectively dead, which is
// expected.
#![cfg_attr(not(feature = "telemetry"), allow(dead_code))]

use std::time::Duration;

use crate::config;

/// `release` value for Sentry events. Composed at compile time so a
/// stripped binary still self-identifies. See `build.rs` for `WK_GIT_SHA`.
pub const CLI_RELEASE: &str = concat!(
    env!("CARGO_PKG_NAME"),
    "@",
    env!("CARGO_PKG_VERSION"),
    "+",
    env!("WK_GIT_SHA"),
);

/// DSN baked in at build time. Optional so source builds and forks
/// without the env var still compile (and run with telemetry inert).
#[cfg(feature = "telemetry")]
const SENTRY_DSN: Option<&str> = option_env!("WK_SENTRY_DSN");

/// Returned by [`init`]; on `Drop` flushes pending events with a short
/// timeout so the process doesn't hang on exit if the network is dead.
pub struct Guard {
    #[cfg(feature = "telemetry")]
    inner: Option<sentry::ClientInitGuard>,
}

impl Drop for Guard {
    fn drop(&mut self) {
        #[cfg(feature = "telemetry")]
        {
            if let Some(g) = self.inner.take() {
                // 2s ceiling — slow enough to catch a real send on a
                // healthy network, short enough that a broken one
                // doesn't make `wk` feel hung at exit.
                let _ = g.flush(Some(Duration::from_secs(2)));
            }
        }
    }
}

/// Initialize the SDK if the feature is compiled in, a DSN is baked
/// in, and the user hasn't opted out. Otherwise returns an inert guard.
pub fn init() -> Guard {
    #[cfg(feature = "telemetry")]
    {
        if !is_enabled() {
            return Guard { inner: None };
        }
        let Some(dsn) = SENTRY_DSN else {
            return Guard { inner: None };
        };
        let Ok(parsed) = dsn.parse::<sentry::types::Dsn>() else {
            return Guard { inner: None };
        };

        let options = sentry::ClientOptions {
            dsn: Some(parsed),
            release: Some(CLI_RELEASE.into()),
            environment: Some(env_name().into()),
            // We never want the SDK guessing at PII (usernames, IPs,
            // env vars). `before_send` enforces the same thing, but
            // belt and suspenders.
            send_default_pii: false,
            attach_stacktrace: true,
            max_breadcrumbs: 50,
            sample_rate: 1.0,
            traces_sample_rate: 0.0,
            before_send: Some(std::sync::Arc::new(|event| Some(scrub_event(event)))),
            ..Default::default()
        };

        let guard = sentry::init(options);

        sentry::configure_scope(|scope| {
            scope.set_tag("client", "wavekat-cli");
            scope.set_tag("os.name", std::env::consts::OS);
            scope.set_tag("os.arch", std::env::consts::ARCH);
            if let Some(id) = config::ensure_install_id() {
                // `install_id` goes in `user.id` because Sentry uses
                // that for per-install dedup / counting, but it's not
                // a user identity — it's a random UUID generated on
                // first run. The scrubber forces username/email/ip to
                // empty so nothing else leaks here.
                scope.set_user(Some(sentry::User {
                    id: Some(id),
                    ..Default::default()
                }));
            }
        });

        Guard { inner: Some(guard) }
    }

    #[cfg(not(feature = "telemetry"))]
    {
        Guard {}
    }
}

/// Should we send anything? Consulted by `init` and again by
/// `report_error`; the env var check makes the env-var opt-out
/// effective even on already-initialized processes (unit tests, etc.).
pub fn is_enabled() -> bool {
    if !cfg!(feature = "telemetry") {
        return false;
    }
    if let Ok(v) = std::env::var("WK_TELEMETRY") {
        match v.trim().to_ascii_lowercase().as_str() {
            "" | "0" | "false" | "off" | "no" => return false,
            _ => {}
        }
    }
    let cfg = config::load_or_default();
    cfg.telemetry.unwrap_or(true)
}

/// Capture an `anyhow::Error` as a Sentry event, tagged with the
/// subcommand name and elapsed duration. No-op when disabled.
#[allow(unused_variables)]
pub fn report_error(command: &str, duration: Duration, err: &anyhow::Error) {
    #[cfg(feature = "telemetry")]
    {
        if !is_enabled() {
            return;
        }
        sentry::configure_scope(|scope| {
            scope.set_tag("cli.command", command);
            scope.set_tag("error.kind", classify_error(err));
            scope.set_extra(
                "duration_ms",
                serde_json::Value::from(duration.as_millis() as u64),
            );
        });
        sentry::integrations::anyhow::capture_anyhow(err);
    }
}

/// First-run notice — printed once on stderr, then suppressed via a
/// persisted flag. Called from `main` before dispatch; cheap.
pub fn maybe_print_first_run_notice() {
    if !cfg!(feature = "telemetry") {
        return;
    }
    let mut cfg = config::load_or_default();
    if cfg.telemetry_notice_shown {
        return;
    }
    if cfg.telemetry == Some(false) {
        // User already opted out somehow — don't pester.
        cfg.telemetry_notice_shown = true;
        let _ = config::save(&cfg);
        return;
    }
    eprintln!(
        "wk: anonymous error reports help us fix bugs faster.\n     \
         set WK_TELEMETRY=0 (or run `wk config telemetry off`) to opt out.\n     \
         what we collect: https://wavekat.com/privacy/#telemetry"
    );
    cfg.telemetry_notice_shown = true;
    let _ = config::save(&cfg);
}

fn env_name() -> &'static str {
    if cfg!(debug_assertions) {
        "dev"
    } else {
        "production"
    }
}

/// Best-effort error categorization for the `error.kind` tag. Substring
/// matching is intentionally a placeholder — the `client::ClientError`
/// refactor (next PR) will replace this with structured info propagated
/// from the failure site.
fn classify_error(err: &anyhow::Error) -> &'static str {
    let s = format!("{err:?}").to_lowercase();
    if s.contains("not signed in") || s.contains("no credentials") {
        "auth_missing"
    } else if s.contains("decoding response") {
        "decode"
    } else if s.contains("dns")
        || s.contains("tls")
        || s.contains("connect")
        || s.contains("timed out")
    {
        "network"
    } else if s.contains(" 4") && s.contains(" /api/") {
        "http_4xx"
    } else if s.contains(" 5") && s.contains(" /api/") {
        "http_5xx"
    } else {
        "local_other"
    }
}

// ---------- scrubber ----------------------------------------------------

#[cfg(feature = "telemetry")]
fn scrub_event(mut event: sentry::protocol::Event<'static>) -> sentry::protocol::Event<'static> {
    use sentry::protocol::Value;
    use std::collections::BTreeMap;

    // Drop anything that could carry user identity beyond our own
    // anonymous install_id (which we set on the user object directly
    // in `init`).
    if let Some(user) = event.user.as_mut() {
        user.username = None;
        user.email = None;
        user.ip_address = None;
        user.other = BTreeMap::new();
    }
    event.server_name = None;
    // SDK fills `dist` from the host's hostname in some integrations.
    event.dist = None;

    // Request — wipe bodies and headers, templatize URL.
    if let Some(req) = event.request.as_mut() {
        req.data = None;
        req.cookies = None;
        req.headers = BTreeMap::new();
        req.env = BTreeMap::new();
        if let Some(url) = req.url.as_mut() {
            let templated_path = templatize_path(url.path());
            url.set_path(&templated_path);
            // Wipe query (we don't currently put IDs there, but the
            // SDK might attach arbitrary state).
            url.set_query(None);
            url.set_fragment(None);
        }
        req.query_string = None;
    }

    // Message + exception values: cap and redact home paths.
    if let Some(msg) = event.message.as_mut() {
        *msg = scrub_text(msg);
    }
    for ex in event.exception.values.iter_mut() {
        if let Some(v) = ex.value.as_mut() {
            *v = scrub_text(v);
        }
    }

    // Drop any extras that smell like creds — defense in depth; we
    // shouldn't be setting these but third-party integrations might.
    event.extra.retain(|k, _| !looks_sensitive(k));

    // Same for tag values (keys are ours, values are scrubbed for
    // accidental URL/path content).
    for (_, v) in event.tags.iter_mut() {
        *v = scrub_text(v);
    }

    // Strip request/url-shaped data out of breadcrumbs (the reqwest
    // integration likes to emit these).
    for crumb in event.breadcrumbs.iter_mut() {
        if let Some(msg) = crumb.message.as_mut() {
            *msg = scrub_text(msg);
        }
        let mut clean: BTreeMap<String, Value> = BTreeMap::new();
        for (k, v) in std::mem::take(&mut crumb.data).into_iter() {
            if looks_sensitive(&k) {
                continue;
            }
            let scrubbed = match v {
                Value::String(s) => Value::String(scrub_text(&s)),
                other => other,
            };
            clean.insert(k, scrubbed);
        }
        crumb.data = clean;
    }

    event
}

/// Replace ID-shaped segments in a URL path with `:id`. Takes the
/// path only (the request scrubber strips query and fragment
/// separately) so this never has to think about URL parsing.
#[cfg(feature = "telemetry")]
fn templatize_path(path: &str) -> String {
    path.split('/')
        .map(|seg| if looks_like_id(seg) { ":id" } else { seg })
        .collect::<Vec<_>>()
        .join("/")
}

#[cfg(feature = "telemetry")]
fn looks_like_id(seg: &str) -> bool {
    // `proj_xyz`, `exp_xyz`, `model_xyz`, ... — a short lowercase
    // prefix, an underscore, then ≥3 chars of alphanumeric body.
    if let Some((prefix, body)) = seg.split_once('_') {
        if !prefix.is_empty()
            && prefix.len() <= 6
            && prefix.chars().all(|c| c.is_ascii_lowercase())
            && body.len() >= 3
            && body.chars().all(|c| c.is_ascii_alphanumeric())
        {
            return true;
        }
    }
    // UUID 8-4-4-4-12
    if seg.len() == 36 {
        let dashes: usize = seg.bytes().filter(|b| *b == b'-').count();
        if dashes == 4 && seg.chars().all(|c| c.is_ascii_hexdigit() || c == '-') {
            return true;
        }
    }
    // Long hex strings (truncated content hashes, sha256 prefixes, …).
    if seg.len() >= 16 && seg.chars().all(|c| c.is_ascii_hexdigit()) {
        return true;
    }
    false
}

#[cfg(feature = "telemetry")]
fn looks_sensitive(key: &str) -> bool {
    let k = key.to_ascii_lowercase();
    k.contains("token")
        || k.contains("cookie")
        || k.contains("authorization")
        || k.contains("bearer")
        || k.contains("password")
        || k.contains("secret")
        || k.contains("api_key")
        || k.contains("apikey")
}

#[cfg(feature = "telemetry")]
fn scrub_text(s: &str) -> String {
    let redacted = redact_home(s);
    truncate(&redacted, 200)
}

#[cfg(feature = "telemetry")]
fn redact_home(s: &str) -> String {
    // Replace any literal occurrence of the user's home directory
    // prefix with `~`. Cheap, predictable, and avoids regexes.
    if let Some(home) = dirs::home_dir() {
        let home_str = home.to_string_lossy();
        if !home_str.is_empty() && s.contains(home_str.as_ref()) {
            return s.replace(home_str.as_ref(), "~");
        }
    }
    s.to_string()
}

#[cfg(feature = "telemetry")]
fn truncate(s: &str, n: usize) -> String {
    if s.chars().count() <= n {
        return s.to_string();
    }
    let mut out: String = s.chars().take(n).collect();
    out.push('…');
    out
}

// ---------- tests -------------------------------------------------------

#[cfg(all(test, feature = "telemetry"))]
mod tests {
    use super::*;

    #[test]
    fn templatizes_proj_ids() {
        assert_eq!(
            templatize_path("/api/projects/proj_abc123/annotations"),
            "/api/projects/:id/annotations"
        );
    }

    #[test]
    fn templatizes_uuid() {
        assert_eq!(
            templatize_path("/api/exports/12345678-1234-1234-1234-1234567890ab"),
            "/api/exports/:id"
        );
    }

    #[test]
    fn templatizes_long_hex() {
        assert_eq!(
            templatize_path("/api/files/deadbeefcafebabe"),
            "/api/files/:id"
        );
    }

    #[test]
    fn keeps_non_id_segments() {
        assert_eq!(
            templatize_path("/api/auth/cli/tokens"),
            "/api/auth/cli/tokens"
        );
        // "v1" looks suspicious-ish but doesn't match any id rule.
        assert_eq!(templatize_path("/api/v1/me"), "/api/v1/me");
    }

    #[test]
    fn templatizes_multiple_segments() {
        assert_eq!(
            templatize_path("/api/projects/proj_abc/exports/exp_xyz"),
            "/api/projects/:id/exports/:id"
        );
    }

    #[test]
    fn looks_sensitive_catches_obvious() {
        assert!(looks_sensitive("Authorization"));
        assert!(looks_sensitive("cookie"));
        assert!(looks_sensitive("WK_TOKEN"));
        assert!(looks_sensitive("api_key"));
        assert!(!looks_sensitive("user_id"));
        assert!(!looks_sensitive("status"));
    }

    #[test]
    fn truncates_long_strings_with_ellipsis() {
        let s = "x".repeat(300);
        let out = truncate(&s, 200);
        // 200 chars + 1 ellipsis char.
        assert_eq!(out.chars().count(), 201);
    }

    #[test]
    fn truncate_passthrough_short() {
        assert_eq!(truncate("hello", 200), "hello");
    }

    #[test]
    fn redact_home_replaces_prefix() {
        // Only meaningful when dirs::home_dir() returns Some; on CI
        // boxes this is generally true. Falls through cleanly otherwise.
        if let Some(home) = dirs::home_dir() {
            let home_str = home.to_string_lossy().to_string();
            let example = format!("failed to write {home_str}/cache/x.parquet");
            let scrubbed = redact_home(&example);
            assert!(scrubbed.contains("~/cache/x.parquet"));
            assert!(!scrubbed.contains(&home_str));
        }
    }

    #[test]
    fn classify_recognizes_auth_missing() {
        let err = anyhow::anyhow!("not signed in — run `wk login` first");
        assert_eq!(classify_error(&err), "auth_missing");
    }

    #[test]
    fn classify_recognizes_decode() {
        let err = anyhow::anyhow!("decoding response from /api/me: {{bad}}");
        assert_eq!(classify_error(&err), "decode");
    }

    #[test]
    fn classify_falls_back_to_local_other() {
        let err = anyhow::anyhow!("something idiosyncratic");
        assert_eq!(classify_error(&err), "local_other");
    }
}