mati 0.1.0

Engineering knowledge that survives turnover
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
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
//! Daemon IPC protocol v2 — wire types for the Unix socket boundary.
//!
//! All mutation commands are semantic (no raw `put`/`delete`). Trust-sensitive
//! fields (timestamps, confidence, quality, lifecycle) are daemon-controlled
//! and never cross the wire as client input.
//!
//! ## Wire format
//!
//! Framing: newline-delimited JSON. One JSON object per line, terminated by `\n`.
//! Request size is capped at [`MAX_FRAME_SIZE`] bytes (enforced by the server
//! before full buffering). Oversized requests receive [`ErrorCode::FrameTooLarge`].
//!
//! ## Security properties
//!
//! - All input DTOs use `#[serde(deny_unknown_fields)]`
//! - `Command` is a closed enum — unknown commands are rejected at decode
//! - Session UUID is required on every request (session marker, not auth)
//! - Request ID is correlation only, not idempotency
//!
//! ## Transaction model
//!
//! SurrealKV supports multi-key atomic transactions within a single tree.
//! The real constraint is mati's two-tree architecture: no single transaction
//! can span both the `knowledge` tree and the `sessions` tree.
//!
//! - Same-tree commands: mutation + audit committed in one transaction
//! - Mixed-tree commands: per-tree atomic batches with explicit substep audit

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::store::AgentKind;

// ── Protocol constants ──────────────────────────────────────────────────────

/// Protocol version. Bump on incompatible wire format changes.
/// v1: newline-delimited JSON, flat cmd/args
/// v2: newline-delimited JSON, typed Command enum, session UUID required,
///     request size capped at [`MAX_FRAME_SIZE`]
pub const PROTOCOL_VERSION: u16 = 2;

/// Maximum request size in bytes (including the trailing newline).
/// Enforced by `socket_handle_connection` via `AsyncReadExt::take` before
/// any JSON parsing occurs. Oversized requests receive
/// [`ErrorCode::FrameTooLarge`] without triggering handler side effects.
///
/// Chosen to comfortably fit the largest normal request (FileEnrich ~2-4 KiB)
/// with headroom, while rejecting pathological payloads.
pub const MAX_FRAME_SIZE: usize = 65_536;

// ── Request ─────────────────────────────────────────────────────────────────

/// Daemon IPC request. Deserialized from a bounded frame.
///
/// Unknown top-level fields are rejected. The `cmd` field is internally tagged
/// by `type`, and each command's input DTO independently rejects unknown fields.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Request {
    /// Protocol version — validated at the wire layer before dispatch.
    pub v: u16,
    /// Correlation ID — used to match responses to requests. Not idempotency.
    pub id: Uuid,
    /// Session UUID — required on every request. This is a session marker for
    /// audit/provenance, NOT an authentication token. Peer identity is
    /// established via Unix peer credentials (`peer_cred()`).
    pub session: Uuid,
    /// Client-declared agent identity for attribution (ADR-018).
    /// Optional and additive: pre-multi-agent clients omit this field;
    /// the daemon stamps `Unknown` server-side when absent. NOT verified —
    /// same-UID processes are trusted (THREAT_MODEL.md §3.I).
    #[serde(default)]
    pub agent: Option<AgentKind>,
    /// The command to execute.
    pub cmd: Command,
}

// ── Response ────────────────────────────────────────────────────────────────

/// Daemon IPC response. Serialized into a bounded frame.
#[derive(Debug, Serialize)]
#[serde(tag = "status")]
pub enum Response {
    /// Command succeeded. `data` contains the command-specific result.
    #[serde(rename = "ok")]
    Ok { id: Uuid, data: serde_json::Value },
    /// Command failed. `code` is a structured error code for programmatic
    /// handling; `message` is a human-readable description.
    #[serde(rename = "err")]
    Err {
        id: Uuid,
        code: ErrorCode,
        message: String,
    },
}

impl Response {
    /// Construct a success response.
    pub fn ok(id: Uuid, data: serde_json::Value) -> Self {
        Self::Ok { id, data }
    }

    /// Construct an error response.
    pub fn err(id: Uuid, code: ErrorCode, message: impl Into<String>) -> Self {
        Self::Err {
            id,
            code,
            message: message.into(),
        }
    }
}

// ── Error codes ─────────────────────────────────────────────────────────────

/// Structured error codes for programmatic handling by the CLI proxy.
///
/// Protocol-level errors (before dispatch):
/// - `VersionMismatch`, `FrameTooLarge`, `MalformedRequest`, `SessionMismatch`
///
/// Command-level errors (during dispatch):
/// - `ValidationFailed`, `NotFound`, `Conflict`, `InvalidStateTransition`,
///   `StoreError`, `Internal`
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ErrorCode {
    /// Request protocol version does not match daemon's PROTOCOL_VERSION.
    VersionMismatch,
    /// Request exceeds [`MAX_FRAME_SIZE`] bytes. Rejected before JSON parsing.
    FrameTooLarge,
    /// JSON parse error, unknown fields, or type mismatch.
    MalformedRequest,
    /// Request session UUID does not match daemon's current session.
    /// Client should re-read daemon metadata and retry once.
    SessionMismatch,
    /// Input validation failed (e.g., empty key, invalid slug, bad enum value).
    ValidationFailed,
    /// Referenced record does not exist.
    NotFound,
    /// Key collision (e.g., creating a gotcha that already exists).
    Conflict,
    /// State transition not allowed (e.g., confirming a tombstoned record).
    InvalidStateTransition,
    /// Underlying SurrealKV or tantivy error.
    StoreError,
    /// Unexpected internal error.
    Internal,
}

// ── Command enum ────────────────────────────────────────────────────────────

