codex-threadripper 0.1.7

Human-first CLI that keeps Codex thread history aligned to one provider bucket.
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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
use anyhow::Context;
use anyhow::Result;
use clap::Arg;
use clap::ArgAction;
use clap::CommandFactory;
use clap::FromArgMatches;
use clap::Parser;
use clap::Subcommand;
use notify::Config as NotifyConfig;
use notify::EventKind;
use notify::RecommendedWatcher;
use notify::RecursiveMode;
use notify::Watcher;
use rusqlite::Connection;
use rusqlite::TransactionBehavior;
use rusqlite::backup::Backup;
use serde::Deserialize;
use std::fmt::Write as _;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command as ProcessCommand;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::mpsc;
use std::time::Duration;
use std::time::Instant;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;

const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
const DEFAULT_PROVIDER: &str = "openai";
const DEFAULT_POLL_INTERVAL_MS: u64 = 500;
const SERVICE_LABEL: &str = "dev.wangnov.codex-threadripper";

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Locale {
    En,
    ZhHans,
}

#[derive(Parser, Debug)]
#[command(
    author,
    version,
    about = "placeholder",
    long_about = "placeholder",
    disable_help_flag = true,
    disable_version_flag = true,
    disable_help_subcommand = true
)]
struct Cli {
    #[arg(long, global = true, value_name = "DIR", help = "placeholder")]
    codex_home: Option<PathBuf>,

    #[arg(long, global = true, value_name = "PROVIDER", help = "placeholder")]
    provider: Option<String>,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand, Debug)]
enum Command {
    /// placeholder
    Status,
    /// placeholder
    Sync,
    /// placeholder
    #[command(alias = "daemon")]
    Watch {
        #[arg(
            long,
            default_value_t = DEFAULT_POLL_INTERVAL_MS,
            value_name = "MILLISECONDS",
            help = "placeholder"
        )]
        poll_interval_ms: u64,
    },
    /// placeholder
    PrintPlist {
        #[arg(
            long,
            default_value_t = DEFAULT_POLL_INTERVAL_MS,
            value_name = "MILLISECONDS",
            help = "placeholder"
        )]
        poll_interval_ms: u64,
    },
    /// placeholder
    InstallLaunchd {
        #[arg(
            long,
            default_value_t = DEFAULT_POLL_INTERVAL_MS,
            value_name = "MILLISECONDS",
            help = "placeholder"
        )]
        poll_interval_ms: u64,
    },
    /// placeholder
    UninstallLaunchd,
}

#[derive(Debug, Deserialize)]
struct ConfigToml {
    model_provider: Option<String>,
}

#[derive(Debug)]
struct ReconcileSummary {
    provider: String,
    changed_rows: u64,
    total_rows: u64,
    elapsed: Duration,
    backup_path: Option<PathBuf>,
}

#[derive(Debug)]
struct StatusSummary {
    codex_home: PathBuf,
    sqlite_path: PathBuf,
    config_path: PathBuf,
    provider: String,
    total_rows: u64,
    mismatched_rows: u64,
    distribution: Vec<(String, u64)>,
    launchd_plist_path: PathBuf,
    launchd_installed: bool,
    launchd_loaded: bool,
}

fn main() -> Result<()> {
    let locale = detect_locale();
    let cli = parse_cli(locale)?;
    let codex_home = cli.codex_home.unwrap_or_else(default_codex_home);

    match cli.command {
        Command::Status => {
            let summary = collect_status(&codex_home, cli.provider.as_deref())?;
            print_status(locale, &summary);
        }
        Command::Sync => {
            let summary = reconcile_once_with_backup(&codex_home, cli.provider.as_deref())?;
            print_sync_summary(locale, sync_complete_title(locale), &summary);
        }
        Command::Watch { poll_interval_ms } => {
            run_watch(
                locale,
                &codex_home,
                cli.provider.clone(),
                Duration::from_millis(poll_interval_ms),
            )?;
        }
        Command::PrintPlist { poll_interval_ms } => {
            let exe_path = std::env::current_exe().context(current_exe_error(locale))?;
            let plist = build_launchd_plist(
                exe_path.as_path(),
                &codex_home,
                cli.provider.as_deref(),
                Duration::from_millis(poll_interval_ms),
            );
            println!("{plist}");
        }
        Command::InstallLaunchd { poll_interval_ms } => {
            install_launchd(
                locale,
                &codex_home,
                cli.provider.as_deref(),
                Duration::from_millis(poll_interval_ms),
            )?;
        }
        Command::UninstallLaunchd => {
            uninstall_launchd(locale, &codex_home)?;
        }
    }

    Ok(())
}

fn parse_cli(locale: Locale) -> Result<Cli> {
    let command = localized_command(locale);
    let matches = command.clone().get_matches();
    Cli::from_arg_matches(&matches).map_err(|err| anyhow::anyhow!(err.to_string()))
}

