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