atomcode-telemetry 4.23.1

Open-source terminal AI coding agent
Documentation
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
//! Event and Envelope schema for AtomCode telemetry v2.
//!
//! The events are: open_atomcode, llm_chat, tool_call, use_command,
//! mcp_connect, login_success, take_codingplan, panic, telemetry_disabled.
//!
//! Wire format: envelope fields + event-specific payload, both flattened
//! into one JSON object. Event variant is tagged via `event_id`.

use serde::Serialize;
use uuid::Uuid;

// ---------- SessionMode ----------

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SessionMode {
    Headless,
    Tui,
    Ide,
    Vscode,
    AtomcodeAir,
}

// ---------- Envelope (common to every event) ----------

#[derive(Debug, Clone, Serialize)]
pub struct Envelope {
    pub device_id: Uuid,
    pub launch_id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub account_id: Option<String>,
    pub session_id: Uuid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub turn_id: Option<Uuid>,
    pub ts: i64,
    pub schema_version: u32,
    pub app_version: String,
    pub os: String,
    pub arch: String,
    pub locale: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    /// Vendor host (e.g. `api.openai.com`). Derived from the configured
    /// `base_url` host part — falls back to each vendor's official host
    /// when missing/unparseable. See `resolve_provider_host`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider_host: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub model: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_origin: Option<RepoOrigin>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mode: Option<SessionMode>,
}

#[derive(Debug, Clone, Serialize)]
pub struct RepoOrigin {
    pub host: RepoHost,
    pub has_git: bool,
}

#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RepoHost {
    Gitcode,
    Atomgit,
    Github,
    Gitlab,
    Other,
    None,
}

// ---------- Error kind enums ----------

/// LLM 对话错误类型
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LlmErrorKind {
    NetworkError,
    AuthError,
    RateLimited,
    ServerError,
    StreamInterrupted,
    StreamTimeout,
    ContextOverflow,
    Other,
}

/// 工具调用错误类型
#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ToolErrorKind {
    NotFound,
    InvalidArgs,
    ExecutionFailed,
    DeniedByUser,
    BlockedByHook,
    LoopDetected,
    SkillNotFound,
    SkillDisabled,
    SkillEmptyTemplate,
    /// Tool ran successfully (exit 0) but produced stderr output,
    /// suggesting a partial failure or misleading success.
    Warning,
    Other,
}

/// MCP 连接错误类型
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum McpErrorKind {
    NetworkError,
    AuthError,
    ServerError,
    ExecutionFailed,
    Timeout,
    Other,
}

/// CodingPlan 错误类型
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CodingplanErrorKind {
    AuthError,
    AuthExpired,
    ExecutionFailed,
    NetworkError,
    ServerError,
    Other,
}

/// use_command 错误类型
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum UseCommandErrorKind {
    ExecutionFailed,
    InvalidArgs,
    NotFound,
    Other,
}

/// MCP 传输方式
#[derive(Debug, Clone, Copy, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum McpTransport {
    Stdio,
    Sse,
    StreamableHttp,
}

// ---------- Event payloads ----------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum CodingplanResult {
    Success,
    Fail,
}

// ---------- The Event enum (6 variants) ----------

#[derive(Debug, Clone, Serialize)]
#[serde(tag = "event_id", rename_all = "snake_case")]
pub enum Event {
    /// Fired when AtomCode is launched (interactive CLI, oneshot, or TUI entry).
    /// Not fired for --version / --help / telemetry subcommands.
    OpenAtomcode,

    /// One LLM turn completed (success or failure).
    LlmChat {
        duration_ms: u32,
        tool_calls_count: u32,
        input_tokens: u32,
        output_tokens: u32,
        cached_tokens: u32,
        had_error: bool,
        context_window: u32,
        system_tokens: u32,
        tool_def_tokens: u32,
        tool_result_tokens: u32,
        message_tokens: u32,
        messages_count: u32,
        /// Error category (None when had_error=false or unclassifiable).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<LlmErrorKind>,
        /// Error detail JSON (None when had_error=false).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// Single tool call result (success or failure).
    ToolCall {
        /// Tool name (e.g. "bash", "edit_file", "mcp__xx__yy").
        name: String,
        /// Whether the call succeeded.
        success: bool,
        /// Execution duration in ms (0 if not executed).
        duration_ms: u32,
        /// Error category (None when success=true).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<ToolErrorKind>,
        /// Error/detail JSON (always present for diagnostics even on success).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// A slash command was executed in TUI or daemon.
    /// `type_` is the literal command name (without the leading /).
    UseCommand {
        #[serde(rename = "type")]
        type_: String,
        /// Whether the command succeeded (None for legacy events).
        #[serde(skip_serializing_if = "Option::is_none")]
        success: Option<bool>,
        /// Error category (None when success=true or legacy).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<UseCommandErrorKind>,
        /// Error/detail JSON.
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// MCP server connection attempt (success or failure).
    McpConnect {
        /// MCP server name.
        server_name: String,
        /// Transport type.
        transport: McpTransport,
        /// Whether the connection succeeded.
        success: bool,
        /// Connection duration in ms.
        #[serde(skip_serializing_if = "Option::is_none")]
        duration_ms: Option<u32>,
        /// Error category (None when success=true).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<McpErrorKind>,
        /// Error/detail JSON.
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// OAuth login completed successfully.
    LoginSuccess,

    /// A coding plan run finished.
    TakeCodingplan {
        #[serde(rename = "type")]
        type_: CodingplanResult,
        /// Error category (None when type_=Success).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<CodingplanErrorKind>,
        /// Error/detail JSON.
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// Panic captured by global hook.
    Panic {
        location: String,
        message_head: String,
        thread: String,
        backtrace_top_5: Vec<String>,
        /// Fixed to "panic".
        #[serde(skip_serializing_if = "Option::is_none")]
        error_kind: Option<String>,
        /// Runtime context JSON (session_duration_secs, turns_completed, last_tool_name, last_event).
        #[serde(skip_serializing_if = "Option::is_none")]
        error_data: Option<String>,
    },

    /// Final event before user opts out via `atomcode telemetry disable`.
    /// Only fired if telemetry was currently enabled at the time of the command.
    TelemetryDisabled,

    /// Reserved variant. Will be fired (in a future PR) when an
    /// open-source build of AtomCode attempts to send a request to the
    /// AtomGit LLM gateway. Locking the wire-format `event_id` here
    /// keeps the firing-site PR small.
    CodingplanOfficialBuildRequired,
}

// ---------- Record (wire format) ----------

#[derive(Debug, Clone, Serialize)]
pub struct Record {
    #[serde(flatten)]
    pub envelope: Envelope,
    #[serde(flatten)]
    pub event: Event,
}

// ---------- Tests ----------

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json;

    fn sample_envelope() -> Envelope {
        Envelope {
            device_id: Uuid::nil(),
            launch_id: Uuid::nil(),
            account_id: None,
            session_id: Uuid::nil(),
            turn_id: None,
            ts: 0,
            schema_version: 1,
            app_version: "0.0.0".into(),
            os: "linux".into(),
            arch: "x86_64".into(),
            locale: "en-US".into(),
            provider: None,
            provider_host: None,
            model: None,
            repo_origin: None,
            mode: None,
        }
    }

    #[test]
    fn envelope_omits_none_fields() {
        let s = serde_json::to_string(&sample_envelope()).unwrap();
        assert!(!s.contains("account_id"));
        assert!(!s.contains("turn_id"));
        assert!(!s.contains("provider"));
        assert!(!s.contains("repo_origin"));
        assert!(!s.contains("mode"));
    }

    #[test]
    fn envelope_carries_session_mode() {
        let mut env = sample_envelope();
        env.mode = Some(SessionMode::Headless);
        let v: serde_json::Value = serde_json::to_value(&env).unwrap();
        assert_eq!(v["mode"], "headless");

        env.mode = Some(SessionMode::Tui);
        let v: serde_json::Value = serde_json::to_value(&env).unwrap();
        assert_eq!(v["mode"], "tui");
    }

    #[test]
    fn record_flattens_envelope_and_event() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::OpenAtomcode,
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "open_atomcode");
        assert_eq!(v["schema_version"], 1);
        // Envelope flatten: device_id must be at the top level.
        assert!(v.get("device_id").is_some());
    }

    #[test]
    fn use_command_serializes_type_field() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::UseCommand {
                type_: "compact".into(),
                success: None,
                error_kind: None,
                error_data: None,
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "use_command");
        assert_eq!(v["type"], "compact");
        // skip_serializing_if: None fields must not appear
        assert!(v.get("success").is_none());
        assert!(v.get("error_kind").is_none());
        assert!(v.get("error_data").is_none());
    }

    #[test]
    fn use_command_with_error_fields() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::UseCommand {
                type_: "reload".into(),
                success: Some(false),
                error_kind: Some(UseCommandErrorKind::ExecutionFailed),
                error_data: Some(r#"{"command":"reload","message":"parse error"}"#.into()),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "use_command");
        assert_eq!(v["type"], "reload");
        assert_eq!(v["success"], false);
        assert_eq!(v["error_kind"], "execution_failed");
    }

    #[test]
    fn take_codingplan_serializes_success_fail() {
        let ok = Record {
            envelope: sample_envelope(),
            event: Event::TakeCodingplan {
                type_: CodingplanResult::Success,
                error_kind: None,
                error_data: None,
            },
        };
        let fail = Record {
            envelope: sample_envelope(),
            event: Event::TakeCodingplan {
                type_: CodingplanResult::Fail,
                error_kind: Some(CodingplanErrorKind::AuthError),
                error_data: Some(r#"{"step":"login"}"#.into()),
            },
        };
        let ov: serde_json::Value = serde_json::to_value(&ok).unwrap();
        let fv: serde_json::Value = serde_json::to_value(&fail).unwrap();
        assert_eq!(ov["event_id"], "take_codingplan");
        assert_eq!(ov["type"], "success");
        assert!(ov.get("error_kind").is_none());
        assert_eq!(fv["type"], "fail");
        assert_eq!(fv["error_kind"], "auth_error");
    }

    #[test]
    fn llm_chat_payload_shape() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::LlmChat {
                duration_ms: 100,
                tool_calls_count: 2,
                input_tokens: 500,
                output_tokens: 300,
                cached_tokens: 0,
                had_error: false,
                context_window: 200000,
                system_tokens: 100,
                tool_def_tokens: 200,
                tool_result_tokens: 0,
                message_tokens: 50,
                messages_count: 5,
                error_kind: None,
                error_data: None,
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "llm_chat");
        assert_eq!(v["duration_ms"], 100);
        assert_eq!(v["tool_calls_count"], 2);
        assert_eq!(v["had_error"], false);
        assert_eq!(v["context_window"], 200000);
        assert_eq!(v["system_tokens"], 100);
        assert_eq!(v["tool_def_tokens"], 200);
        assert_eq!(v["message_tokens"], 50);
        assert_eq!(v["messages_count"], 5);
        assert!(v.get("context_used").is_none());
        // skip_serializing_if: None error fields must not appear
        assert!(v.get("error_kind").is_none());
        assert!(v.get("error_data").is_none());
    }

    #[test]
    fn llm_chat_with_error() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::LlmChat {
                duration_ms: 5000,
                tool_calls_count: 0,
                input_tokens: 100,
                output_tokens: 0,
                cached_tokens: 0,
                had_error: true,
                context_window: 200000,
                system_tokens: 100,
                tool_def_tokens: 0,
                tool_result_tokens: 0,
                message_tokens: 0,
                messages_count: 1,
                error_kind: Some(LlmErrorKind::AuthError),
                error_data: Some(r#"{"status_code":401,"message":"Invalid API key"}"#.into()),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["had_error"], true);
        assert_eq!(v["error_kind"], "auth_error");
        assert!(v["error_data"].is_string());
    }

    #[test]
    fn tool_call_success_omits_error_fields() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::ToolCall {
                name: "bash".into(),
                success: true,
                duration_ms: 150,
                error_kind: None,
                error_data: None,
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "tool_call");
        assert_eq!(v["name"], "bash");
        assert_eq!(v["success"], true);
        assert_eq!(v["duration_ms"], 150);
        assert!(v.get("error_kind").is_none());
        assert!(v.get("error_data").is_none());
    }

    #[test]
    fn tool_call_with_error() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::ToolCall {
                name: "edit_file".into(),
                success: false,
                duration_ms: 5,
                error_kind: Some(ToolErrorKind::DeniedByUser),
                error_data: Some(
                    serde_json::json!({
                        "tool_name": "edit_file",
                        "reason": "User rejected file write",
                        "resolution": "Confirm the edit when prompted"
                    }).to_string(),
                ),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "tool_call");
        assert_eq!(v["name"], "edit_file");
        assert_eq!(v["success"], false);
        assert_eq!(v["error_kind"], "denied_by_user");
        assert!(v["error_data"].is_string());
        // Verify the error_data JSON contains reason + resolution
        let ed: serde_json::Value =
            serde_json::from_str(v["error_data"].as_str().unwrap()).unwrap();
        assert_eq!(ed["reason"], "User rejected file write");
        assert_eq!(ed["resolution"], "Confirm the edit when prompted");
    }

    #[test]
    fn tool_call_warning_with_stderr() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::ToolCall {
                name: "bash".into(),
                success: true,
                duration_ms: 43,
                error_kind: Some(ToolErrorKind::Warning),
                error_data: Some(
                    serde_json::json!({
                        "tool_name": "bash",
                        "duration_ms": 43,
                        "args_summary": "bash(command=rm -rf /tmp/test.txt)",
                        "output_tail": "rm: /tmp/test.txt: No such file or directory\n[elapsed: 0.0s, exit: 0]",
                        "reason": "Command succeeded (exit 0) but produced stderr output",
                        "resolution": "Review stderr for potential issues; the command may not have had the intended effect",
                    }).to_string(),
                ),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "tool_call");
        assert_eq!(v["success"], true);
        assert_eq!(v["error_kind"], "warning");
        let ed: serde_json::Value =
            serde_json::from_str(v["error_data"].as_str().unwrap()).unwrap();
        assert_eq!(ed["reason"], "Command succeeded (exit 0) but produced stderr output");
        assert_eq!(ed["resolution"], "Review stderr for potential issues; the command may not have had the intended effect");
    }

