faucet-cli 1.13.0

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
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
//! Change impact analysis (#707) — who breaks if this row's output changes.
//!
//! `faucet plan` knows what a change does to *its own* destination; the Data
//! Movement Catalog (#279) knows the lineage graph across pipelines. This
//! module connects the two: given the row about to run and the schema it
//! would now write, it
//!
//! 1. finds the row's sink dataset in the catalog (by the lineage edge the
//!    row recorded on its last run — a row that has never run has no edge,
//!    and the report says so),
//! 2. computes the **schema delta** against the dataset's last observed
//!    schema — added / removed / retyped columns, with a removed+added pair
//!    reported as a **rename** when the row's own transform chain renames it
//!    (`rename_field` / `rename_keys`),
//! 3. walks the lineage graph **downstream** (source → sink edges, depth
//!    bounded), following each edge's recorded column lineage to say which
//!    downstream columns read an affected column; an edge with no column
//!    lineage (opaque transform, or recorded before lineage existed) makes
//!    everything past it `unknown` — never a false "none",
//! 4. escalates to `breaking` when a downstream pipeline's **data contract**
//!    (its last config snapshot's `contract`) declares an affected column,
//!    naming the contract version, and
//! 5. names the affected datasets' **owners** and declared **consumers**
//!    (a consumer listing `columns` is affected only when one of them is).
//!
//! Severity per item: `breaking` (a removed / retyped / renamed column that
//! something downstream reads), `additive` (only new columns), `unknown`
//! (opaque path), `none`. Everything here is pure over catalog reads —
//! nothing is written and no connector is built.

use crate::error::{CliError, CliResult};
use crate::expand::ExpandedNode;
use crate::serve::history::RunHistory;
use crate::serve::history::catalog::{CatalogConsumer, CatalogDataset, CatalogLineageEdge};
use faucet_lineage::ColumnOp;
use serde::Serialize;
use serde_json::Value;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

/// Default downstream hop bound.
pub const DEFAULT_DEPTH: u32 = 5;
/// Hard ceiling on the hop bound.
pub const MAX_DEPTH: u32 = 32;

/// How a change affects an item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    /// Nothing it reads changes.
    None,
    /// Only new columns appear.
    Additive,
    /// The path is opaque — a change may or may not reach it.
    Unknown,
    /// A column it reads is removed, retyped, or renamed.
    Breaking,
}

impl Severity {
    pub fn as_str(self) -> &'static str {
        match self {
            Severity::None => "none",
            Severity::Additive => "additive",
            Severity::Unknown => "unknown",
            Severity::Breaking => "breaking",
        }
    }
}

/// What happened to one column of the changed row's output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ColumnChange {
    Added,
    Removed,
    Retyped {
        from: String,
        to: String,
    },
    /// `from` (the removed name) is now written as `to`.
    Renamed {
        to: String,
    },
}

impl ColumnChange {
    fn severity(&self) -> Severity {
        match self {
            ColumnChange::Added => Severity::Additive,
            _ => Severity::Breaking,
        }
    }
}

/// The schema delta of the changed row's output.
#[derive(Debug, Clone, Default, Serialize)]
pub struct SchemaDelta {
    pub added: Vec<String>,
    pub removed: Vec<String>,
    pub retyped: Vec<RetypedColumn>,
    pub renamed: Vec<RenamedColumn>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RetypedColumn {
    pub column: String,
    pub from: String,
    pub to: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RenamedColumn {
    pub from: String,
    pub to: String,
}

impl SchemaDelta {
    pub fn is_empty(&self) -> bool {
        self.added.is_empty()
            && self.removed.is_empty()
            && self.retyped.is_empty()
            && self.renamed.is_empty()
    }

    /// The per-column change map the downstream walk starts from.
    fn changes(&self) -> BTreeMap<String, ColumnChange> {
        let mut m = BTreeMap::new();
        for c in &self.added {
            m.insert(c.clone(), ColumnChange::Added);
        }
        for c in &self.removed {
            m.insert(c.clone(), ColumnChange::Removed);
        }
        for r in &self.retyped {
            m.insert(
                r.column.clone(),
                ColumnChange::Retyped {
                    from: r.from.clone(),
                    to: r.to.clone(),
                },
            );
        }
        for r in &self.renamed {
            m.insert(r.from.clone(), ColumnChange::Renamed { to: r.to.clone() });
        }
        m
    }
}

/// One affected downstream column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AffectedColumn {
    /// The column *in the affected dataset*.
    pub column: String,
    /// The change to the upstream column it reads.
    pub change: ColumnChange,
    /// The upstream column(s) it reads that changed.
    pub reads: Vec<String>,
}

/// A downstream data contract that declares an affected column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ContractHit {
    pub pipeline: String,
    pub row: String,
    pub version: String,
    /// The declared fields the change touches.
    pub fields: Vec<String>,
}

/// One declared consumer, with its own severity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AffectedConsumer {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contact: Option<String>,
    pub severity: Severity,
    /// The affected columns it reads (all affected columns when it declared
    /// none).
    pub columns: Vec<String>,
}

