teaql-runtime 5.0.0

TeaQL core, SQL, runtime, dialect, and macro crates for model-driven data access
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
#![allow(clippy::items_after_test_module)] // Save-contract tests intentionally sit near the API.

use std::collections::{BTreeMap, BTreeSet};
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;

use teaql_core::{Entity, MutationValues, Value};

use crate::{
    DataServiceError, GraphNode, GraphOperation, ObjectLocation, RuntimeError, UserContext,
};

tokio::task_local! {
    static GRAPH_FIX_TIME: teaql_core::time::Timestamp;
    static GRAPH_FIX_EVIDENCE: Arc<std::sync::Mutex<Vec<crate::FixEvidence>>>;
}

pub(crate) fn current_graph_fix_time() -> teaql_core::time::Timestamp {
    GRAPH_FIX_TIME
        .try_with(|value| *value)
        .unwrap_or_else(|_| teaql_core::time::Timestamp::now())
}

pub(crate) fn record_graph_fix_evidence(evidence: crate::FixEvidence) {
    let _ = GRAPH_FIX_EVIDENCE.try_with(|current| current.lock().unwrap().push(evidence));
}

// ---------------------------------------------------------------------------
// DynGraphSaver — type-erased graph save capability
// ---------------------------------------------------------------------------

/// Object-safe trait for saving a [`GraphNode`] tree to the database.
///
/// A concrete implementation is registered in [`UserContext`] during setup so
/// that [`Audited::save`] can persist entities without exposing the underlying
/// executor type to business code.
pub(crate) trait DynGraphSaver: Send + Sync {
    fn save_graph_dyn<'a>(
        &'a self,
        context: &'a UserContext,
        node: GraphNode,
    ) -> Pin<Box<dyn Future<Output = Result<GraphNode, RuntimeError>> + Send + 'a>>;

    fn save_ledger_dyn<'a>(
        &'a self,
        context: &'a UserContext,
        node: GraphNode,
        root: crate::EntityRuntimeState,
    ) -> Pin<Box<dyn Future<Output = Result<GraphNode, RuntimeError>> + Send + 'a>>;
}

/// Marker struct that implements [`DynGraphSaver`] for a specific executor type `E`.
///
/// `E` is the full executor type (e.g. `SqlDataServiceExecutor<SqliteDialect, …>`).
/// The struct itself is zero-sized; the actual executor is retrieved from
/// [`UserContext`] at call time.
pub(crate) struct GraphSaverFor<E> {
    _marker: PhantomData<fn() -> E>,
}

impl<E> GraphSaverFor<E> {
    pub(crate) fn new() -> Self {
        Self {
            _marker: PhantomData,
        }
    }
}

impl<E> DynGraphSaver for GraphSaverFor<E>
where
    E: teaql_data_service::QueryExecutor
        + teaql_data_service::MutationExecutor
        + teaql_data_service::TransactionExecutor
        + Send
        + Sync
        + 'static,
    for<'tx> <E as teaql_data_service::TransactionExecutor>::Tx<'tx>: Send + Sync,
{
    fn save_graph_dyn<'a>(
        &'a self,
        context: &'a UserContext,
        node: GraphNode,
    ) -> Pin<Box<dyn Future<Output = Result<GraphNode, RuntimeError>> + Send + 'a>> {
        Box::pin(async move {
            let entity = node.entity.clone();
            let executor = context
                .require_resource::<E>()
                .map_err(|e| RuntimeError::Graph(e.to_string()))?;
            let tx = teaql_data_service::TransactionExecutor::begin(executor)
                .await
                .map_err(|e| RuntimeError::Graph(e.to_string()))?;
            let result = {
                let eds = crate::EntityDataService::for_executor(context, entity, &tx);
                eds.save_graph_internal(node).await
            };
            match result {
                Ok(saved) => {
                    teaql_data_service::Transaction::commit(tx)
                        .await
                        .map_err(|e| RuntimeError::Graph(e.to_string()))?;
                    Ok(saved)
                }
                Err(error) => {
                    teaql_data_service::Transaction::rollback(tx)
                        .await
                        .map_err(|e| RuntimeError::Graph(e.to_string()))?;
                    Err(match error {
                        DataServiceError::Runtime(r) => r,
                        other => RuntimeError::Graph(other.to_string()),
                    })
                }
            }
        })
    }

    fn save_ledger_dyn<'a>(
        &'a self,
        context: &'a UserContext,
        mut node: GraphNode,
        root: crate::EntityRuntimeState,
    ) -> Pin<Box<dyn Future<Output = Result<GraphNode, RuntimeError>> + Send + 'a>> {
        Box::pin(async move {
            let entity = node.entity.clone();
            let executor = context
                .require_resource::<E>()
                .map_err(|e| RuntimeError::Graph(e.to_string()))?;
            let descriptor = context.require_entity(&entity)?;
            let id_prop = descriptor.id_property().ok_or_else(|| {
                RuntimeError::Graph(format!("entity {entity} has no id property"))
            })?;
            let current_id = node
                .values
                .get(&id_prop.name)
                .cloned()
                .unwrap_or(Value::I64(0));
            let root_key = crate::EntityKey::new(entity.clone(), current_id);
            reject_cancelled_new_root(&root, &root_key)?;
            let tx = teaql_data_service::TransactionExecutor::begin(executor)
                .await
                .map_err(|e| RuntimeError::Graph(e.to_string()))?;
            let result = async {
                let eds = crate::EntityDataService::for_executor(context, &entity, &tx);
                let locations = ledger_object_locations(&node);
                let generated_ids = eds
                    .execute_ledger_plan_internal(root.clone(), &locations)
                    .await?;
                if let Some(new_id) = generated_ids.get(&root_key) {
                    node.values.insert(id_prop.name.clone(), new_id.clone());
                }
                // The database is authoritative for IDs, versions, defaults,
                // triggers and conversions. Read on the transaction-owned
                // executor before commit; an ambient post-commit read can race
                // another writer or fail after the write is irreversible.
                let persisted_id = node.values.get(&id_prop.name).cloned().ok_or_else(|| {
                    DataServiceError::Runtime(RuntimeError::Graph(format!(
                        "saved {entity} missing identity field {}",
                        id_prop.name
                    )))
                })?;
                node.values = eds
                    .fetch_graph_current_row_internal(
                        &entity,
                        &id_prop.name,
                        &persisted_id,
                        Vec::new(),
                    )
                    .await?
                    .map(Into::into)
                    .ok_or_else(|| {
                        DataServiceError::Runtime(RuntimeError::Graph(format!(
                            "persisted {entity} record could not be read back"
                        )))
                    })?;
                Ok(())
            }
            .await;
            match result {
                Ok(()) => {
                    teaql_data_service::Transaction::commit(tx)
                        .await
                        .map_err(|e| RuntimeError::Graph(e.to_string()))?;
                }
                Err(error) => {
                    teaql_data_service::Transaction::rollback(tx)
                        .await
                        .map_err(|e| RuntimeError::Graph(e.to_string()))?;
                    return Err(match error {
                        DataServiceError::Runtime(r) => r,
                        other => RuntimeError::Graph(other.to_string()),
                    });
                }
            }
            root.clear_committed();
            Ok(node)
        })
    }
}

