faucet-core 1.14.0

Shared types, traits, and utilities for the faucet-stream ecosystem
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
//! Pipeline-owned governance assembly (RFC 0013, #875, #870).
//!
//! A [`Governance`] value carries every guarantee a run is held to — masking,
//! quality, contract, schema drift, data-flow policy, budget, profiling,
//! metadata columns, metering, resilience, cancellation and the DLQ — and one
//! builder assembles them in the canonical order. Every entry point (the CLI's
//! run / schedule / serve executor, topology, DLQ replay, `verify --repair`,
//! preview) builds the same value, so a pass cannot be forgotten by omission:
//! leaving one out is [`Governance::without`] with a reason, which is recorded
//! and logged.
//!
//! # The canonical order
//!
//! Per page, inside [`run_stream`](crate::pipeline::run_stream): masking →
//! quality → contract → schema drift, then the write. Around the sink,
//! outermost first ([`SINK_LAYERS`]):
//!
//! ```text
//! policy → profiling → [outer tap] → metadata → [inner tap] → budget → destination
//! ```
//!
//! - **policy** is outermost, so it classifies exactly the records about to be
//!   written (post-masking) and can quarantine a row before anything below
//!   sees it;
//! - **profiling** observes accepted rows without the `_faucet_*` columns;
//! - **metadata** stamps those columns closest to the destination;
//! - **budget** is innermost, so it counts what the destination accepts.
//!
//! The two taps are where a caller layers its own observers (the CLI's lineage
//! sampler sits at the outer tap, its `--limit` and fan-out capture at the
//! inner tap); they never reorder the governance layers.
//!
//! # The DLQ is a governed destination (#870)
//!
//! Records routed to the DLQ go through their own stack, assembled by
//! [`Pipeline::run`](crate::Pipeline::run) when a governance value is attached:
//!
//! ```text
//! masking (rules naming the DLQ) → policy (DLQ's attributes) → budget (shared) → metering → DLQ sink
//! ```
//!
//! A DLQ write the policy refuses fails the run; DLQ records and bytes count
//! against the same budget as the main sink and appear in the
//! [`UsageSnapshot`](crate::usage::UsageSnapshot); DLQ writes are retried under
//! `resilience:` when the DLQ sink's write is replay-safe.

pub(crate) mod apply;
mod page;
pub(crate) mod write;

#[cfg(feature = "arrow")]
pub(crate) use page::GovernedBatch;
pub(crate) use page::{Clearance, GovernedNative};
pub use page::{GovernedEnvelopes, GovernedPage};

use crate::budget::{BudgetSink, BudgetSpec, BudgetState, BudgetTimer};
use crate::dlq::DlqConfig;
use crate::error::FaucetError;
use crate::metadata::{CompiledMetadata, MetadataContext, MetadataSink};
use crate::profiling::{Profiler, ProfilingSink};
use crate::traits::{RowOutcome, Sink};
use crate::usage::UsageMeter;
use async_trait::async_trait;
use serde::Serialize;
use serde_json::Value;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use tokio_util::sync::CancellationToken;

/// A governance pass a run is held to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Pass {
    /// PII masking (runs first on every page).
    Masking,
    /// Data-quality checks.
    Quality,
    /// The data contract.
    Contract,
    /// The schema-drift policy.
    SchemaDrift,
    /// The data-flow policy runtime backstop.
    Policy,
    /// Run budget ceilings.
    Budget,
    /// Column profiling.
    Profiling,
    /// `_faucet_*` metadata columns.
    Metadata,
    /// Usage metering.
    Metering,
    /// The resilience (retry / circuit breaker / poison) policy.
    Resilience,
    /// Cooperative cancellation.
    Cancel,
    /// The dead-letter queue.
    Dlq,
}

impl Pass {
    /// Every pass.
    pub const ALL: &'static [Pass] = &[
        Pass::Masking,
        Pass::Quality,
        Pass::Contract,
        Pass::SchemaDrift,
        Pass::Policy,
        Pass::Budget,
        Pass::Profiling,
        Pass::Metadata,
        Pass::Metering,
        Pass::Resilience,
        Pass::Cancel,
        Pass::Dlq,
    ];

    /// The pass's config spelling.
    pub fn as_str(self) -> &'static str {
        match self {
            Pass::Masking => "masking",
            Pass::Quality => "quality",
            Pass::Contract => "contract",
            Pass::SchemaDrift => "schema_drift",
            Pass::Policy => "policy",
            Pass::Budget => "budget",
            Pass::Profiling => "profiling",
            Pass::Metadata => "metadata",
            Pass::Metering => "metering",
            Pass::Resilience => "resilience",
            Pass::Cancel => "cancel",
            Pass::Dlq => "dlq",
        }
    }
}

impl std::fmt::Display for Pass {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A sink-side governance layer, in [`SINK_LAYERS`] order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SinkLayer {
    /// The data-flow policy backstop.
    Policy,
    /// Column profiling.
    Profiling,
    /// The caller's outer observer.
    OuterTap,
    /// `_faucet_*` metadata columns.
    Metadata,
    /// The caller's inner observer.
    InnerTap,
    /// Budget ceilings.
    Budget,
}

/// The canonical sink-side order, outermost first. A core invariant: the
/// builder applies the layers in exactly this order.
pub const SINK_LAYERS: &[SinkLayer] = &[
    SinkLayer::Policy,
    SinkLayer::Profiling,
    SinkLayer::OuterTap,
    SinkLayer::Metadata,
    SinkLayer::InnerTap,
    SinkLayer::Budget,
];

/// A pass a caller turned off on purpose, with the reason.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct OptOut {
    /// The pass.
    pub pass: Pass,
    /// Why it does not apply to this entry point.
    pub reason: String,
}

/// The data-flow policy runtime backstop for one destination.
#[cfg(feature = "policy")]
#[derive(Clone)]
#[non_exhaustive]
pub struct PolicyGovernance {
    /// The compiled policy.
    pub policy: Arc<crate::policy::CompiledPolicy>,
    /// The destination the policy is evaluated against.
    pub facts: crate::policy::SinkFacts,
    /// Metric labels.
    pub scope: crate::policy::PolicyScope,
    /// The masking pass that ran upstream for this destination.
    pub masking: Option<Arc<crate::masking::CompiledMasking>>,
}

