bevy_semantics 0.2.0

Semantic identity, ontology, and query infrastructure for Bevy
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! Mutable semantic registry and bulk authoring support.

use std::any::{type_name, TypeId};
use std::sync::Arc;
use std::sync::RwLock;

use bevy_platform::collections::{HashMap, HashSet};

use crate::error::SemanticError;
use crate::kind::Kind;
use crate::query::SemanticCommand;
use crate::snapshot::SemanticSnapshot;
use crate::weight::Weight;

type SnapshotCache = Arc<RwLock<Option<(u64, Arc<SemanticSnapshot>)>>>;

/// Canonical core semantic kinds and relations.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Core {
    pub relation: Kind,
    pub namespace: Kind,
    pub is_a: Kind,
    pub not_a: Kind,
    pub can_be: Kind,
    pub cant_be: Kind,
    pub has_part: Kind,
    pub part_of: Kind,
    pub inverse_of: Kind,
    pub negates: Kind,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct KindFlags(u8);

impl KindFlags {
    const CORE: Self = Self(1 << 0);
    const TYPED: Self = Self(1 << 1);
    const USER_DEFINED: Self = Self(1 << 2);

    fn contains(self, other: Self) -> bool {
        self.0 & other.0 == other.0
    }

    fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }
}

#[derive(Clone, Debug)]
pub(crate) struct KindMeta {
    pub(crate) kind: Kind,
    pub(crate) name: Arc<str>,
    pub(crate) type_id: Option<TypeId>,
    pub(crate) flags: KindFlags,
}

#[derive(Clone, Debug, Default)]
pub(crate) struct GraphState {
    pub(crate) edges: HashMap<(Kind, Kind, Kind), Option<Weight>>,
}

/// Mutable source of truth for kinds and edge data.
#[derive(Clone, Debug)]
pub struct SemanticRegistry {
    pub(crate) version: u64,
    pub(crate) kinds: Vec<KindMeta>,
    pub(crate) kind_to_index: HashMap<Kind, usize>,
    pub(crate) name_to_kind: HashMap<Arc<str>, Kind>,
    pub(crate) type_to_kind: HashMap<TypeId, Kind>,
    pub(crate) kind_to_type: HashMap<Kind, TypeId>,
    pub(crate) core: Option<Core>,
    pub(crate) graph: GraphState,
    snapshot_cache: SnapshotCache,
    bulk_depth: usize,
    bulk_dirty: bool,
}

impl Default for SemanticRegistry {
    fn default() -> Self {
        let mut registry = Self {
            version: 0,
            kinds: Vec::new(),
            kind_to_index: HashMap::new(),
            name_to_kind: HashMap::new(),
            type_to_kind: HashMap::new(),
            kind_to_type: HashMap::new(),
            core: None,
            graph: GraphState::default(),
            snapshot_cache: Arc::new(RwLock::new(None)),
            bulk_depth: 0,
            bulk_dirty: false,
        };
        registry.begin_bulk_edit();
        let _ = registry
            .seed_core()
            .expect("core kinds are static and should always seed");
        registry.end_bulk_edit();
        registry
    }
}

impl SemanticRegistry {
    /// Return the current registry version.
    pub fn version(&self) -> u64 {
        self.version
    }

    /// Register a new canonical kind name or return the existing `Kind`.
    pub fn register_kind(&mut self, name: impl AsRef<str>) -> Result<Kind, SemanticError> {
        let canonical = canonicalize_name(name.as_ref())?;
        self.register_canonical_kind(canonical, None, KindFlags::USER_DEFINED)
    }

    /// Register the Rust type `T` under its default canonical name.
    pub fn typed_kind<T>(&mut self) -> Result<Kind, SemanticError>
    where
        T: 'static,
    {
        self.typed_kind_named::<T>(type_name::<T>())
    }

