@@ -2576,23 +2576,41 @@ impl Database for DatabaseKv {
25762576 keys:: history:: HistorySubspaceVariant :: Forgotten ,
25772577 ) ) ;
25782578
2579- let loop_events_subspace =
2580- self . subspace
2581- . subspace ( & keys:: history:: EventHistorySubspaceKey :: entire (
2582- from_workflow_id,
2583- location. clone ( ) ,
2584- false ,
2585- ) ) ;
2579+ // Start is {loop location, 0, ...}
2580+ let loop_events_subspace_start = self
2581+ . subspace
2582+ . subspace ( & keys:: history:: EventHistorySubspaceKey :: entire (
2583+ from_workflow_id,
2584+ location. clone ( ) ,
2585+ false ,
2586+ ) )
2587+ . range ( )
2588+ . 0 ;
2589+ // End is {loop location, iteration - 1, ...}
2590+ let loop_events_subspace_end = self
2591+ . subspace
2592+ . subspace ( & keys:: history:: EventHistorySubspaceKey :: new (
2593+ from_workflow_id,
2594+ location. clone ( ) ,
2595+ iteration. saturating_sub ( 1 ) ,
2596+ false ,
2597+ ) )
2598+ . range ( )
2599+ . 1 ;
25862600
25872601 let mut stream = tx. get_ranges_keyvalues (
25882602 universaldb:: RangeOption {
25892603 mode : StreamingMode :: WantAll ,
2590- ..( & loop_events_subspace) . into ( )
2604+ ..(
2605+ loop_events_subspace_start. as_slice ( ) ,
2606+ loop_events_subspace_end. as_slice ( ) ,
2607+ )
2608+ . into ( )
25912609 } ,
25922610 Serializable ,
25932611 ) ;
25942612
2595- // Move all current events under this loop to the forgotten history
2613+ // Move all events under this loop up to the current iteration to the forgotten history
25962614 loop {
25972615 let Some ( entry) = stream. try_next ( ) . await ? else {
25982616 break ;
@@ -2602,15 +2620,15 @@ impl Database for DatabaseKv {
26022620 return Err ( universaldb:: tuple:: PackError :: BadPrefix . into ( ) ) ;
26032621 }
26042622
2605- // Truncate tuple up to ACTIVE and replace it with FORGOTTEN
2623+ // Truncate tuple up to ... ACTIVE and replace it with ... FORGOTTEN
26062624 let truncated_key = & entry. key ( ) [ active_history_subspace. bytes ( ) . len ( ) ..] ;
26072625 let forgotten_key =
26082626 [ forgotten_history_subspace. bytes ( ) , truncated_key] . concat ( ) ;
26092627
26102628 tx. set ( & forgotten_key, entry. value ( ) ) ;
26112629 }
26122630
2613- tx. clear_subspace_range ( & loop_events_subspace ) ;
2631+ tx. clear_range ( & loop_events_subspace_start , & loop_events_subspace_end ) ;
26142632
26152633 // Only retain last 100 events in forgotten history
26162634 if iteration > 100 {
0 commit comments