#[cfg(feature = "policy")]
impl PolicyGovernance {
    /// Enforce `policy` for the destination described by `facts`.
    pub fn new(
        policy: Arc<crate::policy::CompiledPolicy>,
        facts: crate::policy::SinkFacts,
        scope: crate::policy::PolicyScope,
    ) -> Self {
        Self {
            policy,
            facts,
            scope,
            masking: None,
        }
    }

    /// The masking pass that ran upstream, so a column it rewrote by name
    /// counts as masked.
    pub fn with_masking(mut self, masking: Arc<crate::masking::CompiledMasking>) -> Self {
        self.masking = Some(masking);
        self
    }

    /// Whether any rule quarantines at run time.
    pub fn quarantines(&self) -> bool {
        self.policy.quarantines()
    }
}

/// The DLQ as a governed destination (#870).
#[derive(Clone)]
#[non_exhaustive]
pub struct DlqGovernance {
    /// The DLQ wiring (sink, `on_batch_error`, failure budgets).
    pub config: DlqConfig,
    /// The DLQ destination as the policy sees it (its own `attributes`).
    #[cfg(feature = "policy")]
    pub facts: Option<crate::policy::SinkFacts>,
    /// Masking rules that name the DLQ (and not the main destination), applied
    /// to each envelope's payload.
    #[cfg(feature = "masking")]
    pub masking: Option<Arc<crate::masking::CompiledMasking>>,
}

impl DlqGovernance {
    /// Govern `config`'s sink.
    pub fn new(config: DlqConfig) -> Self {
        Self {
            config,
            #[cfg(feature = "policy")]
            facts: None,
            #[cfg(feature = "masking")]
            masking: None,
        }
    }

    /// The DLQ destination's identity and attributes for the policy.
    #[cfg(feature = "policy")]
    pub fn with_facts(mut self, facts: crate::policy::SinkFacts) -> Self {
        self.facts = Some(facts);
        self
    }

    /// Masking rules that apply only at the DLQ.
    #[cfg(feature = "masking")]
    pub fn with_masking(mut self, masking: Arc<crate::masking::CompiledMasking>) -> Self {
        if !masking.is_empty() {
            self.masking = Some(masking);
        }
        self
    }
}

/// Every guarantee a run is held to. Build with the `with_*` methods, opt out
/// of a pass with [`Governance::without`], wrap the destination with
/// [`Governance::sink_builder`], and attach with
/// [`Pipeline::with_governance`](crate::Pipeline::with_governance).
#[derive(Clone, Default)]
#[non_exhaustive]
pub struct Governance {
    #[cfg(feature = "masking")]
    masking: Option<Arc<crate::masking::CompiledMasking>>,
    #[cfg(feature = "quality")]
    quality: Option<Arc<crate::quality::CompiledQuality>>,
    #[cfg(feature = "contract")]
    contract: Option<Arc<crate::contract::CompiledContract>>,
    schema_drift: Option<crate::drift::SchemaDriftPolicy>,
    #[cfg(feature = "policy")]
    policy: Option<PolicyGovernance>,
    budget: Option<BudgetSpec>,
    budget_state: Option<Arc<BudgetState>>,
    profiler: Option<Arc<Mutex<Profiler>>>,
    metadata: Option<(CompiledMetadata, MetadataContext)>,
    usage_meter: Option<Arc<UsageMeter>>,
    resilience: Option<crate::resilience::ResiliencePolicy>,
    cancel: Option<CancellationToken>,
    dlq: Option<DlqGovernance>,
    opt_outs: Vec<OptOut>,
    sink_built: Arc<AtomicBool>,
}

impl std::fmt::Debug for Governance {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Governance")
            .field("active", &self.active())
            .field("opt_outs", &self.opt_outs)
            .finish()
    }
}

impl Governance {
    /// No pass configured.
    pub fn new() -> Self {
        Self::default()
    }

    /// PII masking, applied first on every page. An empty policy is ignored.
    #[cfg(feature = "masking")]
    pub fn with_masking(mut self, masking: Arc<crate::masking::CompiledMasking>) -> Self {
        self.masking = (!masking.is_empty()).then_some(masking);
        self
    }

    /// Data-quality checks.
    #[cfg(feature = "quality")]
    pub fn with_quality(mut self, quality: Arc<crate::quality::CompiledQuality>) -> Self {
        self.quality = Some(quality);
        self
    }

    /// A data contract.
    #[cfg(feature = "contract")]
    pub fn with_contract(mut self, contract: Arc<crate::contract::CompiledContract>) -> Self {
        self.contract = Some(contract);
        self
    }

    /// A schema-drift policy.
    pub fn with_schema_drift(mut self, policy: crate::drift::SchemaDriftPolicy) -> Self {
        self.schema_drift = Some(policy);
        self
    }

    /// The data-flow policy backstop for the destination.
    #[cfg(feature = "policy")]
    pub fn with_policy(mut self, policy: PolicyGovernance) -> Self {
        self.policy = Some(policy);
        self
    }

    /// Budget ceilings. The main sink and the DLQ count against one shared
    /// state. An empty spec is ignored.
    pub fn with_budget(mut self, spec: BudgetSpec) -> Self {
        if spec.max_records.is_none()
            && spec.max_bytes.is_none()
            && spec.max_duration_secs.is_none()
        {
            self.budget = None;
            self.budget_state = None;
        } else {
            self.budget = Some(spec);
            self.budget_state = Some(Arc::new(BudgetState::default()));
        }
        self
    }

    /// Column profiling into `profiler`.
    pub fn with_profiler(mut self, profiler: Arc<Mutex<Profiler>>) -> Self {
        self.profiler = Some(profiler);
        self
    }

    /// `_faucet_*` metadata columns.
    pub fn with_metadata(mut self, meta: CompiledMetadata, ctx: MetadataContext) -> Self {
        self.metadata = Some((meta, ctx));
        self
    }

    /// Usage metering for the sink, the source and the DLQ.
    pub fn with_usage_meter(mut self, meter: Arc<UsageMeter>) -> Self {
        self.usage_meter = Some(meter);
        self
    }

    /// The resilience policy, for sink and DLQ writes.
    pub fn with_resilience(mut self, policy: crate::resilience::ResiliencePolicy) -> Self {
        self.resilience = Some(policy);
        self
    }

