From baea0d4efd962f9d1880ab107e80bc1817290bf0 Mon Sep 17 00:00:00 2001 From: Borealis Date: Thu, 19 Nov 2026 20:02:27 +0800 Subject: [PATCH] fix(hermes): treat a blank API port override as unset `resolveOnboardHermesApiPort` decides whether the caller pinned a port with `env[HERMES_API_PORT_ENV]?.trim()`, but it calls `readHermesApiPort` first, and that reader handed the raw value to `parseServicePortOverride`. A present-but-blank value (a template-expanded empty variable kept as " ", or a stray tab) skipped the `undefined`/empty-string fallback, failed the integer check, and aborted the run with Invalid port: NEMOCLAW_HERMES_API_PORT=" " must be an integer from 8642 through 8652 even though the caller treats the same value as "not requested". On re-onboard that turns a routine reuse of the recorded port into a hard failure. Return the range start for a blank value, matching the caller's rule and the documented "empty means unset" behaviour of the entrypoint. Invalid non-blank values still fail with the same message. Adds blank-value cases to the existing `readHermesApiPort` suite. Signed-off-by: Borealis --- src/lib/onboard/hermes-api-port.test.ts | 4 ++++ src/lib/onboard/hermes-api-port.ts | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/lib/onboard/hermes-api-port.test.ts b/src/lib/onboard/hermes-api-port.test.ts index c82228e0190..e6ab7db2c76 100644 --- a/src/lib/onboard/hermes-api-port.test.ts +++ b/src/lib/onboard/hermes-api-port.test.ts @@ -104,6 +104,10 @@ describe("readHermesApiPort", () => { expect(readHermesApiPort({})).toBe(8642); }); + it.each(["", " ", " "])("treats the blank value %j as unset", (value) => { + expect(readHermesApiPort({ [HERMES_API_PORT_ENV]: value })).toBe(8642); + }); + it.each(["8641", "8653", "9000", "08642", "²"])( "rejects %s outside the allocated Hermes API-port range", (value) => { diff --git a/src/lib/onboard/hermes-api-port.ts b/src/lib/onboard/hermes-api-port.ts index 337dd399d49..f855afddff1 100644 --- a/src/lib/onboard/hermes-api-port.ts +++ b/src/lib/onboard/hermes-api-port.ts @@ -164,6 +164,11 @@ export function isValidHermesApiPort(value: unknown): value is number { */ export function readHermesApiPort(env: NodeJS.ProcessEnv = process.env): number { const raw = env[HERMES_API_PORT_ENV]; + // A present-but-blank value is unset, the same rule the callers apply when + // they test `env[HERMES_API_PORT_ENV]?.trim()`. Without this guard a + // template-expanded empty value fails the parse as an invalid port and + // aborts a re-onboard that would otherwise reuse the recorded port. + if (raw !== undefined && raw.trim() === "") return HERMES_OPENAI_API_PORT; let port: number; try { port = parseServicePortOverride(HERMES_API_PORT_ENV, raw, HERMES_OPENAI_API_PORT);