codec-rs 0.4.1

Isomorphic tokenizer + detokenizer for the Codec binary transport protocol — for Rust. Decodes streaming token IDs from Codec-compliant servers (vLLM, SGLang) and encodes text into IDs for the bidirectional path.
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
// SPDX-License-Identifier: MIT
//! Safety-policy descriptor loading, validation, and discovery.
//!
//! Rust twin of `@codecai/web`'s `safety_policy.ts` (slice 1) and
//! `codecai.safety_policy` (slice 11). Same shapes, same errors, same
//! canonical JSON form for hashing — a descriptor that hashes to
//! `sha256:abc…` in any client hashes to the identical digest here.
//!
//! Used by clients that received `safety_policy_id` + `safety_policy_hash`
//! in `READY` and want to fetch and surface what the server is
//! enforcing. The descriptor is the *sanitized*, publishable shape —
//! categories, actions, classifier family, summary stats — never the
//! operator's internal banned token IDs / classifier thresholds /
//! regex patterns.
//!
//! Discovery follows the existing tokenizer-map convention:
//!
//!   - `<origin>/.well-known/codec/policies/<id>.json`         (mutable)
//!   - `<origin>/.well-known/codec/policies/sha256/<hex>.json` (immutable)
//!
//! A client that received a hash in `READY` SHOULD prefer the
//! content-addressed sibling — it's provably immutable and skips the
//! mutable indirection.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

pub const POLICY_WELL_KNOWN_BASE: &str = "/.well-known/codec/policies";