fn localized_command(locale: Locale) -> clap::Command {
    let mut command = Cli::command();
    command = command
        .version(APP_VERSION)
        .disable_help_flag(true)
        .disable_version_flag(true)
        .disable_help_subcommand(true)
        .about(root_about(locale))
        .long_about(root_long_about(locale))
        .help_template(help_template(locale))
        .subcommand_help_heading(commands_heading(locale))
        .arg(
            Arg::new("help")
                .short('h')
                .long("help")
                .global(true)
                .action(ArgAction::Help)
                .help(help_option_help(locale))
                .help_heading(options_heading(locale)),
        )
        .arg(
            Arg::new("version")
                .short('V')
                .long("version")
                .global(true)
                .action(ArgAction::Version)
                .help(version_option_help(locale))
                .help_heading(options_heading(locale)),
        );
    command = command.mut_arg("codex_home", |arg| {
        arg.help(codex_home_help(locale))
            .help_heading(options_heading(locale))
            .value_name(dir_value_name(locale))
    });
    command = command.mut_arg("provider", |arg| {
        arg.help(provider_help(locale))
            .help_heading(options_heading(locale))
            .value_name(provider_value_name(locale))
    });

    command = command.mut_subcommand("status", |sub| {
        sub.about(status_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
    });
    command = command.mut_subcommand("sync", |sub| {
        sub.about(sync_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
    });
    command = command.mut_subcommand("watch", |sub| {
        sub.about(watch_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
            .mut_arg("poll_interval_ms", |arg| {
                arg.help(poll_interval_help(locale))
                    .help_heading(options_heading(locale))
                    .value_name(milliseconds_value_name(locale))
            })
    });
    command = command.mut_subcommand("print-plist", |sub| {
        sub.about(print_plist_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
            .mut_arg("poll_interval_ms", |arg| {
                arg.help(launchd_poll_help(locale))
                    .help_heading(options_heading(locale))
                    .value_name(milliseconds_value_name(locale))
            })
    });
    command = command.mut_subcommand("install-launchd", |sub| {
        sub.about(install_launchd_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
            .mut_arg("poll_interval_ms", |arg| {
                arg.help(launchd_poll_help(locale))
                    .help_heading(options_heading(locale))
                    .value_name(milliseconds_value_name(locale))
            })
    });
    command = command.mut_subcommand("uninstall-launchd", |sub| {
        sub.about(uninstall_launchd_about(locale))
            .version(APP_VERSION)
            .disable_help_flag(true)
            .disable_version_flag(true)
            .disable_help_subcommand(true)
            .help_template(help_template(locale))
    });

    command
}

fn detect_locale() -> Locale {
    let mut candidates = Vec::new();
    if let Some(value) = std::env::var("CODEX_THREADRIPPER_LANG").ok() {
        candidates.push(value);
    }
    for key in ["LC_ALL", "LC_MESSAGES", "LANG"] {
        if let Some(value) = std::env::var(key).ok() {
            candidates.push(value);
        }
    }

    detect_locale_from_sources(
        candidates.iter().map(String::as_str),
        apple_languages_output().as_deref(),
    )
}

fn detect_locale_from_sources<'a, I>(candidates: I, apple_languages: Option<&str>) -> Locale
where
    I: IntoIterator<Item = &'a str>,
{
    for candidate in candidates {
        if let Some(locale) = parse_locale_tag(candidate) {
            return locale;
        }
    }
    if let Some(output) = apple_languages
        && let Some(locale) = parse_apple_languages(output)
    {
        return locale;
    }
    Locale::En
}

fn parse_locale_tag(input: &str) -> Option<Locale> {
    let normalized = input.trim().to_ascii_lowercase();
    if normalized.is_empty() {
        return None;
    }
    if normalized.starts_with("zh") {
        return Some(Locale::ZhHans);
    }
    if normalized.starts_with("en") {
        return Some(Locale::En);
    }
    None
}

fn parse_apple_languages(output: &str) -> Option<Locale> {
    output
        .split(|ch: char| !(ch.is_ascii_alphanumeric() || ch == '-' || ch == '_'))
        .find_map(parse_locale_tag)
}

fn apple_languages_output() -> Option<String> {
    if !cfg!(target_os = "macos") {
        return None;
    }
    let output = ProcessCommand::new("defaults")
        .args(["read", "-g", "AppleLanguages"])
        .output()
        .ok()?;
    if !output.status.success() {
        return None;
    }
    String::from_utf8(output.stdout).ok()
}

fn default_codex_home() -> PathBuf {
    std::env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("."))
        .join(".codex")
}

fn run_watch(
    locale: Locale,
    codex_home: &Path,
    provider_override: Option<String>,
    poll_interval: Duration,
) -> Result<()> {
    let config_path = codex_home.join("config.toml");
    let shutdown = Arc::new(AtomicBool::new(false));
    let shutdown_for_handler = Arc::clone(&shutdown);
    ctrlc::set_handler(move || {
        shutdown_for_handler.store(true, Ordering::Relaxed);
    })?;

    let (tx, rx) = mpsc::channel();
    let mut watcher = RecommendedWatcher::new(
        move |event| {
            let _ = tx.send(event);
        },
        NotifyConfig::default(),
    )?;

    if let Some(parent) = config_path.parent()
        && parent.exists()
    {
        watcher.watch(parent, RecursiveMode::NonRecursive)?;
    }

    let initial = reconcile_once(codex_home, provider_override.as_deref())?;
    print_sync_summary(locale, watch_started_title(locale), &initial);
    println!(
        "{}",
        watch_running_message(locale, codex_home, poll_interval)
    );

    let mut last_provider = initial.provider.clone();
    let mut next_poll_deadline = Instant::now() + poll_interval;

    while !shutdown.load(Ordering::Relaxed) {
        let timeout = next_poll_deadline.saturating_duration_since(Instant::now());
        match rx.recv_timeout(timeout) {
            Ok(Ok(event)) => {
                if touches_config_file(&event, &config_path) {
                    let summary = reconcile_once(codex_home, provider_override.as_deref())?;
                    if summary.provider != last_provider || summary.changed_rows > 0 {
                        print_sync_summary(locale, config_change_title(locale), &summary);
                    }
                    last_provider = summary.provider;
                    next_poll_deadline = Instant::now() + poll_interval;
                }
            }
            Ok(Err(err)) => {
                eprintln!("{}", watcher_error_message(locale, err));
            }
            Err(mpsc::RecvTimeoutError::Timeout) => {
                let summary = reconcile_once(codex_home, provider_override.as_deref())?;
                if summary.provider != last_provider || summary.changed_rows > 0 {
                    print_sync_summary(locale, background_reconcile_title(locale), &summary);
                }
                last_provider = summary.provider;
                next_poll_deadline = Instant::now() + poll_interval;
            }
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                anyhow::bail!(watcher_disconnected_error(locale));
            }
        }
    }

    println!("{}", watch_stopped_message(locale));
    Ok(())
}

fn touches_config_file(event: &notify::Event, config_path: &Path) -> bool {
    matches!(
        event.kind,
        EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
    ) && event.paths.iter().any(|path| path == config_path)
}

fn collect_status(codex_home: &Path, provider_override: Option<&str>) -> Result<StatusSummary> {
    let config_path = codex_home.join("config.toml");
    let sqlite_path = codex_home.join("state_5.sqlite");
    let provider = match provider_override {
        Some(provider) => provider.to_string(),
        None => read_provider_from_config(codex_home)?,
    };
    let (total_rows, mismatched_rows, distribution) =
        inspect_sqlite_distribution(&sqlite_path, provider.as_str())?;
    let launchd_plist_path = launchd_plist_path()?;
    let launchd_installed = launchd_plist_path.exists();
    let launchd_loaded = if launchd_installed {
        launchd_service_loaded()?
    } else {
        false
    };

    Ok(StatusSummary {
        codex_home: codex_home.to_path_buf(),
        sqlite_path,
        config_path,
        provider,
        total_rows,
        mismatched_rows,
        distribution,
        launchd_plist_path,
        launchd_installed,
        launchd_loaded,
    })
}

fn reconcile_once(codex_home: &Path, provider_override: Option<&str>) -> Result<ReconcileSummary> {
    let provider = match provider_override {
        Some(provider) => provider.to_string(),
        None => read_provider_from_config(codex_home)?,
    };
    let sqlite_path = codex_home.join("state_5.sqlite");
    let started = Instant::now();
    let (changed_rows, total_rows) = reconcile_sqlite_in_place(&sqlite_path, provider.as_str())?;

    Ok(ReconcileSummary {
        provider,
        changed_rows,
        total_rows,
        elapsed: started.elapsed(),
        backup_path: None,
    })
}

fn reconcile_once_with_backup(
    codex_home: &Path,
    provider_override: Option<&str>,
) -> Result<ReconcileSummary> {
    let provider = match provider_override {
        Some(provider) => provider.to_string(),
        None => read_provider_from_config(codex_home)?,
    };
    let sqlite_path = codex_home.join("state_5.sqlite");
    let started = Instant::now();
    let (changed_rows, total_rows, backup_path) =
        reconcile_sqlite_with_backup(&sqlite_path, provider.as_str())?;

    Ok(ReconcileSummary {
        provider,
        changed_rows,
        total_rows,
        elapsed: started.elapsed(),
        backup_path: Some(backup_path),
    })
}

fn read_provider_from_config(codex_home: &Path) -> Result<String> {
    let config_path = codex_home.join("config.toml");
    if !config_path.exists() {
        return Ok(DEFAULT_PROVIDER.to_string());
    }

    let raw = std::fs::read_to_string(&config_path)
        .with_context(|| format!("failed to read {}", config_path.display()))?;
    let parsed: ConfigToml = toml::from_str(&raw)
        .with_context(|| format!("failed to parse {}", config_path.display()))?;
    Ok(parsed
        .model_provider
        .unwrap_or_else(|| DEFAULT_PROVIDER.to_string()))
}

fn inspect_sqlite_distribution(
    sqlite_path: &Path,
    target_provider: &str,
) -> Result<(u64, u64, Vec<(String, u64)>)> {
    let connection = Connection::open(sqlite_path)
        .with_context(|| format!("failed to open {}", sqlite_path.display()))?;
    let total_rows: u64 =
        connection.query_row("SELECT COUNT(*) FROM threads", [], |row| row.get(0))?;
    let mismatched_rows: u64 = connection.query_row(
        "SELECT COUNT(*) FROM threads WHERE model_provider <> ?1",
        [target_provider],
        |row| row.get(0),
    )?;
    let mut statement = connection.prepare(
        "SELECT model_provider, COUNT(*) AS row_count FROM threads GROUP BY model_provider ORDER BY row_count DESC, model_provider ASC",
    )?;
    let distribution = statement
        .query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, u64>(1)?))
        })?
        .collect::<rusqlite::Result<Vec<_>>>()?;

    Ok((total_rows, mismatched_rows, distribution))
}

