chio-listing 0.1.0

Chio generic listing and trust-activation contracts
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
//! Capability marketplace discovery: search and compare extensions on top of
//! the generic listing surface.
//!
//! This module is purely additive to the shipped listing types in
//! [`crate`]. It does not change any existing signatures.
//!
//! A tool server operator annotates a listing with a signed
//! [`ListingPricingHint`] (price-per-call, SLA, revocation rate, recent
//! receipt volume). Agents search a set of `GenericListingReport`s filtered
//! by scope prefix, price ceiling, provider, and freshness, then compare the
//! results in a normalized side-by-side view.
//!
//! Listings with non-`Active` status, stale freshness, or missing/expired
//! pricing hints are filtered out. The search is fail-closed by default: any
//! listing that cannot be verified or would violate the query bounds is
//! rejected rather than returned.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::crypto::PublicKey;
use crate::receipt::SignedExportEnvelope;
use crate::{
    aggregate_generic_listing_reports, normalize_namespace, GenericListingActorKind,
    GenericListingFreshnessState, GenericListingQuery, GenericListingReplicaFreshness,
    GenericListingReport, GenericListingSearchError, GenericListingStatus,
    GenericRegistryPublisher, MonetaryAmount, SignedGenericListing, MAX_GENERIC_LISTING_LIMIT,
};

/// Schema identifier for signed pricing hints.
pub const LISTING_PRICING_HINT_SCHEMA: &str = "chio.marketplace.listing-pricing-hint.v1";

/// Schema identifier for signed marketplace search responses.
pub const LISTING_SEARCH_SCHEMA: &str = "chio.marketplace.search.v1";

/// Schema identifier for marketplace comparison artifacts.
pub const LISTING_COMPARISON_SCHEMA: &str = "chio.marketplace.compare.v1";

/// Maximum number of listings a caller may request back from [`search`].
pub const MAX_MARKETPLACE_SEARCH_LIMIT: usize = MAX_GENERIC_LISTING_LIMIT;

/// Operator-signed pricing + SLA hint paired with a published listing.
///
/// The body is signed separately so that listing publication (subject to
/// registry ownership) and marketplace pricing (subject to the operator's
/// own key) can be decoupled.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ListingPricingHint {
    pub schema: String,
    /// Listing this hint applies to.
    pub listing_id: String,
    /// Namespace of the listing (must match the listing body).
    pub namespace: String,
    /// Provider / operator advertising the price (must match the listing
    /// publisher).
    pub provider_operator_id: String,
    /// Capability scope prefix covered by this hint (e.g.
    /// `"tools:search"` or `"tools:search:*"`). Queries filter against this.
    pub capability_scope: String,
    /// Fixed price charged per invocation under the advertised scope.
    pub price_per_call: MonetaryAmount,
    /// Advertised SLA for invocations under this hint.
    pub sla: ListingSla,
    /// Rolling revocation rate over recent invocations, in basis points.
    /// `0` means "no revocations in the window"; `10_000` means "100%".
    pub revocation_rate_bps: u32,
    /// Number of receipts the provider has produced in the recent window.
    pub recent_receipts_volume: u64,
    /// Unix seconds when the hint was issued.
    pub issued_at: u64,
    /// Unix seconds when the hint expires. Past expiry, the hint is stale
    /// and the listing falls out of the marketplace.
    pub expires_at: u64,
}

impl ListingPricingHint {
    /// Validate the hint's structural invariants. Does not verify the
    /// signature; callers should use [`SignedListingPricingHint::verify_signature`].
    pub fn validate(&self) -> Result<(), String> {
        if self.schema != LISTING_PRICING_HINT_SCHEMA {
            return Err(format!(
                "unsupported listing pricing hint schema: {}",
                self.schema
            ));
        }
        non_empty(&self.listing_id, "listing_id")?;
        non_empty(&self.namespace, "namespace")?;
        non_empty(&self.provider_operator_id, "provider_operator_id")?;
        non_empty(&self.capability_scope, "capability_scope")?;
        non_empty(&self.price_per_call.currency, "price_per_call.currency")?;
        if self.price_per_call.units == 0 {
            return Err("price_per_call.units must be greater than zero".to_string());
        }
        if self.revocation_rate_bps > 10_000 {
            return Err("revocation_rate_bps must be within [0, 10000]".to_string());
        }
        self.sla.validate()?;
        if self.expires_at <= self.issued_at {
            return Err("expires_at must be greater than issued_at".to_string());
        }
        Ok(())
    }