    /// The run's cooperative cancellation token (a duration budget cancels it
    /// too).
    pub fn with_cancel(mut self, cancel: CancellationToken) -> Self {
        self.cancel = Some(cancel);
        self
    }

    /// The DLQ, governed as a destination of its own.
    pub fn with_dlq(mut self, dlq: DlqGovernance) -> Self {
        self.dlq = Some(dlq);
        self
    }

    /// Turn `pass` off for this entry point, saying why. The opt-out is
    /// recorded ([`Governance::opt_outs`]) and logged.
    pub fn without(mut self, pass: Pass, reason: impl Into<String>) -> Self {
        let reason = reason.into();
        match pass {
            #[cfg(feature = "masking")]
            Pass::Masking => self.masking = None,
            #[cfg(feature = "quality")]
            Pass::Quality => self.quality = None,
            #[cfg(feature = "contract")]
            Pass::Contract => self.contract = None,
            Pass::SchemaDrift => self.schema_drift = None,
            #[cfg(feature = "policy")]
            Pass::Policy => self.policy = None,
            Pass::Budget => {
                self.budget = None;
                self.budget_state = None;
            }
            Pass::Profiling => self.profiler = None,
            Pass::Metadata => self.metadata = None,
            Pass::Metering => self.usage_meter = None,
            Pass::Resilience => self.resilience = None,
            Pass::Cancel => self.cancel = None,
            Pass::Dlq => self.dlq = None,
            #[allow(unreachable_patterns)]
            _ => {}
        }
        tracing::info!(pass = pass.as_str(), reason = %reason, "governance pass opted out");
        self.opt_outs.retain(|o| o.pass != pass);
        self.opt_outs.push(OptOut { pass, reason });
        self
    }

    /// The passes turned off with a reason.
    pub fn opt_outs(&self) -> &[OptOut] {
        &self.opt_outs
    }

    /// The reason `pass` was turned off, if it was.
    pub fn opted_out(&self, pass: Pass) -> Option<&str> {
        self.opt_outs
            .iter()
            .find(|o| o.pass == pass)
            .map(|o| o.reason.as_str())
    }

    /// Whether `pass` is configured.
    pub fn is_active(&self, pass: Pass) -> bool {
        match pass {
            #[cfg(feature = "masking")]
            Pass::Masking => self.masking.is_some(),
            #[cfg(feature = "quality")]
            Pass::Quality => self.quality.is_some(),
            #[cfg(feature = "contract")]
            Pass::Contract => self.contract.is_some(),
            Pass::SchemaDrift => self.schema_drift.is_some(),
            #[cfg(feature = "policy")]
            Pass::Policy => self.policy.is_some(),
            Pass::Budget => self.budget.is_some(),
            Pass::Profiling => self.profiler.is_some(),
            Pass::Metadata => self.metadata.is_some(),
            Pass::Metering => self.usage_meter.is_some(),
            Pass::Resilience => self.resilience.is_some(),
            Pass::Cancel => self.cancel.is_some(),
            Pass::Dlq => self.dlq.is_some(),
            #[allow(unreachable_patterns)]
            _ => false,
        }
    }

    /// The configured passes.
    pub fn active(&self) -> Vec<Pass> {
        Pass::ALL
            .iter()
            .copied()
            .filter(|p| self.is_active(*p))
            .collect()
    }

    /// The budget spec, when one is configured.
    pub fn budget(&self) -> Option<&BudgetSpec> {
        self.budget.as_ref()
    }

    /// The shared budget state the main sink and the DLQ count against.
    pub fn budget_state(&self) -> Option<&Arc<BudgetState>> {
        self.budget_state.as_ref()
    }

    /// The cancellation token.
    pub fn cancel(&self) -> Option<&CancellationToken> {
        self.cancel.as_ref()
    }

    /// The usage meter.
    pub fn usage_meter(&self) -> Option<&Arc<UsageMeter>> {
        self.usage_meter.as_ref()
    }

    /// The resilience policy.
    pub fn resilience(&self) -> Option<&crate::resilience::ResiliencePolicy> {
        self.resilience.as_ref()
    }

    /// The DLQ.
    pub fn dlq(&self) -> Option<&DlqGovernance> {
        self.dlq.as_ref()
    }

    /// The masking policy.
    #[cfg(feature = "masking")]
    pub fn masking(&self) -> Option<&Arc<crate::masking::CompiledMasking>> {
        self.masking.as_ref()
    }

    /// The quality checks.
    #[cfg(feature = "quality")]
    pub fn quality(&self) -> Option<&Arc<crate::quality::CompiledQuality>> {
        self.quality.as_ref()
    }

    /// The data contract.
    #[cfg(feature = "contract")]
    pub fn contract(&self) -> Option<&Arc<crate::contract::CompiledContract>> {
        self.contract.as_ref()
    }

    /// The schema-drift policy.
    pub fn schema_drift(&self) -> Option<crate::drift::SchemaDriftPolicy> {
        self.schema_drift
    }

    /// The policy backstop.
    #[cfg(feature = "policy")]
    pub fn policy(&self) -> Option<&PolicyGovernance> {
        self.policy.as_ref()
    }

    /// Whether a policy rule quarantines at run time — such a run takes the
    /// row path (#873).
    pub fn policy_quarantines(&self) -> bool {
        #[cfg(feature = "policy")]
        {
            self.policy
                .as_ref()
                .is_some_and(PolicyGovernance::quarantines)
        }
        #[cfg(not(feature = "policy"))]
        {
            false
        }
    }

    /// Whether any sink-side layer (policy, profiling, metadata, budget) is
    /// configured, so the destination must come from [`Self::sink_builder`].
    pub fn has_sink_layers(&self) -> bool {
        self.is_active(Pass::Budget)
            || self.is_active(Pass::Profiling)
            || self.is_active(Pass::Metadata)
            || self.policy_active()
    }

    fn policy_active(&self) -> bool {
        #[cfg(feature = "policy")]
        {
            self.policy.is_some()
        }
        #[cfg(not(feature = "policy"))]
        {
            false
        }
    }

    /// Whether [`Self::sink_builder`] built a destination for this value.
    pub fn sink_built(&self) -> bool {
        self.sink_built.load(Ordering::Relaxed)
    }

