tokmesh-core 0.1.5

Core library for tokmesh — session parsing, scanning, aggregation, pricing
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
//! Session parsers for different AI coding assistant formats
//!
//! Each client has its own parser that converts to a unified message format.

pub mod amp;
use std::io::Read;
use std::path::{Path, PathBuf, MAIN_SEPARATOR_STR};
pub mod antigravity;
pub mod antigravity_cli;
pub mod augment;
pub mod cherrystudio;
pub mod claudecode;
pub mod cline;
pub mod codebuddy;
pub mod codebuff;
pub mod codex;
pub mod commandcode;
pub mod copilot;
pub mod copilot_desktop;
pub mod copilot_vscode;
pub mod crush;
pub mod cursor;
pub mod devin;
pub mod droid;
pub mod dsh;
pub mod freebuff;
pub mod fx;
pub mod gemini;
pub mod gjc;
pub mod goose;
pub mod grok;
pub mod hermes;
pub mod jcode;
pub mod junie;
pub mod kilo;
pub mod kilocode;
pub mod kimchi;
pub mod kimi;
pub mod kiro;
pub mod lmstudio;
pub mod mcode;
pub mod micode;
pub mod mux;
pub mod omp;
pub mod openclaw;
pub mod opencode;
pub(crate) mod opencode_schema;
pub mod opencodereview;
pub mod pi;
pub mod prime_agent;
pub mod qwen;
pub mod reasonix;
pub mod roocode;
pub mod senpi;
pub mod synthetic;
pub(crate) mod tencent_buddy;
pub mod trae;
pub mod unsloth;
pub(crate) mod utils;
pub mod warp;
pub mod workbuddy;
pub mod zcode;
pub mod zed;

