agent-base 0.15.0

A lightweight Agent Runtime Kernel for building AI agents in Rust
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
use std::sync::{Arc, Mutex};

use tokio::sync::broadcast;

use crate::engine::executor::{ExecError, ParseOutcome};
use crate::engine::runtime::llm_engine::LlmTurnResult;
use crate::engine::runtime::plan_runner::RuntimeCore;
use crate::types::{
    Action, ActionFormat, ActionOutcome, AgentError, AgentResult, BatchStatus, CheckpointData,
    CheckpointStep, FinishReason, MessageRole, Observation, RunOutcome, RuntimeEvent, SessionId,
};

use super::entry::drain_locked;
use super::tool_executor::ToolCallExecutor;
use super::turn_end::TurnEndCtx;
use super::turn_guard::{TurnCtx, TurnMetrics};
use super::turn_loop::PostLlmMwResult;
use super::turn_loop::TurnFlow;

impl RuntimeCore {
    // ── Dispatch ────────────────────────────────────────────────────────

    /// Dispatch one LLM turn result: push messages, run tool calls, fire the
    /// turn-end callback, and decide whether the loop continues or the turn ends.
    #[allow(clippy::too_many_arguments)]
    pub(super) async fn handle_llm_turn<F>(
        &self,
        session_id: &SessionId,
        user_input_owned: &str,
        tool_definitions: &[serde_json::Value],
        turn_count: u32,
        turn_start: std::time::Instant,
        model: &str,
        result: AgentResult<LlmTurnResult>,
        event_rx: &mut broadcast::Receiver<RuntimeEvent>,
        on_event: Arc<Mutex<F>>,
        all_user_inputs: &[String],
        max_turns: u32,
    ) -> AgentResult<TurnFlow>
    where
        F: FnMut(RuntimeEvent) -> AgentResult<()> + Send + 'static,
    {
        let turn = match result {
            Ok(r) => r,
            Err(e) => {
                return self
                    .handle_llm_error(
                        session_id,
                        turn_count,
                        turn_start,
                        model,
                        user_input_owned,
                        e,
                    )
                    .await;
            }
        };

        let finish_reason = FinishReason::from_raw(turn.finish_reason.as_deref());
        tracing::info!(
            session_id = session_id.id,
            turn = turn_count,
            text_len = turn.full_text.len(),
            is_tool_call = turn.is_tool_call,
            tool_call_count = turn.tool_calls.len(),
            "LLM turn result"
        );

        let text_len = turn.full_text.len() as u64;
        let has_thinking = !turn.reasoning_text.is_empty();
        let thinking_bytes = turn.reasoning_text.len() as u64;
        // Four-state parse (M3 seam, D4+OV-7). The built-in tool-call backend
        // owns envelope turns; a text turn goes to the mounted action backend
        // (T10c) — it claims its own shape. A backend that cannot classify a
        // turn its own parse routed to it is a routing bug and fails the run
        // loudly (never a silent nudge).
        let parse = if turn.is_tool_call {
            ToolCallExecutor::new(self, session_id.clone()).classify(&turn)
        } else if let Some(backend) = &self.action_backend {
            match backend.lock().await.parse(&turn) {
                Ok(parse) => parse,
                Err(e) => {
                    return Err(AgentError::internal(format!(
                        "action backend failed to classify a text turn: {e}"
                    )));
                }
            }
        } else {
            ToolCallExecutor::new(self, session_id.clone()).classify(&turn)
        };
        let available_tools: Vec<String> = tool_definitions
            .iter()
            .filter_map(|d| {
                d.get("function")?
                    .get("name")?
                    .as_str()
                    .map(|s| s.to_string())
            })
            .collect();

        let LlmTurnResult {
            full_text,
            reasoning_text,
            usage,
            ttft_ms,
            llm_duration_ms,
            reasoning_only,
            is_tool_call: _,
            tool_calls: _,
            finish_reason: _,
            thinking_signature: _,
        } = turn;

        let turn_ctx = TurnCtx {
            session_id,
            turn_count,
            user_input: user_input_owned,
            finish_reason: &finish_reason.clone(),
            available_tools: &available_tools,
            turn_start,
            model,
            all_user_inputs,
            max_turns,
        };
        let metrics = TurnMetrics {
            ttft_ms,
            llm_duration_ms,
            usage: &usage,
            text_len,
            has_thinking,
            thinking_bytes,
        };

        if reasoning_only {
            return self.handle_reasoning_only(&turn_ctx, &metrics).await;
        }

        self.handle_post_llm_result(
            &turn_ctx,
            &metrics,
            full_text,
            reasoning_text,
            parse,
            &available_tools,
            turn_count,
            finish_reason,
            event_rx,
            on_event,
        )
        .await
    }