fn reject_cancelled_new_root(
    root: &crate::EntityRuntimeState,
    root_key: &crate::EntityKey,
) -> Result<(), RuntimeError> {
    if root.new_keys().contains(root_key) && root.deleted_keys().contains(root_key) {
        return Err(RuntimeError::Graph(format!(
            "cancelled new root {root_key:?}: create-then-delete has no persisted entity to return"
        )));
    }
    Ok(())
}

/// A scalar-only save can reuse an already loaded, immutable relation graph.
/// A changed relation key or a changed local FK would make that graph stale,
/// so the returned entity must expose those relations as NotLoaded instead.
fn can_preserve_loaded_relations(
    root: &crate::EntityRuntimeState,
    root_key: &crate::EntityKey,
    descriptor: &teaql_core::EntityDescriptor,
) -> bool {
    if !root.new_keys().is_empty() || !root.deleted_keys().is_empty() {
        return false;
    }
    let changes = root.current_change_set();
    changes.changes().iter().all(|(key, fields)| {
        key == root_key
            && fields.keys().all(|field| {
                !descriptor
                    .relations
                    .iter()
                    .any(|relation| relation.local_key == *field)
            })
    })
}

#[cfg(test)]
mod save_relation_state_tests {
    use super::can_preserve_loaded_relations;
    use crate::{EntityKey, EntityRuntimeState};
    use teaql_core::{EntityDescriptor, RelationDescriptor, Value};

    fn descriptor() -> EntityDescriptor {
        let mut descriptor = EntityDescriptor::new("Order");
        descriptor
            .relations
            .push(RelationDescriptor::new("customer", "Customer").local_key("customer_id"));
        descriptor
    }

    #[test]
    fn scalar_change_keeps_snapshot_but_relation_changes_invalidate_it() {
        let root = EntityRuntimeState::default();
        let order = EntityKey::new("Order", Value::I64(1));
        let child = EntityKey::new("OrderLine", Value::I64(2));
        root.set(order.clone(), "total_amount", Value::I64(100));
        assert!(can_preserve_loaded_relations(&root, &order, &descriptor()));
        root.set(child, "sku", Value::Text("CHANGED".into()));
        assert!(!can_preserve_loaded_relations(&root, &order, &descriptor()));

        let root = EntityRuntimeState::default();
        root.set(order.clone(), "customer_id", Value::I64(3));
        assert!(!can_preserve_loaded_relations(&root, &order, &descriptor()));
    }
}

#[cfg(test)]
mod transactional_ledger_readback_tests {
    use super::{DynGraphSaver, GraphSaverFor};
    use crate::{EntityKey, EntityRuntimeState, GraphNode, InMemoryMetadataStore, UserContext};
    use std::collections::BTreeMap;
    use std::sync::{Arc, Mutex};
    use teaql_core::{DataType, EntityDescriptor, PropertyDescriptor, Value};
    use teaql_data_service::{
        DataServiceCapabilities, DataServiceExecutor, ExecutionMetadata, MutationExecutor,
        MutationRequest, MutationResult, QueryExecutor, QueryRequest, QueryResult, Transaction,
        TransactionExecutor,
    };

    #[derive(Default)]
    struct State {
        row: BTreeMap<String, Value>,
        calls: Vec<&'static str>,
        fail_readback: bool,
    }

