diff --git a/src/native_checkpoint_guard.rs b/src/native_checkpoint_guard.rs index e531dc5..463d6a7 100644 --- a/src/native_checkpoint_guard.rs +++ b/src/native_checkpoint_guard.rs @@ -491,12 +491,13 @@ impl<'a> CapturedEntries<'a> { completed.guard.verify_replayed_inputs(host.files(), project).map_err(|error| error.to_string()) } - fn finish( + fn finish<'b>( self, recording: RecordingHost, refresh_on_resume: bool, result: &Arc, suffix_base: Option>, + inherited: impl Iterator>, ) -> Option> { if !self.changed { return None; @@ -529,6 +530,37 @@ impl<'a> CapturedEntries<'a> { .expect("the full schedule has ordinary continuations"); entries.remove(index); } + let before_child = recording.prefix().and_then(|(_, calls)| { + // Keep one existing parent anchor before a child's first size + // observation. It can still replay when a new size invalidates + // the nested anchors, even if a different byte grid drops it. + let (child, first_size_call) = self.nested.iter().flatten().flatten() + .filter_map(|entry| entry.guard.nested_source.as_ref()) + .find_map(|source| { + let size = source.observed_size?; + let path = source.path.as_path(); + let (index, observed_size) = calls.iter().enumerate().find_map(|(index, call)| { + let (observed_path, observed_size) = call.observed_file_size()?; + (observed_path == path).then_some((index, observed_size)) + })?; + (observed_size == size).then_some((path, index)) + })?; + let eligible = |entry: &&Arc| { + matches!(entry.slots, Capture::Entry(_)) + && entry.replay_count <= first_size_call + && !entry.guard.immutable.contains(child) + && !entry.guard.generated.contains(child) + }; + let steps = |entry: &&Arc| entry.entry.completed_steps(); + let captured = entries.iter().filter(eligible).max_by_key(steps); + let earlier = inherited.filter(eligible).max_by_key(steps); + [captured, earlier].into_iter().flatten().max_by_key(steps).cloned() + }); + if let Some(entry) = before_child + && !entries.iter().any(|prior| Arc::ptr_eq(prior, &entry)) + { + entries.push(entry); + } entries.extend(self.nested.into_iter().flatten().flatten()); entries.sort_by_key(|candidate| candidate.entry.completed_steps()); let replay_count = entries.iter().map(|entry| entry.replay_count).max()?; @@ -692,7 +724,7 @@ impl CheckpointCache { |slots, entry, host| captures.capture(slots, Arc::new(entry), host), ); let next = result.as_ref().ok().and_then(|result| { - captures.finish(recording, self.minimum_frontier.is_none(), result, None) + captures.finish(recording, self.minimum_frontier.is_none(), result, None, std::iter::empty()) }); (result, next) } @@ -1015,28 +1047,28 @@ impl SelectedCheckpoint { data: file.data.to_vec(), }); let mut captures = CapturedEntries::new(request, files); - if let Some(current) = &self.current_set_for_nested_resume { - // A normal entry checkpoint may still use this replay even - // when the selected child checkpoint came from another revision. - // Keep only earlier entry anchors with the same recorded calls. - let common = current.calls.iter().zip(&self.set.calls[..checkpoint.replay_count]) + // An entry anchor in a different tape can be kept only when the + // actual replay used the same recorded calls through that anchor. + let common = self.current_set_for_nested_resume.as_ref().map_or(0, |current| { + current.calls.iter().zip(&self.set.calls[..checkpoint.replay_count]) .take_while(|(current, old)| same_recorded_call(current, old)) - .count(); - for previous in ¤t.entries { - if previous.replay_count <= common + .count() + }); + let from_current = self.current_set_for_nested_resume.iter() + .flat_map(|current| current.entries.iter()) + .filter(|previous| { + matches!(previous.slots, Capture::Entry(_)) + && previous.replay_count <= common && previous.entry.completed_steps() <= checkpoint.entry.completed_steps() && previous.entry.frontier() <= entry_frontier - && let Some(slots) = schedule.retain(previous) - { - captures.retain(Capture::Entry(slots), previous.clone()); - } - } - } - // Earlier retained positions refer only to calls already replayed. - for previous in &self.set.entries[..=index] { - if previous.replay_count > checkpoint.replay_count { - continue; - } + }); + let previous = self.set.entries[..=index].iter().filter(|previous| { + previous.replay_count <= checkpoint.replay_count + && (!matches!(previous.slots, Capture::Entry(_)) + || previous.entry.frontier() <= entry_frontier) + }); + let inherited = from_current.chain(previous); + for previous in inherited.clone() { match previous.slots { Capture::Entry(_) if previous.entry.frontier() <= entry_frontier => { if let Some(slots) = schedule.retain(previous) { @@ -1180,7 +1212,9 @@ impl SelectedCheckpoint { timings.clones = clones; } let next = measure(&mut timings.retain, timed, || { - result.as_ref().ok().and_then(|result| captures.finish(recording, true, result, suffix_base)) + result.as_ref().ok().and_then(|result| { + captures.finish(recording, true, result, suffix_base, inherited) + }) }); if result.is_ok() { if let Some(observer) = &request.observer {