fn reconcile_sqlite_in_place(sqlite_path: &Path, provider: &str) -> Result<(u64, u64)> {
    let mut connection = Connection::open(sqlite_path)
        .with_context(|| format!("failed to open {}", sqlite_path.display()))?;
    connection.busy_timeout(Duration::from_secs(5))?;

    let total_rows: u64 =
        connection.query_row("SELECT COUNT(*) FROM threads", [], |row| row.get(0))?;
    let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
    let changed_rows = transaction.execute(
        "UPDATE threads SET model_provider = ?1 WHERE model_provider <> ?1",
        [provider],
    )? as u64;
    transaction.commit()?;

    Ok((changed_rows, total_rows))
}

fn reconcile_sqlite_with_backup(sqlite_path: &Path, provider: &str) -> Result<(u64, u64, PathBuf)> {
    let backups_dir = sqlite_path
        .parent()
        .unwrap_or_else(|| Path::new("."))
        .join("backups");
    std::fs::create_dir_all(&backups_dir)
        .with_context(|| format!("failed to create {}", backups_dir.display()))?;

    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system clock is earlier than UNIX_EPOCH")?
        .as_millis();
    let backup_name = format!("state_5.sqlite.{timestamp}.bak");
    let backup_path = backups_dir.join(&backup_name);
    let backup_temp_path = backups_dir.join(format!("{backup_name}.tmp"));

    if backup_temp_path.exists() {
        std::fs::remove_file(&backup_temp_path)
            .with_context(|| format!("failed to remove {}", backup_temp_path.display()))?;
    }

    create_sqlite_backup(sqlite_path, &backup_temp_path)?;
    std::fs::rename(&backup_temp_path, &backup_path)
        .with_context(|| format!("failed to finalize {}", backup_path.display()))?;

    let (changed_rows, total_rows) = reconcile_sqlite_in_place(sqlite_path, provider)?;
    Ok((changed_rows, total_rows, backup_path))
}

