drishti 0.2.0

Rust frontend orchestration framework: design publication plus deterministic reference cores for the Logical Works ink lab, the GPU residency model and the Logic OS Canvas protocol, motion, scene and native host broker; no qualified GPU backend yet.
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
//! Audit vocabulary and the one scope filter of the broker.
//!
//! Issues drishti#8, drishti#9, and drishti#10 meet here. Every broker decision becomes one typed
//! [`AuditEvent`] (caller, action, requested reach, decision, and the missing
//! permission on a denial). Every inspector reads through [`Visibility`], the
//! single filter that binds a read to the caller's [`Scope`] (session plus
//! tenant) unless the injected grant store allows [`Reach::AllTenants`].
//! Delivery uses the same [`same_scope`] rule, so reads and fan-out never
//! drift apart.

use std::fmt;

use super::super::capability::CapabilityError;
use super::{BrokerError, IntentHandle, RegisteredHandle};
use crate::canvas_protocol::{Capability, SessionInvariant};

/// Session plus tenant: the scope an authenticated caller is bound to.
/// # Examples
/// ```
/// use drishti::native_host::broker::Scope;
/// assert_ne!(Scope::new("tenant-a", "s1"), Scope::new("tenant-b", "s1"));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Scope {
    /// Tenant the caller belongs to.
    tenant_id: String,
    /// Session the caller acts in.
    session_id: String,
}

impl Scope {
    /// Bind a scope to a tenant and a session. Readers of tenant-keyed stores
    /// (the tape reader) take one, so no read can name a session alone.
    /// # Examples
    /// ```
    /// use drishti::native_host::broker::Scope;
    /// let scope = Scope::new("tenant-a", "s1");
    /// assert_eq!((scope.tenant_id(), scope.session_id()), ("tenant-a", "s1"));
    /// ```
    #[must_use]
    pub fn new(tenant_id: &str, session_id: &str) -> Self {
        Self {
            tenant_id: tenant_id.to_owned(),
            session_id: session_id.to_owned(),
        }
    }

    /// Tenant the caller belongs to.
    /// # Examples
    /// ```
    /// # use drishti::native_host::broker::Scope;
    /// assert_eq!(Scope::new("tenant-a", "s1").tenant_id(), "tenant-a");
    /// ```
    #[must_use]
    pub fn tenant_id(&self) -> &str {
        &self.tenant_id
    }

    /// Session the caller acts in.
    /// # Examples
    /// ```
    /// # use drishti::native_host::broker::Scope;
    /// assert_eq!(Scope::new("tenant-a", "s1").session_id(), "s1");
    /// ```
    #[must_use]
    pub fn session_id(&self) -> &str {
        &self.session_id
    }

    /// True when a record in `session_id` and `tenant_id` lies in this scope
    /// (both must match; see [`same_scope`]).
    fn holds(&self, session_id: &str, tenant_id: &str) -> bool {
        same_scope(&self.session_id, &self.tenant_id, session_id, tenant_id)
    }
}

/// The one scope rule: both session and tenant must match. Delivery and every
/// inspector call this, so a session id shared by two tenants never joins them.
pub(super) fn same_scope(session_a: &str, tenant_a: &str, session_b: &str, tenant_b: &str) -> bool {
    session_a == session_b && tenant_a == tenant_b
}

/// Who acted: the scope plus the user the session acts for. Only the broker
/// builds one, so read it off a captured [`AuditEvent`].
/// # Examples
/// ```
/// use drishti::cli::drive::{SESSION_ID, USER_SUB, captured, frozen_broker, session};
/// let (broker, events, _open) = frozen_broker();
/// let who = session(&mut String::new(), "tenant-a")?;
/// assert!(broker.register_command_bar(who).is_err());
/// let event = captured(&events).remove(0);
/// let caller = event.caller();
/// assert_eq!((caller.scope().session_id(), caller.user_sub()), (SESSION_ID, USER_SUB));
/// # Ok::<(), drishti::cli::Failure>(())
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Caller {
    /// Scope the caller is bound to.
    scope: Scope,
    /// User subject the session acts for.
    user_sub: String,
}

impl Caller {
    /// The caller behind an authenticated session.
    pub(super) fn of(session: &SessionInvariant) -> Self {
        Self::claimed(
            session.tenant_id(),
            session.session_id(),
            session.user_sub(),
        )
    }

    /// A caller known only by what it claims (a forged or retired handle).
    fn claimed(tenant_id: &str, session_id: &str, user_sub: &str) -> Self {
        Self {
            scope: Scope::new(tenant_id, session_id),
            user_sub: user_sub.to_owned(),
        }
    }

    /// The caller a widget handle claims.
    pub(super) fn from_widget(handle: &RegisteredHandle) -> Self {
        Self::claimed(handle.tenant_id(), handle.session_id(), handle.user_sub())
    }

    /// The caller a command-bar handle claims.
    pub(super) fn from_bar(handle: &IntentHandle) -> Self {
        Self::claimed(handle.tenant_id(), handle.session_id(), handle.user_sub())
    }

    /// The caller a peer handle claims: only the tenant is known.
    pub(super) fn from_tenant(tenant_id: &str) -> Self {
        Self::claimed(tenant_id, "", "")
    }

    /// Scope the caller is bound to.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// let event = captured(&events).remove(0);
    /// assert_eq!(event.caller().scope().tenant_id(), "tenant-a");
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn scope(&self) -> &Scope {
        &self.scope
    }

    /// Subject id of the user behind the session; empty when the caller is
    /// known only by tenant (a peer handle).
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{USER_SUB, captured, frozen_broker, session};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// assert_eq!(captured(&events).remove(0).caller().user_sub(), USER_SUB);
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub fn user_sub(&self) -> &str {
        &self.user_sub
    }
}

/// What the injected grant store is asked about. Kernel capabilities stay in
/// [`Capability`] (schema-locked); cross-tenant inspection is broker-local.
/// # Examples
/// ```
/// use drishti::canvas_protocol::Capability;
/// use drishti::native_host::broker::Permission;
/// assert_eq!(Permission::Capability(Capability::IntentEmit).to_string(), "intent.emit");
/// assert_eq!(Permission::CrossTenantInspect.to_string(), "inspect.cross_tenant");
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Permission {
    /// A kernel capability such as `intent.emit`.
    Capability(Capability),
    /// Read subscriptions and taps beyond the caller's own tenant.
    CrossTenantInspect,
}

impl fmt::Display for Permission {
    /// The capability wire string, or `inspect.cross_tenant`.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Self::Capability(ref capability) => write!(formatter, "{capability}"),
            Self::CrossTenantInspect => formatter.write_str("inspect.cross_tenant"),
        }
    }
}

impl Permission {
    /// The typed refusal for this permission not being granted.
    pub(super) fn refusal(self) -> BrokerError {
        match self {
            Self::Capability(capability) => {
                BrokerError::CapabilityDenied(CapabilityError::NotGranted(capability))
            }
            Self::CrossTenantInspect => BrokerError::CrossTenantDenied,
        }
    }
}

/// How far an inspector read reaches.
/// # Examples
/// ```
/// use drishti::native_host::broker::Reach;
/// assert_ne!(Reach::Own, Reach::AllTenants);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Reach {
    /// The caller's own session and tenant only.
    Own,
    /// Every tenant; needs [`Permission::CrossTenantInspect`].
    AllTenants,
}

/// The kinds of registry entry an audit event names.
/// # Examples
/// ```
/// use drishti::native_host::broker::Subject;
/// assert_ne!(Subject::Widget("a".into()), Subject::Widget("b".into()));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Subject {
    /// A widget registration, by widget id.
    Widget(String),
    /// An ML peer registration.
    MlPeer,
    /// The command-bar registration.
    CommandBar,
    /// A widget subscription.
    Subscription,
    /// A tap listener.
    Tap,
}

