Skip to content

Fix tool call numbering to maintain sequential order across iterations - #885

Merged
aantn merged 1 commit into
masterfrom
fix-tool-numbering-sequence
Aug 21, 2025
Merged

aantn merged 1 commit into
masterfrom
fix-tool-numbering-sequence

Conversation

@pavangudiwada

Copy link
Copy Markdown
Contributor

Before

image

After

CleanShot 2025-08-21 at 10 15 59@2x

@pavangudiwada
pavangudiwada requested a review from aantn August 21, 2025 04:53
@coderabbitai

coderabbitai Bot commented Aug 21, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces a tool_number_offset to ensure sequential tool_number values across batches and in both non-streaming (call) and streaming (call_stream) modes, incrementing the offset after each completed batch of tool invocations. No public API changes.

Changes

Cohort / File(s) Summary of changes
Tool numbering offset for sequential tool invocations
holmes/core/tool_calling_llm.py
Add tool_number_offset; compute tool_number = offset + local_index for each tool call in both call and call_stream; increment offset after each batch; minor comment/order clarifications; no signature changes.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as Caller
  participant L as ToolCallingLLM
  participant T as Tool(s)

  rect rgb(245,248,255)
    note over L: Initialize tool_number_offset = 0
    U->>L: call(...) or call_stream(...)
    alt Non-streaming
      loop For each batch
        L->>T: invoke(tool, tool_number = offset + local_index)
        T-->>L: result
        note over L: After batch completes<br/>offset += batch_tool_count
      end
    else Streaming
      loop For each streamed batch
        L->>T: invoke(tool, tool_number = offset + local_index)
        T-->>L: partial result(s)
        note over L: After batch completes<br/>offset += batch_tool_count
      end
    end
    L-->>U: aggregated result(s)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

Possibly related PRs

  • fix parallel tool calling #661 — Adjusts tool invocation to accept a tool_number and pass it into tool.invoke; closely related to this PR’s introduction of tool_number_offset for global sequencing.

Suggested reviewers

  • arikalon1

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-tool-numbering-sequence

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
holmes/core/tool_calling_llm.py (3)

406-408: Sequential tool-number offset update: correct; prefer basing on futures count and add a debug range log

Using the batch size to advance the offset is right. To make this resilient to any future change that might skip scheduling some tools, increment by the number of scheduled futures and add a small debug log for traceability.

Apply this diff:

-                # Update the tool number offset for the next iteration
-                tool_number_offset += len(tools_to_call)
+                # Update the tool number offset for the next iteration
+                num_tools_in_batch = len(futures)
+                tool_number_offset += num_tools_in_batch
+                logging.debug(
+                    f"Assigned tool_number range {tool_number_offset - num_tools_in_batch + 1}..{tool_number_offset} for iteration {i}"
+                )

723-725: Increment offset after batch completion: base on scheduled futures and add debug

Same nit as the non-streaming path: advance by the number of scheduled futures and emit a helpful debug line.

Apply this diff:

-                # Update the tool number offset for the next iteration
-                tool_number_offset += len(tools_to_call)
+                # Update the tool number offset for the next iteration
+                num_tools_in_batch = len(futures)
+                tool_number_offset += num_tools_in_batch
+                logging.debug(
+                    f"Assigned tool_number range {tool_number_offset - num_tools_in_batch + 1}..{tool_number_offset} for iteration {i}"
+                )

702-703: Surface tool_number in streaming events

Verified findings:

  • The only invoke definition is in holmes/core/tools_utils/tool_executor.py:49, which currently accepts only (tool_name: str, params: dict) and does not include a tool_number parameter.
  • There are no remaining references to tool_index in the codebase.
  • No existing streaming consumers (Python or JS/TS) handle tool_number in START_TOOL or TOOL_RESULT events.

