shape-runtime 0.3.0

Bytecode compiler, builtins, and runtime infrastructure for Shape
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
//! Data-source schema cache backed by unified `shape.lock` artifacts.
//!
//! This replaces the legacy `shape.database.lock.json` sidecar file and keeps
//! external schema state in the shared lockfile model.

use crate::package_lock::{ArtifactDeterminism, LockedArtifact, PackageLock};
use crate::type_schema::{TypeSchema, TypeSchemaBuilder};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use shape_value::KindedSlot;
use shape_wire::WireValue;
use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use std::sync::RwLock;

/// Top-level cache file path. Kept for call-site compatibility.
pub const CACHE_FILENAME: &str = "shape.lock";

const SCHEMA_CACHE_VERSION: u32 = 1;
const SCHEMA_CACHE_NAMESPACE: &str = "external.datasource.schema";
const SCHEMA_CACHE_PRODUCER: &str = "shape-runtime/schema_cache@v1";

static DEFAULT_CACHE_PATH_OVERRIDE: std::sync::LazyLock<RwLock<Option<PathBuf>>> =
    std::sync::LazyLock::new(|| RwLock::new(None));

/// Diagnostic emitted while loading schema artifacts from `shape.lock`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SchemaCacheDiagnostic {
    pub key: String,
    pub message: String,
}

/// Top-level cache model for external data-source schemas.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataSourceSchemaCache {
    /// Schema cache format version.
    pub version: u32,
    /// Source URI -> schema mapping.
    pub sources: HashMap<String, SourceSchema>,
}

/// Schema for a single data-source URI.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceSchema {
    /// The source URI (e.g., "duckdb://analytics.db").
    pub uri: String,
    /// Entity name -> schema mapping.
    pub tables: HashMap<String, EntitySchema>,
    /// RFC3339 timestamp of when schemas were last fetched.
    pub cached_at: String,
}

/// Schema for a single data-source entity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntitySchema {
    /// Entity name.
    pub name: String,
    /// Ordered list of fields.
    pub columns: Vec<FieldSchema>,
}

/// Schema for a single field.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldSchema {
    /// Column name.
    pub name: String,
    /// Shape type: `int`, `number`, `string`, `bool`, `timestamp`.
    #[serde(rename = "type")]
    pub shape_type: String,
    /// Whether the column accepts null values.
    pub nullable: bool,
}

/// Convert a `SourceSchema` into a [`KindedSlot`] typed-object payload.
///
/// Phase 1.B (ADR-006 §2.7.4 audit-accuracy ruling): the runtime-side
/// SourceSchema↔KindedSlot path goes through nested
/// `typed_object_from_pairs`. The current `typed_object_from_pairs`
/// expects the schema to already be registered for the `(uri, tables,
/// cached_at)` and per-table-entity field shapes — those schemas are
/// not currently registered (they were synthesized at runtime under
/// the deleted ValueWord path). The Phase 2c rebuild registers the
/// schemas as predeclared and rebuilds this helper. Until then, this
/// function returns an empty `KindedSlot` placeholder.
///
/// The wire-format path (`source_schema_to_wire`) is unaffected and
/// remains the canonical persisted-format converter.
pub fn source_schema_to_nb(_schema: &SourceSchema) -> KindedSlot {
    KindedSlot::none()
}

/// Decode a `SourceSchema` from a typed-object [`KindedSlot`] payload.
///
/// See [`source_schema_to_nb`] — Phase 2c rebuild deferral. The wire-
/// format path (`source_schema_from_wire`) handles the persisted case.
pub fn source_schema_from_nb(_value: &KindedSlot) -> Result<SourceSchema, String> {
    Err("source_schema_from_nb: pending Phase 2c kind-threaded TypedObject decode — see ADR-006 §2.7.4".to_string())
}