    // ── Branch handlers ─────────────────────────────────────────────────

    /// Reasoning-only: the model emitted thinking content but no answer and no
    /// tool call. Nudge it to commit; fail after consecutive strikes.
    async fn handle_reasoning_only(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
    ) -> AgentResult<TurnFlow> {
        let (strikes, thinking_disabled) = self
            .with_session_mut(turn_ctx.session_id, |session| {
                let strikes = session.run_state.record_reasoning_only();
                (strikes, session.run_state.thinking_disabled_for_rest_of_run)
            })
            .await?;
        if thinking_disabled {
            // The observability for the strike-3 flip lives here: RunState is
            // a pure type (agent-types stays I/O-free).
            tracing::info!(
                strikes,
                "reasoning_only_strikes reached 3, disabling thinking for rest of run"
            );
        }
        tracing::warn!(
            session_id = turn_ctx.session_id.id,
            turn = turn_ctx.turn_count,
            strikes,
            thinking_disabled,
            "reasoning-only response with tools available — nudging the model to commit"
        );

        let guard_ctx = self
            .build_guard_ctx(turn_ctx, "", true, false, false, thinking_disabled)
            .await;
        let decision = self.guard.on_turn(&guard_ctx).await;
        self.execute_guard_decision(decision, turn_ctx, metrics)
            .await
    }

    /// Normal LLM response: apply post-LLM middleware, push messages, then
    /// dispatch on the backend's four-state parse verdict (M3 seam).
    #[allow(clippy::too_many_arguments)]
    async fn handle_post_llm_result<F>(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
        full_text: String,
        reasoning_text: String,
        parse: ParseOutcome,
        available_tools: &[String],
        turn_count: u32,
        finish_reason: FinishReason,
        event_rx: &mut broadcast::Receiver<RuntimeEvent>,
        on_event: Arc<Mutex<F>>,
    ) -> AgentResult<TurnFlow>
    where
        F: FnMut(RuntimeEvent) -> AgentResult<()> + Send + 'static,
    {
        let result = self
            .apply_post_llm_mw(
                turn_ctx.session_id,
                full_text,
                parse,
                available_tools,
                turn_count,
                finish_reason.clone(),
            )
            .await?;

        let PostLlmMwResult {
            full_text,
            parse,
            skip_push,
            follow_up_message,
        } = result;

        // Push text-only assistant message (when there is no action intent).
        if !skip_push && !full_text.is_empty() && !parse.has_action_intent() {
            let reasoning = reasoning_text.clone();
            self.with_session_mut(turn_ctx.session_id, |session| {
                if !reasoning.is_empty() {
                    session.push_assistant_with_reasoning(&full_text, &reasoning);
                } else {
                    session.push_message(MessageRole::Assistant, &full_text);
                }
            })
            .await?;
        }

        if let Some(follow_up) = follow_up_message {
            self.with_session_mut(turn_ctx.session_id, |session| {
                session.push_message(MessageRole::User, &follow_up);
            })
            .await?;
            return Ok(TurnFlow::Continue);
        }

        // Four-state dispatch (D4+OV-7) — behavior-identical to the old
        // is_tool_call/tool_calls chain. Empty text wins over Malformed: a
        // truncated finish with no text still takes the old empty branch.
        match parse {
            ParseOutcome::Actions(actions) if !actions.is_empty() => {
                // Tool-format actions run the tool flow (the built-in
                // backend's own protocol: truncation rescue, breaker,
                // recovery). Any other format came from the mounted action
                // backend and runs its flow (T10c).
                if actions[0].format == ActionFormat::ToolCall {
                    self.handle_tool_call_branch(
                        turn_ctx,
                        metrics,
                        actions,
                        full_text,
                        reasoning_text,
                        event_rx,
                        on_event,
                    )
                    .await
                } else {
                    self.handle_backend_action_branch(
                        turn_ctx, metrics, actions, event_rx, on_event,
                    )
                    .await
                }
            }
            ParseOutcome::Incomplete | ParseOutcome::Actions(_) => {
                self.handle_incomplete_tool_call(turn_ctx, metrics).await
            }
            ParseOutcome::Empty | ParseOutcome::Malformed => {
                if full_text.is_empty() {
                    self.handle_empty_response(turn_ctx, metrics).await
                } else {
                    self.handle_finish_anomaly(turn_ctx, metrics, &finish_reason, &full_text)
                        .await
                }
            }
        }
    }

