quarb 0.73.0

Query engine for arbors (arboreal graphs - tree-spanned or tree-dominated graphs)
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
//! The ambient of a run, below everything that computes: the
//! refusal channel (a function that cannot answer says why, once,
//! and the run reports it), and the little the value layer needs of
//! the run's options, the collation and the sentence bonds, beside
//! the instant and the bonds the tool pins for the session. The pipeline opens an ambient for every run it
//! begins; the last one closed keeps its refusal until it is taken.
//! Thread-local, as a run is.
use crate::diagnostic::Diagnostic;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

/// Sentence segmentation with the bonds of a `.desm` file applied:
/// the abbreviations a sentence may end in without ending.
pub trait SentenceBonds {
    fn unicode_sentences<'a>(&self, text: &'a str) -> Vec<&'a str>;
    fn as_any(&self) -> &dyn std::any::Any;
}

#[cfg(feature = "desm")]
impl SentenceBonds for syndesmos::Syndesmos {
    fn unicode_sentences<'a>(&self, text: &'a str) -> Vec<&'a str> {
        syndesmos::Syndesmos::unicode_sentences(self, text)
    }
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

thread_local! {
    static NOW: Cell<Option<(i64, u32)>> = const { Cell::new(None) };
    static SENTENCE_BONDS: RefCell<Option<Rc<dyn SentenceBonds>>> = const { RefCell::new(None) };
    static AMBIENTS: RefCell<Vec<Ambient>> = const { RefCell::new(Vec::new()) };
    static FINISHED: RefCell<Ambient> = RefCell::new(Ambient::default());
    /// What sources had to tell when they were mounted, until a run
    /// ends and tells it ([`raise_at_mount`]).
    static AT_MOUNT: RefCell<Vec<Diagnostic>> = const { RefCell::new(Vec::new()) };
}

/// Raise a diagnostic outside a run: what a source has to tell when
/// it is mounted (a sheet of a workbook it could not read). No run
/// is open then to carry it, and one recorded on the last run's
/// record would be dropped by the next. A host takes these with
/// [`take_at_mount`] where it reports a run's diagnostics and tells
/// them in front of the run's own, once: the tools and the session
/// do; a parse or a run a host reports nothing of leaves them be.
pub fn raise_at_mount(d: Diagnostic) {
    AT_MOUNT.with(|m| m.borrow_mut().push(d));
}

/// Take what the mounts raised and no run has told yet.
pub fn take_at_mount() -> Vec<Diagnostic> {
    AT_MOUNT.with(|m| std::mem::take(&mut *m.borrow_mut()))
}

/// The invocation instant, pinned once by the tool layer (`qua
/// --now`, or the clock read once at startup) and read by adapters
/// that would otherwise consult the wall clock when resolving a
/// relative window (`since=30m`). A pinned run replays: evaluation
/// never reads a clock, and neither does a mount. `None` before the
/// tool sets it — a library embedder that never pins gets the old
/// wall-clock behavior.
pub fn set_invocation_instant(secs: i64, nanos: u32) {
    NOW.with(|c| c.set(Some((secs, nanos))));
}

pub fn invocation_instant() -> Option<(i64, u32)> {
    NOW.with(|c| c.get())
}

/// The pinned instant's seconds, or the clock's now when none is
/// pinned.
pub fn now_secs() -> i64 {
    match invocation_instant() {
        Some((secs, _)) => secs,
        None => std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0),
    }
}

/// The session's sentence bonds, set once by the tool layer.
/// The unit resolver of the run in progress: the mounted
/// document's own declared units beside the built-in table, so the
/// one quantity coercion (a typed column, a unital reading, a
/// comparison's criterion text) sees what `convert` sees.
type UnitScale = dyn Fn(&str) -> Option<(f64, String)>;

thread_local! {
    static UNIT_SCALE: RefCell<Option<*const UnitScale>> = const { RefCell::new(None) };
}

