Skip to content

Conversation

@nherment
Copy link
Collaborator

@nherment nherment commented Oct 2, 2025

No description provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 2, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Introduces ToolInvokeContext across core, tools, plugins, and tests; updates tool invocation signatures to use context; centralizes token-limit utilities; adds token-based counting and truncation for tool outputs; adjusts tool result formatting to include params; refactors Prometheus tools to token-based sizing and structured responses; updates tests accordingly.

Changes

Cohort / File(s) Summary
Core models and formatting
holmes/core/models.py
ToolCallResult.as_tool_call_message now always uses format_tool_result_data; formatter prepends Params used header when params exist.
LLM tool calling flow
holmes/core/tool_calling_llm.py
Creates and passes ToolInvokeContext (incl. max tokens via get_max_token_count_for_single_tool) to tool.invoke.
Tool base and context model
holmes/core/tools.py
Adds ToolInvokeContext; changes Tool.invoke and abstract _invoke to accept context; propagates context through YAMLTool and logging.
Token counting utilities
holmes/core/tools_utils/token_counting.py
Adds count_tool_response_tokens to measure tokens for a StructuredToolResult via LLM.
Context window limiter
holmes/core/tools_utils/tool_context_window_limiter.py
Adds get_pct_token_count, get_max_token_count_for_single_tool; rewrites prevent_overly_big_tool_response to centralize max-token logic and error handling.
Tool executor
holmes/core/tools_utils/tool_executor.py
invoke now requires context and forwards it to tool.invoke; not-found handling preserved.
Logging API and truncation
holmes/plugins/toolsets/logging_utils/logging_api.py
PodLoggingTool._invoke uses context; adds token-based truncate_logs, TRUNCATION_PROMPT_PREFIX, MIN_NUMBER_OF_CHARACTERS_TO_TRUNCATE; integrates token counting.
Prometheus toolset refactor
holmes/plugins/toolsets/prometheus/prometheus.py
All _invoke methods accept context; introduces MetricsBasedResponse and create_structured_tool_result; switches to token-based size limits using count_tool_response_tokens and get_pct_token_count; config uses query_response_size_limit_pct.
Plugins: signature migration to context
holmes/plugins/toolsets/atlas_mongodb/mongodb_atlas.py, holmes/plugins/toolsets/azure_sql/tools/*, holmes/plugins/toolsets/bash/bash_toolset.py, holmes/plugins/toolsets/datadog/toolset_datadog_*.py, holmes/plugins/toolsets/git.py, holmes/plugins/toolsets/grafana/toolset_grafana*.py, holmes/plugins/toolsets/internet/*, holmes/plugins/toolsets/investigator/core_investigation.py, holmes/plugins/toolsets/kafka.py, holmes/plugins/toolsets/mcp/toolset_mcp.py, holmes/plugins/toolsets/newrelic/newrelic.py, holmes/plugins/toolsets/opensearch/*.py, holmes/plugins/toolsets/rabbitmq/toolset_rabbitmq.py, holmes/plugins/toolsets/robusta/robusta.py, holmes/plugins/toolsets/runbook/runbook_fetcher.py, ...
Replace _invoke(self, params, user_approved=False) with _invoke(self, params, context: ToolInvokeContext). Bodies largely unchanged; some use context.user_approved; added imports where needed.
Tests: context plumbing and helpers
tests/conftest.py
Adds MockLLM and create_mock_tool_invoke_context to build ToolInvokeContext for tests.
Tests: executor and transformers
tests/core/test_tool_executor.py, tests/core/test_tool_transformers.py, tests/core/test_todo_write_tool.py, tests/core/tools_utils/test_tool_context_window_limiter.py
Update calls to pass context; adjust test tools’ _invoke signatures; remove specific token-count call assertions.
Tests: mock toolset machinery
tests/llm/utils/mock_toolset.py, tests/llm/utils/test_mock_toolset.py
Propagate context through mock/live invoke paths; update MockableToolWrapper and ToolExecutor usage; richer logging on errors.
Tests: plugins updates
tests/plugins/toolsets/**
Update all tool invocations to include context; add imports; add truncation tests for logging API; Prometheus tests now pass context.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant U as ToolCallingLLM
  participant T as Tool
  participant C as ToolInvokeContext
  participant L as LLM
  participant Lim as ContextWindowLimiter
  participant Tok as TokenCounter

  U->>U: get_max_token_count_for_single_tool(LLM)
  U->>C: Build context (tool_number, user_approved, llm, max_token_count)
  U->>T: invoke(params, context=C)
  activate T
  T->>T: _invoke(params, context) [tool-specific]
  T-->>U: StructuredToolResult
  deactivate T

  U->>Tok: count_tool_response_tokens(LLM, result)
  Tok-->>U: token_count

  U->>Lim: prevent_overly_big_tool_response(result, LLM)
  Lim-->>U: possibly adjusted result (ERROR if over limit)

  U-->>U: format_tool_result_data(result) incl. Params used
  U-->>U: as_tool_call_message(content=formatted)
Loading
sequenceDiagram
  autonumber
  participant P as PodLoggingTool
  participant C as ToolInvokeContext
  participant L as LLM
  participant Tr as truncate_logs

  P->>P: fetch logs (params)
  P->>Tr: truncate_logs(result, L, C.max_token_count, params)
  Tr->>L: count_tokens_for_message(...)
  alt tokens > limit
    Tr->>Tr: iterative truncation with prefix
    Tr-->>P: shortened result
  else
    Tr-->>P: original result
  end
  P-->>Caller: StructuredToolResult
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~75 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • moshemorad
  • arikalon1
  • aantn
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ROB-2136_increase_prometheus_tool_limit

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3cb2846 and c3dd967.

📒 Files selected for processing (66)
  • holmes/core/models.py (2 hunks)
  • holmes/core/tool_calling_llm.py (2 hunks)
  • holmes/core/tools.py (6 hunks)
  • holmes/core/tools_utils/token_counting.py (1 hunks)
  • holmes/core/tools_utils/tool_context_window_limiter.py (1 hunks)
  • holmes/core/tools_utils/tool_executor.py (2 hunks)
  • holmes/plugins/toolsets/atlas_mongodb/mongodb_atlas.py (7 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/analyze_connection_failures.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/analyze_database_connections.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/analyze_database_health_status.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/analyze_database_performance.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/analyze_database_storage.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/get_active_alerts.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/get_slow_queries.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/get_top_cpu_queries.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/get_top_data_io_queries.py (2 hunks)
  • holmes/plugins/toolsets/azure_sql/tools/get_top_log_io_queries.py (2 hunks)
  • holmes/plugins/toolsets/bash/bash_toolset.py (4 hunks)
  • holmes/plugins/toolsets/datadog/toolset_datadog_general.py (4 hunks)
  • holmes/plugins/toolsets/datadog/toolset_datadog_metrics.py (5 hunks)
  • holmes/plugins/toolsets/datadog/toolset_datadog_rds.py (3 hunks)
  • holmes/plugins/toolsets/datadog/toolset_datadog_traces.py (4 hunks)
  • holmes/plugins/toolsets/git.py (6 hunks)
  • holmes/plugins/toolsets/grafana/toolset_grafana.py (2 hunks)
  • holmes/plugins/toolsets/grafana/toolset_grafana_tempo.py (9 hunks)
  • holmes/plugins/toolsets/internet/internet.py (2 hunks)
  • holmes/plugins/toolsets/internet/notion.py (2 hunks)
  • holmes/plugins/toolsets/investigator/core_investigation.py (2 hunks)
  • holmes/plugins/toolsets/kafka.py (7 hunks)
  • holmes/plugins/toolsets/logging_utils/logging_api.py (5 hunks)
  • holmes/plugins/toolsets/mcp/toolset_mcp.py (2 hunks)
  • holmes/plugins/toolsets/newrelic/newrelic.py (2 hunks)
  • holmes/plugins/toolsets/opensearch/opensearch.py (5 hunks)
  • holmes/plugins/toolsets/opensearch/opensearch_traces.py (3 hunks)
  • holmes/plugins/toolsets/prometheus/prometheus.py (15 hunks)
  • holmes/plugins/toolsets/rabbitmq/toolset_rabbitmq.py (3 hunks)
  • holmes/plugins/toolsets/robusta/robusta.py (4 hunks)
  • holmes/plugins/toolsets/runbook/runbook_fetcher.py (2 hunks)
  • holmes/plugins/toolsets/servicenow/servicenow.py (4 hunks)
  • tests/conftest.py (2 hunks)
  • tests/core/test_todo_write_tool.py (5 hunks)
  • tests/core/test_tool_executor.py (2 hunks)
  • tests/core/test_tool_transformers.py (36 hunks)
  • tests/core/tools_utils/test_tool_context_window_limiter.py (0 hunks)
  • tests/core/transformers/test_llm_summarize.py (8 hunks)
  • tests/integration/test_kubernetes_transformer_execution.py (9 hunks)
  • tests/integration/test_tool_execution_pipeline.py (9 hunks)
  • tests/llm/fixtures/test_ask_holmes/54_azure_sql/demo_alerts_report.py (2 hunks)
  • tests/llm/utils/mock_toolset.py (4 hunks)
  • tests/llm/utils/test_mock_toolset.py (9 hunks)
  • tests/mocks/toolset_mocks.py (2 hunks)
  • tests/plugins/prompt/test_toolsets_instructions.py (2 hunks)
  • tests/plugins/toolsets/datadog/metrics/test_datadog_metrics.py (11 hunks)
  • tests/plugins/toolsets/datadog/metrics/test_datadog_metrics_live.py (12 hunks)
  • tests/plugins/toolsets/datadog/rds/test_datadog_rds_integration.py (10 hunks)
  • tests/plugins/toolsets/datadog/rds/test_datadog_rds_live.py (3 hunks)
  • tests/plugins/toolsets/datadog/test_toolset_datadog_general.py (3 hunks)
  • tests/plugins/toolsets/datadog/traces/test_datadog_traces.py (12 hunks)
  • tests/plugins/toolsets/datadog/traces/test_datadog_traces_live.py (8 hunks)
  • tests/plugins/toolsets/grafana/test_grafana_tempo_tools.py (24 hunks)
  • tests/plugins/toolsets/grafana/test_grafana_tempo_unit.py (8 hunks)
  • tests/plugins/toolsets/test_internet.py (2 hunks)
  • tests/plugins/toolsets/test_logging_api.py (7 hunks)
  • tests/plugins/toolsets/test_prometheus_integration.py (7 hunks)
  • tests/plugins/toolsets/test_runbook.py (1 hunks)
  • tests/plugins/toolsets/test_tool_kafka.py (8 hunks)

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@nherment nherment closed this Oct 2, 2025
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