cloacina 0.11.1

A Rust library for resilient task execution and orchestration.
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/*
 *  Copyright 2026 Colliery Software
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

//! Scoped runtime unifying all cloacina registries.
//!
//! [`Runtime`] owns the registries for tasks, workflows, triggers, computation
//! graphs, and stream backends. Every entry can be registered and unregistered
//! at runtime, which is the mechanism the reconciler uses to hot-swap packages.
//!
//! The process-global static registries that predated `Runtime` were deleted
//! in CLOACI-T-0509. [`Runtime::new`] seeds itself from the `inventory` entries
//! emitted by the macros; the reconciler and Python bindings push into it
//! directly via [`Runtime::register_task`], [`Runtime::register_workflow`], etc.
//!
//! ```rust,ignore
//! use cloacina::Runtime;
//!
//! let runtime = Runtime::new(); // seeded from inventory
//! runtime.register_task(namespace, || Arc::new(my_task()));
//! runtime.unregister_workflow("obsolete_workflow");
//! ```
//!
//! # Tenant scoping (CLOACI-T-0924)
//!
//! One `Runtime` allocation is shared by every tenant's runner in
//! `cloacina-server` (`TenantRunnerCache::shared_runtime`), so the name-keyed
//! registries below carry the tenant **in the key**: they are
//! [`TenantKey`]`(tenant, name)` maps, matching the `EndpointKey` convention
//! CLOACI-T-0921 established for the `EndpointRegistry`. (`tasks` stays keyed
//! by [`TaskNamespace`] — `tenant::package::workflow::task` is persisted on
//! `task_executions` rows, so its readers genuinely have all four components.
//! Every reader of the other five registries addresses them by *bare name*.)
//!
//! The tenant is not threaded through every `get_workflow(&str)` call site.
//! Instead the **handle** carries it: `Runtime` is a cheap `Arc` share plus a
//! scope, and [`Runtime::scoped_to_tenant`] / [`Runtime::untenanted_view`] /
//! [`Runtime::admin_view`] produce differently-scoped views over the *same*
//! registries. `DefaultRunner` binds its handle to `config.tenant_id()` at
//! construction, so the scheduler, executor, reconciler and cron scheduler it
//! builds all inherit it.
//!
//! Resolution order is CLOACI-T-0921's, verbatim: the caller's own tenant, then
//! the untenanted entry (embedded / pre-multi-tenancy, and what
//! [`Runtime::seed_from_inventory`] always writes), then — for admin views only
//! — a *unique* cross-tenant match. Two tenants owning a name is never a guess.
//! An untenanted deployment therefore behaves exactly as it did before this
//! change: every entry is untenanted and every lookup hits step 2.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use parking_lot::RwLock;

use crate::computation_graph::stream_backend::{
    StreamBackendFactory, StreamBackendFuture, StreamConfig,
};
use crate::computation_graph::triggerless::TriggerlessGraphRegistration;
use crate::task::{Task, TaskNamespace};
use crate::tenant_scope::{resolve_tenant_key, visible_keys, TenantKey, TenantOwner, TenantScope};
use crate::trigger::Trigger;
use crate::workflow::Workflow;
use cloacina_computation_graph::{
    ComputationGraphConstructor, ComputationGraphRegistration, ReactorConstructor,
    ReactorRegistration,
};

/// Type alias for trigger-less graph constructor functions.
pub(crate) type TriggerlessGraphConstructor =
    Box<dyn Fn() -> TriggerlessGraphRegistration + Send + Sync>;

/// Type alias for task constructor functions.
pub(crate) type TaskConstructorFn = Box<dyn Fn() -> Arc<dyn Task> + Send + Sync>;

/// Type alias for workflow constructor functions.
pub(crate) type WorkflowConstructorFn = Box<dyn Fn() -> Workflow + Send + Sync>;

/// Type alias for trigger constructor functions.
pub(crate) type TriggerConstructorFn = Box<dyn Fn() -> Arc<dyn Trigger> + Send + Sync>;

/// A registration was refused because the name is already claimed inside the
/// same tenant by a different package (CLOACI-T-0924).
///
/// This mirrors `RegistryError::EndpointOwnershipConflict` from
/// CLOACI-T-0921: silently replacing the incumbent would cross-wire two
/// packages' entities, so the second package must rename.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum RuntimeRegistrationError {
    #[error(
        "{kind} '{name}' is already registered in tenant '{tenant}' by {existing}; \
         {incoming} cannot claim the same name — rename the {kind} in one of the two packages"
    )]
    OwnershipConflict {
        kind: &'static str,
        name: String,
        tenant: String,
        existing: String,
        incoming: String,
    },
}

/// One entry in a [`ScopedRegistry`]: the constructor plus who claimed it.
struct ScopedEntry<V> {
    owner: TenantOwner,
    value: V,
}

/// A `(tenant, name)`-keyed registry with owner-checked replacement.
///
/// Shared by the five bare-name registries so they have exactly one keying and
/// one resolution rule between them.
struct ScopedRegistry<V> {
    /// Operator-facing noun used in conflict errors ("workflow", "reactor", …).
    kind: &'static str,
    entries: RwLock<HashMap<TenantKey, ScopedEntry<V>>>,
}

impl<V> ScopedRegistry<V> {
    fn new(kind: &'static str) -> Self {
        Self {
            kind,
            entries: RwLock::new(HashMap::new()),
        }
    }

    /// Insert under `scope`'s own key, rejecting a claim on a live
    /// `(tenant, name)` held by a *different, named* package.
    fn insert(
        &self,
        scope: TenantScope<'_>,
        name: String,
        owner: TenantOwner,
        value: V,
    ) -> Result<(), RuntimeRegistrationError> {
        let key = scope.own_key(&name);
        let mut guard = self.entries.write();
        if let Some(existing) = guard.get(&key) {
            if !existing.owner.may_replace(&owner) {
                return Err(RuntimeRegistrationError::OwnershipConflict {
                    kind: self.kind,
                    name,
                    tenant: key.tenant_label().to_string(),
                    existing: existing.owner.label(),
                    incoming: owner.label(),
                });
            }
        }
        guard.insert(key, ScopedEntry { owner, value });
        Ok(())
    }

    /// Resolve `name` within `scope` and apply `f` to the stored value while
    /// the read lock is held (the values are constructor closures, so this is
    /// "call the constructor" at every call site).
    fn with<R>(&self, scope: TenantScope<'_>, name: &str, f: impl FnOnce(&V) -> R) -> Option<R> {
        let guard = self.entries.read();
        let key = resolve_tenant_key(&*guard, scope, name).ok()?;
        guard.get(&key).map(|entry| f(&entry.value))
    }

    /// Remove the entry `name` resolves to within `scope`. A non-admin caller
    /// can therefore never unregister another tenant's entry.
    fn remove(&self, scope: TenantScope<'_>, name: &str) -> bool {
        let mut guard = self.entries.write();
        let Ok(key) = resolve_tenant_key(&*guard, scope, name) else {
            return false;
        };
        guard.remove(&key).is_some()
    }

    /// Distinct entry names visible to `scope` (own tenant + untenanted, or
    /// everything for an admin view). Deduplicated, because a tenant entry and
    /// an untenanted entry can share a name.
    fn names(&self, scope: TenantScope<'_>) -> Vec<String> {
        let guard = self.entries.read();
        let mut seen = HashSet::new();
        visible_keys(&*guard, scope)
            .filter(|k| seen.insert(k.name.clone()))
            .map(|k| k.name.clone())
            .collect()
    }

    /// Every `(tenant, name)` key in the registry, unfiltered. Diagnostics and
    /// tests only.
    fn all_keys(&self) -> Vec<TenantKey> {
        self.entries.read().keys().cloned().collect()
    }

    /// Provenance of the entry `name` resolves to within `scope`, if any.
    fn owner(&self, scope: TenantScope<'_>, name: &str) -> Option<TenantOwner> {
        let guard = self.entries.read();
        let key = resolve_tenant_key(&*guard, scope, name).ok()?;
        guard.get(&key).map(|entry| entry.owner.clone())
    }

    /// Build the conflict error for an incoming claim on `name`.
    fn conflict(
        &self,
        scope: TenantScope<'_>,
        name: &str,
        existing: &TenantOwner,
        incoming: &TenantOwner,
    ) -> RuntimeRegistrationError {
        RuntimeRegistrationError::OwnershipConflict {
            kind: self.kind,
            name: name.to_string(),
            tenant: scope.own_key(name).tenant_label().to_string(),
            existing: existing.label(),
            incoming: incoming.label(),
        }
    }

    /// Whether `incoming` may claim `name` in `scope`: `Ok(true)` when the
    /// name is free, `Ok(false)` when an entry `incoming` is allowed to reuse
    /// or replace already exists, `Err` when a different package owns it.
    fn check_claim(
        &self,
        scope: TenantScope<'_>,
        name: &str,
        incoming: &TenantOwner,
    ) -> Result<bool, RuntimeRegistrationError> {
        match self.owner(scope, name) {
            None => Ok(true),
            Some(existing) if existing.may_replace(incoming) => Ok(false),
            Some(existing) => Err(self.conflict(scope, name, &existing, incoming)),
        }
    }

    fn len(&self) -> usize {
        self.entries.read().len()
    }
}

/// A scoped runtime holding the registries for every cloacina extension point.
///
/// All five namespaces — tasks, workflows, triggers, computation graphs, and
/// stream backends — are registered and unregistered through the same surface.
/// `Runtime` is cheap to clone: it shares its registries via `Arc`.
///
/// A clone carries the same tenant scope; use [`Runtime::scoped_to_tenant`],
/// [`Runtime::untenanted_view`] or [`Runtime::admin_view`] to get a
/// differently-scoped view over the same registries.
#[derive(Clone)]
pub struct Runtime {
    inner: Arc<RuntimeInner>,
    scope: RuntimeScope,
}

/// The owned form of [`TenantScope`] carried on a [`Runtime`] handle.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
struct RuntimeScope {
    tenant_id: Option<String>,
    is_admin: bool,
}

impl RuntimeScope {
    fn as_tenant_scope(&self) -> TenantScope<'_> {
        TenantScope {
            tenant_id: self.tenant_id.as_deref(),
            is_admin: self.is_admin,
        }
    }
}

struct RuntimeInner {
    /// Already `tenant::package::workflow::task`-keyed — see the module docs
    /// for why this one keeps [`TaskNamespace`] rather than [`TenantKey`].
    tasks: RwLock<HashMap<TaskNamespace, TaskConstructorFn>>,
    workflows: ScopedRegistry<WorkflowConstructorFn>,
    triggers: ScopedRegistry<TriggerConstructorFn>,
    computation_graphs: ScopedRegistry<ComputationGraphConstructor>,
    triggerless_graphs: ScopedRegistry<TriggerlessGraphConstructor>,
    reactors: ScopedRegistry<ReactorConstructor>,
    /// Keyed by backend *kind* (`"kafka"`, `"mock"`), not by a tenant-authored
    /// entity name — CLOACI-T-0921's audit classified this as collision-safe by
    /// construction, so it stays a plain name map.
    stream_backends: RwLock<HashMap<String, StreamBackendFactory>>,
}

impl Runtime {
    /// Create a runtime seeded with every macro-registered entry from the
    /// `inventory` crate (tasks, workflows, triggers, computation graphs,
    /// stream backends).
    ///
    /// `inventory` collects entries in a linker section and is read lazily
    /// after `main()`, so every entry registered by the `#[task]`,
    /// `#[workflow]`, `#[trigger]`, `#[computation_graph]`, and stream-backend
    /// macros in the current binary is visible here. For a blank-slate runtime
    /// (used by isolation-sensitive tests), use [`Runtime::empty`] instead.
    pub fn new() -> Self {
        let rt = Self::empty();
        rt.seed_from_inventory();
        rt
    }

    /// Create an empty runtime with no registered entries in any namespace.
    ///
    /// Use this when you want complete isolation — no macro-registered tasks,
    /// workflows, triggers, CGs, or stream backends are installed. Intended
    /// for unit tests; production code should generally use [`Runtime::new`].
    pub fn empty() -> Self {
        Self {
            inner: Arc::new(RuntimeInner {
                tasks: RwLock::new(HashMap::new()),
                workflows: ScopedRegistry::new("workflow"),
                triggers: ScopedRegistry::new("trigger"),
                computation_graphs: ScopedRegistry::new("computation graph"),
                triggerless_graphs: ScopedRegistry::new("trigger-less computation graph"),
                reactors: ScopedRegistry::new("reactor"),
                stream_backends: RwLock::new(HashMap::new()),
            }),
            scope: RuntimeScope::default(),
        }
    }

    // -----------------------------------------------------------------------
    // Tenant scoping (CLOACI-T-0924)
    // -----------------------------------------------------------------------

    /// The scope this handle registers and resolves under.
    fn scope(&self) -> TenantScope<'_> {
        self.scope.as_tenant_scope()
    }

    /// A view over the *same* registries bound to `tenant_id`.
    ///
    /// Registrations made through the returned handle land under
    /// `(tenant_id, name)`; lookups try that tenant first, then fall back to
    /// the untenanted (inventory / embedded) entries. This is what
    /// `DefaultRunner` calls with `config.tenant_id()`.
    pub fn scoped_to_tenant(&self, tenant_id: impl Into<String>) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            scope: RuntimeScope {
                tenant_id: Some(tenant_id.into()),
                is_admin: false,
            },
        }
    }

    /// A view over the same registries with no tenant — the embedded /
    /// pre-multi-tenancy scope, and the scope `seed_from_inventory` writes
    /// under.
    pub fn untenanted_view(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            scope: RuntimeScope::default(),
        }
    }

    /// A view that may resolve any tenant's entry, but only when the name is
    /// unique across tenants. Intended for operator/diagnostic surfaces.
    pub fn admin_view(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            scope: RuntimeScope {
                tenant_id: None,
                is_admin: true,
            },
        }
    }

    /// The tenant this handle is bound to, if any.
    pub fn tenant_id(&self) -> Option<&str> {
        self.scope.tenant_id.as_deref()
    }

    /// Whether this handle is an admin view.
    pub fn is_admin(&self) -> bool {
        self.scope.is_admin
    }

    /// `true` when both handles are views over the same registry allocation.
    ///
    /// Replaces `Arc::ptr_eq` comparisons of `Arc<Runtime>`: two per-tenant
    /// runners share one `RuntimeInner` but hold differently-scoped handles.
    pub fn shares_registries_with(&self, other: &Runtime) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }

    /// Populate the runtime from the `inventory` entries emitted by the
    /// macros.
    ///
    /// `inventory`'s linker-section collection works across `dlopen`'d cdylibs
    /// on Linux/macOS, so the reconciler calls this again after loading a new
    /// workflow package to pick up the entries emitted by that cdylib.
    ///
    /// Inventory entries are always registered **untenanted**, whatever scope
    /// this handle carries (CLOACI-T-0924). They are host-binary, compile-time
    /// declarations — never tenant-authored — and the reconciler re-runs this
    /// after every `dlopen`, so scoping them would cross-stamp one tenant's
    /// cdylib entries onto whichever tenant happened to load next. Untenanted
    /// entries stay reachable from every tenant via the resolution fallback,
    /// which is exactly the pre-CLOACI-T-0924 behaviour.
    pub fn seed_from_inventory(&self) {
        use crate::inventory_entries::{
            ComputationGraphEntry, ReactorEntry, StreamBackendEntry, TaskEntry, TriggerEntry,
            TriggerlessGraphEntry, WorkflowEntry,
        };

        let global = self.untenanted_view();

        for entry in inventory::iter::<TaskEntry> {
            let ns = (entry.namespace)();
            let ctor = entry.constructor;
            self.register_task(ns, move || ctor());
        }

        for entry in inventory::iter::<WorkflowEntry> {
            global.register_workflow(entry.name.to_string(), entry.constructor);
        }

        for entry in inventory::iter::<TriggerEntry> {
            global.register_trigger(entry.name.to_string(), entry.constructor);
        }

        for entry in inventory::iter::<ComputationGraphEntry> {
            global.register_computation_graph(entry.name.to_string(), entry.constructor);
        }

        for entry in inventory::iter::<TriggerlessGraphEntry> {
            global.register_triggerless_graph(entry.name.to_string(), entry.constructor);
        }

        for entry in inventory::iter::<ReactorEntry> {
            global.register_reactor(entry.name.to_string(), entry.constructor);
        }

        for entry in inventory::iter::<StreamBackendEntry> {
            let factory = entry.factory;
            self.register_stream_backend(
                entry.type_name.to_string(),
                Box::new(move |config| factory(config)),
            );
        }
    }

    // -----------------------------------------------------------------------
    // Task registry
    // -----------------------------------------------------------------------

    /// Register a task constructor for the given namespace.
    pub fn register_task<F>(&self, namespace: TaskNamespace, factory: F)
    where
        F: Fn() -> Arc<dyn Task> + Send + Sync + 'static,
    {
        self.inner
            .tasks
            .write()
            .insert(namespace, Box::new(factory));
    }

    /// Remove a task constructor. Returns true if the entry existed.
    pub fn unregister_task(&self, namespace: &TaskNamespace) -> bool {
        self.inner.tasks.write().remove(namespace).is_some()
    }

    /// Look up and instantiate a task by namespace.
    pub fn get_task(&self, namespace: &TaskNamespace) -> Option<Arc<dyn Task>> {
        self.inner.tasks.read().get(namespace).map(|ctor| ctor())
    }

    /// Check if a task is registered for the given namespace.
    #[cfg(test)]
    pub(crate) fn has_task(&self, namespace: &TaskNamespace) -> bool {
        self.inner.tasks.read().contains_key(namespace)
    }

    /// Snapshot of every currently-registered task namespace. Used by code
    /// that needs to enumerate tasks (e.g. collecting all tasks belonging to
    /// a specific tenant/package/workflow triple during Python import).
    pub fn task_namespaces(&self) -> Vec<TaskNamespace> {
        self.inner.tasks.read().keys().cloned().collect()
    }

    // -----------------------------------------------------------------------
    // Workflow registry
    // -----------------------------------------------------------------------

    /// Register a workflow constructor by name, under this handle's tenant.
    ///
    /// Unattributed registration: it never conflicts, so it cannot fail. Use
    /// [`Runtime::try_register_workflow`] from the package loader, where the
    /// owning package is known and a same-tenant collision must be loud.
    pub fn register_workflow<F>(&self, name: String, constructor: F)
    where
        F: Fn() -> Workflow + Send + Sync + 'static,
    {
        let _ = self.inner.workflows.insert(
            self.scope(),
            name,
            TenantOwner::unknown(),
            Box::new(constructor),
        );
    }

    /// Register a workflow constructor attributed to `owner`, rejecting a
    /// same-tenant claim on a name a *different* package already owns.
    pub fn try_register_workflow<F>(
        &self,
        owner: &TenantOwner,
        name: String,
        constructor: F,
    ) -> Result<(), RuntimeRegistrationError>
    where
        F: Fn() -> Workflow + Send + Sync + 'static,
    {
        self.inner
            .workflows
            .insert(self.scope(), name, owner.clone(), Box::new(constructor))
    }

    /// Remove a workflow constructor. Returns true if the entry existed.
    pub fn unregister_workflow(&self, name: &str) -> bool {
        self.inner.workflows.remove(self.scope(), name)
    }

    /// Look up and instantiate a workflow by name, within this handle's scope.
    pub fn get_workflow(&self, name: &str) -> Option<Workflow> {
        self.inner.workflows.with(self.scope(), name, |ctor| ctor())
    }

    /// Get every workflow name visible to this handle's scope.
    pub fn workflow_names(&self) -> Vec<String> {
        self.inner.workflows.names(self.scope())
    }

    /// Every `(tenant, workflow)` key in the process, unfiltered. Diagnostics
    /// and cross-tenant isolation tests.
    pub fn workflow_keys(&self) -> Vec<TenantKey> {
        self.inner.workflows.all_keys()
    }

    // -----------------------------------------------------------------------
    // Trigger registry
    // -----------------------------------------------------------------------

    /// Register a trigger constructor by name, under this handle's tenant.
    pub fn register_trigger<F>(&self, name: String, factory: F)
    where
        F: Fn() -> Arc<dyn Trigger> + Send + Sync + 'static,
    {
        let _ = self.inner.triggers.insert(
            self.scope(),
            name,
            TenantOwner::unknown(),
            Box::new(factory),
        );
    }

    /// Register a trigger constructor attributed to `owner`, rejecting a
    /// same-tenant claim on a name a *different* package already owns.
    pub fn try_register_trigger<F>(
        &self,
        owner: &TenantOwner,
        name: String,
        factory: F,
    ) -> Result<(), RuntimeRegistrationError>
    where
        F: Fn() -> Arc<dyn Trigger> + Send + Sync + 'static,
    {
        self.inner
            .triggers
            .insert(self.scope(), name, owner.clone(), Box::new(factory))
    }

    /// Ask whether `owner` may claim the trigger `name` in this handle's scope
    /// *before* registering it (CLOACI-T-0924).
    ///
    /// The package loader has a "reuse whatever the cdylib's `inventory`
    /// already registered" fast path, which would otherwise let a second
    /// package silently adopt (and later tear down) a first package's trigger.
    /// `Ok(true)` — free, register it. `Ok(false)` — an entry this owner may
    /// reuse or replace exists. `Err` — a different package owns the name in
    /// this tenant.
    pub fn may_claim_trigger(
        &self,
        owner: &TenantOwner,
        name: &str,
    ) -> Result<bool, RuntimeRegistrationError> {
        self.inner.triggers.check_claim(self.scope(), name, owner)
    }

    /// Remove a trigger constructor. Returns true if the entry existed.
    pub fn unregister_trigger(&self, name: &str) -> bool {
        self.inner.triggers.remove(self.scope(), name)
    }

    /// Look up and instantiate a trigger by name, within this handle's scope.
    pub fn get_trigger(&self, name: &str) -> Option<Arc<dyn Trigger>> {
        self.inner.triggers.with(self.scope(), name, |ctor| ctor())
    }

    /// Get every trigger name visible to this handle's scope.
    pub fn trigger_names(&self) -> Vec<String> {
        self.inner.triggers.names(self.scope())
    }

    /// Every `(tenant, trigger)` key in the process, unfiltered.
    pub fn trigger_keys(&self) -> Vec<TenantKey> {
        self.inner.triggers.all_keys()
    }

    // -----------------------------------------------------------------------
    // Computation graph registry
    // -----------------------------------------------------------------------

    /// Register a computation graph constructor by graph name, under this
    /// handle's tenant.
    pub fn register_computation_graph<F>(&self, name: String, constructor: F)
    where
        F: Fn() -> ComputationGraphRegistration + Send + Sync + 'static,
    {
        let _ = self.inner.computation_graphs.insert(
            self.scope(),
            name,
            TenantOwner::unknown(),
            Box::new(constructor),
        );
    }

    /// Register a computation graph attributed to `owner`, rejecting a
    /// same-tenant claim on a name a *different* package already owns.
    pub fn try_register_computation_graph<F>(
        &self,
        owner: &TenantOwner,
        name: String,
        constructor: F,
    ) -> Result<(), RuntimeRegistrationError>
    where
        F: Fn() -> ComputationGraphRegistration + Send + Sync + 'static,
    {
        self.inner.computation_graphs.insert(
            self.scope(),
            name,
            owner.clone(),
            Box::new(constructor),
        )
    }

    /// Remove a computation graph constructor. Returns true if the entry existed.
    pub fn unregister_computation_graph(&self, name: &str) -> bool {
        self.inner.computation_graphs.remove(self.scope(), name)
    }

    /// Look up and instantiate a computation graph registration by name.
    pub fn get_computation_graph(&self, name: &str) -> Option<ComputationGraphRegistration> {
        self.inner
            .computation_graphs
            .with(self.scope(), name, |ctor| ctor())
    }

    /// Get every computation graph name visible to this handle's scope.
    pub fn computation_graph_names(&self) -> Vec<String> {
        self.inner.computation_graphs.names(self.scope())
    }

    /// Every `(tenant, graph)` key in the process, unfiltered.
    pub fn computation_graph_keys(&self) -> Vec<TenantKey> {
        self.inner.computation_graphs.all_keys()
    }

    // -----------------------------------------------------------------------
    // Trigger-less computation graph registry
    // -----------------------------------------------------------------------

    /// Register a trigger-less computation graph constructor by graph name.
    ///
    /// Trigger-less graphs are declared with `#[computation_graph(graph =
    /// { ... })]` (no `trigger = reactor(...)` clause) and operate on a
    /// `Context<Value>`. They are invoked directly by workflow tasks
    /// (T-02) and Python decorators (T-03).
    pub fn register_triggerless_graph<F>(&self, name: String, constructor: F)
    where
        F: Fn() -> TriggerlessGraphRegistration + Send + Sync + 'static,
    {
        let _ = self.inner.triggerless_graphs.insert(
            self.scope(),
            name,
            TenantOwner::unknown(),
            Box::new(constructor),
        );
    }

    /// Register a trigger-less graph attributed to `owner`, rejecting a
    /// same-tenant claim on a name a *different* package already owns.
    pub fn try_register_triggerless_graph<F>(
        &self,
        owner: &TenantOwner,
        name: String,
        constructor: F,
    ) -> Result<(), RuntimeRegistrationError>
    where
        F: Fn() -> TriggerlessGraphRegistration + Send + Sync + 'static,
    {
        self.inner.triggerless_graphs.insert(
            self.scope(),
            name,
            owner.clone(),
            Box::new(constructor),
        )
    }

    /// Ask whether `owner` may claim the trigger-less graph `name` in this
    /// handle's scope before registering it. See
    /// [`Runtime::may_claim_trigger`] for the three outcomes.
    pub fn may_claim_triggerless_graph(
        &self,
        owner: &TenantOwner,
        name: &str,
    ) -> Result<bool, RuntimeRegistrationError> {
        self.inner
            .triggerless_graphs
            .check_claim(self.scope(), name, owner)
    }

    /// Remove a trigger-less graph constructor. Returns true if the entry existed.
    pub fn unregister_triggerless_graph(&self, name: &str) -> bool {
        self.inner.triggerless_graphs.remove(self.scope(), name)
    }

    /// Look up and instantiate a trigger-less graph registration by name.
    pub fn get_triggerless_graph(&self, name: &str) -> Option<TriggerlessGraphRegistration> {
        self.inner
            .triggerless_graphs
            .with(self.scope(), name, |ctor| ctor())
    }

    /// Get every trigger-less graph name visible to this handle's scope.
    pub fn triggerless_graph_names(&self) -> Vec<String> {
        self.inner.triggerless_graphs.names(self.scope())
    }

    /// Every `(tenant, trigger-less graph)` key in the process, unfiltered.
    pub fn triggerless_graph_keys(&self) -> Vec<TenantKey> {
        self.inner.triggerless_graphs.all_keys()
    }

    // -----------------------------------------------------------------------
    // Reactor registry
    // -----------------------------------------------------------------------

    /// Register a reactor constructor by name.
    ///
    /// Reactors declared via `#[reactor]` or synthesized by the bundled form
    /// of `#[computation_graph]` land here. Graphs that declare
    /// `trigger = reactor(X)` bind to the named reactor at load time.
    pub fn register_reactor<F>(&self, name: String, constructor: F)
    where
        F: Fn() -> ReactorRegistration + Send + Sync + 'static,
    {
        let _ = self.inner.reactors.insert(
            self.scope(),
            name,
            TenantOwner::unknown(),
            Box::new(constructor),
        );
    }

    /// Register a reactor attributed to `owner`, rejecting a same-tenant claim
    /// on a name a *different* package already owns.
    pub fn try_register_reactor<F>(
        &self,
        owner: &TenantOwner,
        name: String,
        constructor: F,
    ) -> Result<(), RuntimeRegistrationError>
    where
        F: Fn() -> ReactorRegistration + Send + Sync + 'static,
    {
        self.inner
            .reactors
            .insert(self.scope(), name, owner.clone(), Box::new(constructor))
    }

    /// Remove a reactor constructor. Returns true if the entry existed.
    pub fn unregister_reactor(&self, name: &str) -> bool {
        self.inner.reactors.remove(self.scope(), name)
    }

    /// Look up and instantiate a reactor registration by name.
    pub fn get_reactor(&self, name: &str) -> Option<ReactorRegistration> {
        self.inner.reactors.with(self.scope(), name, |ctor| ctor())
    }

    /// Get every reactor name visible to this handle's scope.
    pub fn reactor_names(&self) -> Vec<String> {
        self.inner.reactors.names(self.scope())
    }

    /// Every `(tenant, reactor)` key in the process, unfiltered.
    pub fn reactor_keys(&self) -> Vec<TenantKey> {
        self.inner.reactors.all_keys()
    }

    // -----------------------------------------------------------------------
    // Stream backend registry
    // -----------------------------------------------------------------------

    /// Register a stream backend factory by type name (e.g. `"kafka"`, `"mock"`).
    pub fn register_stream_backend(&self, type_name: String, factory: StreamBackendFactory) {
        self.inner
            .stream_backends
            .write()
            .insert(type_name, factory);
    }

    /// Remove a stream backend factory. Returns true if the entry existed.
    pub fn unregister_stream_backend(&self, type_name: &str) -> bool {
        self.inner
            .stream_backends
            .write()
            .remove(type_name)
            .is_some()
    }

    /// Check if a stream backend is registered for the given type name.
    #[cfg(test)]
    pub(crate) fn has_stream_backend(&self, type_name: &str) -> bool {
        self.inner.stream_backends.read().contains_key(type_name)
    }

    /// Get the creation future for a stream backend without holding the lock
    /// across await. Returns `None` if the type is not registered.
    pub fn create_stream_backend(
        &self,
        type_name: &str,
        config: StreamConfig,
    ) -> Option<StreamBackendFuture> {
        let guard = self.inner.stream_backends.read();
        let factory = guard.get(type_name)?;
        Some(factory(config))
    }

    /// Get all registered stream backend type names.
    #[cfg(test)]
    pub(crate) fn stream_backend_names(&self) -> Vec<String> {
        self.inner.stream_backends.read().keys().cloned().collect()
    }
}

impl Default for Runtime {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for Runtime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let tasks = self.inner.tasks.read().len();
        let workflows = self.inner.workflows.len();
        let triggers = self.inner.triggers.len();
        let cgs = self.inner.computation_graphs.len();
        let sbs = self.inner.stream_backends.read().len();
        f.debug_struct("Runtime")
            .field("tenant", &self.scope.tenant_id)
            .field("is_admin", &self.scope.is_admin)
            .field("tasks", &tasks)
            .field("workflows", &workflows)
            .field("triggers", &triggers)
            .field("computation_graphs", &cgs)
            .field("stream_backends", &sbs)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::task::TaskNamespace;

    #[test]
    fn register_and_unregister_workflow() {
        let rt = Runtime::empty();
        assert!(!rt.unregister_workflow("nope"));

        let wf = crate::workflow::Workflow::new("unit-test-wf");
        rt.register_workflow("unit-test-wf".to_string(), move || wf.clone());
        assert!(rt.get_workflow("unit-test-wf").is_some());
        assert_eq!(rt.workflow_names(), vec!["unit-test-wf".to_string()]);

        assert!(rt.unregister_workflow("unit-test-wf"));
        assert!(rt.get_workflow("unit-test-wf").is_none());
        assert!(rt.workflow_names().is_empty());
    }

    #[test]
    fn register_and_unregister_trigger_by_name() {
        // Triggers need a Trigger trait impl; skip full integration here and
        // cover the lifecycle via the workflow test. The shape of the API is
        // identical across namespaces.
        let rt = Runtime::empty();
        assert!(!rt.unregister_trigger("missing"));
        assert!(rt.get_trigger("missing").is_none());
        assert!(rt.trigger_names().is_empty());
    }

    #[test]
    fn register_and_unregister_task() {
        let rt = Runtime::empty();
        let ns = TaskNamespace::new("t", "p", "w", "task_a");
        assert!(!rt.unregister_task(&ns));
        assert!(!rt.has_task(&ns));
    }

    #[test]
    fn stream_backend_roundtrip_names_only() {
        let rt = Runtime::empty();
        assert!(!rt.has_stream_backend("mock"));
        assert!(rt.stream_backend_names().is_empty());
        assert!(!rt.unregister_stream_backend("mock"));
    }

    #[test]
    fn runtimes_are_independent() {
        let rt1 = Runtime::empty();
        let rt2 = Runtime::empty();
        let wf = crate::workflow::Workflow::new("iso");
        rt1.register_workflow("iso".to_string(), move || wf.clone());

        assert!(rt1.get_workflow("iso").is_some());
        assert!(rt2.get_workflow("iso").is_none());
    }

    #[test]
    fn debug_format_reports_sizes() {
        let rt = Runtime::empty();
        let debug = format!("{:?}", rt);
        assert!(debug.contains("computation_graphs: 0"));
        assert!(debug.contains("stream_backends: 0"));
    }

    // -----------------------------------------------------------------------
    // CLOACI-T-0924: tenant keying
    // -----------------------------------------------------------------------

    fn wf(name: &str) -> crate::workflow::Workflow {
        crate::workflow::Workflow::new(name)
    }

    /// Two tenants register the same workflow name on ONE shared runtime and
    /// each resolves its own — the collision T-0924 was filed for.
    #[test]
    fn two_tenants_same_workflow_name_do_not_collide() {
        let shared = Runtime::empty();
        let acme = shared.scoped_to_tenant("acme");
        let globex = shared.scoped_to_tenant("globex");
        assert!(acme.shares_registries_with(&globex));

        let a = wf("acme-only-desc");
        acme.register_workflow("pipeline".to_string(), move || a.clone());
        let g = wf("globex-only-desc");
        globex.register_workflow("pipeline".to_string(), move || g.clone());

        assert_eq!(
            acme.get_workflow("pipeline").unwrap().name(),
            "acme-only-desc"
        );
        assert_eq!(
            globex.get_workflow("pipeline").unwrap().name(),
            "globex-only-desc"
        );
        // Both entries are physically present under distinct keys.
        assert_eq!(shared.workflow_keys().len(), 2);
    }

    /// Unloading one tenant's entry leaves the other tenant's alive.
    #[test]
    fn unregister_is_tenant_scoped() {
        let shared = Runtime::empty();
        let acme = shared.scoped_to_tenant("acme");
        let globex = shared.scoped_to_tenant("globex");

        let a = wf("a");
        acme.register_workflow("pipeline".to_string(), move || a.clone());
        let g = wf("g");
        globex.register_workflow("pipeline".to_string(), move || g.clone());

        assert!(acme.unregister_workflow("pipeline"));
        assert!(acme.get_workflow("pipeline").is_none());
        assert_eq!(globex.get_workflow("pipeline").unwrap().name(), "g");
        assert_eq!(shared.workflow_keys().len(), 1);
    }

    /// A non-admin handle can never see, resolve, or drop another tenant's
    /// entry.
    #[test]
    fn other_tenants_entries_are_invisible() {
        let shared = Runtime::empty();
        let acme = shared.scoped_to_tenant("acme");
        let a = wf("a");
        acme.register_workflow("private".to_string(), move || a.clone());

        let globex = shared.scoped_to_tenant("globex");
        assert!(globex.get_workflow("private").is_none());
        assert!(globex.workflow_names().is_empty());
        assert!(!globex.unregister_workflow("private"));
        // …and the entry is untouched.
        assert!(acme.get_workflow("private").is_some());
    }

    /// Two packages inside the SAME tenant claiming one name is a loud error,
    /// not a silent overwrite; the incumbent survives. Re-registration by the
    /// same package replaces (the package-reload path depends on it).
    #[test]
    fn same_tenant_cross_package_collision_is_loud() {
        let rt = Runtime::empty().scoped_to_tenant("acme");
        let first = TenantOwner::package("pkg-a");
        let second = TenantOwner::package("pkg-b");

        let a = wf("from-a");
        rt.try_register_workflow(&first, "reports".to_string(), move || a.clone())
            .expect("first claim");

        let b = wf("from-b");
        let err = rt
            .try_register_workflow(&second, "reports".to_string(), move || b.clone())
            .expect_err("second package must not silently replace");
        let msg = err.to_string();
        assert!(msg.contains("pkg-a"), "{msg}");
        assert!(msg.contains("pkg-b"), "{msg}");
        assert!(msg.contains("acme"), "{msg}");
        assert_eq!(rt.get_workflow("reports").unwrap().name(), "from-a");

        // Same owner re-registering replaces.
        let a2 = wf("from-a-v2");
        rt.try_register_workflow(&first, "reports".to_string(), move || a2.clone())
            .expect("same package may replace");
        assert_eq!(rt.get_workflow("reports").unwrap().name(), "from-a-v2");
    }

    /// The same name in two *different* tenants is not a conflict even when
    /// both are owned by named packages.
    #[test]
    fn cross_tenant_same_name_is_not_a_conflict() {
        let shared = Runtime::empty();
        let owner = TenantOwner::package("pkg-a");
        for tenant in ["acme", "globex"] {
            let w = wf(tenant);
            shared
                .scoped_to_tenant(tenant)
                .try_register_workflow(&owner, "reports".to_string(), move || w.clone())
                .expect("distinct tenants never collide");
        }
        assert_eq!(shared.workflow_keys().len(), 2);
    }

    /// EMBEDDED COMPATIBILITY: an untenanted deployment is unchanged — every
    /// entry is untenanted and every lookup, listing and unregister behaves
    /// exactly as it did before tenant keying.
    #[test]
    fn untenanted_path_is_unchanged() {
        let rt = Runtime::empty();
        assert_eq!(rt.tenant_id(), None);
        let w = wf("embedded");
        rt.register_workflow("embedded".to_string(), move || w.clone());
        assert!(rt.get_workflow("embedded").is_some());
        assert_eq!(rt.workflow_names(), vec!["embedded".to_string()]);
        assert!(rt.unregister_workflow("embedded"));
        assert!(rt.get_workflow("embedded").is_none());
        assert!(rt.workflow_names().is_empty());
    }

    /// Untenanted (inventory / macro-seeded) entries stay reachable from every
    /// tenant view — that fallback is what keeps `seed_from_inventory` working
    /// on a per-tenant runner.
    #[test]
    fn untenanted_entries_are_visible_from_a_tenant_view() {
        let shared = Runtime::empty();
        let w = wf("inventory");
        shared.register_workflow("inventory".to_string(), move || w.clone());

        let acme = shared.scoped_to_tenant("acme");
        assert!(acme.get_workflow("inventory").is_some());
        assert_eq!(acme.workflow_names(), vec!["inventory".to_string()]);
    }

    /// The tenant's own entry shadows a same-named untenanted one.
    #[test]
    fn own_tenant_shadows_untenanted() {
        let shared = Runtime::empty();
        let global = wf("global");
        shared.register_workflow("dup".to_string(), move || global.clone());
        let acme = shared.scoped_to_tenant("acme");
        let own = wf("own");
        acme.register_workflow("dup".to_string(), move || own.clone());

        assert_eq!(acme.get_workflow("dup").unwrap().name(), "own");
        assert_eq!(shared.get_workflow("dup").unwrap().name(), "global");
        // Deduplicated in listings even though two keys carry the name.
        assert_eq!(acme.workflow_names(), vec!["dup".to_string()]);
    }

    /// An admin view resolves a unique cross-tenant name and refuses an
    /// ambiguous one rather than guessing.
    #[test]
    fn admin_view_resolves_unique_and_refuses_ambiguous() {
        let shared = Runtime::empty();
        let a = wf("a");
        shared
            .scoped_to_tenant("acme")
            .register_workflow("only-acme".to_string(), move || a.clone());

        let admin = shared.admin_view();
        assert!(admin.is_admin());
        assert!(admin.get_workflow("only-acme").is_some());

        for tenant in ["acme", "globex"] {
            let w = wf(tenant);
            shared
                .scoped_to_tenant(tenant)
                .register_workflow("shared-name".to_string(), move || w.clone());
        }
        assert!(
            admin.get_workflow("shared-name").is_none(),
            "an ambiguous name must not resolve to a guess"
        );
        // But the admin view can still enumerate everything.
        let mut names = admin.workflow_names();
        names.sort();
        assert_eq!(
            names,
            vec!["only-acme".to_string(), "shared-name".to_string()]
        );
    }

    /// The same isolation holds for the reactor registry, which is what the
    /// reconciler's unload bookkeeping walks.
    #[test]
    fn reactors_and_graphs_are_tenant_scoped_too() {
        let shared = Runtime::empty();
        let acme = shared.scoped_to_tenant("acme");
        let globex = shared.scoped_to_tenant("globex");

        for rt in [&acme, &globex] {
            rt.register_triggerless_graph("g".to_string(), || unreachable!());
            rt.register_computation_graph("cg".to_string(), || unreachable!());
        }
        assert_eq!(shared.triggerless_graph_keys().len(), 2);
        assert_eq!(shared.computation_graph_keys().len(), 2);

        assert!(acme.unregister_triggerless_graph("g"));
        assert!(!acme.unregister_triggerless_graph("g"));
        assert_eq!(globex.triggerless_graph_names(), vec!["g".to_string()]);

        assert!(acme.unregister_computation_graph("cg"));
        assert_eq!(globex.computation_graph_names(), vec!["cg".to_string()]);
    }

    /// `seed_from_inventory` always writes untenanted entries, whatever scope
    /// the handle carries — otherwise the reconciler's post-dlopen re-seed
    /// would stamp one tenant's cdylib entries onto the next tenant to load.
    #[test]
    fn inventory_seeding_is_always_untenanted() {
        let shared = Runtime::empty();
        shared.scoped_to_tenant("acme").seed_from_inventory();
        assert!(
            shared.workflow_keys().iter().all(|k| k.tenant_id.is_none()),
            "inventory entries must never be stamped with a tenant"
        );
        assert!(shared.reactor_keys().iter().all(|k| k.tenant_id.is_none()));
        assert!(shared.trigger_keys().iter().all(|k| k.tenant_id.is_none()));
    }
}