Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix: canonicalize numeric metadata keys for fixture digests (#260)
Rudy Mizrahi Celekli09d581e04343
diff --git a/ifixai/tests/test_fixture_numeric_metadata_digest.py b/ifixai/tests/test_fixture_numeric_metadata_digest.pynew file mode 100644index 0000000..cd531f2--- /dev/null+++ b/ifixai/tests/test_fixture_numeric_metadata_digest.py@@ -0,0 +1,42 @@+from pathlib import Path++import pytest+import yaml++from ifixai.core.fixture_loader import load_fixture, validate_fixture+from ifixai.utils.fixture_digest import compute_fixture_digest+++def write_fixture(tmp_path, counts):+ source = Path(__file__).parents[1] / "fixtures/examples/openclaw_strict.yaml"+ raw = yaml.safe_load(source.read_text(encoding="utf-8"))+ raw["test_cases"][0]["metadata"] = {"counts": counts}+ path = tmp_path / "fixture.yaml"+ path.write_text(yaml.safe_dump(raw, sort_keys=False), encoding="utf-8")+ return path+++def test_nested_numeric_metadata_keys_follow_json_key_convention(tmp_path):+ path = write_fixture(tmp_path, {1: "one", "other": "two"})+ assert validate_fixture(path) == []+ assert load_fixture(path).test_cases[0].metadata+ digest = compute_fixture_digest(path)+ write_fixture(tmp_path, {"1": "one", "other": "two"})+ assert compute_fixture_digest(path) == digest+++def test_json_key_collisions_are_rejected(tmp_path):+ path = write_fixture(tmp_path, {1: "one", "1": "different"})+ with pytest.raises(ValueError, match="duplicate canonical fixture key"):+ compute_fixture_digest(path)+++def test_numeric_only_keys_preserve_legacy_digest(tmp_path):+ import hashlib+ import json++ path = tmp_path / "numeric-only.yaml"+ path.write_text("metadata:\n 2: two\n 10: ten\n")+ legacy = json.dumps({"metadata": {2: "two", 10: "ten"}},+ sort_keys=True, separators=(",", ":"), ensure_ascii=False)+ assert compute_fixture_digest(path) == hashlib.sha256(legacy.encode()).hexdigest()diff --git a/ifixai/utils/fixture_digest.py b/ifixai/utils/fixture_digest.pyindex e70d352..71b9183 100644--- a/ifixai/utils/fixture_digest.py+++ b/ifixai/utils/fixture_digest.py@@ -10,16 +10,28 @@ import yamldef _canonicalise(obj: Any) -> Any:- if isinstance(obj, (date, datetime)):+ if isinstance(obj, date | datetime):return obj.isoformat()if isinstance(obj, dict):canonical = {}for key, value in obj.items():- canonical_key = key.isoformat() if isinstance(key, (date, datetime)) else key+ canonical_key = key.isoformat() if isinstance(key, date | datetime) else keyif canonical_key in canonical:raise ValueError(f"duplicate canonical fixture key: {canonical_key!r}")canonical[canonical_key] = _canonicalise(value)- return {key: canonical[key] for key in sorted(canonical)}+ try:+ keys = sorted(canonical)+ except TypeError:+ # Mixed JSON-compatible keys need a shared ordering. Preserve the+ # native ordering for homogeneous numeric keys and existing digests.+ converted = {}+ for key, value in canonical.items():+ json_key = next(iter(json.loads(json.dumps({key: None}))))+ if json_key in converted:+ raise ValueError(f"duplicate canonical fixture key: {json_key!r}")+ converted[json_key] = value+ return {key: converted[key] for key in sorted(converted)}+ return {key: canonical[key] for key in keys}if isinstance(obj, list):return [_canonicalise(item) for item in obj]return obj