    /// Register the Rust type `T` under an explicit canonical name.
    pub fn typed_kind_named<T>(&mut self, name: impl AsRef<str>) -> Result<Kind, SemanticError>
    where
        T: 'static,
    {
        let canonical = canonicalize_name(name.as_ref())?;
        if let Some(namespace) = reserved_namespace_root(canonical.as_ref()) {
            return Err(SemanticError::CannotUseReservedNamespace {
                namespace: namespace.to_string(),
            });
        }
        let type_id = TypeId::of::<T>();

        if let Some(existing_kind) = self.type_to_kind.get(&type_id).copied() {
            let existing_name = self.name(existing_kind).unwrap_or("<unknown>").to_string();
            if existing_name == canonical.as_ref() {
                return Ok(existing_kind);
            }
            return Err(SemanticError::KindTypeConflict {
                kind: existing_kind,
                existing_type_name: existing_name,
                requested_type_name: type_name::<T>().to_string(),
            });
        }

        self.register_canonical_kind(canonical, Some(type_id), KindFlags::TYPED)
    }

    fn register_core_kind(&mut self, name: impl AsRef<str>) -> Result<Kind, SemanticError> {
        let canonical = canonicalize_name(name.as_ref())?;
        self.register_canonical_kind(canonical, None, KindFlags::CORE)
    }

    /// Look up a `Kind` by canonical name.
    pub fn kind(&self, name: impl AsRef<str>) -> Option<Kind> {
        let canonical = canonicalize_name(name.as_ref()).ok()?;
        self.name_to_kind.get(canonical.as_ref()).copied()
    }

    /// Look up the `Kind` bound to a Rust type.
    pub fn kind_of<T>(&self) -> Option<Kind>
    where
        T: 'static,
    {
        self.type_to_kind.get(&TypeId::of::<T>()).copied()
    }

    /// Resolve the canonical name for a `Kind`.
    pub fn name(&self, kind: Kind) -> Option<&str> {
        self.kind_to_index
            .get(&kind)
            .and_then(|index| self.kinds.get(*index))
            .map(|meta| meta.name.as_ref())
    }

    /// Resolve the Rust `TypeId` bound to a `Kind`, if any.
    pub fn type_id(&self, kind: Kind) -> Option<TypeId> {
        self.kind_to_type.get(&kind).copied()
    }

    /// Return the canonical core kinds and relations.
    pub fn core(&self) -> Core {
        self.core
            .expect("core kinds are seeded during registry initialization")
    }

    fn seed_core(&mut self) -> Result<Core, SemanticError> {
        let relation = self.register_core_kind("Relation")?;
        let namespace = self.register_core_kind("Namespace")?;
        let is_a = self.register_core_kind("is_a")?;
        let not_a = self.register_core_kind("not_a")?;
        let can_be = self.register_core_kind("can_be")?;
        let cant_be = self.register_core_kind("cant_be")?;
        let has_part = self.register_core_kind("has_part")?;
        let part_of = self.register_core_kind("part_of")?;
        let inverse_of = self.register_core_kind("inverse_of")?;
        let negates = self.register_core_kind("negates")?;

        for kind in [
            relation, is_a, not_a, can_be, cant_be, has_part, part_of, inverse_of, negates,
        ] {
            let _ = self.add_edge(kind, is_a, relation)?;
        }

        let _ = self.add_edge(has_part, inverse_of, part_of)?;
        let _ = self.add_edge(part_of, inverse_of, has_part)?;
        let _ = self.add_edge(is_a, negates, not_a)?;
        let _ = self.add_edge(not_a, negates, is_a)?;
        let _ = self.add_edge(can_be, negates, cant_be)?;
        let _ = self.add_edge(cant_be, negates, can_be)?;

        let core = Core {
            relation,
            namespace,
            is_a,
            not_a,
            can_be,
            cant_be,
            has_part,
            part_of,
            inverse_of,
            negates,
        };
        self.core = Some(core);
        Ok(core)
    }

    /// Build a fresh immutable snapshot.
    pub fn snapshot(&self) -> SemanticSnapshot {
        SemanticSnapshot::from_registry(self)
    }

    /// Return a cached immutable snapshot when possible.
    #[doc(alias = "snapshot_arc")]
    pub fn snapshot_cached(&self) -> Arc<SemanticSnapshot> {
        if let Some(cached) = self
            .snapshot_cache
            .read()
            .expect("snapshot cache poisoned")
            .as_ref()
            .and_then(|(version, snapshot)| {
                if *version == self.version {
                    Some(Arc::clone(snapshot))
                } else {
                    None
                }
            })
        {
            return cached;
        }

        let snapshot = Arc::new(self.snapshot());
        let mut cache = self
            .snapshot_cache
            .write()
            .expect("snapshot cache poisoned");
        if let Some((version, cached)) = cache.as_ref() {
            if *version == self.version {
                return Arc::clone(cached);
            }
        }

        *cache = Some((self.version, Arc::clone(&snapshot)));
        snapshot
    }