/// All commands available over the daemon IPC protocol.
///
/// Internally tagged by `"type"`. Each variant either has no arguments (unit)
/// or wraps a typed input DTO with `#[serde(deny_unknown_fields)]`.
///
/// There is no public `put` or `delete` command. All mutations are semantic.
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Command {
    // ── A. Pure reads ───────────────────────────────────────────────────
    /// Health check. No arguments.
    #[serde(rename = "ping")]
    Ping,

    /// Snapshot of live daemon metrics — per-command counters and latency
    /// percentiles. Pure read, no audit, no side effects.
    #[serde(rename = "metrics")]
    Metrics,

    /// Single record lookup by key.
    #[serde(rename = "get")]
    Get(GetInput),

    /// Bulk lookup for hook decision: file record + linked gotchas + consultation status.
    #[serde(rename = "hook_evaluate")]
    HookEvaluate(HookEvaluateInput),

    /// Scan all records whose key starts with a prefix.
    #[serde(rename = "scan_prefix")]
    ScanPrefix(ScanPrefixInput),

    /// Version history for a single key.
    #[serde(rename = "history")]
    History(HistoryInput),

    /// Version history for a single key since a timestamp.
    #[serde(rename = "history_since")]
    HistorySince(HistorySinceInput),

    /// Check whether a consultation receipt exists for a key.
    #[serde(rename = "session_check_consulted")]
    SessionCheckConsulted(SessionCheckConsultedInput),

    /// Check whether a recent consultation receipt exists (within TTL).
    #[serde(rename = "session_check_consulted_recent")]
    SessionCheckConsultedRecent(SessionCheckConsultedRecentInput),

    /// BM25 text search or graph traversal.
    #[serde(rename = "mem_query")]
    MemQuery(MemQueryInput),

    /// Scan enforcement events stored as raw JSON in the knowledge tree.
    #[serde(rename = "scan_enforcement_events")]
    ScanEnforcementEvents(ScanEnforcementEventsInput),

    /// Read a runtime configuration value (e.g. enforcement.mode).
    /// Pure read — no audit, no side effects.
    #[serde(rename = "config_get")]
    ConfigGet(ConfigGetInput),

    // ── B. Reads with audited side effects ──────────────────────────────
    /// Single record lookup with consultation receipt side effect.
    #[serde(rename = "mem_get")]
    MemGet(MemGetInput),

    /// Assemble a token-budgeted context packet for session startup.
    #[serde(rename = "mem_bootstrap")]
    MemBootstrap(MemBootstrapInput),

    // ── C. Semantic mutations ───────────────────────────────────────────
    /// Create or update a gotcha record. Always sets confirmed=false.
    #[serde(rename = "gotcha_upsert")]
    GotchaUpsert(GotchaDraftInput),

    /// Confirm a gotcha for hook enforcement. Sets confirmed=true.
    #[serde(rename = "gotcha_confirm")]
    GotchaConfirm(GotchaConfirmInput),

    /// Tombstone a gotcha and clean up file links + graph edges.
    #[serde(rename = "gotcha_tombstone")]
    GotchaTombstone(GotchaTombstoneInput),

    /// Enrich a file record with LLM-derived purpose, entry points, etc.
    /// File record must already exist (created by init/reparse).
    #[serde(rename = "file_enrich")]
    FileEnrich(FileEnrichInput),

    /// Re-analyze a file from disk and update structural fields.
    #[serde(rename = "file_reparse")]
    FileReparse(FileReparseInput),

    /// Post-edit hook compound: consultation hit + file reparse.
    #[serde(rename = "file_edit_hook")]
    FileEditHook(FileEditHookInput),

    /// Extract doc comment from file on disk and update file record purpose.
    #[serde(rename = "doc_capture")]
    DocCapture(DocCaptureInput),

    /// Create or update a decision record.
    #[serde(rename = "decision_upsert")]
    DecisionUpsert(DecisionUpsertInput),

    /// Create or update a dev note.
    #[serde(rename = "dev_note_upsert")]
    DevNoteUpsert(DevNoteUpsertInput),

    /// Write a runtime configuration value. Records an
    /// `EnforcementConfigChanged` event when the value actually changes.
    #[serde(rename = "config_set")]
    ConfigSet(ConfigSetInput),

    /// Append a session analytics event (6 homogeneous event types).
    #[serde(rename = "session_log")]
    SessionLog(SessionLogInput),

    /// Record a consultation hit: receipt + access metrics + daily agg.
    #[serde(rename = "consultation_hit")]
    ConsultationHit(ConsultationHitInput),

    /// Flush session data (collect consulted markers into session:current).
    #[serde(rename = "session_flush")]
    SessionFlush,

    /// Archive session, run promotions, collect stale reviews.
    #[serde(rename = "session_harvest")]
    SessionHarvest,

    /// Bulk-import a batch of pre-built `Record`s into the knowledge tree.
    /// Bypasses the semantic upsert handlers — records are written verbatim
    /// so an `export → import` round-trip preserves every field
    /// (`confirmed`, `source`, `confidence`, `lifecycle`, etc.) without
    /// the destructive resets the typed upsert commands apply.
    ///
    /// Only `gotcha:*`, `decision:*`, `dev_note:*`, `file:*`, `stage:*`,
    /// and `dep:*` keys are accepted (the knowledge-tree namespaces).
    /// Session-tree keys (`session:*`, `analytics:*`, `compliance:*`,
    /// `audit:*`) are rejected at the boundary — those are daemon-owned
    /// telemetry that an `export` should never round-trip.
    #[serde(rename = "record_import")]
    RecordImport(RecordImportInput),
}

// ── Input DTOs ──────────────────────────────────────────────────────────────
//
// Each DTO uses `deny_unknown_fields` so extra fields from a malicious or
// misconfigured client are rejected at decode time, not silently dropped.

