Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(config): retry atomic provider switch writes

Ruben Lütke270299b94777
diff --git a/jarvis/core/config_writer.py b/jarvis/core/config_writer.py
index e283ed6e64..e47437748c 100644
--- a/jarvis/core/config_writer.py
+++ b/jarvis/core/config_writer.py
@@ -21,7 +21,9 @@ import logging
import os
import stat
import sys
+import tempfile
import threading
+import time
from pathlib import Path
import tomlkit
@@ -32,6 +34,7 @@ from .config import DEFAULT_CONFIG_FILE, PROJECT_ROOT
log = logging.getLogger(__name__)
_WRITE_LOCK = threading.Lock()
+_ATOMIC_REPLACE_RETRY_DELAYS_S = (0.0, 0.025, 0.05, 0.1, 0.2, 0.4)
_BOM = ""
# Canonical User-scope ENV var that overrides ``[brain] primary`` at boot
@@ -690,7 +693,12 @@ def _ensure_writable_config_path(path: Path) -> Path:
``resolve_config_path()``; then create the file (+ parent) if absent so an
in-app save/connect persists on EVERY OS instead of raising FileNotFoundError.
"""
- from jarvis.core.config import DEFAULT_CONFIG_FILE as _DEFAULT, resolve_config_path
+ from jarvis.core.config import (
+ DEFAULT_CONFIG_FILE as _DEFAULT,
+ )
+ from jarvis.core.config import (
+ resolve_config_path,
+ )
if path == _DEFAULT:
path = resolve_config_path()
@@ -1494,8 +1502,7 @@ def _atomic_write(path: Path, content: str) -> None:
verweigert``. We restore the flag in ``finally`` so the defense holds
even if the write itself raises.
"""
- tmp = path.with_suffix(path.suffix + ".tmp")
- tmp.write_text(content, encoding="utf-8")
+ tmp = _write_unique_temp(path, content)
was_read_only = False
if path.exists():
@@ -1505,13 +1512,65 @@ def _atomic_write(path: Path, content: str) -> None:
os.chmod(path, mode | stat.S_IWRITE)
try:
- tmp.replace(path)
+ _replace_with_retry(tmp, path, ensure_target_writable=True)
finally:
+ tmp.unlink(missing_ok=True)
if was_read_only and path.exists():
current_mode = path.stat().st_mode
os.chmod(path, current_mode & ~stat.S_IWRITE)
+def _write_unique_temp(path: Path, content: str) -> Path:
+ """Write and flush a unique sibling tempfile for an atomic replacement.
+
+ A fixed ``jarvis.toml.tmp`` name is unsafe across processes: the desktop,
+ drift guard, and another CLI can overwrite or replace the same tempfile.
+ A unique file in the target directory preserves same-filesystem atomicity.
+ """
+ with tempfile.NamedTemporaryFile(
+ mode="w",
+ encoding="utf-8",
+ newline="",
+ prefix=f".{path.name}.",
+ suffix=".tmp",
+ dir=path.parent,
+ delete=False,
+ ) as handle:
+ handle.write(content)
+ handle.flush()
+ os.fsync(handle.fileno())
+ return Path(handle.name)
+
+
+def _replace_with_retry(
+ tmp: Path,
+ path: Path,
+ *,
+ ensure_target_writable: bool,
+) -> None:
+ """Replace ``path`` atomically, tolerating short-lived sharing locks.
+
+ Windows antivirus, indexers, and concurrent config readers can hold the
+ destination briefly and surface ``PermissionError``/WinError 5 or 32. The
+ bounded retry is also safe on POSIX; other error classes still fail fast.
+ """
+ last_error: PermissionError | None = None
+ for delay_s in _ATOMIC_REPLACE_RETRY_DELAYS_S:
+ if delay_s:
+ time.sleep(delay_s)
+ if ensure_target_writable and path.exists():
+ current_mode = path.stat().st_mode
+ if not current_mode & stat.S_IWRITE:
+ os.chmod(path, current_mode | stat.S_IWRITE)
+ try:
+ os.replace(tmp, path)
+ return
+ except PermissionError as exc:
+ last_error = exc
+ if last_error is not None:
+ raise last_error
+
+
# ----------------------------------------------------------------------
# Layer 2 + 3 — config-soll.json + ENV sync (best-effort, cloud-first safe) # i18n-allow
# ----------------------------------------------------------------------
@@ -1585,7 +1644,7 @@ def _sync_computer_use_provider_drift_soll(name: str) -> None: # i18n-allow
ENV.
NEVER raises and NEVER breaks the (already-completed) TOML write. Same
- two-step shape as :func:`_sync_worker_provider_drift_soll`. # i18n-allow: internal config-soll identifier ref
+ two-step shape as :func:`_sync_worker_provider_drift_soll`. # i18n-allow: internal identifier
"""
try:
_update_config_soll_computer_use_provider(name) # i18n-allow
@@ -1718,7 +1777,10 @@ def _update_config_soll_section(top: str, values: dict[str, object]) -> None: #
"""
soll_path = _config_soll_path() # i18n-allow
if not soll_path.exists(): # i18n-allow
- log.debug("config-soll.json absent (%s) — skipping drift-soll sync", soll_path) # i18n-allow
+ log.debug(
+ "config-soll.json absent (%s) — skipping drift-soll sync", # i18n-allow
+ soll_path, # i18n-allow: internal config-soll identifier
+ )
return
with _WRITE_LOCK:
@@ -1745,7 +1807,10 @@ def _update_config_soll_brain_primary(name: str) -> None: # i18n-allow
"""
soll_path = _config_soll_path() # i18n-allow
if not soll_path.exists(): # i18n-allow
- log.debug("config-soll.json absent (%s) — skipping drift-soll sync", soll_path) # i18n-allow
+ log.debug(
+ "config-soll.json absent (%s) — skipping drift-soll sync", # i18n-allow
+ soll_path, # i18n-allow: internal config-soll identifier
+ )
return
with _WRITE_LOCK:
@@ -1777,7 +1842,10 @@ def _update_config_soll_worker_provider(name: str) -> None: # i18n-allow
"""
soll_path = _config_soll_path() # i18n-allow
if not soll_path.exists(): # i18n-allow
- log.debug("config-soll.json absent (%s) — skipping drift-soll sync", soll_path) # i18n-allow
+ log.debug(
+ "config-soll.json absent (%s) — skipping drift-soll sync", # i18n-allow
+ soll_path, # i18n-allow: internal config-soll identifier
+ )
return
with _WRITE_LOCK:
@@ -1800,8 +1868,9 @@ def _update_config_soll_computer_use_provider(name: str) -> None: # i18n-allow
config-soll.json. # i18n-allow
Note the FLAT dotted key ``"brain.computer_use"`` — same layout as
- ``"brain.worker"`` (see :func:`_update_config_soll_worker_provider`), NOT # i18n-allow: internal config-soll identifier ref
- a nested ``data["brain"]["computer_use"]``. Preserves all other keys.
+ ``"brain.worker"`` (see :func:`_update_config_soll_worker_provider`), # i18n-allow
+ not a nested table. # i18n-allow: internal config-soll identifier reference
+ Preserves all other keys.
Graceful no-op when the file is absent.
"""
soll_path = _config_soll_path() # i18n-allow
@@ -1836,7 +1905,10 @@ def _update_config_soll_worker_key(key: str, value: str) -> None: # i18n-allow
"""
soll_path = _config_soll_path() # i18n-allow
if not soll_path.exists(): # i18n-allow
- log.debug("config-soll.json absent (%s) — skipping drift-soll sync", soll_path) # i18n-allow
+ log.debug(
+ "config-soll.json absent (%s) — skipping drift-soll sync", # i18n-allow
+ soll_path, # i18n-allow: internal config-soll identifier
+ )
return
with _WRITE_LOCK:
@@ -1860,9 +1932,11 @@ def _atomic_write_text(path: Path, content: str) -> None:
Used for config-soll.json, which — unlike jarvis.toml — does not carry the # i18n-allow
BUG-010 read-only defense flag.
"""
- tmp = path.with_suffix(path.suffix + ".tmp")
- tmp.write_text(content, encoding="utf-8")
- tmp.replace(path)
+ tmp = _write_unique_temp(path, content)
+ try:
+ _replace_with_retry(tmp, path, ensure_target_writable=False)
+ finally:
+ tmp.unlink(missing_ok=True)
def _set_user_env_var(name: str, value: str) -> None:
diff --git a/tests/unit/test_config_writer.py b/tests/unit/test_config_writer.py
index d544b333b7..720d7d5770 100644
--- a/tests/unit/test_config_writer.py
+++ b/tests/unit/test_config_writer.py
@@ -146,7 +146,6 @@ def test_atomic_write_succeeds_on_read_only_target(sample_toml: Path) -> None:
it afterwards. Without this the User sees WinError 5 in the UI toast
and the provider switch is silently lost.
"""
- import os
import stat
# Mark the file read-only (Windows: clear S_IWRITE) before the patch.
@@ -167,3 +166,34 @@ def test_atomic_write_succeeds_on_read_only_target(sample_toml: Path) -> None:
# Restore writability so tmp_path cleanup can delete the file.
sample_toml.chmod(sample_toml.stat().st_mode | stat.S_IWRITE)
+
+
+def test_atomic_write_retries_transient_replace_denial(
+ sample_toml: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ """A short Windows sharing lock must not lose an in-app provider switch."""
+ import os
+
+ real_replace = os.replace
+ attempted_sources: list[Path] = []
+
+ def flaky_replace(
+ source: str | bytes | Path, destination: str | bytes | Path
+ ) -> None:
+ attempted_sources.append(Path(source))
+ if len(attempted_sources) < 3:
+ raise PermissionError(5, "simulated transient sharing lock")
+ real_replace(source, destination)
+
+ monkeypatch.setattr(config_writer.os, "replace", flaky_replace)
+ monkeypatch.setattr(config_writer.time, "sleep", lambda _seconds: None)
+
+ config_writer.set_voice_mode("realtime", path=sample_toml)
+
+ text = sample_toml.read_text(encoding="utf-8")
+ assert "[voice]" in text
+ assert 'mode = "realtime"' in text
+ assert len(attempted_sources) == 3
+ assert len(set(attempted_sources)) == 1
+ assert attempted_sources[0].name.startswith(f".{sample_toml.name}.")
+ assert list(sample_toml.parent.glob("*.tmp")) == []