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
//! Typed, redacted detail carried by the staged audit event.
#![cfg_attr(
feature = "server",
expect(
clippy::large_stack_arrays,
reason = "utoipa's ToSchema derive for the audit detail enum builds its variant schema array on the stack"
)
)]
use serde::{Deserialize, Serialize};
use std::fmt;
use crate::auth::{PrincipalId, PrincipalKindTag};
use crate::envelope::{CardKind, SpecHash};
use crate::ids::CardUid;
use crate::origin::Origin;
use crate::reference::CardRef;
use crate::vala::api::AuditOutcome;
use crate::vala::api::{QueryClass, TimePartitionWire};
/// Error returned when an audit-detail identifier is empty, malformed, or
/// contains a value that must never enter an audit record.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum AuditDetailValueError {
/// The value was empty after boundary normalization.
#[error("{field} must not be empty")]
Empty {
/// The audit-detail field being validated.
field: &'static str,
},
/// The value contains a control character.
#[error("{field} contains a control character")]
ControlCharacter {
/// The audit-detail field being validated.
field: &'static str,
},
/// The value resembles a credential or other secret.
#[error("{field} contains a secret-like value")]
SecretLike {
/// The audit-detail field being validated.
field: &'static str,
},
/// The value exceeds the scrubbed audit-detail byte ceiling.
#[error("{field} exceeds 1024 UTF-8 bytes")]
TooLong {
/// The audit-detail field being validated.
field: &'static str,
},
/// The value does not match the closed canonical form for its field.
#[error("{field} is not in its canonical form")]
Malformed {
/// The audit-detail field being validated.
field: &'static str,
},
/// Related audit fields violate a closed contract invariant.
#[error("audit detail fields violate the {invariant} invariant")]
InvalidCombination {
/// Name of the violated invariant.
invariant: &'static str,
},
}
macro_rules! audit_detail_value {
($name:ident, $field:literal, $doc:literal) => {
#[doc = $doc]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(transparent)]
pub struct $name(
/// Validated normalized audit-safe text.
String,
);
impl $name {
/// Construct a normalized, non-secret audit-detail value.
///
/// # Errors
/// Returns an error when the value is empty, contains controls, or
/// resembles a credential.
pub fn new(value: impl Into<String>) -> Result<Self, AuditDetailValueError> {
let value = value.into();
let value = value.trim();
if value.is_empty() {
return Err(AuditDetailValueError::Empty { field: $field });
}
if value.chars().any(char::is_control) {
return Err(AuditDetailValueError::ControlCharacter { field: $field });
}
if value.len() > 1_024 {
return Err(AuditDetailValueError::TooLong { field: $field });
}
if is_secret_like(value) {
return Err(AuditDetailValueError::SecretLike { field: $field });
}
Ok(Self(value.to_owned()))
}
/// Borrow the normalized value.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<$name> for String {
fn from(value: $name) -> Self {
value.0
}
}
impl<'de> Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(serde::de::Error::custom)
}
}
};
}
audit_detail_value!(
ScopeHash,
"scope_hash",
"Stable, non-secret digest identifying a card scope."
);
audit_detail_value!(
StoragePath,
"storage_path",
"Logical, non-secret path identifying stored audit data."
);
audit_detail_value!(
BatchId,
"batch_id",
"Idempotent, non-secret identifier for an ingest batch."
);
audit_detail_value!(
RevocationReason,
"revocation_reason",
"Operator-supplied, non-secret justification for revoking a principal."
);
audit_detail_value!(
QueryAuditDigest,
"query_audit_digest",
"Stable non-secret digest used by a Bifrost query audit decision."
);
/// Domain separator prefixed to every Scribe-promotion digest preimage.
///
/// It binds the digest to this exact algorithm and field order so a byte
/// stream produced for any other Wyrd purpose can never collide with a
/// promoted-file-set identity.
const SCRIBE_PROMOTION_DIGEST_DOMAIN: &str = "wyrd.forge.scribe-promotion.v1";
/// Stable, non-secret digest identifying the exact ordered file set promoted by
/// one Forge Scribe-promotion operation.
///
/// The only representable form is the literal `sha256:` prefix followed by
/// exactly 64 lowercase hexadecimal characters. Prepared and terminal audit
/// rows carry the same value, so a reader can prove that a committed promotion
/// moved precisely the file set that was prepared.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(transparent)]
pub struct ForgePromotedFileSetDigest(
/// Canonical `sha256:`-prefixed lowercase hexadecimal digest.
String,
);
impl ForgePromotedFileSetDigest {
/// Byte length of the canonical `sha256:<64 hex>` representation.
const CANONICAL_LEN: usize = 7 + 64;
/// Accepts an already-formed digest in its exact canonical form.
///
/// No normalization is applied: an uppercase, unprefixed, truncated, or
/// differently labelled value is a caller error rather than something to
/// repair, because a repaired digest would silently claim an identity the
/// caller never computed.
///
/// # Errors
/// Returns [`AuditDetailValueError::Malformed`] when the value is not
/// `sha256:` followed by 64 lowercase hexadecimal characters.
pub fn new(value: impl Into<String>) -> Result<Self, AuditDetailValueError> {
let value = value.into();
let Some(body) = value.strip_prefix("sha256:") else {
return Err(AuditDetailValueError::Malformed {
field: "promoted_file_set_digest",
});
};
if value.len() != Self::CANONICAL_LEN
|| !body
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err(AuditDetailValueError::Malformed {
field: "promoted_file_set_digest",
});
}
Ok(Self(value))
}
/// Computes the digest over one ordered promoted-file set.
///
/// The preimage is the domain separator followed, in the caller's promoted
/// order, by each file's lowercase hyphenated file-list UUID, canonical
/// logical path, and normalized checksum. Every field is prefixed by its
/// unsigned 64-bit big-endian UTF-8 byte length so no field boundary can be
/// shifted without changing the digest, and the whole stream is hashed with
/// SHA-256. Order is significant: the promoted order is part of the
/// identity, so callers must not sort here.
#[must_use]
pub fn compute(files: &[ForgePromotedFile]) -> Self {
use sha2::Digest as _;
fn framed(hasher: &mut sha2::Sha256, field: &str) {
hasher.update((field.len() as u64).to_be_bytes());
hasher.update(field.as_bytes());
}
let mut hasher = sha2::Sha256::new();
framed(&mut hasher, SCRIBE_PROMOTION_DIGEST_DOMAIN);
for file in files {
framed(&mut hasher, &file.file_id.hyphenated().to_string());
framed(&mut hasher, file.path.as_str());
framed(&mut hasher, &file.checksum);
}
Self(format!("sha256:{}", hex::encode(hasher.finalize())))
}
/// Borrows the canonical digest text.
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ForgePromotedFileSetDigest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<ForgePromotedFileSetDigest> for String {
fn from(value: ForgePromotedFileSetDigest) -> Self {
value.0
}
}
impl<'de> Deserialize<'de> for ForgePromotedFileSetDigest {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
Self::new(value).map_err(serde::de::Error::custom)
}
}
/// One promoted hot object contributing a tuple to the promoted-file-set digest.
///
/// This is a pure domain value: it normalizes and validates its own fields at
/// construction so [`ForgePromotedFileSetDigest::compute`] can hash them
/// without re-deciding what "normalized" means. It carries no row payload,
/// SQL, credential, or secret.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ForgePromotedFile {
/// Durable `file_list` identity of the promoted hot object.
file_id: uuid::Uuid,
/// Canonical logical path of the promoted object.
path: StoragePath,
/// Normalized lowercase checksum recorded for the promoted object.
checksum: String,
}
impl ForgePromotedFile {
/// Constructs one validated promoted-file tuple.
///
/// The checksum is normalized by trimming surrounding whitespace and
/// lowercasing ASCII, because publication and revalidation read it from
/// different producers and the digest must not depend on which one wrote
/// it. The path is already canonical by [`StoragePath`]'s own contract.
///
/// # Errors
/// Returns [`AuditDetailValueError::Empty`] when the checksum is empty
/// after normalization and [`AuditDetailValueError::ControlCharacter`] when
/// it contains a control character.
pub fn new(
file_id: uuid::Uuid,
path: StoragePath,
checksum: impl Into<String>,
) -> Result<Self, AuditDetailValueError> {
let checksum = checksum.into();
let checksum = checksum.trim().to_ascii_lowercase();
if checksum.is_empty() {
return Err(AuditDetailValueError::Empty { field: "checksum" });
}
if checksum.chars().any(char::is_control) {
return Err(AuditDetailValueError::ControlCharacter { field: "checksum" });
}
Ok(Self {
file_id,
path,
checksum,
})
}
/// Returns the durable `file_list` identity.
#[must_use]
pub const fn file_id(&self) -> uuid::Uuid {
self.file_id
}
/// Borrows the canonical logical path.
#[must_use]
pub const fn path(&self) -> &StoragePath {
&self.path
}
/// Borrows the normalized checksum.
#[must_use]
pub fn checksum(&self) -> &str {
&self.checksum
}
}
/// Closed execution topology recorded by a Bifrost read decision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum QueryExecutionMode {
/// The leader executes every scan locally.
Local,
/// The leader delegates immutable sealed scan leaves.
Distributed,
}
/// Closed phase where a Bifrost security invariant failed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum BifrostSecurityPhase {
/// Query planning and binding.
Planning,
/// Source access or tenant tripwire.
Source,
/// Private peer authentication and execution.
Peer,
}
/// Closed Bifrost security violation classes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum BifrostSecurityViolationKind {
/// Authenticated tenant binding mismatch.
TenantBinding,
/// Tenant-scoped object path mismatch.
TenantPath,
/// A scanned file's footer tenant is missing or foreign.
TenantFile,
/// Invalid peer signature.
PeerSignature,
/// Unknown peer key identifier.
PeerUnknownKey,
/// Replayed peer nonce.
PeerReplay,
/// Wrong peer audience.
PeerAudience,
/// Stale peer fence.
PeerFence,
/// Verified peer tenant mismatch.
PeerTenant,
/// Peer manifest digest mismatch.
PeerManifest,
/// Peer fragment digest mismatch.
PeerFragment,
/// Peer assignment-authority digest mismatch: the recomputed digest over
/// the follower's actual dispatched assignments does not match the
/// digest carried in the peer context claims, so the closed predicate and
/// projection closure cannot be trusted.
PeerAssignmentAuthority,
/// Private stage-operation binding mismatch: an Analytical stage context
/// did not match the receiving follower's own expectation for the
/// operation, either query identity, the pinned snapshot, the stage, the
/// task, the attempt, the reservation, or the authorized permissions, so
/// the operation was refused before any plan decode, task-cache lookup,
/// provider construction, or object I/O.
PeerStageBinding,
}
fn is_secret_like(value: &str) -> bool {
let lower = value.to_ascii_lowercase();
lower.contains("-----begin ")
|| lower.starts_with("bearer ")
|| lower.starts_with("sk-")
|| lower.starts_with("pk-")
|| lower.starts_with("eyj")
|| lower.contains("api_key=")
|| lower.contains("apikey=")
|| lower.contains("authorization=")
|| lower.contains("password=")
|| lower.contains("secret=")
|| lower.contains("token=")
|| lower.contains("/secrets/")
}
/// Closed operation names for storage lifecycle audit rows.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum StorageAuditOperation {
/// An upload session was durably created.
SessionCreated,
/// The object-store backend was initialized.
BackendInitialized,
/// The object-store backend initialization failed.
BackendFailed,
/// An upload completed.
Complete,
/// An upload was aborted.
Abort,
/// A download was initialized.
Download,
/// A stale upload was reclaimed.
Reclaimed,
}
/// Closed stable codes used for audit failures and denial reasons.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AuditErrorCode {
/// The caller lacked the required permission.
PermissionDenied,
/// Audit staging was unavailable.
///
/// No longer recorded: audit never refuses an operation. Retained so
/// retained history that carries it still decodes.
AuditUnavailable,
/// The supplied token was invalid.
InvalidToken,
/// The requested resource was not found.
NotFound,
/// The operation failed in the storage backend.
StorageBackendFailure,
/// The operation failed validation.
ValidationFailed,
}
/// Closed card-registration operation names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum CardRegistrationOperation {
/// A card was registered.
Register,
/// A card was updated.
Update,
/// A card was deleted.
Delete,
}
/// Closed card-registration outcomes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum CardRegistrationOutcome {
/// A new card was created.
Created,
/// The registration was an idempotent no-op.
IdempotentNoop,
/// The registration was deduplicated.
Deduplicated,
}
/// Closed card-scope mint operation names.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum CardScopeMintKind {
/// Minted during API-key exchange.
ApiKeyExchange,
/// Minted during token refresh.
Refresh,
/// Minted for delegation.
Delegation,
/// Minted during JWT bearer exchange.
JwtBearer,
}
/// One verified delegation hop projected into a durable audit record.
///
/// The projection is deliberately identity-only: a delegation chain reaches
/// audit from a token the verifier already accepted, so the record needs the
/// delegator's identity and the card authority it was acting under, never the
/// credential that carried them. Steps are stored in the same initiator-first
/// order the verifier produced, because the order is the delegation, not an
/// incidental encoding.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
pub struct AuditDelegationStep {
/// Stable identity of the delegating principal.
pub principal_id: PrincipalId,
/// Card-free kind discriminator of the delegating principal.
pub principal_kind: PrincipalKindTag,
/// Card bound to the delegator; absent for a `User` principal.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub card_ref: Option<CardRef>,
/// Transitive card authorization the delegator acted under; empty for a
/// `User` principal.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub card_ref_scope: Vec<CardRef>,
}
/// Structured detail for one auditable operation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AuditDetail {
/// Bounded deployment-wide aggregates captured by Oracle admission recovery.
OracleAdmissionRecovery {
/// Number of expired admission leases removed by recovery.
expired_lease_count: u64,
/// Number of unexpired admission leases retained by recovery.
active_lease_count: u64,
/// Interactive slot units retained across active leases.
interactive_slots: u64,
/// Analytical slot units retained across active leases.
analytical_slots: u64,
/// Total slot units retained across every active lease.
total_slots: u64,
},
/// Immutable, scrubbed Bifrost query read decision.
BifrostQueryReadDecision {
/// Digest of the normalized query.
query_digest: QueryAuditDigest,
/// Server-derived admission class.
query_class: QueryClass,
/// Sorted tenant-table binding digests.
binding_digests: Vec<QueryAuditDigest>,
/// Pinned snapshot summary digest.
snapshot_digest: QueryAuditDigest,
/// Hot manifest summary digest.
manifest_digest: QueryAuditDigest,
/// Authorized projection digest.
projection_digest: QueryAuditDigest,
/// Permission decision digest.
permission_digest: QueryAuditDigest,
/// Local or distributed execution decision.
execution: QueryExecutionMode,
/// Selected execution nodes including leader.
selected_node_count: u8,
/// Selected workers excluding leader.
worker_count: u8,
/// Total admission slot demand.
slot_units: u32,
/// Whole-cut retry ordinal, zero or one.
retry_ordinal: u8,
/// Settled deadline in milliseconds.
deadline_ms: u64,
/// Verified initiator-first delegation chain, empty when the caller
/// presented no `act` claim. Omitted from the canonical encoding when
/// empty so records written before delegation attribution keep their
/// original bytes and hash.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
delegation_chain: Vec<AuditDelegationStep>,
},
/// Scrubbed tenant or peer security violation.
BifrostSecurityViolation {
/// Closed violation class.
violation: BifrostSecurityViolationKind,
/// Boundary where validation failed.
phase: BifrostSecurityPhase,
/// Trusted query digest when one exists.
query_digest: Option<QueryAuditDigest>,
/// Verified initiator-first delegation chain, empty and omitted when
/// the refused caller presented no `act` claim.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
delegation_chain: Vec<AuditDelegationStep>,
},
/// A live Iceberg file replacement and its recoverable external boundary.
///
/// The ordered paths are the exact current-snapshot inputs and the exact
/// rewrite outputs. `base_snapshot_id` is the plan fence, never an input
/// file's addition snapshot.
ForgeIcebergRewrite {
/// Stable identity shared by output names, Iceberg, and audit rows.
operation_id: uuid::Uuid,
/// Durable lifecycle transition represented by this row.
phase: ForgeIcebergRewritePhase,
/// Canonical tenant/table resource identity.
group: String,
/// Snapshot observed by the table plan before discovery.
base_snapshot_id: i64,
/// Snapshot returned after a proven replacement commit.
committed_snapshot_id: Option<i64>,
/// Destination partition specification identity.
partition_spec_id: i32,
/// Shared exact time partition for every input in the rewrite group.
time_partition: TimePartitionWire,
/// Target output size captured from the table metadata.
target_file_size_bytes: u64,
/// Exact ordered catalog paths deleted by the Iceberg action.
input_paths: Vec<StoragePath>,
/// Exact ordered rewritten object paths added by the Iceberg action.
output_paths: Vec<StoragePath>,
},
/// A Forge Scribe-promotion operation and its Iceberg catalog boundary.
///
/// Promotion appends already-published hot objects unchanged, so the row
/// records exactly which `file_list` rows and object paths moved, the
/// snapshot the append started from, the snapshot it produced when known,
/// and the digest binding that ordered file set to this operation.
ForgeScribePromotion {
/// Deterministic identifier shared by prepared and terminal rows.
operation_id: uuid::Uuid,
/// Durable phase represented by this audit row.
phase: ForgeScribePromotionPhase,
/// Canonical tenant/table resource identity.
group: String,
/// Snapshot observed before the promotion append.
base_snapshot_id: i64,
/// Snapshot returned by a proven promotion commit, when known.
committed_snapshot_id: Option<i64>,
/// Exact ordered `file_list` rows promoted by the operation.
input_file_ids: Vec<uuid::Uuid>,
/// Exact ordered logical object paths promoted by the operation.
input_paths: Vec<StoragePath>,
/// Digest binding the ordered promoted file set to this operation.
promoted_file_set_digest: ForgePromotedFileSetDigest,
},
/// A Forge snapshot-expiry operation and its Iceberg metadata boundary.
ForgeSnapshotExpire {
/// Deterministic identifier shared by prepared and terminal rows.
operation_id: uuid::Uuid,
/// Durable phase represented by this audit row.
phase: ForgeSnapshotExpirePhase,
/// Canonical tenant/table resource identity.
group: String,
/// Metadata location observed before the expiry commit.
base_metadata_location: StoragePath,
/// Current snapshot observed before the expiry commit.
current_snapshot_id: Option<i64>,
/// Snapshot IDs at the heads of retained Iceberg refs.
retained_ref_heads: Vec<i64>,
/// Exact snapshot IDs selected for expiry, sorted ascending.
selected_snapshot_ids: Vec<i64>,
},
/// A Forge metadata-only manifest rewrite and its Iceberg boundary.
///
/// The operation regroups manifest entries and nothing else: the data files
/// the table logically contains are identical before and after it. Both
/// path collections are exact and ordered, so a reader can prove which
/// manifests were replaced by which without consulting the catalog.
ForgeManifestRewrite {
/// Deterministic identifier shared by prepared and terminal rows.
operation_id: uuid::Uuid,
/// Durable phase represented by this audit row.
phase: ForgeManifestRewritePhase,
/// Canonical tenant/table resource identity.
group: String,
/// Metadata location observed before the rewrite commit.
base_metadata_location: StoragePath,
/// Metadata location returned by a proven commit, when known.
committed_metadata_location: Option<StoragePath>,
/// Exact ordered manifests replaced by the rewrite.
input_manifest_paths: Vec<StoragePath>,
/// Exact ordered manifests written by the rewrite.
output_manifest_paths: Vec<StoragePath>,
},
/// A Forge orphan-GC operation and its bounded object batch.
ForgeOrphanGc {
/// Deterministic identifier shared by prepared and terminal rows.
operation_id: uuid::Uuid,
/// Durable phase represented by this audit row.
phase: ForgeOrphanGcPhase,
/// Canonical tenant/table resource identity.
group: String,
/// Exact sorted object candidates observed before deletion.
candidate_paths: Vec<StoragePath>,
/// Objects deleted by the terminal attempt.
deleted_paths: Vec<StoragePath>,
/// Objects skipped after the final live-set/age check.
skipped_paths: Vec<StoragePath>,
},
/// Authentication failure metadata; credentials are never representable here.
AuthFailure {
/// Stable reason for the authentication refusal.
error_code: AuditErrorCode,
},
/// API-key issuance metadata; the key value is never representable here.
CredentialIssuance {
/// Principal receiving the credential.
target_principal_id: PrincipalId,
/// Identifier of the issued API key, not its secret value.
api_key_id: uuid::Uuid,
/// Credential expiry.
expires_at: chrono::DateTime<chrono::Utc>,
/// Verified initiator-first delegation chain of the issuing request,
/// empty and omitted when the caller presented no `act` claim.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
delegation_chain: Vec<AuditDelegationStep>,
},
/// Token exchange metadata; bearer values are never representable here.
TokenExchange {
/// Subject principal in the exchanged token.
subject_principal_id: PrincipalId,
/// Principal that performed the exchange.
actor_principal_id: PrincipalId,
/// Typed delegation chain.
delegation_chain: Vec<CardRef>,
/// Token expiry.
expires_at: chrono::DateTime<chrono::Utc>,
},
/// Refresh-token family revocation metadata.
RefreshFamilyRevocation {
/// Principal whose refresh-token family was revoked.
principal_id: PrincipalId,
/// Principal kind owning the family.
principal_kind: PrincipalKindTag,
/// Number of active refresh rows revoked.
revoked_token_count: u64,
},
/// Operator-driven suspension of one principal, stopping all further issuance.
///
/// The row exists to answer "who ended this identity, and why": the
/// operator's justification is the one part of the decision that cannot be
/// reconstructed from the surrounding state, so it is carried here rather
/// than left to the request log. The chain is folded in because this
/// detail replaces the attribution-only detail the boundary would
/// otherwise have written.
PrincipalRevocation {
/// Principal suspended so none of its credentials mint again.
principal_id: PrincipalId,
/// Kind the caller declared, and the table the revocation resolved in.
principal_kind: PrincipalKindTag,
/// Operator-supplied justification, screened for secrets.
reason: RevocationReason,
/// Verified initiator-first delegation chain, empty and omitted when
/// the caller presented no `act` claim.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
delegation_chain: Vec<AuditDelegationStep>,
},
/// Authorization decision metadata; the event's subject/actor pair names
/// who was authorized.
AuthzCheck {
/// Typed delegation chain.
delegation_chain: Vec<CardRef>,
/// Authorization outcome.
outcome: AuditOutcome,
/// Stable denial reason, present only for a denial.
deny_reason: Option<AuditErrorCode>,
},
/// Card registration metadata.
CardRegistration {
/// Registered card UID.
card_uid: CardUid,
/// Registered card kind.
card_kind: CardKind,
/// Registration operation.
operation: CardRegistrationOperation,
/// Registration outcome, when applicable.
outcome: Option<CardRegistrationOutcome>,
/// Prior spec hash, when applicable.
before_spec_hash: Option<SpecHash>,
/// Resulting spec hash, when applicable.
after_spec_hash: Option<SpecHash>,
},
/// Card-scope mint metadata; scope members are references, never secrets.
CardScopeMint {
/// How the scope was minted.
mint_kind: CardScopeMintKind,
/// Root card reference.
root_card_ref: CardRef,
/// Stable scope digest.
scope_hash: Option<ScopeHash>,
/// Number of scope members.
scope_member_count: Option<u32>,
/// Typed scope members.
scope_members: Vec<CardRef>,
/// Stable failure code, present only on failure.
failure_code: Option<AuditErrorCode>,
},
/// Storage lifecycle transition metadata.
Storage {
/// Storage transition.
operation: StorageAuditOperation,
/// Multipart upload identifier, when applicable.
upload_id: Option<uuid::Uuid>,
/// Logical storage path.
storage_path: StoragePath,
/// Closed backend identifier.
backend: StorageBackend,
/// HTTP/backend status code.
status_code: u16,
/// Stable failure code, when applicable.
error_code: Option<AuditErrorCode>,
},
/// Delegation attribution for an audited operation that carries no
/// operation-specific detail of its own.
///
/// This variant exists so an RBAC denial or other detail-free event can
/// still name who was acting for whom. It never replaces an
/// operation-specific detail: an event that already has one carries its
/// delegation inside that detail instead.
DelegationAttribution {
/// Verified initiator-first delegation chain.
delegation_chain: Vec<AuditDelegationStep>,
},
/// Ingest batch metadata.
Ingest {
/// Code-origin of the emitting card or agent.
origin: Origin,
/// Idempotent batch identifier.
batch_id: BatchId,
/// Destination Bifrost table.
table: crate::vala::api::BifrostTableName,
/// Number of records in the batch.
record_count: u64,
/// Ingest authorization outcome.
outcome: AuditOutcome,
},
}
impl AuditDetail {
/// Validates bounded Bifrost audit collection and topology invariants.
///
/// Other variants contain their own constructor-validated scalar values.
///
/// # Errors
/// Returns [`AuditDetailValueError`] when a Bifrost read decision exceeds
/// 64 bindings/nodes or carries inconsistent execution/retry/deadline data.
pub fn validate(&self) -> Result<(), AuditDetailValueError> {
let Self::BifrostQueryReadDecision {
binding_digests,
execution,
selected_node_count,
worker_count,
slot_units,
retry_ordinal,
deadline_ms,
..
} = self
else {
return Ok(());
};
if binding_digests.is_empty() || binding_digests.len() > 64 {
return Err(AuditDetailValueError::InvalidCombination {
invariant: "binding count",
});
}
if binding_digests
.windows(2)
.any(|pair| pair[0].as_str() >= pair[1].as_str())
{
return Err(AuditDetailValueError::InvalidCombination {
invariant: "binding digest order",
});
}
if !(1..=64).contains(selected_node_count)
|| *retry_ordinal > 1
|| *slot_units == 0
|| *deadline_ms == 0
{
return Err(AuditDetailValueError::InvalidCombination {
invariant: "Bifrost read bounds",
});
}
let expected_workers = match execution {
QueryExecutionMode::Local => 0,
QueryExecutionMode::Distributed => selected_node_count - 1,
};
if *worker_count != expected_workers {
return Err(AuditDetailValueError::InvalidCombination {
invariant: "execution topology",
});
}
Ok(())
}
}
/// Durable phase recorded for a live Iceberg replacement operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum ForgeIcebergRewritePhase {
/// Rewritten outputs were persisted before the external catalog commit.
Prepared,
/// The exact Iceberg replacement commit completed.
Committed,
/// Reconciliation proved a previously uncertain commit completed.
Recovered,
/// Reconciliation proved the prepared operation was not committed.
Reset,
}
/// Durable phase recorded for a Forge Scribe-promotion operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum ForgeScribePromotionPhase {
/// The exact promoted file set was fixed before the catalog append.
Prepared,
/// The catalog append completed and returned a promotion snapshot.
Committed,
/// Reconciliation proved a previously uncertain append had completed.
Recovered,
/// Reconciliation proved the prepared append was not committed.
Reset,
}
/// Durable phase recorded for a Forge snapshot-expiry operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum ForgeSnapshotExpirePhase {
/// Snapshot IDs were selected and the external commit is about to start.
Prepared,
/// The external Iceberg commit completed.
Committed,
/// Reconciliation proved the external commit completed.
Recovered,
}
/// Durable phase recorded for a Forge metadata-only manifest rewrite.
///
/// The rewrite writes new manifests and then swaps them in with one catalog
/// commit, so it has the same recoverable external boundary as data rewrite:
/// a prepared operation whose commit outcome is unknown is settled by
/// reconciliation into exactly one of `Recovered` or `Reset`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum ForgeManifestRewritePhase {
/// The exact input and output manifests were fixed before the commit.
Prepared,
/// The catalog commit completed and returned a metadata location.
Committed,
/// Reconciliation proved a previously uncertain commit completed.
Recovered,
/// Reconciliation proved the prepared commit was not applied.
Reset,
}
/// Durable transition recorded for one epoch's per-table reader protection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum OracleTableProtectionPhase {
/// First protection for the table, or a conservative widening of it.
Expanded,
/// A narrowing that still protects every remaining active cut.
Narrowed,
/// The last active cut ended and the protection header was removed.
Released,
}
/// Durable phase recorded for a Forge orphan-GC operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum ForgeOrphanGcPhase {
/// A bounded candidate batch was prepared for deletion.
Prepared,
/// Every eligible candidate in the batch was handled.
Committed,
/// Reconciliation completed a previously prepared batch.
Recovered,
}
/// Closed storage backend identifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[cfg_attr(feature = "server", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum StorageBackend {
/// Local filesystem backend.
Local,
/// Amazon S3 backend.
S3,
/// Google Cloud Storage backend.
Gcs,
/// Azure Blob Storage backend.
Azure,
}
/// Serialize detail into the deterministic JSON string used as the audit hash preimage.
///
/// # Panics
///
/// Panics if `AuditDetail` fails JSON serialization, which it cannot.
#[must_use]
pub fn audit_detail_canonical_json(detail: &AuditDetail) -> String {
serde_jcs::to_string(detail).expect("AuditDetail is always JSON-serializable")
}
#[cfg(test)]
mod tests {
use super::{
AuditDetail, AuditDetailValueError, AuditErrorCode, BatchId, QueryAuditDigest,
QueryExecutionMode, ScopeHash, StorageAuditOperation, StoragePath,
audit_detail_canonical_json,
};
use crate::auth::{PrincipalId, PrincipalKindTag};
use crate::origin::{CommitSha, Origin};
use crate::request_id::RequestId;
use crate::vala::api::QueryClass;
use crate::vala::api::{AuditEvent, AuditOutcome, BifrostTableName};
#[test]
fn canonical_json_is_compact_and_stable() {
let detail = AuditDetail::Storage {
operation: StorageAuditOperation::BackendFailed,
upload_id: None,
storage_path: StoragePath::new("cards/a").expect("valid path"),
backend: super::StorageBackend::S3,
status_code: 503,
error_code: Some(AuditErrorCode::StorageBackendFailure),
};
assert_eq!(
audit_detail_canonical_json(&detail),
r#"{"backend":"s3","error_code":"STORAGE_BACKEND_FAILURE","kind":"storage","operation":"backend_failed","status_code":503,"storage_path":"cards/a","upload_id":null}"#
);
assert!(!audit_detail_canonical_json(&detail).contains(' '));
}
/// Pins the scrubbed recovery aggregate as a closed canonical contract.
#[test]
fn oracle_admission_recovery_is_bounded_and_canonical() {
let detail = AuditDetail::OracleAdmissionRecovery {
expired_lease_count: 2,
active_lease_count: 3,
interactive_slots: 5,
analytical_slots: 8,
total_slots: 13,
};
let canonical = audit_detail_canonical_json(&detail);
assert_eq!(
canonical,
r#"{"active_lease_count":3,"analytical_slots":8,"expired_lease_count":2,"interactive_slots":5,"kind":"oracle_admission_recovery","total_slots":13}"#
);
assert_eq!(
serde_json::from_str::<AuditDetail>(&canonical).expect("deserialize recovery detail"),
detail
);
assert_eq!(
serde_json::to_value(&detail)
.expect("serialize recovery detail")
.as_object()
.expect("recovery detail object")
.len(),
6
);
}
/// Preserves the live replacement audit shape across its persisted JSON boundary.
#[test]
fn forge_iceberg_rewrite_round_trips_with_explicit_plan_base() {
let detail = AuditDetail::ForgeIcebergRewrite {
operation_id: uuid::Uuid::from_u128(7),
phase: super::ForgeIcebergRewritePhase::Prepared,
group: "bifrost://tenant/vala/table".to_owned(),
base_snapshot_id: 41,
committed_snapshot_id: None,
partition_spec_id: 3,
time_partition: crate::vala::api::TimePartitionWire::new(
crate::vala::api::TimeGranularityWire::Hour,
chrono::DateTime::from_timestamp(1_767_312_000, 0).expect("fixture instant"),
)
.expect("fixture instant is an exact hour boundary"),
target_file_size_bytes: 1024,
input_paths: vec![StoragePath::new("table/live-a.parquet").expect("valid input")],
output_paths: vec![StoragePath::new("table/rewrite-a.parquet").expect("valid output")],
};
let json = serde_json::to_value(&detail).expect("serialize detail");
assert_eq!(json["kind"], "forge_iceberg_rewrite");
assert_eq!(json["base_snapshot_id"], 41);
assert_eq!(
serde_json::from_value::<AuditDetail>(json).expect("deserialize detail"),
detail
);
}
#[test]
fn all_detail_variants_round_trip() {
let origin = Origin {
repo: "github.com/bohmian-ai/wyrd".to_string(),
commit: CommitSha::new("0123456").expect("valid commit"),
path: Some("cards/agent.yaml".to_string()),
dirty: false,
};
let detail = AuditDetail::Ingest {
origin,
batch_id: BatchId::new("batch-1").expect("valid batch id"),
table: BifrostTableName::new("vala.events"),
record_count: 2,
outcome: AuditOutcome::Allowed,
};
let value = serde_json::to_value(&detail).expect("serialize");
let back: AuditDetail = serde_json::from_value(value).expect("deserialize");
assert_eq!(detail, back);
let _ = PrincipalId::new(uuid::Uuid::now_v7());
}
#[test]
fn constructors_normalize_and_reject_secret_like_values() {
assert_eq!(
StoragePath::new(" cards/a ")
.expect("valid path")
.as_str(),
"cards/a"
);
assert!(matches!(
ScopeHash::new("Bearer very-secret"),
Err(AuditDetailValueError::SecretLike {
field: "scope_hash"
})
));
assert!(matches!(
StoragePath::new("/var/run/secrets/wyrd/api-key"),
Err(AuditDetailValueError::SecretLike {
field: "storage_path"
})
));
assert!(matches!(
BatchId::new("token=plaintext"),
Err(AuditDetailValueError::SecretLike { field: "batch_id" })
));
}
/// Delegation attribution is hash-covered when present and invisible when
/// absent.
///
/// The two halves are one claim: an audit chain can only stay verifiable
/// across this change if a record written without delegation keeps the
/// exact bytes it had before the field existed, while a delegated record
/// carries the chain inside the same canonical detail the entry hash covers.
#[test]
fn delegation_attribution_is_omitted_when_absent_and_hashed_when_present() {
let digest = || QueryAuditDigest::new("sha256:abc").expect("valid digest");
let read = |delegation_chain: Vec<super::AuditDelegationStep>| {
AuditDetail::BifrostQueryReadDecision {
query_digest: digest(),
query_class: QueryClass::Interactive,
binding_digests: vec![digest()],
snapshot_digest: digest(),
manifest_digest: digest(),
projection_digest: digest(),
permission_digest: digest(),
execution: QueryExecutionMode::Local,
selected_node_count: 1,
worker_count: 0,
slot_units: 1,
retry_ordinal: 0,
deadline_ms: 100,
delegation_chain,
}
};
let historical = audit_detail_canonical_json(&read(Vec::new()));
assert!(
!historical.contains("delegation_chain"),
"a nondelegated record keeps its original encoding: {historical}"
);
let step = super::AuditDelegationStep {
principal_id: PrincipalId::new(uuid::Uuid::nil()),
principal_kind: PrincipalKindTag::Service,
card_ref: None,
card_ref_scope: Vec::new(),
};
let delegated = audit_detail_canonical_json(&read(vec![step.clone()]));
assert_ne!(
delegated, historical,
"delegation changes the canonical detail the entry hash covers"
);
assert!(delegated.contains("delegation_chain"));
assert_ne!(
audit_detail_canonical_json(&read(vec![step.clone(), step.clone()])),
delegated,
"a longer chain is a different canonical record"
);
// Order is the delegation, so reversing it must change the record.
let other = super::AuditDelegationStep {
principal_id: PrincipalId::new(uuid::Uuid::max()),
..step.clone()
};
assert_ne!(
audit_detail_canonical_json(&read(vec![step.clone(), other.clone()])),
audit_detail_canonical_json(&read(vec![other, step])),
"initiator-first order survives canonicalization"
);
}
/// Bifrost read details serialize only digests and enforce topology bounds.
#[test]
fn bifrost_read_detail_is_scrubbed_and_bounded() {
let digest = || QueryAuditDigest::new("sha256:abc").expect("valid digest");
let detail = AuditDetail::BifrostQueryReadDecision {
query_digest: digest(),
query_class: QueryClass::Interactive,
binding_digests: vec![digest()],
snapshot_digest: digest(),
manifest_digest: digest(),
projection_digest: digest(),
permission_digest: digest(),
execution: QueryExecutionMode::Local,
selected_node_count: 1,
worker_count: 0,
slot_units: 1,
retry_ordinal: 0,
deadline_ms: 100,
delegation_chain: Vec::new(),
};
detail.validate().expect("bounded detail validates");
let json = audit_detail_canonical_json(&detail);
assert!(!json.contains("SELECT"));
assert!(!json.contains("/var/"));
let invalid = AuditDetail::BifrostQueryReadDecision {
query_digest: digest(),
query_class: QueryClass::Interactive,
binding_digests: (0..65).map(|_| digest()).collect(),
snapshot_digest: digest(),
manifest_digest: digest(),
projection_digest: digest(),
permission_digest: digest(),
execution: QueryExecutionMode::Local,
selected_node_count: 1,
worker_count: 0,
slot_units: 1,
retry_ordinal: 0,
deadline_ms: 100,
delegation_chain: Vec::new(),
};
assert!(matches!(
invalid.validate(),
Err(AuditDetailValueError::InvalidCombination {
invariant: "binding count"
})
));
let duplicate = AuditDetail::BifrostQueryReadDecision {
query_digest: digest(),
query_class: QueryClass::Interactive,
binding_digests: vec![digest(), digest()],
snapshot_digest: digest(),
manifest_digest: digest(),
projection_digest: digest(),
permission_digest: digest(),
execution: QueryExecutionMode::Local,
selected_node_count: 1,
worker_count: 0,
slot_units: 1,
retry_ordinal: 0,
deadline_ms: 100,
delegation_chain: Vec::new(),
};
assert!(matches!(
duplicate.validate(),
Err(AuditDetailValueError::InvalidCombination {
invariant: "binding digest order"
})
));
let unsorted = AuditDetail::BifrostQueryReadDecision {
query_digest: digest(),
query_class: QueryClass::Interactive,
binding_digests: vec![
QueryAuditDigest::new("sha256:z").expect("valid digest"),
QueryAuditDigest::new("sha256:a").expect("valid digest"),
],
snapshot_digest: digest(),
manifest_digest: digest(),
projection_digest: digest(),
permission_digest: digest(),
execution: QueryExecutionMode::Local,
selected_node_count: 1,
worker_count: 0,
slot_units: 1,
retry_ordinal: 0,
deadline_ms: 100,
delegation_chain: Vec::new(),
};
assert!(unsorted.validate().is_err());
}
#[test]
fn serde_cannot_bypass_secret_classification() {
for (field, value) in [
("scope_hash", serde_json::json!("api_key=plaintext")),
("storage_path", serde_json::json!("Bearer plaintext")),
("batch_id", serde_json::json!("password=plaintext")),
] {
let json = match field {
"scope_hash" => serde_json::json!({
"kind": "card_scope_mint",
"mint_kind": "refresh",
"root_card_ref": {"kind": "Agent", "name": "worker", "version": "1.0.0", "space": "prod"},
"scope_hash": value,
"scope_members": []
}),
"storage_path" => serde_json::json!({
"kind": "storage",
"operation": "complete",
"storage_path": value,
"backend": "s3",
"status_code": 200
}),
_ => serde_json::json!({
"kind": "ingest",
"origin": {"repo": "github.com/bohmian-ai/wyrd", "commit": "0123456", "dirty": false},
"batch_id": value,
"table": "vala.events",
"record_count": 1,
"outcome": "allowed"
}),
};
assert!(
serde_json::from_value::<AuditDetail>(json).is_err(),
"{field}"
);
}
}
#[test]
fn audit_detail_golden_vectors_cover_all_variants_and_nested_values() {
let vectors = [
(
r#"{"expires_at":"2026-01-02T03:04:05Z","kind":"credential_issuance","target_principal_id":"00000000-0000-0000-0000-000000000001","api_key_id":"00000000-0000-0000-0000-000000000002"}"#,
r#"{"api_key_id":"00000000-0000-0000-0000-000000000002","expires_at":"2026-01-02T03:04:05Z","kind":"credential_issuance","target_principal_id":"00000000-0000-0000-0000-000000000001"}"#,
),
(
r#"{"kind":"token_exchange","expires_at":"2026-01-02T03:04:05Z","delegation_chain":[{"space":"prod","version":"1.0.0","name":"worker","kind":"Agent"}],"actor_principal_id":"00000000-0000-0000-0000-000000000001","subject_principal_id":"00000000-0000-0000-0000-000000000002"}"#,
r#"{"actor_principal_id":"00000000-0000-0000-0000-000000000001","delegation_chain":[{"kind":"Agent","name":"worker","space":"prod","version":"1.0.0"}],"expires_at":"2026-01-02T03:04:05Z","kind":"token_exchange","subject_principal_id":"00000000-0000-0000-0000-000000000002"}"#,
),
(
r#"{"deny_reason":"PERMISSION_DENIED","delegation_chain":[],"outcome":"denied","kind":"authz_check"}"#,
r#"{"delegation_chain":[],"deny_reason":"PERMISSION_DENIED","kind":"authz_check","outcome":"denied"}"#,
),
(
r#"{"after_spec_hash":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","card_uid":"01890f28-7c4a-7cc3-98e7-4f4a3c2d1b00","operation":"register","kind":"card_registration","card_kind":"Agent","outcome":"created","before_spec_hash":null}"#,
r#"{"after_spec_hash":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","before_spec_hash":null,"card_kind":"Agent","card_uid":"01890f28-7c4a-7cc3-98e7-4f4a3c2d1b00","kind":"card_registration","operation":"register","outcome":"created"}"#,
),
(
r#"{"scope_members":[{"kind":"Agent","name":"worker","version":"1.0.0","space":"prod"}],"root_card_ref":{"space":"prod","name":"root","version":"1.0.0","kind":"Agent"},"scope_hash":"scope-digest","mint_kind":"refresh","kind":"card_scope_mint","scope_member_count":1,"failure_code":null}"#,
r#"{"failure_code":null,"kind":"card_scope_mint","mint_kind":"refresh","root_card_ref":{"kind":"Agent","name":"root","space":"prod","version":"1.0.0"},"scope_hash":"scope-digest","scope_member_count":1,"scope_members":[{"kind":"Agent","name":"worker","space":"prod","version":"1.0.0"}]}"#,
),
(
r#"{"status_code":200,"storage_path":" cards/a ","backend":"s3","operation":"complete","kind":"storage","upload_id":null,"error_code":null}"#,
r#"{"backend":"s3","error_code":null,"kind":"storage","operation":"complete","status_code":200,"storage_path":"cards/a","upload_id":null}"#,
),
(
r#"{"record_count":2,"table":"vala.events","outcome":"allowed","batch_id":"batch-1","origin":{"dirty":false,"path":"cards/agent.yaml","commit":"0123456","repo":"github.com/bohmian-ai/wyrd"},"kind":"ingest"}"#,
r#"{"batch_id":"batch-1","kind":"ingest","origin":{"commit":"0123456","path":"cards/agent.yaml","repo":"github.com/bohmian-ai/wyrd"},"outcome":"allowed","record_count":2,"table":"vala.events"}"#,
),
];
for (input, expected) in vectors {
let detail: AuditDetail = serde_json::from_str(input).expect("golden input");
assert_eq!(audit_detail_canonical_json(&detail), expected);
}
}
#[test]
fn audit_event_golden_shape_distinguishes_absent_and_present_detail() {
let event = AuditEvent::new(
RequestId::now_v7(),
None,
"bifrost.ingest".to_owned(),
"vala.events".to_owned(),
None,
crate::auth::Attribution::direct(PrincipalId::new(uuid::Uuid::now_v7())),
PrincipalKindTag::User,
"bifrost:write".to_owned(),
AuditOutcome::Allowed,
);
let absent = serde_json::to_value(&event).expect("serialize absent detail");
assert!(absent.get("detail").is_none());
let present = event.with_detail(AuditDetail::Storage {
operation: StorageAuditOperation::Complete,
upload_id: None,
storage_path: StoragePath::new("cards/a").expect("valid path"),
backend: super::StorageBackend::S3,
status_code: 200,
error_code: None,
});
assert_eq!(
serde_json::to_value(present).expect("serialize present detail")["detail"]["kind"],
"storage"
);
}
/// The promoted-file-set digest newtype admits only the locked canonical
/// form: the literal `sha256:` prefix followed by exactly 64 lowercase
/// hexadecimal characters.
///
/// Every other shape a caller could plausibly produce — an uppercase
/// digest, a bare hex digest, a different algorithm label, a truncated or
/// over-long body, or a non-hexadecimal character — must be refused at the
/// constructor so no audit row can carry an unverifiable promotion
/// identity.
#[test]
fn forge_promoted_file_set_digest_accepts_only_canonical_sha256() {
let canonical = format!("sha256:{}", "ab12cd34".repeat(8));
assert_eq!(canonical.len(), 7 + 64);
let digest = super::ForgePromotedFileSetDigest::new(&canonical).expect("canonical digest");
assert_eq!(digest.as_str(), canonical);
for rejected in [
canonical.to_ascii_uppercase(),
format!("SHA256:{}", "ab12cd34".repeat(8)),
"ab12cd34".repeat(8),
format!("sha512:{}", "ab12cd34".repeat(8)),
format!("sha256:{}", "ab12cd34".repeat(7)),
format!("sha256:{}0", "ab12cd34".repeat(8)),
format!("sha256:{}zz", "ab12cd34".repeat(7) + "ab12cd"),
"sha256:".to_owned(),
String::new(),
] {
assert!(
super::ForgePromotedFileSetDigest::new(&rejected).is_err(),
"digest newtype accepted non-canonical value `{rejected}`"
);
}
}
/// The digest preimage is exactly the locked byte stream: the
/// `wyrd.forge.scribe-promotion.v1` domain separator followed, in promoted
/// order, by each tuple's lowercase hyphenated file-list UUID, canonical
/// logical path, and normalized checksum, every field prefixed by its
/// unsigned 64-bit big-endian UTF-8 byte length.
///
/// The independent recomputation below pins the separator, the field
/// order, and the framing. The mutations then prove sensitivity: reordering
/// the tuples changes the digest, and shifting one byte across a field
/// boundary changes it too, which unframed concatenation could not detect.
#[test]
fn forge_promoted_file_set_digest_is_ordered_length_framed_and_domain_separated() {
use sha2::Digest as _;
fn expected(files: &[super::ForgePromotedFile]) -> String {
fn framed(hasher: &mut sha2::Sha256, field: &str) {
hasher.update((field.len() as u64).to_be_bytes());
hasher.update(field.as_bytes());
}
let mut hasher = sha2::Sha256::new();
framed(&mut hasher, "wyrd.forge.scribe-promotion.v1");
for file in files {
framed(&mut hasher, &file.file_id().hyphenated().to_string());
framed(&mut hasher, file.path().as_str());
framed(&mut hasher, file.checksum());
}
format!("sha256:{}", hex::encode(hasher.finalize()))
}
let first = super::ForgePromotedFile::new(
uuid::Uuid::from_u128(1),
StoragePath::new("tenant/table/data/a.parquet").expect("path"),
"AB12",
)
.expect("first promoted file");
let second = super::ForgePromotedFile::new(
uuid::Uuid::from_u128(2),
StoragePath::new("tenant/table/data/b.parquet").expect("path"),
"cd34",
)
.expect("second promoted file");
assert_eq!(first.checksum(), "ab12", "checksum must be normalized");
let ordered = [first.clone(), second.clone()];
let digest = super::ForgePromotedFileSetDigest::compute(&ordered);
assert_eq!(digest.as_str(), expected(&ordered));
let reversed = [second, first.clone()];
assert_ne!(
super::ForgePromotedFileSetDigest::compute(&reversed).as_str(),
digest.as_str(),
"digest must depend on promoted order"
);
let shifted = [
first,
super::ForgePromotedFile::new(
uuid::Uuid::from_u128(2),
StoragePath::new("tenant/table/data/b.parquetc").expect("path"),
"d34",
)
.expect("boundary-shifted promoted file"),
];
assert_ne!(
super::ForgePromotedFileSetDigest::compute(&shifted).as_str(),
super::ForgePromotedFileSetDigest::compute(&ordered).as_str(),
"length framing must separate adjacent fields"
);
}
/// The Scribe-promotion audit detail serializes under the locked
/// `forge_scribe_promotion` kind, carries exactly the eight locked fields,
/// round-trips without loss, and refuses an unknown phase.
#[test]
fn forge_scribe_promotion_audit_detail_round_trips_exactly() {
let promoted = [super::ForgePromotedFile::new(
uuid::Uuid::from_u128(7),
StoragePath::new("tenant/table/data/a.parquet").expect("path"),
"ab12",
)
.expect("promoted file")];
let detail = AuditDetail::ForgeScribePromotion {
operation_id: uuid::Uuid::from_u128(11),
phase: super::ForgeScribePromotionPhase::Committed,
group: "tenant/00000000-0000-0000-0000-000000000001/table/events".to_owned(),
base_snapshot_id: 41,
committed_snapshot_id: Some(42),
input_file_ids: vec![uuid::Uuid::from_u128(7)],
input_paths: vec![StoragePath::new("tenant/table/data/a.parquet").expect("path")],
promoted_file_set_digest: super::ForgePromotedFileSetDigest::compute(&promoted),
};
detail.validate().expect("promotion detail is valid");
let value = serde_json::to_value(&detail).expect("serialize promotion detail");
assert_eq!(value["kind"], "forge_scribe_promotion");
assert_eq!(value["phase"], "committed");
let mut keys: Vec<&str> = value
.as_object()
.expect("object detail")
.keys()
.map(String::as_str)
.collect();
keys.sort_unstable();
assert_eq!(
keys,
[
"base_snapshot_id",
"committed_snapshot_id",
"group",
"input_file_ids",
"input_paths",
"kind",
"operation_id",
"phase",
"promoted_file_set_digest",
]
);
let round_tripped: AuditDetail =
serde_json::from_value(value).expect("deserialize promotion detail");
assert_eq!(round_tripped, detail);
assert!(!audit_detail_canonical_json(&detail).is_empty());
for phase in ["prepared", "committed", "recovered", "reset"] {
let parsed: super::ForgeScribePromotionPhase =
serde_json::from_value(serde_json::Value::String(phase.to_owned()))
.expect("closed phase parses");
assert_eq!(
serde_json::to_value(parsed).expect("phase serializes"),
serde_json::Value::String(phase.to_owned())
);
}
assert!(
serde_json::from_value::<super::ForgeScribePromotionPhase>(serde_json::Value::String(
"settled".to_owned()
))
.is_err(),
"phase set must stay closed"
);
}
/// Previously accepted `forge_manifest_rewrite` audit rows must keep
/// deserializing even though no production strategy can create new ones.
///
/// The variant is historical wire compatibility, so this proves every
/// phase serializes under the canonical kind and round-trips exactly.
#[test]
fn forge_manifest_rewrite_public_audit_detail_round_trips() {
for phase in [
super::ForgeManifestRewritePhase::Prepared,
super::ForgeManifestRewritePhase::Committed,
super::ForgeManifestRewritePhase::Recovered,
super::ForgeManifestRewritePhase::Reset,
] {
let detail = AuditDetail::ForgeManifestRewrite {
operation_id: uuid::Uuid::from_u128(23),
phase,
group: "tenant/00000000-0000-0000-0000-000000000001/table/events".to_owned(),
base_metadata_location: StoragePath::new("tenant/table/metadata/v1.json")
.expect("path"),
committed_metadata_location: Some(
StoragePath::new("tenant/table/metadata/v2.json").expect("path"),
),
input_manifest_paths: vec![
StoragePath::new("tenant/table/metadata/m1.avro").expect("path"),
],
output_manifest_paths: vec![
StoragePath::new("tenant/table/metadata/m2.avro").expect("path"),
],
};
let value = serde_json::to_value(&detail).expect("serialize rewrite detail");
assert_eq!(value["kind"], "forge_manifest_rewrite");
let round_tripped: AuditDetail =
serde_json::from_value(value).expect("deserialize rewrite detail");
assert_eq!(round_tripped, detail);
}
}
}