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.

perf(workspace): answer the Codex trust check without rebuilding the config

Ruben Lütkef976a0abb5d8
diff --git a/jarvis/workspace/trust.py b/jarvis/workspace/trust.py
index cc7b231001..1da09d8200 100644
--- a/jarvis/workspace/trust.py
+++ b/jarvis/workspace/trust.py
@@ -106,13 +106,46 @@ def _trust_claude(repo_root: Path, cfg: Path) -> TrustResult:
return TrustResult("claude", False, "error", str(exc))
+def _already_trusted_by_codex(text: str, native: str) -> bool:
+ """Read-only "is this folder already trusted?" — the fast path.
+
+ Deliberately ``tomllib`` and not ``tomlkit``. Both parse the same file, but
+ tomlkit rebuilds it as an editable document that remembers every comment and
+ every space, which is exactly what the WRITE below needs and pure waste for
+ a lookup. The difference is not academic: on a real config that has
+ accumulated a few hundred projects (71 KB) tomlkit took 4.6 seconds and
+ tomllib 6 milliseconds — and this runs before every workspace opens, so that
+ was five seconds of apparently-frozen UI on a folder that needed no change
+ at all.
+
+ A file this cannot read is not an answer, just an absence: the caller falls
+ through to the slow path, which will report a real failure honestly.
+ """
+ import tomllib
+
+ try:
+ projects = tomllib.loads(text).get("projects")
+ except (ValueError, TypeError):
+ return False
+ if not isinstance(projects, dict):
+ return False
+ entry = projects.get(native)
+ return isinstance(entry, dict) and entry.get("trust_level") == "trusted"
+
+
def _trust_codex(repo_root: Path, cfg: Path) -> TrustResult:
native = str(repo_root)
try:
+ existing_text = cfg.read_text(encoding="utf-8") if cfg.exists() else ""
+ if existing_text and _already_trusted_by_codex(existing_text, native):
+ return TrustResult("codex", True, "noop", "already trusted")
+
+ # Only now, when something actually has to change, is the
+ # formatting-preserving parser worth its cost.
import tomlkit
- if cfg.exists():
- doc = tomlkit.parse(cfg.read_text(encoding="utf-8"))
+ if existing_text:
+ doc = tomlkit.parse(existing_text)
else:
cfg.parent.mkdir(parents=True, exist_ok=True)
doc = tomlkit.document()
diff --git a/tests/unit/workspace/test_trust.py b/tests/unit/workspace/test_trust.py
index 1a4f203b4d..c8f9ed1b46 100644
--- a/tests/unit/workspace/test_trust.py
+++ b/tests/unit/workspace/test_trust.py
@@ -108,6 +108,58 @@ def test_codex_idempotent_noop_second_run(tmp_path: Path) -> None:
assert res2.ok and res2.method == "noop"
+def test_codex_noop_never_touches_the_slow_parser(tmp_path: Path, monkeypatch) -> None:
+ """An already-trusted folder must be answered without rebuilding the file.
+
+ The check runs before every workspace opens, and the formatting-preserving
+ parser needed for WRITING is hundreds of times slower than a plain read — on
+ a real config that had collected a few hundred projects it cost 4.6 seconds
+ per open. Sabotaging that parser is the only way to prove the fast path does
+ not quietly depend on it.
+ """
+ home = tmp_path / "home"
+ home.mkdir()
+ repo = _repo(tmp_path)
+ ensure_trusted(repo, ["codex"], home=home)
+
+ import tomlkit
+
+ def _explode(*_args: object, **_kwargs: object) -> None:
+ raise AssertionError("the read-only path must not parse for editing")
+
+ monkeypatch.setattr(tomlkit, "parse", _explode)
+ [res] = ensure_trusted(repo, ["codex"], home=home)
+ assert res.ok and res.method == "noop"
+
+
+def test_codex_still_trusts_a_new_folder_in_a_populated_config(
+ tmp_path: Path,
+) -> None:
+ """The fast path answers "no" for a folder that is simply not in there yet."""
+ home = tmp_path / "home"
+ home.mkdir()
+ first = _repo(tmp_path)
+ ensure_trusted(first, ["codex"], home=home)
+
+ second = tmp_path / "another"
+ second.mkdir()
+ [res] = ensure_trusted(second, ["codex"], home=home)
+ assert res.ok and res.method == "config"
+ data = tomllib.loads((home / ".codex" / "config.toml").read_text(encoding="utf-8"))
+ assert data["projects"][str(first)]["trust_level"] == "trusted"
+ assert data["projects"][str(second)]["trust_level"] == "trusted"
+
+
+def test_codex_falls_through_when_the_config_is_unreadable(tmp_path: Path) -> None:
+ """Broken TOML is not "already trusted" — it must reach the honest error."""
+ home = tmp_path / "home"
+ (home / ".codex").mkdir(parents=True)
+ (home / ".codex" / "config.toml").write_text("this is not = = toml", encoding="utf-8")
+ [res] = ensure_trusted(_repo(tmp_path), ["codex"], home=home)
+ assert res.method == "error"
+ assert res.ok is False
+
+
def test_test_mode_ignores_real_codex_home_env(tmp_path: Path, monkeypatch) -> None:
# A stray CODEX_HOME must NOT redirect the write away from the tmp home.
monkeypatch.setenv("CODEX_HOME", str(tmp_path / "REAL_DO_NOT_TOUCH"))