mod canonicalize#

module canonicalize#

RFC 8785 JSON Canonicalization and whitespace normalization for cache stability.

This module provides deterministic JSON serialization (RFC 8785 / JCS) and whitespace normalization so that semantically equivalent content always produces byte-identical output. This is critical for cache key stability: without canonicalization, tool schemas or JSON content with different key orders would produce different hashes and miss the cache.

Public functions

Functions

fn canonicalize_json(json_str: &str) -> Result<String>#

Canonicalize a JSON string per RFC 8785 (JCS).

Parses the input as JSON, then re-serializes using deterministic key ordering and number formatting per the JSON Canonicalization Scheme.

Errors

Returns AcgError::Serialization if the input is not valid JSON or if canonicalization fails.

fn canonicalize_value(value: &serde_json::Value) -> Result<String>#

Canonicalize a serde_json::Value per RFC 8785.

Errors

Returns AcgError::Serialization if canonicalization fails.

fn normalize_whitespace(text: &str) -> String#

Normalize whitespace in text content.

  1. Trims leading and trailing whitespace.

  2. Collapses runs of horizontal whitespace (spaces, tabs, and other Unicode whitespace characters except \n) into a single ASCII space.

  3. Preserves single newline characters (they separate paragraphs in prompts).

fn sha256_hex(input: &str) -> String#

Compute SHA-256 hex digest with "sha256:" prefix.

The output format is sha256:<hex> where <hex> is the lowercase hexadecimal encoding of the 256-bit digest.