deepstrike-sdk 0.2.41

DeepStrike Rust SDK — agent framework built on deepstrike-core
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! Policy-oriented attempt orchestration.
//!
//! The four independent axes are explicit: [`AttemptBody`] runs work,
//! [`AttemptJudge`] evaluates it, [`CarryPolicy`] prepares the next attempt, and
//! [`StopPolicy`] bounds the loop.  Runtime health and quality verdict are kept
//! as separate fields in [`AttemptOutcome`].

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use async_trait::async_trait;
use deepstrike_core::context::renderer::RenderedContext;
use deepstrike_core::harness::eval::SkillCandidate;
use deepstrike_core::orchestration::workflow::WorkflowNode;
use deepstrike_core::types::message::{Message, Role};
use futures::{Stream, StreamExt};

use crate::harness::{Criterion, Verdict};
use crate::providers::{LLMProvider, StreamEvent};
use crate::run_event::RunEvent;
use crate::runtime::RuntimeRunner;
use crate::tools::ToolChunk;
use crate::{Error, Result};

pub type AttemptBodyStream<'a> = Pin<Box<dyn Stream<Item = Result<AttemptBodyEvent>> + 'a>>;
pub type AttemptLoopStream<'a> = Pin<Box<dyn Stream<Item = Result<AttemptLoopEvent>> + 'a>>;

#[derive(Debug, Clone)]
pub struct AttemptRequest {
    pub session_id: String,
    pub goal: String,
    pub criteria: Vec<Criterion>,
    pub extensions: Option<serde_json::Value>,
}

impl AttemptRequest {
    pub fn new(session_id: impl Into<String>, goal: impl Into<String>) -> Self {
        Self {
            session_id: session_id.into(),
            goal: goal.into(),
            criteria: Vec::new(),
            extensions: None,
        }
    }

    pub fn generated(goal: impl Into<String>) -> Self {
        Self::new(uuid::Uuid::new_v4().to_string(), goal)
    }
}

#[derive(Debug, Clone)]
pub struct AttemptBodyContext {
    pub session_id: String,
    pub goal: String,
    pub criteria: Vec<Criterion>,
    pub extensions: Option<serde_json::Value>,
    pub attempt: u32,
    /// Carry material is context for the next attempt, not a goal rewrite.
    pub context_input: Option<String>,
}

#[derive(Debug, Clone)]
pub enum AttemptBodyEvent {
    Token(String),
    ToolCall {
        id: String,
        name: String,
    },
    ToolDelta {
        call_id: String,
        name: String,
        chunk: ToolChunk,
    },
    ToolSuspend {
        call_id: String,
        name: String,
        suspension_id: String,
        payload: Option<serde_json::Value>,
    },
    ToolResult {
        call_id: String,
        content: String,
        is_error: bool,
    },
    WorkflowNodesSubmitted(Vec<WorkflowNode>),
    BodyError(String),
    BodyDone {
        run_status: String,
        result: String,
        turns: u32,
        total_tokens: u64,
    },
}

pub trait AttemptBody: Send + Sync {
    fn run<'a>(&'a self, context: AttemptBodyContext) -> AttemptBodyStream<'a>;
}

/// Adapts [`RuntimeRunner`] to the body policy slot.
pub struct RuntimeAttemptBody<'a> {
    runner: &'a RuntimeRunner,
}

impl<'a> RuntimeAttemptBody<'a> {
    pub fn new(runner: &'a RuntimeRunner) -> Self {
        Self { runner }
    }
}

