swink-agent 0.8.0

Core scaffolding for running LLM-powered agentic loops
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Core agent loop execution engine.
//!
//! Implements the nested inner/outer loop, tool dispatch, steering/follow-up
//! injection, event emission, retry integration, error/abort handling, and max
//! tokens recovery. Stateless — all state is passed in via [`AgentLoopConfig`].

mod config;
mod event;
mod overflow;
mod stream;
mod tool_dispatch;
mod turn;
mod types;

pub use config::AgentLoopConfig;
pub use event::{AgentEvent, TurnEndReason};
pub use types::*;

use std::error::Error as _;
use std::pin::Pin;
use std::sync::Arc;

use futures::Stream;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use tokio_util::sync::CancellationToken;
use tracing::{Instrument, info, info_span};

use crate::error::AgentError;
use crate::stream::StreamErrorKind;
use crate::types::{AgentMessage, AssistantMessage, ModelSpec, StopReason};
use crate::util::now_timestamp;

// ─── Constants ───────────────────────────────────────────────────────────────

/// Sentinel value used to signal context overflow between `handle_stream_result`
/// and `run_single_turn`.
#[deprecated(
    note = "Overflow recovery now happens in-place in run_single_turn. Retained for backward compatibility."
)]
#[allow(dead_code)]
pub const CONTEXT_OVERFLOW_SENTINEL: &str = "__context_overflow__";

/// Channel capacity for agent events. Sized to handle burst streaming
/// without backpressure under normal operation.
const EVENT_CHANNEL_CAPACITY: usize = 256;

// ─── Entry Points ────────────────────────────────────────────────────────────

/// Start a new agent loop with prompt messages.
///
/// Creates an initial context with the prompt messages, then runs the loop.
/// Returns a stream of `AgentEvent` values.
#[must_use]
pub fn agent_loop(
    prompt_messages: Vec<AgentMessage>,
    system_prompt: String,
    config: AgentLoopConfig,
    cancellation_token: CancellationToken,
) -> Pin<Box<dyn Stream<Item = AgentEvent> + Send>> {
    let initial_new_messages_len = prompt_messages.len();
    run_loop(
        prompt_messages,
        initial_new_messages_len,
        system_prompt,
        config,
        cancellation_token,
    )
}

/// Resume an agent loop from existing messages.
///
/// Resumes from existing messages (no new prompt), calls the loop.
/// Returns a stream of `AgentEvent` values.
#[must_use]
pub fn agent_loop_continue(
    messages: Vec<AgentMessage>,
    system_prompt: String,
    config: AgentLoopConfig,
    cancellation_token: CancellationToken,
) -> Pin<Box<dyn Stream<Item = AgentEvent> + Send>> {
    run_loop(messages, 0, system_prompt, config, cancellation_token)
}

// ─── Internal Loop ───────────────────────────────────────────────────────────

/// The core loop implementation. Spawns a task that drives the loop and sends
/// events through an mpsc channel, returning a stream of events.
fn run_loop(
    initial_messages: Vec<AgentMessage>,
    initial_new_messages_len: usize,
    system_prompt: String,
    config: AgentLoopConfig,
    cancellation_token: CancellationToken,
) -> Pin<Box<dyn Stream<Item = AgentEvent> + Send>> {
    let (tx, rx) = mpsc::channel::<AgentEvent>(EVENT_CHANNEL_CAPACITY);

    tokio::spawn(async move {
        run_loop_inner(
            initial_messages,
            initial_new_messages_len,
            system_prompt,
            config,
            cancellation_token,
            tx,
        )
        .await;
    });

    Box::pin(ReceiverStream::new(rx))
}

/// Send an event through the channel. Returns false if the receiver is dropped.
pub async fn emit(tx: &mpsc::Sender<AgentEvent>, event: AgentEvent) -> bool {
    tx.send(event).await.is_ok()
}

// ─── run_loop_inner ──────────────────────────────────────────────────────────

/// The actual loop logic running inside the spawned task.
#[allow(clippy::too_many_lines)]
async fn run_loop_inner(
    initial_messages: Vec<AgentMessage>,
    initial_new_messages_len: usize,
    system_prompt: String,
    config: AgentLoopConfig,
    cancellation_token: CancellationToken,
    tx: mpsc::Sender<AgentEvent>,
) {
    let config = Arc::new(config);
    let span = info_span!(
        "agent.run",
        model_id = %config.model.model_id,
        provider = %config.model.provider,
        tool_count = config.tools.len(),
        message_count = initial_messages.len(),
    );
    async {
        info!(
            model = %config.model.model_id,
            provider = %config.model.provider,
            tools = config.tools.len(),
            "starting agent loop"
        );
        // Build the transfer chain and push the current agent name (if known)
        // so that circular transfers back to this agent are detected.
        let mut transfer_chain = config.transfer_chain.clone().unwrap_or_default();
        if let Some(ref name) = config.agent_name {
            // Ignore the error — when resuming from a handoff chain the agent
            // name may already be present as the latest hop.
            let _ = transfer_chain.push(name.clone());
        }

        let mut state = LoopState {
            context_messages: initial_messages,
            pending_messages: Vec::new(),
            initial_new_messages_len,
            overflow_signal: false,
            overflow_recovery_attempted: false,
            turn_index: 0,
            accumulated_usage: crate::types::Usage::default(),
            accumulated_cost: crate::types::Cost::default(),
            last_assistant_message: None,
            last_tool_results: Vec::new(),
            transfer_chain,
        };

        // 1. Emit AgentStart
        if !emit(&tx, AgentEvent::AgentStart).await {
            return;
        }

        // 2. Outer loop (follow-up phase)
        'outer: loop {
            // Inner loop (turn + tool phase)
            'inner: loop {
                let turn_result = turn::run_single_turn(
                    &config,
                    &mut state,
                    &system_prompt,
                    &cancellation_token,
                    &tx,
                )
                .await;

                let should_break = match turn_result {
                    TurnOutcome::ContinueInner => {
                        state.turn_index += 1;
                        false
                    }
                    TurnOutcome::BreakInner => {
                        state.turn_index += 1;
                        true
                    }
                    TurnOutcome::Return => return,
                };

                // Post-turn policies are evaluated inside the turn handlers
                // (handle_no_tool_calls / handle_tool_calls) against the
                // committed turn snapshot before TurnEnd is emitted or transfer
                // termination is honored. This lets policies replace the
                // assistant message before listeners observe the turn.

                if should_break {
                    break 'inner;
                }
            }

            // Post-loop policies: evaluate after inner loop exits
            {
                use crate::policy::{PolicyContext, PolicyVerdict, run_post_loop_policies};

                let state_snapshot = {
                    let guard = config
                        .session_state
                        .read()
                        .unwrap_or_else(std::sync::PoisonError::into_inner);
                    guard.clone()
                };
                let policy_ctx = PolicyContext {
                    turn_index: state.turn_index,
                    accumulated_usage: &state.accumulated_usage,
                    accumulated_cost: &state.accumulated_cost,
                    message_count: state.context_messages.len(),
                    overflow_signal: state.overflow_signal,
                    new_messages: &[], // no new messages at post-loop
                    state: &state_snapshot,
                };
                match run_post_loop_policies(&config.post_loop_policies, &policy_ctx) {
                    PolicyVerdict::Continue => {}
                    PolicyVerdict::Stop(_reason) => {
                        let _ = emit(
                            &tx,
                            AgentEvent::AgentEnd {
                                messages: Arc::new(state.context_messages),
                            },
                        )
                        .await;
                        info!("post-loop policy stopped agent");
                        return;
                    }
                    PolicyVerdict::Inject(msgs) => {
                        state.pending_messages.extend(msgs);
                        config
                            .pending_message_snapshot
                            .replace(&state.pending_messages);
                        continue 'outer;
                    }
                }
            }

            // Outer loop: poll follow-up messages
            if let Some(ref provider) = config.message_provider {
                let msgs = provider.poll_follow_up();
                if !msgs.is_empty() {
                    state.pending_messages.extend(msgs);
                    config
                        .pending_message_snapshot
                        .replace(&state.pending_messages);
                    continue 'outer;
                }
            }

            // No follow-up → emit AgentEnd and exit
            let _ = emit(
                &tx,
                AgentEvent::AgentEnd {
                    messages: Arc::new(state.context_messages),
                },
            )
            .await;
            info!("agent loop finished");
            return;
        }
    }
    .instrument(span)
    .await;
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

/// Build a terminal `AssistantMessage` with the given stop reason and message.
fn build_terminal_message(
    model: &ModelSpec,
    stop_reason: StopReason,
    error_message: String,
) -> AssistantMessage {
    AssistantMessage {
        content: vec![],
        provider: model.provider.clone(),
        model_id: model.model_id.clone(),
        usage: crate::types::Usage::default(),
        cost: crate::types::Cost::default(),
        stop_reason,
        error_message: Some(error_message),
        error_kind: None,
        timestamp: now_timestamp(),
        cache_hint: None,
    }
}

/// Build an aborted `AssistantMessage`.
pub fn build_abort_message(model: &ModelSpec) -> AssistantMessage {
    build_terminal_message(
        model,
        StopReason::Aborted,
        "operation aborted via cancellation token".to_string(),
    )
}

/// Build an error `AssistantMessage` from a `AgentError`.
pub fn build_error_message(model: &ModelSpec, error: &AgentError) -> AssistantMessage {
    build_terminal_message(model, StopReason::Error, format_error_with_sources(error))
}

pub fn format_error_with_sources(error: &AgentError) -> String {
    let mut message = error.to_string();
    let mut source = error.source();

    while let Some(err) = source {
        let source_message = err.to_string();
        if !source_message.is_empty() && !message.contains(&source_message) {
            message.push_str(": ");
            message.push_str(&source_message);
        }
        source = err.source();
    }

    message
}

