Relocate source provenance when native suffixes move

This commit is contained in:
gloamxun committed 2026-12-01 22:17:10 +08:00
1 parent 6acd82dbfa
commit 41a459b4ca
16 files changed
+1507 -79

No files matched your search

@@ -189,20 +189,40 @@ impl StatisticsReport {
return None;
};
for usage in [before, after] {
usage.source_payload_bytes = usage.source_payload_bytes.checked_sub(previous)?.checked_add(current)?;
usage.mouth_payload_bytes = usage.mouth_payload_bytes.checked_sub(previous)?.checked_add(current)?;
if let Some(host) = &mut usage.host {
let size = u64::try_from(current).ok()?;
if replayed_entry_bytes != Some(size) {
return None;
}
host.rebind_entry_bytes(u64::try_from(previous).ok()?, size)?;
if usage.host.is_some() && replayed_entry_bytes != u64::try_from(current).ok() {
return None;
}
usage.resize_entry_payload(previous, current, true)?;
}
debug_assert_eq!(self.text, self.phase.text());
Some(())
}
/// Recompute a report for a verified suffix after the initial entry read
/// changed size. The caller authenticates the entry scanner's lifetime.
pub fn with_suffix_entry_resize(&self, previous: usize, current: usize) -> Option<Self> {
let mut phase = self.phase.clone();
match &mut phase {
StatisticsPhase::Shipout { before, after } => {
for usage in [before, after] {
let active = usage.mouth_payload_bytes == previous;
if !active && usage.mouth_payload_bytes != 0 {
return None;
}
usage.resize_entry_payload(previous, current, active)?;
}
}
StatisticsPhase::End { usage, .. } => {
if usage.stacks.input_frames != 0 || usage.mouth_payload_bytes != 0 {
return None;
}
usage.resize_entry_payload(previous, current, false)?;
}
}
let report = Self::new(phase, self.destination, self.span);
report.within_diagnostic_budget().then_some(report)
}
pub fn with_step_offset(&self, offset: StepOffset) -> Option<Self> {
let mut phase = self.phase.clone();
match &mut phase {
@@ -213,12 +233,35 @@ impl StatisticsReport {
StatisticsPhase::End { usage, .. } => usage.steps = offset.apply(usage.steps)?,
}
let report = Self::new(phase, self.destination, self.span);
if let StatisticsPhase::End { usage, .. } = &report.phase
&& usage.diagnostic_bytes_generated.checked_add(report.text.len())? > usage.token_limit
{
return None;
report.within_diagnostic_budget().then_some(report)
}
fn within_diagnostic_budget(&self) -> bool {
match &self.phase {
StatisticsPhase::End { usage, .. } => usage
.diagnostic_bytes_generated
.checked_add(self.text.len())
.is_some_and(|bytes| bytes <= usage.token_limit),
StatisticsPhase::Shipout { .. } => true,
}
Some(report)
}
}
impl ResourceUsage {
fn resize_entry_payload(
&mut self,
previous: usize,
current: usize,
active: bool,
) -> Option<()> {
self.source_payload_bytes = self.source_payload_bytes.checked_sub(previous)?.checked_add(current)?;
if active {
self.mouth_payload_bytes = self.mouth_payload_bytes.checked_sub(previous)?.checked_add(current)?;
}
if let Some(host) = &mut self.host {
host.rebind_entry_bytes(u64::try_from(previous).ok()?, u64::try_from(current).ok()?)?;
}
Some(())
}
}