deps-core 6.0.0

Core abstractions for deps-lsp: caching, errors, and traits
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
//! Background git-ref probes: the typed ref a request URL may be built from, the evidence
//! probes gather, and the bounded stores diagnostics read it from.
//!
//! Diagnostics generation stays synchronous and network-free. A probe runs in a spawned task
//! (see [`TypedEcosystem::probes`](crate::TypedEcosystem::probes)), stores only
//! authoritative answers here, and reports whether anything changed through a [`ProbeReport`].
//! Specs 086 (branch evidence) and 087 (commit lineage and default-branch membership) share
//! this infrastructure; an inconclusive answer (rate limit, 5xx, timeout, overflow) is never
//! stored, so the pin it concerns stays silent.

use std::borrow::Cow;
use std::fmt;
use std::hash::Hash;
use std::num::NonZeroUsize;
use std::time::{Duration, Instant};

use super::{CommitSha, RequirementStatus, TagName, is_dot_segment, is_tag_shaped};
use crate::NormalizedName;
use crate::cache::BodyLimit;
use crate::swr_store::{Recency, SwrPolicy, SwrStore, Upserted};

/// Entries each evidence store keeps before evicting the least recently used one.
pub const EVIDENCE_STORE_CAPACITY: usize = 4096;

/// Longest ref, in bytes, [`ProbeRef::parse`] accepts.
const MAX_PROBE_REF_BYTES: usize = 255;

/// A tag-shaped ref text proven safe to place in the path of a token-bearing request.
///
/// The only constructor is [`Self::parse`], so no probe URL can be built from manifest text
/// that was not checked. It accepts a ref that [`is_tag_shaped`] and consists only of
/// `[A-Za-z0-9._+-]`, at most 255 bytes, with no leading `.` or `-`, no trailing `.` or
/// `.lock`, and no `..`. That rejects `/`, `?`, `#`, `%`, `@{`, whitespace and control
/// characters, every one of which could retarget the request. [`Self::as_path_segment`]
/// additionally percent-encodes the ref, regardless.
///
/// # Examples
///
/// ```
/// use deps_core::lsp_helpers::ProbeRef;
///
/// let git_ref = ProbeRef::parse("v40").unwrap();
/// assert_eq!(git_ref.as_str(), "v40");
/// assert_eq!(ProbeRef::parse("v1+build").unwrap().as_path_segment().to_string(), "v1%2Bbuild");
/// assert!(ProbeRef::parse("1/../../../../user").is_none());
/// assert!(ProbeRef::parse("1?x#y").is_none());
/// assert!(ProbeRef::parse("main").is_none());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProbeRef(String);

impl ProbeRef {
    /// The checked ref for `written`, or `None` when probing it could build an unsafe request.
    #[must_use]
    pub fn parse(written: &str) -> Option<Self> {
        let plain_char = |c: char| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '+' | '-');
        let accepted = is_tag_shaped(written)
            && written.len() <= MAX_PROBE_REF_BYTES
            && written.chars().all(plain_char)
            && !written.starts_with(['.', '-'])
            && !written.ends_with('.')
            && !written.ends_with(".lock")
            && !written.contains("..")
            && !is_dot_segment(written);
        accepted.then(|| Self(written.to_string()))
    }

    /// The ref text.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// The ref percent-encoded for use as one URL path segment.
    #[must_use]
    pub fn as_path_segment(&self) -> PercentEncoded<'_> {
        PercentEncoded(urlencoding::encode(&self.0))
    }
}

/// A string percent-encoded for a URL path segment, rendered through [`fmt::Display`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PercentEncoded<'a>(Cow<'a, str>);

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

/// What the forge answered about a branch named like a partial tag ref.
///
/// Only authoritative answers exist: `200` is [`Self::Exists`], `404` against a repository whose
/// tag list was readable is [`Self::Absent`]. Everything else is inconclusive and not stored.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BranchEvidence {
    /// A branch with that name exists, so the ref may be a documented moving-branch pin.
    Exists,
    /// No branch has that name.
    Absent,
}

/// Whether a probe pass changed what an ecosystem would show.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProbeEffect {
    /// Nothing observable changed.
    #[default]
    Unchanged,
    /// New evidence landed; diagnostics, hover and code actions must be regenerated.
    Republish,
    /// A probe changed how the manifest must be parsed; the document must be re-parsed.
    Reparse,
}

impl ProbeEffect {
    /// The stronger of two effects: [`Self::Reparse`] over [`Self::Republish`] over
    /// [`Self::Unchanged`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::lsp_helpers::ProbeEffect::{Republish, Reparse, Unchanged};
    ///
    /// assert_eq!(Unchanged.max(Republish), Republish);
    /// assert_eq!(Reparse.max(Republish), Reparse);
    /// ```
    #[must_use]
    pub const fn max(self, other: Self) -> Self {
        match (self, other) {
            (Self::Reparse, _) | (_, Self::Reparse) => Self::Reparse,
            (Self::Republish, _) | (_, Self::Republish) => Self::Republish,
            (Self::Unchanged, Self::Unchanged) => Self::Unchanged,
        }
    }
}

