diff --git a/doc/HERMES_GATEWAY_ONBOARDING.md b/doc/HERMES_GATEWAY_ONBOARDING.md index 257a067f88c..d958c68367d 100644 --- a/doc/HERMES_GATEWAY_ONBOARDING.md +++ b/doc/HERMES_GATEWAY_ONBOARDING.md @@ -53,6 +53,15 @@ Paperclip can usually store `http://127.0.0.1:8642` as the gateway URL. For Docker, LAN, tailnet, or reverse-proxy setups, use a URL reachable by the Paperclip server process. +If a run reports a loopback connection refusal, check the listener from the +Paperclip server's host or container. Its loopback address does not refer to +your browser's machine or an agent sandbox. `hermes_gateway` does not start +Hermes: start the API server at the configured address, or set `apiBaseUrl` +to its reachable address. Choose `hermes_local` when Paperclip should launch +the Hermes CLI itself. Connection guidance does not prove whether a remote +run was accepted before a redirect failed; the task remains failed and the +normal failure reporting and retry rules still apply. + Plain HTTP is accepted for loopback. Non-loopback HTTP is denied by default in the join flow; use HTTPS for real remote gateways. For private local development only, the join payload can set diff --git a/packages/adapters/hermes/src/gateway/server/execute.test.ts b/packages/adapters/hermes/src/gateway/server/execute.test.ts index 2bec8c9e4de..efe4df66660 100644 --- a/packages/adapters/hermes/src/gateway/server/execute.test.ts +++ b/packages/adapters/hermes/src/gateway/server/execute.test.ts @@ -607,6 +607,72 @@ describe("execute", () => { expect(result.errorMessage).toContain("host.docker.internal"); }); + it.each([ + ["http://127.0.0.1:8642", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642 }], + ["http://[::1]:8642", { code: "ECONNREFUSED", syscall: "connect", address: "::1", port: 8642 }], + ["http://localhost:8642", { code: "ECONNREFUSED", errors: [ + { code: "ECONNREFUSED", syscall: "connect", address: "::1", port: 8642 }, + { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642 }, + ] }], + ["https://localhost", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 443 }], + ["http://localhost", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 80 }], + ])("explains the server-side loopback gateway at %s without claiming non-dispatch", async (apiBaseUrl, cause) => { + const fetchMock = vi.fn(async (_input: RequestInfo | URL, _init?: RequestInit) => { + throw Object.assign(new Error("fetch failed"), { cause }); + }); + vi.stubGlobal("fetch", fetchMock); + const ctx = makeCtx({ apiBaseUrl, apiKey: "gateway-private-key" }); + ctx.onDispatch = vi.fn(); + const result = await execute(ctx); + + expect(result.errorMessage).toContain("refers to the Paperclip server, not an agent sandbox"); + expect(result.errorMessage).toContain("adapterConfig.apiBaseUrl"); + expect(result.errorMessage).toContain("hermes_local"); + expect(result.errorMessage).not.toContain("gateway-private-key"); + expect(result.errorMeta).toEqual({ category: "gateway_loopback_connection_refused", phase: "create_run" }); + expect(result).toMatchObject({ exitCode: 1, signal: null, timedOut: false, + errorCode: "hermes_gateway_connect_failed", errorFamily: "transient_upstream", retryNotBefore: null }); + expect(result.executionRecovery).toBeUndefined(); + expect(result.resultJson).toBeUndefined(); + expect(ctx.onDispatch).toHaveBeenCalledOnce(); + expect(fetchMock).toHaveBeenCalledOnce(); + expect(fetchMock.mock.calls[0]?.[1]?.redirect).toBeUndefined(); + }); + + it.each([ + ["https://gateway.example.test:8642", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642 }], + ["http://localhost:8642", { code: "ECONNREFUSED", syscall: "connect", address: "192.0.2.1", port: 8642 }], + ["http://localhost:8642", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 9000 }], + ["http://127.0.0.2:8642", { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642 }], + ["http://localhost:8642", { code: "ETIMEDOUT", syscall: "connect", address: "127.0.0.1", port: 8642 }], + ["http://localhost:8642", { code: "EHOSTUNREACH", syscall: "connect", address: "127.0.0.1", port: 8642 }], + ["http://localhost:8642", { code: "ECONNREFUSED", address: "127.0.0.1", port: 8642 }], + ["http://localhost:8642", { message: "ECONNREFUSED connect 127.0.0.1:8642" }], + ["http://localhost:8642", { code: "ECONNREFUSED", errors: [] }], + ["http://localhost:8642", { code: "ECONNREFUSED", errors: [ + { code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642 }, + { code: "ETIMEDOUT", syscall: "connect", address: "::1", port: 8642 }, + ] }], + ])("keeps ambiguous or nonmatching transport errors generic at %s", async (apiBaseUrl, cause) => { + vi.stubGlobal("fetch", vi.fn(async () => { throw Object.assign(new Error("fetch failed"), { cause }); })); + const result = await execute(makeCtx({ apiBaseUrl, apiKey: "secret-key" })); + expect(result.errorCode).toBe("hermes_gateway_connect_failed"); + expect(result.errorMessage).not.toContain("refers to the Paperclip server"); + expect(result.errorMeta).toEqual({}); + expect(result.executionRecovery).toBeUndefined(); + }); + + it("does not infer loopback refusal from an HTTP provider response", async () => { + vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ + code: "ECONNREFUSED", syscall: "connect", address: "127.0.0.1", port: 8642, + }), { status: 503 }))); + const result = await execute(makeCtx({ apiBaseUrl: "http://127.0.0.1:8642", apiKey: "secret-key" })); + expect(result.errorCode).toBe("hermes_gateway_upstream_error"); + expect(result.errorMeta?.category).toBeUndefined(); + expect(result.errorMessage).not.toContain("refers to the Paperclip server"); + expect(result.executionRecovery).toBeUndefined(); + }); + it("redacts echoed auth material from HTTP error payloads", async () => { vi.stubGlobal( "fetch", diff --git a/packages/adapters/hermes/src/gateway/server/execute.ts b/packages/adapters/hermes/src/gateway/server/execute.ts index 41a3293b584..6942af59fc0 100644 --- a/packages/adapters/hermes/src/gateway/server/execute.ts +++ b/packages/adapters/hermes/src/gateway/server/execute.ts @@ -22,6 +22,7 @@ import { } from "../shared/constants.js"; import { allowsInsecureRemoteHttp, + isLoopbackHostname, isRemotePlainHttp, remotePlainHttpDeniedMessage, } from "./transport-security.js"; @@ -38,6 +39,7 @@ type HermesHttpError = Error & { code?: string; retryNotBefore?: string | null; body?: unknown; + transportCause?: unknown; }; type TerminalState = { @@ -377,6 +379,7 @@ async function fetchJson(input: RequestInfo | URL, init: RequestInit): Promise 8) return false; + return refusals.every((value) => { + const refusal = asRecord(value); + return refusal?.code === "ECONNREFUSED" && refusal.syscall === "connect" && + refusal.port === port && typeof refusal.address === "string" && + isLoopbackHostname(refusal.address) && + (hostname === "localhost" || refusal.address.toLowerCase() === hostname); + }); +} + +function errorResult(err: unknown, baseUrl: URL, redactText: TextRedactor = sanitizeSensitiveText): AdapterExecutionResult { const hermesError = err as HermesHttpError; const code = hermesError.code ?? "hermes_gateway_protocol_error"; const classified = hermesError.status ? classifyHttpError(hermesError.status) : null; + const loopbackRefused = isConfiguredLoopbackRefusal(hermesError, baseUrl); const errorMessage = code === "hermes_gateway_auth_failed" ? `${redactErrorMessage(err, redactText)}. Check adapterConfig.apiKey matches the Hermes API_SERVER_KEY for the running gateway.` - : redactErrorMessage(err, redactText); + : loopbackRefused + ? `${redactErrorMessage(err, redactText)}. The configured loopback gateway refers to the Paperclip server, not an agent sandbox or your browser's machine. Check that the Hermes API server is running there, or set adapterConfig.apiBaseUrl to its address reachable from the Paperclip server. Hermes Gateway connects to an already-running gateway; use hermes_local if Paperclip should launch the local Hermes CLI.` + : redactErrorMessage(err, redactText); return { exitCode: 1, signal: null, @@ -774,6 +797,9 @@ function errorResult(err: unknown, redactText: TextRedactor = sanitizeSensitiveT retryNotBefore: hermesError.retryNotBefore ?? null, errorMessage, errorMeta: { + // Diagnosis only: a redirected request may have received a response + // before refusing a later connection. This never proves non-dispatch. + ...(loopbackRefused ? { category: "gateway_loopback_connection_refused", phase: "create_run" } : {}), ...(hermesError.status ? { status: hermesError.status } : {}), ...(hermesError.body ? { body: redactForLog(hermesError.body, [], 0, redactText) as Record } : {}), }, @@ -898,7 +924,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise