smith-protocol 0.1.2

Shared protocol definitions for agent execution system
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
//! # Smith Protocol Library
//!
//! This crate defines the core message protocols and data structures used throughout
//! the Smith AI-powered execution platform. It provides:
//!
//! - **Intent System**: Structured requests for capability execution with security signatures
//! - **Result System**: Standardized responses with execution metadata and audit trails  
//! - **Event System**: Real-time communication protocols for service coordination
//! - **Capability Definitions**: Type-safe capability specifications and parameter schemas
//! - **Security Context**: Comprehensive audit trails and sandbox isolation metadata
//!
//! ## Architecture Overview
//!
//! Smith follows a strict separation between intelligence (AI agents) and execution (secure runners).
//! All communication flows through NATS JetStream using the protocols defined in this crate:
//!
//! ```text
//! AI Agent → Intent → NATS → Policy Validation → Secure Executor → Result → AI Agent
//! ```
//!
//! ## Key Design Principles
//!
//! - **Zero-Trust Security**: All intents are cryptographically signed and policy-validated
//! - **Comprehensive Auditing**: Every action produces detailed audit trails for compliance
//! - **Capability-Based Model**: Fine-grained permissions using versioned capability strings
//! - **Type Safety**: Strongly typed parameters and results with JSON Schema validation
//! - **Time-Based Ordering**: UUIDv7 identifiers provide distributed ordering guarantees
//!
//! ## Basic Usage
//!
//! ```rust,no_run
//! use smith_protocol::{Intent, Capability, IntentResult, RunnerMetadata};
//! use serde_json::json;
//! use std::time::{SystemTime, UNIX_EPOCH};
//!
//! // Create a file read intent
//! let intent = Intent::new(
//!     Capability::FsReadV1,
//!     "production".to_string(),
//!     json!({"path": "/etc/hostname", "max_bytes": 1024}),
//!     30000, // 30 second TTL
//!     "client-public-key".to_string(),
//! );
//!
//! // Results include comprehensive execution metadata
//! let start_time_ns = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
//! let end_time_ns = start_time_ns + 1_000_000; // 1ms later
//! let runner_metadata = RunnerMetadata::empty();
//!
//! let result = IntentResult::success(
//!     intent.id.clone(),
//!     json!({"content": "server.example.com\n"}),
//!     start_time_ns,
//!     end_time_ns,
//!     runner_metadata,
//!     "audit-12345".to_string(),
//! );
//! ```
//!
//! ## Security Model
//!
//! The protocol implements a multi-layer security model:
//!
//! 1. **Cryptographic Signatures**: All intents are signed with Ed25519 keys
//! 2. **Policy Validation**: CUE-based policies validate intents before execution  
//! 3. **Sandbox Isolation**: Multiple isolation layers (Landlock, seccomp, cgroups)
//! 4. **Resource Limits**: CPU, memory, and I/O constraints enforced per capability
//! 5. **Audit Trails**: Complete execution history for compliance and forensics
//!
//! For implementation details, see the specific capability modules and the executor documentation.

use anyhow::Context;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine as _};
use ed25519_dalek::{Signature, Verifier, VerifyingKey, PUBLIC_KEY_LENGTH};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap};
use std::convert::TryInto;
use uuid::Uuid;

#[cfg(feature = "typescript")]
use ts_rs::TS;

/// Protocol version for capability negotiation and compatibility checking.
///
/// This version is incremented when breaking changes are made to the protocol.
/// Clients and servers use this for compatibility verification during handshake.
pub const PROTOCOL_VERSION: u32 = 0;

pub mod benchmark;
pub mod client;
pub mod idempotency;
pub mod negotiation;
pub mod policy;
pub mod policy_abi;
pub mod reasoning;
pub mod result_schema;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(tag = "type", content = "data")]
/// Command sent to the core service
pub enum Command {
    /// Initialize service with client capabilities
    Handshake {
        version: u32,
        capabilities: Vec<String>,
    },
    /// Plan execution request
    Plan {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        goal: String,
        context: HashMap<String, String>,
    },
    /// Execute a tool call
    ToolCall {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        tool: String,
        #[cfg_attr(feature = "typescript", ts(type = "unknown"))]
        args: serde_json::Value,
        timeout_ms: Option<u64>,
    },
    /// Load hook configuration
    HookLoad {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        hook_type: String,
        script: String,
    },
    /// Shell command execution
    ShellExec {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        command: String,
        shell: Option<String>,
        cwd: Option<String>,
        env: HashMap<String, String>,
        timeout_ms: Option<u64>,
    },
    /// Request service shutdown
    Shutdown,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(tag = "type", content = "data")]
