Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(agent-runtime): do not treat TLS authentication wording as unauthorized (#1874)

xpeng5278-webc5e53d30258f
diff --git a/packages/agent-runtime/src/agent-errors.test.ts b/packages/agent-runtime/src/agent-errors.test.ts
index 01e45fcfa..1e8c14ca7 100644
--- a/packages/agent-runtime/src/agent-errors.test.ts
+++ b/packages/agent-runtime/src/agent-errors.test.ts
@@ -235,6 +235,52 @@ describe("classifyAgentError", () => {
});
});
+ it("does not treat TLS authentication wording as an API-key rejection", () => {
+ expect(classifyAgentError(new Error("mutual TLS authentication failed")))
+ .toMatchObject({
+ code: "NETWORK_ERROR",
+ retriable: true,
+ details: { networkCategory: "tls" },
+ });
+
+ const handshake = classifyAgentError(
+ new Error("authentication handshake timed out"),
+ );
+ expect(handshake).not.toMatchObject({ code: "PROVIDER_UNAUTHORIZED" });
+ expect(handshake.retriable).toBe(true);
+ expect(["NETWORK_ERROR", "TIMEOUT"]).toContain(handshake.code);
+
+ expect(classifyAgentError(new Error("Invalid API key"))).toMatchObject({
+ code: "PROVIDER_UNAUTHORIZED",
+ retriable: false,
+ });
+ expect(classifyAgentError(new Error("unauthorized"))).toMatchObject({
+ code: "PROVIDER_UNAUTHORIZED",
+ retriable: false,
+ });
+ expect(classifyAgentError(new Error("permission denied"))).toMatchObject({
+ code: "PROVIDER_UNAUTHORIZED",
+ retriable: false,
+ });
+
+ // A real credential phrase still names the key or the credentials, and a
+ // certificate errno stays terminal even when the text says authentication.
+ expect(classifyAgentError(new Error("invalid authentication credentials")))
+ .toMatchObject({ code: "PROVIDER_UNAUTHORIZED", retriable: false });
+ expect(classifyAgentError(new Error("authentication failed"))).toMatchObject({
+ code: "PROVIDER_UNAUTHORIZED",
+ retriable: false,
+ });
+ expect(classifyAgentError(Object.assign(
+ new Error("mutual TLS authentication failed"),
+ { code: "CERT_HAS_EXPIRED" },
+ ))).toMatchObject({
+ code: "NETWORK_ERROR",
+ retriable: false,
+ details: { networkCategory: "tls", networkCode: "CERT_HAS_EXPIRED" },
+ });
+ });
+
it("classifies pi-ai '<status>: <body>' errorMessage strings", () => {
expect(classifyAgentError('429: {"error":{"type":"rate_limit_error"}}'))
.toMatchObject({ code: "PROVIDER_RATE_LIMITED", retriable: true });
diff --git a/packages/agent-runtime/src/agent-errors.ts b/packages/agent-runtime/src/agent-errors.ts
index 20a761956..7699d4eeb 100644
--- a/packages/agent-runtime/src/agent-errors.ts
+++ b/packages/agent-runtime/src/agent-errors.ts
@@ -253,6 +253,17 @@ function pickNetworkCode(codes: readonly string[]): {
return { code: codes.find((candidate) => NETWORK_PATTERN.test(candidate)) };
}
+/**
+ * TLS handshake / certificate wording. Not an API-key rejection: stacks say
+ * "mutual TLS authentication failed" and "authentication handshake timed out"
+ * for the transport itself.
+ */
+function isTlsTransportText(text: string): boolean {
+ return /certificate|self[- ]signed|(?:^|[^a-z])(?:tls|ssl)(?:[^a-z]|$)|handshake/i.test(
+ text,
+ );
+}
+
/**
* Last resort when no errno survived: classify by wording. Order matters —
* "proxy" outranks the transport wording it usually wraps.
@@ -262,11 +273,7 @@ function networkCategoryFromText(text: string): NetworkFailureCategory {
if (/getaddrinfo|ENOTFOUND|EAI_AGAIN|dns|name resolution/i.test(text)) {
return "dns";
}
- if (
- /certificate|self[- ]signed|(?:^|[^a-z])(?:tls|ssl)(?:[^a-z]|$)|EPROTO|handshake/i.test(
- text,
- )
- ) {
+ if (isTlsTransportText(text) || /EPROTO/i.test(text)) {
return "tls";
}
if (/timed?\s?out|timeout/i.test(text)) return "timeout";
@@ -393,6 +400,33 @@ export function networkFailureDiagnostics(
return { category: network.category, fields: networkDetailFields(network) };
}
+/**
+ * Credential rejection worded without an HTTP status.
+ *
+ * Bare "authentication" is not enough. Keep the API-key / unauthorized
+ * phrases, and keep "authentication" only beside an API key, bearer token,
+ * or credentials — or as a short provider phrase such as "authentication
+ * failed". Handshake, TLS/SSL, and certificate text is transport, so it
+ * never counts.
+ */
+const STATUSLESS_CREDENTIAL_PATTERN =
+ /invalid[ _]api[ _]key|api key not valid|unauthorized|permission denied/i;
+
+const AUTHENTICATION_WITH_CREDENTIAL_PATTERN =
+ /(?:api[-_ ]?key|credentials|bearer).{0,48}\bauthentication\b|\bauthentication\b.{0,48}(?:api[-_ ]?key|credentials|bearer)/i;
+
+const AUTHENTICATION_FAILURE_PATTERN =
+ /\bauthentication[_ ](?:failed|failure|error)\b/i;
+
+function isStatuslessCredentialFailure(message: string): boolean {
+ if (STATUSLESS_CREDENTIAL_PATTERN.test(message)) return true;
+ if (isTlsTransportText(message)) return false;
+ return (
+ AUTHENTICATION_WITH_CREDENTIAL_PATTERN.test(message) ||
+ AUTHENTICATION_FAILURE_PATTERN.test(message)
+ );
+}
+
export function classifyAgentError(err: unknown): ClassifiedAgentError {
const envelope = err !== null && typeof err === "object"
? err as Record<string, unknown> : undefined;
@@ -483,9 +517,15 @@ export function classifyAgentError(err: unknown): ClassifiedAgentError {
// "fetch failed" causes don't fall through to the generic bucket. The cause
// chain is summarized as a coarse category plus the transport errno, so the
// failing layer is identifiable without a user-visible code per layer.
+ // Handshake and certificate wording is the same kind of failure when no
+ // status is present, even if the text says "authentication".
const network = describeNetworkFailure(err, rawMessage);
const certificateFailure = isCertificateVerificationError(network.code);
- if (hasNetworkCause(err, rawMessage) || (status === undefined && certificateFailure)) {
+ if (
+ hasNetworkCause(err, rawMessage) ||
+ (status === undefined &&
+ (certificateFailure || isTlsTransportText(rawMessage)))
+ ) {
return result(
"NETWORK_ERROR",
!certificateFailure,
@@ -528,7 +568,7 @@ export function classifyAgentError(err: unknown): ClassifiedAgentError {
return result("PROVIDER_ERROR", true);
}
- if (/invalid[ _]api[ _]key|api key not valid|unauthorized|authentication|permission denied/i.test(rawMessage)) {
+ if (isStatuslessCredentialFailure(rawMessage)) {
return result("PROVIDER_UNAUTHORIZED", false);
}
if (RATE_LIMIT_PATTERN.test(rawMessage)) {