Skip to content

feat: add full time-accounting macro for inter-turn gaps (5 categories) - #10

Merged
Jacob (JacobPEvans-personal) merged 9 commits into
mainfrom
feat/time-accounting-macro
Oct 20, 2026
Merged

Jacob (JacobPEvans-personal) merged 9 commits into
mainfrom
feat/time-accounting-macro

Conversation

@openbao-service-broker

@openbao-service-broker openbao-service-broker Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The existing AI Session Stall Classification (daily) saved search only classifies outlier stalls and discards everything else, so it cannot answer "where did every second of this session go." This adds a macro that does full, unfiltered time accounting instead.

Live saved-search SPL (pulled verbatim from the Splunk REST API), for reference — this is the pattern the new macro generalizes:

-- "AI Session Stall Classification (daily)", cron 41 6 * * *
index=claude sourcetype=claude:code earliest=-1d type=assistant
| sort 0 source -_time
| streamstats current=f last(_time) as nxt by source
| eval gap=nxt-_time
| where gap>300 AND gap<7200
| spath path=message.content{}.name output=tool
| spath path=message.content{}.type output=ctype
| eval kind=case(
    isnotnull(mvfind(tool,"AskUserQuestion")),"AskUserQuestion",
    isnotnull(mvfind(tool,"ExitPlanMode")),"ExitPlanMode",
    isnotnull(mvfind(tool,"Bash")),"Bash(perm-prompt-or-long-cmd)",
    isnotnull(mvfind(tool,"Agent")),"Agent-wait",
    isnotnull(tool),"other-tool",
    isnotnull(mvfind(ctype,"text")),"text-only(user-away)",
    1=1,"thinking/empty")
| stats count as stalls sum(gap) as lost_sec by kind
| eval lost_hours=round(lost_sec/3600,1)
| sort -lost_sec

What changed

  1. New macro claude_time_accounting_by_gap(sessionId) in default/macros.conf — removes the gap>300 AND gap<7200 outlier filter, uses sourcetype=claude:code* (wildcard, see point 2), and maps onto a 5-category taxonomy instead of the 6-bucket kind field above:

    • TOOL_EXEC — any tool call other than Agent/Task
    • SUBAGENT_WAIT — tool_use name is Agent or Task (spawned subagent)
    • HUMAN_IDLE — no tool_use, content type includes text (turn handed control back)
    • SYSTEM_OVERHEAD — no tool_use, no text content, gap<2s
    • MODEL_ACTIVE — no tool_use, no text content, gap>=2s (thinking/generating)

    Output fields: time_category, turns, gap_sec, gap_min.

    Call `claude_time_accounting_by_gap(<sessionId>)` for one session, or with sessionId="*" for fleet-wide accounting.

    Revision note: this started as a 4-bucket macro that folded SYSTEM_OVERHEAD into MODEL_ACTIVE (documented as a deliberate simplification, "not worth it until a real use case needs sub-2s granularity"). A downstream consumer building a dashboard against this exact macro hit that real use case, so it's now split out as its own bucket at a gap<2s threshold — noted in the macro comment as a starting guess, tunable once dashboard data shows the natural cutoff.

  2. Documented in default/props.conf (comment above [claude:code:session]): production events actually land as bare sourcetype=claude:code, not claude:code:session. This means claude_assistant_events, claude_all_session_events (both in macros.conf), and every claude_* stanza in eventtypes.conf currently match zero events at the ingestion boundary actually in use — they all hardcode the claude:code:session literal. I checked repo history (git log -- default/props.conf) and README/CHANGELOG for any indication this was already fixed; it hasn't been. The new macro avoids the bug by using sourcetype=claude:code* directly. Fixing the existing macros/eventtypes is a separate, larger change (it'd touch the two live saved searches and any dashboards depending on them) and is left as a follow-up rather than folded into this PR.

    Revision note (commit 1f1b928): an earlier version of this comment additionally claimed subagent transcripts get datatype-stamped as claude:code:subagent. That claim was wrong — I verified directly against production (7-day window, 146,804 events): claude:code:subagent has zero hits, ever. Every event, main session and subagent alike, lands as bare claude:code. The only reliable way to distinguish a subagent transcript is the source file path containing /subagents/ — sourcetype gives no signal for this at all. Corrected the comment and flagged the now-confirmed-dormant [claude:code:subagent] stanza in props.conf accordingly. This macro was never affected (it already used the source-partitioning-agnostic sourcetype=claude:code* wildcard and never filtered on the subagent sourcetype), but the documentation was actively misleading anyone who'd try to write a query filtering sourcetype=claude:code:subagent expecting it to work.

Validation