impl AttemptBody for RuntimeAttemptBody<'_> {
    fn run<'a>(&'a self, context: AttemptBodyContext) -> AttemptBodyStream<'a> {
        Box::pin(async_stream::try_stream! {
            let mut criteria: Vec<String> = context
                .criteria
                .iter()
                .map(|criterion| criterion.text.clone())
                .collect();
            // RuntimeRunner has no mutable side-channel. A carried note is therefore supplied as
            // run context while the stable session id preserves the transcript. The goal itself
            // remains byte-for-byte stable under ContinueSession.
            if let Some(note) = &context.context_input {
                criteria.push(format!("[Attempt feedback] {note}"));
            }

            let mut result = String::new();
            let mut terminal: Option<(u32, u64, String)> = None;
            let stream = self.runner
                .run_streaming(
                    &context.goal,
                    &criteria,
                    context.extensions.as_ref(),
                    Some(&context.session_id),
                )
                .await;
            let mut stream = match stream {
                Ok(stream) => stream,
                Err(error) => {
                    yield AttemptBodyEvent::BodyError(error.to_string());
                    yield AttemptBodyEvent::BodyDone {
                        run_status: "error".to_string(),
                        result,
                        turns: 0,
                        total_tokens: 0,
                    };
                    return;
                }
            };

            while let Some(event) = stream.next().await {
                match event {
                    Err(error) => {
                        yield AttemptBodyEvent::BodyError(error.to_string());
                        terminal = Some((0, 0, "error".to_string()));
                        break;
                    }
                    Ok(RunEvent::TextDelta(delta)) => {
                        result.push_str(&delta);
                        yield AttemptBodyEvent::Token(delta);
                    }
                    Ok(RunEvent::ToolCall { id, name }) => {
                        yield AttemptBodyEvent::ToolCall { id, name };
                    }
                    Ok(RunEvent::ToolDelta { call_id, name, chunk }) => {
                        yield AttemptBodyEvent::ToolDelta { call_id, name, chunk };
                    }
                    Ok(RunEvent::ToolSuspend { call_id, name, suspension_id, payload }) => {
                        yield AttemptBodyEvent::ToolSuspend {
                            call_id,
                            name,
                            suspension_id,
                            payload,
                        };
                    }
                    Ok(RunEvent::ToolResult { call_id, content, is_error, .. }) => {
                        yield AttemptBodyEvent::ToolResult { call_id, content, is_error };
                    }
                    Ok(RunEvent::Error(message)) => {
                        yield AttemptBodyEvent::BodyError(message);
                        terminal = Some((0, 0, "error".to_string()));
                        break;
                    }
                    Ok(RunEvent::Done { iterations, total_tokens, status }) => {
                        terminal = Some((iterations, total_tokens, status));
                    }
                    Ok(_) => {}
                }
            }

            let (turns, total_tokens, run_status) =
                terminal.unwrap_or_else(|| (0, 0, "error".to_string()));
            yield AttemptBodyEvent::BodyDone {
                run_status,
                result,
                turns,
                total_tokens,
            };
        })
    }
}

#[derive(Debug, Clone)]
pub struct JudgeContext {
    pub goal: String,
    pub criteria: Vec<Criterion>,
    pub attempt: u32,
    pub result: String,
}

#[derive(Debug, Clone)]
pub struct JudgeResult {
    pub verdict: Verdict,
    pub skill_candidate: Option<SkillCandidate>,
}

impl JudgeResult {
    pub fn new(verdict: Verdict) -> Self {
        Self {
            verdict,
            skill_candidate: None,
        }
    }
}

#[async_trait]
pub trait AttemptJudge: Send + Sync {
    /// `None` defers to a composed fallback judge.
    async fn judge(&self, context: &JudgeContext) -> Result<Option<JudgeResult>>;
}

pub type VerdictFn = Arc<dyn Fn(&JudgeContext) -> Option<Verdict> + Send + Sync>;

pub struct VerdictFnJudge {
    verdict_fn: VerdictFn,
}

impl VerdictFnJudge {
    pub fn new(verdict_fn: VerdictFn) -> Self {
        Self { verdict_fn }
    }
}

#[async_trait]
impl AttemptJudge for VerdictFnJudge {
    async fn judge(&self, context: &JudgeContext) -> Result<Option<JudgeResult>> {
        Ok((self.verdict_fn)(context).map(JudgeResult::new))
    }
}