/// Decode a `SourceSchema` from a shape-wire object payload.
pub fn source_schema_from_wire(value: &WireValue) -> Result<SourceSchema, String> {
    let object = match value {
        WireValue::Object(map) => map,
        _ => return Err("schema payload must be an object".to_string()),
    };

    let uri = object
        .get("uri")
        .and_then(|v| match v {
            WireValue::String(s) => Some(s.clone()),
            _ => None,
        })
        .ok_or_else(|| "schema payload missing string field 'uri'".to_string())?;

    let cached_at = object
        .get("cached_at")
        .and_then(|v| match v {
            WireValue::String(s) => Some(s.clone()),
            _ => None,
        })
        .unwrap_or_default();

    let tables_value = object
        .get("tables")
        .ok_or_else(|| "schema payload missing object field 'tables'".to_string())?;
    let tables_object = match tables_value {
        WireValue::Object(map) => map,
        _ => return Err("schema payload field 'tables' must be an object".to_string()),
    };

    let mut tables = HashMap::new();
    for (table_name, entity_value) in tables_object {
        let entity_obj = match entity_value {
            WireValue::Object(map) => map,
            _ => return Err(format!("table '{table_name}' schema must be an object")),
        };

        let entity_name = entity_obj
            .get("name")
            .and_then(|v| match v {
                WireValue::String(s) => Some(s.clone()),
                _ => None,
            })
            .unwrap_or_else(|| table_name.clone());

        let columns_value = entity_obj
            .get("columns")
            .ok_or_else(|| format!("table '{table_name}' missing 'columns' array"))?;
        let columns_array = match columns_value {
            WireValue::Array(values) => values,
            _ => {
                return Err(format!(
                    "table '{table_name}' field 'columns' must be an array"
                ));
            }
        };

        let mut columns = Vec::new();
        for column_value in columns_array {
            let column_obj = match column_value {
                WireValue::Object(map) => map,
                _ => {
                    return Err(format!(
                        "table '{table_name}' contains non-object column entry"
                    ));
                }
            };

            let name = column_obj
                .get("name")
                .and_then(|v| match v {
                    WireValue::String(s) => Some(s.clone()),
                    _ => None,
                })
                .ok_or_else(|| format!("table '{table_name}' column missing string 'name'"))?;

            let shape_type = column_obj
                .get("type")
                .or_else(|| column_obj.get("shape_type"))
                .and_then(|v| match v {
                    WireValue::String(s) => Some(s.clone()),
                    _ => None,
                })
                .ok_or_else(|| format!("table '{table_name}' column '{name}' missing type"))?;

            let nullable = column_obj
                .get("nullable")
                .and_then(|v| match v {
                    WireValue::Bool(b) => Some(*b),
                    _ => None,
                })
                .unwrap_or(false);

            columns.push(FieldSchema {
                name,
                shape_type,
                nullable,
            });
        }

        tables.insert(
            table_name.clone(),
            EntitySchema {
                name: entity_name,
                columns,
            },
        );
    }

    Ok(SourceSchema {
        uri,
        tables,
        cached_at,
    })
}

impl DataSourceSchemaCache {
    /// Create an empty cache.
    pub fn new() -> Self {
        Self {
            version: SCHEMA_CACHE_VERSION,
            sources: HashMap::new(),
        }
    }

    /// Save cache entries into `shape.lock` artifacts.
    pub fn save(&self, path: &Path) -> std::io::Result<()> {
        let mut lock = PackageLock::read(path).unwrap_or_default();

        // Replace this namespace wholesale to keep the lock deterministic.
        lock.artifacts
            .retain(|artifact| artifact.namespace != SCHEMA_CACHE_NAMESPACE);

        let mut uris: Vec<_> = self.sources.keys().cloned().collect();
        uris.sort();

        for uri in uris {
            let Some(source) = self.sources.get(&uri) else {
                continue;
            };

            let schema_hash = hash_source_schema(source);
            let payload = source_to_payload(source);

            let mut inputs = BTreeMap::new();
            inputs.insert("uri".to_string(), uri.clone());
            inputs.insert("schema_hash".to_string(), schema_hash.clone());

            let determinism = ArtifactDeterminism::External {
                fingerprints: BTreeMap::from([(format!("schema:{uri}"), schema_hash)]),
            };

            let artifact = LockedArtifact::new(
                SCHEMA_CACHE_NAMESPACE,
                uri,
                SCHEMA_CACHE_PRODUCER,
                determinism,
                inputs,
                payload,
            )
            .map_err(std::io::Error::other)?;

            lock.upsert_artifact(artifact)
                .map_err(std::io::Error::other)?;
        }

        lock.write(path)
    }

