Skip to content
TypeScript

fix(provider-acp-registry): surface agent error message on sign-in failure (#30295)

Nicob15f772ebee6
diff --git a/packages/provider-acp-registry/src/server/auth.test.ts b/packages/provider-acp-registry/src/server/auth.test.ts
index b81e2e3fba7..4bb647bd940 100644
--- a/packages/provider-acp-registry/src/server/auth.test.ts
+++ b/packages/provider-acp-registry/src/server/auth.test.ts
@@ -5,6 +5,7 @@ import { assert, it } from "@effect/vitest";
import { ProviderInstanceId, type ProviderAuthState } from "@t3tools/contracts";
import { AcpRegistrySettings } from "../settings.ts";
import * as Deferred from "effect/Deferred";
+import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
@@ -13,7 +14,7 @@ import type * as AcpSchema from "effect-acp/compat";
import { AcpRequestError } from "effect-acp/errors";
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
-import { makeAcpRegistryAuth } from "./auth.ts";
+import { formatFailureDetail, makeAcpRegistryAuth } from "./auth.ts";
import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
@@ -61,6 +62,7 @@ const makeHarness = (
failVerification = false,
settings = decodeSettings({ agentId: "test-agent" }),
providerInstanceId = instanceId,
+ authenticateError?: AcpRequestError,
) =>
Effect.gen(function* () {
const verify = yield* Deferred.make<void>();
@@ -111,6 +113,9 @@ const makeHarness = (
authenticate: (methodId) =>
Effect.gen(function* () {
authenticated.push(methodId);
+ if (authenticateError !== undefined) {
+ return yield* authenticateError;
+ }
if (elicitation)
yield* elicitation(
{
@@ -342,8 +347,137 @@ it.effect("does not report authentication when post-login verification fails", (
h.exit(0);
yield* h.phase("verifying");
yield* Deferred.succeed(h.verify, undefined);
- yield* h.phase("failed");
+ const failed = yield* h.phase("failed");
assert.deepEqual(h.changed, []);
assert.isTrue(h.killed());
+ assert.strictEqual(
+ failed.message,
+ "The provider could not create a session after terminal sign-in. Authentication required",
+ );
+ }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
+);
+
+it.effect("surfaces the agent error message when authenticate fails with AcpRequestError", () =>
+ Effect.gen(function* () {
+ const error = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: "Connect to the VPN, then sign in again.",
+ });
+ const h = yield* makeHarness(
+ browserMethod,
+ false,
+ decodeSettings({ agentId: "test-agent" }),
+ instanceId,
+ error,
+ );
+ yield* h.controller.start("owner", Effect.void, "browser");
+ const failed = yield* h.phase("failed");
+ assert.strictEqual(
+ failed.message,
+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
+ );
+ assert.deepEqual(h.changed, []);
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);
+
+it.effect(
+ "surfaces the agent error message when post-sign-in verification fails for agent auth",
+ () =>
+ Effect.gen(function* () {
+ const h = yield* makeHarness(browserMethod, true);
+ const flow = yield* h.controller.start("owner", Effect.void, "browser");
+ yield* h.phase("waiting");
+ yield* h.controller.respond!("owner", {
+ instanceId,
+ flowId: flow.flowId!,
+ interactionId: "consent",
+ response: { type: "browser", action: "accept" },
+ });
+ yield* h.phase("verifying");
+ yield* Deferred.succeed(h.verify, undefined);
+ const failed = yield* h.phase("failed");
+ assert.strictEqual(
+ failed.message,
+ "The provider could not create a session after sign-in. Authentication required",
+ );
+ assert.deepEqual(h.changed, []);
+ }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
+);
+
+it("formatFailureDetail handles all cause types and formatting edge cases cleanly", () => {
+ const baseWithPeriod = "The ACP agent could not complete sign-in.";
+ const baseWithoutPeriod = "The ACP agent could not complete sign-in";
+
+ // 1. undefined or null cause
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, undefined), baseWithPeriod);
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, null), baseWithPeriod);
+
+ // 2. plain Error object, string, number, or object defect
+ assert.strictEqual(
+ formatFailureDetail(baseWithPeriod, new Error("Something broke")),
+ baseWithPeriod,
+ );
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, "string error"), baseWithPeriod);
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, { foo: "bar" }), baseWithPeriod);
+
+ // 3. AcpRequestError with empty or whitespace-only errorMessage
+ const emptyError = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: "",
+ });
+ const whitespaceError = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: " \t\n ",
+ });
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, emptyError), baseWithPeriod);
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, whitespaceError), baseWithPeriod);
+
+ // 4. AcpRequestError with valid message: ensure no double periods, clean separation
+ const validError = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: "Connect to the VPN, then sign in again.",
+ });
+ assert.strictEqual(
+ formatFailureDetail(baseWithPeriod, validError),
+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
+ );
+ assert.strictEqual(
+ formatFailureDetail(baseWithoutPeriod, validError),
+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
+ );
+
+ // 5. AcpRequestError with trimmed message and message without ending period
+ const unpunctError = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: " Invalid credentials ",
+ });
+ assert.strictEqual(
+ formatFailureDetail(baseWithPeriod, unpunctError),
+ "The ACP agent could not complete sign-in. Invalid credentials",
+ );
+
+ // 6. Nested Cause (e.g. Cause.fail or Cause.die wrapping AcpRequestError)
+ assert.strictEqual(
+ formatFailureDetail(baseWithPeriod, Cause.fail(validError)),
+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
+ );
+ assert.strictEqual(
+ formatFailureDetail(baseWithPeriod, Cause.die(validError)),
+ "The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
+ );
+
+ // 7. Extremely long error message: truncates to 500 characters with ellipsis
+ const longText = "A".repeat(600);
+ const longError = new AcpRequestError({
+ method: "authenticate",
+ code: -32000,
+ errorMessage: longText,
+ });
+ const expectedLong = `The ACP agent could not complete sign-in. ${"A".repeat(500)}…`;
+ assert.strictEqual(formatFailureDetail(baseWithPeriod, longError), expectedLong);
+});
diff --git a/packages/provider-acp-registry/src/server/auth.ts b/packages/provider-acp-registry/src/server/auth.ts
index 240bca67cea..9f8d36880f2 100644
--- a/packages/provider-acp-registry/src/server/auth.ts
+++ b/packages/provider-acp-registry/src/server/auth.ts
@@ -5,6 +5,7 @@ import {
} from "@t3tools/contracts";
import type { AcpRegistrySettings } from "../settings.ts";
import * as SecureRandom from "effect/crypto/SecureRandom";
+import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
@@ -13,7 +14,9 @@ import * as Option from "effect/Option";
import * as Queue from "effect/Queue";
import * as Scope from "effect/Scope";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
+import * as Schema from "effect/Schema";
import type * as AcpSchema from "effect-acp/compat";
+import * as EffectAcpErrors from "effect-acp/errors";
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
import * as ProviderAuthFlow from "@t3tools/provider-core/server/providerAuthFlow";
@@ -22,6 +25,26 @@ import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts";
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
+const isAcpRequestError = Schema.is(EffectAcpErrors.AcpRequestError);
+
+const MAX_ERROR_MESSAGE_LENGTH = 500;
+
+export const formatFailureDetail = (detail: string, cause?: unknown): string => {
+ const unwrapped = Cause.isCause(cause) ? Cause.squash(cause) : cause;
+ if (isAcpRequestError(unwrapped)) {
+ let message = unwrapped.errorMessage.trim();
+ if (message.length > MAX_ERROR_MESSAGE_LENGTH) {
+ message = `${message.slice(0, MAX_ERROR_MESSAGE_LENGTH).trimEnd()}…`;
+ }
+ if (message.length > 0) {
+ const trimmed = detail.trimEnd();
+ const prefix = trimmed.endsWith(".") ? `${trimmed} ` : `${trimmed}. `;
+ return `${prefix}${message}`;
+ }
+ }
+ return detail;
+};
+
type Runtime = Pick<
AcpSessionRuntime.AcpSessionRuntime["Service"],
"initialize" | "authenticate" | "start" | "handleElicitation" | "logout"
@@ -47,7 +70,12 @@ export const makeAcpRegistryAuth = Effect.fn("makeAcpRegistryAuth")(function* (o
const agentKey =
options.settings.source === "local" ? `local:${options.instanceId}` : options.settings.agentId;
const failure = (operation: string, detail: string, cause?: unknown) =>
- new ProviderSetupError({ instanceId: options.instanceId, operation, detail, cause });
+ new ProviderSetupError({
+ instanceId: options.instanceId,
+ operation,
+ detail: formatFailureDetail(detail, cause),
+ cause,
+ });
const resolve = catalog
.resolve(options.settings, options.cwd, options.environment)
.pipe(