/// Run `f` with `scale` as the unit resolver. A scoped thread-local
/// in the manner of `scoped_tls`: the pointer is installed for the
/// duration of `f` and removed before `scale` can go out of scope,
/// so the lifetime erasure below is never observed.
pub(crate) fn with_unit_scale<R>(
    scale: &(dyn Fn(&str) -> Option<(f64, String)> + '_),
    f: impl FnOnce() -> R,
) -> R {
    struct Restore(Option<*const UnitScale>);
    impl Drop for Restore {
        fn drop(&mut self) {
            UNIT_SCALE.with(|s| *s.borrow_mut() = self.0);
        }
    }
    // SAFETY: the raw pointer outlives neither `scale` nor this
    // call — `Restore` puts the previous value back when `f`
    // returns or unwinds, and `unit_scale` only dereferences the
    // pointer while it is installed.
    let erased: *const UnitScale = unsafe {
        std::mem::transmute::<*const (dyn Fn(&str) -> Option<(f64, String)> + '_), *const UnitScale>(
            scale,
        )
    };
    let previous = UNIT_SCALE.with(|s| s.replace(Some(erased)));
    let _restore = Restore(previous);
    f()
}

/// The run's unit resolver's answer for `expr`: `None` when no run
/// installed one (the built-in table alone applies), `Some(answer)`
/// otherwise, the answer itself `None` for a unit nobody declared.
pub fn unit_scale(expr: &str) -> Option<Option<(f64, String)>> {
    let ptr = UNIT_SCALE.with(|s| *s.borrow());
    // SAFETY: a pointer is present only inside `with_unit_scale`,
    // whose resolver is alive for exactly that span.
    ptr.map(|p| unsafe { (*p)(expr) })
}

pub fn set_sentence_bonds(bonds: Option<Rc<dyn SentenceBonds>>) {
    SENTENCE_BONDS.with(|b| *b.borrow_mut() = bonds);
}

pub fn sentence_bonds() -> Option<Rc<dyn SentenceBonds>> {
    SENTENCE_BONDS.with(|b| b.borrow().clone())
}

/// What a run holds for the layers below the pipeline.
#[derive(Default)]
pub(crate) struct Ambient {
    pub(crate) collation: Option<String>,
    pub(crate) bonds: Option<Rc<dyn SentenceBonds>>,
    /// The first refusal, as the diagnostic it is (an error).
    pub(crate) refusal: Option<Diagnostic>,
    /// Whether withheld data was met this run: one warning at the
    /// end (spec: omitted data).
    pub(crate) omitted: bool,
    /// How deep the run stands in a predicate or another key
    /// evaluation: a withheld read there refuses.
    pub(crate) keying: u32,
    /// The warnings and infos raised, in raising order.
    pub(crate) diagnostics: Vec<Diagnostic>,
    /// Whether the value in front of a command is a cell of a
    /// structure `map` runs its body on: a numeric command leaves a
    /// cell of a kind it does not take as it is, where a lone value
    /// of that kind answers null (ruling 2026-11-10). It holds for
    /// one run of commands ([`cells_ground`]).
    pub(crate) cells: bool,
    /// The mark `map` leaves for the run of commands it starts next
    /// ([`mark_cells`]), taken by that run.
    pub(crate) cells_next: bool,
    /// The lone values a numeric command could not take, per
    /// command: one warning each when the run ends, counting them.
    pub(crate) untaken: Vec<Untaken>,
    /// The values that are no quantity a unit trait on a held value
    /// met, per spelling: one warning each when the run ends.
    pub(crate) unconverted: Vec<Unconverted>,
    /// The threads standing on a node with nothing projected that
    /// each stage reading a value met; warned once per stage.
    pub(crate) unprojected: Vec<Unprojected>,
    /// The lists a reducer or an association skipped beside the
    /// values it takes, per command: one warning each when the run
    /// ends, counting them.
    pub(crate) skipped_lists: Vec<SkippedLists>,
    /// The lists a command of the aggregation pipe was handed in one
    /// thread inside a parenthesized pipeline, per command: one
    /// warning each when the run ends, counting the times.
    pub(crate) lone_lists: Vec<LoneList>,
    /// The names already put to a declared surface this run, by the
    /// container asked and the name (a label apart from a property):
    /// such a source is asked once per name, and told of once. A
    /// source that declares nothing is not recorded, its containers
    /// being as many as its nodes.
    pub(crate) surfaced: std::collections::HashMap<u64, Vec<(String, bool)>>,
    /// How deep the run stands in a test of presence (`[::name]`,
    /// `[!::name]`): a name a surface lacks is the answer there, and
    /// a mistake only when nothing asked has it.
    pub(crate) presence: u32,
    /// What the run's tests of presence met, by name, in the order
    /// first tested: the verdict falls when the run ends.
    pub(crate) tested: Vec<Tested>,
    /// The field names the run read on records, in the order first
    /// read: a name no record read under it has is told of when the
    /// run ends ([`read_field`]).
    pub(crate) fields: Vec<FieldRead>,
}

/// One field name the run read on records.
#[derive(Default)]
pub(crate) struct FieldRead {
    name: String,
    /// Whether a record read under the name has the field.
    found: bool,
    /// The fields of the first records that lack it, in the order
    /// met: what the warning names.
    there: Vec<String>,
    /// How many records gave their fields to `there`.
    sampled: u8,
}

impl FieldRead {
    /// The records whose fields a warning draws on, and the names it
    /// holds at most: a stream of a million records lacking the field
    /// is a counter after these.
    const SAMPLED: u8 = 32;
    const HELD: usize = 64;

    fn warning(&self) -> Diagnostic {
        let name = &self.name;
        Diagnostic::warning(
            crate::diagnostic::UNKNOWN_FIELD,
            format!(":{name}"),
            format!(
                "no record read here has the field '{name}'; the fields there: {}",
                crate::graph::steps::surface_names(&self.there, name)
            ),
        )
    }
}

/// One name the run tested for presence.
#[derive(Default)]
pub(crate) struct Tested {
    name: String,
    /// Whether anything asked has the property: a declared surface
    /// that lists it, or a node that read it.
    found: bool,
    /// The declared containers asked, each once.
    asked: Vec<u64>,
    /// The warnings kept for the containers that lack it.
    lacking: Vec<Diagnostic>,
}

impl Ambient {
    fn tested(&mut self, name: &str) -> &mut Tested {
        match self.tested.iter().position(|t| t.name == name) {
            Some(i) => &mut self.tested[i],
            None => {
                self.tested.push(Tested {
                    name: name.to_string(),
                    ..Tested::default()
                });
                self.tested.last_mut().expect("just pushed")
            }
        }
    }
}

/// The lone values of a kind one numeric command does not take, or
/// the numbers outside what it takes.
pub(crate) struct Untaken {
    command: String,
    /// What the command takes: "a number", "a number or a quantity".
    takes: &'static str,
    /// Numbers of the right kind and outside the command's reach
    /// (`-1 | factorial`), counted apart from the values of another
    /// kind and said in their own words.
    outside: bool,
    count: usize,
    /// The first of them, kind and value, as the warning shows it.
    first: String,
}

impl Untaken {
    fn warning(&self) -> Diagnostic {
        let Untaken {
            command,
            takes,
            outside,
            count,
            first,
        } = self;
        Diagnostic::warning(
            crate::diagnostic::NOT_NUMERIC,
            "pipeline",
            if *outside && *count == 1 {
                format!(
                    "{command} takes {takes}: 1 value is outside that ({first}) and answers null"
                )
            } else if *outside {
                format!(
                    "{command} takes {takes}: {count} values are outside that (the first: \
                     {first}) and answer null"
                )
            } else if *count == 1 {
                format!(
                    "{command} takes {takes}: 1 value is of another kind ({first}) and answers \
                     null"
                )
            } else {
                format!(
                    "{command} takes {takes}: {count} values are of another kind (the first: \
                     {first}) and answer null"
                )
            },
        )
    }
}

/// The values that are no quantity one unit trait on a held value
/// met (`$.d<_h>` over a number or a text): the trait only converts
/// there (ruling 2026-11-21), so each answered null.
pub(crate) struct Unconverted {
    /// The operand as written, the warning's site.
    spelling: String,
    unit: String,
    /// The spellings that read such a value, for this operand; none
    /// for a count (`$steps`, `$iter`), which carries no unit at all.
    remedy: Option<String>,
    count: usize,
    /// The first of them, kind and value, as the warning shows it.
    first: String,
}

impl Unconverted {
    fn warning(&self) -> Diagnostic {
        let Unconverted {
            spelling,
            unit,
            remedy,
            count,
            first,
        } = self;
        let met = match count {
            1 => format!("1 value is of another kind ({first}) and answers null"),
            _ => format!("{count} values are of another kind (the first: {first}) and answer null"),
        };
        Diagnostic::warning(
            crate::diagnostic::NOT_A_QUANTITY,
            spelling.clone(),
            match remedy {
                Some(remedy) => {
                    format!("'<_{unit}>' on a held value takes a quantity: {met}; {remedy}")
                }
                None => format!(
                    "'<_{unit}>' on a held value takes a quantity, and a count carries no \
                     unit: {met}"
                ),
            },
        )
    }
}

/// The lists one reducer or association skipped beside the values
/// it takes (LISTS_SKIPPED, ruling 2026-11-22): `@(1; 2)`, `3` and `4`
/// after `@| sum` answer 7, the list being no number.
pub(crate) struct SkippedLists {
    command: String,
    /// What they were skipped beside: "the numbers", "the values it
    /// compares", "the pairs".
    beside: &'static str,
    /// Whether the command pairs reads thread by thread (an
    /// association): the lists are then reads, not values.
    paired: bool,
    count: usize,
    /// The first of them, as the warning shows it.
    first: String,
}

impl SkippedLists {
    fn warning(&self) -> Diagnostic {
        let SkippedLists {
            command,
            beside,
            paired,
            count,
            first,
        } = self;
        Diagnostic::warning(
            crate::diagnostic::LISTS_SKIPPED,
            "pipeline",
            match (*paired, *count) {
                (true, 1) => format!(
                    "{command}: 1 thread reads a list ({first}) and was skipped beside {beside}"
                ),
                (true, _) => format!(
                    "{command}: {count} threads read a list (the first: {first}) and were \
                     skipped beside {beside}"
                ),
                (false, 1) => format!(
                    "{command}: 1 value is a list ({first}) and was skipped beside {beside}; \
                     open it first: '| ... @| {command}' where threads hold lists, \
                     '| flatten | {command}' inside one list"
                ),
                (false, _) => format!(
                    "{command}: {count} values are lists (the first: {first}) and were skipped \
                     beside {beside}; open them first: '| ... @| {command}' where threads hold \
                     them, '| flatten | {command}' inside one list"
                ),
            },
        )
    }
}

/// Lists the command `command` skipped beside the values it takes:
/// the answer is that of the rest, and the run warns once for the
/// command when it ends, counting them and showing the first.
pub(crate) fn record_skipped_lists(
    command: &str,
    beside: &'static str,
    paired: bool,
    count: usize,
    first: impl FnOnce() -> String,
) {
    with_ambient(
        |a| match a.skipped_lists.iter_mut().find(|s| s.command == command) {
            Some(s) => s.count += count,
            None => a.skipped_lists.push(SkippedLists {
                command: command.to_string(),
                beside,
                paired,
                count,
                first: first(),
            }),
        },
    );
}

/// A list one command of the aggregation pipe was handed in one
/// thread, inside a parenthesized pipeline (LONE_LIST, ruling
/// 2026-11-22): `(v @| count)` over `@(12; 30; 8)` is 1, the thread
/// counted, where until 0.73 the list was opened and the answer was
/// 3.
pub(crate) struct LoneList {
    /// The command as `--expand` prints it, its pipe before it:
    /// `@| count`, `@| [1..2]`.
    command: String,
    /// Whether it selects threads (a bracket) or is a command.
    selects: bool,
    /// How many times it was handed such a thread.
    count: usize,
    /// The first list's length and its opening, as the warning
    /// shows them.
    items: usize,
    first: String,
}

impl LoneList {
    fn warning(&self) -> Diagnostic {
        let LoneList {
            command,
            selects,
            count,
            items,
            first,
        } = self;
        let shown = match *items > LONE_LIST_SHOWN {
            true => format!("{first}, {items} items"),
            false => first.clone(),
        };
        let bare = command.strip_prefix("@| ").unwrap_or(command);
        let met = match *count {
            1 => format!("one thread holding a list ({shown})"),
            _ => format!("one thread holding a list {count} times (the first: {shown})"),
        };
        Diagnostic::warning(
            crate::diagnostic::LONE_LIST,
            "pipeline",
            match *selects {
                false => format!(
                    "'{command}' inside parentheses was handed {met} and read the thread, not \
                     the list's items; '| {bare}' reads the list, '| ... {command}' opens it \
                     into threads first"
                ),
                true => format!(
                    "'{command}' inside parentheses was handed {met} and selected among the \
                     threads, not the list's items; the bracket on the value reads the list \
                     ('v{bare}'), '| ... {command}' opens it into threads first"
                ),
            },
        )
    }
}

/// How many items of a lone list its warning shows.
const LONE_LIST_SHOWN: usize = 3;

/// The command `command` (as `--expand` prints it) was handed one
/// thread holding `list` inside a parenthesized pipeline: the run
/// warns once for the command when it ends, counting the times and
/// showing the first list's opening.
pub(crate) fn record_lone_list(
    command: impl FnOnce() -> String,
    selects: bool,
    list: &[crate::value::Value],
) {
    let command = command();
    with_ambient(
        |a| match a.lone_lists.iter_mut().find(|l| l.command == command) {
            Some(l) => l.count += 1,
            None => {
                let mut first: Vec<String> = list
                    .iter()
                    .take(LONE_LIST_SHOWN)
                    .map(|v| {
                        // an item of any size is shown by its opening
                        let s = v.to_quarb();
                        match s.char_indices().nth(40) {
                            Some((cut, _)) => format!("{}…", &s[..cut]),
                            None => s,
                        }
                    })
                    .collect();
                if list.len() > LONE_LIST_SHOWN {
                    first.push("…".to_string());
                }
                a.lone_lists.push(LoneList {
                    command,
                    selects,
                    count: 1,
                    items: list.len(),
                    first: format!("@({})", first.join("; ")),
                });
            }
        },
    );
}

/// The threads one stage met standing on a node with nothing
/// projected, where it reads a value (NOT_A_VALUE, ruling
/// 2026-11-21: "warn"): `//title | cc` answers null for every title,
/// since a thread that never projected holds no topic.
pub(crate) struct Unprojected {
    /// What the warning is counted by: the command's name, or the
    /// stage's text where it is an expression.
    key: String,
    /// The stage as written: `cc`, `+ 2`, `> 10`, `sum`.
    stage: String,
    /// The pipe the stage stands after, `|` or `@|`.
    pipe: &'static str,
    /// What became of them.
    answer: Unread,
    count: usize,
    /// The first node's path.
    first: String,
}

/// What a stage answers a thread it could read no value from.
#[derive(Clone, Copy)]
pub(crate) enum Unread {
    /// The stage answers null.
    Null,
    /// A comparison answers false.
    False,
    /// A command that spreads its answer answers no thread.
    Nothing,
    /// A reduction reads it as null among the rest.
    ReadNull,
}

impl Unread {
    fn told(self, one: bool) -> &'static str {
        match (self, one) {
            (Unread::Null, true) => "answers null",
            (Unread::Null, false) => "answer null",
            (Unread::False, true) => "answers false",
            (Unread::False, false) => "answer false",
            (Unread::Nothing, true) => "answers nothing",
            (Unread::Nothing, false) => "answer nothing",
            (Unread::ReadNull, true) => "is read as null",
            (Unread::ReadNull, false) => "are read as null",
        }
    }
}

