The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(web): dashboard_auth/web_routers/kanban dedupe + _common helpers (resume — verified partial work)
Tekniumf0f7f55f76a0
diff --git a/hermes_cli/dashboard_auth/base.py b/hermes_cli/dashboard_auth/base.pyindex 02db55f65e3..9559ded817f 100644--- a/hermes_cli/dashboard_auth/base.py+++ b/hermes_cli/dashboard_auth/base.py@@ -8,11 +8,10 @@ from typing import Optional@dataclass(frozen=True)class Session:- """A verified identity. Returned by ``complete_login`` and ``verify_session``.+ """A verified interactive identity (from ``complete_login`` / ``verify_session``).- All fields are mandatory. Providers that don't have a concept of orgs- should set ``org_id`` to an empty string. ``access_token`` and- ``refresh_token`` are opaque to Hermes — provider-specific.+ All fields mandatory; providers without orgs set ``org_id=""``. The tokens+ are opaque to Hermes."""user_id: str@@ -27,25 +26,11 @@ class Session:@dataclass(frozen=True)class TokenPrincipal:- """A verified non-interactive (service-to-service) caller.-- The token analog of :class:`Session`. Where a ``Session`` represents an- interactive human identity behind a session cookie, a ``TokenPrincipal``- represents a machine/service caller that authenticated by presenting a- bearer token in the ``Authorization`` request header on a single- request — no login, no cookie, no refresh.-- Returned by :meth:`DashboardAuthProvider.verify_token` and attached to- ``request.state.token_principal`` by the token-auth middleware seam so a- route handler can see *who* called it.-- Fields:- * ``principal`` — stable identifier for the caller (e.g. the provider- name, a service account id, or an agent id). Opaque to the seam.- * ``provider`` — the ``name`` of the provider that verified the token.- * ``scopes`` — capability strings this principal is authorised for.- Empty tuple means "unscoped" (the provider vouches for the caller but- attaches no capability list); a route MAY enforce a required scope.+ """A verified non-interactive (service-to-service) caller — the token analog+ of :class:`Session`: one bearer token on one request, no login/cookie/refresh.++ ``principal`` is an opaque stable caller id; ``scopes`` empty means+ "unscoped" (a route MAY enforce a required scope)."""principal: str@@ -57,20 +42,11 @@ class TokenPrincipal:class LoginStart:"""First leg of the OAuth round trip.- ``redirect_url`` is the URL the browser must navigate to (e.g. the- Portal's ``/oauth/authorize``). ``cookie_payload`` is a dict of cookie- name → serialised value that the auth route will ``Set-Cookie`` on the- response. Used for PKCE state, CSRF nonces, etc. Cookies set here MUST- be HttpOnly + Secure (when over HTTPS) with a TTL ≤ 10 minutes (the- login lifetime).-- SameSite: use ``Lax`` by default. The one exception is the PKCE state- cookie, which is ``SameSite=None; Secure`` over HTTPS — it is set on- the ``/auth/login`` 302 and has to survive the cross-site redirect- chain back from the IDP, which Chromium drops intermittently under- ``Lax`` (crbug 40508226). Over plain HTTP it stays ``Lax``, since- ``SameSite=None`` requires ``Secure``. See- :func:`hermes_cli.dashboard_auth.cookies.set_pkce_cookie`.+ ``redirect_url`` is where the browser goes (the IDP's authorize endpoint);+ ``cookie_payload`` maps cookie name -> serialised PKCE/CSRF state that the+ auth route sets (HttpOnly, Secure over HTTPS, TTL <= 10 min). The PKCE+ cookie is ``SameSite=None; Secure`` over HTTPS so it survives the cross-site+ redirect chain — see :func:`hermes_cli.dashboard_auth.cookies.set_pkce_cookie`."""redirect_url: str@@ -78,61 +54,35 @@ class LoginStart:class ProviderError(Exception):- """IDP unreachable, network error, or other transient failure.-- Middleware translates this to HTTP 503.- """+ """IDP unreachable / transient failure. Middleware -> HTTP 503."""class InvalidCodeError(Exception):- """The OAuth callback ``code`` / ``state`` failed validation.-- Middleware translates this to HTTP 400.- """+ """OAuth callback ``code``/``state`` failed validation. Middleware -> HTTP 400."""class InvalidCredentialsError(Exception):- """A username/password pair was rejected by a password provider.-- Raised by :meth:`DashboardAuthProvider.complete_password_login`. The- ``/auth/password-login`` route translates this to HTTP 401 with a- deliberately generic detail (never distinguishing "unknown user" from- "wrong password") so the endpoint can't be used as a username oracle.- """+ """Username/password rejected. The route answers a generic 401 (never+ distinguishing unknown user from wrong password — no username oracle)."""class RefreshExpiredError(Exception):- """This provider rejects the refresh token as dead or invalid.-- In a multi-provider deployment this does not prove token ownership, so- middleware may try remaining providers. It clears cookies and forces- re-login only after every reachable provider rejects the token.- """+ """This provider rejects the refresh token. Not proof of ownership in a+ multi-provider deployment, so middleware tries the remaining providers and+ forces re-login only after every reachable one rejects it."""def classify_jwks_lookup_error(exc: BaseException) -> Exception:"""Map a ``PyJWKClient.get_signing_key_from_jwt`` failure to the protocol.- Only a genuine transport failure (the IDP's JWKS endpoint could not be- fetched) is a :class:`ProviderError` — middleware turns that into 503- "auth provider unreachable" so a flaky IDP never forces a logout.-- Everything else means the token itself cannot be verified by this- provider and is an :class:`InvalidCodeError` (``verify_session`` returns- ``None``, the middleware tries the next provider / refresh / 401):-- * ``jwt.DecodeError`` — the bearer is not a JWT at all (an opaque peer- key, a legacy session token, garbage). #94558: hosted agents answered- every non-JWT bearer with a fast 503 ``Auth provider 'nous'- unreachable`` even though Portal was healthy, because "cannot parse"- and "cannot reach" were folded into one branch.- * ``jwt.PyJWKSetError`` — the JWKS was fetched fine but holds no key for- this token's ``kid`` (rotated/foreign key). The provider was reached;- the token is simply not one of ours.-- ``PyJWKClientConnectionError`` is the only ``PyJWKClientError`` subclass- that denotes unreachability; a bare ``PyJWKClientError`` (unexpected- JWKS shape) is kept as a provider fault since the IDP misbehaved.+ Only a genuine transport failure (``PyJWKClientConnectionError``, or an+ unexpected JWKS shape = IDP misbehaving) is a :class:`ProviderError` (503,+ never forces logout). A non-JWT bearer (``DecodeError``), a JWKS with no key+ for this ``kid`` (``PyJWKSetError``) or any other invalid token means the+ token is simply not verifiable by this provider -> :class:`InvalidCodeError`+ (``verify_session`` returns ``None``; next provider / refresh / 401).+ Folding "cannot parse" into "cannot reach" once made every opaque bearer a+ fast 503 against a healthy Portal."""try:import jwt@@ -152,83 +102,37 @@ def classify_jwks_lookup_error(exc: BaseException) -> Exception:class DashboardAuthProvider(ABC):"""Protocol every dashboard-auth provider plugin implements.- Lifecycle:- 1. ``start_login`` — user clicks "Log in with X" on the login page.- Provider returns a redirect URL and any PKCE/CSRF state to stash- in short-lived cookies.- 2. Browser bounces through the OAuth IDP and lands at /auth/callback.- 3. ``complete_login`` — exchange the code + verifier for a Session.- 4. ``verify_session`` — called on every request to validate the- access token in the cookie. Returns ``None`` if the token is- expired or invalid (middleware then triggers refresh or logout).- 5. ``refresh_session`` — called when the access token is near expiry.- Returns a new Session with rotated tokens.- 6. ``revoke_session`` — called on /auth/logout. Best-effort.+ Lifecycle: ``start_login`` (redirect URL + PKCE state for cookies) ->+ IDP -> ``complete_login`` (code + verifier -> Session) ->+ ``verify_session`` on every request -> ``refresh_session`` near expiry ->+ ``revoke_session`` on logout (best-effort, must not raise).Failure semantics:- * ``start_login`` may raise ``ProviderError`` if the IDP is- unreachable.- * ``complete_login`` raises ``InvalidCodeError`` on bad code/state;- ``ProviderError`` if the IDP is unreachable.- * ``verify_session`` returns ``None`` on expiry / unknown token;- raises ``ProviderError`` if the IDP is unreachable. Middleware- treats expiry and unreachable differently (expiry → refresh;- unreachable → 503).- * ``refresh_session`` raises ``RefreshExpiredError`` when the refresh- token is invalid for that provider. Middleware tries the remaining- providers because an opaque foreign token can be indistinguishable- from an expired one; it forces re-login only after every reachable- provider rejects the token. Raises ``ProviderError`` on network- failure; middleware still tries remaining providers, but returns 503- without clearing cookies if none succeeds and any was unavailable.- * ``revoke_session`` is best-effort and must not raise.-- Subclasses MUST set ``name`` (lowercase identifier, stable forever)- and ``display_name`` (user-facing label on the login page).-- Password (non-redirect) providers:- A provider that authenticates with a username + password instead of- an OAuth redirect sets ``supports_password = True`` and implements- ``complete_password_login``. The login page then renders a- credential form (POSTing to ``/auth/password-login``) instead of a- "Log in with X" redirect button. Everything downstream of login —- ``verify_session`` / ``refresh_session`` / ``revoke_session``, the- session cookies, the WS-ticket mint — is identical to the OAuth- path, because a password session is just a :class:`Session` with- provider-minted opaque tokens. The OAuth methods (``start_login`` /- ``complete_login``) remain abstract; a pure-password provider that- will never be reached via the redirect flow may implement them as- stubs that raise ``NotImplementedError``.+ * ``start_login`` / ``complete_login`` raise ``ProviderError`` when the+ IDP is unreachable; ``complete_login`` raises ``InvalidCodeError`` on+ a bad code/state.+ * ``verify_session`` returns ``None`` for expired/unknown tokens and+ raises ``ProviderError`` when unreachable — middleware refreshes on+ the former and answers 503 on the latter.+ * ``refresh_session`` raises ``RefreshExpiredError`` when the token is+ invalid for that provider (an opaque foreign token is+ indistinguishable from an expired one, so middleware tries the rest)+ and ``ProviderError`` on network failure (503 without clearing cookies+ if none succeeds).++ Subclasses MUST set ``name`` (stable lowercase id) and ``display_name``.+ Capability flags: ``supports_password`` (renders a credential form and+ implements ``complete_password_login``; ``start_login``/``complete_login``+ may be ``NotImplementedError`` stubs), ``supports_token`` (implements+ ``verify_token`` for the token-auth seam), ``supports_session`` (False for+ token-only credentials such as drain, which are never offered a login).+ Everything downstream of login is identical for every kind of session."""name: str = ""display_name: str = ""-- # When True, this provider authenticates via username + password- # (``complete_password_login``) rather than (or in addition to) the- # OAuth redirect flow. The login page renders a credential form for- # such providers; the ``/auth/password-login`` route dispatches to- # ``complete_password_login``. OAuth-only providers leave this False- # and are completely unaffected.supports_password: bool = False-- # When True, this provider can verify a non-interactive bearer token- # (``verify_token``) presented on a single request by a service-to-service- # caller — no login, no cookie, no refresh. This is the generic- # API-token capability flag, mirroring ``supports_password``: a route- # opts into token auth (see ``token_auth`` middleware seam) and the- # gate consults every ``supports_token`` provider in turn until one- # recognises the token. OAuth/password providers leave this False and- # are completely unaffected. The drain bearer-secret plugin is the- # first consumer, but the capability is deliberately generic so any- # future machine-credential provider drops in without core changes.supports_token: bool = False-- # When True, this provider does the interactive cookie-session flow (login,- # verify, refresh). The login page, /auth/login, and the gate's- # verify/refresh loops consult only supports_session providers, so a- # token-only credential (e.g. drain) is never offered a login. Mirrors- # supports_token.supports_session: bool = True@abstractmethod@@ -258,24 +162,11 @@ class DashboardAuthProvider(ABC):) -> "Session":"""Verify a username/password pair and mint a :class:`Session`.- Only called when ``supports_password`` is True (the- ``/auth/password-login`` route guards on the flag). The default- raises ``NotImplementedError`` so an OAuth-only provider that- forgets to set the flag fails loudly rather than silently- accepting credentials.-- The returned ``Session`` carries provider-minted opaque- ``access_token`` / ``refresh_token`` exactly like the OAuth path,- so all downstream session handling (cookies, verify, refresh,- ws-tickets, logout) is identical.-- Failure semantics:- * ``InvalidCredentialsError`` — username/password rejected. The- route surfaces a generic 401 (no user-vs-password- distinction). Implementations SHOULD spend constant time on- unknown users (dummy hash verify) to avoid a timing oracle.- * ``ProviderError`` — the backing credential store is- unreachable (LDAP/DB down); the route surfaces 503.+ Only called when ``supports_password`` is True. Raise+ ``InvalidCredentialsError`` on rejection (SHOULD spend constant time+ on unknown users to avoid a timing oracle) and ``ProviderError`` when+ the credential store is unreachable. The default raises so a provider+ that forgets the flag fails loudly rather than accepting credentials."""raise NotImplementedError(f"{type(self).__name__} does not support password login "@@ -286,30 +177,12 @@ class DashboardAuthProvider(ABC):def verify_token(self, *, token: str) -> "Optional[TokenPrincipal]":"""Verify a non-interactive bearer token; return its principal.- The token analog of ``verify_session``. Only consulted when- ``supports_token`` is True. Called by the ``token_auth`` middleware- seam for every request to a token-authable route, in registration- order, until one provider returns a non-None principal.-- Contract (mirrors ``verify_session`` stacking semantics):- * Return a :class:`TokenPrincipal` if this provider recognises and- accepts the token.- * Return ``None`` for a token this provider does NOT recognise —- never raise, so the seam can fall through to the next provider.- A malformed/expired/wrong token is "not recognised" → ``None``.- * Raise ``ProviderError`` ONLY for a genuine backing-store outage- (the provider can neither confirm nor deny). The seam treats this- like ``verify_session``: remember it, keep trying other providers,- and surface 503 only if NO provider accepts the token AND at least- one was unreachable.-- Implementations MUST use a constant-time comparison- (``hmac.compare_digest``) when matching a shared secret so the- endpoint isn't a timing oracle.-- The default raises ``NotImplementedError`` so a provider that sets- ``supports_token`` but forgets to implement this fails loudly rather- than silently accepting every caller.+ Stacking mirrors ``verify_session``: return ``None`` (never raise) for+ a token this provider does not recognise so the seam falls through;+ raise ``ProviderError`` ONLY for a genuine backing-store outage (the+ seam surfaces 503 only if no provider accepts the token). Shared+ secrets MUST be compared with ``hmac.compare_digest``. The default+ raises so a mis-flagged provider fails loudly."""raise NotImplementedError(f"{type(self).__name__} does not support token auth "@@ -320,32 +193,19 @@ class DashboardAuthProvider(ABC):def assert_protocol_compliance(cls: type) -> None:"""Raise ``TypeError`` if ``cls`` doesn't fully implement the provider protocol.- Call this in every provider plugin's unit tests::-- def test_protocol_compliance():- assert_protocol_compliance(MyProvider)-- Returns ``None`` on success so callers can assert it explicitly.+ Call it from every provider plugin's unit tests."""- required_methods = (- "start_login",- "complete_login",- "verify_session",- "refresh_session",- "revoke_session",- )- required_attrs = ("name", "display_name")-- for attr in required_attrs:- val = getattr(cls, attr, "")- if not val:+ for attr in ("name", "display_name"):+ if not getattr(cls, attr, ""):raise TypeError(f"{cls.__name__} missing or empty attribute: {attr!r}")- for method in required_methods:+ for method in (+ "start_login", "complete_login", "verify_session",+ "refresh_session", "revoke_session",+ ):if not callable(getattr(cls, method, None)):raise TypeError(f"{cls.__name__} missing method: {method}")- # Also catch the ABC-not-overridden case.if getattr(cls, "__abstractmethods__", None):raise TypeError(f"{cls.__name__} has unimplemented abstract methods: "diff --git a/hermes_cli/dashboard_auth/cookies.py b/hermes_cli/dashboard_auth/cookies.pyindex 0c26d3ac0a3..439b15347c7 100644--- a/hermes_cli/dashboard_auth/cookies.py+++ b/hermes_cli/dashboard_auth/cookies.py@@ -1,68 +1,28 @@"""Cookie helpers for dashboard auth.-Three cookies in play:- - hermes_session_at: the OAuth access token- (HttpOnly, lifetime = token TTL, ~15 min)- - hermes_session_rt: the OAuth refresh token- (HttpOnly, lifetime = 24h, ROTATING + reuse-detected)- Nous Portal issues a rotating refresh token for the- dashboard auth-code grant (Portal NAS #293 / hermes- #37247). ``set_session_cookies`` writes this cookie- whenever the provider returns a non-empty- ``refresh_token``; the middleware uses it to rotate a- fresh access token transparently on AT expiry. A- provider that omits the refresh token (empty string)- degrades gracefully to access-token-only sessions —- the RT cookie is simply not written.- - hermes_session_pkce: short-lived PKCE state + CSRF nonce + provider- hint (HttpOnly, lifetime = 10 minutes)--The two session cookies are ``SameSite=Lax`` and live under the prefix's-Path. The PKCE cookie is the exception: ``SameSite=None`` over HTTPS,-falling back to ``Lax`` on plain HTTP (where ``SameSite=None`` is invalid-without ``Secure``). It is set on the ``/auth/login`` 302 and must survive-the cross-site redirect chain out to the IDP and back to-``/auth/callback``; Chromium intermittently drops ``Lax`` cookies set on a-302 in such a chain (crbug 40508226), which surfaces as "Missing PKCE-state cookie". ``Secure`` is set ONLY when the dashboard was reached over-HTTPS — detected via the request URL scheme, which honours-``X-Forwarded-Proto`` upstream of Fly's TLS terminator when uvicorn is-configured with ``proxy_headers=True``. Loopback dev traffic is always-HTTP so ``Secure`` would lock the cookies out of the browser.--NOTE: uvicorn only honours ``X-Forwarded-Proto`` from a peer inside its-``forwarded_allow_ips`` (default: ``127.0.0.1``). A TLS terminator that-reaches the dashboard from a non-loopback address — e.g. a reverse proxy-in its own container — is not trusted, so the request still looks like-HTTP here and these cookies are written in their HTTP shape.--Cookie prefix selection (browser hardening per-https://datatracker.ietf.org/doc/html/draft-west-cookie-prefixes):-- * Loopback HTTP — bare name. ``__Host-`` / ``__Secure-`` require- ``Secure``, which is incompatible with HTTP.- * Gated HTTPS, direct deploy (Path=/) — ``__Host-`` prefix. Binds the- cookie to the exact origin (no Domain attribute) — strongest spec- guarantee.- * Gated HTTPS, behind a reverse-proxy prefix (Path=/hermes) —- ``__Secure-`` prefix. ``__Host-`` is disallowed when Path != "/";- ``__Secure-`` keeps the Secure-required hardening without the- Path constraint, and the explicit ``Path=/hermes`` covers- same-origin app isolation.--The setters and readers BOTH consult the active prefix because the-cookie *name* changes — a reader that looked up the bare name when the-setter wrote ``__Secure-hermes_session_at`` would never find the value.--Refresh-token handling:- ``set_session_cookies`` accepts ``refresh_token=""`` (provider omitted- it) and silently skips writing the RT cookie in that case, so a- refresh-token-less provider degrades to access-token-only sessions.- ``clear_session_cookies`` always emits a Max-Age=0 deletion for the RT- cookie on logout / session expiry so a stale cookie from an earlier- deployment gets cleared. The transparent rotation flow ("expired AT +- live RT → rotate server-side, else 401 → /login") lives in- ``middleware._attempt_refresh``.+Cookies (all HttpOnly, ``SameSite=Lax`` unless noted, Path = proxy prefix or /):+ - hermes_session_at access token; Max-Age = token TTL (~15 min)+ - hermes_session_rt rotating refresh token; written only when the+ provider returned one (AT-only sessions degrade+ gracefully), always cleared on logout/expiry+ - hermes_session_provider non-secret routing hint so a refresh token is+ not handed to the wrong provider when several+ auth plugins are enabled+ - hermes_session_pkce PKCE state + CSRF nonce + provider hint, 10 min.+ ``SameSite=None; Secure`` over HTTPS because it is+ set on the /auth/login 302 and must survive the+ cross-site redirect chain out to the IDP and back+ (Chromium drops Lax cookies set on such a 302,+ crbug 40508226); plain HTTP falls back to Lax.+ - hermes_sso_attempt one-shot auto-SSO loop-guard marker (60 s)++``Secure`` is set only when the request arrived over HTTPS+(``request.url.scheme``, which honours ``X-Forwarded-Proto`` only from a peer+in uvicorn's ``forwarded_allow_ips``). Cookie-prefix hardening follows+draft-west-cookie-prefixes: bare name over HTTP; ``__Host-`` on gated HTTPS+with Path=/; ``__Secure-`` behind a proxy prefix (``__Host-`` forbids+Path != /). Setters and readers BOTH resolve the name through+:func:`_resolved_name` — a mismatch silently breaks sessions."""from __future__ import annotations@@ -76,76 +36,36 @@ from urllib.parse import unquotefrom fastapi import Requestfrom fastapi.responses import Response-# Bare cookie names — the request-scoped ``_resolved_name`` helper-# decides whether to prepend ``__Host-`` / ``__Secure-`` based on the-# request's HTTPS + prefix combination.SESSION_AT_COOKIE = "hermes_session_at"SESSION_RT_COOKIE = "hermes_session_rt"-# Provider that minted the session. This non-secret routing hint prevents a-# refresh token from being handed to the wrong provider when several dashboard-# auth plugins are enabled (for example Basic + Nous OAuth).SESSION_PROVIDER_COOKIE = "hermes_session_provider"PKCE_COOKIE = "hermes_session_pkce"-# One-shot loop-guard marker for the auto-SSO redirect (Phase 1,-# cloud-auto-discovery). Set when the gate auto-initiates the portal OAuth-# redirect on an unauthenticated document load; its mere PRESENCE on the next-# unauthenticated load tells the gate "we already bounced once" so a genuinely-# absent portal session degrades to the /login page instead of ping-ponging.-# Carries no secret — it's a boolean breadcrumb — but is set HttpOnly/Lax/Secure-# like the others for consistency. Short TTL so a user who returns later gets a-# fresh silent attempt rather than a permanently-disabled one.SSO_ATTEMPT_COOKIE = "hermes_sso_attempt"-# Possible name variants we may have to read back. Sorted so most-strict-# wins on iteration when both happen to be present (shouldn't happen in-# practice — a single request emits exactly one variant).+# Name variants a reader may have to try; most strict first._NAME_VARIANTS = ("__Host-", "__Secure-", "")-# RT cookie Max-Age. Kept at 30 days as a generous upper bound on the cookie's-# browser lifetime; Portal's actual refresh-token TTL (24h, rotating) is the-# real authority — once the RT itself expires/rotates out, a refresh attempt-# returns 400 → RefreshExpiredError → clean re-login, regardless of how long-# the cookie lingers. (Not tightened to 24h here to avoid coupling the cookie-# lifetime to a server-side TTL that can change independently; revisit if the-# stale-cookie refresh churn ever matters.)+# RT cookie lifetime is a generous browser-side upper bound; the provider's own+# RT TTL is the real authority (an expired RT -> RefreshExpiredError -> re-login)._RT_MAX_AGE = 30 * 24 * 60 * 60_PKCE_MAX_AGE = 10 * 60-# Auto-SSO loop-guard marker TTL. Just long enough to cover one redirect-# round trip to the portal and back (a few seconds in practice); kept at 60s-# so a slow portal hop or a manual back-button still trips the guard, while a-# user returning minutes later gets a fresh silent attempt rather than being-# stuck on /login forever. The marker is also cleared explicitly on a-# successful callback and whenever the gate falls back to /login.+# Long enough for one portal round trip / back-button; short enough that a+# user returning later gets a fresh silent attempt rather than a stuck /login._SSO_ATTEMPT_MAX_AGE = 60def _resolved_name(bare: str, *, use_https: bool, prefix: str) -> str:- """Pick the cookie-prefix variant for the active request shape.-- See module docstring for the prefix selection rules. Mismatch- between setter and reader would silently break sessions, so this- function is the single source of truth for naming.- """+ """Cookie-prefix variant for the request shape (see module docstring)."""if not use_https:return bareif prefix:- # Path != "/" forbids __Host-; fall back to __Secure-.return f"__Secure-{bare}"return f"__Host-{bare}"def _cookie_path(prefix: str) -> str:- """Cookie ``Path`` attribute for the active deploy shape.-- Under ``X-Forwarded-Prefix: /hermes`` we want ``Path=/hermes`` so:- a) the browser sends the cookie back on requests under the prefix- (browsers omit the cookie if request path doesn't start with- Path);- b) the cookie doesn't leak to other apps on the same origin- (``mission-control.tilos.com/billing/...``).-- Direct-deploy (no proxy prefix) gets ``Path=/``.- """+ """``Path=/hermes`` under a proxy prefix (so the browser sends the cookie+ back under the prefix and it does not leak to sibling apps), else ``/``."""return prefix if prefix else "/"@@ -160,6 +80,25 @@ def _common_attrs(*, use_https: bool, prefix: str) -> dict:return attrs+def _pkce_attrs(*, use_https: bool, prefix: str) -> dict:+ """Attributes shared by the PKCE set AND clear paths (a shape mismatch+ means the browser silently keeps the stale cookie)."""+ attrs = _common_attrs(use_https=use_https, prefix=prefix)+ if use_https:+ attrs["samesite"] = "none"+ return attrs+++def _set(response: Response, bare: str, value: str, *, max_age: int,+ use_https: bool, prefix: str, attrs: dict | None = None) -> None:+ response.set_cookie(+ _resolved_name(bare, use_https=use_https, prefix=prefix),+ value,+ max_age=max_age,+ **(attrs if attrs is not None else _common_attrs(use_https=use_https, prefix=prefix)),+ )++def set_session_provider_cookie(response: Response,*,@@ -170,12 +109,8 @@ def set_session_provider_cookie("""Persist the non-secret provider routing hint for token refresh."""if not provider:return- response.set_cookie(- _resolved_name(SESSION_PROVIDER_COOKIE, use_https=use_https, prefix=prefix),- provider,- max_age=_RT_MAX_AGE,- **_common_attrs(use_https=use_https, prefix=prefix),- )+ _set(response, SESSION_PROVIDER_COOKIE, provider, max_age=_RT_MAX_AGE,+ use_https=use_https, prefix=prefix)def set_session_cookies(@@ -188,42 +123,20 @@ def set_session_cookies(prefix: str = "",provider: str = "",) -> None:- """Set the session cookies on the response.+ """Set the session cookies.- ``access_token_expires_in`` is in seconds. Use the provider's reported- TTL for the access token.-- ``refresh_token`` is written as the RT cookie when non-empty. Nous Portal- issues a 24h rotating refresh token (hermes #37247); a provider that- omits it returns ``Session.refresh_token == ""`` and we simply don't- persist the RT cookie — the session then behaves as access-token-only- until the AT expires. No other branch changes between the two cases.-- ``prefix`` is the normalised X-Forwarded-Prefix value (e.g. ``/hermes``)- or ``""`` for a direct deploy. It influences both the cookie name- (``__Host-`` vs ``__Secure-`` vs bare) and the ``Path`` attribute.+ ``access_token_expires_in`` is seconds (the provider's reported TTL). An+ empty ``refresh_token`` means "don't persist the RT cookie" — a literal+ empty cookie would be dead state at best, attack surface at worst.+ ``prefix`` is the normalised X-Forwarded-Prefix or ``""``."""- response.set_cookie(- _resolved_name(SESSION_AT_COOKIE, use_https=use_https, prefix=prefix),- access_token,- max_age=access_token_expires_in,- **_common_attrs(use_https=use_https, prefix=prefix),- )- # Contract v1: empty refresh token means "don't persist RT cookie".- # Keeping a literal empty-value cookie around would be dead state at- # best, attack surface at worst.+ _set(response, SESSION_AT_COOKIE, access_token, max_age=access_token_expires_in,+ use_https=use_https, prefix=prefix)if refresh_token:- response.set_cookie(- _resolved_name(SESSION_RT_COOKIE, use_https=use_https, prefix=prefix),- refresh_token,- max_age=_RT_MAX_AGE,- **_common_attrs(use_https=use_https, prefix=prefix),- )+ _set(response, SESSION_RT_COOKIE, refresh_token, max_age=_RT_MAX_AGE,+ use_https=use_https, prefix=prefix)set_session_provider_cookie(- response,- provider=provider,- use_https=use_https,- prefix=prefix,+ response, provider=provider, use_https=use_https, prefix=prefix,)@@ -237,19 +150,14 @@ def _clear_cookie_variants() -> None:"""Emit Max-Age=0 deletions for every plausible name variant of a cookie.- Cookie-prefix rules make the deletion shape load-bearing: a Set-Cookie- for a ``__Host-``/``__Secure-`` name is rejected outright by the- browser unless it carries ``Secure`` (and ``__Host-`` additionally- requires ``Path=/``), so those deletions always carry the attributes- their name demands. The bare-name deletion mirrors the shape the- setter uses (``bare_attrs``) — under RFC 6265bis a deletion sent from- a secure origin may omit ``Secure`` and still delete a Secure cookie,- while a ``Secure`` deletion on a plain-HTTP origin can be ignored, so- matching the setter is the shape that works on both origins.+ We don't know which variant the setter used (it depends on the setting+ request's shape). Prefixed names are rejected by the browser unless they+ carry ``Secure`` (``__Host-`` additionally needs ``Path=/``), so those+ deletions always carry them; the bare deletion mirrors the setter's shape+ (``bare_attrs``), which works on both HTTP and HTTPS origins."""for variant in _NAME_VARIANTS:if variant == "__Host-":- # __Host- demands Secure AND Path=/ or the header is invalid.response.set_cookie(f"{variant}{bare_name}", "", max_age=0,path="/", httponly=True, samesite=https_samesite,@@ -267,18 +175,13 @@ def _clear_cookie_variants()-def clear_session_cookies(response: Response, *, prefix: str = "") -> None:- """Emit Max-Age=0 deletions for both session cookies.+def _lax_bare_attrs(prefix: str) -> dict:+ return {"path": _cookie_path(prefix), "httponly": True, "samesite": "lax"}- To delete a cookie reliably the deletion's ``Path`` must match the- set path AND the cookie name must match the variant the setter used.- We don't know which variant was originally set (cookie prefix- depends on the request that set it), so we emit deletions for every- plausible variant under the active path.- """- bare_attrs = {- "path": _cookie_path(prefix), "httponly": True, "samesite": "lax",- }++def clear_session_cookies(response: Response, *, prefix: str = "") -> None:+ """Delete the AT, RT and provider cookies (every name variant, active path)."""+ bare_attrs = _lax_bare_attrs(prefix)for name in (SESSION_AT_COOKIE, SESSION_RT_COOKIE, SESSION_PROVIDER_COOKIE):_clear_cookie_variants(response, name,@@ -286,33 +189,14 @@ def clear_session_cookies(response: Response, *, prefix: str = "") -> None:)-def _pkce_attrs(*, use_https: bool, prefix: str) -> dict:- """Cookie attributes for the PKCE cookie's set AND clear paths.-- Single source of truth so a deletion always matches the shape the- setter emitted for the same origin — a shape mismatch means the- browser silently keeps the stale cookie.- """- attrs = _common_attrs(use_https=use_https, prefix=prefix)- if use_https:- attrs["samesite"] = "none"- return attrs--def encode_pkce_payload(parts: dict[str, str]) -> str:- """Serialise PKCE segments to the wire value: ``base64url(JSON)``.-- The urlsafe base64 alphabet (``A-Za-z0-9-_``, padding stripped) is a- strict subset of the RFC 6265 cookie-octet set — no ``;`` (attribute- terminator), no ``"`` and no ``\\`` (the chars that make Python's- http.cookies emit the quoted ``\\073`` form, which strict cookie-aware- proxy hops such as Go's net/http reject outright). The ``=`` padding- is stripped because http.cookies treats ``=`` as outside its legal- unquoted set and would re-wrap the value in the quoted form this- codec exists to avoid; the parser restores the padding. JSON carries- the segments, so no delimiter can ever collide with segment values —- the delimiter/quoting bug class this codec replaces (see- :func:`parse_pkce_payload` for the two legacy formats it superseded).+ """Serialise PKCE segments to the wire value ``base64url(JSON)``, no padding.++ The urlsafe alphabet is a strict subset of RFC 6265 cookie-octets (no+ ``;``, ``"``, ``\\``), so http.cookies never quotes it (strict proxy hops+ such as Go net/http reject the quoted form) and no segment value can+ collide with a delimiter. Padding ``=`` would trigger quoting; the parser+ restores it."""raw = json.dumps(parts, separators=(",", ":"), sort_keys=True)return (@@ -329,47 +213,18 @@ def set_pkce_cookie(use_https: bool,prefix: str = "",) -> None:- # SameSite=None when HTTPS: the PKCE cookie is set on the /auth/login- # 302 response (redirecting to the IDP) and must survive the cross-site- # redirect chain (same-site → IDP → same-site callback). Chromium has a- # long-standing bug (crbug 40508226) where SameSite=Lax cookies set on a- # 302 in a cross-site redirect chain are intermittently dropped, causing- # "Missing PKCE state cookie" on the callback. SameSite=None + Secure- # sidesteps the bug — these cookies are explicitly designed for cross-site- # delivery and Chromium processes them reliably during redirects.- # Loopback HTTP degrades to Lax (SameSite=None requires Secure).- #- # Value encoding: ``payload`` is the segment dict- # (``{"provider": …, "state": …, "verifier": …, "next": …}``) and goes- # on the wire as base64url(JSON) via encode_pkce_payload() — plain- # RFC 6265 cookie-octets end to end, so every cookie-aware hop- # (browsers, Go net/http proxies, Python parsers) passes the value- # through untouched. Readers decode via parse_pkce_payload(), which- # also keeps a compatibility ladder for cookies minted by the two- # earlier wire formats during a rolling upgrade.- response.set_cookie(- _resolved_name(PKCE_COOKIE, use_https=use_https, prefix=prefix),- encode_pkce_payload(payload),- max_age=_PKCE_MAX_AGE,- **_pkce_attrs(use_https=use_https, prefix=prefix),- )+ """Set the PKCE cookie (``payload`` is the segment dict; see module docstring+ for the SameSite=None rationale)."""+ _set(response, PKCE_COOKIE, encode_pkce_payload(payload), max_age=_PKCE_MAX_AGE,+ use_https=use_https, prefix=prefix,+ attrs=_pkce_attrs(use_https=use_https, prefix=prefix))def clear_pkce_cookie(response: Response, *, use_https: bool, prefix: str = "",) -> None:- """Emit Max-Age=0 deletions for every plausible PKCE cookie variant.-- A deletion is only honoured when its shape is acceptable to the- browser on the current origin: a ``Secure`` deletion can be dropped- on a plain-HTTP origin, while the ``__Host-``/``__Secure-`` name- variants REQUIRE ``Secure`` to be valid at all. So the bare-name- deletion mirrors the setter's shape for the active origin (Lax- without ``Secure`` over HTTP; ``SameSite=None; Secure`` over HTTPS,- matching :func:`set_pkce_cookie`), and the prefixed variants — which- can only ever have been set on an HTTPS origin — always carry- ``Secure; SameSite=None``.- """+ """Delete every PKCE cookie variant; the bare deletion mirrors the setter's+ shape for the active origin, the prefixed ones carry ``Secure; SameSite=None``."""_clear_cookie_variants(response, PKCE_COOKIE,prefix=prefix, https_samesite="none",@@ -380,13 +235,8 @@ def clear_pkce_cookie(def _read_with_fallback(request: Request, bare_name: str,) -> Optional[str]:- """Read a cookie by checking every prefix variant in order.-- The setter chooses one variant based on the active request shape;- the reader doesn't know which one fired (the request that READS- the cookie may not be the same shape as the request that SET it- in pathological cases). Trying all three guarantees we find it.- """+ """Read a cookie trying every prefix variant (the reading request may not+ have the same shape as the one that set it)."""for variant in _NAME_VARIANTS:value = request.cookies.get(f"{variant}{bare_name}")if value is not None:@@ -396,9 +246,10 @@ def _read_with_fallback(def read_session_cookies(request: Request) -> Tuple[Optional[str], Optional[str]]:"""Returns (access_token, refresh_token), either may be None."""- at = _read_with_fallback(request, SESSION_AT_COOKIE)- rt = _read_with_fallback(request, SESSION_RT_COOKIE)- return at, rt+ return (+ _read_with_fallback(request, SESSION_AT_COOKIE),+ _read_with_fallback(request, SESSION_RT_COOKIE),+ )def read_session_provider(request: Request) -> Optional[str]:@@ -410,50 +261,26 @@ def read_pkce_cookie(request: Request) -> Optional[str]:return _read_with_fallback(request, PKCE_COOKIE)-# base64url wire values are exactly the urlsafe alphabet (padding is-# stripped by the encoder; the decoder restores it). Used as a cheap-# pre-filter before attempting the JSON decode so legacy wire forms-# (which always contain ``%`` or ``;``) never even reach the base64-# decoder.+# Cheap pre-filter: legacy wire forms always contain ``%`` or ``;``, which are+# outside the base64url alphabet, so they never reach the base64 decoder._B64URL_RE = re.compile(r"^[A-Za-z0-9_-]+={0,2}$")def parse_pkce_payload(raw: str) -> dict[str, str]:- """Decode + parse a PKCE cookie value into its segment dict.-- Single inverse of :func:`set_pkce_cookie` /- :func:`encode_pkce_payload`. EVERY reader of the PKCE cookie must go- through this helper — a reader that interprets the raw wire value- itself parses zero segments and silently disables whatever check it- was feeding (provider dispatch, CSRF state, native-flow broker- binding).-- Compatibility ladder — the PKCE cookie has a 10-minute TTL and is- opaque + server-set, so during a rolling upgrade a cookie minted by- one server version can arrive at another. Three formats, tried in- order; each rung is unambiguous:-- 1. **base64url(JSON)** (current): the wire value is pure urlsafe- base64 that decodes to a JSON object. Legacy forms can never- match — they always contain ``%`` (URL-encoded, #99176) or a raw- ``;`` (oldest flat form), both outside the base64url alphabet.- 2. **Oldest flat form** (pre-#99176): raw ``;`` between segments- (``provider=…;state=…;verifier=…``). Split as-is WITHOUT- unquoting the payload — the ``next`` segment carries its own- single URL-encoding, and unquoting here would turn a ``%3B``- inside it into a bogus delimiter and truncate the post-login- target. Neither newer format can contain a raw ``;``.- 3. **URL-encoded flat form** (#99176): the whole flat payload passed- through ``quote(payload, safe="")`` — no raw ``;`` possible- (it is ``%3B``); unquote once, then split.-- Rollout directions: OLD cookie → NEW server is handled here (rungs- 2 and 3 parse both legacy forms correctly). NEW cookie → OLD server- (a rollback, or a mixed fleet routing the callback to a not-yet-- upgraded instance) fails the OAuth state check — the old reader- can't find a ``state`` segment in the base64url blob — and the user- simply retries login against the now-consistent fleet; no data loss,- nothing minted.+ """Decode a PKCE cookie value into its segment dict (inverse of+ :func:`encode_pkce_payload`). EVERY reader must go through this — a reader+ that interprets the raw wire value parses zero segments and silently+ disables the check it feeds (provider dispatch, CSRF state, broker binding).++ Compatibility ladder for cookies minted by an older server during a rolling+ upgrade (10-minute TTL), each rung unambiguous:+ 1. base64url(JSON) — current.+ 2. Oldest flat form with raw ``;`` delimiters — split WITHOUT unquoting+ (the ``next`` segment carries its own URL-encoding; unquoting first+ would turn a ``%3B`` inside it into a bogus delimiter).+ 3. URL-encoded flat form (``quote(payload, safe="")``) — unquote once, split.+ A NEW cookie hitting an OLD server fails the state check and the user+ simply retries; nothing is minted."""if _B64URL_RE.match(raw):try:@@ -465,33 +292,16 @@ def parse_pkce_payload(raw: str) -> dict[str, str]:decoded = Noneif isinstance(decoded, dict):return {str(k): str(v) for k, v in decoded.items()}- if ";" in raw:- # Oldest flat form: already flat, split as-is (no unquote).- return dict(- seg.split("=", 1) for seg in raw.split(";") if "=" in seg- )- # #99176 URL-encoded flat form: unquote once, then split.- return dict(- seg.split("=", 1) for seg in unquote(raw).split(";") if "=" in seg- )+ flat = raw if ";" in raw else unquote(raw)+ return dict(seg.split("=", 1) for seg in flat.split(";") if "=" in seg)def set_sso_attempt_cookie(response: Response, *, use_https: bool, prefix: str = "",) -> None:- """Set the one-shot auto-SSO loop-guard marker (Phase 1).-- Written by the gate the moment it auto-initiates the portal OAuth- redirect on an unauthenticated document load. The value is a constant- (``"1"``) — only its presence matters. Short Max-Age so a stale marker- can't permanently suppress a future silent attempt.- """- response.set_cookie(- _resolved_name(SSO_ATTEMPT_COOKIE, use_https=use_https, prefix=prefix),- "1",- max_age=_SSO_ATTEMPT_MAX_AGE,- **_common_attrs(use_https=use_https, prefix=prefix),- )+ """Set the auto-SSO loop-guard marker; only its presence matters."""+ _set(response, SSO_ATTEMPT_COOKIE, "1", max_age=_SSO_ATTEMPT_MAX_AGE,+ use_https=use_https, prefix=prefix)def read_sso_attempt_cookie(request: Request) -> Optional[str]:@@ -500,26 +310,15 @@ def read_sso_attempt_cookie(request: Request) -> Optional[str]:def clear_sso_attempt_cookie(response: Response, *, prefix: str = "") -> None:- """Emit Max-Age=0 deletions for the auto-SSO marker, every name variant.-- Called on a successful callback and whenever the gate falls back to- /login, so the marker never lingers to suppress a later silent attempt.- """+ """Delete the auto-SSO marker (every variant) so it never suppresses a+ later silent attempt."""_clear_cookie_variants(response, SSO_ATTEMPT_COOKIE,- prefix=prefix, https_samesite="lax",- bare_attrs={- "path": _cookie_path(prefix), "httponly": True, "samesite": "lax",- },+ prefix=prefix, https_samesite="lax", bare_attrs=_lax_bare_attrs(prefix),)def detect_https(request: Request) -> bool:- """Decide whether to set the ``Secure`` cookie flag.-- Reads ``request.url.scheme`` — under uvicorn's ``proxy_headers=True``- (which start_server enables when the gate is active), this honours- ``X-Forwarded-Proto`` from Fly's TLS terminator. Loopback traffic is- always HTTP so this returns False there.- """+ """``Secure`` flag decision: ``request.url.scheme == "https"`` (honours+ ``X-Forwarded-Proto`` under uvicorn ``proxy_headers=True``)."""return request.url.scheme == "https"diff --git a/hermes_cli/dashboard_auth/login_page.py b/hermes_cli/dashboard_auth/login_page.pyindex d440eee9925..201ae3ab050 100644--- a/hermes_cli/dashboard_auth/login_page.py+++ b/hermes_cli/dashboard_auth/login_page.py@@ -1,37 +1,22 @@-"""Server-rendered /login page.--No React, no JavaScript dependency. Listed providers come from the-registry; clicking a provider sends a GET to-``/auth/login?provider=<name>``.--Visual styling mirrors the Nous Research design system (the-``@nous-research/ui`` package the React dashboard uses): the same-``Collapse`` / ``Rules Compressed`` typeface, amber-on-dark colour-tokens (``#170d02`` / ``#ffac02`` / ``#fff``), uppercase + wide-tracking-brand chrome, and the inset-bevel button shadow. Fonts are served-out of the SPA's ``/fonts/`` directory which the dashboard-auth gate-already allowlists pre-auth (see ``_GATE_PUBLIC_PREFIXES`` in-``middleware.py``), so the page renders without needing the React-bundle loaded.--Test-stable class names: the existing test suite extracts the-``class="provider-btn"`` anchor href to walk the OAuth flow. That-class name MUST NOT change without updating-``tests/hermes_cli/test_dashboard_auth_401_reauth.py``.+"""Server-rendered /login page (no React, no SPA bundle, no injected token).++Providers come from the registry; an OAuth provider renders an anchor to+``/auth/login?provider=<name>``, a ``supports_password`` provider renders a+credential form wired by :data:`_PASSWORD_FORM_SCRIPT`. Styling mirrors the+``@nous-research/ui`` design system; fonts load from the SPA's ``/fonts/``+mount, which the gate allowlists pre-auth.++The ``class="provider-btn"`` anchor is test-stable: the suite extracts its+href to walk the OAuth flow."""from __future__ import annotationsimport html+from urllib.parse import quotefrom hermes_cli.dashboard_auth import list_session_providers-# Inline minimal CSS. The dashboard's full skin lives in the React-# bundle, which we deliberately do NOT load here — the login page must-# not depend on the SPA build being present or on the injected session-# token.-#-# Single curly braces are placeholders for ``str.format``; CSS curlies-# are doubled (``{{`` / ``}}``).+# Single curly braces are ``str.format`` placeholders; CSS curlies are doubled._LOGIN_HTML_TEMPLATE = """\<!doctype html><html lang="en">@@ -404,14 +389,10 @@ an SSH tunnel or Tailscale.</p>"""-# Inline script that wires every password provider form to POST JSON to-# ``/auth/password-login`` and navigate on success. Emitted ONLY when at-# least one ``supports_password`` provider is listed (OAuth-only login-# pages stay script-free, preserving the no-JS contract for that case).-#-# Plain string (NOT run through ``str.format``), so braces are literal —-# do not double them. A single delegated submit handler covers all forms;-# the provider name is read from the form's ``data-provider`` attribute.+# Emitted ONLY when a ``supports_password`` provider is listed, so OAuth-only+# login pages stay script-free. Plain string (not ``str.format``): braces are+# literal. One delegated submit handler covers every form; the provider name+# comes from the form's ``data-provider`` attribute._PASSWORD_FORM_SCRIPT = """\<script>(function () {@@ -461,26 +442,19 @@ _PASSWORD_FORM_SCRIPT = """\def render_login_html(*, next_path: str = "") -> str:"""Return the full HTML for ``GET /login``.- ``next_path`` — when set, the post-login landing path the user- originally requested. Threaded into each provider button's ``href``- as a ``next=`` query parameter so the OAuth round trip carries it- end-to-end. The caller (``routes.login_page``) is responsible for- validating ``next_path`` against the same-origin rules before we- emit it; we still HTML-escape it as defence in depth.+ ``next_path`` is threaded into each provider button/form so the OAuth round+ trip carries it end-to-end. The caller validates it same-origin; it is+ HTML-escaped here as defence in depth."""providers = list_session_providers()if not providers:return _EMPTY_HTML- if next_path:- # URL-encode then HTML-escape. The URL-encode step matches the- # gate's ``_safe_next_target`` output shape (also URL-encoded),- # so a value that round-tripped from /login?next=... back into- # the button href is byte-identical.- from urllib.parse import quote- next_qs = f"&next={html.escape(quote(next_path, safe=''), quote=True)}"- else:- next_qs = ""+ # URL-encode then HTML-escape, matching the gate's ``_safe_next_target``+ # shape so a round-tripped value is byte-identical.+ next_qs = (+ f"&next={html.escape(quote(next_path, safe=''), quote=True)}" if next_path else ""+ )buttons = []needs_password_script = False@@ -494,23 +468,18 @@ def render_login_html(*, next_path: str = "") -> str:f'href="/auth/login?provider={html.escape(p.name, quote=True)}{next_qs}">'f'Sign in with {html.escape(p.display_name)}</a>')- script = _PASSWORD_FORM_SCRIPT if needs_password_script else ""return _LOGIN_HTML_TEMPLATE.format(provider_buttons="\n".join(buttons),- password_script=script,+ password_script=_PASSWORD_FORM_SCRIPT if needs_password_script else "",)def _render_password_form(provider, next_path: str) -> str:- """Render a username/password form for a ``supports_password`` provider.-- The form is wired by :data:`_PASSWORD_FORM_SCRIPT` (a single delegated- submit handler) to POST JSON to ``/auth/password-login`` and navigate- on success. ``next_path`` is carried in a hidden field; it has already- been validated same-origin by the caller and is HTML-escaped here as- defence in depth. The provider ``name`` is emitted in a ``data-``- attribute (not a hidden input) so the script reads it without trusting- form-field ordering.+ """Username/password form for a ``supports_password`` provider.++ ``next_path`` rides in a hidden field (already validated by the caller,+ HTML-escaped here). The provider name is a ``data-`` attribute so the+ script does not depend on field ordering."""pname = html.escape(provider.name, quote=True)plabel = html.escape(provider.display_name)diff --git a/hermes_cli/dashboard_auth/middleware.py b/hermes_cli/dashboard_auth/middleware.pyindex 5b11e98cf2b..d57122f0762 100644--- a/hermes_cli/dashboard_auth/middleware.py+++ b/hermes_cli/dashboard_auth/middleware.py@@ -1,23 +1,17 @@"""Auth-gate middleware for the dashboard.-Engaged when ``app.state.auth_required is True``. The gate's job:-- 1. Allow a small set of routes through unauthenticated (login page,- ``/auth/*`` OAuth round trip, ``/api/auth/providers``, static- assets).- 2. For everything else, demand a valid session cookie and attach the- verified :class:`Session` to ``request.state.session``.- 3. On HTML routes, redirect missing/invalid cookies to ``/login``.- On ``/api/*`` routes, return 401 JSON.--The middleware is a no-op when ``auth_required`` is False (loopback-mode); the legacy ``_SESSION_TOKEN`` ``auth_middleware`` handles those-binds.+Engaged when ``app.state.auth_required is True``; a no-op otherwise (loopback+mode is handled by the legacy ``_SESSION_TOKEN`` ``auth_middleware``). Allows+the auth-bootstrap routes and static assets through unauthenticated; for+everything else demands a bearer token or a valid session cookie and attaches+the verified :class:`Session` to ``request.state.session``. HTML routes are+redirected to ``/login``; ``/api/*`` routes get a 401 JSON envelope."""from __future__ import annotationsimport loggingfrom typing import Awaitable, Callable+from urllib.parse import quotefrom fastapi import Requestfrom fastapi.responses import JSONResponse, RedirectResponse, Response@@ -30,22 +24,31 @@ from hermes_cli.dashboard_auth.base import (RefreshExpiredError,)from hermes_cli.dashboard_auth.cookies import (+ clear_session_cookies,clear_sso_attempt_cookie,+ detect_https,read_session_cookies,read_session_provider,read_sso_attempt_cookie,+ set_session_cookies,set_session_provider_cookie,set_sso_attempt_cookie,)+from hermes_cli.dashboard_auth.prefix import prefix_from_requestfrom hermes_cli.dashboard_auth.public_paths import PUBLIC_API_PATHS+from hermes_cli.dashboard_auth.request_utils import (+ access_token_max_age as _expires_in_seconds,+ client_ip as _client_ip,+ extract_bearer as _extract_bearer,+ is_safe_next_path,+ unreachable_response,+)_log = logging.getLogger(__name__)-# Prefixes that bypass the auth gate. Match via ``path == prefix`` or-# ``path.startswith(prefix)`` — so ``/assets/`` (with trailing slash)-# matches ``/assets/foo.css`` but not ``/assetsleak``. Auth-bootstrap-# (login page, OAuth round trip, provider listing) and static asset-# mounts go here.+# Prefix-matched (``path == p or path.startswith(p)``) bypass list: auth+# bootstrap routes and static asset mounts. ``/assets/`` with the trailing+# slash matches ``/assets/foo.css`` but not ``/assetsleak``._GATE_PUBLIC_PREFIXES: tuple[str, ...] = ("/auth/login","/auth/callback",@@ -66,42 +69,24 @@ _GATE_PUBLIC_PREFIXES: tuple[str, ...] = (def _path_is_public(path: str) -> bool:- """True if ``path`` bypasses the OAuth auth gate.-- Two sources of public-ness:+ """True if ``path`` bypasses the gate.- * :data:`PUBLIC_API_PATHS` — the shared ``/api/*`` allowlist that- the legacy ``_SESSION_TOKEN`` middleware also honours. Matched- exactly (no prefix expansion) so adding ``/api/status`` doesn't- accidentally expose ``/api/status/secret-extension``.- * :data:`_GATE_PUBLIC_PREFIXES` — auth-bootstrap routes and static- mounts. Prefix-matched so ``/assets/foo.css`` lights up via- ``/assets/``.+ :data:`PUBLIC_API_PATHS` (shared with the legacy middleware) is matched+ exactly so ``/api/status`` never exposes ``/api/status/extension``;+ :data:`_GATE_PUBLIC_PREFIXES` is prefix-matched."""if path in PUBLIC_API_PATHS:return True- return any(- path == prefix or path.startswith(prefix)- for prefix in _GATE_PUBLIC_PREFIXES- )---def _client_ip(request: Request) -> str:- fwd = request.headers.get("x-forwarded-for", "")- if fwd:- return fwd.split(",")[0].strip()- return request.client.host if request.client else ""+ return any(path == p or path.startswith(p) for p in _GATE_PUBLIC_PREFIXES)def _ordered_session_providers(provider_hint: str | None,) -> list[DashboardAuthProvider]:- """Prefer the hinted provider without making the hint authoritative.+ """Session providers with the hinted one first (stable sort).- The cookie can outlive a provider rename/removal or become stale after a- deployment change. A stable sort moves a matching provider to the front- while preserving registration order for every remaining candidate; an- unknown hint therefore leaves the normal scan unchanged.+ The hint is a routing preference, not authoritative: a stale/unknown hint+ (provider renamed or removed) leaves the normal registration order intact."""providers = list_session_providers()if provider_hint:@@ -109,46 +94,36 @@ def _ordered_session_providers(return providers-def _unauth_response(request: Request, *, reason: str) -> Response:- """API routes → 401 JSON with ``login_url``; HTML routes → 302 → /login.-- The JSON envelope carries a ``login_url`` field with a ``next=`` query- string so the SPA's global 401 handler can drop the user back where- they were after re-auth. The contract is intentionally simple so any- fetch-wrapper can implement the redirect without parsing details:-- if response.status === 401 && body.error in ("unauthenticated",- "session_expired"):- window.location.assign(body.login_url);-- HTML redirects also carry the ``next=`` query string so direct- navigation to ``/sessions`` (etc.) without a cookie comes back to- ``/sessions`` after login.-- Under a reverse proxy with ``X-Forwarded-Prefix: /hermes``, the- ``login_url`` is prefixed (``/hermes/login?next=...``) so the- browser's window.location.assign / Location: follow lands on the- proxied login page rather than the bare ``/login`` (which the- proxy doesn't route to the dashboard).- """- from hermes_cli.dashboard_auth.prefix import prefix_from_request+def _safe_next_target(request: Request) -> str:+ """URL-encoded ``next`` value for the login redirect, or ``""``.+ Only same-origin relative paths outside the auth flow and ``/api`` are+ kept (see :func:`is_safe_next_path`); the query string is preserved. SPA+ deep links that are dropped fall back to the SPA's own+ ``sessionStorage["hermes.lastLocation"]``.+ """path = request.url.path+ if not path or not is_safe_next_path(path):+ return ""+ query = request.url.query+ return quote(f"{path}?{query}" if query else path, safe="")+++def _unauth_response(request: Request, *, reason: str) -> Response:+ """API routes -> 401 JSON with ``login_url``; HTML routes -> 302 -> /login.++ fetch() follows a 302 opaquely into the cross-origin OAuth dance, so API+ routes never get redirects; the SPA's global 401 handler navigates to+ ``login_url`` when ``error`` is ``unauthenticated`` or ``session_expired``.+ Both shapes carry ``next=`` and the active proxy prefix.+ """next_param = _safe_next_target(request)prefix = prefix_from_request(request)- login_url = (- f"{prefix}/login?next={next_param}" if next_param- else f"{prefix}/login"- )+ login_url = f"{prefix}/login?next={next_param}" if next_param else f"{prefix}/login"- if path.startswith("/api/"):- # API routes never get redirects: the browser fetch() API would- # follow a 302 into the cross-origin OAuth dance opaquely. Return- # 401 with a structured envelope so the SPA can full-page-navigate- # to login_url.+ if request.url.path.startswith("/api/"):error_code = (- "session_expired"- if reason == "invalid_or_expired_session"+ "session_expired" if reason == "invalid_or_expired_session"else "unauthenticated")return JSONResponse(@@ -164,324 +139,161 @@ def _unauth_response(request: Request, *, reason: str) -> Response:def _auto_sso_response(request: Request) -> Response | None:- """Maybe auto-initiate the portal OAuth redirect on an unauth HTML load.-- Returns a 302 → ``/auth/login`` (the existing OAuth-initiation route)- when ALL of the following hold, else ``None`` (caller falls back to the- ordinary ``/login`` interstitial):-- * the request is an HTML document navigation, not an ``/api/*`` fetch- (a fetch() would follow the 302 into the cross-origin OAuth dance- opaquely — same reason ``_unauth_response`` never redirects APIs);- * exactly ONE interactive provider is registered — with two or more we- can't pick for the user, so the ``/login`` chooser must render; with- zero there's nothing to redirect to;- * that provider is OAuth-style, not a password form provider. Password- providers must render ``/login`` so the user can enter credentials;- * the one-shot loop-guard marker is ABSENT. Its presence means we- already bounced to the portal once and came back still- unauthenticated (no portal session) — auto-redirecting again would- ping-pong, so we fall through to ``/login`` and clear the marker.-- The portal ``/oauth/authorize`` auto-approves any current member of the- dashboard's org and is a silent 302 when the user already holds a portal- session, so for the common case (clicked a dashboard link while signed- in to the portal) this removes the interstitial CLICK entirely. It- removes a click, not a security check: the redirect lands on- ``/auth/login`` which runs the unchanged PKCE auth-code flow.+ """302 straight to ``/auth/login`` on an unauthenticated HTML load, or ``None``.++ Only when: the request is a document load (not ``/api/*``); exactly one+ interactive provider is registered and it is OAuth-style (a password+ provider must render the form); and the one-shot loop-guard cookie is+ absent. A present marker means the portal had no session for us last time+ — clear it and fall back to ``/login`` rather than ping-pong. Removes the+ interstitial click, not a security check: ``/auth/login`` runs the+ unchanged PKCE flow."""- path = request.url.path- # APIs never auto-redirect (see _unauth_response). Only document loads.- if path.startswith("/api/"):+ if request.url.path.startswith("/api/"):return None- # Already bounced once and still no session → portal has no session for- # this user. Stop here, clear the marker, let /login render.if read_sso_attempt_cookie(request):- from hermes_cli.dashboard_auth.prefix import prefix_from_requestresp = _unauth_response(request, reason="no_cookie")clear_sso_attempt_cookie(resp, prefix=prefix_from_request(request))return resp- # list_session_providers() already filters on supports_session=True, so- # token-only credentials (drain/service providers) are never candidates.providers = list_session_providers()if len(providers) != 1:- # Zero → nothing to redirect to. Two+ → user must choose at /login.return None-- from hermes_cli.dashboard_auth.prefix import prefix_from_request-provider = providers[0]if getattr(provider, "supports_password", False):return Noneprefix = prefix_from_request(request)next_param = _safe_next_target(request)- from urllib.parse import quoteauth_login = f"{prefix}/auth/login?provider={quote(provider.name, safe='')}"if next_param:auth_login = f"{auth_login}&next={next_param}"resp = RedirectResponse(url=auth_login, status_code=302)- # Drop the one-shot marker so a return trip that's STILL unauthenticated- # (portal had no session) trips the guard above next time instead of- # looping. Detect HTTPS for the Secure flag the same way the auth routes- # do; bind Path via the active prefix.- from hermes_cli.dashboard_auth.cookies import detect_https- set_sso_attempt_cookie(- resp, use_https=detect_https(request), prefix=prefix,- )+ set_sso_attempt_cookie(resp, use_https=detect_https(request), prefix=prefix)audit_log(AuditEvent.LOGIN_START,- provider=provider.name,- reason="auto_sso",- ip=_client_ip(request),+ provider=provider.name, reason="auto_sso", ip=_client_ip(request),)return resp-def _safe_next_target(request: Request) -> str:- """Build the URL-encoded ``next`` query value, or empty string.+def _verify_access_token(+ request: Request, *, access_token: str, provider_hint: str | None = None,+ audit: bool = True,+):+ """Run ``verify_session`` across the provider stack; Session or ``None``.- Only same-origin relative paths are accepted; absolute URLs or- ``//evil.com`` open-redirect attempts are silently dropped. The empty- string return means the caller produces a bare ``/login`` URL — fine,- user lands at the dashboard root after re-auth.- """- path = request.url.path- # Reject anything that doesn't start with "/" or starts with "//"- # (protocol-relative URL — would open-redirect to an attacker host).- if not path or not path.startswith("/") or path.startswith("//"):- return ""- # Don't redirect back to the auth routes themselves — that loops.- if any(- path == p or path.startswith(p)- for p in ("/login", "/auth/", "/api/auth/")- ):- return ""- # Reject ALL ``/api/*`` paths. The 401-envelope code path fires for- # any unauthenticated SPA fetch (e.g. ``GET /api/analytics/models``- # from ModelsPage), and the SPA's global 401 handler full-page- # navigates to ``login_url``. After the OAuth round trip the user- # would land on the API URL and see raw JSON instead of the- # dashboard. SPA routes survive (they don't start with ``/api/``);- # the SPA's own ``sessionStorage["hermes.lastLocation"]`` fallback- # in ``web/src/lib/api.ts`` covers the deep-link case.- if path == "/api" or path.startswith("/api/"):- return ""- # Preserve query string if present (e.g. /sessions?page=2).- query = request.url.query- target = f"{path}?{query}" if query else path- # urlencode the whole thing as a single value.- from urllib.parse import quote- return quote(target, safe="")---def _extract_bearer(request: Request) -> str:- """Return the ``Authorization: Bearer <token>`` value, or ""."""- auth = request.headers.get("authorization", "")- parts = auth.split(" ", 1)- if len(parts) == 2 and parts[0].strip().lower() == "bearer":- return parts[1].strip()- return ""---def _verify_bearer(request: Request, *, access_token: str):- """Verify a native-app bearer access token via the session-provider stack.-- Returns the :class:`Session` on success, or ``None`` if no provider- recognises the token (expired/invalid/unknown). Mirrors the cookie path's- verify loop, including the "one provider unreachable ⇒ don't force- re-login" semantics: a transient IDP outage returns a 503 rather than a- 401, so the desktop retries instead of dropping the user to full re-login.- Unlike the cookie path there is no server-side refresh — the desktop owns- its refresh token and rotates via ``/auth/native/refresh``.+ A provider that does not recognise the token returns ``None`` and the+ next is tried. A ``ProviderError`` (IDP/JWKS unreachable) must NOT abort+ the chain — the token may belong to a different, reachable provider. If+ no provider verifies AND at least one was unreachable, raises+ ``ProviderError(name)`` so the caller returns 503 instead of forcing a+ re-login through a possibly-unreachable refresh."""unreachable_provider: str | None = None- for provider in list_session_providers():+ for provider in _ordered_session_providers(provider_hint):try:session = provider.verify_session(access_token=access_token)except ProviderError as e:_log.warning(- "dashboard-auth: provider %r unreachable during bearer verify: %s",- provider.name, e,+ "dashboard-auth: provider %r unreachable during %s: %s",+ provider.name, "verify" if audit else "bearer verify", e,)+ if audit:+ audit_log(+ AuditEvent.SESSION_VERIFY_FAILURE,+ provider=provider.name,+ reason="provider_unreachable",+ ip=_client_ip(request),+ )if unreachable_provider is None:unreachable_provider = provider.namecontinueif session is not None:return sessionif unreachable_provider is not None:- # Signal transient outage to the caller via a sentinel exception the- # middleware turns into 503. Raising keeps the "don't logout on a- # flaky IDP" contract identical to the cookie path.raise ProviderError(unreachable_provider)return None+def _verify_bearer(request: Request, *, access_token: str):+ """Verify a native-app bearer token (no cookie, no server-side refresh —+ the desktop rotates via ``/auth/native/refresh``). Same 503-on-outage+ semantics as the cookie path.+ """+ return _verify_access_token(request, access_token=access_token, audit=False)++async def gated_auth_middleware(request: Request,call_next: Callable[[Request], Awaitable[Response]],) -> Response:- """Engaged only when ``app.state.auth_required is True``.-- No-op pass-through in loopback mode so the legacy auth_middleware can- handle those binds via ``_SESSION_TOKEN``.- """+ """Engaged only when ``app.state.auth_required is True``."""if not getattr(request.app.state, "auth_required", False):return await call_next(request)- # A request already authenticated by the token-auth seam (a service caller- # on a registered token route) carries ``token_authenticated`` — it is NOT- # a cookie session and must not be bounced to /login. Pass it through; the- # seam already attached ``request.state.token_principal``.+ # Already authenticated by the token-auth seam (service caller on a+ # registered token route): not a cookie session, must not bounce to /login.if getattr(request.state, "token_authenticated", False):return await call_next(request)- path = request.url.path- if _path_is_public(path):+ if _path_is_public(request.url.path):return await call_next(request)- # RFC 8252 native-app bearer path (goal: no session cookies). The desktop- # authenticates REST with ``Authorization: Bearer <access_token>`` — the- # SAME provider-minted access token the cookie flow stores in- # ``hermes_session_at``. Verify it with the identical ``verify_session``- # provider stack and attach the Session; on success we're done, with no- # cookie set or read. A missing/expired/invalid bearer falls through to- # the cookie path (a request may legitimately carry neither). Token- # rotation for this path is the desktop's job via /auth/native/refresh —- # the gate never sets a cookie here, so the transparent cookie-rotation- # below must not run for a bearer caller.+ # RFC 8252 native-app bearer path: the same provider-minted access token+ # the cookie flow stores, verified with the same provider stack, no cookie+ # read or set. A presented-but-invalid bearer gets the structured 401 so+ # the desktop refreshes/re-logs instead of following a cookie redirect.bearer = _extract_bearer(request)if bearer:try:bearer_session = _verify_bearer(request, access_token=bearer)except ProviderError as e:- # At least one provider's IDP/JWKS was unreachable and none- # verified the token — transient outage, not bad credentials.- return JSONResponse(- {"detail": f"Auth provider {str(e)!r} unreachable"},- status_code=503,- )+ return unreachable_response(str(e))if bearer_session is not None:request.state.session = bearer_sessionreturn await call_next(request)- # A bearer was presented but didn't verify (expired/invalid/unknown).- # Return the structured 401 so the desktop knows to refresh or- # re-login, rather than falling through to the cookie/login redirect.return _unauth_response(request, reason="invalid_or_expired_session")at, _rt = read_session_cookies(request)provider_hint = read_session_provider(request)if not at and not _rt:- # Neither token present — no session at all. Nothing to verify or- # refresh. Before falling back to the /login interstitial, try to- # silently bounce the user through the portal OAuth flow: the portal- # auto-approves org members and 302s straight back when they already- # hold a portal session, so the interstitial click is pure friction- # for the common case. The one-shot loop-guard inside _auto_sso_response- # prevents a ping-pong when the portal genuinely has no session.+ # No session at all: try the silent portal bounce before /login.auto = _auto_sso_response(request)if auto is not None:return autoreturn _unauth_response(request, reason="no_cookie")- # Try every registered provider's verify_session in turn. Providers- # MUST return None for tokens they don't recognise (not raise). This- # lets multiple providers stack — the first one that recognises a- # token wins.- #- # When the access-token cookie is absent but a refresh-token cookie is- # present, skip verification and go straight to the refresh path below.- # This is the COMMON expiry case, not an edge case: the access-token- # cookie is set with ``Max-Age = access_token_expires_in`` (~15 min), so- # the browser EVICTS it the moment the token lapses, while the- # refresh-token cookie lives for 30 days. From that point the browser- # sends only ``hermes_session_rt``. If we bailed on ``not at`` here we'd- # bounce the user to /login on every expiry despite holding a perfectly- # good refresh token — defeating the whole transparent-refresh feature.+ # An absent AT with a present RT is the COMMON expiry case (the AT cookie's+ # Max-Age tracks the token TTL, so the browser evicts it first) — skip+ # straight to refresh rather than bouncing to /login.session = Noneif at:- # Try every registered provider's verify_session in turn. A provider- # that doesn't recognise the token returns None and we move on; the- # first provider that returns a Session wins.- #- # A provider may instead raise ProviderError (its IDP/JWKS is- # unreachable, so it can neither confirm nor deny the token). With- # multiple providers stacked, that MUST NOT abort the chain — the- # token may belong to a *different*, reachable provider. (Concretely:- # a self-hosted-OIDC session hits the `nous` provider first, which- # tries to reach Nous Portal's JWKS; if that's unreachable it raises,- # but the `self-hosted` provider can still verify the token.) So we- # remember the unreachable error and keep going. Only if NO provider- # verifies the token AND at least one was unreachable do we surface a- # 503 — distinguishing "transient IDP outage" (don't force re-login)- # from "token genuinely invalid" (fall through to refresh/relogin).- unreachable_provider: str | None = None- for provider in _ordered_session_providers(provider_hint):- try:- session = provider.verify_session(access_token=at)- except ProviderError as e:- _log.warning(- "dashboard-auth: provider %r unreachable during verify: %s",- provider.name, e,- )- audit_log(- AuditEvent.SESSION_VERIFY_FAILURE,- provider=provider.name,- reason="provider_unreachable",- ip=_client_ip(request),- )- if unreachable_provider is None:- unreachable_provider = provider.name- continue- if session is not None:- break- if session is None and unreachable_provider is not None:- # No provider could verify the token and at least one couldn't be- # reached — treat as a transient outage rather than forcing a- # re-login through a (possibly also-unreachable) refresh.- return JSONResponse(- {"detail": f"Auth provider {unreachable_provider!r} unreachable"},- status_code=503,+ try:+ session = _verify_access_token(+ request, access_token=at, provider_hint=provider_hint,)+ except ProviderError as e:+ return unreachable_response(str(e))if session is None:- # Access token is expired/invalid. Before forcing re-login, try to- # rotate it using the refresh token (if the session cookie carries- # one). On success we re-set the rotated cookies on the response and- # serve the request transparently; only after every provider rejects- # the RT do we fall through to clear-and-relogin.+ # Rotate via the refresh token before forcing re-login. On success the+ # rotated cookies are re-set and the request served transparently.try:refreshed = _attempt_refresh(- request,- refresh_token=_rt,- provider_hint=provider_hint,+ request, refresh_token=_rt, provider_hint=provider_hint,)except ProviderError as e:- # At least one provider could not confirm or reject the RT, and no- # other provider refreshed it. Preserve the cookies and surface a- # transient outage instead of turning uncertainty into a logout.- return JSONResponse(- {"detail": f"Auth provider {str(e)!r} unreachable"},- status_code=503,- )+ # Uncertain (provider unreachable), not rejected: keep the cookies.+ return unreachable_response(str(e))if refreshed is not None:new_session, refreshing_provider = refreshedrequest.state.session = new_sessionresponse = await call_next(request)- # Persist the ROTATED tokens. Portal rotates the refresh token on- # every refresh and runs reuse-detection, so writing the new RT- # back is mandatory: a stale RT cookie would replay a rotated- # token on the next refresh and (outside Portal's grace) revoke- # the whole session. Bind cookie Secure/Path to the request shape.- from hermes_cli.dashboard_auth.cookies import (- detect_https,- set_session_cookies,- )- from hermes_cli.dashboard_auth.prefix import prefix_from_request-+ # Writing the ROTATED RT back is mandatory: Portal runs reuse+ # detection, so replaying the stale RT would revoke the session.set_session_cookies(response,access_token=new_session.access_token,@@ -505,23 +317,14 @@ async def gated_auth_middleware(ip=_client_ip(request),)response = _unauth_response(request, reason="invalid_or_expired_session")- # Clear the dead cookies so the browser doesn't keep sending them.- # Refresh already failed (or there was no RT), so the only correct- # next step is full re-auth via /login. Importing locally avoids a- # cycle with cookies → middleware at module load. Pass the active- # prefix so the deletion's Path matches the set-Path (otherwise- # the browser ignores it).- from hermes_cli.dashboard_auth.cookies import clear_session_cookies- from hermes_cli.dashboard_auth.prefix import prefix_from_request+ # Refresh failed (or no RT): clear the dead cookies under the active+ # prefix so the deletion Path matches the set Path.clear_session_cookies(response, prefix=prefix_from_request(request))return responserequest.state.session = sessionresponse = await call_next(request)if not provider_hint and session.provider:- from hermes_cli.dashboard_auth.cookies import detect_https- from hermes_cli.dashboard_auth.prefix import prefix_from_request-set_session_provider_cookie(response,provider=session.provider,@@ -531,30 +334,13 @@ async def gated_auth_middleware(return response-def _expires_in_seconds(session) -> int:- """Seconds until the access token's ``exp``, floored at 60.-- Mirrors the auth-route's ``max(60, exp - now)`` so the access-token- cookie's Max-Age tracks the token lifetime even on a slightly skewed- clock. ``time`` imported locally to keep the module's import surface- minimal.- """- import time-- return max(60, int(session.expires_at) - int(time.time()))--def _attempt_refresh(request: Request, *, refresh_token, provider_hint: str | None = None):- """Try to rotate an expired session via the refresh token.-- The provider hint only changes candidate order. ``RefreshExpiredError``- rejects the token for that candidate, but cannot prove ownership because- providers such as Basic raise it for foreign opaque tokens too. Likewise,- ``ProviderError`` only makes that candidate unavailable. Both are audited- and the remaining providers are tried. Returns ``None`` only when there is- no RT or every reachable provider rejects it. If no provider succeeds and- at least one raised ``ProviderError``, re-raises with that provider's name- so the caller can return 503 without clearing potentially valid cookies.+ """Rotate an expired session via the refresh token; ``(Session, provider_name)`` or ``None``.++ ``RefreshExpiredError`` rejects the token for that candidate only (Basic+ raises it for foreign opaque tokens too), so remaining providers are+ tried. If none succeeds and any raised ``ProviderError``, re-raises with+ that provider's name so the caller returns 503 without clearing cookies."""if not refresh_token:return Nonediff --git a/hermes_cli/dashboard_auth/native_flow.py b/hermes_cli/dashboard_auth/native_flow.pyindex 5dfe800c274..2e1abcf7db2 100644--- a/hermes_cli/dashboard_auth/native_flow.py+++ b/hermes_cli/dashboard_auth/native_flow.py@@ -1,70 +1,32 @@"""Gateway-brokered RFC 8252 (OAuth 2.0 for Native Apps) authorization store.-The desktop app is a *native* OAuth client that wants to sign in to a gated-gateway **without an embedded webview and without relying on browser session-cookies**. It cannot be a direct OAuth client of the upstream IDP (Nous-Portal): the Portal ``client_id`` is per-gateway-instance-(``agent:{instance_id}``) and the Portal validates that the ``redirect_uri``-ends in ``/auth/callback`` on the gateway's own public origin — a desktop-loopback ``127.0.0.1`` redirect is rejected. So the **gateway brokers** the-flow: it is the authorization server *to the desktop*, and an OAuth client *to-the Portal*. This is still a textbook RFC 8252 deployment — system browser,-loopback redirect, PKCE, tokens returned to the app (never cookies).--Wire shape (all gateway-side state lives in this module):-- 1. Desktop generates its OWN PKCE pair ``(cv_d, cc_d)`` and a ``state``, opens- a loopback listener on ``127.0.0.1:<port>``, and opens the system browser- to the gateway's ``GET /auth/native/authorize?...`` carrying ``cc_d``,- ``state``, and its loopback ``redirect_uri``.- 2. The gateway ``authorize`` route stashes a **pending authorization**- (``register_pending``) keyed by an opaque ``broker_state`` and runs the- EXISTING upstream PKCE flow (``provider.start_login`` → Portal- ``/oauth/authorize`` → gateway ``/auth/callback``). The desktop's- ``cc_d`` / ``state`` / loopback ``redirect_uri`` ride through the upstream- round trip inside the gateway's own PKCE cookie, so no desktop secret is- ever exposed to the Portal.- 3. On the upstream callback the gateway holds a verified :class:`Session`. It- **mints a one-time gateway authorization code** (``complete_pending``)- bound to the desktop's ``cc_d``, and 302s the browser to the desktop's- ``redirect_uri?code=<gw_code>&state=<state>``.- 4. The desktop's loopback listener catches ``gw_code``, then POSTs- ``/auth/native/token`` with ``gw_code`` + its ``cv_d``. The gateway- verifies ``SHA256(cv_d) == cc_d`` (``redeem_code``), consumes the code- (single use), and returns the upstream ``access_token`` /- ``refresh_token`` / ``expires_at`` **in the JSON body**.- 5. The desktop stores those in the OS keychain and authenticates REST with- ``Authorization: Bearer <access_token>`` (via the existing ``token_auth``- seam) and mints ws-tickets the same way — no cookies anywhere.--Password providers ride the same broker with step 2 swapped: there is no-upstream IDP, so ``/auth/native/authorize`` sends the system browser to the-interactive ``/login`` form (broker_state in the PKCE cookie) and a successful-``/auth/password-login`` plays the role of the upstream callback — it calls-:func:`complete_pending` and bounces the browser to the loopback redirect.-Steps 4–5 are identical. The point of brokering a password login at all is-that the system browser can autofill from the OS password manager (macOS-Passwords, etc.), which no embedded desktop webview can.--Security properties this module guarantees:-- * **PKCE binding (RFC 7636).** A gateway code is redeemable only by the client- that presented the matching ``code_challenge``. An attacker who intercepts- the loopback ``gw_code`` (e.g. a hostile process racing the redirect) cannot- exchange it without ``cv_d``, which never leaves the desktop.- * **Single use.** ``redeem_code`` pops the entry; a replay finds nothing.- * **Short TTLs.** A pending authorization lives ``_PENDING_TTL`` seconds (the- interactive login window); a minted code lives ``_CODE_TTL`` seconds (the- loopback round trip is sub-second). Expired entries are refused and GC'd.- * **Opaque, high-entropy handles.** ``broker_state`` and ``gw_code`` are- 256-bit ``secrets.token_urlsafe`` values; comparison is constant-time.- * **No secret logging.** The module stores tokens transiently in memory only- between callback and redemption; nothing here writes them to disk (the- audit log strips token fields).--In-memory and process-local: the dashboard is a single process, so no-distributed coordination is needed (mirrors ``ws_tickets``). A functional API-(not a class) keeps ``time.time`` patchable in tests.+The desktop cannot be a direct OAuth client of the upstream IDP (the Portal+``client_id`` is per gateway instance and only accepts the gateway's own+``/auth/callback`` redirect), so the gateway brokers: it is the authorization+server *to the desktop* and an OAuth client *to the Portal* — still textbook+RFC 8252: system browser, loopback redirect, PKCE, tokens returned to the app+and never as cookies.++ 1. Desktop generates its own PKCE pair (cv_d, cc_d) + ``state``, opens a+ loopback listener, and opens the system browser at+ ``/auth/native/authorize`` with cc_d, state and its loopback redirect_uri.+ 2. The gateway stashes a *pending authorization* (:func:`register_pending`)+ keyed by an opaque ``broker_state`` and runs the EXISTING upstream flow;+ broker_state rides inside the gateway's own PKCE cookie, so no desktop+ secret reaches the Portal.+ 3. On the upstream callback (or a successful password login) the gateway+ mints a one-time gateway code bound to cc_d (:func:`complete_pending`) and+ 302s the browser to ``redirect_uri?code=<gw_code>&state=<state>``.+ 4. The desktop POSTs ``/auth/native/token`` with gw_code + cv_d; the gateway+ checks ``S256(cv_d) == cc_d`` (:func:`redeem_code`), consumes the code and+ returns the upstream tokens in the JSON body.+ 5. The desktop keeps them in the OS keychain and uses ``Authorization: Bearer``.++Security properties: PKCE binding (an intercepted gw_code is useless without+cv_d), single use (redemption pops the entry), short TTLs, 256-bit opaque+handles compared in constant time, no secret logging. In-memory and+process-local (single dashboard process); functional API keeps ``time.time``+patchable in tests."""from __future__ import annotations@@ -80,24 +42,15 @@ from typing import Dict, Optionalfrom hermes_cli.dashboard_auth.base import Session-# TTL for a pending authorization (step 2→3): the whole interactive login,-# including the user typing Portal credentials / approving in the browser.-_PENDING_TTL_SECONDS = 600 # 10 minutes — mirrors the PKCE cookie lifetime.--# TTL for a minted gateway code (step 3→4): only the loopback redirect + the-# desktop's immediate token POST, which is sub-second in practice.-_CODE_TTL_SECONDS = 120 # 2 minutes — generous for a slow local hop.--# Cap the number of concurrent pending/issued entries so a misbehaving or-# malicious client cannot grow the store unbounded. Well above any legitimate-# concurrent-login count for a single desktop user.+# Pending authorization: the whole interactive login (mirrors the PKCE cookie).+_PENDING_TTL_SECONDS = 600+# Minted code: only the loopback redirect + immediate token POST.+_CODE_TTL_SECONDS = 120+# Global cap so a misbehaving client cannot grow the store unbounded._MAX_ENTRIES = 256--# Per-IP cap on concurrent PENDING authorizations. /auth/native/authorize is a-# public (pre-auth) route, so without this a single unauthenticated spammer-# could fill the global store (600s TTL each) and lock out legitimate native-# logins for the pending window. A real desktop runs at most a couple of-# concurrent sign-ins from one address; 8 is generous.+# Per-IP cap on PENDING entries: /auth/native/authorize is a public pre-auth+# route, so one spammer must not fill the global store (600 s each) and lock+# out legitimate native logins._MAX_PENDING_PER_IP = 8_lock = threading.Lock()@@ -105,11 +58,7 @@ _lock = threading.Lock()@dataclassclass _Pending:- """An in-flight native authorization awaiting the upstream callback.-- Created when the desktop hits ``/auth/native/authorize`` and consumed when- the upstream ``/auth/callback`` completes and mints the gateway code.- """+ """In-flight native authorization awaiting the upstream callback."""code_challenge: str # the DESKTOP's S256 challenge (cc_d), base64url no-padredirect_uri: str # the desktop's loopback redirect (127.0.0.1:<port>/...)@@ -127,10 +76,8 @@ class _IssuedCode:expires_at: int-# broker_state -> _Pending-_pending: Dict[str, _Pending] = {}-# gw_code -> _IssuedCode-_issued: Dict[str, _IssuedCode] = {}+_pending: Dict[str, _Pending] = {} # broker_state -> _Pending+_issued: Dict[str, _IssuedCode] = {} # gw_code -> _IssuedCodeclass NativeFlowError(Exception):@@ -157,18 +104,19 @@ def _s256(verifier: str) -> str:def _gc_locked(now: int) -> None:"""Drop expired pending + issued entries. Caller holds ``_lock``."""- expired_p = [k for k, v in _pending.items() if v.expires_at < now]- for k in expired_p:- _pending.pop(k, None)- expired_c = [k for k, v in _issued.items() if v.expires_at < now]- for k in expired_c:- _issued.pop(k, None)+ for store in (_pending, _issued):+ for k in [k for k, v in store.items() if v.expires_at < now]:+ store.pop(k, None)def _capacity_ok_locked() -> bool:return (len(_pending) + len(_issued)) < _MAX_ENTRIES+def _now(now: Optional[int]) -> int:+ return int(time.time()) if now is None else now++def register_pending(*,code_challenge: str,@@ -179,23 +127,11 @@ def register_pending() -> str:"""Stash a pending native authorization; return an opaque ``broker_state``.- Called by ``/auth/native/authorize``. ``code_challenge`` is the DESKTOP's- S256 challenge (``cc_d``) — we never see the verifier until redemption.- ``redirect_uri`` is the desktop's loopback callback and ``client_state`` is- the desktop's own CSRF ``state`` (echoed verbatim on the final redirect).- ``client_ip`` is the requester's address, used only for the per-IP pending- cap below.-- The returned ``broker_state`` is what the gateway threads through its OWN- upstream PKCE round trip (inside the ``hermes_session_pkce`` cookie), so the- callback can find this entry again via :func:`complete_pending`.-- Raises ``NativeFlowError`` if the store is at capacity or the caller's IP- already holds ``_MAX_PENDING_PER_IP`` live pending entries (fail closed —- this is a public pre-auth route, so one spammer must not be able to fill- the global store and deny sign-in to everyone else).+ ``code_challenge`` is the DESKTOP's cc_d (the verifier is never seen until+ redemption). Raises ``NativeFlowError`` (fail closed) when the store is at+ capacity or ``client_ip`` already holds ``_MAX_PENDING_PER_IP`` entries."""- now = int(time.time()) if now is None else now+ now = _now(now)broker_state = secrets.token_urlsafe(32)with _lock:_gc_locked(now)@@ -219,13 +155,9 @@ def register_pending(def get_pending(broker_state: str, *, now: Optional[int] = None) -> _Pending:- """Return the pending authorization for ``broker_state`` without consuming it.-- Read-only peek used by the callback to learn the desktop's ``redirect_uri``- and ``client_state`` for the final 302. Raises :class:`PendingNotFound` if- unknown or expired (the entry is GC'd on expiry).- """- now = int(time.time()) if now is None else now+ """Peek (without consuming) the pending authorization; raises+ :class:`PendingNotFound` if unknown or expired."""+ now = _now(now)with _lock:_gc_locked(now)entry = _pending.get(broker_state)@@ -240,16 +172,12 @@ def complete_pending(session: Session,now: Optional[int] = None,) -> str:- """Consume a pending authorization and mint a one-time gateway code.-- Called by ``/auth/callback`` once the upstream :class:`Session` is verified.- Pops the pending entry (single use), binds a fresh ``gw_code`` to the- desktop's ``code_challenge`` + the verified ``session``, and returns the- ``gw_code`` for the loopback redirect.+ """Consume a pending authorization (single use) and mint a one-time gateway+ code bound to the desktop's challenge + the verified ``session``.Raises :class:`PendingNotFound` if the broker_state is unknown/expired."""- now = int(time.time()) if now is None else now+ now = _now(now)with _lock:_gc_locked(now)pending = _pending.pop(broker_state, None)@@ -274,27 +202,19 @@ def redeem_code() -> Session:"""Verify PKCE + consume a gateway code; return the bound :class:`Session`.- Called by ``/auth/native/token``. Enforces:- * the code exists and is unexpired (else :class:`CodeInvalid`);- * ``S256(code_verifier) == code_challenge`` in constant time (RFC 7636);- * single use — the entry is popped BEFORE the PKCE check so a wrong- verifier cannot be retried against the same code.-- On any failure the code is already consumed (no oracle, no replay).+ The entry is popped BEFORE the PKCE check so a wrong verifier cannot be+ retried against the same code: on any failure the code is already consumed+ (no oracle, no replay). Raises :class:`CodeInvalid`."""- now = int(time.time()) if now is None else now+ now = _now(now)with _lock:_gc_locked(now)issued = _issued.pop(code, None)- # Pop happened under the lock; every return path below has already- # consumed the code, so a replay (valid or not) finds nothing.if issued is None:raise CodeInvalid("unknown, expired, or already-redeemed code")if issued.expires_at < now:raise CodeInvalid("code expired")- expected = issued.code_challenge- actual = _s256(code_verifier)- if not hmac.compare_digest(expected, actual):+ if not hmac.compare_digest(issued.code_challenge, _s256(code_verifier)):raise CodeInvalid("PKCE verification failed")return issued.sessiondiff --git a/hermes_cli/dashboard_auth/prefix.py b/hermes_cli/dashboard_auth/prefix.pyindex c2f1f85ab72..84a24e44c0e 100644--- a/hermes_cli/dashboard_auth/prefix.py+++ b/hermes_cli/dashboard_auth/prefix.py@@ -1,21 +1,12 @@-"""Helpers for X-Forwarded-Prefix support.--Mission-control style deploys reverse-proxy the dashboard at a path-prefix (e.g. ``mission-control.tilos.com/hermes/*`` -> dashboard on-:9119), injecting ``X-Forwarded-Prefix: /hermes`` so the backend can-reconstruct prefixed URLs (Location: headers, OAuth redirect_uri,-cookie Path attributes, SPA asset URLs).--This module is also the home of the ``HERMES_DASHBOARD_PUBLIC_URL`` /-``dashboard.public_url`` resolution — when the operator declares a-complete public URL (scheme + host + optional path prefix), we use-that directly for the OAuth ``redirect_uri`` and skip the-X-Forwarded-Prefix reconstruction. Relief valve for deploys where the-proxy header chain isn't reliable.--The single source of truth for both helpers lives here so the gate-middleware, the OAuth routes, the cookie helpers, and the SPA mount-all agree on validation rules.+"""X-Forwarded-Prefix and public-URL resolution for reverse-proxied deploys.++Mission-control style deploys proxy the dashboard at a path prefix and inject+``X-Forwarded-Prefix: /hermes`` so the backend can build prefixed URLs+(Location headers, OAuth redirect_uri, cookie Path, SPA asset URLs). When the+operator declares a complete ``HERMES_DASHBOARD_PUBLIC_URL`` /+``dashboard.public_url`` instead, that is used verbatim for the OAuth+redirect_uri (relief valve for unreliable proxy header chains). Single source+of truth so the gate, routes, cookies and SPA mount agree on validation."""from __future__ import annotations@@ -26,42 +17,30 @@ from typing import Optional_log = logging.getLogger(__name__)-# Home Assistant Supervisor ingress prefixes are already 63 chars before-# deployments add their own sub-path. Keep a bounded header budget, but leave-# room for mainstream reverse-proxy path mounts.+# Home Assistant ingress prefixes are already 63 chars before a deployment adds+# its own sub-path; keep a bounded header budget with room for real mounts._MAX_PREFIX_LENGTH = 256-# Characters that, if present in a public_url or prefix value, indicate-# either a typo or a header-injection attempt. Reject the whole value-# rather than try to sanitise — the operator can fix their config.+# Presence of any of these in a public_url / prefix means a typo or a+# header-injection attempt: reject the whole value, never sanitise._REJECT_CHARS = frozenset(('"', "'", "<", ">", " ", "\n", "\r", "\t"))-# Remember which (source, value) pairs we've already warned about.-# ``resolve_public_url`` runs on every authenticated request, so an-# un-deduplicated warning would flood the logs once per request for a-# misconfigured deploy. Keyed on the raw value too, so changing the-# config and reloading surfaces a fresh warning.+# ``resolve_public_url`` runs on every authenticated request, so warnings are+# de-duplicated per distinct (source, value) — a changed value warns afresh._warned_malformed_public_urls: set = set()_warned_malformed_prefixes: set = set()def _warn_if_malformed(source: str, raw: str) -> None:- """Warn (once per distinct value) when a non-empty public-url value- was rejected by :func:`_normalise_public_url`.-- A non-empty value that normalises to ``""`` is almost always a- missing scheme (``hermes.example.com`` instead of- ``https://hermes.example.com``) — the single most common cause of- "I set HERMES_DASHBOARD_PUBLIC_URL but the OAuth callback is still- http://". Without this warning the value is silently discarded and- the dashboard falls back to reconstructing the redirect URI from- request headers, which behind a reverse proxy can yield the wrong- scheme. Surfacing it turns a silent footgun into a self-diagnosing- one.+ """Warn once when a non-empty public-url value was rejected.++ Almost always a missing scheme; without this the value is silently+ discarded and the OAuth callback falls back to header reconstruction,+ which behind a proxy can yield the wrong scheme."""cleaned = raw.strip() if raw else ""if not cleaned:- return # empty/unset is a legitimate "no override" — not malformed+ return # empty/unset is a legitimate "no override"key = (source, cleaned)if key in _warned_malformed_public_urls:return@@ -96,45 +75,31 @@ def _warn_if_malformed_prefix(raw: Optional[str], reason: str) -> None:def normalise_prefix(raw: Optional[str]) -> str:- """Normalise an X-Forwarded-Prefix header value.-- Returns a string like ``"/hermes"`` (no trailing slash) or ``""``- when no prefix is set / the header is malformed. We deliberately- reject anything containing ``..`` or non-printable bytes so a- hostile proxy can't inject HTML or path-traversal sequences via the- prefix.+ """Normalise an X-Forwarded-Prefix header to ``"/hermes"`` form (no trailing+ slash) or ``""`` when unset/malformed. ``..``, ``//`` and injection+ characters are rejected so a hostile proxy cannot smuggle HTML or traversal."""- if not raw:- return ""- p = raw.strip()+ p = raw.strip() if raw else ""if not p:return ""if not p.startswith("/"):p = "/" + pp = p.rstrip("/")- if (- "//" in p- or ".." in p- or any(c in p for c in _REJECT_CHARS)- ):+ if "//" in p or ".." in p or any(c in p for c in _REJECT_CHARS):_warn_if_malformed_prefix(- raw,- "it contains a disallowed character or path sequence",+ raw, "it contains a disallowed character or path sequence",)return ""if len(p) > _MAX_PREFIX_LENGTH:_warn_if_malformed_prefix(- raw,- f"it is longer than {_MAX_PREFIX_LENGTH} characters",+ raw, f"it is longer than {_MAX_PREFIX_LENGTH} characters",)return ""return pdef prefix_from_request(request) -> str:- """Convenience wrapper that reads the header off a Starlette/FastAPI- Request and normalises it. Returns ``""`` when no prefix.- """+ """Normalised ``X-Forwarded-Prefix`` from a Starlette request, or ``""``."""return normalise_prefix(request.headers.get("x-forwarded-prefix"))@@ -144,48 +109,26 @@ def prefix_from_request(request) -> str:def _normalise_public_url(raw: Optional[str]) -> str:- """Normalise a ``dashboard.public_url`` value.-- Returns the cleaned URL (scheme://netloc[/path], trailing slash- removed) on success, or ``""`` when the value is empty, malformed,- or contains characters that suggest header injection. The caller- must treat ``""`` as "fall back to request reconstruction" — never- as "the user explicitly chose no public URL", because the two are- indistinguishable from an empty env var.+ """Cleaned ``scheme://netloc[/path]`` (trailing slash stripped so callers+ can append paths) or ``""`` when empty/malformed/injection-suspect. Callers+ must treat ``""`` as "fall back to request reconstruction" — an explicit+ empty value is indistinguishable from an unset env var."""- if not raw:- return ""- url = raw.strip()- if not url:- return ""- # Reject control / quote / whitespace characters before trying to- # parse — urlparse is permissive enough to accept some hostile- # values (e.g. embedded newlines) and we want a hard "no" rather- # than a soft "maybe".- if any(c in url for c in _REJECT_CHARS):+ url = raw.strip() if raw else ""+ if not url or any(c in url for c in _REJECT_CHARS):return ""try:parsed = urllib.parse.urlparse(url)except ValueError:return ""- if parsed.scheme not in {"http", "https"}:+ if parsed.scheme not in {"http", "https"} or not parsed.netloc:return ""- if not parsed.netloc:- return ""- # Strip a single trailing slash so callers can append paths without- # producing ``//`` double-slashes.return url.rstrip("/")def _load_dashboard_section() -> dict:- """Return the ``dashboard`` block from ``config.yaml`` if it exists- and is a dict; otherwise an empty dict.-- Robust to (a) load_config() raising (malformed YAML, IO error,- config.yaml absent), and (b) ``dashboard`` being absent or non-dict.- Both shapes fall through to ``{}`` so the caller can rely on- ``.get(...)`` access.- """+ """``dashboard`` block of config.yaml as a dict, or ``{}`` when the config+ cannot be loaded or the block is absent/non-dict."""try:from hermes_cli.config import load_configexcept Exception:@@ -204,21 +147,12 @@ def _load_dashboard_section() -> dict:def resolve_public_url() -> str:- """Resolve the operator-declared dashboard public URL.-- Precedence (mirrors ``dashboard.oauth.client_id``):-- 1. ``HERMES_DASHBOARD_PUBLIC_URL`` env var (when non-empty after- strip — empty values are treated as unset so a provisioned-but-- not-populated Fly secret can't shadow a valid config.yaml entry).- 2. ``dashboard.public_url`` in ``config.yaml``.- 3. Empty string — signals "no override, reconstruct from request"- to the caller.+ """Operator-declared dashboard public URL, or ``""`` (reconstruct from request).- Each candidate value is run through :func:`_normalise_public_url`.- A malformed env var falls through to the config.yaml entry; a- malformed config entry falls through to ``""``. This means a typo- in one surface doesn't prevent the other from working.+ Precedence: ``HERMES_DASHBOARD_PUBLIC_URL`` env (empty-after-strip counts+ as unset so a provisioned-but-blank secret cannot shadow config.yaml), then+ ``dashboard.public_url``. A malformed value at either level warns and falls+ through to the next, so a typo in one surface never disables the other."""env_raw = os.environ.get("HERMES_DASHBOARD_PUBLIC_URL", "")env_clean = _normalise_public_url(env_raw)diff --git a/hermes_cli/dashboard_auth/request_utils.py b/hermes_cli/dashboard_auth/request_utils.pynew file mode 100644index 00000000000..cfa885c6dac--- /dev/null+++ b/hermes_cli/dashboard_auth/request_utils.py@@ -0,0 +1,54 @@+"""Request-level helpers shared by the auth routes and both middlewares."""+from __future__ import annotations++import time++from fastapi import Request+from fastapi.responses import JSONResponse++# Paths a post-login redirect must never land on: the auth flow itself (would+# loop) and any ``/api/*`` target (renders raw JSON in the address bar and is+# indistinguishable from an attacker weaponising the redirect).+_NEXT_DENY_PREFIXES = ("/login", "/auth/", "/api/auth/")+++def client_ip(request: Request) -> str:+ """First ``X-Forwarded-For`` hop, else the peer address."""+ fwd = request.headers.get("x-forwarded-for", "")+ if fwd:+ return fwd.split(",")[0].strip()+ return request.client.host if request.client else ""+++def extract_bearer(request: Request) -> str:+ """``Authorization: Bearer <token>`` value (scheme case-insensitive), or ``""``."""+ parts = request.headers.get("authorization", "").split(" ", 1)+ if len(parts) == 2 and parts[0].strip().lower() == "bearer":+ return parts[1].strip()+ return ""+++def is_safe_next_path(path: str) -> bool:+ """True if ``path`` is a same-origin post-login target.++ Rejects non-relative and protocol-relative (``//evil``) values, the auth+ routes themselves, and every ``/api`` path.+ """+ if not path.startswith("/") or path.startswith("//"):+ return False+ if any(path == p or path.startswith(p) for p in _NEXT_DENY_PREFIXES):+ return False+ return not (path == "/api" or path.startswith("/api/"))+++def access_token_max_age(session) -> int:+ """Cookie Max-Age for the access token: seconds to ``exp``, floored at 60."""+ return max(60, int(session.expires_at) - int(time.time()))+++def unreachable_response(provider_name: str) -> JSONResponse:+ """503 for a transient IDP/backing-store outage (never a forced re-login)."""+ return JSONResponse(+ {"detail": f"Auth provider {provider_name!r} unreachable"},+ status_code=503,+ )diff --git a/hermes_cli/dashboard_auth/routes.py b/hermes_cli/dashboard_auth/routes.pyindex 77f9c8ee254..1270d5b6488 100644--- a/hermes_cli/dashboard_auth/routes.py+++ b/hermes_cli/dashboard_auth/routes.py@@ -1,17 +1,20 @@"""HTTP routes for the dashboard-auth OAuth round trip.Mounted at root (no prefix) by ``web_server.py``. The router does not-auto-gate; gating is performed by ``gated_auth_middleware``, which-allowlists everything under ``/auth/*`` and ``/api/auth/providers``.--The routes:-- GET /login → server-rendered login page- GET /auth/login?provider=N → 302 to IDP, sets PKCE cookie- GET /auth/callback?code,state → completes login, sets session cookies- POST /auth/logout → clears cookies, best-effort revoke- GET /api/auth/providers → list registered providers (login bootstrap)- GET /api/auth/me → current Session as JSON (auth-required)+auto-gate; ``gated_auth_middleware`` allowlists ``/auth/*``, ``/login`` and+``/api/auth/providers``.++ GET /login server-rendered login page+ GET /auth/login?provider=N 302 to IDP, sets PKCE cookie+ GET /auth/native/authorize RFC 8252 native-app (desktop) login start+ GET /auth/callback completes login, sets session cookies+ POST /auth/password-login username/password login (JSON)+ POST /auth/logout clears cookies, best-effort revoke+ POST /auth/native/token loopback code -> bearer tokens+ POST /auth/native/refresh desktop-held refresh token rotation+ GET /api/auth/providers list registered providers (login bootstrap)+ GET /api/auth/me current Session as JSON (auth-required)+ POST /api/auth/ws-ticket single-use WS upgrade ticket (auth-required)"""from __future__ import annotations@@ -20,6 +23,7 @@ import threadingimport timefrom collections import defaultdict, dequefrom typing import Any, Deque, Dict+from urllib.parse import quote, unquote, urlencode, urlparse, urlunparsefrom fastapi import APIRouter, HTTPException, Requestfrom fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse@@ -29,12 +33,16 @@ from hermes_cli.dashboard_auth import (get_provider,list_providers,list_session_providers,+ native_flow,)+from hermes_cli.dashboard_auth import prefix as _prefix_modfrom hermes_cli.dashboard_auth.audit import AuditEvent, audit_logfrom hermes_cli.dashboard_auth.base import (InvalidCodeError,InvalidCredentialsError,ProviderError,+ RefreshExpiredError,+ Session,)from hermes_cli.dashboard_auth.cookies import (clear_pkce_cookie,@@ -48,144 +56,172 @@ from hermes_cli.dashboard_auth.cookies import (set_session_cookies,)from hermes_cli.dashboard_auth.login_page import render_login_html+from hermes_cli.dashboard_auth.request_utils import (+ access_token_max_age,+ client_ip as _client_ip,+ is_safe_next_path,+)_log = logging.getLogger(__name__)router = APIRouter()+_NO_STORE = {"Cache-Control": "no-store, no-cache, must-revalidate"}+_NATIVE_EXPIRED_DETAIL = "Native login expired or unknown; restart sign-in."-def _redirect_uri(request: Request) -> str:- """Reconstruct the absolute callback URL the IDP redirects back to.-- Three resolution tiers:-- 1. ``HERMES_DASHBOARD_PUBLIC_URL`` env var or- ``dashboard.public_url`` in config.yaml — when set, this is- the complete authority (scheme + host + optional path prefix)- and we append ``/auth/callback`` verbatim. ``X-Forwarded-Prefix``- is IGNORED on this code path because the operator has declared- the public URL — we no longer need to guess from proxy headers,- and stacking the prefix on top would double-prefix the common- case where the prefix is already baked into ``public_url``.- Relief valve for deploys behind reverse proxies whose forwarded- headers aren't reliable.-- 2. ``X-Forwarded-Prefix: /hermes`` (Mission Control deploys) — we- prepend the prefix to the path FastAPI's ``url_for`` produces- (it doesn't natively honour this header — it isn't part of the- Starlette/uvicorn proxy_headers set).-- 3. Bare ``request.url_for("auth_callback")`` — under uvicorn's- ``proxy_headers=True`` this picks up the public https URL from- ``X-Forwarded-Host`` plus ``X-Forwarded-Proto``. Fly.io's- default path.- """- from urllib.parse import urlparse, urlunparse- from hermes_cli.dashboard_auth.prefix import (- prefix_from_request,- resolve_public_url,- )+def _prefix(request: Request) -> str:+ """Normalised ``X-Forwarded-Prefix`` (cookie name/Path + redirect URLs)."""+ return _prefix_mod.prefix_from_request(request)+- # Tier 1: operator-declared public URL.- public_url = resolve_public_url()+def _redirect_uri(request: Request) -> str:+ """Absolute ``/auth/callback`` URL handed to the IDP.++ Tiers: (1) operator-declared ``HERMES_DASHBOARD_PUBLIC_URL`` /+ ``dashboard.public_url`` is the complete authority — ``X-Forwarded-Prefix``+ is ignored so an already-baked-in prefix is not doubled; (2) otherwise+ ``url_for`` (honours ``X-Forwarded-Host/Proto`` under uvicorn+ ``proxy_headers``) with ``X-Forwarded-Prefix`` prepended to the path,+ which Starlette does not do natively.+ """+ public_url = _prefix_mod.resolve_public_url()if public_url:- # ``public_url`` is the complete authority (possibly with a- # path prefix already baked in). Append the auth callback path- # verbatim. ``resolve_public_url`` already stripped any trailing- # slash so we don't produce ``//auth/callback`` double-slashes.return f"{public_url}/auth/callback"-- # Tier 2 + 3: reconstruct from the request URL, optionally with- # X-Forwarded-Prefix layered on top of the path.base = str(request.url_for("auth_callback"))- prefix = prefix_from_request(request)+ prefix = _prefix(request)if not prefix:return baseparsed = urlparse(base)return urlunparse(parsed._replace(path=f"{prefix}{parsed.path}"))-def _client_ip(request: Request) -> str:- fwd = request.headers.get("x-forwarded-for", "")- if fwd:- return fwd.split(",")[0].strip()- return request.client.host if request.client else ""--def _provider_pkce_segments(cookie_payload: dict[str, str]) -> dict[str, str]:- """Segment dict from a provider's ``LoginStart.cookie_payload``.-- Providers serialise their PKCE material as the flat- ``state=…;verifier=…`` string documented on- :class:`~hermes_cli.dashboard_auth.base.LoginStart` (values are- ``token_urlsafe`` — never containing ``;`` or ``=``). This is the ONE- place that flat provider string is parsed; from here on the payload- is a dict all the way to :func:`set_pkce_cookie`'s base64url(JSON)- wire encoding.+ """Parse a provider's flat ``state=…;verifier=…`` PKCE string into a dict.++ The ONE place the provider's flat form is parsed; from here on the payload+ is a dict down to :func:`set_pkce_cookie`'s base64url(JSON) encoding."""flat = cookie_payload.get("hermes_session_pkce", "")- return dict(- seg.split("=", 1) for seg in flat.split(";") if "=" in seg- )+ return dict(seg.split("=", 1) for seg in flat.split(";") if "=" in seg)-def _prefix(request: Request) -> str:- """Resolve the X-Forwarded-Prefix header for the active request.+def _validate_post_login_target(raw: str) -> str:+ """Return ``raw`` (URL-decoded) if it is a safe same-origin path, else ``""``.- Local indirection so the routes pass a consistent value to the- cookie helpers (cookie name + Path attribute) and the gate's- redirect builders (login_url construction). See- ``hermes_cli.dashboard_auth.prefix`` for the normalisation rules.+ Re-validated at every hop (gate -> /login -> /auth/login -> cookie ->+ callback) because a ``next=`` value can re-enter via a crafted URL."""- from hermes_cli.dashboard_auth.prefix import prefix_from_request- return prefix_from_request(request)+ if not raw:+ return ""+ decoded = unquote(raw)+ return decoded if is_safe_next_path(decoded) else ""-# ----------------------------------------------------------------------------# Public: login page (server-rendered HTML, no SPA bundle)-# ---------------------------------------------------------------------------+def _set_pkce(resp, request: Request, payload: dict[str, str]) -> None:+ set_pkce_cookie(+ resp, payload=payload, use_https=detect_https(request), prefix=_prefix(request),+ )[email protected]("/login", name="login_page")-async def login_page(request: Request) -> HTMLResponse:- # Read the ``next=`` query the gate's ``_unauth_response`` set on- # the redirect URL. Validate against the same same-origin rules the- # callback applies (defence in depth — the gate already filters,- # but /login is reachable directly too).- next_path = _validate_post_login_target(- request.query_params.get("next", "")+def _set_session(resp, request: Request, session: Session) -> None:+ set_session_cookies(+ resp,+ access_token=session.access_token,+ refresh_token=session.refresh_token,+ access_token_expires_in=access_token_max_age(session),+ use_https=detect_https(request),+ prefix=_prefix(request),+ provider=session.provider,+ )+++def _bearer_payload(session: Session) -> dict[str, Any]:+ """JSON body for the native token/refresh endpoints (tokens in body, no cookie)."""+ return {+ "access_token": session.access_token,+ "refresh_token": session.refresh_token,+ "token_type": "Bearer",+ "expires_at": session.expires_at,+ "provider": session.provider,+ "user_id": session.user_id,+ }+++def _finish_native_login(+ request: Request, *, broker_state: str, session: Session, provider: str,+) -> str:+ """Mint the one-time loopback code for a pending native authorization.++ Shared tail of ``/auth/callback`` and ``/auth/password-login``: returns the+ desktop's ``redirect_uri?code=…&state=…``. No session cookies are set on+ the native path — the desktop redeems the code at ``/auth/native/token``.+ """+ ip = _client_ip(request)+ try:+ pending = native_flow.get_pending(broker_state)+ gw_code = native_flow.complete_pending(broker_state, session=session)+ except native_flow.NativeFlowError:+ audit_log(+ AuditEvent.NATIVE_TOKEN_FAILURE,+ provider=provider, reason="pending_not_found", ip=ip,+ )+ raise HTTPException(status_code=400, detail=_NATIVE_EXPIRED_DETAIL)+ sep = "&" if "?" in pending.redirect_uri else "?"+ loopback = (+ f"{pending.redirect_uri}{sep}"+ f"{urlencode({'code': gw_code, 'state': pending.client_state})}"+ )+ audit_log(+ AuditEvent.NATIVE_CODE_ISSUED,+ provider=provider, user_id=session.user_id, ip=ip,+ )+ return loopback+++def _login_failure(request: Request, provider: str, reason: str, **extra) -> None:+ audit_log(+ AuditEvent.LOGIN_FAILURE,+ provider=provider, reason=reason, **extra, ip=_client_ip(request),)- return HTMLResponse(- render_login_html(next_path=next_path),- headers={"Cache-Control": "no-store, no-cache, must-revalidate"},+++def _login_success(request: Request, session: Session, provider: str) -> None:+ audit_log(+ AuditEvent.LOGIN_SUCCESS,+ provider=provider,+ user_id=session.user_id,+ email=session.email,+ org_id=session.org_id,+ ip=_client_ip(request),)# ----------------------------------------------------------------------------# Public: provider list for the login-page bootstrap+# Public: login page + provider list# ---------------------------------------------------------------------------[email protected]("/login", name="login_page")+async def login_page(request: Request) -> HTMLResponse:+ # ``next=`` is set by the gate's redirect but /login is reachable directly,+ # so re-validate here too.+ next_path = _validate_post_login_target(request.query_params.get("next", ""))+ return HTMLResponse(render_login_html(next_path=next_path), headers=_NO_STORE)++@router.get("/api/auth/providers", name="auth_providers")async def api_auth_providers() -> Any:- # Advertise only interactive providers; a token-only credential (e.g. drain)- # is not a sign-in option.+ # Only interactive providers are sign-in options; fail closed on zero.providers = list_session_providers()if not providers:- # Q13: fail-closed when zero providers are registered.- return JSONResponse(- {"detail": "no auth providers registered"},- status_code=503,- )+ return JSONResponse({"detail": "no auth providers registered"}, status_code=503)return {"providers": [{"name": p.name,"display_name": p.display_name,- "supports_password": bool(- getattr(p, "supports_password", False)- ),+ "supports_password": bool(getattr(p, "supports_password", False)),}for p in providers],@@ -201,19 +237,14 @@ async def api_auth_providers() -> Any:async def auth_login(request: Request, provider: str, next: str = ""):p = get_provider(provider)if p is None:- raise HTTPException(- status_code=404,- detail=f"Unknown provider: {provider!r}",- )+ raise HTTPException(status_code=404, detail=f"Unknown provider: {provider!r}")if not getattr(p, "supports_session", True):raise HTTPException(status_code=404,detail=f"Provider does not support interactive login: {provider!r}",)+ safe_next = _validate_post_login_target(next)if getattr(p, "supports_password", False):- from urllib.parse import quote-- safe_next = _validate_post_login_target(next)login_url = f"{_prefix(request)}/login"if safe_next:login_url = f"{login_url}?next={quote(safe_next, safe='')}"@@ -222,45 +253,20 @@ async def auth_login(request: Request, provider: str, next: str = ""):try:ls = p.start_login(redirect_uri=_redirect_uri(request))except ProviderError as e:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=provider,- reason="provider_unreachable",- ip=_client_ip(request),- )- raise HTTPException(- status_code=503,- detail=f"Provider unreachable: {e}",- )+ _login_failure(request, provider, "provider_unreachable")+ raise HTTPException(status_code=503, detail=f"Provider unreachable: {e}")- audit_log(- AuditEvent.LOGIN_START,- provider=provider,- ip=_client_ip(request),- )+ audit_log(AuditEvent.LOGIN_START, provider=provider, ip=_client_ip(request))resp = RedirectResponse(url=ls.redirect_url, status_code=302)- # Pack the provider name into the PKCE cookie so the callback can- # find it without a separate cookie. Provider may or may not have- # already included a ``provider`` segment.+ # The PKCE cookie is the only server-controlled channel that survives the+ # IDP round trip (IDPs echo back only code+state), so it carries the+ # provider name and the pre-validated ``next`` path.pkce = _provider_pkce_segments(ls.cookie_payload)pkce.setdefault("provider", provider)- # Carry ``next=`` through the round trip in the PKCE cookie. Real- # IDPs only echo back ``code`` + ``state`` on the callback URL, so- # query-string transport would lose the value — the cookie is the- # only server-controlled channel that survives. Validate before we- # store it so an attacker who reaches /auth/login directly with- # ``next=//evil.example`` can't poison the cookie. Stored as the- # validator's decoded path VERBATIM — JSON has no delimiter to- # collide with, so no extra encoding layer (the callback validator's- # single unquote stays symmetric with the query-string decode).- safe_next = _validate_post_login_target(next)if safe_next:pkce["next"] = safe_next- set_pkce_cookie(- resp, payload=pkce, use_https=detect_https(request),- prefix=_prefix(request),- )+ _set_pkce(resp, request, pkce)return resp@@ -270,21 +276,13 @@ async def auth_login(request: Request, provider: str, next: str = ""):def _validate_loopback_redirect_uri(raw: str) -> str:- """Return ``raw`` if it is a safe loopback redirect_uri, else raise.-- RFC 8252 §7.3 restricts native-app redirects to the loopback interface.- We accept only ``http://127.0.0.1[:port]/...`` and ``http://[::1][:port]/...``- — literal loopback IPs. ``localhost`` is deliberately NOT accepted- (RFC 8252 §8.3: the name can resolve to a non-loopback address via the- hosts file or a hostile resolver, so clients "SHOULD use loopback IP- literals"; the desktop always sends ``127.0.0.1``).- A non-loopback host would let an attacker who can reach ``/auth/native/- authorize`` (a public route) turn the gateway's authenticated callback- into an open redirect that leaks a live authorization code to an- arbitrary origin — so this check is a security boundary, not ergonomics.- """- from urllib.parse import urlparse+ """Accept only ``http://127.0.0.1[:port]/…`` / ``http://[::1][:port]/…``.+ Security boundary, not ergonomics: /auth/native/authorize is public, so a+ non-loopback host would turn the callback into an open redirect leaking a+ live authorization code. ``localhost`` is rejected per RFC 8252 §8.3 (may+ resolve off-loopback); the desktop always sends a literal IP.+ """if not raw:raise HTTPException(status_code=400, detail="redirect_uri required")parsed = urlparse(raw)@@ -293,8 +291,7 @@ def _validate_loopback_redirect_uri(raw: str) -> str:status_code=400,detail="native redirect_uri must be http:// on the loopback interface",)- host = (parsed.hostname or "").lower()- if host not in ("127.0.0.1", "::1"):+ if (parsed.hostname or "").lower() not in ("127.0.0.1", "::1"):raise HTTPException(status_code=400,detail=(@@ -305,6 +302,29 @@ def _validate_loopback_redirect_uri(raw: str) -> str:return raw+def _select_native_provider(provider: str):+ """Resolve the provider for a native authorize request.++ An empty ``provider`` auto-selects when exactly one brokerable (non-password)+ session provider exists — password providers can never be the OAuth broker+ target, so they must not count, otherwise a normal OIDC+basic deployment+ would fail desktop login with a misleading "Unknown provider". With zero+ brokerable providers a lone password provider is still selected so the+ caller can emit an explanatory 400 rather than a 404.+ """+ if provider:+ return get_provider(provider)+ sess_providers = list_session_providers()+ native_eligible = [+ pp for pp in sess_providers if not getattr(pp, "supports_password", False)+ ]+ if len(native_eligible) == 1:+ return native_eligible[0]+ if not native_eligible and len(sess_providers) == 1:+ return sess_providers[0]+ return None++@router.get("/auth/native/authorize", name="auth_native_authorize")async def auth_native_authorize(request: Request,@@ -316,71 +336,29 @@ async def auth_native_authorize():"""Begin an RFC 8252 native-app login for the desktop app.- The desktop opens THIS url in the system browser with its own PKCE- ``code_challenge`` (S256), a loopback ``redirect_uri``, and a CSRF- ``state``. We stash a pending broker authorization, then hand off to the- EXISTING upstream PKCE round trip (``provider.start_login`` → IDP →- ``/auth/callback``), carrying the broker_state in the same PKCE cookie the- cookie flow uses. On the callback we mint a loopback code (see- ``auth_callback``); no browser session cookie is ever set for the desktop.-- Password providers have no upstream IDP round trip to broker, but the- native flow is still exactly what they want: it moves sign-in out of the- desktop's embedded webview (where OS password managers cannot autofill)- into the SYSTEM browser (where they can). For a ``supports_password``- provider we redirect to the interactive ``/login`` form instead of an- IDP, carrying the broker_state in the PKCE cookie; a successful- ``/auth/password-login`` then completes the pending authorization and- bounces the browser to the loopback redirect (see that route).+ Stashes a pending broker authorization keyed by an opaque ``broker_state``+ that rides in the gateway's own PKCE cookie, then runs the existing+ upstream round trip (``start_login`` -> IDP -> ``/auth/callback``). The+ desktop's challenge/state never touch the cookie. Password providers are+ sent to the interactive ``/login`` form instead of an IDP (system browser+ => OS password-manager autofill); ``/auth/password-login`` then completes+ the pending authorization."""- # PKCE method must be S256 (RFC 7636 — plain is disallowed for native apps).if code_challenge_method.upper() != "S256":- raise HTTPException(- status_code=400,- detail="code_challenge_method must be S256",- )+ raise HTTPException(status_code=400, detail="code_challenge_method must be S256")if not code_challenge:raise HTTPException(status_code=400, detail="code_challenge required")_validate_loopback_redirect_uri(redirect_uri)- # Resolve the provider. With exactly one brokerable session provider- # registered (the common hosted case) an empty ``provider`` selects it,- # mirroring the auto-SSO convenience so the desktop needn't hardcode the- # name. Password providers are session providers too, but they can never- # be the target of the native OAuth broker flow (rejected below), so they- # must not count toward "exactly one candidate": otherwise a normal- # SSO-with-password-fallback deployment (one OIDC provider + the bundled- # ``basic`` provider) would see two session providers, skip the- # auto-select, and fail desktop login with a misleading "Unknown provider".- p = get_provider(provider) if provider else None- if p is None and not provider:- native_eligible = [- pp- for pp in list_session_providers()- if not getattr(pp, "supports_password", False)- ]- if len(native_eligible) == 1:- p = native_eligible[0]- elif not native_eligible:- # No brokerable provider at all. Preserve the old behaviour of- # selecting a lone password provider so the explicit 400 below- # (rather than a 404) explains why native OAuth is unavailable.- sess_providers = list_session_providers()- if len(sess_providers) == 1:- p = sess_providers[0]+ p = _select_native_provider(provider)if p is None:- raise HTTPException(- status_code=404, detail=f"Unknown provider: {provider!r}"- )+ raise HTTPException(status_code=404, detail=f"Unknown provider: {provider!r}")if not getattr(p, "supports_session", True):- # Token-only credentials (e.g. drain) are not interactive sign-ins.raise HTTPException(status_code=400,detail=f"Provider does not support native login: {p.name!r}",)- from hermes_cli.dashboard_auth import native_flow-try:broker_state = native_flow.register_pending(code_challenge=code_challenge,@@ -392,27 +370,11 @@ async def auth_native_authorize(raise HTTPException(status_code=503, detail=str(e))if getattr(p, "supports_password", False):- # Password provider: no IDP to redirect through. Land the system- # browser on the interactive /login form with the broker_state in- # the PKCE cookie (the same server-controlled channel the OAuth- # branch uses); /auth/password-login picks it up on success and- # 302s the browser to the desktop's loopback redirect_uri. The- # desktop's challenge/state never touch the cookie — only our- # opaque broker_state does.audit_log(- AuditEvent.NATIVE_AUTHORIZE_START,- provider=p.name,- ip=_client_ip(request),- )- resp = RedirectResponse(- url=f"{_prefix(request)}/login", status_code=302- )- set_pkce_cookie(- resp,- payload={"provider": p.name, "broker": broker_state},- use_https=detect_https(request),- prefix=_prefix(request),+ AuditEvent.NATIVE_AUTHORIZE_START, provider=p.name, ip=_client_ip(request),)+ resp = RedirectResponse(url=f"{_prefix(request)}/login", status_code=302)+ _set_pkce(resp, request, {"provider": p.name, "broker": broker_state})return resptry:@@ -420,24 +382,13 @@ async def auth_native_authorize(except ProviderError as e:raise HTTPException(status_code=503, detail=f"Provider unreachable: {e}")- audit_log(- AuditEvent.NATIVE_AUTHORIZE_START,- provider=p.name,- ip=_client_ip(request),- )+ audit_log(AuditEvent.NATIVE_AUTHORIZE_START, provider=p.name, ip=_client_ip(request))resp = RedirectResponse(url=ls.redirect_url, status_code=302)- # Thread the provider name + broker_state through the gateway's OWN PKCE- # cookie so the callback can (a) dispatch to the right provider and (b)- # find the pending native authorization. The desktop's challenge/state- # never touch this cookie — only our opaque broker_state does.pkce = _provider_pkce_segments(ls.cookie_payload)pkce.setdefault("provider", p.name)pkce["broker"] = broker_state- set_pkce_cookie(- resp, payload=pkce, use_https=detect_https(request),- prefix=_prefix(request),- )+ _set_pkce(resp, request, pkce)return resp@@ -452,223 +403,81 @@ async def auth_callback(pkce_raw = read_pkce_cookie(request)if not pkce_raw:audit_log(- AuditEvent.LOGIN_FAILURE,- reason="missing_pkce_cookie",- ip=_client_ip(request),- )- raise HTTPException(- status_code=400,- detail="Missing PKCE state cookie",+ AuditEvent.LOGIN_FAILURE, reason="missing_pkce_cookie", ip=_client_ip(request),)+ raise HTTPException(status_code=400, detail="Missing PKCE state cookie")- # Parse the segment dict (``provider`` / ``state`` / ``verifier`` /- # ``next`` — ``next`` only present when /auth/login was given a- # next= query). parse_pkce_payload decodes the base64url(JSON) wire- # value, with a compatibility ladder for the two legacy flat formats- # (see its docstring).+ # ``next`` and ``broker`` are read from the server-set cookie ONLY: the IDP+ # echoes back just code+state, so any such query param here is attacker+ # controlled.parts = parse_pkce_payload(pkce_raw)provider_name = parts.get("provider", "")expected_state = parts.get("state", "")verifier = parts.get("verifier", "")- # Read next= from the cookie ONLY. The IDP doesn't echo next= back- # on the callback URL (it only carries ``code`` + ``state``), so any- # next= query parameter on the callback URL is attacker-controlled- # and MUST be ignored.next_from_cookie = parts.get("next", "")- # RFC 8252 native-app flow: /auth/native/authorize stashed a broker_state- # here so this callback can mint a loopback authorization code for the- # desktop instead of setting browser session cookies. Absent for the- # ordinary cookie/SPA login.broker_state = parts.get("broker", "")p = get_provider(provider_name)if p is None:raise HTTPException(- status_code=400,- detail=f"Unknown provider in cookie: {provider_name!r}",+ status_code=400, detail=f"Unknown provider in cookie: {provider_name!r}",)if error:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=provider_name,- reason="idp_error",- error=error,- ip=_client_ip(request),- )+ _login_failure(request, provider_name, "idp_error", error=error)raise HTTPException(status_code=400,detail=f"OAuth error from provider: {error} ({error_description})",)if not state or state != expected_state:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=provider_name,- reason="state_mismatch",- ip=_client_ip(request),- )+ _login_failure(request, provider_name, "state_mismatch")raise HTTPException(- status_code=400,- detail="OAuth state mismatch (CSRF check failed)",+ status_code=400, detail="OAuth state mismatch (CSRF check failed)",)try:session = p.complete_login(- code=code,- state=state,- code_verifier=verifier,+ code=code, state=state, code_verifier=verifier,redirect_uri=_redirect_uri(request),)except InvalidCodeError as e:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=provider_name,- reason="invalid_code",- ip=_client_ip(request),- )+ _login_failure(request, provider_name, "invalid_code")raise HTTPException(status_code=400, detail=f"Invalid code: {e}")except ProviderError as e:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=provider_name,- reason="provider_unreachable",- ip=_client_ip(request),- )- raise HTTPException(- status_code=503,- detail=f"Provider unreachable: {e}",- )-- audit_log(- AuditEvent.LOGIN_SUCCESS,- provider=provider_name,- user_id=session.user_id,- email=session.email,- org_id=session.org_id,- ip=_client_ip(request),- )+ _login_failure(request, provider_name, "provider_unreachable")+ raise HTTPException(status_code=503, detail=f"Provider unreachable: {e}")- expires_in = max(60, session.expires_at - int(time.time()))+ _login_success(request, session, provider_name)- # RFC 8252 native-app branch: the desktop initiated this via- # /auth/native/authorize and is waiting on a loopback listener. Mint a- # one-time gateway authorization code bound to the desktop's PKCE- # challenge and 302 the SYSTEM BROWSER to the desktop's loopback- # redirect_uri — no session cookies are set on this response, and the- # tokens are handed to the desktop only at /auth/native/token. This is- # what lets the desktop avoid both the embedded webview and cookie auth.+ https = detect_https(request)+ prefix = _prefix(request)if broker_state:- from hermes_cli.dashboard_auth import native_flow-- try:- pending = native_flow.get_pending(broker_state)- gw_code = native_flow.complete_pending(- broker_state, session=session- )- except native_flow.NativeFlowError:- audit_log(- AuditEvent.NATIVE_TOKEN_FAILURE,- provider=provider_name,- reason="pending_not_found",- ip=_client_ip(request),- )- raise HTTPException(- status_code=400,- detail="Native login expired or unknown; restart sign-in.",- )- from urllib.parse import urlencode-- sep = "&" if "?" in pending.redirect_uri else "?"- loopback = (- f"{pending.redirect_uri}{sep}"- f"{urlencode({'code': gw_code, 'state': pending.client_state})}"- )- audit_log(- AuditEvent.NATIVE_CODE_ISSUED,- provider=provider_name,- user_id=session.user_id,- ip=_client_ip(request),+ loopback = _finish_native_login(+ request, broker_state=broker_state, session=session, provider=provider_name,)resp = RedirectResponse(url=loopback, status_code=302)- # Clear the PKCE cookie (its job is done) but set NO session cookies:- # the desktop is not a browser session, it redeems the code for a- # bearer token it stores itself.- clear_pkce_cookie(resp, use_https=detect_https(request), prefix=_prefix(request))- clear_sso_attempt_cookie(resp, prefix=_prefix(request))+ clear_pkce_cookie(resp, use_https=https, prefix=prefix)+ clear_sso_attempt_cookie(resp, prefix=prefix)return resp- # Honour the ``next=`` value the gate's _unauth_response set in the- # /login redirect URL and that /auth/login persisted into the PKCE- # cookie. We re-validate against the same-origin rules here — the- # cookie is server-set so this is defence in depth, but a regression- # that lets attacker-controlled bytes into the cookie would otherwise- # produce an open redirect.landing = _validate_post_login_target(next_from_cookie) or "/"resp = RedirectResponse(url=landing, status_code=302)- set_session_cookies(- resp,- access_token=session.access_token,- refresh_token=session.refresh_token,- access_token_expires_in=expires_in,- use_https=detect_https(request),- prefix=_prefix(request),- provider=session.provider,- )- clear_pkce_cookie(resp, use_https=detect_https(request), prefix=_prefix(request))- # Clear the one-shot auto-SSO loop-guard marker now that login succeeded,- # so it never lingers to suppress a future silent attempt after logout.- clear_sso_attempt_cookie(resp, prefix=_prefix(request))+ _set_session(resp, request, session)+ clear_pkce_cookie(resp, use_https=https, prefix=prefix)+ # Clear the one-shot auto-SSO loop-guard so it never suppresses a future+ # silent attempt after logout.+ clear_sso_attempt_cookie(resp, prefix=prefix)return resp-def _validate_post_login_target(raw: str) -> str:- """Return ``raw`` if it's a safe same-origin path, else empty string.-- The ``next`` query param survives a full OAuth round trip — the gate- encodes it into the /login redirect, the login page emits it back into- /auth/login, and the IDP preserves it across /authorize/callback. We- have to re-validate here because the value came back in via the- URL (an attacker could craft a /auth/callback URL with their own- ``next=https://evil.example``).- """- if not raw:- return ""- from urllib.parse import unquote- decoded = unquote(raw)- if not decoded.startswith("/") or decoded.startswith("//"):- return ""- # Don't loop back to login pages or auth flow.- if any(- decoded == p or decoded.startswith(p)- for p in ("/login", "/auth/", "/api/auth/")- ):- return ""- # Reject any ``/api/*`` target. The gate's ``_safe_next_target``- # already filters these out before they reach the cookie, but a- # malicious or stale ``next=`` value that re-enters via the- # callback URL must not be honoured: a successful redirect to an- # API endpoint renders raw JSON in the browser address bar — never- # a useful post-login destination, and indistinguishable from an- # attacker trying to weaponise the redirect.- if decoded == "/api" or decoded.startswith("/api/"):- return ""- return decoded--# ---------------------------------------------------------------------------# Public: password (non-redirect) login# ---------------------------------------------------------------------------#-# Brute-force throttle. The OAuth flow has no guessable secret on our side-# (the IDP owns credentials), but ``/auth/password-login`` accepts a-# password we verify locally, so it's a credential-stuffing target. A-# simple in-process sliding-window limiter per client IP raises the cost-# of online guessing without any external dependency. It is intentionally-# best-effort: process-local (resets on restart), and behind a trusting-# proxy the IP is the proxy's unless X-Forwarded-For is set — which is why-# this is defence-in-depth on top of the provider's own constant-time-# verify, not the only line of defence.+# Brute-force throttle: a process-local sliding window per client IP. Best+# effort defence-in-depth on top of the provider's constant-time verify (resets+# on restart; behind a proxy the IP is the proxy's unless X-Forwarded-For)._PW_RATE_MAX_ATTEMPTS = 10_PW_RATE_WINDOW_SEC = 60.0@@ -677,19 +486,14 @@ _pw_attempts_lock = threading.Lock()def _password_rate_limited(ip: str) -> bool:- """True if ``ip`` has exceeded the password-login attempt budget.+ """True if ``ip`` exceeded the budget; records the attempt when allowed.- Sliding window: prune attempts older than the window, then check the- count. Records the attempt timestamp when allowed. An empty IP (no- discernible client) shares a single bucket — fail-safe toward- throttling rather than letting unattributable traffic through- unmetered.+ An empty IP shares one bucket — fail-safe toward throttling."""now = time.monotonic()cutoff = now - _PW_RATE_WINDOW_SEC- key = ip or "_unknown_"with _pw_attempts_lock:- bucket = _pw_attempts[key]+ bucket = _pw_attempts[ip or "_unknown_"]while bucket and bucket[0] < cutoff:bucket.popleft()if len(bucket) >= _PW_RATE_MAX_ATTEMPTS:@@ -715,64 +519,30 @@ class _PasswordLoginBody(BaseModel):async def auth_password_login(request: Request, body: _PasswordLoginBody):"""Authenticate a username/password against a password provider.- Mirrors the cookie-minting tail of ``/auth/callback`` but skips the- PKCE/state/code machinery (those are OAuth-only). On success sets the- session cookies and returns JSON ``{"ok": true, "next": <path>}`` —- the credential form POSTs via fetch and navigates client-side, so a- 302 (which fetch follows opaquely) is the wrong shape here.-- RFC 8252 native-app branch: when ``/auth/native/authorize`` sent this- browser to ``/login`` (password provider), the PKCE cookie carries the- opaque ``broker=`` handle. Mirroring the ``/auth/callback`` native- branch, success then mints a one-time loopback code instead of a- browser session: ``next`` is the desktop's loopback redirect_uri- (validated at authorize time) carrying ``code`` + ``state``, and NO- session cookies are set — the desktop redeems the code at- ``/auth/native/token`` for bearer tokens it stores itself.-- Failure modes, all deliberately generic so the endpoint can't be used- as a username oracle or a provider-enumeration oracle:- * unknown provider / provider lacks password support → 404- * bad credentials → 401 ("Invalid credentials")- * backing store unreachable → 503- * too many attempts from this IP → 429+ Returns JSON ``{"ok": true, "next": <path>}`` (the form POSTs via fetch,+ which follows a 302 opaquely) and sets the session cookies. When the PKCE+ cookie carries a native ``broker`` handle, ``next`` is instead the+ desktop's loopback redirect and NO cookies are set.++ Failure modes are deliberately generic (no username/provider oracle):+ unknown or non-password provider -> 404; bad credentials -> 401; backing+ store unreachable -> 503; too many attempts from this IP -> 429."""ip = _client_ip(request)if _password_rate_limited(ip):- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=body.provider,- reason="rate_limited",- ip=ip,- )+ _login_failure(request, body.provider, "rate_limited")raise HTTPException(- status_code=429,- detail="Too many login attempts. Try again shortly.",+ status_code=429, detail="Too many login attempts. Try again shortly.",)p = get_provider(body.provider)if p is None or not getattr(p, "supports_password", False):- # Don't leak which providers exist or which support passwords —- # same 404 whether the provider is unknown or OAuth-only.- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=body.provider,- reason="unknown_password_provider",- ip=ip,- )+ _login_failure(request, body.provider, "unknown_password_provider")raise HTTPException(status_code=404, detail="Unknown provider")- # Native-app branch discriminator (see docstring): a broker handle in- # the PKCE cookie means this sign-in was initiated by- # /auth/native/authorize for a desktop app, not a browser session. The- # cookie is server-set (never client-supplied), so it is trustworthy —- # and it also records WHICH provider the native flow was initiated for.- # /login renders a form for every session provider, so without this- # check a flow started for provider A could be completed with provider- # B's credentials, binding B's session into A's pending authorization.- # Enforce equality BEFORE verifying credentials: nothing is minted, the- # pending authorization is preserved, and the user can submit the form- # the flow was actually started for.+ # The native broker handle also records WHICH provider the flow was started+ # for. Enforce equality BEFORE verifying credentials so a flow started for+ # provider A cannot be completed with provider B's credentials.broker_state = ""cookie_provider = ""pkce_raw = read_pkce_cookie(request)@@ -783,9 +553,7 @@ async def auth_password_login(request: Request, body: _PasswordLoginBody):if broker_state and cookie_provider != body.provider:audit_log(AuditEvent.NATIVE_TOKEN_FAILURE,- provider=body.provider,- reason="provider_mismatch",- ip=ip,+ provider=body.provider, reason="provider_mismatch", ip=ip,)raise HTTPException(status_code=400,@@ -800,90 +568,28 @@ async def auth_password_login(request: Request, body: _PasswordLoginBody):username=body.username, password=body.password)except InvalidCredentialsError:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=body.provider,- reason="invalid_credentials",- ip=ip,- )- # Generic message — never distinguish unknown-user from wrong-password.+ _login_failure(request, body.provider, "invalid_credentials")raise HTTPException(status_code=401, detail="Invalid credentials")except NotImplementedError:- # supports_password was True but the method isn't actually- # implemented — a provider bug, not a client error.+ # supports_password True but method not implemented: a provider bug.raise HTTPException(status_code=500, detail="Provider misconfigured")except ProviderError as e:- audit_log(- AuditEvent.LOGIN_FAILURE,- provider=body.provider,- reason="provider_unreachable",- ip=ip,- )+ _login_failure(request, body.provider, "provider_unreachable")raise HTTPException(status_code=503, detail=f"Provider unreachable: {e}")- audit_log(- AuditEvent.LOGIN_SUCCESS,- provider=body.provider,- user_id=session.user_id,- email=session.email,- org_id=session.org_id,- ip=ip,- )+ _login_success(request, session, body.provider)- # Native-app branch: the broker handle was parsed (and its provider- # binding enforced) above, before credential verification.if broker_state:- from hermes_cli.dashboard_auth import native_flow-- try:- pending = native_flow.get_pending(broker_state)- gw_code = native_flow.complete_pending(- broker_state, session=session- )- except native_flow.NativeFlowError:- audit_log(- AuditEvent.NATIVE_TOKEN_FAILURE,- provider=body.provider,- reason="pending_not_found",- ip=ip,- )- raise HTTPException(- status_code=400,- detail="Native login expired or unknown; restart sign-in.",- )- from urllib.parse import urlencode-- sep = "&" if "?" in pending.redirect_uri else "?"- loopback = (- f"{pending.redirect_uri}{sep}"- f"{urlencode({'code': gw_code, 'state': pending.client_state})}"+ loopback = _finish_native_login(+ request, broker_state=broker_state, session=session, provider=body.provider,)- audit_log(- AuditEvent.NATIVE_CODE_ISSUED,- provider=body.provider,- user_id=session.user_id,- ip=ip,- )- # The login page's form script navigates to ``next`` — here the- # loopback listener, which answers with its own "you can close- # this window" page. No session cookies: the desktop is not a- # browser session (mirrors the /auth/callback native branch).resp = JSONResponse({"ok": True, "next": loopback})clear_pkce_cookie(resp, use_https=detect_https(request), prefix=_prefix(request))return resp- expires_in = max(60, session.expires_at - int(time.time()))landing = _validate_post_login_target(body.next) or "/"resp = JSONResponse({"ok": True, "next": landing})- set_session_cookies(- resp,- access_token=session.access_token,- refresh_token=session.refresh_token,- access_token_expires_in=expires_in,- use_https=detect_https(request),- prefix=_prefix(request),- provider=session.provider,- )+ _set_session(resp, request, session)return resp@@ -891,16 +597,13 @@ async def auth_password_login(request: Request, body: _PasswordLoginBody):async def auth_logout(request: Request):_at, rt = read_session_cookies(request)if rt:- # Best-effort revoke. Try every provider so a session minted by- # any registered provider is revoked correctly. Failures are- # logged but never raised.+ # Best-effort revoke on every provider; failures logged, never raised.for provider in list_providers():try:provider.revoke_session(refresh_token=rt)except Exception as e: # noqa: BLE001 — best-effort_log.warning(- "dashboard-auth: revoke on %r failed: %s",- provider.name, e,+ "dashboard-auth: revoke on %r failed: %s", provider.name, e,)sess = getattr(request.state, "session", None)@@ -919,16 +622,21 @@ async def auth_logout(request: Request):# ----------------------------------------------------------------------------# Auth-required: identity probe for the SPA+# Auth-required: identity probe + WS ticket for the SPA# ---------------------------------------------------------------------------[email protected]("/api/auth/me", name="auth_me")-async def api_auth_me(request: Request):- """Return the verified session as JSON. Auth-required (gate enforces)."""+def _require_session(request: Request):sess = getattr(request.state, "session", None)if sess is None:raise HTTPException(status_code=401, detail="Unauthorized")+ return sess++[email protected]("/api/auth/me", name="auth_me")+async def api_auth_me(request: Request):+ """Return the verified session as JSON. Auth-required (gate enforces)."""+ sess = _require_session(request)return {"user_id": sess.user_id,"email": sess.email,@@ -939,45 +647,24 @@ async def api_auth_me(request: Request):}-# ----------------------------------------------------------------------------# Auth-required: WS upgrade ticket (Phase 5)-# -----------------------------------------------------------------------------@router.post("/api/auth/ws-ticket", name="auth_ws_ticket")async def api_auth_ws_ticket(request: Request):- """Mint a short-lived single-use ticket for the authenticated session.-- Browsers cannot set ``Authorization`` on a WebSocket upgrade, so in- gated mode the SPA POSTs this endpoint to get a ``?ticket=`` value to- append to ``/api/pty``, ``/api/console``, ``/api/ws``, ``/api/pub``, or- ``/api/events``.-- The ticket has a 30-second TTL and is single-use. Calling this endpoint- multiple times in quick succession (e.g. one ticket per WS) is the- expected pattern.+ """Mint a 30s single-use ticket for a WS upgrade (browsers cannot set+ ``Authorization`` on the upgrade). One ticket per WS is the expected pattern."""- sess = getattr(request.state, "session", None)- if sess is None:- # Middleware should already have rejected, but check defensively.- raise HTTPException(status_code=401, detail="Unauthorized")-- # Import here so the routes module stays usable in test contexts that- # don't load the ticket store.+ sess = _require_session(request)from hermes_cli.dashboard_auth.ws_tickets import TTL_SECONDS, mint_ticketticket = mint_ticket(user_id=sess.user_id, provider=sess.provider)audit_log(AuditEvent.WS_TICKET_MINTED,- provider=sess.provider,- user_id=sess.user_id,- ip=_client_ip(request),+ provider=sess.provider, user_id=sess.user_id, ip=_client_ip(request),)return {"ticket": ticket, "ttl_seconds": TTL_SECONDS}# ----------------------------------------------------------------------------# Public: RFC 8252 native-app token exchange (loopback code → bearer tokens)+# Public: RFC 8252 native-app token exchange + refresh# ---------------------------------------------------------------------------@@ -990,19 +677,10 @@ class _NativeTokenBody(BaseModel):async def auth_native_token(request: Request, body: _NativeTokenBody):"""Exchange a loopback gateway code + PKCE verifier for bearer tokens.- The desktop POSTs this from its loopback listener after catching the- ``?code=`` redirect. We verify ``SHA256(code_verifier) == code_challenge``- (the challenge captured at ``/auth/native/authorize``), consume the code- (single use), and return the upstream tokens **in the JSON body** — the- desktop stores them in the OS keychain and authenticates with- ``Authorization: Bearer`` thereafter. No cookie is set on this response.-- Failure modes (all deliberately generic — the code is consumed on every- path so there is no verifier oracle and no replay):- * unknown / expired / already-redeemed code, or PKCE mismatch → 400+ The code is consumed on every path (no verifier oracle, no replay); any+ unknown/expired/redeemed code or PKCE mismatch is a generic 400. Tokens go+ in the JSON body; no cookie is set."""- from hermes_cli.dashboard_auth import native_flow-try:session = native_flow.redeem_code(code=body.code, code_verifier=body.code_verifier@@ -1010,28 +688,17 @@ async def auth_native_token(request: Request, body: _NativeTokenBody):except native_flow.CodeInvalid:audit_log(AuditEvent.NATIVE_TOKEN_FAILURE,- reason="invalid_code_or_pkce",- ip=_client_ip(request),+ reason="invalid_code_or_pkce", ip=_client_ip(request),)raise HTTPException(- status_code=400,- detail="Invalid or expired authorization code.",+ status_code=400, detail="Invalid or expired authorization code.",)audit_log(AuditEvent.NATIVE_TOKEN_SUCCESS,- provider=session.provider,- user_id=session.user_id,- ip=_client_ip(request),+ provider=session.provider, user_id=session.user_id, ip=_client_ip(request),)- return {- "access_token": session.access_token,- "refresh_token": session.refresh_token,- "token_type": "Bearer",- "expires_at": session.expires_at,- "provider": session.provider,- "user_id": session.user_id,- }+ return _bearer_payload(session)class _NativeRefreshBody(BaseModel):@@ -1041,22 +708,12 @@ class _NativeRefreshBody(BaseModel):@router.post("/auth/native/refresh", name="auth_native_refresh")async def auth_native_refresh(request: Request, body: _NativeRefreshBody):- """Rotate a native-app session using the desktop-held refresh token.-- The desktop owns its refresh token (OS keychain) rather than a cookie, so- it rotates here instead of relying on the gate's transparent cookie- rotation. Mirrors the middleware's ``_attempt_refresh`` provider stacking:- tries each session provider until one rotates the token, returning the new- access/refresh pair **in the JSON body**.-- Failure modes:- * every provider rejects the RT (dead/expired/reuse-detected) → 401- ``session_expired`` so the desktop starts a fresh native login;- * a provider's IDP is unreachable and none rotated → 503.- """- from hermes_cli.dashboard_auth import list_session_providers- from hermes_cli.dashboard_auth.base import RefreshExpiredError+ """Rotate a desktop-held refresh token (mirrors the gate's ``_attempt_refresh``).+ Tries each session provider (hinted one first) until one rotates the+ token. Every provider rejecting the RT -> 401 ``session_expired`` (desktop+ starts a fresh login); none rotated and one unreachable -> 503.+ """if not body.refresh_token:raise HTTPException(status_code=400, detail="refresh_token required")@@ -1080,28 +737,17 @@ async def auth_native_refresh(request: Request, body: _NativeRefreshBody):continueaudit_log(AuditEvent.REFRESH_SUCCESS,- provider=session.provider,- user_id=session.user_id,- ip=_client_ip(request),+ provider=session.provider, user_id=session.user_id, ip=_client_ip(request),)- return {- "access_token": session.access_token,- "refresh_token": session.refresh_token,- "token_type": "Bearer",- "expires_at": session.expires_at,- "provider": session.provider,- "user_id": session.user_id,- }+ return _bearer_payload(session)if unreachable is not None:raise HTTPException(- status_code=503,- detail=f"Auth provider {unreachable!r} unreachable",+ status_code=503, detail=f"Auth provider {unreachable!r} unreachable",)audit_log(AuditEvent.REFRESH_FAILURE,- reason="all_providers_rejected_rt",- ip=_client_ip(request),+ reason="all_providers_rejected_rt", ip=_client_ip(request),)return JSONResponse({diff --git a/hermes_cli/dashboard_auth/token_auth.py b/hermes_cli/dashboard_auth/token_auth.pyindex 320b4cdb52b..821f5531a8d 100644--- a/hermes_cli/dashboard_auth/token_auth.py+++ b/hermes_cli/dashboard_auth/token_auth.py@@ -1,40 +1,16 @@"""Route-agnostic non-interactive (bearer-token) auth seam for the dashboard.-This is the generic API-token capability (decisions.md Q-C): a reusable seam-that ANY service-to-service / machine-credential provider plugs into, NOT a-drain-specific hook. The drain bearer-secret plugin is merely the first-consumer.--How it fits the existing auth framework:-- * The interactive gate (``gated_auth_middleware``) authenticates a human- via a session cookie on every non-public route. A service caller has no- cookie — it presents a bearer token in the ``Authorization`` header on a- single request. That is what this seam verifies.-- * A route opts in by registering its exact path via- :func:`register_token_route`. Only registered paths are token-authable;- everything else is untouched, so this can never accidentally widen the- auth surface of an existing route.-- * :func:`token_auth_middleware` runs OUTERMOST (installed last in- ``web_server.py``). For a token route it fully owns the auth decision:- authenticate via the stacked token providers, attach the verified- :class:`~hermes_cli.dashboard_auth.base.TokenPrincipal` to- ``request.state.token_principal`` + set ``request.state.token_authenticated``,- and pass through; otherwise reject (401 unauthenticated, or 503 when a- provider's backing store was unreachable). The downstream cookie/session- gates honour ``token_authenticated`` and skip enforcement, so a- token-authed service request is never bounced to ``/login``.-- * Fails closed: a token route with no registered token provider, no token,- or an unrecognised token gets 401 — never an open pass-through.--Provider stacking mirrors ``verify_session``: each ``supports_token`` provider-is consulted in registration order until one returns a principal. A provider-that doesn't recognise the token returns ``None`` and the seam moves on; a-provider whose backing store is unreachable raises ``ProviderError``, which the-seam remembers and surfaces as 503 only if NO provider accepts the token.+The generic API-token capability any machine-credential provider plugs into+(the drain bearer-secret plugin is merely the first consumer). A route opts in+by registering its exact path via :func:`register_token_route`; only registered+paths are token-authable, so the auth surface of existing routes never widens.++:func:`token_auth_middleware` runs OUTERMOST (installed last in web_server.py)+and fully owns the decision for a token route: a recognised token attaches+``request.state.token_principal`` + ``token_authenticated`` (the cookie gates+honour that flag and never bounce the request to /login); otherwise 401, or+503 when a provider's backing store was unreachable. Fails closed: no+provider, no token, or an unrecognised token is always 401."""from __future__ import annotations@@ -48,22 +24,21 @@ from fastapi.responses import JSONResponse, Responsefrom hermes_cli.dashboard_auth import list_token_providersfrom hermes_cli.dashboard_auth.audit import AuditEvent, audit_logfrom hermes_cli.dashboard_auth.base import ProviderError, TokenPrincipal+from hermes_cli.dashboard_auth.request_utils import (+ client_ip as _client_ip,+ extract_bearer as extract_bearer_token,+ unreachable_response,+)_log = logging.getLogger(__name__)-# Exact paths that accept non-interactive bearer-token auth. A route registers-# itself here at import/startup; the seam only acts on registered paths.-_token_routes: set[str] = set()+_token_routes: set[str] = set() # exact paths that accept bearer-token auth_lock = threading.Lock()def register_token_route(path: str) -> None:- """Mark ``path`` (exact match) as token-authable.-- Idempotent. Call at module import / app setup so the seam knows which- routes to guard. Registering a route does NOT make it public — it makes- it authenticate by token instead of by session cookie.- """+ """Mark ``path`` (exact match) as token-authable. Idempotent; does NOT make+ the route public — it authenticates by token instead of by cookie."""with _lock:_token_routes.add(path)@@ -80,40 +55,15 @@ def clear_token_routes() -> None:_token_routes.clear()-def _client_ip(request: Request) -> str:- fwd = request.headers.get("x-forwarded-for", "")- if fwd:- return fwd.split(",")[0].strip()- return request.client.host if request.client else ""---def extract_bearer_token(request: Request) -> str:- """Return the bearer token from the ``Authorization`` header, or "".-- Accepts ``<scheme> <token>`` where scheme is "bearer" (case-insensitive).- Returns an empty string for a missing/malformed header or a non-bearer- scheme — the caller treats "" as "no token presented".- """- auth = request.headers.get("authorization", "")- parts = auth.split(" ", 1)- if len(parts) == 2 and parts[0].strip().lower() == "bearer":- return parts[1].strip()- return ""--def authenticate_token(request: Request,) -> Tuple[Optional[TokenPrincipal], Optional[str]]:"""Try every token provider against the request's bearer token.- Returns ``(principal, unreachable_provider_name)``:- * ``(TokenPrincipal, None)`` — a provider recognised and accepted the token.- * ``(None, None)`` — no token, or no provider recognised it (reject 401).- * ``(None, name)`` — no provider accepted it AND at least one provider's- backing store was unreachable (the caller surfaces 503, not 401, so a- transient outage doesn't read as "bad credentials").-- Never raises: a provider ``ProviderError`` is caught and remembered.+ Returns ``(principal, None)`` on success; ``(None, None)`` for no token or+ no recogniser (401); ``(None, name)`` when no provider accepted it AND at+ least one was unreachable (caller surfaces 503, not "bad credentials").+ Never raises — a buggy provider must not 500 the gate."""token = extract_bearer_token(request)if not token:@@ -145,20 +95,8 @@ async def token_auth_middleware(request: Request,call_next: Callable[[Request], Awaitable[Response]],) -> Response:- """Outermost auth seam for token-authable routes.-- No-op pass-through for any path not registered via- :func:`register_token_route`. For a registered path, token auth is the- only accepted scheme:-- * valid token → attach principal + ``token_authenticated`` flag, pass through.- * unreachable → 503 (provider backing store down; not "bad credentials").- * otherwise → 401 unauthenticated.-- Runs before the cookie/session gates (installed last in ``web_server.py``).- The cookie gates honour ``request.state.token_authenticated`` and skip- enforcement, so a token-authed request is never redirected to ``/login``.- """+ """Outermost auth seam: pass-through for unregistered paths; for a token+ route, valid token -> attach principal + flag, unreachable -> 503, else 401."""path = request.url.pathif not is_token_route(path):return await call_next(request)@@ -177,10 +115,7 @@ async def token_auth_middleware(path=path,ip=_client_ip(request),)- return JSONResponse(- {"detail": f"Auth provider {unreachable!r} unreachable"},- status_code=503,- )+ return unreachable_response(unreachable)audit_log(AuditEvent.TOKEN_AUTH_FAILURE,diff --git a/hermes_cli/web_deps.py b/hermes_cli/web_deps.pyindex e21ccbd36a3..274f5a53e7b 100644--- a/hermes_cli/web_deps.py+++ b/hermes_cli/web_deps.py@@ -1,24 +1,14 @@"""Shared late-binding dependency seam for extracted dashboard routers.-Why this exists-----------------``hermes_cli/web_server.py`` owns all dashboard runtime state: the ephemeral-``_SESSION_TOKEN``, the ``DASHBOARD_HEALTH`` singleton, config helpers, and a-large set of private helper functions the route handlers call. Extracted-``APIRouter`` modules under ``hermes_cli/web_routers/`` need those helpers, but--* importing ``web_server`` at module import time from a router module would be- a circular import (``web_server`` imports the router modules to mount them),- and-* re-homing the helpers/state here would break the many tests (and any third- party code) that ``monkeypatch.setattr(web_server, "_helper", ...)``.+``hermes_cli/web_server.py`` owns all dashboard runtime state and most helper+functions the route handlers call. Extracted ``APIRouter`` modules under+``hermes_cli/web_routers/`` cannot import it at module import time (web_server+imports them to mount them — a cycle) and must not copy its state (tests+``monkeypatch.setattr(web_server, "_helper", ...)`` and expect that to win).Design: **late binding, state stays in web_server.** ``late(name)`` returns a-thin proxy that resolves ``hermes_cli.web_server.<name>`` *at call time*. This-is cycle-safe (the import happens inside the call, long after both modules are-initialised) and keeps ``web_server``'s runtime behaviour byte-identical:-monkeypatching an attribute on ``web_server`` is still authoritative because-every call re-reads the attribute from the live module.+proxy that resolves ``hermes_cli.web_server.<name>`` *at call time*, so+monkeypatched replacements and module state are never frozen at import time."""from __future__ import annotations@@ -36,12 +26,7 @@ def _server():def late(name: str):- """Late-binding proxy for a callable defined on ``web_server``.-- The returned wrapper looks up ``web_server.<name>`` on every call, so- async/sync nature, monkeypatched replacements, and module state are all- resolved at call time — never frozen at import time.- """+ """Late-binding proxy for a callable defined on ``web_server``."""def _proxy(*args: Any, **kwargs: Any):return getattr(_server(), name)(*args, **kwargs)@@ -51,25 +36,15 @@ def late(name: str):return _proxy-def late_attr(name: str) -> Any:- """Read ``web_server.<name>`` right now (for non-callable state reads)."""- return getattr(_server(), name)--class LateState:"""Live proxy for module-level state owned by ``web_server``.- Extracted routers can't ``from web_server import _mcp_oauth_flows`` —- that would freeze the object at import time (breaking tests that mutate- or replace it on ``web_server``) and be a circular import besides. Some- of the state is also defined *after* the router's ``include_router``- point in web_server's body, so even a late module-import wouldn't see it- yet. This proxy forwards every operation the extracted handlers actually- perform — attribute access, item get/set/del, iteration, membership,- ``len``/truthiness, ``with``-blocks (locks), and rich comparisons- (numeric limits) — to ``web_server.<name>`` resolved at operation time,- so mutating or monkeypatching the attribute on ``web_server`` stays- authoritative.+ Forwards every operation the extracted handlers perform — attribute access,+ item get/set/del, iteration, membership, ``len``/truthiness, ``with``-blocks+ (locks), rich comparisons (numeric limits) — to ``web_server.<name>``+ resolved at operation time. Some of that state is defined *after* the+ router's ``include_router`` point, so even a late module import would not+ see it; this proxy does."""__slots__ = ("_name",)@@ -133,21 +108,3 @@ class LateState:def __repr__(self) -> str: # pragma: no cover - debugging aidreturn f"<LateState {object.__getattribute__(self, '_name')} -> {self._target()!r}>"---# --- Named accessors for the shared server state (call-time reads) ------------def get_session_token() -> str:- """Current dashboard session token (``web_server._SESSION_TOKEN``)."""- return _server()._SESSION_TOKEN---def get_dashboard_health():- """The ``DASHBOARD_HEALTH`` singleton owned by web_server."""- return _server().DASHBOARD_HEALTH---def has_valid_session_token(request) -> bool:- """Late-bound alias for ``web_server._has_valid_session_token``."""- return _server()._has_valid_session_token(request)diff --git a/hermes_cli/web_routers/_common.py b/hermes_cli/web_routers/_common.pynew file mode 100644index 00000000000..faea6c71896--- /dev/null+++ b/hermes_cli/web_routers/_common.py@@ -0,0 +1,81 @@+"""Shared plumbing for the extracted dashboard routers.++Everything here is a thin wrapper over the late-binding seam in+:mod:`hermes_cli.web_deps`: web_server still owns the helpers and state, and+every access resolves at call time so ``monkeypatch.setattr(web_server, ...)``+stays authoritative.+"""++from __future__ import annotations++import asyncio+import contextlib+import logging+from typing import Any, Callable, Optional++from fastapi import HTTPException++from hermes_cli.web_deps import LateState, late++# Same logger the handlers used before extraction (identical logger object).+log = logging.getLogger("hermes_cli.web_server")++_profile_scope = late("_profile_scope")+_spawn_hermes_action = late("_spawn_hermes_action")+_profile_cli_args = late("_profile_cli_args")+# Config read-modify-write serialization for off-loop handlers (live lock —+# LateState supports ``with``-blocks).+_CONFIG_MUTATION_LOCK = LateState("_CONFIG_MUTATION_LOCK")+++def config_write_scope(profile: Optional[str]):+ """Profile scope, then the config mutation lock — the write-path nesting+ every config-mutating handler uses."""+ with _profile_scope(profile):+ with _CONFIG_MUTATION_LOCK:+ yield+++async def scoped_to_thread(profile: Optional[str], fn: Callable[[], Any]) -> Any:+ """Run ``fn()`` inside ``_profile_scope(profile)`` on a worker thread."""++ def _run():+ with _profile_scope(profile):+ return fn()++ return await asyncio.to_thread(_run)+++def http_failure(log_msg: str, status: int, prefix: str):+ """Map unexpected exceptions to ``HTTPException(status, f"{prefix}: {exc}")``.++ ``HTTPException`` passes through untouched; anything else is logged with+ ``log_msg`` (full traceback) first.+ """+ try:+ yield+ except HTTPException:+ raise+ except Exception as exc:+ log.exception(log_msg)+ raise HTTPException(status_code=status, detail=f"{prefix}: {exc}")+++def spawn_profile_action(+ profile: Optional[str], argv: list, name: str, *, log_msg: str, prefix: str+) -> dict:+ """Spawn a background ``hermes -p <profile> <argv>`` action; a spawn+ failure is logged and becomes ``500 "<prefix>: <exc>"``."""+ with http_failure(log_msg, 500, prefix):+ proc = _spawn_hermes_action(_profile_cli_args(profile) + argv, name)+ return {"ok": True, "pid": proc.pid, "name": name}+++def require(value: Optional[str], detail: str) -> str:+ """Strip ``value``; 400 with ``detail`` when empty."""+ stripped = (value or "").strip()+ if not stripped:+ raise HTTPException(status_code=400, detail=detail)+ return strippeddiff --git a/hermes_cli/web_routers/cron.py b/hermes_cli/web_routers/cron.pyindex c540c77e170..9299e61d0e9 100644--- a/hermes_cli/web_routers/cron.py+++ b/hermes_cli/web_routers/cron.py@@ -1,19 +1,16 @@-"""Cron dashboard routes (extracted verbatim from web_server.py).+"""Cron dashboard routes.-Handler bodies are byte-identical. The ``*_sync`` workers, profile resolution-and the threadpool wrapper (``_run_cron_dashboard_io``) still live in-web_server — reached via the late-binding seam in :mod:`hermes_cli.web_deps`-so ``monkeypatch.setattr(web_server, ...)`` keeps working (several cron tests-rely on exactly that).+The ``*_sync`` workers, profile resolution and the threadpool wrapper+(``_run_cron_dashboard_io``) live in web_server and are reached through the+late-binding seam so ``monkeypatch.setattr(web_server, ...)`` keeps working."""-import asyncio # noqa: F401 — used by handlers-import functools # noqa: F401-import logging-from typing import Optional # noqa: F401+import asyncio+import functools+from typing import Optional-from fastapi import APIRouter, HTTPException, Request # noqa: F401-from fastapi.responses import JSONResponse # noqa: F401+from fastapi import APIRouter, HTTPException, Request+from fastapi.responses import JSONResponsefrom hermes_cli.web_deps import latefrom hermes_cli.web_models import (@@ -21,16 +18,10 @@ from hermes_cli.web_models import (CronJobUpdate,AutomationBlueprintInstantiate,)--# Same logger the handlers used before extraction (identical logger object).-_log = logging.getLogger("hermes_cli.web_server")+from hermes_cli.web_routers._common import log as _logrouter = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent — includes config readers so existing-# ``monkeypatch.setattr(web_server, "load_config", ...)`` idioms behave-# identically for these routes)._run_cron_dashboard_io = late("_run_cron_dashboard_io")_list_cron_jobs_sync = late("_list_cron_jobs_sync")_get_cron_job_sync = late("_get_cron_job_sync")@@ -51,12 +42,9 @@ _raise_if_cron_registration_error = late("_raise_if_cron_registration_error")load_config = late("load_config")cfg_get = late("cfg_get")-# Retry-After hint (seconds) stamped on retryable cron-fire 503s. Sized to-# clear the common transient windows — a scale-to-zero wake or an s6 gateway-# restart completes well within a minute — so a scheduler that honors it-# spaces its next attempt PAST the outage window instead of burning its whole-# retry budget inside it (OOF-266). Honored by QStash once NAS propagates it;-# harmless (ignored) until then.+# Retry-After hint (seconds) on retryable cron-fire 503s: sized to clear a+# scale-to-zero wake or gateway restart so a scheduler that honors it spaces+# its next attempt past the outage instead of burning its retry budget in it._CRON_FIRE_RETRY_AFTER_SECONDS = 60@@ -82,15 +70,9 @@ async def create_cron_job(body: CronJobCreate, profile: Optional[str] = None):@router.get("/api/cron/delivery-targets")async def get_cron_delivery_targets():- """Delivery targets the cron dropdown should offer.-- Always includes the implicit ``local`` option. Beyond that, the list is- derived dynamically from the configured gateway platforms via- ``cron.scheduler.cron_delivery_targets()`` — no hardcoded platform list. A- configured platform that hasn't set its cron home channel is still returned- with ``home_target_set: false`` so the UI can surface it as "configure a- home channel first" rather than hiding it.- """+ """Delivery targets for the cron dropdown: implicit ``local`` plus the+ configured gateway platforms (a platform without a cron home channel is+ still listed with ``home_target_set: false`` so the UI can say so)."""targets = [{"id": "local",@@ -137,26 +119,13 @@ async def delete_cron_job(job_id: str, profile: Optional[str] = None):async def cron_fire_webhook(request: Request):"""Chronos managed-cron fire webhook (NAS -> agent) — gateway forwarder.- Authenticated by a short-lived NAS-minted JWT (verified by the pluggable- Chronos fire-verifier), NOT the dashboard session cookie — so this path is- in ``PUBLIC_API_PATHS`` to bypass the dashboard auth gate, and the JWT is- the real gate.-- The dashboard is only the PUBLIC DOOR here (on hosted deployments the Fly- proxy exposes exactly one port, the dashboard's). Cron execution belongs- to the GATEWAY process, which owns the live platform adapters — required- for relay-fronted logical platforms (their only sender is the live relay- adapter) and E2EE rooms, neither of which the dashboard's standalone send- path can serve. So after verifying the JWT this handler FORWARDS the fire- to the gateway api_server's own ``/api/cron/fire`` on loopback and passes- the gateway's response through (the gateway re-verifies the JWT — defense- in depth, no new trust link).-- Gateway unreachable (scale-to-zero wake still booting, restart window,- api_server disabled) → 503, so NAS retries per the Chronos contract- (non-2xx = retryable). The store CAS claim de-dupes the eventual double- fire. Deliberately NO local-execution fallback: delivering from the wrong- process is worse than a delayed retry.+ Gated by the NAS-minted JWT (this path is in ``PUBLIC_API_PATHS``), not the+ dashboard cookie. The dashboard is only the public door: execution belongs+ to the GATEWAY process (it owns the live platform adapters relay-fronted+ and E2EE targets need), so the fire is forwarded to the gateway+ api_server's own ``/api/cron/fire`` on loopback and its response passed+ through (the gateway re-verifies the JWT). Gateway unreachable -> 503 so+ NAS retries; deliberately NO local-execution fallback."""from plugins.cron_providers.chronos.verify import get_fire_verifier@@ -181,23 +150,17 @@ async def cron_fire_webhook(request: Request):if not job_id:return JSONResponse({"error": "missing job_id"}, status_code=400)- # _find_cron_job_profile walks every profile and lists its jobs (file- # I/O per profile) — run it off the event loop like the other cron- # dashboard endpoints.+ # Walks every profile's job list (file I/O) — off the event loop.profile = await _run_cron_dashboard_io(_find_cron_job_profile, job_id)if not profile:- # Job is gone (cancelled / completed) — nothing to fire. 200 so NAS- # does not retry a fire that is intentionally absent.+ # Job is gone (cancelled / completed): 200 so NAS does not retry.return JSONResponse({"status": "gone", "job_id": job_id}, status_code=200)forwarded = await _forward_cron_fire_to_gateway(profile, job_id, auth)if forwarded is None:- # Durably stamp the miss on the job record (last_fire_error) so the- # user and their agent can see "scheduled fire could not reach the- # runner" in `cronjob list` / the dashboard — without this, a dead- # 8642 hop is invisible outside gui.log (no execution row is ever- # created because the claim never happens). Best-effort: visibility- # must never break the retry contract below.+ # Stamp the miss on the job record (last_fire_error) so the dead hop is+ # visible in `cronjob list` / the dashboard, not just gui.log.+ # Best-effort: visibility must never break the retry contract below.try:await _run_cron_dashboard_io(_call_cron_for_profile,@@ -211,25 +174,11 @@ async def cron_fire_webhook(request: Request):)except Exception:_log.debug("could not stamp last_fire_error for %s", job_id, exc_info=True)- # Gateway unreachable. Split by OPERATOR INTENT (OOF-266):- #- # - Deliberately stopped gateway (durable desired_state == "stopped",- # written only by the s6 lifecycle commands): retrying can never- # succeed until a human starts the gateway again, so the retry- # budget is pure waste and the resulting 503→502 storms page the- # NAS on-call for a non-incident. Drop with 200 + a structured log- # line, mirroring NAS's own instance_stopped drop in the relay.- # The fire is NOT lost silently: the log names job and profile,- # and the gateway's Chronos provider reconciles + re-arms every- # job on its next startup (plugins/cron_providers/chronos- # start() -> reconcile()), so fires resume when the operator- # starts the gateway.- #- # - Transient window (scale-to-zero wake, restart, crash loop —- # desired_state is "running" or unknown): keep the retryable 503,- # but stamp Retry-After so a scheduler that honors it spaces its- # next attempt past the wake/restart window instead of exhausting- # the whole retry budget inside it.+ # Split by operator intent: a deliberately stopped gateway (durable+ # desired_state == "stopped") can never be reached by retrying, so drop+ # with 200 + a structured log line — the Chronos provider re-arms every+ # job on the next gateway start. A transient window (wake, restart,+ # crash loop) keeps the retryable 503 with a Retry-After hint.if await _run_cron_dashboard_io(_gateway_intentionally_stopped, profile):_log.info("cron fire dropped: gateway for profile %r is deliberately "@@ -259,24 +208,16 @@ async def cron_fire_webhook(request: Request):status_code, gateway_body = forwardedif isinstance(gateway_body, dict):gateway_body.setdefault("job_id", job_id)- headers = (- # The gateway's own 503s (draining, admission failure) are equally- # transient — give the scheduler the same spacing hint.- {"Retry-After": str(_CRON_FIRE_RETRY_AFTER_SECONDS)}- if status_code == 503- else None- )+ # The gateway's own 503s (draining, admission failure) are equally+ # transient — same spacing hint.+ headers = {"Retry-After": str(_CRON_FIRE_RETRY_AFTER_SECONDS)} if status_code == 503 else Nonereturn JSONResponse(gateway_body, status_code=status_code, headers=headers)@router.get("/api/cron/blueprints")async def list_cron_blueprints():- """Return the blueprint catalog as form schemas for the dashboard gallery.-- The ``deliver`` slot's options are rewritten from the user's actually- configured gateway platforms (plus the universal origin/local/all), so the- form never offers a platform that isn't connected.- """+ """Blueprint catalog as form schemas; the ``deliver`` slot's options are+ rewritten from the actually configured gateway platforms."""try:from cron.blueprint_catalog import CATALOG, blueprint_catalog_entry@@ -317,18 +258,14 @@ async def instantiate_blueprint(body: AutomationBlueprintInstantiate, profile: sexcept BlueprintFillError as exc:# Field-level validation error — 422 so the form can show it inline.raise HTTPException(status_code=422, detail=str(exc)) from exc- # Blueprint-created jobs deliver to the dashboard's configured target by+ # Blueprint jobs deliver to the dashboard's configured target by# default; the form's deliver slot overrides via spec["deliver"].spec.pop("origin", None)- # create_job does per-profile file I/O — keep it off the event loop- # like the sibling cron endpoints (partial avoids **spec keys ever- # colliding with the wrapper's own parameters).+ # Off the event loop like the sibling endpoints; partial keeps **spec+ # keys from colliding with the wrapper's own parameters._create = functools.partial(_call_cron_for_profile, profile, "create_job", **spec)created = await _run_cron_dashboard_io(_create)- # Same contract as the other dashboard mutations: reconcile the- # profile-scoped provider (best-effort; fail-closed for external- # providers on a multi-profile dashboard). Off the event loop —- # a Chronos reconcile does file I/O plus NAS network calls.+ # Reconcile the profile-scoped provider (file I/O + NAS calls) off-loop.await _run_cron_dashboard_io(_notify_cron_provider_for_profile, profile)return createdexcept HTTPException:diff --git a/hermes_cli/web_routers/git.py b/hermes_cli/web_routers/git.pyindex 0bd4bb3f19c..4239e636293 100644--- a/hermes_cli/web_routers/git.py+++ b/hermes_cli/web_routers/git.py@@ -1,16 +1,11 @@-"""Git dashboard routes (extracted verbatim from web_server.py).--Handler bodies are byte-identical to their previous in-web_server form; the-helpers they call (``_git_op``, ``_git_path``) still live in web_server and are-reached via the late-binding seam in :mod:`hermes_cli.web_deps`, so-``monkeypatch.setattr(web_server, ...)`` keeps working.-"""+"""Git dashboard routes — thin executor-offloaded wrappers over ``web_git``+(``_git_op`` / ``_git_path`` live in web_server, reached via the late seam)."""from typing import Optionalfrom fastapi import APIRouter-from hermes_cli import web_git as _web_git # noqa: F401 — used by handlers+from hermes_cli import web_git as _web_gitfrom hermes_cli.web_deps import latefrom hermes_cli.web_models import (GitPathBody,@@ -24,8 +19,6 @@ from hermes_cli.web_models import (router = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent)._git_op = late("_git_op")_git_path = late("_git_path")@@ -35,13 +28,10 @@ async def git_status_route(path: str):return await _git_op(_web_git.repo_status, _git_path(path))-# ─── gh CLI auth probe ───────────────────────────────────────────────────────-# Cached `gh auth status` result. Consumed by the desktop composer's GitHub-# suggestion pill: GitHub deliberately has NO MCP catalog entry (its hosted-# MCP requires a per-host OAuth app — generic DCR 404s — and the bundled-# github/* skills via gh CLI are the more capable integration), so the pill-# offers the `/github-auth` skill instead, and only to users who aren't-# already authenticated. The probe is read-only and never prompts.+# Cached `gh auth status` for the desktop composer's GitHub suggestion pill.+# GitHub deliberately has NO MCP catalog entry (hosted MCP needs a per-host+# OAuth app; the gh-CLI skills are the better integration), so the pill offers+# the `/github-auth` skill — only to users who aren't already authenticated._GH_AUTH_TTL_S = 300.0_gh_auth_cache: Optional[tuple] = None # (monotonic_ts, payload)@@ -49,12 +39,8 @@ _gh_auth_cache: Optional[tuple] = None # (monotonic_ts, payload)@router.get("/api/git/gh-auth")async def gh_auth_status_route(refresh: bool = False):- """Report whether the `gh` CLI is present and authenticated.-- Returns ``{"available": bool, "authenticated": bool}``. Cached for five- minutes (`refresh=true` bypasses — the pill uses it after a completed- login so the suggestion withdraws immediately).- """+ """``{"available", "authenticated"}`` for the `gh` CLI; cached 5 min+ (``refresh=true`` bypasses so the pill withdraws right after a login)."""global _gh_auth_cacheimport asyncioimport timediff --git a/hermes_cli/web_routers/mcp.py b/hermes_cli/web_routers/mcp.pyindex 97eda0327f7..c2c967118e8 100644--- a/hermes_cli/web_routers/mcp.py+++ b/hermes_cli/web_routers/mcp.py@@ -1,22 +1,18 @@-"""MCP dashboard routes (extracted verbatim from web_server.py).--Handler bodies are byte-identical. The OAuth flow registry-(``_mcp_oauth_flows`` + lock + pending cap) and the worker/helpers stay in-web_server - reached via the late-binding seam in :mod:`hermes_cli.web_deps`-(``late`` for callables, ``LateState`` for the mutable registry/lock/limit) so-tests that mutate ``web_server._mcp_oauth_flows`` or-``monkeypatch.setattr(web_server, "_run_dashboard_mcp_oauth", ...)`` keep+"""MCP dashboard routes.++The OAuth flow registry (``_mcp_oauth_flows`` + lock + pending cap) and the+worker/helpers stay in web_server — reached via the late-binding seam so tests+that mutate ``web_server._mcp_oauth_flows`` or monkeypatch its helpers keepworking unchanged."""-import asyncio # noqa: F401 — used by handlers-import logging-import secrets # noqa: F401-import threading # noqa: F401-from typing import Any, Dict, Optional # noqa: F401+import asyncio+import secrets+import threading+from typing import Any, Dict, Optional-from fastapi import APIRouter, HTTPException, Request # noqa: F401-from fastapi.responses import HTMLResponse # noqa: F401+from fastapi import APIRouter, HTTPException, Request+from fastapi.responses import HTMLResponsefrom hermes_cli.web_deps import late, LateStatefrom hermes_cli.web_models import (@@ -25,48 +21,39 @@ from hermes_cli.web_models import (MCPServerCreate,MCPServersReplace,)--# Same logger the handlers used before extraction (identical logger object).-_log = logging.getLogger("hermes_cli.web_server")+from hermes_cli.web_routers._common import (+ _profile_cli_args,+ _profile_scope,+ _spawn_hermes_action,+ config_write_scope,+ log as _log,+ scoped_to_thread,+)router = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent)._config_profile_scope = late("_config_profile_scope")_gc_mcp_oauth_flows = late("_gc_mcp_oauth_flows")_mcp_install_action_name = late("_mcp_install_action_name")_mcp_oauth_callback_url = late("_mcp_oauth_callback_url")_mcp_server_summary = late("_mcp_server_summary")_normalize_mcp_server_create = late("_normalize_mcp_server_create")-_profile_cli_args = late("_profile_cli_args")-_profile_scope = late("_profile_scope")_require_token = late("_require_token")_run_dashboard_mcp_oauth = late("_run_dashboard_mcp_oauth")-_spawn_hermes_action = late("_spawn_hermes_action")load_config = late("load_config")save_config = late("save_config")save_env_value = late("save_env_value")-# Live proxies for web_server-owned module state (mutations/monkeypatches-# on web_server remain authoritative; resolved at operation time)._mcp_oauth_flows = LateState("_mcp_oauth_flows")_mcp_oauth_flows_lock = LateState("_mcp_oauth_flows_lock")_MAX_PENDING_MCP_OAUTH_FLOWS = LateState("_MAX_PENDING_MCP_OAUTH_FLOWS")-# Config read-modify-write serialization for off-loop handlers (defined in-# web_server.py; LateState supports ``with``-blocks, so this is the live lock).-_CONFIG_MUTATION_LOCK = LateState("_CONFIG_MUTATION_LOCK")@router.get("/api/mcp/servers")async def list_mcp_servers(profile: Optional[str] = None):from hermes_cli.mcp_config import _get_mcp_servers- def _read():- with _profile_scope(profile):- return _get_mcp_servers()-- servers = await asyncio.to_thread(_read)+ servers = await scoped_to_thread(profile, _get_mcp_servers)return {"servers": [_mcp_server_summary(name, cfg) for name, cfg in sorted(servers.items())@@ -88,23 +75,21 @@ async def add_mcp_server(body: MCPServerCreate, profile: Optional[str] = None):raise HTTPException(status_code=400, detail=str(exc)) from excdef _run():- with _profile_scope(body.profile or profile):- # _save_mcp_server does its own load→mutate→save of config.yaml;- # serialize the whole cycle against other off-loop config writers.- # The duplicate-name check lives under the same lock span so a- # concurrent add of the same name can't slip between check and save.- with _CONFIG_MUTATION_LOCK:- if name in _get_mcp_servers():- raise HTTPException(- status_code=409, detail=f"Server '{name}' already exists"- )- if bearer_token is not None:- server_config["headers"] = _save_bearer_auth_token(name, bearer_token)- if not _save_mcp_server(name, server_config):- raise HTTPException(- status_code=400,- detail=f"Server '{name}' rejected: suspicious command/args configuration",- )+ # _save_mcp_server does its own load→mutate→save; the duplicate-name+ # check sits under the same lock span so a concurrent add of the same+ # name can't slip between check and save.+ with config_write_scope(body.profile or profile):+ if name in _get_mcp_servers():+ raise HTTPException(+ status_code=409, detail=f"Server '{name}' already exists"+ )+ if bearer_token is not None:+ server_config["headers"] = _save_bearer_auth_token(name, bearer_token)+ if not _save_mcp_server(name, server_config):+ raise HTTPException(+ status_code=400,+ detail=f"Server '{name}' rejected: suspicious command/args configuration",+ )try:await asyncio.to_thread(_run)@@ -121,18 +106,15 @@ async def add_mcp_server(body: MCPServerCreate, profile: Optional[str] = None):async def replace_mcp_servers(body: MCPServersReplace, profile: Optional[str] = None):"""Replace the entire ``mcp_servers`` map (the GUI mcp.json editor's save).- The generic ``/api/config`` endpoint deep-merges maps, so it can never- delete a server key, drop an ``enabled: false`` flag, or remove a nested- field — edits looked saved but the stale entry survived on disk. This- endpoint sets the whole map so removals actually persist. Storage stays- the config.yaml ``mcp_servers`` key the CLI/TUI already read.+ The generic ``/api/config`` endpoint deep-merges maps and so can never+ delete a key or drop an ``enabled: false``; this sets the whole map so+ removals actually persist."""from hermes_cli.mcp_config import _replace_mcp_serversdef _run():- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- return _replace_mcp_servers(body.servers)+ with config_write_scope(body.profile or profile):+ return _replace_mcp_servers(body.servers)ok, issues = await asyncio.to_thread(_run)if not ok:@@ -145,9 +127,8 @@ async def remove_mcp_server(name: str, profile: Optional[str] = None):from hermes_cli.mcp_config import _remove_mcp_serverdef _run():- with _profile_scope(profile):- with _CONFIG_MUTATION_LOCK:- return _remove_mcp_server(name)+ with config_write_scope(profile):+ return _remove_mcp_server(name)removed = await asyncio.to_thread(_run)if not removed:@@ -164,39 +145,28 @@ async def test_mcp_server(name: str, profile: Optional[str] = None):_probe_single_server,)- def _read():- with _profile_scope(profile):- return _get_mcp_servers()-- servers = await asyncio.to_thread(_read)+ servers = await scoped_to_thread(profile, _get_mcp_servers)if name not in servers:raise HTTPException(status_code=404, detail=f"Server '{name}' not found")details: Dict[str, Any] = {}# An `auth: oauth` server that serves tools/list anonymously would probe OK- # with no token — a false green. Require a token on disk for it, matching the- # /auth verification (some providers don't enforce auth on tools/list).+ # with no token — a false green. Require a token on disk, matching /auth.needs_oauth_token = servers[name].get("auth") == "oauth"def _probe_scoped():- # Home-only scope (contextvar), NOT _profile_scope. A probe blocks for- # as long as the server takes to spawn/connect — a stdio `npx` cold- # start is many seconds — and _profile_scope holds a process-global- # skills lock for its ENTIRE body. Holding that across the probe- # serialized every other endpoint (config/skills/toolsets all take the- # same lock), so a slow server made unrelated requests time out at 15s.- # The probe touches no skills globals; it only needs the HERMES_HOME- # override for .env interpolation + OAuth token resolution, which the- # contextvar provides (copied into this to_thread worker; and- # _run_on_mcp_loop re-wraps it onto the MCP event-loop thread).+ # Home-only scope (contextvar), NOT _profile_scope: a probe can block for+ # many seconds (stdio `npx` cold start) and _profile_scope holds the+ # process-global skills lock for its whole body, which serialized every+ # other endpoint behind a slow server. The probe only needs the+ # HERMES_HOME override for .env interpolation + OAuth token resolution.with _config_profile_scope(profile):tools = _probe_single_server(name, servers[name], details=details)token_present = _oauth_tokens_present(name) if needs_oauth_token else Truereturn tools, token_presenttry:- # Probe blocks on a dedicated MCP event loop — run in a thread so the- # FastAPI event loop is never blocked.+ # Probe blocks on a dedicated MCP event loop — keep it off the FastAPI loop.tools, token_present = await asyncio.to_thread(_probe_scoped)except Exception as exc:return {@@ -210,9 +180,8 @@ async def test_mcp_server(name: str, profile: Optional[str] = None):"error": "OAuth authentication required — no token found.","tools": [],}- # Additive-optional per-tool schema size (chars of the converted registry- # schema) — the desktop's cost overlay estimates tokens from it. Older- # renderers ignore the extra key; failed probes simply omit it.+ # Optional per-tool schema size (chars) — the desktop's cost overlay+ # estimates tokens from it; failed probes simply omit it.schema_chars = details.get("schema_chars") or {}return {"ok": True,@@ -317,12 +286,10 @@ async def mcp_oauth_flow_status(flow_id: str, request: Request):@router.delete("/api/mcp/oauth/flows/{flow_id}")async def cancel_mcp_oauth_flow(flow_id: str, request: Request):- """Cancel an in-flight MCP OAuth flow (the desktop's inline-card/pill- cancel). mark_error unblocks both worker waits, so the worker exits and- frees the per-server "already in progress" slot — without this, a renderer- that stops polling leaves the flow squatting until its 300s callback- timeout and every retry 409s. Idempotent: an already-settled flow is left- as-is (approved stays approved)."""+ """Cancel an in-flight MCP OAuth flow. mark_error unblocks the worker so+ it frees the per-server "already in progress" slot — otherwise a renderer+ that stops polling leaves the flow squatting until the 300s callback+ timeout and every retry 409s. Idempotent: a settled flow is left as-is."""_require_token(request)flow = _mcp_oauth_flows.get(flow_id)if flow is None:@@ -378,23 +345,18 @@ async def mcp_oauth_callback(async def set_mcp_server_enabled(name: str, body: MCPEnabledToggle, profile: Optional[str] = None):- """Enable or disable an MCP server (takes effect on next session/gateway).-- Toggles the ``enabled`` key on the server's config.yaml entry — the same- flag the agent reads at startup. Disabled servers stay in config so they- can be re-enabled without re-entering their settings.- """+ """Toggle the server's ``enabled`` flag (takes effect on next session/gateway);+ disabled servers stay in config so they can be re-enabled without re-entry."""def _run():- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- cfg = load_config()- servers = cfg.get("mcp_servers")- if not isinstance(servers, dict) or name not in servers:- raise HTTPException(status_code=404, detail=f"Server '{name}' not found")- if not isinstance(servers[name], dict):- raise HTTPException(status_code=400, detail="Malformed server config")- servers[name]["enabled"] = bool(body.enabled)- save_config(cfg)+ with config_write_scope(body.profile or profile):+ cfg = load_config()+ servers = cfg.get("mcp_servers")+ if not isinstance(servers, dict) or name not in servers:+ raise HTTPException(status_code=404, detail=f"Server '{name}' not found")+ if not isinstance(servers[name], dict):+ raise HTTPException(status_code=400, detail="Malformed server config")+ servers[name]["enabled"] = bool(body.enabled)+ save_config(cfg)return {"ok": True, "name": name, "enabled": bool(body.enabled)}return await asyncio.to_thread(_run)@@ -402,14 +364,8 @@ async def set_mcp_server_enabled(@router.get("/api/mcp/catalog")async def list_mcp_catalog(profile: Optional[str] = None):- """Browse the Nous-approved MCP catalog (the optional-mcps/ manifests).-- Each entry reports whether it's already installed and enabled so the UI- can show install / enabled state inline. This is the same catalog- `hermes mcp catalog` / `hermes mcp install` read. ``profile`` scopes- the installed/enabled annotations (the catalog itself is repo-shipped- and identical for every profile).- """+ """Browse the Nous-approved MCP catalog (optional-mcps/ manifests), each+ entry annotated with installed/enabled state for ``profile``."""try:from hermes_cli import mcp_catalogexcept Exception as exc:@@ -443,10 +399,8 @@ async def list_mcp_catalog(profile: Optional[str] = None):{"name": e.name, "prompt": e.prompt, "required": e.required}for e in getattr(auth, "env", []) or []],- # Transport details so the UI can show exactly what connects/runs.- # The trust model (docs: user-guide/features/mcp) tells users to- # inspect command/args/url and the install bootstrap before- # installing — surface them rather than hiding them in the repo.+ # Transport details surfaced on purpose: the trust model asks+ # users to inspect command/args/url + bootstrap before installing."command": transport.command,"args": list(transport.args or []),"url": transport.url,@@ -489,13 +443,8 @@ async def list_mcp_catalog(profile: Optional[str] = None):@router.post("/api/mcp/catalog/install")async def install_mcp_catalog_entry(body: MCPCatalogInstall, profile: Optional[str] = None):- """Install a catalog MCP into config.yaml.-- For HTTP/stdio entries with required env vars, those are written to .env- via the standard env path so the agent can read them at session start.- Entries that need a git bootstrap (``needs_install``) are installed via- the CLI action path because the clone can take time.- """+ """Install a catalog MCP into config.yaml (declared env vars go to .env+ first; git-bootstrap entries run via the background CLI action path)."""from hermes_cli import mcp_catalogname = (body.name or "").strip()@@ -516,9 +465,8 @@ async def install_mcp_catalog_entry(body: MCPCatalogInstall, profile: Optional[s),)- # Validate the complete map before the first write. This preserves the- # existing writer/install flow while ensuring a mixed valid+invalid request- # cannot partially persist credentials.+ # Validate the complete map before the first write so a mixed+ # valid+invalid request cannot partially persist credentials.from hermes_cli.config import validate_env_var_name_for_writetry:@@ -538,13 +486,10 @@ async def install_mcp_catalog_entry(body: MCPCatalogInstall, profile: Optional[sawait asyncio.to_thread(_write_env)- # Git-bootstrap entries can take a while to clone — run via the background- # action path so the request returns immediately and the UI can tail logs.- # The -p subprocess rebinds HERMES_HOME-derived paths in the child.+ # Git-bootstrap entries can take a while to clone — background action path+ # so the request returns immediately. Per-entry action name: a shared+ # "mcp-install" would let a re-click overwrite the tracked process/log.if entry.install is not None:- # Unique per-entry action name: a shared "mcp-install" would let a- # re-click (or a second entry) overwrite the tracked process/log while- # the first clone is still running.action = _mcp_install_action_name(name)try:_spawn_hermes_action(@@ -557,18 +502,12 @@ async def install_mcp_catalog_entry(body: MCPCatalogInstall, profile: Optional[sraise HTTPException(status_code=500, detail=f"Install failed: {exc}")return {"ok": True, "name": name, "background": True, "action": action}- # No git step — install synchronously via the catalog API. install_entry- # routes through load_config/save_config + save_env_value, all call-time- # resolvers, so the context override scopes it. Wrap the to_thread body- # in the scope INSIDE the thread (contextvars don't propagate into- # to_thread the other way around — asyncio.to_thread copies context, so- # setting it here works; keep it explicit for clarity).- def _install_scoped():- with _profile_scope(effective_profile):- mcp_catalog.install_entry(entry, enable=body.enable)-+ # No git step — install synchronously; install_entry goes through the+ # call-time config/env resolvers so the profile scope covers it.try:- await asyncio.to_thread(_install_scoped)+ await scoped_to_thread(+ effective_profile, lambda: mcp_catalog.install_entry(entry, enable=body.enable)+ )except HTTPException:raiseexcept Exception as exc:diff --git a/hermes_cli/web_routers/sessions.py b/hermes_cli/web_routers/sessions.pyindex 657840ed62a..86f593c945d 100644--- a/hermes_cli/web_routers/sessions.py+++ b/hermes_cli/web_routers/sessions.py@@ -1,25 +1,21 @@-"""Session dashboard routes (extracted verbatim from web_server.py).--Three routers because the original registration points are far apart and-global route order matters: ``list_router`` (GET /api/sessions) was registered-before the profiles ``sessions_router`` include, ``search_router``-(GET /api/sessions/search) right after it, and ``manage_router`` (the-mutation/detail endpoints) thousands of lines later - each is mounted at its-original registration point so the app's route table is byte-identical.--Handler bodies are byte-identical; web_server-owned helpers are reached via-the late-binding seam in :mod:`hermes_cli.web_deps` so tests that-``monkeypatch.setattr(web_server, "_helper", ...)`` keep working.+"""Session dashboard routes.++Three routers because global route order matters: ``list_router``+(GET /api/sessions) was registered before the profiles ``sessions_router``+include, ``search_router`` (GET /api/sessions/search) right after it, and+``manage_router`` (mutation/detail endpoints) much later — each is mounted at+its original registration point. web_server-owned helpers are reached via+the late-binding seam so ``monkeypatch.setattr(web_server, ...)`` keeps working."""-import asyncio # noqa: F401 — used by handlers+import asyncioimport json-import logging+import reimport sqlite3-import time # noqa: F401-from typing import Any, Dict, List, Optional # noqa: F401+import time+from typing import Callable, List, Optional-from fastapi import APIRouter, HTTPException, Query, Request # noqa: F401+from fastapi import APIRouter, HTTPException, Query, Requestfrom fastapi.encoders import jsonable_encoderfrom fastapi.responses import StreamingResponse@@ -31,17 +27,13 @@ from hermes_cli.web_models import (SessionPrune,SessionRename,)+from hermes_cli.web_routers._common import log as _logfrom hermes_state import is_malformed_db_error, is_transient_sqlite_error-# Same logger the handlers used before extraction (identical logger object).-_log = logging.getLogger("hermes_cli.web_server")-list_router = APIRouter()search_router = APIRouter()manage_router = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent)._cron_default_profile = late("_cron_default_profile")_cron_profile_home = late("_cron_profile_home")_import_sessions_for_profile = late("_import_sessions_for_profile")@@ -52,21 +44,43 @@ _read_session_import_body = late("_read_session_import_body")_session_latest_descendant = late("_session_latest_descendant")_strip_session_list_rows = late("_strip_session_list_rows")+_ACTIVE_WINDOW_S = 300+++def _csv(value: Optional[str]) -> List[str]:+ """Split a comma-separated query param into stripped, non-empty items."""+ return [s.strip() for s in (value or "").split(",") if s.strip()]+++def _is_active(row: dict, now: float) -> bool:+ return (+ row.get("ended_at") is None+ and (now - row.get("last_active", row.get("started_at", 0))) < _ACTIVE_WINDOW_S+ )+++def _with_db(profile: Optional[str], fn: Callable, *, read_only: bool):+ """Open the profile's session DB, run ``fn(db)``, always close."""+ db = _open_session_db_for_profile(profile, read_only=read_only)+ try:+ return fn(db)+ finally:+ db.close()+++def _serving_profile(profile: Optional[str]) -> str:+ """The profile name rows are stamped with: the requested one, else the+ serving process's own — so default-profile rows never circulate unowned."""+ return _cron_profile_home(profile)[0] if profile else _cron_default_profile()+def _resolve_session_id(db, session_id: str) -> Optional[str]:"""Resolve *session_id*, distinguishing "absent" from "unreadable".- A corrupt ``state.db`` does not raise on every read. The exact-match- lookup goes through the ``sessions`` primary-key index, so a damaged- index simply *misses* and returns None — indistinguishable from a- session that was never there. Only the prefix fallback scans the base- b-tree and raises ``database disk image is malformed``.-- Both outcomes used to end at ``404 Session not found``: one silently, one- as an unhandled 500 that the Desktop surfaced as "session unavailable".- Reporting a corrupt store as an empty one cost a day of looking at- session logic during the 2026-08-31 incident, so classify it here and say- what is actually wrong.+ On a corrupt ``state.db`` the exact-match lookup (primary-key index) just+ misses, while the prefix fallback scans the b-tree and raises "database+ disk image is malformed". Both used to end at 404; report corruption as+ 503 with the actual problem instead of an empty store."""try:return db.resolve_session_id(session_id)@@ -88,9 +102,8 @@ def _resolve_session_id(db, session_id: str) -> Optional[str]:@list_router.get("/api/sessions")def get_sessions(- # ``le=100`` caps the page size (idea from #39200): an unbounded limit- # lets one request drag every session row (plus correlated-subquery- # preview work) out of SQLite in a single hit.+ # ``le=100``: an unbounded limit lets one request drag every session row+ # (plus correlated-subquery preview work) out of SQLite in a single hit.limit: int = Query(20, ge=0, le=100),offset: int = Query(0, ge=0),min_messages: int = 0,@@ -105,18 +118,11 @@ def get_sessions():"""List sessions.- ``archived`` controls how soft-archived sessions are treated:- ``exclude`` (default) hides them, ``only`` returns just the archived ones- (used by the desktop "Archived sessions" settings panel), and ``include``- returns both.-- ``order`` controls pagination order: ``created`` (default, by original- start time) or ``recent`` (by latest activity across the compression- chain). ``recent`` keeps a long-running conversation on the first page- after it auto-compresses into a fresh continuation id.-- Rows omit ``system_prompt``/``model_config`` (the payload-dominating- fields no list UI reads) unless ``full=1`` is passed.+ ``archived``: ``exclude`` (default) / ``only`` / ``include``. ``order``:+ ``created`` (start time) or ``recent`` (latest activity across the+ compression chain, so a long-running chat stays on page one after it+ auto-compresses onto a fresh id). Rows omit ``system_prompt`` /+ ``model_config`` unless ``full=1``."""if archived not in ("exclude", "only", "include"):raise HTTPException(@@ -132,22 +138,18 @@ def get_sessions(if profile:profile_name, _ = _cron_profile_home(profile)try:- # Auto-archive is the only configured write on this GET path. Run it- # through a dedicated maintenance connection, close that writer, then- # open the listing connection read-only.+ # Auto-archive is the only write on this GET path: run it on its own+ # maintenance connection, then open the listing connection read-only._maybe_auto_archive_for_profile(profile)db = _open_session_db_for_profile(profile, read_only=True)try:min_message_count = max(0, min_messages)archived_only = archived == "only"include_archived = archived == "include"- # Optional source scoping: ``source`` includes a single class,- # ``sources`` includes any of several comma-separated classes, and- # ``exclude_sources`` (comma-separated) drops classes. The desktop- # uses these to split recents (exclude=cron) from the cron-jobs- # section (source=cron) into two independent lists.- source_list = [s.strip() for s in (sources or "").split(",") if s.strip()]- exclude_list = [s.strip() for s in (exclude_sources or "").split(",") if s.strip()]+ # Source scoping: the desktop splits recents (exclude=cron) from+ # the cron-jobs section (source=cron) into two independent lists.+ source_list = _csv(sources)+ exclude_list = _csv(exclude_sources)sessions = db.list_sessions_rich(source=source or None,sources=source_list or None,@@ -159,9 +161,8 @@ def get_sessions(include_archived=include_archived,archived_only=archived_only,order_by_last_active=order == "recent",- # SQL-level projection: when the caller didn't ask for full- # rows, skip the system_prompt blob inside SQLite too (pairs- # with the API-level _strip_session_list_rows below).+ # Skip the system_prompt blob inside SQLite too (pairs with+ # _strip_session_list_rows below).compact_rows=not full,include_pinned=True,)@@ -176,18 +177,12 @@ def get_sessions(exclude_children=True,)now = time.time()- # Same ownership contract as get_session_detail: rows are stamped- # with the serving profile even when the request wasn't explicitly- # scoped, so default-profile rows never circulate unowned.row_profile = profile_name or _cron_default_profile()for s in sessions:- s["is_active"] = (- s.get("ended_at") is None- and (now - s.get("last_active", s.get("started_at", 0))) < 300- )+ s["is_active"] = _is_active(s, now)s["profile"] = row_profiles["is_default_profile"] = row_profile == "default"- # SQLite stores the flag as 0/1; expose a real JSON boolean.+ # SQLite stores the flags as 0/1; expose real JSON booleans.s["archived"] = bool(s.get("archived"))s["pinned"] = bool(s.get("pinned"))if not full:@@ -199,11 +194,9 @@ def get_sessions(raiseexcept sqlite3.OperationalError as exc:_log.exception("GET /api/sessions failed")- # 503, not 500: the store is busy, not gone. The desktop keeps the- # sidebar it already has instead of reading a 500 as an authoritative- # empty list. Retrying the OPEN here is deliberately not done — the- # bounded retry lives in SessionDB's read-only constructor, so every- # read-only opener gets it, not just this route.+ # 503, not 500: the store is busy, not gone — the desktop keeps its+ # sidebar instead of reading a 500 as an authoritative empty list.+ # The bounded open-retry lives in SessionDB's read-only constructor.transient = is_transient_sqlite_error(exc)raise HTTPException(status_code=503 if transient else 500,@@ -227,15 +220,13 @@ async def search_sessions(sources: str = None,exclude_sources: str = None,):- """Search sessions by ID plus full-text message content using FTS5.-- Direct session-id matches are surfaced first, then FTS message-content- matches. Results are deduped by compression lineage, not by raw- ``session_id``. Auto-compression rotates a conversation onto a fresh- session id (and leaves the old segment's messages in the FTS index), so one- logical chat can own many ``sessions`` rows that all match the same query.- Branches also use ``parent_session_id``, but they are real alternate- conversations; don't collapse branch-specific hits back into the parent.+ """Search sessions by ID plus FTS5 message content.++ ID matches first, then content matches. Results are deduped by+ compression lineage, not raw ``session_id``: auto-compression rotates a+ chat onto a fresh id and leaves the old segment in the FTS index, so one+ logical chat owns many rows. Branches also use ``parent_session_id`` but+ are real alternate conversations — they are NOT collapsed into the parent."""if not q or not q.strip():return {"results": []}@@ -244,15 +235,13 @@ async def search_sessions(try:safe_limit = max(1, min(int(limit or 20), 100))source_filter = source or None- source_list = [s.strip() for s in (sources or "").split(",") if s.strip()]+ source_list = _csv(sources)include_sources = [source_filter] if source_filter else (source_list or None)- exclude_list = [s.strip() for s in (exclude_sources or "").split(",") if s.strip()]+ exclude_list = _csv(exclude_sources)now = time.time()- # Walk parent_session_id to the compression root, memoized so a- # chain of compression segments only costs one walk. We deliberately- # stop at branch/delegate edges: those sessions may diverge from the- # parent and should remain searchable on their own.+ # Walk parent_session_id to the compression root, memoized per+ # chain; stops at branch/delegate edges (those stay searchable).root_cache: dict = {}def compression_root(session_id: str) -> str:@@ -319,10 +308,8 @@ async def search_sessions(tip_cache[root_id] = tipreturn tip- # Both ID matches and content matches share one keyspace, keyed by- # compression lineage root, so an id-hit and a content-hit on the- # same logical conversation collapse to a single result. The first- # hit for a lineage wins; ID matches run first and take priority.+ # One keyspace for id-hits and content-hits, keyed by lineage root;+ # first hit wins, and ID matches run first.seen: dict = {}def add_lineage_result(raw_sid: str, payload: dict) -> None:@@ -366,10 +353,7 @@ async def search_sessions(payload["id"] = sidseen[root] = payload- # Direct ID matches first: users often paste a session id from CLI,- # logs, or another Hermes surface. FTS can't find those unless the- # id happens to appear in message text. search_sessions_by_id is- # SQL-bounded, so this stays cheap even with thousands of sessions.+ # Direct ID matches first (pasted ids never appear in message text).for row in db.search_sessions_by_id(q,limit=safe_limit,@@ -392,19 +376,14 @@ async def search_sessions(},)- # Auto-add prefix wildcards so partial words match- # e.g. "nimb" → "nimb*" matches "nimby"- # Preserve quoted phrases and existing wildcards as-is- import re- terms = []- for token in re.findall(r'"[^"]*"|\S+', q.strip()):- if token.startswith('"') or token.endswith("*"):- terms.append(token)- else:- terms.append(token + "*")- prefix_query = " ".join(terms)+ # Prefix wildcards so partial words match ("nimb" -> "nimb*");+ # quoted phrases and existing wildcards are kept as-is.+ prefix_query = " ".join(+ tok if tok.startswith('"') or tok.endswith("*") else tok + "*"+ for tok in re.findall(r'"[^"]*"|\S+', q.strip())+ )# Over-fetch so lineage dedup can still surface `limit` distinct- # conversations even when several hits collapse onto one root.+ # conversations when several hits collapse onto one root.fetch_limit = max(safe_limit * 5, 50)matches = db.search_messages(query=prefix_query,@@ -446,65 +425,32 @@ async def search_sessions(@manage_router.post("/api/sessions/bulk-delete")async def bulk_delete_sessions_endpoint(body: BulkDeleteSessions):- """Delete every session in ``body.ids`` in a single DB transaction.-- Backs the dashboard's bulk-select-and-delete flow on the sessions- page. POST (not DELETE) because most HTTP clients refuse to send a- request body on DELETE and a body is the natural shape for a list- of IDs — Starlette accepts both, but POSTing a list keeps proxies,- curl, and the browser ``fetch`` API consistent.-- Per-row contract matches :meth:`SessionDB.delete_sessions`:-- * Unknown IDs are silently skipped (the response ``deleted`` count- reflects what really happened, not the input length). This is- deliberate — UI selection state can race against another tab's- delete, and we'd rather succeed-on-the-rest than fail-the-whole-- batch.- * Children of every deleted parent are orphaned, not cascade-- deleted.- * Active and archived sessions ARE deleted when explicitly- selected — unlike ``DELETE /api/sessions/empty``, the user- hand-picked the rows so we trust the selection.- * Like the other session-delete endpoints, this does NOT pass a- ``sessions_dir`` through; on-disk transcript / request-dump- cleanup runs at the CLI/agent layer on the next prune pass.-- The response carries the actual deleted count, so the dashboard- can surface it in a toast. The IDs that were removed are not- echoed back because the client already knows what it asked to- delete (unknown IDs are silently skipped — see contract above)- and can prune its in-memory list directly from the request.+ """Delete every session in ``body.ids`` in one transaction (POST because+ many clients refuse a DELETE body).++ Contract matches :meth:`SessionDB.delete_sessions`: unknown ids are+ skipped (``deleted`` reports what really happened — selection state can+ race another tab's delete), children are orphaned not cascaded, active+ and archived rows ARE deleted because the user hand-picked them, and+ on-disk transcript cleanup is left to the next prune pass."""- # Enforce a hard cap so a runaway/typo'd selection can't lock the- # DB writer for an extended window. The dashboard pages 20 rows- # at a time; 500 covers a "select all on every page in a- # reasonable scrollback" worst case without opening the door to- # multi-thousand-row transactions.+ # Hard cap so a runaway selection can't lock the writer for long; 500+ # covers "select all on every page of a reasonable scrollback".if len(body.ids) > 500:raise HTTPException(status_code=400,detail="ids must contain at most 500 entries",)- def _delete() -> int:- db = _open_session_db_for_profile(body.profile, read_only=False)- try:- return db.delete_sessions(body.ids)- finally:- db.close()-- deleted = await asyncio.to_thread(_delete)+ deleted = await asyncio.to_thread(+ _with_db, body.profile, lambda db: db.delete_sessions(body.ids), read_only=False+ )return {"ok": True, "deleted": deleted}@manage_router.post("/api/sessions/import")async def import_sessions_endpoint(request: Request):- """Import one or more sessions exported from the dashboard or CLI.-- This is intentionally separate from ``/api/ops/import``: that endpoint- restores a whole Hermes backup archive, while this endpoint is scoped to- session rows/messages and is safe to use from the Sessions page.- """+ """Import sessions exported from the dashboard or CLI (session rows only —+ ``/api/ops/import`` restores a whole backup archive)."""try:raw_body = await _read_session_import_body(request)body = SessionImport.model_validate_json(raw_body)@@ -525,111 +471,69 @@ async def import_sessions_endpoint(request: Request):@manage_router.get("/api/sessions/empty/count")async def count_empty_sessions_endpoint(profile: Optional[str] = None):- """Return the number of empty, ended, non-archived sessions.-- Drives the dashboard's "Delete empty (N)" button — when N is 0 the- UI hides the affordance so users aren't presented with a button- that does nothing. Cheap, single-COUNT query.- """- def _count() -> int:- db = _open_session_db_for_profile(profile, read_only=True)- try:- return db.count_empty_sessions()- finally:- db.close()-- return {"count": await asyncio.to_thread(_count)}+ """Count of empty, ended, non-archived sessions (drives the "Delete empty+ (N)" button, hidden when N is 0)."""+ count = await asyncio.to_thread(+ _with_db, profile, lambda db: db.count_empty_sessions(), read_only=True+ )+ return {"count": count}@manage_router.delete("/api/sessions/empty")async def delete_empty_sessions_endpoint(profile: Optional[str] = None):- """Delete every empty, ended, non-archived session in a single- transaction.-- Safety contract mirrors :meth:`SessionDB.delete_empty_sessions`:-- * "Empty" means the session owns no rows in ``messages`` at all — not- merely ``message_count == 0``. A rewound or in-place-compacted chat- keeps its dropped turns as soft-archived (``active = 0``) rows while- the counter reads zero, and those rows are the only recoverable copy- of the transcript (#95868).- * Active sessions are skipped (``ended_at IS NULL``) so a live- agent isn't yanked mid-handshake.- * Archived sessions are skipped — the user explicitly chose to- keep those rows.- * Children of deleted parents are orphaned, not cascade-deleted.-- Like the single-session ``DELETE /api/sessions/{id}`` endpoint- below, this doesn't pass a ``sessions_dir`` through — the on-disk- transcript / request-dump cleanup is wired at the CLI/agent layer- but the web server historically leaves file cleanup to the next- prune-on-startup pass. Matching that pre-existing trade-off keeps- the two delete endpoints' DB-vs-disk behaviour consistent.- """- def _delete() -> int:- db = _open_session_db_for_profile(profile, read_only=False)- try:- return db.delete_empty_sessions()- finally:- db.close()+ """Delete every empty, ended, non-archived session in one transaction.- deleted = await asyncio.to_thread(_delete)+ Mirrors :meth:`SessionDB.delete_empty_sessions`: "empty" means NO+ ``messages`` rows at all (a rewound/compacted chat reads+ ``message_count == 0`` while its soft-archived rows are the only copy of+ the transcript); active and archived sessions are skipped; children are+ orphaned; on-disk cleanup is left to the next prune pass.+ """+ deleted = await asyncio.to_thread(+ _with_db, profile, lambda db: db.delete_empty_sessions(), read_only=False+ )return {"ok": True, "deleted": deleted}@manage_router.get("/api/sessions/stats")async def get_session_stats(profile: Optional[str] = None):- """Session-store statistics for the Sessions page (mirrors `hermes sessions stats`).-- Registered before ``/api/sessions/{session_id}`` so the literal ``stats``- path isn't captured as a session id by the parameterized route.- """- db = _open_session_db_for_profile(profile, read_only=True)- try:- total = db.session_count(include_archived=True)- active_store = db.session_count(include_archived=False)- archived = db.session_count(archived_only=True)- messages = db.message_count()- by_source: Dict[str, int] = {}+ """Session-store statistics (mirrors `hermes sessions stats`). Registered+ before ``/api/sessions/{session_id}`` so ``stats`` isn't taken as an id."""+ def _stats(db):+ out = {+ "total": db.session_count(include_archived=True),+ "active_store": db.session_count(include_archived=False),+ "archived": db.session_count(archived_only=True),+ "messages": db.message_count(),+ "by_source": {},+ }try:- by_source = db.session_count_by_source(+ out["by_source"] = db.session_count_by_source(include_archived=True,exclude_children=True,)except Exception:pass- return {- "total": total,- "active_store": active_store,- "archived": archived,- "messages": messages,- "by_source": by_source,- }- finally:- db.close()+ return out++ return _with_db(profile, _stats, read_only=True)@manage_router.get("/api/sessions/{session_id}")async def get_session_detail(session_id: str, profile: Optional[str] = None):- db = _open_session_db_for_profile(profile, read_only=True)- try:+ def _detail(db):sid = _resolve_session_id(db, session_id)session = db.get_session(sid) if sid else Noneif not session:raise HTTPException(status_code=404, detail="Session not found")- # Always stamp the owning profile — the serving profile is known even- # when the request carries no ``?profile=`` (it's this process's own- # profile). Stamping only on explicit ``?profile=`` left rows for the- # default/primary profile systematically unowned, so multi-profile- # clients resolved them to whichever gateway happened to be active- # (cross-profile open asymmetry, #67603 family).- session["profile"] = (- _cron_profile_home(profile)[0] if profile else _cron_default_profile()- )+ # Always stamp the owner: stamping only on explicit ``?profile=`` left+ # default-profile rows unowned, so multi-profile clients resolved them+ # to whichever gateway happened to be active.+ session["profile"] = _serving_profile(profile)session["is_default_profile"] = session["profile"] == "default"return session- finally:- db.close()++ return _with_db(profile, _detail, read_only=True)@manage_router.get("/api/sessions/{session_id}/latest-descendant")@@ -637,14 +541,9 @@ async def get_session_latest_descendant(session_id: str,profile: Optional[str] = None,):- def _lookup():- db = _open_session_db_for_profile(profile, read_only=True)- try:- return _session_latest_descendant(session_id, db)- finally:- db.close()-- latest, path = await asyncio.to_thread(_lookup)+ latest, path = await asyncio.to_thread(+ _with_db, profile, lambda db: _session_latest_descendant(session_id, db), read_only=True+ )if not latest:raise HTTPException(status_code=404, detail="Session not found")return {@@ -670,32 +569,26 @@ async def get_session_messages(detail="order must be one of: oldest, latest",)- def _read():- db = _open_session_db_for_profile(profile, read_only=True)- try:- sid = _resolve_session_id(db, session_id)- if not sid:- return None- sid = db.resolve_resume_session_id(sid)- # Always page this endpoint. An omitted limit used to load an- # entire transcript, which can be hundreds of thousands of rows- # for a runaway session and exhaust the dashboard process. Keep- # explicit pagination anchored at the start, while the default- # dashboard view returns the latest page in chronological order.- default_page = limit is None- latest_page = order == "latest" or (order is None and default_page)- _limit = 500 if default_page else min(limit, 500)- return sid, _limit, db.get_messages(- sid,- limit=_limit,- offset=offset,- latest=latest_page,- include_compacted=include_compacted,- )- finally:- db.close()+ def _read(db):+ sid = _resolve_session_id(db, session_id)+ if not sid:+ return None+ sid = db.resolve_resume_session_id(sid)+ # Always page: an omitted limit used to load whole transcripts (hundreds+ # of thousands of rows for a runaway session). Explicit pagination+ # anchors at the start; the default view is the latest page.+ default_page = limit is None+ latest_page = order == "latest" or (order is None and default_page)+ _limit = 500 if default_page else min(limit, 500)+ return sid, _limit, db.get_messages(+ sid,+ limit=_limit,+ offset=offset,+ latest=latest_page,+ include_compacted=include_compacted,+ )- result = await asyncio.to_thread(_read)+ result = await asyncio.to_thread(_with_db, profile, _read, read_only=True)if result is None:raise HTTPException(status_code=404, detail="Session not found")sid, _limit, messages = result@@ -733,62 +626,38 @@ async def get_session_messages(@manage_router.delete("/api/sessions/{session_id}")async def delete_session_endpoint(session_id: str, profile: Optional[str] = None):- # ``profile`` deletes a session belonging to another (local) profile by- # opening its state.db directly. Remote profiles never reach here — the- # desktop routes their DELETE to the remote backend. Omit for current/default.- def _delete():- db = _open_session_db_for_profile(profile, read_only=False)- try:- # Resolve exact ids / unique prefixes like every other session endpoint- # (detail, messages, rename, export all do). A session that no longer- # exists is an idempotent success: DELETE's contract is "ensure it's- # gone", and the desktop optimistically removes the row then RESTORES it- # on any error — so a 404 on an already-absent row resurrected a ghost- # row and surfaced "session not found". /goal + auto-compression churn- # leaves transient empty rows (reaped by empty-session hygiene) that- # race the sidebar snapshot, which is exactly when this fired. Mirrors- # the bulk-delete endpoint, which already treats ghost ids as success.- sid = _resolve_session_id(db, session_id)- if not sid:- return {"ok": True, "already_absent": True}- db.delete_session(sid)- return {"ok": True}- finally:- db.close()+ # ``profile`` opens another local profile's state.db directly (remote+ # profiles never reach here — the desktop routes those to the remote backend).+ def _delete(db):+ # An already-absent session is an idempotent success: the desktop+ # optimistically removes the row and RESTORES it on any error, so a 404+ # here resurrected ghost rows (transient empties from /goal ++ # auto-compression churn racing the sidebar snapshot).+ sid = _resolve_session_id(db, session_id)+ if not sid:+ return {"ok": True, "already_absent": True}+ db.delete_session(sid)+ return {"ok": True}- return await asyncio.to_thread(_delete)+ return await asyncio.to_thread(_with_db, profile, _delete, read_only=False)@manage_router.post("/api/sessions/owner-backfill")async def backfill_session_owner_profiles(body: SessionOwnerBackfill):- """Stamp legacy ``profile_name = NULL`` session rows with this store's own- serving-profile identity (#94724 legacy-session migration).-- Pre-#95407 rows never recorded an owning profile. That was fine while one- backend served everything, but a Desktop with registry topology (≥2- registered connections) fails closed on unowned rows by design — leaving- every pre-campaign session unresumable with no migration path. Each- profile's ``state.db`` belongs to exactly one profile, so stamping that- store's own name is a single-match backfill, never a guess; the value- written is the SAME serving-profile identity the list endpoints already- stamp onto outgoing rows (``row_profile`` in ``get_sessions``). Idempotent- and one-shot-per-row: non-NULL owners are never overwritten and a second- call reports 0.- """- profile_name: Optional[str] = None- if body.profile:- profile_name, _ = _cron_profile_home(body.profile)- stamp = profile_name or _cron_default_profile()+ """Stamp legacy ``profile_name = NULL`` rows with this store's own+ serving-profile identity.- def _backfill():- db = _open_session_db_for_profile(body.profile, read_only=False)- try:- return db.backfill_null_session_profiles(stamp)- finally:- db.close()+ A multi-connection Desktop fails closed on unowned rows, leaving legacy+ sessions unresumable. Each ``state.db`` belongs to exactly one profile,+ so this is a single-match backfill (the same identity ``get_sessions``+ stamps on outgoing rows), idempotent: non-NULL owners are never overwritten.+ """+ stamp = _serving_profile(body.profile)try:- stamped = await asyncio.to_thread(_backfill)+ stamped = await asyncio.to_thread(+ _with_db, body.profile, lambda db: db.backfill_null_session_profiles(stamp), read_only=False+ )except HTTPException:raiseexcept Exception:@@ -806,28 +675,16 @@ async def backfill_session_owner_profiles(body: SessionOwnerBackfill):@manage_router.patch("/api/sessions/{session_id}")async def rename_session_endpoint(session_id: str, body: SessionRename):- """Update a session: rename, archive, hide, pin, and/or mark read/unread.-- ``title`` renames (empty/null clears the title); ``archived`` soft-hides or- restores the session; ``hidden`` controls generic list visibility;- ``pinned`` sets the durable keep flag (exempts the- session from the auto-archive sweep); ``unread`` toggles the read-state- watermark (True = explicitly unread, False = read up to now — see- ``SessionDB.set_session_read``). Any field may be omitted. ``profile``- targets another profile's session.- """- db = _open_session_db_for_profile(body.profile, read_only=False)- try:+ """Update a session: ``title`` (empty clears), ``archived``, ``hidden``,+ ``pinned`` (exempts from the auto-archive sweep), ``unread`` (True =+ explicitly unread, False = read up to now). Any field may be omitted."""+ flags = ("archived", "hidden", "pinned", "unread")++ def _update(db):sid = _resolve_session_id(db, session_id)if not sid:raise HTTPException(status_code=404, detail="Session not found")- if (- body.title is None- and body.archived is None- and body.hidden is None- and body.pinned is None- and body.unread is None- ):+ if body.title is None and all(getattr(body, f) is None for f in flags):raise HTTPException(status_code=400,detail="Nothing to update; provide 'title', 'archived', 'hidden', 'pinned', and/or 'unread'.",@@ -847,31 +704,22 @@ async def rename_session_endpoint(session_id: str, body: SessionRename):if body.unread is not None:db.set_session_read(sid, read=not body.unread)result = {"ok": True, "title": db.get_session_title(sid) or ""}- if body.archived is not None:- result["archived"] = bool(body.archived)- if body.hidden is not None:- result["hidden"] = bool(body.hidden)- if body.pinned is not None:- result["pinned"] = bool(body.pinned)- if body.unread is not None:- result["unread"] = bool(body.unread)+ for f in flags:+ if getattr(body, f) is not None:+ result[f] = bool(getattr(body, f))return result- finally:- db.close()++ return _with_db(body.profile, _update, read_only=False)@manage_router.get("/api/sessions/{session_id}/export")async def export_session_endpoint(session_id: str, profile: Optional[str] = None):"""Stream a single session (metadata + messages) as JSON."""- def _prepare_export():- db = _open_session_db_for_profile(profile, read_only=True)- try:- sid = _resolve_session_id(db, session_id)- return (sid, db.get_session(sid)) if sid else None- finally:- db.close()+ def _prepare_export(db):+ sid = _resolve_session_id(db, session_id)+ return (sid, db.get_session(sid)) if sid else None- prepared = await asyncio.to_thread(_prepare_export)+ prepared = await asyncio.to_thread(_with_db, profile, _prepare_export, read_only=True)if prepared is None or prepared[1] is None:raise HTTPException(status_code=404, detail="Session not found")diff --git a/hermes_cli/web_routers/skills.py b/hermes_cli/web_routers/skills.pyindex 457a9f46ca9..ecc85e6a9f0 100644--- a/hermes_cli/web_routers/skills.py+++ b/hermes_cli/web_routers/skills.py@@ -1,22 +1,16 @@-"""Skills dashboard routes (extracted verbatim from web_server.py).--Two routers because the original registration points are far apart and global-route order matters: ``hub_router`` (the skills-hub install/search/scan-endpoints) was registered before the profiles ``router`` include in-web_server, the plain skills CRUD ``router`` after it - each is mounted at-its original registration point.--Handler bodies are byte-identical; web_server-owned helpers are reached via-the late-binding seam in :mod:`hermes_cli.web_deps` so tests that-``monkeypatch.setattr(web_server, "_spawn_hermes_action", ...)`` keep-working.+"""Skills dashboard routes.++Two routers because global route order matters: ``hub_router`` (skills-hub+install/search/scan) was registered before the profiles router include in+web_server, the plain skills CRUD ``router`` after it — each is mounted at its+original registration point. web_server-owned helpers are reached via the+late-binding seam so ``monkeypatch.setattr(web_server, ...)`` keeps working."""-import asyncio # noqa: F401 — used by handlers-import logging-from typing import Optional # noqa: F401+import asyncio+from typing import Optional-from fastapi import APIRouter, HTTPException # noqa: F401+from fastapi import APIRouter, HTTPExceptionfrom hermes_cli.web_deps import late, LateStatefrom hermes_cli.web_models import (@@ -27,99 +21,69 @@ from hermes_cli.web_models import (SkillUninstallRequest,SkillsUpdateRequest,)--# Same logger the handlers used before extraction (identical logger object).-_log = logging.getLogger("hermes_cli.web_server")+from hermes_cli.web_routers._common import (+ _profile_scope,+ config_write_scope,+ http_failure,+ log as _log,+ require,+ scoped_to_thread,+ spawn_profile_action,+)hub_router = APIRouter()router = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent)._clear_skills_prompt_cache = late("_clear_skills_prompt_cache")_config_profile_scope = late("_config_profile_scope")_hub_action_name = late("_hub_action_name")_installed_hub_identifiers = late("_installed_hub_identifiers")-_profile_cli_args = late("_profile_cli_args")-_profile_scope = late("_profile_scope")_skill_meta_to_payload = late("_skill_meta_to_payload")-_spawn_hermes_action = late("_spawn_hermes_action")load_config = late("load_config")--# Live proxies for web_server-owned module state (mutations/monkeypatches-# on web_server remain authoritative; resolved at operation time)._SKILL_HUB_SOURCE_LABELS = LateState("_SKILL_HUB_SOURCE_LABELS")-# Config read-modify-write serialization for off-loop handlers (see the-# definition in web_server.py). LateState supports ``with``-blocks, so this-# is the live lock object, not a frozen import-time copy.-_CONFIG_MUTATION_LOCK = LateState("_CONFIG_MUTATION_LOCK")@hub_router.post("/api/skills/hub/install")async def install_skill_hub(body: SkillInstallRequest, profile: Optional[str] = None):- identifier = (body.identifier or "").strip()- if not identifier:- raise HTTPException(status_code=400, detail="identifier is required")- name = _hub_action_name("install", identifier)- try:- proc = _spawn_hermes_action(- _profile_cli_args(body.profile or profile)- + ["skills", "install", identifier, "--yes"],- name,- )- except HTTPException:- raise- except Exception as exc:- _log.exception("Failed to spawn skills install")- raise HTTPException(status_code=500, detail=f"Failed to install skill: {exc}")- return {"ok": True, "pid": proc.pid, "name": name}+ identifier = require(body.identifier, "identifier is required")+ return spawn_profile_action(+ body.profile or profile,+ ["skills", "install", identifier, "--yes"],+ _hub_action_name("install", identifier),+ log_msg="Failed to spawn skills install",+ prefix="Failed to install skill",+ )@hub_router.post("/api/skills/hub/uninstall")async def uninstall_skill_hub(body: SkillUninstallRequest, profile: Optional[str] = None):- name = (body.name or "").strip()- if not name:- raise HTTPException(status_code=400, detail="name is required")- action = _hub_action_name("uninstall", name)- try:- proc = _spawn_hermes_action(- _profile_cli_args(body.profile or profile) + ["skills", "uninstall", name, "--yes"],- action,- )- except HTTPException:- raise- except Exception as exc:- _log.exception("Failed to spawn skills uninstall")- raise HTTPException(status_code=500, detail=f"Failed to uninstall skill: {exc}")- return {"ok": True, "pid": proc.pid, "name": action}+ name = require(body.name, "name is required")+ return spawn_profile_action(+ body.profile or profile,+ ["skills", "uninstall", name, "--yes"],+ _hub_action_name("uninstall", name),+ log_msg="Failed to spawn skills uninstall",+ prefix="Failed to uninstall skill",+ )@hub_router.post("/api/skills/hub/update")async def update_skills_hub(body: Optional[SkillsUpdateRequest] = None, profile: Optional[str] = None):- try:- effective = (body.profile if body else None) or profile- proc = _spawn_hermes_action(- _profile_cli_args(effective) + ["skills", "update"], "skills-update"- )- except HTTPException:- raise- except Exception as exc:- _log.exception("Failed to spawn skills update")- raise HTTPException(status_code=500, detail=f"Failed to update skills: {exc}")- return {"ok": True, "pid": proc.pid, "name": "skills-update"}+ return spawn_profile_action(+ (body.profile if body else None) or profile,+ ["skills", "update"],+ "skills-update",+ log_msg="Failed to spawn skills update",+ prefix="Failed to update skills",+ )@hub_router.get("/api/skills/hub/official")async def list_official_skills(profile: Optional[str] = None):- """List the ENTIRE built-in optional-skills catalog shipped with the repo.-- Backs the desktop Capabilities → Skills list: every official optional- skill appears alongside the installed skills with an install affordance.- Local-checkout scan only (no network), plus the per-profile installed- map so the UI can mark rows that are already installed.- """+ """The ENTIRE built-in optional-skills catalog (local scan, no network),+ each row marked installed-or-not for ``profile``."""def _run():from tools.skills_hub import OptionalSkillSource@@ -129,33 +93,23 @@ async def list_official_skills(profile: Optional[str] = None):for m in OptionalSkillSource().list_local():payload = _skill_meta_to_payload(m)ident = payload.get("identifier") or ""- # identifier format: official/<category>/<skill> (category dirs- # mirror skills/): surface the category for row subtitles.+ # identifier format: official/<category>/<skill> — surface the+ # category for row subtitles.rel = ident.split("/", 1)[-1] if "/" in ident else identpayload["category"] = rel.split("/", 1)[0] if "/" in rel else "general"payload["installed"] = ident in installedout.append(payload)return {"skills": out}- try:+ with http_failure("official skills catalog listing failed", 502, "Official catalog failed"):return await asyncio.to_thread(_run)- except HTTPException:- raise- except Exception as exc:- _log.exception("official skills catalog listing failed")- raise HTTPException(status_code=502, detail=f"Official catalog failed: {exc}")@hub_router.get("/api/skills/hub/sources")async def list_skills_hub_sources(profile: Optional[str] = None):- """List the configured skill-hub sources and installed-skill provenance.-- Gives the dashboard something to show BEFORE a search runs — which hubs- are wired up, their trust tier, and a set of featured skills pulled from- the centralized index (zero extra API calls). Without this the Browse-hub- tab is a blank page with no indication it's even connected to anything.- ``profile`` scopes the installed-skill provenance to that profile.- """+ """Configured skill-hub sources + installed-skill provenance, so the+ Browse-hub tab has something to show before a search runs. ``profile``+ scopes the installed-skill provenance."""def _run():from tools.skills_hub import create_source_router@@ -192,12 +146,10 @@ async def list_skills_hub_sources(profile: Optional[str] = None):except Exception:featured = []out.append(entry)- # Tell the UI which sources are worth searching individually (for its- # progressive per-source fan-out). Mirror parallel_search_sources: when- # the centralized index is available it already subsumes the external- # API sources, so they're redundant — skipping them avoids ~70 GitHub- # calls per keystroke. Keep this set in sync with that function's- # ``_api_source_ids``.+ # Which sources are worth searching individually (progressive per-source+ # fan-out). Mirrors parallel_search_sources: an available index already+ # subsumes the external API sources, so skipping them saves ~70 GitHub+ # calls per keystroke. Keep in sync with that function's _api_source_ids._api_source_ids = frozenset({"github", "skills-sh", "clawhub", "lobehub", "well-known"})@@ -210,26 +162,16 @@ async def list_skills_hub_sources(profile: Optional[str] = None):"installed": _installed_hub_identifiers(profile),}- try:+ with http_failure("skills hub sources listing failed", 502, "Hub sources failed"):return await asyncio.to_thread(_run)- except HTTPException:- raise- except Exception as exc:- _log.exception("skills hub sources listing failed")- raise HTTPException(status_code=502, detail=f"Hub sources failed: {exc}")@hub_router.get("/api/skills/hub/search")async def search_skills_hub(q: str = "", source: str = "all", limit: int = 20, profile: Optional[str] = None):- """Search the skill hub across all configured sources.-- Network-bound (parallel source search); runs in a thread so the FastAPI- loop isn't blocked. Returns structured results the UI installs by- identifier via POST /api/skills/hub/install, previews via- /api/skills/hub/preview, and scans via /api/skills/hub/scan.- """+ """Search the skill hub across all configured sources (network-bound,+ runs in a thread). Results install by identifier via /hub/install."""query = (q or "").strip()if not query:return {"results": [], "source_counts": {}, "timed_out": [], "installed": {}}@@ -261,30 +203,16 @@ async def search_skills_hub("installed": _installed_hub_identifiers(profile),}- try:+ with http_failure("skills hub search failed", 502, "Hub search failed"):return await asyncio.to_thread(_run)- except HTTPException:- raise- except Exception as exc:- _log.exception("skills hub search failed")- raise HTTPException(status_code=502, detail=f"Hub search failed: {exc}")@hub_router.get("/api/skills/hub/preview")async def preview_skill_hub(identifier: str = "", profile: Optional[str] = None):- """Fetch a hub skill's SKILL.md content + metadata for in-dashboard reading.-- Resolves the identifier across configured sources (same path the CLI- installer uses), then returns the rendered SKILL.md text and the file- manifest WITHOUT installing anything. This is the 'read the actual skill- before installing' affordance the Browse-hub tab was missing.-- Scoped to ``profile`` so a non-default profile with different hub taps- resolves against ITS source router, not the default profile's.- """- ident = (identifier or "").strip()- if not ident:- raise HTTPException(status_code=400, detail="identifier is required")+ """A hub skill's SKILL.md + file manifest WITHOUT installing it. Scoped to+ ``profile`` so a profile with different hub taps resolves against ITS+ source router."""+ ident = require(identifier, "identifier is required")def _run():from hermes_cli.skills_hub import _resolve_source_meta_and_bundle@@ -301,9 +229,8 @@ async def preview_skill_hub(identifier: str = "", profile: Optional[str] = None)if bundle:for rel, content in (bundle.files or {}).items():if isinstance(content, bytes):- # Some sources (e.g. official optional skills) store every- # file as bytes. Decode text so SKILL.md / docs render;- # only fall back to a placeholder for genuinely-binary data.+ # Some sources store every file as bytes; decode text so+ # SKILL.md renders, placeholder only for genuinely-binary data.try:files[rel] = content.decode("utf-8")except UnicodeDecodeError:@@ -337,20 +264,11 @@ async def preview_skill_hub(identifier: str = "", profile: Optional[str] = None)@hub_router.get("/api/skills/hub/scan")async def scan_skill_hub(identifier: str = "", profile: Optional[str] = None):- """Run the install-time security scan on a hub skill WITHOUT installing it.-- Fetches the bundle, quarantines it, and runs the same `scan_skill` /- `should_allow_install` pipeline the CLI installer uses — then cleans up the- quarantine. Returns the verdict, per-finding detail, trust tier, and the- install-policy decision so the dashboard can show a visual safety result- on demand (the 'scan' button the Browse-hub tab was missing).-- Scoped to ``profile`` so the bundle resolves against that profile's hub- source router, matching where an install would pull it from.- """- ident = (identifier or "").strip()- if not ident:- raise HTTPException(status_code=400, detail="identifier is required")+ """Run the install-time security scan on a hub skill WITHOUT installing it+ (same ``scan_skill`` / ``should_allow_install`` pipeline as the CLI, on a+ quarantined bundle that is cleaned up afterwards). Scoped to ``profile``+ so the bundle resolves where an install would pull it from."""+ ident = require(identifier, "identifier is required")def _run():import shutil as _shutil@@ -379,9 +297,8 @@ async def scan_skill_hub(identifier: str = "", profile: Optional[str] = None):try:q_path = quarantine_bundle(bundle)result = scan_skill(q_path, source=scan_source)- # Advisory SkillEvaluator Tier 1 second opinion (same contract- # as the CLI installer: optional binary, never blocks, errors- # degrade to no data).+ # Advisory SkillEvaluator Tier 1 second opinion: optional binary,+ # never blocks, errors degrade to no data (same as the CLI installer).try:from tools.skillevaluator_scan import (run_tier1_scan, tier1_advisory_enabled,@@ -411,15 +328,8 @@ async def scan_skill_hub(identifier: str = "", profile: Optional[str] = None):if q_path is not None:_shutil.rmtree(q_path, ignore_errors=True)- allowed, reason = should_allow_install(result, force=False)# `allowed` may be None ("ask") for agent-created/dangerous gates.- if allowed is True:- policy = "allow"- elif allowed is None:- policy = "ask"- else:- policy = "block"-+ allowed, reason = should_allow_install(result, force=False)findings = [{"severity": f.severity,@@ -430,7 +340,6 @@ async def scan_skill_hub(identifier: str = "", profile: Optional[str] = None):}for f in result.findings]- # Per-severity tally for an at-a-glance summary.counts = {"critical": 0, "high": 0, "medium": 0, "low": 0}for f in result.findings:if f.severity in counts:@@ -443,13 +352,11 @@ async def scan_skill_hub(identifier: str = "", profile: Optional[str] = None):"trust_level": result.trust_level,"verdict": result.verdict,"summary": result.summary,- "policy": policy,+ "policy": "allow" if allowed is True else "ask" if allowed is None else "block","policy_reason": reason,"findings": findings,"severity_counts": counts,- # Advisory SkillEvaluator Tier 1 block, or None when the- # optional scanner isn't installed/enabled.- "tier1": tier1,+ "tier1": tier1, # None when the optional scanner isn't installed/enabled}try:@@ -502,15 +409,14 @@ async def toggle_skill(body: SkillToggle, profile: Optional[str] = None):from hermes_cli.skills_config import get_disabled_skills, save_disabled_skillsdef _run():- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- config = load_config()- disabled = get_disabled_skills(config)- if body.enabled:- disabled.discard(body.name)- else:- disabled.add(body.name)- save_disabled_skills(config, disabled)+ with config_write_scope(body.profile or profile):+ config = load_config()+ disabled = get_disabled_skills(config)+ if body.enabled:+ disabled.discard(body.name)+ else:+ disabled.add(body.name)+ save_disabled_skills(config, disabled)return {"ok": True, "name": body.name, "enabled": body.enabled}return await asyncio.to_thread(_run)@@ -518,42 +424,35 @@ async def toggle_skill(body: SkillToggle, profile: Optional[str] = None):@router.get("/api/skills/content")async def get_skill_content(name: str, profile: Optional[str] = None):- """Return the raw SKILL.md text for a skill, for the dashboard editor."""+ """Raw SKILL.md text for the dashboard editor."""from tools.skill_manager_tool import _find_skill- def _run():- with _profile_scope(profile):- found = _find_skill(name)- if not found:- raise HTTPException(status_code=404, detail=f"Skill '{name}' not found.")- skill_md = found["path"] / "SKILL.md"- if not skill_md.exists():- raise HTTPException(status_code=404, detail=f"Skill '{name}' has no SKILL.md.")- try:- content = skill_md.read_text(encoding="utf-8")- except OSError as exc:- raise HTTPException(status_code=500, detail=str(exc)) from exc- return {"name": name, "content": content, "path": str(skill_md)}+ def _read():+ found = _find_skill(name)+ if not found:+ raise HTTPException(status_code=404, detail=f"Skill '{name}' not found.")+ skill_md = found["path"] / "SKILL.md"+ if not skill_md.exists():+ raise HTTPException(status_code=404, detail=f"Skill '{name}' has no SKILL.md.")+ try:+ content = skill_md.read_text(encoding="utf-8")+ except OSError as exc:+ raise HTTPException(status_code=500, detail=str(exc)) from exc+ return {"name": name, "content": content, "path": str(skill_md)}- return await asyncio.to_thread(_run)+ return await scoped_to_thread(profile, _read)@router.post("/api/skills")async def create_skill(body: SkillCreate):- """Create a new custom skill (SKILL.md) from the dashboard editor.-- Calls the same validated write path as the agent's ``skill_manage``- tool (frontmatter validation, name/category validation, size limit,- optional security scan) — but bypasses the agent write-approval gate:- a write from the authenticated dashboard IS the user acting directly.- """+ """Create a custom skill via the same validated write path as the agent's+ ``skill_manage`` tool, minus the agent write-approval gate — a write from+ the authenticated dashboard IS the user acting directly."""from tools.skill_manager_tool import _create_skill- def _run():- with _profile_scope(body.profile):- return _create_skill(body.name, body.content, body.category or None)-- result = await asyncio.to_thread(_run)+ result = await scoped_to_thread(+ body.profile, lambda: _create_skill(body.name, body.content, body.category or None)+ )if not result.get("success"):raise HTTPException(status_code=400, detail=result.get("error", "Failed to create skill."))_clear_skills_prompt_cache()@@ -565,11 +464,7 @@ async def update_skill_content(body: SkillContentUpdate):"""Replace the SKILL.md of an existing skill (full rewrite) from the editor."""from tools.skill_manager_tool import _edit_skill- def _run():- with _profile_scope(body.profile):- return _edit_skill(body.name, body.content)-- result = await asyncio.to_thread(_run)+ result = await scoped_to_thread(body.profile, lambda: _edit_skill(body.name, body.content))if not result.get("success"):err = result.get("error", "Failed to update skill.")status = 404 if "not found" in str(err).lower() else 400diff --git a/hermes_cli/web_routers/tools.py b/hermes_cli/web_routers/tools.pyindex f6ebace20c9..f8f72acccbb 100644--- a/hermes_cli/web_routers/tools.py+++ b/hermes_cli/web_routers/tools.py@@ -1,21 +1,15 @@-"""Toolset / terminal-backend dashboard routes (extracted verbatim from-web_server.py).--Handler bodies are byte-identical. The toolset/terminal catalogs-(``_MODEL_CATALOG_TOOLSETS``, ``_TERMINAL_BACKENDS``,-``_TERMINAL_BACKEND_NAMES`` - some defined *after* this router's mount point-in web_server's body) and all helpers stay in web_server - reached via the-late-binding seam in :mod:`hermes_cli.web_deps` (``late`` for callables,-``LateState`` for the catalog constants) so monkeypatching on web_server-stays authoritative.+"""Toolset / terminal-backend dashboard routes.++The toolset/terminal catalogs and helpers stay in web_server (some are defined+*after* this router's mount point) — reached via the late-binding seam so+monkeypatching on web_server stays authoritative."""import asyncio-import logging-import sys # noqa: F401 — used by handlers-from typing import Any, Dict, List, Optional # noqa: F401+import sys+from typing import Any, Dict, List, Optional-from fastapi import APIRouter, HTTPException # noqa: F401+from fastapi import APIRouter, HTTPExceptionfrom hermes_cli.web_deps import late, LateStatefrom hermes_cli.web_models import (@@ -26,37 +20,52 @@ from hermes_cli.web_models import (ToolsetProviderSelect,ToolsetToggle,)--# Same logger the handlers used before extraction (identical logger object).-_log = logging.getLogger("hermes_cli.web_server")+from hermes_cli.web_routers._common import (+ _CONFIG_MUTATION_LOCK,+ _profile_cli_args,+ _profile_scope,+ _spawn_hermes_action,+ config_write_scope,+ log as _log,+ scoped_to_thread,+ spawn_profile_action,+)router = APIRouter()-# Late-bound web_server helpers (resolved at call time; cycle-safe,-# monkeypatch-transparent)._find_toolset_provider_row = late("_find_toolset_provider_row")_probe_terminal_backend = late("_probe_terminal_backend")-_profile_cli_args = late("_profile_cli_args")-_profile_scope = late("_profile_scope")_resolve_toolset_model_plugin = late("_resolve_toolset_model_plugin")-_spawn_hermes_action = late("_spawn_hermes_action")_toolset_model_catalog = late("_toolset_model_catalog")load_config = late("load_config")save_config = late("save_config")run_in_threadpool = late("run_in_threadpool")--# Live proxies for web_server-owned module state (mutations/monkeypatches-# on web_server remain authoritative; resolved at operation time)._MODEL_CATALOG_TOOLSETS = LateState("_MODEL_CATALOG_TOOLSETS")-_TERMINAL_BACKENDS = LateState("_TERMINAL_BACKENDS")-_TERMINAL_BACKEND_NAMES = LateState("_TERMINAL_BACKEND_NAMES")-# Dynamic variants: built-ins + plugin-registered backends, computed per-# request so a plugin installed after server start still shows up.+# Built-ins + plugin-registered backends, computed per request so a plugin+# installed after server start still shows up._terminal_backend_rows = late("_terminal_backend_rows")_terminal_backend_names = late("_terminal_backend_names")-# Config read-modify-write serialization for off-loop handlers (defined in-# web_server.py; LateState supports ``with``-blocks, so this is the live lock).-_CONFIG_MUTATION_LOCK = LateState("_CONFIG_MUTATION_LOCK")+++def _require_known_toolset(name: str) -> None:+ """400 for toolset keys outside the effective configurable set."""+ from hermes_cli.tools_config import _get_effective_configurable_toolsets++ if name not in {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}:+ raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")+++def _dict_section(config: dict, key: str) -> dict:+ """``config[key]`` as a dict, replacing a non-dict value in place."""+ section = config.setdefault(key, {})+ if not isinstance(section, dict):+ section = {}+ config[key] = section+ return section+++def _no_models(name: str) -> dict:+ return {"name": name, "has_models": False, "models": [], "current": None, "default": None}@router.get("/api/tools/toolsets")@@ -127,68 +136,48 @@ async def get_toolsets(profile: Optional[str] = None):@router.put("/api/tools/toolsets/{name}")async def toggle_toolset(name: str, body: ToolsetToggle, profile: Optional[str] = None):- """Enable/disable a configurable toolset for its configuration platform.-- Most toolsets persist to ``platform_toolsets.cli``. Platform-restricted- toolsets instead target their supported platform (for example, Discord's- native toolsets persist to ``platform_toolsets.discord``). The shared- ``_save_platform_tools`` helper keeps the GUI and CLI in lockstep. Scoped- to ``body.profile`` when provided. Returns 400 for unknown toolset keys.- """+ """Enable/disable a configurable toolset for its configuration platform+ (``platform_toolsets.cli`` for most; platform-restricted toolsets target+ their own platform) via the same ``_save_platform_tools`` the CLI uses."""from hermes_cli.tools_config import (_CONFIG_ONLY_TOOLSETS,- _get_effective_configurable_toolsets,_get_platform_tools,_save_platform_tools,_toolset_configuration_platform,)- valid = {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}- if name not in valid:- raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")-+ _require_known_toolset(name)target_platform = _toolset_configuration_platform(name)def _run():- if name in _CONFIG_ONLY_TOOLSETS:- # Config-only capabilities (stt) toggle their own config section's- # ``enabled`` flag — there is no platform_toolsets entry to write.- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- config = load_config()- section = config.setdefault(name, {})- if not isinstance(section, dict):- section = {}- config[name] = section- section["enabled"] = bool(body.enabled)- save_config(config)- return- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- config = load_config()- enabled = set(- _get_platform_tools(- config,- target_platform,- include_default_mcp_servers=False,- )+ with config_write_scope(body.profile or profile):+ config = load_config()+ if name in _CONFIG_ONLY_TOOLSETS:+ # Config-only capabilities (stt) toggle their own section's+ # ``enabled`` flag — there is no platform_toolsets entry.+ _dict_section(config, name)["enabled"] = bool(body.enabled)+ save_config(config)+ return+ enabled = set(+ _get_platform_tools(+ config,+ target_platform,+ include_default_mcp_servers=False,)- if body.enabled:- enabled.add(name)- else:- enabled.discard(name)- _save_platform_tools(config, target_platform, enabled)+ )+ if body.enabled:+ enabled.add(name)+ else:+ enabled.discard(name)+ _save_platform_tools(config, target_platform, enabled)await asyncio.to_thread(_run)- # Install-on-enable: when the newly enabled toolset's provider carries a- # post_setup hook with a registered, UNSATISFIED install-state predicate- # (cua-driver binary missing, etc. — see _POST_SETUP_INSTALLED), spawn the- # same background install `hermes tools` runs interactively. Without this,- # a dashboard/desktop toggle "saves" but the tool silently never appears- # in the schema because its check_fn can't find the binary — the exact- # dead-end that forced users to discover `hermes computer-use install`- # by hand. Best-effort: a spawn failure never fails the toggle.+ # Install-on-enable: a provider with a post_setup hook whose install-state+ # predicate is UNSATISFIED (cua-driver binary missing, etc.) gets the same+ # background install `hermes tools` runs interactively — otherwise the+ # toggle "saves" but the tool never appears because its check_fn can't+ # find the binary. Best-effort: a spawn failure never fails the toggle.post_setup_started: Optional[str] = Noneif body.enabled and name not in _CONFIG_ONLY_TOOLSETS:def _pending_install_key() -> Optional[str]:@@ -232,17 +221,12 @@ async def toggle_toolset(name: str, body: ToolsetToggle, profile: Optional[str]@router.get("/api/tools/toolsets/{name}/config")async def get_toolset_config(name: str, profile: Optional[str] = None):- """Return the provider matrix + key status for a toolset's config panel.-- Surfaces the same provider rows the CLI ``hermes tools`` picker shows- (via ``_visible_providers``), each with its ``env_vars`` annotated with- current ``is_set`` state so the GUI can render provider selection + key- entry. Toolsets without a ``TOOL_CATEGORIES`` entry return an empty- provider list and ``has_category: false``. Returns 400 for unknown keys.- """+ """Provider matrix + key status for a toolset's config panel: the same rows+ the CLI ``hermes tools`` picker shows, each env var annotated with+ ``is_set``. No ``TOOL_CATEGORIES`` entry -> empty list, ``has_category:+ false``."""from hermes_cli.tools_config import (TOOL_CATEGORIES,- _get_effective_configurable_toolsets,_is_provider_active,_visible_providers,provider_readiness_status,@@ -251,9 +235,7 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):from hermes_cli.config import get_env_valuefrom hermes_cli.nous_subscription import get_nous_subscription_features- valid = {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}- if name not in valid:- raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")+ _require_known_toolset(name)def _read():with _profile_scope(profile):@@ -264,9 +246,7 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):active_search_backend = Noneactive_extract_backend = Noneif cat:- # Fetch portal/entitlement state once for the whole matrix — the- # per-provider readiness computation below reuses it instead of- # re-probing per row.+ # Entitlement state fetched once for the whole matrix.features = get_nous_subscription_features(config, force_fresh=True)for prov in _visible_providers(cat, config, force_fresh=True):env_vars = [@@ -279,10 +259,8 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):}for e in prov.get("env_vars", [])]- # Surface the same active-provider determination the CLI picker- # uses (``_is_provider_active``) so the GUI highlights the provider- # actually written to config (e.g. web.backend), not just the first- # keyless one in the list.+ # Same active-provider determination as the CLI picker, so the+ # GUI highlights the provider actually written to config.is_active = _is_provider_active(prov, config, force_fresh=True)if is_active and active_provider is None:active_provider = prov["name"]@@ -294,33 +272,26 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):"post_setup": prov.get("post_setup"),"requires_nous_auth": bool(prov.get("requires_nous_auth")),"is_active": is_active,- # Honest server-side readiness. The GUI's old client-side- # heuristic showed "Ready" for every zero-env-var row —- # including logged-out Nous Subscription rows and never-run- # post_setup installs (see provider_readiness_status).+ # Server-side readiness: zero-env-var rows are NOT+ # automatically ready (logged-out Nous rows, never-run+ # post_setup installs)."status": provider_readiness_status(prov, config, features=features, is_active=is_active),}if name == "web" and prov.get("web_backend"):- # The runtime split web into two capabilities long ago- # (web.search_backend / web.extract_backend); surface each- # row's backend key and which capabilities it can serve so- # the GUI can offer per-capability selection.+ # web is two capabilities (search/extract); surface each+ # row's backend key + capabilities for per-capability selection.row["web_backend"] = prov["web_backend"]row["capabilities"] = web_provider_capabilities(prov["web_backend"])if name == "tts" and prov.get("tts_provider"):- # The provider key written to tts.provider on selection.- # Doubles as the config section holding the provider's- # voice/model settings (tts.<key>.*) so the GUI can render- # those fields inline in the Capabilities panel.+ # Key written to tts.provider; doubles as the config section+ # (tts.<key>.*) holding the provider's voice/model settings.row["tts_provider"] = prov["tts_provider"]providers.append(row)if name == "web":- # Resolve the per-capability active backends exactly the way the- # web_search / web_extract dispatchers do (per-capability key →- # shared web.backend → credential auto-detect), so the GUI badges- # reflect what a tool call would actually hit right now.+ # Resolve active backends exactly as the web_search/web_extract+ # dispatchers do, so badges reflect what a call would hit now.try:from tools.web_tools import _get_extract_backend, _get_search_backend@@ -349,17 +320,12 @@ async def get_toolset_config(name: str, profile: Optional[str] = None):async def get_toolset_models(name: str, provider: Optional[str] = None, profile: Optional[str] = None):- """Return the model catalog for a toolset backend (image/video gen).-- The GUI counterpart of the model picker `hermes tools` runs after a- backend is selected — e.g. FAL's multi-model catalog (speed / strengths /- price per model). ``provider`` names a picker row; omitted, the currently- active provider is used. Toolsets without model catalogs return- ``has_models: false``.- """+ """Model catalog for a toolset backend (image/video gen) — the GUI+ counterpart of the CLI model picker. ``provider`` names a picker row+ (default: the active provider); no catalog -> ``has_models: false``."""section = _MODEL_CATALOG_TOOLSETS.get(name)if section is None:- return {"name": name, "has_models": False, "models": [], "current": None, "default": None}+ return _no_models(name)def _read():with _profile_scope(profile):@@ -382,13 +348,7 @@ async def get_toolset_models(resolved = await asyncio.to_thread(_read)if resolved is None:- return {- "name": name,- "has_models": False,- "models": [],- "current": None,- "default": None,- }+ return _no_models(name)row, plugin, catalog, default_model, current = resolvedmodels = [@@ -416,12 +376,8 @@ async def get_toolset_models(async def select_toolset_model(name: str, body: ToolsetModelSelect, profile: Optional[str] = None):- """Persist a backend model selection (``image_gen.model`` / ``video_gen.model``).-- Validates the model against the resolved backend's catalog — the same- write the CLI's post-selection model picker performs. Returns 400 for- toolsets without model catalogs or unknown model ids.- """+ """Persist a backend model selection (``image_gen.model`` /+ ``video_gen.model``), validated against the resolved backend's catalog."""section = _MODEL_CATALOG_TOOLSETS.get(name)if section is None:raise HTTPException(@@ -433,30 +389,25 @@ async def select_toolset_model(raise HTTPException(status_code=400, detail="model is required")def _run():- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- config = load_config()- row = _find_toolset_provider_row(name, config, body.provider)- plugin = _resolve_toolset_model_plugin(name, row) if row else None- if not plugin:- raise HTTPException(- status_code=400,- detail=f"No model-capable backend is active for {name}",- )+ with config_write_scope(body.profile or profile):+ config = load_config()+ row = _find_toolset_provider_row(name, config, body.provider)+ plugin = _resolve_toolset_model_plugin(name, row) if row else None+ if not plugin:+ raise HTTPException(+ status_code=400,+ detail=f"No model-capable backend is active for {name}",+ )- catalog, _default = _toolset_model_catalog(name, plugin)- if model_id not in catalog:- raise HTTPException(- status_code=400,- detail=f"Unknown model {model_id!r} for backend {plugin!r}",- )+ catalog, _default = _toolset_model_catalog(name, plugin)+ if model_id not in catalog:+ raise HTTPException(+ status_code=400,+ detail=f"Unknown model {model_id!r} for backend {plugin!r}",+ )- section_cfg = config.setdefault(section, {})- if not isinstance(section_cfg, dict):- section_cfg = {}- config[section] = section_cfg- section_cfg["model"] = model_id- save_config(config)+ _dict_section(config, section)["model"] = model_id+ save_config(config)return pluginplugin = await asyncio.to_thread(_run)@@ -467,38 +418,24 @@ async def select_toolset_model(async def select_toolset_provider(name: str, body: ToolsetProviderSelect, profile: Optional[str] = None):- """Persist a provider selection for a toolset (no key prompting).-- Delegates to ``apply_provider_selection`` — the shared, non-interactive- core extracted from the CLI configurator — so the GUI and ``hermes tools``- write identical config keys (``web.backend``, ``tts.provider``, etc.).- API keys and post-setup flows are handled by separate endpoints. Returns- 400 for unknown toolset or provider names.-- For the ``web`` toolset only, an optional ``capability`` ('search' |- 'extract') scopes the selection to ``web.search_backend`` /- ``web.extract_backend`` — the same per-capability overrides the runtime- dispatchers (``tools.web_tools._get_search_backend`` /- ``_get_extract_backend``) resolve first. The provider must actually- support the requested capability (a search-only backend can't be the- extract backend). Omitting ``capability`` keeps the legacy whole-provider- behavior (writes ``web.backend``).-- Managed Nous rows (``managed_nous_feature``) additionally report the- Portal entitlement state: the CLI flow gates these selections on- ``ensure_nous_portal_access`` (inline login), but the GUI has no inline- prompt, so selecting one while logged out / unentitled used to write the- config keys and then never activate (``_is_provider_active`` requires- ``managed_by_nous``). The response now carries an additive- ``needs_nous_auth: true`` + ``feature`` so the client can drive the- existing Nous Portal OAuth flow (``POST /api/providers/oauth/nous/start``)- and refetch.+ """Persist a provider selection via ``apply_provider_selection`` (the+ non-interactive core shared with ``hermes tools``, so both write identical+ config keys). Keys and post-setup are separate endpoints.++ ``web`` only: ``capability`` ('search' | 'extract') scopes the write to+ ``web.<capability>_backend`` (the per-capability override the runtime+ dispatchers resolve first); the provider must support that capability.+ Omitted -> legacy whole-provider write of ``web.backend``.++ Managed Nous rows report Portal entitlement: the GUI has no inline login,+ so an unentitled selection would write config and never activate. The+ response adds ``needs_nous_auth: true`` + ``feature`` so the client can+ drive the Nous Portal OAuth flow and refetch."""from hermes_cli.tools_config import (TOOL_CATEGORIES,apply_provider_selection,web_provider_capabilities,- _get_effective_configurable_toolsets,_visible_providers,)from hermes_cli.nous_subscription import (@@ -506,9 +443,7 @@ async def select_toolset_provider(get_nous_subscription_features,)- valid = {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}- if name not in valid:- raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")+ _require_known_toolset(name)if body.capability is not None:if name != "web":@@ -522,18 +457,20 @@ async def select_toolset_provider(detail=f"Unknown capability: {body.capability!r} (expected 'search' or 'extract')",)+ def _provider_row(config):+ cat = TOOL_CATEGORIES.get(name)+ providers = _visible_providers(cat, config, force_fresh=True) if cat else []+ return next((p for p in providers if p.get("name") == body.provider), None)+def _run():with _profile_scope(body.profile or profile):with _CONFIG_MUTATION_LOCK:config = load_config()if body.capability is not None:- # Per-capability path: resolve the picker row to its backend key- # and write web.<capability>_backend. Does NOT touch web.backend,- # so the other capability keeps resolving through the shared- # fallback chain.- cat = TOOL_CATEGORIES.get(name)- providers = _visible_providers(cat, config, force_fresh=True) if cat else []- prov = next((p for p in providers if p.get("name") == body.provider), None)+ # Per-capability path writes web.<capability>_backend only —+ # web.backend is untouched so the other capability keeps+ # resolving through the shared fallback chain.+ prov = _provider_row(config)if prov is None:raise HTTPException(status_code=400,@@ -550,11 +487,7 @@ async def select_toolset_provider(status_code=400,detail=f"{body.provider} does not support {body.capability}",)- web_cfg = config.setdefault("web", {})- if not isinstance(web_cfg, dict):- web_cfg = {}- config["web"] = web_cfg- web_cfg[f"{body.capability}_backend"] = backend+ _dict_section(config, "web")[f"{body.capability}_backend"] = backendelse:try:apply_provider_selection(name, body.provider, config)@@ -565,23 +498,10 @@ async def select_toolset_provider(if body.capability is not None:response["capability"] = body.capability- # Entitlement check for managed Nous rows — mirrors the gate the CLI- # applies via ensure_nous_portal_access at selection time. This hits- # the network (Portal), so it runs AFTER releasing the mutation lock:- # holding a process-wide config-write lock across a network fetch- # would stall every other config writer behind a slow Portal call.- # Still inside the worker thread + profile scope.- cat = TOOL_CATEGORIES.get(name)- row = None- if cat:- row = next(- (- p- for p in _visible_providers(cat, config, force_fresh=True)- if p.get("name") == body.provider- ),- None,- )+ # Entitlement check for managed Nous rows (mirrors the CLI's+ # ensure_nous_portal_access gate). Hits the Portal, so it runs AFTER+ # releasing the mutation lock — still in the worker thread + scope.+ row = _provider_row(config)managed_feature = (row or {}).get("managed_nous_feature")if managed_feature:features = get_nous_subscription_features(config, force_fresh=True)@@ -601,32 +521,18 @@ async def select_toolset_provider(response["feature"] = managed_featurereturn response- response = await asyncio.to_thread(_run)- return response+ return await asyncio.to_thread(_run)@router.put("/api/tools/toolsets/{name}/env")async def save_toolset_env(name: str, body: ToolsetEnvUpdate, profile: Optional[str] = None):- """Persist API keys for a toolset's provider env vars.-- Writes each ``key: value`` to ``~/.hermes/.env`` via ``save_env_value`` —- the same store ``hermes tools`` writes when it prompts for keys. Keys are- validated against the env-var allowlist for the toolset's category (the- union of every visible provider's ``env_vars``), so the GUI can't write an- arbitrary env var through this endpoint. A blank value is treated as- "leave unchanged" and skipped. Returns the saved/skipped key lists and the- refreshed ``is_set`` status. Returns 400 for unknown toolset or env keys.- """- from hermes_cli.tools_config import (- TOOL_CATEGORIES,- _get_effective_configurable_toolsets,- _visible_providers,- )+ """Persist API keys to ``.env`` via ``save_env_value``. Keys are validated+ against the union of the category's visible-provider ``env_vars`` so this+ can't write arbitrary env vars; a blank value means "leave unchanged"."""+ from hermes_cli.tools_config import TOOL_CATEGORIES, _visible_providersfrom hermes_cli.config import get_env_value, save_env_value- valid_ts = {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}- if name not in valid_ts:- raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")+ _require_known_toolset(name)def _run():with _profile_scope(body.profile or profile):@@ -668,62 +574,35 @@ async def save_toolset_env(name: str, body: ToolsetEnvUpdate, profile: Optional[async def run_toolset_post_setup(name: str, body: ToolsetPostSetup, profile: Optional[str] = None):- """Spawn a provider's post-setup install hook as a background action.-- Post-setup hooks (npm install for browser/Camofox, pip install for- KittenTTS/Piper/ddgs, cua-driver fetch, etc.) are long-running and- text-output, so this follows the spawn-action pattern: it launches- ``hermes tools post-setup <key>`` and the frontend tails the log via- ``GET /api/actions/tools-post-setup/status``. The ``key`` is validated- against the declared post-setup allowlist before spawning. Returns 400- for unknown toolset or post-setup key.-- ``profile`` spawns the hook as ``hermes -p <profile> tools post-setup``.- Most hooks install machine-level artifacts (repo node_modules, shared- pip packages) where the scope is inert, but hooks that read config or- write per-profile state must see the same HERMES_HOME the rest of the- drawer's writes targeted — so the scope is threaded for consistency.- """- from hermes_cli.tools_config import (- _get_effective_configurable_toolsets,- valid_post_setup_keys,- )-- valid_ts = {ts_key for ts_key, _, _ in _get_effective_configurable_toolsets()}- if name not in valid_ts:- raise HTTPException(status_code=400, detail=f"Unknown toolset: {name}")-+ """Spawn ``hermes tools post-setup <key>`` (long-running installs: npm,+ pip, cua-driver fetch) as a background action the frontend tails via+ ``GET /api/actions/tools-post-setup/status``. The key is validated+ against the declared allowlist; ``profile`` is threaded so hooks that+ touch config see the same HERMES_HOME as the drawer's other writes."""+ from hermes_cli.tools_config import valid_post_setup_keys++ _require_known_toolset(name)if body.key not in valid_post_setup_keys():raise HTTPException(status_code=400, detail=f"Unknown post-setup key: {body.key}")- try:- proc = _spawn_hermes_action(- _profile_cli_args(body.profile or profile)- + ["tools", "post-setup", body.key],- "tools-post-setup",- )- except HTTPException:- raise- except Exception as exc:- _log.exception("Failed to spawn tools post-setup")- raise HTTPException(- status_code=500, detail=f"Failed to run post-setup: {exc}"- )- return {"ok": True, "pid": proc.pid, "name": "tools-post-setup", "key": body.key}+ result = spawn_profile_action(+ body.profile or profile,+ ["tools", "post-setup", body.key],+ "tools-post-setup",+ log_msg="Failed to spawn tools post-setup",+ prefix="Failed to run post-setup",+ )+ result["key"] = body.key+ return result@router.get("/api/tools/terminal/backends")async def get_terminal_backends(profile: Optional[str] = None):- """Terminal execution backend rows with health probes for the picker panel.-- Returns ``{active, backends: [{name, label, description, active, status,- detail}]}`` where ``status`` is ``ready`` / ``needs_setup`` /- ``unavailable`` and ``detail`` carries setup guidance for non-ready rows.- Probes are fast (<~2s each) and defensive — a probe failure surfaces as a- status, never an error response.- """+ """Terminal backend rows with health probes: ``status`` is ``ready`` /+ ``needs_setup`` / ``unavailable``; a probe failure is a status, never an+ error response."""def _read():with _profile_scope(profile):config = load_config()@@ -755,12 +634,8 @@ async def get_terminal_backends(profile: Optional[str] = None):async def select_terminal_backend(body: TerminalBackendSelect, profile: Optional[str] = None):- """Persist ``terminal.backend`` in config.yaml.-- Validates against the known backend set (the same enum the raw-config- settings row exposes). Selecting a backend that still needs setup is- allowed — the picker shows guidance instead of blocking, matching the CLI.- """+ """Persist ``terminal.backend``. A backend that still needs setup is+ allowed — the picker shows guidance instead of blocking, like the CLI."""backend = (body.backend or "").strip().lower()valid_names = _terminal_backend_names()if backend not in valid_names:@@ -771,15 +646,10 @@ async def select_terminal_backend()def _run():- with _profile_scope(body.profile or profile):- with _CONFIG_MUTATION_LOCK:- config = load_config()- terminal_cfg = config.setdefault("terminal", {})- if not isinstance(terminal_cfg, dict):- terminal_cfg = {}- config["terminal"] = terminal_cfg- terminal_cfg["backend"] = backend- save_config(config)+ with config_write_scope(body.profile or profile):+ config = load_config()+ _dict_section(config, "terminal")["backend"] = backend+ save_config(config)await asyncio.to_thread(_run)return {"ok": True, "backend": backend}@@ -787,47 +657,27 @@ async def select_terminal_backend(@router.get("/api/tools/computer-use/status")async def get_computer_use_status(profile: Optional[str] = None):- """Cross-platform Computer Use readiness for the desktop card.-- See ``tools.computer_use.permissions.computer_use_status`` for the payload- shape. Read-only and fast (shells ``cua-driver doctor`` + macOS- ``permissions status``).- """+ """Computer Use readiness for the desktop card (payload shape: see+ ``tools.computer_use.permissions.computer_use_status``)."""from tools.computer_use.permissions import computer_use_status- def _read():- with _profile_scope(profile):- return computer_use_status()-- return await asyncio.to_thread(_read)+ return await scoped_to_thread(profile, computer_use_status)@router.post("/api/tools/computer-use/permissions/grant")async def grant_computer_use_permissions(profile: Optional[str] = None):- """Spawn ``hermes computer-use permissions grant`` as a background action.-- macOS-only: ``cua-driver permissions grant`` launches CuaDriver via- LaunchServices so the TCC dialog is attributed to com.trycua.driver, then- waits for approval. The frontend polls ``GET /api/actions/computer-use-- grant/status`` and re-reads ``/status`` once it exits. Windows/Linux have- no TCC toggles to grant, so this returns 400 there.- """+ """Spawn ``hermes computer-use permissions grant`` (macOS-only: launches+ CuaDriver via LaunchServices so the TCC dialog is attributed correctly).+ The frontend polls ``GET /api/actions/computer-use-grant/status``."""if sys.platform != "darwin":raise HTTPException(status_code=400,detail="Computer Use permission grants are a macOS concept.",)- try:- proc = _spawn_hermes_action(- _profile_cli_args(profile)- + ["computer-use", "permissions", "grant"],- "computer-use-grant",- )- except HTTPException:- raise- except Exception as exc:- _log.exception("Failed to spawn computer-use permissions grant")- raise HTTPException(- status_code=500, detail=f"Failed to request permissions: {exc}"- )- return {"ok": True, "pid": proc.pid, "name": "computer-use-grant"}+ return spawn_profile_action(+ profile,+ ["computer-use", "permissions", "grant"],+ "computer-use-grant",+ log_msg="Failed to spawn computer-use permissions grant",+ prefix="Failed to request permissions",+ )diff --git a/plugins/kanban/dashboard/plugin_api.py b/plugins/kanban/dashboard/plugin_api.pyindex 9a76f1d8523..b8a5c016f44 100644--- a/plugins/kanban/dashboard/plugin_api.py+++ b/plugins/kanban/dashboard/plugin_api.py@@ -1,36 +1,17 @@"""Kanban dashboard plugin — backend API routes.-Mounted at /api/plugins/kanban/ by the dashboard plugin system.--This layer is intentionally thin: every handler is a small wrapper around-``hermes_cli.kanban_db`` or a direct SQL query. Writes use the same code-paths the CLI and gateway ``/kanban`` command use, so the three surfaces-cannot drift.--Live updates arrive via the ``/events`` WebSocket, which tails the-append-only ``task_events`` table on a short poll interval (WAL mode lets-reads run alongside the dispatcher's IMMEDIATE write transactions).--Security note---------------Plugin HTTP routes go through the dashboard's session-token auth middleware-(``web_server.auth_middleware``) just like core API routes — every-``/api/plugins/...`` request must present the session bearer token (or the-session cookie set when you load the dashboard HTML). The token is the-random per-process ``_SESSION_TOKEN`` printed at startup; the dashboard's-own pages inject it via ``window.__HERMES_SESSION_TOKEN__`` so logged-in-browsers don't have to handle it manually.--For the ``/events`` WebSocket we still require the session token as a-``?token=`` query parameter (browsers cannot set the ``Authorization``-header on an upgrade request), matching the established pattern used by-the in-browser PTY bridge in ``hermes_cli/web_server.py``.--This means ``hermes dashboard --host 0.0.0.0`` is safe to run on a LAN:-plugin routes are no longer an unauthenticated exception. The auth still-isn't multi-user — anyone who can read the printed URL+token gets full-dashboard access — but they can't ride along just because they can reach-the port.+Mounted at /api/plugins/kanban/ by the dashboard plugin system. Every handler+is a thin wrapper around ``hermes_cli.kanban_db`` (the same code paths the CLI+and gateway ``/kanban`` command use, so the three surfaces cannot drift).++Live updates arrive via the ``/events`` WebSocket, which tails the append-only+``task_events`` table on a short poll (WAL mode lets reads run alongside the+dispatcher's IMMEDIATE write transactions).++Security: plugin HTTP routes sit behind the dashboard's session-token auth+middleware like core API routes. The ``/events`` WebSocket carries its+credential in the query string (browsers cannot set ``Authorization`` on an+upgrade request) and is gated by the dashboard's canonical WS auth check."""from __future__ import annotations@@ -38,12 +19,14 @@ from __future__ import annotationsimport asyncioimport jsonimport logging+import reimport sqlite3import timefrom concurrent.futures import ThreadPoolExecutor+from contextlib import contextmanagerfrom dataclasses import asdictfrom pathlib import Path-from typing import Any, Optional+from typing import Any, Iterator, Optionalfrom fastapi import APIRouter, File, Form, HTTPException, Query, UploadFile, WebSocket, WebSocketDisconnect, status as http_statusfrom fastapi.responses import FileResponse@@ -51,57 +34,40 @@ from pydantic import BaseModel, Fieldfrom hermes_cli import kanban_dbfrom hermes_cli import kanban_diagnostics as kd+from hermes_cli.kanban_db import (+ KANBAN_ATTACHMENT_MAX_BYTES,+ _collision_free_path,+ _safe_attachment_name,+)log = logging.getLogger(__name__)router = APIRouter()-# ----------------------------------------------------------------------------# Auth helper — WebSocket only (HTTP routes live behind the dashboard's-# existing plugin-bypass; this is documented above).-# ---------------------------------------------------------------------------+# --- Connection / board helpers ---------------------------------------------def _ws_upgrade_authorized(ws: "WebSocket") -> bool:- """Authorize a WebSocket upgrade by delegating to the dashboard's canonical- WS auth gate (``hermes_cli.web_server._ws_auth_ok``).-- Delegating (rather than re-implementing a ``_SESSION_TOKEN``-only check)- means this endpoint transparently accepts whatever the core gate accepts- in each mode:-- * loopback / ``--insecure``: legacy ``?token=<_SESSION_TOKEN>``- * gated OAuth: single-use ``?ticket=`` (the browser SDK's- ``buildWsUrl`` mints one per connect)- * server-internal: the process-lifetime ``?internal=`` credential-- The previous bespoke check only understood ``_SESSION_TOKEN``, so the- kanban live-events WS was rejected on every OAuth-gated deployment even- though the rest of the dashboard worked. Routing through the shared gate- also means this can never drift from core auth again.-- Imported lazily so the plugin still loads in test contexts where the- dashboard ``web_server`` module isn't importable (e.g. the bare-FastAPI- test harness); there we accept so the tail loop stays testable, matching- the prior behaviour.+ """Authorize a WebSocket upgrade via the dashboard's canonical WS gate.++ Delegating to ``web_server._ws_auth_ok`` means this endpoint accepts whatever+ the core gate accepts in each mode (loopback ``?token=``, OAuth single-use+ ``?ticket=``, server-internal ``?internal=``) and can never drift from core+ auth. When ``web_server`` isn't importable (bare-FastAPI test harness) we+ accept so the tail loop stays testable."""try:from hermes_cli import web_server as _wsexcept Exception:- # No dashboard context (tests). Accept so the tail loop is still- # testable; in production the dashboard module always imports- # cleanly because it's the caller.return Truereturn bool(_ws._ws_auth_ok(ws))def _resolve_board(board: Optional[str]) -> Optional[str]:- """Validate and normalise a board slug from a query param.+ """Validate/normalise a board slug query param (400 malformed, 404 unknown).- Raises :class:`HTTPException` 400 on malformed slugs so the browser- sees a clean error instead of a 500. Returns the normalised slug,- or ``None`` when the caller omitted the param (which then falls- through to the active board inside ``kb.connect()``).+ Returns ``None`` when the param was omitted so ``kb.connect()`` falls through+ to the active board."""if board is None or board == "":return None@@ -110,24 +76,27 @@ def _resolve_board(board: Optional[str]) -> Optional[str]:except ValueError as exc:raise HTTPException(status_code=400, detail=str(exc))if normed and normed != kanban_db.DEFAULT_BOARD and not kanban_db.board_exists(normed):- raise HTTPException(- status_code=404,- detail=f"board {normed!r} does not exist",- )+ raise HTTPException(status_code=404, detail=f"board {normed!r} does not exist")+ return normed+++def _existing_board_slug(slug: str) -> str:+ """Normalise a path slug and require the board to exist (400 / 404)."""+ try:+ normed = kanban_db._normalize_board_slug(slug)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc))+ if not normed or not kanban_db.board_exists(normed):+ raise HTTPException(status_code=404, detail=f"board {slug!r} does not exist")return normeddef _conn(board: Optional[str] = None):"""Open a kanban_db connection, creating the schema on first use.- Every handler that mutates the DB goes through this so the plugin- self-heals on a fresh install (no user-visible "no such table"- error if somebody hits POST /tasks before GET /board).- ``init_db`` is idempotent.-- ``board`` is the query-param slug (already normalised by- :func:`_resolve_board`). When ``None`` the active board is used- via the resolution chain (env var → ``current`` file → ``default``).+ ``init_db`` is idempotent; running it here means a fresh install self-heals+ (no "no such table" if POST /tasks arrives before GET /board). ``board`` is+ the already-normalised slug; ``None`` resolves to the active board."""try:kanban_db.init_db(board=board)@@ -136,135 +105,94 @@ def _conn(board: Optional[str] = None):return kanban_db.connect(board=board)-# ----------------------------------------------------------------------------# Serialization helpers-# ---------------------------------------------------------------------------+@contextmanager+def _board_conn(board: Optional[str]) -> Iterator[tuple[Optional[str], sqlite3.Connection]]:+ """Resolve the ``board`` query param, open a connection, close it on exit."""+ board = _resolve_board(board)+ conn = _conn(board=board)+ try:+ yield board, conn+ finally:+ conn.close()+++def _require_task(conn: sqlite3.Connection, task_id: str) -> kanban_db.Task:+ task = kanban_db.get_task(conn, task_id)+ if task is None:+ raise HTTPException(status_code=404, detail=f"task {task_id} not found")+ return task+++@contextmanager+def _value_error_400() -> Iterator[None]:+ """Map domain-layer ``ValueError`` (validation refusals) to a 400 response."""+ try:+ yield+ except ValueError as e:+ raise HTTPException(status_code=400, detail=str(e))-# Columns shown by the dashboard, in left-to-right order. "archived" is-# available via a filter toggle rather than a visible column.-#-# Keep this in sync with kanban_db.VALID_STATUSES. In particular,-# ``scheduled`` is a first-class waiting column used for time-based follow-ups;-# if it is omitted here, the board-level fallback below mis-buckets scheduled-# tasks into ``todo`` and makes the dashboard look like the Scheduled column-# disappeared.++# --- Serialization helpers --------------------------------------------------++# Dashboard columns, left-to-right; "archived" is a filter toggle, not a column.+# Keep in sync with kanban_db.VALID_STATUSES — a status missing here (e.g.+# ``scheduled``) gets mis-bucketed into ``todo`` by the board fallback.BOARD_COLUMNS: list[str] = ["triage", "todo", "scheduled", "ready", "running", "blocked", "review", "done",]-_CARD_SUMMARY_PREVIEW_CHARS = 200-def _task_dict(- task: kanban_db.Task,- *,- latest_summary: Optional[str] = None,-) -> dict[str, Any]:+def _task_dict(task: kanban_db.Task, *, latest_summary: Optional[str] = None) -> dict[str, Any]:d = asdict(task)- # Add derived age metrics so the UI can colour stale cards without- # computing deltas client-side.+ # Derived age metrics so the UI can colour stale cards without client deltas.try:d["age"] = kanban_db.task_age(task)except Exception:d["age"] = {"created_age_seconds": None, "started_age_seconds": None, "time_to_complete_seconds": None}- # Surface the latest non-null run summary so dashboards don't show- # blank cards/drawers for tasks where the worker handed off via- # ``task_runs.summary`` (the kanban-worker pattern) instead of- # ``tasks.result``. ``None`` when no run has produced a summary yet.+ # Latest non-null run summary (workers hand off via ``task_runs.summary``,+ # not ``tasks.result``); None until a run has produced one.d["latest_summary"] = latest_summary- # Keep body short on list endpoints; full body comes from /tasks/:id.return ddef _event_dict(event: kanban_db.Event) -> dict[str, Any]:- return {- "id": event.id,- "task_id": event.task_id,- "kind": event.kind,- "payload": event.payload,- "created_at": event.created_at,- "run_id": event.run_id,- }+ return asdict(event)def _comment_dict(c: kanban_db.Comment) -> dict[str, Any]:- return {- "id": c.id,- "task_id": c.task_id,- "author": c.author,- "body": c.body,- "created_at": c.created_at,- }+ return asdict(c)def _attachment_dict(a: kanban_db.Attachment) -> dict[str, Any]:- """Serialise an Attachment for the drawer. ``stored_path`` is the- absolute on-disk path workers read; the UI uses ``id`` for download."""+ """``stored_path`` is the absolute on-disk path workers read; UI downloads by ``id``."""return {- "id": a.id,- "task_id": a.task_id,- "filename": a.filename,- "content_type": a.content_type,- "size": a.size,- "uploaded_by": a.uploaded_by,- "stored_path": a.stored_path,- "created_at": a.created_at,+ "id": a.id, "task_id": a.task_id, "filename": a.filename,+ "content_type": a.content_type, "size": a.size, "uploaded_by": a.uploaded_by,+ "stored_path": a.stored_path, "created_at": a.created_at,}def _run_dict(r: kanban_db.Run) -> dict[str, Any]:- """Serialise a Run for the drawer's Run history section."""- return {- "id": r.id,- "task_id": r.task_id,- "profile": r.profile,- "step_key": r.step_key,- "status": r.status,- "claim_lock": r.claim_lock,- "claim_expires": r.claim_expires,- "worker_pid": r.worker_pid,- "max_runtime_seconds": r.max_runtime_seconds,- "last_heartbeat_at": r.last_heartbeat_at,- "started_at": r.started_at,- "ended_at": r.ended_at,- "outcome": r.outcome,- "summary": r.summary,- "metadata": r.metadata,- "error": r.error,- }---# Hallucination-warning event kinds — see complete_task() in kanban_db.py.-# completion_blocked_hallucination: kernel rejected created_cards with-# phantom ids; task stays in prior state.-# suspected_hallucinated_references: prose scan found t_<hex> in summary-# that doesn't resolve; completion succeeded, advisory only.-_WARNING_EVENT_KINDS = (- "completion_blocked_hallucination",- "suspected_hallucinated_references",-)+ return asdict(r)def _compute_task_diagnostics(conn: sqlite3.Connection,task_ids: Optional[list[str]] = None,) -> dict[str, list[dict]]:- """Run the diagnostic rule engine against every task (or a subset)- and return ``{task_id: [diagnostic_dict, ...]}``.+ """Run the diagnostic rule engine (``kanban_diagnostics``) and return+ ``{task_id: [diagnostic_dict, ...]}``; tasks with no diagnostics are omitted.- Tasks with no active diagnostics are omitted from the result.- Uses ``hermes_cli.kanban_diagnostics`` — see that module for the- rule definitions.+ Three aggregate queries (tasks, events, runs) instead of N per-task lookups.+ Slurps every event/run for the board — fine for the dashboard's typical+ working set (hundreds of tasks); paginate if profiling shows a hotspot."""- from hermes_cli import kanban_diagnostics as kdfrom hermes_cli.config import load_configdiag_config = kd.config_from_runtime_config(load_config())- # Build the candidate task list. We need each task's row + its- # events + its runs. Doing N separate queries works but scales- # poorly; do three aggregate queries instead.if task_ids is not None:if not task_ids:return {}@@ -274,17 +202,11 @@ def _compute_task_diagnostics(tuple(task_ids),).fetchall()else:- rows = conn.execute(- "SELECT * FROM tasks WHERE status != 'archived'",- ).fetchall()+ rows = conn.execute("SELECT * FROM tasks WHERE status != 'archived'").fetchall()if not rows:return {}- # Index events + runs by task id. For very large boards this will- # slurp a lot — acceptable on the dashboard's typical working set- # (hundreds of tasks), but we can add pagination / filtering later- # if profiling shows it's a hotspot.row_ids = [r["id"] for r in rows]placeholders = ",".join(["?"] * len(row_ids))events_by_task: dict[str, list] = {tid: [] for tid in row_ids}@@ -316,20 +238,11 @@ def _compute_task_diagnostics(return out-def _warnings_summary_from_diagnostics(- diagnostics: list[dict],-) -> Optional[dict]:- """Compact summary for cards: {count, highest_severity, kinds,- latest_at}. Replaces the old hallucination-only ``warnings`` object- — same shape additions plus ``highest_severity`` so the UI can color- badges per diagnostic severity.-- Returns None when ``diagnostics`` is empty.- """+def _warnings_summary_from_diagnostics(diagnostics: list[dict]) -> Optional[dict]:+ """Compact card badge summary ``{count, kinds, latest_at, highest_severity}``;+ None when ``diagnostics`` is empty."""if not diagnostics:return None- from hermes_cli.kanban_diagnostics import SEVERITY_ORDER-kinds: dict[str, int] = {}latest = 0highest_idx = -1@@ -342,17 +255,20 @@ def _warnings_summary_from_diagnostics(if la > latest:latest = lasev = d.get("severity")- if sev in SEVERITY_ORDER:- idx = SEVERITY_ORDER.index(sev)+ if sev in kd.SEVERITY_ORDER:+ idx = kd.SEVERITY_ORDER.index(sev)if idx > highest_idx:highest_idx = idxhighest_sev = sev- return {- "count": count,- "kinds": kinds,- "latest_at": latest,- "highest_severity": highest_sev,- }+ return {"count": count, "kinds": kinds, "latest_at": latest, "highest_severity": highest_sev}+++def _attach_diagnostics(task_d: dict, diags: Optional[list[dict]]) -> None:+ """Full list goes in the payload (drawer renders without a second round-trip);+ the card badge only needs the summary."""+ if diags:+ task_d["diagnostics"] = diags+ task_d["warnings"] = _warnings_summary_from_diagnostics(diags)def _links_for(conn: sqlite3.Connection, task_id: str) -> dict[str, list[str]]:@@ -374,9 +290,7 @@ def _links_for(conn: sqlite3.Connection, task_id: str) -> dict[str, list[str]]:return {"parents": parents, "children": children}-# ----------------------------------------------------------------------------# GET /board-# ---------------------------------------------------------------------------+# --- GET /board -------------------------------------------------------------@router.get("/board")def get_board(@@ -386,22 +300,14 @@ def get_board(workflow_template_id: Optional[str] = Query(None, description="Restrict to tasks using this workflow template id",),- current_step_key: Optional[str] = Query(- None, description="Restrict to tasks at this workflow step key",- ),+ current_step_key: Optional[str] = Query(None, description="Restrict to tasks at this workflow step key"),):"""Return the full board grouped by status column.- ``_conn()`` auto-initializes ``kanban.db`` on first call so a fresh- install doesn't surface a "failed to load" error on the plugin tab.-- ``board`` selects which board to read from. Omitting it falls- through to the active board (``HERMES_KANBAN_BOARD`` env → on-disk- ``current`` pointer → ``default``).+ Omitting ``board`` falls through to the active board (``HERMES_KANBAN_BOARD``+ env → on-disk ``current`` pointer → ``default``)."""- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):tasks = kanban_db.list_tasks(conn,tenant=tenant,@@ -409,19 +315,13 @@ def get_board(workflow_template_id=workflow_template_id,current_step_key=current_step_key,)- # Pre-fetch link counts per task (cheap: one query).+ # Link / comment / progress rollups are each one aggregate query rather+ # than N per-task lookups.link_counts: dict[str, dict[str, int]] = {}- for row in conn.execute(- "SELECT parent_id, child_id FROM task_links"- ).fetchall():- link_counts.setdefault(row["parent_id"], {"parents": 0, "children": 0})[- "children"- ] += 1- link_counts.setdefault(row["child_id"], {"parents": 0, "children": 0})[- "parents"- ] += 1-- # Comment + event counts (both cheap aggregates).+ for row in conn.execute("SELECT parent_id, child_id FROM task_links").fetchall():+ link_counts.setdefault(row["parent_id"], {"parents": 0, "children": 0})["children"] += 1+ link_counts.setdefault(row["child_id"], {"parents": 0, "children": 0})["parents"] += 1+comment_counts: dict[str, int] = {r["task_id"]: r["n"]for r in conn.execute(@@ -429,9 +329,7 @@ def get_board()}- # Progress rollup: for each parent, how many children are done / total.- # One pass over task_links joined with child status — cheaper than- # N per-task queries and the plugin uses it to render "N/M".+ # Per parent: children done / total, rendered as "N/M".progress: dict[str, dict[str, int]] = {}for row in conn.execute("SELECT l.parent_id AS pid, t.status AS cstatus "@@ -442,10 +340,6 @@ def get_board(if row["cstatus"] == "done":p["done"] += 1- # Diagnostics rollup for this board — see kanban_diagnostics.- # We get the full structured list per task AND a compact- # summary for the card badge (so cards don't carry the detail- # text; the drawer fetches that via /tasks/:id or /diagnostics).diagnostics_per_task = _compute_task_diagnostics(conn, task_ids=None)latest_event_id = conn.execute(@@ -456,42 +350,28 @@ def get_board(if include_archived:columns["archived"] = []- # Batch-fetch the latest non-null run summary per task in one- # window-function query (avoids N+1 ``latest_summary`` calls- # for boards with hundreds of tasks). Truncated to a card-size- # preview here — the full text is available via /tasks/:id.+ # One window-function query for latest summaries (avoids N+1); cards get+ # a truncated preview, the full text comes from /tasks/:id.summary_map = kanban_db.latest_summaries(conn, [t.id for t in tasks])for t in tasks:full = summary_map.get(t.id)- preview = (- full[:_CARD_SUMMARY_PREVIEW_CHARS] if full else None- )+ preview = (full[:_CARD_SUMMARY_PREVIEW_CHARS] if full else None)d = _task_dict(t, latest_summary=preview)d["link_counts"] = link_counts.get(t.id, {"parents": 0, "children": 0})d["comment_count"] = comment_counts.get(t.id, 0)d["progress"] = progress.get(t.id) # None when the task has no children- diags = diagnostics_per_task.get(t.id)- if diags:- # Full list goes into the payload so the drawer can render- # without a second round-trip. The board-level badge only- # needs the summary.- d["diagnostics"] = diags- d["warnings"] = _warnings_summary_from_diagnostics(diags)+ _attach_diagnostics(d, diagnostics_per_task.get(t.id))col = t.status if t.status in columns else "todo"columns[col].append(d)- # Stable per-column ordering already applied by list_tasks- # (priority DESC, created_at ASC), keep as-is.-- # List of known tenants for the UI filter dropdown.+ # Per-column ordering (priority DESC, created_at ASC) comes from list_tasks.tenants = [r["tenant"]for r in conn.execute("SELECT DISTINCT tenant FROM tasks WHERE tenant IS NOT NULL ORDER BY tenant")]- # List of distinct assignees for the lane-by-profile sub-grouping.assignees = [r["assignee"]for r in conn.execute(@@ -501,21 +381,15 @@ def get_board(]return {- "columns": [- {"name": name, "tasks": columns[name]} for name in columns.keys()- ],+ "columns": [{"name": name, "tasks": columns[name]} for name in columns],"tenants": tenants,"assignees": assignees,"latest_event_id": int(latest_event_id),"now": int(time.time()),}- finally:- conn.close()-# ----------------------------------------------------------------------------# GET /tasks/:id-# ---------------------------------------------------------------------------+# --- GET /tasks/:id ---------------------------------------------------------@router.get("/tasks/{task_id}")def get_task(@@ -528,25 +402,16 @@ def get_task(None, description="With run_state_type: exact value for that run column",),):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):if (run_state_type is None) ^ (run_state_name is None):raise HTTPException(status_code=400,detail="run_state_type and run_state_name must be passed together or omitted",)if run_state_type is not None and run_state_type not in ("status", "outcome"):- raise HTTPException(- status_code=400,- detail="run_state_type must be 'status' or 'outcome'",- )- task = kanban_db.get_task(conn, task_id)- if task is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")- # Drawer/detail view returns the FULL summary (no truncation) so- # operators can read the complete worker handoff without making- # a second round-trip. Cards on /board carry a 200-char preview.+ raise HTTPException(status_code=400, detail="run_state_type must be 'status' or 'outcome'")+ task = _require_task(conn, task_id)+ # Drawer returns the FULL summary (cards on /board carry a 200-char preview).full_summary = kanban_db.latest_summary(conn, task_id)task_d = _task_dict(task, latest_summary=full_summary)links = _links_for(conn, task_id)@@ -564,13 +429,8 @@ def get_task("latest_summary": child_summaries.get(child.id),"result": child.result,})- # Attach diagnostics so the drawer's Diagnostics section can- # render recovery actions without a second round-trip.diags = _compute_task_diagnostics(conn, task_ids=[task_id])- diag_list = diags.get(task_id) or []- if diag_list:- task_d["diagnostics"] = diag_list- task_d["warnings"] = _warnings_summary_from_diagnostics(diag_list)+ _attach_diagnostics(task_d, diags.get(task_id) or [])return {"task": task_d,"comments": [_comment_dict(c) for c in kanban_db.list_comments(conn, task_id)],@@ -588,13 +448,9 @@ def get_task()],}- finally:- conn.close()-# ----------------------------------------------------------------------------# POST /tasks-# ---------------------------------------------------------------------------+# --- POST /tasks ------------------------------------------------------------class CreateTaskBody(BaseModel):title: str@@ -613,103 +469,51 @@ class CreateTaskBody(BaseModel):goal_max_turns: Optional[int] = Nonemodel_override: Optional[str] = Noneprovider_override: Optional[str] = None- # Per-task thinking depth (none|minimal|…|ultra). None = inherit the- # assigned profile's own agent.reasoning_effort.+ # Thinking depth (none|minimal|…|ultra); None inherits the profile's own level.reasoning_effort: Optional[str] = None- # Explicit project link; when omitted, create_task inherits the board's- # scoped project (if any) so a project-scoped board anchors every task.+ # When omitted, create_task inherits the board's scoped project (if any).project_id: Optional[str] = None@router.post("/tasks")def create_task(payload: CreateTaskBody, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn), _value_error_400():+ # CreateTaskBody field names match create_task's keyword parameters.task_id = kanban_db.create_task(- conn,- title=payload.title,- body=payload.body,- assignee=payload.assignee,- created_by="dashboard",- workspace_kind=payload.workspace_kind,- workspace_path=payload.workspace_path,- tenant=payload.tenant,- priority=payload.priority,- parents=payload.parents,- triage=payload.triage,- idempotency_key=payload.idempotency_key,- max_runtime_seconds=payload.max_runtime_seconds,- skills=payload.skills,- goal_mode=payload.goal_mode,- goal_max_turns=payload.goal_max_turns,- model_override=payload.model_override,- provider_override=payload.provider_override,- reasoning_effort=payload.reasoning_effort,- project_id=payload.project_id,- board=board,+ conn, created_by="dashboard", board=board, **payload.model_dump(),)task = kanban_db.get_task(conn, task_id)body: dict[str, Any] = {"task": _task_dict(task) if task else None}- # Surface a dispatcher-presence warning so the UI can show a- # banner when a `ready` task would otherwise sit idle because no- # gateway is running (or dispatch_in_gateway=false). Only emit- # for ready+assigned tasks; triage/todo are expected to wait,- # and unassigned tasks can't be dispatched regardless.+ # Dispatcher-presence warning so the UI can banner a ready+assigned task+ # that would otherwise sit idle (no gateway / dispatch_in_gateway=false).+ # triage/todo are expected to wait; unassigned tasks can't dispatch anyway.if task and task.status == "ready" and task.assignee:try:from hermes_cli.kanban import _check_dispatcher_presencefrom hermes_constants import get_hermes_home- # Scope the probe to the request's active home. The dashboard- # backend can run under a different HERMES_HOME than the- # profile this board belongs to, which otherwise warned "no- # gateway is running" against a live profile gateway (#71211).- running, message = _check_dispatcher_presence(- hermes_home=get_hermes_home()- )+ # Probe the request's active home: the dashboard backend may run+ # under a different HERMES_HOME than the board's profile.+ running, message = _check_dispatcher_presence(hermes_home=get_hermes_home())if not running and message:body["warning"] = messageexcept Exception:- # Probe failure must never block the create itself.- pass+ pass # probe failure must never block the create itselfreturn body- except ValueError as e:- raise HTTPException(status_code=400, detail=str(e))- finally:- conn.close()-# ----------------------------------------------------------------------------# Attachments — upload / list / download / delete (#35338)-# -----------------------------------------------------------------------------# The size cap, filename sanitiser, and collision resolver now live in-# ``kanban_db`` so the dashboard, the agent toolset, and the CLI share one-# implementation and cannot drift. ``_safe_attachment_name`` raises a plain-# ``ValueError`` there; the upload handler's ``except ValueError`` below maps-# it to a 400, preserving the previous response.-from hermes_cli.kanban_db import ( # noqa: E402- KANBAN_ATTACHMENT_MAX_BYTES,- _collision_free_path,- _safe_attachment_name,-)-+# --- Attachments — upload / list / download / delete ------------------------+# Size cap, filename sanitiser, and collision resolver live in ``kanban_db`` so+# the dashboard, agent toolset, and CLI share one implementation.+# ``_safe_attachment_name`` raises ``ValueError`` → mapped to 400 below.@router.get("/tasks/{task_id}/attachments")def list_task_attachments(task_id: str, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:- if kanban_db.get_task(conn, task_id) is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")+ with _board_conn(board) as (board, conn):+ _require_task(conn, task_id)return {- "attachments": [- _attachment_dict(a) for a in kanban_db.list_attachments(conn, task_id)- ]+ "attachments": [_attachment_dict(a) for a in kanban_db.list_attachments(conn, task_id)]}- finally:- conn.close()@router.post("/tasks/{task_id}/attachments")@@ -719,29 +523,18 @@ async def upload_task_attachment(board: Optional[str] = Query(None),uploaded_by: Optional[str] = Form(None),):- """Store an uploaded file for a task and record its metadata.-- The blob lands under ``attachments_root(board)/<task_id>/`` with a- sanitised, collision-resolved name. The worker reads it via the- absolute path surfaced in ``build_worker_context``.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:- if kanban_db.get_task(conn, task_id) is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")-+ """Store an upload under ``attachments_root(board)/<task_id>/`` with a+ sanitised, collision-resolved name and record its metadata."""+ with _board_conn(board) as (board, conn), _value_error_400():+ _require_task(conn, task_id)safe_name = _safe_attachment_name(file.filename or "")- # Stream to disk with a hard size cap so a huge upload can't fill- # the disk. Read in chunks; abort + clean up if the cap is hit.dest_dir = kanban_db.task_attachments_dir(task_id, board=board)dest_dir.mkdir(parents=True, exist_ok=True)-- # Resolve name collisions: foo.pdf → foo (1).pdf, foo (2).pdf, …- dest_path = _collision_free_path(dest_dir, safe_name)+ dest_path = _collision_free_path(dest_dir, safe_name) # foo.pdf → foo (1).pdf …candidate = dest_path.name+ # Stream in chunks with a hard size cap so one upload can't fill the disk.total = 0try:with open(dest_path, "wb") as out:@@ -776,22 +569,16 @@ async def upload_task_attachment()att = kanban_db.get_attachment(conn, att_id)return {"attachment": _attachment_dict(att) if att else None}- except ValueError as e:- raise HTTPException(status_code=400, detail=str(e))- finally:- conn.close()@router.get("/attachments/{attachment_id}")def download_attachment(attachment_id: int, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):att = kanban_db.get_attachment(conn, attachment_id)if att is None:raise HTTPException(status_code=404, detail="attachment not found")- # Confirm the blob still lives under the board's attachments root- # before serving — defense in depth against a tampered DB row.+ # Defense in depth against a tampered DB row: the blob must still live+ # under the board's attachments root.root = kanban_db.attachments_root(board=board).resolve()try:stored = Path(att.stored_path).resolve()@@ -805,26 +592,18 @@ def download_attachment(attachment_id: int, board: Optional[str] = Query(None)):filename=att.filename,media_type=att.content_type or "application/octet-stream",)- finally:- conn.close()@router.delete("/attachments/{attachment_id}")def remove_attachment(attachment_id: int, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):att = kanban_db.delete_attachment(conn, attachment_id)if att is None:raise HTTPException(status_code=404, detail="attachment not found")return {"ok": True, "id": attachment_id}- finally:- conn.close()-# ----------------------------------------------------------------------------# PATCH /tasks/:id (status / assignee / priority / title / body)-# ---------------------------------------------------------------------------+# --- PATCH /tasks/:id and POST /tasks/bulk ---------------------------------class UpdateTaskBody(BaseModel):status: Optional[str] = None@@ -834,125 +613,174 @@ class UpdateTaskBody(BaseModel):body: Optional[str] = Noneresult: Optional[str] = Noneblock_reason: Optional[str] = None- # Structured handoff fields — forwarded to complete_task when status- # transitions to 'done'. Dashboard parity with ``hermes kanban- # complete --summary ... --metadata ...``.+ # Structured handoff fields forwarded to complete_task on -> 'done'+ # (parity with ``hermes kanban complete --summary/--metadata``).summary: Optional[str] = Nonemetadata: Optional[dict] = None- # Per-task model/provider override (the board's model dropdown).- # ``model_override=""`` clears both. ``clear_model_override=True`` is- # the explicit clear signal — needed because Optional[str]=None means- # "field not sent" in a PATCH, not "set to NULL".+ # Model/provider override. In a PATCH ``None`` means "field not sent", so+ # ``clear_model_override=True`` is the explicit clear signal.model_override: Optional[str] = Noneprovider_override: Optional[str] = Noneclear_model_override: bool = False- # Per-task thinking depth. ``"none"`` is a VALUE (thinking off), not a- # clear — use ``clear_reasoning_effort=True`` to fall back to the- # profile's own level. Separate from the model clear so dropping a model- # override doesn't silently reset the depth the operator chose.+ # Thinking depth: ``"none"`` is a VALUE (thinking off); clear separately so+ # dropping a model override doesn't silently reset the depth.reasoning_effort: Optional[str] = Noneclear_reasoning_effort: bool = False+class BulkTaskBody(BaseModel):+ ids: list[str]+ status: Optional[str] = None+ assignee: Optional[str] = None # "" or None = unassign+ priority: Optional[int] = None+ archive: bool = False+ result: Optional[str] = None+ summary: Optional[str] = None+ metadata: Optional[dict] = None+ reclaim_first: bool = False+ # Same semantics as UpdateTaskBody.+ model_override: Optional[str] = None+ provider_override: Optional[str] = None+ clear_model_override: bool = False+ reasoning_effort: Optional[str] = None+ clear_reasoning_effort: bool = False+++class _StatusRejected(Exception):+ """A status the dashboard may never set directly (message is user-facing)."""+++class _UnknownStatus(_StatusRejected):+ pass+++_RUNNING_DIRECT_MSG = "Cannot set status to 'running' directly; use the dispatcher/claim path"++def _reopen_if_review(conn, task_id: str, current) -> Optional[bool]:- """Route a task leaving the ``review`` lane through ``reopen_review_task``- (proper transition: stale-run recovery, parent re-gate, ``review_reopened``- event) instead of a raw status write. Returns the transition result, or- ``None`` when the task isn't in ``review`` so the caller falls through to- its normal handling. Shared by the single-task and bulk status handlers so- the review-reopen routing can't drift between them.- """+ """Route a task leaving ``review`` through ``reopen_review_task`` (stale-run+ recovery, parent re-gate, ``review_reopened`` event) instead of a raw status+ write. Returns None when the task isn't in review so callers fall through."""if current is not None and getattr(current, "status", None) == "review":return kanban_db.reopen_review_task(conn, task_id)return None[email protected]("/tasks/{task_id}")-def update_task(task_id: str, payload: UpdateTaskBody, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:- task = kanban_db.get_task(conn, task_id)- if task is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")+def _to_ready(conn, task_id, p) -> bool:+ # Re-open blocked/scheduled via unblock; "changes requested" (review -> ready)+ # via reopen_review_task; otherwise a direct drag-drop write (todo -> ready).+ current = kanban_db.get_task(conn, task_id)+ if current and current.status in ("blocked", "scheduled"):+ return kanban_db.unblock_task(conn, task_id)+ reopened = _reopen_if_review(conn, task_id, current)+ return reopened if reopened is not None else _set_status_direct(conn, task_id, "ready")+++def _to_todo_or_triage(conn, task_id, p, s) -> bool:+ # Only review -> todo needs the reopen transition; triage skips the query.+ current = kanban_db.get_task(conn, task_id) if s == "todo" else None+ reopened = _reopen_if_review(conn, task_id, current)+ return reopened if reopened is not None else _set_status_direct(conn, task_id, s)+++# Status verb dispatch shared by PATCH /tasks/{id} and POST /tasks/bulk. Each+# handler is (conn, task_id, payload) -> ok. ``review`` routes through+# request_review (never a block, so it can't trip unblock-loop detection);+# ``force=True`` because a dashboard action is an explicit human override of a+# live worker claim.+_STATUS_HANDLERS: dict[str, Any] = {+ "done": lambda conn, tid, p: kanban_db.complete_task(+ conn, tid, result=p.result, summary=p.summary, metadata=p.metadata,+ ),+ "blocked": lambda conn, tid, p: kanban_db.block_task(+ conn, tid, reason=getattr(p, "block_reason", None),+ ),+ "scheduled": lambda conn, tid, p: kanban_db.schedule_task(+ conn, tid, reason=getattr(p, "block_reason", None),+ ),+ "review": lambda conn, tid, p: kanban_db.request_review(+ conn, tid, summary=p.summary, metadata=p.metadata,+ reviewer=(p.assignee or None), force=True,+ ),+ "ready": _to_ready,+ "todo": lambda conn, tid, p: _to_todo_or_triage(conn, tid, p, "todo"),+ "triage": lambda conn, tid, p: _to_todo_or_triage(conn, tid, p, "triage"),+}- review_assignee_deferred = (- payload.status == "review" and payload.assignee is not None++def _apply_status(conn, task_id: str, s: str, p) -> bool:+ if s == "running":+ raise _StatusRejected(_RUNNING_DIRECT_MSG)+ handler = _STATUS_HANDLERS.get(s)+ if handler is None:+ raise _UnknownStatus(s)+ return handler(conn, task_id, p)+++def _set_priority(conn, task_id: str, priority: int, board: Optional[str]) -> None:+ with kanban_db.write_txn(conn):+ conn.execute("UPDATE tasks SET priority = ? WHERE id = ?", (int(priority), task_id))+ conn.execute(+ "INSERT INTO task_events (task_id, kind, payload, created_at) "+ "VALUES (?, 'reprioritized', ?, ?)",+ (task_id, json.dumps({"priority": int(priority)}), int(time.time())),)+ # Mutation-boundary observer: this direct-SQL write bypasses every kanban_db+ # mutator, so report it here — after the txn commits.+ kanban_db.notify_task_updated(conn, task_id, ("priority",), board=board)+++def _apply_model_override(conn, task_id: str, p) -> bool:+ """Raises ValueError/RuntimeError from kanban_db for the caller to map."""+ new_model = (None if p.clear_model_override else (p.model_override or "").strip() or None)+ return kanban_db.set_model_override(conn, task_id, new_model, provider=p.provider_override)+++def _apply_reasoning_effort(conn, task_id: str, p) -> bool:+ new_effort = None if p.clear_reasoning_effort else p.reasoning_effort+ return kanban_db.set_reasoning_effort(conn, task_id, new_effort)+- # --- assignee ----------------------------------------------------- # For a combined assignee+review patch, request_review must capture- # the current implementer before routing the task to the reviewer.+def _wants_model_override(p) -> bool:+ return p.clear_model_override or p.model_override is not None+++def _wants_reasoning_effort(p) -> bool:+ return p.clear_reasoning_effort or p.reasoning_effort is not None++[email protected]("/tasks/{task_id}")+def update_task(task_id: str, payload: UpdateTaskBody, board: Optional[str] = Query(None)):+ with _board_conn(board) as (board, conn):+ _require_task(conn, task_id)++ # For a combined assignee+review patch, request_review must capture the+ # current implementer before the task is routed to the reviewer.+ review_assignee_deferred = (payload.status == "review" and payload.assignee is not None)if payload.assignee is not None and not review_assignee_deferred:try:- ok = kanban_db.assign_task(- conn, task_id, payload.assignee or None,- )+ ok = kanban_db.assign_task(conn, task_id, payload.assignee or None)except RuntimeError as e:raise HTTPException(status_code=409, detail=str(e))if not ok:raise HTTPException(status_code=404, detail="task not found")- # --- status -------------------------------------------------------if payload.status is not None:s = payload.status- ok = True- if s == "done":- ok = kanban_db.complete_task(- conn, task_id,- result=payload.result,- summary=payload.summary,- metadata=payload.metadata,- )- elif s == "blocked":- ok = kanban_db.block_task(conn, task_id, reason=payload.block_reason)- elif s == "scheduled":- ok = kanban_db.schedule_task(conn, task_id, reason=payload.block_reason)- elif s == "review":- # Manual "request review" from the board. Routes through- # request_review so it is NOT a- # block (never trips unblock-loop detection). Only valid from- # running/ready — a False return becomes the 409 toast below.- ok = kanban_db.request_review(- conn, task_id, summary=payload.summary,- metadata=payload.metadata,- reviewer=(payload.assignee or None),- # Dashboard PATCH is an explicit human action — allowed- # to override a live worker claim (M1 guard).- force=True,- )- if ok and review_assignee_deferred and not payload.assignee:- ok = kanban_db.assign_task(conn, task_id, None)- elif s == "ready":- # Re-open a blocked/scheduled/review task, or just an explicit- # status set. "Changes requested" (review -> ready) goes through- # reopen_review_task via _reopen_if_review.- current = kanban_db.get_task(conn, task_id)- if current and current.status in ("blocked", "scheduled"):- ok = kanban_db.unblock_task(conn, task_id)- else:- reopened = _reopen_if_review(conn, task_id, current)- # Direct status write for drag-drop (todo -> ready etc).- ok = reopened if reopened is not None else _set_status_direct(conn, task_id, "ready")- elif s == "archived":+ if s == "archived":ok = kanban_db.archive_task(conn, task_id)- elif s == "running":- raise HTTPException(- status_code=400,- detail="Cannot set status to 'running' directly; use the dispatcher/claim path",- )- elif s in ("todo", "triage", "scheduled"):- # Only a review task moving to 'todo' needs the reopen- # transition; fetch lazily so triage/scheduled skip the query.- current = kanban_db.get_task(conn, task_id) if s == "todo" else None- reopened = _reopen_if_review(conn, task_id, current)- ok = reopened if reopened is not None else _set_status_direct(conn, task_id, s)else:- raise HTTPException(status_code=400, detail=f"unknown status: {s}")+ try:+ ok = _apply_status(conn, task_id, s, payload)+ except _UnknownStatus:+ raise HTTPException(status_code=400, detail=f"unknown status: {s}")+ except _StatusRejected as e:+ raise HTTPException(status_code=400, detail=str(e))+ if s == "review" and ok and review_assignee_deferred and not payload.assignee:+ ok = kanban_db.assign_task(conn, task_id, None)if not ok:- # For ``ready``, name the blocking parent(s) so the dashboard- # can render an actionable toast instead of a silent no-op.- # See #26744.+ # For ``ready``, name the blocking parent(s) so the dashboard can+ # render an actionable toast instead of a silent no-op.if s == "ready":blockers = _parents_blocking_ready(conn, task_id)if blockers:@@ -972,56 +800,21 @@ def update_task(task_id: str, payload: UpdateTaskBody, board: Optional[str] = Qudetail=f"status transition to {s!r} not valid from current state",)- # --- model/provider override ---------------------------------------- if payload.clear_model_override or payload.model_override is not None:- new_model = (- None if payload.clear_model_override- else (payload.model_override or "").strip() or None- )- try:- ok = kanban_db.set_model_override(- conn, task_id, new_model,- provider=payload.provider_override,- )- except (ValueError, RuntimeError) as e:- raise HTTPException(status_code=400, detail=str(e))- if not ok:- raise HTTPException(status_code=404, detail="task not found")-- # --- reasoning effort ----------------------------------------------- if payload.clear_reasoning_effort or payload.reasoning_effort is not None:- new_effort = (- None if payload.clear_reasoning_effort- else payload.reasoning_effort- )- try:- ok = kanban_db.set_reasoning_effort(conn, task_id, new_effort)- except (ValueError, RuntimeError) as e:- raise HTTPException(status_code=400, detail=str(e))- if not ok:- raise HTTPException(status_code=404, detail="task not found")+ for wanted, apply in (+ (_wants_model_override, _apply_model_override),+ (_wants_reasoning_effort, _apply_reasoning_effort),+ ):+ if wanted(payload):+ try:+ ok = apply(conn, task_id, payload)+ except (ValueError, RuntimeError) as e:+ raise HTTPException(status_code=400, detail=str(e))+ if not ok:+ raise HTTPException(status_code=404, detail="task not found")- # --- priority -----------------------------------------------------if payload.priority is not None:- with kanban_db.write_txn(conn):- conn.execute(- "UPDATE tasks SET priority = ? WHERE id = ?",- (int(payload.priority), task_id),- )- conn.execute(- "INSERT INTO task_events (task_id, kind, payload, created_at) "- "VALUES (?, 'reprioritized', ?, ?)",- (task_id, json.dumps({"priority": int(payload.priority)}),- int(time.time())),- )- # Mutation-boundary observer (RFC #58548): this direct-SQL write- # bypasses every kanban_db mutator, so report it here — after- # the txn commits.- kanban_db.notify_task_updated(- conn, task_id, ("priority",), board=board,- )+ _set_priority(conn, task_id, payload.priority, board)- # --- title / body -------------------------------------------------if payload.title is not None or payload.body is not None:with kanban_db.write_txn(conn):sets, vals = [], []@@ -1034,16 +827,14 @@ def update_task(task_id: str, payload: UpdateTaskBody, board: Optional[str] = Qusets.append("body = ?")vals.append(payload.body)vals.append(task_id)- conn.execute(- f"UPDATE tasks SET {', '.join(sets)} WHERE id = ?", vals,- )+ conn.execute(f"UPDATE tasks SET {', '.join(sets)} WHERE id = ?", vals)conn.execute("INSERT INTO task_events (task_id, kind, payload, created_at) ""VALUES (?, 'edited', NULL, ?)",(task_id, int(time.time())),)- # Mutation-boundary observer (RFC #58548), post-commit. Field- # names only — values never leave the DB via this payload.+ # Post-commit mutation observer; field names only — values never+ # leave the DB via this payload.kanban_db.notify_task_updated(conn, task_id,[f for f in ("title", "body") if getattr(payload, f) is not None],@@ -1052,89 +843,40 @@ def update_task(task_id: str, payload: UpdateTaskBody, board: Optional[str] = Quupdated = kanban_db.get_task(conn, task_id)return {"task": _task_dict(updated) if updated else None}- finally:- conn.close()--# ----------------------------------------------------------------------------# DELETE /tasks/:id-# ---------------------------------------------------------------------------@router.delete("/tasks/{task_id}")def delete_task(task_id: str, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):ok = kanban_db.delete_task(conn, task_id)if not ok:raise HTTPException(status_code=404, detail=f"task {task_id} not found")return {"deleted": True, "task_id": task_id}- finally:- conn.close()-def _parents_blocking_ready(- conn: sqlite3.Connection, task_id: str,-) -> list:- """Return parent rows (``id``, ``title``, ``status``) that aren't ``done``- and therefore prevent ``task_id`` from being promoted to ``ready``.-- Used to enrich the 409 response from :func:`update_task` so the- dashboard can show an actionable toast (#26744) instead of a silent- no-op. Returns ``[]`` when nothing blocks the transition (e.g. no- parents, or all parents already done).- """+def _parents_blocking_ready(conn: sqlite3.Connection, task_id: str) -> list:+ """Parent rows (id, title, status) not ``done`` that block promotion to ``ready``."""rows = conn.execute("SELECT t.id, t.title, t.status FROM tasks t ""JOIN task_links l ON l.parent_id = t.id ""WHERE l.child_id = ? AND t.status != 'done'",(task_id,),).fetchall()- return [- {"id": r["id"], "title": r["title"], "status": r["status"]}- for r in rows- ]+ return [{"id": r["id"], "title": r["title"], "status": r["status"]} for r in rows]-def _invalidate_descendants_for_parent_reopen(- conn: sqlite3.Connection,- parent_id: str,- terminations: list[tuple[Optional[int], Optional[str]]],-) -> None:- """Delegate to the domain-layer implementation in :mod:`kanban_db`.-- Kept as a thin shim so ``_set_status_direct`` stays readable; the actual- invalidation (recursive-CTE discovery, per-descendant events + comments,- run closing, failure-counter reset) lives in- :func:`kanban_db.invalidate_descendants_for_parent_reopen` so every- reopen surface shares one implementation. We run inside the caller's- open transaction, so the domain function composes via a savepoint and- returns the worker terminations for us to perform post-commit (events- must be durable BEFORE the kill).- """- result = kanban_db.invalidate_descendants_for_parent_reopen(- conn, parent_id, author="dashboard",- )- terminations.extend(result["terminations"])---def _set_status_direct(- conn: sqlite3.Connection, task_id: str, new_status: str,-) -> bool:- """Direct status write for drag-drop moves that aren't covered by the- structured complete/block/unblock/archive verbs (e.g. todo<->ready,- running<->ready). Appends a ``status`` event row for the live feed.+def _set_status_direct(conn: sqlite3.Connection, task_id: str, new_status: str) -> bool:+ """Direct status write for drag-drop moves not covered by the structured+ complete/block/unblock/archive verbs (todo<->ready, running<->ready).+ Appends a ``status`` event for the live feed.- When this transitions OFF ``running`` to anything other than the- terminal verbs above (which own their own run closing), we close the- active run with outcome='reclaimed' so attempt history isn't- orphaned. ``running -> ready`` via drag-drop is the common case- (user yanking a stuck worker back to the queue).+ Leaving ``running`` closes the active run with outcome='reclaimed' so attempt+ history isn't orphaned, and the worker is terminated only AFTER the txn+ commits (events must be durable before the kill)."""terminations: list[tuple[Optional[int], Optional[str]]] = []effective_status = new_statuswith kanban_db.write_txn(conn):- # Snapshot current state so we know whether to close a run.prev = conn.execute("SELECT status, current_run_id, worker_pid, claim_lock ""FROM tasks WHERE id = ?",@@ -1144,9 +886,7 @@ def _set_status_direct(return Falseif prev["status"] == "running" and new_status == "ready":- resume_status = kanban_db._retry_status_for_run(- conn, task_id, prev["current_run_id"]- )+ resume_status = kanban_db._retry_status_for_run(conn, task_id, prev["current_run_id"])if resume_status == "review":effective_status = ("review"@@ -1154,9 +894,8 @@ def _set_status_direct(else "todo")- # Guard: don't allow promoting to 'ready' unless all parents are done.- # Prevents the dispatcher from spawning a child whose upstream work- # hasn't completed (e.g. T4 dispatched while T3 is still blocked).+ # Never promote to 'ready' unless all parents are done — otherwise the+ # dispatcher spawns a child whose upstream work hasn't completed.if effective_status == "ready":parent_statuses = conn.execute("SELECT t.status FROM tasks t "@@ -1181,13 +920,7 @@ def _set_status_direct(" claim_expires = CASE WHEN ? = 'running' THEN claim_expires ELSE NULL END, "" worker_pid = CASE WHEN ? = 'running' THEN worker_pid ELSE NULL END ""WHERE id = ?",- (- effective_status,- effective_status,- effective_status,- effective_status,- task_id,- ),+ (effective_status,) * 4 + (task_id,),)if cur.rowcount != 1:return False@@ -1205,32 +938,26 @@ def _set_status_direct((task_id,run_id,- json.dumps(- {- "status": effective_status,- "requested_status": new_status,- }- ),+ json.dumps({"status": effective_status, "requested_status": new_status}),int(time.time()),),)if reopening_satisfied_parent:- _invalidate_descendants_for_parent_reopen(- conn,- task_id,- terminations,+ # Domain-layer invalidation composes via a savepoint inside our txn+ # and hands back worker terminations to perform post-commit.+ result = kanban_db.invalidate_descendants_for_parent_reopen(+ conn, task_id, author="dashboard",)+ terminations.extend(result["terminations"])for pid, claim_lock in terminations:kanban_db._terminate_reclaimed_worker(pid, claim_lock)- # If we re-opened something, children may have gone stale.+ # Re-opening something may have made children stale.if effective_status in {"done", "ready", "review"}:kanban_db.recompute_ready(conn)return True-# ----------------------------------------------------------------------------# Comments-# ---------------------------------------------------------------------------+# --- Comments / links -------------------------------------------------------class CommentBody(BaseModel):body: str@@ -1241,23 +968,14 @@ class CommentBody(BaseModel):def add_comment(task_id: str, payload: CommentBody, board: Optional[str] = Query(None)):if not payload.body.strip():raise HTTPException(status_code=400, detail="body is required")- board = _resolve_board(board)- conn = _conn(board=board)- try:- if kanban_db.get_task(conn, task_id) is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")+ with _board_conn(board) as (board, conn):+ _require_task(conn, task_id)kanban_db.add_comment(conn, task_id, author=payload.author or "dashboard", body=payload.body,)return {"ok": True}- finally:- conn.close()-# ----------------------------------------------------------------------------# Links-# ----------------------------------------------------------------------------class LinkBody(BaseModel):parent_id: strchild_id: str@@ -1265,15 +983,9 @@ class LinkBody(BaseModel):@router.post("/links")def add_link(payload: LinkBody, board: Optional[str] = Query(None)):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn), _value_error_400():kanban_db.link_tasks(conn, payload.parent_id, payload.child_id)return {"ok": True}- except ValueError as e:- raise HTTPException(status_code=400, detail=str(e))- finally:- conn.close()@router.delete("/links")@@ -1282,109 +994,39 @@ def delete_link(child_id: str = Query(...),board: Optional[str] = Query(None),):- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):ok = kanban_db.unlink_tasks(conn, parent_id, child_id)return {"ok": bool(ok)}- finally:- conn.close()---# ----------------------------------------------------------------------------# Bulk actions (multi-select on the board)-# -----------------------------------------------------------------------------class BulkTaskBody(BaseModel):- ids: list[str]- status: Optional[str] = None- assignee: Optional[str] = None # "" or None = unassign- priority: Optional[int] = None- archive: bool = False- result: Optional[str] = None- summary: Optional[str] = None- metadata: Optional[dict] = None- reclaim_first: bool = False- # Bulk model/provider override — same semantics as UpdateTaskBody.- model_override: Optional[str] = None- provider_override: Optional[str] = None- clear_model_override: bool = False- # Bulk thinking-depth override — same semantics as UpdateTaskBody.- reasoning_effort: Optional[str] = None- clear_reasoning_effort: bool = False@router.post("/tasks/bulk")def bulk_update(payload: BulkTaskBody, board: Optional[str] = Query(None)):- """Apply the same patch to every id in ``payload.ids``.-- This is an *independent* iteration — per-task failures don't abort- siblings. Returns per-id outcome so the UI can surface partials.- """+ """Apply the same patch to every id. Independent iteration — per-task+ failures don't abort siblings; returns per-id outcome for partials."""ids = [i for i in (payload.ids or []) if i]if not ids:raise HTTPException(status_code=400, detail="ids is required")results: list[dict] = []- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):for tid in ids:entry: dict[str, Any] = {"id": tid, "ok": True}try:- task = kanban_db.get_task(conn, tid)- if task is None:+ if kanban_db.get_task(conn, tid) is None:entry.update(ok=False, error="not found")results.append(entry)continue- if payload.archive:- if not kanban_db.archive_task(conn, tid):- entry.update(ok=False, error="archive refused")+ if payload.archive and not kanban_db.archive_task(conn, tid):+ entry.update(ok=False, error="archive refused")if payload.status is not None and not payload.archive:s = payload.status- if s == "done":- ok = kanban_db.complete_task(- conn, tid,- result=payload.result,- summary=payload.summary,- metadata=payload.metadata,- )- elif s == "blocked":- ok = kanban_db.block_task(conn, tid)- elif s == "review":- # Non-block review handoff (mirror of PATCH /tasks/{id}).- ok = kanban_db.request_review(- conn, tid, summary=payload.summary,- metadata=payload.metadata,- reviewer=(payload.assignee or None),- # Bulk dashboard action: explicit human override.- force=True,- )- elif s == "ready":- cur = kanban_db.get_task(conn, tid)- if cur and cur.status in ("blocked", "scheduled"):- ok = kanban_db.unblock_task(conn, tid)- else:- reopened = _reopen_if_review(conn, tid, cur)- ok = reopened if reopened is not None else _set_status_direct(conn, tid, "ready")- elif s == "running":- entry.update(- ok=False,- error=(- "Cannot set status to 'running' directly; "- "use the dispatcher/claim path"- ),- )+ try:+ ok = _apply_status(conn, tid, s, payload)+ except _UnknownStatus:+ entry.update(ok=False, error=f"unknown status {s!r}")results.append(entry)continue- elif s == "scheduled":- ok = kanban_db.schedule_task(conn, tid)- elif s in {"todo", "triage"}:- # Fetch lazily: only review->todo needs reopen.- cur = kanban_db.get_task(conn, tid) if s == "todo" else None- reopened = _reopen_if_review(conn, tid, cur)- ok = reopened if reopened is not None else _set_status_direct(conn, tid, s)- else:- entry.update(ok=False, error=f"unknown status {s!r}")+ except _StatusRejected as e:+ entry.update(ok=False, error=str(e))results.append(entry)continueif not ok:@@ -1397,108 +1039,50 @@ def bulk_update(payload: BulkTaskBody, board: Optional[str] = Query(None)):reclaim_first=True,)else:- ok = kanban_db.assign_task(- conn, tid, payload.assignee or None,- )+ ok = kanban_db.assign_task(conn, tid, payload.assignee or None)if not ok:entry.update(ok=False, error="assign refused")except RuntimeError as e:entry.update(ok=False, error=str(e))if payload.priority is not None:- with kanban_db.write_txn(conn):- conn.execute(- "UPDATE tasks SET priority = ? WHERE id = ?",- (int(payload.priority), tid),- )- conn.execute(- "INSERT INTO task_events (task_id, kind, payload, created_at) "- "VALUES (?, 'reprioritized', ?, ?)",- (tid, json.dumps({"priority": int(payload.priority)}),- int(time.time())),- )- # Mutation-boundary observer (RFC #58548): the bulk- # editor writes with direct SQL too — report each task's- # committed write.- kanban_db.notify_task_updated(- conn, tid, ("priority",), board=board,- )- if payload.clear_model_override or payload.model_override is not None:- new_model = (- None if payload.clear_model_override- else (payload.model_override or "").strip() or None- )- try:- ok = kanban_db.set_model_override(- conn, tid, new_model,- provider=payload.provider_override,- )- if not ok:- entry.update(ok=False, error="model override refused")- except (ValueError, RuntimeError) as e:- entry.update(ok=False, error=str(e))- if payload.clear_reasoning_effort or payload.reasoning_effort is not None:- new_effort = (- None if payload.clear_reasoning_effort- else payload.reasoning_effort- )- try:- ok = kanban_db.set_reasoning_effort(conn, tid, new_effort)- if not ok:- entry.update(ok=False, error="reasoning override refused")- except (ValueError, RuntimeError) as e:- entry.update(ok=False, error=str(e))- except Exception as e: # defensive — one bad id shouldn't kill the batch+ _set_priority(conn, tid, payload.priority, board)+ for wanted, apply, refused in (+ (_wants_model_override, _apply_model_override, "model override refused"),+ (_wants_reasoning_effort, _apply_reasoning_effort, "reasoning override refused"),+ ):+ if wanted(payload):+ try:+ if not apply(conn, tid, payload):+ entry.update(ok=False, error=refused)+ except (ValueError, RuntimeError) as e:+ entry.update(ok=False, error=str(e))+ except Exception as e: # one bad id shouldn't kill the batchentry.update(ok=False, error=str(e))results.append(entry)return {"results": results}- finally:- conn.close()-# ----------------------------------------------------------------------------# Diagnostics — fleet-wide distress signals (hallucinations, crashes,-# spawn failures, stuck-blocked). See hermes_cli.kanban_diagnostics for-# the rule engine.-# ---------------------------------------------------------------------------+# --- Diagnostics — fleet-wide distress signals (see kanban_diagnostics) ------@router.get("/diagnostics")def list_diagnostics(board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),- severity: Optional[str] = Query(- None,- description="Filter by severity: warning|error|critical",- ),+ severity: Optional[str] = Query(None, description="Filter by severity: warning|error|critical"),):- """Return ``[{task_id, task_title, task_status, task_assignee,- diagnostics: [...]}, ...]`` for every task on the board with at- least one active diagnostic.-- Severity-filterable so the UI can render "just the critical ones"- or the CLI can grep. Useful for the board-header attention strip- AND for ``hermes kanban diagnostics`` which shells to this- endpoint when the dashboard's running, or invokes the engine- directly when it isn't.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Return ``[{task_id, task_title, task_status, task_assignee, diagnostics}]``+ for every task with an active diagnostic, highest severity first then most+ recent. Also consumed by ``hermes kanban diagnostics`` when the dashboard runs."""+ with _board_conn(board) as (board, conn):diags_by_task = _compute_task_diagnostics(conn, task_ids=None)+ if severity and diags_by_task:+ diags_by_task = {+ tid: keep+ for tid, dl in diags_by_task.items()+ if (keep := [d for d in dl if kd.severity_at_or_above(d.get("severity"), severity)])+ }if not diags_by_task:return {"diagnostics": [], "count": 0}- # Narrow by severity if asked.- if severity:- filtered: dict[str, list[dict]] = {}- for tid, dl in diags_by_task.items():- keep = [d for d in dl if kd.severity_at_or_above(d.get("severity"), severity)]- if keep:- filtered[tid] = keep- diags_by_task = filtered- if not diags_by_task:- return {"diagnostics": [], "count": 0}-- # Pull the task rows we need in one query so we can include- # titles/statuses without a per-task lookup.ids = list(diags_by_task.keys())placeholders = ",".join(["?"] * len(ids))rows = {@@ -1519,52 +1103,31 @@ def list_diagnostics("task_assignee": r["assignee"] if r else None,"diagnostics": dl,})- # Sort: highest severity first, then most recent.- from hermes_cli.kanban_diagnostics import SEVERITY_ORDER- sev_idx = {s: i for i, s in enumerate(SEVERITY_ORDER)}+ sev_idx = {s: i for i, s in enumerate(kd.SEVERITY_ORDER)}+def _sort_key(row):top = row["diagnostics"][0]- return (- -sev_idx.get(top.get("severity"), -1),- -(top.get("last_seen_at") or 0),- )+ return (-sev_idx.get(top.get("severity"), -1), -(top.get("last_seen_at") or 0))out.sort(key=_sort_key)- return {- "diagnostics": out,- "count": sum(len(d["diagnostics"]) for d in out),- }- finally:- conn.close()-+ return {"diagnostics": out, "count": sum(len(d["diagnostics"]) for d in out)}-# ----------------------------------------------------------------------------# Worker visibility — cross-task active-worker list and per-run inspection-# ---------------------------------------------------------------------------+# --- Worker visibility — active-worker list, per-run inspect/terminate -------try:import psutil as _psutilexcept ImportError:_psutil = None # type: ignore[assignment]-[email protected]("/workers/active")-def list_active_workers(- board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),-):- """Return every currently-running worker on the board.-- A worker is a ``task_runs`` row whose ``ended_at`` is NULL and whose- ``worker_pid`` is non-NULL, belonging to a task with ``status='running'``.-- Returns ``{workers: [...], count: N, checked_at: <epoch>}``. Each- worker entry carries enough context for the dashboard to link back to- its task without a second round-trip.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+[email protected]("/workers/active")+def list_active_workers(+ board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),+):+ """Every running worker: an open ``task_runs`` row with a ``worker_pid`` whose+ task is ``running``. Returns ``{workers, count, checked_at}``."""+ with _board_conn(board) as (board, conn):rows = conn.execute("""SELECT@@ -1588,26 +1151,15 @@ def list_active_workers(ORDER BY r.started_at ASC""",).fetchall()- workers = [- {- "run_id": row["run_id"],- "task_id": row["task_id"],- "task_title": row["task_title"],- "task_status": row["task_status"],- "task_assignee": row["task_assignee"],- "profile": row["profile"],- "worker_pid": row["worker_pid"],- "started_at": row["started_at"],- "claim_lock": row["claim_lock"],- "claim_expires": row["claim_expires"],- "last_heartbeat_at": row["last_heartbeat_at"],- "max_runtime_seconds": row["max_runtime_seconds"],- }- for row in rows- ]+ workers = [dict(row) for row in rows]return {"workers": workers, "count": len(workers), "checked_at": int(time.time())}- finally:- conn.close()+++def _require_run(conn, run_id: int) -> kanban_db.Run:+ r = kanban_db.get_run(conn, run_id)+ if r is None:+ raise HTTPException(status_code=404, detail=f"run {run_id} not found")+ return r@router.get("/runs/{run_id}")@@ -1615,21 +1167,9 @@ def get_run_endpoint(run_id: int,board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),):- """Direct lookup of a ``task_runs`` row by its integer id.-- Returns ``{run: {...}}`` using the same serialisation as the- per-task run history embedded in ``GET /tasks/{task_id}``.- 404 when no such run exists.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:- r = kanban_db.get_run(conn, run_id)- if r is None:- raise HTTPException(status_code=404, detail=f"run {run_id} not found")- return {"run": _run_dict(r)}- finally:- conn.close()+ """``{run: {...}}`` with the same serialisation as ``GET /tasks/{id}``; 404 if unknown."""+ with _board_conn(board) as (board, conn):+ return {"run": _run_dict(_require_run(conn, run_id))}@router.get("/runs/{run_id}/inspect")@@ -1637,27 +1177,14 @@ def inspect_run_endpoint(run_id: int,board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),):- """Live PID stats for a run's worker process via psutil.-- If the run has already ended, or has no recorded ``worker_pid``,- returns ``{alive: false}`` with a human-readable ``reason``.+ """Live psutil stats for a run's worker process.- When the process is live, returns CPU, memory, thread count, fd count,- status, create_time, and cmdline. ``access_denied`` is set when the- OS refuses inspection rather than raising a 500.-- psutil availability: if psutil is not installed the endpoint still- works but ``alive`` is always returned as ``false`` with- ``reason="psutil not available"``.+ ``{alive: false, reason}`` when the run ended, has no pid, the process is+ gone, or psutil is missing; ``access_denied`` style errors are reported+ inline rather than as a 500."""- board = _resolve_board(board)- conn = _conn(board=board)- try:- r = kanban_db.get_run(conn, run_id)- if r is None:- raise HTTPException(status_code=404, detail=f"run {run_id} not found")- finally:- conn.close()+ with _board_conn(board) as (board, conn):+ r = _require_run(conn, run_id)if r.ended_at is not None:return {"run_id": run_id, "alive": False, "reason": "run already ended"}@@ -1675,10 +1202,9 @@ def inspect_run_endpoint("cpu_percent", "memory_info", "num_threads","status", "create_time", "cmdline",])- # num_fds is POSIX-only; skip gracefully on Windows.try:num_fds = proc.num_fds()- except AttributeError:+ except AttributeError: # POSIX-onlynum_fds = Nonemem = info.get("memory_info")return {@@ -1710,34 +1236,13 @@ def terminate_run_endpoint(payload: TerminateRunBody,board: Optional[str] = Query(None, description="Kanban board slug (omit for current)"),):- """Terminate the worker process backing an in-flight run.-- Resolves ``run_id`` to its parent ``task_id`` and routes through- :func:`kanban_db.reclaim_task` so the SIGTERM->SIGKILL flow,- run-outcome bookkeeping, and event-log append all match what the- existing ``POST /tasks/{task_id}/reclaim`` endpoint does.-- Responses:- * 200 ``{"ok": true, "run_id": ..., "task_id": ...}`` on success.- * 404 when ``run_id`` is unknown.- * 409 when the run has already ended, or the task is no longer in- a claimable state.-- Closes the gap left by PR #28432, which shipped the read-only- sibling endpoints (``/workers/active``, ``/runs/{run_id}``,- ``/runs/{run_id}/inspect``) but no termination control surface.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:- r = kanban_db.get_run(conn, run_id)- if r is None:- raise HTTPException(status_code=404, detail=f"run {run_id} not found")+ """Terminate the worker behind an in-flight run via ``reclaim_task`` so the+ SIGTERM->SIGKILL flow, run bookkeeping, and event log match+ ``POST /tasks/{id}/reclaim``. 404 unknown run; 409 already ended / not reclaimable."""+ with _board_conn(board) as (board, conn):+ r = _require_run(conn, run_id)if r.ended_at is not None:- raise HTTPException(- status_code=409,- detail=f"run {run_id} already ended",- )+ raise HTTPException(status_code=409, detail=f"run {run_id} already ended")ok = kanban_db.reclaim_task(conn, r.task_id, reason=payload.reason)if not ok:raise HTTPException(@@ -1748,34 +1253,19 @@ def terminate_run_endpoint(),)return {"ok": True, "run_id": run_id, "task_id": r.task_id}- finally:- conn.close()-# ----------------------------------------------------------------------------# Recovery actions — reclaim a running claim, reassign to a new profile-# ---------------------------------------------------------------------------+# --- Recovery actions — reclaim / specify / reassign / estimate -------------class ReclaimBody(BaseModel):reason: Optional[str] = None@router.post("/tasks/{task_id}/reclaim")-def reclaim_task_endpoint(- task_id: str,- payload: ReclaimBody,- board: Optional[str] = Query(None),-):- """Release an active worker claim on a running task.-- Used by the dashboard recovery popover when an operator wants to- abort a stuck worker (e.g. one that keeps hallucinating card ids)- without waiting for the claim TTL. Maps 1:1 to- ``hermes kanban reclaim <task_id> --reason ...``.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+def reclaim_task_endpoint(task_id: str, payload: ReclaimBody, board: Optional[str] = Query(None)):+ """Release an active worker claim without waiting for the claim TTL+ (``hermes kanban reclaim <task_id> --reason ...``)."""+ with _board_conn(board) as (board, conn):ok = kanban_db.reclaim_task(conn, task_id, reason=payload.reason)if not ok:raise HTTPException(@@ -1786,53 +1276,31 @@ def reclaim_task_endpoint(),)return {"ok": True, "task_id": task_id}- finally:- conn.close()class SpecifyBody(BaseModel):- """Optional author override. Nothing else is configurable from the- dashboard — model + prompt come from ``auxiliary.triage_specifier``- in config.yaml, same as the CLI."""+ """Only the author is configurable; model + prompt come from+ ``auxiliary.triage_specifier`` in config.yaml, same as the CLI."""author: Optional[str] = None@router.post("/tasks/{task_id}/specify")-def specify_task_endpoint(- task_id: str,- payload: SpecifyBody,- board: Optional[str] = Query(None),-):- """Flesh out a triage-column task via the auxiliary LLM and promote- it to ``todo``. Maps 1:1 to ``hermes kanban specify <task_id>``.-- Returns the outcome shape used by the CLI: ``{ok, task_id, reason,- new_title}``. A non-OK outcome is NOT an HTTP error — the UI renders- the reason inline (e.g. "no auxiliary client configured") so the- operator knows what to fix, and retries without a page reload.-- This endpoint runs in FastAPI's threadpool (sync ``def``) because- the underlying LLM call can take tens of seconds to minutes on- reasoning models, which would block the event loop if we used- ``async def`` without an explicit ``run_in_executor``.+def specify_task_endpoint(task_id: str, payload: SpecifyBody, board: Optional[str] = Query(None)):+ """Flesh out a triage task via the auxiliary LLM and promote it to ``todo``+ (``hermes kanban specify``). Returns ``{ok, task_id, reason, new_title}``; a+ non-OK outcome is NOT an HTTP error — the UI renders the reason inline.++ Sync ``def`` so the slow LLM call runs in FastAPI's threadpool."""board = _resolve_board(board)- # Pin the board for the duration of this call so the specifier module- # (which calls ``kb.connect()`` with no args) hits the right DB. Use a- # context-local override rather than mutating the process-global- # HERMES_KANBAN_BOARD env var — this endpoint runs in FastAPI's- # threadpool, so two concurrent requests for different boards would- # otherwise race on the shared env var and cross-write (issue #38323).+ # Context-local board pin (not the process-global HERMES_KANBAN_BOARD env+ # var): concurrent threadpool requests for different boards would otherwise+ # race on the shared env var and cross-write.with kanban_db.scoped_current_board(board or kanban_db.DEFAULT_BOARD):- # Import lazily so a missing auxiliary client at import time- # doesn't break plugin load.- from hermes_cli import kanban_specify # noqa: WPS433 (intentional)+ from hermes_cli import kanban_specify # lazy: missing aux client must not break plugin load- outcome = kanban_specify.specify_task(- task_id,- author=(payload.author or None),- )+ outcome = kanban_specify.specify_task(task_id, author=(payload.author or None))return {"ok": bool(outcome.ok),@@ -1849,21 +1317,10 @@ class ReassignBody(BaseModel):@router.post("/tasks/{task_id}/reassign")-def reassign_task_endpoint(- task_id: str,- payload: ReassignBody,- board: Optional[str] = Query(None),-):- """Reassign a task to a different profile, optionally reclaiming first.-- Used by the dashboard recovery popover when an operator wants to- retry a task with a different worker profile (e.g. switch to a- smarter model after the assigned profile keeps hallucinating).- Maps 1:1 to ``hermes kanban reassign <task_id> <profile> [--reclaim]``.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+def reassign_task_endpoint(task_id: str, payload: ReassignBody, board: Optional[str] = Query(None)):+ """Reassign to another profile, optionally reclaiming first+ (``hermes kanban reassign <task_id> <profile> [--reclaim]``)."""+ with _board_conn(board) as (board, conn):ok = kanban_db.reassign_task(conn, task_id,payload.profile or None,@@ -1879,16 +1336,10 @@ def reassign_task_endpoint(),)return {"ok": True, "task_id": task_id, "assignee": payload.profile or None}- finally:- conn.close()--# ----------------------------------------------------------------------------# Estimate — a rough token/complexity estimate for a task via the auxiliary-# (auto-routed) model. NOT a dollar cost: providers don't report cost-# reliably, so we estimate tokens + a complexity band with a one-line why.-# ---------------------------------------------------------------------------+# Estimate: rough token/complexity read via the auxiliary model. NOT a dollar+# cost — providers don't report cost reliably._ESTIMATE_SYSTEM_PROMPT = ("You estimate how much work an autonomous coding agent will spend on a ""kanban task. Given the task title and description, respond with STRICT "@@ -1909,36 +1360,23 @@ class EstimateBody(BaseModel):@router.post("/estimate")def estimate_text_endpoint(payload: EstimateBody):- """Estimate from raw title/body — used by the create dialog before a task- exists yet. Same outcome shape as the per-task endpoint below."""+ """Estimate from raw title/body (create dialog, before a task exists)."""return _run_estimate(payload.title, payload.body)@router.post("/tasks/{task_id}/estimate")def estimate_task_endpoint(task_id: str, board: Optional[str] = Query(None)):- """Rough token + complexity estimate for an existing task via the auxiliary- model. Returns ``{ok, est_tokens, complexity, rationale, model}``; a non-OK- outcome is NOT an HTTP error. Runs in FastAPI's threadpool (sync ``def``)- because the LLM call can take several seconds.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Estimate for an existing task; ``{ok, est_tokens, complexity, rationale, model}``."""+ with _board_conn(board) as (board, conn):task = kanban_db.get_task(conn, task_id)- finally:- conn.close()if task is None:raise HTTPException(status_code=404, detail=f"task {task_id} not found")return _run_estimate(task.title, task.body)def _run_estimate(title: str, body: Optional[str]) -> dict:- """Shared estimate core: ask the auto-routed auxiliary model for a rough- token + complexity read on a task described by ``title``/``body``.-- Never raises — a bad config / parse / API error becomes- ``{"ok": False, "reason": ...}`` so the UI can render it inline.- """+ """Never raises — config/parse/API errors become ``{"ok": False, "reason"}``+ so the UI renders them inline."""if not (title or "").strip():return {"ok": False, "reason": "a title is required to estimate"}@@ -1951,10 +1389,7 @@ def _run_estimate(title: str, body: Optional[str]) -> dict:s = (s or "").strip()return s if len(s) <= n else s[:n] + "…"- user_msg = (- f"Title: {_cap(title, 400)}\n\n"- f"Description:\n{_cap(body, 4000) or '(none)'}"- )+ user_msg = (f"Title: {_cap(title, 400)}\n\n" f"Description:\n{_cap(body, 4000) or '(none)'}")try:resp = call_llm(task="kanban_estimator",@@ -1975,16 +1410,14 @@ def _run_estimate(title: str, body: Optional[str]) -> dict:except Exception:raw, model = "", None- # Reuse the same tolerant JSON-blob extraction the specifier uses.+ # Same tolerant JSON-blob extraction the specifier uses.parsed: Optional[dict] = Nonetry:- import json as _json- import re as _reblob = rawif not blob.lstrip().startswith("{"):- m = _re.search(r"\{.*\}", blob, _re.DOTALL)+ m = re.search(r"\{.*\}", blob, re.DOTALL)blob = m.group(0) if m else blob- obj = _json.loads(blob)+ obj = json.loads(blob)if isinstance(obj, dict):parsed = objexcept Exception:@@ -2011,26 +1444,20 @@ def _run_estimate(title: str, body: Optional[str]) -> dict:}-# ----------------------------------------------------------------------------# Plugin config (read dashboard.kanban.* defaults from config.yaml)-# ----------------------------------------------------------------------------[email protected]("/config")-def get_config():- """Return kanban dashboard preferences from ~/.hermes/config.yaml.+# --- Plugin config ----------------------------------------------------------- Reads the ``dashboard.kanban`` section if present; defaults otherwise.- Used by the UI to pre-select tenant filters, toggle markdown rendering,- or set column-width preferences without a round-trip per page load.- """+def _load_config_or_empty() -> dict:try:from hermes_cli.config import load_config- cfg = load_config() or {}+ return load_config() or {}except Exception:- cfg = {}- dash_cfg = (cfg.get("dashboard") or {})- # dashboard.kanban may itself be a dict; fall back to {}.- k_cfg = dash_cfg.get("kanban") or {}+ return {}++[email protected]("/config")+def get_config():+ """Kanban dashboard preferences from the ``dashboard.kanban`` config section."""+ k_cfg = (_load_config_or_empty().get("dashboard") or {}).get("kanban") or {}return {"default_tenant": k_cfg.get("default_tenant") or "","lane_by_profile": bool(k_cfg.get("lane_by_profile", True)),@@ -2039,34 +1466,16 @@ def get_config():}-# ----------------------------------------------------------------------------# Home-channel subscriptions (per-task, per-platform toggles)-# ----------------------------------------------------------------------------#-# Home channels are a first-class gateway concept — each configured platform-# can have exactly one (chat_id, thread_id, name) it considers "home". The-# dashboard surfaces these as per-task toggles so a user can opt a specific-# task into receiving terminal notifications (completed / blocked / gave_up)-# at their telegram/discord/slack home, without touching the CLI.-#-# The wire format mirrors kanban_db.add_notify_sub — (task_id, platform,-# chat_id, thread_id) — so toggle-on creates exactly the same row the-# `/kanban create` slash command would, and the existing gateway notifier-# watcher delivers events without any additional plumbing.-+# --- Home-channel subscriptions (per-task, per-platform toggles) -------------+# Each gateway platform has at most one "home" (chat_id, thread_id, name). A+# toggle-on writes exactly the notify_subs row ``/kanban create`` would, so the+# existing gateway notifier delivers completed/blocked/gave_up with no extra plumbing.def _configured_home_channels() -> list[dict]:- """Return every platform that has a home_channel set, fully hydrated.-- Reads the live GatewayConfig so env-var overlays (``TELEGRAM_HOME_CHANNEL``- etc.) are honored alongside config.yaml. Returns platforms in a stable- order and drops platforms without a home.- """+ """Every platform with a home_channel, from the live GatewayConfig (so env+ overlays like ``TELEGRAM_HOME_CHANNEL`` are honored), sorted by platform."""try:from gateway.config import load_gateway_config- except Exception:- return []- try:gw_cfg = load_gateway_config()except Exception:return []@@ -2081,13 +1490,12 @@ def _configured_home_channels() -> list[dict]:"thread_id": hc.thread_id or "","name": hc.name or "Home",})- # Stable order for deterministic UI — platform name alphabetical.result.sort(key=lambda r: r["platform"])return resultdef _active_profile_name() -> str:- """Return the current Hermes profile name for notify-sub ownership."""+ """Current Hermes profile name for notify-sub ownership."""try:from hermes_cli.profiles import get_active_profile_namereturn get_active_profile_name() or "default"@@ -2095,42 +1503,28 @@ def _active_profile_name() -> str:return "default"-def _home_sub_matches(sub: dict, home: dict) -> bool:- """True if a notify_subs row corresponds to the given home channel."""- return (- sub.get("platform") == home["platform"]- and str(sub.get("chat_id", "")) == str(home["chat_id"])- and str(sub.get("thread_id") or "") == str(home["thread_id"] or "")- )+def _home_for_platform(platform: str, detail: str) -> dict:+ home = next((h for h in _configured_home_channels() if h["platform"] == platform), None)+ if not home:+ raise HTTPException(status_code=404, detail=detail)+ return home@router.get("/home-channels")-def get_home_channels(- task_id: Optional[str] = Query(None),- board: Optional[str] = Query(None),-):- """List every platform with a home channel, plus whether *task_id*- (if given) is currently subscribed to that home.-- When ``task_id`` is omitted, every entry's ``subscribed`` is ``false``- — useful for the "no task selected" state of the UI.- """+def get_home_channels(task_id: Optional[str] = Query(None), board: Optional[str] = Query(None)):+ """Every platform with a home channel plus whether *task_id* (if given) is+ subscribed to it; without ``task_id`` every ``subscribed`` is false."""homes = _configured_home_channels()subscribed_homes: set[tuple[str, str, str]] = set()if task_id:- board = _resolve_board(board)- conn = _conn(board=board)- try:+ with _board_conn(board) as (board, conn):subs = kanban_db.list_notify_subs(conn, task_id)- finally:- conn.close()for sub in subs:- key = (+ subscribed_homes.add((str(sub.get("platform") or ""),str(sub.get("chat_id") or ""),str(sub.get("thread_id") or ""),- )- subscribed_homes.add(key)+ ))result = []for home in homes:key = (home["platform"], home["chat_id"], home["thread_id"])@@ -2140,26 +1534,16 @@ def get_home_channels(@router.post("/tasks/{task_id}/home-subscribe/{platform}")def subscribe_home(task_id: str, platform: str, board: Optional[str] = Query(None)):- """Subscribe *task_id* to notifications routed to *platform*'s home channel.-- Idempotent — re-subscribing is a no-op at the DB layer. 404 if the- platform has no home channel configured. 404 if the task doesn't exist.- """- homes = _configured_home_channels()- home = next((h for h in homes if h["platform"] == platform), None)- if not home:- raise HTTPException(- status_code=404,- detail=f"No home channel configured for platform {platform!r}. "- f"Set one from the messenger via /sethome, or configure "- f"gateway.platforms.{platform}.home_channel in config.yaml.",- )- board = _resolve_board(board)- conn = _conn(board=board)- try:- task = kanban_db.get_task(conn, task_id)- if task is None:- raise HTTPException(status_code=404, detail=f"task {task_id} not found")+ """Subscribe *task_id* to *platform*'s home channel. Idempotent at the DB+ layer; 404 when the platform has no home or the task doesn't exist."""+ home = _home_for_platform(+ platform,+ f"No home channel configured for platform {platform!r}. "+ f"Set one from the messenger via /sethome, or configure "+ f"gateway.platforms.{platform}.home_channel in config.yaml.",+ )+ with _board_conn(board) as (board, conn):+ _require_task(conn, task_id)kanban_db.add_notify_sub(conn,task_id=task_id,@@ -2169,23 +1553,13 @@ def subscribe_home(task_id: str, platform: str, board: Optional[str] = Query(Nonnotifier_profile=_active_profile_name(),)return {"ok": True, "task_id": task_id, "home_channel": home}- finally:- conn.close()@router.delete("/tasks/{task_id}/home-subscribe/{platform}")def unsubscribe_home(task_id: str, platform: str, board: Optional[str] = Query(None)):- """Remove any notify subscription on *task_id* that matches *platform*'s home."""- homes = _configured_home_channels()- home = next((h for h in homes if h["platform"] == platform), None)- if not home:- raise HTTPException(- status_code=404,- detail=f"No home channel configured for platform {platform!r}.",- )- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Remove any notify subscription on *task_id* matching *platform*'s home."""+ home = _home_for_platform(platform, f"No home channel configured for platform {platform!r}.")+ with _board_conn(board) as (board, conn):kanban_db.remove_notify_sub(conn,task_id=task_id,@@ -2194,71 +1568,35 @@ def unsubscribe_home(task_id: str, platform: str, board: Optional[str] = Query(Nthread_id=home["thread_id"] or None,)return {"ok": True, "task_id": task_id, "home_channel": home}- finally:- conn.close()-# ----------------------------------------------------------------------------# Stats (per-profile / per-status counts + oldest-ready age)-# ---------------------------------------------------------------------------+# --- Stats / assignees / worker log / dispatch / model options ---------------@router.get("/stats")def get_stats(board: Optional[str] = Query(None)):- """Per-status + per-assignee counts + oldest-ready age.-- Designed for the dashboard HUD and for router profiles that need to- answer "is this specialist overloaded?" without scanning the whole- board themselves.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Per-status + per-assignee counts + oldest-ready age (HUD and router profiles)."""+ with _board_conn(board) as (board, conn):return kanban_db.board_stats(conn)- finally:- conn.close()@router.get("/assignees")def get_assignees(board: Optional[str] = Query(None)):- """Known profiles + per-profile task counts.-- Returns the union of ``~/.hermes/profiles/*`` on disk and every- distinct assignee currently used on the board. The dashboard uses- this to populate its assignee dropdown so a freshly-created profile- appears in the picker before it's been given any task.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Union of on-disk profiles and assignees used on the board, so a fresh+ profile appears in the picker before it has any task."""+ with _board_conn(board) as (board, conn):return {"assignees": kanban_db.known_assignees(conn)}- finally:- conn.close()-# ----------------------------------------------------------------------------# Worker log (read-only; file written by _default_spawn)-# ----------------------------------------------------------------------------@router.get("/tasks/{task_id}/log")def get_task_log(task_id: str,tail: Optional[int] = Query(None, ge=1, le=2_000_000),board: Optional[str] = Query(None),):- """Return the worker's stdout/stderr log.-- ``tail`` caps the response size (bytes) so the dashboard drawer- doesn't paginate megabytes into the browser. Returns 404 if the task- has never spawned. The on-disk log is rotated at 2 MiB per- ``_rotate_worker_log`` — a single ``.log.1`` is kept, no further- generations, so disk usage per task is bounded at ~4 MiB.- """- board = _resolve_board(board)- conn = _conn(board=board)- try:+ """Worker stdout/stderr log. ``tail`` caps the response bytes; 404 if the+ task never spawned. On-disk log rotates at 2 MiB with one ``.log.1`` kept."""+ with _board_conn(board) as (board, conn):task = kanban_db.get_task(conn, task_id)- finally:- conn.close()if task is None:raise HTTPException(status_code=404, detail=f"task {task_id} not found")content = kanban_db.read_worker_log(task_id, tail_bytes=tail, board=board)@@ -2270,51 +1608,33 @@ def get_task_log("exists": content is not None,"size_bytes": size,"content": content or "",- # Truncated when the on-disk file was larger than the tail cap."truncated": bool(tail and size > tail),}-# ----------------------------------------------------------------------------# Dispatch nudge (optional quick-path so the UI doesn't wait 60 s)-# ----------------------------------------------------------------------------@router.post("/dispatch")def dispatch(dry_run: bool = Query(False),max_n: int = Query(8, alias="max"),board: Optional[str] = Query(None),):- board = _resolve_board(board)- conn = _conn(board=board)- try:- result = kanban_db.dispatch_once(- conn, dry_run=dry_run, max_spawn=max_n, board=board,- )- # DispatchResult is a dataclass.+ """Dispatch nudge so the UI doesn't wait out the 60 s dispatcher tick."""+ with _board_conn(board) as (board, conn):+ result = kanban_db.dispatch_once(conn, dry_run=dry_run, max_spawn=max_n, board=board)try:- return asdict(result)+ return asdict(result) # DispatchResult is a dataclassexcept TypeError:return {"result": str(result)}- finally:- conn.close()--# ----------------------------------------------------------------------------# Model options (the board's per-task model-override dropdown)-# ---------------------------------------------------------------------------@router.get("/model-options")def model_options():- """Authenticated providers + curated model lists for the task drawer's- model-override dropdown.-- Thin wrapper around ``hermes_cli.inventory.build_models_payload`` — the- same substrate the dashboard Models page and the TUI picker use, so the- dropdown can never offer a model/provider pair the rest of Hermes- wouldn't accept. Deliberately skips pricing/capability enrichment and- custom-provider probes: the dropdown needs names fast, not $/Mtok- columns (a slow/offline local endpoint must not hang the drawer).+ """Authenticated providers + curated models for the model-override dropdown.++ Thin wrapper over ``inventory.build_models_payload`` (same substrate as the+ Models page / TUI picker) so the dropdown can't offer a pair the rest of+ Hermes rejects. Skips pricing enrichment and custom-provider probes: a+ slow/offline local endpoint must not hang the drawer."""try:from hermes_cli.inventory import build_models_payload, load_picker_context@@ -2338,14 +1658,11 @@ def model_options():}except Exception:log.exception("kanban model-options failed")- # Degrade to an empty catalog — the UI falls back to a free-text- # input so the feature still works without the inventory module.+ # Empty catalog → the UI falls back to a free-text input.return {"providers": []}-# ----------------------------------------------------------------------------# Boards CRUD (multi-project support)-# ---------------------------------------------------------------------------+# --- Boards CRUD (multi-project support) --------------------------------------class CreateBoardBody(BaseModel):slug: str@@ -2354,9 +1671,8 @@ class CreateBoardBody(BaseModel):icon: Optional[str] = Nonecolor: Optional[str] = Nonedefault_workdir: Optional[str] = None- # First-class Project (id or slug) to scope the board to. When set, the- # board's default_workdir mirrors the project's primary repo and new tasks- # inherit the project (deterministic worktree + branch).+ # Project (id or slug) scoping the board: default_workdir mirrors the+ # project's primary repo and new tasks inherit the project.project_id: Optional[str] = Noneswitch: bool = False@@ -2366,40 +1682,30 @@ class RenameBoardBody(BaseModel):description: Optional[str] = Noneicon: Optional[str] = Nonecolor: Optional[str] = None- # Board-level default project directory for new tasks. ``None`` =- # leave unchanged; empty string = clear; a path = validate + set.+ # For both fields: ``None`` = leave unchanged; "" = clear; value = validate/resolve + set.default_workdir: Optional[str] = None- # Project scope (id or slug). ``None`` = leave unchanged; empty = clear;- # a value = resolve + set (and mirror default_workdir to its primary repo).project_id: Optional[str] = None-# Board transfer exchanges filesystem PATHS, not bytes — same contract as-# profile export/import, and for the same reason: the clients that drive it-# (desktop, dashboard) run the native save/open dialog on the machine that-# hosts the backend, so a path is all either side needs.+# Board transfer exchanges filesystem PATHS, not bytes (same contract as profile+# export/import): the desktop/dashboard clients run the native save/open dialog+# on the machine hosting the backend, so a path is all either side needs.class ExportBoardBody(BaseModel):- # Where to write the archive. Empty → a staging path under the kanban root.- output: str = ""+ output: str = "" # empty → staging path under the kanban rootattachments: bool = Truelogs: bool = Falseclass ImportBoardBody(BaseModel):- # Path to a board .tar.gz on the backend's filesystem.- archive: str- # Override the slug from the archive. Collisions auto-suffix either way.- slug: Optional[str] = None+ archive: str # path to a board .tar.gz on the backend's filesystem+ slug: Optional[str] = None # override the archive's slug; collisions auto-suffixswitch: bool = Falsedef _resolve_project(ref: Optional[str]) -> tuple[Optional[str], Optional[str], Optional[str]]:- """Resolve a project id/slug to ``(id, name, primary_path)``.-- Returns ``(None, None, None)`` for a falsy ref. Raises 400 when a- non-empty ref doesn't resolve to an existing project.- """+ """Resolve a project id/slug to ``(id, name, primary_path)``; ``(None,)*3``+ for a falsy ref, 400 when a non-empty ref doesn't resolve."""if not ref or not ref.strip():return None, None, Nonetry:@@ -2424,7 +1730,7 @@ def _projects_by_id() -> dict[str, Any]:def _board_counts(slug: str) -> dict[str, int]:- """Return ``{status: count}`` for a board. Safe on an empty DB."""+ """``{status: count}`` for a board; ``{}`` on a missing/empty DB."""try:path = kanban_db.kanban_db_path(board=slug)if not path.exists():@@ -2452,13 +1758,15 @@ def _default_workspace_kind(board: dict[str, Any]) -> str:return "dir"+def _annotate_board_meta(meta: dict) -> dict:+ meta["default_workspace_kind"] = _default_workspace_kind(meta)+ _, meta["project_name"], _ = _resolve_project(meta.get("project_id"))+ return meta++@router.get("/projects")def list_kanban_projects():- """List first-class Hermes projects for board scoping.-- Returns ``{projects: [{id, slug, name, primary_path, icon, color}]}``.- Archived projects are excluded — a board can only be scoped to a live one.- """+ """Live (non-archived) projects available for board scoping."""try:from hermes_cli import projects_db as pdbwith pdb.connect_closing() as pconn:@@ -2482,19 +1790,16 @@ def list_kanban_projects():@router.get("/boards")def list_boards(include_archived: bool = Query(False)):- """Return every board on disk with task counts and the active slug."""+ """Every board on disk with task counts and the active slug."""boards = kanban_db.list_boards(include_archived=include_archived)current = kanban_db.get_current_board()proj_map = _projects_by_id()for b in boards:b["is_current"] = (b["slug"] == current)b["counts"] = _board_counts(b["slug"])- # Live cards only — archived tasks are hidden from every default- # board view, so advertising them in the switcher badge makes the- # two counts visibly disagree.- b["total"] = sum(- n for status, n in b["counts"].items() if status != "archived"- )+ # Live cards only — archived tasks are hidden from every default board+ # view, so counting them in the switcher badge would visibly disagree.+ b["total"] = sum(n for status, n in b["counts"].items() if status != "archived")b["default_workspace_kind"] = _default_workspace_kind(b)pid = b.get("project_id") or Noneb["project_id"] = pid@@ -2504,37 +1809,27 @@ def list_boards(include_archived: bool = Query(False)):def _validate_workdir(raw: str) -> str:- """Validate a board default_workdir value; return the resolved path.-- Raises :class:`HTTPException` (400) for relative or non-directory- paths — mirroring the create-board contract.- """+ """Board default_workdir must be an absolute, existing directory (400 otherwise)."""requested = Path(raw).expanduser()if not requested.is_absolute():- raise HTTPException(- status_code=400,- detail="Project directory must be an absolute path.",- )+ raise HTTPException(status_code=400, detail="Project directory must be an absolute path.")if not requested.is_dir():- raise HTTPException(- status_code=400,- detail="Project directory must be an existing directory.",- )+ raise HTTPException(status_code=400, detail="Project directory must be an existing directory.")return str(requested.resolve())@router.post("/boards")def create_board_endpoint(payload: CreateBoardBody):- """Create a new board. Idempotent — ``slug`` collision returns existing."""+ """Create a board. Idempotent — ``slug`` collision returns the existing one."""default_workdir = Noneif payload.default_workdir:default_workdir = _validate_workdir(payload.default_workdir)- # A chosen project scopes the board: its primary repo becomes the default- # workdir (unless one was passed explicitly) and the link is stored.+ # A chosen project's primary repo becomes the default workdir unless one was+ # passed explicitly.project_id, _pname, primary_path = _resolve_project(payload.project_id)if primary_path and not default_workdir:default_workdir = primary_path- try:+ with _value_error_400():meta = kanban_db.create_board(payload.slug,name=payload.name,@@ -2544,40 +1839,27 @@ def create_board_endpoint(payload: CreateBoardBody):default_workdir=default_workdir,project_id=project_id,)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))if payload.switch:- try:+ with _value_error_400():kanban_db.set_current_board(meta["slug"])- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- meta["default_workspace_kind"] = _default_workspace_kind(meta)- _, meta["project_name"], _ = _resolve_project(meta.get("project_id"))- return {"board": meta, "current": kanban_db.get_current_board()}+ return {"board": _annotate_board_meta(meta), "current": kanban_db.get_current_board()}@router.patch("/boards/{slug}")def rename_board(slug: str, payload: RenameBoardBody):- """Update a board's display metadata + default project directory (slug is immutable — create a new one to rename the directory)."""- try:- normed = kanban_db._normalize_board_slug(slug)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- if not normed or not kanban_db.board_exists(normed):- raise HTTPException(status_code=404, detail=f"board {slug!r} does not exist")- # default_workdir: None = leave unchanged; "" = clear; path = validate + set.+ """Update display metadata / default workdir / project scope (slug is immutable)."""+ normed = _existing_board_slug(slug)# write_board_metadata treats a falsy value as "clear", so pass "" through.default_workdir: Optional[str] = Noneif payload.default_workdir is not None:raw = payload.default_workdir.strip()default_workdir = _validate_workdir(raw) if raw else ""- # project_id: None = leave; "" = clear; value = resolve + mirror its repo- # into default_workdir (unless the caller set default_workdir explicitly).+ # A resolved project mirrors its repo into default_workdir unless the caller+ # set default_workdir explicitly.project_id: Optional[str] = None- project_name: Optional[str] = Noneif payload.project_id is not None:if payload.project_id.strip():- project_id, project_name, primary_path = _resolve_project(payload.project_id)+ project_id, _pname, primary_path = _resolve_project(payload.project_id)if primary_path and default_workdir is None:default_workdir = primary_pathelse:@@ -2591,19 +1873,29 @@ def rename_board(slug: str, payload: RenameBoardBody):default_workdir=default_workdir,project_id=project_id,)- meta["default_workspace_kind"] = _default_workspace_kind(meta)- _, meta["project_name"], _ = _resolve_project(meta.get("project_id"))- return {"board": meta}+ return {"board": _annotate_board_meta(meta)}@router.delete("/boards/{slug}")def delete_board(slug: str, delete: bool = Query(False, description="Hard-delete instead of archive")):"""Archive (default) or hard-delete a board."""- try:+ with _value_error_400():res = kanban_db.remove_board(slug, archive=not delete)+ return {"result": res, "current": kanban_db.get_current_board()}+++async def _run_transfer(fn, log_label: str):+ """Run a blocking kanban_transfer call off the event loop, mapping its errors+ to 404 (missing path) / 400 (invalid) / 500 (logged)."""+ try:+ return await asyncio.get_running_loop().run_in_executor(None, fn)+ except FileNotFoundError as exc:+ raise HTTPException(status_code=404, detail=str(exc))except ValueError as exc:raise HTTPException(status_code=400, detail=str(exc))- return {"result": res, "current": kanban_db.get_current_board()}+ except Exception as exc:+ log.exception("%s failed", log_label)+ raise HTTPException(status_code=500, detail=str(exc))@router.post("/boards/{slug}/export")@@ -2617,30 +1909,18 @@ async def export_board_endpoint(slug: str, body: ExportBoardBody):try:staging.mkdir(parents=True, exist_ok=True)except OSError as exc:- raise HTTPException(- status_code=500, detail=f"Could not create export directory: {exc}"- )+ raise HTTPException(status_code=500, detail=f"Could not create export directory: {exc}")stamp = time.strftime("%Y%m%d-%H%M%S")output = str(staging / f"{slug}-{stamp}.tar.gz")- loop = asyncio.get_running_loop()- try:- result = await loop.run_in_executor(- None,- lambda: kanban_transfer.export_board(- slug, output,- include_attachments=body.attachments,- include_logs=body.logs,- ),- )- except FileNotFoundError as exc:- raise HTTPException(status_code=404, detail=str(exc))- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- except Exception as exc:- log.exception("POST /boards/%s/export failed", slug)- raise HTTPException(status_code=500, detail=str(exc))- return result+ return await _run_transfer(+ lambda: kanban_transfer.export_board(+ slug, output,+ include_attachments=body.attachments,+ include_logs=body.logs,+ ),+ f"POST /boards/{slug}/export",+ )@router.post("/boards/import")@@ -2652,55 +1932,30 @@ async def import_board_endpoint(body: ImportBoardBody):if not archive:raise HTTPException(status_code=400, detail="archive path is required")- loop = asyncio.get_running_loop()- try:- result = await loop.run_in_executor(- None,- lambda: kanban_transfer.import_board(- archive, (body.slug or "").strip() or None, activate=body.switch- ),- )- except FileNotFoundError as exc:- raise HTTPException(status_code=404, detail=str(exc))- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- except Exception as exc:- log.exception("POST /boards/import failed")- raise HTTPException(status_code=500, detail=str(exc))+ result = await _run_transfer(+ lambda: kanban_transfer.import_board(+ archive, (body.slug or "").strip() or None, activate=body.switch+ ),+ "POST /boards/import",+ )return {**result, "current": kanban_db.get_current_board()}@router.post("/boards/{slug}/switch")def switch_board(slug: str):- """Persist ``slug`` as the active board for subsequent CLI / slash calls.-- Dashboard users pick boards via a client-side ``localStorage`` — this- endpoint is for ``/kanban boards switch`` parity so gateway slash- commands and the CLI share the same current-board pointer.- """- try:- normed = kanban_db._normalize_board_slug(slug)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc))- if not normed or not kanban_db.board_exists(normed):- raise HTTPException(status_code=404, detail=f"board {slug!r} does not exist")+ """Persist ``slug`` as the active board for CLI / slash-command parity+ (dashboard users pick boards client-side via localStorage)."""+ normed = _existing_board_slug(slug)kanban_db.set_current_board(normed)return {"current": normed}-# ----------------------------------------------------------------------------# WebSocket: /events?since=<event_id>-# -----------------------------------------------------------------------------# Poll interval for the event tail loop. SQLite WAL + 300 ms polling is-# the simplest and most robust approach; it adds a fraction of a percent-# of CPU and has no shared state to synchronize across workers.+# Poll interval for the event tail loop. SQLite WAL + 300 ms polling is the+# simplest robust approach: negligible CPU, no shared state across workers._EVENT_POLL_SECONDS = 0.3-# ----------------------------------------------------------------------------# Profile metadata & description editing (consumed by the kanban orchestrator)-# ---------------------------------------------------------------------------+# --- Profile metadata & description editing (kanban orchestrator) ------------class DescribeBody(BaseModel):description: Optional[str] = None # explicit user-authored text@@ -2712,13 +1967,8 @@ class DescribeAutoBody(BaseModel):@router.get("/profiles")def list_profile_roster():- """Return every installed profile with its description.-- Consumed by the dashboard's settings panel (orchestrator picker)- and the profile-description editing UI. Profiles without a- description still appear here — they're routable on name alone,- just less precisely.- """+ """Every installed profile with its description (profiles without one are+ still routable on name alone, just less precisely)."""try:from hermes_cli import profiles as profiles_modprofiles = profiles_mod.list_profiles()@@ -2742,30 +1992,20 @@ def list_profile_roster():@router.patch("/profiles/{profile_name}")def update_profile_description(profile_name: str, payload: DescribeBody):- """Set or clear the description of a profile.-- Empty string clears the description; non-empty stores it as a- user-authored description (``description_auto: false``) so the- auto-describer won't overwrite it on a sweep without- ``--overwrite``.- """+ """Set (``description_auto: false`` so the auto-describer won't overwrite it+ without ``--overwrite``) or clear (empty string) a profile's description."""try:from hermes_cli import profiles as profiles_modcanon = profiles_mod.normalize_profile_name(profile_name)if canon == "default":from hermes_constants import get_hermes_home # type: ignore- from pathlib import Path as _Path- profile_dir = _Path(get_hermes_home())+ profile_dir = Path(get_hermes_home())else:profile_dir = profiles_mod.get_profile_dir(canon)if not profile_dir.is_dir():raise HTTPException(status_code=404, detail=f"profile '{profile_name}' not found")text = (payload.description or "").strip()- profiles_mod.write_profile_meta(- profile_dir,- description=text,- description_auto=False,- )+ profiles_mod.write_profile_meta(profile_dir, description=text, description_auto=False)except HTTPException:raiseexcept Exception as exc:@@ -2775,18 +2015,11 @@ def update_profile_description(profile_name: str, payload: DescribeBody):@router.post("/profiles/{profile_name}/describe-auto")def auto_describe_profile(profile_name: str, payload: DescribeAutoBody):- """Generate a description for the named profile via the auxiliary- LLM (``auxiliary.profile_describer``). Persists with- ``description_auto: true`` so the dashboard can surface a "review"- badge.-- Maps 1:1 to ``hermes profile describe <name> --auto``. Non-OK- outcomes are NOT HTTP errors — the UI renders the reason inline- (e.g. "no auxiliary client configured") so the operator can fix- config and retry without a page reload.- """+ """Generate a description via ``auxiliary.profile_describer`` and persist it+ with ``description_auto: true`` (``hermes profile describe <name> --auto``).+ Non-OK outcomes are NOT HTTP errors — the UI renders the reason inline."""try:- from hermes_cli import profile_describer # noqa: WPS433 (intentional)+ from hermes_cli import profile_describeroutcome = profile_describer.describe_profile(profile_name,overwrite=bool(payload.overwrite),@@ -2801,9 +2034,7 @@ def auto_describe_profile(profile_name: str, payload: DescribeAutoBody):}-# ----------------------------------------------------------------------------# Decompose endpoint (built-in decomposer fan-out)-# ---------------------------------------------------------------------------+# --- Decompose (built-in decomposer fan-out) ----------------------------------class DecomposeBody(BaseModel):author: Optional[str] = None@@ -2815,28 +2046,15 @@ def decompose_task_endpoint(payload: DecomposeBody,board: Optional[str] = Query(None),):- """Fan a triage-column task out into a graph of child tasks via the- auxiliary LLM, routed to specialist profiles by description. Maps- 1:1 to ``hermes kanban decompose <task_id>``.-- Returns the outcome shape used by the CLI: ``{ok, task_id, reason,- fanout, child_ids, new_title}``. A non-OK outcome is NOT an HTTP- error — the UI renders the reason inline.-- Runs in FastAPI's threadpool (sync ``def``) because the LLM call- can take minutes on reasoning models.- """+ """Fan a triage task out into child tasks via the auxiliary LLM, routed to+ specialist profiles by description (``hermes kanban decompose``). Returns+ ``{ok, task_id, reason, fanout, child_ids, new_title}``; non-OK is NOT an+ HTTP error. Sync ``def`` so the slow LLM call runs in the threadpool."""board = _resolve_board(board)- # Context-local board pin (see specify endpoint above): this sync- # endpoint runs in FastAPI's threadpool, so mutating the process-global- # HERMES_KANBAN_BOARD env var would let concurrent requests for- # different boards race and cross-write (issue #38323).+ # Context-local board pin — see specify_task_endpoint for the race rationale.with kanban_db.scoped_current_board(board or kanban_db.DEFAULT_BOARD):- from hermes_cli import kanban_decompose # noqa: WPS433 (intentional)- outcome = kanban_decompose.decompose_task(- task_id,- author=(payload.author or None),- )+ from hermes_cli import kanban_decompose+ outcome = kanban_decompose.decompose_task(task_id, author=(payload.author or None))return {"ok": bool(outcome.ok),@@ -2848,10 +2066,8 @@ def decompose_task_endpoint(}-# ----------------------------------------------------------------------------# Orchestration settings (kanban.orchestrator_profile / default_assignee /-# auto_decompose) — surfaced to the dashboard's settings panel-# ---------------------------------------------------------------------------+# --- Orchestration settings (kanban.orchestrator_profile / default_assignee /+# auto_decompose / auto_promote_children) ----------------------------------class OrchestrationSettingsBody(BaseModel):orchestrator_profile: Optional[str] = None@@ -2862,20 +2078,15 @@ class OrchestrationSettingsBody(BaseModel):@router.get("/orchestration")def get_orchestration_settings():- """Return the current kanban orchestration knobs from config.yaml- plus the resolved effective values (filling in fallbacks)."""- try:- from hermes_cli.config import load_config- cfg = load_config() or {}- except Exception:- cfg = {}+ """Current orchestration knobs from config.yaml plus the resolved effective+ values (fallbacks filled the same way the decomposer does)."""+ cfg = _load_config_or_empty()kanban_cfg = (cfg.get("kanban") or {}) if isinstance(cfg, dict) else {}explicit_orch = (kanban_cfg.get("orchestrator_profile") or "").strip()explicit_default = (kanban_cfg.get("default_assignee") or "").strip()auto_decompose = bool(kanban_cfg.get("auto_decompose", True))auto_promote_children = bool(kanban_cfg.get("auto_promote_children", True))- # Resolve fallbacks the same way the decomposer does.resolved_orch = explicit_orchresolved_default = explicit_defaulttry:@@ -2887,10 +2098,8 @@ def get_orchestration_settings():resolved_default = active_defaultexcept Exception:active_default = "default"- if not resolved_orch:- resolved_orch = active_default- if not resolved_default:- resolved_default = active_default+ resolved_orch = resolved_orch or active_default+ resolved_default = resolved_default or active_defaultreturn {"orchestrator_profile": explicit_orch,@@ -2903,15 +2112,25 @@ def get_orchestration_settings():}+def _validated_profile_name(raw: Optional[str], profiles_mod) -> str:+ """Strip a profile name; 400 if non-empty and unknown. Fails open when the+ lookup itself errors."""+ name = (raw or "").strip()+ if name and profiles_mod is not None:+ try:+ if not profiles_mod.profile_exists(name):+ raise HTTPException(status_code=400, detail=f"profile '{name}' does not exist")+ except HTTPException:+ raise+ except Exception:+ pass+ return name++@router.put("/orchestration")def set_orchestration_settings(payload: OrchestrationSettingsBody):- """Update the kanban orchestration knobs in ~/.hermes/config.yaml.-- Each field is optional — only fields explicitly passed are- written. ``orchestrator_profile`` / ``default_assignee`` accept- empty strings to clear the override and fall back to the default- profile.- """+ """Update orchestration knobs in config.yaml. Only fields explicitly passed+ are written; empty profile strings clear the override."""try:from hermes_cli.config import load_config, save_configcfg = load_config() or {}@@ -2923,45 +2142,21 @@ def set_orchestration_settings(payload: OrchestrationSettingsBody):kanban_section = {}cfg["kanban"] = kanban_section- # Validate any non-empty profile names exist before saving.try:from hermes_cli import profiles as profiles_modexcept Exception:profiles_mod = None # type: ignoreif payload.orchestrator_profile is not None:- name = (payload.orchestrator_profile or "").strip()- if name and profiles_mod is not None:- try:- if not profiles_mod.profile_exists(name):- raise HTTPException(- status_code=400,- detail=f"profile '{name}' does not exist",- )- except HTTPException:- raise- except Exception:- pass # fail open if the lookup itself errors- kanban_section["orchestrator_profile"] = name-+ kanban_section["orchestrator_profile"] = _validated_profile_name(+ payload.orchestrator_profile, profiles_mod,+ )if payload.default_assignee is not None:- name = (payload.default_assignee or "").strip()- if name and profiles_mod is not None:- try:- if not profiles_mod.profile_exists(name):- raise HTTPException(- status_code=400,- detail=f"profile '{name}' does not exist",- )- except HTTPException:- raise- except Exception:- pass- kanban_section["default_assignee"] = name-+ kanban_section["default_assignee"] = _validated_profile_name(+ payload.default_assignee, profiles_mod,+ )if payload.auto_decompose is not None:kanban_section["auto_decompose"] = bool(payload.auto_decompose)-if payload.auto_promote_children is not None:kanban_section["auto_promote_children"] = bool(payload.auto_promote_children)@@ -2970,27 +2165,21 @@ def set_orchestration_settings(payload: OrchestrationSettingsBody):except Exception as exc:raise HTTPException(status_code=500, detail=f"failed to save config: {exc}")- # Echo back the resolved state (callers usually re-render from it).- return get_orchestration_settings()+ return get_orchestration_settings() # callers re-render from the resolved state++# --- WebSocket: /events?since=<event_id>&board=<slug> ------------------------@router.websocket("/events")async def stream_events(ws: WebSocket):- # Authorize the upgrade via the dashboard's canonical WS gate so the- # correct credential is accepted in every mode (loopback token / gated- # single-use ticket / server-internal credential). Browsers can't set- # Authorization on a WS upgrade, so the credential rides in the query- # string — the browser SDK's buildWsUrl() assembles it.if not _ws_upgrade_authorized(ws):await ws.close(code=http_status.WS_1008_POLICY_VIOLATION)returnawait ws.accept()- # Keep one connection alive for this socket after its first poll. SQLite- # connections are thread-affine by default, so every operation (including- # close) runs on the same dedicated worker. Besides preserving that safety- # contract, this avoids repeatedly creating and deleting the WAL/SHM- # sidecars while an idle dashboard polls for events.+ # One SQLite connection per socket, used (and closed) only on a dedicated+ # single worker thread: sqlite connections are thread-affine, and reusing it+ # avoids churning WAL/SHM sidecars while an idle dashboard polls.event_conn: Optional[sqlite3.Connection] = Noneevent_executor: Optional[ThreadPoolExecutor] = None@@ -3001,16 +2190,13 @@ async def stream_events(ws: WebSocket):event_conn = Nonetry:- since_raw = ws.query_params.get("since", "0")try:- cursor = int(since_raw)+ cursor = int(ws.query_params.get("since", "0"))except ValueError:cursor = 0- # Board selection — pinned at the WS handshake; re-subscribe to- # switch boards. Changing boards mid-stream would require- # reconciling two cursors, so the UI just opens a new WS on- # board change.+ # Board is pinned at the handshake; the UI opens a new WS on board change+ # rather than reconciling two cursors mid-stream.ws_board_raw = ws.query_params.get("board")try:ws_board = kanban_db._normalize_board_slug(ws_board_raw) if ws_board_raw else None@@ -3033,30 +2219,19 @@ async def stream_events(ws: WebSocket):payload = json.loads(r["payload"]) if r["payload"] else Noneexcept Exception:payload = None- out.append({- "id": r["id"],- "task_id": r["task_id"],- "run_id": r["run_id"],- "kind": r["kind"],- "payload": payload,- "created_at": r["created_at"],- })+ out.append({**dict(r), "payload": payload})new_cursor = r["id"]return new_cursor, outwhile True:- # Race receive() against the poll interval to detect client- # disconnect even when no events are being sent. Without this,- # a disconnect is only detected via send_json() raising- # WebSocketDisconnect, so an idle board leaks zombie poll tasks.+ # Race receive() against the poll interval so a client disconnect is+ # detected even when no events are flowing; otherwise an idle board+ # leaks zombie poll tasks until the next send_json() fails.try:- msg = await asyncio.wait_for(- ws.receive(), timeout=_EVENT_POLL_SECONDS- )+ msg = await asyncio.wait_for(ws.receive(), timeout=_EVENT_POLL_SECONDS)if msg["type"] == "websocket.disconnect":return- # Any other client message (pong, text) is ignored; we- # continue polling.+ # Other client messages (pong, text) are ignored.except asyncio.TimeoutError:pass # no client message — poll the DB@@ -3075,13 +2250,11 @@ async def stream_events(ws: WebSocket):except WebSocketDisconnect:returnexcept asyncio.CancelledError:- # Normal shutdown path: dashboard process exit (Ctrl-C) cancels the- # websocket task while it is sleeping in the poll loop.- # CancelledError is a BaseException in 3.8+ so the bare Exception- # handler below would not catch it; without this clause Uvicorn- # surfaces the cancellation as an application traceback. Quiet it.+ # Normal shutdown (Ctrl-C cancels the task mid-poll). CancelledError is a+ # BaseException, so the Exception handler below wouldn't quiet it and+ # Uvicorn would print an application traceback.return- except Exception as exc: # defensive: never crash the dashboard worker+ except Exception as exc: # never crash the dashboard workerlog.warning("Kanban event stream error: %s", exc)try:await ws.close()@@ -3090,10 +2263,7 @@ async def stream_events(ws: WebSocket):finally:if event_executor is not None:try:- await asyncio.get_running_loop().run_in_executor(- event_executor,- _close_event_conn,- )+ await asyncio.get_running_loop().run_in_executor(event_executor, _close_event_conn)except Exception as exc:log.warning("Kanban event stream connection cleanup failed: %s", exc)finally: