fix(user-interaction): reject ask_user_question from delegated subagents

This commit is contained in:
Tianyi Cui
2026-08-02 11:43:44 +08:00
parent 48949c4576
commit 8d92a9bdaa
12 changed files with 153 additions and 9 deletions

View File

@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/ui/tool-ask-user/README.md
README.md: 8e779f4025c20cd200344efb7cb8cd6bc09ba64d
README.zh.md: acaffec0764404a0e0e842ffc2b4efdee8869c4f
README.md: d7866ff018ebfed5afbf105b1a20714490bdb818
README.zh.md: 18a1c8e9f958c174fc34f26a572d88b6c031d7f9

View File

@@ -54,4 +54,5 @@ Append-only; newly visible content follows the reusable request prefix and does
## Known Limitations and Deferred Work
- **A pending question blocks the tool call until the human answers** — the tool declares no `timeout-policy` budget; cancellation rides the turn's `exec.signal` only.
- **Delegated subagents cannot ask the user** — `ask_user_question` rejects calls from a delegated subagent with `DELEGATED_CALLER`; a child that needs a decision must delegate the question to the top-level agent.
- **Native answers render as JSON text** — the canonical value remains structured, but the model-facing result uses compact JSON rather than a richer content-block vocabulary.

View File

@@ -54,4 +54,5 @@
## 已知限制与暂缓事项
- **待处理问题会阻塞工具调用,直至用户作答**:该工具未声明 `timeout-policy` 预算;取消仅沿用当前轮次的 `exec.signal`
- **委托的子代理不能向用户提问**`ask_user_question` 会以 `DELEGATED_CALLER` 拒绝来自委托子代理的调用;需要决策的子代理必须把问题转交给顶层代理。
- **Native 回答渲染为 JSON 文本**:规范值仍为结构化数据,但模型侧结果使用紧凑 JSON而非更丰富的内容块词汇。

View File

