← Back to Pull requests

docs: say that a deferred tool call runs at least once #1577

Open
unknown opened this pull request with 251 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 24, 2026 05:01 UTC
Author

Summary

A tool call deferred by a PreToolUse hook runs at least once, not exactly once. The process can stop after the tool starts and before its result is saved. Then the next resume fires PreToolUse again for the same call, and the tool can run a second time. Nothing in the SDK says this today.

This adds a note to the DeferredToolUse docstring. The note says:

  • A deferred call runs at least once, and why.

  • Tools with side effects should use the call id as an idempotency key. The hook receives the same value as tool_use_id and can pass it to the tool through updatedInput.

  • For an external service, send the key to the service and record that the call started. On resume, look up a started call that has no result before you run it again. Keys at the service can expire before a deferred session does.

  • The key covers only a replay of the same call. A new call in a later turn gets a new id.
  • The last two points come from @gioherazo in #1549.

    No behavior changes.

    Evidence

    Reproduced with the stub-model script from #1549 (a local fake API, no real model), 5 runs per row:

    | Kill point after the resumed tool starts | Tool runs after a second resume | With the tool_use_id key |
    |---|---:|---:|
    | In the tool, after the side effect | 2 (5 of 5) | 1 effect (5 of 5) |
    | In a PostToolUse hook | 2 (5 of 5) | 1 effect (5 of 5) |
    | No kill (control) | 1 (5 of 5) | 1 (5 of 5) |

    Platforms: Windows 11 with SDK 0.2.182 (CLI 2.1.325), and Linux with SDK 0.2.187 (CLI 2.1.330). Both gave the same results. The hook saw the same tool_use_id in all three runs of every test, and it was equal to deferred_tool_use.id. Details are in this comment.

    Testing

  • ruff check and ruff format --check pass on src/claude_agent_sdk/types.py.

  • mypy src/claude_agent_sdk/types.py passes.

  • pytest tests/ passes (1740 passed, 21 skipped, on Windows with Python 3.13).
  • Refs #1549

    Diff: 251 lines (+188 / -63) across 3 files Reviewer: tonydzi Review duration: 9702m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.39) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 251 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.331
    Real PR Conversation Timeline
    1 comments
    "Review active on #1577"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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: 39% (MEDIUM) · Higher = larger blast radius
    39%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 22.2% · Safe to merge
    22.2%