/// When, relative to the registry fetch of a document, a probe runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProbePhase {
    /// Before the fetch, for probes that decide what the fetch asks for (Swift's SCM-to-registry
    /// identity lookups).
    PreFetch,
    /// After the fetch, for probes that need the fetched tag index (GitHub Actions, GitLab CI).
    PostFetch,
}

/// The outcome of one [`TypedEcosystem::probes`](crate::TypedEcosystem::probes)
/// pass.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct ProbeReport {
    /// What changed.
    pub effect: ProbeEffect,
    /// Requests actually sent.
    pub issued: usize,
    /// Probes skipped because the rate budget denied a permit.
    pub skipped_over_budget: usize,
    /// Probes that ended without an authoritative answer.
    pub inconclusive: usize,
    /// Registry identity lookups (Swift's SCM-to-registry swap) that ended without an answer.
    ///
    /// Counted apart from [`Self::inconclusive`]: the CLI reports them in their own note and
    /// they say nothing about the rate budget of ref probes, so [`Self::unanswered`] excludes
    /// them.
    pub incomplete_identity_lookups: usize,
}

impl ProbeReport {
    /// The report of two passes taken together: the stronger effect and the summed counts.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::lsp_helpers::{ProbeEffect, ProbeReport};
    ///
    /// let a = ProbeReport { effect: ProbeEffect::Republish, issued: 2, ..ProbeReport::default() };
    /// let b = ProbeReport { inconclusive: 1, ..ProbeReport::default() };
    /// let merged = a.merge(b);
    /// assert_eq!(merged.effect, ProbeEffect::Republish);
    /// assert_eq!((merged.issued, merged.inconclusive), (2, 1));
    /// let lookups = ProbeReport { incomplete_identity_lookups: 2, ..ProbeReport::default() };
    /// assert_eq!(merged.merge(lookups).incomplete_identity_lookups, 2);
    /// assert_eq!(lookups.unanswered(), 0);
    /// ```
    #[must_use]
    pub const fn merge(self, other: Self) -> Self {
        Self {
            effect: self.effect.max(other.effect),
            issued: self.issued + other.issued,
            skipped_over_budget: self.skipped_over_budget + other.skipped_over_budget,
            inconclusive: self.inconclusive + other.inconclusive,
            incomplete_identity_lookups: self.incomplete_identity_lookups
                + other.incomplete_identity_lookups,
        }
    }

    /// Probes that produced no answer, which the CLI mentions in one stderr note.
    #[must_use]
    pub const fn unanswered(&self) -> usize {
        self.skipped_over_budget + self.inconclusive
    }
}

/// A bounded map that evicts its least recently used entry and marks entries stale after a TTL.
///
/// Stale-while-revalidate: [`Self::get`] keeps serving the last authoritative answer after its
/// TTL, because dropping it would flip a proven pin back to "outdated" (and let update-all
/// downgrade it) merely because the document sat idle. [`Self::get_fresh`] is what a prober
/// asks, so a stale entry is re-probed; a probe without an authoritative answer never replaces
/// it.
///
/// The store behind [`BranchEvidenceStore`] and [`CommitEvidenceStore`]. Reads and writes take
/// one short lock and never block on I/O.
#[derive(Debug)]
pub struct EvidenceStore<K, V>(SwrStore<K, V>);

#[derive(Debug, Clone, Copy)]
enum Freshness {
    Any,
    Fresh,
}

impl<K: Eq + Hash + Clone, V: Clone> EvidenceStore<K, V> {
    /// A store of at most `capacity` entries (at least one) whose entries expire after `ttl`
    /// when given.
    #[must_use]
    pub fn new(capacity: usize, ttl: Option<Duration>) -> Self {
        Self(SwrStore::new(SwrPolicy {
            capacity: NonZeroUsize::new(capacity).unwrap_or(NonZeroUsize::MIN),
            ttl,
            idle: None,
            recency: Recency::ReadsAndWrites,
        }))
    }

    /// The stored value for `key`, refreshing its recency, whether or not its TTL has passed.
    #[must_use]
    pub fn get(&self, key: &K) -> Option<V> {
        self.read_at(key, Instant::now(), Freshness::Any)
    }

    /// The stored value for `key` only while its TTL has not passed: `None` for an absent or
    /// stale entry, which is what tells a prober to ask again.
    #[must_use]
    pub fn get_fresh(&self, key: &K) -> Option<V> {
        self.read_at(key, Instant::now(), Freshness::Fresh)
    }

    fn read_at(&self, key: &K, now: Instant, freshness: Freshness) -> Option<V> {
        match freshness {
            Freshness::Any => self.0.get(key, now).map(|read| read.value),
            Freshness::Fresh => self.0.get_fresh(key, now),
        }
    }

    /// Stores `value` under `key`, evicting the least recently used entry when full, and makes
    /// it fresh. Returns whether the stored value differs from what was there (a new key or a
    /// changed value): re-confirming a stale answer is no change.
    pub fn insert(&self, key: K, value: V) -> bool
    where
        V: PartialEq,
    {
        self.insert_at(key, value, Instant::now())
    }

    fn insert_at(&self, key: K, value: V, now: Instant) -> bool
    where
        V: PartialEq,
    {
        match self.0.upsert(key, value.clone(), now) {
            Upserted::Inserted => true,
            Upserted::Replaced { previous } => previous != value,
        }
    }

    /// Number of stored entries, expired ones included until read.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Whether no entry is stored.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct BranchKey {
    repository: NormalizedName,
    git_ref: ProbeRef,
}

/// Branch evidence per (normalized repository, ref), stale after the TTL.
///
/// Keyed by [`NormalizedName`] so `Actions/Checkout` and `actions/checkout` probe once. A
/// branch can be created or deleted, so entries expire instead of being memoized forever.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use deps_core::lsp_helpers::{BranchEvidence, BranchEvidenceStore, NormalizeName, PackageNaming, ProbeRef};
/// use deps_core::PackageName;
///
/// struct Lowercase;
/// impl PackageNaming for Lowercase {
///     fn normalized_spelling(&self, name: &PackageName) -> String { name.as_str().to_lowercase() }
/// }
///
/// let store = BranchEvidenceStore::new(Duration::from_hours(1));
/// let repo = Lowercase.normalize_package_name(&PackageName::new("Actions/Checkout"));
/// let git_ref = ProbeRef::parse("v40").unwrap();
/// assert!(store.get(&repo, &git_ref).is_none());
/// assert!(store.record(repo.clone(), git_ref.clone(), BranchEvidence::Absent));
/// assert_eq!(store.get(&repo, &git_ref), Some(BranchEvidence::Absent));
/// assert!(!store.record(repo, git_ref, BranchEvidence::Absent));
/// ```
#[derive(Debug)]
pub struct BranchEvidenceStore(EvidenceStore<BranchKey, BranchEvidence>);

impl BranchEvidenceStore {
    /// An empty store whose entries expire after `ttl`.
    #[must_use]
    pub fn new(ttl: Duration) -> Self {
        Self(EvidenceStore::new(EVIDENCE_STORE_CAPACITY, Some(ttl)))
    }

    /// The last authoritative evidence for `git_ref` in `repository`, stale or not.
    #[must_use]
    pub fn get(&self, repository: &NormalizedName, git_ref: &ProbeRef) -> Option<BranchEvidence> {
        self.0.get(&Self::key(repository, git_ref))
    }

    /// The evidence for `git_ref` in `repository` while its TTL has not passed; `None` asks a
    /// prober to refresh it.
    #[must_use]
    pub fn get_fresh(
        &self,
        repository: &NormalizedName,
        git_ref: &ProbeRef,
    ) -> Option<BranchEvidence> {
        self.0.get_fresh(&Self::key(repository, git_ref))
    }

    fn key(repository: &NormalizedName, git_ref: &ProbeRef) -> BranchKey {
        BranchKey {
            repository: repository.clone(),
            git_ref: git_ref.clone(),
        }
    }

    /// Records an authoritative answer; returns whether it changed what was known (the cue to
    /// report [`ProbeEffect::Republish`]).
    pub fn record(
        &self,
        repository: NormalizedName,
        git_ref: ProbeRef,
        evidence: BranchEvidence,
    ) -> bool {
        self.0.insert(
            BranchKey {
                repository,
                git_ref,
            },
            evidence,
        )
    }
}

/// Whether a commit pin descends from the latest release commit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CommitLineage {
    /// The forge reports the pin ahead of the latest release; `ahead_by` is unknown on GitLab.
    AheadOfLatest {
        /// Commits the pin is ahead of the release, when the forge reports it.
        ahead_by: Option<u32>,
    },
    /// The pin is not ahead of the latest release (behind, diverged or identical).
    NotAhead,
}

/// Whether a commit pin is reachable from the repository's default branch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DefaultBranchMembership {
    /// The pin lies on the default branch's history, so it belongs to the project.
    Reachable,
    /// Reachability was not proven (ahead of HEAD, diverged, or anything else).
    NotProven,
}

/// Largest compare response a lineage or membership probe accepts.
///
/// With `per_page=1&page=2` a compare answer is about 13 KiB (`files` is empty); anything bigger
/// is not the endpoint's summary and is inconclusive.
pub const COMPARE_BODY_LIMIT: BodyLimit = BodyLimit::new(256 * 1024);

