Skip to content

feat: add background ask user questions#315

Merged
kermanx merged 6 commits into
mainfrom
xtr/background-ask
Jun 2, 2026
Merged

feat: add background ask user questions#315
kermanx merged 6 commits into
mainfrom
xtr/background-ask

Conversation

@kermanx
Copy link
Copy Markdown
Collaborator

@kermanx kermanx commented Jun 2, 2026

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

AskUserQuestion previously 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 background parameter to AskUserQuestion. When set, the tool registers a question-* 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 with TaskOutput or stopped with TaskStop.

The TUI now recognizes question background tasks in the transcript, replay, tool-call card, and /tasks browser with minimal display support. The SDK exports the new question task info type, and the reference docs plus changeset were updated.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, 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.ts
  • pnpm --filter @moonshot-ai/agent-core typecheck
  • pnpm --filter @moonshot-ai/kimi-code typecheck
  • pnpm --filter @moonshot-ai/kimi-code-sdk typecheck
  • pnpm 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.ts
  • git diff --check

Note: the gen-docs workflow references docs/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.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 2, 2026

🦋 Changeset detected

Latest commit: 75007d7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/agent-core Minor
@moonshot-ai/kimi-code-sdk Minor
@moonshot-ai/kimi-code Minor
@moonshot-ai/migration-legacy Patch

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Jun 2, 2026

pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@75007d7
npx https://pkg.pr.new/@moonshot-ai/kimi-code@75007d7

commit: 75007d7

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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 }),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +88 to +91
export const AskUserQuestionInputSchema: z.ZodType<AskUserQuestionInput> =
flags.enabled('background-ask')
? AskUserQuestionInputSchemaWithBackground
: AskUserQuestionInputBaseSchema;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +128 to +129
if (args.background === true && flags.enabled('background-ask')) {
return this.executeInBackground(args, { toolCallId, turnId, signal });
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@kermanx kermanx merged commit 191059d into main Jun 2, 2026
8 checks passed
@kermanx kermanx deleted the xtr/background-ask branch June 2, 2026 14:12
@github-actions github-actions Bot mentioned this pull request Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant