"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(swarm): task files get unique temp names too (#1536)
Haozhe Wud13eba6f2ded
diff --git a/agent/src/swarm/task_store.py b/agent/src/swarm/task_store.pyindex 70a38dd79..31af4155e 100644--- a/agent/src/swarm/task_store.py+++ b/agent/src/swarm/task_store.py@@ -7,6 +7,7 @@ Provides DAG algorithms: dependency resolution, cycle detection, and topologicalfrom __future__ import annotationsimport threading+import uuidfrom collections import defaultdict, dequefrom pathlib import Path@@ -51,7 +52,10 @@ class TaskStore:task: SwarmTask instance."""path = self._task_path(task.id)- tmp_path = path.with_suffix(".tmp")+ # self._lock is per instance, and resolve_dependencies() writes the same+ # files without it; a shared ".tmp" name let one writer's rename consume+ # another's unwritten temp file (#1536).+ tmp_path = path.with_suffix(f".{uuid.uuid4().hex}.tmp")with self._lock:tmp_path.write_text(task.model_dump_json(indent=2), encoding="utf-8")tmp_path.replace(path)@@ -140,7 +144,7 @@ def resolve_dependencies(tasks_dir: Path, completed_task_id: str) -> list[str]:newly_unblocked.append(task.id)updated_task = SwarmTask.model_validate(updated_data)- tmp_path = path.with_suffix(".tmp")+ tmp_path = path.with_suffix(f".{uuid.uuid4().hex}.tmp")tmp_path.write_text(updated_task.model_dump_json(indent=2), encoding="utf-8")tmp_path.replace(path)diff --git a/agent/tests/test_swarm_store_atomic_write.py b/agent/tests/test_swarm_store_atomic_write.pyindex 2c0a4dc52..34c3b4e35 100644--- a/agent/tests/test_swarm_store_atomic_write.py+++ b/agent/tests/test_swarm_store_atomic_write.py@@ -163,6 +163,39 @@ def test_concurrent_stores_writing_same_run_do_not_crash_or_clobber(tmp_path):assert store_a.load_run("r").final_report in {"FROM-A", "FROM-B"}+def test_concurrent_task_stores_saving_one_task_do_not_crash(tmp_path):+ """The TaskStore sibling of the SwarmStore race (#1536): two workers writing+ the same task file each used ``task-<id>.tmp``, so one rename could consume+ the other's temp file and raise FileNotFoundError."""+ from src.swarm.models import SwarmTask+ from src.swarm.task_store import TaskStore++ errors: list[Exception] = []++ def write(store: TaskStore, task: SwarmTask, gate: threading.Barrier) -> None:+ gate.wait()+ try:+ for _ in range(20):+ store.save_task(task)+ except Exception as exc: # noqa: BLE001 - captured for the assertion below+ errors.append(exc)++ task = SwarmTask(id="t1", agent_id="analyst", prompt_template="do x")+ for _ in range(10):+ gate = threading.Barrier(2)+ threads = [+ threading.Thread(target=write, args=(TaskStore(tmp_path), task, gate)) for _ in range(2)+ ]+ for thread in threads:+ thread.start()+ for thread in threads:+ thread.join()++ assert not errors, f"concurrent save_task raised: {errors[:3]!r}"+ assert TaskStore(tmp_path).load_task("t1").id == "t1"+ assert not list((tmp_path / "tasks").glob("*.tmp")), "a temp file was left behind"++def test_posix_oserror_is_not_treated_transient():"""POSIX no-op guard: a plain OSError (no winerror) is never transient,so off-Windows the retry loop runs exactly once — no behavior change."""