Skip to main content

ag_session/execution_graph/
lifecycle.rs

1use std::collections::BTreeMap;
2use std::num::NonZeroU64;
3
4use ag_protocol::TurnPromptAttachment;
5use serde::{Deserialize, Serialize};
6
7use super::{GraphCompletionGate, GraphExecutionRef};
8
9/// Revision-bound graph control; cancellation is terminal for this graph ID.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub struct GraphControlRequest {
12    /// Owning graph identity.
13    pub graph_id: String,
14    /// Expected current immutable revision.
15    pub revision: NonZeroU64,
16}
17
18/// Explicit restart of dispatch, never an implicit retry of an admitted node.
19#[derive(Clone, Debug, Eq, PartialEq)]
20pub struct ResumeExecutionGraphRequest {
21    /// Graph to resume.
22    pub graph: GraphControlRequest,
23    /// Fresh limits after a process restart. A retained live executor keeps
24    /// its remaining credits and requires its original limits.
25    pub provider_call_limits: BTreeMap<String, usize>,
26}
27
28/// An explicit action against the exact latest execution of a node.
29#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
30pub struct ContinueGraphNodeRequest {
31    /// New attempt/run UUID, also the idempotency identity of this action.
32    pub action_id: String,
33    /// Exact source execution, including its task and operation identities.
34    pub execution: GraphExecutionRef,
35    /// Owning graph.
36    pub graph_id: String,
37    /// Requested continuation.
38    pub kind: GraphNodeContinuation,
39    /// Node to continue.
40    pub node_id: String,
41    /// Expected graph revision.
42    pub revision: NonZeroU64,
43}
44
45/// Provider retries stay inside an attempt; these actions create a new one.
46#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
47pub enum GraphNodeContinuation {
48    /// Retry only a failed, canceled, or interrupted execution.
49    Retry,
50    /// Answer the complete ordered question set from a settled task turn.
51    Answer {
52        /// Ordered answers paired with their exact question text.
53        answers: Vec<GraphQuestionAnswer>,
54    },
55    /// Free-form user guidance for the same task conversation. It may retry
56    /// failed or canceled work, answer clarification questions in prose,
57    /// revise a validated output that has not been accepted, or follow up on
58    /// settled work. Accepted outputs and nodes whose dependents have started
59    /// are final.
60    Reply {
61        /// Images accompanying the guidance, delivered with later attempts.
62        attachments: Vec<TurnPromptAttachment>,
63        /// User-authored guidance.
64        message: String,
65    },
66}
67
68/// One answer to the exact question produced by the source execution.
69#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
70pub struct GraphQuestionAnswer {
71    /// User-supplied response.
72    pub answer: String,
73    /// Exact question text, in its original position.
74    pub question: String,
75}
76
77/// Read-only projection; operation/run records remain execution authorities.
78#[derive(Clone, Debug, Eq, PartialEq)]
79pub struct ExecutionGraphProgress {
80    /// Graph identity.
81    pub graph_id: String,
82    /// Declaration-order node progress, including all execution identities.
83    pub nodes: Vec<GraphNodeProgress>,
84    /// Current immutable revision.
85    pub revision: NonZeroU64,
86    /// Owning session.
87    pub session_id: crate::SessionId,
88    /// Aggregate execution state, independent of desired-outcome acceptance.
89    pub status: ExecutionGraphStatus,
90}
91
92/// Graph completion means every node settled without clarification.
93#[derive(Clone, Copy, Debug, Eq, PartialEq)]
94pub enum ExecutionGraphStatus {
95    /// Dispatch is eligible or work is queued/running.
96    Running,
97    /// Prerequisites, acceptance, clarification, or resources prevent progress.
98    Blocked,
99    /// No live driver; an explicit resume is required.
100    Paused,
101    /// Every node settled successfully without pending questions.
102    Completed,
103    /// At least one node requires an explicit retry.
104    Failed,
105    /// Durable cancellation closed admission, including unstarted nodes.
106    Cancelled,
107}
108
109/// One node's latest state plus immutable attempt/run history.
110#[derive(Clone, Debug, Eq, PartialEq)]
111pub struct GraphNodeProgress {
112    /// Exact unsatisfied requirements for pending work.
113    pub blocked: Vec<GraphBlockedReason>,
114    /// Every admitted execution in order; retries append rather than replace.
115    pub executions: Vec<GraphExecutionRef>,
116    /// Last authoritative failure, if any.
117    pub last_error: Option<String>,
118    /// Node identity.
119    pub node_id: String,
120    /// Questions from the latest settled response only.
121    pub questions: Vec<crate::QuestionItem>,
122    /// Outputs produced by the latest execution, with their evidence state.
123    pub results: Vec<super::GraphResult>,
124    /// State projected from the latest operation/run and settled response.
125    pub status: GraphNodeStatus,
126}
127
128/// Node state never overwrites authoritative operation/run status.
129#[derive(Clone, Copy, Debug, Eq, PartialEq)]
130pub enum GraphNodeStatus {
131    /// Not admitted; inspect blocked reasons.
132    Pending,
133    /// Durable work is waiting for worker selection.
134    Queued,
135    /// Worker owns execution or ordered effects.
136    Running,
137    /// Settled task turn requires a complete clarification reply.
138    AwaitingAnswers,
139    /// Settled response has no pending questions.
140    Succeeded,
141    /// Failed execution or incomplete settlement requires explicit retry.
142    Failed,
143    /// This execution or its graph was canceled.
144    Cancelled,
145}
146
147/// Stable reasons why a pending node cannot currently dispatch.
148#[derive(Clone, Debug, Eq, PartialEq)]
149pub enum GraphBlockedReason {
150    /// A declared predecessor gate has not been satisfied.
151    Dependency {
152        /// Required gate.
153        gate: GraphCompletionGate,
154        /// Predecessor node.
155        node_id: String,
156    },
157    /// Conflicting durable Env work is still settling.
158    EnvironmentBusy,
159    /// Host capacity or a live Env lease must become available.
160    AwaitingDispatch,
161    /// Named shared provider-call budget has no remaining credit.
162    BudgetExhausted(String),
163    /// Explicit resume is required after the driver stops or restarts.
164    ResumeRequired,
165}