impl Unprojected {
    fn warning(&self) -> Diagnostic {
        let Unprojected {
            stage,
            pipe,
            answer,
            count,
            first,
            ..
        } = self;
        let met = match count {
            1 => format!(
                "1 thread stands on a node with nothing projected ({first}) and {}",
                answer.told(true)
            ),
            _ => format!(
                "{count} threads stand on a node with nothing projected (the first: {first}) \
                 and {}",
                answer.told(false)
            ),
        };
        Diagnostic::warning(
            crate::diagnostic::NOT_A_VALUE,
            "pipeline",
            format!("'{stage}' reads a value: {met}; project the value first: ':: {pipe} {stage}'"),
        )
    }
}

/// Open an ambient for a run; it closes when the guard drops, and
/// its refusal then waits in the finished slot.
pub(crate) fn enter(a: Ambient) -> AmbientGuard {
    AMBIENTS.with(|s| s.borrow_mut().push(a));
    AmbientGuard
}

pub(crate) struct AmbientGuard;

impl Drop for AmbientGuard {
    fn drop(&mut self) {
        if let Some(mut done) = AMBIENTS.with(|s| s.borrow_mut().pop()) {
            // the one warning for every omission of the run, unless the
            // run already refused for the same reason
            if done.omitted {
                omitted_warning(&mut done);
            }
            // one warning per numeric command for the lone values it
            // could not take: one line for the stream, not one per
            // thread
            let untaken: Vec<Diagnostic> = done.untaken.iter().map(Untaken::warning).collect();
            done.diagnostics.extend(untaken);
            // and one per unit trait on a held value for what it
            // did not convert
            let unconverted: Vec<Diagnostic> =
                done.unconverted.iter().map(Unconverted::warning).collect();
            done.diagnostics.extend(unconverted);
            let unprojected: Vec<Diagnostic> =
                done.unprojected.iter().map(Unprojected::warning).collect();
            done.diagnostics.extend(unprojected);
            let skipped: Vec<Diagnostic> = done
                .skipped_lists
                .iter()
                .map(SkippedLists::warning)
                .collect();
            done.diagnostics.extend(skipped);
            // the verdict of the tests of presence: a name nothing
            // asked has is told of, container by container; one that
            // something has was the question the test put
            for t in std::mem::take(&mut done.tested) {
                if t.found {
                    continue;
                }
                for d in t.lacking {
                    if !done
                        .diagnostics
                        .iter()
                        .any(|k| k.code == d.code && k.message == d.message)
                    {
                        done.diagnostics.push(d);
                    }
                }
            }
            // the lone lists: a run that refused has its error to
            // tell, and where the command itself refused the list
            // (`sum`) that error names both spellings
            if done.refusal.is_none() {
                let lone: Vec<Diagnostic> = done.lone_lists.iter().map(LoneList::warning).collect();
                done.diagnostics.extend(lone);
            }
            // the verdict of the field reads: a name no record read
            // under it has, one warning each, after the answers; a
            // run that refused has its error to tell
            let fields = std::mem::take(&mut done.fields);
            if done.refusal.is_none() {
                let unknown: Vec<Diagnostic> = fields
                    .iter()
                    .filter(|f| !f.found)
                    .map(FieldRead::warning)
                    .collect();
                done.diagnostics.extend(unknown);
            }
            FINISHED.with(|f| *f.borrow_mut() = done);
        }
    }
}