    /// Empty response: no text, no reasoning, no tool call. Nudge the model;
    /// fail after consecutive strikes.
    async fn handle_empty_response(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
    ) -> AgentResult<TurnFlow> {
        let strikes = self
            .with_session_mut(turn_ctx.session_id, |session| {
                session.run_state.record_empty_response()
            })
            .await?;
        tracing::warn!(
            session_id = turn_ctx.session_id.id,
            turn = turn_ctx.turn_count,
            strikes,
            "empty LLM response (no text, no reasoning, no tool call)"
        );

        let guard_ctx = self
            .build_guard_ctx(turn_ctx, "", false, true, false, false)
            .await;
        let decision = self.guard.on_turn(&guard_ctx).await;
        self.execute_guard_decision(decision, turn_ctx, metrics)
            .await
    }

    /// Tool call branch: notify guard, handle RestoreThinking, then execute.
    #[allow(clippy::too_many_arguments)]
    async fn handle_tool_call_branch<F>(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
        actions: Vec<Action>,
        full_text: String,
        reasoning_text: String,
        event_rx: &mut broadcast::Receiver<RuntimeEvent>,
        on_event: Arc<Mutex<F>>,
    ) -> AgentResult<TurnFlow>
    where
        F: FnMut(RuntimeEvent) -> AgentResult<()> + Send + 'static,
    {
        // 1. Notify guard about tool call
        let thinking_disabled = self
            .session_manager
            .session_or_err(turn_ctx.session_id)
            .await
            .map(|s| s.run_state.thinking_disabled_for_rest_of_run)
            .unwrap_or(false);
        let guard_ctx = self
            .build_guard_ctx(turn_ctx, "", false, false, false, thinking_disabled)
            .await;
        let decision = self.guard.on_tool_call(&guard_ctx).await;

        // 2. Handle RestoreThinking (update RunState)
        if matches!(
            decision,
            crate::engine::react_loop_guard::GuardDecision::RestoreThinking
        ) {
            self.execute_guard_decision(decision, turn_ctx, metrics)
                .await?;
        }

        // 3. Execute tools regardless of guard decision
        ToolCallExecutor::new(self, turn_ctx.session_id.clone())
            .run_actions(
                turn_ctx.user_input,
                turn_ctx.turn_count,
                turn_ctx.turn_start,
                turn_ctx.model,
                actions,
                turn_ctx.finish_reason,
                reasoning_text,
                full_text,
                metrics,
                event_rx,
                on_event,
            )
            .await
    }

