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
This commit is contained in:
sjw1231 committed 2026-11-06 18:02:08 +08:00
1 parent d212c0629a
commit fda29f726d
6 files changed
+25 -21

No files matched your search

+2 -2
View File
@@ -24,5 +24,5 @@
en: 74fa698d4a115e80
zh: 4e9b03b5eab2ae5e
/user-interaction/errors:
en: eb0a4f94282f8c18
zh: 92c41869431b120e
en: 832b165cfa48839d
zh: 6989f734b373c1f9
+5 -4
View File
@@ -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<AskUserQuestionAnswer>
```
+5 -4
View File
@@ -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<AskUserQuestionAnswer>
```
@@ -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<AskUserQuestionAnswer> {
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 })
}
}
}
@@ -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()
})
@@ -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)
})