/// The `status` of a GitHub compare response, parsed to a closed set.
///
/// A status text outside it is not a variant: [`CompareSummary::parse`] returns `None`, so an
/// unknown status is inconclusive instead of being guessed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareStatus {
    /// The head is ahead of the base.
    Ahead,
    /// The head is behind the base.
    Behind,
    /// Base and head each have commits the other lacks.
    Diverged,
    /// Base and head are the same commit.
    Identical,
}

/// The two fields of a compare response a probe reads.
///
/// # Examples
///
/// ```
/// use deps_core::lsp_helpers::{CommitLineage, CompareStatus, CompareSummary};
///
/// let body = br#"{"status":"ahead","ahead_by":3,"behind_by":0,"files":[]}"#;
/// let summary = CompareSummary::parse(body).unwrap();
/// assert_eq!(summary.status, CompareStatus::Ahead);
/// assert_eq!(CommitLineage::from_compare(summary), CommitLineage::AheadOfLatest { ahead_by: Some(3) });
/// assert!(CompareSummary::parse(br#"{"status":"sideways"}"#).is_none());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompareSummary {
    /// How the head relates to the base.
    pub status: CompareStatus,
    /// Commits the head is ahead of the base.
    pub ahead_by: u32,
}

impl CompareSummary {
    /// Reads `body`, or `None` when it is not JSON with a known `status`.
    #[must_use]
    pub fn parse(body: &[u8]) -> Option<Self> {
        #[derive(serde::Deserialize)]
        struct Wire<'a> {
            status: &'a str,
            #[serde(default)]
            ahead_by: u32,
        }
        let wire: Wire<'_> = serde_json::from_slice(body).ok()?;
        let status = match wire.status {
            "ahead" => CompareStatus::Ahead,
            "behind" => CompareStatus::Behind,
            "diverged" => CompareStatus::Diverged,
            "identical" => CompareStatus::Identical,
            _ => return None,
        };
        Some(Self {
            status,
            ahead_by: wire.ahead_by,
        })
    }
}

impl CommitLineage {
    /// The lineage of a pin from `compare/{latest_release}...{pin}`: only `ahead` is ahead.
    #[must_use]
    pub const fn from_compare(summary: CompareSummary) -> Self {
        match summary.status {
            CompareStatus::Ahead => Self::AheadOfLatest {
                ahead_by: Some(summary.ahead_by),
            },
            CompareStatus::Behind | CompareStatus::Diverged | CompareStatus::Identical => {
                Self::NotAhead
            }
        }
    }

    /// The lineage of `pin` from GitLab's `merge_base(latest, pin)`: ahead when the merge base
    /// is the release commit and the pin is a different commit. GitLab reports no count.
    #[must_use]
    pub fn from_merge_base(merge_base: &CommitSha, latest: &CommitSha, pin: &CommitSha) -> Self {
        if merge_base == latest && pin != latest {
            Self::AheadOfLatest { ahead_by: None }
        } else {
            Self::NotAhead
        }
    }
}

impl DefaultBranchMembership {
    /// Membership of a pin from `compare/HEAD...{pin}`: the pin is on the default branch when
    /// it is behind or identical to HEAD. Ahead and diverged commits are not proven.
    #[must_use]
    pub const fn from_compare(summary: CompareSummary) -> Self {
        match summary.status {
            CompareStatus::Behind | CompareStatus::Identical => Self::Reachable,
            CompareStatus::Ahead | CompareStatus::Diverged => Self::NotProven,
        }
    }

    /// Membership of `pin` from GitLab's `merge_base(HEAD, pin)`: reachable when the merge base
    /// is the pin itself.
    #[must_use]
    pub fn from_merge_base(merge_base: &CommitSha, pin: &CommitSha) -> Self {
        if merge_base == pin {
            Self::Reachable
        } else {
            Self::NotProven
        }
    }
}

/// The two proofs that a commit pin newer than the latest release is not a downgrade target.
///
/// Lineage alone is not enough: a fork-network commit compares as ahead of a release commit.
/// Reachability from the default branch is what shows the commit belongs to the project, so
/// both are required.
///
/// # Examples
///
/// Only a [`CommitEvidenceStore`] builds one, from probe answers it was given, so evidence
/// (and the [`AheadProof`] it yields) cannot be written out by hand.
///
/// ```
/// use std::time::Duration;
/// use deps_core::PackageName;
/// use deps_core::lsp_helpers::{
///     CommitEvidenceStore, CommitLineage, CommitSha, DefaultBranchMembership, LowercaseNaming,
///     NormalizeName, TagName,
/// };
///
/// let store = CommitEvidenceStore::new(Duration::from_hours(1));
/// let repo = LowercaseNaming.normalize_package_name(&PackageName::new("o/r"));
/// let release = CommitSha::parse(&"a".repeat(40)).unwrap();
/// let pin = CommitSha::parse(&"b".repeat(40)).unwrap();
/// let fork = CommitSha::parse(&"c".repeat(40)).unwrap();
/// let latest = TagName::from_published("v4.2.2".to_string());
/// let ahead = CommitLineage::AheadOfLatest { ahead_by: Some(3) };
/// store.record_lineage(repo.clone(), release.clone(), pin.clone(), ahead);
/// store.record_membership(repo.clone(), pin.clone(), DefaultBranchMembership::Reachable);
/// store.record_lineage(repo.clone(), release.clone(), fork.clone(), ahead);
/// store.record_membership(repo.clone(), fork.clone(), DefaultBranchMembership::NotProven);
///
/// let proven = store.evidence(&repo, &release, &pin);
/// assert_eq!(proven.proven_ahead(&latest).unwrap().ahead_by(), Some(3));
/// assert!(store.evidence(&repo, &release, &fork).proven_ahead(&latest).is_none());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CommitEvidence {
    lineage: Option<CommitLineage>,
    membership: Option<DefaultBranchMembership>,
}

