Skip to main content

tirea_contract/runtime/phase/
types.rs

1pub use crate::runtime::run::flow::RunAction;
2pub use crate::runtime::tool_call::gate::{SuspendTicket, ToolCallAction};
3
4/// Execution phase in the agent loop.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum Phase {
7    /// Thread started (called once).
8    RunStart,
9    /// Step started - prepare context.
10    StepStart,
11    /// Before LLM inference - build messages, filter tools.
12    BeforeInference,
13    /// After LLM inference - process response.
14    AfterInference,
15    /// Before tool execution.
16    BeforeToolExecute,
17    /// After tool execution.
18    AfterToolExecute,
19    /// Step ended.
20    StepEnd,
21    /// Thread ended (called once).
22    RunEnd,
23}
24
25impl std::fmt::Display for Phase {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Self::RunStart => write!(f, "RunStart"),
29            Self::StepStart => write!(f, "StepStart"),
30            Self::BeforeInference => write!(f, "BeforeInference"),
31            Self::AfterInference => write!(f, "AfterInference"),
32            Self::BeforeToolExecute => write!(f, "BeforeToolExecute"),
33            Self::AfterToolExecute => write!(f, "AfterToolExecute"),
34            Self::StepEnd => write!(f, "StepEnd"),
35            Self::RunEnd => write!(f, "RunEnd"),
36        }
37    }
38}
39
40/// Mutation policy enforced for each phase.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct PhasePolicy {
43    /// Whether tool filtering (`StepContext::tools`) can be mutated.
44    pub allow_tool_filter_mutation: bool,
45    /// Whether `StepContext::run_action` can be mutated.
46    pub allow_run_action_mutation: bool,
47    /// Whether tool execution gate (`blocked/pending`) can be mutated.
48    pub allow_tool_gate_mutation: bool,
49}
50
51impl PhasePolicy {
52    pub const fn read_only() -> Self {
53        Self {
54            allow_tool_filter_mutation: false,
55            allow_run_action_mutation: false,
56            allow_tool_gate_mutation: false,
57        }
58    }
59}
60
61impl Phase {
62    /// Return mutation policy for this phase.
63    pub const fn policy(self) -> PhasePolicy {
64        match self {
65            Self::BeforeInference => PhasePolicy {
66                allow_tool_filter_mutation: true,
67                allow_run_action_mutation: true,
68                allow_tool_gate_mutation: false,
69            },
70            Self::AfterInference => PhasePolicy {
71                allow_tool_filter_mutation: false,
72                allow_run_action_mutation: true,
73                allow_tool_gate_mutation: false,
74            },
75            Self::BeforeToolExecute => PhasePolicy {
76                allow_tool_filter_mutation: false,
77                allow_run_action_mutation: false,
78                allow_tool_gate_mutation: true,
79            },
80            Self::RunStart
81            | Self::StepStart
82            | Self::AfterToolExecute
83            | Self::StepEnd
84            | Self::RunEnd => PhasePolicy::read_only(),
85        }
86    }
87}
88
89/// Result of a step execution.
90#[derive(Debug, Clone, PartialEq)]
91pub enum StepOutcome {
92    /// Continue to next step.
93    Continue,
94    /// Thread complete.
95    Complete,
96    /// Pending external suspension.
97    Pending(Box<SuspendTicket>),
98}