/// One affected dataset.
#[derive(Debug, Clone, Serialize)]
pub struct AffectedDataset {
    pub id: String,
    pub uri: String,
    /// The pipeline / row that writes it (the last recorded edge into it).
    pub pipeline: String,
    pub row: String,
    /// Hops downstream of the changed row's sink (0 = the sink itself).
    pub depth: u32,
    pub severity: Severity,
    /// The path is opaque: an edge on the way carries no column lineage.
    pub opaque: bool,
    pub columns: Vec<AffectedColumn>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contract: Option<ContractHit>,
    pub owners: Vec<String>,
    pub consumers: Vec<AffectedConsumer>,
}

/// Where the planned schema came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PlannedFrom {
    /// The plan's sample, run through the row's transforms.
    Sample,
    /// The catalog's last source schema, pushed through the row's transform
    /// chain by column lineage.
    Lineage,
    /// Nothing — the chain is opaque and no sample was given.
    Unknown,
}

/// The dataset the changed row writes.
#[derive(Debug, Clone, Serialize)]
pub struct SinkDataset {
    pub id: String,
    pub uri: String,
    pub last_run_id: String,
}

/// The whole report.
#[derive(Debug, Clone, Serialize)]
pub struct ImpactReport {
    pub pipeline: String,
    pub row: String,
    /// `None` when the row has never recorded a run (no lineage edge).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub dataset: Option<SinkDataset>,
    pub planned_from: PlannedFrom,
    pub delta: SchemaDelta,
    /// Overall severity — the maximum over the affected items.
    pub severity: Severity,
    /// Downstream datasets (depth 0 = the row's own sink), nearest first.
    pub affected: Vec<AffectedDataset>,
    /// Every owner of an affected dataset, deduplicated.
    pub owners: Vec<String>,
    pub notes: Vec<String>,
}

impl ImpactReport {
    pub fn is_breaking(&self) -> bool {
        self.severity == Severity::Breaking
    }
}

/// The inputs for one analysis.
pub struct ImpactInputs<'a> {
    pub pipeline: &'a str,
    pub node: &'a ExpandedNode,
    /// The schema the row would now write (`infer_schema` shape) — from a
    /// plan sample, when there is one.
    pub planned_schema: Option<&'a Value>,
    pub depth: u32,
}

