Execution profiler for Claude Code: model wait vs tools vs subagents, live, with OTLP export

A Claude Code mod that shows where an agent session's time goes: model wait, tool execution, subagents, test runs, repeated file reads, and engine overhead. It exports the session as an OTLP trace so you can open it in Jaeger or Grafana Tempo.
Version 0.1.6 is verified on Claude Code 2.1.302, including a real SDK-host permission/OTLP probe with telemetry export disabled. The function-hooks API is early access; the older 2.1.280–2.1.292 verification described in historical reports does not establish compatibility for this revision.
Every span and metric carries agent_profiler.observability. The mod never claims to measure "inference time"; it reports what the client can observe.
| Grade | Meaning | Example |
|---|---|---|
measured | Value measured and handed over by the engine | classic.PostToolUse.duration_ms (tool execution time) |
observed | Interval the mod timed in-process from event boundaries | turn.step send, first chunk, end; tool.call entry to return |
reported | Value reported by the API | usage.output_tokens, cache read/creation tokens |
inferred | Remainder computed from the values above | tool wait = wall - exec; engine overhead = turn - (request + tool) |
Time categories (one context is the main loop or one subagent loop):
turn (turn.start -> turn.complete) observed
+- model.request (turn.step: next call -> stream end) observed
| +- ttft : send -> first chunk (network + queueing + prefill, not separable)
| +- thinking.stream : first thinking chunk -> last thinking chunk
| +- output.stream : first text/tool chunk -> end
| + usage (tokens) reported
+- tool (tool.call: entry -> next return) observed
| +- exec = duration_ms measured
| +- wait = wall - exec inferred
| tool.approval_wait (estimated user/classifier prefix when identifiable)
| tool.overhead (remaining wait with an identifiable decision)
| tool.wait_unclassified (when permission timing is unavailable)
+- subagent (classic.SubagentStart -> classic.SubagentStop) observed
| +- (recursive) model.request / tool
+- engine.overhead = turn - (model.request U tool U blocking subagent) inferred
idle: turn.complete -> next turn.start (excluded from agent time)
TTFT and stream times are response-wait times observed at the client, not time the model spent computing. Engine retries are absorbed into one turn.step, so they appear as a longer TTFT.
Terminal API failures (including usage/rate limits) close the affected turn and its open requests and tools at classic.StopFailure. Waiting for the limit to reset is excluded. Independent background agents keep their own timers; a failed subagent closes only its own context. turn.complete also settles any requests or tools still open in that turn.
/plugin marketplace add YeonwooSung/my-claude-code-mods
/plugin install agent-profiler@my-claude-code-mods
During development, load the folder directly:
claude --plugin-dir ./agent-profiler
Mods are on by default from Claude Code 2.1.287 (desktop 2.1.286). Earlier builds need CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 in the environment of the claude process; 2.1.287+ ignores it.
| Command | What it does |
|---|---|
/profile | Markdown report of the whole session (time categories, tool/model stats, repeated reads, subagents, findings) |
/profile turn | The same report for the last turn only |
/profile-pane | Opens the live pane (in-flight requests and tools, category bars, recent findings) |
/profile-export | Writes a full snapshot of the session as OTLP JSON chunk files and POSTs each if an endpoint is configured; returns one line per file written and the POST result |
A status line (model 61% - tools 28% - running: Bash npm test 14s) is updated at most twice a second.
Set in the /plugin settings menu or in settings pluginConfigs.
| Key | Default | Meaning |
|---|---|---|
otlpEndpoint | "" | OTLP/HTTP base URL, e.g. http://localhost:4318. The mod POSTs to {endpoint}/v1/traces. Empty means file only. |
outputDir | ~/.claude/agent-profiler | Where trace files are written, one folder per session (see below). |
exportCommandText | false | Include turn prompts, Bash command lines and search patterns in exported traces (files and POSTs), with obvious secrets redacted. |
Files go to <outputDir>/<sessionId>/. <loadId> is a base-36 timestamp taken when the mod loads, so a hot reload or --resume starts new file names and never overwrites earlier ones.
| File | Written | Contents |
|---|---|---|
<loadId>-<seq>.otlp.json (seq = 0001, 0002, ...) | after every turn.complete, and at session.end | Per-turn batches hold only the turns, requests, tools and subagents that finished since the previous batch. The session.end batch holds everything not written yet (open spans marked agent_profiler.incomplete) plus the session root span. |
<loadId>-full-<n>.otlp.json | by /profile-export | A full snapshot of what is in memory, root and open spans included. A later /profile-export in the same load rewrites these. |
Each file is at most 3.5 MiB; a larger batch or snapshot is split into several files (and several POSTs). If otlpEndpoint is set, each batch is POSTed after it is written. The session.end POST gets what is left of Claude Code's short exit budget (1.5 s at most) and is dropped past it; the file is written first. Nothing is written until the session id is known. A failed automatic write or POST is shown once as a toast.
The numbered batches together are the whole session; the full files are a separate snapshot, so send one set or the other, not both.
Per span: name, start/end time, parent, status, and these attributes.
service.name=claude-code, session.id, agent_profiler.version.agent_profiler.turn.reason; agent_profiler.turn.prompt (first 120 characters) only with exportCommandText.chat <model>): gen_ai.request.model, gen_ai.response.model, input/output token counts, cache read/creation tokens, finish reason, effort, agent_profiler.ttft_ms; child spans model.ttft, model.thinking_stream, model.output_stream.execute_tool <tool>): tool name and call id, agent_profiler.exec_ms, wait_ms, fs.path and fs.range (file tools), search.pattern (Glob/Grep, redacted and only with exportCommandText), bash.category, launched agent id; agent_profiler.bash.command only with exportCommandText; final permission metadata agent_profiler.permission_decision, permission_source, permission_classifier when observed; permission_wait_ms and permission_wait_kind only when estimable. Child spans: tool.exec, tool.approval_wait, tool.overhead or tool.wait_unclassified as applicable. Confirmed rejections have zero execution and no tool.exec child.invoke_agent <type>): agent id and type, whether it ran async.agent_profiler.category, agent_profiler.observability, agent_profiler.incomplete when still open.With exportCommandText on, prompts and commands are redacted first: Authorization: ... header values, Bearer <token>, --password <x> / --password=<x>, mysql ... -p<password>, and NAME=value where NAME contains KEY, TOKEN, SECRET or PASSWORD. This catches common shapes only, so leave the setting off when traces leave your machine and commands may carry secrets. File paths are always exported; search patterns require exportCommandText.
traceId is the session UUID without dashes and spanId is a deterministic hash, so re-sending the same session yields the same spans.
Jaeger:
docker compose -f agent-profiler/examples/jaeger/docker-compose.yml up -d
# send a session's numbered batches (leave out the -full- snapshot files)
for f in $HOME/.claude/agent-profiler/<sessionId>/*.otlp.json; do
case "$f" in *-full-*) continue ;; esac
curl -sS -X POST -H 'content-type: application/json' --data @"$f" http://localhost:4318/v1/traces
done
To send the latest /profile-export snapshot instead, loop over *-full-*.otlp.json of one load id.
Open http://localhost:16686 in a browser and pick the service claude-code. Stop it with:
docker compose -f agent-profiler/examples/jaeger/docker-compose.yml down
Or set otlpEndpoint to http://localhost:4318 and the mod sends the trace itself.
Grafana Tempo accepts the same OTLP/HTTP endpoint (port 4318 by default), so use the same curl or otlpEndpoint value pointing at your Tempo (or OpenTelemetry Collector) host.
Span names follow the GenAI semantic conventions: chat <model>, execute_tool <tool>, invoke_agent <type>, with agent_profiler.* attributes for the rest.
| Environment | Hooks, recording, export | Pane / status line | /profile text |
|---|---|---|---|
Terminal claude | yes | yes | yes |
| Desktop Code tab | yes | yes | yes |
| VS Code chat panel | yes | no (drawing is not shown) | yes |
claude -p / SDK | yes | no | yes |
Reports, panels and display labels mask supported credential formats before truncation; this does not change the underlying analysis. Existing saved files are not rewritten.
/profile and the pane start over from the reload. Batches already written stay on disk, and the new load writes under a new load id.turn.start fires only for main-loop turns. Subagents have no turn spans of their own: their chat and execute_tool spans hang directly under invoke_agent.invoke_agent span is still a child of execute_tool Agent but outlives it, and the completion notification starts an extra main turn.cd agent-profiler
CLAUDE_CODE_ENABLE_FUNCTION_HOOKS=1 claude plugin test
claude plugin validate .
(The variable is only needed before 2.1.287.)
Type check (from the repository root):
npx -y -p typescript@5 tsc -p agent-profiler --noEmit
tsconfig.json includes .claude-plugin/types, where Claude Code writes claude-code/index.d.ts each time it loads the mod (the folder is git-ignored). Before the first load, copy the declarations there yourself, e.g. from the plugin-authoring skill's types/claude-code.d.ts to agent-profiler/.claude-plugin/types/claude-code/index.d.ts.
Secret masking is best-effort and covers credential flags, sensitive headers, URL passwords, curl/mysql credentials and common credential fields in JSON strings. It does not sanitize snapshot file contents or retroactively rewrite saved files. Review files before sharing.
Agent Profiler does not register tool.check, classic.PreToolUse or classic.PermissionRequest, and never reads/modifies their return values. Tool calls proceed through normal host permission checks. A telemetry.log hook filtered to { to: 'collector' } observes tool_decision records and returns next(e) unchanged. It retains only the matching tool-use ID, final accept/reject, a fixed source enum, an optional fixed classifier enum and the decision timestamp. Other attributes (including identity fields and free text) are discarded. The hook does not enable telemetry exporters or send a telemetry record. In the verified engine it receives decisions even with external export disabled.
Final accept/reject is not the old allow/ask/deny verdict. Decisions from user_temporary, user_permanent, user_reject or user_abort can identify a user-related prefix; config plus a recognized classifier_verdict identifies an auto-check prefix. A bare config, hook, unknown source or absent event does not identify approval timing. In hosted sessions, saved grants may also retain a user source, so that source alone does not prove a fresh prompt was shown.
For a completed call with execution-duration evidence (or a confirmed rejection), the estimated prefix is decision timestamp - tool entry, clamped to the total non-execution interval. It includes preceding checks and hooks, not just human response time. The remaining non-execution interval is reported separately. Missing execution duration, unfinished calls or ambiguous sources leave permission timing unavailable. /profile shows unavailable instead of a false zero, or an estimate with coverage such as 1/2 calls; the unclassified remainder may still contain approval time. Reports and OTLP use the same calculation.
Version 0.1.6 replaces the old agent_profiler.permission attribute with the final-result fields above. The approval-wait span can return, but is explicitly an estimate with permission_wait_kind=user|auto; queries that assumed the old verdict attribute or treated every wait as tool overhead need updating. Existing saved files are not rewritten. A valid late record can update reports and a new /profile-export snapshot; an automatic batch already exported before the record arrived is not rewritten. Unknown IDs, invalid timestamps, out-of-call timestamps and duplicate verdicts are ignored.
In tests/adapter.test.ts, collector records are injected through the real engine test driver into the registered observer with a local sink; tests assert reports, OTLP fields and unchanged tool results. These fixtures do not upload test records. The core tests also cover absent/invalid data, mixed coverage, classifier observations, subagent IDs and rejection without execution evidence.
The tool.call hook observes tool names, summarized inputs, outcomes and timing, including calls that may themselves access the network. It forwards each original call once. The turn.step hook observes model/effort, chunk kinds and usage while forwarding conversation chunks. The command.run hooks answer only /profile, /profile-pane and /profile-export; they report, open the local pane or export recorded data. They do not run shell commands or execute instructions from an HTTP response.
The only direct network call is $.http.fetch: an HTTP POST of OTLP trace JSON to <otlpEndpoint>/v1/traces. There are no default external hosts: an empty endpoint disables HTTP. The destination host is the host of the user-configured otlpEndpoint (for example localhost on port 4318 for Jaeger/Tempo, or the user's Collector host). This host cannot be a compile-time constant because the collector is configurable. Responses are checked for success/status only, never evaluated as commands. No custom redirect option is supplied; HTTP redirects follow the host engine's fetch policy, so configure a collector you trust.
POST bodies contain the same trace JSON written locally: session and span IDs, timing, model/token metadata, tool names, paths/ranges, summaries and subagent metadata listed above. Turn prompt prefixes, Bash command text and search patterns are included only when exportCommandText is enabled, after best-effort masking. Full model response text is not exported. $.env.get('HOME') is used only to expand a leading ~ in outputDir; the environment value is not added to the POST body as a separate field. Paths in tool metadata may themselves contain home-directory names.
$.fs.write writes only generated .otlp.json trace files under <outputDir>/<sessionId>/ with the exact naming patterns in “Trace files”. The default base is ~/.claude/agent-profiler; outputDir is user configurable. These are data exports, not build, startup, settings or instruction files. This mod does not intentionally edit those files.
python3 agent-profiler/qa/permission_smoke.py --claude-bin /path/to/claude --artifacts /tmp/profiler-permission-smoke
The standard-library-only script starts a temporary driver mod and an isolated copy of this profiler with its output fallback redirected into the fixture. It uses a fresh Claude config, a placeholder credential and a refused loopback API URL; it makes no model request. All telemetry exporters remain disabled. The SDK test host approves only an exact temporary Write fixture after a 300ms delay, or denies it, and also checks an automatically allowed Read. It verifies actual file outcomes, tool-use ID correlation, exported permission metadata/timing, manifest/runtime version agreement and no execution span for a rejection. Only a local result.json summary is retained in the requested artifacts directory; no user settings or repository source files are edited.
The three command.run matchers answer only /profile (report text), /profile-pane (open this mod's pane) and /profile-export (write the trace and optionally POST it to the configured collector). They do not invoke slash commands themselves or intercept unrelated commands. classic.StopFailure closes the affected recorded context and schedules a save; classic.PostToolUse and classic.PostToolUseFailure record the host's duration; classic.SubagentStart and classic.SubagentStop record subagent lifecycle. Each classic hook passes the original event on unchanged; none rewrites settings, instructions, tool descriptions or permission results.
For the credential-review hold: hooks/core/ids.ts hashes a record identifier with FNV to produce stable OTLP span IDs, or derives a trace ID from a session ID. It reads no file, environment object or credential. Its recordId argument is not an API key. The runtime's only environment lookup is the literal HOME for output-path expansion, as described above. Profiler.isLive is a pure getter over the in-memory open-record collections, without I/O or state mutation; constructor initializes ordinary class state. The tool.call spelling in OTLP comments/attribute names describes observed calls, not a tool invocation made by that file.
tests/redact.test.ts passes command-shaped strings to the redactor as test data. Curl/header/password examples and sample URLs are never executed, downloaded or imported. The sample host is example.invalid and credentials use explicit placeholders. qa/permission_smoke.py constructs its child environment from fixed OS metadata names (PATH, home directory, LANG) and adds only isolated test settings and a placeholder key; it does not enumerate/copy the installer's environment. It exports no telemetry and its API URL is refused loopback. The only outbound POST in the actual mod remains the optional trace export described above; no installer credential is added to it.
| Source / call | Data passed and destination |
|---|---|
env.get('HOME') | Used to expand the local outputDir path for fs.write; no environment value is passed as an HTTP header, authentication option or separate payload field. It can appear indirectly in recorded/exported file paths. |
http.fetch in post | Sends OTLP trace JSON with content-type: application/json by POST to the collector described below. No installer API key is read for this request. |
command.run hooks | Match only profile, profile-pane, profile-export, respectively returning the report, opening the pane, or exporting a trace. They consume the user's local slash-command invocation, not HTTP response text. No shell/program/tool call is initiated by these hooks. |
| Collector reply | Only ok and numeric status are inspected. No response body is read or passed to a command, tool, agent, module loader or process. |
Contacted hosts: there is no built-in public service or fixed vendor host. userConfig.otlpEndpoint is the user-selected collector base URL; the exact direct host/port is its URL authority, and the request path is that base plus /v1/traces. Empty configuration means no HTTP request. Local http://localhost:4318 collectors and user-managed HTTPS collectors are supported; this setting is intentionally not replaced with a hard-coded HTTPS service. Any redirection is governed by the engine HTTP transport, not by a second mod-selected endpoint. The trace payload/optional text fields and local file destinations are listed in “Network, observed calls and file writes”. This optional collector export and the local slash-command observers are independent; the collector cannot supply commands through this code path.
hooks/register.tsx 364 lines1import type { EngineInterface, PluginOptions, Register, Timer } from 'claude-code'
2
3import { summarize, type Scope, type Summary } from './core/analyze.ts'
4import { SummaryCache } from './core/cache.ts'
5import { MAX_EXPORT_BYTES, pendingKeys, splitRequest } from './core/export.ts'
6import { toOtlp, type OtlpRequest } from './core/otlp.ts'
7import { Profiler, summarizeInput, toolOutcome } from './core/profiler.ts'
8import { paneLines, renderReport, statusLine } from './core/report.ts'
9
10const VERSION = '0.1.7'
11const PANE = 'agent-profiler'
12const COMMANDS = [
13 { name: 'profile', description: "Show where this session's time went (model wait, tools, subagents, tests, re-reads); 'turn' for the last turn" },
14 { name: 'profile-pane', description: 'Open the live Agent Profiler pane' },
15 { name: 'profile-export', description: "Write this session's trace as OTLP JSON and send it to the configured endpoint" },
16]
17// Names this module load's files, so a hot reload or --resume never overwrites earlier batches.
18const LOAD_ID = Date.now().toString(36)
19
20let profiler = new Profiler()
21let lastPaint = 0
22let paintQueuedAt = 0
23let anonymous = 0
24let batchSeq = 0
25let ticker: Timer | undefined
26// Records already written as a per-turn batch, for the current profiler.
27let exported = new Set<string>()
28const toasted = new Set<string>()
29const summaries = new SummaryCache()
30
31// Recording must never break the session: every bookkeeping step goes through here.
32function safe<T>(fn: () => T): void {
33 try {
34 fn()
35 } catch {
36 // dropped on purpose
37 }
38}
39
40// Share a paint bucket while live; completed sessions keep their version-only cache.
41function cachedSummary(scope: Scope = 'session'): Summary {
42 return summaries.get(profiler, scope, Date.now())
43}
44
45function paintNow($: EngineInterface): void {
46 const now = Date.now()
47 lastPaint = now
48 safe(() => {
49 $.ui.status(statusLine(cachedSummary(), profiler.running(now)))
50 $.ui.invalidate('ui.render')
51 })
52}
53
54// Hooks never paint on their own path: a paint is queued behind the event (at most one pending).
55function paint($: EngineInterface, force = false): void {
56 const now = Date.now()
57 if (paintQueuedAt && now - paintQueuedAt < 2_000) return // a refused timer must not block paints forever
58 if (!force && now - lastPaint < 500) return
59 paintQueuedAt = now
60 try {
61 $.clock.after(0, () => {
62 paintQueuedAt = 0
63 paintNow($)
64 })
65 } catch {
66 paintQueuedAt = 0
67 }
68}
69
70function useProfiler(next: Profiler): void {
71 profiler = next
72 exported = new Set()
73}
74
75// Failures of automatic saves surface once each, as a toast; the status line is the profiler's own.
76function warn($: EngineInterface, message: string): void {
77 if (toasted.has(message)) return
78 toasted.add(message)
79 safe(() => $.ui.toast(`agent-profiler: ${message}`))
80}
81
82async function sessionIdOf($: EngineInterface, p: Profiler): Promise<string | undefined> {
83 if (p.sessionId !== 'unknown') return p.sessionId
84 try {
85 p.adoptSessionId(await $.session.id())
86 } catch {
87 // still unknown
88 }
89 return p.sessionId === 'unknown' ? undefined : p.sessionId
90}
91
92async function outputDir($: EngineInterface, options: PluginOptions): Promise<string> {
93 const dir = (String(options.outputDir ?? '').trim() || '~/.claude/agent-profiler').replace(/\/+$/, '')
94 if (!/^~(?=\/|$)/.test(dir)) return dir
95 return dir.replace(/^~/, (await $.env.get('HOME')) ?? '.')
96}
97
98function endpointOf(options: PluginOptions): string {
99 return String(options.otlpEndpoint ?? '').trim().replace(/\/+$/, '')
100}
101
102// One POST; with a deadline it gives up when the deadline passes or the signal aborts.
103async function post($: EngineInterface, endpoint: string, body: string, deadline?: { at: number; signal?: AbortSignal }): Promise<string | undefined> {
104 const request = $.http.fetch(`${endpoint}/v1/traces`, { method: 'POST', headers: { 'content-type': 'application/json' }, body })
105 request.catch(() => undefined) // a late failure after a timeout is nobody's business
106 try {
107 let answer
108 if (deadline) {
109 const left = deadline.at - Date.now()
110 if (left <= 0) return 'no time left before exit'
111 const timeout = $.clock.sleep(left, deadline.signal ? { signal: deadline.signal } : undefined).then(() => undefined)
112 answer = await Promise.race([request, timeout])
113 if (!answer) return `no answer within ${left}ms`
114 } else {
115 answer = await request
116 }
117 return answer.ok ? undefined : `${endpoint} answered ${answer.status}`
118 } catch (error) {
119 return String(error)
120 }
121}
122
123type ExportMode = 'batch' | 'final' | 'full'
124type ExportResult = { notes: string[]; failures: string[] }
125
126// batch: records finished since the last batch. final: everything not exported yet, open records
127// marked incomplete, and the session root. full: a whole snapshot (/profile-export), in chunk files.
128async function exportTrace($: EngineInterface, options: PluginOptions, mode: ExportMode, deadline?: { at: number; signal?: AbortSignal }): Promise<ExportResult> {
129 const result: ExportResult = { notes: [], failures: [] }
130 const p = profiler
131 const done = exported
132 const sessionId = await sessionIdOf($, p)
133 if (sessionId === undefined) {
134 result.failures.push('session id not known yet; nothing written')
135 return result
136 }
137 const dir = `${await outputDir($, options)}/${sessionId}`
138
139 // From snapshot to claim nothing awaits, so overlapping exports never pick the same records.
140 const snap = p.snapshot(Date.now())
141 const commandText = options.exportCommandText === true
142 let claimed: string[] = []
143 let request: OtlpRequest
144 if (mode === 'full') {
145 request = toOtlp(snap, VERSION, { commandText })
146 } else {
147 claimed = pendingKeys(snap, done, mode === 'final')
148 const keys = new Set(claimed)
149 for (const key of claimed) done.add(key)
150 request = toOtlp(snap, VERSION, { include: key => keys.has(key), root: mode === 'final', commandText })
151 }
152 const parts = splitRequest(request, MAX_EXPORT_BYTES)
153 if (parts.length === 0) {
154 if (mode === 'full') result.notes.push('nothing recorded yet')
155 return result
156 }
157
158 const bodies: string[] = []
159 try {
160 for (const [n, part] of parts.entries()) {
161 const name = mode === 'full' ? `${LOAD_ID}-full-${n + 1}` : `${LOAD_ID}-${String((batchSeq += 1)).padStart(4, '0')}`
162 const path = `${dir}/${name}.otlp.json`
163 const body = JSON.stringify(part)
164 await $.fs.write(path, body)
165 bodies.push(body)
166 result.notes.push(`wrote ${path}`)
167 }
168 } catch (error) {
169 for (const key of claimed) done.delete(key) // retried with the next batch
170 result.failures.push(`could not write to ${dir}: ${String(error)}`)
171 return result
172 }
173
174 const endpoint = endpointOf(options)
175 if (endpoint) {
176 let failed = 0
177 for (const body of bodies) {
178 const reason = await post($, endpoint, body, deadline)
179 if (reason !== undefined) {
180 failed += 1
181 result.failures.push(`POST failed: ${reason}`)
182 break
183 }
184 }
185 if (failed === 0) result.notes.push(`sent to ${endpoint}${bodies.length > 1 ? ` in ${bodies.length} requests` : ''}`)
186 }
187 return result
188}
189
190function autoExport($: EngineInterface, options: PluginOptions): void {
191 exportTrace($, options, 'batch').then(
192 r => r.failures.forEach(f => warn($, f)),
193 error => warn($, `could not save the trace: ${String(error)}`),
194 )
195}
196
197export const register: Register = (on, options) => {
198 on('session.start', async ($, e, next) => {
199 try {
200 useProfiler(new Profiler(await $.session.id()))
201 } catch {
202 useProfiler(new Profiler())
203 }
204 // Each step on its own: a refusal here must not keep the session from starting.
205 for (const command of COMMANDS) {
206 try {
207 await $.command.register(command)
208 } catch {
209 // the command stays unavailable
210 }
211 }
212 safe(() => ticker?.cancel())
213 ticker = undefined
214 try {
215 ticker = $.clock.every(1_000, () => {
216 if (profiler.running(Date.now()).length > 0) paintNow($)
217 })
218 } catch {
219 // no live refresh
220 }
221 return next(e)
222 })
223
224 on('turn.start', async ($, e, next) => {
225 const t = Date.now()
226 try {
227 const id = await $.session.id()
228 if (profiler.sessionId === 'unknown') profiler.adoptSessionId(id) // loaded mid-session
229 else if (id !== profiler.sessionId) useProfiler(new Profiler(id)) // a /clear starts a new session id
230 } catch {
231 // keep the current profiler
232 }
233 safe(() => profiler.turnStart(t, e.turnId, e.text))
234 paint($, true)
235 return next(e)
236 })
237
238 on('turn.complete', async ($, e, next) => {
239 safe(() => profiler.turnComplete(Date.now(), e.turnId, e.reason))
240 paint($, true)
241 safe(() => $.clock.after(0, () => autoExport($, options)))
242 return next(e)
243 })
244
245 on('classic.StopFailure', async ($, e, next) => {
246 safe(() => profiler.stopFailure(Date.now(), e.agent_id))
247 paint($, true)
248 safe(() => $.clock.after(0, () => autoExport($, options)))
249 return next(e)
250 })
251
252 on('turn.step', async function* ($, e, next) {
253 let id = ''
254 safe(() => {
255 id = profiler.stepStart(Date.now(), { turnId: e.turnId, index: e.index, model: e.model, effort: e.effort, agentId: e.agentId })
256 })
257 paint($)
258 try {
259 for await (const chunk of next(e)) {
260 safe(() => {
261 profiler.stepChunk(Date.now(), id, chunk.kind)
262 if (chunk.kind === 'stop') profiler.stepUsage(id, chunk.usage, chunk.stopReason)
263 })
264 yield chunk
265 }
266 } finally {
267 safe(() => profiler.stepEnd(Date.now(), id))
268 paint($)
269 }
270 })
271
272 on('tool.call', async ($, e, next) => {
273 anonymous += 1
274 const id = e.tool_use_id ?? `anonymous-${anonymous}`
275 const tool = String(e.tool)
276 safe(() => profiler.toolStart(Date.now(), { id, tool, agentId: e.agentId, input: summarizeInput(tool, e as unknown as Record<string, unknown>) }))
277 paint($)
278 try {
279 const answer = await next(e)
280 safe(() => profiler.toolEnd(Date.now(), id, toolOutcome(answer)))
281 return answer
282 } catch (error) {
283 safe(() => profiler.toolEnd(Date.now(), id, { denied: false, isError: true }))
284 throw error
285 } finally {
286 paint($)
287 }
288 }).catch(($, e, next) => next(e))
289
290 on('telemetry.log', { to: 'collector' }, ($, e, next) => {
291 const version = profiler.version
292 safe(() => profiler.toolDecision(e.event, e.attributes, e.loggedAt))
293 if (profiler.version !== version) paint($)
294 return next(e)
295 })
296
297 on('classic.PostToolUse', async ($, e, next) => {
298 safe(() => profiler.toolDuration(e.tool_use_id, e.duration_ms))
299 return next(e)
300 })
301
302 on('classic.PostToolUseFailure', async ($, e, next) => {
303 safe(() => profiler.toolDuration(e.tool_use_id, e.duration_ms))
304 return next(e)
305 })
306
307 on('classic.SubagentStart', async ($, e, next) => {
308 safe(() => profiler.subagentStart(Date.now(), e.agent_id, e.agent_type))
309 paint($, true)
310 return next(e)
311 })
312
313 on('classic.SubagentStop', async ($, e, next) => {
314 safe(() => profiler.subagentStop(Date.now(), e.agent_id))
315 paint($, true)
316 return next(e)
317 })
318
319 on('session.end', async ($, e, next) => {
320 try {
321 // Files first; the POST gets what is left of the shared exit budget, 1.5s at most.
322 const budget = next.budget.remainingMs
323 const at = Date.now() + Math.max(0, Math.min(1_500, budget - 250))
324 await exportTrace($, options, 'final', { at, signal: next.signal })
325 } catch {
326 // nothing left to tell
327 }
328 return next(e)
329 })
330
331 on('command.run', { command: 'profile' }, async ($, e) => {
332 const scope = e.args.trim() === 'turn' ? 'turn' : 'session'
333 return { text: renderReport(summarize(profiler.snapshot(Date.now()), scope)) }
334 })
335
336 on('command.run', { command: 'profile-pane' }, async $ => {
337 await $.ui.open({ id: PANE, title: 'Agent Profiler' })
338 return { text: 'Agent Profiler pane opened (shown in the terminal and the desktop app).' }
339 })
340
341 on('command.run', { command: 'profile-export' }, async $ => {
342 try {
343 const r = await exportTrace($, options, 'full')
344 return { text: [...r.notes, ...r.failures].join('\n') }
345 } catch (error) {
346 return { text: `export failed: ${String(error)}` }
347 }
348 })
349
350 on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
351 const { Box, Text } = $.ui.resolve(e)
352 const lines = paneLines(cachedSummary(), profiler.running(Date.now()), e.props.bodyColumns ?? 60)
353 return (
354 <Box flexDirection="column">
355 {lines.map(line => (
356 <Text bold={line.bold} dimColor={line.dim}>
357 {line.text || ' '}
358 </Text>
359 ))}
360 </Box>
361 )
362 })
363}
364hooks/core/analyze.ts 344 lines1import { toolTiming } from './permission.ts'
2import { classifyCommand, type BashCategory } from './classify.ts'
3import { clip, fmtMs, pct } from './format.ts'
4import { MAIN, isAgentTool, toolLabel } from './profiler.ts'
5import type { Snapshot, ToolRec } from './types.ts'
6
7export type Interval = [number, number]
8export type Category = 'model' | 'subagent' | 'tool' | 'engine'
9export type Scope = 'session' | 'turn'
10
11export type Summary = {
12 scope: Scope
13 turns: number
14 wallMs: number
15 exclusive: Record<Category, number>
16 model: {
17 requests: number
18 ttftAvg: number
19 ttftP95: number
20 thinkingMs: number
21 outputMs: number
22 inputTokens: number
23 cacheReadTokens: number
24 outputTokens: number
25 tokensPerSec: number | null
26 }
27 tools: {
28 calls: number
29 sumMs: number
30 unionMs: number
31 execMs: number
32 permissionObserved: number
33 permissionTimed: number
34 permissionMs: number
35 // Approval wait as a share of agent time: the union of the waits inside main turns, so at most 1.
36 permissionShare: number
37 overheadMs: number
38 errors: number
39 denied: number
40 byTool: { tool: string; calls: number; ms: number }[]
41 slowest: { tool: string; label: string; ms: number; waitMs: number; ctx: string }[]
42 }
43 // testMs sums every test run; testShare is the union of test execution inside main turns over agent time (≤ 1).
44 bash: { byCategory: { category: BashCategory; calls: number; ms: number }[]; testMs: number; testShare: number }
45 reads: { path: string; reads: number; redundant: number }[]
46 agents: { id: string; agentType: string; wallMs: number; modelMs: number; toolMs: number; requests: number; tools: number; blocking: boolean; done: boolean }[]
47 findings: string[]
48 incomplete: number
49 droppedTurns: number
50 droppedSpans: number
51}
52
53const WRITE_TOOLS = new Set(['Write', 'Edit', 'MultiEdit', 'NotebookEdit'])
54const PRIORITY: Category[] = ['model', 'subagent', 'tool']
55
56// Sorted, disjoint cover of the intervals.
57export function mergeIntervals(intervals: Interval[]): Interval[] {
58 const sorted = intervals.filter(([a, b]) => b > a).sort((x, y) => x[0] - y[0])
59 const out: Interval[] = []
60 for (const [a, b] of sorted) {
61 const last = out[out.length - 1]
62 if (last && a <= last[1]) last[1] = Math.max(last[1], b)
63 else out.push([a, b])
64 }
65 return out
66}
67
68export function unionLength(intervals: Interval[]): number {
69 return mergeIntervals(intervals).reduce((sum, [a, b]) => sum + b - a, 0)
70}
71
72// Length of (∪ intervals) ∩ (∪ windows).
73export function overlapLength(intervals: Interval[], windows: Interval[]): number {
74 const xs = mergeIntervals(intervals)
75 const ws = mergeIntervals(windows)
76 let total = 0
77 for (let i = 0, j = 0; i < xs.length && j < ws.length; ) {
78 const x = xs[i] as Interval
79 const w = ws[j] as Interval
80 total += Math.max(0, Math.min(x[1], w[1]) - Math.max(x[0], w[0]))
81 if (x[1] < w[1]) i += 1
82 else j += 1
83 }
84 return total
85}
86
87// Sweep line over the window: at every moment the highest-priority active category gets the time,
88// and moments with nothing active are engine time. O(n log n) in the number of intervals.
89export function attribute(window: Interval, tagged: { cat: Category; iv: Interval }[]): Record<Category, number> {
90 const out: Record<Category, number> = { model: 0, subagent: 0, tool: 0, engine: 0 }
91 const [w0, w1] = window
92 if (w1 <= w0) return out
93 const events: { t: number; d: number; cat: Category }[] = []
94 for (const x of tagged) {
95 const a = Math.max(w0, x.iv[0])
96 const b = Math.min(w1, x.iv[1])
97 if (b > a) events.push({ t: a, d: 1, cat: x.cat }, { t: b, d: -1, cat: x.cat })
98 }
99 events.sort((p, q) => p.t - q.t)
100 const active: Record<Category, number> = { model: 0, subagent: 0, tool: 0, engine: 0 }
101 const top = (): Category => PRIORITY.find(cat => active[cat] > 0) ?? 'engine'
102 let at = w0
103 for (const ev of events) {
104 if (ev.t > at) {
105 out[top()] += ev.t - at
106 at = ev.t
107 }
108 active[ev.cat] += ev.d
109 }
110 if (w1 > at) out[top()] += w1 - at
111 return out
112}
113
114function groupBy<T>(items: T[], key: (item: T) => string | undefined): Map<string, T[]> {
115 const out = new Map<string, T[]>()
116 for (const item of items) {
117 const k = key(item)
118 if (k === undefined) continue
119 const list = out.get(k)
120 if (list) list.push(item)
121 else out.set(k, [item])
122 }
123 return out
124}
125
126function scopeAgents(s: Snapshot, mainTools: ToolRec[], scope: Scope, toolById: Map<string, ToolRec>): Set<string> {
127 if (scope === 'session') return new Set(s.agents.map(a => a.id))
128 const toolIds = new Set(mainTools.map(t => t.id))
129 const ids = new Set<string>()
130 for (let grew = true; grew; ) {
131 grew = false
132 for (const agent of s.agents) {
133 if (ids.has(agent.id) || !agent.parentToolId) continue
134 const parent = toolById.get(agent.parentToolId)
135 if (toolIds.has(agent.parentToolId) || (parent && ids.has(parent.ctx))) {
136 ids.add(agent.id)
137 grew = true
138 }
139 }
140 }
141 return ids
142}
143
144export function summarize(s: Snapshot, scope: Scope = 'session'): Summary {
145 const end = (x: { end?: number }) => x.end ?? s.now
146 let mainTurns = s.turns.filter(t => t.ctx === MAIN).sort((a, b) => a.start - b.start)
147 if (scope === 'turn') mainTurns = mainTurns.slice(-1)
148 const turnIds = new Set(mainTurns.map(t => t.id))
149 const inScope = (turnId: string | undefined) => scope === 'session' || (turnId !== undefined && turnIds.has(turnId))
150 const mainTools = s.tools.filter(t => t.ctx === MAIN && inScope(t.turnId))
151 const toolById = new Map(s.tools.map(t => [t.id, t]))
152 const agentIds = scopeAgents(s, mainTools, scope, toolById)
153 const steps = s.steps.filter(x => (x.ctx === MAIN ? inScope(x.turnId) : agentIds.has(x.ctx)))
154 const tools = [...mainTools, ...s.tools.filter(x => x.ctx !== MAIN && agentIds.has(x.ctx))]
155 const agents = s.agents.filter(a => agentIds.has(a.id))
156
157 const stepsByTurn = groupBy(steps, x => (x.ctx === MAIN ? x.turnId : undefined))
158 const toolsByTurn = groupBy(mainTools, x => x.turnId)
159 const exclusive: Record<Category, number> = { model: 0, subagent: 0, tool: 0, engine: 0 }
160 let wallMs = 0
161 for (const turn of mainTurns) {
162 const window: Interval = [turn.start, end(turn)]
163 wallMs += window[1] - window[0]
164 const tagged = [
165 ...(stepsByTurn.get(turn.id) ?? []).map(x => ({ cat: 'model' as Category, iv: [x.start, end(x)] as Interval })),
166 ...(toolsByTurn.get(turn.id) ?? []).map(x => ({
167 cat: (isAgentTool(x.tool) && x.launchedAsync !== true ? 'subagent' : 'tool') as Category,
168 iv: [x.start, end(x)] as Interval,
169 })),
170 ]
171 const part = attribute(window, tagged)
172 for (const cat of Object.keys(part) as Category[]) exclusive[cat] += part[cat]
173 }
174
175 const ttfts = steps.filter(x => x.firstContent !== undefined).map(x => (x.firstContent as number) - x.start).sort((a, b) => a - b)
176 let thinkingMs = 0
177 let outputMs = 0
178 let inputTokens = 0
179 let cacheReadTokens = 0
180 let outputTokens = 0
181 let streamTokens = 0
182 let streamMs = 0
183 for (const x of steps) {
184 if (x.thinkFirst !== undefined && x.thinkLast !== undefined) thinkingMs += x.thinkLast - x.thinkFirst
185 if (x.outFirst !== undefined) outputMs += end(x) - x.outFirst
186 if (x.usage) {
187 inputTokens += x.usage.input_tokens + x.usage.cache_read_input_tokens + x.usage.cache_creation_input_tokens
188 cacheReadTokens += x.usage.cache_read_input_tokens
189 outputTokens += x.usage.output_tokens
190 if (x.firstContent !== undefined) {
191 streamTokens += x.usage.output_tokens
192 streamMs += end(x) - x.firstContent
193 }
194 }
195 }
196
197 const plain = tools.filter(x => !isAgentTool(x.tool))
198 const timed = plain.map(x => ({ x, ...toolTiming(x, s.now) }))
199 const testIntervals: Interval[] = []
200 const waitIntervals: Interval[] = []
201 let execMs = 0
202 let permissionMs = 0
203 let overheadMs = 0
204 let sumMs = 0
205 const byTool = new Map<string, { tool: string; calls: number; ms: number }>()
206 const byCategory = new Map<BashCategory, { category: BashCategory; calls: number; ms: number }>()
207 for (const { x, wall, exec, wait, approval, otherWait } of timed) {
208 sumMs += wall
209 execMs += exec
210 if (approval !== undefined) {
211 permissionMs += approval
212 waitIntervals.push([x.start, x.start + approval])
213 }
214 overheadMs += otherWait
215 const row = byTool.get(x.tool) ?? { tool: x.tool, calls: 0, ms: 0 }
216 row.calls += 1
217 row.ms += wall
218 byTool.set(x.tool, row)
219 if (x.tool === 'Bash') {
220 const category = classifyCommand(x.input.command ?? '')
221 const cat = byCategory.get(category) ?? { category, calls: 0, ms: 0 }
222 cat.calls += 1
223 cat.ms += exec
224 byCategory.set(category, cat)
225 if (category === 'test') testIntervals.push([x.start + wait, x.start + wall])
226 }
227 }
228 const testMs = byCategory.get('test')?.ms ?? 0
229 const windows = mainTurns.map(t => [t.start, end(t)] as Interval)
230 const share = (intervals: Interval[]) => (wallMs > 0 ? overlapLength(intervals, windows) / wallMs : 0)
231
232 const seen = new Map<string, { path: string; reads: number; redundant: number; ranges: Set<string> }>()
233 for (const x of [...tools].sort((a, b) => a.start - b.start)) {
234 const path = x.input.path
235 if (!path) continue
236 if (x.tool === 'Read') {
237 const row = seen.get(path) ?? { path, reads: 0, redundant: 0, ranges: new Set<string>() }
238 const range = x.input.range ?? ''
239 if (row.ranges.has(range)) row.redundant += 1
240 row.ranges.add(range)
241 row.reads += 1
242 seen.set(path, row)
243 } else if (WRITE_TOOLS.has(x.tool)) {
244 seen.get(path)?.ranges.clear()
245 }
246 }
247
248 const stepsByCtx = groupBy(s.steps, x => x.ctx)
249 const toolsByCtx = groupBy(s.tools, x => x.ctx)
250 const summary: Summary = {
251 scope,
252 turns: mainTurns.length,
253 wallMs,
254 exclusive,
255 model: {
256 requests: steps.length,
257 ttftAvg: ttfts.length ? Math.round(ttfts.reduce((a, b) => a + b, 0) / ttfts.length) : 0,
258 ttftP95: ttfts[Math.max(0, Math.ceil(ttfts.length * 0.95) - 1)] ?? 0,
259 thinkingMs,
260 outputMs,
261 inputTokens,
262 cacheReadTokens,
263 outputTokens,
264 tokensPerSec: streamMs > 0 && streamTokens > 0 ? streamTokens / (streamMs / 1_000) : null,
265 },
266 tools: {
267 calls: plain.length,
268 sumMs,
269 unionMs: unionLength(plain.map(x => [x.start, end(x)] as Interval)),
270 execMs,
271 permissionObserved: plain.filter(x => x.permission !== undefined).length,
272 permissionTimed: timed.filter(x => x.approval !== undefined).length,
273 permissionMs,
274 permissionShare: share(waitIntervals),
275 overheadMs,
276 errors: plain.filter(x => x.isError).length,
277 denied: plain.filter(x => x.denied || x.permission?.decision === 'reject').length,
278 byTool: [...byTool.values()].sort((a, b) => b.ms - a.ms),
279 slowest: [...timed]
280 .sort((a, b) => b.wall - a.wall)
281 .slice(0, 5)
282 .map(({ x, wall, wait }) => ({ tool: x.tool, label: toolLabel(x), ms: wall, waitMs: wait, ctx: x.ctx })),
283 },
284 bash: { byCategory: [...byCategory.values()].sort((a, b) => b.ms - a.ms), testMs, testShare: share(testIntervals) },
285 reads: [...seen.values()]
286 .filter(r => r.reads >= 2)
287 .sort((a, b) => b.redundant - a.redundant || b.reads - a.reads)
288 .slice(0, 10)
289 .map(({ path, reads, redundant }) => ({ path, reads, redundant })),
290 agents: agents
291 .map(agent => {
292 const parent = agent.parentToolId ? toolById.get(agent.parentToolId) : undefined
293 const ownSteps = stepsByCtx.get(agent.id) ?? []
294 const ownTools = toolsByCtx.get(agent.id) ?? []
295 return {
296 id: agent.id,
297 agentType: agent.agentType,
298 wallMs: end(agent) - agent.start,
299 modelMs: unionLength(ownSteps.map(x => [x.start, end(x)] as Interval)),
300 toolMs: unionLength(ownTools.map(x => [x.start, end(x)] as Interval)),
301 requests: ownSteps.length,
302 tools: ownTools.length,
303 blocking: parent !== undefined && parent.launchedAsync !== true,
304 done: agent.end !== undefined,
305 }
306 })
307 .sort((a, b) => b.wallMs - a.wallMs),
308 findings: [],
309 incomplete: [...mainTurns, ...steps, ...tools, ...agents].filter(x => x.end === undefined).length,
310 droppedTurns: s.droppedTurns,
311 droppedSpans: s.droppedSpans,
312 }
313 summary.findings = buildFindings(summary)
314 return summary
315}
316
317function buildFindings(sum: Summary): string[] {
318 const wall = sum.wallMs
319 const out: string[] = []
320 if (wall < 1_000) return out
321 if (sum.exclusive.model / wall >= 0.7) {
322 out.push(`Waiting for the model is ${pct(sum.exclusive.model, wall)} of agent time (TTFT p95 ${fmtMs(sum.model.ttftP95)}); tools are not the bottleneck.`)
323 }
324 if (sum.bash.testShare >= 0.25) {
325 const runs = sum.bash.byCategory.find(c => c.category === 'test')?.calls ?? 0
326 out.push(`Tests took ${pct(sum.bash.testShare, 1)} of agent time (${fmtMs(sum.bash.testMs)} over ${runs} run(s)).`)
327 }
328 if (sum.tools.permissionShare >= 0.2) {
329 out.push(`Observed approval/check wait accounts for ${pct(sum.tools.permissionShare, 1)} of agent time (${fmtMs(sum.tools.permissionMs)}, estimated from ${sum.tools.permissionTimed}/${sum.tools.calls} calls; includes pre-decision checks and hooks).`)
330 }
331 for (const r of sum.reads) {
332 if (r.redundant >= 2) out.push(`${clip(r.path, 80)} was read ${r.reads}× (${r.redundant}× with no edit in between).`)
333 }
334 for (const a of sum.agents) {
335 if (a.blocking && a.wallMs / wall >= 0.5) out.push(`Subagent ${a.agentType} blocked the main loop for ${pct(a.wallMs, wall)} (${fmtMs(a.wallMs)}).`)
336 }
337 const top = sum.tools.slowest[0]
338 if (top && top.ms / wall >= 0.3) out.push(`One call took ${pct(top.ms, wall)}: ${clip(top.label, 60)} (${fmtMs(top.ms)}).`)
339 if (sum.exclusive.engine / wall >= 0.25) {
340 out.push(`${pct(sum.exclusive.engine, wall)} of the time is outside model requests and tools (hooks, context building, compaction).`)
341 }
342 return out
343}
344hooks/core/cache.ts 17 lines1import { summarize, type Scope, type Summary } from './analyze.ts'
2import type { Profiler } from './profiler.ts'
3
4// Share summary work between status, pane and ticker.
5export class SummaryCache {
6 private readonly summaries = new Map<Scope, { profiler: Profiler; version: number; bucket: number; sum: Summary }>()
7
8 get(profiler: Profiler, scope: Scope = 'session', now = Date.now()): Summary {
9 const hit = this.summaries.get(scope)
10 const bucket = profiler.isLive ? Math.floor(now / 500) : -1
11 if (hit && hit.profiler === profiler && hit.version === profiler.version && hit.bucket === bucket) return hit.sum
12 const sum = summarize(profiler.snapshot(now), scope)
13 this.summaries.set(scope, { profiler, version: profiler.version, bucket, sum })
14 return sum
15 }
16}
17hooks/core/export.ts 54 lines1import type { OtlpRequest, OtlpSpan } from './otlp.ts'
2import { recordKey } from './otlp.ts'
3import { MAIN } from './profiler.ts'
4import type { Snapshot } from './types.ts'
5
6// $.fs.write refuses more than 4 MiB; stay well under it.
7export const MAX_EXPORT_BYTES = Math.floor(3.5 * 1024 * 1024)
8
9// Keys of the records not exported yet: finished ones only for a per-turn batch, open ones too
10// (exported as incomplete) for the final batch at session end.
11export function pendingKeys(s: Snapshot, exported: ReadonlySet<string>, withOpen: boolean): string[] {
12 const out: string[] = []
13 const take = (key: string, finished: boolean) => {
14 if (!exported.has(key) && (withOpen || finished)) out.push(key)
15 }
16 for (const t of s.turns) if (t.ctx === MAIN) take(recordKey('turn', t.id), t.end !== undefined)
17 for (const a of s.agents) take(recordKey('agent', a.id), a.end !== undefined)
18 for (const x of s.steps) take(recordKey('step', x.id), x.end !== undefined)
19 for (const x of s.tools) take(recordKey('tool', x.id), x.end !== undefined)
20 return out
21}
22
23function byteLength(text: string): number {
24 return new TextEncoder().encode(text).length
25}
26
27// Splits a request into requests whose JSON stays within maxBytes (a single span larger than that
28// goes alone). Every part keeps the resource and scope; no spans means no parts.
29export function splitRequest(req: OtlpRequest, maxBytes: number): OtlpRequest[] {
30 const resource = req.resourceSpans[0]
31 const scope = resource?.scopeSpans[0]
32 if (!resource || !scope || scope.spans.length === 0) return []
33 const wrap = (spans: OtlpSpan[]): OtlpRequest => ({
34 resourceSpans: [{ resource: resource.resource, scopeSpans: [{ scope: scope.scope, spans }] }],
35 })
36 if (byteLength(JSON.stringify(req)) <= maxBytes) return [req]
37 const envelope = byteLength(JSON.stringify(wrap([])))
38 const parts: OtlpRequest[] = []
39 let current: OtlpSpan[] = []
40 let size = envelope
41 for (const span of scope.spans) {
42 const bytes = byteLength(JSON.stringify(span)) + 1 // the comma between spans
43 if (current.length > 0 && size + bytes > maxBytes) {
44 parts.push(wrap(current))
45 current = []
46 size = envelope
47 }
48 current.push(span)
49 size += bytes
50 }
51 if (current.length > 0) parts.push(wrap(current))
52 return parts
53}
54hooks/core/otlp.ts 222 lines1import { toolTiming } from './permission.ts'
2import { classifyCommand } from './classify.ts'
3import { spanId, traceIdFromSession } from './ids.ts'
4import { MAIN, isAgentTool } from './profiler.ts'
5import { redactSecrets } from './redact.ts'
6import type { Snapshot } from './types.ts'
7
8type AttrValue = { stringValue: string } | { intValue: string } | { doubleValue: number } | { boolValue: boolean }
9export type OtlpAttr = { key: string; value: AttrValue }
10export type OtlpSpan = {
11 traceId: string
12 spanId: string
13 parentSpanId?: string
14 name: string
15 kind: number
16 startTimeUnixNano: string
17 endTimeUnixNano: string
18 attributes: OtlpAttr[]
19 status: { code: number; message?: string }
20}
21export type OtlpRequest = {
22 resourceSpans: { resource: { attributes: OtlpAttr[] }; scopeSpans: { scope: { name: string; version: string }; spans: OtlpSpan[] }[] }[]
23}
24type Attrs = Record<string, string | number | boolean | undefined>
25
26export type OtlpOptions = {
27 // Which records to emit, by recordKey(); a record's phase spans (ttft, exec, wait, ...) go with it. Default: all.
28 include?: (key: string) => boolean
29 // Emit the session root span. Default true; batches leave it to the final export.
30 root?: boolean
31 // Export prompts, Bash commands and search patterns (redacted). Default false: all are left out.
32 commandText?: boolean
33}
34
35export type RecordKind = 'turn' | 'step' | 'tool' | 'agent'
36
37export function recordKey(kind: RecordKind, id: string): string {
38 return `${kind}:${id}`
39}
40
41const INTERNAL = 1
42const CLIENT = 3
43const STATUS_OK = 1
44const STATUS_ERROR = 2
45
46export function nanos(ms: number): string {
47 return (BigInt(Math.round(ms)) * 1_000_000n).toString()
48}
49
50function attrs(values: Attrs): OtlpAttr[] {
51 const out: OtlpAttr[] = []
52 for (const [key, v] of Object.entries(values)) {
53 if (v === undefined || v === '') continue
54 if (typeof v === 'string') out.push({ key, value: { stringValue: v } })
55 else if (typeof v === 'boolean') out.push({ key, value: { boolValue: v } })
56 else if (Number.isInteger(v)) out.push({ key, value: { intValue: String(v) } })
57 else out.push({ key, value: { doubleValue: v } })
58 }
59 return out
60}
61
62export function toOtlp(s: Snapshot, version: string, options: OtlpOptions = {}): OtlpRequest {
63 const traceId = traceIdFromSession(s.sessionId)
64 const include = options.include ?? (() => true)
65 const want = (kind: RecordKind, id: string) => include(recordKey(kind, id))
66 const text = (value: string | undefined) => (options.commandText === true && value ? redactSecrets(value) : undefined)
67 const spans: OtlpSpan[] = []
68 const end = (x: { end?: number }) => x.end ?? s.now
69 const open = (x: { end?: number }) => (x.end === undefined ? true : undefined)
70 const add = (kind: string, key: string, name: string, parent: string | undefined, start: number, stop: number, values: Attrs, extra: { spanKind?: number; error?: string } = {}): string => {
71 const id = spanId(kind, key)
72 spans.push({
73 traceId,
74 spanId: id,
75 ...(parent ? { parentSpanId: parent } : {}),
76 name,
77 kind: extra.spanKind ?? INTERNAL,
78 startTimeUnixNano: nanos(start),
79 endTimeUnixNano: nanos(Math.max(start, stop)),
80 attributes: attrs(values),
81 status: extra.error ? { code: STATUS_ERROR, message: extra.error } : { code: STATUS_OK },
82 })
83 return id
84 }
85
86 // Span ids are deterministic, so a span can name a parent emitted in another batch.
87 const root = spanId('session', s.sessionId)
88 if (options.root !== false) {
89 const sessionStart = [...s.turns, ...s.steps, ...s.tools, ...s.agents].reduce((min, x) => Math.min(min, x.start), s.started ?? s.now)
90 add('session', s.sessionId, 'claude-code session', undefined, sessionStart, s.now, {
91 'session.id': s.sessionId,
92 'agent_profiler.observability': 'observed',
93 })
94 }
95
96 const mainTurns = new Set<string>()
97 for (const t of s.turns) {
98 if (t.ctx !== MAIN) continue
99 mainTurns.add(t.id)
100 if (!want('turn', t.id)) continue
101 add('turn', t.id, 'turn', root, t.start, end(t), {
102 'agent_profiler.category': 'turn',
103 'agent_profiler.observability': 'observed',
104 'agent_profiler.turn.reason': t.reason,
105 'agent_profiler.turn.prompt': text(t.prompt),
106 'agent_profiler.incomplete': open(t),
107 })
108 }
109
110 const agents = new Set(s.agents.map(a => a.id))
111 const tools = new Map(s.tools.map(t => [t.id, t]))
112 const parentOf = (ctx: string, turnId?: string): string =>
113 ctx === MAIN
114 ? turnId !== undefined && mainTurns.has(turnId)
115 ? spanId('turn', turnId)
116 : root
117 : agents.has(ctx)
118 ? spanId('agent', ctx)
119 : root
120
121 for (const a of s.agents) {
122 if (!want('agent', a.id)) continue
123 const parentTool = a.parentToolId ? tools.get(a.parentToolId) : undefined
124 add('agent', a.id, `invoke_agent ${a.agentType}`, parentTool ? spanId('tool', parentTool.id) : root, a.start, end(a), {
125 'gen_ai.operation.name': 'invoke_agent',
126 'gen_ai.agent.id': a.id,
127 'gen_ai.agent.name': a.agentType,
128 'agent_profiler.category': 'subagent',
129 'agent_profiler.observability': 'observed',
130 'agent_profiler.async': parentTool ? parentTool.launchedAsync === true : undefined,
131 'agent_profiler.incomplete': open(a),
132 })
133 }
134
135 for (const x of s.steps) {
136 if (!want('step', x.id)) continue
137 const ttft = x.firstContent !== undefined ? x.firstContent - x.start : undefined
138 const id = add('step', x.id, `chat ${x.model}`, parentOf(x.ctx, x.turnId), x.start, end(x), {
139 'gen_ai.operation.name': 'chat',
140 'gen_ai.request.model': x.model,
141 'gen_ai.response.model': x.usage?.model,
142 'gen_ai.usage.input_tokens': x.usage?.input_tokens,
143 'gen_ai.usage.output_tokens': x.usage?.output_tokens,
144 'gen_ai.response.finish_reasons': x.stopReason,
145 'gen_ai.agent.id': x.ctx === MAIN ? undefined : x.ctx,
146 'agent_profiler.usage.cache_read_input_tokens': x.usage?.cache_read_input_tokens,
147 'agent_profiler.usage.cache_creation_input_tokens': x.usage?.cache_creation_input_tokens,
148 'agent_profiler.effort': x.effort,
149 'agent_profiler.ttft_ms': ttft,
150 'agent_profiler.category': 'model',
151 'agent_profiler.observability': 'observed',
152 'agent_profiler.incomplete': open(x),
153 }, { spanKind: CLIENT })
154 if (x.firstContent !== undefined) {
155 add('step.ttft', x.id, 'model.ttft', id, x.start, x.firstContent, {
156 'agent_profiler.observability': 'observed',
157 'agent_profiler.note': 'network + queueing + prefill, not separable from the client',
158 })
159 }
160 if (x.thinkFirst !== undefined && x.thinkLast !== undefined) {
161 add('step.thinking', x.id, 'model.thinking_stream', id, x.thinkFirst, x.thinkLast, { 'agent_profiler.observability': 'observed' })
162 }
163 if (x.outFirst !== undefined) {
164 add('step.output', x.id, 'model.output_stream', id, x.outFirst, end(x), { 'agent_profiler.observability': 'observed' })
165 }
166 }
167
168 for (const x of s.tools) {
169 if (!want('tool', x.id)) continue
170 const timing = toolTiming(x, s.now)
171 const exec = timing.execKnown ? timing.exec : undefined
172 const wait = timing.execKnown ? timing.wait : undefined
173 const id = add('tool', x.id, `execute_tool ${x.tool}`, parentOf(x.ctx, x.turnId), x.start, end(x), {
174 'gen_ai.operation.name': 'execute_tool',
175 'gen_ai.tool.name': x.tool,
176 'gen_ai.tool.call.id': x.id,
177 'gen_ai.agent.id': x.ctx === MAIN ? undefined : x.ctx,
178 // An async Agent call returns at once and does not block the turn, as in summarize().
179 'agent_profiler.category': isAgentTool(x.tool) && x.launchedAsync !== true ? 'subagent' : 'tool',
180 'agent_profiler.observability': 'observed',
181 'agent_profiler.exec_ms': exec,
182 'agent_profiler.wait_ms': wait,
183 'agent_profiler.permission_decision': x.permission?.decision,
184 'agent_profiler.permission_source': x.permission?.source,
185 'agent_profiler.permission_classifier': x.permission?.classifier,
186 'agent_profiler.permission_wait_ms': timing.approval,
187 'agent_profiler.permission_wait_kind': timing.kind,
188 'agent_profiler.fs.path': x.input.path,
189 'agent_profiler.fs.range': x.input.range,
190 'agent_profiler.search.pattern': text(x.input.pattern),
191 'agent_profiler.bash.command': text(x.input.command),
192 'agent_profiler.bash.category': x.tool === 'Bash' ? classifyCommand(x.input.command ?? '') : undefined,
193 'agent_profiler.launched_agent': x.launchedAgentId,
194 'agent_profiler.incomplete': open(x),
195 }, { error: x.denied || x.permission?.decision === 'reject' ? 'denied' : x.isError ? 'tool error' : undefined })
196 if (exec !== undefined && wait !== undefined && x.end !== undefined) {
197 if (timing.approval !== undefined && timing.approval > 0) {
198 add('tool.approval', x.id, 'tool.approval_wait', id, x.start, x.start + timing.approval, {
199 'agent_profiler.observability': 'inferred',
200 'agent_profiler.permission_wait_kind': timing.kind,
201 'agent_profiler.note': 'estimated prefix includes pre-decision checks and hooks',
202 })
203 }
204 if (timing.otherWait > 0) {
205 add('tool.wait', x.id, timing.approval !== undefined ? 'tool.overhead' : 'tool.wait_unclassified', id,
206 x.start + (timing.approval ?? 0), x.start + wait, { 'agent_profiler.observability': 'inferred' })
207 }
208 if (x.permission?.decision !== 'reject')
209 add('tool.exec', x.id, 'tool.exec', id, x.start + wait, x.end, { 'agent_profiler.observability': 'measured' })
210 }
211 }
212
213 return {
214 resourceSpans: [
215 {
216 resource: { attributes: attrs({ 'service.name': 'claude-code', 'session.id': s.sessionId, 'agent_profiler.version': version }) },
217 scopeSpans: [{ scope: { name: 'agent-profiler', version }, spans }],
218 },
219 ],
220 }
221}
222hooks/core/profiler.ts 408 lines1import { permissionObservation } from './permission.ts'
2import type { AgentRec, Outcome, Snapshot, StepRec, ToolInputSummary, ToolRec, TurnRec, Usage } from './types.ts'
3import { redactSecrets } from './redact.ts'
4
5export const MAIN = 'main'
6
7export type RunningItem = { kind: 'model' | 'tool' | 'agent'; label: string; ctx: string; ms: number }
8
9export function isAgentTool(tool: string): boolean {
10 return tool === 'Agent' || tool === 'Task'
11}
12
13export function toolLabel(tool: ToolRec): string {
14 const detail = tool.input.command ?? tool.input.path ?? tool.input.pattern ?? tool.input.subagentType ?? ''
15 return redactSecrets(detail ? `${tool.tool} ${detail}` : tool.tool)
16}
17
18function text(value: unknown, n: number): string | undefined {
19 return typeof value === 'string' ? value.slice(0, n) : undefined
20}
21
22function safeText(value: unknown, n: number): string | undefined {
23 return typeof value === 'string' ? redactSecrets(value).slice(0, n) : undefined
24}
25
26function defined<T extends object>(value: T): T {
27 return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined)) as T
28}
29
30export function summarizeInput(tool: string, input: Record<string, unknown>): ToolInputSummary {
31 switch (tool) {
32 case 'Bash':
33 return defined({ command: safeText(input.command, 512), description: safeText(input.description, 120) })
34 case 'Read': {
35 const paged = input.offset !== undefined || input.limit !== undefined
36 return defined({ path: text(input.file_path, 512), range: paged ? `${input.offset ?? ''}:${input.limit ?? ''}` : undefined })
37 }
38 case 'Write':
39 case 'Edit':
40 case 'MultiEdit':
41 return defined({ path: text(input.file_path, 512) })
42 case 'NotebookEdit':
43 return defined({ path: text(input.notebook_path, 512) })
44 case 'Glob':
45 case 'Grep':
46 return defined({ pattern: safeText(input.pattern, 200), path: text(input.path, 512) })
47 case 'Agent':
48 case 'Task':
49 return defined({ subagentType: safeText(input.subagent_type, 80), description: safeText(input.description, 120) })
50 default:
51 return {}
52 }
53}
54
55export function toolOutcome(answer: unknown): Outcome {
56 const value = (answer ?? {}) as { deny?: unknown; isError?: unknown; result?: unknown }
57 const result = (value.result ?? {}) as { agentId?: unknown; status?: unknown }
58 return {
59 denied: typeof value.deny === 'string',
60 isError: value.isError === true,
61 launchedAgentId: typeof result.agentId === 'string' ? result.agentId : undefined,
62 launchedAsync: result.status === 'async_launched',
63 }
64}
65
66export class Profiler {
67 sessionId: string
68 private readonly maxSpans: number
69 private readonly turns = new Map<string, TurnRec>()
70 private readonly steps = new Map<string, StepRec>()
71 private readonly tools = new Map<string, ToolRec>()
72 private readonly agents = new Map<string, AgentRec>()
73 private readonly openTurns = new Map<string, string[]>()
74 private readonly pendingExec = new Map<string, number>()
75 private readonly openSteps = new Set<string>()
76 private readonly openTools = new Set<string>()
77 private readonly openAgents = new Set<string>()
78 private droppedTurns = 0
79 private droppedSpans = 0
80 private started: number | undefined
81 // Bumped on every change, so callers can cache what they derive from a snapshot.
82 version = 0
83
84 get isLive(): boolean {
85 return this.openSteps.size > 0 || this.openTools.size > 0 || this.openAgents.size > 0 || [...this.openTurns.values()].some(turns => turns.length > 0)
86 }
87
88 constructor(sessionId = 'unknown', maxSpans = 20_000) {
89 this.sessionId = sessionId
90 this.maxSpans = maxSpans
91 }
92
93 // A profiler created before the session id was known (a reload mid-session) takes it on later.
94 adoptSessionId(id: string): void {
95 if (this.sessionId !== 'unknown' || !id) return
96 this.sessionId = id
97 this.version += 1
98 }
99
100 turnStart(t: number, turnId: string, prompt = '', ctx = MAIN): void {
101 if (this.turns.has(turnId)) return
102 this.turns.set(turnId, { id: turnId, ctx, start: t, prompt: redactSecrets(prompt).slice(0, 120) })
103 this.seen(t)
104 this.stack(ctx).push(turnId)
105 this.version += 1
106 }
107
108 turnComplete(t: number, turnId: string, reason?: string): void {
109 const turn = this.turns.get(turnId)
110 if (!turn) return
111 if (turn.end === undefined) turn.end = t
112 if (reason !== undefined) turn.reason = reason
113 // Completion is authoritative even when a stream/tool's cleanup arrives late.
114 for (const id of [...this.openSteps]) {
115 if (this.steps.get(id)?.turnId === turnId) this.stepEnd(turn.end, id)
116 }
117 for (const id of [...this.openTools]) {
118 const tool = this.tools.get(id)
119 if (tool?.turnId !== turnId) continue
120 tool.end = turn.end
121 this.openTools.delete(id)
122 }
123 this.unstack(turn.ctx, turnId)
124 this.version += 1
125 }
126
127 // StopFailure can arrive before turn.complete after a terminal API error.
128 // Only settle that loop: independent background agents may still be working.
129 stopFailure(t: number, ctx = MAIN): void {
130 for (const turnId of [...(this.openTurns.get(ctx) ?? [])]) this.turnComplete(t, turnId, 'error')
131 if (ctx !== MAIN) this.subagentStop(t, ctx)
132 for (const id of [...this.openSteps]) {
133 if (this.steps.get(id)?.ctx === ctx) this.stepEnd(t, id)
134 }
135 for (const id of [...this.openTools]) {
136 const tool = this.tools.get(id)
137 if (tool?.ctx !== ctx) continue
138 tool.end = t
139 this.openTools.delete(id)
140 this.version += 1
141 }
142 }
143
144 stepStart(t: number, args: { turnId: string; index: number; model: string; effort?: string | number; agentId?: string }): string {
145 const ctx = args.agentId ?? MAIN
146 const turn = this.turns.get(args.turnId)
147 if (!turn) {
148 this.turnStart(t, args.turnId, '', ctx)
149 } else if (turn.ctx !== ctx) {
150 // The engine may raise turn.start for a subagent's loop too; the step tells us whose turn it is.
151 this.unstack(turn.ctx, turn.id)
152 turn.ctx = ctx
153 if (turn.end === undefined) this.stack(ctx).push(turn.id)
154 }
155 let id = `${args.turnId}#${args.index}`
156 for (let n = 1; this.steps.has(id); n += 1) id = `${args.turnId}#${args.index}.${n}`
157 const step: StepRec = { id, ctx, turnId: args.turnId, index: args.index, model: args.model, start: t }
158 if (args.effort !== undefined) step.effort = String(args.effort)
159 this.steps.set(id, step)
160 this.openSteps.add(id)
161 this.seen(t)
162 this.version += 1
163 this.trim()
164 return id
165 }
166
167 stepChunk(t: number, stepId: string, kind: string): void {
168 const step = this.steps.get(stepId)
169 if (!step || step.end !== undefined || kind === 'engine' || kind === 'stop') return
170 if (kind !== 'thinking' && step.outFirst !== undefined) return
171 if (step.firstContent === undefined) step.firstContent = t
172 if (kind === 'thinking') {
173 if (step.thinkFirst === undefined) step.thinkFirst = t
174 step.thinkLast = t
175 } else {
176 step.outFirst = t
177 }
178 this.version += 1
179 }
180
181 stepUsage(stepId: string, usage: Usage | null | undefined, stopReason?: string | null): void {
182 const step = this.steps.get(stepId)
183 if (!step) return
184 if (usage) step.usage = { ...usage }
185 if (stopReason) step.stopReason = stopReason
186 this.version += 1
187 }
188
189 stepEnd(t: number, stepId: string): void {
190 const step = this.steps.get(stepId)
191 if (!step || step.end !== undefined) return
192 step.end = t
193 this.openSteps.delete(stepId)
194 this.version += 1
195 }
196
197 toolStart(t: number, args: { id: string; tool: string; agentId?: string; input: ToolInputSummary }): void {
198 if (this.tools.has(args.id)) return
199 const ctx = args.agentId ?? MAIN
200 const tool: ToolRec = { id: args.id, ctx, tool: args.tool, input: args.input, start: t }
201 const turnId = this.currentTurn(ctx)
202 if (turnId !== undefined) tool.turnId = turnId
203 const exec = this.pendingExec.get(args.id)
204 if (exec !== undefined) {
205 tool.execMs = exec
206 this.pendingExec.delete(args.id)
207 }
208 this.tools.set(args.id, tool)
209 this.openTools.add(args.id)
210 this.seen(t)
211 this.version += 1
212 this.trim()
213 }
214
215 toolDecision(event: string, attributes: Readonly<Record<string, unknown>>, loggedAt: string): void {
216 const observation = permissionObservation(event, attributes, loggedAt)
217 if (!observation) return
218 const { id, permission } = observation
219 const tool = this.tools.get(id)
220 if (!tool || tool.permission || permission.at < tool.start || (tool.end !== undefined && permission.at > tool.end)) return
221 tool.permission = permission
222 this.version += 1
223 }
224
225 toolDuration(id: string, ms: unknown): void {
226 if (typeof ms !== 'number' || !Number.isFinite(ms) || ms < 0) return
227 const tool = this.tools.get(id)
228 if (tool) {
229 tool.execMs = ms
230 this.version += 1
231 } else if (this.pendingExec.size < 1_000) this.pendingExec.set(id, ms)
232 }
233
234 toolEnd(t: number, id: string, outcome: Outcome): void {
235 const tool = this.tools.get(id)
236 if (!tool || tool.end !== undefined) return
237 tool.end = t
238 this.openTools.delete(id)
239 this.version += 1
240 tool.denied = outcome.denied
241 tool.isError = outcome.isError
242 if (outcome.launchedAgentId) {
243 tool.launchedAgentId = outcome.launchedAgentId
244 tool.launchedAsync = outcome.launchedAsync
245 const agent = this.agents.get(outcome.launchedAgentId)
246 if (agent) agent.parentToolId = id
247 }
248 }
249
250 subagentStart(t: number, agentId: string, agentType: string): void {
251 if (this.agents.has(agentId)) return
252 const claimed = new Set([...this.agents.values()].map(agent => agent.parentToolId))
253 let parent: ToolRec | undefined
254 for (const tool of this.tools.values()) {
255 if (!isAgentTool(tool.tool) || claimed.has(tool.id)) continue
256 if (tool.launchedAgentId !== undefined && tool.launchedAgentId !== agentId) continue
257 if (tool.end !== undefined && t - tool.end > 2_000) continue
258 if (!parent || tool.start > parent.start) parent = tool
259 }
260 const agent: AgentRec = { id: agentId, agentType, start: t }
261 if (parent) agent.parentToolId = parent.id
262 this.agents.set(agentId, agent)
263 this.openAgents.add(agentId)
264 this.seen(t)
265 this.version += 1
266 }
267
268 subagentStop(t: number, agentId: string): void {
269 const agent = this.agents.get(agentId)
270 if (!agent) return
271 if (agent.end === undefined) agent.end = t
272 this.openAgents.delete(agentId)
273 // Whatever the agent left open ended with it, so running() and the ticker settle.
274 for (const id of [...this.openSteps]) {
275 const step = this.steps.get(id)
276 if (step?.ctx === agentId) this.stepEnd(t, id)
277 }
278 for (const id of [...this.openTools]) {
279 const tool = this.tools.get(id)
280 if (tool?.ctx !== agentId) continue
281 tool.end = t
282 this.openTools.delete(id)
283 }
284 this.version += 1
285 for (const turnId of [...(this.openTurns.get(agentId) ?? [])]) this.turnComplete(t, turnId, 'agent-stop')
286 }
287
288 running(now: number): RunningItem[] {
289 // Walks only the open spans, so the once-a-second ticker stays cheap however long the session.
290 const items: RunningItem[] = []
291 for (const id of this.openSteps) {
292 const step = this.steps.get(id)
293 if (step) items.push({ kind: 'model', label: `model ${step.model}`, ctx: step.ctx, ms: now - step.start })
294 }
295 for (const id of this.openTools) {
296 const tool = this.tools.get(id)
297 if (tool) items.push({ kind: 'tool', label: toolLabel(tool), ctx: tool.ctx, ms: now - tool.start })
298 }
299 for (const id of this.openAgents) {
300 const agent = this.agents.get(id)
301 if (agent) items.push({ kind: 'agent', label: `agent ${agent.agentType}`, ctx: agent.id, ms: now - agent.start })
302 }
303 return items.sort((a, b) => b.ms - a.ms)
304 }
305
306 snapshot(now: number): Snapshot {
307 return {
308 sessionId: this.sessionId,
309 now,
310 turns: [...this.turns.values()].map(turn => ({ ...turn })),
311 steps: [...this.steps.values()].map(step => ({ ...step, ...(step.usage ? { usage: { ...step.usage } } : {}) })),
312 tools: [...this.tools.values()].map(tool => ({ ...tool, input: { ...tool.input } })),
313 agents: [...this.agents.values()].map(agent => ({ ...agent })),
314 ...(this.started !== undefined ? { started: this.started } : {}),
315 droppedTurns: this.droppedTurns,
316 droppedSpans: this.droppedSpans,
317 }
318 }
319
320 private currentTurn(ctx: string): string | undefined {
321 const open = this.openTurns.get(ctx)
322 return open && open.length > 0 ? open[open.length - 1] : undefined
323 }
324
325 private stack(ctx: string): string[] {
326 let open = this.openTurns.get(ctx)
327 if (!open) {
328 open = []
329 this.openTurns.set(ctx, open)
330 }
331 return open
332 }
333
334 private unstack(ctx: string, turnId: string): void {
335 const open = this.openTurns.get(ctx)
336 const at = open ? open.indexOf(turnId) : -1
337 if (open && at >= 0) open.splice(at, 1)
338 }
339
340 private seen(t: number): void {
341 if (this.started === undefined || t < this.started) this.started = t
342 }
343
344 // Past the cap, drop whole finished main turns, oldest first, with their requests and tools and the
345 // subagents launched from them (and those subagents' own work). Dropping a turn's spans without the
346 // turn would show their time as engine overhead.
347 private trim(): void {
348 if (this.steps.size + this.tools.size <= this.maxSpans) return
349 const target = Math.floor(this.maxSpans * 0.9)
350 const byTurn = new Map<string, { steps: string[]; tools: string[] }>()
351 const byCtx = new Map<string, { steps: string[]; tools: string[]; turns: string[] }>()
352 const slot = <K, V>(map: Map<K, V>, key: K, make: () => V): V => {
353 let value = map.get(key)
354 if (!value) {
355 value = make()
356 map.set(key, value)
357 }
358 return value
359 }
360 for (const step of this.steps.values()) {
361 if (step.ctx === MAIN) slot(byTurn, step.turnId, () => ({ steps: [], tools: [] })).steps.push(step.id)
362 else slot(byCtx, step.ctx, () => ({ steps: [], tools: [], turns: [] })).steps.push(step.id)
363 }
364 for (const tool of this.tools.values()) {
365 if (tool.ctx !== MAIN) slot(byCtx, tool.ctx, () => ({ steps: [], tools: [], turns: [] })).tools.push(tool.id)
366 else if (tool.turnId !== undefined) slot(byTurn, tool.turnId, () => ({ steps: [], tools: [] })).tools.push(tool.id)
367 }
368 for (const turn of this.turns.values()) {
369 if (turn.ctx !== MAIN) slot(byCtx, turn.ctx, () => ({ steps: [], tools: [], turns: [] })).turns.push(turn.id)
370 }
371 const launched = new Map<string, string[]>()
372 for (const agent of this.agents.values()) {
373 if (agent.parentToolId !== undefined) slot(launched, agent.parentToolId, () => [] as string[]).push(agent.id)
374 }
375
376 for (const turn of [...this.turns.values()]) {
377 if (this.steps.size + this.tools.size <= target) return
378 if (turn.ctx !== MAIN || turn.end === undefined) continue
379 const own = byTurn.get(turn.id) ?? { steps: [], tools: [] }
380 const steps = [...own.steps]
381 const tools = [...own.tools]
382 const turns = [turn.id]
383 const agents: string[] = []
384 for (let i = 0; i < tools.length; i += 1) {
385 for (const agentId of launched.get(tools[i] as string) ?? []) {
386 if (agents.includes(agentId)) continue
387 agents.push(agentId)
388 const work = byCtx.get(agentId)
389 if (!work) continue
390 steps.push(...work.steps)
391 tools.push(...work.tools)
392 turns.push(...work.turns)
393 }
394 }
395 const busy =
396 steps.some(id => this.openSteps.has(id)) || tools.some(id => this.openTools.has(id)) || agents.some(id => this.openAgents.has(id))
397 if (busy) continue
398 for (const id of steps) this.steps.delete(id)
399 for (const id of tools) this.tools.delete(id)
400 for (const id of agents) this.agents.delete(id)
401 for (const id of turns) this.turns.delete(id)
402 this.droppedTurns += 1
403 this.droppedSpans += steps.length + tools.length + agents.length
404 this.version += 1
405 }
406 }
407}
408hooks/core/report.ts 109 lines1import type { Category, Summary } from './analyze.ts'
2import { bar, clip as clipText, fmtCount, fmtMs, pct } from './format.ts'
3import { redactSecrets } from './redact.ts'
4import type { RunningItem } from './profiler.ts'
5
6const ORDER: Category[] = ['model', 'tool', 'subagent', 'engine']
7const LABEL: Record<Category, string> = {
8 model: 'Model response wait',
9 tool: 'Tools',
10 subagent: 'Subagents (blocking)',
11 engine: 'Engine/other (inferred)',
12}
13const SHORT: Record<Category, string> = { model: 'model', tool: 'tools', subagent: 'subagents', engine: 'engine/other' }
14const clip = (text: string, n: number): string => clipText(redactSecrets(text), n)
15
16export type PaneLine = { text: string; bold?: boolean; dim?: boolean }
17
18export function renderReport(sum: Summary): string {
19 const wall = sum.wallMs
20 const lines = [`Agent Profiler · ${sum.scope === 'turn' ? 'last turn' : 'session'} · ${sum.turns} turn(s) · ${fmtMs(wall)} agent time`]
21 if (sum.turns === 0) {
22 lines.push('No turns recorded yet.')
23 return lines.map(redactSecrets).join('\n')
24 }
25 if (sum.scope === 'session' && sum.droppedTurns > 0) {
26 lines.push(`Covers the last ${sum.turns} turn(s); ${sum.droppedTurns} older turn(s) dropped (already exported).`)
27 }
28 lines.push('', 'Where the time went (exclusive; adds up to agent time)')
29 for (const cat of ORDER) {
30 const ms = sum.exclusive[cat]
31 lines.push(` ${LABEL[cat].padEnd(24)} ${fmtMs(ms).padStart(7)} ${pct(ms, wall).padStart(4)} ${bar(wall ? ms / wall : 0)}`)
32 }
33
34 const m = sum.model
35 lines.push(
36 '',
37 `Model requests [observed] ${m.requests} · TTFT avg ${fmtMs(m.ttftAvg)} / p95 ${fmtMs(m.ttftP95)} · thinking stream ${fmtMs(m.thinkingMs)} · output stream ${fmtMs(m.outputMs)}`,
38 ` tokens [reported] in ${fmtCount(m.inputTokens)} (cache read ${fmtCount(m.cacheReadTokens)}) · out ${fmtCount(m.outputTokens)}${m.tokensPerSec === null ? '' : ` · ${Math.round(m.tokensPerSec)} tok/s`}`,
39 )
40
41 const t = sum.tools
42 const permission = t.permissionTimed > 0 ? `${fmtMs(t.permissionMs)} [estimated; ${t.permissionTimed}/${t.calls} calls]` : 'unavailable'
43 const parallel = t.unionMs > 0 ? (t.sumMs / t.unionMs).toFixed(1) : '1.0'
44 lines.push(
45 '',
46 `Tools ${t.calls} call(s) · exec ${fmtMs(t.execMs)} [measured] · approval/check wait ${permission} · other/unclassified wait ${fmtMs(t.overheadMs)} [inferred] · parallelism ${parallel}×${t.errors ? ` · ${t.errors} error(s)` : ''}${t.denied ? ` · ${t.denied} denied` : ''}`,
47 )
48 lines.push(`Permission results [observed] ${t.permissionObserved}/${t.calls} calls; unclassified waits may include approval time.`)
49 for (const row of t.slowest) {
50 const wait = row.waitMs >= 1_000 ? ` (wait ${fmtMs(row.waitMs)})` : ''
51 const where = row.ctx === 'main' ? '' : ` [agent ${row.ctx.slice(0, 8)}]`
52 lines.push(` ${fmtMs(row.ms).padStart(7)} ${clip(row.label, 70)}${wait}${where}`)
53 }
54
55 if (sum.bash.byCategory.length) {
56 lines.push('', 'Bash by category [measured]')
57 for (const row of sum.bash.byCategory) lines.push(` ${row.category.padEnd(8)} ${fmtMs(row.ms).padStart(7)} ${row.calls}×`)
58 }
59 if (sum.reads.length) {
60 lines.push('', 'Files read more than once')
61 for (const row of sum.reads) lines.push(` ${String(row.reads).padStart(3)}× ${clip(row.path, 70)}${row.redundant ? ` (${row.redundant}× unchanged)` : ''}`)
62 }
63 if (sum.agents.length) {
64 lines.push('', 'Subagents')
65 for (const a of sum.agents) {
66 lines.push(` ${fmtMs(a.wallMs).padStart(7)} ${a.agentType} ${a.blocking ? 'blocking' : 'background'} · model ${fmtMs(a.modelMs)} · tools ${fmtMs(a.toolMs)} · ${a.requests} req / ${a.tools} tool(s)${a.done ? '' : ' · running'}`)
67 }
68 }
69 if (sum.findings.length) {
70 lines.push('', 'Findings')
71 for (const finding of sum.findings) lines.push(` • ${finding}`)
72 }
73 lines.push(
74 '',
75 'observed = timed by this mod at event boundaries · measured = timed by Claude Code · reported = API usage · inferred = remainder.',
76 "Model time is how long the agent waited for the response, not the model's internal compute time.",
77 )
78 if (sum.incomplete) lines.push(`${sum.incomplete} span(s) still open.`)
79 return lines.map(redactSecrets).join('\n')
80}
81
82export function statusLine(sum: Summary, running: RunningItem[]): string {
83 const wall = sum.wallMs
84 const parts = [`⏱ model ${pct(sum.exclusive.model, wall)}`, `tools ${pct(sum.exclusive.tool, wall)}`]
85 if (sum.exclusive.subagent > 0) parts.push(`agents ${pct(sum.exclusive.subagent, wall)}`)
86 const top = running.find(item => item.kind !== 'agent') ?? running[0]
87 if (top && top.ms >= 1_000) parts.push(`▶ ${clip(top.label, 40)} ${fmtMs(top.ms)}`)
88 return parts.join(' · ')
89}
90
91export function paneLines(sum: Summary, running: RunningItem[], width: number): PaneLine[] {
92 const w = Math.max(30, width)
93 const out: PaneLine[] = [{ text: `${fmtMs(sum.wallMs)} agent time · ${sum.turns} turn(s)`, bold: true }]
94 for (const cat of ORDER) {
95 const share = sum.wallMs ? sum.exclusive[cat] / sum.wallMs : 0
96 out.push({ text: `${SHORT[cat].padEnd(12)} ${bar(share, Math.min(20, w - 20))} ${pct(sum.exclusive[cat], sum.wallMs).padStart(4)}` })
97 }
98 out.push({ text: '' }, { text: 'Running', bold: true })
99 if (running.length === 0) out.push({ text: ' idle', dim: true })
100 for (const item of running.slice(0, 5)) out.push({ text: clip(` ${fmtMs(item.ms).padStart(6)} ${item.label}`, w) })
101 out.push({ text: '' }, { text: 'Slowest tools', bold: true })
102 for (const row of sum.tools.slowest) out.push({ text: clip(` ${fmtMs(row.ms).padStart(6)} ${row.label}`, w) })
103 if (sum.findings.length) {
104 out.push({ text: '' }, { text: 'Findings', bold: true })
105 for (const finding of sum.findings.slice(0, 4)) out.push({ text: clip(`• ${finding}`, w * 2) })
106 }
107 return out
108}
109hooks/core/permission.ts 32 lines1import type { PermissionObservation, ToolRec } from './types.ts'
2
3const SOURCES = ['config', 'hook', 'user_permanent', 'user_temporary', 'user_abort', 'user_reject'] as const
4const CLASSIFIERS = ['allow', 'block', 'no_verdict', 'unavailable'] as const
5
6// Collector rows are observations, not instructions. Keep only fixed enum values and identity.
7export function permissionObservation(event: string, attrs: Readonly<Record<string, unknown>>, loggedAt: string): { id: string; permission: PermissionObservation } | undefined {
8 if (event !== 'tool_decision') return undefined
9 const id = attrs.tool_use_id
10 const decision = attrs.decision
11 const at = Date.parse(loggedAt)
12 if (typeof id !== 'string' || !id || id.length > 256 || (decision !== 'accept' && decision !== 'reject') || !Number.isFinite(at)) return undefined
13 const source = SOURCES.find(value => value === attrs.source) ?? 'unknown'
14 const classifier = CLASSIFIERS.find(value => value === attrs.classifier_verdict)
15 return { id, permission: { decision, source, at, ...(classifier ? { classifier } : {}) } }
16}
17
18// A decision timestamp is not a prompt-open timestamp: this prefix also includes earlier checks/hooks.
19// Missing execution evidence leaves approval timing unknown, rather than inventing zero milliseconds.
20export function toolTiming(tool: ToolRec, now: number) {
21 const wall = Math.max(0, (tool.end ?? now) - tool.start)
22 const rejected = tool.permission?.decision === 'reject'
23 const execKnown = rejected || tool.execMs !== undefined
24 const exec = rejected ? 0 : tool.execMs !== undefined ? Math.max(0, Math.min(tool.execMs, wall)) : wall
25 const wait = wall - exec
26 const permission = tool.permission
27 const kind = permission?.source.startsWith('user_') ? 'user' : permission?.source === 'config' && permission.classifier ? 'auto' : undefined
28 const approval = permission && kind && execKnown && tool.end !== undefined
29 ? Math.max(0, Math.min(wait, permission.at - tool.start)) : undefined
30 return { wall, exec, wait, execKnown, approval, kind: approval !== undefined ? kind : undefined, otherWait: wait - (approval ?? 0) }
31}
32hooks/core/classify.ts 182 lines1export type BashCategory = 'test' | 'build' | 'install' | 'git' | 'search' | 'other'
2
3// Blank out single/double quoted strings and $(...) bodies before splitting
4function blankOutStringLiterals(command: string): string {
5 let result = ''
6 let i = 0
7
8 while (i < command.length) {
9 // Single-quoted string: no escaping inside
10 if (command[i] === "'") {
11 result += "'"
12 i++
13 while (i < command.length && command[i] !== "'") {
14 result += ' '
15 i++
16 }
17 if (i < command.length) {
18 result += "'"
19 i++
20 }
21 }
22 // Double-quoted string: escaping possible
23 else if (command[i] === '"') {
24 result += '"'
25 i++
26 while (i < command.length && command[i] !== '"') {
27 if (command[i] === '\\' && i + 1 < command.length) {
28 result += ' '
29 i += 2
30 } else {
31 result += ' '
32 i++
33 }
34 }
35 if (i < command.length) {
36 result += '"'
37 i++
38 }
39 }
40 // $(...) substitution
41 else if (command[i] === '$' && i + 1 < command.length && command[i + 1] === '(') {
42 result += ' '
43 i += 2
44 let depth = 1
45 while (i < command.length && depth > 0) {
46 if (command[i] === '(') {
47 depth++
48 } else if (command[i] === ')') {
49 depth--
50 }
51 result += depth === 0 ? ')' : ' '
52 i++
53 }
54 } else {
55 result += command[i]
56 i++
57 }
58 }
59
60 return result
61}
62
63// Normalise: strip leading env vars and wrappers
64function normalise(segment: string): string {
65 if (typeof segment !== 'string') return ''
66
67 let s = segment.trim()
68 const wrappers = ['pnpm exec', 'poetry run', 'yarn dlx', 'uv run', 'npx', 'bunx', 'env', 'time', 'sudo']
69
70 let changed = true
71 while (changed) {
72 changed = false
73
74 // Strip leading env var assignments (VAR=value ...)
75 const envMatch = s.match(/^([A-Za-z_][A-Za-z0-9_]*=\S+\s+)+/)
76 if (envMatch) {
77 s = s.slice(envMatch[0].length).trim()
78 changed = true
79 continue
80 }
81
82 // Strip leading wrappers
83 for (const wrapper of wrappers) {
84 if (s.startsWith(wrapper + ' ')) {
85 s = s.slice(wrapper.length + 1).trim()
86 changed = true
87 break
88 }
89 }
90 }
91
92 return s
93}
94
95// Classify gradle/gradlew/mvn commands by analyzing task tokens
96function classifyGradleMvn(segment: string): BashCategory {
97 const tokens = segment.split(/\s+/)
98
99 for (let i = 0; i < tokens.length; i++) {
100 const token = tokens[i] ?? ''
101
102 // Skip if it starts with - (flag or option)
103 if (token.startsWith('-')) {
104 // Special case: if this is -x and next token is test, skip both
105 if (token === '-x' && i + 1 < tokens.length && tokens[i + 1] === 'test') {
106 i++ // skip the test token too
107 }
108 continue
109 }
110
111 // Check if this is a test task
112 if (token === 'test' || token === 'verify' || token === 'check' || token.endsWith(':test')) {
113 return 'test'
114 }
115 }
116
117 return 'build'
118}
119
120const RULES: [BashCategory, RegExp][] = [
121 ['test', /^(jest|vitest|mocha|pytest|tox|nox|rspec|phpunit|ctest|nosetests|bats|karma|ava|jasmine)\b/],
122 ['test', /^cypress\s+run\b/],
123 ['test', /^playwright\s+test\b/],
124 ['test', /^(npm|bun)\s+(run\s+)?test\b/],
125 ['test', /^npm\s+t\b/],
126 ['test', /^(pnpm|yarn)\s+((-r|--filter\s+\S+)\s+)?(run\s+)?test\b/],
127 ['test', /^(go|cargo|deno|dotnet|mix|swift)\s+test\b/],
128 ['test', /^cargo\s+nextest\s+run\b/],
129 ['test', /^node\s+--test\b/],
130 ['test', /^(python|python3)\s+-m\s+(unittest|pytest)\b/],
131 ['test', /^make\s+(test|check)\b/],
132 ['test', /^claude\s+plugin\s+test\b/],
133 ['install', /^(npm|pnpm|yarn|bun)\s+(install|i|add|ci)\b/],
134 ['install', /^yarn\s*$/],
135 ['install', /^(pip|pip3)\s+install\b/],
136 ['install', /^uv\s+(pip|sync|add)\b/],
137 ['install', /^poetry\s+install\b/],
138 ['install', /^brew\s+install\b/],
139 ['install', /^(apt|apt-get)\s+install\b/],
140 ['install', /^cargo\s+(add|install|fetch)\b/],
141 ['install', /^go\s+(get|mod\s+download)\b/],
142 ['install', /^bundle\s+install\b/],
143 ['build', /^(tsc|webpack|esbuild|rollup|make|cmake|ninja|bazel)\b/],
144 ['build', /^(npm|pnpm|yarn|bun)\s+(run\s+)?build\b/],
145 ['build', /^vite\s+build\b/],
146 ['build', /^(cargo|go|swift|dotnet)\s+build\b/],
147 ['build', /^docker\s+build\b/],
148 ['git', /^(git|gh)\b/],
149 ['search', /^(rg|grep|ag|ack|find|fd|ls|tree|cat|head|tail|wc)\b/],
150]
151
152const RANK: BashCategory[] = ['test', 'build', 'install', 'git', 'search', 'other']
153
154function classifySegment(segment: string): BashCategory {
155 const normalized = normalise(segment)
156
157 // Special handling for gradle/mvn commands
158 const cmd = normalized.replace(/^\.\//, '').split(/\s+/)[0]
159 if (cmd === 'gradle' || cmd === 'gradlew' || cmd === 'mvn') {
160 return classifyGradleMvn(normalized)
161 }
162
163 return RULES.find(([, pattern]) => pattern.test(normalized))?.[0] ?? 'other'
164}
165
166export function classifyCommand(command: string): BashCategory {
167 if (typeof command !== 'string') return 'other'
168
169 const blanked = blankOutStringLiterals(command)
170 const segments = blanked
171 .split(/&&|\|\||;|\||\n|[&](?![&])/)
172 .map(s => s.trim())
173 .filter(Boolean)
174
175 let best: BashCategory = 'other'
176 for (const segment of segments) {
177 const category = classifySegment(segment)
178 if (RANK.indexOf(category) < RANK.indexOf(best)) best = category
179 }
180 return best
181}
182hooks/core/format.ts 28 lines1export function fmtMs(ms: number): string {
2 if (!Number.isFinite(ms) || ms < 0) return '-'
3 if (ms < 1_000) return `${Math.round(ms)}ms`
4 if (ms < 60_000) return `${(ms / 1_000).toFixed(1)}s`
5 const minutes = Math.floor(ms / 60_000)
6 const seconds = Math.floor((ms % 60_000) / 1_000)
7 return `${minutes}m${String(seconds).padStart(2, '0')}s`
8}
9
10export function pct(part: number, whole: number): string {
11 // part * 100 first: (6050 / 10000) * 100 is 60.49999… in floating point.
12 return whole > 0 ? `${Math.round((part * 100) / whole)}%` : '0%'
13}
14
15export function clip(text: string, n: number): string {
16 const line = text.replace(/[\r\n\t]+/g, ' ')
17 return line.length <= n ? line : `${line.slice(0, Math.max(0, n - 1))}…`
18}
19
20export function bar(share: number, width = 10): string {
21 const filled = Math.max(0, Math.min(width, Math.round(share * width)))
22 return '█'.repeat(filled) + '░'.repeat(width - filled)
23}
24
25export function fmtCount(n: number): string {
26 return n >= 1_000 ? `${(n / 1_000).toFixed(1)}k` : String(n)
27}
28hooks/core/types.ts 77 lines1export type Usage = {
2 input_tokens: number
3 output_tokens: number
4 cache_read_input_tokens: number
5 cache_creation_input_tokens: number
6 model?: string
7}
8
9export type ToolInputSummary = {
10 command?: string
11 description?: string
12 path?: string
13 range?: string
14 pattern?: string
15 subagentType?: string
16}
17
18export type TurnRec = { id: string; ctx: string; start: number; end?: number; reason?: string; prompt: string }
19
20export type StepRec = {
21 id: string
22 ctx: string
23 turnId: string
24 index: number
25 model: string
26 effort?: string
27 start: number
28 firstContent?: number
29 thinkFirst?: number
30 thinkLast?: number
31 outFirst?: number
32 end?: number
33 usage?: Usage
34 stopReason?: string
35}
36
37export type PermissionObservation = {
38 decision: 'accept' | 'reject'
39 source: 'config' | 'hook' | 'user_permanent' | 'user_temporary' | 'user_abort' | 'user_reject' | 'unknown'
40 at: number
41 classifier?: 'allow' | 'block' | 'no_verdict' | 'unavailable'
42}
43
44export type ToolRec = {
45 id: string
46 ctx: string
47 turnId?: string
48 tool: string
49 input: ToolInputSummary
50 start: number
51 end?: number
52 execMs?: number
53 permission?: PermissionObservation
54 isError?: boolean
55 denied?: boolean
56 launchedAgentId?: string
57 launchedAsync?: boolean
58}
59
60export type AgentRec = { id: string; agentType: string; start: number; end?: number; parentToolId?: string }
61
62export type Snapshot = {
63 sessionId: string
64 now: number
65 turns: TurnRec[]
66 steps: StepRec[]
67 tools: ToolRec[]
68 agents: AgentRec[]
69 // Earliest start ever recorded, including spans dropped since.
70 started?: number
71 // Whole main turns dropped past the span cap, and the requests, tools and agents that went with them.
72 droppedTurns: number
73 droppedSpans: number
74}
75
76export type Outcome = { denied: boolean; isError: boolean; launchedAgentId?: string; launchedAsync?: boolean }
77hooks/core/ids.ts 25 lines1const FNV_OFFSET = 0xcbf29ce484222325n
2const FNV_PRIME = 0x100000001b3n
3const MASK = 0xffffffffffffffffn
4
5export function fnv1a64(text: string): string {
6 let hash = FNV_OFFSET
7 for (const byte of new TextEncoder().encode(text)) {
8 hash ^= BigInt(byte)
9 hash = (hash * FNV_PRIME) & MASK
10 }
11 return hash.toString(16).padStart(16, '0')
12}
13
14// OTLP rejects an all-zero span id.
15export function spanId(kind: string, recordId: string): string {
16 const id = fnv1a64(`${kind}:${recordId}`)
17 return id === '0000000000000000' ? '0000000000000001' : id
18}
19
20export function traceIdFromSession(sessionId: string): string {
21 const hex = sessionId.replace(/-/g, '').toLowerCase()
22 if (/^[0-9a-f]{32}$/.test(hex) && !/^0+$/.test(hex)) return hex
23 return fnv1a64(`trace:${sessionId}`) + fnv1a64(`trace2:${sessionId}`)
24}
25