7f39abb613
Add declarative single-leg import, fixed-anchor benchmark, masked shared-policy PPO prototype, regression tests, and consolidated documentation.
57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
"""Stdlib-only reporting and shutdown helpers; safe to import before Kit."""
|
|
|
|
import json
|
|
import os
|
|
import re
|
|
import tempfile
|
|
import traceback
|
|
from pathlib import Path
|
|
|
|
CRITICAL_LOG = re.compile(r"\[(?:Error|Fatal)\]|buffer\s+overflow", re.IGNORECASE)
|
|
|
|
|
|
def write_json(path, data):
|
|
"""Replace complete reports atomically; never serialize NaN/Inf as valid JSON."""
|
|
path = Path(path)
|
|
content = json.dumps(data, indent=2, allow_nan=False) + "\n"
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
temporary = None
|
|
try:
|
|
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=path.parent, delete=False) as stream:
|
|
temporary = Path(stream.name)
|
|
stream.write(content)
|
|
os.replace(temporary, path)
|
|
finally:
|
|
if temporary is not None:
|
|
temporary.unlink(missing_ok=True)
|
|
|
|
|
|
def critical_log_lines(path):
|
|
with Path(path).open(errors="replace") as stream:
|
|
return [line.rstrip() for line in stream if CRITICAL_LOG.search(line)]
|
|
|
|
|
|
def finish_run(app, env, path, result, exit_code=0):
|
|
"""Close the environment before publishing success and Kit's fast exit."""
|
|
if env is not None:
|
|
try:
|
|
env.close()
|
|
except BaseException:
|
|
error = traceback.format_exc()
|
|
traceback.print_exc()
|
|
result.update(passed=False, shutdown_error=error)
|
|
if "milestone_complete" in result:
|
|
result["milestone_complete"] = False
|
|
exit_code = 1
|
|
if not result.get("passed"):
|
|
exit_code = exit_code or 1
|
|
if exit_code:
|
|
result["passed"] = False
|
|
result["status"] = "completed" if not exit_code else "failed"
|
|
try:
|
|
write_json(path, result)
|
|
except BaseException:
|
|
traceback.print_exc()
|
|
exit_code = 1
|
|
app.close(exit_code=exit_code)
|