    /// The builder that wraps the destination in the canonical order.
    pub fn sink_builder(&self) -> GovernedSinkBuilder<'_> {
        GovernedSinkBuilder {
            gov: self,
            inner_tap: None,
            outer_tap: None,
        }
    }

    /// Run the per-page passes — masking → quality → contract → schema drift
    /// (against `sink`'s live schema) — over one page, outside a pipeline run.
    ///
    /// The returned [`GovernedOutput`] holds the governed survivors and the
    /// quarantine envelopes the passes built; write the page with
    /// [`Sink::write_governed`]. A pass that aborts (a quality `abort`, a
    /// contract `fail`, a drift `fail`) is the error. Each call starts from a
    /// fresh destination-schema read, so this suits one-off pages (tests,
    /// previews, conformance), not a page loop — use a
    /// [`Pipeline`](crate::Pipeline) for that.
    pub async fn govern<Si: Sink + ?Sized>(
        &self,
        records: Vec<Value>,
        sink: &Si,
    ) -> Result<GovernedOutput, FaucetError> {
        let specs = apply::GovernanceSpecs {
            #[cfg(feature = "masking")]
            masking: self.masking.as_ref(),
            #[cfg(feature = "quality")]
            quality: self.quality.as_ref(),
            #[cfg(feature = "contract")]
            contract: self.contract.as_ref(),
            schema_drift: self.schema_drift.as_ref(),
            pipeline: "unnamed",
            row: "",
            run_id: "",
            sink_name: sink.connector_name(),
            has_dlq: false,
        };
        let mut state = apply::GovernanceState::default();
        let out = apply::apply_governance(records, &specs, &mut state, sink).await?;
        Ok(GovernedOutput {
            page: out.records,
            envelopes: out.envelopes,
        })
    }

    /// The compat features this governance contributes to path selection.
    pub(crate) fn compat_features(&self) -> Vec<crate::compat::Feature> {
        use crate::compat::Feature as F;
        [
            (self.policy_quarantines(), F::PolicyQuarantine),
            (self.policy_active(), F::Policy),
            (self.is_active(Pass::Budget), F::Budget),
            (self.is_active(Pass::Profiling), F::Profiling),
            (self.is_active(Pass::Metadata), F::MetadataColumns),
        ]
        .into_iter()
        .filter_map(|(on, f)| on.then_some(f))
        .collect()
    }

    /// The DLQ wiring with its sink wrapped in the governed stack (#870):
    /// masking (rules naming the DLQ) → policy (the DLQ's attributes) → budget
    /// (shared with the main sink) → metering. `None` without a DLQ. For a
    /// caller that drives the write loop itself (a topology sink node).
    pub fn dlq_config(&self) -> Option<DlqConfig> {
        self.governed_dlq()
    }

    /// Wrap the DLQ sink in its governed stack (#870). Returns `None` when no
    /// DLQ is configured.
    pub(crate) fn governed_dlq(&self) -> Option<DlqConfig> {
        let dlq = self.dlq.as_ref()?;
        Some(govern_dlq(
            dlq,
            self.usage_meter.as_ref(),
            self.budget.as_ref().zip(self.budget_state.as_ref()),
            self.cancel.clone(),
            #[cfg(feature = "policy")]
            self.policy.as_ref(),
        ))
    }
}

/// What [`Governance::govern`] produced for one page.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct GovernedOutput {
    /// The records that survived every pass.
    pub page: GovernedPage<'static>,
    /// Dead-letter envelopes for the records a pass quarantined.
    pub envelopes: GovernedEnvelopes,
}

/// Wrap a DLQ's sink in the governed stack: masking → policy → budget →
/// metering → the DLQ sink.
pub(crate) fn govern_dlq(
    dlq: &DlqGovernance,
    meter: Option<&Arc<UsageMeter>>,
    budget: Option<(&BudgetSpec, &Arc<BudgetState>)>,
    cancel: Option<CancellationToken>,
    #[cfg(feature = "policy")] policy: Option<&PolicyGovernance>,
) -> DlqConfig {
    let mut sink: Box<dyn Sink> = Box::new(SharedSink(Arc::clone(&dlq.config.sink)));
    if let Some(m) = meter {
        sink = Box::new(DlqMeteredSink {
            inner: sink,
            meter: Arc::clone(m),
        });
    }
    if let Some((spec, state)) = budget {
        sink = Box::new(BudgetSink::wrap_shared(
            sink,
            spec.clone(),
            Arc::clone(state),
            cancel.unwrap_or_default(),
        ));
    }
    #[cfg(feature = "policy")]
    if let (Some(p), Some(facts)) = (policy, dlq.facts.as_ref()) {
        let mut ps = crate::policy::PolicySink::for_dlq(
            sink,
            Arc::clone(&p.policy),
            facts.clone(),
            p.scope.clone(),
        );
        if let Some(m) = &p.masking {
            ps = ps.with_masking(Arc::clone(m));
        }
        sink = Box::new(ps);
    }
    #[cfg(feature = "masking")]
    if let Some(m) = &dlq.masking {
        sink = Box::new(DlqMaskingSink {
            inner: sink,
            masking: Arc::clone(m),
        });
    }
    DlqConfig {
        sink: Arc::from(sink),
        ..dlq.config.clone()
    }
}

type Tap = Box<dyn FnOnce(Box<dyn Sink>) -> Box<dyn Sink> + Send>;

/// Wraps a destination in the canonical [`SINK_LAYERS`] order. Built by
/// [`Governance::sink_builder`].
pub struct GovernedSinkBuilder<'g> {
    gov: &'g Governance,
    inner_tap: Option<Tap>,
    outer_tap: Option<Tap>,
}

impl<'g> GovernedSinkBuilder<'g> {
    /// An observer layered between the budget and the metadata columns.
    pub fn inner_tap(
        mut self,
        tap: impl FnOnce(Box<dyn Sink>) -> Box<dyn Sink> + Send + 'static,
    ) -> Self {
        self.inner_tap = Some(Box::new(tap));
        self
    }

    /// An observer layered between the metadata columns and profiling.
    pub fn outer_tap(
        mut self,
        tap: impl FnOnce(Box<dyn Sink>) -> Box<dyn Sink> + Send + 'static,
    ) -> Self {
        self.outer_tap = Some(Box::new(tap));
        self
    }

