Skip to main content

workyard_application/observation/
contract.rs

1mod participant;
2pub use participant::{ParticipantReport, ParticipantState};
3
4use crate::contracts::ProblemOutcome;
5use serde::Serialize;
6use trine::NativePath;
7use workyard_core::{Task, TaskInspection, TaskSnapshot};
8
9/// Whether this report uses recorded facts or a live inspection.
10#[derive(Clone, Copy, PartialEq, Eq, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum Provenance {
13	/// Derived from saved workspace facts.
14	Recorded,
15	/// Derived from current inspection.
16	Inspected,
17}
18
19/// Last recorded lifecycle and participant facts.
20#[derive(Serialize)]
21pub struct SavedTask {
22	/// Recorded locations of the backing repositories.
23	pub backing_repositories: std::collections::BTreeMap<String, NativePath>,
24	#[serde(skip)]
25	pub(crate) repository_sources: std::collections::BTreeMap<String, NativePath>,
26	#[serde(skip)]
27	pub(crate) removed: std::collections::BTreeSet<String>,
28	/// Current membership lifecycle of the task.
29	pub lifecycle: &'static str,
30	/// Invocation history tracked separately from task lifecycle.
31	pub operation: crate::contracts::OperationReport,
32	/// Diagnostic describing a failure or an unresolved condition.
33	pub problem: Option<ProblemOutcome>,
34	/// Members associated with this task or operation.
35	pub participants: Vec<crate::contracts::ParticipantOutcome>,
36}
37
38impl From<Task> for SavedTask {
39	fn from(task: Task) -> Self {
40		let removed = task
41			.removal
42			.as_ref()
43			.into_iter()
44			.flat_map(|removal| task.participants.iter().zip(&removal.participants))
45			.filter(|(_, progress)| progress.phase == workyard_core::RemovalPhase::Removed)
46			.map(|(participant, _)| participant.name.clone())
47			.collect();
48		Self {
49			removed,
50			backing_repositories: task
51				.participants
52				.iter()
53				.map(|participant| {
54					(
55						participant.name.clone(),
56						NativePath::new(&participant.common_dir).expect("recorded backing path"),
57					)
58				})
59				.collect(),
60			repository_sources: task
61				.participants
62				.iter()
63				.map(|participant| {
64					(
65						participant.name.clone(),
66						NativePath::new(&participant.source).expect("recorded repository source"),
67					)
68				})
69				.collect(),
70			lifecycle: task.lifecycle.as_str(),
71			operation: task.operation.into(),
72			problem: task.problem.map(Into::into),
73			participants: task.participants.into_iter().map(Into::into).collect(),
74		}
75	}
76}
77
78/// Recorded state, current observations and diagnostics for one task.
79#[derive(Serialize)]
80pub struct TaskReport {
81	/// Name used to identify this entry.
82	pub name: Option<String>,
83	/// Current membership lifecycle of the task.
84	pub lifecycle: Option<&'static str>,
85	/// User-facing health classification for the observed task.
86	pub condition: super::Condition,
87	/// Origin of the facts in this report.
88	pub provenance: super::Provenance,
89	/// Location of the task record when available.
90	pub record: NativePath,
91	/// Last published task information.
92	pub recorded: Option<SavedTask>,
93	/// Independent evidence available for checking the task record.
94	pub evidence: Option<SavedTask>,
95	/// Index of the participant associated with outstanding effect evidence.
96	pub pending_participant: Option<usize>,
97	/// Whether another operation currently holds admission.
98	pub busy: bool,
99	/// Diagnostics collected while reading or inspecting the task.
100	pub problems: Vec<ProblemOutcome>,
101	/// Facts collected from the current filesystem or Git inspection.
102	pub current: Vec<ParticipantReport>,
103}
104
105impl From<TaskSnapshot> for TaskReport {
106	fn from(snapshot: TaskSnapshot) -> Self {
107		let mut report = Self {
108			lifecycle: snapshot
109				.task
110				.as_ref()
111				.or(snapshot.evidence.as_ref())
112				.map(|task| task.lifecycle.as_str()),
113			condition: super::Condition::Unknown,
114			provenance: super::Provenance::Recorded,
115			name: snapshot.name,
116			record: NativePath::new(snapshot.record).expect("record path"),
117			recorded: snapshot.task.map(Into::into),
118			evidence: snapshot.evidence.map(Into::into),
119			pending_participant: snapshot.pending,
120			busy: snapshot.busy,
121			problems: snapshot.problems.into_iter().map(Into::into).collect(),
122			current: Vec::new(),
123		};
124		report.classify();
125		report
126	}
127}
128
129impl From<TaskInspection> for TaskReport {
130	fn from(inspection: TaskInspection) -> Self {
131		let mut report = Self::from(inspection.snapshot);
132		report.provenance = super::Provenance::Inspected;
133		report.current = inspection
134			.participants
135			.into_iter()
136			.map(Into::into)
137			.collect();
138		report.classify();
139		report
140	}
141}
142
143/// Workspace-scoped result of a task query.
144#[derive(Serialize, Default)]
145pub struct Observation {
146	#[serde(skip)]
147	pub(crate) explicit_workspace: Option<NativePath>,
148	/// Directory holding authoritative Workyard state.
149	pub state_dir: Option<NativePath>,
150	/// Directory containing the task workspaces.
151	pub tasks_root: Option<NativePath>,
152	/// Task reports matching this query.
153	pub tasks: Vec<TaskReport>,
154	/// Task selector supplied by the caller, if any.
155	pub selected_task: Option<String>,
156	/// Actionable guidance associated with this result.
157	pub guidance: Option<String>,
158	/// Diagnostic describing a failure or an unresolved condition.
159	pub problem: Option<ProblemOutcome>,
160}