Skip to content

Commit 6ecbeda

Browse files
committed
Accept finite Cosmoaudition wire numbers
1 parent 0ca54d2 commit 6ecbeda

3 files changed

Lines changed: 42 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
- Preserved Cosmoaudition frame-level signals and sources beside resolved
1919
routes, and withheld malformed or out-of-range executable controls with an
2020
explicit bridge reason.
21+
- Accepted finite JavaScript wire numbers whose integer spelling exceeds a
22+
signed 64-bit persistence value, so high-magnitude observations such as the
23+
Bitcoin hashrate cannot invalidate an otherwise valid modulation frame.
2124
- Aligned active project language to **Sonic Matter Framework**: MASA
2225
describes, Cosmoaudition observes and modulates, and GERM cultivates. The
2326
projects remain distinct.

server/cosmoaudition.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,33 @@ def _reject_nonfinite_json(value: str) -> None:
5050
raise ValueError(f"non-finite JSON constant: {value}")
5151

5252

53+
def _decode_json_integer(value: str) -> int | float:
54+
"""Keep ordinary integers exact and accept finite JavaScript wire numbers.
55+
56+
Cosmoaudition serializes IEEE-754 numbers with ``JSON.stringify``. A value
57+
such as the Bitcoin hashrate can therefore arrive with an integer spelling
58+
outside GERM's signed-64-bit persistence boundary even though the producer
59+
already represents it as a finite float. Preserve normal JSON integers as
60+
integers, but materialize those wider wire values as finite floats so one
61+
observation cannot invalidate the complete modulation frame.
62+
"""
63+
64+
decoded = int(value)
65+
if -(2**63) <= decoded < 2**63:
66+
return decoded
67+
widened = float(value)
68+
if not math.isfinite(widened):
69+
raise ValueError("JSON integer exceeds the finite-number boundary")
70+
return widened
71+
72+
5373
def _decode_json_object(raw: bytes) -> dict[str, Any]:
5474
try:
55-
value = json.loads(raw, parse_constant=_reject_nonfinite_json)
75+
value = json.loads(
76+
raw,
77+
parse_constant=_reject_nonfinite_json,
78+
parse_int=_decode_json_integer,
79+
)
5680
except (UnicodeError, json.JSONDecodeError, ValueError, RecursionError) as exc:
5781
raise CosmoauditionBridgeError("Cosmoaudition returned invalid JSON") from exc
5882
if not isinstance(value, dict):

tests/test_server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,20 @@ def test_cosmoaudition_bridge_rejects_excessively_nested_json() -> None:
973973
_decode_json_object(payload)
974974

975975

976+
def test_cosmoaudition_bridge_accepts_finite_javascript_number_beyond_int64() -> None:
977+
payload = _decode_json_object(b'{"hashrate":976061469948095400000}')
978+
979+
assert payload["hashrate"] == float("976061469948095400000")
980+
assert isinstance(payload["hashrate"], float)
981+
982+
983+
def test_cosmoaudition_bridge_rejects_integer_beyond_finite_float_range() -> None:
984+
payload = b'{"value":' + b"1" + (b"0" * 400) + b"}"
985+
986+
with pytest.raises(CosmoauditionBridgeError, match="invalid JSON"):
987+
_decode_json_object(payload)
988+
989+
976990
def test_cosmoaudition_status_requires_boolean_remote_health(
977991
monkeypatch: pytest.MonkeyPatch,
978992
) -> None:

0 commit comments

Comments
 (0)