From fda29f726da9c2d23269d4a3638eef296a830f61 Mon Sep 17 00:00:00 2001 From: sjw1231 Date: Fri, 6 Nov 2026 18:02:08 +0800 Subject: [PATCH] fix(user-questions): keep the answerer's message in PROVIDER_ERROR Wrapping a non-HarnessError answerer failure replaced its message with a generic sentence, so callers and the model lost the concrete reason, such as a closed connection. The PROVIDER_ERROR wrapper now keeps the original message and falls back to the generic sentence only for a value that is not an Error. Refs #7869 --- docs/subsystems/user-questions.i18n.yaml | 4 ++-- docs/subsystems/user-questions.md | 9 +++++---- docs/subsystems/user-questions.zh.md | 9 +++++---- packages/interaction/user-questions/src/index.ts | 10 ++++++---- .../user-questions/tests/user-questions.spec.ts | 12 ++++++------ packages/plan/plan-mode/tests/plan-mode.spec.ts | 2 +- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/docs/subsystems/user-questions.i18n.yaml b/docs/subsystems/user-questions.i18n.yaml index bacd251ef1..483df1b1a4 100644 --- a/docs/subsystems/user-questions.i18n.yaml +++ b/docs/subsystems/user-questions.i18n.yaml @@ -24,5 +24,5 @@ en: 74fa698d4a115e80 zh: 4e9b03b5eab2ae5e /user-interaction/errors: - en: eb0a4f94282f8c18 - zh: 92c41869431b120e + en: 832b165cfa48839d + zh: 6989f734b373c1f9 diff --git a/docs/subsystems/user-questions.md b/docs/subsystems/user-questions.md index c87fe49f14..daf0db242a 100644 --- a/docs/subsystems/user-questions.md +++ b/docs/subsystems/user-questions.md @@ -209,7 +209,7 @@ After a Host restart, the Remote layer resumes the root agent when the Session r ## Errors -`UserQuestionError` extends `HarnessError`, so `ctx.tools.execute()` preserves `{ name, code }` for model-facing tool failures such as `EMPTY_QUESTIONS`, `BAD_INTENT`, `NO_PROVIDER`, `ASK_ABORTED`, or UI-side cancellation. An answerer failure that is not a `HarnessError` becomes `PROVIDER_ERROR` with the original as `cause`; another `HarnessError` keeps its own `name` and `code`. +`UserQuestionError` extends `HarnessError`, so `ctx.tools.execute()` preserves `{ name, code }` for model-facing tool failures such as `EMPTY_QUESTIONS`, `BAD_INTENT`, `NO_PROVIDER`, `ASK_ABORTED`, or UI-side cancellation. An answerer failure that is not a `HarnessError` becomes `PROVIDER_ERROR` with the original message and the original as `cause`; another `HarnessError` keeps its own `name` and `code`. ```ts type-equiv /** Stable error taxonomy for user-questions failures. */ @@ -287,9 +287,10 @@ async askTimed( request: AskUserQuestionRequest & { agent: Agent }, callId: Tool * @throws {UserQuestionError} code `ASK_ABORTED` when the supplied signal * is already or becomes aborted, `CALLER_NOT_LIVE` when a supplied agent * is not the registry's exact live instance, `DELEGATED_CALLER` when - * that live agent has subagent origin, or `PROVIDER_ERROR` when an answerer - * fails with a value that is not a `HarnessError`; a coded `HarnessError` - * from an answerer keeps its `name` and `code`. + * that live agent has subagent origin, or `PROVIDER_ERROR`, keeping the + * original message, when an answerer fails with a value that is not a + * `HarnessError`; a coded `HarnessError` from an answerer keeps its `name` + * and `code`. */ async ask(request: AskUserQuestionRequest): Promise ``` diff --git a/docs/subsystems/user-questions.zh.md b/docs/subsystems/user-questions.zh.md index 4dc465f750..aeb1818584 100644 --- a/docs/subsystems/user-questions.zh.md +++ b/docs/subsystems/user-questions.zh.md @@ -209,7 +209,7 @@ Host 重启后,Session 重开时 Remote 层会恢复根 agent。之后的迟 ## 错误 -`UserQuestionError` 继承 `HarnessError`,因此 `ctx.tools.execute()` 会保留 `{ name, code }`,用于面向模型的工具失败,如 `EMPTY_QUESTIONS`、`BAD_INTENT`、`NO_PROVIDER`、`ASK_ABORTED` 或 UI 侧取消。不是 `HarnessError` 的应答方失败会变为 `PROVIDER_ERROR`,原错误作为 `cause`;其他 `HarnessError` 保留自身的 `name` 与 `code`。 +`UserQuestionError` 继承 `HarnessError`,因此 `ctx.tools.execute()` 会保留 `{ name, code }`,用于面向模型的工具失败,如 `EMPTY_QUESTIONS`、`BAD_INTENT`、`NO_PROVIDER`、`ASK_ABORTED` 或 UI 侧取消。不是 `HarnessError` 的应答方失败会变为 `PROVIDER_ERROR`,保留原 message,原错误作为 `cause`;其他 `HarnessError` 保留自身的 `name` 与 `code`。 ```ts type-equiv /** Stable error taxonomy for user-questions failures. */ @@ -287,9 +287,10 @@ async askTimed( request: AskUserQuestionRequest & { agent: Agent }, callId: Tool * @throws {UserQuestionError} code `ASK_ABORTED` when the supplied signal * is already or becomes aborted, `CALLER_NOT_LIVE` when a supplied agent * is not the registry's exact live instance, `DELEGATED_CALLER` when - * that live agent has subagent origin, or `PROVIDER_ERROR` when an answerer - * fails with a value that is not a `HarnessError`; a coded `HarnessError` - * from an answerer keeps its `name` and `code`. + * that live agent has subagent origin, or `PROVIDER_ERROR`, keeping the + * original message, when an answerer fails with a value that is not a + * `HarnessError`; a coded `HarnessError` from an answerer keeps its `name` + * and `code`. */ async ask(request: AskUserQuestionRequest): Promise ``` diff --git a/packages/interaction/user-questions/src/index.ts b/packages/interaction/user-questions/src/index.ts index 0242301291..5c850eeb54 100644 --- a/packages/interaction/user-questions/src/index.ts +++ b/packages/interaction/user-questions/src/index.ts @@ -286,9 +286,10 @@ export class UserQuestionService extends TypertRemoteService { * @throws {UserQuestionError} code `ASK_ABORTED` when the supplied signal * is already or becomes aborted, `CALLER_NOT_LIVE` when a supplied agent * is not the registry's exact live instance, `DELEGATED_CALLER` when - * that live agent has subagent origin, or `PROVIDER_ERROR` when an answerer - * fails with a value that is not a `HarnessError`; a coded `HarnessError` - * from an answerer keeps its `name` and `code`. + * that live agent has subagent origin, or `PROVIDER_ERROR`, keeping the + * original message, when an answerer fails with a value that is not a + * `HarnessError`; a coded `HarnessError` from an answerer keeps its `name` + * and `code`. */ async ask(request: AskUserQuestionRequest): Promise { if (request.signal?.aborted) { @@ -359,7 +360,8 @@ export class UserQuestionService extends TypertRemoteService { throw abortedQuestion(error) } if (restored instanceof HarnessError) throw restored - throw new UserQuestionError('the user-questions answerer failed before the user answered', 'PROVIDER_ERROR', { cause: error }) + const message = error instanceof Error ? error.message : 'the user-questions answerer failed before the user answered' + throw new UserQuestionError(message, 'PROVIDER_ERROR', { cause: error }) } } } diff --git a/packages/interaction/user-questions/tests/user-questions.spec.ts b/packages/interaction/user-questions/tests/user-questions.spec.ts index a4f3208de1..09a32fd961 100644 --- a/packages/interaction/user-questions/tests/user-questions.spec.ts +++ b/packages/interaction/user-questions/tests/user-questions.spec.ts @@ -174,12 +174,12 @@ describe('UserQuestionService', () => { }) it.each([ - ['an ordinary Error', new Error('provider failed')], + ['an ordinary Error', new Error('provider failed'), 'provider failed'], ['a namesake Error without a string code', Object.assign(new Error('provider failed'), { name: 'UserQuestionError', - })], - ['a non-Error rejection', { name: 'UserQuestionError', code: 'ASK_CANCELLED' }], - ])('classifies %s from the provider as PROVIDER_ERROR', async (_label, rejection) => { + }), 'provider failed'], + ['a non-Error rejection', { name: 'UserQuestionError', code: 'ASK_CANCELLED' }, 'the user-questions answerer failed before the user answered'], + ])('classifies %s from the provider as PROVIDER_ERROR', async (_label, rejection, message) => { const ctx = new Context() await ctx.plugin(UserQuestionService) registerAnswerer(ctx, { ask: vi.fn().mockRejectedValue(rejection) }) @@ -188,7 +188,7 @@ describe('UserQuestionService', () => { questions: [{ id: 'confirm', question: 'Proceed?' }], }).then(() => undefined, (error: unknown) => error) expect(failure).toBeInstanceOf(UserQuestionError) - expect(failure).toMatchObject({ name: 'UserQuestionError', code: 'PROVIDER_ERROR', cause: rejection }) + expect(failure).toMatchObject({ name: 'UserQuestionError', code: 'PROVIDER_ERROR', message, cause: rejection }) }) it('rejects empty question batches before reaching the provider', async () => { @@ -579,7 +579,7 @@ describe('askTimed', () => { registerAnswerer(ctx, { ask: () => Promise.reject(crash) }) await expect(ctx.userQuestions.askTimed({ agent, questions: timedQuestions }, timedCallId, 120_000)) - .rejects.toMatchObject({ name: 'UserQuestionError', code: 'PROVIDER_ERROR', cause: crash }) + .rejects.toMatchObject({ name: 'UserQuestionError', code: 'PROVIDER_ERROR', message: 'provider crashed', cause: crash }) await ctx.fiber.dispose() }) diff --git a/packages/plan/plan-mode/tests/plan-mode.spec.ts b/packages/plan/plan-mode/tests/plan-mode.spec.ts index 0ebe17cb31..b65e283886 100644 --- a/packages/plan/plan-mode/tests/plan-mode.spec.ts +++ b/packages/plan/plan-mode/tests/plan-mode.spec.ts @@ -1177,7 +1177,7 @@ describe('exit_plan_mode', () => { registerQuestionAnswerer(ctx, { ask: () => { throw new Error('review aborted') } }) const result = await callExit(ctx, agent) expect(result.isError).toBe(true) - expect(result.content).toEqual([{ type: 'text', text: 'Error: the user-questions answerer failed before the user answered' }]) + expect(result.content).toEqual([{ type: 'text', text: 'Error: review aborted' }]) expect(result.error?.info).toEqual({ name: 'UserQuestionError', code: 'PROVIDER_ERROR' }) expect(foldPlanMode(agent.session.snapshotEvents())).toBe(true) })