@@ -694,6 +694,13 @@ const debugAgentComplete = debug("agent:complete");
694694const debugAgentRetry = debug ( "agent:retry" ) ;
695695const debugAgentFailure = debug ( "agent:failure" ) ;
696696const debugAgentClose = debug ( "agent:close" ) ;
697+ const debugAgentTools = debug ( "agent:tools" ) ;
698+ const debugAgentParse = debug ( "agent:parse" ) ;
699+ const debugLauncherStart = debug ( "launcher:start" ) ;
700+ const debugLauncherProgram = debug ( "launcher:program" ) ;
701+ const debugLauncherTypecheck = debug ( "launcher:typecheck" ) ;
702+ const debugLauncherResult = debug ( "launcher:result" ) ;
703+ const debugWorkflowEvent = debug ( "workflow:event" ) ;
697704
698705export type AgentAddonContext = {
699706 spec : NormalizedAgentSpec < any , any > ;
@@ -1453,6 +1460,7 @@ export async function launchRigProgram(programPath: string, options: LaunchOptio
14531460 const cwd = options . cwd ?? process . cwd ( ) ;
14541461 const resolvedPath = isAbsolute ( programPath ) ? programPath : resolve ( cwd , programPath ) ;
14551462
1463+ debugLauncherProgram ( { mode : "import" , program : resolvedPath , cwd, server : options . startServer === true } ) ;
14561464 configureAgent ( defaultAgentFactory ( { cwd, ...( options . startServer ? { startServer : true } : { } ) } ) ) ;
14571465 await import ( pathToFileURL ( resolvedPath ) . href ) ;
14581466}
@@ -1626,6 +1634,7 @@ async function hasEsmPackageContext(filePath: string): Promise<boolean> {
16261634}
16271635
16281636async function typecheckProgram ( programPath : string , cwd : string , displayPath = programPath ) : Promise < void > {
1637+ debugLauncherTypecheck ( { phase : "start" , program : displayPath , cwd } ) ;
16291638 const execFileAsync = promisify ( execFile ) ;
16301639 const skillTsconfigPath = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , "tsconfig.json" ) ;
16311640 const candidateTsconfigPaths = [ resolve ( cwd , "tsconfig.json" ) , skillTsconfigPath ] ;
@@ -1672,6 +1681,7 @@ async function typecheckProgram(programPath: string, cwd: string, displayPath =
16721681 env : { ...process . env , npm_config_ignore_scripts : "true" } ,
16731682 } ,
16741683 ) ;
1684+ debugLauncherTypecheck ( { phase : "passed" , program : displayPath } ) ;
16751685 } catch ( error ) {
16761686 const execError = error as NodeJS . ErrnoException & { stdout ?: string ; stderr ?: string } ;
16771687 if ( execError . code === "ENOENT" ) {
@@ -1691,6 +1701,7 @@ async function typecheckProgram(programPath: string, cwd: string, displayPath =
16911701 . replaceAll ( relativeShadowPath , displayPath ) ;
16921702 const detail = diagnostics ? `\n${ diagnostics } ` : "" ;
16931703 const hasCjsEsmMismatch = diagnostics . includes ( "TS1295" ) || diagnostics . includes ( "TS1479" ) ;
1704+ debugLauncherTypecheck ( { phase : "failed" , program : displayPath , diagnostics } ) ;
16941705 const hint = hasCjsEsmMismatch
16951706 ? "\nHint: add {\"type\":\"module\"} to a package.json in the same directory as your rig program."
16961707 : "" ;
@@ -1725,9 +1736,12 @@ async function runRootAgentFromStdin(
17251736 if ( ! rootAgent ) {
17261737 throw new Error ( "Expected program to export a root value (agent, workflow, string, or prompt builder) as default export." ) ;
17271738 }
1739+ debugLauncherProgram ( { mode : "file" , program : resolvedPath , root : rootAgent . agentName , promptLength : prompt . length } ) ;
17281740
17291741 const result = await rootAgent ( coerceStdinInput ( rootAgent , prompt ) ) ;
1730- io . stdout . write ( renderStdout ( result ) ) ;
1742+ const rendered = renderStdout ( result ) ;
1743+ debugLauncherResult ( { mode : "file" , root : rootAgent . agentName , bytes : Buffer . byteLength ( rendered ) } ) ;
1744+ io . stdout . write ( rendered ) ;
17311745}
17321746
17331747async function runProgramCodeFromStdin (
@@ -1759,12 +1773,15 @@ async function runProgramCodeFromStdin(
17591773 if ( ! rootAgent ) {
17601774 throw new Error ( "Expected program to export a root value (agent, workflow, string, or prompt builder) as default export." ) ;
17611775 }
1776+ debugLauncherProgram ( { mode : "stdin" , program : tempProgramPath , root : rootAgent . agentName , codeLength : programCode . length } ) ;
17621777 const input = noInputInvocation ( rootAgent ) ;
17631778 if ( input === undefined ) {
17641779 throw new Error ( "Expected stdin program root agent to have no input (omit input or use input: s.object({}))." ) ;
17651780 }
17661781 const result = await rootAgent ( input ) ;
1767- io . stdout . write ( renderStdout ( result ) ) ;
1782+ const rendered = renderStdout ( result ) ;
1783+ debugLauncherResult ( { mode : "stdin" , root : rootAgent . agentName , bytes : Buffer . byteLength ( rendered ) } ) ;
1784+ io . stdout . write ( rendered ) ;
17681785 } finally {
17691786 await rm ( tempDir , { recursive : true , force : true } ) ;
17701787 }
@@ -1825,6 +1842,7 @@ export async function runLauncherCli(
18251842 io : LauncherIo = process ,
18261843) : Promise < void > {
18271844 const scriptName = process . argv [ 1 ] ? basename ( process . argv [ 1 ] ) : "launcher" ;
1845+ debugLauncherStart ( { script : scriptName , argv } ) ;
18281846 if ( argv . some ( isLauncherHelpArg ) ) {
18291847 io . stdout . write ( `${ renderLauncherUsage ( scriptName ) } \n` ) ;
18301848 return ;
@@ -2230,6 +2248,7 @@ export async function runWorkflow<Input, Output>(
22302248 timer ?. unref ( ) ;
22312249
22322250 const emit = ( event : WorkflowEvent ) : void => {
2251+ debugWorkflowEvent ( ( ) => ( { workflow : definition . meta . name , event } ) ) ;
22332252 try {
22342253 options . onEvent ?.( event ) ;
22352254 } catch {
@@ -2374,6 +2393,7 @@ function normalizeToolConfig<T extends { skipPermission?: boolean }>(tool: T): T
23742393}
23752394
23762395function normalizeTools ( tools : Tool < any > [ ] , agentName : string ) : Tool < any > [ ] {
2396+ debugAgentTools ( ( ) => ( { agent : agentName , tools : tools . map ( ( tool ) => tool ?. name ) } ) ) ;
23772397 return tools . map ( ( tool , index ) => {
23782398 if ( ! tool || typeof tool !== "object" ) {
23792399 throw new Error ( `Invalid tool for agent "${ agentName } " at tools[${ index } ]. Expected a tool definition object.` ) ;
@@ -2714,6 +2734,7 @@ export type ResponseAnalysisResult = { ok: true; output: unknown } | { ok: false
27142734export function analyzeResponse ( response : string , outputSchema : Schema , agentName : string , turn : number ) : ResponseAnalysisResult {
27152735 const parsed = parseJson ( response ) ;
27162736 if ( ! parsed . ok ) {
2737+ debugAgentParse ( { agent : agentName , turn, kind : "parse" , error : parsed . error } ) ;
27172738 return {
27182739 ok : false ,
27192740 error : new AgentError ( {
@@ -2729,6 +2750,7 @@ export function analyzeResponse(response: string, outputSchema: Schema, agentNam
27292750
27302751 const result = validate ( parsed . value , outputSchema ) ;
27312752 if ( ! result . ok ) {
2753+ debugAgentParse ( { agent : agentName , turn, kind : "validation" , error : result . error } ) ;
27322754 return {
27332755 ok : false ,
27342756 error : new AgentError ( {
@@ -2742,6 +2764,7 @@ export function analyzeResponse(response: string, outputSchema: Schema, agentNam
27422764 } ;
27432765 }
27442766
2767+ debugAgentParse ( { agent : agentName , turn, kind : "ok" } ) ;
27452768 return { ok : true , output : parsed . value } ;
27462769}
27472770
0 commit comments