use crate::TokenBreakdown;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CostSource {
    #[default]
    Unknown,
    ProviderReported,
    Estimated,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct UnifiedMessage {
    pub client: String,
    pub model_id: String,
    pub provider_id: String,
    pub session_id: String,
    pub workspace_key: Option<String>,
    pub workspace_label: Option<String>,
    pub timestamp: i64,
    pub date: String,
    pub tokens: TokenBreakdown,
    pub cost: f64,
    #[serde(default)]
    pub cost_source: CostSource,
    #[serde(default)]
    pub duration_ms: Option<i64>,
    #[serde(default = "default_message_count")]
    pub message_count: i32,
    pub agent: Option<String>,
    pub dedup_key: Option<String>,
    /// Human-readable session title/name when the source client stores one
    /// (e.g. OpenCode's `session.title` column). `None` for clients that
    /// don't record a title; the Sessions tab falls back to showing just
    /// the session ID in that case.
    #[serde(default)]
    pub session_title: Option<String>,
    /// True if this message is the first assistant response after a user turn.
    /// Used to count user interaction turns (as opposed to API message count).
    #[serde(default)]
    pub is_turn_start: bool,
}

const fn default_message_count() -> i32 {
    1
}

pub fn normalize_agent_name(agent: &str) -> String {
    let cleaned = strip_zero_width_chars(agent);
    let trimmed = cleaned.trim();
    let stripped = strip_agent_prefix(trimmed);
    let canonical = canonicalize_agent_name(stripped);
    let agent_lower = canonical.to_lowercase();

    if agent_lower.contains("plan") {
        if agent_lower.contains("omo") || agent_lower.contains("sisyphus") {
            return "Planner-Sisyphus".to_string();
        }
        return titlecase_agent(&canonical);
    }

    if agent_lower == "omo" || agent_lower == "sisyphus" {
        return "Sisyphus".to_string();
    }

    if agent_lower == "orchestrator-sisyphus" {
        return "Atlas".to_string();
    }

    titlecase_agent(&canonical)
}

pub fn normalize_opencode_agent_name(agent: &str) -> String {
    let cleaned = strip_zero_width_chars(agent);
    let trimmed = cleaned.trim();
    let stripped = strip_agent_prefix(trimmed);
    let canonical = canonicalize_agent_name(stripped);
    let agent_lower = canonical.to_lowercase();

    if let Some(normalized) = normalize_oh_my_opencode_agent_name(&agent_lower) {
        return normalized;
    }

    normalize_agent_name(&canonical)
}

pub fn normalize_copilot_agent_name(agent: &str) -> String {
    // Hardcoded brand name for the default native agent
    if agent.eq_ignore_ascii_case("github.copilot.default") {
        return "GitHub Copilot".to_string();
    }

    // Native github.copilot.* agents: strip prefix, titlecase remainder
    const GITHUB_COPILOT_PREFIX: &str = "github.copilot.";
    if agent
        .get(..GITHUB_COPILOT_PREFIX.len())
        .is_some_and(|head| head.eq_ignore_ascii_case(GITHUB_COPILOT_PREFIX))
    {
        let remainder = &agent[GITHUB_COPILOT_PREFIX.len()..];
        let hyphenated = remainder.replace('.', "-");
        return titlecase_agent(&hyphenated);
    }

    // Plugin:team:slug format — titlecase each colon-separated part, join with ": "
    const PLUGIN_PREFIX: &str = "Plugin:";
    if agent
        .get(..PLUGIN_PREFIX.len())
        .is_some_and(|head| head.eq_ignore_ascii_case(PLUGIN_PREFIX))
    {
        let rest = &agent[PLUGIN_PREFIX.len()..];
        let parts: Vec<&str> = rest.splitn(2, ':').collect();
        if parts.len() == 2 {
            let team = titlecase_agent(parts[0]);
            let slug = titlecase_agent(parts[1]);
            return format!("{}: {}", team, slug);
        }
        return titlecase_agent(rest);
    }

    normalize_agent_name(agent)
}

fn normalize_oh_my_opencode_agent_name(agent_lower: &str) -> Option<String> {
    let normalized = match agent_lower {
        // Parenthesized format and dash format
        "sisyphus (ultraworker)"
        | "sisyphus - ultraworker"
        | "sisyphus ultraworker"
        | "sisyphus" => "Sisyphus",
        "hephaestus (deep agent)"
        | "hephaestus - deep agent"
        | "hephaestus deep agent"
        | "hephaestus" => "Hephaestus",
        "prometheus (plan builder)"
        | "prometheus - plan builder"
        | "prometheus plan builder"
        | "prometheus (planner)"
        | "prometheus" => "Prometheus",
        "atlas (plan executor)" | "atlas - plan executor" | "atlas plan executor" | "atlas" => {
            "Atlas"
        }
        "metis (plan consultant)"
        | "metis - plan consultant"
        | "metis plan consultant"
        | "metis" => "Metis",
        "momus (plan critic)"
        | "momus - plan critic"
        | "momus plan critic"
        | "momus (plan reviewer)"
        | "momus" => "Momus",
        "orchestrator-sisyphus" => "Atlas",
        "sisyphus-junior" => "Sisyphus-Junior",
        "planner-sisyphus" => "Planner-Sisyphus",
        _ => return None,
    };

    Some(normalized.to_string())
}

/// Strip zero-width Unicode characters that oh-my-openagent uses as
/// invisible sort-order prefixes (U+200B ZERO WIDTH SPACE, U+200C ZERO
/// WIDTH NON-JOINER, U+200D ZERO WIDTH JOINER, U+FEFF BOM/ZWNBSP).
fn strip_zero_width_chars(s: &str) -> String {
    if !s.contains(['\u{200B}', '\u{200C}', '\u{200D}', '\u{FEFF}']) {
        return s.to_string();
    }
    s.chars()
        .filter(|c| !matches!(c, '\u{200B}' | '\u{200C}' | '\u{200D}' | '\u{FEFF}'))
        .collect()
}

fn strip_agent_prefix(name: &str) -> &str {
    for prefix in &["astrape:", "oh-my-claudecode:", "oh-my-codex:"] {
        if name
            .get(..prefix.len())
            .is_some_and(|head| head.eq_ignore_ascii_case(prefix))
        {
            return &name[prefix.len()..];
        }
    }
    name
}

fn canonicalize_agent_name(name: &str) -> String {
    name.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn titlecase_word(word: &str) -> String {
    match word.to_lowercase().as_str() {
        "ui" => "UI".to_string(),
        "ux" => "UX".to_string(),
        "api" => "API".to_string(),
        _ => {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(c) => {
                    let upper: String = c.to_uppercase().collect();
                    upper + &chars.collect::<String>()
                }
            }
        }
    }
}

fn titlecase_agent(name: &str) -> String {
    if name.is_empty() {
        return String::new();
    }
    name.split('-')
        .flat_map(|part| part.split_whitespace())
        .map(titlecase_word)
        .collect::<Vec<_>>()
        .join(" ")
}

impl UnifiedMessage {
    pub fn new(
        client: impl Into<String>,
        model_id: impl Into<String>,
        provider_id: impl Into<String>,
        session_id: impl Into<String>,
        timestamp: i64,
        tokens: TokenBreakdown,
        cost: f64,
    ) -> Self {
        Self::new_full(
            client,
            model_id,
            provider_id,
            session_id,
            timestamp,
            tokens,
            cost,
            None,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new_with_agent(
        client: impl Into<String>,
        model_id: impl Into<String>,
        provider_id: impl Into<String>,
        session_id: impl Into<String>,
        timestamp: i64,
        tokens: TokenBreakdown,
        cost: f64,
        agent: Option<String>,
    ) -> Self {
        Self::new_full(
            client,
            model_id,
            provider_id,
            session_id,
            timestamp,
            tokens,
            cost,
            agent,
            None,
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new_with_dedup(
        client: impl Into<String>,
        model_id: impl Into<String>,
        provider_id: impl Into<String>,
        session_id: impl Into<String>,
        timestamp: i64,
        tokens: TokenBreakdown,
        cost: f64,
        dedup_key: Option<String>,
    ) -> Self {
        Self::new_full(
            client,
            model_id,
            provider_id,
            session_id,
            timestamp,
            tokens,
            cost,
            None,
            dedup_key,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn new_full(
        client: impl Into<String>,
        model_id: impl Into<String>,
        provider_id: impl Into<String>,
        session_id: impl Into<String>,
        timestamp: i64,
        tokens: TokenBreakdown,
        cost: f64,
        agent: Option<String>,
        dedup_key: Option<String>,
    ) -> Self {
        let client = client.into();
        let model_id = model_id.into();
        let date = timestamp_to_date(timestamp);
        Self {
            client,
            model_id,
            provider_id: provider_id.into(),
            session_id: session_id.into(),
            workspace_key: None,
            workspace_label: None,
            timestamp,
            date,
            tokens,
            cost,
            cost_source: CostSource::Unknown,
            duration_ms: None,
            message_count: default_message_count(),
            agent,
            dedup_key,
            session_title: None,
            is_turn_start: false,
        }
    }

    pub fn set_workspace(
        &mut self,
        workspace_key: Option<String>,
        workspace_label: Option<String>,
    ) {
        self.workspace_key = workspace_key;
        self.workspace_label = workspace_label;
    }

    pub(crate) fn refresh_derived_fields(&mut self) {
        self.date = timestamp_to_date(self.timestamp);
    }

    pub(crate) fn set_timestamp(&mut self, timestamp: i64) {
        self.timestamp = timestamp;
        self.refresh_derived_fields();
    }

    pub fn mark_provider_reported_cost(&mut self) {
        self.cost_source = CostSource::ProviderReported;
    }

    pub(crate) fn mark_estimated_cost(&mut self) {
        self.cost_source = CostSource::Estimated;
    }

    pub(crate) fn has_authoritative_cost(&self) -> bool {
        self.cost_source == CostSource::ProviderReported
    }
}

pub fn normalize_workspace_key(raw: &str) -> Option<String> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }

    let preserve_unc_prefix = trimmed.starts_with("\\\\") || trimmed.starts_with("//");
    let mut normalized = trimmed.replace('\\', "/");

    if preserve_unc_prefix {
        let body = normalized.trim_start_matches('/');
        let mut collapsed = body.to_string();
        while collapsed.contains("//") {
            collapsed = collapsed.replace("//", "/");
        }
        normalized = format!("//{}", collapsed);
    } else {
        while normalized.contains("//") {
            normalized = normalized.replace("//", "/");
        }
    }

    let minimum_len = if preserve_unc_prefix { 2 } else { 1 };
    if normalized.len() > minimum_len {
        normalized = normalized.trim_end_matches('/').to_string();
    }

    if normalized.is_empty() {
        None
    } else {
        Some(normalized)
    }
}

fn is_absolute_workspace_key(key: &str) -> bool {
    normalize_workspace_key(key)
        .is_some_and(|key| key.starts_with('/') || windows_drive_root(&key, ':', '/').is_some())
}

/// Split a Windows drive anchor off the front of `key`, returning the drive
/// letter and the remainder starting at `separator`.
///
/// Shared with the slug decoder, which sees the same anchor with both the colon
/// and the separator encoded as `-` (`C:\\Users\\me` becomes `C--Users-me`). One
/// test for what counts as a drive means a path and the slug built from it can
/// never disagree about whether they are absolute.
fn windows_drive_root(key: &str, colon: char, separator: char) -> Option<(char, &str)> {
    let drive = key.chars().next()?;
    if !drive.is_ascii_alphabetic() {
        return None;
    }
    let remaining = key[1..].strip_prefix(colon)?;
    remaining
        .starts_with(separator)
        .then_some((drive, remaining))
}

pub fn workspace_label_from_key(key: &str) -> Option<String> {
    key.rsplit('/')
        .find(|segment| !segment.is_empty())
        .map(|segment| segment.to_string())
}

/// Convert Unix milliseconds to a local YYYY-MM-DD date string.
/// Marker between a repository and the worktree checked out inside it, e.g.
/// `claude-witness ⑃ lens-backfill-findings`. Worktrees are the common case for
/// agent CLIs that isolate each task, and a repo-only label would render a dozen
/// identical rows.
pub const WORKTREE_SEPARATOR: &str = " ⑃ ";

/// Path segments that mean "everything below me is a worktree, not the repo".
const WORKTREE_MARKERS: [&str; 2] = [".claude/worktrees/", ".git/worktrees/"];

/// The same markers as they appear in a dash-encoded Claude Code slug. A deleted
/// worktree cannot be resolved against the filesystem, but the marker survives
/// verbatim in the slug, so the repo prefix is still recoverable from the string.
const ENCODED_WORKTREE_MARKERS: [&str; 2] = ["--claude-worktrees-", "--git-worktrees-"];

/// Split a dash-encoded slug at its worktree marker into (repo slug, worktree
/// name). Lets rollup and labeling keep working for worktrees whose directories
/// have since been deleted — otherwise those rows keep the raw slug forever.
fn split_encoded_worktree(key: &str) -> Option<(String, String)> {
    let (index, marker_len) = first_encoded_worktree_marker(key)?;
    let repo = &key[..index];
    // Nested worktrees name the row after the INNERMOST one, the same way the
    // path form resolves `.../worktrees/outer/.claude/worktrees/inner`.
    let mut worktree = &key[index + marker_len..];
    while let Some((inner, inner_len)) = first_encoded_worktree_marker(worktree) {
        worktree = &worktree[inner + inner_len..];
    }
    Some((repo.to_string(), worktree.to_string()))
}

/// Earliest encoded worktree marker in `key`, as `(offset, marker length)`.
///
/// Smallest offset, not first marker in the array: a nested slug carries both
/// kinds, and the repository ends at whichever one appears first in the string.
/// Occurrences that would leave an empty repo or an empty worktree name are not
/// splits at all.
fn first_encoded_worktree_marker(key: &str) -> Option<(usize, usize)> {
    ENCODED_WORKTREE_MARKERS
        .iter()
        .filter_map(|marker| key.find(marker).map(|index| (index, marker.len())))
        .filter(|(index, marker_len)| *index > 0 && index + marker_len < key.len())
        .min()
}

/// The repository root a workspace key belongs to, with any worktree suffix
/// removed. Returns `None` when the key is not inside a worktree, so callers can
/// tell "already a repo root" from "rolled up to one".
///
/// Only path-shaped keys are handled: clients that store an opaque id (Warp's
/// workspace UUID) have nothing to roll up and are returned untouched.
///
/// Nested worktrees resolve to the outermost repository, because the first
/// marker in the path is the one the repo owns.
pub fn workspace_repo_root(key: &str) -> Option<String> {
    let key = normalize_workspace_key(key)?;
    // Smallest offset, not first marker in the array: a path can contain both
    // kinds (`/a/.git/worktrees/x/.claude/worktrees/y`), and iterating the array
    // would answer with whichever marker happens to be listed first rather than
    // with the outermost one. That named `/a/.git/worktrees/x` as the repo, so
    // `--merge-worktrees` gave one repository two rows.
    let index = WORKTREE_MARKERS
        .iter()
        .filter_map(|marker| find_segment_marker(&key, marker))
        .filter(|index| !key[..*index].trim_end_matches('/').is_empty())
        .min()?;
    Some(key[..index].trim_end_matches('/').to_string())
}

/// Locate `marker` where it starts a path segment.
///
/// Substring matching is wrong here: a plain directory named `my.git` makes
/// `/notes/my.git/worktrees/draft` contain `.git/worktrees/` even though nothing
/// in it is a repository, and stripping there would roll the row up under a
/// `/notes/my` that does not exist.
fn find_segment_marker(key: &str, marker: &str) -> Option<usize> {
    let mut from = 0;
    while let Some(offset) = key[from..].find(marker) {
        let index = from + offset;
        if index == 0 || key.as_bytes()[index - 1] == b'/' {
            return Some(index);
        }
        from = index + 1;
    }
    None
}

/// The repo root of a worktree checked out *beside* its repository.
///
/// `git worktree add ../feature-x` leaves nothing in the worktree's own path to
/// key on — the only link is a `.git` FILE holding
/// `gitdir: /path/to/repo/.git/worktrees/feature-x`. A normal checkout has a
/// `.git` DIRECTORY there, so the read simply fails and this returns `None`;
/// a submodule points at `.git/modules/...`, which carries no worktree marker
/// and is likewise rejected.
pub fn workspace_git_worktree_root(path: &str) -> Option<String> {
    // Only an absolute key names a directory on its own. Claude Code's slug is
    // relative, so `join` resolved it against the process working directory and
    // read `$CWD/<slug>/.git` — a file the user never named, planted by whoever
    // owns the directory the binary was started in, yet trusted to rename the
    // row and, under `--merge-worktrees`, to re-key it.
    //
    // Both spellings have to be absolute, because the test and the read see
    // different strings: `is_absolute_workspace_key` normalizes separators, so a
    // Windows-shaped key passes it on any host, while the read below hands the
    // RAW key to `Path`. On POSIX `Path::new(r"C:\Users\me\repo")` is one
    // relative filename, so that key resolved against `$CWD` again — the same
    // hole the slug closed, entered through a different door.
    if !is_absolute_workspace_key(path) || !Path::new(path).is_absolute() {
        return None;
    }
    let contents = read_git_pointer_file(&Path::new(path).join(".git"))?;
    let pointer = contents
        .lines()
        .find_map(|line| line.trim().strip_prefix("gitdir:"))?
        .trim();
    // Git may record the pointer relative to the worktree directory.
    let joined = if Path::new(pointer).is_absolute() || pointer.starts_with('/') {
        normalize_workspace_key(pointer)?
    } else {
        normalize_workspace_key(&Path::new(path).join(pointer).to_string_lossy())?
    };
    repo_root_from_gitdir(&lexically_normalize(&joined)?)
}

/// Largest `.git` pointer file worth reading.
///
/// Git writes a single short `gitdir: <path>` line. Anything past this is not a
/// pointer file, and reading it whole would let an unrelated directory dictate
/// how much memory a report allocates.
const GIT_POINTER_MAX_BYTES: u64 = 64 * 1024;

/// Read a `.git` pointer file, refusing anything that is not an ordinary file of
/// plausible size.
///
/// This runs once per distinct workspace key on every report and every TUI
/// refresh, against directories the user never named — the workspace key comes
/// from whatever a client recorded. A bare `read_to_string` there is a liability:
/// `open` on a FIFO blocks until someone writes to the other end, so a `.git`
/// FIFO wedges the whole report forever, and a 20MB regular file is read in full
/// on every refresh. `symlink_metadata` first so a `.git` symlink is resolved
/// deliberately rather than followed blind, and the resolved target is checked
/// again: only a regular file within the cap is ever opened.
fn read_git_pointer_file(path: &Path) -> Option<String> {
    let metadata = std::fs::symlink_metadata(path).ok()?;
    let metadata = if metadata.is_symlink() {
        std::fs::metadata(path).ok()?
    } else {
        metadata
    };
    if !metadata.is_file() || metadata.len() > GIT_POINTER_MAX_BYTES {
        return None;
    }

    // Bounded read rather than `read_to_string`: `stat` and `open` are two
    // syscalls, and the file can grow between them.
    let mut contents = String::new();
    std::fs::File::open(path)
        .ok()?
        .take(GIT_POINTER_MAX_BYTES)
        .read_to_string(&mut contents)
        .ok()?;
    Some(contents)
}

/// Resolve `.` and `..` segments without touching the filesystem.
///
/// A relative pointer joins into `/work/feature-x/../api/.git/worktrees/x`,
/// which names the right directory but is not the string the repository's own
/// row is keyed by. Rollup compares identities as strings, so leaving the `..`
/// in would keep the worktree and its repo in separate rows — the exact thing
/// reading the pointer is meant to fix. Lexical rather than `canonicalize` so a
/// symlinked path keeps the spelling its own row uses.
fn lexically_normalize(key: &str) -> Option<String> {
    let prefix_len = if key.starts_with("//") {
        2
    } else if key.starts_with('/') {
        1
    } else {
        0
    };
    let (prefix, rest) = key.split_at(prefix_len);
    let mut segments: Vec<&str> = Vec::new();
    for segment in rest.split('/') {
        match segment {
            "" | "." => {}
            ".." => {
                // Above an absolute root there is nothing to pop, and POSIX
                // defines `/..` as `/`; a relative key has to keep the segment.
                if segments.last().is_some_and(|last| *last != "..") {
                    segments.pop();
                } else if prefix.is_empty() {
                    segments.push("..");
                }
            }
            other => segments.push(other),
        }
    }
    normalize_workspace_key(&format!("{prefix}{}", segments.join("/")))
}

/// The repository a `gitdir:` pointer belongs to.
///
/// Git always writes `<git dir>/worktrees/<name>`, so the segment before
/// `worktrees` is the repository's git directory: `<repo>/.git` for an ordinary
/// checkout, and the repository itself (`/srv/repo.git`) when it is bare. Bare
/// layouts have to be handled here rather than by `workspace_repo_root`, which
/// deliberately refuses to read `.git/worktrees/` out of a `repo.git` segment —
/// from a bare path alone there is nothing to distinguish a repository from a
/// directory that merely ends in `.git`. Arriving through a `.git` pointer file
/// is what makes it certain.
fn repo_root_from_gitdir(gitdir: &str) -> Option<String> {
    let (git_dir, worktree) = gitdir.rsplit_once("/worktrees/")?;
    if worktree.is_empty() {
        return None;
    }
    let root = git_dir.strip_suffix("/.git").unwrap_or(git_dir);
    (!root.is_empty()).then(|| root.to_string())
}

/// The repository root for `path` whether its worktree lives inside the repo
/// (a path prefix) or beside it (a `gitdir:` pointer file).
pub fn workspace_repo_root_resolved(path: &str) -> Option<String> {
    workspace_repo_root(path).or_else(|| workspace_git_worktree_root(path))
}

/// The real filesystem path a workspace key names: the key itself when it is
/// already a path, or the directory a Claude Code slug was built from.
///
/// `None` for keys that are not paths at all (Warp's workspace UUID) and for
/// slugs whose directory is gone — which is exactly when there is no parent
/// segment available to disambiguate a colliding label with.
pub fn workspace_path_for_key(key: &str) -> Option<String> {
    workspace_path_for_decoded_key(key, decode_claude_project_slug(key).as_deref())
}

/// [`workspace_path_for_key`] with the slug decode already done.
///
/// Decoding walks the filesystem, and a caller that needs the label, the path
/// and the repo root for one key would otherwise pay for that walk three times.
pub fn workspace_path_for_decoded_key(key: &str, decoded: Option<&str>) -> Option<String> {
    if let Some(decoded) = decoded {
        return Some(decoded.to_string());
    }
    let normalized = normalize_workspace_key(key)?;
    normalized.contains('/').then_some(normalized)
}

/// Human-readable label for a workspace key: `repo` or `repo ⑃ worktree`.
///
/// The key is whatever the originating client wrote to disk, so this also has to
/// cope with Claude Code's dash-mangled directory slug
/// (`-Users-zetian-devpro-ing-claude-witness`), which carries no `/` to split on
/// and therefore used to render as the entire path — the exact prefix every row
/// shares, so truncation dropped the only distinguishing part.
pub fn workspace_display_label(key: &str) -> Option<String> {
    workspace_display_label_for_decoded_key(key, decode_claude_project_slug(key).as_deref())
}

/// [`workspace_display_label`] with the slug decode already done. See
/// [`workspace_path_for_decoded_key`] for why the decode is hoisted out.
pub fn workspace_display_label_for_decoded_key(key: &str, decoded: Option<&str>) -> Option<String> {
    // Normalize before splitting: a client that recorded a raw Windows path
    // (`C:\a\repo`) carries no `/` to split on, so without this the label
    // would be the whole path — the same unreadable row this function exists to
    // prevent, on the one platform the slug decoder cannot help with either.
    let path = decoded
        .map(str::to_string)
        .or_else(|| normalize_workspace_key(key))
        .unwrap_or_else(|| key.to_string());

    if let Some(root) = workspace_repo_root_resolved(&path) {
        let repo = workspace_label_from_key(&root)?;
        return match workspace_label_from_key(&path) {
            Some(worktree) => Some(format!("{repo}{WORKTREE_SEPARATOR}{worktree}")),
            None => Some(repo),
        };
    }

    // Undecodable slug (the directory was deleted): the marker still tells us
    // where the repo ends, so name it from the string rather than giving up and
    // showing the whole mangled path.
    if let Some((repo_slug, worktree)) = split_encoded_worktree(&path) {
        let repo = decode_claude_project_slug(&repo_slug)
            .and_then(|decoded| workspace_label_from_key(&decoded))
            .or_else(|| last_slug_segment(&repo_slug))?;
        return Some(format!("{repo}{WORKTREE_SEPARATOR}{worktree}"));
    }

    workspace_label_from_key(&path)
}

/// Repo identity for a dash-encoded worktree slug whose directory no longer
/// exists, so rollup can still merge it into its repository. Prefers the repo's
/// real path when THAT still resolves, falling back to the repo slug itself —
/// which keeps deleted worktrees of one repo together even then.
pub fn workspace_repo_root_from_slug(key: &str) -> Option<String> {
    let (repo_slug, _) = split_encoded_worktree(key)?;
    Some(decode_claude_project_slug(&repo_slug).unwrap_or(repo_slug))
}

/// Best-effort trailing name of a dash-encoded slug whose directory is gone. The
/// original `/` boundaries are unrecoverable, so this returns the last dash
/// segment — a hint, not an exact path.
fn last_slug_segment(slug: &str) -> Option<String> {
    slug.rsplit('-')
        .find(|segment| !segment.is_empty())
        .map(|segment| segment.to_string())
}

/// Claude Code names each project directory after the absolute path it was
/// launched from, replacing every non-alphanumeric byte with `-`. That map is
/// lossy — `/`, `.`, `+` and `-` all collapse to `-` — so it cannot be inverted
/// by string surgery alone. Instead this walks the filesystem, re-applying the
/// same map to real directory names to find which one the slug came from, which
/// makes the recovered path exact rather than a guess.
///
/// Returns `None` for keys that are already real paths, when no directory on
/// disk matches (a project whose folder has since been deleted or renamed), and
/// when more than one does (`a.b` beside `a-b`): the answer is adopted as a
/// grouping identity, so a guess would move usage between projects.
pub fn decode_claude_project_slug(key: &str) -> Option<String> {
    // A real path (already usable) keeps its separators; `normalize_workspace_key`
    // rewrites Windows backslashes to `/`, so one check covers both platforms.
    if key.contains('/') {
        return None;
    }

    let (root, remaining) = slug_root_and_remainder(key)?;
    let mut budget = SLUG_DECODE_STEP_BUDGET;
    match resolve_slug_under(&root, remaining, &mut budget) {
        SlugResolution::Resolved(path) => Some(path),
        SlugResolution::DeadEnd | SlugResolution::Refused => None,
    }
}

/// Filesystem probes one slug decode may spend before it gives up.
///
/// `resolve_slug_under` backtracks, so its search tree grows with the number of
/// dashes a slug can split on: an adversarial 69-character key laid out over a
/// few symlinked directories took 7.3s for a single label, and the labeler asked
/// for it once per method. A real slug resolves in roughly one probe per path
/// segment, so this is orders of magnitude above any honest decode while still
/// making the worst case finite. Exceeding it costs a prettier label, never
/// correctness: the caller falls back to naming the row from the slug string.
const SLUG_DECODE_STEP_BUDGET: u32 = 4_096;

/// Charge `cost` probes against `budget`, reporting whether it could be paid.
fn spend_slug_budget(budget: &mut u32, cost: u32) -> bool {
    match budget.checked_sub(cost) {
        Some(remaining) => {
            *budget = remaining;
            true
        }
        None => {
            *budget = 0;
            false
        }
    }
}

/// Split a slug into the filesystem root it was anchored at and the rest.
///
/// A POSIX slug begins at `/`, so it opens with the separator-turned-dash. A
/// Windows slug encodes the drive instead (`C:\Users\me` becomes `C--Users-me`:
/// one dash for the colon, one for the separator), so the root has to be
/// reconstructed from the drive letter rather than assumed to be `/`.
fn slug_root_and_remainder(key: &str) -> Option<(PathBuf, &str)> {
    if key.starts_with('-') {
        // Pass the whole key through: `slug_matches_prefix` expects every segment
        // to arrive separator-first, including the first one.
        return Some((PathBuf::from(MAIN_SEPARATOR_STR), key));
    }

    // Both the colon and the separator arrive encoded as `-`; the remainder
    // keeps its leading dash so `slug_matches_prefix` still sees every segment
    // separator-first.
    let (drive, remaining) = windows_drive_root(key, '-', '-')?;
    Some((
        PathBuf::from(format!("{drive}:{MAIN_SEPARATOR_STR}")),
        remaining,
    ))
}

/// What walking one slug remainder under one directory found.
///
/// `DeadEnd` and `Refused` are kept apart on purpose. A parent that hears
/// `DeadEnd` from a child keeps trying that child's siblings; `Refused` means
/// the slug already fits more than one directory somewhere below (or the walk
/// ran out of budget before that could be ruled out), and no sibling can make
/// the answer unique again. When both were `None` a tie found one level down
/// read as a dead end, and the parent handed back whichever other sibling
/// completed — a third directory that happened to fit became the grouping
/// identity for usage from all three.
#[derive(Debug, PartialEq, Eq)]
enum SlugResolution {
    /// Exactly one directory under this node completes the slug.
    Resolved(String),
    /// Nothing under this node completes the slug.
    DeadEnd,
    /// More than one directory completes the slug, or the walk could not
    /// finish checking, so no result from this subtree can be shown unique.
    Refused,
}

/// Walk `remaining` against the real directories under `dir`.
///
/// A dash in the slug is ambiguous — it may be a `/` boundary, or part of a
/// directory name that genuinely contains `-`, `.` or `+` — so a single greedy
/// pass mis-resolves paths like `claude-witness` (one directory, not two). This
/// consumes one real directory at a time, backtracks when a branch dead-ends,
/// and refuses when two branches complete at any depth of the walk, which
/// makes every result it does give exact.
fn resolve_slug_under(dir: &Path, remaining: &str, budget: &mut u32) -> SlugResolution {
    if remaining.is_empty() {
        // Hand back a normalized key, not a native path. Every consumer compares
        // against forward-slash markers (`workspace_repo_root` looks for
        // `.claude/worktrees/`), and on Windows `Path::join` produces `\`, so
        // returning the native spelling would silently defeat worktree rollup and
        // make a decoded key unequal to the same directory recorded by a client
        // that stores a real path.
        return match normalize_workspace_key(&dir.to_string_lossy()) {
            Some(path) => SlugResolution::Resolved(path),
            None => SlugResolution::DeadEnd,
        };
    }

    // One probe for the directory listing this node is about to make.
    if !spend_slug_budget(budget, 1) {
        return SlugResolution::Refused;
    }

    // A directory this process cannot list is a dead end, not a refusal:
    // nothing under it was shown to fit the slug.
    let Ok(entries) = std::fs::read_dir(dir) else {
        return SlugResolution::DeadEnd;
    };
    let matched: Vec<String> = entries
        .flatten()
        .map(|entry| entry.file_name().to_string_lossy().to_string())
        // Name filter first: it is pure string work, and it rejects nearly every
        // entry in a large directory. The `is_dir` check below costs a `stat` per
        // survivor, so ordering it second keeps the syscalls proportional to
        // matches rather than to directory size.
        .filter(|name| slug_matches_prefix(remaining, name))
        .collect();
    // The `stat` per survivor is the other unbounded cost here, so charge for it
    // before paying it.
    if !spend_slug_budget(budget, matched.len() as u32) {
        return SlugResolution::Refused;
    }

    // Longest candidate first: `IngTian.github.io` is the likelier match when a
    // shorter `IngTian` also exists, so the common case completes before the
    // budget can run out on the long shot.
    let mut candidates: Vec<String> = matched
        .into_iter()
        // `Path::is_dir` follows symlinks where `DirEntry::file_type` would not.
        // Symlinked directories are load-bearing here: macOS reaches temp dirs
        // through `/var -> /private/var`, and users symlink project roots.
        .filter(|name| dir.join(name).is_dir())
        .collect();
    // Order deterministically instead of trusting readdir order, so a budget
    // that runs out refuses the same slug on every run.
    candidates.sort_by(|a, b| b.len().cmp(&a.len()).then_with(|| a.cmp(b)));

    // Every candidate is walked, not just the first that completes. `a.b` and
    // `a-b` encode identically, and when both exist and both complete the slug
    // nothing on disk says which one Claude Code was launched from. The decoded
    // path becomes the grouping identity under worktree rollup, so answering
    // with either would book that usage against a directory the slug may never
    // have named. A tie here is still fine when only one branch completes.
    let mut resolved: Option<String> = None;
    for name in candidates {
        let consumed = slugify_path_segment(&name).len() + 1;
        match resolve_slug_under(&dir.join(&name), &remaining[consumed..], budget) {
            SlugResolution::Resolved(path) => {
                if resolved.is_some() {
                    return SlugResolution::Refused;
                }
                resolved = Some(path);
            }
            // A refusal below is final no matter where it came from. A tie under
            // this child already fits the slug to two directories, and the
            // siblings still unwalked can only add fits, never remove them; a
            // budget that ran dry under it left those siblings unchecked. Either
            // way nothing this level finds afterwards can be shown to be unique,
            // so it must not fall through to be walked as if the child had
            // merely dead-ended.
            SlugResolution::Refused => return SlugResolution::Refused,
            SlugResolution::DeadEnd => {}
        }
    }

    match resolved {
        Some(path) => SlugResolution::Resolved(path),
        None => SlugResolution::DeadEnd,
    }
}

/// Whether `remaining` starts with `-` + the encoded form of `name`, ending on a
/// segment boundary so a directory cannot match half of a longer name.
fn slug_matches_prefix(remaining: &str, name: &str) -> bool {
    let encoded = slugify_path_segment(name);
    let Some(rest) = remaining.strip_prefix('-') else {
        return false;
    };
    let Some(tail) = rest.strip_prefix(encoded.as_str()) else {
        return false;
    };
    tail.is_empty() || tail.starts_with('-')
}

/// Claude Code's forward map: every non-alphanumeric byte becomes `-`.
fn slugify_path_segment(name: &str) -> String {
    name.chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
        .collect()
}

/// A temp directory whose path is spelled the way `read_dir` reports it.
///
/// The slug decoder matches against real directory entries, so the fixture path
/// has to agree with what the OS enumerates. On Windows `%TEMP%` is often an 8.3
/// short name (`RUNNER~1`) while `read_dir` yields the long name
/// (`runneradmin`), and `canonicalize` returns a `\\?\` verbatim prefix that is
/// not a walkable root — so strip that and let the walk start at the drive.
/// Without this the slug describes a path no directory listing contains and the
/// decode correctly finds nothing.
///
/// Lives outside `mod tests` so the decoder tests here and the aggregation tests
/// in `crate::lib` share one copy: two copies means a verbatim-prefix fix lands
/// in one of them and the other keeps failing on Windows only.
#[cfg(test)]
pub(crate) fn canonical_tempdir() -> (tempfile::TempDir, PathBuf) {
    let temp = tempfile::tempdir().unwrap();
    let canonical = std::fs::canonicalize(temp.path()).unwrap();
    let spelled = canonical.to_string_lossy().to_string();
    let stripped = spelled
        .strip_prefix(r"\\?\")
        .map(PathBuf::from)
        .unwrap_or(canonical);
    (temp, stripped)
}

/// Convert Unix milliseconds to a local YYYY-MM-DD date string.
fn timestamp_to_date(timestamp_ms: i64) -> String {
    // Prefer the process-wide pinned bucketing timezone when set (tokens.ci
    // travel-proof submit); otherwise fall back to the machine local zone.
    crate::bucket_tz::bucket_timezone().date_of_ms(timestamp_ms)
}

fn timestamp_to_date_with_timezone<Tz>(timestamp_ms: i64, timezone: &Tz) -> String
where
    Tz: chrono::TimeZone,
    Tz::Offset: std::fmt::Display,
{
    match timezone.timestamp_millis_opt(timestamp_ms) {
        chrono::LocalResult::Single(dt) => dt.format("%Y-%m-%d").to_string(),
        _ => String::new(),
    }
}

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

    #[test]
    fn warp_cache_parser_preserves_requests_and_spend_without_tokens() {
        let file = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(
            file.path(),
            r#"{
  "version": 1,
  "syncedAt": "2026-05-29T12:00:00Z",
  "usage": {
    "requestsUsed": 42,
    "requestLimit": 100,
    "spendCents": 1234,
    "nextRefreshTime": "2026-06-01T00:00:00Z"
  },
  "workspaces": [
    {
      "id": "workspace-1",
      "name": "Personal",
      "requestsUsed": 12,
      "spendCents": 345
    }
  ]
}"#,
        )
        .unwrap();

        let messages = crate::sessions::warp::parse_warp_file(file.path());
        assert_eq!(messages.len(), 1);

        let workspace = messages
            .iter()
            .find(|message| message.session_id == "warp-aggregate-workspace-1")
            .unwrap();
        assert_eq!(workspace.client, "warp");
        assert_eq!(workspace.model_id, "aggregate-requests");
        assert_eq!(workspace.provider_id, "warp");
        assert_eq!(workspace.workspace_label.as_deref(), Some("Personal"));
        assert_eq!(workspace.message_count, 12);
        assert_eq!(workspace.tokens, TokenBreakdown::default());
        assert!((workspace.cost - 3.45).abs() < 1e-9);

        std::fs::write(
            file.path(),
            r#"{
  "version": 1,
  "syncedAt": "2026-05-29T12:00:00Z",
  "usage": {
    "requestsUsed": 42,
    "requestLimit": 100,
    "spendCents": 1234,
    "nextRefreshTime": "2026-06-01T00:00:00Z"
  },
  "workspaces": []
}"#,
        )
        .unwrap();

        let messages = crate::sessions::warp::parse_warp_file(file.path());
        assert_eq!(messages.len(), 1);
        let account = &messages[0];
        assert_eq!(account.session_id, "warp-aggregate-account");
        assert_eq!(account.message_count, 42);
        assert_eq!(account.tokens, TokenBreakdown::default());
        assert!((account.cost - 12.34).abs() < 1e-9);
    }

    #[test]
    fn test_timestamp_to_date_with_positive_offset() {
        let kst = FixedOffset::east_opt(9 * 60 * 60).unwrap();
        let ts = 1772512200000_i64; // 2026-03-03T04:30:00Z
        let date = timestamp_to_date_with_timezone(ts, &kst);
        assert_eq!(date, "2026-03-03");
    }

    #[test]
    fn test_timestamp_to_date_with_negative_offset() {
        let pst = FixedOffset::west_opt(8 * 60 * 60).unwrap();
        let ts = 1772512200000_i64; // 2026-03-03T04:30:00Z
        let date = timestamp_to_date_with_timezone(ts, &pst);
        assert_eq!(date, "2026-03-02");
    }

    #[test]
    fn test_timestamp_to_date_invalid_timestamp() {
        let utc = FixedOffset::east_opt(0).unwrap();
        let date = timestamp_to_date_with_timezone(i64::MAX, &utc);
        assert_eq!(date, "");
    }

    #[test]
    fn test_unified_message_creation() {
        let tokens = TokenBreakdown {
            input: 100,
            output: 50,
            cache_read: 0,
            cache_write: 0,
            reasoning: 0,
        };

        let msg = UnifiedMessage::new(
            "opencode",
            "claude-3-5-sonnet",
            "anthropic",
            "test-session-id",
            1733011200000,
            tokens,
            0.05,
        );

        assert_eq!(msg.client, "opencode");
        assert_eq!(msg.model_id, "claude-3-5-sonnet");
        assert_eq!(msg.session_id, "test-session-id");
        assert_eq!(msg.date, timestamp_to_date(1733011200000));
        assert_eq!(msg.cost, 0.05);
        assert_eq!(msg.agent, None);
        assert_eq!(msg.workspace_key, None);
        assert_eq!(msg.workspace_label, None);
    }

    #[test]
    fn test_normalize_workspace_key_normalizes_slashes_and_trailing_separator() {
        assert_eq!(
            normalize_workspace_key(r"C:\Users\alice\repo\"),
            Some("C:/Users/alice/repo".to_string())
        );
        assert_eq!(
            normalize_workspace_key("/Users/alice//repo/"),
            Some("/Users/alice/repo".to_string())
        );
    }

    #[test]
    fn test_normalize_workspace_key_preserves_unc_prefix() {
        assert_eq!(
            normalize_workspace_key(r"\\server\share\repo\"),
            Some("//server/share/repo".to_string())
        );
        assert_eq!(
            normalize_workspace_key("//server//share///repo/"),
            Some("//server/share/repo".to_string())
        );
    }

    #[test]
    fn test_workspace_label_from_key_uses_last_path_segment() {
        assert_eq!(
            workspace_label_from_key("/Users/alice/my-repo"),
            Some("my-repo".to_string())
        );
        assert_eq!(
            workspace_label_from_key("encoded-project-key"),
            Some("encoded-project-key".to_string())
        );
    }

    #[test]
    fn test_normalize_agent_name() {
        assert_eq!(normalize_agent_name("OmO"), "Sisyphus");
        assert_eq!(normalize_agent_name("Sisyphus"), "Sisyphus");
        assert_eq!(normalize_agent_name("omo"), "Sisyphus");
        assert_eq!(normalize_agent_name("sisyphus"), "Sisyphus");
        assert_eq!(
            normalize_agent_name("Sisyphus (Ultraworker)"),
            "Sisyphus (Ultraworker)"
        );

        assert_eq!(
            normalize_opencode_agent_name("Sisyphus (Ultraworker)"),
            "Sisyphus"
        );
        assert_eq!(normalize_opencode_agent_name("hephaestus"), "Hephaestus");
        assert_eq!(normalize_opencode_agent_name("prometheus"), "Prometheus");
        assert_eq!(normalize_opencode_agent_name("atlas"), "Atlas");
        assert_eq!(normalize_opencode_agent_name("metis"), "Metis");
        assert_eq!(normalize_opencode_agent_name("momus"), "Momus");
        assert_eq!(
            normalize_opencode_agent_name("sisyphus-junior"),
            "Sisyphus-Junior"
        );
        assert_eq!(
            normalize_opencode_agent_name("planner-sisyphus"),
            "Planner-Sisyphus"
        );

        assert_eq!(
            normalize_opencode_agent_name("Hephaestus (Deep Agent)"),
            "Hephaestus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Prometheus (Plan Builder)"),
            "Prometheus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Prometheus (Planner)"),
            "Prometheus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Atlas (Plan Executor)"),
            "Atlas"
        );
        assert_eq!(
            normalize_opencode_agent_name("Metis (Plan Consultant)"),
            "Metis"
        );
        assert_eq!(
            normalize_opencode_agent_name("Momus (Plan Critic)"),
            "Momus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Momus (Plan Reviewer)"),
            "Momus"
        );

        assert_eq!(normalize_agent_name("OmO-Plan"), "Planner-Sisyphus");
        assert_eq!(normalize_agent_name("Planner-Sisyphus"), "Planner-Sisyphus");
        assert_eq!(normalize_agent_name("omo-plan"), "Planner-Sisyphus");

        assert_eq!(normalize_agent_name("orchestrator-sisyphus"), "Atlas");
        assert_eq!(
            normalize_opencode_agent_name("orchestrator-sisyphus"),
            "Atlas"
        );
        assert_eq!(normalize_agent_name("explore"), "Explore");
        assert_eq!(normalize_agent_name("CustomAgent"), "CustomAgent");

        assert_eq!(normalize_agent_name("executor"), "Executor");
        assert_eq!(
            normalize_agent_name("task-orchestrator"),
            "Task Orchestrator"
        );
        assert_eq!(normalize_agent_name("git-committer"), "Git Committer");
        assert_eq!(
            normalize_agent_name("frontend-ui-ux-engineer"),
            "Frontend UI UX Engineer"
        );
        assert_eq!(
            normalize_agent_name("astrape:executor-high"),
            "Executor High"
        );
        assert_eq!(
            normalize_agent_name("oh-my-claudecode:code-reviewer"),
            "Code Reviewer"
        );
    }

    #[test]
    fn test_normalize_copilot_agent_name() {
        assert_eq!(
            normalize_copilot_agent_name("github.copilot.default"),
            "GitHub Copilot"
        );
        assert_eq!(
            normalize_copilot_agent_name("GITHUB.COPILOT.DEFAULT"),
            "GitHub Copilot"
        );
        assert_eq!(normalize_copilot_agent_name("github.copilot.chat"), "Chat");
        assert_eq!(
            normalize_copilot_agent_name("Plugin:software-engineering-team:se-ux-ui-designer"),
            "Software Engineering Team: Se UX UI Designer"
        );
        assert_eq!(
            normalize_copilot_agent_name("plugin:my-team:my-agent"),
            "My Team: My Agent"
        );
        assert_eq!(
            normalize_copilot_agent_name("Plugin:code-review-team:api-reviewer"),
            "Code Review Team: API Reviewer"
        );
        assert_eq!(
            normalize_copilot_agent_name("some-custom-agent"),
            "Some Custom Agent"
        );
        assert_eq!(normalize_agent_name("oh-my-codex:librarian"), "Librarian");
        assert_eq!(normalize_agent_name("astrape:executor"), "Executor");
        assert_eq!(normalize_agent_name("plan-reviewer"), "Plan Reviewer");
        assert_eq!(normalize_agent_name("astrape:planner"), "Planner");

        assert_eq!(
            normalize_opencode_agent_name("astrape:sisyphus"),
            "Sisyphus"
        );
        assert_eq!(
            normalize_opencode_agent_name("oh-my-claudecode:executor"),
            "Executor"
        );

        // New dash format (oh-my-openagent current)
        assert_eq!(
            normalize_opencode_agent_name("Sisyphus - Ultraworker"),
            "Sisyphus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Hephaestus - Deep Agent"),
            "Hephaestus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Prometheus - Plan Builder"),
            "Prometheus"
        );
        assert_eq!(
            normalize_opencode_agent_name("Atlas - Plan Executor"),
            "Atlas"
        );
        assert_eq!(
            normalize_opencode_agent_name("Metis - Plan Consultant"),
            "Metis"
        );
        assert_eq!(
            normalize_opencode_agent_name("Momus - Plan Critic"),
            "Momus"
        );

        // ZWSP-prefixed names (oh-my-openagent sort-order prefixes)
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}Sisyphus - Ultraworker"),
            "Sisyphus"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}\u{200B}\u{200B}Prometheus - Plan Builder"),
            "Prometheus"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}\u{200B}\u{200B}\u{200B}Atlas - Plan Executor"),
            "Atlas"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{FEFF}Momus - Plan Critic"),
            "Momus"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}sisyphus-junior"),
            "Sisyphus-Junior"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}sisyphus"),
            "Sisyphus"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}  Sisyphus   -   Ultraworker  "),
            "Sisyphus"
        );
        assert_eq!(
            normalize_opencode_agent_name("\u{200B}\u{200B}\u{200B}   Prometheus    Plan Builder"),
            "Prometheus"
        );
    }

    #[test]
    fn test_strip_zero_width_chars() {
        assert_eq!(strip_zero_width_chars("hello"), "hello");
        assert_eq!(strip_zero_width_chars("\u{200B}hello"), "hello");
        assert_eq!(
            strip_zero_width_chars("\u{200B}\u{200B}\u{200B}hello"),
            "hello"
        );
        assert_eq!(strip_zero_width_chars("\u{FEFF}hello"), "hello");
        assert_eq!(strip_zero_width_chars("\u{200C}hello\u{200D}"), "hello");
        assert_eq!(strip_zero_width_chars(""), "");
        assert_eq!(
            strip_zero_width_chars("no special chars"),
            "no special chars"
        );
    }
}