/// Run the analysis against `store`.
pub async fn analyze(store: &dyn RunHistory, inputs: ImpactInputs<'_>) -> CliResult<ImpactReport> {
    let read = |e: crate::serve::history::HistoryError| {
        CliError::Config(format!("catalog read failed: {e}"))
    };
    let depth = inputs.depth.clamp(1, MAX_DEPTH);
    let edges = store.catalog_lineage(None, depth).await.map_err(read)?;
    let node = inputs.node;
    let mut notes = Vec::new();

    // 1. The row's sink dataset: the edge this (pipeline, row) last recorded.
    let own_edge = edges
        .iter()
        .find(|e| e.pipeline == inputs.pipeline && e.row == node.id)
        .cloned();
    let Some(own_edge) = own_edge else {
        notes.push(format!(
            "row '{}' of pipeline '{}' has no recorded run in the catalog — no lineage to walk; \
             run it once (with a `catalog:` block) and plan again",
            node.id, inputs.pipeline
        ));
        return Ok(ImpactReport {
            pipeline: inputs.pipeline.to_string(),
            row: node.id.clone(),
            dataset: None,
            planned_from: match inputs.planned_schema {
                Some(_) => PlannedFrom::Sample,
                None => PlannedFrom::Unknown,
            },
            delta: SchemaDelta::default(),
            severity: Severity::None,
            affected: Vec::new(),
            owners: Vec::new(),
            notes,
        });
    };
    let sink = store
        .catalog_get_dataset(&own_edge.dst_id)
        .await
        .map_err(read)?
        .map(|d| d.dataset);
    let source = store
        .catalog_get_dataset(&own_edge.src_id)
        .await
        .map_err(read)?
        .map(|d| d.dataset);

    // 2. Current vs planned schema.
    let current = sink.as_ref().and_then(|d| d.current_schema.clone());
    let ops = crate::lineage_glue::column_ops(&node.transforms, masking_present(node));
    let (planned, planned_from) = match inputs.planned_schema {
        Some(s) => (Some(s.clone()), PlannedFrom::Sample),
        None => match source.as_ref().and_then(|d| d.current_schema.as_ref()) {
            Some(src_schema) => match push_schema(src_schema, &ops) {
                Some(p) => (Some(p), PlannedFrom::Lineage),
                None => {
                    notes.push(
                        "the row's transform chain is opaque (flatten / explode / keys_case / \
                         sql / wasm / custom): pass `--sample` to plan the output schema"
                            .to_string(),
                    );
                    (None, PlannedFrom::Unknown)
                }
            },
            None => {
                notes.push(
                    "the catalog has no schema for the row's source dataset: pass `--sample` \
                     to plan the output schema"
                        .to_string(),
                );
                (None, PlannedFrom::Unknown)
            }
        },
    };
    if current.is_none() {
        notes.push(
            "the catalog has no observed schema for the row's sink dataset yet — the delta \
             cannot be computed"
                .to_string(),
        );
    }
    let delta = match (&current, &planned) {
        (Some(cur), Some(plan)) => schema_delta(cur, plan, &ops),
        _ => SchemaDelta::default(),
    };
    let unknown_delta = planned.is_none() || current.is_none();

    // 3. Downstream walk.
    let datasets = load_datasets(store, &edges, &own_edge.dst_id).await?;
    let mut affected = walk_downstream(&own_edge, &edges, &datasets, &delta, unknown_delta, depth);

    // 4. Contract escalation, per affected dataset's writing pipeline.
    let mut snapshots: HashMap<String, Option<crate::serve::history::catalog::ConfigSnapshot>> =
        HashMap::new();
    for a in affected.iter_mut() {
        if a.depth == 0 {
            // The changed row's own contract is what `plan` already checks.
            continue;
        }
        let snap = match snapshots.get(&a.pipeline) {
            Some(s) => s.clone(),
            None => {
                let s = store
                    .catalog_last_config_snapshot(&a.pipeline)
                    .await
                    .map_err(read)?;
                snapshots.insert(a.pipeline.clone(), s.clone());
                s
            }
        };
        let Some(contract) = snap
            .as_ref()
            .and_then(|s| s.rows.get(&a.row))
            .and_then(|r| r.contract.as_ref())
        else {
            continue;
        };
        let declared: Vec<String> = contract
            .get("fields")
            .and_then(Value::as_array)
            .map(|f| {
                f.iter()
                    .filter_map(|x| x.get("name").and_then(Value::as_str))
                    .map(String::from)
                    .collect()
            })
            .unwrap_or_default();
        let version = contract
            .get("version")
            .and_then(Value::as_str)
            .unwrap_or("?")
            .to_string();
        let hit: Vec<String> = if a.opaque {
            // Every declared field may be affected; the contract is named so a
            // reviewer knows a promise sits on this path.
            declared.clone()
        } else {
            a.columns
                .iter()
                .filter(|c| c.change.severity() == Severity::Breaking)
                .map(|c| c.column.clone())
                .filter(|c| declared.contains(c))
                .collect()
        };
        if hit.is_empty() {
            continue;
        }
        if !a.opaque {
            a.severity = Severity::Breaking;
        }
        a.contract = Some(ContractHit {
            pipeline: a.pipeline.clone(),
            row: a.row.clone(),
            version,
            fields: hit,
        });
    }

    let severity = affected
        .iter()
        .map(|a| a.severity)
        .max()
        .unwrap_or(Severity::None);
    let mut owners: Vec<String> = affected
        .iter()
        .flat_map(|a| a.owners.iter().cloned())
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect();
    owners.sort();
    Ok(ImpactReport {
        pipeline: inputs.pipeline.to_string(),
        row: node.id.clone(),
        dataset: sink.as_ref().map(|d| SinkDataset {
            id: d.id.clone(),
            uri: d.uri.clone(),
            last_run_id: d.last_run_id.clone(),
        }),
        planned_from,
        delta,
        severity,
        affected,
        owners,
        notes,
    })
}

#[cfg(feature = "masking")]
fn masking_present(node: &ExpandedNode) -> bool {
    node.masking.is_some()
}
#[cfg(not(feature = "masking"))]
fn masking_present(_node: &ExpandedNode) -> bool {
    false
}

/// Load every dataset reachable downstream of `root` (plus `root`) in one map.
async fn load_datasets(
    store: &dyn RunHistory,
    edges: &[CatalogLineageEdge],
    root: &str,
) -> CliResult<HashMap<String, CatalogDataset>> {
    let mut wanted: BTreeSet<String> = BTreeSet::from([root.to_string()]);
    let mut frontier = vec![root.to_string()];
    while let Some(id) = frontier.pop() {
        for e in edges.iter().filter(|e| e.src_id == id) {
            if wanted.insert(e.dst_id.clone()) {
                frontier.push(e.dst_id.clone());
            }
        }
    }
    let mut out = HashMap::new();
    for id in wanted {
        if let Some(d) = store
            .catalog_get_dataset(&id)
            .await
            .map_err(|e| CliError::Config(format!("catalog read failed: {e}")))?
        {
            out.insert(id, d.dataset);
        }
    }
    Ok(out)
}

/// Column names + type strings of an `infer_schema`-shaped object.
fn columns_of(schema: &Value) -> BTreeMap<String, String> {
    schema
        .get("properties")
        .and_then(Value::as_object)
        .map(|p| p.iter().map(|(k, v)| (k.clone(), type_string(v))).collect())
        .unwrap_or_default()
}

/// A stable rendering of a property's type (`"string"`, `"integer|null"`, …).
fn type_string(prop: &Value) -> String {
    match prop.get("type") {
        Some(Value::String(s)) => s.clone(),
        Some(Value::Array(items)) => {
            let mut parts: Vec<String> = items
                .iter()
                .filter_map(Value::as_str)
                .map(String::from)
                .collect();
            parts.sort();
            parts.join("|")
        }
        _ => "unknown".to_string(),
    }
}