fn with_ambient<R>(f: impl FnOnce(&mut Ambient) -> R) -> R {
    AMBIENTS.with(|s| {
        let mut s = s.borrow_mut();
        match s.last_mut() {
            Some(top) => f(top),
            None => FINISHED.with(|fin| f(&mut fin.borrow_mut())),
        }
    })
}

/// Read the current ambient.
pub(crate) fn ambient<R>(f: impl FnOnce(&Ambient) -> R) -> R {
    with_ambient(|a| f(a))
}

/// Record a refusal from inside an evaluation — the engine's own
/// bounds and an adapter's failed fetch alike (a fetch that fails
/// mid-query must surface as an error, never as an empty answer):
/// the entry points turn the first one into [`QuarbError::refused`]
/// and treat the result as void. Outside an evaluation it is
/// cleared by the next one.
pub fn record_refusal(msg: String) {
    record_refusal_coded(crate::diagnostic::REFUSED, "query", msg);
}

/// [`record_refusal`] under the code of the rule that declines the
/// query (`QUANTIFIER_BOUND`, `FRONTIER_BOUND`) and the site that
/// raised it (`round 17`). The first refusal of a run is the one
/// reported.
pub fn record_refusal_coded(code: &str, site: impl Into<String>, msg: String) {
    with_ambient(|a| {
        if a.refusal.is_none() {
            a.refusal = Some(Diagnostic::error(code, site, msg));
        }
    });
}

