Luxe 0.1.3: your sessions on your phone, joinable sessions, and nested AGENTS.md

- luxe remote puts the sessions on your computer in reach of your own phone or tablet over
  Tailscale: write, steer, answer prompts, send photos, browse files, diffs and GitHub, with a
  paired device and your own Tailscale login.
- luxe join lets another program on the same machine join a running session over the RPC protocol.
- An AGENTS.md in a subfolder reaches the model the first time it works there.
- Long sessions stay fast: no stall before each model call, lighter memory, cheaper drawing.
- A session is open in one Luxe at a time, web_search shares results across its phrasings, and a
  grep glob starting with ! excludes.

The CHANGELOG has the full list.
This commit is contained in:
Evan Whittaker
2026-10-20 02:40:13 -05:00
parent 99e30d6d0f
commit d2129a886f
423 changed files with 31666 additions and 1669 deletions

View File

@@ -0,0 +1,352 @@
//! Markdown as HTML — model text for the phone app, drawn the way the transcript draws it.
//!
//! The line renderer lays text out in cells at a width it knows; a browser reflows on every turn of
//! the phone, so this walks the same events, in the same dialect, into elements instead, and keeps
//! the TUI's choices: the alert's word and mark, a fence's language and copy affordance on its
//! rule, a link's address after its words. How those look (bullets, `> `, the rules) is the app's
//! stylesheet. Code is highlighted into the same roles the terminal paints, each a class naming
//! its theme token (`t-accent-secondary`), never an inline style: the app's CSP forbids those, and
//! the token's color comes from the user's theme as it is now.
//!
//! What the model wrote is data: every character of it is escaped, raw HTML shows as its source
//! the way the TUI shows it, and a link only gets an `href` when it leaves for the web or mail.
use pulldown_cmark::{Alignment, BlockQuoteKind, CodeBlockKind, Event, Parser, Tag, TagEnd};
use super::fence::fence_language;
use super::layout::parser_options;
use super::voice::{alert_icon, alert_label};
use crate::glyphs::{GlyphTier, Icon};
use crate::highlight::{highlight_roles, Role};
/// Which reader a block is rendered for, as in the transcript.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Voice {
/// Assistant prose, fences highlighted
#[default]
Answer,
/// Model reasoning: line breaks kept, fences left plain so the block stays muted
Reasoning,
}
/// The app's font ships with the Nerd Font marks, so every glyph is drawn at that tier.
const TIER: GlyphTier = GlyphTier::Nerd;
/// `src` as HTML for `voice`, with no wrapping element: the app supplies that and its class.
///
/// Partial input is fine. A reply is rendered as it streams, and pulldown-cmark closes whatever is
/// still open at the end, so an unterminated fence is a fence.
pub fn render(src: &str, voice: Voice) -> String {
let mut writer = HtmlWriter { voice, ..HtmlWriter::default() };
for event in Parser::new_ext(src, parser_options()) {
writer.event(event);
}
writer.out
}
/// One source line highlighted as HTML, the way a diff row's code is: on its own, as the TUI's
/// diff views highlight it. Everything in `text` comes back escaped.
pub fn code_line(text: &str, lang: Option<&str>) -> String {
let mut out = String::new();
// the highlighter keeps every character of a line, so only empty text has no first line
for (role, part) in highlight_roles(text, lang).first().into_iter().flatten() {
role_span_into(&mut out, *role, part);
}
out
}
/// `text` in the span for its highlight role, or bare when the role is body text.
fn role_span_into(out: &mut String, role: Role, text: &str) {
let Some(class) = role_class(role) else { return escape_into(out, text) };
out.push_str(&format!("<span class=\"{class}\">"));
escape_into(out, text);
out.push_str("</span>");
}
/// One walk's state: the HTML so far, and the open things whose end changes what is written.
#[derive(Default)]
struct HtmlWriter {
out: String,
voice: Voice,
link: Option<Link>,
fence: Option<Fence>,
table: Option<Table>,
}
/// A link being written: where it goes, what it says so far, and whether it got an `href`.
struct Link {
url: String,
words: String,
live: bool,
}
/// A code block being collected, since it is highlighted whole.
struct Fence {
lang: Option<String>,
code: String,
}
/// A table being written: how each column aligns, and which cell is next.
struct Table {
alignments: Vec<Alignment>,
cell: usize,
head: bool,
}
impl HtmlWriter {
fn event(&mut self, event: Event<'_>) {
match event {
Event::Start(tag) => self.start(tag),
Event::End(tag) => self.end(tag),
// block HTML shows as the source the model wrote, as it does in the terminal
Event::Text(text) | Event::Html(text) => self.text(&text),
Event::Code(code) => self.code(&code),
Event::SoftBreak if self.voice == Voice::Reasoning => self.out.push_str("<br>"),
Event::SoftBreak => self.out.push(' '),
Event::HardBreak => self.out.push_str("<br>"),
Event::Rule => self.out.push_str("<hr>"),
// inline HTML, footnotes and the rest: the terminal leaves them out too
_ => {}
}
}
fn start(&mut self, tag: Tag<'_>) {
match tag {
Tag::Paragraph => self.out.push_str("<p>"),
Tag::Heading { level, .. } => self.out.push_str(&format!("<{level}>")),
Tag::BlockQuote(kind) => self.open_quote(kind),
Tag::CodeBlock(kind) => {
self.fence = Some(Fence { lang: fence_lang(kind), code: "".into() })
}
Tag::List(Some(1)) => self.out.push_str("<ol>"),
Tag::List(Some(start)) => self.out.push_str(&format!("<ol start=\"{start}\">")),
Tag::List(None) => self.out.push_str("<ul>"),
Tag::Item => self.out.push_str("<li>"),
Tag::Table(alignments) => self.open_table(alignments),
Tag::TableHead | Tag::TableRow => self.open_row(),
Tag::TableCell => self.open_cell(),
Tag::Emphasis => self.out.push_str("<em>"),
Tag::Strong => self.out.push_str("<strong>"),
Tag::Strikethrough => self.out.push_str("<del>"),
Tag::Link { dest_url, .. } => self.open_link(&dest_url),
// an image's words are its alt text, which follows as text
_ => {}
}
}
fn end(&mut self, tag: TagEnd) {
let close = match tag {
TagEnd::Paragraph => "</p>",
TagEnd::Heading(level) => return self.out.push_str(&format!("</{level}>")),
TagEnd::BlockQuote(_) => "</blockquote>",
TagEnd::CodeBlock => return self.close_fence(),
TagEnd::List(true) => "</ol>",
TagEnd::List(false) => "</ul>",
TagEnd::Item => "</li>",
TagEnd::Table => return self.close_table(),
TagEnd::TableHead => return self.close_head(),
TagEnd::TableRow => "</tr>",
TagEnd::TableCell => return self.close_cell(),
TagEnd::Emphasis => "</em>",
TagEnd::Strong => "</strong>",
TagEnd::Strikethrough => "</del>",
TagEnd::Link => return self.close_link(),
_ => "",
};
self.out.push_str(close);
}
fn text(&mut self, text: &str) {
if let Some(fence) = &mut self.fence {
return fence.code.push_str(text);
}
if let Some(link) = &mut self.link {
link.words.push_str(text);
}
escape_into(&mut self.out, text);
}
fn code(&mut self, code: &str) {
if let Some(link) = &mut self.link {
link.words.push_str(code);
}
self.out.push_str("<code>");
escape_into(&mut self.out, code);
self.out.push_str("</code>");
}
/// Opens a quote; an alert writes its word out first, since its severity is in the tag rather
/// than in its prose, and the color is only polish.
fn open_quote(&mut self, kind: Option<BlockQuoteKind>) {
let Some(kind) = kind else { return self.out.push_str("<blockquote>") };
let label = alert_label(kind);
self.out.push_str(&format!("<blockquote class=\"alert {}\">", label.to_lowercase()));
self.out.push_str("<p class=\"alert-label\">");
escape_into(&mut self.out, &icon_label(alert_icon(kind), label));
self.out.push_str("</p>");
}
fn open_link(&mut self, url: &str) {
let live = leaves_for_the_web(url);
if live {
self.out.push_str("<a href=\"");
escape_into(&mut self.out, url);
self.out.push_str("\" target=\"_blank\" rel=\"noopener noreferrer\">");
} else {
self.out.push_str("<span class=\"link\">");
}
self.link = Some(Link { url: url.to_string(), words: String::new(), live });
}
/// Closes a link and writes its address after it, once, unless the words already are the
/// address or a table cell holds it, as the terminal does.
fn close_link(&mut self) {
let Some(link) = self.link.take() else { return };
self.out.push_str(if link.live { "</a>" } else { "</span>" });
let repeats = link.url.trim_start_matches("mailto:") == link.words.trim();
if link.url.is_empty() || repeats || self.table.is_some() {
return;
}
self.out.push_str("<span class=\"url\"> (");
escape_into(&mut self.out, &link.url);
self.out.push_str(")</span>");
}
}
impl HtmlWriter {
/// Writes a code block between its rules: the language and copy on the top one. An empty
/// block draws nothing, as in the terminal.
fn close_fence(&mut self) {
let Some(fence) = self.fence.take() else { return };
if fence.code.is_empty() {
return;
}
self.out.push_str("<figure class=\"fence\"><figcaption>");
if let Some(language) = fence_language(fence.lang.as_deref()) {
self.out.push_str("<span class=\"lang\">");
let icon = crate::widgets::language_icon(&language);
escape_into(&mut self.out, &icon_label(icon, &language));
self.out.push_str("</span>");
}
self.out.push_str("<span class=\"fill\"></span><button type=\"button\" class=\"copy\">");
escape_into(&mut self.out, &icon_label(Icon::Copy, "copy"));
self.out.push_str("</button></figcaption><pre><code>");
self.fence_body(&fence);
self.out.push_str("</code></pre></figure>");
}
/// The code, highlighted for an answer and plain for reasoning, which a palette would pull out
/// of its muted voice.
fn fence_body(&mut self, fence: &Fence) {
if self.voice == Voice::Reasoning {
return escape_into(&mut self.out, fence.code.trim_end_matches('\n'));
}
let lines = highlight_roles(&fence.code, fence.lang.as_deref());
for (index, line) in lines.iter().enumerate() {
if index > 0 {
self.out.push('\n');
}
for (role, text) in line {
role_span_into(&mut self.out, *role, text);
}
}
}
fn open_table(&mut self, alignments: Vec<Alignment>) {
self.table = Some(Table { alignments, cell: 0, head: true });
self.out.push_str("<div class=\"table\"><table><thead>");
}
fn open_row(&mut self) {
if let Some(table) = &mut self.table {
table.cell = 0;
}
self.out.push_str("<tr>");
}
fn open_cell(&mut self) {
let Some(table) = &mut self.table else { return };
let tag = if table.head { "th" } else { "td" };
let align = match table.alignments.get(table.cell) {
Some(Alignment::Right) => " class=\"right\"",
Some(Alignment::Center) => " class=\"center\"",
_ => "",
};
self.out.push_str(&format!("<{tag}{align}>"));
}
fn close_cell(&mut self) {
let Some(table) = &mut self.table else { return };
self.out.push_str(if table.head { "</th>" } else { "</td>" });
table.cell += 1;
}
/// pulldown-cmark puts the header cells straight under the head, with no row of their own.
fn close_head(&mut self) {
if let Some(table) = &mut self.table {
table.head = false;
}
self.out.push_str("</tr></thead><tbody>");
}
fn close_table(&mut self) {
self.table = None;
self.out.push_str("</tbody></table></div>");
}
}
/// The language a code block declared: an indented one declares none.
fn fence_lang(kind: CodeBlockKind<'_>) -> Option<String> {
match kind {
CodeBlockKind::Fenced(lang) => Some(lang.to_string()),
CodeBlockKind::Indented => None,
}
}
/// `{glyph} {label}`, the shape the terminal's labels take at the Nerd tier.
fn icon_label(icon: Icon, label: &str) -> String {
format!("{} {label}", icon.at(TIER))
}
/// The classes a role is drawn with: its theme token, then its weight or slant. Plain text takes
/// none, which keeps a highlighted block about half the size.
fn role_class(role: Role) -> Option<String> {
use ratatui::style::Modifier;
if role == Role::Text {
return None;
}
let (token, modifier) = role.token();
let mut class = format!("t-{}", luxe_core::theme::css::css_name(token));
if modifier.contains(Modifier::BOLD) {
class.push_str(" b");
}
if modifier.contains(Modifier::ITALIC) {
class.push_str(" i");
}
Some(class)
}
/// Whether a link may be followed from the app: to the web or to mail, never `javascript:`, a
/// `data:` page, or a path on the bridge itself.
fn leaves_for_the_web(url: &str) -> bool {
let lower = url.trim_start().to_ascii_lowercase();
["https://", "http://", "mailto:"].iter().any(|scheme| lower.starts_with(scheme))
}
/// Appends `text` with every character that means something in HTML escaped, quotes included,
/// since some of it lands in attributes.
fn escape_into(out: &mut String, text: &str) {
for character in text.chars() {
match character {
'&' => out.push_str("&amp;"),
'<' => out.push_str("&lt;"),
'>' => out.push_str("&gt;"),
'"' => out.push_str("&quot;"),
'\'' => out.push_str("&#39;"),
_ => out.push(character),
}
}
}
#[cfg(test)]
mod tests;