    /// Wrap `destination`. Starts the duration-budget timer, which lives as
    /// long as the returned [`GovernedSink`].
    pub fn build(mut self, destination: Box<dyn Sink>) -> GovernedSink {
        let gov = self.gov;
        let mut layers = Vec::new();
        let mut sink = destination;
        let mut timer = None;
        for layer in SINK_LAYERS.iter().rev() {
            match layer {
                SinkLayer::Budget => {
                    if let (Some(spec), Some(state)) = (&gov.budget, &gov.budget_state) {
                        let cancel = gov.cancel.clone().unwrap_or_default();
                        timer = Some(BudgetTimer::start_for(
                            spec.max_duration_secs,
                            Arc::clone(state),
                            cancel.clone(),
                        ));
                        sink = Box::new(BudgetSink::wrap_shared(
                            sink,
                            spec.clone(),
                            Arc::clone(state),
                            cancel,
                        ));
                        layers.push(*layer);
                    }
                }
                SinkLayer::InnerTap => {
                    if let Some(tap) = self.inner_tap.take() {
                        sink = tap(sink);
                        layers.push(*layer);
                    }
                }
                SinkLayer::Metadata => {
                    if let Some((meta, ctx)) = &gov.metadata {
                        sink = Box::new(MetadataSink::new(sink, meta.clone(), ctx.clone()));
                        layers.push(*layer);
                    }
                }
                SinkLayer::OuterTap => {
                    if let Some(tap) = self.outer_tap.take() {
                        sink = tap(sink);
                        layers.push(*layer);
                    }
                }
                SinkLayer::Profiling => {
                    if let Some(p) = &gov.profiler {
                        sink = Box::new(ProfilingSink::new(sink, Arc::clone(p)));
                        layers.push(*layer);
                    }
                }
                SinkLayer::Policy =>
                {
                    #[cfg(feature = "policy")]
                    if let Some(p) = &gov.policy {
                        let mut ps = crate::policy::PolicySink::new(
                            sink,
                            Arc::clone(&p.policy),
                            p.facts.clone(),
                            p.scope.clone(),
                        );
                        if let Some(facts) = gov.dlq.as_ref().and_then(|d| d.facts.clone()) {
                            ps = ps.with_dlq_destination(facts);
                        }
                        if let Some(m) = &p.masking {
                            ps = ps.with_masking(Arc::clone(m));
                        }
                        sink = Box::new(ps);
                        layers.push(*layer);
                    }
                }
            }
        }
        layers.reverse();
        gov.sink_built.store(true, Ordering::Relaxed);
        GovernedSink {
            sink,
            budget: gov.budget_state.clone(),
            _timer: timer,
            layers,
        }
    }
}

/// A destination wrapped in its governance layers. It is a [`Sink`] itself;
/// keep it alive for the run (it owns the duration-budget timer).
pub struct GovernedSink {
    sink: Box<dyn Sink>,
    budget: Option<Arc<BudgetState>>,
    _timer: Option<BudgetTimer>,
    layers: Vec<SinkLayer>,
}

impl GovernedSink {
    /// The applied layers, outermost first.
    pub fn layers(&self) -> &[SinkLayer] {
        &self.layers
    }

    /// The budget state, when a budget is configured.
    pub fn budget_state(&self) -> Option<&Arc<BudgetState>> {
        self.budget.as_ref()
    }

    /// A duration ceiling cancels the run cooperatively, so the pipeline
    /// returns `Ok` with a partial result; turn that into the budget failure.
    pub fn finish<T>(&self, result: Result<T, FaucetError>) -> Result<T, FaucetError> {
        match (&result, self.budget.as_ref().and_then(|s| s.verdict())) {
            (Ok(_), Some(verdict)) => Err(verdict.error()),
            _ => result,
        }
    }

    /// Unwrap into the boxed sink (the duration timer stops).
    pub fn into_inner(self) -> Box<dyn Sink> {
        self.sink
    }
}

#[async_trait]
impl Sink for GovernedSink {
    crate::forward_sink!(sink);
}

/// An `Arc`-shared sink as an owned one.
pub(crate) struct SharedSink(pub(crate) Arc<dyn Sink>);

#[async_trait]
impl Sink for SharedSink {
    crate::forward_sink!(0);
}

/// Feeds DLQ writes into the usage meter.
struct DlqMeteredSink {
    inner: Box<dyn Sink>,
    meter: Arc<UsageMeter>,
}

#[async_trait]
impl Sink for DlqMeteredSink {
    crate::forward_sink!(inner; except write_batch, write_batch_reported, write_batch_partial);

    async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
        let n = self.inner.write_batch(records).await?;
        self.meter
            .add_dlq_written(n as u64, crate::usage::estimate_page_bytes(records));
        Ok(n)
    }

    async fn write_batch_reported(
        &self,
        records: &[Value],
    ) -> Result<crate::accounting::WriteReport, FaucetError> {
        let r = self.inner.write_batch_reported(records).await?;
        self.meter.add_dlq_written(
            r.accepted as u64,
            crate::usage::estimate_page_bytes(records),
        );
        Ok(r)
    }

    async fn write_batch_partial(&self, records: &[Value]) -> Result<Vec<RowOutcome>, FaucetError> {
        let outcomes = self.inner.write_batch_partial(records).await?;
        let ok = outcomes.iter().filter(|o| o.is_ok()).count();
        self.meter
            .add_dlq_written(ok as u64, crate::usage::estimate_page_bytes(records));
        Ok(outcomes)
    }
}

/// Applies DLQ-only masking rules to each envelope's payload.
#[cfg(feature = "masking")]
struct DlqMaskingSink {
    inner: Box<dyn Sink>,
    masking: Arc<crate::masking::CompiledMasking>,
}

#[cfg(feature = "masking")]
impl DlqMaskingSink {
    fn mask(&self, records: &[Value]) -> Vec<Value> {
        let mut out = records.to_vec();
        let payloads: Vec<Value> = out
            .iter_mut()
            .map(|e| match e.get_mut("payload") {
                Some(p) => p.take(),
                None => Value::Null,
            })
            .collect();
        let masked = crate::masking::apply_masking(payloads, &self.masking).records;
        for (envelope, payload) in out.iter_mut().zip(masked) {
            if let Some(slot) = envelope.get_mut("payload") {
                *slot = payload;
            }
        }
        out
    }
}

