Skip to content

The agent that grows with you

fix(file-ops): refuse nonstandard JSON constants before writes

Rudy Celekli1c78e721bc2d
diff --git a/tests/tools/test_json_write_constants.py b/tests/tools/test_json_write_constants.py
new file mode 100644
index 00000000000..a4f12f375d2
--- /dev/null
+++ b/tests/tools/test_json_write_constants.py
@@ -0,0 +1,35 @@
+"""JSON file writes reject non-JSON constants without changing existing bytes."""
+
+import json
+
+from tools import file_tools, terminal_tool
+from tools.registry import registry
+
+
+def test_json_write_refuses_nonstandard_constants_without_overwriting(tmp_path, monkeypatch):
+ home = tmp_path / "home"
+ home.mkdir()
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ task = "strict-json-write"
+ path = tmp_path / "config.json"
+ original = '{"value": 1}\n'
+ path.write_text(original, encoding="utf-8")
+ terminal_tool.register_task_env_overrides(task, {"env_type": "local", "cwd": str(tmp_path)})
+ try:
+ read = json.loads(registry.dispatch("read_file", {"path": str(path)}, task_id=task))
+ assert "error" not in read, read
+ for token in ("NaN", "Infinity", "-Infinity"):
+ result = json.loads(registry.dispatch("write_file", {
+ "path": str(path), "content": '{"value": ' + token + '}\n',
+ }, task_id=task))
+ assert "error" in result, result
+ assert path.read_text(encoding="utf-8") == original
+ valid = '{"value": ["NaN", "Infinity", "-Infinity", 1.5, null]}\n'
+ result = json.loads(registry.dispatch("write_file", {
+ "path": str(path), "content": valid,
+ }, task_id=task))
+ assert "error" not in result, result
+ assert path.read_text(encoding="utf-8") == valid
+ finally:
+ file_tools.clear_file_ops_cache(task)
+ terminal_tool.clear_task_env_overrides(task)
diff --git a/tools/file_operations_lint.py b/tools/file_operations_lint.py
index ff3273c0ead..0ade3321924 100644
--- a/tools/file_operations_lint.py
+++ b/tools/file_operations_lint.py
@@ -68,8 +68,13 @@ def _looks_like_linter_unusable(base_cmd: str, output: str) -> bool:
def _lint_json_inproc(content: str) -> tuple[bool, str]:
"""In-process JSON syntax check. Returns (ok, error_message)."""
+ def reject_constant(value: str):
+ # json.loads accepts these JavaScript extensions by default, but strict
+ # JSON consumers reject them and file writes promise a syntax gate.
+ raise ValueError(f"{value} is not a valid JSON constant")
+
try:
- json.loads(content)
+ json.loads(content, parse_constant=reject_constant)
return True, ""
except json.JSONDecodeError as e:
return False, f"JSONDecodeError: {e.msg} (line {e.lineno}, column {e.colno})"