Add DuoToolCalls aggregation engine for Duo Chat tool calls

Stacked on !264993 (merged), which adds the engine, its specs and the development fixture. This MR only mounts the engine on GraphQL: the type, the three config/authz/graphql/authorization_todo.txt entries, the request spec and the regenerated docs and introspection. Its diff is shown against that branch.

Related to #633445 (closed)

What does this MR do?

Adds a ClickHouse aggregation engine, Analytics::AggregationEngines::DuoToolCalls, over the ai_usage_events table. It covers only the two Duo Chat tool call events: tool_succeeded_duo_chat and tool_failed_duo_chat.

The engine is mounted on the Analytics GraphQL type as duoToolCalls. It is an experiment in milestone 19.6. It requires the read_pro_ai_analytics ability, like the other Duo engines. The generic duoUsageEvents engine gets no new fields, as the issue asks.

  • Dimensions: toolName, outcome (succeeded or failed), toolAccess (read, write or NULL), flowType, user, group, timestamp.
  • Metrics: totalCount, usersCount, sessionsCount, writeSessionsCount, readOnlySessionsCount, successRate (NULL when there are no calls).
  • Filters: toolName (in and not in), outcome, toolAccess, flowType (in and not in), userId (in and not in), groupId, timestamp range.

toolAccess is derived in the query from the Ai::ToolRules::Registry privilege groups. These are constants, so there is no database lookup. Destroy tools such as run_command count as write. Tool names the registry does not know, for example MCP server tools, are NULL. A session that called such a tool does not count as read-only.

readOnlySessionsCount uses a window function over the per-row groups the framework already builds. This is the first engine to do that, so reviewers may want to look at that expression. The window sorts every tool call in scope by session before the outer aggregate; a per-session CTE was not an option because supporting_cte joins on a table column and the session ID lives in the extras JSON. The metric is evaluated within the applied filters, which the field description says.

The generated DuoToolCalls* GraphQL types are added to config/authz/graphql/authorization_todo.txt, next to the other Duo engines: read_pro_ai_analytics has no granular token permission definition yet, so the field cannot carry the granular authorization directive the mergeRequests engine uses.

Business impact

Duo Chat is a large share of Duo spend and no customer-facing surface shows what it did. Tool calls (tool_succeeded_duo_chat, tool_failed_duo_chat) are the only signal we record. This field lets a group owner ask which tools chat calls most, which fail most, and how many sessions only read versus made changes, with the same read_pro_ai_analytics access as the other Duo engines. No new data is collected; the read/write grouping comes from the tool rules registry. One ClickHouse aggregation per request over the group's tool-call events in the requested range; no engine-specific limits beyond the framework's. An unauthorised caller gets null, as for the sibling engines.

Queries this change generates

ClickHouse SQL the engine compiles for the GraphiQL query below (obtained with rails runner; the two IN (...) lists come from Ai::ToolRules::Registry and are inlined as string literals):

SELECT inner.aeq_tool_name AS aeq_tool_name, inner.aeq_tool_access AS aeq_tool_access,
       COUNT(*) AS aeq_total_count,
       countIf(inner.aeq_success_rate = 1) / nullIf(COUNT(*), 0) AS aeq_success_rate
FROM (
  SELECT nullIf(JSONExtractString(extras, 'tool_name'), '') AS aeq_tool_name,
         multiIf(nullIf(JSONExtractString(extras, 'tool_name'), '') IN (<read tool names>), 'read',
                 nullIf(JSONExtractString(extras, 'tool_name'), '') IN (<write tool names>), 'write',
                 NULL) AS aeq_tool_access,
         event = <tool_succeeded_duo_chat> AS aeq_success_rate,
         ai_usage_events.traversal_path, ai_usage_events.event, ai_usage_events.timestamp, ai_usage_events.user_id
  FROM ai_usage_events
  WHERE startsWith(ai_usage_events.traversal_path, '<org_id>/<group_id>/')
    AND ai_usage_events.event IN (<tool_succeeded_duo_chat>, <tool_failed_duo_chat>)
    AND ai_usage_events.timestamp BETWEEN <from_epoch> AND <to_epoch>
  GROUP BY ALL
) inner
GROUP BY ALL
ORDER BY aeq_total_count DESC
LIMIT 501 OFFSET 0

The session metrics add these inner expressions (captured from the engine spec, log/clickhouse.log); readOnlySessionsCount is the first window function in an engine:

nullIf(JSONExtractInt(extras, 'session_id'), 0)                                           AS aeq_sessions_count,
if(<tool_access expr> = 'write', nullIf(JSONExtractInt(extras, 'session_id'), 0), NULL)   AS aeq_write_sessions_count,
if(min(any(ifNull(<tool_access expr> = 'read', 0)))
     OVER (PARTITION BY any(nullIf(JSONExtractInt(extras, 'session_id'), 0))) = 1,
   any(nullIf(JSONExtractInt(extras, 'session_id'), 0)), NULL)                            AS aeq_read_only_sessions_count
-- outer: COUNT(DISTINCT ...) over each

ai_usage_events is partitioned by month on timestamp, so the range prunes partitions. No PostgreSQL query beyond the usual association resolution for user/group dimensions.

Tests

  • Engine spec: ee/spec/models/analytics/aggregation_engines/duo_tool_calls_spec.rb. It has 13 examples and passes locally against ClickHouse.
  • GraphQL request spec: ee/spec/requests/api/graphql/analytics/ai_analytics/duo_tool_calls_spec.rb.
  • GraphQL docs and introspection are regenerated.

Follow-ups

These will be separate MRs:

  • The GLQL data source in gitlab-org/glql, plus the gem bump.
  • The GLQL docs page.

How to test

Run this in GraphiQL as a user with access to the group:

query {
  group(fullPath: "gitlab-org") {
    analytics {
      duoToolCalls {
        aggregated(orderBy: [{ identifier: "totalCount", direction: DESC }]) {
          nodes { dimensions { toolName toolAccess } totalCount successRate }
        }
      }
    }
  }
}

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Chandra Saripaka

Merge request reports

Loading
Loading