feat: add background ask user questions#315
Conversation
🦋 Changeset detectedLatest commit: 75007d7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a1a558be7e
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| try { | ||
| taskId = backgroundManager.registerTask( | ||
| new QuestionBackgroundTask( | ||
| (taskSignal) => this.executeQuestion(args, { toolCallId, turnId, signal: taskSignal }), |
There was a problem hiding this comment.
Cancel the pending dialog when stopping background questions
When a background AskUserQuestion is stopped via TaskStop, this task signal only aborts the caller-side RPC wait; in the real SDK RPC path (packages/agent-core/src/rpc/client.ts::mapRpcFunction) the abort option is not forwarded to the question handler, so the QuestionController.show() promise and modal remain pending. The task is marked killed, but the user still sees a stale question dialog until they answer or the session cancels all dialogs, so TaskStop does not actually cancel the background question in the interactive TUI.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1ae05def70
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| export const AskUserQuestionInputSchema: z.ZodType<AskUserQuestionInput> = | ||
| flags.enabled('background-ask') | ||
| ? AskUserQuestionInputSchemaWithBackground | ||
| : AskUserQuestionInputBaseSchema; |
There was a problem hiding this comment.
Recompute the question schema when flags change
When KIMI_CODE_EXPERIMENTAL_BACKGROUND_ASK (or the master flag) is enabled after this module has already been imported, this exported schema stays stuck in the earlier disabled state even though description and resolveExecution read flags.enabled('background-ask') live. In that SDK/host setup the tool advertises and executes background=true, but the model-facing parameters still omit background and reject it via additionalProperties: false, so the feature cannot be used unless the env was set before the first import. Build the schema at tool construction time (or otherwise read the flag live) to keep it consistent with the rest of the flag checks.
Useful? React with 👍 / 👎.
| if (args.background === true && flags.enabled('background-ask')) { | ||
| return this.executeInBackground(args, { toolCallId, turnId, signal }); |
There was a problem hiding this comment.
Gate background questions on task-management tools
When a profile enables AskUserQuestion but does not enable TaskOutput/TaskStop (a supported configuration; Bash and Agent already disable background mode in this case), this branch still registers a background question and returns instructions to manage it with tools the model cannot call. That leaves the agent unable to inspect the answer before the automatic notification or cancel a no-longer-needed question from its own tool flow. Pass the same task-management availability check into this tool and fall back to inline questions when those tools are inactive.
Useful? React with 👍 / 👎.
Related Issue
No linked issue. This addresses a requested workflow gap where a structured user question can block the agent even when the agent can continue with other work while waiting for the answer.
Problem
AskUserQuestionpreviously only supported a foreground reverse-RPC flow: the tool call waited until the user answered or dismissed the dialog. That is right when the next step depends on the answer, but it is unnecessarily blocking when the question is optional, long-running, or can be answered while the agent continues a parallel path.What changed
Added a
backgroundparameter toAskUserQuestion. When set, the tool registers aquestion-*background task through the existing background task manager, immediately returns the task ID, and writes the eventual structured answer into the task output. The existing question RPC method remains unchanged; the background task simply calls the same request path under the task lifecycle and can be inspected withTaskOutputor stopped withTaskStop.The TUI now recognizes question background tasks in the transcript, replay, tool-call card, and
/tasksbrowser with minimal display support. The SDK exports the new question task info type, and the reference docs plus changeset were updated.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.Verification:
pnpm vitest run packages/agent-core/test/tools/ask-user.test.ts packages/agent-core/test/tools/input-schema-io.test.ts apps/kimi-code/test/tui/background-task-status.test.ts apps/kimi-code/test/tui/tasks-browser.test.ts apps/kimi-code/test/tui/components/messages/tool-call.test.ts apps/kimi-code/test/tui/kimi-tui-message-flow.test.tspnpm --filter @moonshot-ai/agent-core typecheckpnpm --filter @moonshot-ai/kimi-code typecheckpnpm --filter @moonshot-ai/kimi-code-sdk typecheckpnpm exec oxlint packages/agent-core/src/agent/background/question-task.ts packages/agent-core/src/tools/builtin/collaboration/ask-user.ts apps/kimi-code/src/tui/components/dialogs/tasks-browser.ts apps/kimi-code/src/tui/components/messages/tool-call.ts apps/kimi-code/src/tui/controllers/session-event-handler.ts apps/kimi-code/src/tui/controllers/session-replay.ts apps/kimi-code/src/tui/utils/background-task-status.ts packages/node-sdk/src/types.tsgit diff --checkNote: the
gen-docsworkflow referencesdocs/scripts/sync-changelog.mjs, but that script is not present in this checkout, so the changelog sync command could not run. The affected bilingual reference docs were updated directly.