pub struct LlmEvalJudge {
    eval_provider: Box<dyn LLMProvider>,
    extract_skill_on_pass: bool,
}

impl LlmEvalJudge {
    pub fn new(eval_provider: impl LLMProvider + 'static) -> Self {
        Self {
            eval_provider: Box::new(eval_provider),
            extract_skill_on_pass: false,
        }
    }

    pub fn extract_skill_on_pass(mut self, enabled: bool) -> Self {
        self.extract_skill_on_pass = enabled;
        self
    }
}

#[async_trait]
impl AttemptJudge for LlmEvalJudge {
    async fn judge(&self, context: &JudgeContext) -> Result<Option<JudgeResult>> {
        use deepstrike_core::harness::eval::{
            build_eval_messages, parse_verdict, Criterion as EvalCriterion,
        };

        let criteria: Vec<EvalCriterion> = context
            .criteria
            .iter()
            .map(|criterion| EvalCriterion {
                text: criterion.text.clone(),
                required: criterion.required,
                weight: criterion.weight,
            })
            .collect();
        let messages = build_eval_messages(
            &context.goal,
            &criteria,
            &context.result,
            context.attempt,
            self.extract_skill_on_pass,
        );
        let rendered = rendered_context_from_messages(messages);
        let state = self.eval_provider.create_run_state();
        let mut stream = self
            .eval_provider
            .stream(&rendered, &[], None, state.as_ref())
            .await?;
        let mut text = String::new();
        while let Some(event) = stream.next().await {
            if let StreamEvent::TextDelta { delta } = event? {
                text.push_str(&delta);
            }
        }
        if text.is_empty() {
            return Err(Error::Other("attempt judge produced no text".to_string()));
        }

        let parsed = parse_verdict(&text);
        Ok(Some(JudgeResult {
            verdict: Verdict {
                passed: parsed.passed,
                overall_score: parsed.overall_score,
                feedback: parsed.feedback,
                details: parsed
                    .details
                    .into_iter()
                    .map(|detail| crate::harness::CriterionResult {
                        criterion: detail.criterion,
                        passed: detail.passed,
                        score: detail.score,
                        feedback: detail.feedback,
                    })
                    .collect(),
            },
            skill_candidate: parsed.skill_candidate,
        }))
    }
}

pub struct HybridJudge<P, F> {
    primary: P,
    fallback: F,
}

impl<P, F> HybridJudge<P, F> {
    pub fn new(primary: P, fallback: F) -> Self {
        Self { primary, fallback }
    }
}

#[async_trait]
impl<P, F> AttemptJudge for HybridJudge<P, F>
where
    P: AttemptJudge,
    F: AttemptJudge,
{
    async fn judge(&self, context: &JudgeContext) -> Result<Option<JudgeResult>> {
        match self.primary.judge(context).await? {
            Some(result) => Ok(Some(result)),
            None => self.fallback.judge(context).await,
        }
    }
}

#[derive(Debug, Clone)]
pub struct CarryContext {
    pub root_session_id: String,
    pub goal: String,
    pub attempt: u32,
    pub previous_verdict: Option<Verdict>,
}

#[derive(Debug, Clone)]
pub struct PreparedAttempt {
    pub session_id: String,
    pub goal: String,
    pub context_input: Option<String>,
}

#[async_trait]
pub trait CarryPolicy: Send + Sync {
    async fn prepare(&self, context: CarryContext) -> Result<PreparedAttempt>;
}

/// Default carry: stable session + unchanged goal + feedback as context.
#[derive(Debug, Clone, Copy, Default)]
pub struct ContinueSession;

#[async_trait]
impl CarryPolicy for ContinueSession {
    async fn prepare(&self, context: CarryContext) -> Result<PreparedAttempt> {
        Ok(PreparedAttempt {
            session_id: context.root_session_id,
            goal: context.goal,
            context_input: context
                .previous_verdict
                .map(|verdict| verdict.feedback)
                .filter(|feedback| !feedback.is_empty()),
        })
    }
}

