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(dictation): retry transient history replacement conflicts

rubenluetke10-beep0d78557048f0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3f2fee4275..442c5677a1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ versioning per [SemVer](https://semver.org/).
- Keep delegated results attached to their originating chat and serialize direct chat sessions.
- Require a separate confirmation before ending a voice conversation.
- Bound Windows clipboard reads so dictation cannot remain blocked on delayed clipboard data.
+- Retry brief file locks when saving dictation history so completed restorations are retained.
- Restore macOS desktop controls and request native permissions at the point of use.
- Defer optional browser setup on headless installations and repair cross-platform lifecycle behavior.
- Strengthen execution, credential storage, refresh and release-admission boundaries.
diff --git a/jarvis/dictation/history.py b/jarvis/dictation/history.py
index 52a470ba65..ec4eb67356 100644
--- a/jarvis/dictation/history.py
+++ b/jarvis/dictation/history.py
@@ -479,6 +479,8 @@ class DictationHistory:
log.debug("dictation statistics write failed", exc_info=True)
def _write(self, entries: list[DictationEntry]) -> None:
+ from jarvis.dictation.stats import _replace_with_retry
+
payload = {"version": 1, "entries": [e.to_storage_dict() for e in entries]}
self._path.parent.mkdir(parents=True, exist_ok=True)
# Atomic tempfile + os.replace: a crash mid-write never leaves a torn
@@ -489,12 +491,14 @@ class DictationHistory:
try:
with os.fdopen(fd, "w", encoding="utf-8") as fh:
json.dump(payload, fh, ensure_ascii=False, indent=2)
- os.replace(tmp_name, self._path)
+ # External readers can briefly deny replacement even under our lock.
+ # Retry only this completed snapshot, not the mutation or its counters.
+ _replace_with_retry(tmp_name, self._path)
except Exception:
try:
os.unlink(tmp_name)
except OSError:
- pass
+ pass # Preserve the original storage error if cleanup also fails.
raise
diff --git a/tests/fakes/fake_atomic_replace.py b/tests/fakes/fake_atomic_replace.py
new file mode 100644
index 0000000000..d98742e0f3
--- /dev/null
+++ b/tests/fakes/fake_atomic_replace.py
@@ -0,0 +1,22 @@
+"""Deterministic transient or permanent failures for one atomic file replacement."""
+
+from __future__ import annotations
+
+import os
+from pathlib import Path
+
+
+class FailingReplace:
+ def __init__(self, target: Path, failures: int, error: OSError | None = None) -> None:
+ self.target = target
+ self.failures = failures
+ self.error = error or PermissionError(13, "Simulated external file lock")
+ self.sources: list[str] = []
+ self._replace = os.replace
+
+ def __call__(self, source: str, destination: Path) -> None:
+ if Path(destination) == self.target:
+ self.sources.append(str(source))
+ if len(self.sources) <= self.failures:
+ raise self.error
+ self._replace(source, destination)
diff --git a/tests/unit/dictation/test_history_atomic_replace.py b/tests/unit/dictation/test_history_atomic_replace.py
new file mode 100644
index 0000000000..ac0136a03d
--- /dev/null
+++ b/tests/unit/dictation/test_history_atomic_replace.py
@@ -0,0 +1,70 @@
+"""History writes retain their atomic snapshot through short external file locks."""
+
+from __future__ import annotations
+
+import errno
+
+import pytest
+
+from jarvis.dictation import stats
+from jarvis.dictation.history import DictationHistory
+from tests.fakes.fake_atomic_replace import FailingReplace
+
+
[email protected]("operation", ["add", "update", "delete", "clear"])
+def test_history_retries_transient_replace_without_repeating_the_mutation(
+ tmp_path, monkeypatch, operation,
+):
+ store = DictationHistory(tmp_path / "history.json")
+ seed = store.add(raw_text="seed", text="seed")
+ assert seed is not None
+ failing = FailingReplace(store.path, failures=2)
+ sleeps = []
+ monkeypatch.setattr(stats.os, "replace", failing)
+ monkeypatch.setattr(stats.time, "sleep", sleeps.append)
+
+ if operation == "add":
+ added = store.add(raw_text="new", text="new")
+ assert added is not None
+ assert [entry.text for entry in store.list_all()] == ["new", "seed"]
+ elif operation == "update":
+ updated = store.update(seed.id, text="restored")
+ assert updated is not None
+ assert [entry.text for entry in store.list_all()] == ["restored"]
+ elif operation == "delete":
+ assert store.delete(seed.id)
+ assert store.list_all() == []
+ else:
+ assert store.clear()
+ assert store.list_all() == []
+
+ assert len(failing.sources) == 3
+ assert len(set(failing.sources)) == 1 # Retry the same completed tempfile.
+ assert 0 < sum(sleeps) <= 1
+ assert not list(tmp_path.glob(".dictation_history_*.tmp"))
+
+
[email protected]("permission_error", [True, False])
+def test_history_write_failure_is_bounded_and_preserves_the_previous_file(
+ tmp_path, monkeypatch, caplog, permission_error,
+):
+ store = DictationHistory(tmp_path / "history.json")
+ seed = store.add(raw_text="seed", text="seed")
+ assert seed is not None
+ original = store.path.read_bytes()
+ error = PermissionError(13, "Locked") if permission_error else OSError(errno.ENOSPC, "Full")
+ failing = FailingReplace(store.path, failures=100, error=error)
+ sleeps = []
+ monkeypatch.setattr(stats.os, "replace", failing)
+ monkeypatch.setattr(stats.time, "sleep", sleeps.append)
+
+ assert store.update(seed.id, text="must not replace the saved version") is None
+ assert store.path.read_bytes() == original
+ assert not list(tmp_path.glob(".dictation_history_*.tmp"))
+ assert "could not update a dictation history entry" in caplog.text
+ if permission_error:
+ assert 2 <= len(failing.sources) <= 10
+ assert 0 < sum(sleeps) <= 1
+ else:
+ assert len(failing.sources) == 1
+ assert sleeps == []