    #[test]
    fn mcp_connect_success_omits_error_fields() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::McpConnect {
                server_name: "github".into(),
                transport: McpTransport::Stdio,
                success: true,
                duration_ms: Some(320),
                error_kind: None,
                error_data: None,
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "mcp_connect");
        assert_eq!(v["server_name"], "github");
        assert_eq!(v["transport"], "stdio");
        assert_eq!(v["success"], true);
        assert_eq!(v["duration_ms"], 320);
        assert!(v.get("error_kind").is_none());
        assert!(v.get("error_data").is_none());
    }

    #[test]
    fn mcp_connect_with_error() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::McpConnect {
                server_name: "remote-api".into(),
                transport: McpTransport::Sse,
                success: false,
                duration_ms: Some(5000),
                error_kind: Some(McpErrorKind::Timeout),
                error_data: Some(
                    serde_json::json!({
                        "server_name": "remote-api",
                        "transport": "sse",
                        "reason": "Connection timed out after 5s",
                        "resolution": "Check MCP server URL and network connectivity"
                    }).to_string(),
                ),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "mcp_connect");
        assert_eq!(v["server_name"], "remote-api");
        assert_eq!(v["transport"], "sse");
        assert_eq!(v["success"], false);
        assert_eq!(v["error_kind"], "timeout");
        let ed: serde_json::Value =
            serde_json::from_str(v["error_data"].as_str().unwrap()).unwrap();
        assert_eq!(ed["reason"], "Connection timed out after 5s");
        assert_eq!(ed["resolution"], "Check MCP server URL and network connectivity");
    }

    #[test]
    fn panic_without_error_fields() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::Panic {
                location: "src/main.rs:42".into(),
                message_head: "index out of bounds".into(),
                thread: "main".into(),
                backtrace_top_5: vec!["func_a".into(), "func_b".into()],
                error_kind: None,
                error_data: None,
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "panic");
        assert_eq!(v["location"], "src/main.rs:42");
        assert_eq!(v["message_head"], "index out of bounds");
        assert!(v.get("error_kind").is_none());
        assert!(v.get("error_data").is_none());
    }

    #[test]
    fn panic_with_error_context() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::Panic {
                location: "src/agent.rs:100".into(),
                message_head: "assertion failed".into(),
                thread: "tokio-runtime".into(),
                backtrace_top_5: vec![],
                error_kind: Some("panic".into()),
                error_data: Some(
                    serde_json::json!({
                        "session_duration_secs": 300,
                        "turns_completed": 5,
                        "last_tool_name": "bash",
                        "last_event": "llm_chat"
                    }).to_string(),
                ),
            },
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "panic");
        assert_eq!(v["error_kind"], "panic");
        let ed: serde_json::Value =
            serde_json::from_str(v["error_data"].as_str().unwrap()).unwrap();
        assert_eq!(ed["session_duration_secs"], 300);
        assert_eq!(ed["turns_completed"], 5);
    }

    #[test]
    fn all_variants_have_event_id_tag() {
        let cases = [
            Event::OpenAtomcode,
            Event::LlmChat {
                duration_ms: 0,
                tool_calls_count: 0,
                input_tokens: 0,
                output_tokens: 0,
                cached_tokens: 0,
                had_error: false,
                context_window: 0,
                system_tokens: 0,
                tool_def_tokens: 0,
                tool_result_tokens: 0,
                message_tokens: 0,
                messages_count: 0,
                error_kind: None,
                error_data: None,
            },
            Event::ToolCall {
                name: "bash".into(),
                success: true,
                duration_ms: 0,
                error_kind: None,
                error_data: None,
            },
            Event::UseCommand {
                type_: "x".into(),
                success: None,
                error_kind: None,
                error_data: None,
            },
            Event::McpConnect {
                server_name: "test".into(),
                transport: McpTransport::Stdio,
                success: true,
                duration_ms: None,
                error_kind: None,
                error_data: None,
            },
            Event::LoginSuccess,
            Event::TakeCodingplan {
                type_: CodingplanResult::Success,
                error_kind: None,
                error_data: None,
            },
            Event::Panic {
                location: "x:1".into(),
                message_head: "".into(),
                thread: "main".into(),
                backtrace_top_5: vec![],
                error_kind: None,
                error_data: None,
            },
            Event::TelemetryDisabled,
        ];
        for e in &cases {
            let v = serde_json::to_value(e).unwrap();
            assert!(v.get("event_id").is_some(), "missing event_id: {:?}", e);
        }
        assert_eq!(cases.len(), 9);
    }

    #[test]
    fn telemetry_disabled_serializes_with_correct_event_id() {
        let r = Record {
            envelope: sample_envelope(),
            event: Event::TelemetryDisabled,
        };
        let v: serde_json::Value = serde_json::to_value(&r).unwrap();
        assert_eq!(v["event_id"], "telemetry_disabled");
    }

    #[test]
    fn session_mode_ide_serializes_as_ide() {
        assert_eq!(
            serde_json::to_string(&SessionMode::Ide).unwrap(),
            "\"ide\""
        );
    }
}

#[cfg(test)]
mod codingplan_required_event_tests {
    use super::*;

    #[test]
    fn codingplan_official_build_required_serialises_with_snake_case_event_id() {
        let e = Event::CodingplanOfficialBuildRequired;
        let v = serde_json::to_value(&e).expect("serialise");
        assert_eq!(
            v["event_id"], "codingplan_official_build_required",
            "got: {v}"
        );
    }
}