/// Push a source schema through the row's column ops: the planned output
/// columns, each typed like its single origin (or `unknown` for a literal /
/// multi-origin column). `None` when the chain is opaque.
pub fn push_schema(source: &Value, ops: &[ColumnOp]) -> Option<Value> {
    let cols = columns_of(source);
    let inputs: Vec<String> = cols.keys().cloned().collect();
    let lineage = faucet_lineage::derive_column_lineage(&inputs, ops)?;
    let mut props = serde_json::Map::new();
    for (out, ins) in &lineage.edges {
        let ty = match ins.as_slice() {
            [one] => cols.get(one).cloned().unwrap_or_else(|| "unknown".into()),
            _ => "unknown".to_string(),
        };
        props.insert(out.clone(), serde_json::json!({ "type": ty }));
    }
    Some(serde_json::json!({ "type": "object", "properties": props }))
}

/// The delta between the current and planned schema, with renames recovered
/// from the row's own `Rename` ops (a rename would otherwise read as a drop
/// plus an add).
pub fn schema_delta(current: &Value, planned: &Value, ops: &[ColumnOp]) -> SchemaDelta {
    let cur = columns_of(current);
    let plan = columns_of(planned);
    let mut added: Vec<String> = plan
        .keys()
        .filter(|k| !cur.contains_key(*k))
        .cloned()
        .collect();
    let mut removed: Vec<String> = cur
        .keys()
        .filter(|k| !plan.contains_key(*k))
        .cloned()
        .collect();
    let mut retyped = Vec::new();
    for (name, cur_ty) in &cur {
        if let Some(plan_ty) = plan.get(name)
            && plan_ty != cur_ty
            && plan_ty != "unknown"
        {
            retyped.push(RetypedColumn {
                column: name.clone(),
                from: cur_ty.clone(),
                to: plan_ty.clone(),
            });
        }
    }
    let mut renamed = Vec::new();
    for op in ops {
        if let ColumnOp::Rename(pairs) = op {
            for (from, to) in pairs {
                if removed.contains(from) && added.contains(to) {
                    removed.retain(|c| c != from);
                    added.retain(|c| c != to);
                    renamed.push(RenamedColumn {
                        from: from.clone(),
                        to: to.clone(),
                    });
                }
            }
        }
    }
    added.sort();
    removed.sort();
    SchemaDelta {
        added,
        removed,
        retyped,
        renamed,
    }
}

/// The recorded column-lineage facet of an edge as `out → ins`.
fn edge_fields(edge: &CatalogLineageEdge) -> Option<BTreeMap<String, Vec<String>>> {
    let fields = edge.column_lineage.as_ref()?.get("fields")?.as_object()?;
    Some(
        fields
            .iter()
            .map(|(out, ins)| {
                (
                    out.clone(),
                    ins.as_array()
                        .map(|a| {
                            a.iter()
                                .filter_map(Value::as_str)
                                .map(String::from)
                                .collect()
                        })
                        .unwrap_or_default(),
                )
            })
            .collect(),
    )
}

/// The affected consumers of a dataset given its affected column set.
fn affected_consumers(
    consumers: &[CatalogConsumer],
    columns: &[AffectedColumn],
    opaque: bool,
) -> Vec<AffectedConsumer> {
    consumers
        .iter()
        .filter_map(|c| {
            let hits: Vec<&AffectedColumn> = if c.columns.is_empty() {
                columns.iter().collect()
            } else {
                columns
                    .iter()
                    .filter(|a| c.columns.contains(&a.column))
                    .collect()
            };
            let severity = if opaque {
                Severity::Unknown
            } else {
                hits.iter()
                    .map(|a| a.change.severity())
                    .max()
                    .unwrap_or(Severity::None)
            };
            if severity == Severity::None {
                return None;
            }
            Some(AffectedConsumer {
                name: c.name.clone(),
                kind: c.kind.clone(),
                contact: c.contact.clone(),
                severity,
                columns: hits.iter().map(|a| a.column.clone()).collect(),
            })
        })
        .collect()
}

