Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(evidence): read a checkpoint config or report as utf-8-sig too

PerryLink4a5456e10279
diff --git a/laya/evidence.py b/laya/evidence.py
index 11888787..3c23564d 100644
--- a/laya/evidence.py
+++ b/laya/evidence.py
@@ -188,7 +188,11 @@ def _read_json_object(path: str) -> Dict[str, Any]:
quality failure -- naming no file at all, exactly the thing this docstring refuses to allow.
"""
try:
- with open(path, "r", encoding="utf-8") as handle:
+ # `utf-8-sig`: a checkpoint config or report written by a Windows editor starts with a
+ # UTF-8 BOM, which plain `utf-8` keeps as U+FEFF, so a valid JSON artifact was refused as
+ # "is not valid JSON". The other caller-file readers in this repo open the same way
+ # (laya/train.py, docker/entrypoint.py); a file without a BOM reads identically.
+ with open(path, "r", encoding="utf-8-sig") as handle:
document = json.load(handle)
except json.JSONDecodeError as exc:
raise ValueError("%s is not valid JSON: %s" % (path, exc)) from exc
diff --git a/tests/test_evidence.py b/tests/test_evidence.py
index 4cee5a59..be7a9bfa 100644
--- a/tests/test_evidence.py
+++ b/tests/test_evidence.py
@@ -379,6 +379,19 @@ check_true("and its differing digest reaches it as the conflict it names",
check_true("every identity field is printed for a report with no config block",
all((" %-18s" % key) in _summary for key in evidence._IDENTITY_KEYS))
+# A UTF-8 BOM is what Notepad and Excel write. `_read_json_object` opened the artifact as plain
+# utf-8, so the BOM became U+FEFF and a valid config was refused as "not valid JSON". The other
+# caller-file readers in this repo open as utf-8-sig for this reason (laya/train.py,
+# docker/entrypoint.py); utf-8-sig leaves a file without a BOM unchanged.
+_bom_path = os.path.join(tmp, "bom-artifact.json")
+with open(_bom_path, "wb") as f:
+ f.write(b"\xef\xbb\xbf" + json.dumps({"a": 1}).encode("utf-8"))
+try:
+ _bom_doc = evidence._read_json_object(_bom_path)
+except ValueError as exc:
+ _bom_doc = "refused: %s" % exc
+check("a UTF-8 BOM artifact is read, not refused as invalid JSON", _bom_doc, {"a": 1})
+
# ------------------------------------------------------ no torch required
import subprocess
_BLOCK = (