@@ -452,6 +452,15 @@ export function registerHomeHandlers(): void {
452452 `caps=[${ mentions . capabilities . map ( ( c ) => c . name ) . join ( ',' ) } ] skills=[${ mentions . skills . map ( ( s ) => s . name ) . join ( ',' ) } ] ` +
453453 `→ ${ focusCap ? 'focusCap(主Agent介绍/组队)' : directAgent ? 'directAgent' : '主Agent路由' } ` ,
454454 )
455+ logger . info (
456+ `[trace:cap] home.chat.start session=${ sid } focusCap=${ focusCap ?. id ?? '-' } (${ focusCap ?. name ?? '-' } ) ` +
457+ `msgLen=${ message . length } msgHead=${ JSON . stringify ( message . slice ( 0 , 80 ) ) } ` ,
458+ )
459+ signal . addEventListener (
460+ 'abort' ,
461+ ( ) => logger . warn ( `[trace:cap] home.signal.abort session=${ sid } ` ) ,
462+ { once : true } ,
463+ )
455464
456465 try {
457466 if ( directAgent ) {
@@ -464,9 +473,11 @@ export function registerHomeHandlers(): void {
464473 )
465474 if ( ! graph ) throw new Error ( '组队图构建失败' )
466475 const question = mentions . cleanText || message
476+ logger . info ( `[trace:cap] home.directAgent → runTeam agents=${ directAgent . map ( ( a ) => a . id ) . join ( ',' ) } ` )
467477 const result = await runTeam ( graph , question , sid , buildDeps , emitStream , signal )
468478 addMessage ( { sessionId : sid , role : 'assistant' , content : result . output } )
469479 emitStream ( { type : 'message_stop' , stop_reason : 'end_turn' } )
480+ logger . info ( `[trace:cap] home.directAgent.end session=${ sid } outputLen=${ result . output . length } ` )
470481 return { runId : sid }
471482 }
472483
@@ -509,6 +520,10 @@ export function registerHomeHandlers(): void {
509520 if ( result . hitIterationLimit ) {
510521 logger . warn ( '[home] 主 Agent 达工具轮次上限,已强制无工具收尾' )
511522 }
523+ logger . info (
524+ `[trace:cap] home.mainAgent.end session=${ sid } hitIterLimit=${ result . hitIterationLimit } ` +
525+ `finalTextLen=${ finalText . length } textTail=${ JSON . stringify ( finalText . slice ( - 80 ) ) } ` ,
526+ )
512527
513528 // —— 创建幻觉补跑:自称已入库 / 否认持久化,但从未调 propose_* → 按 kind 定向挂工具再跑 ——
514529 // 挡「嘴上创建成功、确认卡从未出现」;澄清追问不触发(见 needsCreateRecovery)。
@@ -591,13 +606,25 @@ export function registerHomeHandlers(): void {
591606
592607 // 流结束:判定直答 vs 组队
593608 const decision = detector . decide ( )
609+ logger . info (
610+ `[trace:cap] home.router.decide session=${ sid } kind=${ decision . kind } ` +
611+ ( decision . kind === 'team' ? ` json=${ JSON . stringify ( decision . json ) } ` : '' ) ,
612+ )
594613 if ( decision . kind === 'team' ) {
595614 // 组队:拼编排图跑 runner,事件经 orch_event 转前端
596615 logger . info ( '[home-router] 判为组队:' , JSON . stringify ( decision . json ) )
597616 emitStream ( { type : 'run_id' , sessionId : sid } )
598617 const graph = buildTeamGraph ( decision . json , getAgent , getCapability )
599618 if ( graph ) {
619+ logger . info (
620+ `[trace:cap] home.team.run session=${ sid } graphNodes=${ graph . nodes . length } ` +
621+ `types=[${ graph . nodes . map ( ( n ) => `${ n . id } :${ n . type } ` ) . join ( ',' ) } ]` ,
622+ )
600623 const teamResult = await runTeam ( graph , message , sid , buildDeps , emitStream , signal )
624+ logger . info (
625+ `[trace:cap] home.team.done session=${ sid } outputLen=${ teamResult . output . length } ` +
626+ `tail=${ JSON . stringify ( teamResult . output . slice ( - 80 ) ) } ` ,
627+ )
601628 addMessage ( {
602629 sessionId : sid ,
603630 role : 'assistant' ,
@@ -611,6 +638,7 @@ export function registerHomeHandlers(): void {
611638 } else {
612639 // 组队 JSON 指向的 role/capability 全失效 → 回退直答
613640 logger . warn ( '[home-router] 组队图构建失败(role/capability 失效),回退直答' )
641+ logger . warn ( `[trace:cap] home.team.graph_build_failed session=${ sid } json=${ JSON . stringify ( decision . json ) } ` )
614642 const direct = detector . flushDirect ( )
615643 if ( direct ) emitStream ( { type : 'text' , text : direct } )
616644 addMessage ( {
@@ -625,6 +653,9 @@ export function registerHomeHandlers(): void {
625653 }
626654 } else {
627655 // 直答:flush 尾窗残留推前端,存档
656+ logger . info (
657+ `[trace:cap] home.direct session=${ sid } focusCap=${ ! ! focusCap } finalTextLen=${ finalText . length } ` ,
658+ )
628659 const tail = detector . flushDirect ( )
629660 if ( tail ) emitStream ( { type : 'text' , text : tail } )
630661 addMessage ( {
@@ -644,13 +675,16 @@ export function registerHomeHandlers(): void {
644675 )
645676
646677 // 10. 结束事件:触顶收尾用 max_iterations,便于前端/日志区分假 end_turn
678+ const stopReason = result . hitIterationLimit ? 'max_iterations' : 'end_turn'
679+ logger . info ( `[trace:cap] home.message_stop session=${ sid } reason=${ stopReason } aborted=${ signal . aborted } ` )
647680 emitStream ( {
648681 type : 'message_stop' ,
649- stop_reason : result . hitIterationLimit ? 'max_iterations' : 'end_turn' ,
682+ stop_reason : stopReason ,
650683 } )
651684 } catch ( e ) {
652685 // 错误推到 AI 气泡位置(而非聊天区上方),含可重试提示
653686 const msg = e instanceof Error ? e . message : String ( e )
687+ logger . error ( `[trace:cap] home.error session=${ sid } aborted=${ signal . aborted } err=${ msg } ` , e )
654688 emitStream ( { type : 'error' , error : msg } )
655689 emitStream ( { type : 'message_stop' , stop_reason : 'error' } )
656690 throw e
0 commit comments