Skip to content

Your lightweight open-source personal agent.

fix(utils): preserve exact newlines in atomic text writes (#6475)

Quanxin Liuc4dac649da99
diff --git a/nanobot/utils/atomic_file.py b/nanobot/utils/atomic_file.py
index 3bc20cb55..718d87497 100644
--- a/nanobot/utils/atomic_file.py
+++ b/nanobot/utils/atomic_file.py
@@ -137,7 +137,7 @@ def atomic_write_text(path: Path, content: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_name(f".{path.name}.{uuid.uuid4().hex}.tmp")
try:
- with open(tmp, "x", encoding="utf-8") as handle:
+ with open(tmp, "x", encoding="utf-8", newline="") as handle:
handle.write(content)
handle.flush()
os.fsync(handle.fileno())
diff --git a/tests/test_update_jobs.py b/tests/test_update_jobs.py
index 5534c59ed..d955ec0ce 100644
--- a/tests/test_update_jobs.py
+++ b/tests/test_update_jobs.py
@@ -1539,8 +1539,10 @@ async def test_stale_first_supervisor_cannot_take_new_confirmation(managed_insta
assert not (directory / "approval.json").exists()
assert not (installation_state_dir(spec.baseline.environment) / "stop.json").exists()
# B retains its first supervision budget and completes the explicit retry.
+ # This path performs a real backup, pip install, restart and snapshot cleanup.
+ # Parallel Windows runners can exceed the stale-launcher's 40s rejection budget.
try:
- completed = await asyncio.to_thread(subprocess.run, [*command, current.id], capture_output=True, timeout=40)
+ completed = await asyncio.to_thread(subprocess.run, [*command, current.id], capture_output=True, timeout=120)
except subprocess.TimeoutExpired:
# Identify the blocked phase without exposing the confirmation nonce or
# making a slow snapshot indistinguishable from a handoff deadlock.
@@ -1549,7 +1551,7 @@ async def test_stale_first_supervisor_cannot_take_new_confirmation(managed_insta
"mutation_started": status.mutation_started, "message": status.message,
"snapshot_complete": (directory / "program-backup.json").exists(),
"activation_complete": (directory / "activation.json").exists()}
- pytest.fail(f"Confirmed supervisor exceeded 40s: {progress}", pytrace=False)
+ pytest.fail(f"Confirmed supervisor exceeded 120s: {progress}", pytrace=False)
assert completed.returncode == 0, completed.stderr.decode()
assert read_record(directory / "supervision.json")["confirmation_id"] == current.id
assert JobStatus.read(directory).phase == Phase.SUCCEEDED
diff --git a/tests/utils/test_helpers.py b/tests/utils/test_helpers.py
index 3e6c35d48..77dbaec11 100644
--- a/tests/utils/test_helpers.py
+++ b/tests/utils/test_helpers.py
@@ -194,14 +194,17 @@ def test_write_text_atomic_keeps_file_when_directory_fsync_is_unsupported(
assert len(fsync_calls) == 1
-def test_atomic_write_text_creates_parent_and_replaces_exact_content(tmp_path: Path) -> None:
[email protected]("newline", ["\n", "\r\n", "\r"])
+def test_atomic_write_text_creates_parent_and_replaces_exact_content(
+ tmp_path: Path, newline: str,
+) -> None:
target = tmp_path / "runs" / "record.json"
atomic_write_text(target, "old")
assert target.read_text(encoding="utf-8") == "old"
- content = '{"message": "café"}'
+ content = f'{{{newline} "message": "café"{newline}}}{newline}'
atomic_write_text(target, content)
- assert target.read_text(encoding="utf-8") == content
+ assert target.read_bytes() == content.encode("utf-8")
atomic_write_text(target, "")
assert target.read_text(encoding="utf-8") == ""