fn create_sqlite_backup(sqlite_path: &Path, backup_path: &Path) -> Result<()> {
    let source = Connection::open(sqlite_path)
        .with_context(|| format!("failed to open {}", sqlite_path.display()))?;
    source.busy_timeout(Duration::from_secs(5))?;
    let mut destination = Connection::open(backup_path)
        .with_context(|| format!("failed to open {}", backup_path.display()))?;
    let backup = Backup::new(&source, &mut destination)?;
    backup.step(-1)?;
    Ok(())
}

fn install_launchd(
    locale: Locale,
    codex_home: &Path,
    provider_override: Option<&str>,
    poll_interval: Duration,
) -> Result<()> {
    let plist_path = launchd_plist_path()?;
    let launch_agents_dir = plist_path
        .parent()
        .with_context(|| format!("launchd plist path has no parent: {}", plist_path.display()))?;
    std::fs::create_dir_all(launch_agents_dir)?;

    let exe_path = std::env::current_exe().context(current_exe_error(locale))?;
    let plist = build_launchd_plist(
        exe_path.as_path(),
        codex_home,
        provider_override,
        poll_interval,
    );
    std::fs::write(&plist_path, plist)
        .with_context(|| format!("failed to write {}", plist_path.display()))?;

    let domain = launchctl_domain()?;
    let plist_path_str = plist_path.to_string_lossy().to_string();
    let _ = run_launchctl(["bootout", domain.as_str(), plist_path_str.as_str()]);
    run_launchctl(["bootstrap", domain.as_str(), plist_path_str.as_str()])?;
    let service_target = launchctl_service_target()?;
    run_launchctl(["kickstart", "-k", service_target.as_str()])?;

    println!("{}", install_launchd_done(locale));
    println!("{}", launchd_label_message(locale, SERVICE_LABEL));
    println!("{}", launchd_plist_message(locale, &plist_path));
    println!("{}", launchd_codex_home_message(locale, codex_home));
    println!("{}", launchd_polling_message(locale, poll_interval));
    println!();
    println!("{}", next_steps_heading(locale));
    for line in install_next_steps(locale, exe_path.as_path(), codex_home)? {
        println!("{line}");
    }
    Ok(())
}

fn uninstall_launchd(locale: Locale, codex_home: &Path) -> Result<()> {
    let plist_path = launchd_plist_path()?;
    if plist_path.exists() {
        let domain = launchctl_domain()?;
        let plist_path_str = plist_path.to_string_lossy().to_string();
        let _ = run_launchctl(["bootout", domain.as_str(), plist_path_str.as_str()]);
        std::fs::remove_file(&plist_path)
            .with_context(|| format!("failed to remove {}", plist_path.display()))?;
        println!("{}", uninstall_launchd_done(locale));
        println!("{}", launchd_plist_message(locale, &plist_path));
        println!();
        println!("{}", next_steps_heading(locale));
        println!(
            "{}",
            run_status_next_step(
                locale,
                &cli_status_command(std::env::current_exe()?, codex_home)
            )
        );
    } else {
        println!("{}", no_launchd_plist_message(locale, &plist_path));
    }
    Ok(())
}

fn build_launchd_plist(
    exe_path: &Path,
    codex_home: &Path,
    provider_override: Option<&str>,
    poll_interval: Duration,
) -> String {
    let logs_dir = logs_dir().unwrap_or_else(|_| default_logs_dir());
    let stdout_path = logs_dir.join("codex-threadripper.log");
    let stderr_path = logs_dir.join("codex-threadripper.error.log");

    let mut arguments = vec![
        xml_escape(exe_path.to_string_lossy().as_ref()),
        "--codex-home".to_string(),
        xml_escape(codex_home.to_string_lossy().as_ref()),
    ];
    if let Some(provider) = provider_override {
        arguments.push("--provider".to_string());
        arguments.push(xml_escape(provider));
    }
    arguments.push("watch".to_string());
    arguments.push("--poll-interval-ms".to_string());
    arguments.push(poll_interval.as_millis().to_string());

    let mut plist = String::new();
    let _ = write!(
        plist,
        r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>{SERVICE_LABEL}</string>
  <key>ProgramArguments</key>
  <array>
"#
    );
    for argument in arguments {
        let _ = writeln!(plist, "    <string>{argument}</string>");
    }
    let _ = write!(
        plist,
        r#"  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>{}</string>
  <key>StandardErrorPath</key>
  <string>{}</string>
</dict>
</plist>
"#,
        xml_escape(stdout_path.to_string_lossy().as_ref()),
        xml_escape(stderr_path.to_string_lossy().as_ref()),
    );
    plist
}

fn launchd_plist_path() -> Result<PathBuf> {
    Ok(launch_agents_dir()?.join(format!("{SERVICE_LABEL}.plist")))
}

fn launch_agents_dir() -> Result<PathBuf> {
    Ok(home_dir()?.join("Library/LaunchAgents"))
}

fn logs_dir() -> Result<PathBuf> {
    Ok(home_dir()?.join("Library/Logs"))
}

fn default_logs_dir() -> PathBuf {
    PathBuf::from("/tmp")
}

fn home_dir() -> Result<PathBuf> {
    if let Some(path) = std::env::var_os("HOME") {
        return Ok(PathBuf::from(path));
    }
    if let Some(path) = std::env::var_os("USERPROFILE") {
        return Ok(PathBuf::from(path));
    }
    let home_drive = std::env::var_os("HOMEDRIVE");
    let home_path = std::env::var_os("HOMEPATH");
    match (home_drive, home_path) {
        (Some(drive), Some(path)) => {
            let mut joined = PathBuf::from(drive);
            joined.push(path);
            Ok(joined)
        }
        _ => anyhow::bail!("HOME is not set"),
    }
}

fn launchctl_domain() -> Result<String> {
    let uid = current_uid()?;
    Ok(format!("gui/{uid}"))
}

fn launchctl_service_target() -> Result<String> {
    Ok(format!("{}/{}", launchctl_domain()?, SERVICE_LABEL))
}