    /// Start a bulk-edit session that defers snapshot invalidation until commit.
    pub fn batch(&mut self) -> SemanticRegistryBatch<'_> {
        self.begin_bulk_edit();
        SemanticRegistryBatch {
            registry: self,
            changed: false,
            committed: false,
        }
    }

    /// Insert or update an unweighted semantic edge.
    pub fn add_edge(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
    ) -> Result<bool, SemanticError> {
        self.upsert_edge(subject, relation, target, None)
    }

    /// Insert or update a weighted semantic edge.
    pub fn add_edge_weighted(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
        weight: Weight,
    ) -> Result<bool, SemanticError> {
        self.upsert_edge(subject, relation, target, Some(weight))
    }

    /// Unregister a kind and remove any incident edges and type bindings.
    pub fn unregister_kind(&mut self, kind: Kind) -> Result<bool, SemanticError> {
        let index = self
            .kind_to_index
            .get(&kind)
            .copied()
            .ok_or(SemanticError::UnknownKind { kind })?;

        let meta = self.kinds[index].clone();
        if meta.flags.contains(KindFlags::CORE) {
            return Err(SemanticError::CannotUnregisterCoreKind { kind });
        }

        if let Some(type_id) = meta.type_id {
            self.kind_to_type.remove(&kind);
            self.type_to_kind.remove(&type_id);
        }

        self.name_to_kind.remove(meta.name.as_ref());
        self.kind_to_index.remove(&kind);

        let removed_meta = self.kinds.swap_remove(index);
        if let Some(moved_meta) = self.kinds.get(index) {
            self.kind_to_index.insert(moved_meta.kind, index);
        }
        debug_assert_eq!(removed_meta.kind, kind);

        self.graph.edges.retain(|key, _| {
            let (subject, relation, target) = *key;
            subject != kind && relation != kind && target != kind
        });

        self.bump_version();
        Ok(true)
    }

    /// Unregister every kind whose canonical name lives under the given namespace prefix.
    pub fn unregister_namespace(
        &mut self,
        namespace: impl AsRef<str>,
    ) -> Result<bool, SemanticError> {
        let requested = namespace.as_ref();
        if is_reserved_namespace_name(requested) {
            return Err(SemanticError::CannotUseReservedNamespace {
                namespace: reserved_namespace_root(requested)
                    .unwrap_or(requested)
                    .to_string(),
            });
        }
        let prefix = canonicalize_namespace_prefix(namespace.as_ref())?;
        let prefix = prefix.as_ref();

        let mut removed_kinds = Vec::new();
        for meta in &self.kinds {
            if meta.name.starts_with(prefix) {
                if meta.flags.contains(KindFlags::CORE) {
                    return Err(SemanticError::CannotUnregisterCoreKind { kind: meta.kind });
                }
                removed_kinds.push(meta.kind);
            }
        }

        if removed_kinds.is_empty() {
            return Ok(false);
        }

        let removed_set: HashSet<Kind> = removed_kinds.iter().copied().collect();

        self.kinds.retain(|meta| !removed_set.contains(&meta.kind));
        self.kind_to_index.clear();
        self.name_to_kind.clear();
        self.type_to_kind.clear();
        self.kind_to_type.clear();

        for (index, meta) in self.kinds.iter().enumerate() {
            self.kind_to_index.insert(meta.kind, index);
            self.name_to_kind.insert(meta.name.clone(), meta.kind);
            if let Some(type_id) = meta.type_id {
                self.kind_to_type.insert(meta.kind, type_id);
                self.type_to_kind.insert(type_id, meta.kind);
            }
        }

        self.graph.edges.retain(|key, _| {
            let (subject, relation, target) = *key;
            !removed_set.contains(&subject)
                && !removed_set.contains(&relation)
                && !removed_set.contains(&target)
        });

        self.bump_version();
        Ok(true)
    }

    /// Remove a semantic edge if it exists.
    pub fn remove_edge(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
    ) -> Result<bool, SemanticError> {
        self.ensure_known_kind(subject)?;
        self.ensure_known_kind(relation)?;
        self.ensure_known_kind(target)?;

        let key = (subject, relation, target);
        if self.graph.edges.remove(&key).is_some() {
            self.bump_version();
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Check whether an exact semantic edge exists.
    pub fn has_edge(&self, subject: Kind, relation: Kind, target: Kind) -> bool {
        self.graph.edges.contains_key(&(subject, relation, target))
    }

    /// Apply a single semantic command immediately.
    pub fn apply_command(&mut self, command: SemanticCommand) -> Result<bool, SemanticError> {
        match command {
            SemanticCommand::RegisterKind { name } => {
                let before = self.version;
                let _ = self.register_kind(name.as_ref())?;
                Ok(self.version != before)
            }
            SemanticCommand::RegisterTypedKind { type_id, name } => {
                let canonical = name.ok_or_else(|| SemanticError::MalformedCommand {
                    command: "RegisterTypedKind",
                    reason: "missing canonical name".to_string(),
                })?;
                let before = self.version;
                self.typed_kind_command(type_id, canonical)?;
                Ok(self.version != before)
            }
            SemanticCommand::AddEdge {
                subject,
                relation,
                target,
                weight,
            } => match weight {
                Some(weight) => self.add_edge_weighted(subject, relation, target, weight),
                None => self.add_edge(subject, relation, target),
            },
            SemanticCommand::UnregisterNamespace { namespace } => {
                self.unregister_namespace(namespace.as_ref())
            }
            SemanticCommand::UnregisterKind { kind } => self.unregister_kind(kind),
            SemanticCommand::RemoveEdge {
                subject,
                relation,
                target,
            } => self.remove_edge(subject, relation, target),
        }
    }

    /// Apply a batch of semantic commands with one bulk invalidation.
    pub fn apply_commands<I>(&mut self, commands: I) -> Result<bool, SemanticError>
    where
        I: IntoIterator<Item = SemanticCommand>,
    {
        self.begin_bulk_edit();
        let result = (|| {
            let mut changed = false;
            for command in commands {
                changed |= self.apply_command(command)?;
            }
            Ok(changed)
        })();
        self.end_bulk_edit();
        result
    }

    fn typed_kind_command(
        &mut self,
        type_id: TypeId,
        name: Arc<str>,
    ) -> Result<Kind, SemanticError> {
        let canonical = canonicalize_name(name.as_ref())?;
        if let Some(namespace) = reserved_namespace_root(canonical.as_ref()) {
            return Err(SemanticError::CannotUseReservedNamespace {
                namespace: namespace.to_string(),
            });
        }
        if let Some(existing_kind) = self.type_to_kind.get(&type_id).copied() {
            let existing_name = self.name(existing_kind).unwrap_or("<unknown>").to_string();
            if existing_name == canonical.as_ref() {
                return Ok(existing_kind);
            }
            return Err(SemanticError::KindTypeConflict {
                kind: existing_kind,
                existing_type_name: existing_name,
                requested_type_name: name.as_ref().to_string(),
            });
        }

        self.register_canonical_kind(canonical, Some(type_id), KindFlags::TYPED)
    }

    fn register_canonical_kind(
        &mut self,
        canonical: Arc<str>,
        type_id: Option<TypeId>,
        base_flags: KindFlags,
    ) -> Result<Kind, SemanticError> {
        if !base_flags.contains(KindFlags::CORE) {
            if let Some(namespace) = reserved_namespace_root(canonical.as_ref()) {
                return Err(SemanticError::CannotUseReservedNamespace {
                    namespace: namespace.to_string(),
                });
            }
        }

        if let Some(existing_kind) = self.name_to_kind.get(canonical.as_ref()).copied() {
            if let Some(requested_type) = type_id {
                if let Some(existing_type) = self.type_to_kind.get(&requested_type).copied() {
                    if existing_type != existing_kind {
                        return Err(SemanticError::KindTypeConflict {
                            kind: existing_kind,
                            existing_type_name: self
                                .name(existing_kind)
                                .unwrap_or("<unknown>")
                                .to_string(),
                            requested_type_name: canonical.as_ref().to_string(),
                        });
                    }
                }
                if let Some(existing_type) = self.kind_to_type.get(&existing_kind).copied() {
                    if existing_type != requested_type {
                        return Err(SemanticError::KindTypeConflict {
                            kind: existing_kind,
                            existing_type_name: self
                                .name(existing_kind)
                                .unwrap_or("<unknown>")
                                .to_string(),
                            requested_type_name: format!("{requested_type:?}"),
                        });
                    }
                } else {
                    self.kind_to_type.insert(existing_kind, requested_type);
                    self.type_to_kind.insert(requested_type, existing_kind);
                    if let Some(index) = self.kind_to_index.get(&existing_kind).copied() {
                        if let Some(meta) = self.kinds.get_mut(index) {
                            meta.type_id = Some(requested_type);
                            meta.flags = meta.flags.union(KindFlags::TYPED);
                        }
                    }
                    self.bump_version();
                }
            }
            if let Some(index) = self.kind_to_index.get(&existing_kind).copied() {
                if let Some(meta) = self.kinds.get_mut(index) {
                    let updated_flags = meta.flags.union(base_flags);
                    if updated_flags != meta.flags {
                        meta.flags = updated_flags;
                        self.bump_version();
                    }
                }
            }
            return Ok(existing_kind);
        }

        let kind = hash_canonical_name(canonical.as_ref());
        if let Some(existing_index) = self.kind_to_index.get(&kind).copied() {
            let existing_name = self
                .kinds
                .get(existing_index)
                .map(|meta| meta.name.as_ref().to_string())
                .unwrap_or_else(|| "<unknown>".to_string());
            return Err(SemanticError::KindHashCollision {
                kind,
                existing_name,
                requested_name: canonical.as_ref().to_string(),
            });
        }

        let index = self.kinds.len();
        self.kinds.push(KindMeta {
            kind,
            name: canonical.clone(),
            type_id,
            flags: base_flags,
        });
        self.kind_to_index.insert(kind, index);
        self.name_to_kind.insert(canonical, kind);
        if let Some(type_id) = type_id {
            self.kind_to_type.insert(kind, type_id);
            self.type_to_kind.insert(type_id, kind);
        }
        self.bump_version();
        Ok(kind)
    }

    fn upsert_edge(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
        weight: Option<Weight>,
    ) -> Result<bool, SemanticError> {
        self.ensure_known_kind(subject)?;
        self.ensure_known_kind(relation)?;
        self.ensure_known_kind(target)?;

        let key = (subject, relation, target);
        match self.graph.edges.get_mut(&key) {
            Some(existing) if *existing == weight => Ok(false),
            Some(existing) => {
                *existing = weight;
                self.bump_version();
                Ok(true)
            }
            None => {
                self.graph.edges.insert(key, weight);
                self.bump_version();
                Ok(true)
            }
        }
    }

    fn ensure_known_kind(&self, kind: Kind) -> Result<(), SemanticError> {
        if self.kind_to_index.contains_key(&kind) {
            Ok(())
        } else {
            Err(SemanticError::UnknownKind { kind })
        }
    }

    fn bump_version(&mut self) {
        self.version = self.version.wrapping_add(1);
        if self.bulk_depth == 0 {
            self.invalidate_snapshot_cache();
        } else {
            self.bulk_dirty = true;
        }
    }

    fn invalidate_snapshot_cache(&self) {
        *self
            .snapshot_cache
            .write()
            .expect("snapshot cache poisoned") = None;
    }

    fn begin_bulk_edit(&mut self) {
        self.bulk_depth += 1;
    }

    fn end_bulk_edit(&mut self) {
        if self.bulk_depth == 0 {
            return;
        }

        self.bulk_depth -= 1;
        if self.bulk_depth == 0 && self.bulk_dirty {
            self.bulk_dirty = false;
            self.invalidate_snapshot_cache();
        }
    }
}

/// Bulk registry editing session that defers snapshot invalidation until commit.
#[must_use]
pub struct SemanticRegistryBatch<'a> {
    registry: &'a mut SemanticRegistry,
    changed: bool,
    committed: bool,
}

