@@ -257,8 +257,11 @@ const ROLLOUT_TOOL_ITEMS = new Set([
257257 * rollout, which the `--json` stream lacks. The two streams list tool items and
258258 * assistant messages in the same order but under different ids, so they're
259259 * paired by position. Tool pairs must also agree on the command or MCP tool,
260- * and any count or content mismatch leaves that kind of event untouched.
261- * A request no event came from, like a compaction call, gets an empty message.
260+ * and any count or content mismatch leaves those messages or tool calls
261+ * untouched. A request no event came from, like a compaction call, gets an
262+ * empty message. If the messages or tool calls didn't pair, only a request with
263+ * no `response_item` records gets one, since the others may just be missing
264+ * their requestId.
262265 *
263266 * rollout: reasoning → message → function_call(c1) → token_usage_record(r1)
264267 * → function_call_output(c1)
@@ -271,6 +274,8 @@ export function enrichFromRollout(
271274 interface Request {
272275 id ?: string ;
273276 usage ?: RequestUsage ;
277+ /** No response item before its usage record, like a compaction call. */
278+ itemless ?: boolean ;
274279 }
275280 const callStarts = new Map < string , { at ?: string ; request : Request } > ( ) ;
276281 const callEnds = new Map < string , string | undefined > ( ) ;
@@ -283,7 +288,7 @@ export function enrichFromRollout(
283288 const at = str ( record . timestamp ) ;
284289 const payload = isRecord ( record . payload ) ? record . payload : { } ;
285290 if ( record . type === 'token_usage_record' ) {
286- const request = open ?? { } ;
291+ const request = open ?? { itemless : true } ;
287292 request . id = str ( payload . response_id ) ;
288293 request . usage = rolloutUsage ( payload . usage ) ;
289294 requests . push ( { at, request } ) ;
@@ -320,37 +325,39 @@ export function enrichFromRollout(
320325 const assistant = events . filter (
321326 ( e ) => e . type === 'message' && e . role === 'assistant'
322327 ) ;
323- if ( assistant . length === messages . length ) {
328+ const messagesPaired = assistant . length === messages . length ;
329+ if ( messagesPaired ) {
324330 assistant . forEach ( ( event , i ) => {
325331 event . timestamp = messages [ i ] . at ?? event . timestamp ;
326332 tag ( event , messages [ i ] . request ) ;
327333 } ) ;
328334 }
329335 const calls = events . filter ( ( e ) => e . type === 'tool_call' ) ;
330- if (
331- calls . length !== toolItems . length ||
332- calls . some ( ( event , i ) => ! sameCall ( event , toolItems [ i ] ) )
333- ) {
334- return ;
335- }
336- const callIdByItem = new Map < string , string > ( ) ;
337- calls . forEach ( ( event , i ) => {
338- const itemId = String ( toolItems [ i ] . id ) ;
339- const start = callStarts . get ( itemId ) ;
340- if ( event . tool ?. id ) callIdByItem . set ( event . tool . id , itemId ) ;
341- if ( ! start ) return ;
342- event . timestamp = start . at ?? event . timestamp ;
343- tag ( event , start . request ) ;
344- } ) ;
345- for ( const event of events ) {
346- if ( event . type !== 'tool_result' || ! event . tool ?. id ) continue ;
347- const callId = callIdByItem . get ( event . tool . id ) ;
348- const end = callId ? callEnds . get ( callId ) : undefined ;
349- if ( end ) event . timestamp = end ;
336+ const toolsPaired =
337+ calls . length === toolItems . length &&
338+ calls . every ( ( event , i ) => sameCall ( event , toolItems [ i ] ) ) ;
339+ if ( toolsPaired ) {
340+ const callIdByItem = new Map < string , string > ( ) ;
341+ calls . forEach ( ( event , i ) => {
342+ const itemId = String ( toolItems [ i ] . id ) ;
343+ const start = callStarts . get ( itemId ) ;
344+ if ( event . tool ?. id ) callIdByItem . set ( event . tool . id , itemId ) ;
345+ if ( ! start ) return ;
346+ event . timestamp = start . at ?? event . timestamp ;
347+ tag ( event , start . request ) ;
348+ } ) ;
349+ for ( const event of events ) {
350+ if ( event . type !== 'tool_result' || ! event . tool ?. id ) continue ;
351+ const callId = callIdByItem . get ( event . tool . id ) ;
352+ const end = callId ? callEnds . get ( callId ) : undefined ;
353+ if ( end ) event . timestamp = end ;
354+ }
350355 }
351356 const tagged = new Set ( events . map ( ( e ) => e . requestId ) ) ;
352357 for ( const { at, request } of requests ) {
353358 if ( ! request . id || tagged . has ( request . id ) ) continue ;
359+ // Unpaired events have no requestId, so their requests only look silent.
360+ if ( ! ( messagesPaired && toolsPaired ) && ! request . itemless ) continue ;
354361 const silent : TranscriptEvent = {
355362 type : 'message' ,
356363 role : 'assistant' ,
0 commit comments