/// Raise a warning or an info beside the answer (spec:
/// Diagnostics): kept in raising order and handed back with the
/// run's outcome. An error goes through [`record_refusal_coded`].
/// Push the omission warning once, unless the run refused as OMITTED.
fn omitted_warning(a: &mut Ambient) {
    let refused = a
        .refusal
        .as_ref()
        .is_some_and(|r| r.code == crate::diagnostic::OMITTED);
    let warned = a.diagnostics.iter().any(|d| {
        d.code == crate::diagnostic::OMITTED && d.severity == crate::diagnostic::Severity::Warning
    });
    if !refused && !warned {
        a.diagnostics.push(Diagnostic::warning(
            crate::diagnostic::OMITTED,
            "source",
            crate::diagnostic::OMITTED_TEXT,
        ));
    }
}
/// A withheld property was read (spec: omitted data): the run warns
/// once at its end; inside a key evaluation it refuses now.
pub fn record_omitted() {
    with_ambient(|a| a.omitted = true);
    if with_ambient(|a| a.keying) > 0 {
        refuse_omitted();
    }
}
/// The source dropped whole nodes for the caller (spec: omitted
/// data): the warning, once, after the run.
pub fn record_omitted_nodes() {
    with_ambient(|a| {
        a.omitted = true;
        omitted_warning(a);
    });
}
/// Run `f` as a key evaluation: a predicate, a sort key, a group or
/// top key, where a withheld read refuses rather than reads absent.
pub fn with_keying<R>(f: impl FnOnce() -> R) -> R {
    with_ambient(|a| a.keying += 1);
    let out = f();
    with_ambient(|a| a.keying = a.keying.saturating_sub(1));
    out
}
/// Run `f` as a test of presence: a predicate that is a property
/// alone asks whether it is there, so a declared surface that lacks
/// the name is not told of at once. The warning is kept, and raised
/// when the run ends only if nothing the test asked has the name
/// (ruling 2026-11-11): `//*[::name]` over a database tells the
/// tables that have the column from those that lack it in silence,
/// and `/employees/*[::manger_id]` is still a misspelling.
pub(crate) fn with_presence<R>(f: impl FnOnce() -> R) -> R {
    with_ambient(|a| a.presence += 1);
    let out = f();
    with_ambient(|a| a.presence = a.presence.saturating_sub(1));
    out
}
/// Whether the run stands in a test of presence.
pub(crate) fn in_presence() -> bool {
    with_ambient(|a| a.presence > 0)
}
/// Whether a test of presence already put `name` to the declared
/// surface of `container`.
pub(crate) fn presence_asked(container: u64, name: &str) -> bool {
    with_ambient(|a| {
        a.tested
            .iter()
            .any(|t| t.name == name && t.asked.contains(&container))
    })
}
/// A test of presence put `name` to the declared surface of
/// `container`: it has the property, or the warning that it lacks
/// it is kept for the verdict.
pub(crate) fn presence_ask(container: u64, name: &str, lacking: Option<Diagnostic>) {
    with_ambient(|a| {
        let t = a.tested(name);
        t.asked.push(container);
        match lacking {
            Some(d) => t.lacking.push(d),
            None => t.found = true,
        }
    });
}
/// A test of presence read `name` on a node: something has it.
pub(crate) fn presence_found(name: &str) {
    with_ambient(|a| {
        // the common case first: the name already found
        match a.tested.iter_mut().find(|t| t.name == name) {
            Some(t) => t.found = true,
            None => a.tested(name).found = true,
        }
    });
}
/// The field of `record` under `name`, for a read the query wrote
/// (`:name` on a record, `$.r:name`, `r["name"]`): the answer is the
/// field or nothing, as ever, and the read is tallied. A name that no
/// record read under it has is a warning when the run ends, naming
/// the fields the records have (UNKNOWN_FIELD, ruling 2026-11-11: a
/// record's missing field is null, and no longer in silence); where
/// one record has it the others' nulls are the data's.
pub(crate) fn read_field<'a>(
    record: &'a crate::value::Record,
    name: &str,
) -> Option<&'a crate::value::Value> {
    let read = record.get(name);
    field_read(record, name, read.is_some());
    read
}
/// Tally one read of `name` on `record` for the run's verdict
/// ([`read_field`]): `found` is whether the record has the field (a
/// label lookup finds a key as a value, `r[[k]]`).
pub(crate) fn field_read(record: &crate::value::Record, name: &str, found: bool) {
    with_ambient(|a| {
        let i = match a.fields.iter().position(|f| f.name == name) {
            Some(i) => i,
            None => {
                a.fields.push(FieldRead {
                    name: name.to_string(),
                    ..FieldRead::default()
                });
                a.fields.len() - 1
            }
        };
        let f = &mut a.fields[i];
        if found {
            f.found = true;
        } else if !f.found && f.sampled < FieldRead::SAMPLED {
            f.sampled += 1;
            for (k, _) in record {
                if f.there.len() >= FieldRead::HELD {
                    break;
                }
                if !f.there.iter().any(|t| t == k) {
                    f.there.push(k.clone());
                }
            }
        }
    });
}
/// The refusal a key over omitted values raises.
pub fn refuse_omitted() {
    record_refusal_coded(
        crate::diagnostic::OMITTED,
        "source",
        crate::diagnostic::OMITTED_TEXT.to_string(),
    );
}
pub fn record_diagnostic(d: Diagnostic) {
    with_ambient(|a| a.diagnostics.push(d));
}

