Skip to content

The agent that grows with you

fix: restore prior device OAuth state if persistence fails

Teknium965f18b02f32
diff --git a/evals/mcp_device_flow.py b/evals/mcp_device_flow.py
index 5b9ea60208d..af151f047cc 100644
--- a/evals/mcp_device_flow.py
+++ b/evals/mcp_device_flow.py
@@ -121,15 +121,38 @@ def run_cli(repo, mode):
oauth.update(client_id="fixture-client", client_secret="fixture-client-secret")
config = {"mcp_servers": {"fixture": {"url": base + "/mcp", "auth": "oauth", "oauth": oauth}}}
(home / "config.yaml").write_text(json.dumps(config))
+ previous = {}
+ if mode == "persistence":
+ token_dir = home / "mcp-tokens"
+ token_dir.mkdir()
+ previous = {"fixture.json": '{"access_token":"old-fixture","token_type":"Bearer"}',
+ "fixture.client.json": '{"client_id":"old-client"}',
+ "fixture.meta.json": '{"issuer":"https://old.example"}'}
+ for filename, value in previous.items():
+ (token_dir / filename).write_text(value)
env = {key: value for key, value in os.environ.items()
if not key.startswith("HERMES_") and not any(part in key for part in ("API_KEY", "TOKEN", "SECRET"))}
env.update(HOME=str(home), HERMES_HOME=str(home), PYTHONPATH=str(repo), PYTHONDONTWRITEBYTECODE="1")
command = ["reauth", "fixture"] if mode == "preregistered" else ["login", "fixture", "--flow", "device"]
- result = subprocess.run([sys.executable, "-m", "hermes_cli.main", "mcp", *command],
+ argv = [sys.executable, "-m", "hermes_cli.main", "mcp", *command]
+ if mode == "persistence":
+ # Inject a filesystem write error after real registration/metadata writes.
+ argv = [sys.executable, "-c", '''
+from tools import mcp_oauth
+write = mcp_oauth._write_json
+def fail_token(path, data):
+ if path.name == "fixture.json":
+ raise OSError("fixture disk failure")
+ return write(path, data)
+mcp_oauth._write_json = fail_token
+from hermes_cli.main import main
+main()
+''', "mcp", *command]
+ result = subprocess.run(argv,
cwd=repo, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=40)
token_path = home / "mcp-tokens" / "fixture.json"
refresh_output = None
- if token_path.exists():
+ if token_path.exists() and not previous:
tokens = json.loads(token_path.read_text())
tokens["expires_at"] = time.time() - 60
token_path.write_text(json.dumps(tokens))
@@ -137,7 +160,8 @@ def run_cli(repo, mode):
cwd=repo, env=env, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=30)
refresh_output = refreshed.stdout + refreshed.stderr
return {"mode": mode, "returncode": result.returncode, "output": result.stdout + result.stderr,
- "token_persisted": token_path.exists(), "refresh_output": refresh_output, "wire": wire}
+ "token_persisted": token_path.exists(), "refresh_output": refresh_output, "wire": wire,
+ "state_preserved": all((home / "mcp-tokens" / k).read_text() == v for k, v in previous.items())}
if __name__ == "__main__":
diff --git a/tests/tools/test_mcp_device_flow.py b/tests/tools/test_mcp_device_flow.py
index 8bbd830d185..031e3bd4dea 100644
--- a/tests/tools/test_mcp_device_flow.py
+++ b/tests/tools/test_mcp_device_flow.py
@@ -24,11 +24,12 @@ def test_device_login_registers_authorizes_and_persists(mode):
assert not any(row["path"] == "/register" for row in result["wire"])
[email protected]("mode", ["denied", "expiry", "unsupported", "issuer", "resource", "malformed"])
[email protected]("mode", ["denied", "expiry", "unsupported", "issuer", "resource", "malformed", "persistence"])
def test_device_login_failure_does_not_persist_or_disclose_credentials(mode):
result = run_cli(Path(__file__).resolve().parents[2], mode)
assert "unrecognized arguments" not in result["output"], result
- assert not result["token_persisted"], result
+ assert result["state_preserved"], result
+ assert result["token_persisted"] == (mode == "persistence"), result
assert "Authentication failed" in result["output"], result
assert "fixture-device-secret" not in result["output"], result
assert "Authenticated" not in result["output"], result
diff --git a/tools/mcp_oauth_device.py b/tools/mcp_oauth_device.py
index d4f9be3e58e..0acb847d182 100644
--- a/tools/mcp_oauth_device.py
+++ b/tools/mcp_oauth_device.py
@@ -187,7 +187,12 @@ async def login_device(name, server_url, oauth_config):
except httpx.HTTPError:
raise RuntimeError("Device OAuth network request failed") from None
# Validate the entire grant before touching disk; reuse the existing scoped store.
- await storage.set_client_info(provider.context.client_info)
- storage.save_oauth_metadata(provider.context.oauth_metadata)
- await storage.set_tokens(tokens)
+ previous = storage.snapshot()
+ try:
+ await storage.set_client_info(provider.context.client_info)
+ storage.save_oauth_metadata(provider.context.oauth_metadata)
+ await storage.set_tokens(tokens)
+ except OSError:
+ storage.restore(previous)
+ raise
get_manager().evict(name)