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
//! `Tenanted<S>`, `Scoped<'a, S>`, and the sealed `TenantScopedStore` marker
//! trait — the structural foundation for tenant-scoped storage access.
//!
//! Per AUTHZ-0 principle 1 ("trust is structural, not procedural"), an
//! activation must not be able to hold a bare storage handle and call its
//! query API directly. The wrapper introduced here is the load-bearing
//! enforcement: an activation receives a [`Tenanted<S>`], the inner store
//! is unreachable, and the only way to call any storage method is through
//! a tenant-tagged [`Scoped<'a, S>`] borrow obtained by
//! [`Tenanted::scoped`].
//!
//! See `plans/AUTHZ/AUTHZ-DATA-1-WRAPPER.md` for the contract and
//! `plans/AUTHZ/AUTHZ-DATA-S01-output.md` §3 for the design rationale
//! (wrapper-plus-trait, option a with elements of b and c).
//!
//! # Sealing summary
//!
//! | Protection | Mechanism |
//! |----------------------------------|----------------------------------------------------------|
//! | Inner store unreachable | `Tenanted::inner` is module-private |
//! | No fabrication of `Tenanted` | `Tenanted::new_sealed` is `pub(crate)` |
//! | Marker trait cannot be implemented externally | `TenantScopedStore: seal::SealedStore` (private super) |
//! | `TenantBoundary` proof unforgeable | `TenantBoundary::new_sealed` is `pub(crate)` |
//! | No accidental `Default` | Not derived on any sealed type |
//! | No leaky `Deserialize` | Not derived on any sealed type |
//!
//! # The three structural compile-fails (per ticket §"Failing examples")
//!
//! ## 1. Reaching for the inner store directly
//!
//! ```compile_fail
//! use plexus_auth_core::tenant::storage::Tenanted;
//! fn leak<S>(t: &Tenanted<S>)
//! where
//! S: plexus_auth_core::TenantScopedStore,
//! {
//! let _ = &t.inner;
//! }
//! ```
//!
//! Diagnostic: `field 'inner' of struct 'Tenanted' is private` (E0616).
//!
//! ## 2. Constructing a `Tenant` from a literal inside an activation
//!
//! ```compile_fail
//! use plexus_auth_core::Tenant;
//! let _ = Tenant::try_new("victim-tenant");
//! ```
//!
//! Diagnostic: `associated function 'try_new' is private` (E0624).
//!
//! ## 3. Implementing `TenantScopedStore` for a sibling-crate type without
//! the sealed super-trait
//!
//! ```compile_fail
//! struct MyStore;
//! impl plexus_auth_core::TenantScopedStore for MyStore {
//! type Error = std::io::Error;
//! }
//! ```
//!
//! Diagnostic: `the trait bound 'MyStore: SealedStore' is not satisfied`
//! (E0277), pointing to the private `seal::SealedStore` super-trait.
//!
//! # Canonical happy path (passing doc test)
//!
//! The doc test below demonstrates the end-to-end pattern using the
//! framework-supplied [`reference::InMemoryKvStore`] (a pre-sealed
//! reference store kept in this crate so the canonical pattern is
//! exercise-able from a doc test). In a real activation, the type
//! satisfying [`TenantScopedStore`] is the activation's own storage
//! handle, the impl is generated by `AUTHZ-DATA-2-MACRO`, and domain
//! methods are added by a trait implemented on
//! [`Scoped<'_, MyStore>`] from inside the activation crate (Rust's
//! orphan rule forbids inherent impls on a foreign type — see the
//! run-notes for the design implication).
//!
//! ```
//! use plexus_auth_core::tenant::storage::{
//! reference::InMemoryKvStore, Scoped, TenantScopedStore, Tenanted,
//! };
//! use plexus_auth_core::Tenant;
//!
//! // 1. The framework hands the activation a `Tenanted<S>`.
//! // Here we mint one via the framework-blessed reference store.
//! let store = InMemoryKvStore::new();
//! store.put_for_doc_test("acme", "widget-1", b"sprocket".to_vec());
//! store.put_for_doc_test("acme", "widget-2", b"flange".to_vec());
//! store.put_for_doc_test("beta", "widget-1", b"do-not-leak".to_vec());
//! let tenanted: Tenanted<InMemoryKvStore> =
//! plexus_auth_core::tenant::storage::__doctest_blessed_tenanted(store);
//!
//! // 2. The framework hands the activation a `&Tenant` extension.
//! let tenant: Tenant =
//! plexus_auth_core::tenant::storage::__doctest_blessed_tenant("acme");
//!
//! // 3. The activation calls `.scoped(tenant)` and invokes domain
//! // methods on the borrow. For the reference store, those methods
//! // are pre-defined on `Scoped<'_, InMemoryKvStore>`.
//! let scoped: Scoped<'_, InMemoryKvStore> = tenanted.scoped(&tenant);
//! let names = scoped.list_keys();
//! assert_eq!(names.len(), 2);
//! assert!(names.contains(&"widget-1".to_string()));
//! assert!(names.contains(&"widget-2".to_string()));
//! ```
use crateTenant;
// ---------------------------------------------------------------------------
// Sealing module — private super-trait pattern.
//
// `SealedStore` lives in a `pub(crate)` module. The trait itself is `pub`
// inside the module so framework-internal types can implement it via the
// macro `seal_store_impl!`, but the path `seal::SealedStore` is
// `pub(crate)` overall — third-party crates cannot name it and therefore
// cannot satisfy the super-trait bound on `TenantScopedStore`.
//
// AUTHZ-DATA-1-WRAPPER ticket §"`TenantScopedStore` marker trait", row
// "Sealing", names this private re-export pattern.
// ---------------------------------------------------------------------------
pub
/// Crate-private macro that emits an empty `SealedStore` impl for a type.
///
/// Friend modules inside `plexus-auth-core` use this when introducing a
/// new `TenantScopedStore` candidate type. Outside crates cannot use it
/// because [`seal::SealedStore`] is unreachable.
///
/// Macros that are referenced via `$crate::tenant::storage::seal::...`
/// do not need an explicit `use seal_store_impl` import inside
/// sub-modules of this crate; rustc resolves the macro by name at the
/// invocation site within the same crate.
// ---------------------------------------------------------------------------
// TenantBoundary — zero-sized witness that a tenant boundary was crossed.
// ---------------------------------------------------------------------------
/// Zero-sized witness that a tenant boundary was crossed structurally.
///
/// A `TenantBoundary` value exists if and only if it was minted by
/// `plexus-auth-core` — its constructor is `pub(crate)`. The framework
/// hands a `TenantBoundary` into every [`Tenanted<S>`] at construction;
/// the value rides along on every [`Scoped<'a, S>`] obtained from that
/// wrapper. Downstream audit / observability layers can accept a
/// `TenantBoundary` argument to attest "this code path is structurally
/// downstream of a tenant scoping" without re-deriving the fact.
///
/// # Sealing
///
/// - **No fabrication.** `new_sealed` is `pub(crate)`.
/// - **No backdoor.** No public constructor, no public field, no
/// `From`/`Into`, no `Default`.
/// - **Copy-able.** `Copy` is derived so a `Scoped::boundary()` accessor
/// can return a value without invalidating the parent borrow's
/// witness.
// ---------------------------------------------------------------------------
// TenantScopedStore — the public marker trait.
// ---------------------------------------------------------------------------
/// Sealed capability marker for types that can be wrapped in a
/// [`Tenanted<S>`].
///
/// Implementing this trait is a structural declaration: "this storage
/// handle's domain methods should be defined on [`Scoped<'_, Self>`], not
/// on `Self` directly." The framework wraps the handle in [`Tenanted<S>`]
/// at activation construction; the activation reaches the methods only
/// through [`Tenanted::scoped`].
///
/// # Bounds
///
/// - `Send + Sync + 'static`: the wrapper crosses async boundaries and
/// is shared across concurrent requests.
/// - `seal::SealedStore`: the private super-trait that seals this trait.
/// Third-party crates cannot name `seal::SealedStore` and therefore
/// cannot implement `TenantScopedStore`.
///
/// # Associated `Error`
///
/// The framework standardizes on a single error type per store so that
/// activation domain methods on [`Scoped<'a, S>`] can name a single
/// failure type. `Error: std::error::Error + Send + Sync + 'static`
/// keeps it compatible with `anyhow::Error`, `tracing` formatting, and
/// the dispatch layer's audit envelope.
///
/// # No required methods
///
/// This trait carries no methods; it is a capability marker. Activation
/// authors will, in `AUTHZ-DATA-2-MACRO` and per-activation tickets,
/// define a domain trait inside their own crate and implement that trait
/// on [`Scoped<'a, MyStore>`]. (Rust's orphan rule for inherent impls
/// forbids `impl Scoped<'_, MyStore>` from outside this crate; the
/// activation pattern uses a trait + impl, generated by the framework
/// macro.)
// ---------------------------------------------------------------------------
// Tenanted<S> — the wrapper.
// ---------------------------------------------------------------------------
/// A tenant-scoped wrapper around a storage handle.
///
/// `Tenanted<S>` is the only legal carrier for a storage handle inside an
/// activation. The inner store is module-private; the constructor is
/// crate-private; the only public access path is [`scoped`](Self::scoped),
/// which requires a `&Tenant`.
///
/// # Construction
///
/// Activation code never constructs a `Tenanted<S>` directly. The
/// framework's hub-builder layer (a follow-up ticket) accepts a bare `S`
/// and hands a `Tenanted<S>` to the activation at startup. Inside
/// `plexus-auth-core`, the crate-private `new_sealed` is the only
/// constructor.
///
/// # Sealing
///
/// - **No fabrication.** Constructor is `pub(crate)`.
/// - **No inner reach.** The `inner` field is module-private; activation
/// code that writes `self.store.inner.method()` fails to compile.
/// - **No `Default`.** Not derived; a default-constructed wrapper would
/// widen the access boundary silently.
/// - **No `Deserialize`.** Not derived; the wrapper is not transport.
// ---------------------------------------------------------------------------
// Scoped<'a, S> — the tenant-tagged borrow.
// ---------------------------------------------------------------------------
/// A tenant-tagged borrow of a storage handle.
///
/// `Scoped<'a, S>` is produced by [`Tenanted::scoped`]. It bundles a
/// borrow of the inner store with a borrow of the active tenant. The
/// activation defines its domain methods via a trait implemented on
/// `Scoped<'a, MyStore>` (Rust's orphan rule forbids inherent impls on a
/// foreign type from another crate; the AUTHZ-DATA-2-MACRO layer
/// generates the trait + impl).
///
/// # Lifetime
///
/// The single `'a` lifetime is the shorter of the parent
/// [`Tenanted<S>`]'s borrow and the supplied `&Tenant`. Both must
/// outlive the `Scoped`. This is what makes "fabricated tenant" and
/// "mismatched tenants" structural failures: the lifetime threads
/// through every call site.
///
/// # Public surface
///
/// The framework supplies only [`store`](Self::store),
/// [`tenant`](Self::tenant), and [`boundary`](Self::boundary). The
/// activation's own domain methods live in a trait implementation on
/// `Scoped<'_, MyStore>` inside the activation's own crate.
///
/// # Sealing
///
/// - **No fabrication.** Constructor is module-private; the only path
/// is [`Tenanted::scoped`].
/// - **No struct-literal.** Fields are module-private.
/// - **No `Default`.** Not derived.
/// - **No `Deserialize`.** Not derived; the borrow is not transport.
// ---------------------------------------------------------------------------
// Reference store — `InMemoryKvStore` (the doc-test vehicle).
//
// A pre-sealed example `TenantScopedStore` is required because:
//
// 1. Rust's orphan rule (E0116) forbids `impl Scoped<'_, MyStore>` as an
// inherent impl from any crate that does not own `Scoped`. Activation
// code uses a trait + impl pattern (generated by AUTHZ-DATA-2-MACRO);
// that pattern is also what doc tests must follow.
// 2. The seal on `TenantScopedStore` is strict: no crate outside
// `plexus-auth-core` can satisfy `seal::SealedStore`. So the
// canonical happy-path doc test cannot define its own example store
// inline. The reference store lives inside this crate.
//
// `InMemoryKvStore` is a useful generic K/V example in its own right.
// Per the run-notes, it is the minimal type addition needed to give the
// ticket's acceptance criterion 6 a passing doc test.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Doc-test helpers (hidden — not part of the public API).
//
// These are `pub` because doc tests compile as external crates; they
// must be reachable. They are `#[doc(hidden)]` because they are NOT the
// framework's normal construction path — they exist solely so the
// canonical happy-path doc test can compile and run.
//
// The structural seal is unchanged: `TenantScopedStore` is still
// implementable only from inside this crate, so these helpers can wrap
// only types that are themselves sealed inside `plexus-auth-core`. A
// third-party crate gains nothing from calling them.
// ---------------------------------------------------------------------------
/// Doc-test helper: wrap a pre-sealed store in a `Tenanted<S>`.
///
/// **Not part of the framework's public construction path.** The
/// hub-builder API in a follow-up ticket is the supported way for an
/// activation to receive a `Tenanted<S>`.
/// Doc-test helper: mint a `Tenant` from a string literal.
///
/// **Not part of the framework's public construction path.** Real
/// callers obtain a `Tenant` only via [`crate::TenantResolver`].
// ---------------------------------------------------------------------------
// Unit tests.
// ---------------------------------------------------------------------------