1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2026 The Sashiko Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use anyhow::Result;
use async_trait::async_trait;
use serde_json::Value;
use super::{
AiErrorClass, AiMessage, AiProvider, AiRequest, AiResponse, AiResponseFormat, AiRole, AiTool,
AiUsage, ToolCall, classify_ai_error,
};
/// The unified result of executing an [`LlmSession`].
pub struct SessionResult<T> {
/// The validated output of the session.
pub output: T,
/// The full conversation history.
pub history: Vec<AiMessage>,
/// Accumulated token usage statistics.
pub usage: AiUsage,
}
/// Result of validating a session's final response.
#[derive(Debug)]
pub enum ValidationError {
/// The response was invalid but can be retried.
/// Contains a feedback message to append to the LLM prompt.
FormatViolation(String),
/// A fatal error that cannot be resolved by retrying.
Fatal(String),
}
/// Action to take upon encountering a provider error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ErrorAction {
/// Retry the request after appending the feedback message to the prompt history.
RetryWithFeedback(String),
/// Abort the session immediately.
Fail,
}
/// Represents a stateful, task-oriented interaction session with an LLM.
#[async_trait]
pub trait LlmSession: Send {
/// The final output type returned by the session after validation.
type Output: Send;
/// The system prompt guiding the LLM.
fn system_prompt(&self) -> String;
/// The initial user prompt.
fn initial_user_prompt(&self) -> String;
/// The user prompt to store in history/logs (for space saving).
/// Defaults to `initial_user_prompt()`.
fn log_user_prompt(&self) -> String {
self.initial_user_prompt()
}
/// Customizes the validation feedback message.
fn format_validation_feedback(&self, violation: &str) -> String {
format!(
"Previous attempt was rejected: {}. Please correct your output format.",
violation
)
}
/// Optional list of tools available in this session.
fn tools(&self) -> Option<Vec<AiTool>> {
None
}
/// Optional temperature override.
fn temperature(&self) -> Option<f32> {
None
}
/// Optional context tag for logging.
fn context_tag(&self) -> Option<String> {
None
}
/// Optional expected response format.
fn response_format(&self) -> Option<AiResponseFormat> {
None
}
/// Executes a tool call requested by the LLM.
async fn call_tool(&mut self, name: &str, _args: Value) -> Result<Value> {
anyhow::bail!("Tool execution not implemented for this session: {}", name)
}
/// Executes multiple tool calls requested by the LLM.
/// Default implementation runs them sequentially.
async fn call_tools(&mut self, calls: Vec<ToolCall>) -> Result<Vec<(String, Value)>> {
let mut results = Vec::with_capacity(calls.len());
for call in calls {
let res = self.call_tool(&call.function_name, call.arguments).await?;
results.push((call.id, res));
}
Ok(results)
}
/// Validates the final response content.
fn validate(&mut self, response: &AiResponse) -> Result<Self::Output, ValidationError>;
/// Hook to handle provider errors (e.g. safety blocks, rate limits).
fn handle_provider_error(&mut self, error: &anyhow::Error, _attempt: usize) -> ErrorAction {
let err_str = error.to_string();
if err_str.contains("RECITATION") || err_str.contains("blocked") {
ErrorAction::RetryWithFeedback(
"IMPORTANT: Your previous response was blocked by a recitation filter. \
Please do NOT copy large blocks of code verbatim in your response. \
Describe changes in prose, or use highly simplified pseudo-code if you must show code structure."
.to_string(),
)
} else {
ErrorAction::Fail
}
}
}
/// Orchestrates the execution of an [`LlmSession`].
pub struct SessionRunner<'a> {
provider: &'a dyn AiProvider,
max_turns: usize,
max_validation_attempts: usize,
max_transient_retries: usize,
max_provider_error_retries: usize,
on_turn: Option<Box<dyn Fn(usize, usize) + Send + Sync + 'a>>,
}
impl<'a> SessionRunner<'a> {
/// Creates a new `SessionRunner` with default limits.
pub fn new(provider: &'a dyn AiProvider) -> Self {
Self {
provider,
max_turns: 15,
max_validation_attempts: 3,
max_transient_retries: 5,
max_provider_error_retries: 3,
on_turn: None,
}
}
/// Configures the maximum validation retries.
pub fn with_max_validation_attempts(mut self, attempts: usize) -> Self {
self.max_validation_attempts = attempts;
self
}
/// Configures the maximum conversational turns.
pub fn with_max_turns(mut self, turns: usize) -> Self {
self.max_turns = turns;
self
}
/// Configures the maximum transient and rate-limit retries.
pub fn with_max_transient_retries(mut self, retries: usize) -> Self {
self.max_transient_retries = retries;
self
}
/// Configures the maximum provider error retries.
pub fn with_max_provider_error_retries(mut self, retries: usize) -> Self {
self.max_provider_error_retries = retries;
self
}
/// Configures a turn callback.
pub fn with_turn_callback<F>(mut self, cb: F) -> Self
where
F: Fn(usize, usize) + Send + Sync + 'a,
{
self.on_turn = Some(Box::new(cb));
self
}
/// Runs the session to completion. Returns the validated output and conversation history (for logging).
pub async fn run<S>(&self, session: &mut S) -> Result<SessionResult<S::Output>>
where
S: LlmSession,
{
let mut history = vec![AiMessage {
role: AiRole::User,
content: Some(session.initial_user_prompt()),
thought: None,
thought_signature: None,
tool_calls: None,
tool_call_id: None,
}];
let mut log_history = vec![AiMessage {
role: AiRole::User,
content: Some(session.log_user_prompt()),
thought: None,
thought_signature: None,
tool_calls: None,
tool_call_id: None,
}];
let mut turns = 0;
let mut validation_attempts = 0;
let mut transient_retries = 0;
let mut provider_error_retries = 0;
let mut total_prompt_tokens = 0;
let mut total_completion_tokens = 0;
let mut total_cached_tokens = 0;
loop {
turns += 1;
if turns > self.max_turns {
anyhow::bail!("Session exceeded max turns limit ({})", self.max_turns);
}
if let Some(ref cb) = self.on_turn {
cb(turns, self.max_turns);
}
let request = AiRequest {
system: Some(session.system_prompt()),
messages: history.clone(),
tools: session.tools(),
temperature: session.temperature(),
response_format: session.response_format(),
context_tag: session.context_tag(),
};
let resp = match self.provider.generate_content(request).await {
Ok(r) => r,
Err(e) => match classify_ai_error(&e) {
AiErrorClass::RateLimit { retry_after }
| AiErrorClass::Transient { retry_after } => {
transient_retries += 1;
if transient_retries > self.max_transient_retries {
anyhow::bail!(
"Session failed after {} transient/rate-limit errors. Last error: {}",
self.max_transient_retries,
e
);
}
tracing::warn!(
"API error ({}), pausing for {:?} before retry (attempt {}/{})...",
e,
retry_after,
transient_retries,
self.max_transient_retries
);
tokio::time::sleep(retry_after).await;
turns = turns.saturating_sub(1);
continue;
}
AiErrorClass::Fatal => {
match session.handle_provider_error(&e, provider_error_retries) {
ErrorAction::RetryWithFeedback(feedback) => {
provider_error_retries += 1;
if provider_error_retries > self.max_provider_error_retries {
anyhow::bail!(
"Session failed after {} provider error retries. Last error: {}",
self.max_provider_error_retries,
e
);
}
let msg = AiMessage {
role: AiRole::User,
content: Some(feedback.clone()),
thought: None,
thought_signature: None,
tool_calls: None,
tool_call_id: None,
};
history.push(msg.clone());
log_history.push(msg);
turns = turns.saturating_sub(1);
continue;
}
ErrorAction::Fail => return Err(e),
}
}
},
};
if resp.truncated {
anyhow::bail!("LLM output was truncated by provider (e.g. hit max tokens)");
}
if let Some(usage) = &resp.usage {
total_prompt_tokens += usage.prompt_tokens;
total_completion_tokens += usage.completion_tokens;
total_cached_tokens += usage.cached_tokens.unwrap_or(0);
}
let assistant_msg = AiMessage {
role: AiRole::Assistant,
content: resp.content.clone(),
thought: resp.thought.clone(),
thought_signature: resp.thought_signature.clone(),
tool_calls: resp.tool_calls.clone(),
tool_call_id: None,
};
history.push(assistant_msg.clone());
log_history.push(assistant_msg);
// Handle Tool Calls
if let Some(tool_calls) = &resp.tool_calls {
let results = session.call_tools(tool_calls.clone()).await?;
for (call_id, result) in results {
let tool_msg = AiMessage {
role: AiRole::Tool,
content: Some(result.to_string()),
thought: None,
thought_signature: None,
tool_calls: None,
tool_call_id: Some(call_id),
};
history.push(tool_msg.clone());
log_history.push(tool_msg);
}
continue; // Loop again to feed tool results back to LLM
}
// No tool calls: validate response
match session.validate(&resp) {
Result::Ok(output) => {
let usage = AiUsage {
prompt_tokens: total_prompt_tokens,
completion_tokens: total_completion_tokens,
total_tokens: total_prompt_tokens + total_completion_tokens,
cached_tokens: Some(total_cached_tokens),
};
return Ok(SessionResult {
output,
history: log_history,
usage,
});
}
Result::Err(ValidationError::FormatViolation(violation)) => {
validation_attempts += 1;
if validation_attempts >= self.max_validation_attempts {
anyhow::bail!(
"Failed to generate valid response after {} validation attempts. Last violation: {}",
self.max_validation_attempts,
violation
);
}
let feedback = session.format_validation_feedback(&violation);
let msg = AiMessage {
role: AiRole::User,
content: Some(feedback),
thought: None,
thought_signature: None,
tool_calls: None,
tool_call_id: None,
};
history.push(msg.clone());
log_history.push(msg);
turns = turns.saturating_sub(1);
}
Result::Err(ValidationError::Fatal(err)) => {
anyhow::bail!("Fatal validation error: {}", err);
}
}
}
}
}