interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / crates / spark-harness / tests / provisional_guards.rs

368 lines · 11986 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Headstart M2 (#535): one test per guard that a deletion mutation must
17//! break, plus bounded duplicate memory and reserved audit ids.
18
19mod common;
20
21use common::{enabled, H};
22use serde_json::json;
23use spark_harness::completion::CompletionError;
24use spark_harness::execution::TaskState;
25use spark_harness::readiness::{
26 verify_interface, InterfaceSubmission, OperationClass, ReadinessLevel,
27};
28use spark_harness::scheduler::{
29 Event, EventKind, Outcome, ProvisionalConfig, Revocation, SchedError, RESERVED_ID_PREFIX,
30};
31
32fn remove_receipt(h: &H, receipt_id: &str) {
33 std::fs::remove_file(
34 h.engine
35 .completion_store
36 .join("receipts")
37 .join(format!("{receipt_id}.json")),
38 )
39 .unwrap();
40}
41
42#[test]
43fn heartbeat_after_lease_expiry_is_refused() {
44 let mut h = H::new(enabled());
45 h.register("a", 1, &[]);
46 h.advance(1_000);
47 // No Tick has run yet, so a is still Provisional, but its lease is gone.
48 let r = h.send(EventKind::Heartbeat {
49 task: "a".into(),
50 generation: h.generation("a"),
51 beat: 1,
52 });
53 assert!(matches!(r, Err(SchedError::LeaseExpired(_))), "{r:?}");
54 assert_eq!(h.sched.state("a"), Some(TaskState::Provisional));
55 let fx = h.ok(EventKind::Tick);
56 assert_eq!(fx.aborted, vec!["a"]);
57}
58
59#[test]
60fn finalize_rechecks_the_whole_ancestor_chain() {
61 let mut h = H::new(enabled());
62 h.register("a", 1, &[]);
63 h.register("b", 1, &["a"]);
64 h.register("c", 1, &["b"]);
65 h.publish("a", 1);
66 h.publish("b", 1);
67 h.work_done("c");
68 h.finish("a");
69 h.finish("b");
70 assert!(h.sched.may_finalize("c").is_ok());
71 // Only the grandparent's completion disappears; b's own proof is fine.
72 let a = h.sched.task("a").unwrap().completion.clone().unwrap();
73 remove_receipt(&h, &a.receipt_id);
74 assert!(h.sched.may_finalize("b").is_err());
75 let r = h.sched.may_finalize("c");
76 assert!(
77 matches!(r, Err(SchedError::Proof { ref task, .. }) if task == "a"),
78 "{r:?}"
79 );
80 assert!(h
81 .send(EventKind::Finalize {
82 task: "c".into(),
83 generation: h.generation("c")
84 })
85 .is_err());
86 assert_eq!(h.sched.state("c"), Some(TaskState::AwaitingFinalProof));
87}
88
89#[test]
90fn proofs_are_accepted_only_for_active_tasks() {
91 let mut h = H::new(enabled());
92 h.register("p", 1, &[]);
93 h.register("b", 1, &["p"]);
94 assert_eq!(h.sched.state("b"), Some(TaskState::Blocked));
95 // A valid receipt for b exists, but b has not started.
96 let claim = h.mint_interface("b", 1);
97 let r = h.send(EventKind::InterfacePublished {
98 task: "b".into(),
99 generation: 1,
100 claim,
101 });
102 assert!(matches!(r, Err(SchedError::WrongState { .. })), "{r:?}");
103 assert!(h.sched.task("b").unwrap().interface.is_none());
104}
105
106#[test]
107fn failed_final_upgrade_falls_back_to_the_interface_level() {
108 let mut h = H::new(enabled());
109 h.register("a", 1, &[]);
110 h.register("b", 1, &["a"]);
111 h.publish("a", 1);
112 h.finish("a");
113 // b was bound at the interface level; a is final now, so it upgrades.
114 assert!(h.sched.may_start("b", OperationClass::Commit).is_ok());
115 let a = h.sched.task("a").unwrap().completion.clone().unwrap();
116 remove_receipt(&h, &a.receipt_id);
117 // The interface still verifies: reversible work may go on...
118 let r = h
119 .sched
120 .may_start("b", OperationClass::ReadOnlyAnalysis)
121 .unwrap();
122 assert_eq!(r[0].level(), ReadinessLevel::InterfaceVerified);
123 // ...but nothing irreversible.
124 assert!(matches!(
125 h.sched.may_start("b", OperationClass::Commit),
126 Err(SchedError::NotPermitted { .. })
127 ));
128}
129
130#[test]
131fn reserved_ids_and_audit_events_cannot_be_delivered() {
132 let mut h = H::new(enabled());
133 h.register("a", 1, &[]);
134 let r = h.sched.deliver(Event {
135 id: format!("{RESERVED_ID_PREFIX}audit-3"),
136 kind: EventKind::Tick,
137 });
138 assert!(matches!(r, Err(SchedError::InvalidEvent(_))), "{r:?}");
139 let r = h.send(EventKind::AuditRevoke {
140 revocations: vec![Revocation {
141 task: "a".into(),
142 reason: "forged".into(),
143 }],
144 });
145 assert!(matches!(r, Err(SchedError::InvalidEvent(_))), "{r:?}");
146 assert_eq!(h.sched.state("a"), Some(TaskState::Provisional));
147 // A caller id that looks like an old audit id does not block the audit.
148 let (claim, _) = h.publish("a", 1);
149 h.sched
150 .deliver(Event {
151 id: "audit-4-a".into(),
152 kind: EventKind::Tick,
153 })
154 .unwrap();
155 remove_receipt(&h, &claim.receipt_id);
156 assert_eq!(h.sched.audit().unwrap(), vec!["a"]);
157}
158
159#[test]
160fn duplicate_memory_is_bounded() {
161 let mut h = H::new(enabled());
162 h.register("a", 1, &[]);
163 let (claim, _) = h.publish("a", 1);
164 let publish = Event {
165 id: "pub-a-1".into(),
166 kind: EventKind::InterfacePublished {
167 task: "a".into(),
168 generation: 1,
169 claim,
170 },
171 };
172 // Same claim again under a new id: no change, so it applies cleanly.
173 assert!(matches!(
174 h.sched.deliver(publish.clone()),
175 Ok(Outcome::Applied(_))
176 ));
177 assert_eq!(h.sched.deliver(publish.clone()), Ok(Outcome::Duplicate));
178 let bump = Event {
179 id: "gen-a-2".into(),
180 kind: EventKind::NewGeneration {
181 task: "a".into(),
182 generation: 2,
183 },
184 };
185 h.sched.deliver(bump.clone()).unwrap();
186 assert_eq!(h.sched.deliver(bump), Ok(Outcome::Duplicate));
187 // The generation-1 bucket is gone: the old event is refused as stale,
188 // and either way it changes nothing.
189 let before = h.sched.snapshot();
190 assert!(matches!(
191 h.sched.deliver(publish),
192 Err(SchedError::StaleGeneration { .. })
193 ));
194 assert_eq!(h.sched.snapshot(), before);
195 // Heartbeats live in a window of `dedup_window` ids, however many come.
196 let first = Event {
197 id: "beat-1".into(),
198 kind: EventKind::Heartbeat {
199 task: "a".into(),
200 generation: 2,
201 beat: 1,
202 },
203 };
204 h.sched.deliver(first.clone()).unwrap();
205 for beat in 2..=500 {
206 h.ok(EventKind::Heartbeat {
207 task: "a".into(),
208 generation: 2,
209 beat,
210 });
211 }
212 let window = enabled().dedup_window;
213 assert!(
214 h.sched.remembered_event_ids() <= window + 2,
215 "{} ids remembered",
216 h.sched.remembered_event_ids()
217 );
218 // The first beat left the window long ago; redelivered, it is refused
219 // by its beat number and the lease is not renewed.
220 let before = h.sched.snapshot();
221 let r = h.sched.deliver(first);
222 assert!(matches!(r, Err(SchedError::StaleHeartbeat { .. })), "{r:?}");
223 assert_eq!(h.sched.snapshot(), before);
224}
225
226/// Inspector reproduction: with a tiny window, a Cancel redelivered after
227/// the task was retried and the window rolled over must not abort again.
228#[test]
229fn cancel_redelivered_after_retry_and_window_rollover_is_refused() {
230 let mut h = H::new(ProvisionalConfig {
231 dedup_window: 4,
232 ..enabled()
233 });
234 h.register("a", 1, &[]);
235 h.register("b", 1, &["a"]);
236 let cancel = Event {
237 id: "cancel-a".into(),
238 kind: EventKind::Cancel {
239 task: "a".into(),
240 generation: 1,
241 },
242 };
243 let fx = match h.sched.deliver(cancel.clone()) {
244 Ok(Outcome::Applied(fx)) => fx,
245 other => panic!("{other:?}"),
246 };
247 assert_eq!(fx.aborted, vec!["a", "b"]);
248 for t in ["a", "b"] {
249 h.ok(EventKind::Retry {
250 task: t.into(),
251 generation: 1,
252 });
253 }
254 for _ in 0..6 {
255 h.ok(EventKind::Tick);
256 }
257 assert_eq!(h.sched.state("a"), Some(TaskState::Provisional));
258 let before = h.sched.snapshot();
259 let r = h.sched.deliver(cancel);
260 assert!(
261 matches!(r, Err(SchedError::StaleGeneration { .. })),
262 "{r:?}"
263 );
264 assert_eq!(h.sched.snapshot(), before);
265 assert_eq!(h.sched.state("a"), Some(TaskState::Provisional));
266 assert_eq!(h.sched.state("b"), Some(TaskState::Blocked));
267}
268
269/// A late Retry must not undo a later Cancel of the retried generation.
270#[test]
271fn retry_redelivered_after_a_second_cancel_is_refused() {
272 let mut h = H::new(enabled());
273 h.register("a", 1, &[]);
274 h.ok(EventKind::Cancel {
275 task: "a".into(),
276 generation: 1,
277 });
278 let retry = Event {
279 id: "retry-a".into(),
280 kind: EventKind::Retry {
281 task: "a".into(),
282 generation: 1,
283 },
284 };
285 h.sched.deliver(retry.clone()).unwrap();
286 h.ok(EventKind::Cancel {
287 task: "a".into(),
288 generation: 2,
289 });
290 assert_eq!(h.sched.state("a"), Some(TaskState::Aborted));
291 let r = h.sched.deliver(retry);
292 assert!(
293 matches!(r, Err(SchedError::StaleGeneration { .. })),
294 "{r:?}"
295 );
296 assert_eq!(h.sched.state("a"), Some(TaskState::Aborted));
297 assert_eq!(h.generation("a"), 2);
298}
299
300/// Late Revoke and Finalize redeliveries must not act on a newer generation.
301#[test]
302fn revoke_and_finalize_redelivered_after_rerun_are_refused() {
303 let mut h = H::new(enabled());
304 h.register("a", 1, &[]);
305 h.register("b", 1, &["a"]);
306 h.publish("a", 1);
307 h.finish("a");
308 h.work_done("b");
309 let finalize = Event {
310 id: "fin-b".into(),
311 kind: EventKind::Finalize {
312 task: "b".into(),
313 generation: 1,
314 },
315 };
316 h.sched.deliver(finalize.clone()).unwrap();
317 let revoke = Event {
318 id: "rv-b".into(),
319 kind: EventKind::Revoke {
320 task: "b".into(),
321 generation: 1,
322 reason: "operator".into(),
323 },
324 };
325 h.sched.deliver(revoke.clone()).unwrap();
326 // b re-runs at generation 2 against the final a.
327 assert_eq!(h.sched.state("b"), Some(TaskState::Provisional));
328 let r = h.sched.deliver(revoke);
329 assert!(
330 matches!(r, Err(SchedError::StaleGeneration { .. })),
331 "{r:?}"
332 );
333 assert_eq!(h.sched.state("b"), Some(TaskState::Provisional));
334 h.work_done("b");
335 let r = h.sched.deliver(finalize);
336 assert!(
337 matches!(r, Err(SchedError::StaleGeneration { .. })),
338 "{r:?}"
339 );
340 assert_eq!(h.sched.state("b"), Some(TaskState::AwaitingFinalProof));
341 assert_eq!(h.generation("b"), 2);
342}
343
344#[test]
345fn interface_republished_at_a_new_commit_retires_the_old_receipt() {
346 let mut h = H::new(enabled());
347 h.register("a", 1, &[]);
348 let old = h.mint_interface("a", 1);
349 // Same generation and interface, new commit: a new receipt replaces it.
350 let new = h
351 .engine
352 .publish_interface(&InterfaceSubmission {
353 task_id: "a".into(),
354 generation: 1,
355 source_commit: "2222222222222222222222222222222222222222".into(),
356 interface: json!({ "task": "a", "api_version": 1 }),
357 })
358 .unwrap()
359 .claim();
360 assert_eq!(old.digest, new.digest);
361 assert_ne!(old.receipt_id, new.receipt_id);
362 let store = &h.engine.completion_store;
363 assert!(verify_interface(store, "a", 1, &new).is_ok());
364 assert!(matches!(
365 verify_interface(store, "a", 1, &old),
366 Err(CompletionError::NotCurrentCompletion(_))
367 ));
368}