/// The emit paths a refusal can come from. The audit event names the path,
/// never the caller's kind or payload, so a hostile emitter cannot grow the
/// trail with its own bytes.
/// # Examples
/// ```
/// use drishti::native_host::broker::Emitter;
/// assert_ne!(Emitter::Widget, Emitter::Prediction);
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Emitter {
    /// `emit`: a registered widget.
    Widget,
    /// `emit_intent`: the command bar.
    Intent,
    /// `emit_prediction`: a verified ML peer.
    Prediction,
}

/// Every API that enumerates broker state for a caller. A new inspector adds a
/// variant here and an entry in [`Inspector::ALL`]; the suite iterates `ALL`.
/// # Examples
/// ```
/// use drishti::native_host::broker::Inspector;
/// assert!(Inspector::ALL.contains(&Inspector::Taps));
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Inspector {
    /// `registered_subscriptions`: the subscription registry.
    Subscriptions,
    /// `tap`: the live signal stream.
    Taps,
}

impl Inspector {
    /// Every inspector, for suites that must cover each one.
    /// # Examples
    /// ```
    /// use drishti::native_host::broker::Inspector;
    /// assert_eq!(Inspector::ALL, [Inspector::Subscriptions, Inspector::Taps]);
    /// ```
    pub const ALL: [Self; 2] = [Self::Subscriptions, Self::Taps];
}

/// What a decision was about.
/// # Examples
/// ```
/// use drishti::native_host::broker::{AuditAction, Subject};
/// assert_ne!(AuditAction::Register(Subject::MlPeer), AuditAction::Revoke(Subject::MlPeer));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AuditAction {
    /// An envelope was stamped, delivered, and routed.
    Route {
        /// Identity of the routed envelope.
        signal_id: String,
        /// Signal kind of the routed envelope.
        kind: String,
        /// Source that emitted it.
        source_id: String,
    },
    /// A registration or subscription was requested.
    Register(Subject),
    /// A registration, subscription, or tap was revoked.
    Revoke(Subject),
    /// An inspector read was requested.
    Inspect(Inspector),
    /// An emit was refused (an admitted emit is audited as `Route`).
    Emit(Emitter),
}

/// The outcome of a decision.
/// # Examples
/// ```
/// use drishti::native_host::broker::{BrokerError, Decision};
/// assert_ne!(Decision::Allowed, Decision::Denied(BrokerError::RateLimited));
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Decision {
    /// The action was admitted.
    Allowed,
    /// The action was refused, with the typed reason.
    Denied(BrokerError),
}

impl Decision {
    /// The decision a broker result records.
    pub(super) fn of<T>(result: &Result<T, BrokerError>) -> Self {
        match result.as_ref().err() {
            None => Self::Allowed,
            Some(error) => Self::Denied(error.clone()),
        }
    }

    /// The permission a denial lacked, when the denial was a missing one.
    /// # Examples
    /// ```
    /// use drishti::native_host::broker::{BrokerError, Decision, Permission};
    /// let cross = Decision::Denied(BrokerError::CrossTenantDenied);
    /// assert_eq!(cross.missing(), Some(Permission::CrossTenantInspect));
    /// // A denial for any other reason, and an admission, miss nothing.
    /// assert_eq!(Decision::Denied(BrokerError::RateLimited).missing(), None);
    /// assert_eq!(Decision::Allowed.missing(), None);
    /// ```
    #[must_use]
    pub fn missing(&self) -> Option<Permission> {
        match *self {
            Self::Denied(BrokerError::CapabilityDenied(CapabilityError::NotGranted(
                ref capability,
            ))) => Some(Permission::Capability(capability.clone())),
            Self::Denied(BrokerError::CrossTenantDenied) => Some(Permission::CrossTenantInspect),
            Self::Denied(_) | Self::Allowed => None,
        }
    }
}

