Skip to content

Commit aed7b39

Browse files
committed
fix(provider-acp-registry): surface agent error message on sign-in failure
When an ACP agent replies with a JSON-RPC error during authenticate or session verification, append the agent's error message to the failure detail so users see the actionable cause instead of a generic message. Closes #30218
1 parent cefb544 commit aed7b39

2 files changed

Lines changed: 165 additions & 3 deletions

File tree

‎packages/provider-acp-registry/src/server/auth.test.ts‎

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { assert, it } from "@effect/vitest";
55
import { ProviderInstanceId, type ProviderAuthState } from "@t3tools/contracts";
66
import { AcpRegistrySettings } from "../settings.ts";
77
import * as Deferred from "effect/Deferred";
8+
import * as Cause from "effect/Cause";
89
import * as Effect from "effect/Effect";
910
import * as Option from "effect/Option";
1011
import * as Schema from "effect/Schema";
@@ -13,7 +14,7 @@ import type * as AcpSchema from "effect-acp/compat";
1314
import { AcpRequestError } from "effect-acp/errors";
1415

1516
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
16-
import { makeAcpRegistryAuth } from "./auth.ts";
17+
import { formatFailureDetail, makeAcpRegistryAuth } from "./auth.ts";
1718
import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
1819
import type * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
1920

