Wire bounded output collection into the code mode runtime (#57850)

## Why

Bounding collected output also requires limiting the raw event backlog between
V8 and the cell actor. Previously, runtime events and cell output accumulated
in an unbounded channel and vector.

## What changed

- Select the event transport and `OutputCollector` from the cell's
  `output_truncation` mode. Server mode uses an ordered, bounded channel with
  backpressure; client mode retains untruncated output.
- Transfer the collector with each observation and restore it on failed
  delivery, preserving retained media and omission metadata.
- Include server-mode script errors in the observation's output budget. Build
  completion output only after completion wins against termination, so a
  rejected error cannot evict output returned by termination.
- Keep the public service on client-side truncation.

## Testing

Add regression coverage for channel backpressure, event ordering, receiver
closure, failed observation delivery, termination during completion commit,
and oversized errors across UTF-8 and omission boundaries.

GitOrigin-RevId: 7604b60416e33c636ca41138be65dc0fd452774a
This commit is contained in:
Sean Huang
2026-10-28 22:33:13 +00:00
committed by copyberry
parent 6d97d6ecb4
commit 92e802fb61
21 changed files with 686 additions and 355 deletions
@@ -11,6 +11,7 @@ mod output_collection;
mod output_render;
pub(crate) use output_collection::BoundedOutput;
#[allow(unused_imports, reason = "Renderer is wired in the next stack layer")]
pub(crate) use output_render::render_output;
/// The actor transfers the whole collector at each observation, so a failed
@@ -38,6 +39,20 @@ impl OutputCollector {
}
}
/// Server errors share the observation's budget; preserve their presence
/// for completion status. Client errors retain their original formatting.
pub(crate) fn collect_error(&mut self, error_text: String) -> String {
match self {
Self::Client(_) => error_text,
Self::Server(output) => {
output
.get_or_insert_with(Box::default)
.push_error(&error_text);
String::new()
}
}
}
pub(crate) fn take(&mut self) -> Self {
match self {
Self::Client(items) => Self::Client(std::mem::take(items)),
@@ -435,6 +435,34 @@ impl HeadTailOutput {
}
impl BoundedOutput {
/// A completed error is one logical text item, including its label. Do not
/// allocate an arbitrarily large formatted copy before selecting its output.
pub(crate) fn push_error(&mut self, error: &str) {
const LABEL: &str = "Script error:\n";
// No selection can retain more than MAX_OUTPUT_BYTES from either end
// of this item. Keep both possible spans until push() decides how the
// existing head, tail and item pressure affect them. Round outward so
// a UTF-8 boundary cannot pull bytes from the prefix across the gap.
let head_end = error.ceil_char_boundary(error.len().min(MAX_OUTPUT_BYTES - LABEL.len()));
let tail_start =
error.floor_char_boundary(error.len().saturating_sub(MAX_OUTPUT_BYTES).max(head_end));
self.push(OutputItem::InputText {
text: format!("{LABEL}{}{}", &error[..head_end], &error[tail_start..]),
});
let skipped = tail_start - head_end;
if skipped > 0 {
// This item alone exceeds the content budget. Its newest, retained
// suffix owns the gap, even when an image separates it from older loss.
let Retention::HeadTail(output) = &mut self.state else {
unreachable!("an oversized error must enter head/tail retention");
};
let Some(RetainedItem::Partial(text)) = output.tail.back_mut() else {
unreachable!("the tail retains the end of the oversized error");
};
text.omitted = text.omitted.saturating_add(skipped as u64);
}
}
pub(crate) fn push(&mut self, item: OutputItem) {
if let Err(loss) = self.media.admit(&item) {
self.push_retained(RetainedItem::Gap(loss));
@@ -299,3 +299,34 @@ fn repeated_eviction_keeps_recent_writes_and_gaps_on_each_side_of_an_image() {
assert_eq!(after.omitted.text_bytes, (writes * chunk.len()) as u64);
assert_eq!(after.omitted.text_items, (1 + writes - recent) as u64);
}
#[test]
fn terminal_error_matches_ordinary_text_across_loss_boundaries() {
let label = "Script error:\n";
// Rounding inward at the emoji can wrongly pull XYZ into the retained suffix.
let error = format!(
"{}XYZ{}💡{}",
"p".repeat(MAX_OUTPUT_BYTES - label.len() - 3),
"m".repeat(22),
"s".repeat(MAX_OUTPUT_BYTES - 3),
);
let half = text(&"p".repeat(MAX_OUTPUT_BYTES / 2));
for (prefix, gap_count) in [
(Vec::new(), 1),
(vec![text(""); MAX_OUTPUT_ITEMS / 2], 1),
(vec![half.clone(), text("lost"), image("i".into()), half], 2),
] {
let mut terminal = BoundedOutput::default();
let mut ordinary = BoundedOutput::default();
for item in prefix {
terminal.push(item.clone());
ordinary.push(item);
}
terminal.push_error(&error);
ordinary.push(text(&format!("{label}{error}")));
assert!(retained_capacity(&terminal) < MAX_OUTPUT_BYTES + TEXT_BLOCK_BYTES);
let actual = terminal.into_collected_output();
assert_eq!(actual.gaps.len(), gap_count);
assert!(actual == ordinary.into_collected_output());
}
}
@@ -30,6 +30,10 @@ const MAX_POSITIONED_NOTICES: usize = 4;
/// budget: Core's warning describes the retained text alone. The shared
/// formatter moves collector boundaries along with their source content.
/// If losses are too scattered for individual notices, report their totals once.
#[cfg_attr(
not(test),
allow(dead_code, reason = "Renderer is wired in the next stack layer")
)]
pub(crate) fn render_output(
output: CollectedOutput,
max_output_tokens: Option<usize>,