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);