    #[derive(Clone)]
    struct Ambient(Arc<Mutex<State>>);

    struct Tx(Arc<Mutex<State>>);

    fn row_result(state: &State) -> QueryResult {
        QueryResult {
            rows: vec![teaql_core::CompactRow::from_map(state.row.clone())],
            metadata: ExecutionMetadata::unrecorded_query(1),
        }
    }

    impl DataServiceExecutor for Ambient {
        type Error = std::io::Error;

        fn capabilities(&self) -> DataServiceCapabilities {
            DataServiceCapabilities {
                query: true,
                mutation: true,
                transaction: true,
                ..Default::default()
            }
        }
    }

    impl DataServiceExecutor for Tx {
        type Error = std::io::Error;

        fn capabilities(&self) -> DataServiceCapabilities {
            Ambient(self.0.clone()).capabilities()
        }
    }

    impl QueryExecutor for Ambient {
        async fn query(&self, _request: QueryRequest) -> Result<QueryResult, Self::Error> {
            let mut state = self.0.lock().unwrap();
            state.calls.push("ambient-query");
            Ok(row_result(&state))
        }
    }

    impl QueryExecutor for Tx {
        async fn query(&self, _request: QueryRequest) -> Result<QueryResult, Self::Error> {
            let mut state = self.0.lock().unwrap();
            state.calls.push("transaction-query");
            if state.fail_readback && state.calls.contains(&"transaction-mutate") {
                return Ok(QueryResult {
                    rows: Vec::new(),
                    metadata: ExecutionMetadata::unrecorded_query(0),
                });
            }
            Ok(row_result(&state))
        }
    }

    impl MutationExecutor for Ambient {
        async fn mutate(&self, _request: MutationRequest) -> Result<MutationResult, Self::Error> {
            panic!("ledger writes must use the transaction executor")
        }
    }

    impl MutationExecutor for Tx {
        async fn mutate(&self, request: MutationRequest) -> Result<MutationResult, Self::Error> {
            let mut state = self.0.lock().unwrap();
            state.calls.push("transaction-mutate");
            match request {
                MutationRequest::Update(command) => {
                    assert_eq!(command.expected_version, Some(1));
                    for (field, value) in command.values {
                        state.row.insert(field, value);
                    }
                }
                MutationRequest::Delete(command) => {
                    assert_eq!(command.expected_version, Some(1));
                    state.row.insert("version".to_owned(), Value::I64(-2));
                }
                other => panic!("unexpected mutation: {other:?}"),
            }
            Ok(MutationResult {
                affected_rows: 1,
                generated_values: Default::default(),
                persisted_snapshot: None,
                metadata: ExecutionMetadata::unrecorded_query(0),
            })
        }
    }

    impl TransactionExecutor for Ambient {
        type Tx<'a> = Tx;

