@@ -3,6 +3,11 @@ import type { Execution } from '@bitcode/execution-generics/Execution';
33import { log , writePromptIO , writeStepTraceJSON , writeRawLLMIO } from '@bitcode/logger' ;
44import { DIAG_ENABLED , DIAG_TRACES , DIAG_FULL_TRACES , DIAG_FULL_PROMPTS , DIAG_TRACE_MAX , DIAG_WRITE_PROMPT_IO , DIAG_WRITE_STEP_TRACES } from './config' ;
55import { collectExecutionTrace , summarizeExecutionTrace } from './trace' ;
6+ import {
7+ writeLlmCallDebug ,
8+ shouldHardStopAfterLlmCall ,
9+ getLlmCallDebugRunDir ,
10+ } from './llm-call-debug' ;
611
712function getCtx ( execution : Execution , sequence ?: string ) {
813 const phase = ( execution as any ) . findUp ?.( 'phase' , 'current' ) ;
@@ -83,6 +88,36 @@ export async function logLLMSubstepStart(
8388 content : { systemPrompt, userPrompt, combinedPrompt, ...ctx } ,
8489 } ) . catch ( ( ) => { } ) ;
8590
91+ // Monorepo .tmp ledger (call-by-call pipeline debug).
92+ try {
93+ const f = writeLlmCallDebug ( {
94+ kind : 'request' ,
95+ sequence,
96+ phase : ctx . phase != null ? String ( ctx . phase ) : undefined ,
97+ agentName : ctx . agentName != null ? String ( ctx . agentName ) : undefined ,
98+ step : ctx . step != null ? String ( ctx . step ) : undefined ,
99+ failsafe : ctx . failsafe != null ? String ( ctx . failsafe ) : undefined ,
100+ path : Array . isArray ( ctx . path ) ? ctx . path . map ( String ) : undefined ,
101+ correlationId : ctx . correlationId != null ? String ( ctx . correlationId ) : undefined ,
102+ provider : ( llmConfig as any ) ?. provider ,
103+ model : llmConfig ?. model ,
104+ systemPrompt,
105+ userPrompt,
106+ combinedPrompt,
107+ } ) ;
108+ if ( f ) {
109+ log ( '[llm-call-debug] request written' , 'info' , {
110+ file : f ,
111+ dir : getLlmCallDebugRunDir ( String ( ctx . correlationId || '' ) ) ,
112+ agent : ctx . agentName ,
113+ step : ctx . step ,
114+ failsafe : ctx . failsafe ,
115+ generation : sequence ,
116+ model : llmConfig ?. model ,
117+ } ) ;
118+ }
119+ } catch { /* never break the pipeline for debug I/O */ }
120+
86121 if ( ! ( DIAG_ENABLED || DIAG_FULL_PROMPTS || DIAG_TRACES ) ) return ;
87122 log ( '[llm substep] start' , 'debug' , {
88123 ...ctx ,
@@ -121,6 +156,34 @@ export async function logLLMSubstepSuccess(
121156 content : { content : output ?. content , usage : output ?. usage , metadata : output ?. metadata } ,
122157 } ) . catch ( ( ) => { } ) ;
123158
159+ try {
160+ const f = writeLlmCallDebug ( {
161+ kind : 'response' ,
162+ sequence,
163+ phase : ctx . phase != null ? String ( ctx . phase ) : undefined ,
164+ agentName : ctx . agentName != null ? String ( ctx . agentName ) : undefined ,
165+ step : ctx . step != null ? String ( ctx . step ) : undefined ,
166+ failsafe : ctx . failsafe != null ? String ( ctx . failsafe ) : undefined ,
167+ path : Array . isArray ( ctx . path ) ? ctx . path . map ( String ) : undefined ,
168+ correlationId : ctx . correlationId != null ? String ( ctx . correlationId ) : undefined ,
169+ provider : output ?. metadata ?. provider ,
170+ model : output ?. metadata ?. model ,
171+ content : output ?. content ,
172+ usage : output ?. usage ,
173+ } ) ;
174+ if ( f ) {
175+ log ( '[llm-call-debug] response written' , 'info' , {
176+ file : f ,
177+ agent : ctx . agentName ,
178+ step : ctx . step ,
179+ failsafe : ctx . failsafe ,
180+ generation : sequence ,
181+ model : output ?. metadata ?. model ,
182+ outputLen : String ( output ?. content || '' ) . length ,
183+ } ) ;
184+ }
185+ } catch { /* ignore */ }
186+
124187 if ( DIAG_ENABLED || DIAG_FULL_PROMPTS || DIAG_TRACES ) {
125188 log ( '[llm substep] success' , 'debug' , {
126189 ...ctx ,
@@ -281,18 +344,37 @@ export function logStepTrace(stepExec: Execution, stepName: string) {
281344// Optional debug-stop helpers to keep core code minimal
282345export function shouldDebugStopAfterFirstReason ( substepExec : Execution , sequence : string ) : boolean {
283346 try {
284- if ( sequence !== 'reason' ) return false ;
285- const flag = String ( process ?. env ?. BITCODE_DEBUG_STOP_AFTER_FIRST_REASON || '' ) . toLowerCase ( ) === '1' ;
286- if ( ! flag ) return false ;
287- const pathArr = ( substepExec as any ) . getPath ?.( ) || [ ] ;
288- const isPlanStep = pathArr . includes ( 'plan' ) ;
289- const inPrepareFailsafe = pathArr . some ( ( p : string ) => String ( p ) . includes ( 'prepare_concise_context' ) ) ;
290- const isFirstGen = pathArr . includes ( 'gen-0' ) ;
291- const ctx = getCtx ( substepExec ) ;
292- const agentFilter = process ?. env ?. BITCODE_DEBUG_STOP_AGENT_FILTER ;
293- const agentMatches = agentFilter ? String ( ctx . agentName || '' ) . includes ( String ( agentFilter ) ) : true ;
294- if ( isPlanStep && inPrepareFailsafe && isFirstGen && agentMatches ) {
295- log ( '[llm substep] debug-stop' , 'info' , { ...ctx , reason : 'BITCODE_DEBUG_STOP_AFTER_FIRST_REASON' } ) ;
347+ const pathArr : string [ ] = ( substepExec as any ) . getPath ?.( ) || [ ] ;
348+ const ctx = getCtx ( substepExec , sequence ) ;
349+ const decision = shouldHardStopAfterLlmCall ( {
350+ sequence,
351+ pathArr,
352+ phase : ctx . phase != null ? String ( ctx . phase ) : undefined ,
353+ agentName : ctx . agentName != null ? String ( ctx . agentName ) : undefined ,
354+ step : ctx . step != null ? String ( ctx . step ) : undefined ,
355+ failsafe : ctx . failsafe != null ? String ( ctx . failsafe ) : undefined ,
356+ } ) ;
357+ if ( decision . stop ) {
358+ try {
359+ writeLlmCallDebug ( {
360+ kind : 'abort' ,
361+ sequence,
362+ phase : ctx . phase != null ? String ( ctx . phase ) : undefined ,
363+ agentName : ctx . agentName != null ? String ( ctx . agentName ) : undefined ,
364+ step : ctx . step != null ? String ( ctx . step ) : undefined ,
365+ failsafe : ctx . failsafe != null ? String ( ctx . failsafe ) : undefined ,
366+ path : pathArr . map ( String ) ,
367+ correlationId : ctx . correlationId != null ? String ( ctx . correlationId ) : undefined ,
368+ provider : ctx . provider != null ? String ( ctx . provider ) : undefined ,
369+ model : ctx . model != null ? String ( ctx . model ) : undefined ,
370+ extra : { reason : decision . reason } ,
371+ } ) ;
372+ } catch { /* ignore */ }
373+ log ( '[llm substep] debug-stop' , 'info' , {
374+ ...ctx ,
375+ reason : decision . reason || 'BITCODE_DEBUG_STOP_AFTER_FIRST_REASON' ,
376+ debugDir : getLlmCallDebugRunDir ( String ( ctx . correlationId || '' ) ) ,
377+ } ) ;
296378 return true ;
297379 }
298380 } catch { }
0 commit comments