/// Mark the run of commands started next as standing on a cell of a
/// structure (`map`'s body on a cell of a table, an item of a list,
/// a field of a record) or, with `false`, on a lone value (its body
/// on a scalar topic).
pub(crate) fn mark_cells(cells: bool) {
    with_ambient(|a| a.cells_next = cells);
}

/// A nested pipeline's own ground: its commands stand on a cell
/// where `map` marked the run ([`mark_cells`]), on lone values
/// otherwise, whatever the enclosing run stands on, until the guard
/// drops. So the mark reaches the commands of `map`'s body and no
/// further: a path piped inside the body
/// (`map((^/books/* | ::title | sqrt @| first))`) runs its commands
/// on threads, each a lone value.
pub(crate) struct CellsGround(bool);

pub(crate) fn cells_ground() -> CellsGround {
    CellsGround(with_ambient(|a| {
        let here = std::mem::take(&mut a.cells_next);
        std::mem::replace(&mut a.cells, here)
    }))
}

impl Drop for CellsGround {
    fn drop(&mut self) {
        let before = self.0;
        with_ambient(|a| a.cells = before);
    }
}

/// Whether the value in front of a command is a cell
/// ([`cells_ground`]).
pub(crate) fn in_cells() -> bool {
    with_ambient(|a| a.cells)
}

