aion-proto 0.31.0

Shared gRPC and serde wire contracts for Aion servers, clients, and workers.
Documentation
//! Describe facts and windowed workflow-history wire contracts.

use crate::{ProtoRunId, ProtoWorkflowId, WireEnvelope};

/// Proto representation of `DescribeWorkflowResponse`.
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, prost::Message)]
pub struct ProtoDescribeWorkflowResponse {
    /// Serde-encoded `aion_core::WorkflowSummary` envelope.
    #[prost(message, optional, tag = "1")]
    pub summary: Option<WireEnvelope>,
    /// Optional serde-encoded `aion_core::Event` envelopes.
    #[prost(message, repeated, tag = "2")]
    pub history: Vec<WireEnvelope>,
    /// Run resolved by the describe read.
    #[prost(message, optional, tag = "3")]
    pub run_id: Option<ProtoRunId>,
    /// Sequence number at the head of the history snapshot.
    #[prost(uint64, tag = "4")]
    pub history_head_seq: u64,
    /// Current lease's terminal workflow event, when present.
    #[prost(message, optional, tag = "5")]
    pub terminal_event: Option<WireEnvelope>,
    /// What the serving install says about itself (ADR-016, WA-010 R4).
    ///
    /// Always stamped by a server; absent only from a response an older
    /// server produced, which a reader keeps as "not reported" rather than
    /// reading as zero.
    #[prost(message, optional, tag = "6")]
    pub provenance: Option<ProtoReadProvenance>,
    /// What the whole history says about lease recording (WA-010 R4).
    /// Absent only from an older server's response.
    #[prost(message, optional, tag = "7")]
    pub lease_recording: Option<ProtoLeaseRecording>,
}

/// Proto representation of [`aion_core::LeaseRecording`].
#[derive(Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, prost::Message)]
pub struct ProtoLeaseRecording {
    /// `ActivityLeased` events in the whole history.
    #[prost(uint64, tag = "1")]
    pub leases_recorded: u64,
    /// `ActivityStarted` events in the whole history.
    #[prost(uint64, tag = "2")]
    pub dispatched_attempts: u64,
    /// Distinct `(activity, attempt)` keys with at least one lease.
    #[prost(uint64, tag = "3")]
    pub attempts_with_a_lease: u64,
}

impl From<aion_core::LeaseRecording> for ProtoLeaseRecording {
    fn from(value: aion_core::LeaseRecording) -> Self {
        Self {
            leases_recorded: value.leases_recorded,
            dispatched_attempts: value.dispatched_attempts,
            attempts_with_a_lease: value.attempts_with_a_lease,
        }
    }
}

impl From<ProtoLeaseRecording> for aion_core::LeaseRecording {
    fn from(value: ProtoLeaseRecording) -> Self {
        Self {
            leases_recorded: value.leases_recorded,
            dispatched_attempts: value.dispatched_attempts,
            attempts_with_a_lease: value.attempts_with_a_lease,
        }
    }
}

/// The lease-recording counts a response carried, or `None` from an older
/// server — kept distinct from zeros, which are counts the server took.
#[must_use]
pub fn decode_lease_recording(
    value: Option<ProtoLeaseRecording>,
) -> Option<aion_core::LeaseRecording> {
    value.map(aion_core::LeaseRecording::from)
}

/// Proto representation of [`aion_core::ReadProvenance`].
#[derive(Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, prost::Message)]
pub struct ProtoReadProvenance {
    /// Leases the serving install knew and failed to record since boot.
    #[prost(uint64, tag = "1")]
    pub lease_record_failures_total: u64,
}

impl From<aion_core::ReadProvenance> for ProtoReadProvenance {
    fn from(value: aion_core::ReadProvenance) -> Self {
        Self {
            lease_record_failures_total: value.lease_record_failures_total,
        }
    }
}

impl From<ProtoReadProvenance> for aion_core::ReadProvenance {
    fn from(value: ProtoReadProvenance) -> Self {
        Self::new(value.lease_record_failures_total)
    }
}

/// The provenance a response carried, or `None` when it carried none — a
/// response an older server produced. Presence is kept: `provenance` is a
/// proto3 message field with explicit presence, so an old server's absence
/// and a new server's zero are distinguishable at the decode, and this is
/// the one place that knowledge would otherwise die.
#[must_use]
pub fn decode_read_provenance(
    value: Option<ProtoReadProvenance>,
) -> Option<aion_core::ReadProvenance> {
    value.map(aion_core::ReadProvenance::from)
}

/// Proto representation of `ReadHistoryRequest`.
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, prost::Message)]
pub struct ProtoReadHistoryRequest {
    /// Namespace that scopes the operation.
    #[prost(string, tag = "1")]
    pub namespace: String,
    /// Target workflow identifier.
    #[prost(message, optional, tag = "2")]
    pub workflow_id: Option<ProtoWorkflowId>,
    /// First sequence number to include; absent starts at the beginning.
    #[prost(uint64, optional, tag = "3")]
    pub from_seq: Option<u64>,
    /// Maximum number of events in this page; absent uses the server default.
    #[prost(uint32, optional, tag = "4")]
    pub limit: Option<u32>,
}

/// Proto representation of `ReadHistoryResponse`.
#[derive(Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, prost::Message)]
pub struct ProtoReadHistoryResponse {
    /// Serde-encoded `aion_core::Event` envelopes.
    #[prost(message, repeated, tag = "1")]
    pub events: Vec<WireEnvelope>,
    /// Cursor for the next page, absent when this page reaches the head.
    #[prost(uint64, optional, tag = "2")]
    pub next_from_seq: Option<u64>,
    /// Sequence number at the head of the history snapshot.
    #[prost(uint64, tag = "3")]
    pub head_seq: u64,
}