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