Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/crates/spark-harness/src/scheduler/dedup.rs # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / crates / spark-harness / src / scheduler / dedup.rs` 136 lines ยท 5093 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 //! Bounded memory of applied event ids, for idempotent redelivery. 17 //! 18 //! Two scopes keep it bounded: 19 //! - Every task-directed event carries the task's generation and is 20 //! remembered under that task and generation. When the task moves to a 21 //! higher generation the older buckets are dropped: a redelivery of such 22 //! an event is then refused as a stale generation instead of reported as 23 //! a duplicate, and either way it changes nothing. Each of these events 24 //! happens at most a few times per generation, so a bucket stays small. 25 //! - The rest is remembered in a FIFO window of 26 //! `ProvisionalConfig::dedup_window` ids, and each stays safe when a 27 //! redelivery arrives after its id left the window: `RegisterTask` and 28 //! `AddDependency` are refused (duplicate node or edge), `Heartbeat` is 29 //! refused because its beat number is not above the last accepted one 30 //! (heartbeats are the one task-directed event that repeats without 31 //! bound, so they are not kept per generation), a repeated `Tick` only 32 //! re-checks leases, and `AuditRevoke` ids are reserved and never 33 //! redelivered. 34 //! 35 //! Pruning is deterministic, so a journal replay rebuilds the same memory. 36 37 use super::core::EventKind; 38 use serde::Serialize; 39 use std::collections::{BTreeMap, VecDeque}; 40 41 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] 42 pub struct Seen { 43 /// id -> payload digest for every remembered event. 44 ids: BTreeMap, 45 /// task -> generation -> ids of generation-bound events. 46 by_task: BTreeMap>>, 47 /// ids of generation-free events, oldest first. 48 window: VecDeque, 49 } 50 51 /// The task and generation an event is bound to, if any. 52 pub(crate) fn generation_scope(kind: &EventKind) -> Option<(&str, u64)> { 53 match kind { 54 EventKind::InterfacePublished { 55 task, generation, .. 56 } 57 | EventKind::CompletionVerified { 58 task, generation, .. 59 } 60 | EventKind::NewGeneration { task, generation } 61 | EventKind::ProducerFailed { 62 task, generation, .. 63 } 64 | EventKind::WorkDone { 65 task, generation, .. 66 } 67 | EventKind::Finalize { task, generation } 68 | EventKind::Cancel { task, generation } 69 | EventKind::Retry { task, generation } 70 | EventKind::Revoke { 71 task, generation, .. 72 } 73 | EventKind::BeginCommit { 74 task, generation, .. 75 } 76 | EventKind::CommitOutcome { 77 task, generation, .. 78 } 79 | EventKind::BindApproval { 80 task, generation, .. 81 } => Some((task.as_str(), *generation)), 82 EventKind::RegisterTask { .. } 83 | EventKind::AddDependency { .. } 84 | EventKind::Heartbeat { .. } 85 | EventKind::AuditRevoke { .. } 86 | EventKind::Tick => None, 87 } 88 } 89 90 impl Seen { 91 pub fn get(&self, id: &str) -> Option<&String> { 92 self.ids.get(id) 93 } 94 95 pub fn contains(&self, id: &str) -> bool { 96 self.ids.contains_key(id) 97 } 98 99 pub fn len(&self) -> usize { 100 self.ids.len() 101 } 102 103 pub(crate) fn record(&mut self, id: &str, digest: String, kind: &EventKind, window: usize) { 104 self.ids.insert(id.to_string(), digest); 105 match generation_scope(kind) { 106 Some((task, generation)) => self 107 .by_task 108 .entry(task.to_string()) 109 .or_default() 110 .entry(generation) 111 .or_default() 112 .push(id.to_string()), 113 None => { 114 self.window.push_back(id.to_string()); 115 while self.window.len() > window { 116 if let Some(old) = self.window.pop_front() { 117 self.ids.remove(&old); 118 } 119 } 120 } 121 } 122 } 123 124 /// Forget generation-bound ids below each task's current generation. 125 pub(crate) fn prune(&mut self, current: impl Fn(&str) -> Option) { 126 let Seen { ids, by_task, .. } = self; 127 for (task, gens) in by_task.iter_mut() { 128 let Some(now) = current(task) else { continue }; 129 let keep = gens.split_off(&now); 130 for old in std::mem::replace(gens, keep).into_values().flatten() { 131 ids.remove(&old); 132 } 133 } 134 by_task.retain(|_, gens| !gens.is_empty()); 135 } 136 }