use eclipse_core::message::Role;
use eclipse_core::session::SessionStore;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Retry {
Ask(String),
Refused(String),
}
pub fn target(store: &SessionStore, session: Option<&str>) -> Retry {
let Some(id) = session else {
return Retry::Refused("nothing to retry: this session has no turns yet".into());
};
let records = match store.load_records(id) {
Ok(records) => records,
Err(e) => return Retry::Refused(format!("nothing to retry: {e}")),
};
if let Some(last) = records.last() {
if last.message.role == Role::Assistant && !last.message.tool_calls.is_empty() {
return Retry::Refused(
"the last run is parked on a tool call: answer the approval prompt, then /retry"
.into(),
);
}
}
match records
.iter()
.rev()
.find(|r| r.message.role == Role::User)
.map(|r| r.message.content.clone().unwrap_or_default())
{
Some(text) if !text.trim().is_empty() => Retry::Ask(text),
Some(_) => Retry::Refused("nothing to retry: the last question was empty".into()),
None => Retry::Refused("nothing to retry: no question in this session yet".into()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use eclipse_core::message::{Message, ToolCall};
fn store() -> (SessionStore, tempfile::TempDir) {
let dir = tempfile::tempdir().unwrap();
let store = SessionStore::new(dir.path());
(store, dir)
}
fn tool_call() -> ToolCall {
ToolCall {
id: "call_1".into(),
kind: "function".into(),
function: eclipse_core::message::FunctionCall {
name: "shell".into(),
arguments: "{}".into(),
},
}
}
#[test]
fn a_session_with_only_a_question_retries_that_question() {
let (store, _dir) = store();
store
.append("s", &Message::user("why is the sky blue"))
.unwrap();
assert_eq!(
target(&store, Some("s")),
Retry::Ask("why is the sky blue".into())
);
}
#[test]
fn the_newest_question_is_found_under_the_debris_of_a_failed_turn() {
let (store, _dir) = store();
store.append("s", &Message::user("first")).unwrap();
store.append("s", &Message::assistant("an answer")).unwrap();
store.append("s", &Message::user("second")).unwrap();
store
.append("s", &Message::tool_result("call_1", "boom"))
.unwrap();
assert_eq!(target(&store, Some("s")), Retry::Ask("second".into()));
}
#[test]
fn a_run_parked_on_a_tool_call_is_refused_and_named() {
let (store, _dir) = store();
store.append("s", &Message::user("do it")).unwrap();
store
.append("s", &Message::assistant_tool_calls(None, vec![tool_call()]))
.unwrap();
match target(&store, Some("s")) {
Retry::Refused(why) => {
assert!(why.contains("parked on a tool call"), "{why}");
assert!(
why.contains("approval"),
"and says where to answer it: {why}"
);
}
other => panic!("a parked run must be refused: {other:?}"),
}
}
#[test]
fn no_session_and_no_question_are_two_different_refusals() {
let (store, _dir) = store();
assert!(matches!(target(&store, None), Retry::Refused(_)));
store.append("s", &Message::assistant("hi")).unwrap();
match target(&store, Some("s")) {
Retry::Refused(why) => assert!(why.contains("no question"), "{why}"),
other => panic!("{other:?}"),
}
assert!(matches!(target(&store, Some("ghost")), Retry::Refused(_)));
}
}