        async fn begin(&self) -> Result<Self::Tx<'_>, Self::Error> {
            self.0.lock().unwrap().calls.push("begin");
            Ok(Tx(self.0.clone()))
        }
    }

    impl Transaction for Tx {
        type Error = std::io::Error;

        async fn commit(self) -> Result<(), Self::Error> {
            let mut state = self.0.lock().unwrap();
            state.calls.push("commit");
            // Simulate a concurrent writer becoming visible just after commit.
            state.row.insert("version".to_owned(), Value::I64(3));
            state
                .row
                .insert("name".to_owned(), Value::Text("other writer".to_owned()));
            Ok(())
        }

        async fn rollback(self) -> Result<(), Self::Error> {
            let mut state = self.0.lock().unwrap();
            state.calls.push("rollback");
            state.row.insert("version".to_owned(), Value::I64(1));
            state
                .row
                .insert("name".to_owned(), Value::Text("before".to_owned()));
            Ok(())
        }
    }

    #[tokio::test]
    async fn ledger_save_returns_transaction_snapshot_before_concurrent_commit_race() {
        let state = Arc::new(Mutex::new(State {
            row: BTreeMap::from([
                ("id".to_owned(), Value::I64(1)),
                ("version".to_owned(), Value::I64(1)),
                ("name".to_owned(), Value::Text("before".to_owned())),
            ]),
            calls: Vec::new(),
            fail_readback: false,
        }));
        let descriptor = EntityDescriptor::new("Task")
            .property(PropertyDescriptor::new("id", DataType::I64).id())
            .property(PropertyDescriptor::new("version", DataType::I64).version())
            .property(PropertyDescriptor::new("name", DataType::Text));
        let context = UserContext::default()
            .with_metadata(InMemoryMetadataStore::new().with_entity(descriptor));
        let root = EntityRuntimeState::default();
        let key = EntityKey::new_static("Task", 1_i64);
        root.set_original_version(key.clone(), 1);
        root.set(key, "name", Value::Text("updated".to_owned()));
        let node = GraphNode::new("Task")
            .value("id", Value::I64(1))
            .value("version", Value::I64(1))
            .value("name", Value::Text("before".to_owned()));
        let mut context = context;
        context.insert_resource(Ambient(state.clone()));

        let saved = GraphSaverFor::<Ambient>::new()
            .save_ledger_dyn(&context, node, root)
            .await
            .unwrap();
        assert_eq!(saved.values.get("version"), Some(&Value::I64(2)));
        assert_eq!(
            saved.values.get("name"),
            Some(&Value::Text("updated".to_owned()))
        );
        let state = state.lock().unwrap();
        assert_eq!(state.row.get("version"), Some(&Value::I64(3)));
        assert_eq!(state.calls.last(), Some(&"commit"));
        assert!(!state.calls.contains(&"ambient-query"));
    }

    #[tokio::test]
    async fn failed_authoritative_readback_rolls_back_before_reporting_failure() {
        let state = Arc::new(Mutex::new(State {
            row: BTreeMap::from([
                ("id".to_owned(), Value::I64(1)),
                ("version".to_owned(), Value::I64(1)),
                ("name".to_owned(), Value::Text("before".to_owned())),
            ]),
            calls: Vec::new(),
            fail_readback: true,
        }));
        let descriptor = EntityDescriptor::new("Task")
            .property(PropertyDescriptor::new("id", DataType::I64).id())
            .property(PropertyDescriptor::new("version", DataType::I64).version())
            .property(PropertyDescriptor::new("name", DataType::Text));
        let mut context = UserContext::default()
            .with_metadata(InMemoryMetadataStore::new().with_entity(descriptor));
        context.insert_resource(Ambient(state.clone()));
        let root = EntityRuntimeState::default();
        let key = EntityKey::new_static("Task", 1_i64);
        root.set_original_version(key.clone(), 1);
        root.set(key, "name", Value::Text("updated".to_owned()));
        let node = GraphNode::new("Task")
            .value("id", Value::I64(1))
            .value("version", Value::I64(1))
            .value("name", Value::Text("before".to_owned()));

        let error = GraphSaverFor::<Ambient>::new()
            .save_ledger_dyn(&context, node, root.clone())
            .await
            .unwrap_err();
        assert!(error.to_string().contains("could not be read back"));
        let state = state.lock().unwrap();
        assert_eq!(state.calls.last(), Some(&"rollback"));
        assert!(!state.calls.contains(&"commit"));
        assert_eq!(state.row.get("version"), Some(&Value::I64(1)));
        assert_eq!(
            root.get_original_version(&EntityKey::new_static("Task", 1_i64)),
            Some(1)
        );
    }

    #[tokio::test]
    async fn soft_delete_returns_authoritative_tombstone_before_commit() {
        let state = Arc::new(Mutex::new(State {
            row: BTreeMap::from([
                ("id".to_owned(), Value::I64(1)),
                ("version".to_owned(), Value::I64(1)),
                ("name".to_owned(), Value::Text("before".to_owned())),
            ]),
            calls: Vec::new(),
            fail_readback: false,
        }));
        let descriptor = EntityDescriptor::new("Task")
            .property(PropertyDescriptor::new("id", DataType::I64).id())
            .property(PropertyDescriptor::new("version", DataType::I64).version())
            .property(PropertyDescriptor::new("name", DataType::Text));
        let mut context = UserContext::default()
            .with_metadata(InMemoryMetadataStore::new().with_entity(descriptor));
        context.insert_resource(Ambient(state.clone()));
        let root = EntityRuntimeState::default();
        let key = EntityKey::new_static("Task", 1_i64);
        root.set_original_version(key.clone(), 1);
        root.mark_as_delete(key);
        let node = GraphNode::new("Task")
            .value("id", Value::I64(1))
            .value("version", Value::I64(1))
            .value("name", Value::Text("before".to_owned()));

        let saved = GraphSaverFor::<Ambient>::new()
            .save_ledger_dyn(&context, node, root)
            .await
            .unwrap();
        assert_eq!(saved.values.get("version"), Some(&Value::I64(-2)));
        let state = state.lock().unwrap();
        assert_eq!(state.calls.last(), Some(&"commit"));
        assert!(!state.calls.contains(&"ambient-query"));
    }

    #[tokio::test]
    async fn cancelled_new_root_cannot_return_a_fictitious_persisted_entity() {
        let state = Arc::new(Mutex::new(State::default()));
        let descriptor = EntityDescriptor::new("Task")
            .property(PropertyDescriptor::new("id", DataType::I64).id())
            .property(PropertyDescriptor::new("version", DataType::I64).version())
            .property(PropertyDescriptor::new("name", DataType::Text));
        let mut context = UserContext::default()
            .with_metadata(InMemoryMetadataStore::new().with_entity(descriptor));
        context.insert_resource(Ambient(state.clone()));
        let root = EntityRuntimeState::default();
        let key = EntityKey::new_static("Task", 7_i64);
        root.mark_as_new(key.clone());
        root.mark_as_delete(key);
        let node = GraphNode::new("Task")
            .value("id", Value::I64(7))
            .value("version", Value::I64(0))
            .value("name", Value::Text("cancelled".to_owned()));

        let error = GraphSaverFor::<Ambient>::new()
            .save_ledger_dyn(&context, node, root)
            .await
            .expect_err("cancelled root has no persisted row to return");
        assert!(error.to_string().contains("cancelled new root"));
        let calls = &state.lock().unwrap().calls;
        assert!(!calls.contains(&"transaction-mutate"));
        assert!(!calls.contains(&"commit"));
    }
}

// ---------------------------------------------------------------------------
// Standalone graph-node extraction (no executor needed)
// ---------------------------------------------------------------------------

/// Convert a typed entity into a [`GraphNode`] tree.
///
/// This only requires metadata (entity descriptors) from the [`UserContext`],
/// **not** the database executor.  It is the standalone equivalent of
/// [`EntityDataService::graph_node_from_entity`].
pub fn graph_node_from_entity<T: Entity>(
    context: &UserContext,
    entity: T,
) -> Result<GraphNode, RuntimeError> {
    let descriptor = T::entity_descriptor();
    let loaded_fields = descriptor
        .properties
        .iter()
        .filter(|property| entity.is_field_loaded(&property.name))
        .map(|property| Value::Text(property.name.clone()))
        .collect::<Vec<_>>();
    let dirty_fields = entity.dirty_fields();
    let original_values = entity.original_values();
    let is_new = entity.is_new();
    let is_deleted = entity.is_marked_as_delete();
    let comment = entity.get_comment();
    let mut node = graph_node_from_values(context, &descriptor.name, entity.into_values())?;
    node.values
        .insert("_loaded_fields".to_owned(), Value::List(loaded_fields));
    node.dirty_fields = dirty_fields;
    node.original_values = original_values;
    if is_new {
        node.operation = GraphOperation::Create;
    }
    if is_deleted {
        node.operation = GraphOperation::Remove;
        node.relations.clear();
    }
    if let Some(c) = comment {
        node.set_comment(c);
    }
    Ok(node)
}

/// Recursively convert entity mutation values into a [`GraphNode`] tree.
///
/// Relations are resolved via the entity descriptors stored in `context`.
fn graph_node_from_values(
    context: &UserContext,
    entity: &str,
    values: MutationValues,
) -> Result<GraphNode, RuntimeError> {
    let descriptor = context.require_entity(entity)?;
    let mut node = GraphNode::new(entity);

    for (field, value) in values {
        if field == "_comment" {
            if let Value::Text(comment) = value {
                node.set_comment(comment);
            }
            continue;
        }
        if field == "_dirty_fields" {
            if let Value::List(fields) = value {
                let mut dirty = BTreeSet::new();
                for f in fields {
                    if let Value::Text(t) = f {
                        dirty.insert(t);
                    }
                }
                node.dirty_fields = Some(dirty);
            }
            continue;
        }
        if field == "_original_values" {
            if let Value::Object(orig) = value {
                node.original_values = Some(orig.into());
            }
            continue;
        }
        if field == "_is_new" {
            if matches!(value, Value::Bool(true)) {
                node.operation = GraphOperation::Create;
            }
            continue;
        }
        if field == "_is_deleted" {
            if matches!(value, Value::Bool(true)) {
                node.operation = GraphOperation::Remove;
            }
            continue;
        }
        let Some(relation) = descriptor.relation_by_name(&field) else {
            node.values.insert(field, value);
            continue;
        };

        match value {
            Value::Null => {
                node.relations.entry(field).or_default();
            }
            Value::Object(record) => {
                let child =
                    graph_node_from_values(context, &relation.target_entity, record.into())?;
                node.relations.entry(field).or_default().push(child);
            }
            Value::List(values) => {
                let children = node.relations.entry(field.clone()).or_default();
                for value in values {
                    let Value::Object(record) = value else {
                        return Err(RuntimeError::Graph(format!(
                            "relation {}.{} expects object children, got {:?}",
                            entity, field, value
                        )));
                    };
                    children.push(graph_node_from_values(
                        context,
                        &relation.target_entity,
                        record.into(),
                    )?);
                }
            }
            other => {
                return Err(RuntimeError::Graph(format!(
                    "relation {}.{} expects object/list/null, got {:?}",
                    entity, field, other
                )));
            }
        }
    }

    Ok(node)
}

fn merge_relation_mutations_into_root(
    root: &crate::EntityRuntimeState,
    node: &GraphNode,
) -> Result<(), RuntimeError> {
    for children in node.relations.values() {
        for child in children {
            let id = child.values.get("id").cloned().ok_or_else(|| {
                RuntimeError::Graph(format!(
                    "related mutation {} is missing its id",
                    child.entity
                ))
            })?;
            let key = crate::EntityKey::new(child.entity.clone(), id);

            match child.operation {
                GraphOperation::Create => {
                    root.mark_as_new(key.clone());
                    for (field, value) in &child.values {
                        root.set(key.clone(), field, value.clone());
                    }
                }
                GraphOperation::Upsert => {
                    if let Some(fields) = &child.dirty_fields {
                        for field in fields {
                            if let Some(value) = child.values.get(field) {
                                root.set(key.clone(), field, value.clone());
                            }
                        }
                    }
                }
                GraphOperation::Remove => root.mark_as_delete(key.clone()),
                GraphOperation::Reference => {}
            }

            if let Some(version) = child
                .original_values
                .as_ref()
                .and_then(|values| values.get("version"))
                .and_then(Value::try_i64)
            {
                root.set_original_version(key, version);
            }
            merge_relation_mutations_into_root(root, child)?;
        }
    }
    Ok(())
}

