Skip to content

Official Codebase of 3DCodeVerse

Python

events: `3dcv status` died on the truncated last line a killed run leaves

gaoypeng869cb01d3f21
diff --git a/harness/codeverse/events.py b/harness/codeverse/events.py
index b0f933fd..09f283ac 100644
--- a/harness/codeverse/events.py
+++ b/harness/codeverse/events.py
@@ -7,11 +7,14 @@ tests read. Keep payloads small (paths + numbers, not file contents).
from __future__ import annotations
import json
+import logging
import threading
import time
from pathlib import Path
from typing import Any
+log = logging.getLogger(__name__)
+
class EventLog:
def __init__(self, path: Path):
@@ -28,6 +31,21 @@ class EventLog:
fh.write(line + "\n")
def read(self) -> list[dict[str, Any]]:
+ """Read the log, skipping unparseable lines with a debug log.
+
+ ``emit`` appends one buffered ``write``, so a SIGKILL / OOM kill / reboot
+ leaves a partial trailing line — and a killed run is exactly when someone
+ types ``3dcv status``. A truncated last line must never lose the rest of
+ the file (same contract as ``cost.ledger.load_ledger``)."""
if not self.path.is_file():
return []
- return [json.loads(line) for line in self.path.read_text().splitlines() if line.strip()]
+ out: list[dict[str, Any]] = []
+ text = self.path.read_text(encoding="utf-8", errors="replace")
+ for i, line in enumerate(text.splitlines()):
+ if not line.strip():
+ continue
+ try:
+ out.append(json.loads(line))
+ except ValueError as e:
+ log.debug("events %s:%d unreadable: %s", self.path, i + 1, e)
+ return out
diff --git a/harness/tests/core/test_events.py b/harness/tests/core/test_events.py
new file mode 100644
index 00000000..588f6c47
--- /dev/null
+++ b/harness/tests/core/test_events.py
@@ -0,0 +1,55 @@
+"""EventLog durability: a run killed mid-write leaves a partial trailing line,
+and `3dcv status` is the first thing anyone types on a run that died (CP-3 / RS-6)."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+from typer.testing import CliRunner
+
+from codeverse.cli.main import app
+from codeverse.contracts.common import Backends, Language, Track
+from codeverse.contracts.spec import Spec
+from codeverse.events import EventLog
+from codeverse.workspace import Workspace
+
+runner = CliRunner()
+
+
+def test_read_skips_a_truncated_last_line(tmp_path: Path) -> None:
+ """`emit` writes one buffered line, so a SIGKILL truncates it mid-flush."""
+ log = EventLog(tmp_path / "events.jsonl")
+ log.emit("round.start", round=0)
+ log.emit("build.done", ok=True)
+ with log.path.open("a") as fh: # what a SIGKILL leaves behind
+ fh.write('{"t": 1756000001.0, "event": "round.start", "rou')
+
+ evs = log.read()
+
+ assert [e["event"] for e in evs] == ["round.start", "build.done"]
+
+
+def test_read_skips_a_bad_line_in_the_middle_and_undecodable_bytes(tmp_path: Path) -> None:
+ p = tmp_path / "events.jsonl"
+ p.write_bytes(b'{"event": "a"}\n{"event": "b\xff\n\n{"event": "c"}\n')
+
+ assert [e["event"] for e in EventLog(p).read()] == ["a", "c"]
+
+
+def test_status_still_prints_the_run_when_events_are_truncated(tmp_path: Path) -> None:
+ """The whole status view used to be lost to the last line of the log."""
+ runs = tmp_path / "runs"
+ ws = Workspace(runs / "stool").create()
+ spec = Spec(id="stool", track=Track.STATIC_OBJECT, language=Language.BLENDER,
+ prompt="a small wooden stool", backends=Backends(generator="api-agent:gemini:gemini-3.7-flash"))
+ ws.write_json(ws.spec_path, spec)
+ log = EventLog(ws.events_path)
+ log.emit("run.start")
+ with log.path.open("a") as fh:
+ fh.write('{"t": 1756000001.0, "event": "round.start", "rou')
+
+ res = runner.invoke(app, ["status", "stool", "--runs-dir", str(runs)])
+
+ assert res.exit_code == 0, res.output
+ assert "a small wooden stool" in res.output
+ assert "run.start" in res.output