Skip to main content

ag_session/execution_graph/
lifecycle.rs

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