Repository navigation
fix(tunnel): stop agent gateway forwards on shutdown #6450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7b837e1
fix(tunnel): stop agent gateway forwards on shutdown
rluo8 7cd24b8
fix(tunnel): stop agent gateway forwards on shutdown
rluo8 2b47214
fix(tunnel): stop agent gateway forwards on shutdown
rluo8 29f6ddf
chore: merge main into fix/6392-nemohermes-stop-forward
ericksoa 41033af
fix(tunnel): integrate hardened gateway shutdown
ericksoa bc74d0c
chore: merge main into fix/6392-nemohermes-stop-forward
ericksoa 01b2fc7
test(dcode): keep image suite within size budget
ericksoa d06b7b2
chore: merge main into fix/6392-nemohermes-stop-forward
ericksoa c0c13f0
fix(tunnel): use target registry for gateway stop
ericksoa 6bbf4ff
chore: merge main into fix/6392-nemohermes-stop-forward
ericksoa 488f3ba
fix(tunnel): tolerate unreadable sandbox registry
cjagwani 32a0451
fix(tunnel): keep stop cleanup registry-scoped
ericksoa 6f34b33
fix(agent): validate registered agent names
ericksoa 5cc9044
chore: merge main into fix/6392-nemohermes-stop-forward
ericksoa 3253c81
fix(tunnel): validate forward cleanup sandbox names
ericksoa 8e45e36
fix(stop): fail closed on invalid sandbox pid cleanup
ericksoa e5191a1
Merge origin/main into fix/6392-nemohermes-stop-forward
ericksoa 453700b
Merge origin/main into fix/6392-nemohermes-stop-forward
ericksoa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { stopAgentForwardPortsForStop } from "./agent-forward-stop"; | ||
|
|
||
| function forwardList(entries: Array<{ sandbox: string; port: number; status?: string }>): string { | ||
| return [ | ||
| "SANDBOX BIND PORT PID STATUS", | ||
| ...entries.map( | ||
| (entry, index) => | ||
| `${entry.sandbox} 127.0.0.1 ${String(entry.port)} ${String(1000 + index)} ${ | ||
| entry.status ?? "running" | ||
| }`, | ||
| ), | ||
| ].join("\n"); | ||
| } | ||
|
|
||
| describe("stopAgentForwardPortsForStop", () => { | ||
| it("stops declared and runtime dashboard forwards with sandbox-scoped commands", () => { | ||
| const runOpenshell = vi.fn(); | ||
| const runCaptureOpenshell = vi.fn(() => | ||
| forwardList([ | ||
| { sandbox: "nemohermes", port: 18789 }, | ||
| { sandbox: "nemohermes", port: 8642 }, | ||
| { sandbox: "nemohermes", port: 18792 }, | ||
| ]), | ||
| ); | ||
| const info = vi.fn<(message: string) => void>(); | ||
|
|
||
| stopAgentForwardPortsForStop("nemohermes", { | ||
| getSessionAgent: () => ({ | ||
| displayName: "Hermes Agent", | ||
| forward_ports: [18789, 8642, "8642", 0, 80, 70000, "not-a-port"], | ||
| }), | ||
| getAgentDisplayName: (agent) => agent?.displayName ?? "OpenClaw", | ||
| getSandbox: () => ({ dashboardPort: 18792 }), | ||
| resolveOpenshell: () => "/usr/local/bin/openshell", | ||
| runOpenshell, | ||
| runCaptureOpenshell, | ||
| info, | ||
| }); | ||
|
|
||
| expect(runOpenshell).toHaveBeenCalledTimes(3); | ||
| expect(runOpenshell).toHaveBeenNthCalledWith(1, ["forward", "stop", "18789", "nemohermes"], { | ||
| ignoreError: true, | ||
| suppressOutput: true, | ||
| }); | ||
| expect(runOpenshell).toHaveBeenNthCalledWith(2, ["forward", "stop", "8642", "nemohermes"], { | ||
| ignoreError: true, | ||
| suppressOutput: true, | ||
| }); | ||
| expect(runOpenshell).toHaveBeenNthCalledWith(3, ["forward", "stop", "18792", "nemohermes"], { | ||
| ignoreError: true, | ||
| suppressOutput: true, | ||
| }); | ||
| expect(runCaptureOpenshell).toHaveBeenCalledTimes(3); | ||
| expect(info.mock.calls.map((call) => call[0]).join("\n")).toContain( | ||
| "Stopped Hermes Agent host port forward 8642", | ||
| ); | ||
| }); | ||
|
|
||
| it("skips OpenClaw or agents without declared forwards", () => { | ||
| const resolveOpenshell = vi.fn(() => "/usr/local/bin/openshell"); | ||
| const runOpenshell = vi.fn(); | ||
|
|
||
| stopAgentForwardPortsForStop("openclaw-sandbox", { | ||
| getSessionAgent: () => null, | ||
| resolveOpenshell, | ||
| runOpenshell, | ||
| }); | ||
|
|
||
| stopAgentForwardPortsForStop("empty-agent", { | ||
| getSessionAgent: () => ({ displayName: "Empty Agent", forward_ports: [] }), | ||
| getSandbox: () => null, | ||
| resolveOpenshell, | ||
| runOpenshell, | ||
| }); | ||
|
|
||
| expect(resolveOpenshell).not.toHaveBeenCalled(); | ||
| expect(runOpenshell).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("leaves forwards alone when OpenShell reports a different owner", () => { | ||
| const runOpenshell = vi.fn(); | ||
| const warn = vi.fn<(message: string) => void>(); | ||
|
|
||
| stopAgentForwardPortsForStop("nemohermes", { | ||
| getSessionAgent: () => ({ displayName: "Hermes Agent", forward_ports: [8642] }), | ||
| getAgentDisplayName: (agent) => agent?.displayName ?? "OpenClaw", | ||
| getSandbox: () => null, | ||
| resolveOpenshell: () => "/usr/local/bin/openshell", | ||
| runOpenshell, | ||
| runCaptureOpenshell: () => forwardList([{ sandbox: "other-sandbox", port: 8642 }]), | ||
| warn, | ||
| }); | ||
|
|
||
| expect(runOpenshell).not.toHaveBeenCalled(); | ||
| expect(warn.mock.calls.map((call) => call[0]).join("\n")).toContain( | ||
| "belongs to another sandbox", | ||
| ); | ||
| }); | ||
|
|
||
| it("does not stop a forward when ownership cannot be enumerated", () => { | ||
| const runOpenshell = vi.fn(); | ||
| const warn = vi.fn<(message: string) => void>(); | ||
|
|
||
| stopAgentForwardPortsForStop("nemohermes", { | ||
| getSessionAgent: () => ({ displayName: "Hermes Agent", forward_ports: [8642] }), | ||
| getAgentDisplayName: (agent) => agent?.displayName ?? "OpenClaw", | ||
| getSandbox: () => null, | ||
| resolveOpenshell: () => "/usr/local/bin/openshell", | ||
| runOpenshell, | ||
| runCaptureOpenshell: () => { | ||
| throw new Error("forward list failed"); | ||
| }, | ||
| warn, | ||
| }); | ||
|
|
||
| expect(runOpenshell).not.toHaveBeenCalled(); | ||
| expect(warn.mock.calls.map((call) => call[0]).join("\n")).toContain( | ||
| "Could not enumerate OpenShell forwards", | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { spawnSync } from "node:child_process"; | ||
|
|
||
| import { resolveOpenshell } from "../adapters/openshell/resolve"; | ||
| import * as agentRuntime from "../agent/runtime"; | ||
| import { | ||
| bestEffortForwardStopForSandbox, | ||
| type ForwardListRunner, | ||
| type ForwardStopRunner, | ||
| } from "../onboard/forward-cleanup"; | ||
| import * as registry from "../state/registry"; | ||
|
|
||
| type Reporter = (message: string) => void; | ||
|
|
||
| type AgentWithForwards = { | ||
| displayName?: string; | ||
| forward_ports?: unknown; | ||
| }; | ||
|
|
||
| type SandboxWithDashboardPort = { | ||
| dashboardPort?: unknown; | ||
| }; | ||
|
|
||
| type StopAgentForwardPortsDeps = { | ||
| getSessionAgent?: (sandboxName?: string) => AgentWithForwards | null; | ||
| getAgentDisplayName?: (agent: AgentWithForwards | null) => string; | ||
| getSandbox?: (sandboxName: string) => SandboxWithDashboardPort | null; | ||
| resolveOpenshell?: () => string | null; | ||
| runOpenshell?: ForwardStopRunner; | ||
| runCaptureOpenshell?: ForwardListRunner; | ||
| info?: Reporter; | ||
| warn?: Reporter; | ||
| }; | ||
|
|
||
| function getAgentForwardPorts(agent: AgentWithForwards, dashboardPort: unknown): number[] { | ||
| const ports = new Set<number>(); | ||
| const candidates = [ | ||
| ...(Array.isArray(agent.forward_ports) ? agent.forward_ports : []), | ||
| dashboardPort, | ||
| ]; | ||
| for (const rawPort of candidates) { | ||
| const port = | ||
| typeof rawPort === "number" | ||
| ? rawPort | ||
| : typeof rawPort === "string" && /^\d+$/.test(rawPort.trim()) | ||
| ? Number(rawPort.trim()) | ||
| : NaN; | ||
| if (Number.isInteger(port) && port >= 1024 && port <= 65535) { | ||
| ports.add(port); | ||
| } | ||
| } | ||
| return [...ports]; | ||
| } | ||
|
|
||
| function makeRunOpenshell(openshell: string): ForwardStopRunner { | ||
| return (args, opts) => { | ||
| const result = spawnSync(openshell, args, { | ||
| encoding: "utf-8", | ||
| stdio: opts.suppressOutput ? "ignore" : "inherit", | ||
| }); | ||
| if (!opts.ignoreError && result.status !== 0) { | ||
| throw new Error(`openshell ${args.join(" ")} failed`); | ||
| } | ||
| return result; | ||
| }; | ||
| } | ||
|
|
||
| function makeRunCaptureOpenshell(openshell: string): ForwardListRunner { | ||
| return (args, opts) => { | ||
| const result = spawnSync(openshell, args, { | ||
| encoding: "utf-8", | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| timeout: opts.timeout, | ||
| }); | ||
| if (result.status !== 0) { | ||
| throw new Error(`openshell ${args.join(" ")} failed`); | ||
| } | ||
| return result.stdout || ""; | ||
| }; | ||
| } | ||
|
|
||
| export function stopAgentForwardPortsForStop( | ||
| sandboxName: string | undefined, | ||
| deps: StopAgentForwardPortsDeps = {}, | ||
| ): void { | ||
| if (!sandboxName) return; | ||
|
|
||
| const getSessionAgent = deps.getSessionAgent ?? agentRuntime.getSessionAgent; | ||
| const agent = getSessionAgent(sandboxName); | ||
| if (!agent) return; | ||
|
|
||
| const getSandbox = deps.getSandbox ?? registry.getSandbox; | ||
| const ports = getAgentForwardPorts(agent, getSandbox(sandboxName)?.dashboardPort); | ||
| if (ports.length === 0) return; | ||
|
|
||
| const displayName = deps.getAgentDisplayName | ||
| ? deps.getAgentDisplayName(agent) | ||
| : agentRuntime.getAgentDisplayName( | ||
| agent as Parameters<typeof agentRuntime.getAgentDisplayName>[0], | ||
| ); | ||
| const warn = deps.warn ?? (() => {}); | ||
| const info = deps.info ?? (() => {}); | ||
|
|
||
| const openshell = (deps.resolveOpenshell ?? resolveOpenshell)(); | ||
| if (!openshell) { | ||
| warn(`openshell not found - cannot stop ${displayName} host port forwards.`); | ||
| return; | ||
| } | ||
|
|
||
| const runOpenshell = deps.runOpenshell ?? makeRunOpenshell(openshell); | ||
| const runCaptureOpenshell = deps.runCaptureOpenshell ?? makeRunCaptureOpenshell(openshell); | ||
|
|
||
| for (const port of ports) { | ||
| const result = bestEffortForwardStopForSandbox( | ||
| runOpenshell, | ||
| runCaptureOpenshell, | ||
| port, | ||
| sandboxName, | ||
| ); | ||
| if (result === "stopped") { | ||
| info( | ||
| `Stopped ${displayName} host port forward ${String(port)} for sandbox '${sandboxName}'.`, | ||
| ); | ||
| } else if (result === "owned-other") { | ||
| warn( | ||
| `Keeping ${displayName} host port forward ${String(port)}; it belongs to another sandbox.`, | ||
| ); | ||
| } else if (result === "list-failed") { | ||
| warn( | ||
| `Could not enumerate OpenShell forwards; skipping ${displayName} host port forward ${String( | ||
| port, | ||
| )} cleanup.`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.