@@ -13,6 +13,7 @@ import { BLOCK_CATEGORY_ORDER } from '$lib/constants/python';
1313import { isSubsystem , isInterface } from '$lib/nodes/shapes' ;
1414import { blockImportPaths } from '$lib/nodes/generated/blocks' ;
1515import { ENGINE_MODULE , enginePath } from '$lib/constants/engine' ;
16+ import { generateEngineSetup } from './engineCodegen' ;
1617import { graphStore , findParentSubsystem } from '$lib/stores/graph' ;
1718import {
1819 runStreamingSimulation ,
@@ -421,6 +422,14 @@ export function generatePythonCode(
421422 }
422423 lines . push ( '' ) ;
423424
425+ // 1b. Engine-specific setup (e.g. fastsim port() wraps); no-op by default.
426+ const engineSetup = generateEngineSetup ( importGroups ) ;
427+ if ( engineSetup ) {
428+ lines . push ( `# ${ engineSetup . header } ` ) ;
429+ lines . push ( ...engineSetup . lines ) ;
430+ lines . push ( '' ) ;
431+ }
432+
424433 // 2. Code context (user-defined variables/functions)
425434 if ( codeContext . trim ( ) ) {
426435 lines . push ( '# CODE CONTEXT' ) ;
@@ -475,7 +484,10 @@ export function generatePythonCode(
475484 lines . push ( '# NODE ID MAPPING (for data extraction)' ) ;
476485 lines . push ( '_node_id_map = {' ) ;
477486 for ( const [ nodeId , varName ] of nodeVars ) {
478- lines . push ( ` id(${ varName } ): "${ nodeId } ",` ) ;
487+ // _block_key (REPL setup) uses the engine's stable block_id when
488+ // present and falls back to id(), so static entries here stay
489+ // consistent with the mutation-added ones in _apply_mutations.
490+ lines . push ( ` _block_key(${ varName } ): "${ nodeId } ",` ) ;
479491 }
480492 lines . push ( '}' ) ;
481493 lines . push ( '' ) ;
@@ -599,6 +611,17 @@ function generateFormattedPythonCode(
599611 }
600612 lines . push ( '' ) ;
601613
614+ // Engine-specific setup (e.g. fastsim port() wraps); no-op by default.
615+ const engineSetup = generateEngineSetup ( importGroups ) ;
616+ if ( engineSetup ) {
617+ lines . push ( divider ) ;
618+ lines . push ( `# ${ engineSetup . header } ` ) ;
619+ lines . push ( divider ) ;
620+ lines . push ( '' ) ;
621+ lines . push ( ...engineSetup . lines ) ;
622+ lines . push ( '' ) ;
623+ }
624+
602625 // Code context (user-defined variables/functions)
603626 if ( codeContext . trim ( ) ) {
604627 lines . push ( divider ) ;
0 commit comments