Explain refused local Hermes gateway connections (#15520)

Apply the reviewed change for Explain refused local Hermes gateway connections.

Validation: required local typecheck and tests, passing CI, Greptile 5/5, and independent review of the exact source head.

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Devin FoleyandPaperclip authored and GitHub committed 2026-10-07 18:55:55 -07:00
1 parent 6b171c9616
commit ed0c6958f8
3 files changed
+104 -3

No files matched your search

+9
View File
@@ -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
@@ -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",
@@ -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<u
} catch (err) {
const fetchErr = new Error(`Hermes gateway request failed: ${fetchFailureMessage(err)}`) as HermesHttpError;
fetchErr.code = "hermes_gateway_connect_failed";
fetchErr.transportCause = err instanceof Error ? err.cause : undefined;
throw fetchErr;
}
const body = await readResponseJson(response);
@@ -758,13 +761,33 @@ function redactErrorMessage(err: unknown, redactText: TextRedactor = sanitizeSen
return redactText(String(err));
}
function errorResult(err: unknown, redactText: TextRedactor = sanitizeSensitiveText): AdapterExecutionResult {
function isConfiguredLoopbackRefusal(error: HermesHttpError, baseUrl: URL): boolean {
if (error.code !== "hermes_gateway_connect_failed" || !isLoopbackHostname(baseUrl.hostname)) return false;
const cause = asRecord(error.transportCause);
if (cause?.code !== "ECONNREFUSED") return false;
const port = Number(baseUrl.port || (baseUrl.protocol === "https:" ? 443 : 80));
const hostname = baseUrl.hostname.toLowerCase().replace(/^\[|\]$/g, "");
const refusals = Array.isArray(cause.errors) ? cause.errors : [cause];
if (refusals.length === 0 || refusals.length > 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<string, unknown> } : {}),
},
@@ -898,7 +924,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
};
}
} catch (err) {
return errorResult(err, redactText);
return errorResult(err, baseUrl, redactText);
}
await ctx.onLog("stdout", `[hermes-gateway] run created: ${runId}\n`);