ag_session/execution_graph/proposal_state.rs
1//! Durable proposal inspection and explicit action contracts.
2
3use ag_protocol::{GraphProposalResponse, TurnPrompt, TurnPromptAttachment};
4use serde::{Deserialize, Serialize};
5
6use super::{ExecutionGraph, GraphProposalLimits};
7use crate::{
8 AgentDefinition, AgentSelection, PermissionMode, SessionId, StartExecutionGraphRequest,
9};
10
11/// Idempotent generation admission; the host captures all other policy,
12/// including whether the proposal adds nodes to the session's graph that can
13/// still run or proposes a new graph.
14#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
15pub struct GenerateGraphProposalRequest {
16 /// Caller delivery identity, reused on retries.
17 pub id: String,
18 /// Original user request and provenance.
19 pub request: TurnPrompt,
20 /// Owning prepared session.
21 pub session_id: SessionId,
22}
23
24impl GenerateGraphProposalRequest {
25 /// Asks for a proposal for the task `text`, without attachments, under
26 /// the delivery identity `id`.
27 #[must_use]
28 pub fn from_text(id: String, session_id: SessionId, text: String) -> Self {
29 Self {
30 id,
31 request: TurnPrompt::from_text(text),
32 session_id,
33 }
34 }
35}
36
37/// Explicit mutation of an inspected proposal version.
38#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
39pub enum GraphProposalAction {
40 /// Regenerate using the captured catalog, model, and remaining call limit.
41 Regenerate,
42 /// Complete ordered answers to the exact question set.
43 Answer(Vec<String>),
44 /// Regenerate with free-form user feedback added to the history. Feedback
45 /// on a clarification is recorded together with the open questions;
46 /// feedback images are kept for every later generation and node.
47 Revise(TurnPrompt),
48 /// Accept the exact inspected version and its captured limits.
49 Approve,
50 /// Cancel generation or discard a pending proposal permanently.
51 Cancel,
52}
53
54/// Original immutable host capture, independent of executable graphs.
55#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
56pub struct GraphProposalState {
57 /// Snapshot of the session graph the proposal adds nodes to, at the
58 /// revision current when the host admitted the request; `None` for a new
59 /// graph. Approval appends the nodes only while that revision is current.
60 #[serde(default, skip_serializing_if = "Option::is_none")]
61 pub base: Option<ExecutionGraph>,
62 /// Captured provider and model for generation and execution.
63 #[serde(with = "super::selection")]
64 pub agent: AgentSelection,
65 /// Captured enabled agent definitions.
66 pub definitions: Vec<AgentDefinition>,
67 /// Original delivery request.
68 pub input: GenerateGraphProposalRequest,
69 /// Captured host ceilings; no action replenishes them.
70 pub limits: GraphProposalLimits,
71 /// Captured permission ceiling.
72 #[serde(with = "permission")]
73 pub permission: PermissionMode,
74 /// Current immutable generation version.
75 pub version: GraphProposalVersion,
76}
77
78/// A generation reservation becomes terminal once; later work gets a new
79/// version.
80#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
81pub struct GraphProposalVersion {
82 /// Complete answer set supplied to this generation, in question order.
83 pub answers: Vec<String>,
84 /// Approval receipt, bound to this exact outcome and limits.
85 pub approval: Option<GraphProposalApproval>,
86 /// Revision feedback images, oldest first, delivered after the original
87 /// request images.
88 #[serde(default)]
89 pub attachments: Vec<TurnPromptAttachment>,
90 /// Terminal discard flag, including cancellation during generation.
91 pub is_cancelled: bool,
92 /// Consecutive host-assigned generation version.
93 pub number: u32,
94 /// Generation progress, clarification, failure, or resolved preview.
95 pub outcome: GraphProposalVersionOutcome,
96 /// Parent reference used to locate the exact generating worker run.
97 pub parent_id: String,
98 /// Worker-owned utility identity, captured on completion when available.
99 pub utility_run_id: Option<String>,
100}
101
102/// Generation outcome; only a resolved preview can be approved.
103#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
104pub enum GraphProposalVersionOutcome {
105 /// Reserved durably before worker submission; restart never resubmits it.
106 Generating,
107 /// Complete wire response and optional executable resolution.
108 Generated {
109 /// Resolved graph and fixed execution budgets, absent for
110 /// clarification. For an expansion, the expanded next revision of
111 /// the captured base, with no budgets of its own.
112 launch: Option<Box<StartExecutionGraphRequest>>,
113 /// Model-authored proposal or clarification.
114 response: GraphProposalResponse,
115 },
116 /// Failed generation or rejected resolution; requires an explicit retry.
117 Failed(String),
118}
119
120/// First decision recorded for a chat reply to one proposal version.
121#[derive(Clone, Copy, Debug, Eq, PartialEq)]
122pub enum GraphProposalReplyOutcome {
123 /// Approve the exact replied-to version and start execution.
124 Approve,
125 /// Discard the proposal permanently.
126 Cancel,
127 /// Regenerate with the reply as answers or feedback.
128 Revise,
129}
130
131/// Receipt for one exact-version approval, separate from output acceptance.
132#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
133pub struct GraphProposalApproval {
134 /// Published executable snapshot identity, or the expanded graph's.
135 pub graph_id: String,
136 /// Captured ceilings inspected with the approved version.
137 pub limits: GraphProposalLimits,
138 /// Exact inspected generation version.
139 pub version: u32,
140}
141
142mod permission {
143 use std::borrow::Borrow;
144
145 use serde::{Deserialize, Deserializer, Serializer, de};
146
147 use crate::PermissionMode;
148
149 pub(super) fn serialize<S: Serializer>(
150 value: impl Borrow<PermissionMode>,
151 serializer: S,
152 ) -> Result<S::Ok, S::Error> {
153 serializer.serialize_str(value.borrow().label())
154 }
155
156 pub(super) fn deserialize<'de, D: Deserializer<'de>>(
157 deserializer: D,
158 ) -> Result<PermissionMode, D::Error> {
159 String::deserialize(deserializer)?
160 .parse()
161 .map_err(de::Error::custom)
162 }
163}