fn current_uid() -> Result<u32> {
    let output = ProcessCommand::new("id").arg("-u").output()?;
    if !output.status.success() {
        anyhow::bail!("failed to read current uid with `id -u`");
    }
    let uid = String::from_utf8(output.stdout)?.trim().parse::<u32>()?;
    Ok(uid)
}

fn launchd_service_loaded() -> Result<bool> {
    let service_target = launchctl_service_target()?;
    let output = ProcessCommand::new("launchctl")
        .arg("print")
        .arg(service_target)
        .output()?;
    Ok(output.status.success())
}

fn run_launchctl<I, S>(args: I) -> Result<()>
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let args_vec = args
        .into_iter()
        .map(|value| value.as_ref().to_string())
        .collect::<Vec<_>>();
    let output = ProcessCommand::new("launchctl").args(&args_vec).output()?;
    if output.status.success() {
        return Ok(());
    }

    let stderr = String::from_utf8_lossy(&output.stderr);
    let stdout = String::from_utf8_lossy(&output.stdout);
    anyhow::bail!(
        "launchctl {} failed\nstdout: {}\nstderr: {}",
        args_vec.join(" "),
        stdout.trim(),
        stderr.trim()
    );
}

fn xml_escape(input: &str) -> String {
    input
        .replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

fn print_status(locale: Locale, summary: &StatusSummary) {
    println!("{}", status_title(locale));
    println!();
    println!(
        "{}: {}",
        status_codex_home_label(locale),
        summary.codex_home.display()
    );
    println!(
        "{}: {}",
        status_config_file_label(locale),
        summary.config_path.display()
    );
    println!(
        "{}: {}",
        status_sqlite_file_label(locale),
        summary.sqlite_path.display()
    );
    println!(
        "{}: {}",
        status_target_provider_label(locale),
        summary.provider
    );
    println!(
        "{}: {}",
        status_total_threads_label(locale),
        summary.total_rows
    );
    println!(
        "{}: {}",
        status_rows_needing_reconcile_label(locale),
        summary.mismatched_rows
    );
    println!();
    println!("{}", status_distribution_heading(locale));
    for (provider, count) in &summary.distribution {
        println!("  {provider}: {count}");
    }
    println!();
    println!("{}", status_background_service_heading(locale));
    println!(
        "  {}: {}",
        status_plist_path_label(locale),
        summary.launchd_plist_path.display()
    );
    println!(
        "  {}: {}",
        status_installed_label(locale),
        yes_no(locale, summary.launchd_installed)
    );
    println!(
        "  {}: {}",
        status_loaded_label(locale),
        yes_no(locale, summary.launchd_loaded)
    );
}

fn print_sync_summary(locale: Locale, title: &str, summary: &ReconcileSummary) {
    println!("{title}");
    println!(
        "{}: {}",
        status_target_provider_label(locale),
        summary.provider
    );
    println!(
        "{}: {}",
        sync_rows_updated_label(locale),
        summary.changed_rows
    );
    println!(
        "{}: {}",
        status_total_threads_label(locale),
        summary.total_rows
    );
    println!(
        "{}: {} ms",
        sync_elapsed_label(locale),
        summary.elapsed.as_millis()
    );
    if let Some(backup_path) = &summary.backup_path {
        println!("{}: {}", sync_backup_label(locale), backup_path.display());
    }
}

fn yes_no(locale: Locale, value: bool) -> &'static str {
    match (locale, value) {
        (Locale::En, true) => "yes",
        (Locale::En, false) => "no",
        (Locale::ZhHans, true) => "",
        (Locale::ZhHans, false) => "",
    }
}

fn root_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Keep CODEX_HOME/state_5.sqlite aligned with the current model_provider.",
        Locale::ZhHans => "让 CODEX_HOME/state_5.sqlite 持续对齐当前 model_provider。",
    }
}

fn root_long_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => {
            "codex-threadripper is a human-first maintenance tool for Codex thread history.\n\nIt reads the active provider from CODEX_HOME/config.toml and rewrites CODEX_HOME/state_5.sqlite so every thread stays in the same provider bucket. That makes thread lists and resume flows stop fragmenting across providers.\n\nExamples:\n  codex-threadripper status\n  codex-threadripper sync\n  codex-threadripper watch\n  codex-threadripper install-launchd"
        }
        Locale::ZhHans => {
            "codex-threadripper 是一个面向人的 Codex 线程历史维护工具。\n\n它会读取 CODEX_HOME/config.toml 里的当前 provider,并改写 CODEX_HOME/state_5.sqlite,让所有线程始终落在同一个 provider 桶里。这样线程列表和 resume 流程就不会再被 provider 切碎。\n\n示例:\n  codex-threadripper status\n  codex-threadripper sync\n  codex-threadripper watch\n  codex-threadripper install-launchd"
        }
    }
}

fn help_template(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "{before-help}{about-section}Usage: {usage}\n\n{all-args}{after-help}",
        Locale::ZhHans => "{before-help}{about-section}用法:{usage}\n\n{all-args}{after-help}",
    }
}

fn commands_heading(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Commands",
        Locale::ZhHans => "命令",
    }
}

fn options_heading(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Options",
        Locale::ZhHans => "选项",
    }
}

fn dir_value_name(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "DIR",
        Locale::ZhHans => "目录",
    }
}

fn provider_value_name(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "PROVIDER",
        Locale::ZhHans => "PROVIDER",
    }
}

fn milliseconds_value_name(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "MILLISECONDS",
        Locale::ZhHans => "毫秒",
    }
}

fn codex_home_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Codex home directory. Defaults to $HOME/.codex.",
        Locale::ZhHans => "Codex home 目录。默认值是 $HOME/.codex。",
    }
}

fn provider_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Force a provider instead of reading model_provider from config.toml.",
        Locale::ZhHans => "强制指定 provider,跳过从 config.toml 读取 model_provider。",
    }
}

