trajectory.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Trajectory saving + scratchpad helpers (``_convert_to_trajectory_format`` stays an AIAgent method — batch_runner.py calls it)."""
  2. import json
  3. import logging
  4. import os
  5. from datetime import datetime
  6. from typing import Any, Dict, List
  7. logger = logging.getLogger(__name__)
  8. def convert_scratchpad_to_think(content: str) -> str:
  9. """Convert <REASONING_SCRATCHPAD> tags to <think> tags."""
  10. if not content or "<REASONING_SCRATCHPAD>" not in content:
  11. return content
  12. return content.replace("<REASONING_SCRATCHPAD>", "<think>").replace("</REASONING_SCRATCHPAD>", "</think>")
  13. def has_incomplete_scratchpad(content: str) -> bool:
  14. """Whether content has an opening <REASONING_SCRATCHPAD> without a closing tag."""
  15. return bool(content) and "<REASONING_SCRATCHPAD>" in content and "</REASONING_SCRATCHPAD>" not in content
  16. def _lock_append_handle(f, acquire: bool) -> None:
  17. """Exclusive whole-file lock on an append handle: ``flock`` on POSIX, a 1-byte
  18. ``msvcrt.locking`` range at offset 0 on Windows (append position is restored by the OS)."""
  19. if os.name == "nt":
  20. import msvcrt
  21. f.seek(0)
  22. msvcrt.locking(f.fileno(), msvcrt.LK_LOCK if acquire else msvcrt.LK_UNLCK, 1)
  23. f.seek(0, os.SEEK_END)
  24. else:
  25. import fcntl
  26. fcntl.flock(f.fileno(), fcntl.LOCK_EX if acquire else fcntl.LOCK_UN)
  27. def save_trajectory(trajectory: list[dict[str, Any]], model: str, completed: bool, filename: str | None = None):
  28. """Append a ShareGPT-format entry to a JSONL file (default trajectory_samples.jsonl / failed_trajectories.jsonl by ``completed``)."""
  29. if filename is None:
  30. filename = "trajectory_samples.jsonl" if completed else "failed_trajectories.jsonl"
  31. entry = {"conversations": trajectory, "timestamp": datetime.now().isoformat(), "model": model, "completed": completed}
  32. try:
  33. line = json.dumps(entry, ensure_ascii=False) + "\n" # serialize before taking the lock
  34. with open(filename, "a", encoding="utf-8") as f:
  35. # Gateway sessions and batch workers append to the SAME default file; without an
  36. # exclusive lock around write+flush, entries larger than one write() interleave and the
  37. # JSONL stops parsing (#12684).
  38. _lock_append_handle(f, True)
  39. try:
  40. f.write(line)
  41. f.flush()
  42. finally:
  43. _lock_append_handle(f, False)
  44. logger.info("Trajectory saved to %s", filename)
  45. except Exception as e:
  46. logger.warning("Failed to save trajectory: %s", e)