Skip to content

The agent that grows with you

fix(plugins/photon): clear stale token and re-enable channel after setup (#72763)

webtecnicacd158466d1fb
diff --git a/plugins/platforms/photon/auth.py b/plugins/platforms/photon/auth.py
index e1327795a97..066d5e99cdf 100644
--- a/plugins/platforms/photon/auth.py
+++ b/plugins/platforms/photon/auth.py
@@ -180,6 +180,45 @@ def store_photon_token(token: str) -> None:
_save_auth(auth)
+def clear_photon_token() -> None:
+ """Remove any stored Photon dashboard token from auth.json.
+
+ Used to discard a stale/expired token before re-authentication.
+ """
+ auth = _load_auth()
+ pool = auth.get("credential_pool", {})
+ photon = pool.get("photon", [])
+ if isinstance(photon, list) and photon:
+ pool["photon"] = []
+ _save_auth(auth)
+ # Also clear the legacy shape if present.
+ providers = auth.get("providers", {})
+ if "photon" in providers:
+ providers["photon"] = {}
+ _save_auth(auth)
+
+
+def check_photon_token_valid(token: str) -> bool:
+ """Return True if the token is accepted by the dashboard API.
+
+ Makes a lightweight ``GET /api/auth/get-session`` call. A non-401
+ response (including non-auth errors like network blips) is treated as
+ "probably valid" so transient failures don't force unnecessary re-login.
+ Only a definitive 401 / 403 is treated as stale.
+ """
+ if not token:
+ return False
+ try:
+ resp = _dashboard_get("/api/auth/get-session", token)
+ if resp.status_code in (401, 403):
+ return False
+ return True
+ except Exception:
+ # Transient error — don't force a re-auth; let the caller's
+ # management-call error propagate if the token really is bad.
+ return True
+
+
def load_project_credentials() -> Tuple[Optional[str], Optional[str]]:
"""Return the runtime SDK creds ``(spectrum_project_id, project_secret)``.
diff --git a/plugins/platforms/photon/cli.py b/plugins/platforms/photon/cli.py
index 89e1c6bc8bc..af8b3344188 100644
--- a/plugins/platforms/photon/cli.py
+++ b/plugins/platforms/photon/cli.py
@@ -127,10 +127,23 @@ def _run_device_login(args: argparse.Namespace) -> int:
def _cmd_setup(args: argparse.Namespace) -> int:
- # 1. Login (skip if we already have a token).
+ # 1. Login (skip if we already have a valid token).
token = photon_auth.load_photon_token()
+ if token:
+ # Validate the existing token — the dashboard token has a short TTL
+ # and can go stale between runs (observed: ~3-4 days). Reusing a
+ # stale token causes every management call to fail with 401 and
+ # leaves the operator confused about why setup "succeeds" but nothing
+ # works. Check upfront so we fail fast and fall back to fresh login.
+ print("[1/5] Checking existing Photon token...")
+ if photon_auth.check_photon_token_valid(token):
+ print(" ✓ token is valid")
+ else:
+ print(" ✗ token is stale (dashboard rejected it) — re-authenticating")
+ photon_auth.clear_photon_token()
+ token = None
if not token:
- print("[1/5] No Photon token found — running device login...")
+ print("[1/5] No valid Photon token found — running device login...")
rc = _run_device_login(args)
if rc != 0:
return rc
@@ -270,6 +283,17 @@ def _cmd_setup(args: argparse.Namespace) -> int:
if rc != 0:
return rc
+ # 7. Ensure the photon platform is enabled in config.yaml so the
+ # gateway loads it on next start. Without this the channel stays
+ # disabled even after a successful provisioning run, silently
+ # keeping iMessage offline.
+ try:
+ from hermes_cli.config import write_platform_config_field
+ write_platform_config_field("photon", "enabled", True, raw=True)
+ print(" ✓ photon platform enabled in config.yaml")
+ except Exception as e:
+ print(f" (could not enable Photon in config: {e})", file=sys.stderr)
+
print()
print("✓ Photon setup complete.")
print(" Start the gateway: hermes gateway start")