fn help_option_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Print help",
        Locale::ZhHans => "显示帮助信息",
    }
}

fn version_option_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Print version",
        Locale::ZhHans => "显示版本号",
    }
}

fn status_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => {
            "Show the current config provider, SQLite distribution, and launchd service state."
        }
        Locale::ZhHans => "显示当前 config provider、SQLite 分布和 launchd 服务状态。",
    }
}

fn sync_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Reconcile state_5.sqlite once right now.",
        Locale::ZhHans => "立刻执行一次 state_5.sqlite 收敛。",
    }
}

fn watch_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Keep watching config.toml and keep reconciling new rows in state_5.sqlite.",
        Locale::ZhHans => "持续监听 config.toml,并持续收敛 state_5.sqlite 里的新增行。",
    }
}

fn poll_interval_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "How often to reconcile SQLite while watching.",
        Locale::ZhHans => "watch 模式下,定时收敛 SQLite 的频率。",
    }
}

fn print_plist_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Print the launchd plist that would run this tool in the background.",
        Locale::ZhHans => "打印后台运行这个工具所需的 launchd plist。",
    }
}

fn install_launchd_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Install and load a launchd service for background watching.",
        Locale::ZhHans => "安装并加载一个用于后台 watch 的 launchd 服务。",
    }
}

fn uninstall_launchd_about(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Unload and remove the launchd service.",
        Locale::ZhHans => "卸载并移除 launchd 服务。",
    }
}

fn launchd_poll_help(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Polling interval for the background watcher.",
        Locale::ZhHans => "后台 watcher 的轮询间隔。",
    }
}

fn current_exe_error(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "failed to resolve current executable",
        Locale::ZhHans => "无法解析当前可执行文件路径",
    }
}

fn sync_complete_title(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Sync complete",
        Locale::ZhHans => "同步完成",
    }
}

fn watch_started_title(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Watch started",
        Locale::ZhHans => "已开始监听",
    }
}

fn config_change_title(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Config change detected",
        Locale::ZhHans => "检测到配置变更",
    }
}

fn background_reconcile_title(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Background reconcile",
        Locale::ZhHans => "后台收敛",
    }
}

fn watch_running_message(locale: Locale, codex_home: &Path, poll_interval: Duration) -> String {
    match locale {
        Locale::En => format!(
            "Watching {} with a {} ms poll interval. Press Ctrl-C to stop.",
            codex_home.display(),
            poll_interval.as_millis()
        ),
        Locale::ZhHans => format!(
            "正在监听 {},轮询间隔 {} ms。按 Ctrl-C 停止。",
            codex_home.display(),
            poll_interval.as_millis()
        ),
    }
}

fn watcher_error_message(locale: Locale, err: notify::Error) -> String {
    match locale {
        Locale::En => format!("Watcher error: {err}"),
        Locale::ZhHans => format!("监听器错误:{err}"),
    }
}

fn watcher_disconnected_error(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "watcher disconnected",
        Locale::ZhHans => "监听器已断开",
    }
}

fn watch_stopped_message(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Watch stopped.",
        Locale::ZhHans => "监听已停止。",
    }
}

fn install_launchd_done(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Installed launchd service.",
        Locale::ZhHans => "已安装 launchd 服务。",
    }
}

fn uninstall_launchd_done(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Removed launchd service.",
        Locale::ZhHans => "已移除 launchd 服务。",
    }
}

fn launchd_label_message(locale: Locale, label: &str) -> String {
    match locale {
        Locale::En => format!("Service label: {label}"),
        Locale::ZhHans => format!("服务标签:{label}"),
    }
}

fn launchd_plist_message(locale: Locale, path: &Path) -> String {
    match locale {
        Locale::En => format!("Plist path: {}", path.display()),
        Locale::ZhHans => format!("Plist 路径:{}", path.display()),
    }
}

fn launchd_codex_home_message(locale: Locale, path: &Path) -> String {
    match locale {
        Locale::En => format!("Codex home: {}", path.display()),
        Locale::ZhHans => format!("Codex home:{}", path.display()),
    }
}

fn launchd_polling_message(locale: Locale, poll_interval: Duration) -> String {
    match locale {
        Locale::En => format!("Polling every {} ms.", poll_interval.as_millis()),
        Locale::ZhHans => format!("轮询间隔:{} ms。", poll_interval.as_millis()),
    }
}

fn next_steps_heading(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Next steps",
        Locale::ZhHans => "下一步",
    }
}

fn install_next_steps(locale: Locale, exe_path: &Path, codex_home: &Path) -> Result<Vec<String>> {
    let status_command = cli_status_command(exe_path, codex_home);
    let service_target = launchctl_service_target()?;
    let log_path = logs_dir()
        .unwrap_or_else(|_| default_logs_dir())
        .join("codex-threadripper.log");

    Ok(vec![
        run_status_next_step(locale, &status_command),
        inspect_service_next_step(locale, &format!("launchctl print {service_target}")),
        tail_log_next_step(locale, &format!("tail -f {}", log_path.display())),
    ])
}

fn cli_status_command(exe_path: impl AsRef<Path>, codex_home: &Path) -> String {
    format!(
        "{} --codex-home {} status",
        shell_quote(exe_path.as_ref().display().to_string()),
        shell_quote(codex_home.display().to_string())
    )
}

fn shell_quote(input: String) -> String {
    if input
        .chars()
        .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '/' | '.' | '_' | '-'))
    {
        return input;
    }
    format!("'{}'", input.replace('\'', r"'\''"))
}

fn run_status_next_step(locale: Locale, command: &str) -> String {
    match locale {
        Locale::En => format!("Run this to verify status: {command}"),
        Locale::ZhHans => format!("运行这条命令查看状态:{command}"),
    }
}

fn inspect_service_next_step(locale: Locale, command: &str) -> String {
    match locale {
        Locale::En => format!("Run this to inspect the launchd service: {command}"),
        Locale::ZhHans => format!("运行这条命令查看 launchd 服务:{command}"),
    }
}

