Skip to main content

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