Replay verified completed native jobs on exact document undo

This commit is contained in:
gloamxun committed 2026-11-29 07:19:47 +08:00
1 parent d8af3518a7
commit f387b1db61
4 files changed
+250 -30

No files matched your search

+122 -30
View File
@@ -25,6 +25,8 @@ pub struct EntryCheckpointWork {
/// Child input at the resume frontier, when the entry scanner was suspended.
pub reused_source: Option<PathBuf>,
pub reused_steps: usize,
/// The complete kernel result matched a previously successful input.
pub reused_completion: bool,
pub replayed_calls: usize,
pub rejection: Option<String>,
}
@@ -37,6 +39,7 @@ pub(super) struct CheckpointCache {
pub(super) struct PublishedCheckpoints {
current: Arc<CheckpointSet>,
previous_body: Option<Arc<CheckpointSet>>,
previous_completion: Option<Arc<CompletedExecution>>,
}
struct CachedCheckpoint {
@@ -47,16 +50,28 @@ struct CachedCheckpoint {
}
struct CompletedExecution {
result: Arc<RevisionExecution>,
guard: PrefixInputGuard,
calls: Arc<[Call]>,
}
pub(super) struct CheckpointSet {
entries: Vec<Arc<CachedCheckpoint>>,
calls: Vec<Call>,
calls: Arc<[Call]>,
completion: Option<Arc<CompletedExecution>>,
refresh_on_resume: bool,
edit_frontier: Option<usize>,
}
pub(super) struct SelectedCheckpoint {
set: Arc<CheckpointSet>,
index: usize,
candidate: ExecutionCandidate,
}
enum ExecutionCandidate {
Boundary(usize),
Completed(Arc<CompletedExecution>),
}
const SOURCE_INTERVALS: usize = 4;
@@ -298,6 +313,7 @@ impl<'a> CapturedEntries<'a> {
self,
recording: RecordingHost,
refresh_on_resume: bool,
result: &Arc<RevisionExecution>,
) -> Option<Arc<CheckpointSet>> {
if !self.changed {
return None;
@@ -333,11 +349,33 @@ impl<'a> CapturedEntries<'a> {
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()?;
// Only automatic sessions retain completed jobs, and only when the
// recorder reached actual closeout without filling its call budget.
let completion = refresh_on_resume.then(|| {
let (trace, _) = recording.prefix()?;
let guard = PrefixInputGuard::capture(
self.request,
self.entry_path.as_deref()?,
trace,
recording.generated_outputs(),
self.initial?,
).ok()?;
Some(guard)
}).flatten();
let mut calls = recording.into_calls()?;
calls.truncate(replay_count);
if completion.is_none() {
calls.truncate(replay_count);
}
let calls = Arc::<[Call]>::from(calls);
let completion = completion.map(|guard| Arc::new(CompletedExecution {
result: result.clone(),
guard,
calls: calls.clone(),
}));
Some(Arc::new(CheckpointSet {
entries,
calls,
completion,
refresh_on_resume,
edit_frontier,
}))
@@ -376,6 +414,9 @@ impl CheckpointCache {
*published = None;
return;
};
if published.as_ref().is_some_and(|set| Arc::ptr_eq(&set.current, &next)) {
return;
}
if published
.as_ref()
.and_then(|set| set.previous_body.as_ref())
@@ -400,9 +441,13 @@ impl CheckpointCache {
published.previous_body.clone()
}
});
let previous_completion = published.as_ref().and_then(|published| {
published.current.completion.clone().or_else(|| published.previous_completion.clone())
});
*published = Some(Arc::new(PublishedCheckpoints {
current: next,
previous_body,
previous_completion,
}));
}
@@ -437,11 +482,9 @@ impl CheckpointCache {
},
|slots, entry, host| captures.capture(slots, entry, host),
);
let next = if result.is_ok() {
captures.finish(recording, self.minimum_frontier.is_none())
} else {
None
};
let next = result.as_ref().ok().and_then(|result| {
captures.finish(recording, self.minimum_frontier.is_none(), result)
});
(result, next)
}
}
@@ -462,6 +505,12 @@ impl PublishedCheckpoints {
.current
.candidates()
.chain(self.previous_body.iter().flat_map(|body| body.candidates()))
.chain(self.current.completion.iter().chain(&self.previous_completion).map(|result| {
SelectedCheckpoint {
set: self.current.clone(),
candidate: ExecutionCandidate::Completed(result.clone()),
}
}))
.collect();
candidates.sort_by_key(|candidate| std::cmp::Reverse(candidate.completed_steps()));
let mut candidates = VecDeque::from(candidates);
@@ -471,10 +520,9 @@ impl PublishedCheckpoints {
check(cancellation)?;
let first = std::iter::from_fn(|| candidates.pop_front())
.find(|candidate| {
let checkpoint = &candidate.set.entries[candidate.index];
match checkpoint
match candidate
.accept_entry(project)
.and_then(|()| checkpoint.guard.verify_project(request))
.and_then(|()| candidate.guard().verify_project(request))
{
Ok(_) => true,
Err(error) => {
@@ -505,10 +553,9 @@ impl PublishedCheckpoints {
let mut rejection = None;
let remaining = std::iter::from_fn(|| candidates.pop_front());
for candidate in std::iter::once(first).chain(remaining) {
let checkpoint = &candidate.set.entries[candidate.index];
match checkpoint
match candidate
.accept_entry(project)
.and_then(|()| checkpoint.guard.verify(request, world, preparation))
.and_then(|()| candidate.guard().verify(request, world, preparation))
{
Ok(()) => {
selected = Some(candidate);
@@ -525,23 +572,23 @@ impl PublishedCheckpoints {
KernelPreparation::Resume,
)?;
let selected = selected.expect("resume preparation chose a verified checkpoint");
let checkpoint = &selected.set.entries[selected.index];
let partial_source = checkpoint.guard.nested_source.as_deref()
let guard = selected.guard();
let partial_source = guard.nested_source.as_deref()
.and_then(|path| project.file(path)).map(|file| &file.version);
for (index, call) in selected.set.calls[..checkpoint.replay_count].iter().enumerate() {
for (index, call) in selected.calls().iter().enumerate() {
check(cancellation)?;
if let Err(error) = call.replay(&mut prepared.files, partial_source) {
replay_rejection.get_or_insert(CompileError::Host(error));
// Other checkpoints sharing this call cannot use the
// invalidated prefix either. Drop the draft on retry.
candidates.retain(|candidate| {
!Arc::ptr_eq(&candidate.set, &selected.set)
|| candidate.set.entries[candidate.index].replay_count <= index
let calls = candidate.calls();
calls.len() <= index || !std::ptr::eq(calls.as_ptr(), selected.calls().as_ptr())
});
continue 'retry;
}
}
if let Err(error) = checkpoint.guard.verify_replayed_inputs(&prepared.files, project) {
if let Err(error) = guard.verify_replayed_inputs(&prepared.files, project) {
replay_rejection.get_or_insert(error);
continue;
}
@@ -567,7 +614,8 @@ impl CheckpointSet {
let replay_count = entries.last()?.replay_count;
Some(Arc::new(Self {
entries,
calls: self.calls[..replay_count].to_vec(),
calls: Arc::from(&self.calls[..replay_count]),
completion: None,
refresh_on_resume: self.refresh_on_resume,
edit_frontier: self.edit_frontier,
}))
@@ -585,7 +633,7 @@ impl CheckpointSet {
.rev()
.map(|index| SelectedCheckpoint {
set: Arc::clone(self),
index,
candidate: ExecutionCandidate::Boundary(index),
})
}
}
@@ -613,7 +661,42 @@ impl CachedCheckpoint {
impl SelectedCheckpoint {
fn completed_steps(&self) -> usize {
self.set.entries[self.index].entry.completed_steps()
match &self.candidate {
ExecutionCandidate::Boundary(index) => self.set.entries[*index].entry.completed_steps(),
ExecutionCandidate::Completed(job) => job.result.execution.steps,
}
}
fn guard(&self) -> &PrefixInputGuard {
match &self.candidate {
ExecutionCandidate::Boundary(index) => &self.set.entries[*index].guard,
ExecutionCandidate::Completed(job) => &job.guard,
}
}
fn accept_entry(&self, project: &ProjectSnapshot) -> Result<(), String> {
match &self.candidate {
ExecutionCandidate::Boundary(index) => self.set.entries[*index].accept_entry(project),
ExecutionCandidate::Completed(job) => {
let path = &job.guard.entry;
if job.guard.project.file(path).zip(project.file(path))
.is_some_and(|(old, new)| old.version == new.version && old.data == new.data)
{
Ok(())
} else {
Err("completed checkpoint entry changed".into())
}
}
}
}
fn calls(&self) -> &[Call] {
match &self.candidate {
ExecutionCandidate::Boundary(index) => {
&self.set.calls[..self.set.entries[*index].replay_count]
}
ExecutionCandidate::Completed(job) => &job.calls,
}
}
pub(super) fn resume(
@@ -631,7 +714,18 @@ impl SelectedCheckpoint {
),
CompileError,
> {
let checkpoint = &self.set.entries[self.index];
let index = match &self.candidate {
ExecutionCandidate::Boundary(index) => *index,
ExecutionCandidate::Completed(completed) => {
work.retained_checkpoints = self.set.entries.len();
work.reused_frontier = Some(source.data.len());
work.reused_steps = completed.result.execution.steps;
work.reused_completion = true;
work.replayed_calls = completed.calls.len();
return Ok((job.reuse_completed_after_host_replay(&completed.result, files), self.set));
}
};
let checkpoint = &self.set.entries[index];
let frontier = checkpoint.entry.frontier();
work.retained_checkpoints = self.set.entries.len();
work.reused_frontier = Some(frontier);
@@ -663,7 +757,7 @@ impl SelectedCheckpoint {
});
let mut captures = CapturedEntries::new(request, files);
// Earlier retained positions refer only to calls already replayed.
for previous in &self.set.entries[..=self.index] {
for previous in &self.set.entries[..=index] {
if previous.replay_count > checkpoint.replay_count {
continue;
}
@@ -709,11 +803,9 @@ impl SelectedCheckpoint {
|slots, entry, host| captures.capture(slots, entry, host),
)
.ok_or_else(changed_prefix)?;
let next = if result.is_ok() {
captures.finish(recording, true)
} else {
None
};
let next = result.as_ref().ok().and_then(|result| {
captures.finish(recording, true, result)
});
if let Some(next) = next {
next.report_capture(work);
Ok((result, next))