← Back to Pull requests

feat(agent-core-v2): add the WaitFor tool for waiting on background tasks #3060

Closed
unknown closed this pull request without merging on Aug 19, 2026 03:27 UTC
View on GitHub
unknown unknown commented on Aug 18, 2026 17:14 UTC
Author

Related Issue

No public issue — the problem is explained below.

Problem

In goal mode, once the model dispatches background work (sub-agents or background bash), its only way to "wait" is to end the turn. Goal mode then immediately starts a new turn; the model finds the tasks still unfinished and ends that turn again — an idle re-invocation loop that wastes goal turns and tokens. There is no primitive for waiting on a background task inside the current turn.

What changed

  • New built-in WaitFor(timeout, task_id?) tool (agent-core-v2 task domain, next to TaskList/TaskOutput/TaskStop): suspends the tool call inside the current turn until the waited task finishes or the timeout (required, ≤ 600s) elapses. No LLM requests are made while waiting; a user interruption aborts the wait and never touches the task. Reuses the existing IAgentTaskService.wait primitive; the no-task_id form races the tasks that were running at call time.

  • Timeline-shaped result: the finished task's metadata + output preview + output_path first, then a [completed_during_wait] section (metadata + TaskOutput pointers for tasks that finished within the window), then [still_running]. A timeout returns the still-running list and is not an error; an unknown task_id errors; no running tasks returns immediately.

  • Exactly-once result delivery: tasks reported by WaitFor are recorded as delivered via a durable task.waitDelivered record that folds into the existing undo/compaction-aware notification delivery set, so their automatic completion notification is suppressed. Already-enqueued notification requests are aborted, and a final enqueue-time recheck in notifyAgentTask closes the remaining build→enqueue race (caught by the new harness tests).

  • Goal mode continuation prompt now tells the model to call WaitFor instead of ending the turn when it is waiting on background tasks (both GOAL_CONTINUATION_PROMPT and goal-active-reminder.md).

  • Telemetry: new wait_for_completed event (outcome, timeout_ms, waited_ms, has_task_id, extra_completed_count).

  • Gating: wait_for experimental flag, default on (KIMI_CODE_EXPERIMENTAL_WAIT_FOR=false disables). Tool auto-approved like TaskList/TaskOutput; added to agent/coder/tower-worker profiles; docs (en/zh) updated; changeset included.
  • Tests: 30+ new tests — tool-level (schema, all outcomes, abort propagation), notification dedup timing (suppress on mark, abort pending, enqueue-time recheck, status-keyed, no cross-talk), restart/undo delivery discipline, harness end-to-end with real AgentTaskService, goal-mode scenarios (bash/sub-agent/timeout-retry/ten-turn chain), and deadlock-attempt cases (nested wait chains unwind leaf-first; cross-agent waits are rejected; never-settling tasks are bounded by the timeout).

    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 (.changeset/wait-for-tool.md, minor).

  • Updated docs (docs/en|zh/reference/tools.md, kept both locales in sync).
  • Diff: 406 lines (+304 / -102) across 5 files Reviewer: HelloWorldU Review duration: 612m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 406 lines diff across 5 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.359
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3060"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%