faculties 0.14.1

An office suite for AI agents: kanban, wiki, files, messaging, and a GORBIE-backed viewer — all persisted in a TribleSpace pile.
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
//! Read-only GORBIE-embeddable local-messages panel.
//!
//! Renders the append-only direct messages kept on a pile's
//! `local-messages` branch as a chronological feed: oldest at the top,
//! newest at the bottom. Each message lays out as a sharp-cornered
//! paper-card bubble — sender + recipient chips, body text (with
//! search-match underlines when a notebook-wide search is active),
//! optional read receipts, and a short id footer.
//!
//! The widget holds UI + cached-query state only; the host supplies
//! the local-messages workspace (required) and an optional `relations`
//! workspace at render time.
//!
//! Identity display is resolved against the relations branch (if
//! supplied): `alias → first_name last_name → display_name → 8-char
//! hex prefix`. If relations is absent the widget quietly degrades to
//! the hex-prefix view. Per-person color chips use
//! `GORBIE::themes::colorhash::ral_categorical` keyed on the user id
//! bytes.
//!
//! ```ignore
//! let mut panel = MessagesPanel::default();
//! panel.render(ctx, messages_ws, Some(relations_ws));
//! ```

use std::collections::HashMap;

use GORBIE::prelude::CardCtx;
use GORBIE::themes::colorhash;
use triblespace::core::id::Id;
use triblespace::core::metadata;
use triblespace::core::repo::pile::Pile;
use triblespace::core::repo::{CommitHandle, Workspace};
use triblespace::core::trible::TribleSet;
use triblespace::core::inline::encodings::hash::{Blake3, Handle};
use triblespace::core::inline::Inline;
use triblespace::macros::{find, pattern};
use triblespace::prelude::blobencodings::LongString;
use triblespace::prelude::View;

use crate::schemas::local_messages::{local, KIND_MESSAGE_ID, KIND_READ_ID};
use crate::schemas::relations::{relations as rel, KIND_PERSON_ID};

/// Handle to a long-string blob (message bodies).
type TextHandle = Inline<Handle<LongString>>;

// ── ID / time helpers ────────────────────────────────────────────────

/// Full hex of an Id — used as a fallback label when no friendly name
/// is resolvable from the relations branch.
fn id_hex(id: Id) -> String {
    format!("{id:x}")
}

fn now_tai_ns() -> i128 {
    hifitime::Epoch::now()
        .map(|e| e.to_tai_duration().total_nanoseconds())
        .unwrap_or(0)
}

fn format_age(now_key: i128, maybe_key: Option<i128>) -> String {
    let Some(key) = maybe_key else {
        return "-".to_string();
    };
    let delta_ns = now_key.saturating_sub(key);
    let delta_s = (delta_ns / 1_000_000_000).max(0) as i64;
    if delta_s < 60 {
        format!("{delta_s}s")
    } else if delta_s < 60 * 60 {
        format!("{}m", delta_s / 60)
    } else if delta_s < 24 * 60 * 60 {
        format!("{}h", delta_s / 3600)
    } else {
        format!("{}d", delta_s / 86_400)
    }
}

fn format_age_key(now_key: i128, past_key: i128) -> String {
    format_age(now_key, Some(past_key))
}

/// Absolute timestamp from a TAI ns key. Used for hover tooltips so the
/// compact age chips can still surface precise times on demand.
fn format_timestamp_key(key: i128) -> String {
    let ns = hifitime::Duration::from_total_nanoseconds(key);
    let epoch = hifitime::Epoch::from_tai_duration(ns);
    let (y, m, d, h, min, s, _) = epoch.to_gregorian_utc();
    format!("{y:04}-{m:02}-{d:02} {h:02}:{min:02}:{s:02} UTC")
}

// ── Color palette (reuses compass.rs conventions) ────────────────────

// Theme-adaptive neutrals (mirror of compass.rs). The accent /
// read / person colors are legible on both themes, but the
// frame / bubble / muted greys need to flip so theme-aware text
// doesn't land dark-on-dark in light mode.

fn color_frame(ui: &egui::Ui) -> egui::Color32 {
    if ui.visuals().dark_mode {
        egui::Color32::from_rgb(0x29, 0x32, 0x36) // RAL 7016
    } else {
        egui::Color32::from_rgb(0xec, 0xec, 0xec)
    }
}

fn color_muted(ui: &egui::Ui) -> egui::Color32 {
    if ui.visuals().dark_mode {
        egui::Color32::from_rgb(0x9a, 0x9a, 0x9a)
    } else {
        egui::Color32::from_rgb(0x6a, 0x6a, 0x6a)
    }
}

fn color_read() -> egui::Color32 {
    // RAL 6017 may green — "read" accent, matches playground diagnostics.
    egui::Color32::from_rgb(0x4a, 0x77, 0x29)
}

/// Deterministic per-person color chip via GORBIE's colorhash palette.
fn person_color(id: Id) -> egui::Color32 {
    colorhash::ral_categorical(id.as_ref())
}

// ── Row structs ──────────────────────────────────────────────────────

#[derive(Clone, Debug)]
struct MessageRow {
    id: Id,
    from: Id,
    to: Id,
    body: String,
    /// TAI ns of the message's `metadata::created_at` (sort key).
    created_at: Option<i128>,
    /// Read receipts for this message. Each entry is `(reader, ts_ns)`.
    reads: Vec<(Id, i128)>,
}

impl MessageRow {
    fn sort_key(&self) -> i128 {
        self.created_at.unwrap_or(i128::MIN)
    }
}

/// Everything we know about a person for UI purposes.
#[derive(Clone, Debug, Default)]
struct Person {
    alias: Option<String>,
    first_name: Option<String>,
    last_name: Option<String>,
    display_name: Option<String>,
}

impl Person {
    /// Display name: alias > first+last > display_name > hex prefix.
    fn display(&self, fallback_id: Id) -> String {
        if let Some(a) = self.alias.as_ref() {
            if !a.trim().is_empty() {
                return a.clone();
            }
        }
        match (self.first_name.as_ref(), self.last_name.as_ref()) {
            (Some(f), Some(l)) if !f.trim().is_empty() && !l.trim().is_empty() => {
                return format!("{f} {l}");
            }
            (Some(f), _) if !f.trim().is_empty() => return f.clone(),
            (_, Some(l)) if !l.trim().is_empty() => return l.clone(),
            _ => {}
        }
        if let Some(d) = self.display_name.as_ref() {
            if !d.trim().is_empty() {
                return d.clone();
            }
        }
        id_hex(fallback_id)
    }
}

// ── Cached message query state ───────────────────────────────────────

/// Cached fact spaces + head markers + resolved people map. Rebuilt
/// whenever the local-messages head advances or the relations head
/// changes.
struct MessagesLive {
    space: TribleSet,
    cached_head: Option<CommitHandle>,
    relations_cached_head: Option<CommitHandle>,
    people: HashMap<Id, Person>,
}

impl MessagesLive {
    /// Refresh cached fact spaces + people map from the provided
    /// workspaces.
    fn refresh(
        ws: &mut Workspace<Pile>,
        relations_ws: Option<&mut Workspace<Pile>>,
    ) -> Self {
        let space = ws
            .checkout(..)
            .map(|co| co.into_facts())
            .unwrap_or_else(|e| {
                eprintln!("[messages] checkout: {e:?}");
                TribleSet::new()
            });
        let cached_head = ws.head();

        let (relations_cached_head, people) = match relations_ws {
            Some(rws) => {
                let head = rws.head();
                let rspace = rws
                    .checkout(..)
                    .map(|co| co.into_facts())
                    .unwrap_or_else(|e| {
                        eprintln!("[messages] relations checkout: {e:?}");
                        TribleSet::new()
                    });
                let people = build_people(&rspace, rws);
                (head, people)
            }
            None => (None, HashMap::new()),
        };

        MessagesLive {
            space,
            cached_head,
            relations_cached_head,
            people,
        }
    }

    fn text(&self, ws: &mut Workspace<Pile>, h: TextHandle) -> String {
        ws.get::<View<str>, LongString>(h)
            .map(|v| {
                let s: &str = v.as_ref();
                s.to_string()
            })
            .unwrap_or_default()
    }

