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(agentic-ide): keep scratch folders out of the recent-workspace list

Ruben Lütke907de276b5bd
diff --git a/jarvis/agentic_ide/recents.py b/jarvis/agentic_ide/recents.py
index 94685c0014..8420e931d0 100644
--- a/jarvis/agentic_ide/recents.py
+++ b/jarvis/agentic_ide/recents.py
@@ -11,11 +11,20 @@ and every read is defensive: a truncated or hand-edited file degrades to "no
recents" instead of breaking the wizard. Entries that no longer exist on disk are
dropped on read, so a deleted or unplugged folder disappears by itself rather
than failing at launch.
+
+**Only folders a person actually chose belong here.** Scratch directories are
+filtered out on the way in AND on the way out, because this list is the wizard's
+front page: a user who sees six folders they never opened cannot tell which of
+them they are supposed to trust. That is not hypothetical — an automated run
+against the real store once pushed the one genuine entry to the bottom of the
+list behind its own throwaway folders, so the filter runs on read as well and
+heals a store that was already polluted.
"""
from __future__ import annotations
import json
import os
+import tempfile
import time
from dataclasses import asdict, dataclass, field
from pathlib import Path
@@ -26,6 +35,51 @@ from loguru import logger
MAX_RECENTS = 8
+def _temp_roots() -> list[Path]:
+ """Every directory this machine treats as scratch space.
+
+ ``gettempdir()`` is the authoritative answer on all three OSes, but the
+ environment variables are checked too: a session can be started with a
+ different ``TMPDIR``/``TEMP`` than the one the running process resolved, and
+ a folder under either of them is throwaway all the same.
+ """
+ seen: list[Path] = []
+ candidates = [tempfile.gettempdir(), *(os.environ.get(k) for k in ("TMPDIR", "TEMP", "TMP"))]
+ for raw in candidates:
+ if not raw:
+ continue
+ try:
+ resolved = Path(raw).expanduser().resolve()
+ except (OSError, ValueError):
+ continue
+ if resolved not in seen:
+ seen.append(resolved)
+ return seen
+
+
+def is_throwaway(path: str | Path) -> bool:
+ """True when ``path`` lives in scratch space and must not be remembered.
+
+ A folder under the system temp directory is by definition disposable — a
+ test fixture, an unpacked archive, an editor's scratch checkout. Opening one
+ still works; it simply does not earn a place in the history, because the
+ folder is usually gone before the user looks at the list again.
+ """
+ try:
+ resolved = Path(path).expanduser().resolve()
+ except (OSError, ValueError):
+ return False
+ for root in _temp_roots():
+ if resolved == root:
+ return True
+ try:
+ resolved.relative_to(root)
+ except ValueError:
+ continue
+ return True
+ return False
+
+
@dataclass(slots=True)
class RecentWorkspace:
"""One previously opened workspace."""
@@ -68,6 +122,10 @@ def load(*, verify_exists: bool = True) -> list[RecentWorkspace]:
path = str(item.get("path") or "").strip()
if not path:
continue
+ # Filtered on read as well as on write, so a store polluted before this
+ # rule existed cleans itself up the next time anything is written.
+ if is_throwaway(path):
+ continue
if verify_exists:
try:
if not Path(path).is_dir():
@@ -94,9 +152,17 @@ def load(*, verify_exists: bool = True) -> list[RecentWorkspace]:
def remember(path: str, *, terminals: int, agents: dict[str, int]) -> None:
"""Record (or refresh) one workspace as the most recent.
+ Scratch folders are declined outright — see :func:`is_throwaway`. This is the
+ fail-closed half of the rule: the caller does not have to know which folders
+ deserve a place in the history, and a future caller that forgets cannot
+ pollute the list.
+
Best-effort: a failure to persist must never stop a session from opening, so
problems are logged and swallowed.
"""
+ if is_throwaway(path):
+ logger.debug("Agentic IDE: not remembering throwaway folder {}", path)
+ return
resolved = str(Path(path).expanduser())
entry = RecentWorkspace(
path=resolved,
@@ -137,4 +203,11 @@ def forget(path: str) -> bool:
return False
-__all__ = ["MAX_RECENTS", "RecentWorkspace", "forget", "load", "remember"]
+__all__ = [
+ "MAX_RECENTS",
+ "RecentWorkspace",
+ "forget",
+ "is_throwaway",
+ "load",
+ "remember",
+]
diff --git a/tests/conftest.py b/tests/conftest.py
index a0d1127a9e..85e02cb7fe 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -83,6 +83,38 @@ def _browser_lock_pinned_on():
reset_browser_login_required()
[email protected](autouse=True)
+def _agentic_ide_history_in_tmp(tmp_path_factory, monkeypatch): # noqa: ANN001
+ """Keep every suite away from the developer's real Agentic-IDE history.
+
+ Two small JSON files decide what the workspace picker shows on its front
+ page: the recent-folder list and the resume snapshot. Any test that opens a
+ workspace writes both, and it happened for real — a run left seven pytest
+ temp folders in the recents list, crowding out the one folder the developer
+ had actually opened.
+
+ This lives in the ROOT conftest on purpose. The same redirect existed one
+ directory down, covering the suite that was known to open workspaces, and
+ the leak came in through a different suite that also does. A guarantee that
+ only holds for the tests somebody remembered is not a guarantee.
+
+ The scratch-folder rule is stood down for the same reason it exists.
+ ``recents`` refuses to remember anything under the system temp directory —
+ and pytest hands out ``tmp_path`` from exactly there, so with the rule live
+ every test that opens a workspace in a temporary folder would be recording
+ nothing and asserting against an empty list. Redirecting the STORE is what
+ protects the developer's data; the rule itself is verified against the real
+ temp directory in ``test_recents_scratch_folders.py``.
+ """
+ from jarvis.agentic_ide import recents, resume_store
+
+ root = tmp_path_factory.mktemp("agentic-ide-history")
+ monkeypatch.setattr(recents, "_store_path", lambda: root / "recents.json")
+ monkeypatch.setattr(resume_store, "_store_path", lambda: root / "last_session.json")
+ monkeypatch.setattr(recents, "_temp_roots", list)
+ yield root
+
+
@pytest.fixture(autouse=True)
def _reset_bus():
"""Reset the global default bus before and after each test."""
diff --git a/tests/unit/agentic_ide/conftest.py b/tests/unit/agentic_ide/conftest.py
index 1c5c2a648a..6539dd99f9 100644
--- a/tests/unit/agentic_ide/conftest.py
+++ b/tests/unit/agentic_ide/conftest.py
@@ -9,7 +9,13 @@ guarantee that only holds until somebody writes the next test.
unredirected, a test would write into the developer's real data directory and
leave the app offering to reopen a workspace that pytest invented in a
temporary folder.
-2. **The trust pre-seed.** Opening a workspace marks its folder as trusted in
+2. **The recent-folder history.** Same store, same failure, one page earlier in
+ the wizard — and it happened: a run against the real store filled seven of
+ the eight slots on the picker's front page with pytest folders and pushed the
+ one folder the developer had actually opened to the bottom. ``recents``
+ declines throwaway paths on its own now, but the redirect stays, because a
+ test must not depend on a production filter to keep its hands off real data.
+3. **The trust pre-seed.** Opening a workspace marks its folder as trusted in
Claude Code's and Codex's own config files so no pane stops on a "do you
trust this directory?" dialog. Against a real home that means every test run
permanently added its throwaway folders to the developer's CLI configs. That
diff --git a/tests/unit/agentic_ide/test_recents_scratch_folders.py b/tests/unit/agentic_ide/test_recents_scratch_folders.py
new file mode 100644
index 0000000000..ccf56e903e
--- /dev/null
+++ b/tests/unit/agentic_ide/test_recents_scratch_folders.py
@@ -0,0 +1,138 @@
+"""The recent-folder list holds folders a person chose, and nothing else.
+
+This is the wizard's front page. When it fills up with folders the user never
+opened, the list stops being a shortcut and becomes noise they have to read
+past — and that is not hypothetical: an automated run once left seven scratch
+folders there, pushing the one real project to the bottom.
+
+The rule is enforced on the way in AND on the way out, because a store polluted
+before the rule existed must clean itself up rather than wait for someone to
+notice.
+
+**Note on the fixture.** pytest hands out its temporary directories from inside
+the system temp tree, so a naive test would see its own "project" classified as
+scratch — correctly. The scratch boundary is therefore moved to a controlled
+sub-directory for the tests that need a project on the other side of it, and
+the real boundary is asserted separately.
+"""
+
+from __future__ import annotations
+
+import json
+import tempfile
+from pathlib import Path
+
+import pytest
+
+from jarvis.agentic_ide import recents
+
+# Captured at import, before any fixture can replace it. The root conftest
+# stands the scratch rule down for the whole suite (pytest's own temporary
+# folders live inside the system temp tree, so the rule would otherwise fire on
+# every test that opens a workspace) — the tests below that assert the REAL
+# boundary put the genuine implementation back.
+_REAL_TEMP_ROOTS = recents._temp_roots
+
+
+def real_temp_rule(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(recents, "_temp_roots", _REAL_TEMP_ROOTS)
+
+
+def store(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ target = tmp_path / "recents.json"
+ monkeypatch.setattr(recents, "_store_path", lambda: target)
+ return target
+
+
+def scratch(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
+ """A scratch tree, with everything else in ``tmp_path`` outside it."""
+ root = tmp_path / "scratch"
+ root.mkdir()
+ monkeypatch.setattr(recents, "_temp_roots", lambda: [root.resolve()])
+ return root
+
+
+def _folder(parent: Path, name: str) -> Path:
+ made = parent / name
+ made.mkdir(parents=True, exist_ok=True)
+ return made
+
+
+def test_a_scratch_folder_is_never_remembered(tmp_path: Path, store: Path, scratch: Path) -> None:
+ project = _folder(tmp_path, "webshop")
+ throwaway = _folder(scratch, "test_resuming_counts_conversat0")
+
+ recents.remember(str(project), terminals=2, agents={"claude": 2})
+ recents.remember(str(throwaway), terminals=1, agents={"codex": 1})
+
+ assert [r.path for r in recents.load()] == [str(project)]
+
+
+def test_an_already_polluted_store_reads_clean(tmp_path: Path, store: Path, scratch: Path) -> None:
+ """The file on disk is the one yesterday's bug left behind; the list is not."""
+ project = _folder(tmp_path, "webshop")
+ throwaway = _folder(scratch, "test_resuming_reopens_the_work0")
+ store.write_text(
+ json.dumps(
+ [
+ {"path": str(throwaway), "name": throwaway.name, "last_used": 99.0},
+ {"path": str(project), "name": "webshop", "last_used": 1.0},
+ ]
+ ),
+ encoding="utf-8",
+ )
+
+ assert [r.path for r in recents.load()] == [str(project)]
+
+
+def test_the_next_write_removes_the_pollution_from_disk(
+ tmp_path: Path, store: Path, scratch: Path
+) -> None:
+ project = _folder(tmp_path, "webshop")
+ throwaway = _folder(scratch, "test_resuming_starts_no_agent_0")
+ store.write_text(
+ json.dumps([{"path": str(throwaway), "name": throwaway.name, "last_used": 99.0}]),
+ encoding="utf-8",
+ )
+
+ recents.remember(str(project), terminals=1, agents={"claude": 1})
+
+ on_disk = json.loads(store.read_text(encoding="utf-8"))
+ assert [entry["path"] for entry in on_disk] == [str(project)]
+
+
[email protected]("real_temp_rule")
+def test_the_real_system_temp_directory_is_the_boundary(tmp_path: Path) -> None:
+ """Asserted against the machine's actual temp dir, not a stand-in.
+
+ ``tmp_path`` lives inside it, which is precisely why the automated runs
+ polluted the store in the first place.
+ """
+ assert recents.is_throwaway(tempfile.gettempdir()) is True
+ assert recents.is_throwaway(str(tmp_path)) is True
+
+
[email protected]("real_temp_rule")
+def test_the_real_rule_leaves_a_home_directory_alone() -> None:
+ assert recents.is_throwaway(str(Path.home())) is False
+
+
+def test_real_work_is_not_mistaken_for_scratch(scratch: Path) -> None:
+ """The filter must not reach past the temp tree into somebody's projects.
+
+ A folder merely NAMED like scratch space stays remembered — the rule is
+ about where a folder lives, not what it is called.
+ """
+ assert recents.is_throwaway(str(Path.home())) is False
+ assert recents.is_throwaway(str(Path.home() / "projects" / "tmp")) is False
+ assert recents.is_throwaway(str(scratch.parent / "temp-invoices")) is False
+
+
[email protected]("real_temp_rule")
+def test_an_unreadable_path_is_not_treated_as_scratch() -> None:
+ """Refusing to remember is the exception; a malformed entry is not evidence
+ of one, so an unresolvable path falls through to being kept."""
+ assert recents.is_throwaway("\x00not-a-path") is False