/// Event emitted by the core service
pub enum Event {
    /// Service ready with capabilities
    Ready {
        version: u32,
        capabilities: Vec<String>,
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        service_id: Uuid,
    },
    /// State change notification
    StateChange {
        #[cfg_attr(feature = "typescript", ts(type = "string | undefined"))]
        request_id: Option<Uuid>,
        state: ServiceState,
        #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
        timestamp: u64,
    },
    /// Log message
    Log {
        level: LogLevel,
        message: String,
        #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
        timestamp: u64,
        #[cfg_attr(feature = "typescript", ts(type = "string | undefined"))]
        request_id: Option<Uuid>,
    },
    /// Token usage tracking
    TokenUsage {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
        tokens_used: u64,
        #[cfg_attr(feature = "typescript", ts(type = "bigint | undefined"))]
        tokens_remaining: Option<u64>,
    },
    /// Shell command output
    ShellOutput {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        stdout: Option<String>,
        stderr: Option<String>,
        exit_code: Option<i32>,
        finished: bool,
    },
    /// Tool call result
    ToolResult {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        success: bool,
        #[cfg_attr(feature = "typescript", ts(type = "unknown"))]
        result: serde_json::Value,
        error: Option<String>,
    },
    /// Hook execution result
    HookResult {
        #[cfg_attr(feature = "typescript", ts(type = "string"))]
        request_id: Uuid,
        action: HookAction,
    },
    /// Graph/DAG state delta
    GraphDelta {
        #[cfg_attr(feature = "typescript", ts(type = "string | undefined"))]
        request_id: Option<Uuid>,
        nodes_added: Vec<GraphNode>,
        edges_added: Vec<GraphEdge>,
        #[cfg_attr(feature = "typescript", ts(type = "string[]"))]
        nodes_removed: Vec<Uuid>,
        #[cfg_attr(feature = "typescript", ts(type = "string[]"))]
        edges_removed: Vec<Uuid>,
    },
    /// Service error
    Error {
        #[cfg_attr(feature = "typescript", ts(type = "string | undefined"))]
        request_id: Option<Uuid>,
        error_code: String,
        message: String,
        #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
        timestamp: u64,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Service operational state
pub enum ServiceState {
    Starting,
    Ready,
    Processing,
    Error,
    Shutting,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Log levels matching standard conventions
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(tag = "type", content = "data")]
/// Hook action result from script execution
pub enum HookAction {
    Allow(serde_json::Value),
    Block { reason: String },
    Transform(serde_json::Value),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Graph node for DAG visualization
pub struct GraphNode {
    #[cfg_attr(feature = "typescript", ts(type = "string"))]
    pub id: Uuid,
    pub label: String,
    pub node_type: String,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Graph edge for DAG visualization
pub struct GraphEdge {
    #[cfg_attr(feature = "typescript", ts(type = "string"))]
    pub id: Uuid,
    #[cfg_attr(feature = "typescript", ts(type = "string"))]
    pub from: Uuid,
    #[cfg_attr(feature = "typescript", ts(type = "string"))]
    pub to: Uuid,
    pub label: Option<String>,
    pub edge_type: String,
}

/// Capability strings for handshake negotiation
pub mod capabilities {
    pub const SHELL_EXEC: &str = "shell_exec";
    pub const HOOKS_JS: &str = "hooks_js";
    pub const HOOKS_RUST: &str = "hooks_rust";
    pub const REPLAY: &str = "replay";
    pub const NATS: &str = "nats";
    pub const TRACING: &str = "tracing";
}

// JetStream-native protocol definitions for Smith executor platform

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(rename_all = "snake_case")]
/// Intent capabilities enum for JetStream execution
pub enum Capability {
    #[serde(rename = "fs.read.v1")]
    FsReadV1,
    #[serde(rename = "http.fetch.v1")]
    HttpFetchV1,
    #[serde(rename = "fs.write.v1")]
    FsWriteV1,
    #[serde(rename = "git.clone.v1")]
    GitCloneV1,
    #[serde(rename = "archive.read.v1")]
    ArchiveReadV1,
    #[serde(rename = "sqlite.query.v1")]
    SqliteQueryV1,
    #[serde(rename = "bench.report.v1")]
    BenchReportV1,
    #[serde(rename = "shell.exec.v1")]
    ShellExec,
    /// Alias for HttpFetchV1 (for test compatibility)
    HttpFetch,
}

impl std::fmt::Display for Capability {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Capability::FsReadV1 => write!(f, "fs.read.v1"),
            Capability::HttpFetchV1 => write!(f, "http.fetch.v1"),
            Capability::FsWriteV1 => write!(f, "fs.write.v1"),
            Capability::GitCloneV1 => write!(f, "git.clone.v1"),
            Capability::ArchiveReadV1 => write!(f, "archive.read.v1"),
            Capability::SqliteQueryV1 => write!(f, "sqlite.query.v1"),
            Capability::BenchReportV1 => write!(f, "bench.report.v1"),
            Capability::ShellExec => write!(f, "shell.exec.v1"),
            Capability::HttpFetch => write!(f, "http.fetch.v1"),
        }
    }
}

impl std::str::FromStr for Capability {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse_capability_string(s)
    }
}

impl Capability {
    /// Parse a capability string into a Capability enum
    fn parse_capability_string(s: &str) -> Result<Self, anyhow::Error> {
        match s {
            "fs.read.v1" => Ok(Capability::FsReadV1),
            "http.fetch.v1" => Ok(Capability::HttpFetchV1),
            "fs.write.v1" => Ok(Capability::FsWriteV1),
            "git.clone.v1" => Ok(Capability::GitCloneV1),
            "archive.read.v1" => Ok(Capability::ArchiveReadV1),
            "sqlite.query.v1" => Ok(Capability::SqliteQueryV1),
            "bench.report.v1" => Ok(Capability::BenchReportV1),
            "shell.exec.v1" => Ok(Capability::ShellExec),
            "http.fetch" => Ok(Capability::HttpFetch), // For test compatibility
            _ => Err(anyhow::anyhow!("Unknown capability: {}", s)),
        }
    }

