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
aien-sovereign-core-mv / crates / aien-runtime / src / trace_observer.rs
| 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 | //! Correlation tap on the scheduler's admit/preempt decision (#398). |
| 17 | //! |
| 18 | //! Implements the scheduler's read-only `DecisionObserver`. For each |
| 19 | //! `AdmitPreemptRecord` it emits `sequence_admitted` for every queue head the |
| 20 | //! scheduler admitted (`AdmittedPrefill` or `AdmittedDecode` in |
| 21 | //! `alternatives.admission_examined`) and `sequence_preempted` for every id in |
| 22 | //! `choice.preempted`. Only sequences registered through |
| 23 | //! [`TraceDecisionObserver::register`] produce events; unknown sequences are |
| 24 | //! skipped without allocating. Observational only: it never changes a |
| 25 | //! scheduling decision, and it copies ids, never prompt or token data. |
| 26 | |
| 27 | use aien_scheduler::dual_observer::{AdmissionVerdict, AdmitPreemptRecord, DecisionObserver}; |
| 28 | use aien_trace::{ |
| 29 | now_unix_ms, CorrelationIds, EventKind, EventStatus, EvidenceRefs, TraceEvent, TraceId, |
| 30 | TraceSink, |
| 31 | }; |
| 32 | use parking_lot::Mutex; |
| 33 | use std::collections::HashMap; |
| 34 | use std::sync::Arc; |
| 35 | |
| 36 | pub struct TraceDecisionObserver { |
| 37 | sink: Arc<dyn TraceSink>, |
| 38 | /// sequence (packed u64) -> (trace id, parent event id) |
| 39 | trace_for_sequence: Mutex<HashMap<u64, (TraceId, u64)>>, |
| 40 | } |
| 41 | |
| 42 | impl TraceDecisionObserver { |
| 43 | pub fn new(sink: Arc<dyn TraceSink>) -> Self { |
| 44 | Self { |
| 45 | sink, |
| 46 | trace_for_sequence: Mutex::new(HashMap::new()), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /// Opt a sequence in. `parent` is the event its scheduler events hang under. |
| 51 | pub fn register(&self, seq: u64, trace: TraceId, parent: u64) { |
| 52 | self.trace_for_sequence.lock().insert(seq, (trace, parent)); |
| 53 | } |
| 54 | |
| 55 | /// Forget a sequence (after it finished or was cancelled). |
| 56 | pub fn unregister(&self, seq: u64) { |
| 57 | self.trace_for_sequence.lock().remove(&seq); |
| 58 | } |
| 59 | |
| 60 | fn emit(&self, seq: u64, kind: EventKind, step_id: u64) { |
| 61 | let Some((trace_id, parent)) = self.trace_for_sequence.lock().get(&seq).copied() else { |
| 62 | return; |
| 63 | }; |
| 64 | self.sink.emit(TraceEvent { |
| 65 | trace_id, |
| 66 | event_id: self.sink.next_event_id(), |
| 67 | parent_event_id: Some(parent), |
| 68 | at_unix_ms: now_unix_ms(), |
| 69 | kind, |
| 70 | status: EventStatus::Ok, |
| 71 | effect_certainty: None, |
| 72 | ids: CorrelationIds { |
| 73 | sequence_id: Some(seq), |
| 74 | step_id: Some(step_id), |
| 75 | ..Default::default() |
| 76 | }, |
| 77 | refs: EvidenceRefs::default(), |
| 78 | note: None, |
| 79 | }); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl DecisionObserver for TraceDecisionObserver { |
| 84 | fn observe_admit_preempt(&self, record: &AdmitPreemptRecord) { |
| 85 | if !self.sink.enabled() { |
| 86 | return; |
| 87 | } |
| 88 | for c in &record.alternatives.admission_examined { |
| 89 | if matches!( |
| 90 | c.verdict, |
| 91 | AdmissionVerdict::AdmittedDecode | AdmissionVerdict::AdmittedPrefill { .. } |
| 92 | ) { |
| 93 | self.emit(c.seq.to_u64(), EventKind::SequenceAdmitted, record.step_id); |
| 94 | } |
| 95 | } |
| 96 | for s in &record.choice.preempted { |
| 97 | self.emit(s.to_u64(), EventKind::SequencePreempted, record.step_id); |
| 98 | } |
| 99 | } |
| 100 | } |