fn tail_log_next_step(locale: Locale, command: &str) -> String {
    match locale {
        Locale::En => format!("Run this to watch the live log: {command}"),
        Locale::ZhHans => format!("运行这条命令查看实时日志:{command}"),
    }
}

fn no_launchd_plist_message(locale: Locale, path: &Path) -> String {
    match locale {
        Locale::En => format!("No launchd plist is installed at {}.", path.display()),
        Locale::ZhHans => format!("{} 这里还没有安装 launchd plist。", path.display()),
    }
}

fn status_title(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Codex Threadripper",
        Locale::ZhHans => "Codex Threadripper",
    }
}

fn status_codex_home_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Codex home",
        Locale::ZhHans => "Codex home",
    }
}

fn status_config_file_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Config file",
        Locale::ZhHans => "配置文件",
    }
}

fn status_sqlite_file_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "SQLite file",
        Locale::ZhHans => "SQLite 文件",
    }
}

fn status_target_provider_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Target provider",
        Locale::ZhHans => "目标 provider",
    }
}

fn status_total_threads_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Total threads",
        Locale::ZhHans => "线程总数",
    }
}

fn status_rows_needing_reconcile_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Rows needing reconcile",
        Locale::ZhHans => "待收敛行数",
    }
}

fn status_distribution_heading(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Provider distribution:",
        Locale::ZhHans => "Provider 分布:",
    }
}

fn status_background_service_heading(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Background service:",
        Locale::ZhHans => "后台服务:",
    }
}

fn status_plist_path_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Plist path",
        Locale::ZhHans => "Plist 路径",
    }
}

fn status_installed_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Installed",
        Locale::ZhHans => "已安装",
    }
}

fn status_loaded_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Loaded",
        Locale::ZhHans => "已加载",
    }
}

fn sync_rows_updated_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Rows updated",
        Locale::ZhHans => "已更新行数",
    }
}

fn sync_elapsed_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Elapsed",
        Locale::ZhHans => "耗时",
    }
}

fn sync_backup_label(locale: Locale) -> &'static str {
    match locale {
        Locale::En => "Backup",
        Locale::ZhHans => "备份文件",
    }
}

#[cfg(test)]
mod tests {
    use super::APP_VERSION;
    use super::Cli;
    use super::Command;
    use super::DEFAULT_POLL_INTERVAL_MS;
    use super::Locale;
    use super::build_launchd_plist;
    use super::detect_locale_from_sources;
    use super::install_next_steps;
    use super::localized_command;
    use super::parse_apple_languages;
    use super::parse_locale_tag;
    use super::read_provider_from_config;
    use super::reconcile_sqlite_in_place;
    use super::reconcile_sqlite_with_backup;
    use anyhow::Result;
    use clap::FromArgMatches;
    use clap::error::ErrorKind;
    use rusqlite::Connection;
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;
    use std::time::Duration;

    #[test]
    fn parses_supported_locale_tags() {
        assert_eq!(parse_locale_tag("zh_CN.UTF-8"), Some(Locale::ZhHans));
        assert_eq!(parse_locale_tag("zh-Hans-CN"), Some(Locale::ZhHans));
        assert_eq!(parse_locale_tag("en_US.UTF-8"), Some(Locale::En));
        assert_eq!(parse_locale_tag("en-GB"), Some(Locale::En));
        assert_eq!(parse_locale_tag("ja_JP.UTF-8"), None);
    }

    #[test]
    fn parses_apple_languages_output() {
        let output = "(\n    \"zh-Hans-CN\",\n    \"en-US\"\n)\n";
        assert_eq!(parse_apple_languages(output), Some(Locale::ZhHans));
    }

    #[test]
    fn detects_locale_from_sources() {
        let locale = detect_locale_from_sources(["", "zh_CN.UTF-8"], None);
        assert_eq!(locale, Locale::ZhHans);

        let locale = detect_locale_from_sources([""], Some("(\n    en-US\n)\n"));
        assert_eq!(locale, Locale::En);
    }

    #[test]
    fn localizes_help_surface() {
        let mut command = localized_command(Locale::ZhHans);
        let mut help = Vec::new();
        command.write_long_help(&mut help).unwrap();
        let rendered = String::from_utf8(help).unwrap();
        assert!(rendered.contains("面向人的 Codex 线程历史维护工具"));
        assert!(rendered.contains("用法:"));
        assert!(rendered.contains("命令:"));
        assert!(rendered.contains("选项:"));
        assert!(rendered.contains("安装并加载一个用于后台 watch 的 launchd 服务"));
        assert!(rendered.contains("强制指定 provider,跳过从 config.toml 读取 model_provider"));
        assert!(rendered.contains("显示帮助信息"));
        assert!(rendered.contains("显示版本号"));

        let mut command = localized_command(Locale::En);
        let mut help = Vec::new();
        command.write_long_help(&mut help).unwrap();
        let rendered = String::from_utf8(help).unwrap();
        assert!(rendered.contains("human-first maintenance tool for Codex thread history"));
        assert!(rendered.contains("Usage:"));
        assert!(rendered.contains("Commands:"));
        assert!(rendered.contains("Options:"));
        assert!(rendered.contains("Install and load a launchd service for background watching"));
        assert!(
            rendered
                .contains("Force a provider instead of reading model_provider from config.toml")
        );
        assert!(rendered.contains("Print help"));
        assert!(rendered.contains("Print version"));
    }

    #[test]
    fn subcommand_help_and_version_are_stable() {
        let command = localized_command(Locale::En);
        let help_error = command
            .clone()
            .try_get_matches_from(["codex-threadripper", "install-launchd", "--help"])
            .unwrap_err();
        assert_eq!(help_error.kind(), ErrorKind::DisplayHelp);

        let version_error = command
            .try_get_matches_from(["codex-threadripper", "install-launchd", "--version"])
            .unwrap_err();
        assert_eq!(version_error.kind(), ErrorKind::DisplayVersion);
        assert!(version_error.to_string().contains(APP_VERSION));
    }

