use std::time::Duration;
use async_trait::async_trait;
use crate::backend::{
AppendFrameOutcome, CancelFlowPolicy, CancelFlowWait, CapabilitySet, ClaimPolicy,
FailOutcome, FailureClass, FailureReason, Frame, Handle, LeaseRenewal, PendingWaitpoint,
ReclaimToken, ResumeSignal, WaitpointSpec,
};
use crate::contracts::{CancelFlowResult, ExecutionSnapshot, FlowSnapshot, ReportUsageResult};
#[cfg(feature = "core")]
use crate::contracts::{EdgeDirection, EdgeSnapshot};
#[cfg(feature = "streaming")]
use crate::contracts::{StreamCursor, StreamFrames};
use crate::engine_error::EngineError;
#[cfg(feature = "streaming")]
use crate::types::AttemptIndex;
use crate::types::{BudgetId, ExecutionId, FlowId, LaneId, TimestampMs};
#[async_trait]
pub trait EngineBackend: Send + Sync + 'static {
async fn claim(
&self,
lane: &LaneId,
capabilities: &CapabilitySet,
policy: ClaimPolicy,
) -> Result<Option<Handle>, EngineError>;
async fn renew(&self, handle: &Handle) -> Result<LeaseRenewal, EngineError>;
async fn progress(
&self,
handle: &Handle,
percent: Option<u8>,
message: Option<String>,
) -> Result<(), EngineError>;
async fn append_frame(
&self,
handle: &Handle,
frame: Frame,
) -> Result<AppendFrameOutcome, EngineError>;
async fn complete(&self, handle: &Handle, payload: Option<Vec<u8>>) -> Result<(), EngineError>;
async fn fail(
&self,
handle: &Handle,
reason: FailureReason,
classification: FailureClass,
) -> Result<FailOutcome, EngineError>;
async fn cancel(&self, handle: &Handle, reason: &str) -> Result<(), EngineError>;
async fn suspend(
&self,
handle: &Handle,
waitpoints: Vec<WaitpointSpec>,
timeout: Option<Duration>,
) -> Result<Handle, EngineError>;
async fn create_waitpoint(
&self,
handle: &Handle,
waitpoint_key: &str,
expires_in: Duration,
) -> Result<PendingWaitpoint, EngineError>;
async fn observe_signals(&self, handle: &Handle) -> Result<Vec<ResumeSignal>, EngineError>;
async fn claim_from_reclaim(&self, token: ReclaimToken) -> Result<Option<Handle>, EngineError>;
async fn delay(&self, handle: &Handle, delay_until: TimestampMs) -> Result<(), EngineError>;
async fn wait_children(&self, handle: &Handle) -> Result<(), EngineError>;
async fn describe_execution(
&self,
id: &ExecutionId,
) -> Result<Option<ExecutionSnapshot>, EngineError>;
async fn describe_flow(&self, id: &FlowId) -> Result<Option<FlowSnapshot>, EngineError>;
#[cfg(feature = "core")]
async fn list_edges(
&self,
flow_id: &FlowId,
direction: EdgeDirection,
) -> Result<Vec<EdgeSnapshot>, EngineError>;
async fn cancel_flow(
&self,
id: &FlowId,
policy: CancelFlowPolicy,
wait: CancelFlowWait,
) -> Result<CancelFlowResult, EngineError>;
async fn report_usage(
&self,
handle: &Handle,
budget: &BudgetId,
dimensions: crate::backend::UsageDimensions,
) -> Result<ReportUsageResult, EngineError>;
#[cfg(feature = "streaming")]
async fn read_stream(
&self,
execution_id: &ExecutionId,
attempt_index: AttemptIndex,
from: StreamCursor,
to: StreamCursor,
count_limit: u64,
) -> Result<StreamFrames, EngineError>;
#[cfg(feature = "streaming")]
async fn tail_stream(
&self,
execution_id: &ExecutionId,
attempt_index: AttemptIndex,
after: StreamCursor,
block_ms: u64,
count_limit: u64,
) -> Result<StreamFrames, EngineError>;
}
#[allow(dead_code)]
fn _assert_dyn_compatible(_: &dyn EngineBackend) {}