    /// Load cache entries from `shape.lock` artifacts.
    pub fn load(path: &Path) -> std::io::Result<Self> {
        let (cache, diagnostics) = Self::load_with_diagnostics(path)?;
        if let Some(diag) = diagnostics.first() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                format!("invalid schema artifact '{}': {}", diag.key, diag.message),
            ));
        }
        Ok(cache)
    }

    /// Load cache entries from `shape.lock` artifacts and collect diagnostics for
    /// stale/invalid artifacts while keeping valid entries.
    pub fn load_with_diagnostics(
        path: &Path,
    ) -> std::io::Result<(Self, Vec<SchemaCacheDiagnostic>)> {
        let lock = PackageLock::read(path).ok_or_else(|| {
            std::io::Error::new(std::io::ErrorKind::NotFound, "shape.lock not found")
        })?;

        let mut sources = HashMap::new();
        let mut diagnostics = Vec::new();
        for artifact in lock
            .artifacts
            .iter()
            .filter(|artifact| artifact.namespace == SCHEMA_CACHE_NAMESPACE)
        {
            let payload = match artifact.payload() {
                Ok(payload) => payload,
                Err(err) => {
                    diagnostics.push(SchemaCacheDiagnostic {
                        key: artifact.key.clone(),
                        message: format!("payload decode failed: {err}"),
                    });
                    continue;
                }
            };
            let source = match payload_to_source(&artifact.key, &payload) {
                Ok(source) => source,
                Err(err) => {
                    diagnostics.push(SchemaCacheDiagnostic {
                        key: artifact.key.clone(),
                        message: format!("payload parse failed: {err}"),
                    });
                    continue;
                }
            };

            if let Some(expected_hash) = source_fingerprint_hash(artifact, &source.uri) {
                let actual_hash = hash_source_schema(&source);
                if expected_hash != actual_hash {
                    diagnostics.push(SchemaCacheDiagnostic {
                        key: artifact.key.clone(),
                        message: format!(
                            "stale schema fingerprint (expected {expected_hash}, computed {actual_hash})"
                        ),
                    });
                    continue;
                }
            } else {
                diagnostics.push(SchemaCacheDiagnostic {
                    key: artifact.key.clone(),
                    message: "missing schema fingerprint".to_string(),
                });
                continue;
            }
            sources.insert(source.uri.clone(), source);
        }

        Ok((
            Self {
                version: SCHEMA_CACHE_VERSION,
                sources,
            },
            diagnostics,
        ))
    }

    /// Try to load cache entries, returning an empty cache if lockfile is missing.
    pub fn load_or_empty(path: &Path) -> Self {
        match Self::load(path) {
            Ok(cache) => cache,
            Err(_) => Self::new(),
        }
    }

    /// Get schema for a specific source URI.
    pub fn get_source(&self, uri: &str) -> Option<&SourceSchema> {
        self.sources.get(uri)
    }

    /// Insert or update a source schema.
    pub fn upsert_source(&mut self, schema: SourceSchema) {
        self.sources.insert(schema.uri.clone(), schema);
    }

    /// Check if offline mode is enabled via `SHAPE_OFFLINE=true`.
    pub fn is_offline() -> bool {
        std::env::var("SHAPE_OFFLINE")
            .map(|value| value == "true" || value == "1")
            .unwrap_or(false)
    }
}

/// Load cached schemas and convert matching sources into runtime `TypeSchema`s.
///
/// `uri_prefixes` filters which sources are included (for example:
/// `["duckdb://"]` or `["postgres://", "postgresql://"]`).
pub fn load_cached_type_schemas_for_uri_prefixes(
    cache_path: &Path,
    uri_prefixes: &[&str],
) -> std::io::Result<Vec<TypeSchema>> {
    let (schemas, _diagnostics) =
        load_cached_type_schemas_for_uri_prefixes_with_diagnostics(cache_path, uri_prefixes)?;
    Ok(schemas)
}

/// Like [`load_cached_type_schemas_for_uri_prefixes`] but also returns
/// non-fatal diagnostics for invalid/stale artifacts that were ignored.
pub fn load_cached_type_schemas_for_uri_prefixes_with_diagnostics(
    cache_path: &Path,
    uri_prefixes: &[&str],
) -> std::io::Result<(Vec<TypeSchema>, Vec<SchemaCacheDiagnostic>)> {
    let (cache, diagnostics) = DataSourceSchemaCache::load_with_diagnostics(cache_path)?;
    let mut schemas = Vec::new();

    for source in cache.sources.values() {
        if !uri_prefixes.is_empty()
            && !uri_prefixes
                .iter()
                .any(|prefix| source.uri.starts_with(prefix))
        {
            continue;
        }

        for table in source.tables.values() {
            schemas.push(type_schema_from_entity(table));
        }
    }

    Ok((schemas, diagnostics))
}

/// Resolve the default `shape.lock` path from the current working directory.
pub fn default_cache_path() -> PathBuf {
    if let Ok(guard) = DEFAULT_CACHE_PATH_OVERRIDE.read() {
        if let Some(path) = guard.as_ref() {
            return path.clone();
        }
    }

    std::env::current_dir()
        .unwrap_or_default()
        .join(CACHE_FILENAME)
}

/// Override the default lock/cache path used by extension-side schema cache
/// helpers during this process.
pub fn set_default_cache_path(path: Option<PathBuf>) {
    if let Ok(mut guard) = DEFAULT_CACHE_PATH_OVERRIDE.write() {
        *guard = path;
    }
}

/// Load one cached source schema by URI.
pub fn load_cached_source_for_uri(cache_path: &Path, uri: &str) -> std::io::Result<SourceSchema> {
    let (source, diagnostics) = load_cached_source_for_uri_with_diagnostics(cache_path, uri)?;
    if let Some(diag) = diagnostics.first() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("invalid schema artifact '{}': {}", diag.key, diag.message),
        ));
    }
    Ok(source)
}

/// Load one cached source schema by URI and keep non-fatal diagnostics for
/// stale/invalid artifacts that were ignored.
pub fn load_cached_source_for_uri_with_diagnostics(
    cache_path: &Path,
    uri: &str,
) -> std::io::Result<(SourceSchema, Vec<SchemaCacheDiagnostic>)> {
    let (cache, diagnostics) = DataSourceSchemaCache::load_with_diagnostics(cache_path)?;
    let source = cache.get_source(uri).cloned().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::NotFound,
            format!("no cached schema for '{uri}'"),
        )
    })?;
    Ok((source, diagnostics))
}

impl Default for DataSourceSchemaCache {
    fn default() -> Self {
        Self::new()
    }
}

impl SourceSchema {
    /// Get schema for a specific entity.
    pub fn get_entity(&self, name: &str) -> Option<&EntitySchema> {
        self.tables.get(name)
    }

    /// Get all entity names.
    pub fn entity_names(&self) -> Vec<&str> {
        self.tables.keys().map(|name| name.as_str()).collect()
    }
}

impl EntitySchema {
    /// Get a field by name.
    pub fn get_field(&self, name: &str) -> Option<&FieldSchema> {
        self.columns.iter().find(|column| column.name == name)
    }

    /// Get all field names.
    pub fn field_names(&self) -> Vec<&str> {
        self.columns
            .iter()
            .map(|column| column.name.as_str())
            .collect()
    }
}

fn hash_source_schema(source: &SourceSchema) -> String {
    let mut hasher = Sha256::new();
    hasher.update(source.uri.as_bytes());
    hasher.update([0]);

    let mut entity_names: Vec<_> = source.tables.keys().cloned().collect();
    entity_names.sort();
    for entity_name in entity_names {
        hasher.update(entity_name.as_bytes());
        hasher.update([0]);

        if let Some(entity) = source.tables.get(&entity_name) {
            for field in &entity.columns {
                hasher.update(field.name.as_bytes());
                hasher.update([0]);
                hasher.update(field.shape_type.as_bytes());
                hasher.update([0]);
                hasher.update([if field.nullable { 1 } else { 0 }]);
            }
        }
    }

    format!("sha256:{:x}", hasher.finalize())
}

fn source_fingerprint_hash<'a>(artifact: &'a LockedArtifact, uri: &str) -> Option<&'a str> {
    if let ArtifactDeterminism::External { fingerprints } = &artifact.determinism {
        let key = format!("schema:{uri}");
        if let Some(value) = fingerprints.get(&key) {
            return Some(value.as_str());
        }
    }
    artifact.inputs.get("schema_hash").map(String::as_str)
}