#[cfg(feature = "masking")]
#[async_trait]
impl Sink for DlqMaskingSink {
    crate::forward_sink!(inner; except write_batch, write_batch_reported, write_batch_partial);

    async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
        self.inner.write_batch(&self.mask(records)).await
    }

    async fn write_batch_reported(
        &self,
        records: &[Value],
    ) -> Result<crate::accounting::WriteReport, FaucetError> {
        self.inner.write_batch_reported(&self.mask(records)).await
    }

    async fn write_batch_partial(&self, records: &[Value]) -> Result<Vec<RowOutcome>, FaucetError> {
        self.inner.write_batch_partial(&self.mask(records)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::Source;
    use serde_json::json;
    use std::collections::HashMap;

    type Seen = Arc<Mutex<Vec<(&'static str, Value)>>>;

    /// Records the first record of every page it sees, then forwards.
    struct Tap {
        name: &'static str,
        inner: Box<dyn Sink>,
        seen: Seen,
    }

    #[async_trait]
    impl Sink for Tap {
        crate::forward_sink!(inner; except write_batch);
        async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
            if let Some(r) = records.first() {
                self.seen.lock().unwrap().push((self.name, r.clone()));
            }
            self.inner.write_batch(records).await
        }
    }

    fn tap(
        name: &'static str,
        seen: &Seen,
    ) -> impl FnOnce(Box<dyn Sink>) -> Box<dyn Sink> + Send + 'static {
        let seen = Arc::clone(seen);
        move |inner| Box::new(Tap { name, inner, seen })
    }

    struct Dest(Seen);

    #[async_trait]
    impl Sink for Dest {
        async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
            for r in records {
                self.0.lock().unwrap().push(("dest", r.clone()));
            }
            Ok(records.len())
        }
    }

    fn profiler() -> Arc<Mutex<Profiler>> {
        Arc::new(Mutex::new(Profiler::new(
            serde_json::from_value(json!({})).unwrap(),
        )))
    }

    fn metadata() -> (CompiledMetadata, MetadataContext) {
        (
            CompiledMetadata::compile(&crate::metadata::MetadataColumnsSpec::default())
                .unwrap()
                .unwrap(),
            MetadataContext {
                run_id: "r".into(),
                source: "s".into(),
            },
        )
    }

    fn is_subsequence(layers: &[SinkLayer]) -> bool {
        let mut canon = SINK_LAYERS.iter();
        layers.iter().all(|l| canon.any(|c| c == l))
    }

    /// The core invariant (#875): the builder wraps the destination in exactly
    /// the canonical order — metadata below the outer tap and above the inner
    /// tap, the budget innermost, counting what the destination accepts.
    #[tokio::test]
    async fn layers_wrap_the_destination_in_the_canonical_order() {
        let (meta, ctx) = metadata();
        let g = Governance::new()
            .with_budget(BudgetSpec {
                max_records: Some(10),
                ..Default::default()
            })
            .with_metadata(meta, ctx)
            .with_profiler(profiler());
        let seen: Seen = Arc::default();
        let governed = g
            .sink_builder()
            .inner_tap(tap("inner", &seen))
            .outer_tap(tap("outer", &seen))
            .build(Box::new(Dest(Arc::clone(&seen))));
        assert_eq!(
            governed.layers(),
            &[
                SinkLayer::Profiling,
                SinkLayer::OuterTap,
                SinkLayer::Metadata,
                SinkLayer::InnerTap,
                SinkLayer::Budget,
            ]
        );
        assert!(is_subsequence(governed.layers()));
        assert!(g.sink_built());
        governed.write_batch(&[json!({"a": 1})]).await.unwrap();
        let seen = seen.lock().unwrap().clone();
        let names: Vec<&str> = seen.iter().map(|(n, _)| *n).collect();
        assert_eq!(names, ["outer", "inner", "dest"]);
        assert!(
            seen[0].1.get("_faucet_run_id").is_none(),
            "the outer tap sees the source record"
        );
        assert_eq!(
            seen[1].1["_faucet_run_id"], "r",
            "metadata is stamped above the inner tap"
        );
        assert_eq!(governed.budget_state().unwrap().records(), 1);
        assert!(governed.finish(Ok::<_, FaucetError>(())).is_ok());
        let _ = governed.into_inner();
    }

    #[tokio::test]
    async fn an_empty_governance_builds_the_bare_destination() {
        let g = Governance::new().with_budget(BudgetSpec::default());
        assert!(!g.has_sink_layers());
        assert!(g.budget().is_none());
        let governed = g.sink_builder().build(Box::new(Dest(Arc::default())));
        assert!(governed.layers().is_empty());
        assert!(governed.budget_state().is_none());
        assert_eq!(governed.write_batch(&[json!({})]).await.unwrap(), 1);
    }

    #[tokio::test]
    async fn a_spent_duration_turns_the_partial_result_into_the_verdict() {
        let cancel = CancellationToken::new();
        let g = Governance::new()
            .with_cancel(cancel.clone())
            .with_budget(BudgetSpec {
                max_duration_secs: Some(1),
                ..Default::default()
            });
        let governed = g.sink_builder().build(Box::new(Dest(Arc::default())));
        tokio::time::timeout(std::time::Duration::from_secs(5), cancel.cancelled())
            .await
            .expect("the duration ceiling cancels the run");
        let err = governed.finish(Ok::<_, FaucetError>(())).unwrap_err();
        assert!(matches!(err, FaucetError::BudgetExceeded { .. }), "{err}");
    }

    #[test]
    fn opt_outs_clear_the_pass_and_are_recorded() {
        let (meta, ctx) = metadata();
        let g = Governance::new()
            .with_budget(BudgetSpec {
                max_records: Some(1),
                ..Default::default()
            })
            .with_metadata(meta, ctx)
            .with_profiler(profiler())
            .with_cancel(CancellationToken::new())
            .with_usage_meter(Arc::new(UsageMeter::new()))
            .with_resilience(crate::resilience::ResiliencePolicy::default())
            .with_schema_drift(crate::drift::SchemaDriftPolicy::compile(
                &serde_json::from_value(json!({})).unwrap(),
            ));
        for p in [
            Pass::Budget,
            Pass::Metadata,
            Pass::Profiling,
            Pass::Cancel,
            Pass::Metering,
            Pass::Resilience,
            Pass::SchemaDrift,
        ] {
            assert!(g.is_active(p), "{p}");
        }
        assert!(g.has_sink_layers());
        let g = g
            .without(Pass::Budget, "why")
            .without(Pass::Profiling, "first")
            .without(Pass::Profiling, "second")
            .without(Pass::Dlq, "none");
        assert!(!g.is_active(Pass::Budget));
        assert!(g.budget_state().is_none());
        assert!(!g.is_active(Pass::Profiling));
        assert_eq!(g.opted_out(Pass::Profiling), Some("second"));
        assert_eq!(g.opted_out(Pass::Budget), Some("why"));
        assert_eq!(g.opted_out(Pass::Masking), None);
        assert_eq!(g.opt_outs().len(), 3);
        assert!(g.active().contains(&Pass::Metadata));
        assert!(format!("{g:?}").contains("opt_outs"));
        for p in Pass::ALL {
            assert_eq!(p.to_string(), p.as_str());
        }
        let g = g
            .without(Pass::Metadata, "m")
            .without(Pass::Cancel, "c")
            .without(Pass::Metering, "u")
            .without(Pass::Resilience, "r")
            .without(Pass::SchemaDrift, "d");
        assert!(!g.has_sink_layers());
        assert!(g.cancel().is_none() && g.usage_meter().is_none() && g.resilience().is_none());
        assert!(g.schema_drift().is_none() && g.dlq().is_none());
    }

    struct Rows(Vec<Value>);

    #[async_trait]
    impl Source for Rows {
        async fn fetch_with_context(
            &self,
            _ctx: &HashMap<String, Value>,
        ) -> Result<Vec<Value>, FaucetError> {
            Ok(self.0.clone())
        }
    }

    /// A pipeline holding sink-side passes refuses to run a destination that
    /// did not come from the builder: the passes would silently not apply.
    #[tokio::test]
    async fn a_pipeline_refuses_an_unbuilt_destination() {
        let g = Governance::new().with_budget(BudgetSpec {
            max_records: Some(5),
            ..Default::default()
        });
        let source = Rows(vec![json!({"a": 1})]);
        let raw = Dest(Arc::default());
        let err = crate::Pipeline::new(&source, &raw)
            .with_governance(g.clone())
            .run()
            .await
            .unwrap_err();
        assert!(
            err.to_string().contains("Governance::sink_builder"),
            "{err}"
        );
        let governed = g.sink_builder().build(Box::new(raw));
        let r = crate::Pipeline::new(&source, &governed)
            .with_governance(g)
            .run()
            .await
            .unwrap();
        assert_eq!(r.records_written, 1);
    }

    /// Counts governed writes; the default forwards to the reported write.
    struct GovernedOnly {
        governed: std::sync::atomic::AtomicUsize,
        rows: Seen,
    }

    #[async_trait]
    impl Sink for GovernedOnly {
        async fn write_batch(&self, records: &[Value]) -> Result<usize, FaucetError> {
            for r in records {
                self.rows.lock().unwrap().push(("plain", r.clone()));
            }
            Ok(records.len())
        }
        async fn write_governed(
            &self,
            page: &GovernedPage<'_>,
        ) -> Result<crate::accounting::WriteReport, FaucetError> {
            self.governed
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            self.write_batch_reported(page.records()).await
        }
    }

    /// RFC 0014: the engine's plain write path hands the sink a governed page.
    #[tokio::test]
    async fn the_plain_write_path_writes_through_write_governed() {
        let source = Rows(vec![json!({"a": 1}), json!({"a": 2})]);
        let sink = GovernedOnly {
            governed: Default::default(),
            rows: Arc::default(),
        };
        let r = crate::Pipeline::new(&source, &sink).run().await.unwrap();
        assert_eq!(r.records_written, 2);
        assert_eq!(sink.governed.load(std::sync::atomic::Ordering::Relaxed), 1);
        assert_eq!(sink.rows.lock().unwrap().len(), 2);
    }

    /// A wrapper that changes the plain write keeps its change on the
    /// governed path: `write_governed` is not forwarded past it.
    #[tokio::test]
    async fn a_wrapper_changing_write_batch_keeps_the_governed_write_its_own() {
        let seen: Seen = Arc::default();
        let wrapped = Tap {
            name: "tap",
            inner: Box::new(Dest(Arc::clone(&seen))),
            seen: Arc::clone(&seen),
        };
        let page = Governance::new()
            .govern(vec![json!({"k": 1})], &wrapped)
            .await
            .unwrap();
        assert!(page.envelopes.is_empty());
        let report = wrapped.write_governed(&page.page).await.unwrap();
        assert_eq!(report.accepted, 1);
        let names: Vec<&str> = seen.lock().unwrap().iter().map(|(n, _)| *n).collect();
        assert_eq!(names, ["tap", "dest"]);
    }

    #[cfg(feature = "masking")]
    #[tokio::test]
    async fn govern_runs_the_passes_and_returns_a_governed_page() {
        let spec: crate::masking::MaskingSpec = serde_json::from_value(json!({
            "rules": [{"match": {"fields": ["email"]}, "action": {"type": "redact"}}]
        }))
        .unwrap();
        let g = Governance::new().with_masking(Arc::new(
            crate::masking::CompiledMasking::compile(&spec).unwrap(),
        ));
        let out = g
            .govern(vec![json!({"email": "a@b.co"})], &Dest(Arc::default()))
            .await
            .unwrap();
        assert_eq!(out.page.records()[0]["email"], "***");
    }

    #[cfg(feature = "quality")]
    #[tokio::test]
    async fn govern_fails_on_an_aborting_pass() {
        let spec: crate::quality::QualitySpec = serde_json::from_value(json!({
            "record": [{"type": "not_null", "field": "id", "on_failure": "abort"}]
        }))
        .unwrap();
        let g = Governance::new().with_quality(Arc::new(
            crate::quality::CompiledQuality::compile(&spec).unwrap(),
        ));
        let err = g
            .govern(vec![json!({"id": null})], &Dest(Arc::default()))
            .await
            .unwrap_err();
        assert!(matches!(err, FaucetError::QualityFailure { .. }), "{err}");
    }

    #[test]
    fn compat_features_follow_the_sink_layers() {
        use crate::compat::Feature as F;
        let (meta, ctx) = metadata();
        let g = Governance::new()
            .with_budget(BudgetSpec {
                max_bytes: Some(1),
                ..Default::default()
            })
            .with_metadata(meta, ctx)
            .with_profiler(profiler());
        let f = g.compat_features();
        assert!(f.contains(&F::Budget) && f.contains(&F::MetadataColumns));
        assert!(f.contains(&F::Profiling));
        assert!(!f.contains(&F::PolicyQuarantine));
        assert!(!g.policy_quarantines());
        assert!(!crate::compat::path_allowed(
            crate::compat::WritePath::Native,
            &f
        ));
        assert!(!crate::compat::path_allowed(
            crate::compat::WritePath::Columnar,
            &f
        ));
        assert!(crate::compat::path_allowed(
            crate::compat::WritePath::Row,
            &f
        ));
    }

    /// The reported and partial DLQ writes go through the same masking and
    /// metering as a plain write: the payload is masked and the accepted rows
    /// are counted as dead-lettered.
    #[cfg(feature = "masking")]
    #[tokio::test]
    async fn the_governed_dlq_masks_and_meters_every_write_kind() {
        let spec: crate::masking::MaskingSpec = serde_json::from_value(json!({
            "rules": [{"match": {"fields": ["email"]}, "action": {"type": "redact"}}]
        }))
        .unwrap();
        let masking = Arc::new(crate::masking::CompiledMasking::compile(&spec).unwrap());
        let seen: Seen = Arc::default();
        let meter = Arc::new(UsageMeter::new());
        let dlq = DlqGovernance::new(DlqConfig::new(Arc::new(Dest(Arc::clone(&seen)))))
            .with_masking(masking);
        let config = Governance::new()
            .with_usage_meter(Arc::clone(&meter))
            .with_dlq(dlq)
            .dlq_config()
            .unwrap();
        let envelope = || json!({"payload": {"email": "a@b.co"}});
        let report = config
            .sink
            .write_batch_reported(&[envelope()])
            .await
            .unwrap();
        assert_eq!(report.accepted, 1);
        let outcomes = config
            .sink
            .write_batch_partial(&[envelope(), envelope()])
            .await
            .unwrap();
        assert!(outcomes.iter().all(|o| o.is_ok()));
        let seen = seen.lock().unwrap().clone();
        assert_eq!(seen.len(), 3);
        assert!(seen.iter().all(|(_, r)| r["payload"]["email"] == "***"));
        assert_eq!(meter.snapshot().dlq_records_written, 3);
    }

    /// Opting out of the record passes clears them.
    #[cfg(all(feature = "quality", feature = "contract", feature = "policy"))]
    #[test]
    fn the_record_passes_can_be_opted_out() {
        let quality: crate::quality::QualitySpec = serde_json::from_value(json!({})).unwrap();
        let contract: crate::contract::ContractSpec = serde_json::from_value(json!({
            "version": "1", "fields": [{"name": "id", "type": "integer"}]
        }))
        .unwrap();
        let policy: crate::policy::PolicySpec = serde_json::from_value(json!({
            "classifications": [], "rules": []
        }))
        .unwrap();
        let g = Governance::new()
            .with_quality(Arc::new(
                crate::quality::CompiledQuality::compile(&quality).unwrap(),
            ))
            .with_contract(Arc::new(
                crate::contract::CompiledContract::compile(&contract).unwrap(),
            ))
            .with_policy(PolicyGovernance::new(
                Arc::new(crate::policy::CompiledPolicy::compile(&policy).unwrap()),
                crate::policy::SinkFacts {
                    id: "w".into(),
                    kind: "stdout".into(),
                    attributes: Default::default(),
                },
                crate::policy::PolicyScope {
                    pipeline: "p".into(),
                    row: "w".into(),
                },
            ));
        assert!(g.quality().is_some() && g.contract().is_some() && g.policy().is_some());
        let g = g
            .without(Pass::Masking, "m")
            .without(Pass::Quality, "q")
            .without(Pass::Contract, "c")
            .without(Pass::Policy, "p");
        assert!(g.quality().is_none() && g.contract().is_none() && g.policy().is_none());
        assert_eq!(g.opt_outs().len(), 4);
    }

    #[cfg(feature = "masking")]
    #[tokio::test]
    async fn dlq_only_masking_rewrites_the_envelope_payload() {
        let spec: crate::masking::MaskingSpec = serde_json::from_value(json!({
            "rules": [{"match": {"fields": ["email"]}, "action": {"type": "redact"}}]
        }))
        .unwrap();
        let masking = Arc::new(crate::masking::CompiledMasking::compile(&spec).unwrap());
        let seen: Seen = Arc::default();
        let dlq = DlqGovernance::new(DlqConfig::new(Arc::new(Dest(Arc::clone(&seen)))))
            .with_masking(Arc::clone(&masking));
        let config = Governance::new().with_dlq(dlq).dlq_config().unwrap();
        let envelope = json!({"payload": {"email": "a@b.co"}, "reason": "partial"});
        config
            .sink
            .write_batch(&[envelope, json!({"reason": "no payload"})])
            .await
            .unwrap();
        let seen = seen.lock().unwrap().clone();
        assert_eq!(seen[0].1["payload"]["email"], "***");
        assert!(seen[1].1.get("payload").is_none());
        let elsewhere: crate::masking::MaskingSpec = serde_json::from_value(json!({
            "rules": [{"match": {"fields": ["x"]}, "action": {"type": "redact"},
                       "applies_to": ["elsewhere"]}]
        }))
        .unwrap();
        let empty = DlqGovernance::new(DlqConfig::new(Arc::new(Dest(Arc::default()))))
            .with_masking(Arc::new(
                crate::masking::CompiledMasking::compile_for_sink(&elsewhere, &["dlq"]).unwrap(),
            ));
        assert!(empty.masking.is_none(), "an empty policy is ignored");
        assert!(Governance::new().dlq_config().is_none());
    }
}