// ── Types ────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CategoryAction {
    Stop,
    Redact,
    Regenerate,
    Flag,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ClassifierHost {
    Server,
    Client,
    Both,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EngineFeature {
    LogitsProcessor,
    HiddenStates,
    SamplingChain,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Category {
    pub name: String,
    pub action: CategoryAction,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub description: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ClassifierBlock {
    pub family: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub host: Option<ClassifierHost>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub requires_engine_features: Option<Vec<EngineFeature>>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct RulesSummary {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub banned_token_id_count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub regex_pattern_count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub grammar_constraint_count: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub multi_token_pattern_count: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct ClientHooksBlock {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub prefilter_categories: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub client_classifier_family: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct PublisherBlock {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub contact: Option<String>,
}

/// The sanitized, publishable safety-policy descriptor.
///
/// Matches `spec/safety-policy.schema.json` v1. Field order mirrors the
/// TS / Python clients so canonical JSON output is byte-identical.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SafetyPolicyDescriptor {
    pub id: String,
    pub version: String,
    pub tokenizers: Vec<String>,
    pub categories: Vec<Category>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub category_registry: Option<String>,
    pub classifier: ClassifierBlock,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub rules_summary: Option<RulesSummary>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub client_hooks: Option<ClientHooksBlock>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub published_at: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub publisher: Option<PublisherBlock>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SafetyPolicyPointer {
    pub id: String,
    pub url: String,
    pub hash: String,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub published_at: Option<String>,
}

// ── Errors ──────────────────────────────────────────────────────────────────

#[derive(Debug, thiserror::Error)]
pub enum SafetyPolicyError {
    #[error("SafetyPolicyDescriptor validation failed: {0}")]
    Validation(String),

    #[error("SafetyPolicyDescriptor parse failed: {0}")]
    Parse(#[from] serde_json::Error),

    #[error("SafetyPolicyDescriptor hash mismatch.\n  expected: {expected}\n  actual:   {actual}")]
    HashMismatch { expected: String, actual: String },

    #[error("Invalid policy id {id:?}: {reason}")]
    InvalidId { id: String, reason: &'static str },

    #[error("Invalid policy hash hex: must be 64-char lowercase hex (got {got:?})")]
    InvalidHashHex { got: String },

    #[error("Pointer id {got:?} does not match requested id {expected:?}")]
    PointerIdMismatch { got: String, expected: String },

    #[error("Pointer url must be http(s): got {got:?}")]
    PointerBadUrl { got: String },

    #[error("Pointer hash must be sha256:<64 hex chars>: got {got:?}")]
    PointerBadHash { got: String },

    #[error("Inline descriptor id {got:?} does not match requested id {expected:?}")]
    InlineIdMismatch { got: String, expected: String },

    #[cfg(feature = "http")]
    #[error("No safety-policy document at {url} (HTTP {status})")]
    NotFound { url: String, status: u16 },

    #[cfg(feature = "http")]
    #[error("http error: {0}")]
    Http(#[from] reqwest::Error),
}

// ── Validation ──────────────────────────────────────────────────────────────
//
// Hand-written shape check matching the TS / Python validators. Run
// against the parsed serde_json::Value before attempting to deserialize
// so we get clean error messages rather than serde's terser ones.

/// Documented charset spec mirrored across all client validators.
/// Kept as a string constant so error messages cite the same regex
/// the TS / Python clients reference.
const CATEGORY_NAME_RE: &str = r"^[a-z0-9_-]+$";

fn category_name_ok(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '-')
}

fn id_ok(s: &str) -> bool {
    !s.is_empty()
        && s.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '-' || c == '.' || c == '/')
        && !s.contains("..")
        && !s.starts_with('/')
        && !s.ends_with('/')
}

fn hex64_lower_ok(s: &str) -> bool {
    s.len() == 64 && s.chars().all(|c| c.is_ascii_hexdigit() && (!c.is_ascii_alphabetic() || c.is_ascii_lowercase()))
}

/// Validate a parsed JSON value as a SafetyPolicyDescriptor candidate.
///
/// Use before [`SafetyPolicyDescriptor::from_json`] when you want
/// human-readable error messages; otherwise serde's deserializer will
/// surface its own (terser) errors automatically.
pub fn validate_safety_policy(value: &serde_json::Value) -> Result<(), SafetyPolicyError> {
    let v = value
        .as_object()
        .ok_or_else(|| SafetyPolicyError::Validation("not an object".into()))?;

    let id = v.get("id").and_then(|x| x.as_str()).filter(|s| !s.is_empty())
        .ok_or_else(|| SafetyPolicyError::Validation("id must be a non-empty string".into()))?;
    let _ = id;

    v.get("version").and_then(|x| x.as_str())
        .ok_or_else(|| SafetyPolicyError::Validation("version must be a string".into()))?;

    let tokenizers = v.get("tokenizers").and_then(|x| x.as_array())
        .filter(|a| !a.is_empty())
        .ok_or_else(|| SafetyPolicyError::Validation(
            "tokenizers must be a non-empty array of tokenizer ids".into(),
        ))?;
    for t in tokenizers {
        if !t.is_string() {
            return Err(SafetyPolicyError::Validation(
                "tokenizers entries must be strings".into(),
            ));
        }
    }

    let categories = v.get("categories").and_then(|x| x.as_array())
        .filter(|a| !a.is_empty())
        .ok_or_else(|| SafetyPolicyError::Validation(
            "categories must be a non-empty array".into(),
        ))?;
    for c in categories {
        let cat = c.as_object().ok_or_else(|| {
            SafetyPolicyError::Validation("category entry must be an object".into())
        })?;
        let name = cat.get("name").and_then(|x| x.as_str()).ok_or_else(|| {
            SafetyPolicyError::Validation("category.name must be a string".into())
        })?;
        if !category_name_ok(name) {
            return Err(SafetyPolicyError::Validation(format!(
                "category.name must match {CATEGORY_NAME_RE} (got {name:?})"
            )));
        }
        let action = cat.get("action").and_then(|x| x.as_str()).ok_or_else(|| {
            SafetyPolicyError::Validation(format!(
                "category.action for {name:?} must be one of stop|redact|regenerate|flag"
            ))
        })?;
        if !matches!(action, "stop" | "redact" | "regenerate" | "flag") {
            return Err(SafetyPolicyError::Validation(format!(
                "category.action for {name:?} must be one of stop|redact|regenerate|flag"
            )));
        }
        if let Some(desc) = cat.get("description") {
            if !desc.is_string() && !desc.is_null() {
                return Err(SafetyPolicyError::Validation(format!(
                    "category.description for {name:?} must be a string when present"
                )));
            }
        }
    }

    let cls = v.get("classifier").and_then(|x| x.as_object()).ok_or_else(|| {
        SafetyPolicyError::Validation("classifier must be an object".into())
    })?;
    let family = cls.get("family").and_then(|x| x.as_str()).filter(|s| !s.is_empty())
        .ok_or_else(|| SafetyPolicyError::Validation(
            "classifier.family must be a non-empty string".into(),
        ))?;
    let _ = family;
    if let Some(host) = cls.get("host") {
        if !host.is_null() {
            let h = host.as_str().ok_or_else(|| {
                SafetyPolicyError::Validation(format!(
                    "classifier.host must be one of server|client|both (got {host})"
                ))
            })?;
            if !matches!(h, "server" | "client" | "both") {
                return Err(SafetyPolicyError::Validation(format!(
                    "classifier.host must be one of server|client|both (got {h:?})"
                )));
            }
        }
    }
    if let Some(feats) = cls.get("requires_engine_features") {
        if !feats.is_null() {
            let arr = feats.as_array().ok_or_else(|| {
                SafetyPolicyError::Validation(
                    "classifier.requires_engine_features must be an array".into(),
                )
            })?;
            for f in arr {
                let s = f.as_str().ok_or_else(|| {
                    SafetyPolicyError::Validation(
                        "classifier.requires_engine_features entry must be a string".into(),
                    )
                })?;
                if !matches!(s, "logits_processor" | "hidden_states" | "sampling_chain") {
                    return Err(SafetyPolicyError::Validation(format!(
                        "classifier.requires_engine_features entry must be one of \
                         logits_processor|hidden_states|sampling_chain (got {s:?})"
                    )));
                }
            }
        }
    }

    if let Some(rs) = v.get("rules_summary") {
        if !rs.is_null() {
            let m = rs.as_object().ok_or_else(|| {
                SafetyPolicyError::Validation("rules_summary must be an object when present".into())
            })?;
            for k in [
                "banned_token_id_count",
                "regex_pattern_count",
                "grammar_constraint_count",
                "multi_token_pattern_count",
            ] {
                if let Some(val) = m.get(k) {
                    if !val.is_null() && !val.as_u64().is_some() {
                        return Err(SafetyPolicyError::Validation(format!(
                            "rules_summary.{k} must be a non-negative integer when present"
                        )));
                    }
                }
            }
        }
    }

    Ok(())
}

// ── Builder ─────────────────────────────────────────────────────────────────

impl SafetyPolicyDescriptor {
    /// Parse + validate a JSON byte slice into a SafetyPolicyDescriptor.
    ///
    /// Validates first via [`validate_safety_policy`] for clean error
    /// messages, then deserializes via serde. Rejects shapes the
    /// validator accepts but serde would silently coerce (e.g. extra
    /// fields are ignored on the rust side which is the desired
    /// forward-compat behavior).
    pub fn from_json(bytes: &[u8]) -> Result<Self, SafetyPolicyError> {
        let parsed: serde_json::Value = serde_json::from_slice(bytes)?;
        validate_safety_policy(&parsed)?;
        let descriptor: SafetyPolicyDescriptor = serde_json::from_value(parsed)?;
        Ok(descriptor)
    }

    /// Canonical JSON serialization for hashing + well-known publish.
    ///
    /// Matches the TS / Python / supervisor format: 2-space indent +
    /// trailing newline. Fields with `None` values are omitted (via
    /// serde's `skip_serializing_if`) so the canonical bytes match
    /// across stacks.
    pub fn canonical_bytes(&self) -> Result<Vec<u8>, SafetyPolicyError> {
        let mut buf = Vec::new();
        let formatter = serde_json::ser::PrettyFormatter::with_indent(b"  ");
        let mut ser = serde_json::Serializer::with_formatter(&mut buf, formatter);
        self.serialize(&mut ser)?;
        buf.push(b'\n');
        Ok(buf)
    }

    /// Canonical sha256 hash of a descriptor.
    ///
    /// Returns `sha256:<64 hex chars>` matching what
    /// `codecai-maps policies hash` emits and what servers should
    /// publish in `READY.safety_policy_hash`.
    pub fn hash(&self) -> Result<String, SafetyPolicyError> {
        let bytes = self.canonical_bytes()?;
        let mut h = Sha256::new();
        h.update(&bytes);
        Ok(format!("sha256:{:x}", h.finalize()))
    }
}

fn parse_hash(hash: &str) -> Result<String, SafetyPolicyError> {
    if let Some(rest) = hash.strip_prefix("sha256:") {
        let lower = rest.to_ascii_lowercase();
        if !hex64_lower_ok(&lower) {
            return Err(SafetyPolicyError::InvalidHashHex { got: hash.to_string() });
        }
        Ok(lower)
    } else {
        let lower = hash.to_ascii_lowercase();
        if !hex64_lower_ok(&lower) {
            return Err(SafetyPolicyError::InvalidHashHex { got: hash.to_string() });
        }
        Ok(lower)
    }
}

// ── URL builders ────────────────────────────────────────────────────────────

fn strip_trailing_slash(s: &str) -> &str {
    s.strip_suffix('/').unwrap_or(s)
}

/// Per-policy URL by mutable id (e.g. `acme/strict-v3`).
pub fn well_known_policy_url(origin: &str, policy_id: &str) -> Result<String, SafetyPolicyError> {
    if !id_ok(policy_id) {
        return Err(SafetyPolicyError::InvalidId {
            id: policy_id.to_string(),
            reason: "must match [a-z0-9._/-]+ and contain no traversal",
        });
    }
    Ok(format!(
        "{}{POLICY_WELL_KNOWN_BASE}/{}.json",
        strip_trailing_slash(origin),
        policy_id,
    ))
}

/// Content-addressed URL by sha256 hex (no `sha256:` prefix).
pub fn well_known_policy_hash_url(origin: &str, hash_hex: &str) -> Result<String, SafetyPolicyError> {
    let lower = hash_hex.to_ascii_lowercase();
    if !hex64_lower_ok(&lower) {
        return Err(SafetyPolicyError::InvalidHashHex { got: hash_hex.to_string() });
    }
    Ok(format!(
        "{}{POLICY_WELL_KNOWN_BASE}/sha256/{}.json",
        strip_trailing_slash(origin),
        lower,
    ))
}

// ── Pointer detection ───────────────────────────────────────────────────────

fn is_pointer_shape(value: &serde_json::Value) -> bool {
    let Some(obj) = value.as_object() else { return false; };
    obj.get("id").is_some_and(|v| v.is_string())
        && obj.get("url").is_some_and(|v| v.is_string())
        && obj.get("hash").is_some_and(|v| v.is_string())
        // Inline descriptors always carry `categories`; pointers never do.
        && !obj.contains_key("categories")
}

fn validate_pointer(
    value: &serde_json::Value,
    expected_id: &str,
) -> Result<SafetyPolicyPointer, SafetyPolicyError> {
    let pointer: SafetyPolicyPointer = serde_json::from_value(value.clone())?;
    if pointer.id != expected_id {
        return Err(SafetyPolicyError::PointerIdMismatch {
            got: pointer.id,
            expected: expected_id.to_string(),
        });
    }
    if !(pointer.url.starts_with("https://") || pointer.url.starts_with("http://")) {
        return Err(SafetyPolicyError::PointerBadUrl { got: pointer.url });
    }
    if !pointer.hash.starts_with("sha256:") || !hex64_lower_ok(&pointer.hash[7..].to_ascii_lowercase()) {
        return Err(SafetyPolicyError::PointerBadHash { got: pointer.hash });
    }
    Ok(pointer)
}

// ── HTTP loader + discovery (gated on `http` feature) ───────────────────────

#[cfg(feature = "http")]
mod http_impl {
    use super::*;

    fn build_async_client() -> Result<reqwest::Client, reqwest::Error> {
        reqwest::Client::builder()
            .user_agent("codec-rs/0.1")
            .gzip(true)
            .brotli(true)
            .build()
    }

    /// Fetch + verify + cache a safety-policy descriptor.
    ///
    /// If `hash` is provided, the fetched bytes MUST hash to it
    /// (returns [`SafetyPolicyError::HashMismatch`] otherwise).
    pub async fn load_safety_policy(
        url: &str,
        hash: Option<&str>,
    ) -> Result<SafetyPolicyDescriptor, SafetyPolicyError> {
        let client = build_async_client()?;
        let resp = client.get(url).send().await?.error_for_status()?;
        let bytes = resp.bytes().await?;

        if let Some(expected) = hash {
            let want = parse_hash(expected)?;
            let mut h = Sha256::new();
            h.update(&bytes);
            let actual = format!("{:x}", h.finalize());
            if actual != want {
                return Err(SafetyPolicyError::HashMismatch { expected: want, actual });
            }
        }

        SafetyPolicyDescriptor::from_json(&bytes)
    }

    /// Resolve a safety-policy descriptor via `.well-known/codec/policies/`.
    ///
    /// If `hash` is provided, fetches the immutable content-addressed
    /// sibling at `<origin>/.well-known/codec/policies/sha256/<hex>.json`
    /// and verifies the bytes match. Otherwise fetches the mutable
    /// per-id document and follows a pointer if present.
    pub async fn discover_safety_policy(
        origin: &str,
        id: &str,
        hash: Option<&str>,
    ) -> Result<SafetyPolicyDescriptor, SafetyPolicyError> {
        let client = build_async_client()?;

        if let Some(h) = hash {
            let hash_hex = parse_hash(h)?;
            let url = well_known_policy_hash_url(origin, &hash_hex)?;
            let resp = client.get(&url).send().await?;
            if resp.status() == reqwest::StatusCode::NOT_FOUND {
                return Err(SafetyPolicyError::NotFound {
                    url,
                    status: resp.status().as_u16(),
                });
            }
            let resp = resp.error_for_status()?;
            let bytes = resp.bytes().await?;
            let mut hasher = Sha256::new();
            hasher.update(&bytes);
            let actual = format!("{:x}", hasher.finalize());
            if actual != hash_hex {
                return Err(SafetyPolicyError::HashMismatch {
                    expected: hash_hex,
                    actual,
                });
            }
            let parsed: serde_json::Value = serde_json::from_slice(&bytes)?;
            if is_pointer_shape(&parsed) {
                let pointer = validate_pointer(&parsed, id)?;
                return load_safety_policy(&pointer.url, Some(&pointer.hash)).await;
            }
            let descriptor = SafetyPolicyDescriptor::from_json(&bytes)?;
            if descriptor.id != id {
                return Err(SafetyPolicyError::InlineIdMismatch {
                    got: descriptor.id,
                    expected: id.to_string(),
                });
            }
            return Ok(descriptor);
        }

        let url = well_known_policy_url(origin, id)?;
        let resp = client.get(&url).send().await?;
        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(SafetyPolicyError::NotFound {
                url,
                status: resp.status().as_u16(),
            });
        }
        let resp = resp.error_for_status()?;
        let bytes = resp.bytes().await?;
        let parsed: serde_json::Value = serde_json::from_slice(&bytes)?;
        if is_pointer_shape(&parsed) {
            let pointer = validate_pointer(&parsed, id)?;
            return load_safety_policy(&pointer.url, Some(&pointer.hash)).await;
        }
        let descriptor = SafetyPolicyDescriptor::from_json(&bytes)?;
        if descriptor.id != id {
            return Err(SafetyPolicyError::InlineIdMismatch {
                got: descriptor.id,
                expected: id.to_string(),
            });
        }
        Ok(descriptor)
    }
}

#[cfg(feature = "http")]
pub use http_impl::{discover_safety_policy, load_safety_policy};

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

    fn valid_json() -> serde_json::Value {
        serde_json::json!({
            "id": "acme/strict-v3",
            "version": "1",
            "tokenizers": ["meta-llama/llama-3"],
            "categories": [
                {"name": "secrets", "action": "stop"},
                {"name": "pii", "action": "redact", "description": "Email and phone."},
            ],
            "classifier": {
                "family": "llama-guard-3-1b",
                "host": "server",
                "requires_engine_features": ["logits_processor", "sampling_chain"],
            },
            "rules_summary": {
                "banned_token_id_count": 4128,
                "regex_pattern_count": 47,
            },
            "client_hooks": {
                "prefilter_categories": ["secrets", "pii"],
                "client_classifier_family": "prompt-guard-86m",
            },
            "published_at": "2026-05-09T00:00:00Z",
        })
    }

    fn valid_descriptor() -> SafetyPolicyDescriptor {
        let bytes = serde_json::to_vec(&valid_json()).unwrap();
        SafetyPolicyDescriptor::from_json(&bytes).unwrap()
    }

    // ── Validation ─────────────────────────────────────────────────────────

    #[test]
    fn validate_accepts_minimal_valid_descriptor() {
        validate_safety_policy(&valid_json()).unwrap();
    }

    #[test]
    fn validate_rejects_missing_required_fields() {
        validate_safety_policy(&serde_json::json!({})).unwrap_err();
        let mut bad = valid_json();
        bad["id"] = serde_json::Value::String(String::new());
        validate_safety_policy(&bad).unwrap_err();
        let mut bad = valid_json();
        bad["tokenizers"] = serde_json::json!([]);
        validate_safety_policy(&bad).unwrap_err();
        let mut bad = valid_json();
        bad["categories"] = serde_json::json!([]);
        validate_safety_policy(&bad).unwrap_err();
    }

    #[test]
    fn validate_rejects_bad_category_name() {
        let mut bad = valid_json();
        bad["categories"] = serde_json::json!([{"name": "BadCaps", "action": "stop"}]);
        validate_safety_policy(&bad).unwrap_err();
    }

    #[test]
    fn validate_rejects_unknown_action() {
        let mut bad = valid_json();
        bad["categories"] = serde_json::json!([{"name": "secrets", "action": "banhammer"}]);
        validate_safety_policy(&bad).unwrap_err();
    }

    #[test]
    fn validate_rejects_unknown_engine_features() {
        let mut bad = valid_json();
        bad["classifier"]["requires_engine_features"] = serde_json::json!(["weather_api"]);
        validate_safety_policy(&bad).unwrap_err();
    }

    // ── Hash determinism ───────────────────────────────────────────────────

    #[test]
    fn hash_is_deterministic_for_identical_input() {
        let d = valid_descriptor();
        let a = d.hash().unwrap();
        let b = d.hash().unwrap();
        assert_eq!(a, b);
        assert!(a.starts_with("sha256:"));
        assert_eq!(a.len() - "sha256:".len(), 64);
    }

    #[test]
    fn hash_differs_when_category_action_changes() {
        let d1 = valid_descriptor();
        let mut json2 = valid_json();
        json2["categories"][0]["action"] = serde_json::Value::String("flag".into());
        let bytes = serde_json::to_vec(&json2).unwrap();
        let d2 = SafetyPolicyDescriptor::from_json(&bytes).unwrap();
        assert_ne!(d1.hash().unwrap(), d2.hash().unwrap());
    }

    #[test]
    fn canonical_bytes_match_2_space_indent_with_trailing_newline() {
        let d = valid_descriptor();
        let raw = d.canonical_bytes().unwrap();
        let text = std::str::from_utf8(&raw).unwrap();
        assert!(text.ends_with('\n'));
        // pretty-printed: contains a newline-then-two-spaces indent.
        assert!(text.contains("\n  "));
        // Round-trips through serde.
        let _: serde_json::Value = serde_json::from_str(text).unwrap();
    }

    // ── URL builders ───────────────────────────────────────────────────────

    #[test]
    fn well_known_policy_url_preserves_slashes_and_strips_trailing() {
        let url = well_known_policy_url("https://acme.example/", "acme/strict-v3").unwrap();
        assert_eq!(
            url,
            "https://acme.example/.well-known/codec/policies/acme/strict-v3.json"
        );
    }

    #[test]
    fn well_known_policy_url_rejects_traversal() {
        well_known_policy_url("https://acme.example", "../etc").unwrap_err();
        well_known_policy_url("https://acme.example", "/abs").unwrap_err();
        well_known_policy_url("https://acme.example", "trailing/").unwrap_err();
    }

    #[test]
    fn well_known_policy_url_rejects_bad_charset() {
        well_known_policy_url("https://acme.example", "Acme/Strict").unwrap_err();
    }

    #[test]
    fn well_known_policy_hash_url_uses_sha256_path() {
        let hex = "a".repeat(64);
        let url = well_known_policy_hash_url("https://acme.example", &hex).unwrap();
        assert_eq!(
            url,
            format!("https://acme.example/.well-known/codec/policies/sha256/{hex}.json")
        );
    }

    #[test]
    fn well_known_policy_hash_url_rejects_malformed_hex() {
        well_known_policy_hash_url("https://acme.example", "not-hex").unwrap_err();
    }

    // ── Round-trip ─────────────────────────────────────────────────────────

    #[test]
    fn descriptor_round_trip_canonical_bytes_to_json() {
        let d = valid_descriptor();
        let raw = d.canonical_bytes().unwrap();
        let d2 = SafetyPolicyDescriptor::from_json(&raw).unwrap();
        assert_eq!(d, d2);
    }

    #[test]
    fn from_json_rejects_bad_descriptor() {
        let bytes = b"{}";
        SafetyPolicyDescriptor::from_json(bytes).unwrap_err();
    }
}