@@ -61,6 +62,7 @@ const makeHarness = (
6162
failVerification = false,
6263
settings = decodeSettings({ agentId: "test-agent" }),
6364
providerInstanceId = instanceId,
65+
authenticateError?: AcpRequestError,
6466
) =>
6567
Effect.gen(function* () {
6668
const verify = yield* Deferred.make<void>();
@@ -111,6 +113,9 @@ const makeHarness = (
111113
authenticate: (methodId) =>
112114
Effect.gen(function* () {
113115
authenticated.push(methodId);
116+
if (authenticateError !== undefined) {
117+
return yield* authenticateError;
118+
}
114119
if (elicitation)
115120
yield* elicitation(
116121
{
@@ -342,8 +347,137 @@ it.effect("does not report authentication when post-login verification fails", (
342347
h.exit(0);
343348
yield* h.phase("verifying");
344349
yield* Deferred.succeed(h.verify, undefined);
345-
yield* h.phase("failed");
350+
const failed = yield* h.phase("failed");
346351
assert.deepEqual(h.changed, []);
347352
assert.isTrue(h.killed());
353+
assert.strictEqual(
354+
failed.message,
355+
"The provider could not create a session after terminal sign-in. Authentication required",
356+
);
357+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
358+
);
359+
360+
it.effect("surfaces the agent error message when authenticate fails with AcpRequestError", () =>
361+
Effect.gen(function* () {
362+
const error = new AcpRequestError({
363+
method: "authenticate",
364+
code: -32000,
365+
errorMessage: "Connect to the VPN, then sign in again.",
366+
});
367+
const h = yield* makeHarness(
368+
browserMethod,
369+
false,
370+
decodeSettings({ agentId: "test-agent" }),
371+
instanceId,
372+
error,
373+
);
374+
yield* h.controller.start("owner", Effect.void, "browser");
375+
const failed = yield* h.phase("failed");
376+
assert.strictEqual(
377+
failed.message,
378+
"The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
379+
);
380+
assert.deepEqual(h.changed, []);
348381
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
349382
);
383+
384+
it.effect(
385+
"surfaces the agent error message when post-sign-in verification fails for agent auth",
386+
() =>
387+
Effect.gen(function* () {
388+
const h = yield* makeHarness(browserMethod, true);
389+
const flow = yield* h.controller.start("owner", Effect.void, "browser");
390+
yield* h.phase("waiting");
391+
yield* h.controller.respond!("owner", {
392+
instanceId,
393+
flowId: flow.flowId!,
394+
interactionId: "consent",
395+
response: { type: "browser", action: "accept" },
396+
});
397+
yield* h.phase("verifying");
398+
yield* Deferred.succeed(h.verify, undefined);
399+
const failed = yield* h.phase("failed");
400+
assert.strictEqual(
401+
failed.message,
402+
"The provider could not create a session after sign-in. Authentication required",
403+
);
404+
assert.deepEqual(h.changed, []);
405+
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
406+
);
407+
408+
it("formatFailureDetail handles all cause types and formatting edge cases cleanly", () => {
409+
const baseWithPeriod = "The ACP agent could not complete sign-in.";
410+
const baseWithoutPeriod = "The ACP agent could not complete sign-in";
411+
412+
// 1. undefined or null cause
413+
assert.strictEqual(formatFailureDetail(baseWithPeriod, undefined), baseWithPeriod);
414+
assert.strictEqual(formatFailureDetail(baseWithPeriod, null), baseWithPeriod);
415+
416+
// 2. plain Error object, string, number, or object defect
417+
assert.strictEqual(
418+
formatFailureDetail(baseWithPeriod, new Error("Something broke")),
419+
baseWithPeriod,
420+
);
421+
assert.strictEqual(formatFailureDetail(baseWithPeriod, "string error"), baseWithPeriod);
422+
assert.strictEqual(formatFailureDetail(baseWithPeriod, { foo: "bar" }), baseWithPeriod);
423+
424+
// 3. AcpRequestError with empty or whitespace-only errorMessage
425+
const emptyError = new AcpRequestError({
426+
method: "authenticate",
427+
code: -32000,
428+
errorMessage: "",
429+
});
430+
const whitespaceError = new AcpRequestError({
431+
method: "authenticate",
432+
code: -32000,
433+
errorMessage: " \t\n ",
434+
});
435+
assert.strictEqual(formatFailureDetail(baseWithPeriod, emptyError), baseWithPeriod);
436+
assert.strictEqual(formatFailureDetail(baseWithPeriod, whitespaceError), baseWithPeriod);
437+
438+
// 4. AcpRequestError with valid message: ensure no double periods, clean separation
439+
const validError = new AcpRequestError({
440+
method: "authenticate",
441+
code: -32000,
442+
errorMessage: "Connect to the VPN, then sign in again.",
443+
});
444+
assert.strictEqual(
445+
formatFailureDetail(baseWithPeriod, validError),
446+
"The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
447+
);
448+
assert.strictEqual(
449+
formatFailureDetail(baseWithoutPeriod, validError),
450+
"The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
451+
);
452+
453+
// 5. AcpRequestError with trimmed message and message without ending period
454+
const unpunctError = new AcpRequestError({
455+
method: "authenticate",
456+
code: -32000,
457+
errorMessage: " Invalid credentials ",
458+
});
459+
assert.strictEqual(
460+
formatFailureDetail(baseWithPeriod, unpunctError),
461+
"The ACP agent could not complete sign-in. Invalid credentials",
462+
);
463+
464+
// 6. Nested Cause (e.g. Cause.fail or Cause.die wrapping AcpRequestError)
465+
assert.strictEqual(
466+
formatFailureDetail(baseWithPeriod, Cause.fail(validError)),
467+
"The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
468+
);
469+
assert.strictEqual(
470+
formatFailureDetail(baseWithPeriod, Cause.die(validError)),
471+
"The ACP agent could not complete sign-in. Connect to the VPN, then sign in again.",
472+
);
473+
474+
// 7. Extremely long error message: truncates to 500 characters with ellipsis
475+
const longText = "A".repeat(600);
476+
const longError = new AcpRequestError({
477+
method: "authenticate",
478+
code: -32000,
479+
errorMessage: longText,
480+
});
481+
const expectedLong = `The ACP agent could not complete sign-in. ${"A".repeat(500)}…`;
482+
assert.strictEqual(formatFailureDetail(baseWithPeriod, longError), expectedLong);
483+
});

‎packages/provider-acp-registry/src/server/auth.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "@t3tools/contracts";
66
import type { AcpRegistrySettings } from "../settings.ts";
77
import * as SecureRandom from "effect/crypto/SecureRandom";
8+
import * as Cause from "effect/Cause";
89
import * as Deferred from "effect/Deferred";
910
import * as Effect from "effect/Effect";
1011
import * as Exit from "effect/Exit";
@@ -13,7 +14,9 @@ import * as Option from "effect/Option";
1314
import * as Queue from "effect/Queue";
1415
import * as Scope from "effect/Scope";
1516
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
17+
import * as Schema from "effect/Schema";
1618
import type * as AcpSchema from "effect-acp/compat";
19+
import * as EffectAcpErrors from "effect-acp/errors";
1720

1821
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
1922
import * as ProviderAuthFlow from "@t3tools/provider-core/server/providerAuthFlow";
@@ -22,6 +25,26 @@ import * as AcpRegistrySupport from "./AcpRegistrySupport.ts";
2225
import * as AcpRegistryRuntimeCoordinator from "./AcpRegistryRuntimeCoordinator.ts";
2326
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
2427

28+
const isAcpRequestError = Schema.is(EffectAcpErrors.AcpRequestError);
29+
30+
const MAX_ERROR_MESSAGE_LENGTH = 500;
31+
32+
export const formatFailureDetail = (detail: string, cause?: unknown): string => {
33+
const unwrapped = Cause.isCause(cause) ? Cause.squash(cause) : cause;
34+
if (isAcpRequestError(unwrapped)) {
35+
let message = unwrapped.errorMessage.trim();
36+
if (message.length > MAX_ERROR_MESSAGE_LENGTH) {
37+
message = `${message.slice(0, MAX_ERROR_MESSAGE_LENGTH).trimEnd()}…`;
38+
}
39+
if (message.length > 0) {
40+
const trimmed = detail.trimEnd();
41+
const prefix = trimmed.endsWith(".") ? `${trimmed} ` : `${trimmed}. `;
42+
return `${prefix}${message}`;
43+
}
44+
}
45+
return detail;
46+
};
47+
2548
type Runtime = Pick<
2649
AcpSessionRuntime.AcpSessionRuntime["Service"],
2750
"initialize" | "authenticate" | "start" | "handleElicitation" | "logout"
@@ -47,7 +70,12 @@ export const makeAcpRegistryAuth = Effect.fn("makeAcpRegistryAuth")(function* (o
4770
const agentKey =
4871
options.settings.source === "local" ? `local:${options.instanceId}` : options.settings.agentId;
4972
const failure = (operation: string, detail: string, cause?: unknown) =>
50-
new ProviderSetupError({ instanceId: options.instanceId, operation, detail, cause });
73+
new ProviderSetupError({
74+
instanceId: options.instanceId,
75+
operation,
76+
detail: formatFailureDetail(detail, cause),
77+
cause,
78+
});
5179
const resolve = catalog
5280
.resolve(options.settings, options.cwd, options.environment)
5381
.pipe(

0 commit comments

Comments
 (0)