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
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
// SPDX-License-Identifier: AGPL-3.0-or-later
use clap::{CommandFactory, Parser, Subcommand, ValueEnum};
use kaizen::DataSource;
use kaizen::feedback::types::FeedbackLabel;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
const LONG_ABOUT: &str = "Deploy and share kaizen: real-time-tailable agent sessions, retros, and experiments to improve your repo, across Cursor, Claude Code, Codex, and Mistral Vibe. One SQLite store; redact before any sync. Docs: https://github.com/marquesds/kaizen/blob/main/docs/README.md";
#[derive(Parser)]
#[command(
name = "kaizen",
about = "AI agent session telemetry and insights",
long_about = LONG_ABOUT,
version,
propagate_version = true
)]
struct Cli {
/// Keep Phase 0-2 direct SQLite mode for this invocation.
#[arg(long, global = true)]
no_daemon: bool,
#[command(subcommand)]
cmd: Command,
}
#[derive(Subcommand)]
enum Command {
/// Ingest events from hooks or other sources.
#[command(next_help_heading = "Operate")]
Ingest {
#[command(subcommand)]
subcmd: IngestCommand,
},
/// Manage the local Kaizen daemon.
#[command(next_help_heading = "Operate")]
Daemon {
#[command(subcommand)]
subcmd: DaemonCommand,
},
/// Session list/show commands.
#[command(next_help_heading = "Trust & observe")]
Sessions {
#[command(subcommand)]
subcmd: SessionsCommand,
},
/// Search indexed session events.
#[command(next_help_heading = "Trust & observe")]
Search {
#[command(subcommand)]
subcmd: SearchCommand,
},
/// Structured trace query over local session events.
#[command(next_help_heading = "Trust & observe")]
Query {
expr: String,
#[arg(long)]
since: Option<String>,
#[arg(long, default_value_t = 50)]
limit: usize,
#[arg(long)]
json: bool,
#[arg(long)]
workspace: Option<PathBuf>,
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Aggregate session + cost stats across all agents.
#[command(next_help_heading = "Trust & observe")]
Summary {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Read from every registered workspace on this machine.
#[arg(long)]
all_workspaces: bool,
/// Emit JSON (same fields as the MCP `kaizen_summary` tool with json=true).
#[arg(long)]
json: bool,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
/// `local` (default) | `provider` (remote cache) | `mixed`. With `provider`/`mixed`, `--refresh` can call remote APIs.
#[arg(long, value_enum, default_value_t = DataSource::Local)]
source: DataSource,
},
/// Open interactive TUI.
#[command(next_help_heading = "Trust & observe")]
Tui {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Idempotent workspace setup (writes config, patches hooks, installs skill).
#[command(next_help_heading = "Trust & observe")]
Init {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Start proxy tasks and report deep model-call capture readiness.
#[arg(long)]
deep: bool,
},
/// Verify config, store, and hook wiring for this workspace.
#[command(next_help_heading = "Trust & observe")]
Doctor {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Load previous local agent sessions into Kaizen stores.
#[command(next_help_heading = "Trust & observe")]
Load {
/// workspace root; omit to load all registered workspaces
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Emit JSON load summary.
#[arg(long)]
json: bool,
},
/// Prune local sessions older than retention window (see `[retention].hot_days` or `--days`).
#[command(next_help_heading = "Operate")]
Gc {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Keep sessions started within the last N days (overrides config when set).
#[arg(long)]
days: Option<u32>,
/// Run VACUUM after delete (slow; reclaims file space).
#[arg(long)]
vacuum: bool,
},
/// Migrate local store between SQLite-only and tiered storage.
#[command(next_help_heading = "Operate")]
Migrate {
#[command(subcommand)]
subcmd: MigrateCommand,
},
/// Rich session insights: activity by day, top tools, recent sessions.
#[command(next_help_heading = "Trust & observe")]
Insights {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Read from every registered workspace on this machine.
#[arg(long)]
all_workspaces: bool,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
/// `local` | `provider` | `mixed`; `--refresh` can call remote APIs.
#[arg(long, value_enum, default_value_t = DataSource::Local)]
source: DataSource,
},
/// Skill and Cursor rule adoption from observed path refs in payloads (not silent injection).
#[command(next_help_heading = "Trust & observe")]
Guidance {
/// Trailing window in days (default 7).
#[arg(long, default_value_t = 7)]
days: u32,
/// Emit JSON report.
#[arg(long)]
json: bool,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
/// `local` | `provider` | `mixed`; `--refresh` can call remote APIs.
#[arg(long, value_enum, default_value_t = DataSource::Local)]
source: DataSource,
},
/// Smart metrics: code hotspots, slow tools, token sinks.
#[command(next_help_heading = "Trust & observe")]
Metrics {
#[command(subcommand)]
subcmd: Option<MetricsCommand>,
/// Trailing window in days (default 7).
#[arg(long, default_value_t = 7)]
days: u32,
/// Emit JSON report.
#[arg(long)]
json: bool,
/// Rebuild repo snapshot even if fingerprint unchanged.
#[arg(long)]
force: bool,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Read from every registered workspace on this machine.
#[arg(long)]
all_workspaces: bool,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
/// `local` | `provider` | `mixed`; `--refresh` can call remote APIs.
#[arg(long, value_enum, default_value_t = DataSource::Local)]
source: DataSource,
},
/// Run an agent command with Kaizen proxy/session env.
#[command(next_help_heading = "Trust & observe", trailing_var_arg = true)]
Observe {
/// Agent profile: claude, codex, cursor, or auto.
#[arg(long, default_value = "auto")]
agent: String,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Command and arguments to run.
#[arg(required = true, num_args = 1.., allow_hyphen_values = true)]
command: Vec<String>,
},
/// Flush local outbox to the configured ingest endpoint.
#[command(next_help_heading = "Operate")]
Sync {
#[command(subcommand)]
subcmd: SyncCommand,
},
/// Optional telemetry sinks (file NDJSON, PostHog, Datadog, OTLP, dev) alongside Kaizen sync.
#[command(next_help_heading = "Operate")]
Telemetry {
#[command(subcommand)]
subcmd: TelemetrySubcommand,
},
/// Experiment binding + report.
#[command(next_help_heading = "Improve")]
Exp {
#[command(subcommand)]
subcmd: ExpCommand,
},
/// Mine and manage local regression cases.
#[command(next_help_heading = "Improve")]
Cases {
#[command(subcommand)]
subcmd: CasesCommand,
},
/// Local automation rules over trace queries.
#[command(next_help_heading = "Improve")]
Rules {
#[command(subcommand)]
subcmd: RulesCommand,
},
/// Built-in local health alerts.
#[command(next_help_heading = "Improve")]
Alerts {
#[command(subcommand)]
subcmd: AlertsCommand,
},
/// Local review queue from rules and cases.
#[command(next_help_heading = "Improve")]
Review {
#[command(subcommand)]
subcmd: ReviewCommand,
},
/// Weekly-style heuristic retro report.
#[command(next_help_heading = "Improve")]
Retro {
/// Trailing window in days (default 7).
#[arg(long, default_value_t = 7)]
days: u32,
/// Print Markdown to stdout; do not write a file.
#[arg(long)]
dry_run: bool,
/// Emit JSON report on stdout (no file write).
#[arg(long)]
json: bool,
/// Overwrite this ISO week's report if it exists.
#[arg(long)]
force: bool,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
#[arg(long, value_enum, default_value_t = DataSource::Local)]
source: DataSource,
},
/// List registered workspaces on this machine.
#[command(next_help_heading = "Trust & observe")]
Projects {
#[command(subcommand)]
subcmd: ProjectsCommand,
},
/// Model Context Protocol server (stdio) — see docs/mcp.md.
#[command(next_help_heading = "Integrations")]
Mcp,
/// Upgrade kaizen to the latest release.
#[command(next_help_heading = "Operate")]
Upgrade {
/// Build from crates.io instead of installing a release binary.
#[arg(long)]
from_source: bool,
},
/// Print shell completion script to stdout; redirect or eval to install.
#[command(next_help_heading = "Shell")]
Completions {
#[arg(value_enum)]
shell: CompletionShell,
},
/// Local HTTP forwarder for Anthropic-style APIs + proxy telemetry. See docs/llm-proxy.md.
#[command(next_help_heading = "Operate")]
Proxy {
#[command(subcommand)]
subcmd: ProxyCommand,
},
/// LLM-as-a-Judge evaluations for agent sessions. See docs/usage.md.
#[command(next_help_heading = "Improve")]
Eval {
#[command(subcommand)]
subcmd: EvalCommand,
},
/// Prompt/system-prompt version tracking. See docs/usage.md.
#[command(next_help_heading = "Improve")]
Prompt {
#[command(subcommand)]
subcmd: PromptCommand,
},
/// Human feedback on agent sessions (score/label/note).
#[command(next_help_heading = "Improve")]
Feedback {
#[command(subcommand)]
subcmd: FeedbackCommand,
},
/// Post-stop test/lint outcomes (opt-in). See docs/outcomes.md.
#[command(next_help_heading = "Trust & observe")]
Outcomes {
#[command(subcommand)]
subcmd: OutcomesCommand,
},
/// Internal: sample OS stats for a hook PID. Spawned by ingest when opt-in.
#[command(hide = true, name = "__sampler-run")]
SamplerRun {
#[arg(long)]
workspace: PathBuf,
#[arg(long)]
session: String,
#[arg(long)]
pid: u32,
},
}
#[derive(Subcommand)]
enum EvalCommand {
/// Run LLM-as-a-Judge evals on unevaluated sessions.
Run {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Only evaluate sessions started in the last N days.
#[arg(long, default_value_t = 7)]
since_days: u64,
/// Print what would be evaluated without calling the judge.
#[arg(long)]
dry_run: bool,
/// Emit JSON array.
#[arg(long)]
json: bool,
},
/// List stored eval results.
List {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Only show sessions with score >= this value (0.0 = show all).
#[arg(long, default_value_t = 0.0)]
min_score: f64,
/// Emit JSON array.
#[arg(long)]
json: bool,
},
/// Print the rendered judge prompt for a session (no LLM call).
Prompt {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Session ID to render the prompt for.
session_id: String,
/// Rubric to use.
#[arg(long, default_value = "tool-efficiency-v1")]
rubric: String,
},
}
#[derive(Subcommand)]
enum ProjectsCommand {
/// List registered workspaces.
List,
}
#[derive(Subcommand)]
enum CasesCommand {
/// Mine cases from low evals and bad feedback.
Mine(SharedSinceJson),
/// Create one case for a session.
Create {
#[arg(long)]
session: String,
#[arg(long)]
reason: String,
#[arg(long)]
label: Option<String>,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
},
/// List cases.
List {
#[arg(long)]
status: Option<String>,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
},
/// Show one case.
Show(IdJson),
/// Archive one case.
Archive(IdOnly),
}
#[derive(Subcommand)]
enum RulesCommand {
/// Create local rule.
Create {
#[arg(long)]
name: String,
#[arg(long)]
filter: String,
#[arg(long)]
action: String,
#[arg(long)]
message: Option<String>,
#[command(flatten)]
ws: WorkspaceFlags,
},
/// List rules.
List(JsonOnly),
/// Run enabled rules.
Run {
#[arg(long)]
since: Option<String>,
#[arg(long)]
dry_run: bool,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
},
/// Enable a rule.
Enable(IdOnly),
/// Disable a rule.
Disable(IdOnly),
}
#[derive(Subcommand)]
enum AlertsCommand {
/// Check built-in alert conditions.
Check {
#[arg(long, default_value_t = 7)]
days: u64,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
},
}
#[derive(Subcommand)]
enum ReviewCommand {
/// List review items.
List {
#[arg(long)]
status: Option<String>,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
},
/// Show one review item.
Show(IdJson),
/// Mark review item resolved.
Resolve(IdOnly),
/// Mark review item dismissed.
Dismiss(IdOnly),
}
#[derive(clap::Args)]
struct WorkspaceFlags {
#[arg(long)]
workspace: Option<PathBuf>,
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
}
#[derive(clap::Args)]
struct SharedSinceJson {
#[arg(long)]
since: Option<String>,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
}
#[derive(clap::Args)]
struct IdJson {
id: String,
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
}
#[derive(clap::Args)]
struct IdOnly {
id: String,
#[command(flatten)]
ws: WorkspaceFlags,
}
#[derive(clap::Args)]
struct JsonOnly {
#[arg(long)]
json: bool,
#[command(flatten)]
ws: WorkspaceFlags,
}
#[derive(Subcommand)]
enum PromptCommand {
/// List all recorded prompt snapshots.
List {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Emit JSON array.
#[arg(long)]
json: bool,
},
/// Show files in a snapshot by fingerprint prefix.
Show {
fingerprint: String,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Emit JSON.
#[arg(long)]
json: bool,
},
/// Diff two snapshots.
Diff {
fingerprint_a: String,
fingerprint_b: String,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
/// Shells supported by clap_complete (redirect stdout to a file, or eval).
#[derive(Copy, Clone, Debug, ValueEnum, Eq, PartialEq)]
enum CompletionShell {
Bash,
Elvish,
Fish,
Powershell,
Zsh,
}
#[derive(Subcommand)]
enum ProxyCommand {
/// Bind and forward until interrupted.
Run {
/// Address to listen, e.g. 127.0.0.1:3847 (overrides [proxy] in config TOML).
#[arg(long)]
listen: Option<String>,
/// Upstream base URL, e.g. https://api.anthropic.com (no trailing slash).
#[arg(long)]
upstream: Option<String>,
/// Provider defaults and hints: anthropic, openai, or auto.
#[arg(long)]
provider: Option<String>,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum TelemetrySubcommand {
/// Append exporter template to `~/.kaizen/config.toml` (alias of `configure`).
Init {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Exporter template to append without prompting.
#[arg(long = "type", value_enum)]
exporter_type: Option<TelemetryExporterKind>,
/// File exporter path, absolute or relative to each workspace.
#[arg(long)]
path: Option<PathBuf>,
},
/// Call configured provider `health`, show query settings, and exporter resolution (redacted).
Doctor {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Run one provider `pull` into local `remote_*` cache (stub until APIs are fully wired).
Pull {
/// Trailing window in days (passed to the provider; coarse).
#[arg(long, default_value_t = 7)]
days: u32,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Replay events from local SQLite through telemetry exporters (PostHog, Datadog, OTLP, dev).
///
/// Does not POST to Kaizen ingest or modify the sync outbox. Requires `[sync].team_salt_hex`
/// and at least one enabled `[[telemetry.exporters]]`. Re-running sends duplicates (no dedupe).
/// Sessions pruned by `[retention].hot_days` are absent from the store (same as `retro`).
Push {
/// Trailing window in days (`end = now`, same idea as `retro` / `metrics`).
#[arg(long, default_value_t = 7)]
days: u32,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Every workspace registered for this machine (see `kaizen summary --all-workspaces`).
#[arg(long)]
all_workspaces: bool,
/// Print per-workspace event and batch counts without calling exporters.
#[arg(long)]
dry_run: bool,
},
/// Print JSON shapes for canonical telemetry items (see `sync::canonical`).
PrintSchema,
/// Validating wizard: append a `[[telemetry.exporters]]` row after live `health` succeeds.
Configure {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Exporter template to append without prompting.
#[arg(long = "type", value_enum)]
exporter_type: Option<TelemetryExporterKind>,
/// File exporter path, absolute or relative to each workspace.
#[arg(long)]
path: Option<PathBuf>,
/// API key (DD_API_KEY for datadog, POSTHOG_API_KEY for posthog). Falls back to env.
#[arg(long)]
api_key: Option<String>,
/// Datadog site (e.g. `datadoghq.com`, `us5.datadoghq.com`). Falls back to DD_SITE.
#[arg(long)]
site: Option<String>,
/// PostHog host. Falls back to POSTHOG_HOST.
#[arg(long)]
host: Option<String>,
/// OTLP endpoint. Falls back to OTEL_EXPORTER_OTLP_ENDPOINT.
#[arg(long)]
endpoint: Option<String>,
/// Fail instead of prompting for missing values; for scripts and CI.
#[arg(long)]
non_interactive: bool,
},
/// Send one synthetic event to every configured `[[telemetry.exporters]]` and report ok/fail.
Test {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Redacted: merged telemetry exporter resolution (TOML + env).
PrintEffectiveConfig {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Read local NDJSON from the `file` exporter (default: `<workspace>/.kaizen/telemetry.ndjson`).
Tail {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// File path (absolute or relative to workspace).
#[arg(long, short = 'f')]
file: Option<PathBuf>,
/// Print current file contents and exit (no follow).
#[arg(long)]
no_follow: bool,
/// Pretty-print each JSON line.
#[arg(long)]
json: bool,
},
}
#[derive(Clone, Copy, ValueEnum)]
enum TelemetryExporterKind {
File,
Posthog,
Datadog,
Otlp,
Dev,
}
impl TelemetryExporterKind {
fn as_str(self) -> &'static str {
match self {
Self::File => "file",
Self::Posthog => "posthog",
Self::Datadog => "datadog",
Self::Otlp => "otlp",
Self::Dev => "dev",
}
}
}
#[derive(Subcommand)]
enum ExpCommand {
/// Create experiment in Draft state (records control/treatment commits).
New {
#[arg(long)]
name: String,
#[arg(long)]
hypothesis: String,
#[arg(long)]
change: String,
/// tokens_per_session|cost_per_session|success_rate|tool_loops|duration_minutes|files_per_session
#[arg(long)]
metric: String,
/// git|branch|manual
#[arg(long, default_value = "git")]
bind: String,
#[arg(long, default_value_t = 14)]
duration_days: u32,
/// target delta pct, e.g. -10.0 for -10%
#[arg(long, default_value_t = -10.0, allow_hyphen_values = true)]
target_pct: f64,
#[arg(long)]
control_commit: Option<String>,
#[arg(long)]
treatment_commit: Option<String>,
#[arg(long)]
control_branch: Option<String>,
#[arg(long)]
treatment_branch: Option<String>,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Transition experiment from Draft to Running.
Start {
id: String,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// List all experiments.
List {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Show one experiment's metadata.
Status {
id: String,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Manual variant tag for a session.
Tag {
id: String,
#[arg(long)]
session: String,
/// control|treatment|excluded
#[arg(long)]
variant: String,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Render markdown (or JSON) report with bootstrap CI.
Report {
id: String,
#[arg(long)]
json: bool,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Mark experiment Concluded.
Conclude {
id: String,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Mark experiment Archived (must be Concluded first).
Archive {
id: String,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Print MDE at 80% power / 95% CI for a metric given expected sample size.
Power {
/// tokens_per_session|cost_per_session|success_rate|…
#[arg(long)]
metric: String,
/// Expected sessions per arm.
#[arg(long)]
baseline_n: usize,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum SyncCommand {
/// Run sync loop until interrupted (or use --once).
Run {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Single flush then exit (for tests / scripts).
#[arg(long)]
once: bool,
},
/// Show outbox depth and last flush / error state.
Status {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum DaemonCommand {
/// Run daemon in foreground, or spawn it and exit with `--background`.
Start {
/// Spawn daemon as child process, wait until ready, then exit.
#[arg(long)]
background: bool,
},
/// Gracefully stop daemon.
Stop,
/// Show daemon pid, uptime, queue depth, and last error.
Status,
}
#[derive(Subcommand)]
enum MetricsCommand {
/// Rebuild repo snapshot and Ladybug sidecar.
Index {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Rebuild even when fingerprint unchanged.
#[arg(long)]
force: bool,
},
/// Field coverage and trace-correlation health.
Quality {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Trailing window in days.
#[arg(long, default_value_t = 7)]
days: u32,
/// Emit JSON report.
#[arg(long)]
json: bool,
},
}
#[derive(Subcommand)]
enum IngestCommand {
/// Read hook event from stdin and log it.
Hook {
/// hook source agent
#[arg(long, value_enum)]
source: Source,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum SessionsCommand {
/// List sessions for current workspace.
List {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Read from every registered workspace on this machine.
#[arg(long)]
all_workspaces: bool,
/// Emit JSON (same as MCP with json=true)
#[arg(long)]
json: bool,
/// Cap rows after sorting (newest first). Omit for 100 rows; 0 returns all.
#[arg(long)]
limit: Option<usize>,
/// Force a full agent transcript rescan before reading. This can take a while on large workspaces.
#[arg(short, long)]
refresh: bool,
},
/// Load previous local agent sessions into Kaizen stores.
Load {
/// workspace root; omit to load all registered workspaces
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
/// Emit JSON load summary.
#[arg(long)]
json: bool,
},
/// Show full details for a session.
Show {
id: String,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Attach human feedback (score/label/note) to a session.
Annotate {
id: String,
#[arg(long, value_parser = clap::value_parser!(u8).range(1..=5))]
score: Option<u8>,
#[arg(long, value_enum)]
label: Option<FeedbackLabel>,
#[arg(long)]
note: Option<String>,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Render nested tool-span tree for a session.
Tree {
id: String,
#[arg(long, default_value = "999")]
depth: u32,
#[arg(long)]
json: bool,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Render Datadog-style trace spans for a session.
Trace {
id: String,
#[arg(long)]
json: bool,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Full-text search session events.
Search {
query: String,
#[arg(long)]
since: Option<String>,
#[arg(long)]
agent: Option<String>,
#[arg(long)]
kind: Option<String>,
#[arg(long, default_value_t = 50)]
limit: usize,
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum SearchCommand {
/// Drop and rebuild the workspace search index.
Reindex {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
}
#[derive(Subcommand)]
enum OutcomesCommand {
/// Show stored JSON row for a session.
Show {
id: String,
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
},
/// Internal: run tests/lint and upsert `session_outcomes` (ingest spawns this).
#[command(hide = true)]
Measure {
/// workspace root (db + repo path)
#[arg(long)]
workspace: PathBuf,
/// Session id
#[arg(long)]
session: String,
},
}
#[derive(Subcommand)]
enum FeedbackCommand {
/// List feedback records for the workspace.
List {
#[arg(long)]
workspace: Option<PathBuf>,
/// project name shorthand for --workspace (mutually exclusive)
#[arg(long, conflicts_with = "workspace")]
project: Option<String>,
#[arg(long, value_enum)]
label: Option<FeedbackLabel>,
#[arg(long)]
since: Option<String>,
#[arg(long)]
json: bool,
},
}
#[derive(ValueEnum, Clone, Debug)]
enum Source {
Cursor,
Claude,
Vibe,
}
#[derive(Subcommand)]
enum MigrateCommand {
/// Export SQLite rows into hot log + cold Parquet.
V2 {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
/// Keep future/skewed timestamps instead of failing validation.
#[arg(long)]
allow_skew: bool,
},
/// Restore raw SQLite events from hot log + cold Parquet.
V1 {
/// workspace root (default: cwd)
#[arg(long)]
workspace: Option<PathBuf>,
},
}
fn resolve_ws(
workspace: Option<&std::path::Path>,
project: Option<&str>,
) -> anyhow::Result<Option<PathBuf>> {
match (workspace, project) {
(None, None) => Ok(None),
(w, p) => kaizen::shell::cli::resolve_target(w, p).map(|(path, _)| Some(path)),
}
}
fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let cli = Cli::parse();
if cli.no_daemon {
unsafe { std::env::set_var("KAIZEN_DAEMON", "0") };
}
match cli.cmd {
Command::Daemon { subcmd } => dispatch_daemon(subcmd),
Command::Ingest {
subcmd:
IngestCommand::Hook {
source,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
ingest_hook(source, ws)
}
Command::Sessions {
subcmd:
SessionsCommand::List {
workspace,
project,
all_workspaces,
json,
limit,
refresh,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_sessions_list(
ws.as_deref(),
json,
refresh,
all_workspaces,
limit,
)
}
Command::Sessions {
subcmd:
SessionsCommand::Load {
workspace,
project,
json,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::load::cmd_load(ws.as_deref(), json)
}
Command::Sessions {
subcmd:
SessionsCommand::Show {
id,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_session_show(&id, ws.as_deref())
}
Command::Sessions {
subcmd:
SessionsCommand::Annotate {
id,
score,
label,
note,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::feedback::cmd_sessions_annotate(&id, score, label, note, ws.as_deref())
}
Command::Sessions {
subcmd:
SessionsCommand::Tree {
id,
depth,
json,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_sessions_tree(&id, depth, json, ws.as_deref())
}
Command::Sessions {
subcmd:
SessionsCommand::Trace {
id,
json,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_sessions_trace(&id, json, ws.as_deref())
}
Command::Sessions {
subcmd:
SessionsCommand::Search {
query,
since,
agent,
kind,
limit,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::search::cmd_sessions_search(
ws.as_deref(),
&query,
since.as_deref(),
agent.as_deref(),
kind.as_deref(),
limit,
)
}
Command::Search {
subcmd: SearchCommand::Reindex { workspace, project },
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::search::cmd_search_reindex(ws.as_deref())
}
Command::Query {
expr,
since,
limit,
json,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::core_query::cmd_query(
ws.as_deref(),
&expr,
since.as_deref(),
limit,
json,
)
}
Command::Feedback {
subcmd:
FeedbackCommand::List {
workspace,
project,
label,
since,
json,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::feedback::cmd_feedback_list(ws.as_deref(), label, since, json)
}
Command::Summary {
workspace,
project,
all_workspaces,
json,
refresh,
source,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_summary(ws.as_deref(), json, refresh, all_workspaces, source)
}
Command::Tui { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?
.map(Ok)
.unwrap_or_else(|| kaizen::core::workspace::resolve(None))?;
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
let result = rt.block_on(kaizen::ui::tui::run(&ws));
rt.shutdown_timeout(std::time::Duration::from_millis(500));
result
}
Command::Init {
workspace,
project,
deep,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::cli::cmd_init(ws.as_deref(), deep)
}
Command::Doctor { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
let code = kaizen::shell::doctor::cmd_doctor(ws.as_deref())?;
// Non-zero: store/IO failure; hooks missing stay 0
if code != 0 {
std::process::exit(code);
}
Ok(())
}
Command::Load {
workspace,
project,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::load::cmd_load(ws.as_deref(), json)
}
Command::Gc {
workspace,
project,
days,
vacuum,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::gc::cmd_gc(ws.as_deref(), days, vacuum)
}
Command::Migrate { subcmd } => match subcmd {
MigrateCommand::V2 {
workspace,
allow_skew,
} => kaizen::shell::migrate::cmd_migrate_v2(workspace.as_deref(), allow_skew),
MigrateCommand::V1 { workspace } => {
kaizen::shell::migrate::cmd_migrate_v1(workspace.as_deref())
}
},
Command::Completions { shell } => {
let sh = match shell {
CompletionShell::Bash => clap_complete::Shell::Bash,
CompletionShell::Elvish => clap_complete::Shell::Elvish,
CompletionShell::Fish => clap_complete::Shell::Fish,
CompletionShell::Powershell => clap_complete::Shell::PowerShell,
CompletionShell::Zsh => clap_complete::Shell::Zsh,
};
let mut cmd = Cli::command();
clap_complete::generate(sh, &mut cmd, "kaizen", &mut std::io::stdout());
let _ = std::io::stdout().flush();
Ok(())
}
Command::Insights {
workspace,
project,
all_workspaces,
refresh,
source,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::insights::cmd_insights(ws.as_deref(), all_workspaces, refresh, source)
}
Command::Guidance {
days,
json,
workspace,
project,
refresh,
source,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::guidance::cmd_guidance(ws.as_deref(), days, json, refresh, source)
}
Command::Metrics {
subcmd,
days,
json,
force,
workspace,
project,
all_workspaces,
refresh,
source,
} => match subcmd {
Some(MetricsCommand::Index {
workspace,
project,
force,
}) => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::metrics::cmd_metrics_index(ws.as_deref(), force)
}
Some(MetricsCommand::Quality {
workspace,
project,
days,
json,
}) => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::metrics::cmd_metrics_quality(ws.as_deref(), days, json)
}
None => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::metrics::cmd_metrics(
ws.as_deref(),
days,
json,
force,
all_workspaces,
refresh,
source,
)
}
},
Command::Observe {
agent,
workspace,
project,
command,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::observe::cmd_observe(ws.as_deref(), &agent, &command)
}
Command::Sync {
subcmd:
SyncCommand::Run {
workspace,
project,
once,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::sync::cmd_sync_run(ws.as_deref(), once)
}
Command::Sync {
subcmd: SyncCommand::Status { workspace, project },
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::sync::cmd_sync_status(ws.as_deref())
}
Command::Telemetry { subcmd } => match subcmd {
TelemetrySubcommand::Init {
workspace,
project,
exporter_type,
path,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_init(
ws.as_deref(),
kaizen::shell::telemetry::ConfigureOptions {
exporter_type: exporter_type.map(|t| t.as_str().to_string()),
path,
..Default::default()
},
)
}
TelemetrySubcommand::Doctor { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_doctor(ws.as_deref())
}
TelemetrySubcommand::Pull {
days,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_pull(ws.as_deref(), days)
}
TelemetrySubcommand::Push {
days,
workspace,
project,
all_workspaces,
dry_run,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_push(
ws.as_deref(),
all_workspaces,
days,
dry_run,
)
}
TelemetrySubcommand::PrintSchema => {
kaizen::shell::telemetry::cmd_telemetry_print_schema()
}
TelemetrySubcommand::Configure {
workspace,
project,
exporter_type,
path,
api_key,
site,
host,
endpoint,
non_interactive,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_configure(
ws.as_deref(),
kaizen::shell::telemetry::ConfigureOptions {
exporter_type: exporter_type.map(|t| t.as_str().to_string()),
path,
api_key,
site,
host,
endpoint,
non_interactive,
},
)
}
TelemetrySubcommand::Test { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_test(ws.as_deref())
}
TelemetrySubcommand::PrintEffectiveConfig { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry::cmd_telemetry_print_effective(ws.as_deref())
}
TelemetrySubcommand::Tail {
workspace,
project,
file,
no_follow,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::telemetry_tail::cmd_telemetry_tail(
ws.as_deref(),
file,
no_follow,
json,
)
}
},
Command::Retro {
days,
dry_run,
json,
force,
workspace,
project,
refresh,
source,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::retro::cmd_retro(
ws.as_deref(),
days,
dry_run,
json,
force,
refresh,
source,
)
}
Command::Projects { subcmd } => match subcmd {
ProjectsCommand::List => kaizen::shell::projects::cmd_projects_list(),
},
Command::Exp { subcmd } => dispatch_exp(subcmd),
Command::Cases { subcmd } => dispatch_cases(subcmd),
Command::Rules { subcmd } => dispatch_rules(subcmd),
Command::Alerts { subcmd } => dispatch_alerts(subcmd),
Command::Review { subcmd } => dispatch_review(subcmd),
Command::Upgrade { from_source } => kaizen::shell::upgrade::cmd_upgrade(from_source),
Command::Mcp => {
// Requires multi-threaded runtime (rmcp + spawn_blocking in tools)
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
rt.block_on(kaizen::mcp::run_stdio_server())
}
Command::Proxy {
subcmd:
ProxyCommand::Run {
listen,
upstream,
provider,
workspace,
project,
},
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::proxy::cmd_proxy_run(ws.as_deref(), listen, upstream, provider)
}
Command::Eval { subcmd } => match subcmd {
EvalCommand::Run {
workspace,
project,
since_days,
dry_run,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::eval::cmd_eval_run(ws.as_deref(), since_days, dry_run, json)
}
EvalCommand::List {
workspace,
project,
min_score,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::eval::cmd_eval_list(ws.as_deref(), min_score, json)
}
EvalCommand::Prompt {
workspace,
project,
session_id,
rubric,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::eval::cmd_eval_prompt(ws.as_deref(), &session_id, &rubric)
}
},
Command::Prompt { subcmd } => match subcmd {
PromptCommand::List {
workspace,
project,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::prompt::cmd_prompt_list(ws.as_deref(), json)
}
PromptCommand::Show {
fingerprint,
workspace,
project,
json,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::prompt::cmd_prompt_show(&fingerprint, ws.as_deref(), json)
}
PromptCommand::Diff {
fingerprint_a,
fingerprint_b,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::prompt::cmd_prompt_diff(
&fingerprint_a,
&fingerprint_b,
ws.as_deref(),
)
}
},
Command::Outcomes { subcmd } => match subcmd {
OutcomesCommand::Show {
id,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
kaizen::shell::outcomes_cmd::cmd_outcomes_show(&id, ws.as_deref())
}
OutcomesCommand::Measure { workspace, session } => {
kaizen::shell::outcomes_cmd::cmd_outcomes_measure(&workspace, &session)
}
},
Command::SamplerRun {
workspace,
session,
pid,
} => kaizen::shell::sampler_cmd::cmd_sampler_run(&workspace, &session, pid),
}
}
fn dispatch_daemon(cmd: DaemonCommand) -> anyhow::Result<()> {
match cmd {
DaemonCommand::Start { background } => {
if !background {
return kaizen::daemon::start_foreground();
}
let started = kaizen::daemon::start_background()?;
if started.already_running {
println!("daemon already running");
} else {
println!("daemon started");
}
println!("pid: {}", started.pid);
println!("socket: {}", started.paths.sock.display());
println!("log: {}", started.paths.log.display());
Ok(())
}
DaemonCommand::Stop => {
println!("{}", kaizen::daemon::stop()?);
Ok(())
}
DaemonCommand::Status => {
match kaizen::daemon::status_outcome()? {
kaizen::daemon::DaemonStatusOutcome::Running(st) => {
println!("status: running");
println!("pid: {}", st.pid);
println!("uptime_ms: {}", st.uptime_ms);
println!("queue_depth: {}", st.queue_depth);
println!(
"last_error: {}",
st.last_error.unwrap_or_else(|| "-".to_string())
);
for capture in st.capture {
println!("capture: {}", capture.workspace);
println!(" deep: {}", capture.deep);
println!(" hooks: {}", capture.hooks.len());
println!(" watchers: {}", capture.watchers.len());
println!(" proxies: {}", capture.proxies.len());
if !capture.errors.is_empty() {
println!(" errors: {}", capture.errors.join("; "));
}
}
}
kaizen::daemon::DaemonStatusOutcome::Stopped { socket } => {
println!("status: stopped");
println!("socket: {}", socket.display());
}
}
Ok(())
}
}
}
fn ws(flags: &WorkspaceFlags) -> anyhow::Result<Option<PathBuf>> {
resolve_ws(flags.workspace.as_deref(), flags.project.as_deref())
}
fn dispatch_cases(cmd: CasesCommand) -> anyhow::Result<()> {
match cmd {
CasesCommand::Mine(a) => {
kaizen::shell::cases::cmd_cases_mine(ws(&a.ws)?.as_deref(), a.since.as_deref(), a.json)
}
CasesCommand::Create {
session,
reason,
label,
json,
ws: f,
} => kaizen::shell::cases::cmd_cases_create(
ws(&f)?.as_deref(),
&session,
&reason,
label,
json,
),
CasesCommand::List {
status,
json,
ws: f,
} => kaizen::shell::cases::cmd_cases_list(ws(&f)?.as_deref(), status, json),
CasesCommand::Show(a) => {
kaizen::shell::cases::cmd_cases_show(ws(&a.ws)?.as_deref(), &a.id, a.json)
}
CasesCommand::Archive(a) => {
kaizen::shell::cases::cmd_cases_archive(ws(&a.ws)?.as_deref(), &a.id)
}
}
}
fn dispatch_rules(cmd: RulesCommand) -> anyhow::Result<()> {
match cmd {
RulesCommand::Create {
name,
filter,
action,
message,
ws: f,
} => kaizen::shell::rules::cmd_rules_create(
ws(&f)?.as_deref(),
&name,
&filter,
&action,
message,
),
RulesCommand::List(a) => {
kaizen::shell::rules::cmd_rules_list(ws(&a.ws)?.as_deref(), a.json)
}
RulesCommand::Run {
since,
dry_run,
json,
ws: f,
} => {
kaizen::shell::rules::cmd_rules_run(ws(&f)?.as_deref(), since.as_deref(), dry_run, json)
}
RulesCommand::Enable(a) => {
kaizen::shell::rules::cmd_rules_enable(ws(&a.ws)?.as_deref(), &a.id, true)
}
RulesCommand::Disable(a) => {
kaizen::shell::rules::cmd_rules_enable(ws(&a.ws)?.as_deref(), &a.id, false)
}
}
}
fn dispatch_alerts(cmd: AlertsCommand) -> anyhow::Result<()> {
match cmd {
AlertsCommand::Check { days, json, ws: f } => {
kaizen::shell::alerts::cmd_alerts_check(ws(&f)?.as_deref(), days, json)
}
}
}
fn dispatch_review(cmd: ReviewCommand) -> anyhow::Result<()> {
match cmd {
ReviewCommand::List {
status,
json,
ws: f,
} => kaizen::shell::review::cmd_review_list(ws(&f)?.as_deref(), status, json),
ReviewCommand::Show(a) => {
kaizen::shell::review::cmd_review_show(ws(&a.ws)?.as_deref(), &a.id, a.json)
}
ReviewCommand::Resolve(a) => {
kaizen::shell::review::cmd_review_resolve(ws(&a.ws)?.as_deref(), &a.id)
}
ReviewCommand::Dismiss(a) => {
kaizen::shell::review::cmd_review_dismiss(ws(&a.ws)?.as_deref(), &a.id)
}
}
}
fn dispatch_exp(cmd: ExpCommand) -> anyhow::Result<()> {
use kaizen::shell::exp;
match cmd {
ExpCommand::New {
name,
hypothesis,
change,
metric,
bind,
duration_days,
target_pct,
control_commit,
treatment_commit,
control_branch,
treatment_branch,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_new(
ws.as_deref(),
exp::NewArgs {
name,
hypothesis,
change,
metric,
bind,
duration_days,
target_pct,
control_commit,
treatment_commit,
control_branch,
treatment_branch,
},
)
}
ExpCommand::Start {
id,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_start(ws.as_deref(), &id)
}
ExpCommand::List { workspace, project } => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_list(ws.as_deref())
}
ExpCommand::Status {
id,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_status(ws.as_deref(), &id)
}
ExpCommand::Tag {
id,
session,
variant,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_tag(ws.as_deref(), &id, &session, &variant)
}
ExpCommand::Report {
id,
json,
refresh,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_report(ws.as_deref(), &id, json, refresh)
}
ExpCommand::Conclude {
id,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_conclude(ws.as_deref(), &id)
}
ExpCommand::Archive {
id,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_archive(ws.as_deref(), &id)
}
ExpCommand::Power {
metric,
baseline_n,
refresh,
workspace,
project,
} => {
let ws = resolve_ws(workspace.as_deref(), project.as_deref())?;
exp::cmd_power(ws.as_deref(), &metric, baseline_n, refresh)
}
}
}
fn ingest_hook(source: Source, workspace: Option<PathBuf>) -> anyhow::Result<()> {
let mut input = String::new();
std::io::stdin().read_to_string(&mut input)?;
let src = match source {
Source::Cursor => kaizen::shell::ingest::IngestSource::Cursor,
Source::Claude => kaizen::shell::ingest::IngestSource::Claude,
Source::Vibe => kaizen::shell::ingest::IngestSource::Vibe,
};
if kaizen::daemon::enabled() {
let response = kaizen::daemon::request_blocking(kaizen::ipc::DaemonRequest::IngestHook {
source: src,
payload: input,
workspace: workspace.map(|p| {
kaizen::core::paths::canonical(&p)
.to_string_lossy()
.to_string()
}),
})?;
return match response {
kaizen::ipc::DaemonResponse::Ack { .. } => Ok(()),
kaizen::ipc::DaemonResponse::Error { message, .. } => Err(anyhow::anyhow!(message)),
_ => Err(anyhow::anyhow!("unexpected daemon ingest response")),
};
}
kaizen::shell::ingest::ingest_hook_text(src, &input, workspace)
}
#[cfg(test)]
mod cli_parser_tests {
use super::Cli;
use clap::CommandFactory;
#[test]
fn clap_cli_debug_assert() {
Cli::command().debug_assert();
}
}