    /// Friendly display name for an Id, falling back to hex prefix.
    fn display_name(&self, id: Id) -> String {
        match self.people.get(&id) {
            Some(p) => p.display(id),
            None => id_hex(id),
        }
    }

    /// Collect every message with its from/to/body/created_at and fold
    /// in the read-receipt events that target it.
    fn messages(&self, ws: &mut Workspace<Pile>) -> Vec<MessageRow> {
        let mut by_id: HashMap<Id, MessageRow> = HashMap::new();

        let rows: Vec<(Id, Id, Id, TextHandle, (i128, i128))> = find!(
            (
                mid: Id,
                from: Id,
                to: Id,
                body: TextHandle,
                ts: (i128, i128)
            ),
            pattern!(&self.space, [{
                ?mid @
                metadata::tag: &KIND_MESSAGE_ID,
                local::from: ?from,
                local::to: ?to,
                local::body: ?body,
                metadata::created_at: ?ts,
            }])
        )
        .collect();

        for (mid, from, to, body_handle, ts) in rows {
            if by_id.contains_key(&mid) {
                continue;
            }
            let body = self.text(ws, body_handle);
            by_id.insert(
                mid,
                MessageRow {
                    id: mid,
                    from,
                    to,
                    body,
                    created_at: Some(ts.0),
                    reads: Vec::new(),
                },
            );
        }

        // Read-receipt pairing.
        let mut latest: HashMap<(Id, Id), i128> = HashMap::new();
        for (mid, reader, ts) in find!(
            (mid: Id, reader: Id, ts: (i128, i128)),
            pattern!(&self.space, [{
                _?event @
                metadata::tag: &KIND_READ_ID,
                local::about_message: ?mid,
                local::reader: ?reader,
                local::read_at: ?ts,
            }])
        ) {
            let key = (mid, reader);
            let entry = latest.entry(key).or_insert(i128::MIN);
            if ts.0 > *entry {
                *entry = ts.0;
            }
        }
        for ((mid, reader), ts) in latest {
            if let Some(row) = by_id.get_mut(&mid) {
                row.reads.push((reader, ts));
            }
        }

        for row in by_id.values_mut() {
            row.reads.sort_by(|a, b| b.1.cmp(&a.1));
        }

        by_id.into_values().collect()
    }

}

/// Build the people map by scanning the relations fact space.
fn build_people(
    relations_space: &TribleSet,
    relations_ws: &mut Workspace<Pile>,
) -> HashMap<Id, Person> {
    let mut people: HashMap<Id, Person> = HashMap::new();

    let person_ids: Vec<Id> = find!(
        pid: Id,
        pattern!(relations_space, [{ ?pid @ metadata::tag: &KIND_PERSON_ID }])
    )
    .collect();
    for pid in &person_ids {
        people.insert(*pid, Person::default());
    }

    let alias_rows: Vec<(Id, String)> = find!(
        (pid: Id, alias: String),
        pattern!(relations_space, [{ ?pid @ rel::alias: ?alias }])
    )
    .collect();
    for (pid, alias) in alias_rows {
        if let Some(p) = people.get_mut(&pid) {
            match p.alias.as_ref() {
                Some(existing) if existing.as_str() <= alias.as_str() => {}
                _ => p.alias = Some(alias),
            }
        }
    }

    let relations_text = |ws: &mut Workspace<Pile>, h: TextHandle| -> Option<String> {
        ws.get::<View<str>, LongString>(h).ok().map(|v| {
            let s: &str = v.as_ref();
            s.to_string()
        })
    };

    let first_rows: Vec<(Id, TextHandle)> = find!(
        (pid: Id, h: TextHandle),
        pattern!(relations_space, [{ ?pid @ rel::first_name: ?h }])
    )
    .collect();
    for (pid, h) in first_rows {
        if people.contains_key(&pid) {
            if let Some(v) = relations_text(relations_ws, h) {
                if let Some(p) = people.get_mut(&pid) {
                    p.first_name.get_or_insert(v);
                }
            }
        }
    }

    let last_rows: Vec<(Id, TextHandle)> = find!(
        (pid: Id, h: TextHandle),
        pattern!(relations_space, [{ ?pid @ rel::last_name: ?h }])
    )
    .collect();
    for (pid, h) in last_rows {
        if people.contains_key(&pid) {
            if let Some(v) = relations_text(relations_ws, h) {
                if let Some(p) = people.get_mut(&pid) {
                    p.last_name.get_or_insert(v);
                }
            }
        }
    }

    let display_rows: Vec<(Id, TextHandle)> = find!(
        (pid: Id, h: TextHandle),
        pattern!(relations_space, [{ ?pid @ rel::display_name: ?h }])
    )
    .collect();
    for (pid, h) in display_rows {
        if people.contains_key(&pid) {
            if let Some(v) = relations_text(relations_ws, h) {
                if let Some(p) = people.get_mut(&pid) {
                    p.display_name.get_or_insert(v);
                }
            }
        }
    }

    people
}

// ── Widget ───────────────────────────────────────────────────────────

/// GORBIE-embeddable local-messages panel with compose, relations
/// identity lookup, scroll-to-bottom on new messages, and automatic
/// read-receipts for inbound messages.
///
/// See the module docs for construction examples.
pub struct MessagesPanel {
    /// Rebuilt when the messages / relations head changes.
    live: Option<MessagesLive>,
}

impl Default for MessagesPanel {
    fn default() -> Self {
        Self { live: None }
    }
}

impl MessagesPanel {
    /// New read-only panel.
    pub fn new() -> Self {
        Self::default()
    }

    /// Backwards-compatibility shim: the panel no longer has an
    /// internal scroll area (the notebook's own scroll handles
    /// overflow), so a configured height is a no-op.
    pub fn with_height(self, _height: f32) -> Self {
        self
    }

    /// Render the panel. `ws` must point at the local-messages branch;
    /// `relations_ws` is optional and, when provided, is used for
    /// friendly-name resolution.
    pub fn render(
        &mut self,
        ctx: &mut CardCtx<'_>,
        ws: &mut Workspace<Pile>,
        mut relations_ws: Option<&mut Workspace<Pile>>,
    ) {
        // Refresh cached state if any head advanced.
        let head = ws.head();
        let rhead = relations_ws.as_ref().and_then(|w| w.head());
        let need_refresh = match self.live.as_ref() {
            None => true,
            Some(l) => l.cached_head != head || l.relations_cached_head != rhead,
        };
        if need_refresh {
            self.live = Some(MessagesLive::refresh(
                ws,
                relations_ws.as_mut().map(|w| &mut **w),
            ));
        }

        ctx.section("Messages", |ctx| {
            let Some(live) = self.live.as_ref() else { return };

            // Pre-materialize everything the UI closure needs.
            let mut messages = live.messages(ws);
            messages.sort_by(|a, b| {
                a.sort_key()
                    .cmp(&b.sort_key())
                    .then_with(|| a.id.cmp(&b.id))
            });

            // Build a name lookup for every id we'll paint.
            let mut names: HashMap<Id, String> = HashMap::new();
            for m in &messages {
                names
                    .entry(m.from)
                    .or_insert_with(|| live.display_name(m.from));
                names.entry(m.to).or_insert_with(|| live.display_name(m.to));
                for (r, _) in &m.reads {
                    names.entry(*r).or_insert_with(|| live.display_name(*r));
                }
            }

            let now = now_tai_ns();
            let count = messages.len();
            let latest_age = messages
                .iter()
                .filter_map(|m| m.created_at)
                .max()
                .map(|k| format_age_key(now, k));

            // Open a notebook-wide search session — makes the bar
            // appear in the top-right and lets us filter messages by
            // body / from-name / to-name substring.
            let mut search = ctx.search();
            let needle = search.query().to_lowercase();
            let search_active = !needle.is_empty();

            ctx.grid(|g| {
                // Header row: count on the left, "LAST <age>" right.
                g.full(|ctx| {
                    let ui = ctx.ui_mut();
                    ui.horizontal(|ui| {
                        ui.spacing_mut().item_spacing.x = 6.0;
                        ui.label(
                            egui::RichText::new(format!("{count} MESSAGES"))
                                .monospace()
                                .strong()
                                .small()
                                .color(color_muted(ui)),
                        );
                        if let Some(age) = latest_age.as_ref() {
                            ui.with_layout(
                                egui::Layout::right_to_left(egui::Align::Center),
                                |ui| {
                                    ui.label(
                                        egui::RichText::new(format!(
                                            "LAST {}",
                                            age.to_uppercase()
                                        ))
                                        .monospace()
                                        .small()
                                        .strong()
                                        .color(color_muted(ui)),
                                    );
                                },
                            );
                        }
                    });
                });

                if messages.is_empty() {
                    g.full(|ctx| {
                        render_messages_empty_state(
                            ctx.ui_mut(),
                            "No messages yet.",
                            None,
                        );
                    });
                    return;
                }

                // One grid cell per message; the notebook's own scroll
                // area handles overflow. No nested ScrollArea + no
                // arrival/stickiness state machine — the viewer is
                // read-only, so the user just scrolls the notebook.
                for msg in &messages {
                    if search_active && !message_matches_search(msg, &names, &needle) {
                        continue;
                    }
                    let match_info = if search_active {
                        Some(search.report(egui::Id::new(("messages_match", msg.id))))
                    } else {
                        None
                    };
                    let is_focused =
                        match_info.as_ref().map_or(false, |i| i.is_focused);
                    g.full(|ctx| {
                        let ui = ctx.ui_mut();
                        let pre_y = ui.cursor().min.y;
                        render_message(ui, msg, now, &names, &needle, is_focused);
                        if let Some(info) = match_info {
                            if info.should_scroll_to {
                                let post_y = ui.cursor().min.y;
                                let msg_rect = egui::Rect::from_min_max(
                                    egui::pos2(ui.min_rect().left(), pre_y),
                                    egui::pos2(ui.min_rect().right(), post_y),
                                );
                                ui.scroll_to_rect(
                                    msg_rect,
                                    Some(egui::Align::Center),
                                );
                            }
                        }
                    });
                }
            });

            // Read-only viewer — no writes to apply post-render.
            let _ = ws;
        });
    }
}