View File

@@ -0,0 +1,135 @@
use super::{code_line, render, Voice};
fn answer(src: &str) -> String {
render(src, Voice::Answer)
}
/// What a model writes is shown, never run: block HTML is escaped and inline HTML left out.
///
/// The break this catches: text written into the page unescaped, which would let a reply run
/// script in the app.
#[test]
fn model_html_is_shown_not_run() {
let html = answer("<script>alert(1)</script>\n\nhi <img src=x onerror=alert(1)> & bye");
assert!(!html.contains("<script"), "{html}");
assert!(!html.contains("<img"), "{html}");
assert!(html.contains("&lt;script&gt;alert(1)&lt;/script&gt;"), "{html}");
assert!(html.contains("&amp; bye"), "{html}");
}
/// The break this catches: any scheme getting an `href`, so a tap on `javascript:` runs it.
#[test]
fn only_web_and_mail_links_are_followed() {
let unsafe_link = answer("[tap](javascript:alert(1))");
assert!(!unsafe_link.contains("href"), "{unsafe_link}");
assert!(unsafe_link.contains("<span class=\"link\">tap</span>"), "{unsafe_link}");
let page = answer("[docs](https://example.com/a)");
assert!(page.contains("<a href=\"https://example.com/a\""), "{page}");
assert!(answer("[me](mailto:a@b.c)").contains("href=\"mailto:a@b.c\""));
assert!(!answer("[up](/api/sessions)").contains("href"));
}
/// The break this catches: an address written into `href` unescaped, so a quote in it closes the
/// attribute and opens one of the model's choosing.
#[test]
fn a_link_cannot_add_attributes() {
let html = answer("[a](<https://x.y/\" onclick=\"steal()>)");
assert!(!html.contains("\" onclick"), "{html}");
assert!(html.contains("&quot; onclick=&quot;steal()"), "{html}");
}
/// The address follows the words, as in the terminal, but not when the words are the address.
///
/// The break this catches: the repeat check dropped, doubling every bare link.
#[test]
fn a_link_names_its_address_unless_its_words_are_it() {
let named = answer("[docs](https://example.com)");
assert!(named.contains("<span class=\"url\"> (https://example.com)</span>"), "{named}");
let bare = answer("<https://example.com>");
assert!(!bare.contains("class=\"url\""), "{bare}");
}
/// The break this catches: a fence losing its language label, copy control or highlighting
/// classes, which are what make it read like the terminal's.
#[test]
fn a_fence_is_framed_with_its_language_and_copy() {
let html = answer("```Rust\nfn main() {}\n```");
assert!(html.contains("<span class=\"lang\">"), "{html}");
assert!(html.contains(" rust</span>"), "{html}");
assert!(html.contains("<button type=\"button\" class=\"copy\">"), "{html}");
assert!(html.contains("<span class=\"t-accent-secondary\">fn</span>"), "{html}");
assert!(html.contains("<span class=\"t-accent\">main</span>"), "{html}");
}
/// The break this catches: a diff row's code left unhighlighted, highlighted for the wrong
/// language, or written into the page unescaped.
#[test]
fn a_line_of_code_is_highlighted_and_escaped() {
let html = code_line("fn main() { a < b }", Some("rs"));
assert!(html.starts_with("<span class=\"t-accent-secondary\">fn</span>"), "{html}");
assert!(html.contains("&lt;"), "{html}");
assert!(!html.contains(" < "), "{html}");
assert_eq!(code_line("a < b", None), "a &lt; b");
}
/// A fence that says it is plain text names no language, and an empty one draws nothing.
///
/// The break this catches: the label taken from the raw info string instead of
/// `fence_language`, or an empty block drawn as an empty frame.
#[test]
fn a_plain_or_empty_fence_names_nothing() {
let plain = answer("```text\nx\n```");
assert!(plain.contains("<figure class=\"fence\">"), "{plain}");
assert!(!plain.contains("class=\"lang\""), "{plain}");
assert_eq!(answer("```\n```"), "");
}
/// Streaming input ends wherever the model has got to; a fence still open is still a fence.
///
/// The break this catches: code collected and only written at a closing fence that has not
/// arrived yet, so a streaming block shows nothing.
#[test]
fn an_unfinished_fence_is_still_drawn() {
let html = answer("Here:\n\n```rust\nlet x");
assert!(html.contains("<pre><code>"), "{html}");
assert!(html.contains("x</code>"), "{html}");
}
/// Reasoning keeps its line breaks and leaves its code plain; an answer reflows.
///
/// The break this catches: the voice ignored, so reasoning runs its notes together or wears a
/// syntax palette.
#[test]
fn reasoning_keeps_breaks_and_plain_code() {
assert_eq!(render("a\nb", Voice::Reasoning), "<p>a<br>b</p>");
assert_eq!(answer("a\nb"), "<p>a b</p>");
let fence = render("```rust\nfn main() {}\n```", Voice::Reasoning);
assert!(!fence.contains("t-"), "{fence}");
assert!(fence.contains("fn main() {}</code>"), "{fence}");
}
/// The break this catches: an alert drawn as a plain quote, losing the only thing that made it
/// one.
#[test]
fn an_alert_writes_its_word() {
let html = answer("> [!WARNING]\n> careful");
assert!(html.starts_with("<blockquote class=\"alert warning\"><p class=\"alert-label\">"));
assert!(html.contains(" Warning</p>"), "{html}");
assert!(html.contains("<p>careful</p>"), "{html}");
}
/// The break this catches: the column count not reset per row, so the second body row takes
/// alignments that belong to cells past the end.
#[test]
fn table_cells_keep_their_column_alignment() {
let html = answer("| a | b |\n|---|--:|\n| 1 | 2 |\n| 3 | 4 |");
assert!(html.contains("<thead><tr><th>a</th><th class=\"right\">b</th></tr></thead>"));
assert!(html.contains("<tr><td>3</td><td class=\"right\">4</td></tr>"), "{html}");
}
/// The break this catches: a list's start number dropped, renumbering a list the model resumed.
#[test]
fn an_ordered_list_keeps_its_start() {
assert!(answer("3. x\n4. y").starts_with("<ol start=\"3\"><li>x</li>"));
assert!(answer("1. x").starts_with("<ol><li>"));
}

View File

@@ -37,20 +37,25 @@ fn push_block_gap(lines: &mut Vec<MarkdownLine>) {
}
}
/// Renders markdown in `voice`, preserving the structural block kinds conversation layout needs.
pub(crate) fn layout_markdown_in(
src: &str,
theme: &ThemeStyles,
voice: MarkdownVoice,
) -> Vec<MarkdownLine> {
/// The dialect model markdown is read in, here and in the phone app's HTML.
pub(super) fn parser_options() -> Options {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_STRIKETHROUGH);
opts.insert(Options::ENABLE_TABLES);
// Alerts (`> [!WARNING]`) are the GitHub dialect models were trained on and write unprompted.
// Without this the tag is body text, and the quote opens with a literal `[!WARNING]`.
opts.insert(Options::ENABLE_GFM);
opts
}
/// Renders markdown in `voice`, preserving the structural block kinds conversation layout needs.
pub(crate) fn layout_markdown_in(
src: &str,
theme: &ThemeStyles,
voice: MarkdownVoice,
) -> Vec<MarkdownLine> {
let mut renderer = MarkdownRenderer::new(theme, voice);
for event in Parser::new_ext(src, opts) {
for event in Parser::new_ext(src, parser_options()) {
renderer.event(event);
}
// Flush any remaining spans