/// BFS downstream from the changed row's sink, carrying the affected column
/// set across each edge's column lineage.
fn walk_downstream(
    own_edge: &CatalogLineageEdge,
    edges: &[CatalogLineageEdge],
    datasets: &HashMap<String, CatalogDataset>,
    delta: &SchemaDelta,
    unknown_delta: bool,
    depth: u32,
) -> Vec<AffectedDataset> {
    // Depth 0: the sink itself.
    let root_changes = delta.changes();
    let root_columns: Vec<AffectedColumn> = root_changes
        .iter()
        .map(|(col, change)| AffectedColumn {
            column: col.clone(),
            change: change.clone(),
            reads: vec![col.clone()],
        })
        .collect();
    let root_ds = datasets.get(&own_edge.dst_id);
    let root_severity = if unknown_delta {
        Severity::Unknown
    } else {
        root_columns
            .iter()
            .map(|c| c.change.severity())
            .max()
            .unwrap_or(Severity::None)
    };
    let mut out = vec![AffectedDataset {
        id: own_edge.dst_id.clone(),
        uri: own_edge.dst_uri.clone(),
        pipeline: own_edge.pipeline.clone(),
        row: own_edge.row.clone(),
        depth: 0,
        severity: root_severity,
        opaque: unknown_delta,
        columns: root_columns.clone(),
        contract: None,
        owners: root_ds.map(|d| d.owners.clone()).unwrap_or_default(),
        consumers: root_ds
            .map(|d| affected_consumers(&d.consumers, &root_columns, unknown_delta))
            .unwrap_or_default(),
    }];

    // (dataset id, affected columns at that dataset, opaque so far)
    let mut frontier: Vec<(String, Vec<AffectedColumn>, bool)> =
        vec![(own_edge.dst_id.clone(), root_columns, unknown_delta)];
    let mut seen: HashSet<String> = HashSet::from([own_edge.dst_id.clone()]);
    for hop in 1..=depth {
        let mut next = Vec::new();
        for (id, cols, opaque_so_far) in &frontier {
            for e in edges.iter().filter(|e| &e.src_id == id) {
                if !seen.insert(e.dst_id.clone()) {
                    continue;
                }
                let (affected_cols, opaque) = match edge_fields(e) {
                    Some(fields) if !opaque_so_far => {
                        let mut acc: Vec<AffectedColumn> = Vec::new();
                        for (out_col, ins) in &fields {
                            let reads: Vec<&AffectedColumn> =
                                cols.iter().filter(|c| ins.contains(&c.column)).collect();
                            // A column added upstream is not read by anything
                            // recorded yet, so only non-additive changes flow.
                            let breaking: Vec<&AffectedColumn> = reads
                                .iter()
                                .copied()
                                .filter(|c| c.change != ColumnChange::Added)
                                .collect();
                            if let Some(worst) = breaking.first() {
                                acc.push(AffectedColumn {
                                    column: out_col.clone(),
                                    change: worst.change.clone(),
                                    reads: breaking.iter().map(|c| c.column.clone()).collect(),
                                });
                            }
                        }
                        (acc, false)
                    }
                    _ => (Vec::new(), true),
                };
                let ds = datasets.get(&e.dst_id);
                let severity = if opaque {
                    Severity::Unknown
                } else {
                    affected_cols
                        .iter()
                        .map(|c| c.change.severity())
                        .max()
                        .unwrap_or(Severity::None)
                };
                if severity == Severity::None {
                    // Nothing it reads changes; nothing past it can either.
                    continue;
                }
                out.push(AffectedDataset {
                    id: e.dst_id.clone(),
                    uri: e.dst_uri.clone(),
                    pipeline: e.pipeline.clone(),
                    row: e.row.clone(),
                    depth: hop,
                    severity,
                    opaque,
                    columns: affected_cols.clone(),
                    contract: None,
                    owners: ds.map(|d| d.owners.clone()).unwrap_or_default(),
                    consumers: ds
                        .map(|d| affected_consumers(&d.consumers, &affected_cols, opaque))
                        .unwrap_or_default(),
                });
                next.push((e.dst_id.clone(), affected_cols, opaque));
            }
        }
        if next.is_empty() {
            break;
        }
        frontier = next;
    }
    out
}

