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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// `apr-serve-v1` algorithm-level PARTIAL discharge for the 4
// inference-server falsifiers (health-readiness, graceful shutdown,
// 404 routing, concurrent inference isolation).
//
// Contract: `contracts/apr-serve-v1.yaml`.
/// Default graceful-shutdown timeout (30 seconds) per server_lifecycle
/// invariant.
pub const AC_APRSERVE_SHUTDOWN_TIMEOUT_S: u32 = 30;
/// Server lifecycle states per the contract's state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServerState {
Init,
Binding,
Loading,
Ready,
Draining,
Stopped,
}
// =============================================================================
// FALSIFY-SRV-001 — health returns 200 only when ready
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HealthReadinessVerdict {
/// /health returns 200 only in Ready state; 503 in all others.
Pass,
/// /health returned 200 in non-Ready state — health-lying regression.
Fail,
}
#[must_use]
pub fn verdict_from_health_readiness(state: ServerState, http_status: u16) -> HealthReadinessVerdict {
match (state, http_status) {
(ServerState::Ready, 200) => HealthReadinessVerdict::Pass,
// 200 in any non-Ready state is the regression class.
(_, 200) => HealthReadinessVerdict::Fail,
// Any non-200 in Ready is also wrong (probably 503 by the test).
(ServerState::Ready, _) => HealthReadinessVerdict::Fail,
// Non-Ready state returning 503 (or any non-200) is correct.
_ => HealthReadinessVerdict::Pass,
}
}
// =============================================================================
// FALSIFY-SRV-002 — graceful shutdown completes in-flight
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GracefulShutdownVerdict {
/// In-flight count == 0 before exit AND exit_code == 0 AND shutdown
/// duration ≤ timeout.
Pass,
/// In-flight requests dropped (connection reset), or timeout exceeded.
Fail,
}
#[must_use]
pub fn verdict_from_graceful_shutdown(
in_flight_at_exit: u32,
exit_code: i32,
shutdown_duration_s: u32,
) -> GracefulShutdownVerdict {
if in_flight_at_exit > 0 {
return GracefulShutdownVerdict::Fail;
}
if exit_code != 0 {
return GracefulShutdownVerdict::Fail;
}
if shutdown_duration_s > AC_APRSERVE_SHUTDOWN_TIMEOUT_S {
return GracefulShutdownVerdict::Fail;
}
GracefulShutdownVerdict::Pass
}
// =============================================================================
// FALSIFY-SRV-003 — unknown path returns 404 (not 500 / HTML)
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnknownPathVerdict {
/// Status 404 AND Content-Type: application/json.
Pass,
/// 500, 200, or HTML response — fell through to default Actix handler.
Fail,
}
#[must_use]
pub fn verdict_from_unknown_path(http_status: u16, content_type: &str) -> UnknownPathVerdict {
if http_status != 404 {
return UnknownPathVerdict::Fail;
}
let lower = content_type.to_lowercase();
if !lower.contains("application/json") {
return UnknownPathVerdict::Fail;
}
UnknownPathVerdict::Pass
}
// =============================================================================
// FALSIFY-SRV-004 — concurrent inference isolation
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConcurrentIsolationVerdict {
/// All N concurrent identical requests at temperature=0 return
/// identical output (no KV-cache contamination).
Pass,
/// At least 2 distinct outputs across requests.
Fail,
}
#[must_use]
pub fn verdict_from_concurrent_isolation(responses: &[&str]) -> ConcurrentIsolationVerdict {
if responses.is_empty() {
return ConcurrentIsolationVerdict::Fail;
}
let first = responses[0];
for r in responses.iter().skip(1) {
if *r != first {
return ConcurrentIsolationVerdict::Fail;
}
}
ConcurrentIsolationVerdict::Pass
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Provenance pins.
// -------------------------------------------------------------------------
#[test]
fn provenance_shutdown_timeout_30s() {
assert_eq!(AC_APRSERVE_SHUTDOWN_TIMEOUT_S, 30);
}
#[test]
fn provenance_state_machine_six_states() {
// Smoke: count of valid lifecycle states matches contract enumeration.
let all = [
ServerState::Init,
ServerState::Binding,
ServerState::Loading,
ServerState::Ready,
ServerState::Draining,
ServerState::Stopped,
];
assert_eq!(all.len(), 6);
}
// -------------------------------------------------------------------------
// Section 2: SRV-001 health readiness.
// -------------------------------------------------------------------------
#[test]
fn fs001_pass_ready_returns_200() {
assert_eq!(
verdict_from_health_readiness(ServerState::Ready, 200),
HealthReadinessVerdict::Pass
);
}
#[test]
fn fs001_pass_loading_returns_503() {
assert_eq!(
verdict_from_health_readiness(ServerState::Loading, 503),
HealthReadinessVerdict::Pass
);
}
#[test]
fn fs001_pass_init_returns_503() {
assert_eq!(
verdict_from_health_readiness(ServerState::Init, 503),
HealthReadinessVerdict::Pass
);
}
#[test]
fn fs001_pass_draining_returns_503() {
assert_eq!(
verdict_from_health_readiness(ServerState::Draining, 503),
HealthReadinessVerdict::Pass
);
}
#[test]
fn fs001_fail_loading_returns_200() {
// The exact regression: health-lying during model load.
assert_eq!(
verdict_from_health_readiness(ServerState::Loading, 200),
HealthReadinessVerdict::Fail
);
}
#[test]
fn fs001_fail_ready_returns_503() {
// Ready state must return 200, not 503.
assert_eq!(
verdict_from_health_readiness(ServerState::Ready, 503),
HealthReadinessVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 3: SRV-002 graceful shutdown.
// -------------------------------------------------------------------------
#[test]
fn fs002_pass_clean_shutdown() {
assert_eq!(
verdict_from_graceful_shutdown(0, 0, 5),
GracefulShutdownVerdict::Pass
);
}
#[test]
fn fs002_pass_at_timeout() {
assert_eq!(
verdict_from_graceful_shutdown(0, 0, 30),
GracefulShutdownVerdict::Pass
);
}
#[test]
fn fs002_fail_in_flight_dropped() {
assert_eq!(
verdict_from_graceful_shutdown(3, 0, 5),
GracefulShutdownVerdict::Fail
);
}
#[test]
fn fs002_fail_nonzero_exit() {
assert_eq!(
verdict_from_graceful_shutdown(0, 1, 5),
GracefulShutdownVerdict::Fail
);
}
#[test]
fn fs002_fail_timeout_exceeded() {
assert_eq!(
verdict_from_graceful_shutdown(0, 0, 31),
GracefulShutdownVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 4: SRV-003 unknown path 404 routing.
// -------------------------------------------------------------------------
#[test]
fn fs003_pass_404_json() {
assert_eq!(
verdict_from_unknown_path(404, "application/json"),
UnknownPathVerdict::Pass
);
}
#[test]
fn fs003_pass_404_json_charset() {
assert_eq!(
verdict_from_unknown_path(404, "application/json; charset=utf-8"),
UnknownPathVerdict::Pass
);
}
#[test]
fn fs003_fail_500_internal_error() {
// The regression: routing failure surfaced as 500.
assert_eq!(
verdict_from_unknown_path(500, "application/json"),
UnknownPathVerdict::Fail
);
}
#[test]
fn fs003_fail_404_html() {
// Actix default — HTML 404 is the regression class.
assert_eq!(
verdict_from_unknown_path(404, "text/html"),
UnknownPathVerdict::Fail
);
}
#[test]
fn fs003_fail_200_returned() {
assert_eq!(
verdict_from_unknown_path(200, "application/json"),
UnknownPathVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 5: SRV-004 concurrent inference isolation.
// -------------------------------------------------------------------------
#[test]
fn fs004_pass_10_identical() {
let r = ["The capital of France is Paris."; 10];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Pass
);
}
#[test]
fn fs004_pass_single_request() {
let r = ["A response"];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Pass
);
}
#[test]
fn fs004_fail_kv_cache_contamination() {
// The regression: req 7 leaked content from req 6.
let r = [
"Paris.", "Paris.", "Paris.", "Paris.", "Paris.",
"Paris.", "Paris.", "Lyon.", "Paris.", "Paris.",
];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Fail
);
}
#[test]
fn fs004_fail_empty() {
let r: [&str; 0] = [];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 6: Realistic — full healthy server passes all 4.
// -------------------------------------------------------------------------
#[test]
fn realistic_healthy_server_passes_all_4() {
// Ready + 200, in-flight==0 + exit 0, 404 JSON, 5 identical responses.
assert_eq!(
verdict_from_health_readiness(ServerState::Ready, 200),
HealthReadinessVerdict::Pass
);
assert_eq!(
verdict_from_graceful_shutdown(0, 0, 8),
GracefulShutdownVerdict::Pass
);
assert_eq!(
verdict_from_unknown_path(404, "application/json"),
UnknownPathVerdict::Pass
);
let r = ["4", "4", "4", "4", "4"];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Pass
);
}
#[test]
fn realistic_pre_fix_all_4_failures() {
// 001: health lies during loading.
assert_eq!(
verdict_from_health_readiness(ServerState::Loading, 200),
HealthReadinessVerdict::Fail
);
// 002: SIGTERM dropped 5 in-flight requests.
assert_eq!(
verdict_from_graceful_shutdown(5, 0, 1),
GracefulShutdownVerdict::Fail
);
// 003: HTML 404 from Actix default handler.
assert_eq!(
verdict_from_unknown_path(404, "text/html"),
UnknownPathVerdict::Fail
);
// 004: KV-cache contaminated request 8.
let r = ["A", "A", "B"];
assert_eq!(
verdict_from_concurrent_isolation(&r),
ConcurrentIsolationVerdict::Fail
);
}
}