|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import fs from "node:fs"; |
| 5 | +import { createRequire } from "node:module"; |
| 6 | +import os from "node:os"; |
| 7 | +import path from "node:path"; |
| 8 | +import { |
| 9 | + closeServer, |
| 10 | + createUnownedHealthListener, |
| 11 | + listenOn, |
| 12 | + restoreEnv, |
| 13 | + waitForPortFree, |
| 14 | +} from "./helpers/hermes-tool-gateway-broker-ownership-fixture"; |
| 15 | +import { describe, expect, test as it } from "./helpers/owned-test-resources"; |
| 16 | +import { testTimeout } from "./helpers/timeouts"; |
| 17 | + |
| 18 | +const require = createRequire(import.meta.url); |
| 19 | +const BROKER_WRAPPER = path.join( |
| 20 | + import.meta.dirname, |
| 21 | + "..", |
| 22 | + "src", |
| 23 | + "lib", |
| 24 | + "hermes-tool-gateway-broker.ts", |
| 25 | +); |
| 26 | + |
| 27 | +const BROKER_TEST_TIMEOUT_MS = testTimeout(45_000); |
| 28 | + |
| 29 | +/** Every reuse input that must still refuse a listener NemoClaw does not own. */ |
| 30 | +const UNOWNED_LISTENER_CASES = [ |
| 31 | + { label: "a matching hash", forceRestart: false, hashMatches: true }, |
| 32 | + { label: "a mismatched hash", forceRestart: false, hashMatches: false }, |
| 33 | + { label: "a restart request", forceRestart: true, hashMatches: true }, |
| 34 | + { label: "a restart request and a mismatched hash", forceRestart: true, hashMatches: false }, |
| 35 | +]; |
| 36 | + |
| 37 | +/** Every reuse input that resolves to an ordinary restart when the port is dead. */ |
| 38 | +const UNREACHABLE_PORT_CASES = [ |
| 39 | + { label: "an unowned broker and a matching hash", currentBrokerOwned: false, forceRestart: false, hashMatches: true }, |
| 40 | + { label: "an unowned broker and a mismatched hash", currentBrokerOwned: false, forceRestart: false, hashMatches: false }, |
| 41 | + { label: "an unowned broker and a restart request", currentBrokerOwned: false, forceRestart: true, hashMatches: true }, |
| 42 | + { label: "an unowned broker, a restart request, and a mismatched hash", currentBrokerOwned: false, forceRestart: true, hashMatches: false }, |
| 43 | + { label: "an owned broker and a matching hash", currentBrokerOwned: true, forceRestart: false, hashMatches: true }, |
| 44 | + { label: "an owned broker and a mismatched hash", currentBrokerOwned: true, forceRestart: false, hashMatches: false }, |
| 45 | + { label: "an owned broker and a restart request", currentBrokerOwned: true, forceRestart: true, hashMatches: true }, |
| 46 | + { label: "an owned broker, a restart request, and a mismatched hash", currentBrokerOwned: true, forceRestart: true, hashMatches: false }, |
| 47 | +]; |
| 48 | + |
| 49 | +function loadBrokerWithHome(home: string) { |
| 50 | + process.env.HOME = home; |
| 51 | + // The module resolves its credential paths at load time, and |
| 52 | + // `brokerStartedThisRun` is module state, so each case needs a fresh copy. |
| 53 | + delete require.cache[require.resolve(BROKER_WRAPPER)]; |
| 54 | + return require(BROKER_WRAPPER); |
| 55 | +} |
| 56 | + |
| 57 | +describe("Hermes managed-tool broker ownership", () => { |
| 58 | + it("refuses to adopt a healthy listener it does not own", () => { |
| 59 | + const broker = require(BROKER_WRAPPER); |
| 60 | + |
| 61 | + expect( |
| 62 | + broker.planHermesToolGatewayBrokerReuse({ |
| 63 | + brokerHealthy: true, |
| 64 | + currentBrokerOwned: false, |
| 65 | + forceRestart: false, |
| 66 | + hashMatches: true, |
| 67 | + }), |
| 68 | + ).toBe("refuse-unowned-listener"); |
| 69 | + }); |
| 70 | + |
| 71 | + // Ownership is the only input that may admit a listener. A mismatched hash or |
| 72 | + // a restart request must not downgrade the refusal into an ordinary "no |
| 73 | + // broker" outcome, because only the refusal names the held port. |
| 74 | + it.each(UNOWNED_LISTENER_CASES)( |
| 75 | + "keeps refusing an unowned listener with $label", |
| 76 | + ({ forceRestart, hashMatches }) => { |
| 77 | + const broker = require(BROKER_WRAPPER); |
| 78 | + |
| 79 | + expect( |
| 80 | + broker.planHermesToolGatewayBrokerReuse({ |
| 81 | + brokerHealthy: true, |
| 82 | + currentBrokerOwned: false, |
| 83 | + forceRestart, |
| 84 | + hashMatches, |
| 85 | + }), |
| 86 | + ).toBe("refuse-unowned-listener"); |
| 87 | + }, |
| 88 | + ); |
| 89 | + |
| 90 | + it("treats an omitted restart request as no restart", () => { |
| 91 | + const broker = require(BROKER_WRAPPER); |
| 92 | + |
| 93 | + // `ensureHermesToolGatewayBroker` forwards `options.forceRestart`, which is |
| 94 | + // undefined whenever a caller omits it, so the default is the production |
| 95 | + // path rather than a convenience. |
| 96 | + expect( |
| 97 | + broker.planHermesToolGatewayBrokerReuse({ |
| 98 | + brokerHealthy: true, |
| 99 | + currentBrokerOwned: true, |
| 100 | + hashMatches: true, |
| 101 | + }), |
| 102 | + ).toBe("reuse-current"); |
| 103 | + expect( |
| 104 | + broker.planHermesToolGatewayBrokerReuse({ |
| 105 | + brokerHealthy: true, |
| 106 | + currentBrokerOwned: false, |
| 107 | + hashMatches: true, |
| 108 | + }), |
| 109 | + ).toBe("refuse-unowned-listener"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("adopts a healthy listener it owns when the runtime hash still matches", () => { |
| 113 | + const broker = require(BROKER_WRAPPER); |
| 114 | + |
| 115 | + expect( |
| 116 | + broker.planHermesToolGatewayBrokerReuse({ |
| 117 | + brokerHealthy: true, |
| 118 | + currentBrokerOwned: true, |
| 119 | + forceRestart: false, |
| 120 | + hashMatches: true, |
| 121 | + }), |
| 122 | + ).toBe("reuse-current"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("declines an owned broker when the runtime hash moved or a restart was requested", () => { |
| 126 | + const broker = require(BROKER_WRAPPER); |
| 127 | + |
| 128 | + expect( |
| 129 | + broker.planHermesToolGatewayBrokerReuse({ |
| 130 | + brokerHealthy: true, |
| 131 | + currentBrokerOwned: true, |
| 132 | + forceRestart: false, |
| 133 | + hashMatches: false, |
| 134 | + }), |
| 135 | + ).toBe("no-usable-broker"); |
| 136 | + expect( |
| 137 | + broker.planHermesToolGatewayBrokerReuse({ |
| 138 | + brokerHealthy: true, |
| 139 | + currentBrokerOwned: true, |
| 140 | + forceRestart: true, |
| 141 | + hashMatches: true, |
| 142 | + }), |
| 143 | + ).toBe("no-usable-broker"); |
| 144 | + expect( |
| 145 | + broker.planHermesToolGatewayBrokerReuse({ |
| 146 | + brokerHealthy: true, |
| 147 | + currentBrokerOwned: true, |
| 148 | + forceRestart: true, |
| 149 | + hashMatches: false, |
| 150 | + }), |
| 151 | + ).toBe("no-usable-broker"); |
| 152 | + }); |
| 153 | + |
| 154 | + // An unreachable port cannot be refused as unowned; it is the ordinary |
| 155 | + // restart case whether or not a prior broker was ours. |
| 156 | + it.each(UNREACHABLE_PORT_CASES)( |
| 157 | + "reports no usable broker when nothing answers on the port with $label", |
| 158 | + ({ currentBrokerOwned, forceRestart, hashMatches }) => { |
| 159 | + const broker = require(BROKER_WRAPPER); |
| 160 | + |
| 161 | + expect( |
| 162 | + broker.planHermesToolGatewayBrokerReuse({ |
| 163 | + brokerHealthy: false, |
| 164 | + currentBrokerOwned, |
| 165 | + forceRestart, |
| 166 | + hashMatches, |
| 167 | + }), |
| 168 | + ).toBe("no-usable-broker"); |
| 169 | + }, |
| 170 | + ); |
| 171 | + |
| 172 | + it( |
| 173 | + "reports no broker when a foreign listener answers health after a stale hash is left behind", |
| 174 | + async ({ skip }) => { |
| 175 | + const previousHome = process.env.HOME; |
| 176 | + const home = fs.mkdtempSync(path.join(os.tmpdir(), "nc-broker-unowned-")); |
| 177 | + const impostor = createUnownedHealthListener(); |
| 178 | + |
| 179 | + try { |
| 180 | + const staging = loadBrokerWithHome(home); |
| 181 | + const credsDir = path.dirname(staging.HERMES_TOOL_GATEWAY_STATE_DIR); |
| 182 | + const hashPath = path.join(credsDir, "hermes-tool-gateway-broker.hash"); |
| 183 | + const pidPath = path.join(credsDir, "hermes-tool-gateway-broker.pid"); |
| 184 | + |
| 185 | + // Let the module write its own runtime hash rather than recomputing it |
| 186 | + // here. A hand-built hash would stop matching the moment the runtime |
| 187 | + // inputs change, and this case would then pass for the wrong reason. |
| 188 | + // The hash is written when the broker is spawned, so it exists whether |
| 189 | + // or not that broker went on to become healthy. |
| 190 | + staging.ensureHermesToolGatewayBroker({ startWithoutCredential: true }); |
| 191 | + const runtimeHash = fs.readFileSync(hashPath, "utf8"); |
| 192 | + |
| 193 | + staging.killStaleHermesToolGatewayBroker(); |
| 194 | + await waitForPortFree(staging.HERMES_TOOL_GATEWAY_PORT); |
| 195 | + |
| 196 | + // A broker that crashed leaves its runtime hash behind while the pid |
| 197 | + // goes away. That is the exact state this regression covers. |
| 198 | + fs.writeFileSync(hashPath, runtimeHash, { mode: 0o600 }); |
| 199 | + expect(fs.existsSync(pidPath)).toBe(false); |
| 200 | + |
| 201 | + const settled = loadBrokerWithHome(home); |
| 202 | + await listenOn(impostor, settled.HERMES_TOOL_GATEWAY_PORT); |
| 203 | + // The probe shells out to curl. Where a harness blocks loopback HTTP |
| 204 | + // for subprocesses, no listener can be observed as healthy and this |
| 205 | + // case would assert nothing, so report it as skipped instead of passed. |
| 206 | + skip( |
| 207 | + !settled.isHermesToolGatewayBrokerHealthy(), |
| 208 | + "curl cannot read a loopback response in this environment", |
| 209 | + ); |
| 210 | + |
| 211 | + // Reachability alone must not answer "broker ready". |
| 212 | + expect(settled.ensureHermesToolGatewayBroker({})).toBe(false); |
| 213 | + } finally { |
| 214 | + await closeServer(impostor); |
| 215 | + restoreEnv("HOME", previousHome); |
| 216 | + delete require.cache[require.resolve(BROKER_WRAPPER)]; |
| 217 | + fs.rmSync(home, { recursive: true, force: true }); |
| 218 | + } |
| 219 | + }, |
| 220 | + BROKER_TEST_TIMEOUT_MS, |
| 221 | + ); |
| 222 | +}); |
0 commit comments