fn type_schema_from_entity(entity: &EntitySchema) -> TypeSchema {
    let schema_name = format!("DbRow_{}", entity.name);
    let builder =
        entity
            .columns
            .iter()
            .fold(
                TypeSchemaBuilder::new(&schema_name),
                |builder, field| match field.shape_type.as_str() {
                    "int" => builder.i64_field(&field.name),
                    "number" => builder.f64_field(&field.name),
                    "string" => builder.string_field(&field.name),
                    "bool" => builder.bool_field(&field.name),
                    "timestamp" => builder.timestamp_field(&field.name),
                    _ => builder.any_field(&field.name),
                },
            );
    builder.build()
}

fn source_to_payload(source: &SourceSchema) -> shape_wire::WireValue {
    let mut entities: Vec<_> = source.tables.values().collect();
    entities.sort_by(|left, right| left.name.cmp(&right.name));

    let entity_values = entities
        .into_iter()
        .map(|entity| {
            let field_values = entity
                .columns
                .iter()
                .map(|field| {
                    shape_wire::WireValue::Object(BTreeMap::from([
                        (
                            "name".to_string(),
                            shape_wire::WireValue::String(field.name.clone()),
                        ),
                        (
                            "shape_type".to_string(),
                            shape_wire::WireValue::String(field.shape_type.clone()),
                        ),
                        (
                            "nullable".to_string(),
                            shape_wire::WireValue::Bool(field.nullable),
                        ),
                    ]))
                })
                .collect::<Vec<_>>();

            shape_wire::WireValue::Object(BTreeMap::from([
                (
                    "name".to_string(),
                    shape_wire::WireValue::String(entity.name.clone()),
                ),
                (
                    "columns".to_string(),
                    shape_wire::WireValue::Array(field_values),
                ),
            ]))
        })
        .collect::<Vec<_>>();

    shape_wire::WireValue::Object(BTreeMap::from([
        (
            "uri".to_string(),
            shape_wire::WireValue::String(source.uri.clone()),
        ),
        (
            "cached_at".to_string(),
            shape_wire::WireValue::String(source.cached_at.clone()),
        ),
        (
            "tables".to_string(),
            shape_wire::WireValue::Array(entity_values),
        ),
    ]))
}

fn payload_to_source(
    key_hint: &str,
    payload: &shape_wire::WireValue,
) -> Result<SourceSchema, String> {
    let shape_wire::WireValue::Object(map) = payload else {
        return Err("source payload must be an object".to_string());
    };

    let uri = map
        .get("uri")
        .and_then(shape_wire::WireValue::as_str)
        .map(|value| value.to_string())
        .filter(|value| !value.is_empty())
        .unwrap_or_else(|| key_hint.to_string());
    let cached_at = map
        .get("cached_at")
        .and_then(shape_wire::WireValue::as_str)
        .unwrap_or("")
        .to_string();

    let tables_value = map
        .get("tables")
        .ok_or_else(|| "source payload missing 'tables'".to_string())?;
    let shape_wire::WireValue::Array(table_values) = tables_value else {
        return Err("source payload 'tables' must be an array".to_string());
    };

    let mut tables = HashMap::new();
    for table_value in table_values {
        let entity = payload_to_entity(table_value)?;
        tables.insert(entity.name.clone(), entity);
    }

    Ok(SourceSchema {
        uri,
        tables,
        cached_at,
    })
}

fn payload_to_entity(value: &shape_wire::WireValue) -> Result<EntitySchema, String> {
    let shape_wire::WireValue::Object(table_map) = value else {
        return Err("table payload must be an object".to_string());
    };

    let name = table_map
        .get("name")
        .and_then(shape_wire::WireValue::as_str)
        .ok_or_else(|| "table payload missing 'name'".to_string())?
        .to_string();
    let columns_value = table_map
        .get("columns")
        .ok_or_else(|| "table payload missing 'columns'".to_string())?;
    let shape_wire::WireValue::Array(column_values) = columns_value else {
        return Err("table payload 'columns' must be an array".to_string());
    };

    let mut columns = Vec::with_capacity(column_values.len());
    for column_value in column_values {
        columns.push(payload_to_field(column_value)?);
    }

    Ok(EntitySchema { name, columns })
}

