diff --git a/packages/provider-acp-registry/src/server/auth.test.ts b/packages/provider-acp-registry/src/server/auth.test.ts index 8624246a9a2a7..be34f17c30934 100644 --- a/packages/provider-acp-registry/src/server/auth.test.ts +++ b/packages/provider-acp-registry/src/server/auth.test.ts @@ -520,4 +520,33 @@ it("formatFailureDetail handles all cause types and formatting edge cases cleanl }); const expectedLong = `The ACP agent could not complete sign-in. ${"A".repeat(500)}…`; assert.strictEqual(formatFailureDetail(baseWithPeriod, longError), expectedLong); + + // 8. A surrogate pair straddling the limit is dropped whole, not split + const straddlingError = new AcpRequestError({ + method: "authenticate", + code: -32000, + errorMessage: `${"A".repeat(499)}🔑 Try again.`, + }); + assert.strictEqual( + formatFailureDetail(baseWithPeriod, straddlingError), + `The ACP agent could not complete sign-in. ${"A".repeat(499)}…`, + ); + const endingError = new AcpRequestError({ + method: "authenticate", + code: -32000, + errorMessage: `${"A".repeat(498)}🔑 Try again.`, + }); + assert.strictEqual( + formatFailureDetail(baseWithPeriod, endingError), + `The ACP agent could not complete sign-in. ${"A".repeat(498)}🔑…`, + ); + const flagError = new AcpRequestError({ + method: "authenticate", + code: -32000, + errorMessage: `${"A".repeat(498)}🇧🇪 Try again.`, + }); + assert.strictEqual( + formatFailureDetail(baseWithPeriod, flagError), + `The ACP agent could not complete sign-in. ${"A".repeat(498)}…`, + ); }); diff --git a/packages/provider-acp-registry/src/server/auth.ts b/packages/provider-acp-registry/src/server/auth.ts index a6e4cba90764a..4347f25447086 100644 --- a/packages/provider-acp-registry/src/server/auth.ts +++ b/packages/provider-acp-registry/src/server/auth.ts @@ -29,11 +29,17 @@ import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRunti const isAcpRequestError = Schema.is(EffectAcpErrors.AcpRequestError); const MAX_ERROR_MESSAGE_LENGTH = 500; +const errorGraphemes = new Intl.Segmenter(undefined, { granularity: "grapheme" }); const appendDetail = (detail: string, reason: string): string => { let message = reason.trim(); if (message.length > MAX_ERROR_MESSAGE_LENGTH) { - message = `${message.slice(0, MAX_ERROR_MESSAGE_LENGTH).trimEnd()}…`; + let end = 0; + for (const { index, segment } of errorGraphemes.segment(message)) { + if (index + segment.length > MAX_ERROR_MESSAGE_LENGTH) break; + end = index + segment.length; + } + message = `${message.slice(0, end).trimEnd()}…`; } if (message.length === 0) return detail; const trimmed = detail.trimEnd(); diff --git a/scripts/scenarios/acp-sign-in-error-keeps-whole-characters.e2e.ts b/scripts/scenarios/acp-sign-in-error-keeps-whole-characters.e2e.ts new file mode 100644 index 0000000000000..cc3a74979f5f4 --- /dev/null +++ b/scripts/scenarios/acp-sign-in-error-keeps-whole-characters.e2e.ts @@ -0,0 +1,90 @@ +import { ProviderDriverKind, ProviderInstanceId } from "@t3tools/contracts"; +import { expect } from "e2e"; +import * as Effect from "effect/Effect"; +import * as Path from "effect/Path"; + +import { T3_HOME } from "../e2e.config.ts"; +import { rpcClient, runPromise, test, WEB } from "./t3.ts"; + +const DETAIL = "Could not reach the demo sign-in server."; +// The key emoji straddles the 500-unit limit on the agent's diagnostic. +const DIAGNOSTIC = `${`${DETAIL} ${"Connection refused. ".repeat(26)}`.slice(0, 499)}🔑 Try again.`; +const ACP = String.raw` +import { createInterface } from "node:readline"; + +for await (const line of createInterface({ input: process.stdin })) { + const { id, method } = JSON.parse(line); + if (id === undefined) continue; + const result = method === "initialize" ? { + protocolVersion: 2, + info: { name: "demo-sign-in-agent", version: "1.0.0" }, + authMethods: [{ type: "agent", methodId: "login", name: "Sign in" }], + } : method === "auth/logout" ? {} : undefined; + const error = method === "auth/login" + ? { code: -32603, message: ${JSON.stringify(DIAGNOSTIC)} } + : method === "session/new" + ? { code: -32000, message: "Authentication required" } + : { code: -32601, message: "Method not found" }; + process.stdout.write(JSON.stringify({ jsonrpc: "2.0", id, ...(result ? { result } : { error }) }) + "\n"); +} +`; + +test( + "ACP sign-in errors keep whole characters when shortened", + WEB, + async ({ t3, app, screen }) => { + const instanceId = ProviderInstanceId.make("demo-sign-in-agent"); + const project = await t3.project("demo", { "sign-in-agent.mjs": ACP }); + await runPromise( + Effect.gen(function* () { + const path = yield* Path.Path; + const client = yield* rpcClient(T3_HOME, app.baseUrl ?? ""); + yield* client["server.updateSettings"]({ + patch: {}, + providerInstanceMutation: { + operation: "upsert", + instanceId, + instance: { + driver: ProviderDriverKind.make("acpRegistry"), + displayName: "Demo sign-in agent", + enabled: true, + config: { + source: "local", + commandPath: process.execPath, + commandArgs: [path.join(project, "sign-in-agent.mjs")], + }, + }, + }, + }); + }).pipe(Effect.scoped), + ); + + try { + await t3.open(`/settings/providers?instanceId=${instanceId}`); + await screen.getByRole("button", "Sign in", { exact: true }).tap(); + const failure = screen.getByText( + /^The ACP agent could not complete sign-in\. Could not reach the demo sign-in server\./, + ); + try { + await failure.waitFor(); + const message = (await failure.textContent()) ?? ""; + expect(message.toWellFormed().slice(-12)).not.toContain("�"); + expect(message).toBe( + `The ACP agent could not complete sign-in. ${DIAGNOSTIC.slice(0, 499)}…`, + ); + } finally { + await app.screenshot("acp-request-error"); + } + } finally { + await runPromise( + Effect.gen(function* () { + const client = yield* rpcClient(T3_HOME, app.baseUrl ?? ""); + yield* client["server.updateSettings"]({ + patch: {}, + providerInstanceMutation: { operation: "remove", instanceId }, + }); + }).pipe(Effect.scoped), + ); + } + }, +);