ag_session/execution_graph/session_graph.rs
1//! Read models a frontend shows for a session's graph proposals and execution
2//! graphs: a compact summary beside the chat and an inspector snapshot with
3//! the actions it offers, plus the snapshot-bound action request. Hosts fill
4//! every label and offered action with the wording and rules their own views
5//! use, so frontends show them without restating them.
6
7use std::num::NonZeroU64;
8
9use super::{ExecutionGraphStatus, GraphNode, GraphNodeProgress, GraphProposalState, GraphResult};
10use crate::{QuestionItem, ReasoningLevel, SessionId};
11
12/// What a frontend shows beside a session's chat about its latest graph
13/// proposal and, once approved, the graph that proposal launched.
14#[derive(Clone, Debug, Eq, PartialEq)]
15pub struct SessionGraphSummary {
16 /// Nodes whose validated output a dependent waits for the user to
17 /// accept, in declaration order.
18 pub awaiting_approval: Vec<SessionGraphApproval>,
19 /// Nodes of the launched graph that succeeded.
20 pub done: usize,
21 /// Line describing `phase`, or the launched graph's status, as the host
22 /// shows it.
23 pub label: String,
24 /// Where the latest proposal stands.
25 pub phase: SessionGraphProposalPhase,
26 /// Live status of the approved graph, once the host reads it.
27 pub status: Option<ExecutionGraphStatus>,
28 /// Nodes of the resolved proposal; zero before one resolves and after
29 /// cancellation.
30 pub total: usize,
31 /// Number of the latest proposal version.
32 pub version: u32,
33}
34
35/// One node output that waits for the user's approval.
36#[derive(Clone, Debug, Eq, PartialEq)]
37pub struct SessionGraphApproval {
38 /// Node that produced the output.
39 pub node_id: String,
40 /// Whether a chat reply may also revise the output instead of accepting
41 /// it.
42 pub takes_revision: bool,
43 /// Line telling the user how to act on it, as the host shows it.
44 pub text: String,
45}
46
47/// Where a graph proposal version stands.
48#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum SessionGraphProposalPhase {
50 /// The user approved this version and its graph launched.
51 Approved,
52 /// The version resolved to a graph that waits for the user's approval.
53 AwaitingApproval,
54 /// The proposal was discarded; no graph launched.
55 Cancelled,
56 /// Generation failed; a reply can revise or cancel it.
57 Failed,
58 /// The model is still generating this version.
59 Generating,
60 /// The model asked clarification questions first.
61 NeedsInput,
62 /// The model proposed a graph the host could not resolve.
63 Unresolved,
64}
65
66/// Which graph a [`SessionGraphs`] read selects, and which of its nodes.
67#[derive(Clone, Debug, Eq, PartialEq)]
68pub struct SessionGraphsRequest {
69 /// Graph to select; it must belong to `session_id`. `None` selects the
70 /// latest proposal's approved graph, or else the session's first graph.
71 pub graph_id: Option<String>,
72 /// Node of the selected graph to select; `None`, or a node the graph no
73 /// longer has, selects its first node.
74 pub node_id: Option<String>,
75 /// Session whose graphs to read.
76 pub session_id: SessionId,
77}
78
79/// A session's graphs as an inspector shows them, without any action.
80#[derive(Clone, Debug, Eq, PartialEq)]
81pub struct SessionGraphs {
82 /// The selected graph, or `None` when the session has none.
83 pub graph: Option<SessionGraph>,
84 /// Every graph of the session in creation order.
85 pub graphs: Vec<SessionGraphEntry>,
86 /// The session's latest graph proposal, if any.
87 pub proposal: Option<SessionGraphProposal>,
88}
89
90/// One graph of a session in a [`SessionGraphs`] listing.
91#[derive(Clone, Debug, Eq, PartialEq)]
92pub struct SessionGraphEntry {
93 /// Graph identity.
94 pub id: String,
95 /// Whether the graph is a focused review that the host drives.
96 pub is_focused_review: bool,
97 /// Current immutable revision.
98 pub revision: NonZeroU64,
99}
100
101/// A session's latest graph proposal with its rendered preview.
102#[derive(Clone, Debug, Eq, PartialEq)]
103pub struct SessionGraphProposal {
104 /// How the user acts on the proposal while it is pending, as the host
105 /// says it; `None` once it is approved or cancelled.
106 pub hint: Option<String>,
107 /// Where the proposal stands.
108 pub phase: SessionGraphProposalPhase,
109 /// The proposal's details as Markdown, with its captured request and
110 /// node instructions quoted.
111 pub preview: String,
112 /// Clarification questions the version asks before it can resolve.
113 pub questions: Vec<QuestionItem>,
114 /// The retained proposal state.
115 pub state: GraphProposalState,
116}
117
118/// The selected graph of a [`SessionGraphs`] read with its live progress.
119#[derive(Clone, Debug, Eq, PartialEq)]
120pub struct SessionGraph {
121 /// Actions the graph takes as a whole now, among
122 /// [`SessionGraphActionKind::Cancel`],
123 /// [`SessionGraphActionKind::ExtendBudget`], and
124 /// [`SessionGraphActionKind::Resume`], as the host's inspector offers
125 /// them.
126 pub actions: Vec<SessionGraphActionKind>,
127 /// The spent budget [`SessionGraphActionKind::ExtendBudget`] adds
128 /// provider calls to, while `actions` offers it: the selected node's,
129 /// or else the first one a pending node waits for, as the host's
130 /// inspector shows it.
131 pub exhausted_budget: Option<String>,
132 /// Graph identity.
133 pub id: String,
134 /// Whether the graph is a focused review that the host drives.
135 pub is_focused_review: bool,
136 /// The selected node, or `None` when the graph has no progress yet.
137 pub node_id: Option<String>,
138 /// Every node in declaration order.
139 pub nodes: Vec<SessionGraphNode>,
140 /// What the host says about how the graph is driven, such as for a
141 /// focused review, when it says anything.
142 pub note: Option<String>,
143 /// Outputs of the selected node's latest execution.
144 pub outputs: Vec<SessionGraphOutput>,
145 /// Current immutable revision.
146 pub revision: NonZeroU64,
147 /// Opaque token of everything an action on this graph is checked
148 /// against: identity, revision, status, and node progress. It changes
149 /// whenever any of them does, so an action can name the exact snapshot
150 /// it was chosen from.
151 pub snapshot: String,
152 /// Live status, or `None` when the host has no progress for the graph.
153 pub status: Option<ExecutionGraphStatus>,
154 /// `status` as the host shows it.
155 pub status_label: String,
156}
157
158/// One node of a [`SessionGraph`] with what a frontend shows about it.
159#[derive(Clone, Debug, Eq, PartialEq)]
160pub struct SessionGraphNode {
161 /// Actions the node takes now, among [`SessionGraphActionKind::Reply`]
162 /// and [`SessionGraphActionKind::Retry`], as the host's inspector offers
163 /// them with the node selected.
164 pub actions: Vec<SessionGraphActionKind>,
165 /// Whether a dependent waits for the user to accept this node's output.
166 pub awaits_approval: bool,
167 /// Why the pending node cannot dispatch, each as the host shows it.
168 pub blocked: Vec<String>,
169 /// How the user acts on the node, such as answering its questions in
170 /// chat, when it waits for them.
171 pub hint: Option<String>,
172 /// The node's definition.
173 pub node: GraphNode,
174 /// The node's live progress, once the host reads it.
175 pub progress: Option<GraphNodeProgress>,
176 /// Reasoning effort the node runs with.
177 pub reasoning_level: ReasoningLevel,
178 /// The node's dependencies in declaration order.
179 pub requirements: Vec<SessionGraphRequirement>,
180 /// The node's status as the host shows it.
181 pub status_label: String,
182}
183
184/// One dependency of a [`SessionGraphNode`].
185#[derive(Clone, Debug, Eq, PartialEq)]
186pub struct SessionGraphRequirement {
187 /// What the dependency waits for, as the host shows it.
188 pub gate: String,
189 /// Whether the dependency no longer blocks the node.
190 pub is_satisfied: bool,
191 /// The dependency as a node timeline shows it, naming its source.
192 pub label: String,
193 /// Node the dependency waits for.
194 pub node_id: String,
195}
196
197/// One output of the selected node with its stored body.
198#[derive(Clone, Debug, Eq, PartialEq)]
199pub struct SessionGraphOutput {
200 /// Actions the output takes now, [`SessionGraphActionKind::Accept`] or
201 /// none, as the host's inspector offers them with the output selected.
202 pub actions: Vec<SessionGraphActionKind>,
203 /// The stored output body, unchanged.
204 pub body: String,
205 /// The body as Markdown a frontend shows.
206 pub display: String,
207 /// The output's exact reference and evidence state.
208 pub result: GraphResult,
209}
210
211/// An action a graph inspector offers on a graph, one of its nodes, or one
212/// of the selected node's outputs.
213#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
214pub enum SessionGraphActionKind {
215 /// Accept a validated output, releasing the dependents that wait for it.
216 Accept,
217 /// Cancel the graph for good.
218 Cancel,
219 /// Add provider calls to the graph's spent budget.
220 ExtendBudget,
221 /// Send guidance that revises a settled node's output.
222 Reply,
223 /// Restart dispatch of a paused graph.
224 Resume,
225 /// Run a failed or cancelled node again.
226 Retry,
227}
228
229/// One inspector action on a session's graph, bound to the snapshot it was
230/// chosen from.
231#[derive(Clone, Debug, Eq, PartialEq)]
232pub struct SessionGraphActionRequest {
233 /// What to do.
234 pub action: SessionGraphAction,
235 /// Graph to act on; it must belong to `session_id`.
236 pub graph_id: String,
237 /// Session that owns the graph.
238 pub session_id: SessionId,
239 /// [`SessionGraph::snapshot`] of the read the action was chosen from.
240 /// The host refuses the action once the graph no longer matches it.
241 pub snapshot: String,
242}
243
244/// What a [`SessionGraphActionRequest`] does.
245#[derive(Clone, Debug, Eq, PartialEq)]
246pub enum SessionGraphAction {
247 /// Accepts `output_id`, one output of `node_id`'s latest execution.
248 Accept {
249 /// Node that produced the output.
250 node_id: String,
251 /// [`crate::GraphResultRef::id`] of the output.
252 output_id: String,
253 },
254 /// Cancels the graph permanently, without cancelling other session work.
255 Cancel,
256 /// Adds provider calls to `budget_id`, a spent budget of the graph.
257 ExtendBudget {
258 /// Calls to add, from one to
259 /// [`crate::ExtendGraphBudgetRequest::MAX_ADDITIONAL_CALLS`].
260 additional_calls: usize,
261 /// The spent budget to extend, as [`SessionGraph::exhausted_budget`]
262 /// named it; a pending node of the snapshot must wait for it.
263 budget_id: String,
264 },
265 /// Sends `message` as guidance that revises `node_id`'s output.
266 Reply {
267 /// Guidance text; it must not be blank.
268 message: String,
269 /// Node to revise.
270 node_id: String,
271 },
272 /// Restarts dispatch of a paused graph.
273 Resume,
274 /// Runs `node_id` again after it failed or was cancelled.
275 Retry {
276 /// Node to run again.
277 node_id: String,
278 },
279}