impl<'a> SemanticRegistryBatch<'a> {
    /// Return whether the batch made any observable change.
    pub fn changed(&self) -> bool {
        self.changed
    }

    fn finish(&mut self) {
        if self.committed {
            return;
        }
        self.registry.end_bulk_edit();
        self.committed = true;
    }

    pub fn register_kind(&mut self, name: impl AsRef<str>) -> Result<Kind, SemanticError> {
        let before = self.registry.version();
        let kind = self.registry.register_kind(name)?;
        self.changed |= self.registry.version() != before;
        Ok(kind)
    }

    pub fn typed_kind<T>(&mut self) -> Result<Kind, SemanticError>
    where
        T: 'static,
    {
        let before = self.registry.version();
        let kind = self.registry.typed_kind::<T>()?;
        self.changed |= self.registry.version() != before;
        Ok(kind)
    }

    pub fn typed_kind_named<T>(&mut self, name: impl AsRef<str>) -> Result<Kind, SemanticError>
    where
        T: 'static,
    {
        let before = self.registry.version();
        let kind = self.registry.typed_kind_named::<T>(name)?;
        self.changed |= self.registry.version() != before;
        Ok(kind)
    }

    pub fn add_edge(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
    ) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self.registry.add_edge(subject, relation, target)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    pub fn add_edge_weighted(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
        weight: Weight,
    ) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self
            .registry
            .add_edge_weighted(subject, relation, target, weight)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    /// Unregister a kind and clean up all incident edges.
    pub fn unregister_kind(&mut self, kind: Kind) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self.registry.unregister_kind(kind)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    pub fn unregister_namespace(
        &mut self,
        namespace: impl AsRef<str>,
    ) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self.registry.unregister_namespace(namespace)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    pub fn remove_edge(
        &mut self,
        subject: Kind,
        relation: Kind,
        target: Kind,
    ) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self.registry.remove_edge(subject, relation, target)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    pub fn apply_command(&mut self, command: SemanticCommand) -> Result<bool, SemanticError> {
        let before = self.registry.version();
        let changed = self.registry.apply_command(command)?;
        self.changed |= changed || self.registry.version() != before;
        Ok(changed)
    }

