Skip to content

The agent that grows with you

fix(agent): keep trajectory serialization inside the best-effort handler

teknium1330004e59797
diff --git a/agent/trajectory.py b/agent/trajectory.py
index aa4dacf54f1..4c8616d90d3 100644
--- a/agent/trajectory.py
+++ b/agent/trajectory.py
@@ -39,8 +39,8 @@ def save_trajectory(trajectory: List[Dict[str, Any]], model: str, completed: boo
if filename is None:
filename = "trajectory_samples.jsonl" if completed else "failed_trajectories.jsonl"
entry = {"conversations": trajectory, "timestamp": datetime.now().isoformat(), "model": model, "completed": completed}
- line = json.dumps(entry, ensure_ascii=False) + "\n"
try:
+ line = json.dumps(entry, ensure_ascii=False) + "\n" # serialize before taking the lock
with open(filename, "a", encoding="utf-8") as f:
# Gateway sessions and batch workers append to the SAME default file; without an
# exclusive lock around write+flush, entries larger than one write() interleave and the