    #[test]
    fn reads_provider_from_config() -> Result<()> {
        let dir = tempfile::tempdir()?;
        fs::write(dir.path().join("config.toml"), "model_provider = \"vm\"\n")?;
        let provider = read_provider_from_config(dir.path())?;
        assert_eq!(provider, "vm");
        Ok(())
    }

    #[test]
    fn defaults_to_openai_without_config() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let provider = read_provider_from_config(dir.path())?;
        assert_eq!(provider, "openai");
        Ok(())
    }

    #[test]
    fn reconciles_sqlite_rows_in_place() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let sqlite_path = dir.path().join("state_5.sqlite");
        seed_sqlite(&sqlite_path)?;

        let (changed_rows, total_rows) = reconcile_sqlite_in_place(&sqlite_path, "openai")?;
        let connection = Connection::open(&sqlite_path)?;
        let other_rows: u64 = connection.query_row(
            "SELECT COUNT(*) FROM threads WHERE model_provider <> 'openai'",
            [],
            |row| row.get(0),
        )?;

        assert_eq!(changed_rows, 2);
        assert_eq!(total_rows, 3);
        assert_eq!(other_rows, 0);
        Ok(())
    }

    #[test]
    fn sync_backup_preserves_pre_reconcile_state() -> Result<()> {
        let dir = tempfile::tempdir()?;
        let sqlite_path = dir.path().join("state_5.sqlite");
        seed_sqlite(&sqlite_path)?;

        let (changed_rows, total_rows, backup_path) =
            reconcile_sqlite_with_backup(&sqlite_path, "openai")?;
        let live_connection = Connection::open(&sqlite_path)?;
        let backup_connection = Connection::open(&backup_path)?;
        let live_other_rows: u64 = live_connection.query_row(
            "SELECT COUNT(*) FROM threads WHERE model_provider <> 'openai'",
            [],
            |row| row.get(0),
        )?;
        let backup_other_rows: u64 = backup_connection.query_row(
            "SELECT COUNT(*) FROM threads WHERE model_provider <> 'openai'",
            [],
            |row| row.get(0),
        )?;

        assert_eq!(changed_rows, 2);
        assert_eq!(total_rows, 3);
        assert_eq!(live_other_rows, 0);
        assert_eq!(backup_other_rows, 2);
        Ok(())
    }

    #[test]
    fn watch_uses_five_hundred_ms_by_default() -> Result<()> {
        let matches =
            localized_command(Locale::En).try_get_matches_from(["codex-threadripper", "watch"])?;
        let cli = Cli::from_arg_matches(&matches)?;

        match cli.command {
            Command::Watch { poll_interval_ms } => {
                assert_eq!(poll_interval_ms, DEFAULT_POLL_INTERVAL_MS);
            }
            other => panic!("expected watch command, got {other:?}"),
        }

        Ok(())
    }

    #[test]
    fn generates_launchd_plist() {
        let plist = build_launchd_plist(
            PathBuf::from("/tmp/codex-threadripper").as_path(),
            PathBuf::from("/tmp/codex-home").as_path(),
            Some("openai"),
            Duration::from_millis(DEFAULT_POLL_INTERVAL_MS),
        );
        assert!(plist.contains("dev.wangnov.codex-threadripper"));
        assert!(plist.contains("--codex-home"));
        assert!(plist.contains("--provider"));
        assert!(plist.contains("watch"));
    }

    #[test]
    fn builds_install_next_steps() -> Result<()> {
        let steps = install_next_steps(
            Locale::ZhHans,
            PathBuf::from("/tmp/codex threadripper").as_path(),
            PathBuf::from("/tmp/codex home").as_path(),
        )?;
        assert_eq!(steps.len(), 3);
        assert!(steps[0].contains("运行这条命令查看状态"));
        assert!(
            steps[0].contains("'/tmp/codex threadripper' --codex-home '/tmp/codex home' status")
        );
        assert!(steps[1].contains("launchctl print"));
        assert!(steps[2].contains("tail -f"));
        Ok(())
    }

    fn seed_sqlite(path: &Path) -> Result<()> {
        let connection = Connection::open(path)?;
        connection.execute_batch(
            "
            CREATE TABLE threads (
                id TEXT PRIMARY KEY,
                rollout_path TEXT NOT NULL,
                created_at INTEGER NOT NULL,
                updated_at INTEGER NOT NULL,
                source TEXT NOT NULL,
                model_provider TEXT NOT NULL,
                cwd TEXT NOT NULL,
                title TEXT NOT NULL,
                sandbox_policy TEXT NOT NULL,
                approval_mode TEXT NOT NULL,
                tokens_used INTEGER NOT NULL DEFAULT 0,
                has_user_event INTEGER NOT NULL DEFAULT 0,
                archived INTEGER NOT NULL DEFAULT 0,
                archived_at INTEGER,
                git_sha TEXT,
                git_branch TEXT,
                git_origin_url TEXT,
                cli_version TEXT NOT NULL DEFAULT '',
                first_user_message TEXT NOT NULL DEFAULT '',
                agent_nickname TEXT,
                agent_role TEXT,
                memory_mode TEXT NOT NULL DEFAULT 'enabled',
                model TEXT,
                reasoning_effort TEXT,
                agent_path TEXT
            );
            INSERT INTO threads (
                id, rollout_path, created_at, updated_at, source, model_provider, cwd, title,
                sandbox_policy, approval_mode
            ) VALUES
                ('1', '/tmp/a', 1, 1, 'cli', 'vm', '/tmp', 'a', 'workspace-write', 'auto'),
                ('2', '/tmp/b', 1, 1, 'cli', 'cp', '/tmp', 'b', 'workspace-write', 'auto'),
                ('3', '/tmp/c', 1, 1, 'cli', 'openai', '/tmp', 'c', 'workspace-write', 'auto');
            ",
        )?;
        Ok(())
    }
}