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(credentials): read through portable fallback store

Ruben Lütkef9976bc87da5
diff --git a/jarvis/core/config.py b/jarvis/core/config.py
index fbc704840c..027ba6c920 100644
--- a/jarvis/core/config.py
+++ b/jarvis/core/config.py
@@ -2523,7 +2523,13 @@ def _ensure_keyring_backend() -> None:
def get_secret(key: str, env_fallback: str | None = None) -> str | None:
- """Retrieve a secret value. Priority: keyring → ENV fallback → .env.
+ """Retrieve a secret value from every portable credential source.
+
+ Priority is OS keyring → ENV → ``.env`` → local-file fallback.
+ The final read-through is required even when a nominally working OS
+ keyring returns ``None``: a previous in-app save may have fallen back to
+ ``data/credentials.json`` while that keyring was temporarily unavailable.
+ Without the read-through the saved key becomes invisible after restart.
Args:
key: Secret name in the Credential Manager (e.g. "anthropic_api_key").
@@ -2563,7 +2569,7 @@ def get_secret(key: str, env_fallback: str | None = None) -> str | None:
if env_fallback and (val := os.environ.get(env_fallback)):
return val
- # Last fallback: .env file (dev only)
+ # Development fallback: .env file.
env_file = PROJECT_ROOT / ".env"
if env_file.exists() and env_fallback:
for line in env_file.read_text(encoding="utf-8").splitlines():
@@ -2574,6 +2580,16 @@ def get_secret(key: str, env_fallback: str | None = None) -> str | None:
if k.strip() == env_fallback:
return v.strip().strip('"').strip("'")
+ # Portable read-through. Do not replace a healthy OS backend merely because
+ # it returned no value: consult the file store directly so a key written
+ # during an earlier keyring outage remains recoverable after restart.
+ try:
+ val = _FileCredStore().get(KEYRING_SERVICE, key)
+ if val:
+ return val
+ except Exception: # noqa: BLE001, S110 — unreadable fallback is absent
+ pass
+
return None
@@ -2655,6 +2671,16 @@ def set_secret(key: str, value: str) -> bool:
import keyring
keyring.set_password(KEYRING_SERVICE, key, value)
+ # A successful OS-keyring write supersedes any stale file fallback.
+ # When the installed backend IS the file fallback, deleting here would
+ # erase the value that was just written.
+ if not _FILE_BACKEND_ACTIVE:
+ try:
+ store = _FileCredStore()
+ if store.get(KEYRING_SERVICE, key) is not None:
+ store.delete(KEYRING_SERVICE, key)
+ except Exception: # noqa: BLE001, S110 — secure write already succeeded
+ pass
return True
except Exception as exc: # noqa: BLE001
# A reachable-but-unusable OS keyring (e.g. a locked Linux Secret Service)
@@ -2673,13 +2699,19 @@ def set_secret(key: str, value: str) -> bool:
def delete_secret(key: str) -> bool:
- """Remove a secret from the OS keyring (or the headless file fallback)."""
+ """Remove a secret from both the OS keyring and local-file fallback.
+
+ Deleting only the currently active backend can resurrect a stale fallback
+ value on the next process start. The operation is intentionally idempotent:
+ an already-absent key is a successful deletion.
+ """
_ensure_keyring_backend()
+ keyring_ok = False
try:
import keyring
keyring.delete_password(KEYRING_SERVICE, key)
- return True
+ keyring_ok = True
except Exception: # noqa: BLE001
# Locked/unusable OS keyring: degrade to the file store and retry, so a
# file-stored credential is actually removed rather than left stale.
@@ -2688,10 +2720,19 @@ def delete_secret(key: str) -> bool:
import keyring
keyring.delete_password(KEYRING_SERVICE, key)
- return True
- except Exception: # noqa: BLE001
- return False
- return False
+ keyring_ok = True
+ except Exception: # noqa: BLE001, S110
+ pass
+
+ file_ok = False
+ try:
+ store = _FileCredStore()
+ if store.get(KEYRING_SERVICE, key) is not None:
+ store.delete(KEYRING_SERVICE, key)
+ file_ok = True
+ except Exception: # noqa: BLE001, S110
+ pass
+ return keyring_ok or file_ok
# ----------------------------------------------------------------------
diff --git a/tests/unit/core/test_get_secret_env_fallback.py b/tests/unit/core/test_get_secret_env_fallback.py
index 9187d8c5fd..201a74445d 100644
--- a/tests/unit/core/test_get_secret_env_fallback.py
+++ b/tests/unit/core/test_get_secret_env_fallback.py
@@ -36,3 +36,30 @@ def test_absent_everywhere_returns_none(monkeypatch):
monkeypatch.setattr(keyring, "get_password", lambda *a, **k: None)
monkeypatch.delenv("TOTALLY_UNSET_SLOT", raising=False)
assert get_secret("totally_unset_slot") is None
+
+
+def test_file_fallback_is_read_when_working_keyring_returns_none(monkeypatch, tmp_path):
+ """A prior fallback save remains visible after the OS keyring recovers."""
+ from jarvis.core import config
+
+ monkeypatch.setattr(keyring, "get_password", lambda *a, **k: None)
+ monkeypatch.setattr(config, "DATA_DIR", tmp_path)
+ monkeypatch.delenv("OPENAI_API_KEY", raising=False)
+ config._FileCredStore().set(
+ config.KEYRING_SERVICE, "openai_api_key", "file-saved-value"
+ )
+
+ assert get_secret("openai_api_key", "OPENAI_API_KEY") == "file-saved-value"
+
+
+def test_environment_still_precedes_file_fallback(monkeypatch, tmp_path):
+ from jarvis.core import config
+
+ monkeypatch.setattr(keyring, "get_password", lambda *a, **k: None)
+ monkeypatch.setattr(config, "DATA_DIR", tmp_path)
+ monkeypatch.setenv("OPENAI_API_KEY", "environment-value")
+ config._FileCredStore().set(
+ config.KEYRING_SERVICE, "openai_api_key", "file-saved-value"
+ )
+
+ assert get_secret("openai_api_key", "OPENAI_API_KEY") == "environment-value"
diff --git a/tests/unit/core/test_secret_locked_keyring.py b/tests/unit/core/test_secret_locked_keyring.py
index fbf4dcf035..a03a7bfc3b 100644
--- a/tests/unit/core/test_secret_locked_keyring.py
+++ b/tests/unit/core/test_secret_locked_keyring.py
@@ -79,3 +79,18 @@ def test_delete_secret_degrades_to_file_store_on_locked_keyring(locked_os_keyrin
# Old behaviour: keyring.delete_password raises -> returns False, value stays.
assert c.delete_secret("del_prior") is True
assert c.get_secret("del_prior") is None
+
+
+def test_delete_secret_removes_stale_file_value_when_os_delete_succeeds(
+ monkeypatch, tmp_path
+) -> None:
+ """A later process must not resurrect a value from the fallback file."""
+ monkeypatch.setattr(c, "DATA_DIR", tmp_path)
+ monkeypatch.setattr(c, "_KEYRING_BACKEND_READY", True)
+ monkeypatch.setattr(c, "_FILE_BACKEND_ACTIVE", False)
+ monkeypatch.setattr(keyring, "delete_password", lambda *_args: None)
+ store = c._FileCredStore()
+ store.set(c.KEYRING_SERVICE, "stale_key", "stale-value")
+
+ assert c.delete_secret("stale_key") is True
+ assert store.get(c.KEYRING_SERVICE, "stale_key") is None