"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): tolerate a half-written final line in read_events
Rudy Celekli5cdf5636025e
diff --git a/agent/src/swarm/store.py b/agent/src/swarm/store.pyindex 8289e64c1..788321185 100644--- a/agent/src/swarm/store.py+++ b/agent/src/swarm/store.py@@ -277,11 +277,24 @@ class SwarmStore:return []events: list[SwarmEvent] = []- lines = events_file.read_text(encoding="utf-8").strip().splitlines()- for line in lines[after_index:]:+ text = events_file.read_text(encoding="utf-8")+ lines = text.strip().splitlines()+ # ``append_event`` is not atomic across processes/instances: a reader+ # (the SSE poll loop, ``_persist_recovery``) can observe a final line+ # that is still being written, or one cut short by a crashed host. That+ # unterminated tail is not an event yet, so skip it instead of raising+ # and killing the stream; the next poll sees it whole.+ tail_unterminated = bool(text) and not text.endswith("\n")+ for position, line in enumerate(lines[after_index:], start=after_index):stripped = line.strip()- if stripped:+ if not stripped:+ continue+ try:events.append(SwarmEvent.model_validate_json(stripped))+ except ValueError:+ if tail_unterminated and position == len(lines) - 1:+ break+ raisereturn eventsdef hydrate_run(self, run: SwarmRun) -> SwarmRun:diff --git a/agent/tests/test_swarm_events_partial_tail.py b/agent/tests/test_swarm_events_partial_tail.pynew file mode 100644index 000000000..961e0cd5b--- /dev/null+++ b/agent/tests/test_swarm_events_partial_tail.py@@ -0,0 +1,40 @@+"""``SwarmStore.read_events`` must tolerate a half-written final event line."""++from __future__ import annotations++import pytest++from src.swarm.models import SwarmEvent+from src.swarm.store import SwarmStore+++def _store(tmp_path):+ store = SwarmStore(tmp_path)+ (tmp_path / "run1").mkdir()+ return store+++def test_partial_trailing_line_is_skipped_not_raised(tmp_path):+ store = _store(tmp_path)+ store.append_event("run1", SwarmEvent(type="run_started", timestamp="2026-01-01T00:00:00+00:00"))+ events_file = tmp_path / "run1" / "events.jsonl"+ with events_file.open("a", encoding="utf-8") as f:+ f.write('{"type": "task_started", "agent_') # writer mid-line / crashed host++ events = store.read_events("run1")+ assert [e.type for e in events] == ["run_started"]++ # Once the writer finishes the line, the next poll sees the event.+ with events_file.open("a", encoding="utf-8") as f:+ f.write('id": "a1", "data": {}, "timestamp": "2026-01-01T00:00:01+00:00"}\n')+ events = store.read_events("run1", after_index=1)+ assert [e.type for e in events] == ["task_started"]+++def test_corrupt_terminated_line_still_raises(tmp_path):+ store = _store(tmp_path)+ store.append_event("run1", SwarmEvent(type="run_started", timestamp="2026-01-01T00:00:00+00:00"))+ with (tmp_path / "run1" / "events.jsonl").open("a", encoding="utf-8") as f:+ f.write("not json\n")+ with pytest.raises(ValueError):+ store.read_events("run1")