Recommended optional refactoring:

  • holmes/core/tools_utils/tool_executor.py (line 49):
    Change

    def invoke(self, tool_name: str, params: dict) -> StructuredToolResult:

    to

    def invoke(self, tool_name: str, params: dict, tool_number: int) -> StructuredToolResult:

    and propagate tool_number into the underlying tool call.

  • holmes/core/tool_calling_llm.py:
    • After submitting each tool invocation, yield a StreamMessage(START_TOOL) with

    data={"tool_name": t.function.name, "id": t.id, "tool_number": assigned_tool_number}

    • When emitting TOOL_RESULT, include "tool_number": assigned_tool_number in the payload.

  • Downstream clients/UI:
    Ensure any consumers of START_TOOL and TOOL_RESULT extract and display the new tool_number field so tool calls are consistently ordered in the UI/telemetry.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 6550389 and c3bcd89.

📒 Files selected for processing (1)
  • holmes/core/tool_calling_llm.py (4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

📄 CodeRabbit Inference Engine (CLAUDE.md)

**/*.py: ALWAYS place Python imports at the top of the file, not inside functions or methods
Type hints are required (project is type-checked with mypy)
Use Ruff for formatting and linting (configured in pyproject.toml)

Files:

  • holmes/core/tool_calling_llm.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: build (3.12)
  • GitHub Check: build (3.10)
  • GitHub Check: build (3.11)
  • GitHub Check: Pre-commit checks
  • GitHub Check: llm_evals
🔇 Additional comments (1)
holmes/core/tool_calling_llm.py (1)

608-609: Initialize tool_number_offset in streaming path — LGTM

This correctly resets numbering per streaming session and aligns with the non-streaming behavior.

@github-actions

Copy link
Copy Markdown
Contributor

Results of HolmesGPT evals

  • ask_holmes: 32/39 test cases were successful, 2 regressions, 2 skipped, 2 setup failures
Test suite Test case Status
ask 01_how_many_pods ✅
ask 02_what_is_wrong_with_pod ✅
ask 03_what_is_the_command_to_port_forward ❌
ask 04_related_k8s_events ↪️
ask 05_image_version ✅
ask 09_crashpod ✅
ask 10_image_pull_backoff ✅
ask 11_init_containers ✅
ask 14_pending_resources ✅
ask 15_failed_readiness_probe ✅
ask 17_oom_kill ✅
ask 18_crash_looping_v2 ✅
ask 19_detect_missing_app_details ✅
ask 20_long_log_file_search ✅
ask 24_misconfigured_pvc ✅
ask 28_permissions_error 🚧
ask 29_events_from_alert_manager ↪️
ask 39_failed_toolset ✅
ask 41_setup_argo ✅
ask 42_dns_issues_steps_new_tools ⚠️
ask 43_current_datetime_from_prompt ✅
ask 45_fetch_deployment_logs_simple ✅
ask 51_logs_summarize_errors ✅
ask 53_logs_find_term ✅
ask 54_not_truncated_when_getting_pods ✅
ask 59_label_based_counting ✅
ask 60_count_less_than 🚧
ask 61_exact_match_counting ✅
ask 63_fetch_error_logs_no_errors ✅
ask 79_configmap_mount_issue ✅
ask 83_secret_not_found ✅
ask 86_configmap_like_but_secret ✅
ask 93_calling_datadog ✅
ask 93_calling_datadog ✅
ask 93_calling_datadog ✅
ask 97_logs_clarification_needed ❌
ask 110_k8s_events_image_pull ✅
ask 24a_misconfigured_pvc_basic ✅
ask 13a_pending_node_selector_basic ✅

Legend

  • ✅ the test was successful
  • ↪️ the test was skipped
  • ⚠️ the test failed but is known to be flaky or known to fail
  • 🚧 the test had a setup failure (not a code regression)
  • 🔧 the test failed due to mock data issues (not a code regression)
  • ❌ the test failed and should be fixed before merging the PR

@aantn
aantn enabled auto-merge (squash) August 21, 2025 06:03

@aantn aantn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM. Thanks.

@aantn
aantn merged commit 9746fa6 into master Aug 21, 2025
10 of 11 checks passed
@aantn
aantn deleted the fix-tool-numbering-sequence branch August 21, 2025 06:03
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.

2 participants