/// One audited decision. `seq` orders events from the broker; the sink may see
/// concurrent callers interleave, never one caller out of order.
/// # Examples
/// ```
/// use drishti::cli::drive::{captured, frozen_broker, session};
/// use drishti::native_host::broker::{AuditAction, Decision, Subject};
/// let (broker, events, _open) = frozen_broker();
/// let who = session(&mut String::new(), "tenant-a")?;
/// assert!(broker.register_command_bar(who).is_err());
/// let event = captured(&events).remove(0);
/// assert_eq!(event.action(), &AuditAction::Register(Subject::CommandBar));
/// assert!(matches!(event.decision(), Decision::Denied(_)));
/// # Ok::<(), drishti::cli::Failure>(())
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct AuditEvent {
    /// Broker-wide ordinal, assigned when the event is published.
    seq: u64,
    /// Injected-clock millisecond time of the decision.
    at_ms: u64,
    /// Who acted.
    caller: Caller,
    /// What was decided.
    action: AuditAction,
    /// The reach the caller requested.
    reach: Reach,
    /// The outcome.
    decision: Decision,
}

impl AuditEvent {
    /// Assemble an event. Only the broker builds these.
    pub(super) const fn new(
        seq: u64,
        at_ms: u64,
        caller: Caller,
        action: AuditAction,
        reach: Reach,
        decision: Decision,
    ) -> Self {
        Self {
            seq,
            at_ms,
            caller,
            action,
            reach,
            decision,
        }
    }

    /// Broker-wide ordinal, assigned when the event is published.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who.clone()).is_err());
    /// assert!(broker.register_command_bar(who).is_err());
    /// let seqs: Vec<u64> = captured(&events).iter().map(|event| event.seq()).collect();
    /// assert_eq!(seqs, [0, 1]);
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn seq(&self) -> u64 {
        self.seq
    }

    /// Injected-clock millisecond time of the decision.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// // The frozen broker's clock reads 1,000 ms.
    /// assert_eq!(captured(&events).remove(0).at_ms(), 1_000);
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn at_ms(&self) -> u64 {
        self.at_ms
    }

    /// Who acted.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-b")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// assert_eq!(captured(&events).remove(0).caller().scope().tenant_id(), "tenant-b");
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn caller(&self) -> &Caller {
        &self.caller
    }

    /// What was decided.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// use drishti::native_host::broker::{AuditAction, Subject};
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// let event = captured(&events).remove(0);
    /// assert_eq!(event.action(), &AuditAction::Register(Subject::CommandBar));
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn action(&self) -> &AuditAction {
        &self.action
    }

    /// The reach the caller requested.
    /// # Examples
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// use drishti::native_host::broker::Reach;
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// assert_eq!(captured(&events).remove(0).reach(), Reach::Own);
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn reach(&self) -> Reach {
        self.reach
    }

    /// The outcome.
    /// # Examples
    /// ```
    /// use drishti::canvas_protocol::Capability;
    /// use drishti::cli::drive::{captured, frozen_broker, session};
    /// use drishti::native_host::broker::Permission;
    /// let (broker, events, _open) = frozen_broker();
    /// let who = session(&mut String::new(), "tenant-a")?;
    /// assert!(broker.register_command_bar(who).is_err());
    /// // The closed grant store refused `intent.emit`.
    /// let missing = captured(&events).remove(0).decision().missing();
    /// assert_eq!(missing, Some(Permission::Capability(Capability::IntentEmit)));
    /// # Ok::<(), drishti::cli::Failure>(())
    /// ```
    #[must_use]
    pub const fn decision(&self) -> &Decision {
        &self.decision
    }
}

/// The one filter every inspector reads through: a caller, the reach it asked
/// for, and the rule that decides which records it may see.
#[derive(Clone)]
pub(super) struct Visibility {
    /// Resolved from the broker's own registration, never from the request.
    caller: Caller,
    /// Reach already checked against the grant store.
    reach: Reach,
}

impl Visibility {
    /// Bind a checked reach to a resolved caller.
    pub(super) const fn new(caller: Caller, reach: Reach) -> Self {
        Self { caller, reach }
    }

    /// The resolved caller.
    pub(super) const fn caller(&self) -> &Caller {
        &self.caller
    }

    /// True when a record in `session_id` and `tenant_id` is visible.
    pub(super) fn admits(&self, session_id: &str, tenant_id: &str) -> bool {
        matches!(self.reach, Reach::AllTenants) || self.caller.scope.holds(session_id, tenant_id)
    }
}