fn payload_to_field(value: &shape_wire::WireValue) -> Result<FieldSchema, String> {
    let shape_wire::WireValue::Object(column_map) = value else {
        return Err("column payload must be an object".to_string());
    };

    let name = column_map
        .get("name")
        .and_then(shape_wire::WireValue::as_str)
        .ok_or_else(|| "column payload missing 'name'".to_string())?
        .to_string();
    let shape_type = column_map
        .get("shape_type")
        .and_then(shape_wire::WireValue::as_str)
        .ok_or_else(|| "column payload missing 'shape_type'".to_string())?
        .to_string();
    let nullable = column_map
        .get("nullable")
        .and_then(shape_wire::WireValue::as_bool)
        .ok_or_else(|| "column payload missing 'nullable'".to_string())?;

    Ok(FieldSchema {
        name,
        shape_type,
        nullable,
    })
}

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

    fn sample_cache() -> DataSourceSchemaCache {
        let mut cache = DataSourceSchemaCache::new();
        let mut tables = HashMap::new();
        tables.insert(
            "users".to_string(),
            EntitySchema {
                name: "users".to_string(),
                columns: vec![
                    FieldSchema {
                        name: "id".to_string(),
                        shape_type: "int".to_string(),
                        nullable: false,
                    },
                    FieldSchema {
                        name: "name".to_string(),
                        shape_type: "string".to_string(),
                        nullable: false,
                    },
                    FieldSchema {
                        name: "age".to_string(),
                        shape_type: "int".to_string(),
                        nullable: true,
                    },
                ],
            },
        );
        cache.upsert_source(SourceSchema {
            uri: "duckdb://analytics.db".to_string(),
            tables,
            cached_at: "2026-02-12T10:00:00Z".to_string(),
        });
        cache
    }

    #[test]
    fn test_roundtrip_serialization() {
        let cache = sample_cache();

        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(CACHE_FILENAME);
        cache.save(&path).unwrap();

        let loaded = DataSourceSchemaCache::load(&path).unwrap();
        assert_eq!(loaded.version, SCHEMA_CACHE_VERSION);
        assert_eq!(loaded.sources.len(), 1);

        let conn = loaded.get_source("duckdb://analytics.db").unwrap();
        assert_eq!(conn.tables.len(), 1);

        let users = conn.get_entity("users").unwrap();
        assert_eq!(users.columns.len(), 3);
        assert_eq!(users.columns[0].name, "id");
        assert_eq!(users.columns[0].shape_type, "int");
        assert!(!users.columns[0].nullable);
        assert_eq!(users.columns[2].name, "age");
        assert!(users.columns[2].nullable);
    }

    #[test]
    fn test_load_or_empty_missing_file() {
        let cache = DataSourceSchemaCache::load_or_empty(Path::new("/nonexistent/path.toml"));
        assert_eq!(cache.version, SCHEMA_CACHE_VERSION);
        assert!(cache.sources.is_empty());
    }

    #[test]
    fn test_source_helpers() {
        let cache = sample_cache();
        let conn = cache.get_source("duckdb://analytics.db").unwrap();

        let names = conn.entity_names();
        assert!(names.contains(&"users"));

        let users = conn.get_entity("users").unwrap();
        assert_eq!(users.field_names(), vec!["id", "name", "age"]);
        assert!(users.get_field("id").is_some());
        assert!(users.get_field("nonexistent").is_none());
    }

    #[test]
    fn test_upsert_source() {
        let mut cache = DataSourceSchemaCache::new();
        assert!(cache.sources.is_empty());

        cache.upsert_source(SourceSchema {
            uri: "duckdb://test.db".to_string(),
            tables: HashMap::new(),
            cached_at: "2026-01-01T00:00:00Z".to_string(),
        });
        assert_eq!(cache.sources.len(), 1);

        cache.upsert_source(SourceSchema {
            uri: "duckdb://test.db".to_string(),
            tables: HashMap::new(),
            cached_at: "2026-02-01T00:00:00Z".to_string(),
        });
        assert_eq!(cache.sources.len(), 1);
        assert_eq!(
            cache.get_source("duckdb://test.db").unwrap().cached_at,
            "2026-02-01T00:00:00Z"
        );
    }

    #[test]
    fn test_load_with_diagnostics_reports_stale_fingerprint() {
        let cache = sample_cache();
        let source = cache.get_source("duckdb://analytics.db").unwrap();

        let payload = source_to_payload(source);
        let artifact = LockedArtifact::new(
            SCHEMA_CACHE_NAMESPACE,
            source.uri.clone(),
            SCHEMA_CACHE_PRODUCER,
            ArtifactDeterminism::External {
                fingerprints: BTreeMap::from([(
                    format!("schema:{}", source.uri),
                    "sha256:deadbeef".to_string(),
                )]),
            },
            BTreeMap::from([
                ("uri".to_string(), source.uri.clone()),
                ("schema_hash".to_string(), "sha256:deadbeef".to_string()),
            ]),
            payload,
        )
        .unwrap();

        let mut lock = PackageLock::new();
        lock.upsert_artifact(artifact).unwrap();

        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(CACHE_FILENAME);
        lock.write(&path).unwrap();

        let (loaded, diagnostics) = DataSourceSchemaCache::load_with_diagnostics(&path).unwrap();
        assert!(loaded.sources.is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].message.contains("stale schema fingerprint"));
    }

    #[test]
    fn test_load_with_diagnostics_reports_invalid_payload() {
        let artifact = LockedArtifact::new(
            SCHEMA_CACHE_NAMESPACE,
            "broken://source",
            SCHEMA_CACHE_PRODUCER,
            ArtifactDeterminism::External {
                fingerprints: BTreeMap::from([(
                    "schema:broken://source".to_string(),
                    "sha256:abc".to_string(),
                )]),
            },
            BTreeMap::from([
                ("uri".to_string(), "broken://source".to_string()),
                ("schema_hash".to_string(), "sha256:abc".to_string()),
            ]),
            shape_wire::WireValue::String("bad".to_string()),
        )
        .unwrap();

        let mut lock = PackageLock::new();
        lock.upsert_artifact(artifact).unwrap();

        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(CACHE_FILENAME);
        lock.write(&path).unwrap();

        let (loaded, diagnostics) = DataSourceSchemaCache::load_with_diagnostics(&path).unwrap();
        assert!(loaded.sources.is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].message.contains("payload parse failed"));
    }

    #[test]
    fn test_load_cached_type_schemas_for_uri_prefixes_filters_sources() {
        let mut cache = DataSourceSchemaCache::new();

        let mut duck_tables = HashMap::new();
        duck_tables.insert(
            "users".to_string(),
            EntitySchema {
                name: "users".to_string(),
                columns: vec![FieldSchema {
                    name: "id".to_string(),
                    shape_type: "int".to_string(),
                    nullable: false,
                }],
            },
        );
        cache.upsert_source(SourceSchema {
            uri: "duckdb://analytics.db".to_string(),
            tables: duck_tables,
            cached_at: "2026-02-17T00:00:00Z".to_string(),
        });

        let mut pg_tables = HashMap::new();
        pg_tables.insert(
            "orders".to_string(),
            EntitySchema {
                name: "orders".to_string(),
                columns: vec![FieldSchema {
                    name: "id".to_string(),
                    shape_type: "int".to_string(),
                    nullable: false,
                }],
            },
        );
        cache.upsert_source(SourceSchema {
            uri: "postgres://localhost/app".to_string(),
            tables: pg_tables,
            cached_at: "2026-02-17T00:00:00Z".to_string(),
        });

        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(CACHE_FILENAME);
        cache.save(&path).unwrap();

        let duck_schemas =
            load_cached_type_schemas_for_uri_prefixes(&path, &["duckdb://"]).unwrap();
        assert_eq!(duck_schemas.len(), 1);
        assert_eq!(duck_schemas[0].name, "DbRow_users");

        let pg_schemas =
            load_cached_type_schemas_for_uri_prefixes(&path, &["postgres://", "postgresql://"])
                .unwrap();
        assert_eq!(pg_schemas.len(), 1);
        assert_eq!(pg_schemas[0].name, "DbRow_orders");
    }

    #[test]
    fn test_load_cached_source_for_uri_with_diagnostics() {
        let cache = sample_cache();
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join(CACHE_FILENAME);
        cache.save(&path).unwrap();

        let (source, diagnostics) =
            load_cached_source_for_uri_with_diagnostics(&path, "duckdb://analytics.db").unwrap();
        assert_eq!(source.uri, "duckdb://analytics.db");
        assert!(diagnostics.is_empty());
    }

    #[test]
    fn test_default_cache_path_ends_with_shape_lock() {
        let path = default_cache_path();
        assert!(path.ends_with(CACHE_FILENAME));
    }
}