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