@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import { Context } from 'cordis'
import { CallId } from '@deepseek-ai/dsh-llm'
import type { Agent } from '@deepseek-ai/dsh-agent'
import SessionStore from '@deepseek-ai/dsh-session'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
import ToolRegistry from '@deepseek-ai/dsh-tools'
import UserInteractionService, { type AskUserQuestionRequest } from '@deepseek-ai/dsh-user-interaction'
@@ -201,6 +202,7 @@ describe('ask_user_question tool', () => {
it('passes optional header and agent through to the user-interaction request', async () => {
const ctx = await setup()
await ctx.plugin(SessionStore)
const seen: AskUserQuestionRequest[] = []
ctx.userInteraction.registerProvider({
async ask(request) {
@@ -208,7 +210,8 @@ describe('ask_user_question tool', () => {
return { answers: [{ id: 'continue', selected: ['ok'] }] }
},
})
const agent = { id: 'main' } as unknown as Agent
const session = ctx.sessions.create(undefined, { meta: { delegationDepth: 0 } })
const agent = { session } as unknown as Agent
const result = await ctx.tools.execute({
signal: testToolSignal,
@@ -238,6 +241,34 @@ describe('ask_user_question tool', () => {
})
})
it('rejects a delegated subagent with a structured DELEGATED_CALLER error', async () => {
const ctx = await setup()
await ctx.plugin(SessionStore)
const seen: AskUserQuestionRequest[] = []
ctx.userInteraction.registerProvider({
async ask(request) {
seen.push(request)
return { answers: [{ id: 'continue', selected: ['ok'] }] }
},
})
const session = ctx.sessions.create(undefined, { meta: { delegationDepth: 1 } })
const agent = { session } as unknown as Agent
const result = await ctx.tools.execute({
signal: testToolSignal,
callId: CallId('ask-delegated'),
name: 'ask_user_question',
arguments: { questions: [{ id: 'continue', question: 'Continue?' }] },
agent,
})
expect(result).toMatchObject({
isError: true,
error: { info: { name: 'UserInteractionError', code: 'DELEGATED_CALLER' } },
})
expect(seen).toHaveLength(0)
})
it('returns a structured error for empty question batches', async () => {
const ctx = await setup()

View File

@@ -2,5 +2,5 @@
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/ui/user-interaction/README.md
README.md: d62e75d110b8be339c5f9449b0834320f695ac99
README.zh.md: 55258e85e56df2375ed8f195fa0b3b731a9cb816
README.md: 3459f915f2cd94d4083975440731661d8aeb9108
README.zh.md: 26d40e98dbcc15ef18a85cd98205defb765d4469

View File

@@ -18,7 +18,7 @@ Abstract user-interaction seam. It owns `ctx.userInteraction`, the service a mod
- `AskUserQuestionIntent``{ kind: 'plan-review', approve }`; the tagged presentation intent below.
- `AskUserQuestionAnswer``{ answers: [{ id, selected, custom? }] }`.
- `UserInteractionProvider` — UI implementation with `ask(request)`.
- `UserInteractionError``HarnessError` subclass with codes such as `EMPTY_QUESTIONS`, `BAD_INTENT`, `NO_PROVIDER`, `DUPLICATE_PROVIDER`, and `ASK_ABORTED`.
- `UserInteractionError``HarnessError` subclass with codes such as `EMPTY_QUESTIONS`, `BAD_INTENT`, `NO_PROVIDER`, `DUPLICATE_PROVIDER`, `ASK_ABORTED`, and `DELEGATED_CALLER`.
When an answer includes `custom`, `selected` is empty; custom text is an override rather than a supplement to selected choices. A UI may preserve a skipped item as `{ id, selected: [] }`, keeping the existing answer shape while retaining other answers in the batch.
@@ -32,7 +32,7 @@ This is the interface package. Model-facing consumers such as `@deepseek-ai/dsh-
## Model Experience
Indirectly, through `dsh-tool-ask-user`, which retains a successful provider answer as compact JSON or one of these failures: `Error: ask_user_question was aborted before the user answered`, `Error: ask_user_question requires at least one question`, `Error: no user-interaction provider is registered`, or `Error: <message>`. Waiting for the human adds no tokens.
Indirectly, through `dsh-tool-ask-user`, which retains a successful provider answer as compact JSON or one of these failures: `Error: ask_user_question was aborted before the user answered`, `Error: ask_user_question requires at least one question`, `Error: ask_user_question is unavailable to delegated subagents; delegate the question to the top-level agent`, `Error: no user-interaction provider is registered`, or `Error: <message>`. Waiting for the human adds no tokens.
#### KV Cache effect

View File

@@ -18,7 +18,7 @@
- `AskUserQuestionIntent``{ kind: 'plan-review', approve }`;即下文的带标签呈现意图。
- `AskUserQuestionAnswer``{ answers: [{ id, selected, custom? }] }`
- `UserInteractionProvider`:包含 `ask(request)` 的 UI 实现。
- `UserInteractionError``HarnessError` 的子类,包含 `EMPTY_QUESTIONS``BAD_INTENT``NO_PROVIDER``DUPLICATE_PROVIDER``ASK_ABORTED` 等代码。
- `UserInteractionError``HarnessError` 的子类,包含 `EMPTY_QUESTIONS``BAD_INTENT``NO_PROVIDER``DUPLICATE_PROVIDER``ASK_ABORTED``DELEGATED_CALLER` 等代码。
当回答包含 `custom` 时,`selected` 为空自定义文本是所选选项的替代而不是补充。UI 可以把跳过的条目保留为 `{ id, selected: [] }`,既维持现有回答形态,也保留该批次中的其他回答。
@@ -32,7 +32,7 @@
## 模型体验
间接地,通过 `dsh-tool-ask-user`:它会将成功的提供方回答保留为紧凑 JSON或返回以下失败之一`Error: ask_user_question was aborted before the user answered``Error: ask_user_question requires at least one question``Error: no user-interaction provider is registered``Error: <message>`。等待人类回答不会增加 token。
间接地,通过 `dsh-tool-ask-user`:它会将成功的提供方回答保留为紧凑 JSON或返回以下失败之一`Error: ask_user_question was aborted before the user answered``Error: ask_user_question requires at least one question``Error: ask_user_question is unavailable to delegated subagents; delegate the question to the top-level agent``Error: no user-interaction provider is registered``Error: <message>`。等待人类回答不会增加 token。
#### KV Cache 影响

View File

@@ -77,8 +77,15 @@ export class UserInteractionService extends Service {
/**
* Ask the active UI provider and wait for the user's answer.
*
* Human-interaction requests are only valid from a top-level agent: a
* delegated subagent has no human answerer in its own context, so asking
* there would block forever. This mirrors the goal tools' top-level-only
* authority (`create_goal` rejects non-top-level agents).
*
* @param request Questions, owner agent, and abort signal.
* @returns The answer chosen or typed by the human.
* @throws {UserInteractionError} code `DELEGATED_CALLER` when the calling
* agent is a delegated subagent (`session.header.delegationDepth > 0`).
*/
async ask(request: AskUserQuestionRequest): Promise<AskUserQuestionAnswer> {
if (request.signal?.aborted) {
@@ -87,6 +94,11 @@ export class UserInteractionService extends Service {
if (request.questions.length === 0) {
throw new UserInteractionError('ask_user_question requires at least one question', 'EMPTY_QUESTIONS')
}
if ((request.agent?.session.header.delegationDepth ?? 0) > 0) {
throw new UserInteractionError(
'ask_user_question is unavailable to delegated subagents; delegate the question to the top-level agent',
'DELEGATED_CALLER')
}
// A presentation intent asserts two things the types cannot: that the
// named approve label is one of this question's own options, and that a
// plan-review carries the plan it is a review of. A UI honouring the

View File

@@ -1,5 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { Context } from 'cordis'
import type { Agent } from '@deepseek-ai/dsh-agent'
import SessionStore from '@deepseek-ai/dsh-session'
import UserInteractionService, {
UserInteractionError,
type AskUserQuestionRequest,
@@ -84,6 +86,39 @@ describe('UserInteractionService', () => {
expect(p.ask).not.toHaveBeenCalled()
})
it('rejects a delegated subagent before reaching the provider', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
await ctx.plugin(UserInteractionService)
const p = { ask: vi.fn(async () => ({ answers: [] })) }
ctx.userInteraction.registerProvider(p)
const session = ctx.sessions.create(undefined, { meta: { delegationDepth: 1 } })
const agent = { session } as unknown as Agent
await expect(ctx.userInteraction.ask({
questions: [{ id: 'confirm', question: 'Proceed?' }],
agent,
})).rejects.toMatchObject({ name: 'UserInteractionError', code: 'DELEGATED_CALLER' })
expect(p.ask).not.toHaveBeenCalled()
})
it('still reaches the provider for a top-level agent (delegationDepth 0)', async () => {
const ctx = new Context()
await ctx.plugin(SessionStore)
await ctx.plugin(UserInteractionService)
const p = provider('yes')
ctx.userInteraction.registerProvider(p)
const session = ctx.sessions.create(undefined, { meta: { delegationDepth: 0 } })
const agent = { session } as unknown as Agent
const result = await ctx.userInteraction.ask({
questions: [{ id: 'confirm', question: 'Proceed?' }],
agent,
})
expect(result).toEqual({ answers: [{ id: 'confirm', selected: ['yes'] }] })
})
it('rejects an intent whose approve label names none of its own options', async () => {
const ctx = new Context()
await ctx.plugin(UserInteractionService)