impl CommitEvidence {
    /// Lineage against the latest release commit, if probed.
    #[must_use]
    pub const fn lineage(&self) -> Option<CommitLineage> {
        self.lineage
    }

    /// Reachability from the default branch, if probed.
    #[must_use]
    pub const fn membership(&self) -> Option<DefaultBranchMembership> {
        self.membership
    }

    /// The proof that the pin is ahead of release `latest_tag` and on the default branch, or
    /// `None` unless both [`CommitLineage::AheadOfLatest`] and
    /// [`DefaultBranchMembership::Reachable`] hold.
    #[must_use]
    pub fn proven_ahead(&self, latest_tag: &TagName) -> Option<AheadProof> {
        match (self.lineage?, self.membership?) {
            (CommitLineage::AheadOfLatest { ahead_by }, DefaultBranchMembership::Reachable) => {
                Some(AheadProof {
                    ahead_by,
                    latest_tag: latest_tag.clone(),
                })
            }
            (CommitLineage::AheadOfLatest { .. }, DefaultBranchMembership::NotProven)
            | (
                CommitLineage::NotAhead,
                DefaultBranchMembership::Reachable | DefaultBranchMembership::NotProven,
            ) => None,
        }
    }
}

/// A pin proven newer than the latest release and part of the default branch.
///
/// It feeds the status override, the update-all exclusion (update-all only rewrites an
/// `Outdated` pin) and the hover line, see [`HoverProvenance::CommitAheadOfLatestRelease`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AheadProof {
    ahead_by: Option<u32>,
    latest_tag: TagName,
}

impl AheadProof {
    /// Commits the pin is ahead of the release, when the forge reports it.
    #[must_use]
    pub const fn ahead_by(&self) -> Option<u32> {
        self.ahead_by
    }

    /// The latest release tag the pin is ahead of.
    #[must_use]
    pub const fn latest_tag(&self) -> &TagName {
        &self.latest_tag
    }

    /// `status` with a pin the proof covers lifted from `Outdated` to `UpToDate`; any other
    /// status is left alone.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    /// use deps_core::PackageName;
    /// use deps_core::lsp_helpers::{
    ///     CommitEvidenceStore, CommitLineage, CommitSha, DefaultBranchMembership,
    ///     LowercaseNaming, NormalizeName, RequirementStatus, TagName,
    /// };
    ///
    /// let store = CommitEvidenceStore::new(Duration::from_hours(1));
    /// let repo = LowercaseNaming.normalize_package_name(&PackageName::new("o/r"));
    /// let release = CommitSha::parse(&"a".repeat(40)).unwrap();
    /// let pin = CommitSha::parse(&"b".repeat(40)).unwrap();
    /// store.record_lineage(
    ///     repo.clone(),
    ///     release.clone(),
    ///     pin.clone(),
    ///     CommitLineage::AheadOfLatest { ahead_by: None },
    /// );
    /// store.record_membership(repo.clone(), pin.clone(), DefaultBranchMembership::Reachable);
    /// let latest_tag = TagName::from_published("v4.2.2".to_string());
    /// let proof = store
    ///     .evidence(&repo, &release, &pin)
    ///     .proven_ahead(&latest_tag)
    ///     .unwrap();
    /// assert_eq!(proof.override_status(RequirementStatus::Outdated), RequirementStatus::UpToDate);
    /// assert_eq!(proof.override_status(RequirementStatus::Unresolved), RequirementStatus::Unresolved);
    /// ```
    #[must_use]
    pub const fn override_status(&self, status: RequirementStatus) -> RequirementStatus {
        match status {
            RequirementStatus::Outdated => RequirementStatus::UpToDate,
            RequirementStatus::UpToDate | RequirementStatus::Unresolved => status,
        }
    }

