File size: 29,792 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 | /**
* Perfetto Tracing for Claude Code (Ant-only)
*
* This module generates traces in the Chrome Trace Event format that can be
* viewed in ui.perfetto.dev or Chrome's chrome://tracing.
*
* NOTE: This feature is ant-only and eliminated from external builds.
*
* The trace file includes:
* - Agent hierarchy (parent-child relationships in a swarm)
* - API requests with TTFT, TTLT, prompt length, cache stats, msg ID, speculative flag
* - Tool executions with name, duration, and token usage
* - User input waiting time
*
* Usage:
* 1. Enable via CLAUDE_CODE_PERFETTO_TRACE=1 or CLAUDE_CODE_PERFETTO_TRACE=<path>
* 2. Optionally set CLAUDE_CODE_PERFETTO_WRITE_INTERVAL_S=<positive integer> to write the
* trace file periodically (default: write only on exit).
* 3. Run Claude Code normally
* 4. Trace file is written to ~/.claude/traces/trace-<session-id>.json
* or to the specified path
* 5. Open in ui.perfetto.dev to visualize
*/
import { feature } from 'bun:bundle'
import { mkdirSync, writeFileSync } from 'fs'
import { mkdir, writeFile } from 'fs/promises'
import { dirname, join } from 'path'
import { getSessionId } from '../../bootstrap/state.js'
import { registerCleanup } from '../cleanupRegistry.js'
import { logForDebugging } from '../debug.js'
import {
getClaudeConfigHomeDir,
isEnvDefinedFalsy,
isEnvTruthy,
} from '../envUtils.js'
import { errorMessage } from '../errors.js'
import { djb2Hash } from '../hash.js'
import { jsonStringify } from '../slowOperations.js'
import { getAgentId, getAgentName, getParentSessionId } from '../teammate.js'
/**
* Chrome Trace Event format types
* See: https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
*/
export type TraceEventPhase =
| 'B' // Begin duration event
| 'E' // End duration event
| 'X' // Complete event (with duration)
| 'i' // Instant event
| 'C' // Counter event
| 'b' // Async begin
| 'n' // Async instant
| 'e' // Async end
| 'M' // Metadata event
export type TraceEvent = {
name: string
cat: string
ph: TraceEventPhase
ts: number // Timestamp in microseconds
pid: number // Process ID (we use 1 for main, agent IDs for subagents)
tid: number // Thread ID (we use numeric hash of agent name or 1 for main)
dur?: number // Duration in microseconds (for 'X' events)
args?: Record<string, unknown>
id?: string // For async events
scope?: string
}
/**
* Agent info for tracking hierarchy
*/
type AgentInfo = {
agentId: string
agentName: string
parentAgentId?: string
processId: number
threadId: number
}
/**
* Pending span for tracking begin/end pairs
*/
type PendingSpan = {
name: string
category: string
startTime: number
agentInfo: AgentInfo
args: Record<string, unknown>
}
// Global state for the Perfetto tracer
let isEnabled = false
let tracePath: string | null = null
// Metadata events (ph: 'M' — process/thread names, parent links) are kept
// separate so they survive eviction — Perfetto UI needs them to label
// tracks. Bounded by agent count (~3 events per agent).
const metadataEvents: TraceEvent[] = []
const events: TraceEvent[] = []
// events[] cap. Cron-driven sessions run for days; 22 push sites × many
// turns would otherwise grow unboundedly (periodicWrite flushes to disk but
// does not truncate — it writes the full snapshot). At ~300B/event this is
// ~30MB, enough trace history for any debugging session. Eviction drops the
// oldest half when hit, amortized O(1).
const MAX_EVENTS = 100_000
const pendingSpans = new Map<string, PendingSpan>()
const agentRegistry = new Map<string, AgentInfo>()
let totalAgentCount = 0
let startTimeMs = 0
let spanIdCounter = 0
let traceWritten = false // Flag to avoid double writes
// Map agent IDs to numeric process IDs (Perfetto requires numeric IDs)
let processIdCounter = 1
const agentIdToProcessId = new Map<string, number>()
// Periodic write interval handle
let writeIntervalId: ReturnType<typeof setInterval> | null = null
const STALE_SPAN_TTL_MS = 30 * 60 * 1000 // 30 minutes
const STALE_SPAN_CLEANUP_INTERVAL_MS = 60 * 1000 // 1 minute
let staleSpanCleanupId: ReturnType<typeof setInterval> | null = null
/**
* Convert a string to a numeric hash for use as thread ID
*/
function stringToNumericHash(str: string): number {
return Math.abs(djb2Hash(str)) || 1 // Ensure non-zero
}
/**
* Get or create a numeric process ID for an agent
*/
function getProcessIdForAgent(agentId: string): number {
const existing = agentIdToProcessId.get(agentId)
if (existing !== undefined) return existing
processIdCounter++
agentIdToProcessId.set(agentId, processIdCounter)
return processIdCounter
}
/**
* Get current agent info
*/
function getCurrentAgentInfo(): AgentInfo {
const agentId = getAgentId() ?? getSessionId()
const agentName = getAgentName() ?? 'main'
const parentSessionId = getParentSessionId()
// Check if we've already registered this agent
const existing = agentRegistry.get(agentId)
if (existing) return existing
const info: AgentInfo = {
agentId,
agentName,
parentAgentId: parentSessionId,
processId: agentId === getSessionId() ? 1 : getProcessIdForAgent(agentId),
threadId: stringToNumericHash(agentName),
}
agentRegistry.set(agentId, info)
totalAgentCount++
return info
}
/**
* Get timestamp in microseconds relative to trace start
*/
function getTimestamp(): number {
return (Date.now() - startTimeMs) * 1000
}
/**
* Generate a unique span ID
*/
function generateSpanId(): string {
return `span_${++spanIdCounter}`
}
/**
* Evict pending spans older than STALE_SPAN_TTL_MS.
* Mirrors the TTL cleanup pattern in sessionTracing.ts.
*/
function evictStaleSpans(): void {
const now = getTimestamp()
const ttlUs = STALE_SPAN_TTL_MS * 1000 // Convert ms to microseconds
for (const [spanId, span] of pendingSpans) {
if (now - span.startTime > ttlUs) {
// Emit an end event so the span shows up in the trace as incomplete
events.push({
name: span.name,
cat: span.category,
ph: 'E',
ts: now,
pid: span.agentInfo.processId,
tid: span.agentInfo.threadId,
args: {
...span.args,
evicted: true,
duration_ms: (now - span.startTime) / 1000,
},
})
pendingSpans.delete(spanId)
}
}
}
/**
* Build the full trace document (Chrome Trace JSON format).
*/
function buildTraceDocument(): string {
return jsonStringify({
traceEvents: [...metadataEvents, ...events],
metadata: {
session_id: getSessionId(),
trace_start_time: new Date(startTimeMs).toISOString(),
agent_count: totalAgentCount,
total_event_count: metadataEvents.length + events.length,
},
})
}
/**
* Drop the oldest half of events[] when over MAX_EVENTS. Called from the
* stale-span cleanup interval (60s). The half-batch splice keeps this
* amortized O(1) — we don't pay splice cost per-push. A synthetic marker
* is inserted so the gap is visible in ui.perfetto.dev.
*/
function evictOldestEvents(): void {
if (events.length < MAX_EVENTS) return
const dropped = events.splice(0, MAX_EVENTS / 2)
events.unshift({
name: 'trace_truncated',
cat: '__metadata',
ph: 'i',
ts: dropped[dropped.length - 1]?.ts ?? 0,
pid: 1,
tid: 0,
args: { dropped_events: dropped.length },
})
logForDebugging(
`[Perfetto] Evicted ${dropped.length} oldest events (cap ${MAX_EVENTS})`,
)
}
/**
* Initialize Perfetto tracing
* Call this early in the application lifecycle
*/
export function initializePerfettoTracing(): void {
const envValue = process.env.CLAUDE_CODE_PERFETTO_TRACE
logForDebugging(
`[Perfetto] initializePerfettoTracing called, env value: ${envValue}`,
)
// Wrap in feature() for dead code elimination - entire block removed from external builds
if (feature('PERFETTO_TRACING')) {
if (!envValue || isEnvDefinedFalsy(envValue)) {
logForDebugging(
'[Perfetto] Tracing disabled (env var not set or disabled)',
)
return
}
isEnabled = true
startTimeMs = Date.now()
// Determine trace file path
if (isEnvTruthy(envValue)) {
const tracesDir = join(getClaudeConfigHomeDir(), 'traces')
tracePath = join(tracesDir, `trace-${getSessionId()}.json`)
} else {
// Use the provided path
tracePath = envValue
}
logForDebugging(
`[Perfetto] Tracing enabled, will write to: ${tracePath}, isEnabled=${isEnabled}`,
)
// Start periodic full-trace write if CLAUDE_CODE_PERFETTO_WRITE_INTERVAL_S is a positive integer
const intervalSec = parseInt(
process.env.CLAUDE_CODE_PERFETTO_WRITE_INTERVAL_S ?? '',
10,
)
if (intervalSec > 0) {
writeIntervalId = setInterval(() => {
void periodicWrite()
}, intervalSec * 1000)
// Don't let the interval keep the process alive on its own
if (writeIntervalId.unref) writeIntervalId.unref()
logForDebugging(
`[Perfetto] Periodic write enabled, interval: ${intervalSec}s`,
)
}
// Start stale span cleanup interval
staleSpanCleanupId = setInterval(() => {
evictStaleSpans()
evictOldestEvents()
}, STALE_SPAN_CLEANUP_INTERVAL_MS)
if (staleSpanCleanupId.unref) staleSpanCleanupId.unref()
// Register cleanup to write final trace on exit
registerCleanup(async () => {
logForDebugging('[Perfetto] Cleanup callback invoked')
await writePerfettoTrace()
})
// Also register a beforeExit handler as a fallback
// This ensures the trace is written even if cleanup registry is not called
process.on('beforeExit', () => {
logForDebugging('[Perfetto] beforeExit handler invoked')
void writePerfettoTrace()
})
// Register a synchronous exit handler as a last resort
// This is the final fallback to ensure trace is written before process exits
process.on('exit', () => {
if (!traceWritten) {
logForDebugging(
'[Perfetto] exit handler invoked, writing trace synchronously',
)
writePerfettoTraceSync()
}
})
// Emit process metadata events for main process
const mainAgent = getCurrentAgentInfo()
emitProcessMetadata(mainAgent)
}
}
/**
* Emit metadata events for a process/agent
*/
function emitProcessMetadata(agentInfo: AgentInfo): void {
if (!isEnabled) return
// Process name
metadataEvents.push({
name: 'process_name',
cat: '__metadata',
ph: 'M',
ts: 0,
pid: agentInfo.processId,
tid: 0,
args: { name: agentInfo.agentName },
})
// Thread name (same as process for now)
metadataEvents.push({
name: 'thread_name',
cat: '__metadata',
ph: 'M',
ts: 0,
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: { name: agentInfo.agentName },
})
// Add parent info if available
if (agentInfo.parentAgentId) {
metadataEvents.push({
name: 'parent_agent',
cat: '__metadata',
ph: 'M',
ts: 0,
pid: agentInfo.processId,
tid: 0,
args: {
parent_agent_id: agentInfo.parentAgentId,
},
})
}
}
/**
* Check if Perfetto tracing is enabled
*/
export function isPerfettoTracingEnabled(): boolean {
return isEnabled
}
/**
* Register a new agent in the trace
* Call this when a subagent/teammate is spawned
*/
export function registerAgent(
agentId: string,
agentName: string,
parentAgentId?: string,
): void {
if (!isEnabled) return
const info: AgentInfo = {
agentId,
agentName,
parentAgentId,
processId: getProcessIdForAgent(agentId),
threadId: stringToNumericHash(agentName),
}
agentRegistry.set(agentId, info)
totalAgentCount++
emitProcessMetadata(info)
}
/**
* Unregister an agent from the trace.
* Call this when an agent completes, fails, or is aborted to free memory.
*/
export function unregisterAgent(agentId: string): void {
if (!isEnabled) return
agentRegistry.delete(agentId)
agentIdToProcessId.delete(agentId)
}
/**
* Start an API call span
*/
export function startLLMRequestPerfettoSpan(args: {
model: string
promptTokens?: number
messageId?: string
isSpeculative?: boolean
querySource?: string
}): string {
if (!isEnabled) return ''
const spanId = generateSpanId()
const agentInfo = getCurrentAgentInfo()
pendingSpans.set(spanId, {
name: 'API Call',
category: 'api',
startTime: getTimestamp(),
agentInfo,
args: {
model: args.model,
prompt_tokens: args.promptTokens,
message_id: args.messageId,
is_speculative: args.isSpeculative ?? false,
query_source: args.querySource,
},
})
// Emit begin event
events.push({
name: 'API Call',
cat: 'api',
ph: 'B',
ts: pendingSpans.get(spanId)!.startTime,
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: pendingSpans.get(spanId)!.args,
})
return spanId
}
/**
* End an API call span with response metadata
*/
export function endLLMRequestPerfettoSpan(
spanId: string,
metadata: {
ttftMs?: number
ttltMs?: number
promptTokens?: number
outputTokens?: number
cacheReadTokens?: number
cacheCreationTokens?: number
messageId?: string
success?: boolean
error?: string
/** Time spent in pre-request setup (client creation, retries) before the successful attempt */
requestSetupMs?: number
/** Timestamps (Date.now()) of each attempt start — used to emit retry sub-spans */
attemptStartTimes?: number[]
},
): void {
if (!isEnabled || !spanId) return
const pending = pendingSpans.get(spanId)
if (!pending) return
const endTime = getTimestamp()
const duration = endTime - pending.startTime
const promptTokens =
metadata.promptTokens ?? (pending.args.prompt_tokens as number | undefined)
const ttftMs = metadata.ttftMs
const ttltMs = metadata.ttltMs
const outputTokens = metadata.outputTokens
const cacheReadTokens = metadata.cacheReadTokens
// Compute derived metrics
// ITPS: input tokens per second (prompt processing speed)
const itps =
ttftMs !== undefined && promptTokens !== undefined && ttftMs > 0
? Math.round((promptTokens / (ttftMs / 1000)) * 100) / 100
: undefined
// OTPS: output tokens per second (sampling speed)
const samplingMs =
ttltMs !== undefined && ttftMs !== undefined ? ttltMs - ttftMs : undefined
const otps =
samplingMs !== undefined && outputTokens !== undefined && samplingMs > 0
? Math.round((outputTokens / (samplingMs / 1000)) * 100) / 100
: undefined
// Cache hit rate: percentage of prompt tokens from cache
const cacheHitRate =
cacheReadTokens !== undefined &&
promptTokens !== undefined &&
promptTokens > 0
? Math.round((cacheReadTokens / promptTokens) * 10000) / 100
: undefined
const requestSetupMs = metadata.requestSetupMs
const attemptStartTimes = metadata.attemptStartTimes
// Merge metadata with original args
const args = {
...pending.args,
ttft_ms: ttftMs,
ttlt_ms: ttltMs,
prompt_tokens: promptTokens,
output_tokens: outputTokens,
cache_read_tokens: cacheReadTokens,
cache_creation_tokens: metadata.cacheCreationTokens,
message_id: metadata.messageId ?? pending.args.message_id,
success: metadata.success ?? true,
error: metadata.error,
duration_ms: duration / 1000,
request_setup_ms: requestSetupMs,
// Derived metrics
itps,
otps,
cache_hit_rate_pct: cacheHitRate,
}
// Emit Request Setup sub-span when there was measurable setup time
// (client creation, param building, retries before the successful attempt)
const setupUs =
requestSetupMs !== undefined && requestSetupMs > 0
? requestSetupMs * 1000
: 0
if (setupUs > 0) {
const setupEndTs = pending.startTime + setupUs
events.push({
name: 'Request Setup',
cat: 'api,setup',
ph: 'B',
ts: pending.startTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: {
request_setup_ms: requestSetupMs,
attempt_count: attemptStartTimes?.length ?? 1,
},
})
// Emit retry attempt sub-spans within Request Setup.
// Each failed attempt runs from its start to the next attempt's start.
if (attemptStartTimes && attemptStartTimes.length > 1) {
// attemptStartTimes[0] is the reference point (first attempt).
// Convert wall-clock deltas into Perfetto-relative microseconds.
const baseWallMs = attemptStartTimes[0]!
for (let i = 0; i < attemptStartTimes.length - 1; i++) {
const attemptStartUs =
pending.startTime + (attemptStartTimes[i]! - baseWallMs) * 1000
const attemptEndUs =
pending.startTime + (attemptStartTimes[i + 1]! - baseWallMs) * 1000
events.push({
name: `Attempt ${i + 1} (retry)`,
cat: 'api,retry',
ph: 'B',
ts: attemptStartUs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: { attempt: i + 1 },
})
events.push({
name: `Attempt ${i + 1} (retry)`,
cat: 'api,retry',
ph: 'E',
ts: attemptEndUs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
})
}
}
events.push({
name: 'Request Setup',
cat: 'api,setup',
ph: 'E',
ts: setupEndTs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
})
}
// Emit sub-spans for First Token and Sampling phases (before API Call end)
// Using B/E pairs in proper nesting order for correct Perfetto visualization
if (ttftMs !== undefined) {
// First Token starts after request setup (if any)
const firstTokenStartTs = pending.startTime + setupUs
const firstTokenEndTs = firstTokenStartTs + ttftMs * 1000
// First Token phase: from successful attempt start to first token
events.push({
name: 'First Token',
cat: 'api,ttft',
ph: 'B',
ts: firstTokenStartTs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: {
ttft_ms: ttftMs,
prompt_tokens: promptTokens,
itps,
cache_hit_rate_pct: cacheHitRate,
},
})
events.push({
name: 'First Token',
cat: 'api,ttft',
ph: 'E',
ts: firstTokenEndTs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
})
// Sampling phase: from first token to last token
// Note: samplingMs = ttltMs - ttftMs still includes setup time in ttltMs,
// so we compute the actual sampling duration for the span as the time from
// first token to API call end (endTime), not samplingMs directly.
const actualSamplingMs =
ttltMs !== undefined ? ttltMs - ttftMs - setupUs / 1000 : undefined
if (actualSamplingMs !== undefined && actualSamplingMs > 0) {
events.push({
name: 'Sampling',
cat: 'api,sampling',
ph: 'B',
ts: firstTokenEndTs,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: {
sampling_ms: actualSamplingMs,
output_tokens: outputTokens,
otps,
},
})
events.push({
name: 'Sampling',
cat: 'api,sampling',
ph: 'E',
ts: firstTokenEndTs + actualSamplingMs * 1000,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
})
}
}
// Emit API Call end event (after sub-spans)
events.push({
name: pending.name,
cat: pending.category,
ph: 'E',
ts: endTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args,
})
pendingSpans.delete(spanId)
}
/**
* Start a tool execution span
*/
export function startToolPerfettoSpan(
toolName: string,
args?: Record<string, unknown>,
): string {
if (!isEnabled) return ''
const spanId = generateSpanId()
const agentInfo = getCurrentAgentInfo()
pendingSpans.set(spanId, {
name: `Tool: ${toolName}`,
category: 'tool',
startTime: getTimestamp(),
agentInfo,
args: {
tool_name: toolName,
...args,
},
})
// Emit begin event
events.push({
name: `Tool: ${toolName}`,
cat: 'tool',
ph: 'B',
ts: pendingSpans.get(spanId)!.startTime,
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: pendingSpans.get(spanId)!.args,
})
return spanId
}
/**
* End a tool execution span
*/
export function endToolPerfettoSpan(
spanId: string,
metadata?: {
success?: boolean
error?: string
resultTokens?: number
},
): void {
if (!isEnabled || !spanId) return
const pending = pendingSpans.get(spanId)
if (!pending) return
const endTime = getTimestamp()
const duration = endTime - pending.startTime
const args = {
...pending.args,
success: metadata?.success ?? true,
error: metadata?.error,
result_tokens: metadata?.resultTokens,
duration_ms: duration / 1000,
}
// Emit end event
events.push({
name: pending.name,
cat: pending.category,
ph: 'E',
ts: endTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args,
})
pendingSpans.delete(spanId)
}
/**
* Start a user input waiting span
*/
export function startUserInputPerfettoSpan(context?: string): string {
if (!isEnabled) return ''
const spanId = generateSpanId()
const agentInfo = getCurrentAgentInfo()
pendingSpans.set(spanId, {
name: 'Waiting for User Input',
category: 'user_input',
startTime: getTimestamp(),
agentInfo,
args: {
context,
},
})
// Emit begin event
events.push({
name: 'Waiting for User Input',
cat: 'user_input',
ph: 'B',
ts: pendingSpans.get(spanId)!.startTime,
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: pendingSpans.get(spanId)!.args,
})
return spanId
}
/**
* End a user input waiting span
*/
export function endUserInputPerfettoSpan(
spanId: string,
metadata?: {
decision?: string
source?: string
},
): void {
if (!isEnabled || !spanId) return
const pending = pendingSpans.get(spanId)
if (!pending) return
const endTime = getTimestamp()
const duration = endTime - pending.startTime
const args = {
...pending.args,
decision: metadata?.decision,
source: metadata?.source,
duration_ms: duration / 1000,
}
// Emit end event
events.push({
name: pending.name,
cat: pending.category,
ph: 'E',
ts: endTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args,
})
pendingSpans.delete(spanId)
}
/**
* Emit an instant event (marker)
*/
export function emitPerfettoInstant(
name: string,
category: string,
args?: Record<string, unknown>,
): void {
if (!isEnabled) return
const agentInfo = getCurrentAgentInfo()
events.push({
name,
cat: category,
ph: 'i',
ts: getTimestamp(),
pid: agentInfo.processId,
tid: agentInfo.threadId,
args,
})
}
/**
* Emit a counter event for tracking metrics over time
*/
export function emitPerfettoCounter(
name: string,
values: Record<string, number>,
): void {
if (!isEnabled) return
const agentInfo = getCurrentAgentInfo()
events.push({
name,
cat: 'counter',
ph: 'C',
ts: getTimestamp(),
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: values,
})
}
/**
* Start an interaction span (wraps a full user request cycle)
*/
export function startInteractionPerfettoSpan(userPrompt?: string): string {
if (!isEnabled) return ''
const spanId = generateSpanId()
const agentInfo = getCurrentAgentInfo()
pendingSpans.set(spanId, {
name: 'Interaction',
category: 'interaction',
startTime: getTimestamp(),
agentInfo,
args: {
user_prompt_length: userPrompt?.length,
},
})
// Emit begin event
events.push({
name: 'Interaction',
cat: 'interaction',
ph: 'B',
ts: pendingSpans.get(spanId)!.startTime,
pid: agentInfo.processId,
tid: agentInfo.threadId,
args: pendingSpans.get(spanId)!.args,
})
return spanId
}
/**
* End an interaction span
*/
export function endInteractionPerfettoSpan(spanId: string): void {
if (!isEnabled || !spanId) return
const pending = pendingSpans.get(spanId)
if (!pending) return
const endTime = getTimestamp()
const duration = endTime - pending.startTime
// Emit end event
events.push({
name: pending.name,
cat: pending.category,
ph: 'E',
ts: endTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: {
...pending.args,
duration_ms: duration / 1000,
},
})
pendingSpans.delete(spanId)
}
// ---------------------------------------------------------------------------
// Periodic write helpers
// ---------------------------------------------------------------------------
/**
* Stop the periodic write timer.
*/
function stopWriteInterval(): void {
if (staleSpanCleanupId) {
clearInterval(staleSpanCleanupId)
staleSpanCleanupId = null
}
if (writeIntervalId) {
clearInterval(writeIntervalId)
writeIntervalId = null
}
}
/**
* Force-close any remaining open spans at session end.
*/
function closeOpenSpans(): void {
for (const [spanId, pending] of pendingSpans) {
const endTime = getTimestamp()
events.push({
name: pending.name,
cat: pending.category,
ph: 'E',
ts: endTime,
pid: pending.agentInfo.processId,
tid: pending.agentInfo.threadId,
args: {
...pending.args,
incomplete: true,
duration_ms: (endTime - pending.startTime) / 1000,
},
})
pendingSpans.delete(spanId)
}
}
/**
* Write the full trace to disk. Errors are logged but swallowed so that a
* transient I/O problem does not crash the session — the next periodic tick
* (or the final exit write) will retry with a complete snapshot.
*/
async function periodicWrite(): Promise<void> {
if (!isEnabled || !tracePath || traceWritten) return
try {
await mkdir(dirname(tracePath), { recursive: true })
await writeFile(tracePath, buildTraceDocument())
logForDebugging(
`[Perfetto] Periodic write: ${events.length} events to ${tracePath}`,
)
} catch (error) {
logForDebugging(
`[Perfetto] Periodic write failed: ${errorMessage(error)}`,
{ level: 'error' },
)
}
}
/**
* Final async write: close open spans and write the complete trace.
* Idempotent — sets `traceWritten` on success so subsequent calls are no-ops.
*/
async function writePerfettoTrace(): Promise<void> {
if (!isEnabled || !tracePath || traceWritten) {
logForDebugging(
`[Perfetto] Skipping final write: isEnabled=${isEnabled}, tracePath=${tracePath}, traceWritten=${traceWritten}`,
)
return
}
stopWriteInterval()
closeOpenSpans()
logForDebugging(
`[Perfetto] writePerfettoTrace called: events=${events.length}`,
)
try {
await mkdir(dirname(tracePath), { recursive: true })
await writeFile(tracePath, buildTraceDocument())
traceWritten = true
logForDebugging(`[Perfetto] Trace finalized at: ${tracePath}`)
} catch (error) {
logForDebugging(
`[Perfetto] Failed to write final trace: ${errorMessage(error)}`,
{ level: 'error' },
)
}
}
/**
* Final synchronous write (fallback for process 'exit' handler where async is forbidden).
*/
function writePerfettoTraceSync(): void {
if (!isEnabled || !tracePath || traceWritten) {
logForDebugging(
`[Perfetto] Skipping final sync write: isEnabled=${isEnabled}, tracePath=${tracePath}, traceWritten=${traceWritten}`,
)
return
}
stopWriteInterval()
closeOpenSpans()
logForDebugging(
`[Perfetto] writePerfettoTraceSync called: events=${events.length}`,
)
try {
const dir = dirname(tracePath)
// eslint-disable-next-line custom-rules/no-sync-fs -- Only called from process.on('exit') handler
mkdirSync(dir, { recursive: true })
// eslint-disable-next-line custom-rules/no-sync-fs, eslint-plugin-n/no-sync -- Required for process 'exit' handler which doesn't support async
writeFileSync(tracePath, buildTraceDocument())
traceWritten = true
logForDebugging(`[Perfetto] Trace finalized synchronously at: ${tracePath}`)
} catch (error) {
logForDebugging(
`[Perfetto] Failed to write final trace synchronously: ${errorMessage(error)}`,
{ level: 'error' },
)
}
}
/**
* Get all recorded events (for testing)
*/
export function getPerfettoEvents(): TraceEvent[] {
return [...metadataEvents, ...events]
}
/**
* Reset the tracer state (for testing)
*/
export function resetPerfettoTracer(): void {
if (staleSpanCleanupId) {
clearInterval(staleSpanCleanupId)
staleSpanCleanupId = null
}
stopWriteInterval()
metadataEvents.length = 0
events.length = 0
pendingSpans.clear()
agentRegistry.clear()
agentIdToProcessId.clear()
totalAgentCount = 0
processIdCounter = 1
spanIdCounter = 0
isEnabled = false
tracePath = null
startTimeMs = 0
traceWritten = false
}
/**
* Trigger a periodic write immediately (for testing)
*/
export async function triggerPeriodicWriteForTesting(): Promise<void> {
await periodicWrite()
}
/**
* Evict stale spans immediately (for testing)
*/
export function evictStaleSpansForTesting(): void {
evictStaleSpans()
}
export const MAX_EVENTS_FOR_TESTING = MAX_EVENTS
export function evictOldestEventsForTesting(): void {
evictOldestEvents()
}
|