fn hydrate_ledger_relations(
    context: &UserContext,
    root: &crate::EntityRuntimeState,
    node: &mut GraphNode,
    visited: &mut BTreeSet<crate::EntityKey>,
) -> Result<(), RuntimeError> {
    let descriptor = context.require_entity(&node.entity)?;
    for relation in &descriptor.relations {
        let Some(local_value) = node.values.get(&relation.local_key).cloned() else {
            continue;
        };
        let existing = node.relations.entry(relation.name.clone()).or_default();
        let existing_keys = existing
            .iter()
            .filter_map(|child| {
                child
                    .values
                    .get("id")
                    .cloned()
                    .map(|id| crate::EntityKey::new(child.entity.clone(), id))
            })
            .collect::<BTreeSet<_>>();
        let mut discovered = Vec::new();
        for (key, changes) in root.current_change_set().changes() {
            if key.entity.as_ref() != relation.target_entity || existing_keys.contains(key) {
                continue;
            }
            let foreign_value = if relation.foreign_key == "id" {
                Some(&key.id)
            } else {
                changes.get(&relation.foreign_key)
            };
            if foreign_value != Some(&local_value) || !visited.insert(key.clone()) {
                continue;
            }
            let mut values: crate::EntityValues = changes.clone().into();
            values
                .entry("id".to_owned())
                .or_insert_with(|| key.id.clone());
            let operation = if root.deleted_keys().contains(key) {
                GraphOperation::Remove
            } else if root.new_keys().contains(key) || root.get_original_version(key).is_none() {
                GraphOperation::Create
            } else {
                GraphOperation::Upsert
            };
            let mut child = GraphNode::new(key.entity.to_string());
            child.values = values;
            child.operation = operation;
            hydrate_ledger_relations(context, root, &mut child, visited)?;
            discovered.push(child);
        }
        existing.extend(discovered);
    }
    Ok(())
}

fn preflight_graph(
    context: &UserContext,
    node: &mut GraphNode,
    location: &ObjectLocation,
    root: Option<&crate::EntityRuntimeState>,
) -> Result<(), RuntimeError> {
    if !matches!(
        node.operation,
        GraphOperation::Remove | GraphOperation::Reference
    ) {
        let before = node.values.clone();
        let status = match node.operation {
            GraphOperation::Create => crate::CheckObjectStatus::Create,
            GraphOperation::Upsert => crate::CheckObjectStatus::Update,
            GraphOperation::Remove | GraphOperation::Reference => unreachable!(),
        };
        crate::mark_entity_status(&mut node.values, status);
        let result = context.check_and_fix_values_at(&node.entity, &mut node.values, location);
        crate::clear_entity_status(&mut node.values);
        result?;

        if let Some(root) = root
            && let Some(id) = node.values.get("id").cloned()
        {
            let key = crate::EntityKey::new(node.entity.clone(), id);
            for (field, value) in &node.values {
                if before.get(field) != Some(value) {
                    root.set(key.clone(), field.clone(), value.clone());
                }
            }
        }
    }

    for (relation, children) in &mut node.relations {
        for (index, child) in children.iter_mut().enumerate() {
            let child_location = location.clone().member(relation).element(index);
            preflight_graph(context, child, &child_location, root)?;
        }
    }
    Ok(())
}

/// Retain the model-relative path discovered during graph preflight for the
/// sparse SQL-payload gate. Multiple references to one ledger entity may
/// exist; the first path in deterministic relation order is its diagnostic
/// location, and a root entity always retains the empty root path.
fn ledger_object_locations(node: &GraphNode) -> BTreeMap<crate::EntityKey, ObjectLocation> {
    fn visit(
        node: &GraphNode,
        location: &ObjectLocation,
        locations: &mut BTreeMap<crate::EntityKey, ObjectLocation>,
    ) {
        if let Some(id) = node.values.get("id").cloned() {
            let key = crate::EntityKey::new(node.entity.clone(), id);
            locations.entry(key).or_insert_with(|| location.clone());
        }
        for (relation, children) in &node.relations {
            for (index, child) in children.iter().enumerate() {
                let child_location = location.clone().member(relation).element(index);
                visit(child, &child_location, locations);
            }
        }
    }

    let mut locations = BTreeMap::new();
    visit(node, &ObjectLocation::root(), &mut locations);
    locations
}

#[cfg(test)]
mod ledger_location_tests {
    use super::ledger_object_locations;
    use crate::{EntityKey, GraphNode};
    use teaql_core::Value;

    #[test]
    fn nested_ledger_entity_keeps_model_and_json_error_paths() {
        let mut order = GraphNode::new("Order");
        order.values.insert("id".to_owned(), Value::U64(7));
        let mut line = GraphNode::new("OrderLine");
        line.values.insert("id".to_owned(), Value::U64(9));
        order.relations.insert("line_items".to_owned(), vec![line]);

        let locations = ledger_object_locations(&order);
        assert!(locations[&EntityKey::new("Order", 7_u64)].is_root());
        let child = &locations[&EntityKey::new("OrderLine", 9_u64)];
        assert_eq!(child.model_path(), "line_items[0]");
        assert_eq!(child.instance_path(), "/lineItems/0");
    }
}

// ---------------------------------------------------------------------------
// AuditedSaveExt — the `.save(&context)` method on `Audited<T>`
// ---------------------------------------------------------------------------

/// Extension trait that provides the `.save(&context)` method on [`Audited<T>`](teaql_core::Audited).
///
/// # Example
/// ```ignore
/// use teaql_runtime::AuditedSaveExt;
///
/// school.audit_as("创建学校").save(&context).await?;
/// ```
pub trait AuditedSaveExt {
    type Entity;

    fn save<'a>(
        self,
        context: &'a UserContext,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Entity, RuntimeError>> + Send + 'a>>;
}

impl<T> AuditedSaveExt for teaql_core::Audited<T>
where
    T: Entity + Send + 'static,
{
    type Entity = T;

    fn save<'a>(
        self,
        context: &'a UserContext,
    ) -> Pin<Box<dyn Future<Output = Result<Self::Entity, RuntimeError>> + Send + 'a>> {
        Box::pin(async move {
            let _entity_name = T::entity_descriptor().name;
            let entity = self.into_entity(); // applies comment onto the entity
            let mut node = graph_node_from_entity(context, entity)?;
            preflight_graph(context, &mut node, &ObjectLocation::root(), None)?;
            let saver = context
                .require_resource::<Arc<dyn DynGraphSaver>>()
                .map_err(|e| {
                    RuntimeError::Graph(format!(
                        "no DynGraphSaver registered — did you call register_executor()? ({})",
                        e
                    ))
                })?;
            let saved = saver.save_graph_dyn(context, node).await?;
            T::from_compact_row(teaql_core::CompactRow::from_map(saved.values.into()))
                .map_err(|e| RuntimeError::Graph(e.to_string()))
        })
    }
}

/// Persist an audited generated entity, including pending ledger changes that
/// may span multiple related entities sharing the same [`EntityRuntimeState`](crate::EntityRuntimeState).
///
/// Generated service crates use this as the implementation behind
/// `entity.audit_as("why").save(&context)`. The audited wrapper is required by the
/// function signature; no unaudited entity write entry point is exposed.
#[doc(hidden)]
pub async fn save_audited_ledger_entity<T>(
    audited: teaql_core::Audited<T>,
    context: &UserContext,
) -> Result<T, RuntimeError>
where
    T: crate::LedgerEntity + Send + 'static,
{
    let evidence = Arc::new(std::sync::Mutex::new(Vec::new()));
    let result = GRAPH_FIX_TIME
        .scope(
            teaql_core::time::Timestamp::now(),
            GRAPH_FIX_EVIDENCE.scope(
                evidence.clone(),
                save_audited_ledger_entity_inner(audited, context),
            ),
        )
        .await;
    context.replace_last_fix_evidence(evidence.lock().unwrap().clone());
    result
}

/// Persist an audited generated entity through an executor that is already
/// bound to an outer transaction.
///
/// This function never commits or rolls back the executor. The returned
/// mutation ledger must only be cleared after the owner commits the enclosing
/// transaction; retaining it on rollback keeps the mutation intent retryable.
#[doc(hidden)]
pub async fn save_audited_ledger_entity_with_executor<T, E>(
    audited: teaql_core::Audited<T>,
    context: &UserContext,
    executor: &E,
) -> Result<(T, Option<crate::EntityRuntimeState>), RuntimeError>
where
    T: crate::LedgerEntity + Send + 'static,
    E: teaql_data_service::QueryExecutor + teaql_data_service::MutationExecutor + Send + Sync,
{
    let evidence = Arc::new(std::sync::Mutex::new(Vec::new()));
    let result = GRAPH_FIX_TIME
        .scope(
            teaql_core::time::Timestamp::now(),
            GRAPH_FIX_EVIDENCE.scope(
                evidence.clone(),
                save_audited_ledger_entity_with_executor_inner(audited, context, executor),
            ),
        )
        .await;
    context.replace_last_fix_evidence(evidence.lock().unwrap().clone());
    result
}

