Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions agent/insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,18 +264,21 @@ def _get_tool_usage(self, cutoff: float, source: str = None) -> List[Dict]:
except (json.JSONDecodeError, TypeError, AttributeError):
continue

# Merge: prefer tool_name source, supplement with tool_calls source
# for tools not already counted
# Merge: combine tool_name and tool_calls sources.
# These record tool usage via different paths (tool_name from tool
# execution results, tool_calls from assistant-side call records).
# Across mixed session sources (gateway / CLI / ACP), the two may
# capture disjoint sets of calls, so we sum rather than take max.
if not tool_counts and tool_calls_counts:
# No tool_name data at all — use tool_calls exclusively
tool_counts = tool_calls_counts
elif tool_counts and tool_calls_counts:
# Both sources have data — use whichever has the higher count per tool
# (they may overlap, so take the max to avoid double-counting)
# Both sources present — sum counts since they may capture
# disjoint tool calls across different session sources.
all_tools = set(tool_counts) | set(tool_calls_counts)
merged = Counter()
for tool in all_tools:

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.

This sums globally aggregated counters, so it doubles ordinary completed calls that have both assistant tool_calls and a tool-result tool_name row. Merge by (session_id, tool_name) first, then aggregate by tool name.

merged[tool] = max(tool_counts.get(tool, 0), tool_calls_counts.get(tool, 0))
merged[tool] = tool_counts.get(tool, 0) + tool_calls_counts.get(tool, 0)
tool_counts = merged

# Convert to the expected format
Expand Down