    /// Get all available capabilities as strings
    pub fn all_capabilities() -> Vec<&'static str> {
        vec![
            "fs.read.v1",
            "http.fetch.v1",
            "fs.write.v1",
            "git.clone.v1",
            "archive.read.v1",
            "sqlite.query.v1",
            "bench.report.v1",
            "shell.exec.v1",
        ]
    }

    /// Check if a capability string is valid
    pub fn is_valid_capability(s: &str) -> bool {
        Self::all_capabilities().contains(&s)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Intent sent to executors via JetStream
pub struct Intent {
    /// Unique intent identifier (UUIDv7 for time ordering)
    pub id: String,
    /// Capability being requested
    pub capability: Capability,
    /// Routing domain/shard
    pub domain: String,
    /// Capability-specific parameters
    #[cfg_attr(feature = "typescript", ts(type = "unknown"))]
    pub params: serde_json::Value,
    /// Creation timestamp in nanoseconds
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub created_at_ns: u128,
    /// Time-to-live in milliseconds
    pub ttl_ms: u32,
    /// Nonce for deduplication and replay defense
    pub nonce: String,
    /// Public key of the signer (base64)
    pub signer: String,
    /// Detached signature of canonical intent body (base64)
    pub signature_b64: String,
    /// Additional metadata for the intent
    #[cfg_attr(feature = "typescript", ts(type = "Record<string, unknown>"))]
    pub metadata: std::collections::HashMap<String, serde_json::Value>,
}

impl Intent {
    /// Verify the signature of this intent using the embedded signer public key.
    pub fn verify_signature(&self) -> anyhow::Result<bool> {
        if self.signature_b64.trim().is_empty() {
            return Ok(false);
        }

        let signer_bytes = BASE64
            .decode(self.signer.trim())
            .context("Failed to decode signer public key from base64")?;

        let signer_array: [u8; PUBLIC_KEY_LENGTH] = signer_bytes.try_into().map_err(|_| {
            anyhow::anyhow!("Signer public key must be {PUBLIC_KEY_LENGTH} bytes after decoding")
        })?;

        let verifying_key = VerifyingKey::from_bytes(&signer_array)
            .map_err(|err| anyhow::anyhow!("Invalid signer public key: {err}"))?;

        self.verify_with_key(&verifying_key)
    }

    /// Verify the signature of this intent with a supplied verifying key.
    pub fn verify_with_key(&self, verifying_key: &VerifyingKey) -> anyhow::Result<bool> {
        if self.signature_b64.trim().is_empty() {
            return Ok(false);
        }

        let signature_bytes = BASE64
            .decode(self.signature_b64.trim())
            .context("Failed to decode intent signature from base64")?;

        let signature = Signature::from_slice(&signature_bytes)
            .map_err(|err| anyhow::anyhow!("Invalid signature format: {err}"))?;

        let canonical_json = self.canonical_json()?;

        match verifying_key.verify(canonical_json.as_bytes(), &signature) {
            Ok(_) => Ok(true),
            Err(_) => Ok(false),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Result of intent execution
pub struct IntentResult {
    /// ID of the original intent
    pub intent_id: String,
    /// Execution status
    pub status: ExecutionStatus,
    /// Success output (if status is Ok)
    #[cfg_attr(feature = "typescript", ts(type = "unknown | undefined"))]
    pub output: Option<serde_json::Value>,
    /// Error details (if status is Error/Denied/Timeout/Killed)
    pub error: Option<ExecutionError>,
    /// When execution started (nanoseconds)
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub started_at_ns: u128,
    /// When execution finished (nanoseconds)
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub finished_at_ns: u128,
    /// Runtime metadata from the executor
    pub runner_meta: RunnerMetadata,
    /// Reference to audit log entry
    pub audit_ref: AuditRef,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Reference to an audit log entry
pub struct AuditRef {
    /// Audit log entry ID
    pub id: String,
    /// Timestamp of the audit entry
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub timestamp: u64,
    /// Audit trail hash for integrity verification
    pub hash: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(rename_all = "lowercase")]
/// Execution status enum
pub enum ExecutionStatus {
    Ok,
    Error,
    Denied,
    Timeout,
    Killed,
    /// Alias for Ok (for test compatibility)
    Success,
    /// Alias for Error (for test compatibility)
    Failed,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Error details for failed executions
pub struct ExecutionError {
    /// Error code
    pub code: String,
    /// Human-readable error message
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Runtime metadata from executor
pub struct RunnerMetadata {
    /// Process ID of the runner
    pub pid: u32,
    /// CPU time used in milliseconds
    pub cpu_ms: u32,
    /// Maximum RSS in kilobytes
    pub max_rss_kb: u32,
    /// Capability digest used for bundle enforcement (optional for compatibility)
    pub capability_digest: Option<String>,
}

impl RunnerMetadata {
    /// Create empty metadata (used for denied/failed executions)
    pub fn empty() -> Self {
        Self {
            pid: 0,
            cpu_ms: 0,
            max_rss_kb: 0,
            capability_digest: None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Audit log entry for compliance and debugging
pub struct AuditEntry {
    /// Intent that was processed
    pub intent_id: String,
    /// Execution result summary
    pub result_status: ExecutionStatus,
    /// Timestamp when audit was created
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub timestamp_ns: u128,
    /// Executor instance that processed this intent
    pub executor_id: String,
    /// Policy decisions made during admission
    pub policy_decisions: Vec<PolicyDecision>,
    /// Security context
    pub security_context: SecurityContext,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Policy decision made during admission
pub struct PolicyDecision {
    /// Policy rule that was evaluated
    pub rule_name: String,
    /// Result of the policy evaluation
    pub decision: PolicyResult,
    /// Human-readable reason for the decision
    pub reason: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(rename_all = "lowercase")]
/// Policy evaluation result
pub enum PolicyResult {
    Allow,
    Deny,
    Transform,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Security context for audit trail
pub struct SecurityContext {
    /// Sandbox mode used for execution
    pub sandbox_mode: Option<SandboxMode>,
    /// User namespace ID
    pub user_ns: Option<u32>,
    /// Mount namespace ID
    pub mount_ns: Option<u32>,
    /// PID namespace ID
    pub pid_ns: Option<u32>,
    /// Network namespace ID
    pub net_ns: Option<u32>,
    /// Cgroup path
    pub cgroup_path: Option<String>,
    /// Landlock restrictions applied
    pub landlock_enabled: bool,
    /// Seccomp filter applied
    pub seccomp_enabled: bool,
    /// Allow-list entries accessed during execution
    pub allowlist_hits: Option<Vec<AllowlistHit>>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
#[serde(rename_all = "lowercase")]
/// Sandbox execution modes
pub enum SandboxMode {
    /// Full sandbox with all security features enabled
    Full,
    /// Partial sandbox with limited features (development/testing)
    Demo,
    /// No sandboxing (unsafe, for compatibility only)
    Unsafe,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(
    feature = "typescript",
    ts(export, export_to = "client/src/lib/smith-protocol/generated.ts")
)]
/// Allow-list access record
pub struct AllowlistHit {
    /// Type of resource accessed
    pub resource_type: String,
    /// Resource identifier (path, URL, etc.)
    pub resource_id: String,
    /// Access operation (read, write, execute, etc.)
    pub operation: String,
    /// Timestamp of access
    #[cfg_attr(feature = "typescript", ts(type = "bigint"))]
    pub timestamp_ns: u128,
}

/// Resource usage metrics for audit
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceUsage {
    /// Peak memory usage in KB
    pub peak_memory_kb: u32,
    /// CPU time consumed in milliseconds
    pub cpu_time_ms: u32,
    /// Wall clock time for execution in milliseconds
    pub wall_time_ms: u32,
    /// Number of file descriptors used
    pub fd_count: u32,
    /// Number of bytes read from disk
    pub disk_read_bytes: u64,
    /// Number of bytes written to disk
    pub disk_write_bytes: u64,
    /// Number of bytes sent over network
    pub network_tx_bytes: u64,
    /// Number of bytes received over network
    pub network_rx_bytes: u64,
}

/// Capability specification for discovery and documentation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct CapabilitySpec {
    /// Capability name (e.g., "fs.read.v1")
    pub name: String,
    /// Human-readable description
    pub description: String,
    /// JSON schema for parameter validation
    pub params_schema: serde_json::Value,
    /// Example parameters
    pub example_params: serde_json::Value,
    /// Resource requirements and limits
    pub resource_requirements: ResourceRequirements,
    /// Security implications and recommendations
    pub security_notes: Vec<String>,
}

/// Resource requirements for a capability
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct ResourceRequirements {
    /// Typical CPU time required (ms)
    pub cpu_ms_typical: u32,
    /// Maximum memory usage (KB)
    pub memory_kb_max: u32,
    /// Network access required
    pub network_access: bool,
    /// File system access required
    pub filesystem_access: bool,
    /// External command execution required
    pub external_commands: bool,
}

/// Resource limits enforced during intent execution
///
/// These limits provide defense-in-depth resource isolation and are enforced
/// by the secure executor's jailer. Violations result in execution termination.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
pub struct ExecutionLimits {
    /// CPU time limit per 100ms interval
    pub cpu_ms_per_100ms: u32,
    /// Memory limit in bytes
    pub mem_bytes: u64,
    /// I/O limit in bytes
    pub io_bytes: u64,
    /// Maximum number of processes
    pub pids_max: u32,
    /// Total timeout in milliseconds
    pub timeout_ms: u64,
}

impl Default for ExecutionLimits {
    fn default() -> Self {
        Self {
            cpu_ms_per_100ms: 50,         // 50% CPU usage
            mem_bytes: 128 * 1024 * 1024, // 128MB
            io_bytes: 10 * 1024 * 1024,   // 10MB
            pids_max: 10,
            timeout_ms: 30000, // 30 seconds
        }
    }
}

/// Enhanced audit event (replaces AuditEntry for better compatibility)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditEvent {
    /// Intent that was processed
    pub intent_id: String,
    /// Execution result summary
    pub result_status: ExecutionStatus,
    /// Timestamp when audit was created
    pub timestamp_ns: u128,
    /// Executor instance that processed this intent
    pub executor_id: String,
    /// Policy decisions made during admission
    pub policy_decisions: Vec<PolicyDecision>,
    /// Security context
    pub security_context: SecurityContext,
    /// Resource usage metrics
    pub resource_usage: Option<ResourceUsage>,
}

/// Capability-specific parameter types
pub mod params {
    use super::*;

    /// Parameters for fs.read.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct FsReadV1 {
        /// File path to read
        pub path: String,
        /// Maximum bytes to read (optional)
        pub max_bytes: Option<u64>,
        /// Whether to follow symlinks
        pub follow_symlinks: Option<bool>,
    }

    /// Parameters for http.fetch.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct HttpFetchV1 {
        /// URL to fetch
        pub url: String,
        /// HTTP method (default: GET)
        pub method: Option<String>,
        /// HTTP headers
        pub headers: Option<HashMap<String, String>>,
        /// Request body (for POST/PUT)
        pub body: Option<String>,
        /// Timeout in milliseconds
        pub timeout_ms: Option<u32>,
    }

    /// Parameters for archive.read.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct ArchiveReadV1 {
        /// Archive file path to read
        pub path: String,
        /// Whether to extract file contents (default: false, metadata only)
        pub extract_content: Option<bool>,
    }

    /// Parameters for sqlite.query.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct SqliteQueryV1 {
        /// SQLite database file path
        pub database_path: String,
        /// SQL query to execute (read-only)
        pub query: String,
        /// Query parameters for prepared statements
        pub params: Option<Vec<serde_json::Value>>,
        /// Maximum rows to return (default: 1000)
        pub max_rows: Option<u32>,
        /// Query timeout in milliseconds (default: 30000)
        pub timeout_ms: Option<u32>,
    }

    /// Parameters for bench.report.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct BenchReportV1 {
        /// Benchmark name/identifier
        pub benchmark_name: String,
        /// Performance metrics to report
        pub metrics: HashMap<String, f64>,
        /// Benchmark metadata
        pub metadata: Option<HashMap<String, serde_json::Value>>,
        /// Historical data retention days (default: 30)
        pub retention_days: Option<u32>,
    }

    /// Parameters for shell.exec.v1 capability
    #[derive(Debug, Clone, Serialize, Deserialize)]
    #[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
    pub struct ShellExecV1 {
        /// Command to execute
        pub command: String,
        /// Command arguments
        pub args: Option<Vec<String>>,
        /// Environment variables
        pub env: Option<HashMap<String, String>>,
        /// Working directory
        pub cwd: Option<String>,
        /// Timeout in milliseconds (default: 30000, max: 600000)
        pub timeout_ms: Option<u32>,
        /// Data to write to stdin
        pub stdin: Option<String>,
    }
}

/// Intent creation and signing utilities
impl Intent {
    /// Create a new intent with the given parameters
    pub fn new(
        capability: Capability,
        domain: String,
        params: serde_json::Value,
        ttl_ms: u32,
        signer: String,
    ) -> Self {
        let id = Self::generate_intent_id();
        let nonce = Self::generate_nonce();
        let created_at_ns = Self::current_timestamp_ns();

        Self {
            id,
            capability,
            domain,
            params,
            created_at_ns,
            ttl_ms,
            nonce,
            signer,
            signature_b64: String::new(), // Will be set by signing process
            metadata: std::collections::HashMap::new(),
        }
    }

    /// Generate a time-ordered intent ID using UUIDv7
    fn generate_intent_id() -> String {
        uuid::Uuid::now_v7().to_string()
    }

    /// Generate a random nonce for deduplication
    fn generate_nonce() -> String {
        uuid::Uuid::new_v4().to_string()
    }

    /// Get current timestamp in nanoseconds since epoch
    fn current_timestamp_ns() -> u128 {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    }

    /// Get canonical JSON representation for signing
    pub fn canonical_json(&self) -> anyhow::Result<String> {
        let canonical = self.create_unsigned_copy();
        let sorted_json = Self::sort_json_keys(canonical)?;
        Ok(serde_json::to_string(&sorted_json)?)
    }

    /// Create a copy of the intent without signature for canonical form
    fn create_unsigned_copy(&self) -> Self {
        let mut canonical = self.clone();
        canonical.signature_b64 = String::new();
        canonical
    }

    /// Sort JSON keys for deterministic output
    fn sort_json_keys(value: impl Serialize) -> anyhow::Result<serde_json::Value> {
        let mut json = serde_json::to_value(value)?;
        if let serde_json::Value::Object(ref mut map) = json {
            // Remove signature_b64 field completely for canonical form
            map.remove("signature_b64");

            let sorted: BTreeMap<_, _> = map.iter().collect();
            *map = sorted
                .into_iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect();
        }
        Ok(json)
    }

    /// Check if intent has expired based on current time
    pub fn is_expired(&self) -> bool {
        let now_ns = Self::current_timestamp_ns();
        let expiry_ns = self.created_at_ns + self.ttl_as_nanoseconds();
        now_ns > expiry_ns
    }

    /// Convert TTL from milliseconds to nanoseconds
    fn ttl_as_nanoseconds(&self) -> u128 {
        (self.ttl_ms as u128) * 1_000_000
    }

    /// Get the subject pattern for this intent
    pub fn subject(&self) -> String {
        smith_bus::builders::IntentSubject::with_domain(&self.capability.to_string(), &self.domain)
    }

    /// Get the result subject for this intent
    pub fn result_subject(&self) -> String {
        smith_bus::builders::ResultSubject::for_intent(&self.id)
    }
}

impl IntentResult {
    /// Create a successful result
    pub fn success(
        intent_id: String,
        output: serde_json::Value,
        started_at_ns: u128,
        finished_at_ns: u128,
        runner_meta: RunnerMetadata,
        audit_ref: String,
    ) -> Self {
        Self::create_result(
            intent_id,
            ExecutionStatus::Ok,
            Some(output),
            None,
            started_at_ns,
            finished_at_ns,
            runner_meta,
            audit_ref,
        )
    }

    /// Create an error result
    pub fn error(
        intent_id: String,
        error_code: String,
        error_message: String,
        started_at_ns: u128,
        finished_at_ns: u128,
        runner_meta: RunnerMetadata,
        audit_ref: String,
    ) -> Self {
        let error = Some(ExecutionError {
            code: error_code,
            message: error_message,
        });

        Self::create_result(
            intent_id,
            ExecutionStatus::Error,
            None,
            error,
            started_at_ns,
            finished_at_ns,
            runner_meta,
            audit_ref,
        )
    }

    /// Create a denied result
    pub fn denied(intent_id: String, reason: String, audit_ref: String) -> Self {
        let now_ns = Intent::current_timestamp_ns();
        let error = Some(ExecutionError {
            code: "POLICY_DENIED".to_string(),
            message: reason,
        });
        let empty_metadata = RunnerMetadata::empty();

        Self::create_result(
            intent_id,
            ExecutionStatus::Denied,
            None,
            error,
            now_ns,
            now_ns,
            empty_metadata,
            audit_ref,
        )
    }

    /// Create a result with the given parameters
    #[allow(clippy::too_many_arguments)]
    fn create_result(
        intent_id: String,
        status: ExecutionStatus,
        output: Option<serde_json::Value>,
        error: Option<ExecutionError>,
        started_at_ns: u128,
        finished_at_ns: u128,
        runner_meta: RunnerMetadata,
        audit_ref: String,
    ) -> Self {
        Self {
            intent_id,
            status,
            output,
            error,
            started_at_ns,
            finished_at_ns,
            runner_meta,
            audit_ref: AuditRef {
                id: audit_ref,
                timestamp: std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs(),
                hash: "placeholder".to_string(),
            },
        }
    }
}

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

    #[test]
    fn test_capability_serialization() {
        let cap = Capability::FsReadV1;
        let serialized = serde_json::to_string(&cap).unwrap();
        assert_eq!(serialized, r#""fs.read.v1""#);

        let deserialized: Capability = serde_json::from_str(&serialized).unwrap();
        assert_eq!(deserialized, cap);
    }

    #[test]
    fn test_capability_from_str() {
        assert_eq!(
            "fs.read.v1".parse::<Capability>().unwrap(),
            Capability::FsReadV1
        );
        assert_eq!(
            "http.fetch.v1".parse::<Capability>().unwrap(),
            Capability::HttpFetchV1
        );
        assert!("invalid".parse::<Capability>().is_err());
    }

    #[test]
    fn test_intent_creation() {
        let intent = Intent::new(
            Capability::FsReadV1,
            "test".to_string(),
            json!({"path": "/etc/hostname"}),
            30000,
            "test-signer".to_string(),
        );

        assert!(!intent.id.is_empty());
        assert_eq!(intent.capability, Capability::FsReadV1);
        assert_eq!(intent.domain, "test");
        assert_eq!(intent.ttl_ms, 30000);
        assert!(!intent.nonce.is_empty());
        assert_eq!(intent.signer, "test-signer");
    }

    #[test]
    fn test_intent_subjects() {
        let intent = Intent::new(
            Capability::HttpFetchV1,
            "web".to_string(),
            json!({"url": "https://example.com"}),
            10000,
            "test-signer".to_string(),
        );

        assert_eq!(intent.subject(), "smith.intents.http.fetch.v1.web");
        assert_eq!(
            intent.result_subject(),
            format!("smith.results.{}", intent.id)
        );
    }

    #[test]
    fn test_intent_expiration() {
        let mut intent = Intent::new(
            Capability::FsReadV1,
            "test".to_string(),
            json!({"path": "/tmp/test"}),
            100, // 100ms TTL
            "test-signer".to_string(),
        );

        // Should not be expired immediately
        assert!(!intent.is_expired());

        // Simulate expired intent by setting old timestamp
        intent.created_at_ns = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
            - 200_000_000; // 200ms ago

        assert!(intent.is_expired());
    }

    #[test]
    fn test_intent_canonical_json() {
        let intent = Intent::new(
            Capability::FsReadV1,
            "test".to_string(),
            json!({"path": "/etc/hostname", "max_bytes": 1024}),
            30000,
            "test-signer".to_string(),
        );

        let canonical = intent.canonical_json().unwrap();

        // Should be valid JSON
        let _: serde_json::Value = serde_json::from_str(&canonical).unwrap();

        // Should not contain signature
        assert!(!canonical.contains("signature_b64"));
    }

    #[test]
    fn test_intent_signature_verification() {
        use ed25519_dalek::SigningKey;

        let signing_bytes = [42u8; 32];
        let signing_key = SigningKey::from_bytes(&signing_bytes);
        let verifying_key = signing_key.verifying_key();

        let mut intent = Intent::new(
            Capability::FsReadV1,
            "test".to_string(),
            json!({"path": "/etc/hostname"}),
            30_000,
            BASE64.encode(verifying_key.to_bytes()),
        );

        let canonical = intent.canonical_json().unwrap();
        let signature = signing_key.sign(canonical.as_bytes());
        intent.signature_b64 = BASE64.encode(signature.to_bytes());

        assert!(intent.verify_signature().unwrap());

        let mut invalid_signature = signature.to_bytes();
        invalid_signature[0] ^= 0xFF;
        intent.signature_b64 = BASE64.encode(invalid_signature);
        assert!(!intent.verify_signature().unwrap());
    }

    #[test]
    fn test_intent_result_creation() {
        let intent_id = "test-intent-123".to_string();
        let audit_ref = "audit-ref-456".to_string();
        let runner_meta = RunnerMetadata {
            pid: 1234,
            cpu_ms: 500,
            max_rss_kb: 2048,
            capability_digest: Some("test-capability-digest".to_string()),
        };

        // Test successful result
        let success_result = IntentResult::success(
            intent_id.clone(),
            json!({"content": "file contents"}),
            1640995200000000000,
            1640995201000000000,
            runner_meta.clone(),
            audit_ref.clone(),
        );

        assert_eq!(success_result.intent_id, intent_id);
        assert!(matches!(success_result.status, ExecutionStatus::Ok));
        assert!(success_result.output.is_some());
        assert!(success_result.error.is_none());

        // Test error result
        let error_result = IntentResult::error(
            intent_id.clone(),
            "FILE_NOT_FOUND".to_string(),
            "File does not exist".to_string(),
            1640995200000000000,
            1640995201000000000,
            runner_meta.clone(),
            audit_ref.clone(),
        );

        assert_eq!(error_result.intent_id, intent_id);
        assert!(matches!(error_result.status, ExecutionStatus::Error));
        assert!(error_result.output.is_none());
        assert!(error_result.error.is_some());
        assert_eq!(error_result.error.as_ref().unwrap().code, "FILE_NOT_FOUND");

        // Test denied result
        let denied_result = IntentResult::denied(
            intent_id.clone(),
            "Policy violation: unauthorized path".to_string(),
            audit_ref.clone(),
        );

        assert_eq!(denied_result.intent_id, intent_id);
        assert!(matches!(denied_result.status, ExecutionStatus::Denied));
        assert!(denied_result.error.is_some());
        assert_eq!(denied_result.error.as_ref().unwrap().code, "POLICY_DENIED");
    }

    #[test]
    fn test_fs_read_params() {
        let params = params::FsReadV1 {
            path: "/etc/hostname".to_string(),
            max_bytes: Some(1024),
            follow_symlinks: Some(false),
        };

        let json = serde_json::to_value(&params).unwrap();
        let deserialized: params::FsReadV1 = serde_json::from_value(json).unwrap();

        assert_eq!(deserialized.path, "/etc/hostname");
        assert_eq!(deserialized.max_bytes, Some(1024));
        assert_eq!(deserialized.follow_symlinks, Some(false));
    }

    #[test]
    fn test_http_fetch_params() {
        let mut headers = HashMap::new();
        headers.insert("User-Agent".to_string(), "Smith/1.0".to_string());

        let params = params::HttpFetchV1 {
            url: "https://api.example.com/data".to_string(),
            method: Some("POST".to_string()),
            headers: Some(headers.clone()),
            body: Some(r#"{"key":"value"}"#.to_string()),
            timeout_ms: Some(5000),
        };

        let json = serde_json::to_value(&params).unwrap();
        let deserialized: params::HttpFetchV1 = serde_json::from_value(json).unwrap();

        assert_eq!(deserialized.url, "https://api.example.com/data");
        assert_eq!(deserialized.method, Some("POST".to_string()));
        assert_eq!(deserialized.headers, Some(headers));
        assert_eq!(deserialized.timeout_ms, Some(5000));
    }

    #[test]
    fn test_audit_entry_structure() {
        let policy_decision = PolicyDecision {
            rule_name: "path_allowlist".to_string(),
            decision: PolicyResult::Allow,
            reason: "Path is in allowed list".to_string(),
        };

        let security_context = SecurityContext {
            sandbox_mode: Some(SandboxMode::Full),
            user_ns: Some(1001),
            mount_ns: Some(2002),
            pid_ns: Some(3003),
            net_ns: Some(4004),
            cgroup_path: Some("/sys/fs/cgroup/smith/executor-123".to_string()),
            landlock_enabled: true,
            seccomp_enabled: true,
            allowlist_hits: None,
        };

        let audit_entry = AuditEntry {
            intent_id: "intent-123".to_string(),
            result_status: ExecutionStatus::Ok,
            timestamp_ns: 1640995200000000000,
            executor_id: "executor-001".to_string(),
            policy_decisions: vec![policy_decision],
            security_context,
        };

        // Test serialization roundtrip
        let json = serde_json::to_string(&audit_entry).unwrap();
        let deserialized: AuditEntry = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.intent_id, "intent-123");
        assert_eq!(deserialized.executor_id, "executor-001");
        assert_eq!(deserialized.policy_decisions.len(), 1);
        assert!(deserialized.security_context.landlock_enabled);
    }

    #[test]
    fn test_all_capability_variants() {
        // Test that all capability variants can be created and have proper string representations
        let capabilities = vec![
            (Capability::FsReadV1, "fs.read.v1"),
            (Capability::HttpFetchV1, "http.fetch.v1"),
            (Capability::FsWriteV1, "fs.write.v1"),
            (Capability::GitCloneV1, "git.clone.v1"),
            (Capability::ArchiveReadV1, "archive.read.v1"),
            (Capability::SqliteQueryV1, "sqlite.query.v1"),
            (Capability::BenchReportV1, "bench.report.v1"),
        ];

        for (capability, expected_string) in capabilities {
            // Test Display trait
            assert_eq!(capability.to_string(), expected_string);

            // Test FromStr trait
            let parsed: Capability = expected_string.parse().unwrap();
            assert_eq!(parsed, capability);

            // Test serialization roundtrip
            let serialized = serde_json::to_string(&capability).unwrap();
            let deserialized: Capability = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, capability);
        }
    }

    #[test]
    fn test_capability_all_capabilities() {
        let all_caps = Capability::all_capabilities();
        assert_eq!(all_caps.len(), 8);

        // Verify all expected capabilities are present
        assert!(all_caps.contains(&"fs.read.v1"));
        assert!(all_caps.contains(&"http.fetch.v1"));
        assert!(all_caps.contains(&"fs.write.v1"));
        assert!(all_caps.contains(&"git.clone.v1"));
        assert!(all_caps.contains(&"archive.read.v1"));
        assert!(all_caps.contains(&"sqlite.query.v1"));
        assert!(all_caps.contains(&"bench.report.v1"));
        assert!(all_caps.contains(&"shell.exec.v1"));
    }

    #[test]
    fn test_capability_is_valid_capability() {
        // Test valid capabilities
        assert!(Capability::is_valid_capability("fs.read.v1"));
        assert!(Capability::is_valid_capability("http.fetch.v1"));
        assert!(Capability::is_valid_capability("sqlite.query.v1"));

        // Test invalid capabilities
        assert!(!Capability::is_valid_capability("invalid.capability"));
        assert!(!Capability::is_valid_capability(""));
        assert!(!Capability::is_valid_capability("fs.read.v2")); // Wrong version
    }

    #[test]
    fn test_execution_status_variants() {
        let statuses = vec![
            ExecutionStatus::Ok,
            ExecutionStatus::Error,
            ExecutionStatus::Denied,
            ExecutionStatus::Timeout,
            ExecutionStatus::Killed,
        ];

        for status in statuses {
            // Test serialization roundtrip
            let serialized = serde_json::to_string(&status).unwrap();
            let deserialized: ExecutionStatus = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, status);

            // Test Debug trait
            let debug_str = format!("{:?}", status);
            assert!(!debug_str.is_empty());
        }
    }

    #[test]
    fn test_execution_error_structure() {
        let error = ExecutionError {
            code: "FILE_NOT_FOUND".to_string(),
            message: "The specified file could not be found".to_string(),
        };

        // Test serialization
        let json = serde_json::to_string(&error).unwrap();
        let deserialized: ExecutionError = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.code, "FILE_NOT_FOUND");
        assert_eq!(
            deserialized.message,
            "The specified file could not be found"
        );

        // Test Clone trait
        let cloned = error.clone();
        assert_eq!(cloned.code, error.code);
        assert_eq!(cloned.message, error.message);
    }

    #[test]
    fn test_runner_metadata() {
        let metadata = RunnerMetadata {
            pid: 12345,
            cpu_ms: 1500,
            max_rss_kb: 8192,
            capability_digest: Some("sha256:abcd1234".to_string()),
        };

        // Test serialization roundtrip
        let json = serde_json::to_string(&metadata).unwrap();
        let deserialized: RunnerMetadata = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.pid, 12345);
        assert_eq!(deserialized.cpu_ms, 1500);
        assert_eq!(deserialized.max_rss_kb, 8192);
        assert_eq!(
            deserialized.capability_digest,
            Some("sha256:abcd1234".to_string())
        );

        // Test empty metadata
        let empty = RunnerMetadata::empty();
        assert_eq!(empty.pid, 0);
        assert_eq!(empty.cpu_ms, 0);
        assert_eq!(empty.max_rss_kb, 0);
        assert_eq!(empty.capability_digest, None);
    }

    #[test]
    fn test_sandbox_mode_variants() {
        let modes = vec![
            (SandboxMode::Full, "full"),
            (SandboxMode::Demo, "demo"),
            (SandboxMode::Unsafe, "unsafe"),
        ];

        for (mode, expected_string) in modes {
            // Test serialization
            let serialized = serde_json::to_string(&mode).unwrap();
            assert_eq!(serialized, format!("\"{}\"", expected_string));

            // Test deserialization
            let deserialized: SandboxMode = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, mode);
        }
    }

    #[test]
    fn test_policy_result_variants() {
        let results = vec![
            (PolicyResult::Allow, "allow"),
            (PolicyResult::Deny, "deny"),
            (PolicyResult::Transform, "transform"),
        ];

        for (result, expected_string) in results {
            // Test serialization
            let serialized = serde_json::to_string(&result).unwrap();
            assert_eq!(serialized, format!("\"{}\"", expected_string));

            // Test deserialization
            let deserialized: PolicyResult = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, result);
        }
    }

    #[test]
    fn test_policy_decision_structure() {
        let decision = PolicyDecision {
            rule_name: "file_access_check".to_string(),
            decision: PolicyResult::Allow,
            reason: "File is within allowed directory".to_string(),
        };

        // Test serialization roundtrip
        let json = serde_json::to_string(&decision).unwrap();
        let deserialized: PolicyDecision = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.rule_name, "file_access_check");
        assert_eq!(deserialized.decision, PolicyResult::Allow);
        assert_eq!(deserialized.reason, "File is within allowed directory");
    }

    #[test]
    fn test_security_context_comprehensive() {
        // Test full security context
        let full_context = SecurityContext {
            sandbox_mode: Some(SandboxMode::Full),
            user_ns: Some(1001),
            mount_ns: Some(2002),
            pid_ns: Some(3003),
            net_ns: Some(4004),
            cgroup_path: Some("/sys/fs/cgroup/smith/test-123".to_string()),
            landlock_enabled: true,
            seccomp_enabled: true,
            allowlist_hits: Some(vec![AllowlistHit {
                resource_type: "file".to_string(),
                resource_id: "/etc/hostname".to_string(),
                operation: "read".to_string(),
                timestamp_ns: 1640995200000000000,
            }]),
        };

        // Test serialization roundtrip
        let json = serde_json::to_string(&full_context).unwrap();
        let deserialized: SecurityContext = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.sandbox_mode, Some(SandboxMode::Full));
        assert_eq!(deserialized.user_ns, Some(1001));
        assert!(deserialized.landlock_enabled);
        assert_eq!(deserialized.allowlist_hits.as_ref().unwrap().len(), 1);

        // Test minimal security context
        let minimal_context = SecurityContext {
            sandbox_mode: None,
            user_ns: None,
            mount_ns: None,
            pid_ns: None,
            net_ns: None,
            cgroup_path: None,
            landlock_enabled: false,
            seccomp_enabled: false,
            allowlist_hits: None,
        };

        let minimal_json = serde_json::to_string(&minimal_context).unwrap();
        let minimal_deserialized: SecurityContext = serde_json::from_str(&minimal_json).unwrap();

        assert_eq!(minimal_deserialized.sandbox_mode, None);
        assert!(!minimal_deserialized.landlock_enabled);
        assert_eq!(minimal_deserialized.allowlist_hits, None);
    }

    #[test]
    fn test_intent_result_timeout_and_killed() {
        let intent_id = "timeout-test-123".to_string();
        let audit_ref = "audit-timeout-456".to_string();
        let runner_meta = RunnerMetadata::empty();

        // Test timeout result creation (utility function)
        let start_time = 1640995200000000000;
        let end_time = 1640995230000000000; // 30 seconds later

        let timeout_result = IntentResult::create_result(
            intent_id.clone(),
            ExecutionStatus::Timeout,
            None,
            Some(ExecutionError {
                code: "EXECUTION_TIMEOUT".to_string(),
                message: "Execution exceeded maximum allowed time".to_string(),
            }),
            start_time,
            end_time,
            runner_meta.clone(),
            audit_ref.clone(),
        );

        assert_eq!(timeout_result.status, ExecutionStatus::Timeout);
        assert!(timeout_result.error.is_some());
        assert_eq!(
            timeout_result.error.as_ref().unwrap().code,
            "EXECUTION_TIMEOUT"
        );

        // Test killed result creation (utility function)
        let killed_result = IntentResult::create_result(
            intent_id.clone(),
            ExecutionStatus::Killed,
            None,
            Some(ExecutionError {
                code: "PROCESS_KILLED".to_string(),
                message: "Process was terminated by signal".to_string(),
            }),
            start_time,
            end_time,
            runner_meta.clone(),
            audit_ref.clone(),
        );

        assert_eq!(killed_result.status, ExecutionStatus::Killed);
        assert!(killed_result.error.is_some());
        assert_eq!(killed_result.error.as_ref().unwrap().code, "PROCESS_KILLED");
    }
}