use base64::Engine as _;
use agent_base::Content;
use codexec::{ArtifactHub, artifact_line};
use phi_ns::{ImageRef, ImageSource};
use super::{CellEffects, CellEvent, Observation};
use crate::script_tool::truncate_output;
pub(super) fn render(
obs: Observation,
max_chars: Option<usize>,
hub: Option<&ArtifactHub>,
) -> Vec<Content> {
let mut blocks: Vec<String> = Vec::new();
let mut artifacts: Vec<Content> = Vec::new();
if !obs.stdout.is_empty() {
blocks.push(obs.stdout);
}
if !obs.stderr.is_empty() {
blocks.push(format!("stderr:\n{}", obs.stderr));
}
if !obs.progress.is_empty() {
blocks.push(format!("progress:\n{}", obs.progress.join("\n")));
}
for tally in &obs.effects {
blocks.push(effects_line(tally));
}
if let Some(err) = &obs.error {
blocks.push(format!("error: {err}"));
}
let mut images = Vec::new();
for event in &obs.events {
match event {
CellEvent::Image(image) => images.push(image_content(image)),
CellEvent::TodoChanged { id, status } => {
blocks.push(format!("todo {id}: {status}"));
}
CellEvent::Notice { text } => blocks.push(format!("notice: {text}")),
CellEvent::Artifact {
capture_id,
kind,
bytes,
summary,
} => {
blocks.push(artifact_line(capture_id, kind, *bytes, summary));
artifacts.push(Content::artifact(
capture_id.clone(),
kind.clone(),
summary.clone(),
));
}
}
}
let mut out = Vec::new();
if blocks.is_empty() {
if images.is_empty() {
out.push(Content::text("(no output)"));
}
} else {
out.push(Content::text(fit_to_cap(
&blocks.join("\n"),
max_chars,
hub,
&mut artifacts,
)));
}
out.extend(artifacts);
out.extend(images);
out
}
fn effects_line(tally: &CellEffects) -> String {
let counts = tally
.counts
.iter()
.map(|(cap, n)| format!("{n} {cap}"))
.collect::<Vec<_>>()
.join(", ");
let denied = if tally.denied > 0 {
format!(" ({} denied)", tally.denied)
} else {
String::new()
};
if counts.is_empty() {
return format!(
"effects: cell {}: {} denied",
tally.cell_index, tally.denied
);
}
format!("effects: cell {}: {}{denied}", tally.cell_index, counts)
}
fn fit_to_cap(
body: &str,
max_chars: Option<usize>,
hub: Option<&ArtifactHub>,
artifacts: &mut Vec<Content>,
) -> String {
let Some(max_chars) = max_chars else {
return body.to_string();
};
if body.chars().count() <= max_chars {
return body.to_string();
}
let Some(r) = hub.and_then(|h| h.store().spill("observation", body).ok()) else {
return truncate_output(body, Some(max_chars));
};
let line = artifact_line(&r.capture_id, &r.kind, r.bytes, &r.summary);
if line.chars().count() + 1 >= max_chars {
return truncate_output(body, Some(max_chars));
}
artifacts.push(Content::artifact(
r.capture_id.clone(),
r.kind.clone(),
r.summary.clone(),
));
let budget = max_chars - line.chars().count() - 1;
format!("{}\n{}", truncate_output(body, Some(budget)), line)
}
fn image_content(image: &ImageRef) -> Content {
let bytes = match &image.source {
ImageSource::Inline(bytes) => Ok(bytes.clone()),
ImageSource::Path(path) => std::fs::read(path),
};
match bytes {
Ok(bytes) => {
let (bytes, mime) = wire_image_bytes(bytes, image.mime);
Content::image(
base64::engine::general_purpose::STANDARD.encode(bytes),
mime,
)
}
Err(e) => Content::text(format!("image \"{}\": bytes unavailable ({e})", image.alt)),
}
}
const IMAGE_TRANSCODE_OVER_BYTES: usize = 100 * 1024;
const IMAGE_JPEG_QUALITY: u8 = 85;
fn wire_image_bytes(bytes: Vec<u8>, mime: &'static str) -> (Vec<u8>, &'static str) {
if bytes.len() <= IMAGE_TRANSCODE_OVER_BYTES {
return (bytes, mime);
}
let Ok(img) = image::load_from_memory(&bytes) else {
return (bytes, mime);
};
let mut out = Vec::new();
let encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut out, IMAGE_JPEG_QUALITY);
if img.to_rgb8().write_with_encoder(encoder).is_err() || out.len() >= bytes.len() {
return (bytes, mime);
}
(out, "image/jpeg")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::script_tool::TRUNCATION_MARKER;
use codexec::observation::ExecError;
use phi_cap::Capability;
#[test]
fn inline_image_bytes_ride_content() {
let image = ImageRef::inline("image/png", vec![1, 2, 3], "three bytes");
match image_content(&image) {
Content::Image { data, mime_type } => {
assert_eq!(mime_type, "image/png");
assert_eq!(
data,
base64::engine::general_purpose::STANDARD.encode([1u8, 2, 3])
);
}
other => panic!("expected image content, got {other:?}"),
}
}
#[test]
fn a_failed_image_read_becomes_an_honest_sentence() {
let image = ImageRef::from_path("image/png", "/nonexistent/never.png", "a shot of nothing");
match image_content(&image) {
Content::Text { text } => {
assert!(
text.contains("a shot of nothing"),
"alt must survive: {text}"
);
assert!(text.contains("bytes unavailable"), "{text}");
}
other => panic!("expected honest text, got {other:?}"),
}
}
fn noisy_png(width: u32, height: u32) -> Vec<u8> {
let mut rgb = image::RgbImage::new(width, height);
let mut state = 0x2545_f491_4f6c_dd1du64;
for p in rgb.pixels_mut() {
state = state
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
*p = image::Rgb([
(state >> 33) as u8,
(state >> 41) as u8,
(state >> 49) as u8,
]);
}
let mut buf = Vec::new();
rgb.write_with_encoder(image::codecs::png::PngEncoder::new(&mut buf))
.unwrap();
buf
}
#[test]
fn a_large_png_goes_on_the_wire_as_a_smaller_jpeg_of_the_same_size() {
let png = noisy_png(900, 600);
assert!(
png.len() > IMAGE_TRANSCODE_OVER_BYTES,
"fixture must exceed the threshold, got {} bytes",
png.len()
);
let image = ImageRef::inline("image/png", png.clone(), "a noisy screenshot");
match image_content(&image) {
Content::Image { data, mime_type } => {
assert_eq!(mime_type, "image/jpeg", "the wire copy is a JPEG");
let jpeg = base64::engine::general_purpose::STANDARD
.decode(data)
.unwrap();
assert!(
jpeg.len() < png.len(),
"transcode must shrink: {} vs {} original bytes",
jpeg.len(),
png.len()
);
let decoded = image::load_from_memory(&jpeg).unwrap();
assert_eq!(
(decoded.width(), decoded.height()),
(900, 600),
"geometry is what the model's coordinate math relies on — only the codec may change"
);
}
other => panic!("expected image content, got {other:?}"),
}
}
#[test]
fn a_small_picture_keeps_its_exact_bytes() {
let mut buf = Vec::new();
image::RgbImage::new(16, 16)
.write_with_encoder(image::codecs::png::PngEncoder::new(&mut buf))
.unwrap();
assert!(buf.len() <= IMAGE_TRANSCODE_OVER_BYTES);
let image = ImageRef::inline("image/png", buf.clone(), "a tiny icon");
match image_content(&image) {
Content::Image { data, mime_type } => {
assert_eq!(mime_type, "image/png", "under the budget: untouched");
assert_eq!(data, base64::engine::general_purpose::STANDARD.encode(buf));
}
other => panic!("expected image content, got {other:?}"),
}
}
#[test]
fn undecodable_oversized_bytes_still_reach_the_model() {
let garbage = vec![0xABu8; IMAGE_TRANSCODE_OVER_BYTES + 1];
let image = ImageRef::inline("image/png", garbage.clone(), "a corrupt capture");
match image_content(&image) {
Content::Image { data, mime_type } => {
assert_eq!(
mime_type, "image/png",
"no silent re-label when nothing was re-encoded"
);
assert_eq!(
data,
base64::engine::general_purpose::STANDARD.encode(garbage),
"a picture the codec cannot parse is still better than a silent loss"
);
}
other => panic!("expected image content, got {other:?}"),
}
}
#[test]
fn an_empty_observation_says_so() {
let out = render(Observation::empty(), None, None);
assert_eq!(out.len(), 1);
assert_eq!(content_text(&out), "(no output)");
}
#[test]
fn an_oversized_observation_is_truncated_not_rejected() {
let obs = Observation {
stdout: format!("HEAD\n{}\nTAIL", "x".repeat(30_000)),
stderr: "a warning from the tail side".to_string(),
error: Some(ExecError::CellError {
index: 0,
kind: "ValueError".to_string(),
message: "boom".to_string(),
}),
events: vec![CellEvent::TodoChanged {
id: "1".to_string(),
status: "done".to_string(),
}],
..Observation::empty()
};
let out = render(obs, Some(16_000), None);
let text = content_text(&out);
assert!(
text.chars().count() <= 16_000,
"must fit the cap, got {} chars",
text.chars().count()
);
assert!(text.contains(TRUNCATION_MARKER), "{text:.60}");
assert!(text.starts_with("HEAD"), "{text:.60}");
for tail_piece in [
"TAIL",
"a warning from the tail side",
"error in block #0 (ValueError): boom",
"todo 1: done",
] {
assert!(text.contains(tail_piece), "lost {tail_piece:?}: {text:.60}");
}
}
#[test]
fn a_small_observation_rides_whole() {
let obs = Observation {
stdout: "42".to_string(),
..Observation::empty()
};
let out = render(obs, Some(16_000), None);
assert_eq!(content_text(&out), "42");
}
#[test]
fn the_effect_tally_and_progress_trail_reach_the_model() {
let obs = Observation {
progress: vec![
"[0.3s] downloaded 40%".to_string(),
"[1.2s] still running".to_string(),
],
effects: vec![
CellEffects {
cell_index: 0,
counts: vec![(Capability::ReadFs, 2), (Capability::WriteFs, 1)],
denied: 1,
},
CellEffects {
cell_index: 1,
counts: vec![(Capability::Net, 1)],
denied: 0,
},
],
..Observation::empty()
};
let text = content_text(&render(obs, None, None));
assert!(
text.contains("progress:\n[0.3s] downloaded 40%\n[1.2s] still running"),
"the trail rides in tick order: {text}"
);
assert!(
text.contains("effects: cell 0: 2 read_fs, 1 write_fs (1 denied)"),
"counts per capability, denials marked: {text}"
);
assert!(
text.contains("effects: cell 1: 1 net"),
"a clean cell shows no denial rider: {text}"
);
}
#[test]
fn a_denied_only_cell_says_so_in_one_line() {
let obs = Observation {
effects: vec![CellEffects {
cell_index: 2,
counts: Vec::new(),
denied: 3,
}],
..Observation::empty()
};
let text = content_text(&render(obs, None, None));
assert!(
text.contains("effects: cell 2: 3 denied"),
"no dangling separator when nothing ran: {text}"
);
}
#[test]
fn images_survive_the_truncation() {
let obs = Observation {
stdout: "x".repeat(30_000),
events: vec![CellEvent::Image(ImageRef::inline(
"image/png",
vec![1, 2, 3],
"three bytes",
))],
..Observation::empty()
};
let out = render(obs, Some(500), None);
assert!(
out.iter().any(|c| matches!(c, Content::Image { .. })),
"the typed channel must keep the image"
);
assert!(content_text(&out).chars().count() <= 500);
}
#[test]
fn an_oversized_observation_spills_and_keeps_a_reference_line() {
let tmp = tempfile::TempDir::new().unwrap();
let hub = ArtifactHub::new(tmp.path().join("captures"));
let body = format!("HEAD\n{}\nTAIL", "x".repeat(30_000));
let obs = Observation {
stdout: body.clone(),
..Observation::empty()
};
let out = render(obs, Some(16_000), Some(&hub));
let text = content_text(&out);
assert!(
text.chars().count() <= 16_000,
"must fit the cap, got {} chars",
text.chars().count()
);
assert!(text.starts_with("HEAD"), "{text:.80}");
assert!(
text.contains("TAIL"),
"the tail preview must survive: {text:.120}"
);
assert!(
text.contains("obs.fetch('art_0001') for the full body"),
"the way back must be on the pane: {text:.200}"
);
assert_eq!(
hub.store().fetch("art_0001").as_deref(),
Some(body.as_str()),
"the spill must be the full body"
);
assert!(
out.iter().any(
|c| matches!(c, Content::Artifact { capture_id, .. } if capture_id == "art_0001")
),
"the typed face rides alongside: {out:?}"
);
}
#[test]
fn a_spilled_reference_event_renders_line_and_typed_face() {
let obs = Observation {
events: vec![CellEvent::Artifact {
capture_id: "art_0001".to_string(),
kind: "stdout".to_string(),
bytes: 11,
summary: "first body".to_string(),
}],
..Observation::empty()
};
let out = render(obs, None, None);
let text = content_text(&out);
assert!(
text.contains(
"artifact art_0001 (stdout, 11 bytes): first body — obs.fetch('art_0001') for the full body"
),
"{text}"
);
assert!(
out.iter().any(|c| matches!(c, Content::Artifact { .. })),
"{out:?}"
);
assert_eq!(text.matches("first body").count(), 1, "{text}");
}
fn content_text(contents: &[Content]) -> String {
contents
.iter()
.filter_map(|c| match c {
Content::Text { text } => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n")
}
}