Skip to main content

agent_types/
guard.rs

1//! Guard-related pure types: GuardDecision, GuardCtx.
2
3use serde::{Deserialize, Serialize};
4
5use crate::execution::FinishReason;
6use crate::session::SessionId;
7
8/// Guard decision — returned by guard, executed by base loop.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub enum GuardDecision {
11    /// Continue loop, optionally inject nudge message
12    Continue { nudge: Option<String> },
13    /// Normal completion (fire_turn_end + RunOutcome::Completed)
14    Complete,
15    /// Abnormal termination (fire_guard_fail + RunOutcome::Failed)
16    Fail { error: String },
17
18    // ─── Thinking control ─────────────────────────────────────
19    /// Temporarily disable thinking functionality
20    ///
21    /// Used for reasoning-only loop scenarios: model keeps thinking but produces no output.
22    /// After calling, runtime will:
23    /// 1. Set thinking_disabled_for_rest_of_run = true
24    /// 2. Inject nudge message
25    /// 3. Continue loop
26    DisableThinking { nudge: String },
27
28    /// Restore thinking functionality to previous state
29    ///
30    /// Used for thinking recovery scenarios: model starts working normally (has text or tool call).
31    /// After calling, runtime will:
32    /// 1. Restore thinking_disabled_for_rest_of_run to original state
33    /// 2. Reset related counters
34    RestoreThinking,
35}
36
37/// Guard context information — built by runtime, passed to guard.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct GuardCtx {
40    pub session_id: SessionId,
41    pub turn_count: u32,
42    pub user_input: String,
43    pub model_response: String,
44    pub finish_reason: FinishReason,
45    pub available_tools: Vec<String>,
46    // RunState information
47    pub reasoning_only_strikes: usize,
48    pub empty_response_strikes: usize,
49    #[serde(alias = "run_has_tool_calls")]
50    pub run_has_actions: bool,
51    /// The tools this run has called so far, in order, by name. A completion
52    /// judge handed only `user_input` and `model_response` cannot tell a report
53    /// from a shrug — both are a short text-only turn. This is the work it can
54    /// check the response against. Names only; nothing sensitive rides along.
55    #[serde(default)]
56    pub run_actions: Vec<String>,
57    /// The most recent assistant tool calls were ALL rejected before
58    /// execution (truncated/invalid arguments) and the model was told to
59    /// re-issue them (`RunState.truncation_strikes > 0`). While this is
60    /// true, a text-only response cannot be task completion — the work the
61    /// text describes was never executed (session 20260904_efad759c: a
62    /// fabricated success narrative passed the completion judge).
63    pub last_tool_calls_invalid: bool,
64    /// All user messages in the current session, ordered oldest-first.
65    /// Guards can use this to reconstruct full conversation context
66    /// (e.g. "continue" after a multi-turn discussion).
67    pub all_user_inputs: Vec<String>,
68    // Scene hints (runtime detected, guard can trust or ignore)
69    pub is_reasoning_only: bool,
70    pub is_empty_response: bool,
71    pub is_text_only: bool,
72    /// The run ended with a human step pending: the latest tool batch parked
73    /// on human input (a design-canvas screen awaiting a pick) and no tool ran
74    /// after it. Ending here is *waiting for the user*, not an incomplete task
75    /// — the next input is their answer. A "continue working" nudge in this
76    /// state steamrolls interactive flows (session 20261022_bb8c2dc3: the
77    /// completion judge pushed the model on and it decided the user's design
78    /// choices for them).
79    #[serde(default)]
80    pub awaiting_human: bool,
81    // Environment state
82    pub thinking_disabled: bool,
83    /// Original thinking configuration (for restoration)
84    ///
85    /// From RunState.original_thinking_enabled
86    pub original_thinking_enabled: bool,
87    /// Remaining turns before hitting max_turns limit
88    ///
89    /// Guards can use this to nudge the model to wrap up when running low on turns.
90    /// When remaining_turns == 0, the run will be terminated with MaxTurnsExceeded.
91    pub remaining_turns: u32,
92}