"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(session): add atomic file locking to prevent race conditions in all harnesses
ZJZAC2f44c85f7c69
diff --git a/anygen/agent-harness/cli_anything/anygen/core/session.py b/anygen/agent-harness/cli_anything/anygen/core/session.pyindex 1fcb843b7..8622b64e4 100644--- a/anygen/agent-harness/cli_anything/anygen/core/session.py+++ b/anygen/agent-harness/cli_anything/anygen/core/session.py@@ -1,11 +1,38 @@"""Session management — undo/redo and command history for AnyGen CLI."""import json+import osfrom dataclasses import dataclass, fieldfrom datetime import datetime, timezonefrom pathlib import Path+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++@dataclassclass HistoryEntry:command: str@@ -101,9 +128,7 @@ class Session:"history": [e.to_dict() for e in self._history],"redo_stack": [e.to_dict() for e in self._redo_stack],}- Path(path).parent.mkdir(parents=True, exist_ok=True)- with open(path, "w") as f:- json.dump(data, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(path, data, indent=2, sort_keys=True, default=str)def _load(self, path: str):p = Path(path)diff --git a/audacity/agent-harness/cli_anything/audacity/core/session.py b/audacity/agent-harness/cli_anything/audacity/core/session.pyindex af108cee5..b13f2f3cd 100644--- a/audacity/agent-harness/cli_anything/audacity/core/session.py+++ b/audacity/agent-harness/cli_anything/audacity/core/session.py@@ -8,6 +8,32 @@ import jsonimport osimport copyfrom typing import Dict, Any, Optional, List+++def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)from datetime import datetime@@ -119,9 +145,7 @@ class Session:# Save projectself.project["metadata"]["modified"] = datetime.now().isoformat()- os.makedirs(os.path.dirname(os.path.abspath(save_path)), exist_ok=True)- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/blender/agent-harness/cli_anything/blender/core/session.py b/blender/agent-harness/cli_anything/blender/core/session.pyindex 0359cdef1..67ccea2b8 100644--- a/blender/agent-harness/cli_anything/blender/core/session.py+++ b/blender/agent-harness/cli_anything/blender/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages project state with undo/redo history."""@@ -111,8 +137,7 @@ class Session:# Save projectself.project["metadata"]["modified"] = datetime.now().isoformat()- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/cli-anything-plugin/HARNESS.md b/cli-anything-plugin/HARNESS.mdindex 9b16737ef..155f91e8b 100644--- a/cli-anything-plugin/HARNESS.md+++ b/cli-anything-plugin/HARNESS.md@@ -67,6 +67,36 @@ designed for humans, without needing a display or mouse.5. **Add rendering/export** — The export pipeline calls the backend module.Generate valid intermediate files, then invoke the real software for conversion.6. **Add session management** — State persistence, undo/redo++ **Session file locking** — When saving session JSON, use exclusive file locking+ to prevent concurrent writes from corrupting data. Never use bare+ `open("w") + json.dump()` — `open("w")` truncates the file before any lock+ can be acquired. Instead, open with `"r+"`, lock, then truncate inside the lock:+ ```python+ def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+") # no truncation on open+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w") # first save — file doesn't exist yet+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass # Windows / unsupported FS — proceed unlocked+ try:+ f.seek(0)+ f.truncate() # truncate INSIDE the lock+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)+ ```7. **Add the REPL with unified skin** — Interactive mode wrapping the subcommands.- Copy `repl_skin.py` from the plugin (`cli-anything-plugin/repl_skin.py`) into`utils/repl_skin.py` in your CLI packagediff --git a/drawio/agent-harness/cli_anything/drawio/core/session.py b/drawio/agent-harness/cli_anything/drawio/core/session.pyindex 9a9d05620..722beaebb 100644--- a/drawio/agent-harness/cli_anything/drawio/core/session.py+++ b/drawio/agent-harness/cli_anything/drawio/core/session.py@@ -14,6 +14,33 @@ from xml.etree import ElementTree as ETfrom ..utils import drawio_xml+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ path = str(path)+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++SESSION_DIR = Path.home() / ".drawio-cli" / "sessions"MAX_UNDO_DEPTH = 50@@ -126,8 +153,7 @@ class Session:"timestamp": time.time(),}path = SESSION_DIR / f"{self.session_id}.json"- with open(path, "w") as f:- json.dump(state, f, indent=2, sort_keys=True)+ _locked_save_json(path, state, indent=2, sort_keys=True)return str(path)@classmethoddiff --git a/gimp/agent-harness/cli_anything/gimp/core/session.py b/gimp/agent-harness/cli_anything/gimp/core/session.pyindex 233834d1d..dc97c13e0 100644--- a/gimp/agent-harness/cli_anything/gimp/core/session.py+++ b/gimp/agent-harness/cli_anything/gimp/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages project state with undo/redo history."""@@ -111,8 +137,7 @@ class Session:# Save projectself.project["metadata"]["modified"] = datetime.now().isoformat()- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/gimp/agent-harness/cli_anything/gimp/tests/test_core.py b/gimp/agent-harness/cli_anything/gimp/tests/test_core.pyindex 4d9f56c5e..86dd0643a 100644--- a/gimp/agent-harness/cli_anything/gimp/tests/test_core.py+++ b/gimp/agent-harness/cli_anything/gimp/tests/test_core.py@@ -476,3 +476,76 @@ class TestSession:for i in range(10):sess.snapshot(f"action {i}")assert len(sess._undo_stack) == 5+++# ── Concurrent Save Tests ───────────────────────────────────────++class TestLockedSaveJson:+ """Tests for _locked_save_json atomic file writes."""++ def test_basic_save(self, tmp_path):+ from cli_anything.gimp.core.session import _locked_save_json+ path = str(tmp_path / "test.json")+ _locked_save_json(path, {"key": "value"}, indent=2)+ with open(path) as f:+ data = json.load(f)+ assert data == {"key": "value"}++ def test_overwrite_existing(self, tmp_path):+ from cli_anything.gimp.core.session import _locked_save_json+ path = str(tmp_path / "test.json")+ _locked_save_json(path, {"version": 1}, indent=2)+ _locked_save_json(path, {"version": 2}, indent=2)+ with open(path) as f:+ data = json.load(f)+ assert data == {"version": 2}++ def test_overwrite_shorter_data(self, tmp_path):+ """Ensure truncation works — shorter data doesn't leave old bytes."""+ from cli_anything.gimp.core.session import _locked_save_json+ path = str(tmp_path / "test.json")+ _locked_save_json(path, {"key": "a" * 1000}, indent=2)+ _locked_save_json(path, {"k": 1}, indent=2)+ with open(path) as f:+ data = json.load(f)+ assert data == {"k": 1}++ def test_creates_parent_dirs(self, tmp_path):+ from cli_anything.gimp.core.session import _locked_save_json+ path = str(tmp_path / "nested" / "dir" / "test.json")+ _locked_save_json(path, {"nested": True})+ with open(path) as f:+ data = json.load(f)+ assert data == {"nested": True}++ def test_concurrent_writes_produce_valid_json(self, tmp_path):+ """Multiple threads writing to the same file should not corrupt it."""+ from cli_anything.gimp.core.session import _locked_save_json+ import threading++ path = str(tmp_path / "concurrent.json")+ errors = []++ def writer(thread_id):+ try:+ for i in range(50):+ _locked_save_json(+ path,+ {"thread": thread_id, "iteration": i},+ indent=2, sort_keys=True,+ )+ except Exception as e:+ errors.append(e)++ threads = [threading.Thread(target=writer, args=(t,)) for t in range(4)]+ for t in threads:+ t.start()+ for t in threads:+ t.join()++ assert not errors, f"Errors during concurrent writes: {errors}"+ # Final file must be valid JSON+ with open(path) as f:+ data = json.load(f)+ assert "thread" in data+ assert "iteration" in datadiff --git a/inkscape/agent-harness/cli_anything/inkscape/core/session.py b/inkscape/agent-harness/cli_anything/inkscape/core/session.pyindex 3df553e79..2db41a8d4 100644--- a/inkscape/agent-harness/cli_anything/inkscape/core/session.py+++ b/inkscape/agent-harness/cli_anything/inkscape/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages project state with undo/redo history."""@@ -110,9 +136,7 @@ class Session:raise ValueError("No save path specified.")self.project["metadata"]["modified"] = datetime.now().isoformat()- os.makedirs(os.path.dirname(os.path.abspath(save_path)), exist_ok=True)- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/kdenlive/agent-harness/cli_anything/kdenlive/core/session.py b/kdenlive/agent-harness/cli_anything/kdenlive/core/session.pyindex 13b1a668b..cf532bc3d 100644--- a/kdenlive/agent-harness/cli_anything/kdenlive/core/session.py+++ b/kdenlive/agent-harness/cli_anything/kdenlive/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages project state with undo/redo history."""@@ -106,11 +132,7 @@ class Session:raise ValueError("No save path specified.")self.project["metadata"]["modified"] = datetime.now().isoformat()- parent = os.path.dirname(os.path.abspath(save_path))- if parent:- os.makedirs(parent, exist_ok=True)- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/libreoffice/agent-harness/cli_anything/libreoffice/core/session.py b/libreoffice/agent-harness/cli_anything/libreoffice/core/session.pyindex 91a29db3f..2f2f5ce34 100644--- a/libreoffice/agent-harness/cli_anything/libreoffice/core/session.py+++ b/libreoffice/agent-harness/cli_anything/libreoffice/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages document project state with undo/redo history."""@@ -117,9 +143,7 @@ class Session:raise ValueError("No save path specified.")self.project["metadata"]["modified"] = datetime.now().isoformat()- os.makedirs(os.path.dirname(os.path.abspath(save_path)), exist_ok=True)- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/obs-studio/agent-harness/cli_anything/obs_studio/core/session.py b/obs-studio/agent-harness/cli_anything/obs_studio/core/session.pyindex 089f33f4e..e5275b567 100644--- a/obs-studio/agent-harness/cli_anything/obs_studio/core/session.py+++ b/obs-studio/agent-harness/cli_anything/obs_studio/core/session.py@@ -7,6 +7,32 @@ from typing import Dict, Any, Optional, Listfrom datetime import datetime+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++class Session:"""Manages project state with undo/redo history."""@@ -106,9 +132,7 @@ class Session:raise ValueError("No save path specified.")self.project["metadata"]["modified"] = datetime.now().isoformat()- os.makedirs(os.path.dirname(os.path.abspath(save_path)), exist_ok=True)- with open(save_path, "w") as f:- json.dump(self.project, f, indent=2, sort_keys=True, default=str)+ _locked_save_json(save_path, self.project, indent=2, sort_keys=True, default=str)self.project_path = save_pathself._modified = Falsediff --git a/shotcut/agent-harness/cli_anything/shotcut/core/session.py b/shotcut/agent-harness/cli_anything/shotcut/core/session.pyindex 7341c81f3..21fa977fa 100644--- a/shotcut/agent-harness/cli_anything/shotcut/core/session.py+++ b/shotcut/agent-harness/cli_anything/shotcut/core/session.py@@ -15,6 +15,33 @@ from lxml import etreefrom ..utils import mlt_xml+def _locked_save_json(path, data, **dump_kwargs) -> None:+ """Atomically write JSON with exclusive file locking."""+ path = str(path)+ try:+ f = open(path, "r+")+ except FileNotFoundError:+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)+ f = open(path, "w")+ with f:+ _locked = False+ try:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_EX)+ _locked = True+ except (ImportError, OSError):+ pass+ try:+ f.seek(0)+ f.truncate()+ json.dump(data, f, **dump_kwargs)+ f.flush()+ finally:+ if _locked:+ import fcntl+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)++SESSION_DIR = Path.home() / ".shotcut-cli" / "sessions"MAX_UNDO_DEPTH = 50@@ -154,8 +181,7 @@ class Session:"timestamp": time.time(),}path = SESSION_DIR / f"{self.session_id}.json"- with open(path, "w") as f:- json.dump(state, f, indent=2, sort_keys=True)+ _locked_save_json(path, state, indent=2, sort_keys=True)return str(path)@classmethod