/// A lone value the numeric command `command` could not take: the
/// answer is null, and the run warns once for the command when it
/// ends, counting them and showing the first.
pub(crate) fn record_untaken(command: &str, takes: &'static str, first: impl FnOnce() -> String) {
    record_not_taken(command, takes, false, first);
}

/// A number the numeric command `command` takes the kind of and not
/// the value (a negative under `factorial`): the answer is null,
/// and the run warns once for the command, in the words of a value
/// outside what it takes, counting them and showing the first.
pub(crate) fn record_outside(command: &str, takes: &'static str, first: impl FnOnce() -> String) {
    record_not_taken(command, takes, true, first);
}

fn record_not_taken(
    command: &str,
    takes: &'static str,
    outside: bool,
    first: impl FnOnce() -> String,
) {
    with_ambient(|a| {
        match a
            .untaken
            .iter_mut()
            .find(|u| u.command == command && u.outside == outside)
        {
            Some(u) => u.count += 1,
            None => a.untaken.push(Untaken {
                command: command.to_string(),
                takes,
                outside,
                count: 1,
                first: first(),
            }),
        }
    });
}

/// A value that is no quantity under the unit trait on a held value
/// written `spelling` (NOT_A_QUANTITY): the answer is null, and the
/// run warns once for the spelling when it ends, counting them,
/// showing the first and naming the `remedy`.
pub(crate) fn record_unconverted(
    spelling: &str,
    unit: &str,
    remedy: impl FnOnce() -> Option<String>,
    first: impl FnOnce() -> String,
) {
    with_ambient(
        |a| match a.unconverted.iter_mut().find(|u| u.spelling == spelling) {
            Some(u) => u.count += 1,
            None => a.unconverted.push(Unconverted {
                spelling: spelling.to_string(),
                unit: unit.to_string(),
                remedy: remedy(),
                count: 1,
                first: first(),
            }),
        },
    );
}

/// A thread standing on a node with nothing projected that the stage
/// `stage` read a value from: it answered `answer`, and the run warns
/// once for the stage, counting such threads and naming the first.
pub(crate) fn record_unprojected(
    key: &str,
    pipe: &'static str,
    answer: Unread,
    stage: impl FnOnce() -> String,
    first: impl FnOnce() -> String,
) {
    with_ambient(|a| {
        match a
            .unprojected
            .iter_mut()
            .find(|u| u.key == key && u.pipe == pipe)
        {
            Some(u) => u.count += 1,
            None => a.unprojected.push(Unprojected {
                key: key.to_string(),
                stage: stage(),
                pipe,
                answer,
                count: 1,
                first: first(),
            }),
        }
    });
}

/// Whether `name` was already put to the declared surface of
/// `container` this run (`label` tells a label from a property). A
/// lookup and no more: every null read of a property passes here,
/// on any source, and one that declares nothing is never recorded.
pub(crate) fn surface_asked(container: u64, name: &str, label: bool) -> bool {
    with_ambient(|a| {
        a.surfaced
            .get(&container)
            .is_some_and(|seen| seen.iter().any(|(n, l)| n == name && *l == label))
    })
}