async fn save_audited_ledger_entity_with_executor_inner<T, E>(
    audited: teaql_core::Audited<T>,
    context: &UserContext,
    executor: &E,
) -> Result<(T, Option<crate::EntityRuntimeState>), RuntimeError>
where
    T: crate::LedgerEntity + Send + 'static,
    E: teaql_data_service::QueryExecutor + teaql_data_service::MutationExecutor + Send + Sync,
{
    let entity = audited.into_entity();
    let root = entity.entity_runtime_state();
    if let Some(error) = root
        .as_ref()
        .and_then(|root| root.first_composition_error())
    {
        return Err(RuntimeError::Graph(format!(
            "generated entity graph attachment failed before save: {error}"
        )));
    }
    let mut node = graph_node_from_entity(context, entity)?;

    if let Some(root) = root {
        let root_id = node.values.get("id").cloned().unwrap_or(Value::I64(0));
        let root_key = crate::EntityKey::new(node.entity.clone(), root_id);
        reject_cancelled_new_root(&root, &root_key)?;
        if let Some(changes) = root.current_change_set().changes().get(&root_key) {
            for (field, value) in changes {
                node.values.insert(field.clone(), value.clone());
            }
        }
        let mut visited = BTreeSet::from([root_key.clone()]);
        hydrate_ledger_relations(context, &root, &mut node, &mut visited)?;
        preflight_graph(context, &mut node, &ObjectLocation::root(), Some(&root))?;
        merge_relation_mutations_into_root(&root, &node)?;
        let has_ledger_changes = !root.current_change_set().changes().is_empty()
            || !root.deleted_keys().is_empty()
            || !root.new_keys().is_empty();
        if has_ledger_changes {
            let entity_name = node.entity.clone();
            let descriptor = context.require_entity(&entity_name)?;
            let preserve_relations = can_preserve_loaded_relations(&root, &root_key, descriptor);
            let id_property = descriptor.id_property().ok_or_else(|| {
                RuntimeError::Graph(format!("entity {entity_name} has no id property"))
            })?;
            let data_service =
                crate::EntityDataService::for_executor(context, &entity_name, executor);
            let locations = ledger_object_locations(&node);
            let generated_ids = data_service
                .execute_ledger_plan_internal(root.clone(), &locations)
                .await
                .map_err(data_service_error_into_runtime)?;

            if let Some(new_id) = generated_ids.get(&root_key) {
                node.values.insert(id_property.name.clone(), new_id.clone());
            }
            // Even a soft delete returns the authoritative tombstone, not a
            // version inferred from the prior in-memory snapshot.
            let persisted_id = node.values.get(&id_property.name).cloned().ok_or_else(|| {
                RuntimeError::Graph(format!(
                    "saved {entity_name} missing identity field {}",
                    id_property.name
                ))
            })?;
            node.values = data_service
                .fetch_graph_current_row_internal(
                    &entity_name,
                    &id_property.name,
                    &persisted_id,
                    Vec::new(),
                )
                .await
                .map_err(data_service_error_into_runtime)?
                .map(Into::into)
                .ok_or_else(|| {
                    RuntimeError::Graph(format!(
                        "persisted {entity_name} record could not be read back"
                    ))
                })?;
            let row = teaql_core::CompactRow::from_map(node.values.into());
            let entity = if preserve_relations {
                T::from_compact_row_with_context(row, &root)
            } else {
                T::from_compact_row(row)
            }
            .map_err(|error| RuntimeError::Graph(error.to_string()))?;
            return Ok((entity, Some(root)));
        }
    }

    preflight_graph(context, &mut node, &ObjectLocation::root(), None)?;
    let entity_name = node.entity.clone();
    let saved = crate::EntityDataService::for_executor(context, entity_name, executor)
        .save_graph_internal(node)
        .await
        .map_err(data_service_error_into_runtime)?;
    let entity = T::from_compact_row(teaql_core::CompactRow::from_map(saved.values.into()))
        .map_err(|error| RuntimeError::Graph(error.to_string()))?;
    Ok((entity, None))
}

fn data_service_error_into_runtime<E: std::error::Error>(
    error: DataServiceError<E>,
) -> RuntimeError {
    match error {
        DataServiceError::Runtime(error) => error,
        other => RuntimeError::Graph(other.to_string()),
    }
}

async fn save_audited_ledger_entity_inner<T>(
    audited: teaql_core::Audited<T>,
    context: &UserContext,
) -> Result<T, RuntimeError>
where
    T: crate::LedgerEntity + Send + 'static,
{
    let _entity_name = T::entity_descriptor().name;
    let entity = audited.into_entity();
    let root = entity.entity_runtime_state();
    if let Some(error) = root
        .as_ref()
        .and_then(|root| root.first_composition_error())
    {
        return Err(RuntimeError::Graph(format!(
            "generated entity graph attachment failed before save: {error}"
        )));
    }
    let mut node = graph_node_from_entity(context, entity)?;
    let saver = context
        .require_resource::<Arc<dyn DynGraphSaver>>()
        .map_err(|e| {
            RuntimeError::Graph(format!(
                "no DynGraphSaver registered — did you call register_executor()? ({e})"
            ))
        })?;

    if let Some(root) = root {
        let root_id = node.values.get("id").cloned().unwrap_or(Value::I64(0));
        let root_key = crate::EntityKey::new(node.entity.clone(), root_id);
        reject_cancelled_new_root(&root, &root_key)?;
        if let Some(changes) = root.current_change_set().changes().get(&root_key) {
            for (field, value) in changes {
                node.values.insert(field.clone(), value.clone());
            }
        }
        let mut visited = BTreeSet::from([root_key.clone()]);
        hydrate_ledger_relations(context, &root, &mut node, &mut visited)?;
        preflight_graph(context, &mut node, &ObjectLocation::root(), Some(&root))?;
        merge_relation_mutations_into_root(&root, &node)?;
        let has_ledger_changes = !root.current_change_set().changes().is_empty()
            || !root.deleted_keys().is_empty()
            || !root.new_keys().is_empty();
        if has_ledger_changes {
            let descriptor = context.require_entity(&node.entity)?;
            let preserve_relations = can_preserve_loaded_relations(&root, &root_key, descriptor);
            let saved = saver.save_ledger_dyn(context, node, root.clone()).await?;
            let row = teaql_core::CompactRow::from_map(saved.values.into());
            return if preserve_relations {
                T::from_compact_row_with_context(row, &root)
            } else {
                T::from_compact_row(row)
            }
            .map_err(|e| RuntimeError::Graph(e.to_string()));
        }
    }

    preflight_graph(context, &mut node, &ObjectLocation::root(), None)?;
    let saved = saver.save_graph_dyn(context, node).await?;
    T::from_compact_row(teaql_core::CompactRow::from_map(saved.values.into()))
        .map_err(|e| RuntimeError::Graph(e.to_string()))
}