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