/// Record that `name` was put to the declared surface of
/// `container`: such a source is asked once per name, its containers
/// being few.
pub(crate) fn surface_ask(container: u64, name: &str, label: bool) {
    with_ambient(|a| {
        a.surfaced
            .entry(container)
            .or_default()
            .push((name.to_string(), label));
    });
}

/// Raise a warning beside the answer from an adapter, once per run
/// for one text: what a read could not answer the way it was asked,
/// where the run goes on (a refusal ends it: [`record_refusal`]).
pub fn record_warning(code: &str, site: &str, message: String) {
    record_warning_once(code, site, message);
}

/// Raise an info beside the answer from an adapter, once per run for
/// one text: what a read cost and how it could cost less, where the
/// answer is what was asked for (a warning is for an answer that is
/// not: [`record_warning`]).
pub fn record_info(code: &str, site: &str, message: String) {
    record_info_once(code, site, message);
}

/// Raise a warning beside the answer, once per run for one text.
pub(crate) fn record_warning_once(code: &str, site: &str, message: String) {
    with_ambient(|a| {
        if !a
            .diagnostics
            .iter()
            .any(|d| d.code == code && d.message == message)
        {
            a.diagnostics.push(Diagnostic::warning(code, site, message));
        }
    });
}

/// Raise an info beside the answer, once per run for one text: a
/// command that runs on every thread tells what it did once.
pub(crate) fn record_info_once(code: &str, site: &str, message: String) {
    with_ambient(|a| {
        if !a
            .diagnostics
            .iter()
            .any(|d| d.code == code && d.message == message)
        {
            a.diagnostics.push(Diagnostic::info(code, site, message));
        }
    });
}

/// Drop the diagnostics raised so far in the run that `drop` names:
/// a warning on dirt a later command cleaned (`| # | clean`) is no
/// longer a warning the answer should carry.
pub fn retract_diagnostics(drop: impl Fn(&Diagnostic) -> bool) {
    with_ambient(|a| a.diagnostics.retain(|d| !drop(d)));
}

/// Raise a debug diagnostic when the run asks for them
/// (`RunOptions::debug`); the message is built only then. Behind
/// the `debug-diagnostics` feature: a build without it raises none,
/// whatever the level.
#[cfg(feature = "debug-diagnostics")]
pub fn record_debug(code: &str, site: impl Into<String>, message: impl FnOnce() -> String) {
    if crate::pipeline::run_option(|o| o.debug) {
        record_diagnostic(Diagnostic::new(
            crate::diagnostic::Severity::Debug,
            code,
            site,
            message(),
        ));
    }
}

/// The debug diagnostics are not built in (the `debug-diagnostics`
/// feature is off): nothing is raised.
#[cfg(not(feature = "debug-diagnostics"))]
pub fn record_debug(_code: &str, _site: impl Into<String>, _message: impl FnOnce() -> String) {}

/// Take the warnings and infos the last run raised.
pub fn take_diagnostics() -> Vec<Diagnostic> {
    FINISHED.with(|f| std::mem::take(&mut f.borrow_mut().diagnostics))
}

/// Take the refusal recorded during the last run as the diagnostic
/// it is: code, site and message.
pub fn take_refusal_diagnostic() -> Option<Diagnostic> {
    FINISHED.with(|f| f.borrow_mut().refusal.take())
}

/// A fetch's outcome inside an evaluation: its value, or — when
/// the read failed — a refusal recorded under `what` and the
/// type's default, so a failed read never answers as an empty
/// one. For a read whose caller caches its answer, match the
/// result instead and return before caching.
pub fn fetched<T: Default, E: std::fmt::Display>(
    result: Result<T, E>,
    what: impl FnOnce() -> String,
) -> T {
    match result {
        Ok(v) => v,
        Err(e) => {
            record_refusal(format!("{}: {e}", what()));
            T::default()
        }
    }
}

/// [`fetched`] for a read answered as an `Option`: `None` after
/// the refusal is recorded.
pub fn fetched_opt<T, E: std::fmt::Display>(
    result: Result<T, E>,
    what: impl FnOnce() -> String,
) -> Option<T> {
    match result {
        Ok(v) => Some(v),
        Err(e) => {
            record_refusal(format!("{}: {e}", what()));
            None
        }
    }
}

/// Whether a refusal stands recorded in this evaluation: a walk
/// that meets one mid-expansion (a cycle under the acyclic promise,
/// a negative weight under a budget) ends with nothing rather than
/// walking on with the paths it has left.
pub(crate) fn refusal_pending() -> bool {
    with_ambient(|a| a.refusal.is_some())
}

/// Take the refusal recorded during the last [`eval`] /
/// [`eval_traced`], if any. The caller that observes it must treat
/// the accompanying result as void.
pub fn take_refusal() -> Option<String> {
    take_refusal_diagnostic().map(|d| d.message)
}