    /// Returns true when the hint is valid at the given unix timestamp.
    #[must_use]
    pub fn is_live_at(&self, now: u64) -> bool {
        now >= self.issued_at && now < self.expires_at
    }
}

pub type SignedListingPricingHint = SignedExportEnvelope<ListingPricingHint>;

/// Service-level advertisement paired with a pricing hint.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ListingSla {
    pub max_latency_ms: u64,
    /// Availability SLA expressed in basis points. `10_000` means 100.00%.
    pub availability_bps: u32,
    pub throughput_rps: u64,
}

impl ListingSla {
    pub fn validate(&self) -> Result<(), String> {
        if self.max_latency_ms == 0 {
            return Err("sla.max_latency_ms must be greater than zero".to_string());
        }
        if self.availability_bps == 0 || self.availability_bps > 10_000 {
            return Err("sla.availability_bps must be within (0, 10000]".to_string());
        }
        if self.throughput_rps == 0 {
            return Err("sla.throughput_rps must be greater than zero".to_string());
        }
        Ok(())
    }
}

/// Marketplace query for [`search`].
///
/// All fields are optional filters; an empty [`ListingQuery`] returns every
/// active listing across every report within [`Self::limit_or_default`].
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ListingQuery {
    /// Capability scope prefix to match against the hint's
    /// `capability_scope`. Matching is a literal prefix match after trim.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub capability_scope_prefix: Option<String>,
    /// Namespace filter. Same normalization as [`GenericListingQuery`].
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub namespace: Option<String>,
    /// Actor-kind filter. Defaults to `ToolServer` when unset.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub actor_kind: Option<GenericListingActorKind>,
    /// Only return listings whose price per call is less than or equal to
    /// this ceiling. Currency must match; listings with differing currency
    /// are filtered out.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_price_per_call: Option<MonetaryAmount>,
    /// Require a specific provider operator id (matches hint and publisher).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider_operator_id: Option<String>,
    /// Require fresh listings only. When set to `true` any stale/divergent
    /// listing is rejected. Defaults to `true`.
    #[serde(default = "default_require_fresh")]
    pub require_fresh: bool,
    /// Maximum number of results to return.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
}

fn default_require_fresh() -> bool {
    true
}

impl ListingQuery {
    #[must_use]
    pub fn limit_or_default(&self) -> usize {
        self.limit
            .unwrap_or(100)
            .clamp(1, MAX_MARKETPLACE_SEARCH_LIMIT)
    }

    /// Translate this marketplace query into a listing query for use with
    /// [`aggregate_generic_listing_reports`].
    #[must_use]
    pub fn to_listing_query(&self) -> GenericListingQuery {
        GenericListingQuery {
            namespace: self.namespace.clone(),
            actor_kind: Some(
                self.actor_kind
                    .unwrap_or(GenericListingActorKind::ToolServer),
            ),
            actor_id: None,
            status: Some(GenericListingStatus::Active),
            limit: Some(self.limit_or_default()),
        }
    }
}

/// A listing projected into the marketplace with its accompanying pricing
/// hint, publisher, and freshness metadata.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Listing {
    pub rank: u64,
    pub listing: SignedGenericListing,
    pub pricing: SignedListingPricingHint,
    pub publisher: GenericRegistryPublisher,
    pub freshness: GenericListingReplicaFreshness,
}

impl Listing {
    /// The listing identifier is the primary handle agents reference.
    #[must_use]
    pub fn listing_id(&self) -> &str {
        &self.listing.body.listing_id
    }

