printobserver_supervisor_api/lib.rs
1//! `printobserver-supervisor-api`.
2//!
3//! Owns: the port the supervising agent is reached through — the trait for
4//! running one supervision turn and for closing a session, the two shapes those
5//! methods carry, and that port's own error type.
6//!
7//! May depend on: `printobserver-types` only. A port that named an
8//! implementation would stop being a port.
9//!
10//! # Opening a session is not a call of its own
11//!
12//! Opening or continuing is [`SupervisorPort::run_turn`]'s own doing, and the
13//! [`SessionPhase`] it answers is what says which happened. A separate open
14//! call would let a caller open a session it then never took a turn in.
15//!
16//! # Why the methods answer a boxed future
17//!
18//! Every method is asynchronous, and the trait is dyn-compatible and shareable
19//! across threads, because the supervision core holds all four ports behind
20//! `Arc<dyn Port>`. An `async fn` in a trait is not dyn-compatible, so each
21//! method answers a [`BoxFuture`] instead.
22
23use core::future::Future;
24use core::pin::Pin;
25use std::path::PathBuf;
26
27use printobserver_types::schemars::JsonSchema;
28use printobserver_types::serde::{Deserialize, Serialize};
29use printobserver_types::{
30 AgentAssessment, EventRecord, PrintId, SessionPhase, SupervisionSession,
31};
32
33/// A future this port's methods answer with, in the one shape a trait object
34/// can carry.
35pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
36
37/// What one supervision turn is asked to consider.
38#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
39#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
40#[schemars(crate = "printobserver_types::schemars")]
41pub struct TurnRequest {
42 /// The print the turn is about.
43 pub print_id: PrintId,
44 /// The event that prompted the turn.
45 pub event: EventRecord,
46 /// The absolute path of the image to look at, when there is one.
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub image_path: Option<PathBuf>,
49 /// The command the turn runs to read the print's context.
50 pub context_command: String,
51}
52
53/// What one supervision turn answered with.
54#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
55#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
56#[schemars(crate = "printobserver_types::schemars")]
57pub struct TurnOutcome {
58 /// The session the turn ran in.
59 pub session: SupervisionSession,
60 /// Whether this turn opened that session or continued it.
61 pub phase: SessionPhase,
62 /// The agent's written record of the turn.
63 pub assessment: AgentAssessment,
64}
65
66/// Why a supervision turn did not produce an assessment.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum SupervisorError {
69 /// The agent answered something that failed validation.
70 InvalidAnswer {
71 /// What was wrong with the answer.
72 detail: String,
73 },
74 /// The harness refused the identity the turn ran under.
75 IdentityRefused {
76 /// What the harness said about it.
77 detail: String,
78 },
79 /// The harness could not be reached, or would not run a turn.
80 Unavailable {
81 /// What went wrong.
82 detail: String,
83 },
84 /// The turn took too long.
85 TimedOut,
86}
87
88impl core::fmt::Display for SupervisorError {
89 fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
90 match self {
91 Self::InvalidAnswer { detail } => {
92 write!(formatter, "the agent's answer failed validation: {detail}")
93 }
94 Self::IdentityRefused { detail } => {
95 write!(formatter, "the harness refused the identity: {detail}")
96 }
97 Self::Unavailable { detail } => {
98 write!(formatter, "the harness is unavailable: {detail}")
99 }
100 Self::TimedOut => formatter.write_str("the turn took too long"),
101 }
102 }
103}
104
105impl core::error::Error for SupervisorError {}
106
107/// The port the supervising agent is reached through.
108///
109/// Every method is asynchronous, the trait is dyn-compatible, and it is
110/// shareable across threads, because the supervision core holds it behind
111/// `Arc<dyn SupervisorPort>`.
112pub trait SupervisorPort: Send + Sync {
113 /// Run one supervision turn, opening the session if it is not open.
114 fn run_turn(&self, request: TurnRequest)
115 -> BoxFuture<'_, Result<TurnOutcome, SupervisorError>>;
116
117 /// Close the session watching one print.
118 fn close_session(
119 &self,
120 print_id: PrintId,
121 close_reason: String,
122 ) -> BoxFuture<'_, Result<(), SupervisorError>>;
123}