@@ -354,17 +354,23 @@ class FlowGraphBuilder {
354354 value : Math . round ( sellerValue ) , quantity : productQty , category : "source" , // Seller = demand root
355355 } ) ;
356356
357+ // Terminal type: use registry output type of the terminal machine, not resolvedType
358+ // (resolvedType can be an input type like "casing" instead of "power_core")
359+ const terminalMachine = registry . get ( chainResult . machine ) ;
360+ const terminalType = terminalMachine ?. outputs ?. [ 0 ] ?. type || chainResult . type || "?" ;
361+
357362 let topParentId = sellerId ;
363+ let qaNodeId = null ;
358364 if ( hasQA ) {
359- const qaId = nextId ++ ;
365+ qaNodeId = nextId ++ ;
360366 nodes . push ( {
361- id : qaId , machine : "quality_assurance" , type : chainResult . resolvedType || chainResult . type || "?" ,
367+ id : qaNodeId , machine : "quality_assurance" , type : terminalType ,
362368 name : "Quality Assurance" ,
363369 value : Math . round ( chainResult . value * ( 1 + qa . value ) ) ,
364- quantity : 0 , category : qa . category || "multipurpose" , // Set by Pass 2
370+ quantity : 0 , category : qa . category || "multipurpose" ,
365371 } ) ;
366- edges . push ( { from : qaId , to : sellerId , itemType : chainResult . resolvedType || "?" , quantity : 0 , kind : "main" } ) ;
367- topParentId = qaId ;
372+ edges . push ( { from : qaNodeId , to : sellerId , itemType : terminalType , quantity : 0 , kind : "main" } ) ;
373+ topParentId = qaNodeId ;
368374 }
369375
370376 walkNode ( chainResult , topParentId , false , false ) ;
@@ -529,8 +535,9 @@ class FlowGraphBuilder {
529535
530536 // --- Phase C: Excess routing ---
531537 // Byproduct chain nodes that produce more than their main chain consumers
532- // need get "Sell Excess" nodes to show where surplus goes.
533- // Rebuild consumersOf after all edges are finalized
538+ // need get routed to QA → Seller to show where surplus goes.
539+ const excessTarget = qaNodeId || sellerId ;
540+
534541 const finalConsumersOf = new Map ( ) ;
535542 for ( const edge of edges ) {
536543 if ( ! finalConsumersOf . has ( edge . from ) ) finalConsumersOf . set ( edge . from , [ ] ) ;
@@ -539,31 +546,20 @@ class FlowGraphBuilder {
539546
540547 for ( const n of nodes ) {
541548 if ( n . quantity <= 0 ) continue ;
542- // Only check nodes that have at least one main/enhancement consumer
543549 const outEdges = finalConsumersOf . get ( n . id ) || [ ] ;
544550 const mainOutEdges = outEdges . filter ( e => e . kind !== "byproduct" ) ;
545551 if ( mainOutEdges . length === 0 ) continue ;
546552
547- // Sum demand from main chain consumers
548553 let totalConsumed = 0 ;
549554 for ( const e of mainOutEdges ) {
550555 totalConsumed += e . quantity || 0 ;
551556 }
552557
553558 const excess = n . quantity - totalConsumed ;
554559 if ( excess > 0 && totalConsumed > 0 ) {
555- // Create a "Sell Excess" node
556- const excessId = nextId ++ ;
557- const ds = config . hasDoubleSeller ? 2 : 1 ;
558- const excessValue = Math . round ( ( n . value || 0 ) * ds ) ;
559- nodes . push ( {
560- id : excessId , machine : "sell_excess" , type : n . type ,
561- name : "Sell Excess" , value : excessValue ,
562- quantity : excess , category : "source" ,
563- } ) ;
564- nodeById . set ( excessId , nodes [ nodes . length - 1 ] ) ;
560+ // Route excess to QA (or seller if no QA)
565561 edges . push ( {
566- from : n . id , to : excessId ,
562+ from : n . id , to : excessTarget ,
567563 itemType : ITEM_TYPES [ n . type ] || n . type ,
568564 quantity : excess , kind : "byproduct" ,
569565 } ) ;
0 commit comments