use std::collections::HashMap;
use super::message::{Role, text_of};
use super::projection::REDACTED;
use super::{Error, Turn};
use crate::pyfmt::dumps;
use crate::state::State;
use crate::text::{head, len, strip, tail};
pub const RESULT_CHARS: usize = 4096;
fn omitted(count: usize, call: &str) -> String {
format!(
" … [{count} chars omitted; pass call=\"{call}\" to recall_turn to read this result whole] … "
)
}
fn paged(text: &str, call: Option<&str>, limit: usize) -> String {
let total = len(text);
let Some(call) = call.filter(|_| total > limit) else {
return text.to_string();
};
let half = limit / 2;
format!(
"{}{}{}",
head(text, half),
omitted(total - 2 * half, call),
tail(text, total - half)
)
}
fn args(arguments: &serde_json::Value) -> String {
dumps(arguments, true)
}
pub fn render_turn(turn: &Turn, result_chars: usize) -> String {
if turn.redacted {
return format!("Turn {}: {REDACTED}", turn.turn);
}
let names: HashMap<&str, &str> = turn
.messages
.iter()
.flat_map(|message| &message.tool_calls)
.map(|call| (call.id.as_str(), call.name.as_str()))
.collect();
let mut lines = vec![format!("Turn {} (verbatim):", turn.turn)];
for message in &turn.messages {
match message.role {
Role::User => lines.push(format!("USER: {}", text_of(message))),
Role::Assistant => {
let prose = text_of(message);
let prose = strip(&prose);
if !prose.is_empty() {
lines.push(format!("AGENT: {prose}"));
}
for call in &message.tool_calls {
lines.push(format!(
"AGENT calls {}({})",
call.name,
args(&call.arguments)
));
}
}
Role::Tool => {
let call_id = message.tool_call_id.as_deref();
let name = call_id.map_or("tool", |id| names.get(id).copied().unwrap_or("tool"));
let result = paged(&text_of(message), call_id, result_chars);
lines.push(format!("TOOL {name} → {result}"));
}
Role::System => {}
}
}
lines.join("\n")
}
pub fn call_ids(turn: &Turn) -> Vec<&str> {
turn.messages
.iter()
.flat_map(|message| &message.tool_calls)
.map(|call| call.id.as_str())
.collect()
}
pub fn render_call(turn: &Turn, call: &str) -> Option<String> {
let results: HashMap<&str, String> = turn
.messages
.iter()
.filter(|message| message.role == Role::Tool)
.map(|message| {
(
message.tool_call_id.as_deref().unwrap_or(""),
text_of(message).into_owned(),
)
})
.collect();
turn.messages
.iter()
.flat_map(|message| &message.tool_calls)
.find(|made| made.id == call)
.map(|made| {
let result = results.get(made.id.as_str()).map_or("?", String::as_str);
format!(
"Turn {}, call {call} (verbatim):\nAGENT calls {}({})\nTOOL {} → {result}",
turn.turn,
made.name,
args(&made.arguments),
made.name
)
})
}
pub struct Corrective {
pub error: String,
pub hint: Option<String>,
}
pub fn recall(
store: &dyn State,
target: &str,
turn: i64,
label: &str,
call: Option<&str>,
) -> Result<Result<String, Corrective>, Error> {
if turn < 1 {
return Ok(Err(Corrective {
error: format!("Turn numbers start at 1; got {turn}"),
hint: Some(
"Turn numbers appear in square brackets in the conversation log, e.g. [7] or [3-6]."
.to_string(),
),
}));
}
let (mut kept, mut last) = (None, 0);
let read = store.read_turns_each(target, 0, None, &mut |found| {
last = found.turn;
if kept.is_none() && found.turn as i64 == turn {
kept = Some(found);
}
Ok(true)
});
match read {
Ok(()) => {}
Err(Error::Conversation(refused)) => {
return Ok(Err(Corrective {
error: format!("[{}] {}", refused.code, refused.message),
hint: None,
}));
}
Err(error) => return Err(error),
}
let Some(found) = kept.as_ref() else {
return Ok(Err(Corrective {
error: format!("Turn {turn} does not exist"),
hint: Some(format!("{label} has turns 1-{last}.")),
}));
};
let Some(call) = call else {
return Ok(Ok(render_turn(found, RESULT_CHARS)));
};
Ok(match render_call(found, call) {
Some(text) => Ok(text),
None => Err(Corrective {
error: format!("Turn {turn} made no call {}", crate::repr::repr(call)),
hint: Some(format!(
"Its calls: {}.",
match call_ids(found).join(", ") {
ids if ids.is_empty() => "none".to_string(),
ids => ids,
}
)),
}),
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::conversation::message::from_json;
use crate::store::time::Stamp;
use serde_json::json;
fn turn(messages: Vec<serde_json::Value>) -> Turn {
Turn {
conversation_id: "s1".to_string(),
turn: 2,
messages: messages.iter().map(|m| from_json(m).unwrap()).collect(),
created_at: Stamp::parse("2026-10-12T22:27:56Z").unwrap(),
actor: None,
redacted: false,
}
}
#[test]
fn a_turn_renders_verbatim_with_a_long_result_paged() {
let long = "x".repeat(5000);
let made = turn(vec![
json!({"role": "user", "content": "do it é", "reasoning": null, "tool_calls": [], "tool_call_id": null}),
json!({"role": "assistant", "content": " thinking ", "reasoning": null, "tool_calls": [{"id": "t1", "name": "Read", "arguments": {"path": "/a b", "n": 1}}], "tool_call_id": null}),
json!({"role": "tool", "content": long, "reasoning": null, "tool_calls": [], "tool_call_id": "t1"}),
json!({"role": "tool", "content": "orphan", "reasoning": null, "tool_calls": [], "tool_call_id": null}),
json!({"role": "assistant", "content": "done", "reasoning": null, "tool_calls": [], "tool_call_id": null}),
]);
let text = render_turn(&made, RESULT_CHARS);
let lines: Vec<&str> = text.lines().collect();
assert_eq!(lines[0], "Turn 2 (verbatim):");
assert_eq!(lines[1], "USER: do it é");
assert_eq!(lines[2], "AGENT: thinking");
assert_eq!(lines[3], "AGENT calls Read({\"path\":\"/a b\",\"n\":1})");
assert!(lines[4].starts_with(&format!("TOOL Read → {}", "x".repeat(2048))));
assert!(lines[4].contains(
" … [904 chars omitted; pass call=\"t1\" to recall_turn to read this result whole] … "
));
assert!(lines[4].ends_with(&"x".repeat(2048)));
assert_eq!(lines[5], "TOOL tool → orphan");
assert_eq!(lines[6], "AGENT: done");
assert_eq!(call_ids(&made), ["t1"]);
let whole = render_call(&made, "t1").unwrap();
assert!(whole.starts_with("Turn 2, call t1 (verbatim):\nAGENT calls Read({\"path\":\"/a b\",\"n\":1})\nTOOL Read → xxxx"));
assert!(whole.ends_with(&"x".repeat(5000)));
assert!(render_call(&made, "t9").is_none());
let mut blanked = turn(vec![]);
blanked.redacted = true;
assert_eq!(render_turn(&blanked, RESULT_CHARS), "Turn 2: [redacted]");
}
}