// ── Row rendering ────────────────────────────────────────────────────

/// True if the message's body, sender display name, or recipient
/// display name contains the (lowercased) needle.
fn message_matches_search(
    msg: &MessageRow,
    names: &HashMap<Id, String>,
    needle: &str,
) -> bool {
    if msg.body.to_lowercase().contains(needle) {
        return true;
    }
    for id in [msg.from, msg.to] {
        if let Some(name) = names.get(&id) {
            if name.to_lowercase().contains(needle) {
                return true;
            }
        }
    }
    false
}

fn render_message(
    ui: &mut egui::Ui,
    msg: &MessageRow,
    now: i128,
    names: &HashMap<Id, String>,
    // Lowercased search needle ("" = no search).
    search_needle: &str,
    // True when this bubble is the bar's currently-focused match;
    // makes every needle occurrence inside render with the double
    // underline (same emphasis the typst widget uses).
    focused: bool,
) {
    let bubble_fill = ui.visuals().window_fill;
    let from_color = person_color(msg.from);
    let to_color = person_color(msg.to);
    // Sender/recipient stripes flank the bubble (compass-card idiom).
    // Width 18 fits a 9-pt monospace name rotated 90°. Inner content
    // is inset on both sides to leave room.
    const STRIPE_WIDTH: f32 = 18.0;
    const STRIPE_GAP: f32 = 8.0;
    const STROKE_INSET: f32 = 1.0;

    ui.vertical(|ui| {
    let inner_margin = egui::Margin {
        left: (STROKE_INSET + STRIPE_WIDTH + STRIPE_GAP) as i8,
        right: (STROKE_INSET + STRIPE_WIDTH + STRIPE_GAP) as i8,
        top: 6,
        bottom: 6,
    };
    let frame_resp = egui::Frame::NONE
        .fill(bubble_fill)
        .stroke(egui::Stroke::new(STROKE_INSET, color_frame(ui)))
        // Hard offset shadow + sharp corners: same paper-card idiom
        // compass goals use, giving the bubble physical "lift" instead
        // of a backlit LCD look.
        .shadow(egui::epaint::Shadow {
            offset: [2, 2],
            blur: 0,
            spread: 0,
            color: egui::Color32::from_black_alpha(48),
        })
        .corner_radius(egui::CornerRadius::ZERO)
        .inner_margin(inner_margin)
        .show(ui, |ui| {
            // Header row: just the age, right-aligned. Sender/recipient
            // are conveyed via the colored side stripes painted after
            // the Frame returns — no need for in-header chips.
            //
            // `Align::Min` on the cross-axis (top) so the layout
            // doesn't try to fill the cell's available_rect.height —
            // with frame-delayed cell sizing, that fill would feed
            // back into next frame's larger cell, growing forever.
            ui.with_layout(
                egui::Layout::right_to_left(egui::Align::Min),
                |ui| {
                    let (age, hover) = match msg.created_at {
                        Some(k) => {
                            (format_age_key(now, k), Some(format_timestamp_key(k)))
                        }
                        None => ("-".to_string(), None),
                    };
                    let resp = ui.label(
                        egui::RichText::new(age)
                            .monospace()
                            .small()
                            .color(color_muted(ui)),
                    );
                    if let Some(h) = hover {
                        resp.on_hover_text(h);
                    }
                },
            );

            ui.add_space(2.0);

            // Body. When a search is active, occurrences of the needle
            // are underlined inline; the bar's focused match gets a
            // second underline overlay via `highlight_label`.
            let base = egui::TextFormat {
                font_id: egui::TextStyle::Body.resolve(ui.style()),
                color: ui.visuals().text_color(),
                ..Default::default()
            };
            GORBIE::search::highlight_label(
                ui,
                &msg.body,
                search_needle,
                base,
                focused,
            );

            // Read receipts — compact "✓✓ NameA · NameB · 2h" line in
            // the may-green accent. Newest receipt's age is used as the
            // overall age; individual ages show on hover.
            if !msg.reads.is_empty() {
                ui.add_space(4.0);
                ui.horizontal_wrapped(|ui| {
                    ui.spacing_mut().item_spacing.x = 4.0;
                    ui.label(
                        egui::RichText::new("\u{2713}\u{2713}")
                            .small()
                            .color(color_read()),
                    );
                    let mut first = true;
                    for (reader, ts) in &msg.reads {
                        if !first {
                            ui.label(
                                egui::RichText::new("\u{00b7}")
                                    .small()
                                    .color(color_muted(ui)),
                            );
                        }
                        first = false;
                        let name = names
                            .get(reader)
                            .cloned()
                            .unwrap_or_else(|| id_hex(*reader));
                        // Tint each reader name with its own person
                        // color so the reader list matches the
                        // sender/recipient chips above.
                        let response = ui.label(
                            egui::RichText::new(name)
                                .small()
                                .color(person_color(*reader)),
                        );
                        response.on_hover_text(format!(
                            "read {} · {}",
                            format_age_key(now, *ts),
                            format_timestamp_key(*ts),
                        ));
                    }
                    // Newest-reader age as a trailing muted suffix.
                    if let Some((_, newest_ts)) =
                        msg.reads.iter().max_by_key(|(_, t)| *t)
                    {
                        ui.label(
                            egui::RichText::new(format!(
                                "\u{00b7} {}",
                                format_age_key(now, *newest_ts)
                            ))
                            .small()
                            .color(color_muted(ui)),
                        );
                    }
                });
            }

            // Short id footer.
            ui.horizontal(|ui| {
                ui.label(
                    egui::RichText::new(id_hex(msg.id))
                        .monospace()
                        .small()
                        .color(color_muted(ui)),
                );
            });
        });

    // ── Sender / recipient stripes (compass-card idiom) ─────────────
    //
    // After the Frame has measured + painted, lay two colored stripes
    // along the bubble's left and right edges (inset by the stroke so
    // the 1px outline draws around them). Each stripe carries the
    // person's monospace name rotated 90° — sender top-down on the
    // left, recipient bottom-up on the right — so the bubble reads
    // like an envelope: FROM ➝ TO without any in-body chips eating
    // the header.
    let outer = frame_resp.response.rect;
    let from_name = names
        .get(&msg.from)
        .cloned()
        .unwrap_or_else(|| id_hex(msg.from));
    let to_name = names
        .get(&msg.to)
        .cloned()
        .unwrap_or_else(|| id_hex(msg.to));
    paint_party_stripe(
        ui.painter(),
        outer,
        StripeSide::Left,
        from_color,
        &from_name.to_uppercase(),
    );
    paint_party_stripe(
        ui.painter(),
        outer,
        StripeSide::Right,
        to_color,
        &to_name.to_uppercase(),
    );
    });
}

