@@ -33,6 +33,7 @@ class SerializableResult:
3333 artifacts : Optional [Dict [str , Any ]] = None
3434 iteration : int = 0
3535 error : Optional [str ] = None
36+ target_island : Optional [int ] = None # Island where child should be placed
3637
3738
3839def _worker_init (config_dict : dict , evaluation_file : str , parent_env : dict = None ) -> None :
@@ -170,8 +171,7 @@ def _run_iteration_worker(
170171 # Build prompt
171172 if _worker_config .prompt .programs_as_changes_description :
172173 parent_changes_desc = (
173- parent .changes_description
174- or _worker_config .prompt .initial_changes_description
174+ parent .changes_description or _worker_config .prompt .initial_changes_description
175175 )
176176 child_changes_desc = parent_changes_desc
177177 else :
@@ -223,7 +223,9 @@ def _run_iteration_worker(
223223
224224 diff_blocks = extract_diffs (llm_response , _worker_config .diff_pattern )
225225 if not diff_blocks :
226- return SerializableResult (error = "No valid diffs found in response" , iteration = iteration )
226+ return SerializableResult (
227+ error = "No valid diffs found in response" , iteration = iteration
228+ )
227229
228230 if _worker_config .prompt .programs_as_changes_description :
229231 try :
@@ -236,20 +238,34 @@ def _run_iteration_worker(
236238 return SerializableResult (error = str (e ), iteration = iteration )
237239
238240 child_code , _ = apply_diff_blocks (parent .code , code_blocks )
239- child_changes_desc , desc_applied = apply_diff_blocks (parent_changes_desc , desc_blocks )
241+ child_changes_desc , desc_applied = apply_diff_blocks (
242+ parent_changes_desc , desc_blocks
243+ )
240244
241245 # Must update the previous changes description
242- if desc_applied == 0 or not child_changes_desc .strip () or child_changes_desc .strip () == parent_changes_desc .strip ():
246+ if (
247+ desc_applied == 0
248+ or not child_changes_desc .strip ()
249+ or child_changes_desc .strip () == parent_changes_desc .strip ()
250+ ):
243251 return SerializableResult (
244252 error = "changes_description was not updated or empty, program is discarded" ,
245253 iteration = iteration ,
246254 )
247255
248- changes_summary = format_diff_summary (code_blocks )
256+ changes_summary = format_diff_summary (
257+ code_blocks ,
258+ max_line_len = _worker_config .prompt .diff_summary_max_line_len ,
259+ max_lines = _worker_config .prompt .diff_summary_max_lines ,
260+ )
249261 else :
250262 # All diffs applied only to code
251263 child_code = apply_diff (parent .code , llm_response , _worker_config .diff_pattern )
252- changes_summary = format_diff_summary (diff_blocks )
264+ changes_summary = format_diff_summary (
265+ diff_blocks ,
266+ max_line_len = _worker_config .prompt .diff_summary_max_line_len ,
267+ max_lines = _worker_config .prompt .diff_summary_max_lines ,
268+ )
253269 else :
254270 from openevolve .utils .code_utils import parse_full_rewrite
255271
@@ -297,6 +313,9 @@ def _run_iteration_worker(
297313
298314 iteration_time = time .time () - iteration_start
299315
316+ # Get target island from snapshot (where child should be placed)
317+ target_island = db_snapshot .get ("sampling_island" )
318+
300319 return SerializableResult (
301320 child_program_dict = child_program .to_dict (),
302321 parent_id = parent .id ,
@@ -305,6 +324,7 @@ def _run_iteration_worker(
305324 llm_response = llm_response ,
306325 artifacts = artifacts ,
307326 iteration = iteration ,
327+ target_island = target_island ,
308328 )
309329
310330 except Exception as e :
@@ -539,9 +559,14 @@ async def run_evolution(
539559 # Reconstruct program from dict
540560 child_program = Program (** result .child_program_dict )
541561
542- # Add to database (will auto-inherit parent's island)
543- # No need to specify target_island - database will handle parent island inheritance
544- self .database .add (child_program , iteration = completed_iteration )
562+ # Add to database with explicit target_island to ensure proper island placement
563+ # This fixes issue #391: children should go to the target island, not inherit
564+ # from the parent (which may be from a different island due to fallback sampling)
565+ self .database .add (
566+ child_program ,
567+ iteration = completed_iteration ,
568+ target_island = result .target_island ,
569+ )
545570
546571 # Store artifacts
547572 if result .artifacts :
@@ -588,10 +613,8 @@ async def run_evolution(
588613
589614 # Island management
590615 # get current program island id
591- island_id = child_program .metadata .get (
592- "island" , self .database .current_island
593- )
594- #use this to increment island generation
616+ island_id = child_program .metadata .get ("island" , self .database .current_island )
617+ # use this to increment island generation
595618 self .database .increment_island_generation (island_idx = island_id )
596619
597620 # Check migration
@@ -709,7 +732,7 @@ async def run_evolution(
709732 f"(best score: { best_score :.4f} )"
710733 )
711734 break
712-
735+
713736 else :
714737 # Event-based early stopping
715738 if current_score == self .config .convergence_threshold :
0 commit comments