Skip to content

The agent that grows with you

fix(honcho): take the refresh locks around install_grant

Erosika7113a6c18dd9
diff --git a/plugins/memory/honcho/oauth.py b/plugins/memory/honcho/oauth.py
index 3b90b9880c7..66fb9e71ac1 100644
--- a/plugins/memory/honcho/oauth.py
+++ b/plugins/memory/honcho/oauth.py
@@ -390,17 +390,19 @@ def install_grant(
) -> OAuthCredential:
"""Apply a fresh OAuth grant (an OAuthTokenResponse dict) to ``path`` for ``host``: deep-merge the
grant's ``config`` into the file root (preserving other hosts and root keys), then write the host's
- ``apiKey`` and ``oauth`` block. ``apply_config=False`` stores tokens only."""
+ ``apiKey`` and ``oauth`` block. ``apply_config=False`` stores tokens only. Runs under the same locks
+ as a refresh so a login cannot interleave with a sibling's read-modify-write of the file."""
now = time.time() if now is None else now
cred = OAuthCredential.from_token_response(grant, now=now, client_id=client_id, token_endpoint=token_endpoint)
- # Strict: a fresh login must not seed its root-merge from a store that exists but could not be read.
- raw = _read_config_strict(path)
- granted_config = grant.get("config")
- if isinstance(granted_config, dict):
- cred.consent_peer_name = granted_config.get("peerName")
- if apply_config:
- _deep_merge(raw, granted_config)
- _persist_credential(path, host, cred, raw)
+ with _refresh_lock, _config_refresh_lock(path):
+ # Strict: a fresh login must not seed its root-merge from a store that exists but could not be read.
+ raw = _read_config_strict(path)
+ granted_config = grant.get("config")
+ if isinstance(granted_config, dict):
+ cred.consent_peer_name = granted_config.get("peerName")
+ if apply_config:
+ _deep_merge(raw, granted_config)
+ _persist_credential(path, host, cred, raw)
return cred
def apply_token_to_client(client: Any, token: str) -> bool:
diff --git a/tests/honcho_plugin/test_oauth.py b/tests/honcho_plugin/test_oauth.py
index e3116111f21..3415412c612 100644
--- a/tests/honcho_plugin/test_oauth.py
+++ b/tests/honcho_plugin/test_oauth.py
@@ -184,6 +184,38 @@ class TestInstallGrant:
client_id="c", token_endpoint="e",
)
+ def test_reads_and_writes_under_the_refresh_locks(self, tmp_path, monkeypatch):
+ path = tmp_path / "honcho.json"
+ _write(path, {"hosts": {"other": _host_block()}})
+ seen = {}
+ real_read, real_persist = oauth._read_config_strict, oauth._persist_credential
+ locked_paths = []
+
+ @oauth.contextmanager
+ def spy_file_lock(p):
+ locked_paths.append(p)
+ yield
+
+ def spy_read(p):
+ seen["read_locked"] = oauth._refresh_lock.locked()
+ return real_read(p)
+
+ def spy_persist(p, host, cred, raw=None):
+ seen["persist_locked"] = oauth._refresh_lock.locked()
+ return real_persist(p, host, cred, raw)
+
+ monkeypatch.setattr(oauth, "_config_refresh_lock", spy_file_lock)
+ monkeypatch.setattr(oauth, "_read_config_strict", spy_read)
+ monkeypatch.setattr(oauth, "_persist_credential", spy_persist)
+ oauth.install_grant(
+ path, "hermes", {"access_token": "hch-at-new", "refresh_token": "hch-rt-new", "expires_in": 60},
+ client_id="c", token_endpoint="e",
+ )
+ assert seen == {"read_locked": True, "persist_locked": True}
+ assert locked_paths == [path]
+ assert not oauth._refresh_lock.locked()
+ assert "other" in json.loads(path.read_text())["hosts"]
+
class TestApplyTokenToClient:
def test_mutates_live_bearer(self):