/// Human rendering for `faucet plan --impact`.
pub fn render_human(r: &ImpactReport) -> String {
    let mut out = String::new();
    out.push_str(&format!(
        "  impact: {} ({} affected dataset(s), planned schema from {})\n",
        r.severity.as_str(),
        r.affected.len(),
        match r.planned_from {
            PlannedFrom::Sample => "the sample",
            PlannedFrom::Lineage => "the catalog's source schema",
            PlannedFrom::Unknown => "nothing — unknown",
        }
    ));
    if let Some(d) = &r.dataset {
        out.push_str(&format!(
            "    sink dataset: {} (last run {})\n",
            d.uri, d.last_run_id
        ));
    }
    let d = &r.delta;
    if d.is_empty() {
        out.push_str("    schema delta: none\n");
    } else {
        out.push_str("    schema delta:");
        if !d.added.is_empty() {
            out.push_str(&format!(" +{}", d.added.join(" +")));
        }
        if !d.removed.is_empty() {
            out.push_str(&format!(" -{}", d.removed.join(" -")));
        }
        for t in &d.retyped {
            out.push_str(&format!(" ~{}:{}→{}", t.column, t.from, t.to));
        }
        for n in &d.renamed {
            out.push_str(&format!(" {}→{}", n.from, n.to));
        }
        out.push('\n');
    }
    for a in &r.affected {
        out.push_str(&format!(
            "    [{}] depth {} {} (written by {} / {}){}\n",
            a.severity.as_str(),
            a.depth,
            a.uri,
            a.pipeline,
            a.row,
            if a.opaque {
                " — opaque path, column lineage unknown"
            } else {
                ""
            }
        ));
        for c in &a.columns {
            let what = match &c.change {
                ColumnChange::Added => "added".to_string(),
                ColumnChange::Removed => "removed".to_string(),
                ColumnChange::Retyped { from, to } => format!("retyped {from}→{to}"),
                ColumnChange::Renamed { to } => format!("renamed to {to}"),
            };
            if a.depth == 0 {
                out.push_str(&format!("      {}: {what}\n", c.column));
            } else {
                out.push_str(&format!(
                    "      {} reads {} ({what})\n",
                    c.column,
                    c.reads.join(", ")
                ));
            }
        }
        if let Some(c) = &a.contract {
            out.push_str(&format!(
                "      contract v{} of {} / {} declares: {}\n",
                c.version,
                c.pipeline,
                c.row,
                c.fields.join(", ")
            ));
        }
        if !a.owners.is_empty() {
            out.push_str(&format!("      owners: {}\n", a.owners.join(", ")));
        }
        for c in &a.consumers {
            out.push_str(&format!(
                "      consumer {} [{}]{}{}\n",
                c.name,
                c.severity.as_str(),
                c.kind
                    .as_deref()
                    .map(|k| format!(" ({k})"))
                    .unwrap_or_default(),
                c.contact
                    .as_deref()
                    .map(|k| format!(" → {k}"))
                    .unwrap_or_default(),
            ));
        }
    }
    if !r.owners.is_empty() {
        out.push_str(&format!("    owners to notify: {}\n", r.owners.join(", ")));
    }
    for n in &r.notes {
        out.push_str(&format!("    note: {n}\n"));
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::serve::history::catalog::{
        CatalogAnnotation, CatalogUpdate, ConfigSnapshot, DatasetObservation, DatasetRole,
        RowSnapshot, dataset_id,
    };
    use crate::serve::history::memory::MemoryHistory;
    use chrono::Utc;
    use serde_json::json;

    fn schema(cols: &[(&str, &str)]) -> Value {
        let props: serde_json::Map<String, Value> = cols
            .iter()
            .map(|(n, t)| (n.to_string(), json!({ "type": t })))
            .collect();
        json!({ "type": "object", "properties": props })
    }

    fn update(
        pipeline: &str,
        row: &str,
        src: &str,
        src_schema: Value,
        dst: &str,
        dst_schema: Value,
        lineage: Option<Value>,
    ) -> CatalogUpdate {
        CatalogUpdate {
            run_id: format!("run-{pipeline}"),
            pipeline: pipeline.into(),
            row: row.into(),
            recorded_at: Utc::now(),
            sources: vec![DatasetObservation {
                uri: src.into(),
                kind: "csv".into(),
                role: DatasetRole::Source,
                schema: Some(src_schema),
                records: 10,
            }],
            sink: DatasetObservation {
                uri: dst.into(),
                kind: "jsonl".into(),
                role: DatasetRole::Sink,
                schema: Some(dst_schema),
                records: 10,
            },
            column_lineage: lineage,
        }
    }

    fn node(transforms_yaml: &str) -> ExpandedNode {
        let cfg = crate::config::PipelineConfig::from_text(
            &format!(
                "version: 1\nname: a\npipeline:\n  source: {{ type: csv, config: {{ path: ./in.csv }} }}\n  sink: {{ type: jsonl, config: {{ path: ./a.jsonl }} }}\n{transforms_yaml}"
            ),
            std::path::Path::new("a.yaml"),
        )
        .unwrap();
        crate::expand::expand(&cfg).unwrap().remove(0)
    }

    /// A → B → C chain: A writes a.jsonl; B reads a.jsonl (renames email →
    /// contact) into b.jsonl; C reads b.jsonl opaquely into c.jsonl.
    async fn seed() -> MemoryHistory {
        let store = MemoryHistory::new(std::time::Duration::from_secs(60));
        store
            .catalog_record(&update(
                "a",
                "row-0",
                "file:///in.csv",
                schema(&[
                    ("id", "integer"),
                    ("email", "string"),
                    ("amount", "integer"),
                ]),
                "file:///a.jsonl",
                schema(&[
                    ("id", "integer"),
                    ("email", "string"),
                    ("amount", "integer"),
                ]),
                Some(
                    json!({ "fields": { "id": ["id"], "email": ["email"], "amount": ["amount"] } }),
                ),
            ))
            .await
            .unwrap();
        store
            .catalog_record(&update(
                "b",
                "row-0",
                "file:///a.jsonl",
                schema(&[
                    ("id", "integer"),
                    ("email", "string"),
                    ("amount", "integer"),
                ]),
                "file:///b.jsonl",
                schema(&[("id", "integer"), ("contact", "string")]),
                Some(json!({ "fields": { "id": ["id"], "contact": ["email"] } })),
            ))
            .await
            .unwrap();
        store
            .catalog_record(&update(
                "c",
                "row-0",
                "file:///b.jsonl",
                schema(&[("id", "integer"), ("contact", "string")]),
                "file:///c.jsonl",
                schema(&[("blob", "string")]),
                None,
            ))
            .await
            .unwrap();
        store
            .catalog_annotate(
                &dataset_id("file:///b.jsonl"),
                &CatalogAnnotation {
                    owners: Some(vec!["team-b".into()]),
                    consumers: vec![
                        CatalogConsumer {
                            name: "contacts-dashboard".into(),
                            kind: Some("dashboard".into()),
                            contact: Some("#bi".into()),
                            columns: vec!["contact".into()],
                            registered_by: "test".into(),
                            registered_at: Utc::now(),
                        },
                        CatalogConsumer {
                            name: "id-export".into(),
                            kind: None,
                            contact: None,
                            columns: vec!["id".into()],
                            registered_by: "test".into(),
                            registered_at: Utc::now(),
                        },
                    ],
                    replace_consumers: false,
                },
            )
            .await
            .unwrap();
        store
    }

    #[tokio::test]
    async fn dropping_a_read_column_is_breaking_downstream_and_names_owners() {
        let store = seed().await;
        let n = node("  transforms:\n    - type: drop\n      config: { fields: [email] }\n");
        let planned = schema(&[("id", "integer"), ("amount", "integer")]);
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: Some(&planned),
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(r.severity, Severity::Breaking, "{r:?}");
        assert_eq!(r.delta.removed, vec!["email"]);
        assert_eq!(r.planned_from, PlannedFrom::Sample);
        // depth 0 = a.jsonl, depth 1 = b.jsonl (contact reads email), depth 2 = c (opaque).
        assert_eq!(r.affected.len(), 3);
        let b = &r.affected[1];
        assert_eq!(b.uri, "file:///b.jsonl");
        assert_eq!(b.severity, Severity::Breaking);
        assert_eq!(b.columns[0].column, "contact");
        assert_eq!(b.columns[0].reads, vec!["email"]);
        assert_eq!(b.owners, vec!["team-b"]);
        let names: Vec<&str> = b.consumers.iter().map(|c| c.name.as_str()).collect();
        assert_eq!(names, vec!["contacts-dashboard"], "id-export reads only id");
        assert_eq!(b.consumers[0].severity, Severity::Breaking);
        let c = &r.affected[2];
        assert_eq!(c.severity, Severity::Unknown);
        assert!(c.opaque);
        assert_eq!(r.owners, vec!["team-b"]);
        let text = render_human(&r);
        assert!(
            text.contains("[breaking] depth 1 file:///b.jsonl"),
            "{text}"
        );
        assert!(text.contains("contact reads email (removed)"), "{text}");
        assert!(text.contains("owners to notify: team-b"), "{text}");
    }

    #[tokio::test]
    async fn adding_a_column_is_additive_and_does_not_reach_downstream() {
        let store = seed().await;
        let n = node("");
        let planned = schema(&[
            ("id", "integer"),
            ("email", "string"),
            ("amount", "integer"),
            ("country", "string"),
        ]);
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: Some(&planned),
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(r.severity, Severity::Additive);
        assert_eq!(r.delta.added, vec!["country"]);
        assert_eq!(r.affected.len(), 1, "{r:?}");
        assert_eq!(r.affected[0].depth, 0);
    }

    #[tokio::test]
    async fn rename_in_the_chain_is_reported_as_a_rename_and_breaks_readers() {
        let store = seed().await;
        let n = node(
            "  transforms:\n    - type: rename_field\n      config: { fields: { email: mail } }\n",
        );
        // No sample: planned from the source schema through the chain.
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: None,
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(r.planned_from, PlannedFrom::Lineage);
        assert_eq!(
            r.delta.renamed,
            vec![RenamedColumn {
                from: "email".into(),
                to: "mail".into()
            }]
        );
        assert!(r.delta.added.is_empty() && r.delta.removed.is_empty());
        assert_eq!(r.severity, Severity::Breaking);
        assert!(render_human(&r).contains("email→mail"));
    }

    #[tokio::test]
    async fn contract_downstream_escalates_and_names_the_version() {
        let store = seed().await;
        // B declares a contract over `contact`.
        let mut rows = std::collections::BTreeMap::new();
        rows.insert(
            "row-0".to_string(),
            RowSnapshot {
                source: crate::serve::history::catalog::ConnectorSnapshot {
                    kind: "jsonl".into(),
                    config: json!({}),
                },
                sink: crate::serve::history::catalog::ConnectorSnapshot {
                    kind: "jsonl".into(),
                    config: json!({}),
                },
                transforms: vec![],
                state_key: None,
                delivery_guarantee: "x".into(),
                on_error: "stop".into(),
                dlq: false,
                contract: Some(
                    json!({ "version": "2", "fields": [{ "name": "contact", "type": "string" }] }),
                ),
            },
        );
        store
            .catalog_record_config_snapshot(&ConfigSnapshot {
                pipeline: "b".into(),
                recorded_at: Utc::now(),
                faucet_version: "t".into(),
                rows,
            })
            .await
            .unwrap();
        let n = node("");
        let planned = schema(&[
            ("id", "integer"),
            ("email", "integer"),
            ("amount", "integer"),
        ]);
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: Some(&planned),
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(
            r.delta.retyped,
            vec![RetypedColumn {
                column: "email".into(),
                from: "string".into(),
                to: "integer".into()
            }]
        );
        let b = &r.affected[1];
        let hit = b.contract.as_ref().expect("contract hit");
        assert_eq!(hit.version, "2");
        assert_eq!(hit.fields, vec!["contact"]);
        assert!(render_human(&r).contains("contract v2 of b / row-0 declares: contact"));
    }

    #[tokio::test]
    async fn never_run_row_reports_no_lineage() {
        let store = seed().await;
        let n = node("");
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "never",
                node: &n,
                planned_schema: None,
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert!(r.dataset.is_none());
        assert!(r.affected.is_empty());
        assert_eq!(r.severity, Severity::None);
        assert!(r.notes[0].contains("no recorded run"));
        assert!(render_human(&r).contains("note: row"));
    }

    #[tokio::test]
    async fn opaque_own_chain_without_sample_is_unknown() {
        let store = seed().await;
        let n = node("  transforms:\n    - type: flatten\n      config: {}\n");
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: None,
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(r.planned_from, PlannedFrom::Unknown);
        assert_eq!(r.severity, Severity::Unknown);
        assert!(r.affected.iter().all(|a| a.opaque));
        assert!(r.notes.iter().any(|n| n.contains("opaque")));
    }

    #[tokio::test]
    async fn planned_schema_comes_from_lineage_when_the_chain_is_analysable() {
        let store = seed().await;
        let n = node(
            "  transforms:\n    - type: rename_field\n      config: { fields: { email: contact } }\n",
        );
        let r = analyze(
            &store,
            ImpactInputs {
                pipeline: "a",
                node: &n,
                planned_schema: None,
                depth: 5,
            },
        )
        .await
        .unwrap();
        assert_eq!(r.planned_from, PlannedFrom::Lineage);
        assert_eq!(
            r.delta.renamed,
            vec![RenamedColumn {
                from: "email".into(),
                to: "contact".into()
            }]
        );
        assert!(r.is_breaking());
        assert_eq!(Severity::Breaking.as_str(), "breaking");
        assert_eq!(Severity::Additive.as_str(), "additive");
        assert_eq!(Severity::Unknown.as_str(), "unknown");
        assert_eq!(Severity::None.as_str(), "none");
    }

    #[test]
    fn schema_delta_folds_a_rename_op_and_render_lists_every_change_kind() {
        let cur = schema(&[("a", "integer"), ("b", "string"), ("c", "integer")]);
        let plan = schema(&[("x", "integer"), ("b", "string"), ("c", "string")]);
        let d = schema_delta(
            &cur,
            &plan,
            &[ColumnOp::Rename(vec![("a".into(), "x".into())])],
        );
        assert!(d.added.is_empty() && d.removed.is_empty());
        assert_eq!(d.renamed[0].from, "a");
        assert_eq!(d.retyped[0].column, "c");
        let r = ImpactReport {
            pipeline: "a".into(),
            row: "row-0".into(),
            dataset: Some(SinkDataset {
                id: "d".into(),
                uri: "file:///a.jsonl".into(),
                last_run_id: "run".into(),
            }),
            planned_from: PlannedFrom::Sample,
            delta: d,
            severity: Severity::Breaking,
            affected: vec![AffectedDataset {
                id: "b".into(),
                uri: "file:///b.jsonl".into(),
                pipeline: "b".into(),
                row: "row-0".into(),
                depth: 1,
                severity: Severity::Breaking,
                opaque: true,
                columns: vec![
                    AffectedColumn {
                        column: "x".into(),
                        change: ColumnChange::Renamed { to: "y".into() },
                        reads: vec!["x".into()],
                    },
                    AffectedColumn {
                        column: "c".into(),
                        change: ColumnChange::Retyped {
                            from: "integer".into(),
                            to: "string".into(),
                        },
                        reads: vec![],
                    },
                ],
                contract: None,
                owners: vec![],
                consumers: vec![],
            }],
            owners: vec![],
            notes: vec![],
        };
        let text = render_human(&r);
        assert!(text.contains("~c:integer→string"), "{text}");
        assert!(text.contains("a→x"), "{text}");
        assert!(text.contains("opaque path"), "{text}");
        assert!(text.contains("renamed to y"), "{text}");
        assert!(text.contains("retyped integer→string"), "{text}");
    }

    #[test]
    fn push_schema_types_follow_single_origins() {
        let src = schema(&[("a", "integer"), ("b", "string")]);
        let ops = vec![
            ColumnOp::Rename(vec![("a".into(), "x".into())]),
            ColumnOp::Set(vec!["lit".into()]),
        ];
        let out = push_schema(&src, &ops).unwrap();
        let cols = columns_of(&out);
        assert_eq!(cols["x"], "integer");
        assert_eq!(cols["b"], "string");
        assert_eq!(cols["lit"], "unknown");
        assert!(push_schema(&src, &[ColumnOp::Opaque]).is_none());
        assert_eq!(
            type_string(&json!({ "type": ["null", "string"] })),
            "null|string"
        );
        assert_eq!(type_string(&json!({})), "unknown");
    }
}