    /// Price advertised per call under this listing.
    #[must_use]
    pub fn price_per_call(&self) -> &MonetaryAmount {
        &self.pricing.body.price_per_call
    }

    /// Returns true only when the underlying listing is `Active` and the
    /// pricing hint is live at `now`.
    #[must_use]
    pub fn is_admissible_at(&self, now: u64) -> bool {
        matches!(self.listing.body.status, GenericListingStatus::Active)
            && self.pricing.body.is_live_at(now)
            && self.freshness.state == GenericListingFreshnessState::Fresh
    }
}

/// Signed marketplace search response.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ListingSearchResponse {
    pub schema: String,
    pub generated_at: u64,
    pub query: ListingQuery,
    pub result_count: u64,
    pub results: Vec<Listing>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub errors: Vec<GenericListingSearchError>,
}

/// Normalized comparison of a set of [`Listing`] entries.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ListingComparison {
    pub schema: String,
    pub generated_at: u64,
    pub entry_count: u64,
    pub rows: Vec<ListingComparisonRow>,
    /// `true` when every non-empty row shares the same price currency.
    pub currency_consistent: bool,
}

/// One normalized row in a [`ListingComparison`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ListingComparisonRow {
    pub listing_id: String,
    pub provider_operator_id: String,
    pub capability_scope: String,
    pub price_per_call: MonetaryAmount,
    /// Price normalized against the row with the minimum price per call
    /// within the same currency. Expressed in basis points where 10_000
    /// means "equal to the minimum".
    pub price_index_bps: u32,
    pub sla: ListingSla,
    pub revocation_rate_bps: u32,
    pub recent_receipts_volume: u64,
    pub freshness_state: GenericListingFreshnessState,
    pub status: GenericListingStatus,
}

