aggregate
Roll up row dicts by a group key to compute metrics such as counts, sessions, edits, and tokens for auditing AI coding agent history.
Instructions
Roll a list of row dicts up by group_by — the generic stats verb.
Reproduces session_stats (group_by ∈ agent/dir/date/
kind over a session inventory) and file_frequency
(group_by="file" over a find_file_edits record stream) as a pure
fold over already-materialized rows — no re-parsing. session_stats is
now a thin preset over this verb (rank_by="stats" + kind_split).
Args:
rows: The row dicts to fold (query output, find_file_edits
records, or a session inventory).
group_by: The bucket key — a row field name (agent / dir /
date / kind / file / model — query rows carry
the producing model where the format records one / …).
Missing/empty values bucket under "(unknown)".
metrics: Which numbers each bucket carries. One or more of
count / sessions / edits / intents / agents /
messages / files / tokens / component_tokens.
Defaults to ["count"].
tokens (F3.3) folds per-row tokens blocks (the shape
session_stats(with_tokens=True) rows carry, or a bare int
total) into {input, output, reasoning, cache_read, cache_write, total, exact, estimated, unknown} — sums over
rows that carry each field (null when none does) plus
honest provenance counters (exact + estimated + unknown == len(rows)).
component_tokens (F3.3) folds per-row component_tokens
blocks (the shape :func:ai_r.tokens.component_tokens produces,
as read_session(with_tokens=True) attaches) into summed
event-taxonomy components (user_turn / assistant_turn /
thinking / plan and a tool_call per-kind sub-dict) +
total + provenance counters (estimated / unknown;
never exact — always an estimate). A component/kind no row
carried stays absent (never a fabricated 0).
rank_by: Group ordering — "default" (edits→sessions→count→label,
the file_frequency order) or "stats" (sessions→edits→label,
the session_stats order).
kind_split: When True, add the session_stats RISK-4 fields
(kind_split_available + a degenerate-split note).
Returns:
{"group_by", "groups": [...], "totals": {...}} (plus
kind_split_available/note when kind_split) or the standard
{"error": ..., "message": ...} dict on an unknown metric/rank_by.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| rows | Yes | ||
| metrics | No | ||
| rank_by | No | default | |
| group_by | Yes | ||
| kind_split | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||