/// Explicit isolation: a new session and goal-appended feedback after attempt 1.
#[derive(Debug, Clone, Copy, Default)]
pub struct FreshWithFeedback;

#[async_trait]
impl CarryPolicy for FreshWithFeedback {
    async fn prepare(&self, context: CarryContext) -> Result<PreparedAttempt> {
        let goal = match context.previous_verdict {
            Some(verdict) if !verdict.feedback.is_empty() => format!(
                "{}\n\n[Attempt {} feedback: {}]",
                context.goal,
                context.attempt.saturating_sub(1),
                verdict.feedback
            ),
            _ => context.goal,
        };
        Ok(PreparedAttempt {
            session_id: if context.attempt == 1 {
                context.root_session_id
            } else {
                uuid::Uuid::new_v4().to_string()
            },
            goal,
            context_input: None,
        })
    }
}

pub type DigestFuture = Pin<Box<dyn Future<Output = Result<String>> + Send>>;
pub type DigestFn = Arc<dyn Fn(Verdict, u32) -> DigestFuture + Send + Sync>;
pub type PassHookFuture<'a> = Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>>;
pub type PassHook =
    Arc<dyn for<'a> Fn(&'a AttemptOutcome, &'a JudgeResult) -> PassHookFuture<'a> + Send + Sync>;

pub struct FreshWithDigest {
    digest: DigestFn,
}

impl FreshWithDigest {
    pub fn new(digest: DigestFn) -> Self {
        Self { digest }
    }
}

#[async_trait]
impl CarryPolicy for FreshWithDigest {
    async fn prepare(&self, context: CarryContext) -> Result<PreparedAttempt> {
        let goal = if let Some(verdict) = context.previous_verdict {
            let digest = (self.digest)(verdict, context.attempt.saturating_sub(1)).await?;
            format!("{}\n\n[Prior attempt digest: {digest}]", context.goal)
        } else {
            context.goal
        };
        Ok(PreparedAttempt {
            session_id: if context.attempt == 1 {
                context.root_session_id
            } else {
                uuid::Uuid::new_v4().to_string()
            },
            goal,
            context_input: None,
        })
    }
}

#[derive(Debug, Clone)]
pub struct StopPolicy {
    pub max_attempts: u32,
    pub max_total_tokens: Option<u64>,
    pub stop_on_failed_verdict: bool,
}

impl StopPolicy {
    pub fn new(max_attempts: u32) -> Self {
        Self {
            max_attempts,
            max_total_tokens: None,
            stop_on_failed_verdict: false,
        }
    }

    pub fn max_total_tokens(mut self, limit: u64) -> Self {
        self.max_total_tokens = Some(limit);
        self
    }

