Skip to content

aether_quant.io

Finite-safe JSON helpers (sanitize_json_finite is formally verified in formal/AetherFormal/JsonSafety.lean).

Finite-safe JSON helpers (sanitize_json_finite is formally verified in formal/AetherFormal/JsonSafety.lean).

atomic_write_json

atomic_write_json(path: Path, payload: Any, *, indent: int | None = None) -> None

Writes payload to path via a same-directory temp file + os.replace.

os.replace is atomic within a filesystem, so a concurrent reader sees either the complete old file or the complete new file - never a torn one - and a writer crash mid-write leaves the previous generation intact. The temp file is cleaned up on failure.

atomic_write_text

atomic_write_text(path: Path, text: str) -> None

atomic_write_json's twin for plain-text content (CSV caches etc.) - same same-directory temp file + os.replace contract.

dumps_json_safe

dumps_json_safe(payload: Any) -> str

sanitize + dumps with allow_nan=False - the single serialization path shared by the Redis producers and the status-file writers, so a non-finite value can never reach Postgres JSONB or the webui again.

load_json_lenient

load_json_lenient(path: Path) -> Any

Reads and parses path, returning None on ANY failure (missing, unreadable, mid-write torn, malformed). Every caller treats None as 'not available right now' and degrades - the monitoring readers' documented contract, now actually true while a writer is mid-replace.

sanitize_json_finite

sanitize_json_finite(value: Any) -> Any

Recursively converts a structure into something json.dumps(..., allow_nan=False) can always serialize:

  • non-finite floats (NaN / +/-inf) -> None ("value unavailable", the repo-wide degrade convention)
  • numpy integer/float scalars -> plain int/float (via .item())
  • dict keys are passed through unchanged (they must already be strings)

Tuples/lists/dicts are rebuilt; everything else returns unchanged.