diff --git a/hermes_cli/web_routers/actions.py b/hermes_cli/web_routers/actions.py index c135e7219f..7757b2366c 100644 --- a/hermes_cli/web_routers/actions.py +++ b/hermes_cli/web_routers/actions.py @@ -143,6 +143,24 @@ async def restart_gateway(profile: Optional[str] = None): return {"ok": True, "pid": proc.pid, "name": "gateway-restart"} +@router.get("/api/gateway/migrate/plan") +async def gateway_migrate_plan(): + """Preflight for folding per-profile gateways into one multiplexer (same JSON as the CLI plan).""" + from hermes_cli.gateway_migrate import build_migration_plan + plan = await asyncio.to_thread(build_migration_plan) + return plan.to_dict() + + +@router.post("/api/gateway/migrate") +async def gateway_migrate(): + """Run ``hermes gateway migrate --multiplex --yes`` detached; the CLI re-runs the preflight and + refuses (exit 1 into the action log) when blocked, so the UI should gate on the plan first.""" + from hermes_cli.web_server_gateway import _spawn_hermes_action + with http_failure("Failed to spawn gateway migrate", 500, "Failed to start gateway migration"): + proc = _spawn_hermes_action(["gateway", "migrate", "--multiplex", "--yes"], "gateway-migrate") + return {"ok": True, "pid": proc.pid, "name": "gateway-migrate"} + + @router.post("/api/gateway/drain") async def gateway_drain(request: Request): """Begin or cancel an external (NAS-driven) gateway drain. diff --git a/hermes_cli/web_server_gateway.py b/hermes_cli/web_server_gateway.py index 9c9d442962..e7763b8af9 100644 --- a/hermes_cli/web_server_gateway.py +++ b/hermes_cli/web_server_gateway.py @@ -262,6 +262,7 @@ _ACTION_LOG_FILES: Dict[str, str] = { "gateway-restart": "gateway-restart.log", "gateway-start": "gateway-start.log", "gateway-stop": "gateway-stop.log", + "gateway-migrate": "gateway-migrate.log", "hermes-update": "hermes-update.log", **{name: f"action-{name}.log" for name in ( "doctor", "security-audit", "backup", "import", "checkpoints-prune", "skills-install", diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts index bf38969ceb..a5275a6ac4 100644 --- a/web/src/lib/api.ts +++ b/web/src/lib/api.ts @@ -956,6 +956,10 @@ export const api = { // Gateway / update actions restartGateway: () => fetchJSON("/api/gateway/restart", { method: "POST" }), + getGatewayMigratePlan: () => + fetchJSON("/api/gateway/migrate/plan"), + migrateGatewayToMultiplex: () => + fetchJSON("/api/gateway/migrate", { method: "POST" }), updateHermes: () => fetchJSON("/api/hermes/update", { method: "POST" }), checkHermesUpdate: (force = false) => @@ -1355,6 +1359,16 @@ export interface AuthMeResponse { expires_at: number; } +/** Preflight for `hermes gateway migrate --multiplex` (mirrors the CLI plan JSON). */ +export interface GatewayMigratePlan { + already_multiplexed: boolean; + blockers: string[]; + command: string; + eligible: boolean; + notices: string[]; + profiles: { profile: string; pid: number | null; service: { kind: string; system: boolean } | null }[]; +} + export interface ActionResponse { archive?: string; name: string; diff --git a/web/src/pages/SystemPage.tsx b/web/src/pages/SystemPage.tsx index ff52c16ca9..d70ec26203 100644 --- a/web/src/pages/SystemPage.tsx +++ b/web/src/pages/SystemPage.tsx @@ -59,6 +59,7 @@ import type { CuratorStatus, PortalStatus, DebugShareResponse, + GatewayMigratePlan, } from "@/lib/api"; function formatBytes(n: number): string { @@ -207,6 +208,7 @@ export default function SystemPage() { const [activeAction, setActiveAction] = useState(null); const [consoleOpen, setConsoleOpen] = useState(false); + const [migratePlan, setMigratePlan] = useState(null); // Add-credential form. const [credProvider, setCredProvider] = useState("openrouter"); @@ -266,8 +268,9 @@ export default function SystemPage() { // Cached (non-forced) check so the version row shows update status on // load without a separate effect / a forced network round-trip. api.checkHermesUpdate(false), + api.getGatewayMigratePlan(), ]) - .then(([s, st, m, p, c, h, cur, prt, upd]) => { + .then(([s, st, m, p, c, h, cur, prt, upd, mig]) => { if (s.status === "fulfilled") setStatus(s.value); if (st.status === "fulfilled") setStats(st.value); if (m.status === "fulfilled") setMemory(m.value); @@ -277,6 +280,7 @@ export default function SystemPage() { if (cur.status === "fulfilled") setCurator(cur.value); if (prt.status === "fulfilled") setPortal(prt.value); if (upd.status === "fulfilled") setUpdateInfo(upd.value); + if (mig.status === "fulfilled") setMigratePlan(mig.value); }) .finally(() => setLoading(false)); }, []); @@ -305,6 +309,17 @@ export default function SystemPage() { } }; + const migrateToMultiplex = async () => { + try { + await api.migrateGatewayToMultiplex(); + setActiveAction("gateway-migrate"); + showToast("Migrating to a single multiplexed gateway", "success"); + setTimeout(loadAll, 5000); + } catch (e) { + showToast(`Gateway migration failed: ${e}`, "error"); + } + }; + // ── Curator ──────────────────────────────────────────────────────── const toggleCuratorPaused = async () => { if (!curator) return; @@ -1081,6 +1096,29 @@ export default function SystemPage() { + {migratePlan && !migratePlan.already_multiplexed && migratePlan.profiles.length > 1 && ( + migratePlan.eligible || migratePlan.blockers.length > 0 + ) && ( + +
+ + Your profiles each run their own gateway. One multiplexed gateway serves every profile from a single process. + + +
+ {migratePlan.blockers.map((b) => ( +
• {b}
+ ))} +
+ )}