/// Classify an `AssistantMessageEvent::Error` into a `AgentError`.
///
/// When `error_kind` is present, structural classification takes priority
/// over string matching on the error message.
pub fn classify_stream_error(
    error_message: &str,
    stop_reason: StopReason,
    error_kind: Option<StreamErrorKind>,
) -> AgentError {
    // Prefer structural classification when the adapter provides it
    if let Some(kind) = error_kind {
        return match kind {
            StreamErrorKind::Throttled => AgentError::ModelThrottled,
            StreamErrorKind::ContextWindowExceeded => AgentError::ContextWindowOverflow {
                model: String::new(),
            },
            StreamErrorKind::Auth => AgentError::StreamError {
                source: Box::new(std::io::Error::other(error_message.to_string())),
            },
            StreamErrorKind::Network => {
                AgentError::network(std::io::Error::other(error_message.to_string()))
            }
            StreamErrorKind::ContentFiltered => AgentError::ContentFiltered,
        };
    }

    // Fallback to string matching for adapters that don't set error_kind
    let lower = error_message.to_lowercase();
    if lower.contains("context window") || lower.contains("context_length_exceeded") {
        return AgentError::ContextWindowOverflow {
            model: String::new(),
        };
    }
    if lower.contains("rate limit") || lower.contains("429") || lower.contains("throttl") {
        return AgentError::ModelThrottled;
    }
    if lower.contains("cache miss")
        || lower.contains("cache not found")
        || lower.contains("cache_miss")
    {
        return AgentError::CacheMiss;
    }
    if lower.contains("content filter") || lower.contains("content_filter") {
        return AgentError::ContentFiltered;
    }
    if stop_reason == StopReason::Aborted {
        return AgentError::Aborted;
    }
    AgentError::StreamError {
        source: Box::new(std::io::Error::other(error_message.to_string())),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn classify_cache_miss_variants() {
        let cases = [
            "cache miss",
            "Cache Miss detected",
            "provider cache_miss",
            "cache not found",
        ];
        for msg in cases {
            let err = classify_stream_error(msg, StopReason::Error, None);
            assert!(
                matches!(err, AgentError::CacheMiss),
                "expected CacheMiss for \"{msg}\", got {err:?}"
            );
            assert!(!err.is_retryable());
        }
    }

    #[test]
    fn classify_non_cache_miss() {
        let err = classify_stream_error("internal server error", StopReason::Error, None);
        assert!(!matches!(err, AgentError::CacheMiss));
    }

    #[test]
    fn classify_content_filtered_by_kind() {
        let err = classify_stream_error(
            "response blocked",
            StopReason::Error,
            Some(StreamErrorKind::ContentFiltered),
        );
        assert!(matches!(err, AgentError::ContentFiltered));
        assert!(!err.is_retryable());
    }

    #[test]
    fn classify_content_filtered_by_string() {
        let err =
            classify_stream_error("content filter violation detected", StopReason::Error, None);
        assert!(matches!(err, AgentError::ContentFiltered));
        assert!(!err.is_retryable());
    }

    #[test]
    fn classify_throttled_by_kind() {
        let err = classify_stream_error(
            "some error",
            StopReason::Error,
            Some(StreamErrorKind::Throttled),
        );
        assert!(matches!(err, AgentError::ModelThrottled));
    }

    #[test]
    fn classify_network_by_kind() {
        let err = classify_stream_error(
            "connection reset",
            StopReason::Error,
            Some(StreamErrorKind::Network),
        );
        assert!(matches!(err, AgentError::NetworkError { .. }));
        assert!(err.is_retryable());
    }

    #[test]
    fn classify_auth_by_kind() {
        let err = classify_stream_error(
            "invalid api key",
            StopReason::Error,
            Some(StreamErrorKind::Auth),
        );
        assert!(matches!(err, AgentError::StreamError { .. }));
        assert!(!err.is_retryable());
    }

    #[test]
    fn classify_context_overflow_by_kind() {
        let err = classify_stream_error(
            "too many tokens",
            StopReason::Error,
            Some(StreamErrorKind::ContextWindowExceeded),
        );
        assert!(matches!(err, AgentError::ContextWindowOverflow { .. }));
    }

    #[test]
    fn structured_kind_takes_priority_over_string() {
        // Message says "rate limit" but kind says Network — kind wins
        let err = classify_stream_error(
            "rate limit exceeded",
            StopReason::Error,
            Some(StreamErrorKind::Network),
        );
        assert!(
            matches!(err, AgentError::NetworkError { .. }),
            "structured kind should override string matching, got {err:?}"
        );
    }

    #[test]
    fn string_fallback_for_unclassified_errors() {
        // No error_kind — string matching should still work for external adapters
        let err = classify_stream_error("rate limit (429)", StopReason::Error, None);
        assert!(matches!(err, AgentError::ModelThrottled));
    }

    #[test]
    fn string_fallback_context_overflow() {
        let err =
            classify_stream_error("context_length_exceeded: too long", StopReason::Error, None);
        assert!(matches!(err, AgentError::ContextWindowOverflow { .. }));
    }

    #[test]
    fn aborted_stop_reason_without_kind() {
        let err = classify_stream_error("operation cancelled", StopReason::Aborted, None);
        assert!(matches!(err, AgentError::Aborted));
    }
}