MCPcopy Create free account
hub / github.com/Noumena-Network/code / logHeadlessProfilerTurn

Function logHeadlessProfilerTurn

src/utils/headlessProfiler.ts:104–179  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

102 * Call this at the end of each turn (before processing next user message).
103 */
104export function logHeadlessProfilerTurn(): void {
105 // Only log in headless mode
106 if (!getIsNonInteractiveSession()) return
107 // Only log if enabled
108 if (!SHOULD_PROFILE) return
109
110 const perf = getPerformance()
111 const allMarks = perf.getEntriesByType('mark')
112
113 // Filter to only our headless marks
114 const marks = allMarks.filter(mark => mark.name.startsWith(MARK_PREFIX))
115 if (marks.length === 0) return
116
117 // Build checkpoint lookup (strip prefix for easier access)
118 const checkpointTimes = new Map<string, number>()
119 for (const mark of marks) {
120 const name = mark.name.slice(MARK_PREFIX.length)
121 checkpointTimes.set(name, mark.startTime)
122 }
123
124 const turnStart = checkpointTimes.get('turn_start')
125 if (turnStart === undefined) return
126
127 // Compute phase durations relative to turn_start
128 const metadata: Record<string, number | string | undefined> = {
129 turn_number: currentTurnNumber,
130 }
131
132 // Time to system message from process start (only meaningful for turn 0)
133 // Use absolute time since perf_hooks startTime is relative to process start
134 const systemMessageTime = checkpointTimes.get('system_message_yielded')
135 if (systemMessageTime !== undefined && currentTurnNumber === 0) {
136 metadata.time_to_system_message_ms = Math.round(systemMessageTime)
137 }
138
139 // Time to query start
140 const queryStartTime = checkpointTimes.get('query_started')
141 if (queryStartTime !== undefined) {
142 metadata.time_to_query_start_ms = Math.round(queryStartTime - turnStart)
143 }
144
145 // Time to first response (first chunk from API)
146 const firstChunkTime = checkpointTimes.get('first_chunk')
147 if (firstChunkTime !== undefined) {
148 metadata.time_to_first_response_ms = Math.round(firstChunkTime - turnStart)
149 }
150
151 // Query overhead (time between query start and API request sent)
152 const apiRequestTime = checkpointTimes.get('api_request_sent')
153 if (queryStartTime !== undefined && apiRequestTime !== undefined) {
154 metadata.query_overhead_ms = Math.round(apiRequestTime - queryStartTime)
155 }
156
157 // Add checkpoint count for debugging
158 metadata.checkpoint_count = marks.length
159
160 // Add entrypoint for segmentation (sdk-ts, sdk-py, sdk-cli, or undefined)
161 if (process.env.CLAUDE_CODE_ENTRYPOINT) {

Callers 2

runHeadlessFunction · 0.85
drainCommandQueueFunction · 0.85

Calls 7

getPerformanceFunction · 0.85
jsonStringifyFunction · 0.85
setMethod · 0.80
logForDebuggingFunction · 0.70
logEventFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected