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
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
//! Shared command-line convention for the preview binaries.
//!
//! Both `canvas_preview` and `trace_check` run through [`run`]: locked
//! standard streams become one [`Streams`], the binary's `execute` returns a
//! typed [`Failure`] or an exit code, and the exit mapping reports the
//! failure on stderr. Every write error propagates as the failure — including
//! the final report, whose failure selects a distinct exit code because no
//! channel remains for its text.

use std::io::Write;
use std::path::Path;
use std::process::ExitCode;

/// Application failure for the preview binaries.
///
/// Every variant names the failure and keeps its source, so the exit-code
/// report and any caller can see why: I/O, UTF-8, and size failures carry
/// their source errors, while invocation faults carry their message.
///
/// # Examples
///
/// ```
/// use drishti::cli::Failure;
///
/// let usage = Failure::from("usage: tool FILE");
/// assert!(matches!(usage, Failure::Input(_)));
/// assert_eq!(usage.to_string(), "usage: tool FILE");
///
/// let io = Failure::from(std::io::Error::other("disk gone"));
/// assert!(io.to_string().starts_with("I/O failure: "));
/// ```
#[derive(Debug, lgwks_macros::Error)]
#[non_exhaustive]
pub enum Failure {
    /// Invalid invocation or input; the message names the problem and no
    /// deeper source exists.
    #[error("{0}")]
    Input(String),
    /// An I/O operation failed.
    #[error("I/O failure: {0}")]
    Io(#[from] std::io::Error),
    /// Input bytes were not valid UTF-8.
    #[error("invalid UTF-8: {0}")]
    Utf8(#[from] std::string::FromUtf8Error),
    /// A byte count overflowed `u64`.
    #[error("input size overflow")]
    SizeOverflow(#[from] std::num::TryFromIntError),
    /// A trace line was not valid JSON.
    #[error("invalid JSON: {0}")]
    Json(#[from] lgwks_std::json::Error),
    /// A trace failed the analytic check.
    #[error("trace rejected: {0}")]
    Check(#[from] crate::preview::TraceCheckError),
    /// A scene reducer step failed.
    #[error("scene failure: {0}")]
    Scene(#[from] crate::canvas_scene::model::Error),
    /// A session binding was malformed.
    #[error("invalid session: {0}")]
    Session(#[from] crate::canvas_protocol::SessionInvariantError),
    /// The windowing event loop failed (`desktop` feature).
    #[cfg(feature = "desktop")]
    #[error("event loop failure: {0}")]
    EventLoop(#[from] lgwks_deps::winit::error::EventLoopError),
    /// The platform refused to create the window (`desktop` feature).
    #[cfg(feature = "desktop")]
    #[error("window creation failure: {0}")]
    Window(#[from] lgwks_deps::winit::error::OsError),
    /// The CPU presentation surface failed (`desktop` feature).
    #[cfg(feature = "desktop")]
    #[error("surface failure: {0}")]
    Surface(#[from] lgwks_deps::softbuffer::SoftBufferError),
}

impl From<&str> for Failure {
    /// Usage and input faults from a static message.
    fn from(message: &str) -> Self {
        Self::Input(String::from(message))
    }
}

impl From<String> for Failure {
    /// Usage and input faults from a computed message.
    fn from(message: String) -> Self {
        Self::Input(message)
    }
}

/// The two output streams of a run: data on standard output, diagnostics on
/// standard error. Only [`run`] builds one; a binary's `execute` receives it
/// by mutable reference.
///
/// # Examples
///
/// ```
/// use std::process::ExitCode;
///
/// use drishti::cli::run;
///
/// let mut reported = false;
/// let code = run("doc-streams", |streams| {
///     streams.tell("data line")?;
///     streams.warn("diagnostic line")?;
///     reported = true;
///     Ok(0)
/// });
/// assert!(reported);
/// assert_eq!(format!("{code:?}"), format!("{:?}", ExitCode::SUCCESS));
/// ```
pub struct Streams<'sink> {
    /// Standard output.
    out: &'sink mut dyn Write,
    /// Standard error.
    err: &'sink mut dyn Write,
}

impl Streams<'_> {
    /// Build streams over locked standard output and error. Only [`run`]
    /// calls this; binaries receive the streams.
    fn new<'sink>(out: &'sink mut dyn Write, err: &'sink mut dyn Write) -> Streams<'sink> {
        Streams { out, err }
    }

    /// A line on standard output. Stdout carries data (JSONL traces), so a
    /// failed line is a failed run, not a warning.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::run;
    ///
    /// let mut told = false;
    /// run("doc-tell", |streams| {
    ///     streams.tell("{\"kind\":\"summary\"}")?;
    ///     told = true;
    ///     Ok(0)
    /// });
    /// assert!(told);
    /// ```
    pub fn tell(&mut self, text: &str) -> Result<(), Failure> {
        writeln!(self.out, "{text}")
            .map_err(|error| Failure::from(format!("the report was not written: {error}")))
    }

    /// A line on standard error. A failed write is a failure too: the run
    /// reports it through the distinct exit code rather than dropping it.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::run;
    ///
    /// let mut warned = false;
    /// run("doc-warn", |streams| {
    ///     streams.warn("backend not compiled")?;
    ///     warned = true;
    ///     Ok(1)
    /// });
    /// assert!(warned);
    /// ```
    pub fn warn(&mut self, text: &str) -> Result<(), Failure> {
        writeln!(self.err, "{text}")
            .map_err(|error| Failure::from(format!("the report was not written: {error}")))
    }

    /// Reborrow standard output for data sinks (trace JSONL).
    ///
    /// # Examples
    ///
    /// ```
    /// use std::io::Write as _;
    ///
    /// use drishti::cli::run;
    ///
    /// let mut wrote = false;
    /// run("doc-out", |streams| {
    ///     streams.out().write_all(b"{\"kind\":\"frame\"}\n")?;
    ///     wrote = true;
    ///     Ok(0)
    /// });
    /// assert!(wrote);
    /// ```
    pub fn out(&mut self) -> &mut dyn Write {
        &mut *self.out
    }
}

/// Read a file bounded by `cap` bytes: refuse oversized inputs before
/// reading, then cap the read itself so a file grown between the check and
/// the read cannot exhaust memory.
///
/// # Errors
///
/// Returns `over_message` when the file exceeds `cap`, or the I/O failure.
///
/// # Examples
///
/// ```
/// use drishti::cli::{Failure, read_bounded};
///
/// let path = std::env::temp_dir().join(format!(
///     "drishti-doc-read-bounded-{}.bin",
///     std::process::id()
/// ));
/// std::fs::write(&path, b"12345").map_err(|error| error.to_string())?;
/// let at_cap = read_bounded(&path, 5, "too big");
/// let over_cap = read_bounded(&path, 4, "too big");
/// std::fs::remove_file(&path).map_err(|error| error.to_string())?;
///
/// assert_eq!(at_cap.map_err(|error| error.to_string()), Ok(b"12345".to_vec()));
/// assert!(matches!(over_cap, Err(Failure::Input(ref message)) if message == "too big"));
/// # Ok::<(), String>(())
/// ```
pub fn read_bounded(path: &Path, cap: u64, over_message: &'static str) -> Result<Vec<u8>, Failure> {
    let size = std::fs::metadata(path)?.len();
    (size <= cap)
        .then_some(())
        .ok_or_else(|| Failure::from(over_message))?;
    let file = std::fs::File::open(path)?;
    let mut bytes = Vec::new();
    use std::io::Read as _;
    file.take(cap.saturating_add(1)).read_to_end(&mut bytes)?;
    (u64::try_from(bytes.len())? <= cap)
        .then_some(())
        .ok_or_else(|| Failure::from(over_message))?;
    Ok(bytes)
}

/// Run a binary: lock the standard streams, execute, and map the outcome to
/// an exit code. Exit 0 is success, 1 is a reported failure, and 2 records a
/// failure whose report could not print because stderr itself is broken.
///
/// # Examples
///
/// ```
/// use std::process::ExitCode;
///
/// use drishti::cli::{Failure, run};
///
/// let ok = run("doc-run", |_| Ok(0));
/// let failed = run("doc-run", |_| Err(Failure::from("refused")));
/// assert_eq!(format!("{ok:?}"), format!("{:?}", ExitCode::SUCCESS));
/// assert_eq!(format!("{failed:?}"), format!("{:?}", ExitCode::FAILURE));
/// ```
pub fn run<F>(program: &'static str, execute: F) -> ExitCode
where
    F: FnOnce(&mut Streams<'_>) -> Result<u8, Failure>,
{
    let stdout = std::io::stdout();
    let stderr = std::io::stderr();
    let (mut out, mut err) = (stdout.lock(), stderr.lock());
    let mut streams = Streams::new(&mut out, &mut err);
    exit(execute(&mut streams), &mut streams, program)
}

/// Map a run outcome to its exit code, reporting failures on stderr.
fn exit(result: Result<u8, Failure>, streams: &mut Streams<'_>, program: &str) -> ExitCode {
    match result {
        Ok(code) => ExitCode::from(code),
        Err(failure) => match streams.warn(&format!("{program}: {failure}")) {
            Ok(()) => ExitCode::from(1),
            // No channel remains for the message text, so the distinct exit
            // code records the unreported failure instead of dropping it.
            Err(_) => ExitCode::from(2),
        },
    }
}

/// Shared conformance-driver scaffolding for the absorbing drivers.
///
/// The `api-drive-*` binaries drive the full absorbed surface for the surface
/// gate, so their digest helpers and frozen fixtures live here once instead
/// of copied per binary. Testing aid only, never product behavior.
pub mod drive {
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};

    use super::Failure;
    use crate::canvas_protocol::Capability;
    use crate::canvas_protocol::SessionInvariant;
    use crate::native_host::broker::{
        AuditEvent, AuditFn, Broker, ClockFn, GrantFn, GrantVerificationResult, IdFn,
        RegisteredHandle, VerifyGrantFn,
    };
    use crate::native_host::protocol::{WidgetDescriptor, WidgetSurface};

    /// The audit events a frozen broker's sink captured.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{Events, captured, frozen_broker};
    ///
    /// let (_, events, _) = frozen_broker();
    /// let shared: Events = events;
    /// assert!(captured(&shared).is_empty());
    /// ```
    pub type Events = Arc<Mutex<Vec<AuditEvent>>>;

    /// Frozen broker for the drivers: fixed clock, counting identities,
    /// always-valid ML grants, a grant store the drive opens with the returned
    /// flag (closed at first), and a capturing audit sink.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::atomic::Ordering;
    ///
    /// use drishti::cli::drive::{captured, frozen_broker};
    ///
    /// let (_, events, open) = frozen_broker();
    /// assert!(!open.load(Ordering::SeqCst));
    /// assert!(captured(&events).is_empty());
    /// ```
    #[must_use]
    pub fn frozen_broker() -> (Broker, Events, Arc<AtomicBool>) {
        let ids = Arc::new(Mutex::new(0_u128));
        let clock: ClockFn = Arc::new(|| 1_000);
        let new_id: IdFn = Arc::new(move || {
            let mut guard = match ids.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            };
            *guard = guard.wrapping_add(1);
            lgwks_std::id::Uuid::from(guard.to_le_bytes())
        });
        let verify: VerifyGrantFn = Arc::new(|_| GrantVerificationResult::Valid);
        let open = Arc::new(AtomicBool::new(false));
        let gate = Arc::clone(&open);
        let grants: GrantFn = Arc::new(move |_, _| gate.load(Ordering::SeqCst));
        let events: Events = Arc::new(Mutex::new(Vec::new()));
        let sink = Arc::clone(&events);
        let audit: AuditFn = Arc::new(move |event: &AuditEvent| match sink.lock() {
            Ok(mut guard) => guard.push(event.clone()),
            Err(poisoned) => poisoned.into_inner().push(event.clone()),
        });
        (
            Broker::new(clock, new_id, verify, grants, audit),
            events,
            open,
        )
    }

    /// Fixed work-object identity for every fixture; a canonical UUID so
    /// digests built from it are identical across runs.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::WORK_OBJECT;
    ///
    /// assert!(lgwks_std::id::Uuid::parse(WORK_OBJECT).is_ok());
    /// ```
    pub const WORK_OBJECT: &str = "11111111-1111-1111-1111-111111111111";
    /// Fixed session identity for every fixture; differs from every other
    /// fixed identity so a mixed-up binding cannot pass.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{SESSION_ID, WORK_OBJECT};
    ///
    /// assert!(lgwks_std::id::Uuid::parse(SESSION_ID).is_ok());
    /// assert_ne!(SESSION_ID, WORK_OBJECT);
    /// ```
    pub const SESSION_ID: &str = "22222222-2222-2222-2222-222222222222";
    /// Fixed policy bundle for every fixture; a canonical UUID distinct from
    /// the session identity.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{POLICY_BUNDLE, SESSION_ID};
    ///
    /// assert!(lgwks_std::id::Uuid::parse(POLICY_BUNDLE).is_ok());
    /// assert_ne!(POLICY_BUNDLE, SESSION_ID);
    /// ```
    pub const POLICY_BUNDLE: &str = "33333333-3333-3333-3333-333333333333";
    /// Fixed user subject for every fixture; a canonical UUID distinct from
    /// the policy bundle.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{POLICY_BUNDLE, USER_SUB};
    ///
    /// assert!(lgwks_std::id::Uuid::parse(USER_SUB).is_ok());
    /// assert_ne!(USER_SUB, POLICY_BUNDLE);
    /// ```
    pub const USER_SUB: &str = "44444444-4444-4444-4444-444444444444";

    /// Register a minimal widget on `broker` and return its handle, for
    /// drivers that need an owner to tap as. The widget holds the emit and
    /// subscribe capabilities and lives in `session`.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{SESSION_ID, frozen_broker, session, widget_owner};
    ///
    /// let (broker, _, _) = frozen_broker();
    /// let mut digest = String::new();
    /// let bound = session(&mut digest, "tenant-a").map_err(|error| error.to_string())?;
    /// let handle = widget_owner(&mut digest, &broker, &bound).map_err(|error| error.to_string())?;
    /// assert!(handle.session_id() == SESSION_ID);
    /// assert!(digest.contains("widget-owner="));
    /// # Ok::<(), String>(())
    /// ```
    pub fn widget_owner(
        digest: &mut String,
        broker: &Broker,
        session: &SessionInvariant,
    ) -> Result<RegisteredHandle, Failure> {
        let descriptor = WidgetDescriptor::new(
            String::from("demo.kind"),
            String::from("widget.owner"),
            String::from("1.0.0"),
            WidgetSurface::Native,
            Vec::new(),
            Vec::new(),
            vec![Capability::SignalEmit, Capability::SignalSubscribe],
        );
        must(
            digest,
            "widget-owner",
            broker.register(descriptor, session.clone()),
        )
    }

    /// A copy of the audit events captured so far, in sink order.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{captured, frozen_broker, session, widget_owner};
    ///
    /// let (broker, events, _) = frozen_broker();
    /// assert!(captured(&events).is_empty());
    /// let mut digest = String::new();
    /// let bound = session(&mut digest, "tenant-a").map_err(|error| error.to_string())?;
    /// widget_owner(&mut digest, &broker, &bound).map_err(|error| error.to_string())?;
    /// let first = captured(&events);
    /// assert_eq!(format!("{first:?}"), format!("{:?}", captured(&events)));
    /// # Ok::<(), String>(())
    /// ```
    #[must_use]
    pub fn captured(events: &Events) -> Vec<AuditEvent> {
        match events.lock() {
            Ok(guard) => guard.clone(),
            Err(poisoned) => poisoned.into_inner().clone(),
        }
    }

    /// Record a value in the digest as one `what=value` line (Debug form).
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::note;
    ///
    /// let mut digest = String::new();
    /// note(&mut digest, "count", &3_u8).map_err(|error| error.to_string())?;
    /// note(&mut digest, "name", "x").map_err(|error| error.to_string())?;
    /// assert_eq!(digest, "count=3\nname=\"x\"\n");
    /// # Ok::<(), String>(())
    /// ```
    pub fn note<T: std::fmt::Debug + ?Sized>(
        digest: &mut String,
        what: &str,
        value: &T,
    ) -> Result<(), Failure> {
        std::fmt::Write::write_fmt(digest, format_args!("{what}={value:?}\n"))
            .map_err(|error| Failure::from(format!("digest unwritable: {error}")))?;
        Ok(())
    }

    /// Build a value through a fallible constructor, recording it, or fail
    /// the drive with context.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::must;
    ///
    /// let mut digest = String::new();
    /// let seven = must(&mut digest, "value", Ok::<u8, String>(7)).map_err(|error| error.to_string())?;
    /// assert_eq!(seven, 7);
    /// assert_eq!(digest, "value=7\n");
    ///
    /// let refused = must(&mut digest, "step", Err::<u8, String>(String::from("boom")));
    /// assert_eq!(
    ///     refused.map_err(|error| error.to_string()),
    ///     Err(String::from("step refused: boom"))
    /// );
    /// assert_eq!(digest, "value=7\n");
    /// # Ok::<(), String>(())
    /// ```
    pub fn must<T: std::fmt::Debug, E: std::fmt::Display>(
        digest: &mut String,
        what: &str,
        result: Result<T, E>,
    ) -> Result<T, Failure> {
        match result {
            Ok(value) => {
                note(digest, what, &value)?;
                Ok(value)
            }
            Err(error) => Err(Failure::from(format!("{what} refused: {error}"))),
        }
    }

    /// Fail the drive with `message` unless `condition` holds.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::ensure;
    ///
    /// assert!(ensure(true, "held").is_ok());
    /// let failed = ensure(false, "a negative control passed");
    /// assert_eq!(
    ///     failed.map_err(|error| error.to_string()),
    ///     Err(String::from("a negative control passed"))
    /// );
    /// ```
    pub fn ensure(condition: bool, message: &'static str) -> Result<(), Failure> {
        crate::native_host::ensure(condition, || Failure::from(message))
    }

    /// Take the refusal out of a negative control: the error of `outcome`, or
    /// a failed drive with `message` when the control was admitted.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::refused;
    ///
    /// let error = refused(Err::<u8, &str>("no"), "control admitted");
    /// assert_eq!(error.map_err(|failure| failure.to_string()), Ok("no"));
    /// let admitted = refused(Ok::<u8, &str>(1), "control admitted");
    /// assert_eq!(
    ///     admitted.map_err(|failure| failure.to_string()),
    ///     Err(String::from("control admitted"))
    /// );
    /// ```
    pub fn refused<T, E>(outcome: Result<T, E>, message: &'static str) -> Result<E, Failure> {
        outcome.err().ok_or_else(|| Failure::from(message))
    }

    /// Parse a fixed fixture UUID, recording it in the digest; a malformed
    /// text fails the drive and leaves the digest untouched.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::{WORK_OBJECT, uuid};
    ///
    /// let mut digest = String::new();
    /// assert!(uuid(&mut digest, "id", WORK_OBJECT).is_ok());
    /// assert!(digest.starts_with("id="));
    ///
    /// let before = digest.clone();
    /// let refused = uuid(&mut digest, "bad-id", "nope").map_err(|error| error.to_string());
    /// assert!(matches!(refused, Err(ref text) if text.starts_with("bad-id refused: ")));
    /// assert_eq!(digest, before);
    /// ```
    pub fn uuid(
        digest: &mut String,
        what: &str,
        text: &str,
    ) -> Result<lgwks_std::id::Uuid, Failure> {
        must(digest, what, lgwks_std::id::Uuid::parse(text))
    }

    /// A nonce the broker never issued, derived from a live one by inverting
    /// every byte, so a negative control forges its handle from the run and no
    /// literal stands in the shipped drive. The result differs from `live` in
    /// every byte.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::forged_nonce;
    ///
    /// let live = lgwks_std::id::Uuid::from([1_u8; 16]);
    /// let forged = forged_nonce(&live);
    /// assert_eq!(forged, lgwks_std::id::Uuid::from([0xFE_u8; 16]));
    /// assert_eq!(forged_nonce(&forged), live);
    /// ```
    #[must_use]
    pub fn forged_nonce(live: &lgwks_std::id::Uuid) -> lgwks_std::id::Uuid {
        lgwks_std::id::Uuid::from((*live.as_bytes()).map(|byte| !byte))
    }

    /// Bound session fixture for one tenant: the fixed work object, session,
    /// policy bundle and user subject, with `tenant` as the only variable.
    ///
    /// # Examples
    ///
    /// ```
    /// use drishti::cli::drive::session;
    ///
    /// let mut digest = String::new();
    /// let bound = session(&mut digest, "tenant-a").map_err(|error| error.to_string())?;
    /// assert_eq!(bound.tenant_id(), "tenant-a");
    /// # Ok::<(), String>(())
    /// ```
    pub fn session(digest: &mut String, tenant: &str) -> Result<SessionInvariant, Failure> {
        must(
            digest,
            "session",
            SessionInvariant::new(
                String::from(WORK_OBJECT),
                String::from(SESSION_ID),
                String::from(POLICY_BUNDLE),
                String::from(tenant),
                String::from(USER_SUB),
            ),
        )
    }
}