    pub fn stop_on_failed_verdict(mut self, enabled: bool) -> Self {
        self.stop_on_failed_verdict = enabled;
        self
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AttemptOutcomeKind {
    Passed,
    FailedJudge,
    Exhausted,
    RunError,
}

#[derive(Debug, Clone)]
pub struct AttemptOutcome {
    pub outcome: AttemptOutcomeKind,
    pub run_status: String,
    pub verdict: Option<Verdict>,
    pub result: String,
    pub attempts: u32,
    pub turns: u32,
    pub total_tokens: u64,
    pub submitted_nodes: Option<Vec<WorkflowNode>>,
}

#[derive(Debug, Clone)]
pub enum AttemptLoopEvent {
    Token(String),
    ToolCall {
        id: String,
        name: String,
    },
    ToolDelta {
        call_id: String,
        name: String,
        chunk: ToolChunk,
    },
    ToolSuspend {
        call_id: String,
        name: String,
        suspension_id: String,
        payload: Option<serde_json::Value>,
    },
    ToolResult {
        call_id: String,
        content: String,
        is_error: bool,
    },
    WorkflowNodesSubmitted(Vec<WorkflowNode>),
    BodyError(String),
    Judging {
        attempt: u32,
    },
    Retrying {
        attempt: u32,
        verdict: Verdict,
    },
    Completed(AttemptOutcome),
}

pub struct AttemptLoop<B, J, C = ContinueSession> {
    body: B,
    judge: J,
    carry: C,
    stop: StopPolicy,
    on_pass: Option<PassHook>,
}

impl<B, J> AttemptLoop<B, J, ContinueSession> {
    pub fn new(body: B, judge: J, stop: StopPolicy) -> Result<Self> {
        validate_stop_policy(&stop)?;
        Ok(Self {
            body,
            judge,
            carry: ContinueSession,
            stop,
            on_pass: None,
        })
    }
}

impl<B, J, C> AttemptLoop<B, J, C> {
    pub fn with_carry<Next>(self, carry: Next) -> AttemptLoop<B, J, Next> {
        AttemptLoop {
            body: self.body,
            judge: self.judge,
            carry,
            stop: self.stop,
            on_pass: self.on_pass,
        }
    }

    /// Runs after a passing verdict and before the completed event is emitted.
    pub fn with_on_pass(mut self, hook: PassHook) -> Self {
        self.on_pass = Some(hook);
        self
    }
}

impl<B, J, C> AttemptLoop<B, J, C>
where
    B: AttemptBody,
    J: AttemptJudge,
    C: CarryPolicy,
{
    pub async fn run(&self, request: AttemptRequest) -> Result<AttemptOutcome> {
        let mut outcome = None;
        let mut stream = self.stream(request);
        while let Some(event) = stream.next().await {
            if let AttemptLoopEvent::Completed(completed) = event? {
                outcome = Some(completed);
            }
        }
        outcome.ok_or_else(|| Error::Other("AttemptLoop ended without an outcome".to_string()))
    }

    pub fn stream<'a>(&'a self, request: AttemptRequest) -> AttemptLoopStream<'a> {
        Box::pin(async_stream::try_stream! {
            let root_session_id = request.session_id.clone();
            let mut previous_verdict: Option<Verdict> = None;
            let mut total_tokens = 0u64;
            let mut total_turns = 0u32;
            let mut submitted_nodes = Vec::new();

            for attempt in 1..=self.stop.max_attempts {
                let prepared = self.carry
                    .prepare(CarryContext {
                        root_session_id: root_session_id.clone(),
                        goal: request.goal.clone(),
                        attempt,
                        previous_verdict: previous_verdict.clone(),
                    })
                    .await?;
                let mut body = self.body.run(AttemptBodyContext {
                    session_id: prepared.session_id,
                    goal: prepared.goal,
                    criteria: request.criteria.clone(),
                    extensions: request.extensions.clone(),
                    attempt,
                    context_input: prepared.context_input,
                });
                let mut terminal = None;
                while let Some(event) = body.next().await {
                    match event? {
                        AttemptBodyEvent::Token(text) => yield AttemptLoopEvent::Token(text),
                        AttemptBodyEvent::ToolCall { id, name } => {
                            yield AttemptLoopEvent::ToolCall { id, name };
                        }
                        AttemptBodyEvent::ToolDelta { call_id, name, chunk } => {
                            yield AttemptLoopEvent::ToolDelta { call_id, name, chunk };
                        }
                        AttemptBodyEvent::ToolSuspend {
                            call_id,
                            name,
                            suspension_id,
                            payload,
                        } => {
                            yield AttemptLoopEvent::ToolSuspend {
                                call_id,
                                name,
                                suspension_id,
                                payload,
                            };
                        }
                        AttemptBodyEvent::ToolResult { call_id, content, is_error } => {
                            yield AttemptLoopEvent::ToolResult { call_id, content, is_error };
                        }
                        AttemptBodyEvent::WorkflowNodesSubmitted(nodes) => {
                            submitted_nodes.extend(nodes.iter().cloned());
                            yield AttemptLoopEvent::WorkflowNodesSubmitted(nodes);
                        }
                        AttemptBodyEvent::BodyError(message) => {
                            yield AttemptLoopEvent::BodyError(message);
                        }
                        AttemptBodyEvent::BodyDone {
                            run_status,
                            result,
                            turns,
                            total_tokens,
                        } => terminal = Some((run_status, result, turns, total_tokens)),
                    }
                }
                let (run_status, result, turns, attempt_tokens) = terminal.ok_or_else(|| {
                    Error::Other("AttemptBody ended without body_done".to_string())
                })?;
                total_tokens = total_tokens.saturating_add(attempt_tokens);
                total_turns = total_turns.saturating_add(turns);

                if is_run_error(&run_status) {
                    yield AttemptLoopEvent::Completed(AttemptOutcome {
                        outcome: AttemptOutcomeKind::RunError,
                        run_status,
                        verdict: None,
                        result,
                        attempts: attempt,
                        turns: total_turns,
                        total_tokens,
                        submitted_nodes: (!submitted_nodes.is_empty())
                            .then(|| submitted_nodes.clone()),
                    });
                    return;
                }

                yield AttemptLoopEvent::Judging { attempt };
                let judged = self
                    .judge
                    .judge(&JudgeContext {
                        goal: request.goal.clone(),
                        criteria: request.criteria.clone(),
                        attempt,
                        result: result.clone(),
                    })
                    .await?
                    .ok_or_else(|| Error::Other("AttemptLoop judge produced no verdict".to_string()))?;
                let verdict = judged.verdict.clone();

                if verdict.passed {
                    let outcome = AttemptOutcome {
                        outcome: AttemptOutcomeKind::Passed,
                        run_status,
                        verdict: Some(verdict),
                        result,
                        attempts: attempt,
                        turns: total_turns,
                        total_tokens,
                        submitted_nodes: (!submitted_nodes.is_empty())
                            .then(|| submitted_nodes.clone()),
                    };
                    if let Some(on_pass) = &self.on_pass {
                        on_pass(&outcome, &judged).await?;
                    }
                    yield AttemptLoopEvent::Completed(outcome);
                    return;
                }

                previous_verdict = Some(verdict.clone());
                let token_limit_reached = self
                    .stop
                    .max_total_tokens
                    .is_some_and(|limit| total_tokens >= limit);
                if self.stop.stop_on_failed_verdict
                    || attempt == self.stop.max_attempts
                    || token_limit_reached
                {
                    yield AttemptLoopEvent::Completed(AttemptOutcome {
                        outcome: if self.stop.stop_on_failed_verdict {
                            AttemptOutcomeKind::FailedJudge
                        } else {
                            AttemptOutcomeKind::Exhausted
                        },
                        run_status,
                        verdict: Some(verdict),
                        result,
                        attempts: attempt,
                        turns: total_turns,
                        total_tokens,
                        submitted_nodes: (!submitted_nodes.is_empty())
                            .then(|| submitted_nodes.clone()),
                    });
                    return;
                }

                yield AttemptLoopEvent::Retrying { attempt, verdict };
            }
        })
    }
}

fn validate_stop_policy(stop: &StopPolicy) -> Result<()> {
    if stop.max_attempts == 0 {
        return Err(Error::Other(
            "AttemptLoop stop.max_attempts must be positive".to_string(),
        ));
    }
    Ok(())
}

fn is_run_error(status: &str) -> bool {
    matches!(
        status.to_ascii_lowercase().as_str(),
        "error" | "invalid_arg" | "user_abort"
    )
}

fn rendered_context_from_messages(messages: Vec<Message>) -> RenderedContext {
    let mut system_parts = Vec::new();
    let mut turns = Vec::new();
    for message in messages {
        if message.role == Role::System {
            if let Some(text) = message.content.as_text() {
                system_parts.push(text.to_owned());
            }
        } else {
            turns.push(message);
        }
    }
    let system_text = system_parts.join("\n\n");
    RenderedContext {
        system_text: system_text.clone(),
        system_stable: system_text,
        system_knowledge: String::new(),
        turns,
        state_turn: None,
        frozen_prefix_len: None,
        budget_overflow: None,
    }
}