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
// SHIP-TWO-001 — `apr-cli-operations-v1` algorithm-level PARTIAL
// discharge for FALSIFY-OPS-004.
//
// Contract: `contracts/apr-cli-operations-v1.yaml`.
// Spec: `docs/specifications/aprender-train/ship-two-models-spec.md`.
//
// ## What FALSIFY-OPS-004 says
//
// rule: Progress percentage monotonically increasing
// prediction: Progress never decreases during model loading
// test: Capture all progress events during pull, verify monotonicity
// if_fails: Progress percentage regression (confuses users)
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Decision rule: given a sequence of progress percentages observed
// during a model pull/load operation, Pass iff:
//
// sequence is non-empty AND
// every value is finite AND in [0.0, 100.0] AND
// for every adjacent pair (a, b): a <= b (monotonically
// non-decreasing)
//
// Non-strict `<=` allows the same value to be emitted twice in a
// row (typical with throttled progress reporting). Strict `<` would
// reject normal progress streams.
/// Binary verdict for `FALSIFY-OPS-004`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ops004Verdict {
/// Sequence is non-empty, all values are finite + in
/// `[0.0, 100.0]`, AND every adjacent pair is non-decreasing.
Pass,
/// One or more of:
/// - `progress_sequence.is_empty()` (caller error — no
/// progress events captured).
/// - Any value is NaN, ±∞, or outside `[0.0, 100.0]`.
/// - Any adjacent pair `(a, b)` has `a > b` (progress
/// regressed).
Fail,
}
/// Pure verdict function for `FALSIFY-OPS-004`.
///
/// Inputs:
/// - `progress_sequence`: percentages (in [0, 100]) emitted by
/// the apr puller in chronological order.
///
/// Pass iff:
/// 1. `!progress_sequence.is_empty()`,
/// 2. Every value is finite,
/// 3. Every value is in `[0.0, 100.0]`,
/// 4. For every `i > 0`: `progress_sequence[i-1] <=
/// progress_sequence[i]`.
///
/// Otherwise `Fail`.
///
/// # Examples
///
/// Strictly increasing — `Pass`:
/// ```
/// use aprender::format::ops_004::{
/// verdict_from_progress_monotonicity, Ops004Verdict,
/// };
/// let v = verdict_from_progress_monotonicity(&[0.0, 25.0, 50.0, 75.0, 100.0]);
/// assert_eq!(v, Ops004Verdict::Pass);
/// ```
///
/// Regression at index 2 — `Fail`:
/// ```
/// use aprender::format::ops_004::{
/// verdict_from_progress_monotonicity, Ops004Verdict,
/// };
/// let v = verdict_from_progress_monotonicity(&[0.0, 50.0, 30.0, 100.0]);
/// assert_eq!(v, Ops004Verdict::Fail);
/// ```
#[must_use]
pub fn verdict_from_progress_monotonicity(progress_sequence: &[f64]) -> Ops004Verdict {
if progress_sequence.is_empty() {
return Ops004Verdict::Fail;
}
for &p in progress_sequence {
if !p.is_finite() {
return Ops004Verdict::Fail;
}
if !(0.0..=100.0).contains(&p) {
return Ops004Verdict::Fail;
}
}
for w in progress_sequence.windows(2) {
if w[0] > w[1] {
return Ops004Verdict::Fail;
}
}
Ops004Verdict::Pass
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Pass band — typical clean progress sequences.
// -------------------------------------------------------------------------
#[test]
fn pass_strictly_increasing() {
let v = verdict_from_progress_monotonicity(&[0.0, 25.0, 50.0, 75.0, 100.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_with_repeats() {
// Throttled emission: 25.0 → 25.0 → 50.0 (same-value adjacent
// pairs are allowed under non-strict <=).
let v = verdict_from_progress_monotonicity(&[0.0, 25.0, 25.0, 50.0, 100.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_single_event() {
// Single 0.0 (or 100.0) sequence is trivially monotonic.
let v = verdict_from_progress_monotonicity(&[0.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_starts_at_nonzero() {
// Resume scenario: progress starts at 50% (cached prefix).
let v = verdict_from_progress_monotonicity(&[50.0, 75.0, 100.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_realistic_dense_progress() {
// Realistic 1% emission cadence.
let dense: Vec<f64> = (0..=100).map(f64::from).collect();
let v = verdict_from_progress_monotonicity(&dense);
assert_eq!(v, Ops004Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 2: Fail band — progress regression.
// -------------------------------------------------------------------------
#[test]
fn fail_simple_regression() {
let v = verdict_from_progress_monotonicity(&[0.0, 50.0, 30.0, 100.0]);
assert_eq!(
v,
Ops004Verdict::Fail,
"regression at index 2 must Fail"
);
}
#[test]
fn fail_drop_to_zero() {
// Progress reset mid-stream.
let v = verdict_from_progress_monotonicity(&[0.0, 50.0, 0.0, 100.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_one_ulp_regression() {
let v = verdict_from_progress_monotonicity(&[
50.0,
f64::from_bits(50.0_f64.to_bits() - 1),
]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_regression_at_end() {
// 99 → 50 at the last pair.
let v = verdict_from_progress_monotonicity(&[0.0, 25.0, 50.0, 99.0, 50.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 3: Fail band — empty sequence (caller error).
// -------------------------------------------------------------------------
#[test]
fn fail_empty_sequence() {
let v = verdict_from_progress_monotonicity(&[]);
assert_eq!(
v,
Ops004Verdict::Fail,
"empty sequence must Fail (no progress observed)"
);
}
// -------------------------------------------------------------------------
// Section 4: Fail band — domain violations (NaN, ±∞).
// -------------------------------------------------------------------------
#[test]
fn fail_nan_in_sequence() {
let v = verdict_from_progress_monotonicity(&[0.0, 25.0, f64::NAN, 50.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_positive_infinity() {
let v = verdict_from_progress_monotonicity(&[0.0, f64::INFINITY, 100.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_negative_infinity() {
let v = verdict_from_progress_monotonicity(&[f64::NEG_INFINITY, 50.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 5: Fail band — out-of-range values.
// -------------------------------------------------------------------------
#[test]
fn fail_negative_percentage() {
let v = verdict_from_progress_monotonicity(&[-1.0, 50.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_above_100_percent() {
let v = verdict_from_progress_monotonicity(&[0.0, 50.0, 101.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
#[test]
fn fail_huge_value() {
let v = verdict_from_progress_monotonicity(&[0.0, 1e10]);
assert_eq!(v, Ops004Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 6: Boundary cases — at exactly 0.0 and 100.0.
// -------------------------------------------------------------------------
#[test]
fn pass_at_floor_0_percent() {
let v = verdict_from_progress_monotonicity(&[0.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_at_ceiling_100_percent() {
let v = verdict_from_progress_monotonicity(&[100.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn pass_full_range_two_events() {
let v = verdict_from_progress_monotonicity(&[0.0, 100.0]);
assert_eq!(v, Ops004Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 7: Realistic — apr pull dataset progress scenarios.
// -------------------------------------------------------------------------
#[test]
fn pass_apr_pull_clean_stream() {
// 1KB granularity progress on 1MB file.
let mut events = Vec::new();
for i in 0..=1024 {
events.push(f64::from(i) * 100.0 / 1024.0);
}
let v = verdict_from_progress_monotonicity(&events);
assert_eq!(v, Ops004Verdict::Pass);
}
#[test]
fn fail_apr_pull_retry_resets_progress() {
// Realistic regression: HF endpoint 503; puller retries
// and emits progress=0 mid-stream.
let v = verdict_from_progress_monotonicity(&[0.0, 30.0, 60.0, 0.0, 30.0, 100.0]);
assert_eq!(
v,
Ops004Verdict::Fail,
"retry reset must Fail (confuses users)"
);
}
#[test]
fn fail_apr_pull_concurrent_writer_overlap() {
// Multi-thread regression: progress events arrive out of
// order due to lockless emission.
let v = verdict_from_progress_monotonicity(&[0.0, 25.0, 50.0, 40.0, 75.0]);
assert_eq!(v, Ops004Verdict::Fail);
}
}