#[derive(Clone, Copy)]
enum StripeSide {
    Left,
    Right,
}

/// Paint a compass-style colored stripe along one vertical edge of
/// `outer`, with `label` rendered as monospace rotated 90°. The text
/// is skipped when the stripe is too short to hold the glyphs (avoids
/// overflowing into the bubble body on one-line messages).
fn paint_party_stripe(
    painter: &egui::Painter,
    outer: egui::Rect,
    side: StripeSide,
    color: egui::Color32,
    label: &str,
) {
    const STRIPE_WIDTH: f32 = 18.0;
    const STROKE_INSET: f32 = 1.0;
    let stripe_min = match side {
        StripeSide::Left => outer.min + egui::vec2(STROKE_INSET, STROKE_INSET),
        StripeSide::Right => egui::pos2(
            outer.right() - STROKE_INSET - STRIPE_WIDTH,
            outer.top() + STROKE_INSET,
        ),
    };
    let stripe_rect = egui::Rect::from_min_size(
        stripe_min,
        egui::vec2(STRIPE_WIDTH, outer.height() - 2.0 * STROKE_INSET),
    );
    painter.rect_filled(stripe_rect, egui::CornerRadius::ZERO, color);

    let font = egui::FontId::monospace(9.0);
    let text_color = colorhash::text_color_on(color);
    let galley = painter.layout_no_wrap(label.to_string(), font, text_color);
    // Need height for the glyphs + a little breathing room.
    if galley.size().x + 6.0 > stripe_rect.height() {
        return;
    }
    let gh = galley.size().y;
    let mut text_shape = match side {
        StripeSide::Left => {
            // 90° clockwise rotation: text reads top-to-bottom. egui's
            // `TextShape::angle = +π/2` rotates around `pos` such that
            // the galley extends LEFT and DOWN from `pos`. So `pos`
            // sits on the right edge of where the text should appear.
            let pos = egui::pos2(
                stripe_rect.left() + (STRIPE_WIDTH + gh) * 0.5,
                stripe_rect.top() + 5.0,
            );
            let mut s = egui::epaint::TextShape::new(pos, galley, text_color);
            s.angle = std::f32::consts::FRAC_PI_2;
            s
        }
        StripeSide::Right => {
            // 90° counter-clockwise (bottom-to-top read) so the
            // recipient name visually faces the sender across the
            // bubble. `angle = -π/2` rotates around `pos` such that
            // the galley extends RIGHT and UP — so `pos` sits at the
            // left edge of where the rotated text should appear.
            let pos = egui::pos2(
                stripe_rect.left() + (STRIPE_WIDTH - gh) * 0.5,
                stripe_rect.bottom() - 5.0,
            );
            let mut s = egui::epaint::TextShape::new(pos, galley, text_color);
            s.angle = -std::f32::consts::FRAC_PI_2;
            s
        }
    };
    text_shape.fallback_color = text_color;
    painter.add(text_shape);
}

/// Centered empty-state block with an envelope glyph, a headline
/// message, and an optional muted sub-line. Used whenever the
/// messages panel has nothing to show.
fn render_messages_empty_state(ui: &mut egui::Ui, headline: &str, hint: Option<&str>) {
    ui.add_space(24.0);
    ui.vertical_centered(|ui| {
        ui.label(
            egui::RichText::new("\u{2709}")
                .size(32.0)
                .color(color_muted(ui)),
        );
        ui.add_space(6.0);
        ui.label(
            egui::RichText::new(headline)
                .monospace()
                .small()
                .strong()
                .color(color_muted(ui)),
        );
        if let Some(h) = hint {
            ui.add_space(2.0);
            ui.label(
                egui::RichText::new(h)
                    .small()
                    .color(color_muted(ui)),
            );
        }
    });
    ui.add_space(24.0);
}