// ── A. Pure read inputs ─────────────────────────────────────────────────────

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GetInput {
    pub key: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HookEvaluateInput {
    pub file_key: String,
    #[serde(default)]
    pub include_recent: bool,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ScanPrefixInput {
    pub prefix: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ScanEnforcementEventsInput {
    #[serde(default)]
    pub since_seq: u64,
    #[serde(default = "default_until_seq")]
    pub until_seq: u64,
}

fn default_until_seq() -> u64 {
    u64::MAX
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HistoryInput {
    pub key: String,
    #[serde(default = "default_history_limit")]
    pub limit: u64,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HistorySinceInput {
    pub key: String,
    pub since_ts: u64,
    #[serde(default = "default_history_limit")]
    pub limit: u64,
}

fn default_history_limit() -> u64 {
    50
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionCheckConsultedInput {
    pub key: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionCheckConsultedRecentInput {
    pub key: String,
    #[serde(default = "default_ttl_secs")]
    pub ttl_secs: u64,
}

fn default_ttl_secs() -> u64 {
    900
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MemQueryInput {
    pub query: String,
    #[serde(default = "default_query_mode")]
    pub mode: QueryMode,
    #[serde(default = "default_query_limit")]
    pub limit: u32,
}

fn default_query_mode() -> QueryMode {
    QueryMode::Text
}

fn default_query_limit() -> u32 {
    20
}

/// Search mode for mem_query.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum QueryMode {
    /// BM25 full-text search over record keys, values, and tags.
    Text,
    /// Filter records by tag (substring, case-insensitive).
    Tag,
    /// 1-hop graph traversal from a seed key.
    Graph,
    /// Semantic search (requires --features semantic).
    Semantic,
}

// ── B. Read-with-side-effect inputs ─────────────────────────────────────────

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MemGetInput {
    pub key: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MemBootstrapInput {
    #[serde(default)]
    pub context_files: Vec<String>,
}

// ── C. Semantic mutation inputs ─────────────────────────────────────────────

/// Gotcha creation/update input. The client expresses intent only — the daemon
/// derives confirmation state, confidence, quality, timestamps, and version.
///
/// Confirmation is ALWAYS reset to `false` on upsert. Use `GotchaConfirm`
/// to re-confirm after editing.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GotchaDraftInput {
    /// Gotcha key, must match `gotcha:<slug>`.
    pub key: String,
    /// Actionable rule text (imperative verb).
    pub rule: String,
    /// Causality sentence explaining why this rule exists.
    pub reason: String,
    /// Severity level.
    pub severity: Severity,
    /// File paths this gotcha applies to.
    #[serde(default)]
    pub affected_files: Vec<String>,
    /// Optional external reference URL.
    #[serde(default)]
    pub ref_url: Option<String>,
    /// Optional tags.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Record-level priority.
    #[serde(default)]
    pub priority: Priority,
    /// Record source — when set, the handler uses this instead of defaulting
    /// to `ClaudeEnrich`. CLI `gotcha add` sends `DeveloperManual` here.
    #[serde(default)]
    pub source: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GotchaConfirmInput {
    pub key: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct GotchaTombstoneInput {
    pub key: String,
}

/// File enrichment input from LLM analysis (e.g., /mati-enrich workflow).
/// The file record must already exist (created by init/reparse).
///
/// Fields that are daemon-managed and MUST NOT appear:
/// - `gotcha_keys` (managed by gotcha lifecycle commands)
/// - `imports` (derived from tree-sitter)
/// - All structural/internal fields (unsafe_count, unwrap_count, etc.)
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileEnrichInput {
    /// File path (maps to `file:<path>`).
    pub path: String,
    /// Purpose sentence (verb-led).
    pub purpose: String,
    /// Function/method entry points identified by enrichment.
    #[serde(default)]
    pub entry_points: Vec<String>,
    /// Decision records that affect this file.
    #[serde(default)]
    pub decision_keys: Vec<String>,
    /// TODO items found during enrichment.
    #[serde(default)]
    pub todos: Vec<String>,
    /// Optional tags.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Record-level priority.
    #[serde(default)]
    pub priority: Priority,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileReparseInput {
    pub path: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileEditHookInput {
    pub path: String,
}

/// Path-only doc capture. The daemon reads the file from disk and extracts
/// the doc comment — no content crosses the wire.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DocCaptureInput {
    pub path: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DecisionUpsertInput {
    /// Key slug (daemon prepends `decision:`).
    pub slug: String,
    /// Human-readable summary ("We use X because Y").
    pub value: String,
    /// Concise decision summary (payload field).
    pub summary: String,
    /// Rationale text (payload field).
    pub rationale: String,
    /// Optional tags.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Record-level priority.
    #[serde(default)]
    pub priority: Priority,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DevNoteUpsertInput {
    /// If absent, daemon auto-generates `dev_note:<slug>-<timestamp>`.
    /// If present, must match an existing `dev_note:*` key (update mode).
    #[serde(default)]
    pub key: Option<String>,
    /// Freeform note text.
    pub text: String,
    /// Optional tags.
    #[serde(default)]
    pub tags: Vec<String>,
    /// Record-level priority.
    #[serde(default)]
    pub priority: Priority,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SessionLogInput {
    /// The event type (closed enum, 6 variants).
    pub event: SessionEvent,
    /// The record key this event pertains to.
    pub key: String,
}

/// Session analytics event types. Each maps to a daily aggregation key prefix.
///
/// `Hit` is NOT included — it has richer side effects and uses the separate
/// `ConsultationHit` command.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum SessionEvent {
    Miss,
    ComplianceMiss,
    ComplianceHit,
    CodexShellMiss,
    Bootstrap,
    PromptNudge,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConsultationHitInput {
    pub key: String,
}

/// Input for `Command::RecordImport`. Records are written verbatim into the
/// knowledge tree, preserving every field. The daemon validates each record's
/// key prefix against the knowledge-namespace allowlist before writing.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RecordImportInput {
    pub records: Vec<crate::store::Record>,
}

/// Input for `Command::ConfigGet`. `key` is the dotted config name
/// (e.g. `enforcement.mode`, `enforcement.retention`).
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigGetInput {
    pub key: String,
}

/// Input for `Command::ConfigSet`. Values are always sent as strings on the
/// wire and parsed/validated by the dispatcher.
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigSetInput {
    pub key: String,
    pub value: String,
}

// ── Shared enums ────────────────────────────────────────────────────────────

/// Severity level for gotcha records. Closed enum.
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    Critical,
    High,
    #[default]
    Normal,
    Low,
}

/// Record-level priority. Closed enum.
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Priority {
    Critical,
    High,
    #[default]
    Normal,
    Low,
}

// ── Conversions from store types ────────────────────────────────────────────

impl From<crate::store::Priority> for Severity {
    fn from(p: crate::store::Priority) -> Self {
        match p {
            crate::store::Priority::Low => Severity::Low,
            crate::store::Priority::Normal => Severity::Normal,
            crate::store::Priority::High => Severity::High,
            crate::store::Priority::Critical => Severity::Critical,
        }
    }
}

impl From<crate::store::Priority> for Priority {
    fn from(p: crate::store::Priority) -> Self {
        match p {
            crate::store::Priority::Low => Priority::Low,
            crate::store::Priority::Normal => Priority::Normal,
            crate::store::Priority::High => Priority::High,
            crate::store::Priority::Critical => Priority::Critical,
        }
    }
}

// ── Command helpers ──────────────────────────────────────────────────────────

impl Command {
    /// Returns the serde rename string for this command variant.
    /// Used for audit logging and tracing spans.
    pub fn kind(&self) -> &'static str {
        match self {
            Self::Ping => "ping",
            Self::Metrics => "metrics",
            Self::Get(_) => "get",
            Self::HookEvaluate(_) => "hook_evaluate",
            Self::ScanPrefix(_) => "scan_prefix",
            Self::History(_) => "history",
            Self::HistorySince(_) => "history_since",
            Self::SessionCheckConsulted(_) => "session_check_consulted",
            Self::SessionCheckConsultedRecent(_) => "session_check_consulted_recent",
            Self::MemQuery(_) => "mem_query",
            Self::ScanEnforcementEvents(_) => "scan_enforcement_events",
            Self::ConfigGet(_) => "config_get",
            Self::ConfigSet(_) => "config_set",
            Self::MemGet(_) => "mem_get",
            Self::MemBootstrap(_) => "mem_bootstrap",
            Self::GotchaUpsert(_) => "gotcha_upsert",
            Self::GotchaConfirm(_) => "gotcha_confirm",
            Self::GotchaTombstone(_) => "gotcha_tombstone",
            Self::FileEnrich(_) => "file_enrich",
            Self::FileReparse(_) => "file_reparse",
            Self::FileEditHook(_) => "file_edit_hook",
            Self::DocCapture(_) => "doc_capture",
            Self::DecisionUpsert(_) => "decision_upsert",
            Self::DevNoteUpsert(_) => "dev_note_upsert",
            Self::SessionLog(_) => "session_log",
            Self::ConsultationHit(_) => "consultation_hit",
            Self::SessionFlush => "session_flush",
            Self::SessionHarvest => "session_harvest",
            Self::RecordImport(_) => "record_import",
        }
    }

    /// Returns the primary target key for this command, if applicable.
    /// Used for audit trail correlation.
    pub fn target_key(&self) -> &str {
        match self {
            Self::Get(i) => &i.key,
            Self::HookEvaluate(i) => &i.file_key,
            Self::ScanPrefix(i) => &i.prefix,
            Self::History(i) => &i.key,
            Self::HistorySince(i) => &i.key,
            Self::SessionCheckConsulted(i) => &i.key,
            Self::SessionCheckConsultedRecent(i) => &i.key,
            Self::MemQuery(i) => &i.query,
            Self::MemGet(i) => &i.key,
            Self::GotchaUpsert(i) => &i.key,
            Self::GotchaConfirm(i) => &i.key,
            Self::GotchaTombstone(i) => &i.key,
            Self::FileEnrich(i) => &i.path,
            Self::FileReparse(i) => &i.path,
            Self::FileEditHook(i) => &i.path,
            Self::DocCapture(i) => &i.path,
            Self::DecisionUpsert(i) => &i.slug,
            Self::DevNoteUpsert(i) => i.key.as_deref().unwrap_or(""),
            Self::SessionLog(i) => &i.key,
            Self::ConsultationHit(i) => &i.key,
            Self::ConfigGet(i) => &i.key,
            Self::ConfigSet(i) => &i.key,
            Self::Ping
            | Self::Metrics
            | Self::MemBootstrap(_)
            | Self::ScanEnforcementEvents(_)
            | Self::SessionFlush
            | Self::SessionHarvest
            | Self::RecordImport(_) => "",
        }
    }

    /// Returns true for commands that mutate state (categories B and C).
    ///
    /// Category B (reads with audited side effects): MemGet, MemBootstrap
    /// Category C (semantic mutations): all 13 mutation commands
    ///
    /// Audit entries are written for all of these.
    pub fn is_mutation(&self) -> bool {
        matches!(
            self,
            // B. Reads with audited side effects
            Self::MemGet(_)
            | Self::MemBootstrap(_)
            // C. Semantic mutations
            | Self::GotchaUpsert(_)
            | Self::GotchaConfirm(_)
            | Self::GotchaTombstone(_)
            | Self::FileEnrich(_)
            | Self::FileReparse(_)
            | Self::FileEditHook(_)
            | Self::DocCapture(_)
            | Self::DecisionUpsert(_)
            | Self::DevNoteUpsert(_)
            | Self::SessionLog(_)
            | Self::ConsultationHit(_)
            | Self::ConfigSet(_)
            | Self::SessionFlush
            | Self::SessionHarvest
            | Self::RecordImport(_)
        )
    }
}

// ── Audit ───────────────────────────────────────────────────────────────────

/// Audit trail entry for commands dispatched through the v2 protocol.
///
/// Written to the sessions tree under `session:audit:<timestamp_ns>`.
/// Lightweight struct — not a full `Record` — to keep audit writes cheap.
///
/// Every mutating command (categories B and C) produces an audit entry.
/// Rejected commands (validation failure, version mismatch) also produce
/// an entry with `accepted = false`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEntry {
    /// Wall-clock timestamp (seconds since epoch).
    pub ts: u64,
    /// Effective UID of the peer that sent the command.
    pub peer_uid: u32,
    /// PID of the peer process (None on platforms that don't expose it).
    pub peer_pid: Option<u32>,
    /// Daemon session UUID — correlates entries within one daemon lifetime.
    pub daemon_session: Uuid,
    /// Request correlation ID from the v2 protocol.
    pub request_id: Uuid,
    /// Command kind string (e.g., "gotcha_upsert", "file_enrich").
    pub command_kind: String,
    /// Primary key affected by this command (empty for unit commands).
    pub target_key: String,
    /// Whether the command was accepted (dispatched to handler) or rejected.
    pub accepted: bool,
    /// Error code if rejected, None if accepted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error_code: Option<ErrorCode>,
}

// ── V1→V2 command mapping ───────────────────────────────────────────────────
//
// Used by the CLI proxy and MCP proxy to convert legacy v1-style (cmd, args)
// calls into v2 Command JSON. This is a transitional bridge — callers that
// are updated to construct typed Commands directly do not need this.

/// Map a v1-style `(cmd_str, args_json)` pair to a v2 Command JSON object.
///
/// **Pure reads only.** All mutation and side-effecting-read callers have been
/// migrated to construct typed `protocol::Command` values directly via
/// `daemon_v2()`. This function is retained only for pure-read commands used
/// by `daemon_result()` and `proxy_daemon_result()`.
///
/// Panics in debug builds if called with a mutation or side-effecting command.
pub fn v1_to_v2_command(cmd: &str, args: &serde_json::Value) -> serde_json::Value {
    use serde_json::json;

    match cmd {
        // Pure reads — the only commands that still use this mapping.
        "ping" => json!({"type": "ping"}),
        "metrics" => json!({"type": "metrics"}),
        "get" => json!({"type": "get", "key": args["key"]}),
        "hook_evaluate" => json!({
            "type": "hook_evaluate",
            "file_key": args["file_key"],
            "include_recent": args.get("include_recent").and_then(|v| v.as_bool()).unwrap_or(false),
        }),
        "scan_prefix" => json!({"type": "scan_prefix", "prefix": args["prefix"]}),
        "history" => {
            json!({"type": "history", "key": args["key"], "limit": args.get("limit").and_then(|v| v.as_u64()).unwrap_or(50)})
        }
        "history_since" => json!({
            "type": "history_since",
            "key": args["key"],
            "since_ts": args.get("since_ts").and_then(|v| v.as_u64()).unwrap_or(0),
            "limit": args.get("limit").and_then(|v| v.as_u64()).unwrap_or(50),
        }),
        "session_check_consulted" => json!({"type": "session_check_consulted", "key": args["key"]}),
        "session_check_consulted_recent" => json!({
            "type": "session_check_consulted_recent",
            "key": args["key"],
            "ttl_secs": args.get("ttl_secs").and_then(|v| v.as_u64()).unwrap_or(900),
        }),
        "mem_query" => json!({
            "type": "mem_query",
            "query": args["query"],
            "mode": args.get("mode").and_then(|v| v.as_str()).unwrap_or("text"),
            "limit": args.get("limit").and_then(|v| v.as_u64()).unwrap_or(20),
        }),
        "scan_enforcement_events" => json!({
            "type": "scan_enforcement_events",
            "since_seq": args.get("since_seq").and_then(|v| v.as_u64()).unwrap_or(0),
            "until_seq": args.get("until_seq").and_then(|v| v.as_u64()).unwrap_or(u64::MAX),
        }),
        // Side-effecting reads — pure read shape on the wire, sessions-tree
        // side effects (consultation receipt, audit) live entirely on the
        // daemon side. Routing these through the typed Command enum is
        // strictly preferable, but the MCP Socket-backend tools.rs paths
        // call into this mapper today; without these arms every mem_get /
        // mem_bootstrap call against a Socket-mode `mati serve` panics the
        // rmcp task and surfaces as `Transport closed` to the client.
        "mem_get" => json!({"type": "mem_get", "key": args["key"]}),
        "mem_bootstrap" => json!({
            "type": "mem_bootstrap",
            "context_files": args.get("context_files").cloned().unwrap_or_else(|| serde_json::json!([])),
        }),
        other => {
            panic!(
                "v1_to_v2_command called with unsupported command '{other}' — \
                 only pure reads are supported; mutation/side-effecting callers \
                 must use daemon_v2() with typed Command"
            );
        }
    }
}

// ── Tests ───────────────────────────────────────────────────────────────────

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

    // ── Wire / protocol ─────────────────────────────────────────────────

    /// γ-C3a: QueryMode owns string-to-enum validation at the protocol
    /// boundary now that tools::mem_query no longer accepts a free-form
    /// string. Pin the unknown-variant rejection so future schema changes
    /// don't silently accept invalid modes.
    #[test]
    fn query_mode_deserialize_rejects_unknown_variant() {
        let result: Result<QueryMode, _> = serde_json::from_str("\"invalid_mode\"");
        assert!(
            result.is_err(),
            "QueryMode deserialization must reject unknown variants, got: {result:?}"
        );
    }

    #[test]
    fn query_mode_deserialize_accepts_all_known_variants() {
        // Snake-case wire form per `#[serde(rename_all = "snake_case")]`.
        for variant in &["text", "tag", "graph", "semantic"] {
            let json = format!("\"{variant}\"");
            let result: Result<QueryMode, _> = serde_json::from_str(&json);
            assert!(
                result.is_ok(),
                "QueryMode must accept {variant:?}, got: {result:?}"
            );
        }
    }

    #[test]
    fn valid_v2_ping_request_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        assert_eq!(req.v, PROTOCOL_VERSION);
        assert!(matches!(req.cmd, Command::Ping));
    }

    #[test]
    fn valid_v2_get_request_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "get", "key": "file:src/main.rs" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::Get(input) => assert_eq!(input.key, "file:src/main.rs"),
            _ => panic!("expected Get"),
        }
    }

    #[test]
    fn valid_gotcha_upsert_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "gotcha_upsert",
                "key": "gotcha:stripe-idempotency",
                "rule": "Always include an idempotency key",
                "reason": "Stripe retries without it cause double charges",
                "severity": "high",
                "affected_files": ["src/payments/stripe.rs"],
                "tags": ["payments", "stripe"]
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::GotchaUpsert(input) => {
                assert_eq!(input.key, "gotcha:stripe-idempotency");
                assert_eq!(input.severity, Severity::High);
                assert_eq!(input.affected_files, vec!["src/payments/stripe.rs"]);
                assert_eq!(input.priority, Priority::Normal); // default
            }
            _ => panic!("expected GotchaUpsert"),
        }
    }

    #[test]
    fn valid_decision_upsert_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "decision_upsert",
                "slug": "unified-retry-strategy",
                "value": "We use exponential backoff because linear retry overloads downstream",
                "summary": "Exponential backoff for all retries",
                "rationale": "Linear retry caused cascading failures in prod 2024-01"
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::DecisionUpsert(input) => {
                assert_eq!(input.slug, "unified-retry-strategy");
                assert!(!input.rationale.is_empty());
            }
            _ => panic!("expected DecisionUpsert"),
        }
    }

    #[test]
    fn valid_session_log_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "session_log",
                "event": "compliance_miss",
                "key": "file:src/main.rs"
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::SessionLog(input) => {
                assert_eq!(input.event, SessionEvent::ComplianceMiss);
                assert_eq!(input.key, "file:src/main.rs");
            }
            _ => panic!("expected SessionLog"),
        }
    }

    #[test]
    fn valid_file_enrich_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "file_enrich",
                "path": "src/store/db.rs",
                "purpose": "Own the storage boundary for all SurrealKV operations",
                "entry_points": ["open", "put", "get"],
                "decision_keys": ["decision:storage-engine"]
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::FileEnrich(input) => {
                assert_eq!(input.path, "src/store/db.rs");
                assert_eq!(input.entry_points.len(), 3);
                assert!(input.todos.is_empty()); // default
            }
            _ => panic!("expected FileEnrich"),
        }
    }

    // ── Rejection tests ─────────────────────────────────────────────────

    #[test]
    fn bad_version_still_decodes_for_error_handling() {
        // v=99 is parseable but the handler must reject it after decode.
        let json = serde_json::json!({
            "v": 99,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        assert_ne!(req.v, PROTOCOL_VERSION);
    }

    #[test]
    fn unknown_field_in_request_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" },
            "extra_field": true
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(result.is_err(), "unknown top-level field must be rejected");
    }

    #[test]
    fn unknown_field_in_command_args_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "get", "key": "file:foo", "smuggled": true }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "unknown field in command args must be rejected"
        );
    }

    #[test]
    fn unknown_command_type_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "raw_put", "key": "gotcha:x", "value": "hacked" }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(result.is_err(), "unknown command type must be rejected");
    }

    #[test]
    fn malformed_uuid_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "not-a-uuid",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(result.is_err(), "malformed UUID must be rejected");
    }

    #[test]
    fn missing_session_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(result.is_err(), "missing session UUID must be rejected");
    }

    #[test]
    fn gotcha_upsert_rejects_server_owned_fields() {
        // Attempt to smuggle `confirmed` through the wire
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "gotcha_upsert",
                "key": "gotcha:test",
                "rule": "test rule",
                "reason": "test reason",
                "severity": "normal",
                "confirmed": true
            }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "server-owned field `confirmed` must be rejected"
        );
    }

    #[test]
    fn file_enrich_rejects_gotcha_keys() {
        // gotcha_keys is daemon-managed, must not cross the wire
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "file_enrich",
                "path": "src/main.rs",
                "purpose": "entry point",
                "gotcha_keys": ["gotcha:smuggled"]
            }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "daemon-managed field `gotcha_keys` must be rejected"
        );
    }

    #[test]
    fn file_enrich_rejects_imports() {
        // imports is daemon-derived from tree-sitter
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "file_enrich",
                "path": "src/main.rs",
                "purpose": "entry point",
                "imports": ["std::io"]
            }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "daemon-derived field `imports` must be rejected"
        );
    }

    #[test]
    fn invalid_severity_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "gotcha_upsert",
                "key": "gotcha:test",
                "rule": "test",
                "reason": "test",
                "severity": "EXTREME"
            }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "invalid severity enum value must be rejected"
        );
    }

    #[test]
    fn invalid_session_event_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "session_log",
                "event": "hit",
                "key": "file:foo"
            }
        });
        let result = serde_json::from_value::<Request>(json);
        assert!(
            result.is_err(),
            "hit is not a SessionEvent variant — must use consultation_hit command"
        );
    }

    // ── Response serialization ──────────────────────────────────────────

    #[test]
    fn ok_response_serializes() {
        let id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let resp = Response::ok(id, serde_json::json!({"pong": true}));
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["status"], "ok");
        assert_eq!(json["data"]["pong"], true);
    }

    #[test]
    fn err_response_serializes_with_code() {
        let id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let resp = Response::err(id, ErrorCode::ValidationFailed, "key must not be empty");
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["status"], "err");
        assert_eq!(json["code"], "validation_failed");
        assert_eq!(json["message"], "key must not be empty");
    }

    #[test]
    fn error_code_roundtrips() {
        let codes = vec![
            ErrorCode::VersionMismatch,
            ErrorCode::FrameTooLarge,
            ErrorCode::MalformedRequest,
            ErrorCode::SessionMismatch,
            ErrorCode::ValidationFailed,
            ErrorCode::NotFound,
            ErrorCode::Conflict,
            ErrorCode::InvalidStateTransition,
            ErrorCode::StoreError,
            ErrorCode::Internal,
        ];
        for code in codes {
            let json = serde_json::to_value(&code).unwrap();
            let back: ErrorCode = serde_json::from_value(json).unwrap();
            assert_eq!(back, code);
        }
    }

    // ── Unit variant commands ───────────────────────────────────────────

    #[test]
    fn session_flush_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "session_flush" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        assert!(matches!(req.cmd, Command::SessionFlush));
    }

    #[test]
    fn session_harvest_decodes() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "session_harvest" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        assert!(matches!(req.cmd, Command::SessionHarvest));
    }

    #[test]
    fn dev_note_upsert_create_mode() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "dev_note_upsert",
                "text": "Remember to update the changelog"
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::DevNoteUpsert(input) => {
                assert!(input.key.is_none()); // create mode
                assert_eq!(input.text, "Remember to update the changelog");
            }
            _ => panic!("expected DevNoteUpsert"),
        }
    }

    #[test]
    fn dev_note_upsert_update_mode() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": {
                "type": "dev_note_upsert",
                "key": "dev_note:changelog-reminder-1712345678",
                "text": "Updated: remember to update changelog AND version"
            }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        match req.cmd {
            Command::DevNoteUpsert(input) => {
                assert_eq!(
                    input.key.as_deref(),
                    Some("dev_note:changelog-reminder-1712345678")
                );
            }
            _ => panic!("expected DevNoteUpsert"),
        }
    }

    // ── Command helper tests ────────────────────────────────────────────

    #[test]
    fn command_kind_covers_all_variants() {
        // Build one instance of each variant and verify kind() matches serde rename.
        let cases: Vec<(&str, Command)> = vec![
            ("ping", Command::Ping),
            ("metrics", Command::Metrics),
            ("get", Command::Get(GetInput { key: "k".into() })),
            (
                "hook_evaluate",
                Command::HookEvaluate(HookEvaluateInput {
                    file_key: "f".into(),
                    include_recent: false,
                }),
            ),
            (
                "scan_prefix",
                Command::ScanPrefix(ScanPrefixInput { prefix: "p".into() }),
            ),
            (
                "history",
                Command::History(HistoryInput {
                    key: "k".into(),
                    limit: 10,
                }),
            ),
            (
                "history_since",
                Command::HistorySince(HistorySinceInput {
                    key: "k".into(),
                    since_ts: 0,
                    limit: 10,
                }),
            ),
            (
                "session_check_consulted",
                Command::SessionCheckConsulted(SessionCheckConsultedInput { key: "k".into() }),
            ),
            (
                "session_check_consulted_recent",
                Command::SessionCheckConsultedRecent(SessionCheckConsultedRecentInput {
                    key: "k".into(),
                    ttl_secs: 900,
                }),
            ),
            (
                "mem_query",
                Command::MemQuery(MemQueryInput {
                    query: "q".into(),
                    mode: QueryMode::Text,
                    limit: 20,
                }),
            ),
            ("mem_get", Command::MemGet(MemGetInput { key: "k".into() })),
            (
                "mem_bootstrap",
                Command::MemBootstrap(MemBootstrapInput {
                    context_files: vec![],
                }),
            ),
            (
                "gotcha_upsert",
                Command::GotchaUpsert(GotchaDraftInput {
                    key: "gotcha:t".into(),
                    rule: "r".into(),
                    reason: "r".into(),
                    severity: Severity::Normal,
                    affected_files: vec![],
                    ref_url: None,
                    tags: vec![],
                    priority: Priority::Normal,
                    source: None,
                }),
            ),
            (
                "gotcha_confirm",
                Command::GotchaConfirm(GotchaConfirmInput {
                    key: "gotcha:t".into(),
                }),
            ),
            (
                "gotcha_tombstone",
                Command::GotchaTombstone(GotchaTombstoneInput {
                    key: "gotcha:t".into(),
                }),
            ),
            (
                "file_enrich",
                Command::FileEnrich(FileEnrichInput {
                    path: "p".into(),
                    purpose: "p".into(),
                    entry_points: vec![],
                    decision_keys: vec![],
                    todos: vec![],
                    tags: vec![],
                    priority: Priority::Normal,
                }),
            ),
            (
                "file_reparse",
                Command::FileReparse(FileReparseInput { path: "p".into() }),
            ),
            (
                "file_edit_hook",
                Command::FileEditHook(FileEditHookInput { path: "p".into() }),
            ),
            (
                "doc_capture",
                Command::DocCapture(DocCaptureInput { path: "p".into() }),
            ),
            (
                "decision_upsert",
                Command::DecisionUpsert(DecisionUpsertInput {
                    slug: "s".into(),
                    value: "v".into(),
                    summary: "s".into(),
                    rationale: "r".into(),
                    tags: vec![],
                    priority: Priority::Normal,
                }),
            ),
            (
                "dev_note_upsert",
                Command::DevNoteUpsert(DevNoteUpsertInput {
                    key: None,
                    text: "t".into(),
                    tags: vec![],
                    priority: Priority::Normal,
                }),
            ),
            (
                "session_log",
                Command::SessionLog(SessionLogInput {
                    event: SessionEvent::Miss,
                    key: "k".into(),
                }),
            ),
            (
                "consultation_hit",
                Command::ConsultationHit(ConsultationHitInput { key: "k".into() }),
            ),
            ("session_flush", Command::SessionFlush),
            ("session_harvest", Command::SessionHarvest),
        ];

        assert_eq!(cases.len(), 25, "must cover all 25 command variants");
        for (expected_kind, cmd) in &cases {
            assert_eq!(
                cmd.kind(),
                *expected_kind,
                "kind() mismatch for {:?}",
                expected_kind
            );
        }
    }

    #[test]
    fn command_is_mutation_classification() {
        // Pure reads — must NOT be mutations
        assert!(!Command::Ping.is_mutation());
        assert!(!Command::Metrics.is_mutation());
        assert!(!Command::Get(GetInput { key: "k".into() }).is_mutation());
        assert!(!Command::MemQuery(MemQueryInput {
            query: "q".into(),
            mode: QueryMode::Text,
            limit: 20,
        })
        .is_mutation());

        // Reads with side effects — ARE mutations (audited)
        assert!(Command::MemGet(MemGetInput { key: "k".into() }).is_mutation());
        assert!(Command::MemBootstrap(MemBootstrapInput {
            context_files: vec![]
        })
        .is_mutation());

        // Semantic mutations — ARE mutations
        assert!(Command::GotchaConfirm(GotchaConfirmInput {
            key: "gotcha:t".into()
        })
        .is_mutation());
        assert!(Command::SessionLog(SessionLogInput {
            event: SessionEvent::Miss,
            key: "k".into(),
        })
        .is_mutation());
        assert!(Command::SessionFlush.is_mutation());
        assert!(Command::SessionHarvest.is_mutation());
    }

    #[test]
    fn command_target_key_returns_expected_values() {
        assert_eq!(Command::Ping.target_key(), "");
        assert_eq!(
            Command::Get(GetInput {
                key: "file:src/main.rs".into()
            })
            .target_key(),
            "file:src/main.rs"
        );
        assert_eq!(
            Command::GotchaUpsert(GotchaDraftInput {
                key: "gotcha:test".into(),
                rule: "r".into(),
                reason: "r".into(),
                severity: Severity::Normal,
                affected_files: vec![],
                ref_url: None,
                tags: vec![],
                priority: Priority::Normal,
                source: None,
            })
            .target_key(),
            "gotcha:test"
        );
        assert_eq!(
            Command::DecisionUpsert(DecisionUpsertInput {
                slug: "my-decision".into(),
                value: "v".into(),
                summary: "s".into(),
                rationale: "r".into(),
                tags: vec![],
                priority: Priority::Normal,
            })
            .target_key(),
            "my-decision"
        );
        // DevNoteUpsert in create mode — no key
        assert_eq!(
            Command::DevNoteUpsert(DevNoteUpsertInput {
                key: None,
                text: "t".into(),
                tags: vec![],
                priority: Priority::Normal,
            })
            .target_key(),
            ""
        );
        assert_eq!(Command::SessionFlush.target_key(), "");
    }

    #[test]
    fn audit_entry_serializes() {
        let entry = AuditEntry {
            ts: 1700000000,
            peer_uid: 501,
            peer_pid: Some(1234),
            daemon_session: Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(),
            request_id: Uuid::parse_str("660e8400-e29b-41d4-a716-446655440000").unwrap(),
            command_kind: "gotcha_upsert".into(),
            target_key: "gotcha:test".into(),
            accepted: true,
            error_code: None,
        };
        let json = serde_json::to_value(&entry).unwrap();
        assert_eq!(json["peer_uid"], 501);
        assert_eq!(json["command_kind"], "gotcha_upsert");
        assert_eq!(json["accepted"], true);
        // error_code should be absent (skip_serializing_if)
        assert!(json.get("error_code").is_none());
    }

    #[test]
    fn audit_entry_rejected_includes_error_code() {
        let entry = AuditEntry {
            ts: 1700000000,
            peer_uid: 501,
            peer_pid: None,
            daemon_session: Uuid::nil(),
            request_id: Uuid::nil(),
            command_kind: "gotcha_confirm".into(),
            target_key: "gotcha:missing".into(),
            accepted: false,
            error_code: Some(ErrorCode::NotFound),
        };
        let json = serde_json::to_value(&entry).unwrap();
        assert_eq!(json["accepted"], false);
        assert_eq!(json["error_code"], "not_found");
        assert!(json["peer_pid"].is_null());
    }

    // ── store::Priority → protocol type conversions ────────────────────

    #[test]
    fn store_priority_to_protocol_severity_preserves_all_variants() {
        use crate::store::Priority as SP;
        assert_eq!(Severity::from(SP::Low), Severity::Low);
        assert_eq!(Severity::from(SP::Normal), Severity::Normal);
        assert_eq!(Severity::from(SP::High), Severity::High);
        assert_eq!(Severity::from(SP::Critical), Severity::Critical);
    }

    #[test]
    fn store_priority_to_protocol_priority_preserves_all_variants() {
        use crate::store::Priority as SP;
        assert_eq!(Priority::from(SP::Low), Priority::Low);
        assert_eq!(Priority::from(SP::Normal), Priority::Normal);
        assert_eq!(Priority::from(SP::High), Priority::High);
        assert_eq!(Priority::from(SP::Critical), Priority::Critical);
    }

    // ── v1_to_v2_command translation tests (pass-29 regression) ─────────
    //
    // Pass 28 shipped a panic-on-default mapper that crashed every Socket-
    // backed `mem_get` and `mem_bootstrap` call (rmcp task panic →
    // "Transport closed"). The test below locks the mapper to the same
    // wire shape the daemon's typed DTOs (`MemGetInput`, `MemBootstrapInput`)
    // expect — both have `deny_unknown_fields`, so the test doubles as a
    // contract check between the proxy layer and `dispatch_v2`.

    #[test]
    fn v1_to_v2_command_handles_mem_get() {
        let mapped = v1_to_v2_command("mem_get", &serde_json::json!({ "key": "file:src/main.rs" }));
        assert_eq!(
            mapped,
            serde_json::json!({ "type": "mem_get", "key": "file:src/main.rs" })
        );

        // Round-trip into a typed Command — proves the wire shape decodes
        // through `MemGetInput::deny_unknown_fields`.
        let cmd: Command = serde_json::from_value(mapped).expect("mem_get must decode as Command");
        match cmd {
            Command::MemGet(input) => assert_eq!(input.key, "file:src/main.rs"),
            other => panic!("expected Command::MemGet, got {:?}", other.kind()),
        }
    }

    #[test]
    fn v1_to_v2_command_handles_mem_bootstrap() {
        // Args present.
        let mapped = v1_to_v2_command(
            "mem_bootstrap",
            &serde_json::json!({ "context_files": ["src/lib.rs", "src/main.rs"] }),
        );
        let cmd: Command =
            serde_json::from_value(mapped).expect("mem_bootstrap must decode as Command");
        match cmd {
            Command::MemBootstrap(input) => {
                assert_eq!(input.context_files, vec!["src/lib.rs", "src/main.rs"]);
            }
            other => panic!("expected Command::MemBootstrap, got {:?}", other.kind()),
        }

        // Args missing — must default to an empty list, not panic.
        let mapped_empty = v1_to_v2_command("mem_bootstrap", &serde_json::json!({}));
        let cmd_empty: Command = serde_json::from_value(mapped_empty).unwrap();
        match cmd_empty {
            Command::MemBootstrap(input) => assert!(input.context_files.is_empty()),
            other => panic!("expected MemBootstrap, got {:?}", other.kind()),
        }
    }

    #[test]
    #[should_panic(expected = "v1_to_v2_command called with unsupported command")]
    fn v1_to_v2_command_panic_message_lists_only_unsupported() {
        // Genuinely unsupported strings (mutations / typos) must still
        // panic loudly — that signals a misrouted Socket-backend caller
        // that should be using `daemon_v2()` with a typed Command.
        let _ = v1_to_v2_command("totally_bogus_cmd_xyz", &serde_json::json!({}));
    }

    #[test]
    fn v1_to_v2_command_no_mutations_silently_accepted() {
        // Fence: every mutating command name must panic — they have no
        // place in the mapper. If a future contributor adds (say) "mem_set"
        // here, this test must catch it.
        let mutation_names = [
            "mem_set",
            "gotcha_upsert",
            "gotcha_confirm",
            "gotcha_tombstone",
            "decision_upsert",
            "dev_note_upsert",
            "file_enrich",
            "file_reparse",
            "file_edit_hook",
            "doc_capture",
            "session_log",
            "consultation_hit",
            "session_flush",
            "session_harvest",
        ];
        for name in mutation_names {
            let result = std::panic::catch_unwind(|| {
                v1_to_v2_command(name, &serde_json::json!({}));
            });
            assert!(
                result.is_err(),
                "mutation command '{name}' must panic in v1_to_v2_command — \
                 mutating callers must use daemon_v2() with typed Command"
            );
        }
    }

    // ── ADR-018: Request.agent additive field ───────────────────────────

    /// Pre-multi-agent clients send wire JSON without an `agent` field.
    /// ADR-018 requires this to keep deserializing. This test is the
    /// backward-compatibility regression bar.
    #[test]
    fn request_without_agent_field_deserializes_as_none() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "cmd": { "type": "ping" }
        });
        let req: Request = serde_json::from_value(json).unwrap();
        assert!(
            req.agent.is_none(),
            "missing `agent` must decode to None (ADR-018 additive contract)"
        );
    }

    #[test]
    fn request_with_agent_field_deserializes_and_preserves_value() {
        for (wire, expected) in [
            ("claude", AgentKind::Claude),
            ("codex", AgentKind::Codex),
            ("cli", AgentKind::Cli),
            ("supervisor", AgentKind::Supervisor),
            ("unknown", AgentKind::Unknown),
        ] {
            let json = serde_json::json!({
                "v": 2,
                "id": "550e8400-e29b-41d4-a716-446655440000",
                "session": "660e8400-e29b-41d4-a716-446655440000",
                "agent": wire,
                "cmd": { "type": "ping" }
            });
            let req: Request = serde_json::from_value(json)
                .unwrap_or_else(|e| panic!("decode failed for agent={wire}: {e}"));
            assert_eq!(req.agent, Some(expected));
        }
    }

    #[test]
    fn request_with_unknown_agent_variant_rejected() {
        let json = serde_json::json!({
            "v": 2,
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "session": "660e8400-e29b-41d4-a716-446655440000",
            "agent": "gemini",
            "cmd": { "type": "ping" }
        });
        let res = serde_json::from_value::<Request>(json);
        assert!(
            res.is_err(),
            "unknown agent variant must reject at decode (closed enum)"
        );
    }

    #[test]
    fn request_with_agent_round_trips_through_serialize_deserialize() {
        let original = Request {
            v: PROTOCOL_VERSION,
            id: Uuid::new_v4(),
            session: Uuid::new_v4(),
            agent: Some(AgentKind::Codex),
            cmd: Command::Ping,
        };
        let bytes = serde_json::to_vec(&original).unwrap();
        let round_tripped: Request = serde_json::from_slice(&bytes).unwrap();
        assert_eq!(round_tripped.agent, Some(AgentKind::Codex));
        assert_eq!(round_tripped.v, PROTOCOL_VERSION);
    }
}