ag_session/execution_graph/result.rs
1//! References to persisted graph node outputs and the execution records that
2//! produced them.
3
4use std::num::NonZeroU64;
5
6use ag_protocol::ProtocolRequestProfile;
7use serde::{Deserialize, Serialize};
8
9use super::GraphContractError;
10
11/// Link to existing execution records; never a second execution status.
12#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
13pub enum GraphExecutionRef {
14 /// One task attempt and its authoritative ordered workflow operation.
15 TaskAttempt {
16 /// Unique attempt identity, distinct from task and operation IDs.
17 attempt_id: String,
18 /// Operation that includes turn execution and host post-processing.
19 operation_id: String,
20 /// Task owning the attempt and conversation.
21 task_id: String,
22 },
23 /// One worker-supervised isolated utility execution.
24 UtilityRun {
25 /// Existing durable utility run identity; provider retries stay inside
26 /// it.
27 run_id: String,
28 },
29}
30
31impl GraphExecutionRef {
32 /// Checks required identity fields without asserting record existence.
33 ///
34 /// # Errors
35 /// Rejects blank attempt, operation, task, or utility-run identities.
36 pub fn validate(&self) -> Result<(), GraphContractError> {
37 match self {
38 Self::TaskAttempt {
39 attempt_id,
40 operation_id,
41 task_id,
42 } => {
43 GraphContractError::require(attempt_id, "attempt_id")?;
44 GraphContractError::require(operation_id, "operation_id")?;
45 GraphContractError::require(task_id, "task_id")?;
46 }
47 Self::UtilityRun { run_id } => GraphContractError::require(run_id, "run_id")?,
48 }
49
50 Ok(())
51 }
52}
53
54/// Immutable address of one persisted output version, excluding its body.
55///
56/// The host must retain the body and verify execution ownership. Revisions and
57/// retries never retarget this reference to a newer response. Validation of a
58/// caller-constructed reference is not proof that storage contains it.
59#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
60pub struct GraphResultRef {
61 /// Existing task-attempt or utility-run authority that produced this
62 /// result.
63 pub execution: GraphExecutionRef,
64 /// Owning graph instance.
65 pub graph_id: String,
66 /// Exact graph revision under which the producer was admitted.
67 pub graph_revision: NonZeroU64,
68 /// Host-allocated immutable result identity.
69 pub id: String,
70 /// Producing node, scoped by graph identity and revision.
71 pub node_id: String,
72 /// Declared output on the producing node.
73 pub output_name: String,
74 /// Protocol schema for the retained output body.
75 pub schema: ProtocolRequestProfile,
76 /// Immutable version within the producing node's named output.
77 pub version: NonZeroU64,
78}
79
80impl GraphResultRef {
81 /// Checks reference shape; storage ownership and lifecycle remain host
82 /// checks.
83 ///
84 /// # Errors
85 /// Returns an error when any required identity or name is blank.
86 pub fn validate(&self) -> Result<(), GraphContractError> {
87 GraphContractError::require(&self.graph_id, "graph_id")?;
88 GraphContractError::require(&self.id, "result_id")?;
89 GraphContractError::require(&self.node_id, "node_id")?;
90 GraphContractError::require(&self.output_name, "output_name")?;
91
92 self.execution.validate()
93 }
94}
95
96/// Host-attested output state, separate from attempt/run lifecycle.
97///
98/// Advancing state requires authoritative evidence for this exact reference.
99/// Caller-supplied state must never establish validation or user acceptance.
100#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
101pub enum GraphResultState {
102 /// Saved output, potentially incomplete or containing clarification.
103 Produced,
104 /// Schema-valid output from settled, successful, question-free execution.
105 Validated,
106 /// Explicitly accepted validated version; later results are not accepted.
107 Accepted,
108}
109
110/// Snapshot supplied by the host's authoritative result store.
111#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
112pub struct GraphResult {
113 /// Immutable producing execution, revision, schema, and version.
114 pub reference: GraphResultRef,
115 /// Validation/acceptance evidence for this exact version.
116 pub state: GraphResultState,
117}
118
119/// First decision recorded for a chat reply to one output awaiting
120/// acceptance.
121#[derive(Clone, Copy, Debug, Eq, PartialEq)]
122pub enum GraphOutputReplyOutcome {
123 /// Accept the exact replied-to output version.
124 Accept,
125 /// Send the reply to the producing node as revision guidance.
126 Revise,
127}
128
129/// Exact input captured with a newly admitted execution.
130///
131/// Redelivery must retain this reference, even if a newer result is available.
132#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
133pub struct GraphResolvedInput {
134 /// Declared input slot on the consumer.
135 pub name: String,
136 /// Exact producer output; never an implicit "latest" query at execution.
137 pub result: GraphResultRef,
138}
139
140#[cfg(test)]
141#[path = "result_test.rs"]
142mod tests;