/// Search a collection of generic listing reports, filtered by marketplace
/// criteria, and pair each surviving listing with its operator-signed
/// pricing hint.
///
/// Listings without a matching, signed, non-expired pricing hint are
/// dropped. Listings whose verified pricing hint does not satisfy the
/// `capability_scope_prefix`, `max_price_per_call`, or
/// `provider_operator_id` filters are also dropped.
///
/// This function does not panic. All hint signature failures and structural
/// validation errors are returned in
/// [`ListingSearchResponse::errors`].
#[must_use]
pub fn search(
    reports: &[GenericListingReport],
    pricing_hints: &[SignedListingPricingHint],
    query: &ListingQuery,
    now: u64,
) -> ListingSearchResponse {
    let listing_query = query.to_listing_query();
    let aggregated = aggregate_generic_listing_reports(reports, &listing_query, now);
    let mut errors = aggregated.errors;

    // Index pricing hints by listing_id for O(n) lookup. Store only the
    // most-recent verified hint per listing.
    let mut indexed_hints: BTreeMap<String, SignedListingPricingHint> = BTreeMap::new();
    for hint in pricing_hints {
        if let Err(error) = hint.body.validate() {
            errors.push(GenericListingSearchError {
                operator_id: hint.body.provider_operator_id.clone(),
                operator_name: None,
                registry_url: String::new(),
                error: format!("pricing hint `{}` invalid: {error}", hint.body.listing_id),
            });
            continue;
        }
        match hint.verify_signature() {
            Ok(true) => {}
            Ok(false) => {
                errors.push(GenericListingSearchError {
                    operator_id: hint.body.provider_operator_id.clone(),
                    operator_name: None,
                    registry_url: String::new(),
                    error: format!(
                        "pricing hint `{}` signature is invalid",
                        hint.body.listing_id
                    ),
                });
                continue;
            }
            Err(error) => {
                errors.push(GenericListingSearchError {
                    operator_id: hint.body.provider_operator_id.clone(),
                    operator_name: None,
                    registry_url: String::new(),
                    error: format!(
                        "pricing hint `{}` verification failed: {error}",
                        hint.body.listing_id
                    ),
                });
                continue;
            }
        }
        if !hint.body.is_live_at(now) {
            continue;
        }
        match indexed_hints.get(&hint.body.listing_id) {
            None => {
                indexed_hints.insert(hint.body.listing_id.clone(), hint.clone());
            }
            Some(existing) if existing.body.issued_at < hint.body.issued_at => {
                indexed_hints.insert(hint.body.listing_id.clone(), hint.clone());
            }
            Some(_) => {}
        }
    }

    let max_price = query.max_price_per_call.as_ref();
    let scope_prefix = query
        .capability_scope_prefix
        .as_deref()
        .map(str::trim)
        .filter(|prefix| !prefix.is_empty());
    let provider_filter = query
        .provider_operator_id
        .as_deref()
        .map(str::trim)
        .filter(|id| !id.is_empty());

    let mut results: Vec<Listing> = Vec::new();
    for aggregated_result in aggregated.results {
        if matches!(
            aggregated_result.listing.body.status,
            GenericListingStatus::Revoked
                | GenericListingStatus::Retired
                | GenericListingStatus::Suspended
                | GenericListingStatus::Superseded
        ) {
            continue;
        }
        if query.require_fresh
            && aggregated_result.freshness.state != GenericListingFreshnessState::Fresh
        {
            continue;
        }

        let Some(hint) = indexed_hints.get(&aggregated_result.listing.body.listing_id) else {
            continue;
        };

        if normalize_namespace(&hint.body.namespace)
            != normalize_namespace(&aggregated_result.listing.body.namespace)
        {
            errors.push(GenericListingSearchError {
                operator_id: hint.body.provider_operator_id.clone(),
                operator_name: None,
                registry_url: String::new(),
                error: format!("pricing hint `{}` namespace mismatch", hint.body.listing_id),
            });
            continue;
        }
        if hint.body.provider_operator_id != aggregated_result.publisher.operator_id {
            errors.push(GenericListingSearchError {
                operator_id: hint.body.provider_operator_id.clone(),
                operator_name: None,
                registry_url: aggregated_result.publisher.registry_url.clone(),
                error: format!(
                    "pricing hint `{}` provider does not match publisher",
                    hint.body.listing_id
                ),
            });
            continue;
        }
        if let Some(prefix) = scope_prefix {
            if !hint.body.capability_scope.starts_with(prefix) {
                continue;
            }
        }
        if let Some(max) = max_price {
            if max.currency != hint.body.price_per_call.currency {
                continue;
            }
            if hint.body.price_per_call.units > max.units {
                continue;
            }
        }
        if let Some(provider) = provider_filter {
            if hint.body.provider_operator_id != provider {
                continue;
            }
        }

        results.push(Listing {
            rank: 0,
            listing: aggregated_result.listing,
            pricing: hint.clone(),
            publisher: aggregated_result.publisher,
            freshness: aggregated_result.freshness,
        });
    }

    // Rank by: price ascending (same currency), then revocation rate
    // ascending, then receipts volume descending, then origin-publisher
    // preference, then listing id for stability.
    results.sort_by(|left, right| {
        let left_currency = &left.pricing.body.price_per_call.currency;
        let right_currency = &right.pricing.body.price_per_call.currency;
        left_currency
            .cmp(right_currency)
            .then(
                left.pricing
                    .body
                    .price_per_call
                    .units
                    .cmp(&right.pricing.body.price_per_call.units),
            )
            .then(
                left.pricing
                    .body
                    .revocation_rate_bps
                    .cmp(&right.pricing.body.revocation_rate_bps),
            )
            .then(
                right
                    .pricing
                    .body
                    .recent_receipts_volume
                    .cmp(&left.pricing.body.recent_receipts_volume),
            )
            .then(
                left.listing
                    .body
                    .listing_id
                    .cmp(&right.listing.body.listing_id),
            )
    });

    for (index, result) in results.iter_mut().enumerate() {
        result.rank = (index + 1) as u64;
    }
    results.truncate(query.limit_or_default());

    ListingSearchResponse {
        schema: LISTING_SEARCH_SCHEMA.to_string(),
        generated_at: now,
        query: query.clone(),
        result_count: results.len() as u64,
        results,
        errors,
    }
}