    /// The hover note for this proof.
    #[must_use]
    pub fn hover_provenance(&self) -> HoverProvenance {
        HoverProvenance::CommitAheadOfLatestRelease {
            ahead_by: self.ahead_by,
            latest_tag: self.latest_tag.clone(),
        }
    }
}

/// An extra, provenance-explaining line of a dependency's hover.
///
/// Returned by
/// [`DependencyFormatter::hover_provenance`](super::DependencyFormatter::hover_provenance). The
/// variants are exhaustive and carry typed fields, which the hover renderer sanitizes; an
/// ecosystem never supplies markdown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HoverProvenance {
    /// A SHA pin proven newer than the latest release (spec 087).
    CommitAheadOfLatestRelease {
        /// Commits ahead of the release, when known.
        ahead_by: Option<u32>,
        /// The latest release tag.
        latest_tag: TagName,
    },
    /// A source-control dependency served by a registry identity (spec 088).
    ResolvedViaRegistryIdentity {
        /// The registry identity the dependency was swapped to.
        identity: crate::PackageName,
        /// The source-control URL as written, credential-redacted.
        scm: crate::redact::RedactedUrl,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct LineageKey {
    repository: NormalizedName,
    latest: CommitSha,
    pin: CommitSha,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct MembershipKey {
    repository: NormalizedName,
    pin: CommitSha,
}

/// Commit lineage and default-branch membership per repository.
///
/// Lineage is keyed by both fixed SHAs and has no expiry: a new latest release changes the key,
/// so the proof is re-evaluated against it. Membership moves with the default branch's HEAD, so
/// it goes stale after the TTL and is re-proven then, but the last answer keeps being served
/// until a probe replaces it.
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use deps_core::lsp_helpers::{CommitEvidenceStore, CommitLineage, CommitSha, NormalizeName, PackageNaming};
/// use deps_core::PackageName;
///
/// struct Lowercase;
/// impl PackageNaming for Lowercase {
///     fn normalized_spelling(&self, name: &PackageName) -> String { name.as_str().to_lowercase() }
/// }
///
/// let store = CommitEvidenceStore::new(Duration::from_hours(1));
/// let repo = Lowercase.normalize_package_name(&PackageName::new("o/r"));
/// let release = CommitSha::parse(&"a".repeat(40)).unwrap();
/// let pin = CommitSha::parse(&"b".repeat(40)).unwrap();
/// store.record_lineage(repo.clone(), release.clone(), pin.clone(), CommitLineage::NotAhead);
/// assert_eq!(store.evidence(&repo, &release, &pin).lineage(), Some(CommitLineage::NotAhead));
/// let newer = CommitSha::parse(&"c".repeat(40)).unwrap();
/// assert_eq!(store.evidence(&repo, &newer, &pin).lineage(), None);
/// ```
#[derive(Debug)]
pub struct CommitEvidenceStore {
    lineage: EvidenceStore<LineageKey, CommitLineage>,
    membership: EvidenceStore<MembershipKey, DefaultBranchMembership>,
}

impl CommitEvidenceStore {
    /// An empty store whose membership entries expire after `membership_ttl`.
    #[must_use]
    pub fn new(membership_ttl: Duration) -> Self {
        Self {
            lineage: EvidenceStore::new(EVIDENCE_STORE_CAPACITY, None),
            membership: EvidenceStore::new(EVIDENCE_STORE_CAPACITY, Some(membership_ttl)),
        }
    }

    /// Everything stored about `pin` against the release commit `latest` in `repository`.
    #[must_use]
    pub fn evidence(
        &self,
        repository: &NormalizedName,
        latest: &CommitSha,
        pin: &CommitSha,
    ) -> CommitEvidence {
        CommitEvidence {
            lineage: self.lineage(repository, latest, pin),
            membership: self.membership(repository, pin),
        }
    }

    /// The stored lineage of `pin` against `latest`.
    #[must_use]
    pub fn lineage(
        &self,
        repository: &NormalizedName,
        latest: &CommitSha,
        pin: &CommitSha,
    ) -> Option<CommitLineage> {
        self.lineage.get(&LineageKey {
            repository: repository.clone(),
            latest: latest.clone(),
            pin: pin.clone(),
        })
    }

    /// The last authoritative membership of `pin`, stale or not.
    #[must_use]
    pub fn membership(
        &self,
        repository: &NormalizedName,
        pin: &CommitSha,
    ) -> Option<DefaultBranchMembership> {
        self.membership.get(&MembershipKey {
            repository: repository.clone(),
            pin: pin.clone(),
        })
    }

    /// The membership of `pin` while its TTL has not passed; `None` asks a prober to refresh
    /// it (HEAD moves, so membership is re-proven, but never dropped meanwhile).
    #[must_use]
    pub fn membership_fresh(
        &self,
        repository: &NormalizedName,
        pin: &CommitSha,
    ) -> Option<DefaultBranchMembership> {
        self.membership.get_fresh(&MembershipKey {
            repository: repository.clone(),
            pin: pin.clone(),
        })
    }

    /// Records an authoritative lineage; returns whether it changed what was known.
    pub fn record_lineage(
        &self,
        repository: NormalizedName,
        latest: CommitSha,
        pin: CommitSha,
        lineage: CommitLineage,
    ) -> bool {
        self.lineage.insert(
            LineageKey {
                repository,
                latest,
                pin,
            },
            lineage,
        )
    }

    /// Records an authoritative membership; returns whether it changed what was known.
    pub fn record_membership(
        &self,
        repository: NormalizedName,
        pin: CommitSha,
        membership: DefaultBranchMembership,
    ) -> bool {
        self.membership
            .insert(MembershipKey { repository, pin }, membership)
    }
}

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

    #[test]
    fn probe_ref_accepts_plain_tag_shapes() {
        for ok in [
            "v40",
            "v1",
            "5.x",
            "v3-node20",
            "1.2.3-rc.1",
            "v1+build",
            "4",
        ] {
            assert_eq!(ProbeRef::parse(ok).unwrap().as_str(), ok, "{ok}");
        }
    }

    #[test]
    fn probe_ref_rejects_traversal_and_url_metacharacters() {
        for bad in [
            "1/../../../../user",
            "1?x#y",
            "1#y",
            "a%2Fb",
            "1%2e%2e",
            "v1/x",
            "v1..2",
            "v1@{u}",
            "v1 2",
            "v1\n",
            "v1\u{7f}",
            "v1\\x",
            "v1.",
            "v1.lock",
            "-1",
            ".1",
            "..",
            ".",
            "",
            "main",
            "v",
            "ï½–1",
        ] {
            assert!(ProbeRef::parse(bad).is_none(), "{bad:?}");
        }
    }

    #[test]
    fn probe_ref_length_boundary_is_255_bytes() {
        let at_limit = format!("v{}", "1".repeat(254));
        assert_eq!(at_limit.len(), 255);
        assert!(ProbeRef::parse(&at_limit).is_some());
        assert!(ProbeRef::parse(&format!("{at_limit}1")).is_none());
    }

    #[test]
    fn probe_ref_path_segment_is_percent_encoded() {
        let git_ref = ProbeRef::parse("1.0+x").unwrap();
        assert_eq!(git_ref.as_path_segment().to_string(), "1.0%2Bx");
    }

    #[test]
    fn probe_effect_max_is_symmetric_and_ordered() {
        use ProbeEffect::{Reparse, Republish, Unchanged};
        for (a, b, want) in [
            (Unchanged, Unchanged, Unchanged),
            (Unchanged, Republish, Republish),
            (Republish, Reparse, Reparse),
            (Reparse, Unchanged, Reparse),
        ] {
            assert_eq!(a.max(b), want);
            assert_eq!(b.max(a), want);
        }
    }

    #[test]
    fn probe_report_unanswered_counts_budget_and_inconclusive() {
        let report = ProbeReport {
            skipped_over_budget: 2,
            inconclusive: 3,
            issued: 9,
            ..ProbeReport::default()
        };
        assert_eq!(report.unanswered(), 5);
    }

    fn store(capacity: usize, ttl: Option<Duration>) -> EvidenceStore<u32, u32> {
        EvidenceStore::new(capacity, ttl)
    }

    #[test]
    fn store_evicts_least_recently_used_at_capacity() {
        let s = store(2, None);
        s.insert(1, 10);
        s.insert(2, 20);
        assert_eq!(s.get(&1), Some(10));
        s.insert(3, 30);
        assert_eq!(s.get(&2), None);
        assert_eq!(s.get(&1), Some(10));
        assert_eq!(s.get(&3), Some(30));
        assert_eq!(s.len(), 2);
    }

    #[test]
    fn store_overwriting_a_key_at_capacity_evicts_nothing() {
        let s = store(2, None);
        s.insert(1, 10);
        s.insert(2, 20);
        assert!(s.insert(1, 11));
        assert!(!s.insert(1, 11));
        assert_eq!(s.get(&2), Some(20));
    }

    #[test]
    fn store_serves_stale_entries_but_does_not_report_them_fresh() {
        let s = store(4, Some(Duration::from_secs(60)));
        let start = Instant::now();
        s.insert_at(1, 10, start);
        let at = |secs| start + Duration::from_secs(secs);
        assert_eq!(s.read_at(&1, at(59), Freshness::Fresh), Some(10));
        assert_eq!(s.read_at(&1, at(60), Freshness::Fresh), None);
        assert_eq!(
            s.read_at(&1, at(60), Freshness::Any),
            Some(10),
            "stale-while-revalidate"
        );
        assert_eq!(s.len(), 1, "expiry removes nothing");
    }

    #[test]
    fn reconfirming_a_stale_answer_refreshes_it_without_reporting_a_change() {
        let s = store(4, Some(Duration::from_secs(60)));
        let start = Instant::now();
        s.insert_at(1, 10, start);
        assert!(!s.insert_at(1, 10, start + Duration::from_secs(61)));
        assert_eq!(
            s.read_at(&1, start + Duration::from_secs(62), Freshness::Fresh),
            Some(10)
        );
        assert!(
            s.insert_at(1, 11, start + Duration::from_secs(63)),
            "a changed answer is a change"
        );
    }

    #[test]
    fn store_without_ttl_never_expires() {
        let s = store(4, None);
        let start = Instant::now();
        s.insert_at(1, 10, start);
        assert_eq!(
            s.read_at(&1, start + Duration::from_hours(24 * 365), Freshness::Fresh),
            Some(10)
        );
    }

    #[test]
    fn store_is_bounded_by_the_requested_capacity() {
        let s = store(8, None);
        for i in 0..100 {
            s.insert(i, i);
        }
        assert_eq!(s.len(), 8);
    }

    fn sha(c: char) -> CommitSha {
        CommitSha::parse(&c.to_string().repeat(40)).unwrap()
    }

    const AHEAD: CommitLineage = CommitLineage::AheadOfLatest { ahead_by: Some(2) };

    #[test]
    fn proven_ahead_truth_table() {
        use CommitLineage::NotAhead;
        use DefaultBranchMembership::{NotProven, Reachable};
        let proof = |lineage, membership| {
            CommitEvidence {
                lineage,
                membership,
            }
            .proven_ahead(&"v1".into())
            .is_some()
        };
        assert!(proof(Some(AHEAD), Some(Reachable)));
        assert!(!proof(Some(AHEAD), Some(NotProven)));
        assert!(!proof(Some(AHEAD), None));
        assert!(!proof(Some(NotAhead), Some(Reachable)));
        assert!(!proof(Some(NotAhead), Some(NotProven)));
        assert!(!proof(None, Some(Reachable)));
        assert!(!proof(None, None));
    }

    #[test]
    fn ahead_proof_only_lifts_an_outdated_status() {
        let proof = CommitEvidence {
            lineage: Some(CommitLineage::AheadOfLatest { ahead_by: Some(1) }),
            membership: Some(DefaultBranchMembership::Reachable),
        }
        .proven_ahead(&"v1".into())
        .unwrap();
        assert_eq!(
            proof.override_status(RequirementStatus::Outdated),
            RequirementStatus::UpToDate
        );
        assert_eq!(
            proof.override_status(RequirementStatus::UpToDate),
            RequirementStatus::UpToDate
        );
        assert_eq!(
            proof.override_status(RequirementStatus::Unresolved),
            RequirementStatus::Unresolved
        );
    }

    fn repo(name: &str) -> NormalizedName {
        NormalizedName::for_test(name)
    }

    #[test]
    fn lineage_key_changes_with_the_latest_release_commit() {
        let s = CommitEvidenceStore::new(Duration::from_secs(60));
        s.record_lineage(repo("o/r"), sha('a'), sha('e'), AHEAD);
        assert_eq!(s.lineage(&repo("o/r"), &sha('a'), &sha('e')), Some(AHEAD));
        assert_eq!(s.lineage(&repo("o/r"), &sha('b'), &sha('e')), None);
    }

    #[test]
    fn lineage_has_no_ttl_but_membership_goes_stale_and_is_still_served() {
        let s = CommitEvidenceStore::new(Duration::from_secs(60));
        let start = Instant::now();
        let later = start + Duration::from_secs(61);
        let lineage_key = LineageKey {
            repository: repo("o/r"),
            latest: sha('a'),
            pin: sha('e'),
        };
        s.lineage.insert_at(lineage_key.clone(), AHEAD, start);
        assert_eq!(
            s.lineage.read_at(&lineage_key, later, Freshness::Fresh),
            Some(AHEAD)
        );
        let membership_key = MembershipKey {
            repository: repo("o/r"),
            pin: sha('e'),
        };
        s.membership.insert_at(
            membership_key.clone(),
            DefaultBranchMembership::Reachable,
            start,
        );
        assert_eq!(
            s.membership
                .read_at(&membership_key, later, Freshness::Fresh),
            None
        );
        assert_eq!(
            s.membership.read_at(&membership_key, later, Freshness::Any),
            Some(DefaultBranchMembership::Reachable),
            "the stale answer is still served"
        );
    }

    #[test]
    fn evidence_stores_share_one_entry_for_one_normalized_repository() {
        let s = BranchEvidenceStore::new(Duration::from_secs(60));
        let git_ref = ProbeRef::parse("v40").unwrap();
        assert!(s.record(repo("o/r"), git_ref.clone(), BranchEvidence::Absent));
        assert_eq!(s.get(&repo("o/r"), &git_ref), Some(BranchEvidence::Absent));
        assert_eq!(s.get(&repo("o/other"), &git_ref), None);
    }
}