    pub fn apply_commands<I>(&mut self, commands: I) -> Result<bool, SemanticError>
    where
        I: IntoIterator<Item = SemanticCommand>,
    {
        let mut changed = false;
        for command in commands {
            changed |= self.apply_command(command)?;
        }
        Ok(changed)
    }

    /// Finalize the batch and build a fresh snapshot.
    pub fn snapshot(self) -> SemanticSnapshot {
        let mut this = self;
        this.finish();
        this.registry.snapshot()
    }
}

impl<'a> std::ops::Deref for SemanticRegistryBatch<'a> {
    type Target = SemanticRegistry;

    fn deref(&self) -> &Self::Target {
        self.registry
    }
}

impl<'a> Drop for SemanticRegistryBatch<'a> {
    fn drop(&mut self) {
        self.finish();
    }
}

pub(crate) fn canonicalize_name(name: &str) -> Result<Arc<str>, SemanticError> {
    let canonical = name.trim();
    if canonical.is_empty() {
        return Err(SemanticError::InvalidName {
            name: name.to_string(),
        });
    }
    Ok(Arc::from(canonical))
}

pub(crate) fn canonicalize_namespace_prefix(namespace: &str) -> Result<Arc<str>, SemanticError> {
    let canonical = canonicalize_name(namespace)?;
    let trimmed = canonical.as_ref().trim_end_matches("::");
    if trimmed.is_empty() {
        return Err(SemanticError::InvalidName {
            name: namespace.to_string(),
        });
    }
    Ok(Arc::from(format!("{trimmed}::")))
}

fn reserved_namespace_root(name: &str) -> Option<&str> {
    let trimmed = name.trim();
    if trimmed.is_empty() {
        return None;
    }

    let root = trimmed
        .split_once("::")
        .map(|(root, _)| root)
        .unwrap_or(trimmed);
    if root.eq_ignore_ascii_case("core") {
        Some(root)
    } else {
        None
    }
}

fn is_reserved_namespace_name(name: &str) -> bool {
    reserved_namespace_root(name).is_some()
}

pub(crate) fn hash_canonical_name(name: &str) -> Kind {
    let mut hasher = blake3::Hasher::new();
    hasher.update(b"bevy_semantics.kind.v1:");
    hasher.update(name.as_bytes());
    let digest = hasher.finalize();
    let bytes = digest.as_bytes();
    let mut raw = [0u8; 8];
    raw.copy_from_slice(&bytes[..8]);
    Kind::from(u64::from_le_bytes(raw))
}