/// Produce a normalized side-by-side comparison of the given listings.
///
/// The comparison computes a `price_index_bps` column that expresses each
/// row's price relative to the cheapest listing **within the same
/// currency**. Rows with mismatched currency receive `price_index_bps =
/// 10_000` in their own sub-group and a `currency_consistent = false`
/// flag on the comparison.
#[must_use]
pub fn compare(listings: &[Listing]) -> ListingComparison {
    let generated_at = listings
        .iter()
        .map(|entry| entry.pricing.body.issued_at)
        .max()
        .unwrap_or_default();
    let mut currencies: BTreeMap<String, u64> = BTreeMap::new();
    for entry in listings {
        let currency = entry.pricing.body.price_per_call.currency.clone();
        let min = currencies.entry(currency).or_insert(u64::MAX);
        *min = (*min).min(entry.pricing.body.price_per_call.units);
    }

    let currency_consistent = currencies.len() <= 1;

    let rows = listings
        .iter()
        .map(|entry| {
            let currency = entry.pricing.body.price_per_call.currency.clone();
            let min = currencies.get(&currency).copied().unwrap_or(u64::MAX);
            let units = entry.pricing.body.price_per_call.units;
            let price_index_bps = if min == 0 || units == 0 {
                10_000
            } else {
                // Multiply in u128 to avoid overflow; clamp to u32::MAX.
                let numerator = (units as u128).saturating_mul(10_000_u128);
                let value = numerator / (min as u128);
                value.min(u32::MAX as u128) as u32
            };
            ListingComparisonRow {
                listing_id: entry.listing.body.listing_id.clone(),
                provider_operator_id: entry.pricing.body.provider_operator_id.clone(),
                capability_scope: entry.pricing.body.capability_scope.clone(),
                price_per_call: entry.pricing.body.price_per_call.clone(),
                price_index_bps,
                sla: entry.pricing.body.sla.clone(),
                revocation_rate_bps: entry.pricing.body.revocation_rate_bps,
                recent_receipts_volume: entry.pricing.body.recent_receipts_volume,
                freshness_state: entry.freshness.state,
                status: entry.listing.body.status,
            }
        })
        .collect::<Vec<_>>();

    ListingComparison {
        schema: LISTING_COMPARISON_SCHEMA.to_string(),
        generated_at,
        entry_count: rows.len() as u64,
        rows,
        currency_consistent,
    }
}

/// Resolve a listing + pricing-hint pair by id from previously aggregated
/// search results, returning `None` when the listing is absent or fails the
/// fail-closed admission check at `now`.
#[must_use]
pub fn resolve_admissible_listing<'a>(
    search_results: &'a [Listing],
    listing_id: &str,
    now: u64,
) -> Option<&'a Listing> {
    search_results
        .iter()
        .find(|listing| listing.listing_id() == listing_id && listing.is_admissible_at(now))
}

/// Returns the [`PublicKey`] that signed the resolved listing's pricing hint.
/// Used by the bid/ask protocol to bind capability tokens back to the
/// provider's advertised pricing authority.
#[must_use]
pub fn provider_signing_key(listing: &Listing) -> &PublicKey {
    &listing.pricing.signer_key
}