I ran the extended/unfiltered pattern directly against production Splunk myself (via vct-splunk-cli, sourcetype=claude:code* sessionId="c024f242-4203-408c-b8bc-c8d4dc8e7f11" earliest=-6h) and got sane results, from the original 4-bucket version of this macro (SYSTEM_OVERHEAD not yet split out — its share of these numbers is folded into MODEL_ACTIVE below, not separately re-validated post-split):

Category Time Turns
HUMAN_IDLE 148.4 min 176
MODEL_ACTIVE (incl. what is now SYSTEM_OVERHEAD) 78.7 min 598
TOOL_EXEC 21.0 min 357
SUBAGENT_WAIT 0.0 min 24

Known limitation (documented in the macro's comment)

Classification is per-source (one session/subagent JSONL file) in isolation. SUBAGENT_WAIT is near-zero here because synchronous Agent/Task tool_use → tool_result pairs are rare within a single source file — a subagent runs in a different source. This macro does not detect that a HUMAN_IDLE-looking gap in the main session actually overlapped with a concurrently-running subagent in a different source; only a local JSONL-parsing script comparing timestamp windows across files can do that today. A future enhancement could join across source values sharing the same sessionId + time window to reclassify overlapping HUMAN_IDLE into SUBAGENT_WAIT, mirroring what a local script does — out of scope here.

Testing

  • default/macros.conf and default/props.conf parse cleanly (configparser, both files)
  • ./scripts/package.sh builds the app tarball successfully with the changes
  • Not deployed to the live Splunk app — source-controlled change only, per scope of this PR

Repo conventions note

This repo is trunk-based (default branch main, no develop), so this PR targets main directly rather than git-flow's feature/* -> develop.

Cross-repo consumer

VisiCore_App_for_AI_Observability is building an "AI Session Time Accounting" dashboard against this macro. Field/bucket names above are current as of the latest commit on this branch; will flag here again if they change further before merge.

Update (2026-07-24): Python cross-source correlation tool

Added scripts/time_accounting/ — a stdlib-only Python complement to the macro above, for exactly the "Known limitation" case: it pairs tool_use/tool_result by ID across a session's main file and its subagent files, and reclassifies a parent's gap as SUBAGENT_WAIT whenever it overlaps a subagent's actual observed window (something SPL join handles poorly — hit three separate silent-failure modes trying: a dropped join key, overwrite=true clobbering the group-by field, and an uncontrolled cross-product from join type=left max=0).

  • session_timeline.py — local JSONL classifier, same 5-bucket taxonomy as the macro.
  • splunk_retro_timeline.py — same classifier, but sourced from index=claude via vct-splunk-cli export instead of local files, so it works retroactively for any session/subagent set still in Splunk.

Validated against local ground truth on a live session: single-digit-minute agreement across all 5 categories between the local-JSONL path and the Splunk-export path. Documented in README under "Time Accounting Tool".

Update (2026-07-24, cont.): fleet-wide session discovery

Added scripts/time_accounting/fleet_discover.py — closes the "any session, no prior sessionId" gap. Discovers every distinct sessionId active in index=claude over a time window (stats count, min(_time), max(_time) by sessionId — sessionId is confirmed correct as the fleet-grouping key since subagents share their parent's sessionId), runs the per-session classifier on each, and aggregates into one fleet-wide view: total time by category across every session, plus top blocking labels (tools/commands/MCP calls) ranked by total minutes lost.

Ran it for real against production (-24h, 14 sessions, no output committed — contains real command text/paths): correctly re-surfaced the known hung mcp__zammad__zammad_create_ticket call (216 min) and independently found a second, previously-unknown orphaned tool call (an Edit in an unrelated ansible-proxmox-ai session, 83 min, no matching result event) — direct evidence the fleet-wide rollup finds real, actionable stalls that single-session analysis misses. This is the tool for "always know what to target to improve AI end-to-end execution times."

Update (2026-07-24, cont.): MODEL_ACTIVE was overclaiming — split into confirmed vs unverified

A direct user challenge ("if a subagent outputs 5 words in 5 minutes, that's downtime, not thinking") led to verifying the single MODEL_ACTIVE bucket against production data. Finding: 100% of the rows that reach the macro's "no tool, no text" branch have EMPTY thinking content (tested a live session, 1hr and 2hr windows). Claude Code logs an empty placeholder thinking block as its own transcript row, with the real content (text or a tool call) landing on a separate row moments later — so the macro's per-row gap-attribution model structurally routes every row with real content into HUMAN_IDLE/TOOL_EXEC before it can ever reach MODEL_ACTIVE. Calling that one confident bucket "model thinking/generating" overstated what the data actually proves.

Fixed by splitting into MODEL_ACTIVE_CONFIRMED (row has measurable text/thinking chars) and MODEL_ACTIVE_UNVERIFIED (it doesn't) — now 6 buckets total. MODEL_ACTIVE_CONFIRMED reads near-zero via this macro specifically, which is documented in the macro comment as an accurate structural consequence, not a bug: the companion Python tool uses a different attribution model (gap before each row, by that row's own output) and found a materially different split on the same live session (~56% confirmed / ~44% unverified) — the two numbers are correct under their own definitions and are not directly comparable. Both are now documented clearly so nobody mistakes one confident number for ground truth again.

The existing "AI Session Stall Classification (daily)" saved search only
classifies outlier stalls (5min-2hr gaps) and discards everything else,
so it cannot answer where every second of a session went. Adds
claude_time_accounting_by_gap, parameterized by sessionId, bucketing
every inter-turn gap into TOOL_EXEC/SUBAGENT_WAIT/MODEL_ACTIVE/HUMAN_IDLE.

Also documents in props.conf that production events land as bare
sourcetype=claude:code rather than the claude:code:session literal this
TA's macros/eventtypes currently hardcode.
A downstream consumer (VisiCore_App_for_AI_Observability dashboard PR)
needs the 5th category broken out rather than folded, which is exactly
the real-use-case condition the prior comment named as the bar for
splitting it. gap<2s -> SYSTEM_OVERHEAD, else MODEL_ACTIVE.
@openbao-service-broker openbao-service-broker Bot changed the title feat: add full time-accounting macro for inter-turn gaps feat: add full time-accounting macro for inter-turn gaps (5 categories) Jul 24, 2026
The prior comment claimed subagent transcripts get datatype-stamped as
claude:code:subagent. Verified directly against production (7-day window,
146,804 events): that sourcetype has zero hits, ever -- every event, main
session and subagent alike, lands as bare claude:code. The only reliable
signal for a subagent transcript is the source file path containing
/subagents/. Corrects the note on [claude:code:session] and flags the
dormant [claude:code:subagent] stanza accordingly.
claude_time_accounting_by_gap classifies per-source in isolation, so it
can't detect a parent session's gap overlapping a concurrently-running
subagent in a different source file. scripts/time_accounting/ adds a
stdlib-only classifier (session_timeline.py) plus a Splunk-export variant
(splunk_retro_timeline.py) that reuses the same logic against index=claude
data for sessions without a local transcript. Validated against local
ground truth: single-digit-minute agreement across all 5 categories.

Assisted-by: Claude:claude-sonnet-5
Claude-Session: https://claude.ai/code/session_012bPR299mQKnurhRohuAaas
fleet_discover.py closes the "any session, no prior sessionId" gap: it
discovers every distinct sessionId active in index=claude over a time
window, runs splunk_retro_timeline on each, and aggregates into one
fleet-wide view of total time by category plus top blocking labels
ranked by minutes lost. Validated against production (-24h, 14
sessions): correctly surfaced a known hung zammad MCP call (216 min)
and independently found a second, previously-unknown orphaned tool
call in an unrelated session.

Assisted-by: Claude:claude-sonnet-5
Claude-Session: https://claude.ai/code/session_012bPR299mQKnurhRohuAaas
Verified directly against a live production session: every row that
reaches the "no tool, no text" branch has EMPTY thinking content --
Claude Code logs an empty placeholder thinking block as its own
transcript row, with real content landing on a separate row moments
later. A single confident MODEL_ACTIVE bucket therefore overclaimed;
neither the SPL macro nor the Python classifier could actually prove
that time was spent generating.

Splits both into MODEL_ACTIVE_CONFIRMED (row has measurable text/
thinking/tool-input output) and MODEL_ACTIVE_UNVERIFIED (it doesn't).
The two tools use different gap-attribution models (macro: gap AFTER
a row, by that row's own content; Python: gap BEFORE a row, by that
row's own output) and so report materially different splits on the
same data -- documented in both the macro comment and README rather
than papered over, since they are not directly comparable numbers.

Assisted-by: Claude:claude-sonnet-5
Claude-Session: https://claude.ai/code/session_012bPR299mQKnurhRohuAaas

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.

No; too custom. This needs re-evaluated completely. Use AI-native data stores

@openbao-service-broker openbao-service-broker Bot added the human:review Human review required before merge to main - AI must not merge without explicit instruction label Sep 24, 2026

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.

No scripts; especially not in Splunk apps

@JacobPEvans-personal
Jacob (JacobPEvans-personal) merged commit 0f2112f into main Oct 20, 2026
5 checks passed
@JacobPEvans-personal
Jacob (JacobPEvans-personal) deleted the feat/time-accounting-macro branch October 20, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

human:review Human review required before merge to main - AI must not merge without explicit instruction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant