Skip to content

The agent that turns websites into APIs!

fix(runtime): save history atomically and decode raw Python output

kalil0321cdb3d7d51d57
diff --git a/src/reverse_api/cli.py b/src/reverse_api/cli.py
index 25c54ba..b77b4e8 100644
--- a/src/reverse_api/cli.py
+++ b/src/reverse_api/cli.py
@@ -2950,6 +2950,8 @@ def _run_script_machine_payload(
"""Run or list generated scripts without prompts and with JSON-safe stdout."""
import subprocess
+ from .runtime_commands import decode_process_output
+
run_id: str | None = None
script: Path | None = None
scripts: list[Path] = []
@@ -3007,7 +3009,6 @@ def _run_script_machine_payload(
if script.suffix != ".py":
import shutil
- from .runtime_commands import decode_process_output
from .utils import build_script_commands
try:
@@ -3087,10 +3088,17 @@ def _run_script_machine_payload(
cmd = [str(venv_python), str(script), *script_args]
emit_event("process_started", run_id=run_id, script_path=str(script))
- result = subprocess.run(
- cmd, capture_output=True, text=True, encoding="utf-8", errors="replace",
- env={**os.environ, "PYTHONIOENCODING": "utf-8"},
- )
+ def run_client() -> subprocess.CompletedProcess[str]:
+ captured = subprocess.run(
+ cmd, capture_output=True,
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
+ )
+ return subprocess.CompletedProcess(
+ captured.args, captured.returncode,
+ decode_process_output(captured.stdout), decode_process_output(captured.stderr),
+ )
+
+ result = run_client()
stderr = result.stderr or ""
if result.returncode != 0 and "ModuleNotFoundError: No module named" in stderr:
@@ -3102,10 +3110,7 @@ def _run_script_machine_payload(
subprocess.run([str(venv_pip), "install", "-q", missing], check=True, capture_output=True, text=True, errors="replace")
emit_event("dependency_install_completed", run_id=run_id, package=missing)
emit_event("process_retried", run_id=run_id, script_path=str(script))
- result = subprocess.run(
- cmd, capture_output=True, text=True, encoding="utf-8", errors="replace",
- env={**os.environ, "PYTHONIOENCODING": "utf-8"},
- )
+ result = run_client()
else:
return _build_run_payload(
identifier=identifier,
diff --git a/src/reverse_api/session.py b/src/reverse_api/session.py
index 7932f11..26f2548 100644
--- a/src/reverse_api/session.py
+++ b/src/reverse_api/session.py
@@ -1,6 +1,7 @@
"""Session and history management for reverse-api."""
import json
+import tempfile
from pathlib import Path
from typing import Any
@@ -26,8 +27,20 @@ class SessionManager:
def save(self) -> None:
"""Save history to disk."""
self.history_path.parent.mkdir(parents=True, exist_ok=True)
- with open(self.history_path, "w", encoding="utf-8") as f:
- json.dump(self.history, f, indent=4)
+ # Close the temporary file before replacement (required on Windows),
+ # and keep it on the same filesystem for atomic replacement.
+ temp_path = None
+ try:
+ with tempfile.NamedTemporaryFile(
+ mode="w", encoding="utf-8", dir=self.history_path.parent,
+ prefix=f".{self.history_path.name}.", suffix=".tmp", delete=False,
+ ) as f:
+ temp_path = Path(f.name)
+ json.dump(self.history, f, indent=4)
+ temp_path.replace(self.history_path)
+ finally:
+ if temp_path is not None:
+ temp_path.unlink(missing_ok=True)
def add_run(self, run_id: str, prompt: str, **kwargs: Any) -> None:
"""Add a new run to history."""
diff --git a/tests/test_session.py b/tests/test_session.py
index 3963c9d..a9fd052 100644
--- a/tests/test_session.py
+++ b/tests/test_session.py
@@ -182,3 +182,24 @@ class TestSessionManagerSave:
sm = SessionManager(history_path)
sm.add_run("run1", "test")
assert history_path.exists()
+
+
+def test_interrupted_save_preserves_previous_history(tmp_path, monkeypatch):
+ import pytest
+
+ path = tmp_path / "history.json"
+ manager = SessionManager(path)
+ manager.add_run("original", "keep me")
+ original = path.read_bytes()
+ manager.history.insert(0, {"run_id": "new"})
+
+ def interrupted_dump(value, stream, **kwargs):
+ stream.write('[{"partial":')
+ raise KeyboardInterrupt
+
+ monkeypatch.setattr("reverse_api.session.json.dump", interrupted_dump)
+ with pytest.raises(KeyboardInterrupt):
+ manager.save()
+ assert path.read_bytes() == original
+ assert SessionManager(path).history[0]["run_id"] == "original"
+ assert not list(tmp_path.glob("*.tmp"))
diff --git a/tests/test_windows_compatibility.py b/tests/test_windows_compatibility.py
index 57a517c..033bae1 100644
--- a/tests/test_windows_compatibility.py
+++ b/tests/test_windows_compatibility.py
@@ -148,7 +148,7 @@ def test_node_diagnostic_probe_decodes_utf8_under_gbk(monkeypatch):
assert "中文 — 🐍" in error
[email protected]("language", ["python", "javascript"])
[email protected]("language", ["python", "python-legacy", "javascript"])
def test_run_json_real_unicode_output(tmp_path, monkeypatch, language):
"""Exercise actual child pipes under a legacy locale, including a spaced path."""
import venv
@@ -160,9 +160,15 @@ def test_run_json_real_unicode_output(tmp_path, monkeypatch, language):
output_dir = tmp_path / "User Name 中文"
scripts = output_dir / "scripts" / "unicode-run"
scripts.mkdir(parents=True)
- if language == "python":
+ if language.startswith("python"):
script = scripts / "api_client.py"
script.write_text("import sys\nprint('中文 — café 🐍')\nprint('erreur — 中文', file=sys.stderr)\n", encoding="utf-8")
+ if language == "python-legacy":
+ script.write_text(
+ "import sys\nsys.stdout.buffer.write('café\\n'.encode('cp1252'))\n"
+ "sys.stderr.buffer.write('erreur café\\n'.encode('cp1252'))\n", encoding="utf-8",
+ )
+ monkeypatch.setattr("locale.getencoding", lambda: "cp1252")
venv.EnvBuilder(with_pip=False).create(output_dir / ".venv")
else:
script = scripts / "api_client.js"
@@ -180,8 +186,8 @@ def test_run_json_real_unicode_output(tmp_path, monkeypatch, language):
result = CliRunner().invoke(cli.main, ["run", "unicode-run", "--json"])
assert result.exit_code == 0, result.output
payload = json.loads(result.output)
- assert payload["stdout"] == "中文 — café 🐍\n"
- assert payload["stderr"] == "erreur — 中文\n"
+ assert payload["stdout"] == ("café\n" if language == "python-legacy" else "中文 — café 🐍\n")
+ assert payload["stderr"] == ("erreur café\n" if language == "python-legacy" else "erreur — 中文\n")
def test_interactive_dependency_retry_preserves_unicode(tmp_path, monkeypatch):