1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use crate::EngineError;
use chrono::{DateTime, Utc};
use dpm_model::{
ActorId, BasisState, Dependency, DependencyId, DependencyKind, DependencyPolicy, Endpoint,
EventTime, Plan, Release, StartBasis, StartRelease, Timeline, WorkItem, WorkItemId, WorkStatus,
};
use serde::{Deserialize, Serialize};
mod provisional;
mod transition;
pub use transition::{Transition, describe_applicability};
/// Machine-readable reason preventing a lifecycle transition at the evaluated time.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum UnmetGate {
/// Containers and milestones are projections, not claimable work.
Aggregate,
/// Proposed work needs explicit approval of its execution contract.
ContractNotRatified,
/// The current lifecycle state does not permit this transition.
Lifecycle {
/// Current authoritative lifecycle.
status: WorkStatus,
},
/// A worker recorded a blocker.
Blocker {
/// Concrete blocker text.
reason: String,
},
/// An unwaived constraint governing this transition is not yet released.
Dependency {
/// Stable identity of the unwaived constraint.
dependency: DependencyId,
/// Whether a human or service may waive the constraint.
policy: DependencyPolicy,
/// Stable identity of the prerequisite.
predecessor: WorkItemId,
/// Human-readable prerequisite key.
key: String,
/// Project containing the prerequisite.
project: String,
/// Assets required by the prerequisite; these are context, not satisfied gates.
asset_keys: Vec<String>,
/// Relation; FS and SS govern the successor's start, FF and SF its finish.
relation: DependencyKind,
/// Positive lag must elapse after the event; negative lag shapes the schedule only.
lag_hours: f64,
/// Predecessor event the relation waits for: start, or finish (verification).
requires: Endpoint,
/// Why the constraint is not released: missing event, elapsing lag or unknown event time.
release: Release,
/// Authoritative state of the prerequisite.
status: WorkStatus,
/// Worker responsible for the prerequisite, if reserved.
owner: Option<ActorId>,
/// Predecessor result the edge accepts for the successor's start.
#[serde(default, skip_serializing_if = "StartBasis::is_verified")]
start_basis: StartBasis,
/// Whether this evaluation accepts a pending submission; false for verification.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
accepts_submission: bool,
/// Pending predecessor attempt whose positive lag is still elapsing.
#[serde(default, skip_serializing_if = "Option::is_none")]
attempt: Option<u32>,
},
/// The execution relies on a predecessor attempt that was rejected; finishing waits for an
/// explicit independent revalidation against a newer attempt.
BasisInvalidated {
/// Provisional edge carrying the basis.
dependency: DependencyId,
/// Stable identity of the predecessor.
predecessor: WorkItemId,
/// Human-readable predecessor key.
key: String,
/// Ordinal of the rejected attempt the execution relies on.
attempt: u32,
/// Rejection of that attempt.
state: BasisState,
/// Predecessor's pending or verified attempt a revalidation may name, if any.
current_attempt: Option<u32>,
},
/// An open direct or inherited decision gates execution.
Decision {
/// Human-readable gate key.
key: String,
},
/// The choice that selects this conditional work, or a containing package, was made after the
/// evaluated time, so the work was not yet in the plan then.
Choice {
/// Human-readable key of the selecting decision.
key: String,
/// When its standing outcome was made.
chosen_at: DateTime<Utc>,
},
/// The work is outside the active graph: not selected, undecided, awaiting an upstream choice,
/// stranded behind excluded work, or an empty join.
Applicability {
/// Derived applicability explaining the exclusion.
applicability: dpm_model::Applicability,
},
}
/// A provisional edge released on a predecessor's pending attempt; a start records it as basis.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProvisionalRelease {
/// Provisional edge.
pub dependency: DependencyId,
/// Predecessor that made the attempt.
pub predecessor: WorkItemId,
/// Human-readable predecessor key.
pub key: String,
/// Ordinal of the pending attempt.
pub attempt: u32,
}
/// Shared eligibility of one lifecycle transition for queries, commands and terminal projections.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GateReport {
/// Transition this report evaluates; `claim` is the readiness used by `next`.
#[serde(default)]
pub transition: Transition,
/// Whether the transition is permitted at the evaluated time.
pub ready: bool,
/// Every unmet condition; an empty list means ready.
pub unmet: Vec<UnmetGate>,
/// Start-gate edges released only by a pending predecessor attempt, not a verified finish.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub provisional: Vec<ProvisionalRelease>,
}
/// Explain whether a transition is permitted at an adapter-supplied time, without changing state.
pub fn gate_report(
plan: &Plan,
work: WorkItemId,
transition: Transition,
now: DateTime<Utc>,
) -> Result<GateReport, EngineError> {
plan.validate()?;
let item = plan
.work_items
.get(&work)
.ok_or(EngineError::MissingWorkItem(work))?;
Ok(evaluate(plan, item, transition, &Timeline::at(plan, now)))
}
/// The one gate evaluation used by every query, command and view.
pub(crate) fn evaluate(
plan: &Plan,
work: &WorkItem,
transition: Transition,
timeline: &Timeline,
) -> GateReport {
let mut unmet = Vec::new();
if !work.is_executable() {
unmet.push(UnmetGate::Aggregate);
}
match work.execution.status {
status if status == transition.lifecycle() => {}
WorkStatus::Proposed => unmet.push(UnmetGate::ContractNotRatified),
status => unmet.push(UnmetGate::Lifecycle { status }),
}
if let Some(reason) = &work.execution.block_reason {
unmet.push(UnmetGate::Blocker {
reason: reason.clone(),
});
}
let applicability = timeline.applicability(work.id);
if !applicability.is_applicable() {
unmet.push(UnmetGate::Applicability {
applicability: applicability.clone(),
});
}
unmet.extend(choice_gates(plan, work, timeline));
let mut provisional = Vec::new();
for dep in timeline
.incoming(plan, work.id)
.filter(|dep| transition.governs(dep.kind))
{
let Some(item) = plan.work_items.get(&dep.predecessor) else {
continue;
};
let edge = if transition.starts() {
timeline.start_edge(plan, dep)
} else {
StartRelease {
release: timeline.edge(plan, dep),
attempt: None,
provisional: false,
}
};
match (edge.release.released_at(), edge.attempt) {
(Some(_), Some(attempt)) if edge.provisional => provisional.push(ProvisionalRelease {
dependency: dep.id,
predecessor: item.id,
key: item.key.to_string(),
attempt,
}),
(None, attempt) => unmet.push(dependency_gate(
plan,
dep,
item,
transition,
edge.release,
attempt,
)),
(Some(_), _) => {}
}
}
if transition.checks_basis() {
unmet.extend(provisional::basis_gates(plan, work));
}
if transition.checks_decisions() {
unmet.extend(
timeline
.decisions(plan, work.id)
.into_iter()
.filter(|(_, release)| release.released_at().is_none())
.map(|(decision, _)| UnmetGate::Decision {
key: decision.key.to_string(),
}),
);
}
GateReport {
transition,
ready: unmet.is_empty(),
unmet,
provisional,
}
}
/// Selecting choices made after the evaluated time: a backfilled event cannot precede them.
fn choice_gates(plan: &Plan, work: &WorkItem, timeline: &Timeline) -> Vec<UnmetGate> {
timeline
.selecting_choices(plan, work.id)
.into_iter()
.filter_map(|(decision, at)| match at {
EventTime::Recorded(chosen_at) if chosen_at > timeline.now() => {
Some(UnmetGate::Choice {
key: decision.key.to_string(),
chosen_at,
})
}
_ => None,
})
.collect()
}
fn dependency_gate(
plan: &Plan,
dep: &Dependency,
item: &WorkItem,
transition: Transition,
release: Release,
attempt: Option<u32>,
) -> UnmetGate {
UnmetGate::Dependency {
dependency: dep.id,
policy: dep.policy,
asset_keys: item
.contract
.assets
.iter()
.filter_map(|r| plan.assets.get(&r.asset))
.map(|r| r.key.to_string())
.collect(),
predecessor: item.id,
key: item.key.to_string(),
project: plan
.projects
.get(&item.project)
.map(|p| p.key.to_string())
.unwrap_or_default(),
relation: dep.kind,
lag_hours: dep.lag_hours,
requires: dep.kind.predecessor_endpoint(),
release,
status: item.execution.status,
owner: item.execution.owner.clone(),
start_basis: dep.start_basis,
accepts_submission: transition.starts() && dep.start_basis == StartBasis::Provisional,
attempt,
}
}
impl GateReport {
/// Human-readable rendering of the same structured conditions returned to agents.
pub fn reasons(&self) -> Vec<String> {
self.unmet.iter().map(describe).collect()
}
}
pub(crate) fn summary(unmet: &[UnmetGate]) -> String {
unmet.iter().map(describe).collect::<Vec<_>>().join("; ")
}
/// Human-readable rendering of one unmet condition.
pub(crate) fn describe(gate: &UnmetGate) -> String {
match gate {
UnmetGate::Aggregate => {
"aggregate work completes through its prerequisites or children".into()
}
UnmetGate::ContractNotRatified => "contract not ratified".into(),
UnmetGate::Lifecycle { status } => format!("work lifecycle state is {status:?}"),
UnmetGate::Blocker { reason } => format!("work is blocked: {reason}"),
UnmetGate::Dependency {
key,
relation,
lag_hours,
policy,
requires,
release,
start_basis,
accepts_submission,
attempt,
..
} => {
let edge = format!("{} {lag_hours:+}h, {policy:?}", relation.abbreviation());
let mut text = if *accepts_submission {
provisional::describe_start(key, *release, *attempt, &edge, *policy)
} else {
transition::describe_release(key, *requires, *release, &edge, *policy)
};
if *start_basis == StartBasis::Provisional && !*accepts_submission {
text.push_str("; a provisional edge releases only the start, never finishing on an unverified result");
}
if *lag_hours < 0.0 {
text.push_str(&format!(
"; the {lag_hours}h lead shapes the schedule only and never releases work before the event"
));
}
text
}
UnmetGate::BasisInvalidated {
key,
attempt,
state,
current_attempt,
..
} => provisional::describe_invalidated(key, *attempt, state, *current_attempt),
UnmetGate::Decision { key } => format!("work awaits decision: {key}"),
UnmetGate::Choice { key, chosen_at } => format!(
"work applies only through {key}, which selected it at {chosen_at}, after the evaluated time"
),
UnmetGate::Applicability { applicability } => {
transition::describe_applicability(applicability)
}
}
}
#[cfg(test)]
mod tests;