use crate::budget::{self, Limits};
use crate::dsl::Call;
use crate::error::AgentError;
use crate::eval::{judge_request, judge_verdict, repair_message};
use crate::io::{AgentIo, ModelHandle};
use crate::persona::{open_trace, Persona, SavedTrace};
use crate::schema::{parse_json, validate};
use futures_util::future::join_all;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::Instant;
use troupekit_core::event::{Event, EventBody, PauseReason, Usage};
use troupekit_core::ids::RunId;
use troupekit_core::message::{ContentPart, Message, Role};
use troupekit_core::store::{Checkpoint, CheckpointStore};
use troupekit_providers::{CompletionRequest, CompletionResponse, Delta, StopReason};
use troupekit_tools::{ToolContext, ToolOutput};
use troupekit_trace::{content, trace, trace_span, Trace, TraceSpec};
pub const RUN_FORMAT: u32 = 1;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PendingCall {
pub id: String,
pub name: String,
pub arguments: Value,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum RunState {
CallModel,
CallTools {
calls: Vec<PendingCall>,
},
Check {
answer: String,
},
Repair {
errors: Vec<String>,
},
Done {
output: Value,
},
Failed {
error: String,
},
Paused {
reason: PauseReason,
resume: Box<RunState>,
},
}
impl RunState {
fn step_name(&self) -> &'static str {
match self {
RunState::CallModel => "call_model",
RunState::CallTools { .. } => "call_tools",
RunState::Check { .. } => "check",
RunState::Repair { .. } => "repair",
RunState::Done { .. } | RunState::Failed { .. } | RunState::Paused { .. } => {
unreachable!("only working states take steps")
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum StepOutcome {
Continue,
Retry {
error: String,
retry_after_ms: Option<u64>,
},
Paused(PauseReason),
Done(Value),
Failed(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct RunSpec {
persona: String,
model: String,
system: String,
tools: Vec<String>,
evals: Vec<Call>,
output: Option<Value>,
max_turns: u32,
max_tokens: u32,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AgentRun {
format: u32,
run_id: RunId,
spec: RunSpec,
limits: Limits,
messages: Vec<Message>,
state: RunState,
model_calls: u32,
repaired: bool,
usage: Usage,
seq: u64,
steps: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
trace: Option<SavedTrace>,
}
impl AgentRun {
pub async fn new(
persona: &Persona,
input: Vec<Message>,
limits: Limits,
io: &dyn AgentIo,
) -> Result<AgentRun, AgentError> {
let setup = |m: String| AgentError::Setup(format!("{}: {m}", persona.source));
if input.is_empty() {
return Err(setup("a run needs at least one input message".into()));
}
if persona.instructions.trim().is_empty() {
return Err(setup("instructions are not loaded".into()));
}
if let Some(t) = persona.tools.iter().find(|t| io.tools().get(t).is_none()) {
return Err(setup(format!("tool `{t}` is not registered")));
}
let mut models = vec![persona.model.clone()];
for e in &persona.evals {
if e.name == "judge" {
models.extend(e.args.get(2).and_then(Value::as_str).map(str::to_string));
} else if io.evals().get(&e.name).is_none() {
return Err(setup(format!("eval `{}` is not registered", e.name)));
}
}
for m in &models {
io.model(m).await.map_err(|e| setup(e.to_string()))?;
}
Ok(AgentRun {
format: RUN_FORMAT,
run_id: RunId::new(),
spec: RunSpec {
persona: persona.name.clone(),
model: persona.model.clone(),
system: persona.system_prompt(),
tools: persona.tools.clone(),
evals: persona.evals.clone(),
output: persona.output.clone(),
max_turns: persona.max_turns,
max_tokens: persona.max_tokens,
},
limits: limits.with_budget(&persona.budget),
messages: input,
state: RunState::CallModel,
model_calls: 0,
repaired: false,
usage: Usage::default(),
seq: 0,
steps: 0,
trace: None,
})
}
pub fn from_checkpoint(cp: &Checkpoint) -> Result<AgentRun, AgentError> {
let run: AgentRun = serde_json::from_value(cp.state.clone())
.map_err(|e| AgentError::Checkpoint(e.to_string()))?;
if run.format != RUN_FORMAT {
return Err(AgentError::Checkpoint(format!(
"run format {} is not {RUN_FORMAT}",
run.format
)));
}
if run.run_id != cp.run_id || run.steps != cp.step {
return Err(AgentError::Checkpoint(
"checkpoint header does not match its state".into(),
));
}
if let Some(saved) = &run.trace {
saved.spec().map_err(AgentError::Checkpoint)?;
}
Ok(run)
}
pub fn checkpoint(&self) -> Checkpoint {
Checkpoint {
run_id: self.run_id,
step: self.steps,
state: serde_json::to_value(self).expect("run state serializes"),
}
}
pub fn run_id(&self) -> RunId {
self.run_id
}
pub fn state(&self) -> &RunState {
&self.state
}
pub fn messages(&self) -> &[Message] {
&self.messages
}
pub fn usage(&self) -> Usage {
self.usage
}
pub fn model_calls(&self) -> u32 {
self.model_calls
}
pub fn resume(&mut self, limits: Limits) -> Result<(), AgentError> {
let RunState::Paused { resume, .. } = &self.state else {
return Err(AgentError::Setup("run is not paused".into()));
};
self.state = (**resume).clone();
self.limits = limits;
Ok(())
}
pub fn set_trace(&mut self, spec: TraceSpec) {
self.trace = Some(SavedTrace::from_spec(&spec));
assert_eq!(self.trace(), Some(spec));
}
pub fn trace(&self) -> Option<TraceSpec> {
self.trace
.as_ref()
.map(|saved| saved.spec().expect("trace spec checked when set or loaded"))
}
pub async fn step(&mut self, io: &dyn AgentIo) -> StepOutcome {
let working = !matches!(
self.state,
RunState::Done { .. } | RunState::Failed { .. } | RunState::Paused { .. }
);
let Some(spec) = self.trace().filter(|_| working) else {
return self.step_once(io).await;
};
if let Some(scope) = troupekit_trace::current() {
let nested = scope.nest(spec.categories(), None);
return nested.in_scope(self.step_once(io)).await;
}
match open_trace(&spec, &self.spec.persona, self.run_id) {
Ok(tracer) => tracer.in_scope(self.step_once(io)).await,
Err(_) => self.step_once(io).await,
}
}
async fn step_once(&mut self, io: &dyn AgentIo) -> StepOutcome {
match &self.state {
RunState::Done { output } => return StepOutcome::Done(output.clone()),
RunState::Failed { error } => return StepOutcome::Failed(error.clone()),
RunState::Paused { reason, .. } => return StepOutcome::Paused(reason.clone()),
_ => {}
}
if self.seq == 0 {
self.emit(
io,
EventBody::RunStarted {
workflow: self.spec.persona.clone(),
},
);
}
self.steps += 1;
let name = self.state.step_name();
let mut span = trace_span!(
Trace::Agents,
"agent.step",
run = self.run_id.to_string(),
state = name,
persona = self.spec.persona.as_str(),
);
self.emit(
io,
EventBody::StepStarted {
step: name.into(),
persona: None,
},
);
let mut used = Usage::default();
let outcome = span
.instrument(async {
match self.state.clone() {
RunState::CallModel => self.call_model(io, &mut used).await,
RunState::CallTools { calls } => self.call_tools(io, &calls).await,
RunState::Check { answer } => self.check(io, &answer, &mut used).await,
RunState::Repair { errors } => {
self.messages.push(repair_message(&errors));
self.state = RunState::CallModel;
StepOutcome::Continue
}
RunState::Done { .. } | RunState::Failed { .. } | RunState::Paused { .. } => {
unreachable!("handled above")
}
}
})
.await;
if let StepOutcome::Failed(error) = &outcome {
span.fail("step failed");
span.record("error", content(error.as_str()));
}
self.emit(
io,
EventBody::StepFinished {
step: name.into(),
usage: used,
},
);
match &outcome {
StepOutcome::Paused(reason) => self.emit(
io,
EventBody::RunPaused {
reason: reason.clone(),
},
),
StepOutcome::Done(_) => self.emit(io, EventBody::RunFinished { usage: self.usage }),
StepOutcome::Failed(error) => self.emit(
io,
EventBody::RunFailed {
error: error.clone(),
},
),
StepOutcome::Continue | StepOutcome::Retry { .. } => {}
}
outcome
}
fn emit(&mut self, io: &dyn AgentIo, body: EventBody) {
io.events().emit(Event {
run_id: self.run_id,
seq: self.seq,
at_ms: io.clock().now_ms(),
body,
});
self.seq += 1;
}
fn fail(&mut self, error: String) -> StepOutcome {
self.state = RunState::Failed {
error: error.clone(),
};
StepOutcome::Failed(error)
}
fn request(&self, io: &dyn AgentIo) -> CompletionRequest {
let tools = io
.tools()
.specs()
.into_iter()
.filter(|s| self.spec.tools.contains(&s.name))
.filter_map(|mut s| {
if let Some(h) = s.hosted {
let used: u64 = self.messages.iter().map(|m| m.hosted_calls(h.name())).sum();
let left = u64::from(h.max_uses()).saturating_sub(used);
if left == 0 {
return None;
}
s.hosted = Some(
h.with_max_uses(u32::try_from(left).expect("left is at most max_uses")),
);
}
Some(s)
})
.collect();
CompletionRequest {
model: None,
system: Some(self.spec.system.clone()),
messages: self.messages.clone(),
tools,
max_tokens: self.spec.max_tokens,
temperature: None,
output_schema: self.spec.output.clone(),
}
}
async fn call_model(&mut self, io: &dyn AgentIo, used: &mut Usage) -> StepOutcome {
let held = self.model_calls < self.spec.max_turns;
trace!(
Trace::Laws,
"law.check",
law = "step_keeps_model_calls_within_max_turns",
held = held,
model_calls = self.model_calls,
max_turns = self.spec.max_turns,
);
if !held {
return self.fail(format!("max_turns {} reached", self.spec.max_turns));
}
let handle = match io.model(&self.spec.model).await {
Ok(h) => h,
Err(e) => return self.fail(e.to_string()),
};
let req = self.request(io);
let reply = match self
.call(io, &handle, &req, true, used, RunState::CallModel)
.await
{
Ok(r) => r,
Err(outcome) => return outcome,
};
self.model_calls += 1;
let calls: Vec<PendingCall> = reply
.message
.tool_calls()
.map(|(id, name, arguments)| PendingCall {
id: id.into(),
name: name.into(),
arguments: arguments.clone(),
})
.collect();
let answer = reply.message.final_text();
let kept = reply
.message
.parts
.iter()
.any(|p| !matches!(p, ContentPart::Text { text } if text.trim().is_empty()));
if kept {
self.messages.push(reply.message);
}
self.state = if reply.stop == StopReason::PauseTurn {
RunState::CallModel
} else if calls.is_empty() {
RunState::Check { answer }
} else {
RunState::CallTools { calls }
};
StepOutcome::Continue
}
async fn call(
&mut self,
io: &dyn AgentIo,
handle: &ModelHandle,
req: &CompletionRequest,
stream: bool,
used: &mut Usage,
resume: RunState,
) -> Result<CompletionResponse, StepOutcome> {
let estimate = budget::estimate(req, &handle.price);
match budget::check(
&self.limits,
&handle.price,
self.usage.cost_usd_micros,
estimate,
io.ledger(),
)
.await
{
Err(e) => {
return Err(StepOutcome::Retry {
error: format!("usage ledger: {e}"),
retry_after_ms: None,
})
}
Ok(Some(layer)) => {
let reason = PauseReason::Budget {
limit: layer.into(),
};
self.state = RunState::Paused {
reason: reason.clone(),
resume: Box::new(resume),
};
return Err(StepOutcome::Paused(reason));
}
Ok(None) => {}
}
let mut span = trace_span!(
Trace::Models,
"model.call",
model = handle.model.id(),
stream = stream,
messages = content(serde_json::to_string(&req.messages).expect("messages serialize")),
);
let started = span.is_enabled().then(Instant::now);
let mut first_delta: Option<Instant> = None;
let result = if stream {
let (sink, clock, run_id, seq) = (io.events(), io.clock(), self.run_id, &mut self.seq);
let first = &mut first_delta;
let timed = started.is_some();
let mut on_delta = |d: &Delta| {
if timed {
first.get_or_insert_with(Instant::now);
}
if let Delta::TextDelta { text } = d {
sink.emit(Event {
run_id,
seq: *seq,
at_ms: clock.now_ms(),
body: EventBody::TextDelta { text: text.clone() },
});
*seq += 1;
}
};
span.instrument(handle.model.stream(req, &mut on_delta)).await
} else {
span.instrument(handle.model.complete(req)).await
};
if let (Some(started), Some(at)) = (started, first_delta) {
span.record("ttft_us", at.saturating_duration_since(started).as_micros() as u64);
}
if let Err(e) = &result {
span.fail("model call failed");
span.record("retryable", e.is_retryable());
span.record("error", content(e.to_string()));
}
let mut reply = match result {
Ok(r) => r,
Err(e) if e.is_retryable() => {
return Err(StepOutcome::Retry {
error: e.to_string(),
retry_after_ms: e.retry_after_ms,
})
}
Err(e) => return Err(self.fail(e.to_string())),
};
reply.usage.cost_usd_micros = handle.price.cost_usd_micros(&reply.usage);
if span.is_enabled() {
span.record("input_tokens", reply.usage.input_tokens);
span.record("output_tokens", reply.usage.output_tokens);
span.record("cost_usd_micros", reply.usage.cost_usd_micros);
span.record("stop", format!("{:?}", reply.stop));
span.record("reply", content(reply.message.text_content()));
}
drop(span);
self.usage += reply.usage;
*used += reply.usage;
if let Err(e) = budget::record(&self.limits, io.ledger(), reply.usage).await {
return Err(self.fail(format!("usage ledger: {e}")));
}
Ok(reply)
}
async fn call_tools(&mut self, io: &dyn AgentIo, calls: &[PendingCall]) -> StepOutcome {
assert!(!calls.is_empty(), "a tool step has at least one call");
for c in calls {
self.emit(
io,
EventBody::ToolCallStarted {
call_id: c.id.clone(),
name: c.name.clone(),
},
);
}
let (run_id, step) = (self.run_id, self.steps);
let allowed = &self.spec.tools;
let outputs = join_all(calls.iter().enumerate().map(|(i, c)| {
let ctx = ToolContext::for_call(run_id, step, i);
async move {
let mut span = trace_span!(
Trace::Tools,
"tool.call",
name = c.name.as_str(),
call_id = c.id.as_str(),
args = content(c.arguments.to_string()),
);
let out = if allowed.contains(&c.name) {
let call = io.tools().call(&c.name, c.arguments.clone(), &ctx);
ToolOutput::from_result(span.instrument(call).await)
} else {
ToolOutput::error(format!("tool `{}` is not allowed for this agent", c.name))
};
if span.is_enabled() {
span.record("is_error", out.is_error);
span.record("output", content(out.content.as_str()));
}
out
}
}))
.await;
assert_eq!(outputs.len(), calls.len());
let mut parts = Vec::with_capacity(calls.len());
for (c, out) in calls.iter().zip(outputs) {
self.emit(
io,
EventBody::ToolCallFinished {
call_id: c.id.clone(),
is_error: out.is_error,
},
);
parts.push(ContentPart::ToolResult {
call_id: c.id.clone(),
content: out.content,
is_error: out.is_error,
});
}
self.messages
.push(Message::new(Role::Tool, parts).expect("tool results form a valid tool message"));
self.state = RunState::CallModel;
StepOutcome::Continue
}
async fn check(&mut self, io: &dyn AgentIo, answer: &str, used: &mut Usage) -> StepOutcome {
let (output, mut errors) = match &self.spec.output {
None => (Value::String(answer.to_string()), Vec::new()),
Some(schema) => match parse_json(answer) {
Err(e) => (Value::Null, vec![e]),
Ok(v) => {
let errs = validate(schema, &v);
(v, errs)
}
},
};
if errors.is_empty() {
for call in self.spec.evals.clone() {
if call.name == "judge" {
match self.judge(io, &call, &output, answer, used).await {
Ok(None) => {}
Ok(Some(reason)) => errors.push(format!("judge: {reason}")),
Err(outcome) => return outcome,
}
continue;
}
let Some(f) = io.evals().get(&call.name) else {
return self.fail(format!("eval `{}` is not registered", call.name));
};
let mut span = trace_span!(Trace::Evals, "eval.run", name = call.name.as_str());
let passed = f(&output, &call.args);
span.record("passed", passed.is_ok());
if let Err(e) = passed {
errors.push(format!("{}: {e}", call.name));
}
}
}
if errors.is_empty() {
self.state = RunState::Done {
output: output.clone(),
};
return StepOutcome::Done(output);
}
if !self.repaired && self.model_calls < self.spec.max_turns {
self.repaired = true;
self.state = RunState::Repair { errors };
return StepOutcome::Continue;
}
self.fail(errors.join("; "))
}
async fn judge(
&mut self,
io: &dyn AgentIo,
call: &Call,
output: &Value,
answer: &str,
used: &mut Usage,
) -> Result<Option<String>, StepOutcome> {
let criteria = call
.args
.first()
.and_then(Value::as_str)
.expect("judge criteria checked at persona load");
let min_score = call
.args
.get(1)
.and_then(Value::as_f64)
.expect("judge min_score checked at persona load");
let selector = call
.args
.get(2)
.and_then(Value::as_str)
.unwrap_or(self.spec.model.as_str())
.to_string();
let mut span = trace_span!(
Trace::Evals,
"eval.judge",
name = "judge",
selector = selector.as_str(),
);
let handle = match io.model(&selector).await {
Ok(h) => h,
Err(e) => {
span.fail(e.to_string());
return Err(self.fail(format!("judge: {e}")));
}
};
let req = judge_request(criteria, output);
let resume = RunState::Check {
answer: answer.to_string(),
};
let reply = match span.instrument(self.call(io, &handle, &req, false, used, resume)).await {
Ok(reply) => reply,
Err(outcome) => {
span.fail("judge call did not complete");
return Err(outcome);
}
};
let text = reply.message.text_content();
let verdict = judge_verdict(&text, min_score);
if span.is_enabled() {
span.record("passed", verdict.is_ok());
if let Some(score) = parse_json(&text).ok().and_then(|v| v["score"].as_f64()) {
span.record("score", score);
}
}
Ok(verdict.err())
}
}
pub async fn drive(
run: &mut AgentRun,
io: &dyn AgentIo,
store: &dyn CheckpointStore,
) -> Result<StepOutcome, AgentError> {
if store.latest(run.run_id).await?.is_none() {
store.save(run.checkpoint()).await?;
}
loop {
let outcome = run.step(io).await;
store.save(run.checkpoint()).await?;
if outcome != StepOutcome::Continue {
return Ok(outcome);
}
}
}