fn non_empty(value: &str, field: &str) -> Result<(), String> {
    if value.trim().is_empty() {
        Err(format!("{field} must not be empty"))
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crypto::Keypair;
    use crate::{
        GenericListingArtifact, GenericListingBoundary, GenericListingCompatibilityReference,
        GenericListingFreshnessWindow, GenericListingSearchPolicy, GenericListingSubject,
        GenericListingSummary, GenericNamespaceOwnership, GenericRegistryPublisherRole,
        GENERIC_LISTING_ARTIFACT_SCHEMA, GENERIC_LISTING_REPORT_SCHEMA,
    };

    fn sample_namespace(keypair: &Keypair) -> GenericNamespaceOwnership {
        GenericNamespaceOwnership {
            namespace: "https://registry.chio.example".to_string(),
            owner_id: "operator-a".to_string(),
            owner_name: Some("Operator A".to_string()),
            registry_url: "https://registry.chio.example".to_string(),
            signer_public_key: keypair.public_key(),
            registered_at: 1,
            transferred_from_owner_id: None,
        }
    }

    fn sample_listing(
        keypair: &Keypair,
        listing_id: &str,
        status: GenericListingStatus,
    ) -> SignedGenericListing {
        let body = GenericListingArtifact {
            schema: GENERIC_LISTING_ARTIFACT_SCHEMA.to_string(),
            listing_id: listing_id.to_string(),
            namespace: "https://registry.chio.example".to_string(),
            published_at: 10,
            expires_at: Some(1000),
            status,
            namespace_ownership: sample_namespace(keypair),
            subject: GenericListingSubject {
                actor_kind: GenericListingActorKind::ToolServer,
                actor_id: format!("server-{listing_id}"),
                display_name: None,
                metadata_url: None,
                resolution_url: None,
                homepage_url: None,
            },
            compatibility: GenericListingCompatibilityReference {
                source_schema: "chio.certify.check.v1".to_string(),
                source_artifact_id: format!("artifact-{listing_id}"),
                source_artifact_sha256: format!("sha-{listing_id}"),
            },
            boundary: GenericListingBoundary::default(),
        };
        SignedGenericListing::sign(body, keypair).expect("sign listing")
    }

    fn sample_publisher(operator_id: &str) -> GenericRegistryPublisher {
        GenericRegistryPublisher {
            role: GenericRegistryPublisherRole::Origin,
            operator_id: operator_id.to_string(),
            operator_name: Some(format!("Operator {operator_id}")),
            registry_url: format!("https://{operator_id}.chio.example"),
            upstream_registry_urls: Vec::new(),
        }
    }

    fn sample_report(
        keypair: &Keypair,
        operator_id: &str,
        generated_at: u64,
        listings: Vec<SignedGenericListing>,
    ) -> GenericListingReport {
        GenericListingReport {
            schema: GENERIC_LISTING_REPORT_SCHEMA.to_string(),
            generated_at,
            query: GenericListingQuery::default(),
            namespace: sample_namespace(keypair),
            publisher: sample_publisher(operator_id),
            freshness: GenericListingFreshnessWindow {
                max_age_secs: 300,
                valid_until: generated_at + 300,
            },
            search_policy: GenericListingSearchPolicy::default(),
            summary: GenericListingSummary {
                matching_listings: listings.len() as u64,
                returned_listings: listings.len() as u64,
                active_listings: listings.len() as u64,
                suspended_listings: 0,
                superseded_listings: 0,
                revoked_listings: 0,
                retired_listings: 0,
            },
            listings,
        }
    }

    fn sample_pricing_hint(
        operator_keypair: &Keypair,
        operator_id: &str,
        listing_id: &str,
        scope: &str,
        price_units: u64,
        issued_at: u64,
    ) -> SignedListingPricingHint {
        let body = ListingPricingHint {
            schema: LISTING_PRICING_HINT_SCHEMA.to_string(),
            listing_id: listing_id.to_string(),
            namespace: "https://registry.chio.example".to_string(),
            provider_operator_id: operator_id.to_string(),
            capability_scope: scope.to_string(),
            price_per_call: MonetaryAmount {
                units: price_units,
                currency: "USD".to_string(),
            },
            sla: ListingSla {
                max_latency_ms: 250,
                availability_bps: 9_990,
                throughput_rps: 50,
            },
            revocation_rate_bps: 25,
            recent_receipts_volume: 1_000,
            issued_at,
            expires_at: issued_at + 600,
        };
        SignedListingPricingHint::sign(body, operator_keypair).expect("sign hint")
    }

    #[test]
    fn search_filters_by_scope_prefix_and_price_ceiling() {
        let registry_keypair = Keypair::generate();
        let listing_cheap = sample_listing(
            &registry_keypair,
            "listing-cheap",
            GenericListingStatus::Active,
        );
        let listing_pricey = sample_listing(
            &registry_keypair,
            "listing-pricey",
            GenericListingStatus::Active,
        );
        let listing_other_scope = sample_listing(
            &registry_keypair,
            "listing-offscope",
            GenericListingStatus::Active,
        );
        let report = sample_report(
            &registry_keypair,
            "operator-a",
            100,
            vec![
                listing_cheap.clone(),
                listing_pricey.clone(),
                listing_other_scope.clone(),
            ],
        );

        let operator_keypair = Keypair::generate();
        let hints = vec![
            sample_pricing_hint(
                &operator_keypair,
                "operator-a",
                "listing-cheap",
                "tools:search",
                50,
                110,
            ),
            sample_pricing_hint(
                &operator_keypair,
                "operator-a",
                "listing-pricey",
                "tools:search:premium",
                500,
                110,
            ),
            sample_pricing_hint(
                &operator_keypair,
                "operator-a",
                "listing-offscope",
                "tools:write",
                10,
                110,
            ),
        ];

        let query = ListingQuery {
            capability_scope_prefix: Some("tools:search".to_string()),
            max_price_per_call: Some(MonetaryAmount {
                units: 100,
                currency: "USD".to_string(),
            }),
            ..ListingQuery::default()
        };
        let response = search(&[report], &hints, &query, 120);

        assert_eq!(response.result_count, 1);
        assert_eq!(response.results[0].listing_id(), "listing-cheap");
        assert_eq!(response.results[0].price_per_call().units, 50);
    }

    #[test]
    fn search_rejects_non_active_listings_and_missing_hints() {
        let registry_keypair = Keypair::generate();
        let revoked = sample_listing(
            &registry_keypair,
            "listing-revoked",
            GenericListingStatus::Revoked,
        );
        let active_no_hint = sample_listing(
            &registry_keypair,
            "listing-no-hint",
            GenericListingStatus::Active,
        );
        let report = sample_report(
            &registry_keypair,
            "operator-a",
            100,
            vec![revoked, active_no_hint],
        );
        let response = search(&[report], &[], &ListingQuery::default(), 120);
        assert_eq!(response.result_count, 0);
    }

    #[test]
    fn search_fails_closed_on_tampered_pricing_hint_signature() {
        let registry_keypair = Keypair::generate();
        let listing = sample_listing(&registry_keypair, "listing-1", GenericListingStatus::Active);
        let report = sample_report(&registry_keypair, "operator-a", 100, vec![listing]);

        let operator_keypair = Keypair::generate();
        let mut hint = sample_pricing_hint(
            &operator_keypair,
            "operator-a",
            "listing-1",
            "tools:search",
            10,
            110,
        );
        // Tamper: mutate body after signing.
        hint.body.price_per_call.units = 1;

        let response = search(&[report], &[hint], &ListingQuery::default(), 120);
        assert_eq!(response.result_count, 0);
        assert!(response
            .errors
            .iter()
            .any(|error| error.error.contains("signature is invalid")));
    }

    #[test]
    fn search_rejects_stale_pricing_hint() {
        let registry_keypair = Keypair::generate();
        let listing = sample_listing(&registry_keypair, "listing-1", GenericListingStatus::Active);
        let report = sample_report(&registry_keypair, "operator-a", 100, vec![listing]);

        let operator_keypair = Keypair::generate();
        // Hint expires at 710.
        let stale = sample_pricing_hint(
            &operator_keypair,
            "operator-a",
            "listing-1",
            "tools:search",
            10,
            110,
        );

        let response = search(&[report], &[stale], &ListingQuery::default(), 2_000);
        assert_eq!(response.result_count, 0);
    }

    #[test]
    fn compare_normalizes_prices_within_currency() {
        let registry_keypair = Keypair::generate();
        let listing_a =
            sample_listing(&registry_keypair, "listing-a", GenericListingStatus::Active);
        let listing_b =
            sample_listing(&registry_keypair, "listing-b", GenericListingStatus::Active);
        let report = sample_report(
            &registry_keypair,
            "operator-a",
            100,
            vec![listing_a, listing_b],
        );

        let operator_keypair = Keypair::generate();
        let hints = vec![
            sample_pricing_hint(
                &operator_keypair,
                "operator-a",
                "listing-a",
                "tools:search",
                100,
                110,
            ),
            sample_pricing_hint(
                &operator_keypair,
                "operator-a",
                "listing-b",
                "tools:search",
                200,
                110,
            ),
        ];
        let response = search(&[report], &hints, &ListingQuery::default(), 120);
        let comparison = compare(&response.results);
        assert_eq!(comparison.entry_count, 2);
        assert!(comparison.currency_consistent);
        // listing-a is cheapest; its price_index should be 10_000 (1.0x).
        let row_a = comparison
            .rows
            .iter()
            .find(|row| row.listing_id == "listing-a")
            .expect("row a present");
        let row_b = comparison
            .rows
            .iter()
            .find(|row| row.listing_id == "listing-b")
            .expect("row b present");
        assert_eq!(row_a.price_index_bps, 10_000);
        assert_eq!(row_b.price_index_bps, 20_000);
    }

    #[test]
    fn compare_flags_currency_inconsistency() {
        let registry_keypair = Keypair::generate();
        let listing = sample_listing(&registry_keypair, "listing-a", GenericListingStatus::Active);
        let operator_keypair = Keypair::generate();

        let hint_usd = SignedListingPricingHint::sign(
            ListingPricingHint {
                schema: LISTING_PRICING_HINT_SCHEMA.to_string(),
                listing_id: "listing-a".to_string(),
                namespace: "https://registry.chio.example".to_string(),
                provider_operator_id: "operator-a".to_string(),
                capability_scope: "tools:search".to_string(),
                price_per_call: MonetaryAmount {
                    units: 100,
                    currency: "USD".to_string(),
                },
                sla: ListingSla {
                    max_latency_ms: 500,
                    availability_bps: 9_990,
                    throughput_rps: 10,
                },
                revocation_rate_bps: 0,
                recent_receipts_volume: 10,
                issued_at: 100,
                expires_at: 500,
            },
            &operator_keypair,
        )
        .expect("sign usd");
        let hint_eur = SignedListingPricingHint::sign(
            ListingPricingHint {
                schema: LISTING_PRICING_HINT_SCHEMA.to_string(),
                listing_id: "listing-b".to_string(),
                namespace: "https://registry.chio.example".to_string(),
                provider_operator_id: "operator-a".to_string(),
                capability_scope: "tools:search".to_string(),
                price_per_call: MonetaryAmount {
                    units: 80,
                    currency: "EUR".to_string(),
                },
                sla: ListingSla {
                    max_latency_ms: 500,
                    availability_bps: 9_990,
                    throughput_rps: 10,
                },
                revocation_rate_bps: 0,
                recent_receipts_volume: 10,
                issued_at: 100,
                expires_at: 500,
            },
            &operator_keypair,
        )
        .expect("sign eur");

        let listings = vec![
            Listing {
                rank: 1,
                listing: listing.clone(),
                pricing: hint_usd,
                publisher: sample_publisher("operator-a"),
                freshness: GenericListingReplicaFreshness {
                    state: GenericListingFreshnessState::Fresh,
                    age_secs: 10,
                    max_age_secs: 300,
                    valid_until: 400,
                    generated_at: 100,
                },
            },
            Listing {
                rank: 2,
                listing,
                pricing: hint_eur,
                publisher: sample_publisher("operator-a"),
                freshness: GenericListingReplicaFreshness {
                    state: GenericListingFreshnessState::Fresh,
                    age_secs: 10,
                    max_age_secs: 300,
                    valid_until: 400,
                    generated_at: 100,
                },
            },
        ];
        let comparison = compare(&listings);
        assert!(!comparison.currency_consistent);
    }
}