    /// Mounted-backend action branch (T10c): the same turn plumbing the tool
    /// backend gets — guard notification, 账+数 checkpoints, turn-end metrics —
    /// around the backend's own `execute` and history protocol. The tool
    /// backend's truncation rescue / markup salvage / circuit breaker is its
    /// protocol and does not run here (flow-face unification stays the named
    /// T10 debt). An interrupt mid-batch leaves the batch dangling from
    /// `BeforeActions` on — resume synthesizes its fates (OV-4).
    #[allow(clippy::too_many_arguments)]
    async fn handle_backend_action_branch<F>(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
        actions: Vec<Action>,
        event_rx: &mut broadcast::Receiver<RuntimeEvent>,
        on_event: Arc<Mutex<F>>,
    ) -> AgentResult<TurnFlow>
    where
        F: FnMut(RuntimeEvent) -> AgentResult<()> + Send + 'static,
    {
        let session_id = turn_ctx.session_id;
        let Some(backend) = &self.action_backend else {
            return Err(AgentError::internal(
                "non-tool actions with no action backend mounted (routing bug)",
            ));
        };

        // 1. Notify the guard about the action batch (D5 counts actions the
        //    same way calls are counted) and honor RestoreThinking — the same
        //    turn plumbing the tool branch runs.
        let thinking_disabled = self
            .session_manager
            .session_or_err(session_id)
            .await
            .map(|s| s.run_state.thinking_disabled_for_rest_of_run)
            .unwrap_or(false);
        let guard_ctx = self
            .build_guard_ctx(turn_ctx, "", false, false, false, thinking_disabled)
            .await;
        let decision = self.guard.on_tool_call(&guard_ctx).await;
        if matches!(
            decision,
            crate::engine::react_loop_guard::GuardDecision::RestoreThinking
        ) {
            self.execute_guard_decision(decision, turn_ctx, metrics)
                .await?;
        }

        let mut slot = backend.lock().await;
        // 2. Session liveness (D10): one rebuild attempt, never a loop.
        if !slot.is_alive() {
            slot.rebuild().await.map_err(|e| {
                AgentError::internal(format!("action backend session rebuild failed: {e}"))
            })?;
        }
        let format = slot.format();

        // 3. 账+数 in one write (D5+OV-11): the step and the counting state
        //    that goes with it land on the journal together.
        let run_state = self
            .with_session_mut(session_id, |session| session.run_state.clone())
            .await?;
        self.event_bus.emit(RuntimeEvent::Checkpoint {
            session_id: session_id.clone(),
            checkpoint: Box::new(CheckpointData::new(
                session_id.clone(),
                turn_ctx.user_input.to_string(),
                CheckpointStep::BeforeActions {
                    actions: actions.clone(),
                },
                turn_ctx.turn_count,
                format,
                run_state,
            )),
            agent_id: None,
            trace_id: None,
        });

        // 4. Execute against the run's ExecCtx (cancel + interaction). A
        //    user interrupt releases the batch mid-compute.
        let tool_start = std::time::Instant::now();
        let ctx = self.exec_ctx();
        let executed = tokio::select! {
            biased;
            out = slot.execute(actions.clone(), &ctx) => Some(out),
            _ = ctx.cancel.cancelled() => None,
        };
        let n = actions.len() as u32;
        let names: Vec<String> = actions.iter().map(|a| a.name.clone()).collect();
        let duration_ms = tool_start.elapsed().as_millis() as u64;
        match executed {
            None => {
                // Interrupt: nothing after BeforeActions is written, so the
                // batch stays dangling and resume reconstructs its fates (D3).
                // Same terminal shape as the tool flow's cancelled error:
                // drain, persist, ride up (RunCancelled comes from run_turn).
                drop(slot);
                drain_locked(event_rx, &on_event)?;
                let session = self.session_manager.session_or_err(session_id).await?;
                if let Err(e) = self.session_manager.session_store().save(&session).await {
                    tracing::warn!(session_id = session_id.id, error = %e, "Failed to persist session");
                    let config = self.config_snapshot_async().await;
                    if config.execution.fail_on_persist_error {
                        return Err(AgentError::internal(format!(
                            "Session persistence failed: {e}"
                        )));
                    }
                }
                Err(AgentError::Cancelled)
            }
            Some(Ok(observation)) => {
                // The backend's protocol pair, pushed verbatim (D7 — the
                // kernel never interprets history_messages).
                self.with_session_mut(session_id, |session| {
                    for msg in &observation.history_messages {
                        session.push_chat_message(msg.clone());
                    }
                })
                .await?;
                // 5. Counting + AfterActions in one write (D5+OV-11): the
                //    batch ran to fates (D15), so it counts as one batch.
                let run_state = self
                    .with_session_mut(session_id, |session| {
                        session.run_state.record_actions(
                            actions.len(),
                            &actions.iter().map(|a| a.name.clone()).collect::<Vec<_>>(),
                        );
                        session.run_state.clone()
                    })
                    .await?;
                let ok = observation.outcomes.iter().filter(|o| o.ok).count() as u32;
                self.event_bus.emit(RuntimeEvent::Checkpoint {
                    session_id: session_id.clone(),
                    checkpoint: Box::new(CheckpointData::new(
                        session_id.clone(),
                        turn_ctx.user_input.to_string(),
                        CheckpointStep::AfterActions {
                            actions,
                            observation: Some(observation),
                        },
                        turn_ctx.turn_count,
                        format,
                        run_state,
                    )),
                    agent_id: None,
                    trace_id: None,
                });
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    tool_duration_ms: duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    tool_call_count: n,
                    tools_used: &names,
                    tool_success: ok,
                    tool_failed: n - ok,
                    llm_calls: 1,
                    ..TurnEndCtx::new(
                        session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Completed,
                    )
                })
                .await;
                Ok(TurnFlow::Continue)
            }
            Some(Err(ExecError::NothingRan { reason })) => {
                // D15: not one action got off the ground. The fates say so —
                // the backend renders them in its own shape — and the batch
                // still lands as a checkpoint pair, so resume replays this
                // story verbatim instead of synthesizing a second one. The
                // tool backend's consecutive-failure recovery is tool
                // protocol and does not apply here; the model reads the
                // reason and adapts.
                let fates: Vec<ActionOutcome> = actions
                    .iter()
                    .map(|a| ActionOutcome {
                        action_id: a.id.clone(),
                        name: a.name.clone(),
                        ok: false,
                        denied: false,
                        summary: format!("not run ({reason})"),
                        details: None,
                    })
                    .collect();
                let history_messages = slot.render_fates(&actions, &fates);
                drop(slot);
                self.with_session_mut(session_id, |session| {
                    for msg in &history_messages {
                        session.push_chat_message(msg.clone());
                    }
                })
                .await?;
                let run_state = self
                    .with_session_mut(session_id, |session| session.run_state.clone())
                    .await?;
                self.event_bus.emit(RuntimeEvent::Checkpoint {
                    session_id: session_id.clone(),
                    checkpoint: Box::new(CheckpointData::new(
                        session_id.clone(),
                        turn_ctx.user_input.to_string(),
                        CheckpointStep::AfterActions {
                            actions,
                            observation: Some(Observation {
                                outcomes: fates,
                                events: vec![],
                                history_messages,
                                batch_status: BatchStatus::Completed,
                            }),
                        },
                        turn_ctx.turn_count,
                        format,
                        run_state,
                    )),
                    agent_id: None,
                    trace_id: None,
                });
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    tool_duration_ms: duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    tool_call_count: n,
                    tools_used: &names,
                    tool_failed: n,
                    error_message: Some(&reason),
                    llm_calls: 1,
                    ..TurnEndCtx::new(
                        session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Failed {
                            error: reason.clone(),
                        },
                    )
                })
                .await;
                Ok(TurnFlow::Continue)
            }
        }
    }

    /// Incomplete tool call: model signalled tool_use but no complete tool calls
    /// were parsed (stream cut off mid-delta, JSON incomplete, etc.).
    async fn handle_incomplete_tool_call(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
    ) -> AgentResult<TurnFlow> {
        let strikes = self
            .with_session_mut(turn_ctx.session_id, |session| {
                session.run_state.record_empty_response()
            })
            .await?;
        tracing::warn!(
            session_id = turn_ctx.session_id.id,
            turn = turn_ctx.turn_count,
            strikes,
            "model signalled tool call but no complete tool calls were parsed"
        );
        let guard_ctx = self
            .build_guard_ctx(turn_ctx, "", false, true, false, false)
            .await;
        let decision = self.guard.on_turn(&guard_ctx).await;
        self.execute_guard_decision(decision, turn_ctx, metrics)
            .await
    }

    /// Finish-reason anomaly: tool_use with no parsed tool calls, or truncated.
    async fn handle_finish_anomaly(
        &self,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
        finish_reason: &FinishReason,
        full_text: &str,
    ) -> AgentResult<TurnFlow> {
        match finish_reason {
            FinishReason::ToolUse => {
                let error = "finish_reason=tool_use but no tool calls were parsed";
                tracing::warn!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    error,
                    "model signalled tool_use but no tool calls were parsed"
                );
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    llm_calls: 1,
                    error_message: Some(error),
                    ..TurnEndCtx::new(
                        turn_ctx.session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Failed {
                            error: error.to_string(),
                        },
                    )
                })
                .await;
                Ok(TurnFlow::Done(RunOutcome::Failed {
                    error: error.to_string(),
                }))
            }
            FinishReason::Truncated { reason } => {
                let error = format!("response truncated ({:?})", reason);
                tracing::warn!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    ?reason,
                    "text-only response truncated by token limit"
                );
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    llm_calls: 1,
                    error_message: Some(&error),
                    ..TurnEndCtx::new(
                        turn_ctx.session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Failed {
                            error: error.clone(),
                        },
                    )
                })
                .await;
                Ok(TurnFlow::Done(RunOutcome::Failed { error }))
            }
            _ => {
                // Text-only branch — use the guard to determine completion.
                let guard_ctx = self
                    .build_guard_ctx(turn_ctx, full_text, false, false, true, false)
                    .await;
                let decision = self.guard.on_turn(&guard_ctx).await;
                self.execute_guard_decision(decision, turn_ctx, metrics)
                    .await
            }
        }
    }

    /// LLM stream error: fire turn-end and persist session on cancellation.
    async fn handle_llm_error(
        &self,
        session_id: &SessionId,
        turn_count: u32,
        turn_start: std::time::Instant,
        model: &str,
        user_input_owned: &str,
        e: crate::types::AgentError,
    ) -> AgentResult<TurnFlow> {
        let stream_outcome = if e.is_cancelled() {
            RunOutcome::Cancelled
        } else {
            RunOutcome::Failed {
                error: e.to_string(),
            }
        };
        self.fire_turn_end(TurnEndCtx {
            error_message: Some(&e.to_string()),
            ..TurnEndCtx::new(
                session_id,
                turn_count,
                turn_start,
                model,
                user_input_owned,
                stream_outcome,
            )
        })
        .await;
        // Persist session on cancellation (LLM-stream path bypasses handle_tool_error)
        if e.is_cancelled()
            && let Ok(session) = self.session_manager.session_or_err(session_id).await
        {
            let _ = self.session_manager.session_store().save(&session).await;
        }
        Err(e)
    }

    // ── Guard decision execution ────────────────────────────────────────

    /// Execute a guard decision — fire events, inject nudge, or terminate.
    pub(super) async fn execute_guard_decision(
        &self,
        decision: crate::engine::react_loop_guard::GuardDecision,
        turn_ctx: &TurnCtx<'_>,
        metrics: &TurnMetrics<'_>,
    ) -> AgentResult<TurnFlow> {
        use crate::engine::react_loop_guard::GuardDecision;
        match decision {
            GuardDecision::Continue { nudge } => {
                tracing::info!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    "guard decided to continue"
                );
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    llm_calls: 1,
                    ..TurnEndCtx::new(
                        turn_ctx.session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Continuing,
                    )
                })
                .await;
                if let Some(msg) = nudge {
                    self.with_session_mut(turn_ctx.session_id, |session| {
                        session.push_message(MessageRole::User, &msg);
                    })
                    .await?;
                }
                Ok(TurnFlow::Continue)
            }
            GuardDecision::Complete => {
                tracing::info!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    "guard decided task is complete"
                );
                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    llm_calls: 1,
                    ..TurnEndCtx::new(
                        turn_ctx.session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Completed,
                    )
                })
                .await;
                Ok(TurnFlow::Done(RunOutcome::Completed))
            }
            GuardDecision::Fail { error } => {
                tracing::warn!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    error = %error,
                    "guard decided to fail"
                );
                self.fire_guard_fail(turn_ctx, metrics, &error).await;
                Ok(TurnFlow::Done(RunOutcome::Failed { error }))
            }
            GuardDecision::DisableThinking { nudge } => {
                self.with_session_mut(turn_ctx.session_id, |session| {
                    session.run_state.thinking_disabled_for_rest_of_run = true;
                    session.push_message(MessageRole::User, &nudge);
                })
                .await?;

                self.fire_turn_end(TurnEndCtx {
                    ttft_ms: metrics.ttft_ms,
                    llm_duration_ms: metrics.llm_duration_ms,
                    usage: metrics.usage,
                    text_length: metrics.text_len,
                    has_thinking: metrics.has_thinking,
                    thinking_bytes: metrics.thinking_bytes,
                    llm_calls: 1,
                    ..TurnEndCtx::new(
                        turn_ctx.session_id,
                        turn_ctx.turn_count,
                        turn_ctx.turn_start,
                        turn_ctx.model,
                        turn_ctx.user_input,
                        RunOutcome::Continuing,
                    )
                })
                .await;

                tracing::info!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    "thinking disabled by guard"
                );
                Ok(TurnFlow::Continue)
            }
            GuardDecision::RestoreThinking => {
                let original = self
                    .session_manager
                    .session_or_err(turn_ctx.session_id)
                    .await
                    .map(|s| s.run_state.original_thinking_enabled)
                    .unwrap_or(false);

                self.with_session_mut(turn_ctx.session_id, |session| {
                    session.run_state.thinking_disabled_for_rest_of_run = !original;
                })
                .await?;

                tracing::info!(
                    session_id = turn_ctx.session_id.id,
                    turn = turn_ctx.turn_count,
                    thinking_enabled = original,
                    "thinking restored by guard"
                );
                Ok(TurnFlow::Continue)
            }
        }
    }
}