glass-browser 0.3.9

Local browser intelligence runtime with semantic memory, verified workflows, multi-surface understanding, CLI, TUI, MCP, and Rust APIs
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
//! Independent connection-environment and presentation-policy contracts.
//!
//! Width chooses layout only. Transport and graphics claims require separate
//! evidence, and unknown measurements stay unknown.

use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

pub const MAX_CONNECTION_EVIDENCE: usize = 16;
pub const MAX_CONNECTION_EVIDENCE_BYTES: usize = 160;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LayoutClass {
    Phone,
    Compact,
    Wide,
}

impl LayoutClass {
    pub const fn from_columns(columns: u16) -> Self {
        if columns <= 72 {
            Self::Phone
        } else if columns <= 109 {
            Self::Compact
        } else {
            Self::Wide
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TransportClass {
    Local,
    RemoteFast,
    RemoteConstrained,
    Mosh,
    UnknownRemote,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum GraphicsClass {
    Kitty,
    Sixel,
    ITermInline,
    Herdr,
    Ansi,
    SemanticOnly,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ShellKind {
    Local,
    Ssh,
    Mosh,
    UnknownRemote,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum MultiplexerKind {
    None,
    Tmux,
    Screen,
    Herdr,
    Nested,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ActivityClass {
    Interactive,
    Settled,
    Idle,
    Background,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PixelIntent {
    Off,
    Auto,
    On,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum QualityIntent {
    Auto,
    Data,
    Balanced,
    Smooth,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PresentationProfile {
    LocalSmooth,
    LocalBalanced,
    LocalDegraded,
    RemoteInteractive,
    RemoteConstrained,
    MobileRemote,
    MoshSemantic,
    SemanticOnly,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PolicyReason {
    ExplicitPixelsDisabled,
    LocalTransport,
    RemoteTransportMeasuredFast,
    RemoteTransportConstrained,
    RemoteTransportUnknown,
    PhoneRemoteSemanticDefault,
    MoshStateSynchronized,
    GraphicsUnavailable,
    BackgroundPaused,
    IdleThrottled,
    CaptureScaleReduced,
    FrameRateReduced,
    WriterBackpressure,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConnectionMeasurements {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rtt_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub estimated_throughput_mbps: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terminal_write_latency_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cell_pixel_width: Option<u16>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cell_pixel_height: Option<u16>,
}

impl ConnectionMeasurements {
    pub fn validate(self) -> Result<Self, ConnectionError> {
        for (field, value, maximum) in [
            ("rttMs", self.rtt_ms, 120_000.0),
            (
                "estimatedThroughputMbps",
                self.estimated_throughput_mbps,
                1_000_000.0,
            ),
            (
                "terminalWriteLatencyMs",
                self.terminal_write_latency_ms,
                120_000.0,
            ),
        ] {
            if value.is_some_and(|value| !value.is_finite() || value < 0.0 || value > maximum) {
                return Err(ConnectionError::InvalidMeasurement(field));
            }
        }
        Ok(self)
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ConnectionSignals {
    pub ssh: bool,
    pub mosh: bool,
    pub tmux: bool,
    pub screen: bool,
    pub herdr: bool,
}

impl ConnectionSignals {
    pub fn from_process() -> Self {
        Self {
            ssh: std::env::var_os("SSH_CONNECTION").is_some()
                || std::env::var_os("SSH_TTY").is_some(),
            mosh: std::env::var_os("MOSH_CONNECTION").is_some(),
            tmux: std::env::var_os("TMUX").is_some(),
            screen: std::env::var_os("STY").is_some(),
            herdr: std::env::var("HERDR_ENV").is_ok_and(|value| value == "1"),
        }
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ConnectionOverrides {
    pub layout: Option<LayoutClass>,
    pub transport: Option<TransportClass>,
    pub graphics: Option<GraphicsClass>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConnectionEnvironment {
    pub layout: LayoutClass,
    pub transport: TransportClass,
    pub graphics: GraphicsClass,
    pub shell: ShellKind,
    pub multiplexer: MultiplexerKind,
    pub terminal_columns: u16,
    pub terminal_rows: u16,
    pub measurements: ConnectionMeasurements,
    pub evidence: Vec<String>,
}

impl ConnectionEnvironment {
    pub fn detect(
        terminal_columns: u16,
        terminal_rows: u16,
        signals: &ConnectionSignals,
        probed_graphics: Option<GraphicsClass>,
        measurements: ConnectionMeasurements,
        overrides: ConnectionOverrides,
    ) -> Result<Self, ConnectionError> {
        if terminal_columns == 0 || terminal_rows == 0 {
            return Err(ConnectionError::InvalidGeometry);
        }
        let measurements = measurements.validate()?;
        let shell = if signals.mosh {
            ShellKind::Mosh
        } else if signals.ssh {
            ShellKind::Ssh
        } else {
            ShellKind::Local
        };
        let transport = overrides.transport.unwrap_or(match shell {
            ShellKind::Local => TransportClass::Local,
            ShellKind::Mosh => TransportClass::Mosh,
            ShellKind::Ssh | ShellKind::UnknownRemote => classify_remote_measurements(measurements),
        });
        let multiplexers = [signals.tmux, signals.screen, signals.herdr]
            .into_iter()
            .filter(|value| *value)
            .count();
        let multiplexer = if multiplexers > 1 {
            MultiplexerKind::Nested
        } else if signals.herdr {
            MultiplexerKind::Herdr
        } else if signals.tmux {
            MultiplexerKind::Tmux
        } else if signals.screen {
            MultiplexerKind::Screen
        } else {
            MultiplexerKind::None
        };
        let graphics = overrides.graphics.unwrap_or_else(|| {
            probed_graphics.unwrap_or(if shell == ShellKind::Local {
                GraphicsClass::Unknown
            } else {
                GraphicsClass::SemanticOnly
            })
        });
        let mut evidence = Vec::new();
        push_evidence(
            &mut evidence,
            match shell {
                ShellKind::Local => "no SSH/Mosh metadata; local shell",
                ShellKind::Ssh => "SSH metadata present; link quality unknown",
                ShellKind::Mosh => "Mosh metadata present; terminal pixels not assumed",
                ShellKind::UnknownRemote => "remote shell metadata incomplete",
            },
        );
        if overrides.layout.is_some() {
            push_evidence(&mut evidence, "layout selected explicitly");
        }
        if overrides.transport.is_some() {
            push_evidence(&mut evidence, "transport selected explicitly");
        }
        if overrides.graphics.is_some() {
            push_evidence(&mut evidence, "graphics selected explicitly");
        } else if probed_graphics.is_some() {
            push_evidence(&mut evidence, "graphics selected by active probe");
        } else {
            push_evidence(&mut evidence, "graphics capability unproven");
        }
        Ok(Self {
            layout: overrides
                .layout
                .unwrap_or_else(|| LayoutClass::from_columns(terminal_columns)),
            transport,
            graphics,
            shell,
            multiplexer,
            terminal_columns,
            terminal_rows,
            measurements,
            evidence,
        })
    }

    pub const fn remote(&self) -> bool {
        !matches!(self.transport, TransportClass::Local)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PresentationPolicy {
    pub profile: PresentationProfile,
    /// Zero means terminal pixel acquisition/presentation is paused.
    pub requested_fps: u16,
    pub active_floor_fps: u16,
    pub capture_scale: f32,
    pub semantic_primary: bool,
    pub continuous_pixels: bool,
    pub drop_obsolete_frames: bool,
    pub reasons: Vec<PolicyReason>,
}

impl PresentationPolicy {
    pub fn select(
        environment: &ConnectionEnvironment,
        activity: ActivityClass,
        pixel_intent: PixelIntent,
        quality: QualityIntent,
    ) -> Self {
        let mut reasons = BTreeSet::new();
        if pixel_intent == PixelIntent::Off {
            reasons.insert(PolicyReason::ExplicitPixelsDisabled);
            return semantic_policy(PresentationProfile::SemanticOnly, reasons);
        }
        if environment.transport == TransportClass::Mosh {
            reasons.insert(PolicyReason::MoshStateSynchronized);
            return semantic_policy(PresentationProfile::MoshSemantic, reasons);
        }
        if matches!(
            environment.graphics,
            GraphicsClass::SemanticOnly | GraphicsClass::Unknown
        ) && pixel_intent == PixelIntent::Auto
        {
            reasons.insert(PolicyReason::GraphicsUnavailable);
            return semantic_policy(PresentationProfile::SemanticOnly, reasons);
        }
        if environment.layout == LayoutClass::Phone
            && environment.remote()
            && pixel_intent == PixelIntent::Auto
        {
            reasons.insert(PolicyReason::PhoneRemoteSemanticDefault);
            return semantic_policy(PresentationProfile::MobileRemote, reasons);
        }

        let (profile, mut requested_fps, floor, scale, semantic_primary) = match environment
            .transport
        {
            TransportClass::Local => {
                reasons.insert(PolicyReason::LocalTransport);
                match quality {
                    QualityIntent::Data => (PresentationProfile::LocalDegraded, 20, 20, 0.5, false),
                    QualityIntent::Balanced | QualityIntent::Auto => {
                        (PresentationProfile::LocalBalanced, 30, 30, 1.0, false)
                    }
                    QualityIntent::Smooth => (PresentationProfile::LocalSmooth, 60, 30, 1.0, false),
                }
            }
            TransportClass::RemoteFast => {
                reasons.insert(PolicyReason::RemoteTransportMeasuredFast);
                let fps = match quality {
                    QualityIntent::Data => 20,
                    QualityIntent::Balanced | QualityIntent::Auto => 24,
                    QualityIntent::Smooth => 30,
                };
                (PresentationProfile::RemoteInteractive, fps, 20, 0.85, false)
            }
            TransportClass::RemoteConstrained | TransportClass::UnknownRemote => {
                reasons.insert(
                    if environment.transport == TransportClass::RemoteConstrained {
                        PolicyReason::RemoteTransportConstrained
                    } else {
                        PolicyReason::RemoteTransportUnknown
                    },
                );
                let fps = match quality {
                    QualityIntent::Data => 3,
                    QualityIntent::Balanced | QualityIntent::Auto => 6,
                    QualityIntent::Smooth => 12,
                };
                (
                    PresentationProfile::RemoteConstrained,
                    fps,
                    3,
                    if quality == QualityIntent::Smooth {
                        0.65
                    } else {
                        0.5
                    },
                    true,
                )
            }
            TransportClass::Mosh => unreachable!("Mosh handled above"),
        };
        let continuous_pixels = match activity {
            ActivityClass::Interactive => true,
            ActivityClass::Settled => {
                requested_fps = requested_fps.min(5);
                reasons.insert(PolicyReason::IdleThrottled);
                false
            }
            ActivityClass::Idle => {
                requested_fps = requested_fps.min(3);
                reasons.insert(PolicyReason::IdleThrottled);
                false
            }
            ActivityClass::Background => {
                requested_fps = 0;
                reasons.insert(PolicyReason::BackgroundPaused);
                false
            }
        };
        Self {
            profile,
            requested_fps,
            active_floor_fps: floor,
            capture_scale: scale,
            semantic_primary,
            continuous_pixels,
            drop_obsolete_frames: true,
            reasons: reasons.into_iter().collect(),
        }
    }
}

fn semantic_policy(
    profile: PresentationProfile,
    reasons: BTreeSet<PolicyReason>,
) -> PresentationPolicy {
    PresentationPolicy {
        profile,
        requested_fps: 0,
        active_floor_fps: 0,
        capture_scale: 0.5,
        semantic_primary: true,
        continuous_pixels: false,
        drop_obsolete_frames: true,
        reasons: reasons.into_iter().collect(),
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PresentationObservatory {
    pub requested_fps: f64,
    pub acquisition_fps: f64,
    pub presentation_fps: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frame_age_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capture_latency_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encode_latency_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub decode_latency_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub terminal_write_latency_ms: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input_to_present_ms: Option<f64>,
    pub capture_scale: f32,
    pub encoded_bytes_per_second: f64,
    pub producer_dropped: u64,
    pub mailbox_dropped: u64,
    pub writer_dropped: u64,
    pub stale_rejected: u64,
    pub browser_revision: u64,
    pub geometry_revision: u64,
    pub frame_generation: u64,
}

impl PresentationObservatory {
    pub fn validate(self) -> Result<Self, ConnectionError> {
        for (field, value) in [
            ("requestedFps", Some(self.requested_fps)),
            ("acquisitionFps", Some(self.acquisition_fps)),
            ("presentationFps", Some(self.presentation_fps)),
            ("frameAgeMs", self.frame_age_ms),
            ("captureLatencyMs", self.capture_latency_ms),
            ("encodeLatencyMs", self.encode_latency_ms),
            ("decodeLatencyMs", self.decode_latency_ms),
            ("terminalWriteLatencyMs", self.terminal_write_latency_ms),
            ("inputToPresentMs", self.input_to_present_ms),
            ("encodedBytesPerSecond", Some(self.encoded_bytes_per_second)),
        ] {
            if value.is_some_and(|value| !value.is_finite() || value < 0.0) {
                return Err(ConnectionError::InvalidMeasurement(field));
            }
        }
        if !(0.5..=1.0).contains(&self.capture_scale) {
            return Err(ConnectionError::InvalidMeasurement("captureScale"));
        }
        Ok(self)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionError {
    InvalidGeometry,
    InvalidMeasurement(&'static str),
}

impl std::fmt::Display for ConnectionError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidGeometry => formatter.write_str("terminal geometry must be non-zero"),
            Self::InvalidMeasurement(field) => write!(formatter, "invalid {field} measurement"),
        }
    }
}

impl std::error::Error for ConnectionError {}

fn push_evidence(evidence: &mut Vec<String>, value: &str) {
    if evidence.len() < MAX_CONNECTION_EVIDENCE {
        evidence.push(value.chars().take(MAX_CONNECTION_EVIDENCE_BYTES).collect());
    }
}

fn classify_remote_measurements(measurements: ConnectionMeasurements) -> TransportClass {
    if measurements.rtt_ms.is_some_and(|value| value >= 150.0)
        || measurements
            .estimated_throughput_mbps
            .is_some_and(|value| value <= 5.0)
    {
        TransportClass::RemoteConstrained
    } else if measurements.rtt_ms.is_some_and(|value| value <= 80.0)
        && measurements
            .estimated_throughput_mbps
            .is_some_and(|value| value >= 10.0)
    {
        TransportClass::RemoteFast
    } else {
        TransportClass::UnknownRemote
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn environment(
        columns: u16,
        signals: ConnectionSignals,
        graphics: Option<GraphicsClass>,
    ) -> ConnectionEnvironment {
        ConnectionEnvironment::detect(
            columns,
            32,
            &signals,
            graphics,
            ConnectionMeasurements::default(),
            ConnectionOverrides::default(),
        )
        .unwrap()
    }

    #[test]
    fn terminal_width_changes_layout_but_never_transport() {
        let signals = ConnectionSignals {
            ssh: true,
            ..ConnectionSignals::default()
        };
        let phone = environment(60, signals.clone(), None);
        let wide = environment(160, signals, None);
        assert_eq!(phone.layout, LayoutClass::Phone);
        assert_eq!(wide.layout, LayoutClass::Wide);
        assert_eq!(phone.transport, TransportClass::UnknownRemote);
        assert_eq!(wide.transport, TransportClass::UnknownRemote);
    }

    #[test]
    fn remote_matrix_is_conservative_and_mosh_is_semantic() {
        let unknown = environment(
            140,
            ConnectionSignals {
                ssh: true,
                tmux: true,
                ..ConnectionSignals::default()
            },
            None,
        );
        assert_eq!(unknown.graphics, GraphicsClass::SemanticOnly);
        assert_eq!(unknown.multiplexer, MultiplexerKind::Tmux);
        let policy = PresentationPolicy::select(
            &unknown,
            ActivityClass::Interactive,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        assert_eq!(policy.profile, PresentationProfile::RemoteConstrained);
        assert_eq!(policy.requested_fps, 12);
        let mosh = environment(
            78,
            ConnectionSignals {
                mosh: true,
                ..ConnectionSignals::default()
            },
            Some(GraphicsClass::Kitty),
        );
        let policy = PresentationPolicy::select(
            &mosh,
            ActivityClass::Interactive,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        assert_eq!(policy.profile, PresentationProfile::MoshSemantic);
        assert_eq!(policy.requested_fps, 0);
    }

    #[test]
    fn local_profiles_restore_sixty_and_thirty_fps() {
        let local = environment(
            140,
            ConnectionSignals::default(),
            Some(GraphicsClass::Kitty),
        );
        let smooth = PresentationPolicy::select(
            &local,
            ActivityClass::Interactive,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        let balanced = PresentationPolicy::select(
            &local,
            ActivityClass::Interactive,
            PixelIntent::On,
            QualityIntent::Balanced,
        );
        assert_eq!((smooth.requested_fps, smooth.active_floor_fps), (60, 30));
        assert_eq!(balanced.requested_fps, 30);
    }

    #[test]
    fn phone_remote_is_semantic_by_default_but_pixels_can_be_explicit() {
        let phone = environment(
            60,
            ConnectionSignals {
                ssh: true,
                ..ConnectionSignals::default()
            },
            Some(GraphicsClass::Kitty),
        );
        let automatic = PresentationPolicy::select(
            &phone,
            ActivityClass::Interactive,
            PixelIntent::Auto,
            QualityIntent::Smooth,
        );
        let explicit = PresentationPolicy::select(
            &phone,
            ActivityClass::Interactive,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        assert_eq!(
            (automatic.profile, automatic.requested_fps),
            (PresentationProfile::MobileRemote, 0)
        );
        assert_eq!(
            (explicit.profile, explicit.requested_fps),
            (PresentationProfile::RemoteConstrained, 12)
        );
    }

    #[test]
    fn activity_throttles_without_erasing_active_profile() {
        let local = environment(
            140,
            ConnectionSignals::default(),
            Some(GraphicsClass::Kitty),
        );
        let idle = PresentationPolicy::select(
            &local,
            ActivityClass::Idle,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        let background = PresentationPolicy::select(
            &local,
            ActivityClass::Background,
            PixelIntent::On,
            QualityIntent::Smooth,
        );
        assert_eq!(
            (idle.profile, idle.requested_fps),
            (PresentationProfile::LocalSmooth, 3)
        );
        assert_eq!(background.requested_fps, 0);
        assert!(background.reasons.contains(&PolicyReason::BackgroundPaused));
    }

    #[test]
    fn explicit_overrides_are_independent_and_explained() {
        let environment = ConnectionEnvironment::detect(
            160,
            40,
            &ConnectionSignals {
                ssh: true,
                ..ConnectionSignals::default()
            },
            None,
            ConnectionMeasurements::default(),
            ConnectionOverrides {
                layout: Some(LayoutClass::Phone),
                transport: Some(TransportClass::RemoteFast),
                graphics: Some(GraphicsClass::Sixel),
            },
        )
        .unwrap();
        assert_eq!(
            (
                environment.layout,
                environment.transport,
                environment.graphics
            ),
            (
                LayoutClass::Phone,
                TransportClass::RemoteFast,
                GraphicsClass::Sixel
            )
        );
        assert_eq!(environment.terminal_columns, 160);
        assert!(
            environment
                .evidence
                .iter()
                .any(|value| value.contains("transport selected explicitly"))
        );
    }

    #[test]
    fn issue_33_phone_design_assets_decode_with_real_dimensions() {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../..");
        for (name, minimum_width, minimum_height) in [
            ("remote-ios-concept.jpg", 390, 800),
            ("remote-android-concept.jpg", 200, 400),
        ] {
            let path = root.join("docs/design/v0.3.3").join(name);
            let size = imagesize::size(&path).unwrap_or_else(|error| {
                panic!(
                    "{} must be a decodable release design asset: {error}",
                    path.display()
                )
            });
            assert!(size.width >= minimum_width && size.height >= minimum_height);
        }
    }
}