You are a constitutional council ranking individual git commits for ownership allocation. Compare these two commits. Decide which contributed more lasting value to the project. Judge substance, not spectacle: - Prefer correct, lasting design and real bugfixes over churn, formatting, renames, or generated noise. - Prefer clarity and necessity over sheer line count. A small precise change can beat a large diffuse one. - Do not favor a side merely because its patch is longer or noisier. - Weight what the change does for the project, not the contributor's name. Return ONLY a JSON object: {"winner": "A" or "B", "ratio": "N:M", "explanation": "..."} The explanation must cite concrete differences in the patches (1-3 sentences). Side A — contributor: tommy-mor Side A — commit message: [07715165] nice Side A — unified diff (full patch): diff --git a/server/src/api/ui_html.rs b/server/src/api/ui_html.rs index c3c62a76f424010d77a6090c84dd0b82098f573e..da2536112faea313352624cf2ce0ddd0ab3377c1 100644 --- a/server/src/api/ui_html.rs +++ b/server/src/api/ui_html.rs @@ -6,7 +6,7 @@ use axum::{ use std::collections::HashMap; use crate::{ - html::{demo_counter_panel, js_string_literal, ranking_panel, JsBuilder}, + html::{js_string_literal, ranking_panel, JsBuilder}, parser::parse_reddit_url, parser_render::parser_panel_morph, state::AppState, @@ -35,13 +35,6 @@ pub async fn post_ui_html( }; match action { - HtmlUiAction::BumpDemoCounter => { - let count = state.bump_demo_counter().await; - let panel = demo_counter_panel(count, state.event_log.path().to_string_lossy().as_ref()); - JsBuilder::new() - .morph_selector("#demo-counter-panel", panel) - .into_response() - } HtmlUiAction::RecordVote { a, b, @@ -54,8 +47,8 @@ pub async fn post_ui_html( { return ui_js_warn(&e).into_response(); } - let mut group = state.group.write().await; - let panel = ranking_panel(&mut group); + let group = state.group.read().await; + let panel = ranking_panel(&group); JsBuilder::new() .morph_selector("#ranking-panel", panel) .into_response() @@ -87,20 +80,6 @@ mod tests { assert!(matches!(err, HtmlUiParseError::MissingRpc)); } - #[test] - fn bump_action_deserializes() { - let template = serde_json::json!({ "action": "bump_demo_counter" }); - let mut form = HashMap::new(); - form.insert( - UI_RPC_FIELD.to_string(), - serde_json::to_string(&template).unwrap(), - ); - assert_eq!( - parse_html_ui_from_form(&form).unwrap(), - HtmlUiAction::BumpDemoCounter - ); - } - #[test] fn record_vote_action_deserializes() { let template = serde_json::json!({ diff --git a/server/src/events.rs b/server/src/events.rs index b969242534e184d4f0a689543a479670b08a18df..eff80aef0257f706d2341f666e63d6a3d921bf6e 100644 --- a/server/src/events.rs +++ b/server/src/events.rs @@ -5,8 +5,6 @@ use serde::{Deserialize, Serialize}; pub enum Event { /// Page view recorded (path → counter in views.json). ViewRecorded { path: String, ts: i64 }, - /// Demo counter bump from `POST /ui` (persisted in the single JSONL log). - DemoCounterBumped { ts: i64, value: u64 }, /// Pairwise comparison vote (replayed into [`crate::reducer::GroupState`] on boot). VoteRecorded { ts: i64, diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index 5b1d0b5a887d89e7e80796aa7a6c8ed5baaf2782..d69ed962b5c8625bc83c933b1825f2cc1d0868e2 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -13,7 +13,7 @@ use crate::{ form_template::template_json_compact, parser_action::ParserAction, parser_render::parser_panel, - ranking::ranked_items, + ranking::ranked_items_cached, reducer::GroupState, state::AppState, ui_action::UI_RPC_FIELD, @@ -199,10 +199,8 @@ fn layout(title: &str, body: Markup, views: u64, theme: &str, theme_next: &str) } } -pub fn ranking_panel(group: &mut GroupState) -> Markup { - const MAX_ITERS: usize = 10_000; - const TOL: f64 = 1e-8; - let items = ranked_items(group, MAX_ITERS, TOL); +pub fn ranking_panel(group: &GroupState) -> Markup { + let items = ranked_items_cached(group); html! { section id="ranking-panel" class="demo-panel" { h2 { "Ranking" } @@ -260,35 +258,6 @@ pub fn vote_panel() -> Markup { } -pub fn demo_counter_panel(count: u64, event_log_path: &str) -> Markup { - let rpc = template_json_compact(&serde_json::json!({ "action": "bump_demo_counter" })) - .expect("rpc json"); - html! { - section id="demo-counter-panel" class="demo-panel" { - h1 { "sorter2" } - p class="muted" { - "Pairwise ranking scaffold — votes persist to JSONL and replay on boot." - } - p class="demo-count" { - strong { "Counter: " } - span id="demo-count-value" { (count) } - } - p class="muted small" { - "Event log: " code { (event_log_path) } - } - form method="post" action="/ui" id="demo-bump-form" { - input type="hidden" name=(UI_RPC_FIELD) value=(rpc); - button type="submit" class="btn-primary" { "Bump (POST /ui → eval JS)" } - } - p class="muted small" { - "Uses hidden " - code { "__rpc__" } - " JSON + Idiomorph morph — no full page reload." - } - } - } -} - pub async fn home( State(state): State, jar: CookieJar, @@ -297,16 +266,15 @@ pub async fn home( let path = uri.path().to_string(); state.views.increment(path.clone()); let views = state.views.get_views(&path); - let count = *state.demo_counter.read().await; let theme = theme_from_jar(&jar); let theme_next = theme_next_from_uri(&uri); - let mut group = state.group.write().await; + let group = state.group.read().await; let empty_action = ParserAction::suggest(String::new(), None); let body = html! { + h1 { "sorter2" } (parser_panel("", &empty_action)) (vote_panel()) - (ranking_panel(&mut group)) - (demo_counter_panel(count, state.event_log.path().to_string_lossy().as_ref())) + (ranking_panel(&group)) }; layout("sorter2", body, views, theme, &theme_next) } diff --git a/server/src/lib.rs b/server/src/lib.rs index 6716c5b282e7980a7a0f03d63ad8b25eda61cc55..fa423640d598f4ba97a5885d228e78d7b97f7a22 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -9,6 +9,7 @@ pub mod parser_render; pub mod path_types; pub mod ranking; pub mod reducer; +pub mod settlement; pub mod state; pub mod ui_action; pub mod views; diff --git a/server/src/ranking.rs b/server/src/ranking.rs index 89d3280126a8d8f841721ce8cb63ff735d68752a..2d706762792ba9239bb3f1c2e4974a2fde908013 100644 --- a/server/src/ranking.rs +++ b/server/src/ranking.rs @@ -91,6 +91,11 @@ pub fn compute_group_ranking(group: &mut GroupState, max_iters: usize, tol: f64) pub fn ranked_items(group: &mut GroupState, max_iters: usize, tol: f64) -> Vec { compute_group_ranking(group, max_iters, tol); + ranked_items_cached(group) +} + +/// Read cached scores without recomputing (HTTP fast path). +pub fn ranked_items_cached(group: &GroupState) -> Vec { let mut items: Vec = group .idx_to_item .iter() @@ -105,7 +110,12 @@ pub fn ranked_items(group: &mut GroupState, max_iters: usize, tol: f64) -> Vec, max_iters: usize, tol: f64) -> Vec { +pub fn compute_scores_from_edges( + n: usize, + edges: impl Iterator, + max_iters: usize, + tol: f64, +) -> Vec { if n == 0 { return vec![]; } diff --git a/server/src/settlement.rs b/server/src/settlement.rs new file mode 100644 index 0000000000000000000000000000000000000000..1f722ceaea62cda22c28ab71551f259fbf049b81 --- /dev/null +++ b/server/src/settlement.rs @@ -0,0 +1,114 @@ +use std::sync::Arc; + +use tokio::sync::{mpsc, oneshot, RwLock}; + +use crate::{ + event_log::EventLog, + events::Event, + ranking::compute_scores_from_edges, + reducer::{GroupState, VoteData}, +}; + +const MAX_ITERS: usize = 10_000; +const TOL: f64 = 1e-8; + +pub struct SettlementCommand { + pub vote: VoteData, + pub event: Event, + pub reply: oneshot::Sender>, +} + +#[derive(Clone)] +pub struct SettlementClient { + tx: mpsc::Sender, +} + +impl SettlementClient { + pub fn spawn(group: Arc>, event_log: Arc) -> Self { + let (tx, rx) = mpsc::channel(64); + tokio::spawn(settlement_worker(rx, group, event_log)); + Self { tx } + } + + pub async fn record_vote(&self, vote: VoteData, event: Event) -> Result<(), String> { + let (reply, rx) = oneshot::channel(); + self.tx + .send(SettlementCommand { + vote, + event, + reply, + }) + .await + .map_err(|_| "settlement worker stopped".to_string())?; + rx.await + .map_err(|_| "settlement worker stopped".to_string())? + } +} + +async fn settlement_worker( + mut rx: mpsc::Receiver, + group: Arc>, + event_log: Arc, +) { + while let Some(first) = rx.recv().await { + let mut batch = vec![first]; + while let Ok(more) = rx.try_recv() { + batch.push(more); + } + + let mut disk_err: Option = None; + for cmd in &batch { + if let Err(e) = event_log.append(&cmd.event).await { + disk_err = Some(e.to_string()); + break; + } + } + + if let Some(err) = disk_err { + for cmd in batch { + let _ = cmd.reply.send(Err(err.clone())); + } + continue; + } + + let (edges, n) = { + let mut w = group.write().await; + for cmd in &batch { + w.apply_vote(cmd.vote.clone()); + } + (w.edges.clone(), w.idx_to_item.len()) + }; + + let new_scores = compute_scores_from_edges( + n, + edges.iter().map(|(&k, &v)| (k, v)), + MAX_ITERS, + TOL, + ); + + { + let mut w = group.write().await; + w.cached_scores = new_scores; + w.dirty = false; + } + + for cmd in batch { + let _ = cmd.reply.send(Ok(())); + } + } +} + +/// Compute ranking cache from current in-memory edges (startup replay only). +pub fn warm_ranking_cache(group: &mut GroupState) { + if !group.dirty { + return; + } + let n = group.idx_to_item.len(); + group.cached_scores = compute_scores_from_edges( + n, + group.edges.iter().map(|(&k, &v)| (k, v)), + MAX_ITERS, + TOL, + ); + group.dirty = false; +} diff --git a/server/src/state.rs b/server/src/state.rs index 8ec9902e2ecc31cf8208f7ad6365891dc5537eed..1922541a4064c2de1df2d993a461cae783320e05 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -6,6 +6,7 @@ use crate::{ event_log::EventLog, events::Event, reducer::{GroupState, VoteData}, + settlement::{warm_ranking_cache, SettlementClient}, views::ViewStore, }; @@ -38,8 +39,8 @@ pub struct AppState { pub cfg: Arc, pub event_log: Arc, pub views: ViewStore, - pub demo_counter: Arc>, pub group: Arc>, + settlement: SettlementClient, } impl AppState { @@ -48,14 +49,10 @@ impl AppState { let views_path = format!("{}/views.json", cfg.data_dir); let views = ViewStore::new(&views_path); - let mut demo_counter: u64 = 0; let mut group = GroupState::new(); if let Ok((events, _)) = event_log.load_all().await { for ev in events { match ev { - Event::DemoCounterBumped { value, .. } => { - demo_counter = demo_counter.max(value); - } Event::VoteRecorded { ts, a, @@ -74,30 +71,20 @@ impl AppState { } } + warm_ranking_cache(&mut group); + + let group = Arc::new(RwLock::new(group)); + let settlement = SettlementClient::spawn(group.clone(), event_log.clone()); + Self { cfg: Arc::new(cfg), event_log, views, - demo_counter: Arc::new(RwLock::new(demo_counter)), - group: Arc::new(RwLock::new(group)), + group, + settlement, } } - pub async fn bump_demo_counter(&self) -> u64 { - let mut guard = self.demo_counter.write().await; - *guard += 1; - let value = *guard; - drop(guard); - - let ts = crate::html::now_ms(); - let _ = self - .event_log - .append(&Event::DemoCounterBumped { ts, value }) - .await; - - value - } - pub async fn record_vote( &self, a: &str, @@ -109,23 +96,14 @@ impl AppState { let vote = VoteData::from_recorded(ts, a, b, ratio_left, ratio_right) .ok_or_else(|| "invalid vote: need two distinct non-empty items".to_string())?; - { - let mut group = self.group.write().await; - group.apply_vote(vote.clone()); - } - - let _ = self - .event_log - .append(&Event::VoteRecorded { - ts, - a: vote.a.as_str().to_string(), - b: vote.b.as_str().to_string(), - ratio_left: vote.ratio_left, - ratio_right: vote.ratio_right, - }) - .await - .map_err(|e| e.to_string())?; + let event = Event::VoteRecorded { + ts, + a: vote.a.as_str().to_string(), + b: vote.b.as_str().to_string(), + ratio_left: vote.ratio_left, + ratio_right: vote.ratio_right, + }; - Ok(()) + self.settlement.record_vote(vote, event).await } } diff --git a/server/src/ui_action.rs b/server/src/ui_action.rs index 1d488e1ad1d8fd81bd9d016d69255adfe9b22fc8..5d94c84113607b3bcd8d31d03ef5b8e1b87b67a7 100644 --- a/server/src/ui_action.rs +++ b/server/src/ui_action.rs @@ -12,8 +12,6 @@ pub const UI_RPC_FIELD: &str = "__rpc__"; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] #[serde(tag = "action", rename_all = "snake_case")] pub enum HtmlUiAction { - /// Demo: morph `#demo-counter-panel` after bumping the persisted counter. - BumpDemoCounter, /// Record a pairwise vote and morph `#ranking-panel`. RecordVote { a: String, @@ -52,18 +50,6 @@ pub fn parse_html_ui_from_form( mod tests { use super::*; - #[test] - fn bump_demo_counter_round_trip() { - let template = serde_json::json!({ "action": "bump_demo_counter" }); - let mut form = HashMap::new(); - form.insert( - UI_RPC_FIELD.to_string(), - serde_json::to_string(&template).unwrap(), - ); - let a = parse_html_ui_from_form(&form).unwrap(); - assert_eq!(a, HtmlUiAction::BumpDemoCounter); - } - #[test] fn record_vote_round_trip_with_form_holes() { let template = serde_json::json!({ diff --git a/server/tests/integration_health.rs b/server/tests/integration_health.rs index 8a58bc14aac68c779e7ce4ffa57d27b3220e9953..1d816e008b9bf629967e3b3c786c41afefc284e3 100644 --- a/server/tests/integration_health.rs +++ b/server/tests/integration_health.rs @@ -38,7 +38,7 @@ async fn healthz_ok() { } #[tokio::test] -async fn home_has_demo_panel() { +async fn home_has_main_panels() { let (addr, _tmp) = start_test_server().await; let client = reqwest::Client::new(); let html = client diff --git a/server/tests/integration_ui.rs b/server/tests/integration_ui.rs index bee79407dec120699a083f1a75adf9291e207fc7..7ede46d681fefbe25b1d98fc33520a185515eb1c 100644 --- a/server/tests/integration_ui.rs +++ b/server/tests/integration_ui.rs @@ -25,33 +25,6 @@ async fn start_test_server() -> (SocketAddr, TempDir) { (addr, tmp) } -#[tokio::test] -async fn post_ui_bump_returns_javascript_morph() { - let (addr, tmp) = start_test_server().await; - let rpc = serde_json::json!({ "action": "bump_demo_counter" }).to_string(); - let mut form = HashMap::new(); - form.insert(UI_RPC_FIELD.to_string(), rpc); - - let client = reqwest::Client::new(); - let body = client - .post(format!("http://{addr}/ui")) - .form(&form) - .send() - .await - .unwrap() - .text() - .await - .unwrap(); - - assert!(body.contains("Idiomorph.morph")); - assert!(body.contains("demo-counter-panel")); - assert!(body.contains("Counter:")); - - let log_path = tmp.path().join("events.jsonl"); - let log = std::fs::read_to_string(log_path).unwrap(); - assert!(log.contains("demo_counter_bumped")); -} - #[tokio::test] async fn post_ui_record_vote_morphs_ranking_and_persists() { let (addr, tmp) = start_test_server().await; @@ -90,8 +63,8 @@ async fn post_ui_record_vote_morphs_ranking_and_persists() { port: 0, }; let state = create_app_state(cfg).await; - let mut group = state.group.write().await; - let ranked = sorter2_server::ranking::ranked_items(&mut group, 10_000, 1e-8); + let group = state.group.read().await; + let ranked = sorter2_server::ranking::ranked_items_cached(&group); assert_eq!(ranked.len(), 2); assert_eq!(ranked[0].item.as_str(), "alpha"); } diff --git a/test/smoke.clj b/test/smoke.clj index b332324743b58baf3fe96d7477353749f137feae..11887c48282088e140d823a88ba616f6325835b3 100644 --- a/test/smoke.clj +++ b/test/smoke.clj @@ -26,7 +26,7 @@ false)))))) (deftest http-smoke-against-running-server - (testing "build, start, healthz, home contains RPC demo" + (testing "build, start, healthz, home contains main panels" (let [root (repo-root) data-dir (.getAbsolutePath (doto (io/file (System/getProperty "java.io.tmpdir") (str "sorter2-smoke-" (System/currentTimeMillis))) Side B — contributor: tommy-mor Side B — commit message: [08565bea] Tokenize prose refs for garden URL links (#147) * Tokenize prose refs for garden URL links Co-authored-by: tommy * Stop prose URLs at line boundaries Co-authored-by: tommy * Require braced DSL item bodies Co-authored-by: tommy --------- Co-authored-by: Cursor Agent Side B — unified diff (full patch): diff --git a/server/src/dsl.rs b/server/src/dsl.rs index 4203c2f59dd8825d7a91513c4023f3ccd37f1efc..b3c785674560a0b42a7f4c3aa13d80cd350f485b 100644 --- a/server/src/dsl.rs +++ b/server/src/dsl.rs @@ -1,7 +1,5 @@ use std::collections::HashMap; -use rand::Rng; - /// Parsed DSL document. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Document { @@ -39,31 +37,57 @@ pub enum DslError { /// Matches the legacy Python parser behavior: /// - Supports toggle markers (open == close), e.g. ```...``` /// - Supports nested markers (open != close), e.g. { ... { ... } ... } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BlockKind { + CodeFence, + DoubleBrace, + Brace, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct MaskedBlock { + kind: BlockKind, +} + #[derive(Debug, Default, Clone)] pub struct BlockMasker { pub replacements: HashMap, + blocks: HashMap, + next_id: u32, } impl BlockMasker { pub fn new() -> Self { Self { replacements: HashMap::new(), + blocks: HashMap::new(), + next_id: 0, } } - fn new_token(&mut self) -> String { - let mut rng = rand::thread_rng(); - let n: u32 = rng.gen(); - let token = format!("__BLOCK_{:08x}__", n); - // Extremely unlikely collision; if it happens, regenerate. - if self.replacements.contains_key(&token) { - return self.new_token(); + fn new_token(&mut self, haystack: &str) -> String { + loop { + let token = format!("__BLOCK_{:08x}__", self.next_id); + self.next_id = self.next_id.wrapping_add(1); + if !self.replacements.contains_key(&token) && !haystack.contains(&token) { + return token; + } } - token } /// Replace outermost balanced blocks with tokens. pub fn mask(&mut self, text: &str, open_marker: &str, close_marker: &str) -> String { + self.mask_kind(text, open_marker, close_marker, BlockKind::Brace) + } + + /// Replace outermost balanced blocks with typed deterministic tokens. + pub fn mask_kind( + &mut self, + text: &str, + open_marker: &str, + close_marker: &str, + kind: BlockKind, + ) -> String { if text.is_empty() { return text.to_string(); } @@ -97,9 +121,10 @@ impl BlockMasker { // Found end of outermost block let s = start_idx.max(0) as usize; let original_block = &text[s..i]; - let token = self.new_token(); + let token = self.new_token(text); self.replacements .insert(token.clone(), original_block.to_string()); + self.blocks.insert(token.clone(), MaskedBlock { kind }); result_parts.push(token); current_idx = i; } @@ -176,13 +201,22 @@ impl BlockMasker { } token.to_string() } + + pub fn block_kind(&self, token: &str) -> Option { + self.blocks.get(token).map(|b| b.kind) + } } fn mask_all(mut masker: BlockMasker, text: &str) -> (BlockMasker, String) { // Mask hierarchy: Code -> Double Brace -> Single Brace. - let t = masker.mask(text, "```", "```"); - let t = masker.mask(&t, "{{", "}}"); - let t = masker.mask(&t, "{", "}"); + let t = masker.mask_kind(text, "```", "```", BlockKind::CodeFence); + let t = masker.mask_kind(&t, "{{", "}}", BlockKind::DoubleBrace); + let t = masker.mask_kind(&t, "{", "}", BlockKind::Brace); + (masker, t) +} + +fn mask_code_fences(mut masker: BlockMasker, text: &str) -> (BlockMasker, String) { + let t = masker.mask_kind(text, "```", "```", BlockKind::CodeFence); (masker, t) } @@ -253,7 +287,34 @@ fn skip_ws(s: &str, mut i: usize) -> usize { i } -fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum ProseToken { + Text(String), + ItemRef(String), +} + +fn trim_prose_item_ref_end(s: &str, mut end: usize) -> usize { + while end > 0 { + let Some((idx, c)) = s[..end].char_indices().next_back() else { + break; + }; + if matches!( + c, + '.' | ',' | ';' | ':' | '!' | '?' | ')' | ']' | '}' | '"' | '\'' + ) { + end = idx; + } else { + break; + } + } + end +} + +fn parse_item_name_at_with_mode( + s: &str, + i: usize, + trim_trailing_punctuation: bool, +) -> Option<(String, usize)> { let bytes = s.as_bytes(); if i >= bytes.len() { return None; @@ -263,6 +324,9 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if s[i..].starts_with("https://") || s[i..].starts_with("http://") { let mut j = i; while j < bytes.len() { + if trim_trailing_punctuation && bytes[j] == b'\n' { + break; + } if bytes[j..].starts_with(b"__BLOCK_") || is_ws_byte(bytes[j]) { break; } @@ -271,6 +335,12 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if j <= i { return None; } + if trim_trailing_punctuation { + j = trim_prose_item_ref_end(s, j); + if j <= i { + return None; + } + } return Some((s[i..j].to_string(), j)); } @@ -296,6 +366,12 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { if j <= i + 2 { return None; } + if trim_trailing_punctuation { + j = trim_prose_item_ref_end(s, j); + if j <= i + 2 { + return None; + } + } let raw = &s[i..j]; if !is_item_name(raw) { return None; @@ -336,6 +412,46 @@ fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { Some((format!("~/{}", name), j)) } +fn parse_item_name_at(s: &str, i: usize) -> Option<(String, usize)> { + parse_item_name_at_with_mode(s, i, false) +} + +pub fn parse_prose_item_ref_at(s: &str, i: usize) -> Option<(String, usize)> { + parse_item_name_at_with_mode(s, i, true) +} + +pub fn tokenize_prose_item_refs(text: &str) -> Vec { + if text.is_empty() { + return Vec::new(); + } + let (masker, masked) = mask_code_fences(BlockMasker::new(), text); + let mut tokens = Vec::new(); + let mut text_start = 0usize; + let mut i = 0usize; + + while i < masked.len() { + if let Some((raw, end)) = parse_prose_item_ref_at(&masked, i) { + if text_start < i { + tokens.push(ProseToken::Text(masker.unmask(&masked[text_start..i]))); + } + tokens.push(ProseToken::ItemRef(masker.unmask(&raw))); + i = end; + text_start = i; + continue; + } + + let Some((_, c)) = masked[i..].char_indices().next() else { + break; + }; + i += c.len_utf8(); + } + + if text_start < masked.len() { + tokens.push(ProseToken::Text(masker.unmask(&masked[text_start..]))); + } + tokens +} + fn parse_block_token_at(s: &str, i: usize) -> Option<(String, usize)> { let bytes = s.as_bytes(); if i >= bytes.len() { @@ -401,6 +517,12 @@ fn parse_block_prefixed_statement( tail: &str, masker: &BlockMasker, ) -> Result { + if masker.block_kind(block_token) == Some(BlockKind::CodeFence) { + return Err(DslError::Parse( + "vote explanations must use `{ ... }`; code fences belong inside body blocks" + .to_string(), + )); + } // vote: block item_ref comparison item_ref let s = tail.trim_start(); if s.is_empty() { @@ -462,6 +584,11 @@ fn parse_item_definition_statement(stripped: &str, masker: &BlockMasker) -> Resu } if let Some((tok, end)) = parse_block_token_at(stripped, i) { + if masker.block_kind(&tok) == Some(BlockKind::CodeFence) { + return Err(DslError::Parse( + "item bodies must use `{ ... }`; code fences belong inside body blocks".to_string(), + )); + } let body = masker.extract_body(&tok); let tail = stripped[end..].trim(); if !tail.is_empty() { @@ -572,6 +699,10 @@ pub fn parse_full(text: &str) -> Result { if let Some((tok, end)) = parse_block_token_at(stripped, 0) { if stripped[end..].trim().is_empty() { + if masker.block_kind(&tok) == Some(BlockKind::CodeFence) { + prose_buffer.push(line); + continue; + } pending_block = Some(tok); continue; } @@ -619,6 +750,70 @@ mod tests { assert_eq!(roundtrip, input); } + #[test] + fn blockmasker_tokens_are_deterministic_and_typed() { + let input = "x ```code``` y {body}"; + let (masker, masked) = mask_all(BlockMasker::new(), input); + assert!(masked.contains("__BLOCK_00000000__")); + assert!(masked.contains("__BLOCK_00000001__")); + assert_eq!( + masker.block_kind("__BLOCK_00000000__"), + Some(BlockKind::CodeFence) + ); + assert_eq!( + masker.block_kind("__BLOCK_00000001__"), + Some(BlockKind::Brace) + ); + assert_eq!(masker.unmask(&masked), input); + } + + #[test] + fn prose_tokenizer_finds_tilde_dash_and_raw_url_refs() { + let tokens = + tokenize_prose_item_refs("see ~/a/b then -/example.com/x and https://Example.com/A/B."); + assert_eq!( + tokens, + vec![ + ProseToken::Text("see ".to_string()), + ProseToken::ItemRef("~/a/b".to_string()), + ProseToken::Text(" then ".to_string()), + ProseToken::ItemRef("-/example.com/x".to_string()), + ProseToken::Text(" and ".to_string()), + ProseToken::ItemRef("https://Example.com/A/B".to_string()), + ProseToken::Text(".".to_string()), + ] + ); + } + + #[test] + fn prose_tokenizer_stops_raw_urls_at_newlines() { + let tokens = tokenize_prose_item_refs("https://example.com/a/b.\n-/example.com/a/b"); + assert_eq!( + tokens, + vec![ + ProseToken::ItemRef("https://example.com/a/b".to_string()), + ProseToken::Text(".\n".to_string()), + ProseToken::ItemRef("-/example.com/a/b".to_string()), + ] + ); + } + + #[test] + fn prose_tokenizer_does_not_linkify_inside_code_fences() { + let tokens = tokenize_prose_item_refs( + "before ```json\n{\"url\":\"https://example.com\"}\n``` after ~/x", + ); + assert_eq!( + tokens, + vec![ + ProseToken::Text( + "before ```json\n{\"url\":\"https://example.com\"}\n``` after ".to_string() + ), + ProseToken::ItemRef("~/x".to_string()), + ] + ); + } + #[test] fn parse_item_with_body_strips_outer_braces() { let input = "~/rust { Systems language }"; @@ -633,8 +828,8 @@ mod tests { } #[test] - fn parse_item_with_fenced_json_body_preserves_braces() { - let input = "~/item/in/url ```json\n{\"test\": true}\n```"; + fn parse_item_with_braced_fenced_json_body_preserves_braces() { + let input = "~/item/in/url {\n```json\n{\"test\": true}\n```\n}"; let doc = parse_full(input).unwrap(); assert_eq!( doc.statements, @@ -645,6 +840,51 @@ mod tests { ); } + #[test] + fn parse_rejects_singleton_fenced_json_item_body() { + let input = "~/item/in/url ```json\n{\"test\": true}\n```"; + let err = parse_full(input).unwrap_err().to_string(); + assert!( + err.contains("item bodies must use"), + "unexpected error: {err}" + ); + } + + #[test] + fn parse_keeps_standalone_code_fence_as_prose() { + let input = "```json\n{\"test\": true}\n```"; + let doc = parse_full(input).unwrap(); + assert_eq!( + doc.statements, + vec![Stmt::Prose { + text: input.to_string(), + }] + ); + } + + #[test] + fn parse_rejects_code_fence_vote_explanation() { + let input = "```json\n{\"why\": true}\n```\n~/a 2:1 ~/b"; + let err = parse_full(input).unwrap_err().to_string(); + assert!( + err.contains("vote explanations must start"), + "unexpected error: {err}" + ); + } + + #[test] + fn parse_raw_url_item_with_braced_fenced_json_body() { + let input = "https://example.com/itembody/slug {\n```json\n{\"test\": true}\n```\n}"; + let doc = parse_full(input).unwrap(); + assert_eq!( + doc.statements, + vec![Stmt::Item { + title: "https://example.com/itembody/slug".to_string(), + body: Some("```json\n{\"test\": true}\n```".to_string()), + }] + ); + } + #[test] fn parse_vote_ratio_and_symbols() { let d1 = parse_full("{because}\n~/a 3:1 ~/b").unwrap(); diff --git a/server/src/external_resolver.rs b/server/src/external_resolver.rs index 8481f1fbff1c4265da182a225f539f97d6829002..04bc3de32b6df1aefe197d942db67de2cbdbde4f 100644 --- a/server/src/external_resolver.rs +++ b/server/src/external_resolver.rs @@ -256,7 +256,7 @@ fn children_to_dsl(children: &[ResolvedChild]) -> String { .filter(|s| !s.trim().is_empty()) .unwrap_or(child.title.as_str()); if body.trim_start().starts_with("```") { - out.push_str(&format!("{} {}\n\n", child.url, body.trim())); + out.push_str(&format!("{} {{\n{}\n}}\n\n", child.url, body.trim())); } else { out.push_str(&format!( "{} {{\n{}\n}}\n\n", @@ -378,7 +378,8 @@ mod tests { title: "#1 title".into(), body: Some("```json\n{\"test\": true}\n```".into()), }]); - assert!(dsl.contains("https://github.com/o/r/issues/1 ```json")); + assert!(dsl.contains("https://github.com/o/r/issues/1 {\n```json")); assert!(dsl.contains("{\"test\": true}")); + assert!(dsl.contains("```\n}\n")); } } diff --git a/server/src/html/breadcrumb_path.rs b/server/src/html/breadcrumb_path.rs index 3e949ce2cb70de3f024f46af773a93f3f8852260..e48612d7d3c4df52c161499a11865d4caca607aa 100644 --- a/server/src/html/breadcrumb_path.rs +++ b/server/src/html/breadcrumb_path.rs @@ -88,16 +88,12 @@ impl ExternalOntologyPath { .filter(|x| !x.is_empty()) .map(|x| x.to_string()) .collect(); - let segments = if segments == ["."] { - vec![] - } else { - segments - }; + let segments = if segments == ["."] { vec![] } else { segments }; Self { item, segments } } pub(super) fn is_root(&self) -> bool { - self.segments.len() <= 1 + self.segments.is_empty() } pub(super) fn segments(&self) -> &[String] { @@ -108,3 +104,28 @@ impl ExternalOntologyPath { self.item.as_str() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn external_root_and_host_paths_are_distinct() { + let root = ExternalOntologyPath::from_input(""); + assert!(root.is_root()); + assert!(root.segments().is_empty()); + + let host = ExternalOntologyPath::from_input("example.com"); + assert!(!host.is_root()); + assert_eq!(host.segments(), &["example.com".to_string()]); + } + + #[test] + fn external_path_keeps_each_url_segment_for_breadcrumbs() { + let path = ExternalOntologyPath::from_input("https://example.com/a/b"); + assert_eq!( + path.segments(), + &["example.com".to_string(), "a".to_string(), "b".to_string()] + ); + } +} diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index 8876a1a00a16ee3bf7a371f7a5ca27b5862b62cc..d5877eeb3632ed5d770f23a04cb4d1881209bc74 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -20,27 +20,30 @@ mod search; pub mod ui_action; use breadcrumb_path::{ExternalOntologyPath, OntologyPath}; -pub use auth::{auth_complete_page, auth_signed_in_fragment, choose_username_error_fragment, choose_username_page}; +pub use auth::{ + auth_complete_page, auth_signed_in_fragment, choose_username_error_fragment, + choose_username_page, +}; pub use editor::{editor_check, editor_page}; pub use forum::{ home, room_page, room_thread_post_view, room_thread_view, thread_feed_html, thread_feed_html_for_room, thread_feed_region_markup, thread_post_view, thread_view, ThreadNav, }; +pub use forum::user_profile_page; pub(crate) use forum::{ fragment_new_thread_slot, login_to_post_hint_markup, room_members_section_markup, thread_ui_collapse_redacted_post, thread_ui_expand_post_full, thread_ui_expand_redacted_post, user_can_post_room, user_can_view_room, }; +pub(crate) use garden::{encode_pin_cookie_value, vote_compare_post_success_js, GARDEN_PIN_COOKIE}; pub use garden::{ external_garden_index, external_ontology_path, garden_index, ontology_path, - room_external_garden_index, room_external_ontology_path, room_garden_index, - room_ontology_path, room_vote_compare_page, vote_compare_page, + room_external_garden_index, room_external_ontology_path, room_garden_index, room_ontology_path, + room_vote_compare_page, vote_compare_page, }; -pub(crate) use garden::{encode_pin_cookie_value, vote_compare_post_success_js, GARDEN_PIN_COOKIE}; pub use routing::RouteContext; pub use search::{search_page, search_results_fragment}; -pub use forum::user_profile_page; pub use ui_action::{parse_html_ui_from_form, HtmlUiAction, HtmlUiParseError, UI_RPC_FIELD}; /// Public profile URL path for a stored username (no `@`). @@ -107,8 +110,8 @@ pub struct ThemeForm { pub async fn post_theme(Form(form): Form) -> impl IntoResponse { let theme = normalize_theme(&form.theme); let next = sanitize_theme_next(form.next.as_deref()); - let loc = HeaderValue::try_from(next.as_str()) - .unwrap_or_else(|_| HeaderValue::from_static("/")); + let loc = + HeaderValue::try_from(next.as_str()).unwrap_or_else(|_| HeaderValue::from_static("/")); Response::builder() .status(StatusCode::SEE_OTHER) .header(header::LOCATION, loc) @@ -179,7 +182,9 @@ pub(crate) struct JsQueryBuilder { impl JsBuilder { pub(crate) fn new() -> Self { - Self { snippets: Vec::new() } + Self { + snippets: Vec::new(), + } } pub(crate) fn morph_selector(self, selector: &str, markup: Markup) -> Self { @@ -195,7 +200,12 @@ impl JsBuilder { self.qs(selector).morph_inner(markup) } - pub(crate) fn morph_expr(mut self, expr: &str, markup: Markup, morph_style: Option<&str>) -> Self { + pub(crate) fn morph_expr( + mut self, + expr: &str, + markup: Markup, + morph_style: Option<&str>, + ) -> Self { let html = js_string_literal(&markup.into_string()); let opts = morph_style .map(|style| format!(", {{morphStyle: {}}}", js_string_literal(style))) @@ -217,7 +227,11 @@ impl JsBuilder { self.qs(&format!("#{id}")) } - pub(crate) fn if_current_path_matches(mut self, path: &str, f: impl FnOnce(JsBuilder) -> JsBuilder) -> Self { + pub(crate) fn if_current_path_matches( + mut self, + path: &str, + f: impl FnOnce(JsBuilder) -> JsBuilder, + ) -> Self { let inner = f(JsBuilder::new()).build(); self.snippets.push(format!( "var __slugHere = window.location.pathname + window.location.search; var __slugPath = {path}; if (__slugHere === __slugPath || __slugHere.indexOf(__slugPath + '?') === 0) {{ {inner} }}", @@ -226,7 +240,11 @@ impl JsBuilder { self } - pub(crate) fn if_current_path_not_matches(mut self, path: &str, f: impl FnOnce(JsBuilder) -> JsBuilder) -> Self { + pub(crate) fn if_current_path_not_matches( + mut self, + path: &str, + f: impl FnOnce(JsBuilder) -> JsBuilder, + ) -> Self { let inner = f(JsBuilder::new()).build(); self.snippets.push(format!( "var __slugHere = window.location.pathname + window.location.search; var __slugPath = {path}; if (!(__slugHere === __slugPath || __slugHere.indexOf(__slugPath + '?') === 0)) {{ {inner} }}", @@ -301,7 +319,17 @@ pub(super) fn layout( garden_room_wire: Option<&str>, garden_path_prefix: Option<&str>, ) -> Markup { - layout_embed_controls(title, view, body, views, theme, theme_next, garden_room_wire, garden_path_prefix, true) + layout_embed_controls( + title, + view, + body, + views, + theme, + theme_next, + garden_room_wire, + garden_path_prefix, + true, + ) } /// Minimal document shell: no bottom controls, no garden HUD data attributes (`data-garden-room` / @@ -315,15 +343,7 @@ pub(super) fn layout_full_bleed_chromeless( theme_next: &str, ) -> Markup { layout_embed_controls( - title, - view, - body, - views, - theme, - theme_next, - None, - None, - false, + title, view, body, views, theme, theme_next, None, None, false, ) } @@ -545,7 +565,58 @@ fn item_body_title_snippet(body: &str) -> Option { Some(format!("{truncated}{ellipsis}")) } -/// Replace ~/path slugs in raw text with clickable links. +fn garden_href_for_item_ref( + raw_ref: &str, + garden_prefix: &str, +) -> Option<(crate::path_types::ItemId, String)> { + let key = slug_types::canonicalize_item(raw_ref); + let id = crate::path_types::ItemId::parse(&key)?; + let href = if let Some(tail) = id.tilde_tail() { + if tail.is_empty() { + garden_prefix.trim_end_matches('/').to_string() + } else { + format!("{}/{}", garden_prefix.trim_end_matches('/'), tail) + } + } else if id.as_str().starts_with("https://") || id.as_str().starts_with("http://") { + let display = id.display_path(); + let rest = display.strip_prefix("-/").unwrap_or(display.as_str()); + let ext_prefix = format!("{}-", garden_prefix.trim_end_matches('~')); + format!("{}/{}", ext_prefix, rest) + } else { + return None; + }; + Some((id, href)) +} + +fn push_item_ref_anchor( + out: &mut String, + raw_ref: &str, + garden_prefix: &str, + item_bodies: Option<&HashMap>, +) -> bool { + let Some((id, href)) = garden_href_for_item_ref(raw_ref, garden_prefix) else { + return false; + }; + out.push_str(r#"'); + out.push_str(&escape_html(raw_ref)); + out.push_str(""); + true +} + +/// Replace item refs in raw prose with clickable garden links. /// /// When `item_bodies` is set, matching ontology items get a `title` attribute with a truncated /// body preview for native browser tooltips (forum posts, item pages). @@ -554,53 +625,16 @@ pub(super) fn linkify_slugs_with_prefix( garden_prefix: &str, item_bodies: Option<&HashMap>, ) -> String { - let escaped = escape_html(raw); - let mut out = String::with_capacity(escaped.len() + 64); - let mut i = 0; - let s = escaped.as_str(); - while i < s.len() { - let rest = &s[i..]; - if let Some(after_tilde) = rest.strip_prefix("~/") { - let path_len = after_tilde - .chars() - .take_while(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == '-' || *c == '/') - .map(|c| c.len_utf8()) - .sum::(); - if path_len > 0 { - let path = &after_tilde[..path_len]; - out.push_str(r#" out.push_str(&escape_html(&text)), + crate::dsl::ProseToken::ItemRef(raw_ref) => { + if !push_item_ref_anchor(&mut out, &raw_ref, garden_prefix, item_bodies) { + out.push_str(&escape_html(&raw_ref)); } - out.push('>'); - out.push_str("~/"); - out.push_str(path); - out.push_str(""); - i += 2 + path_len; - continue; } } - if let Some((j, c)) = rest.char_indices().next() { - out.push(c); - i += j + c.len_utf8(); - } else { - break; - } } out } @@ -638,7 +672,13 @@ fn spotify_embed_src(url: &str) -> Option { if !(host == "open.spotify.com" || host == "www.open.spotify.com") { return None; } - let path = tail.split('#').next().unwrap_or(tail).split('?').next().unwrap_or(tail); + let path = tail + .split('#') + .next() + .unwrap_or(tail) + .split('?') + .next() + .unwrap_or(tail); let mut segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect(); if segs.first().is_some_and(|s| s.starts_with("intl-")) { segs.remove(0); @@ -670,8 +710,16 @@ fn youtube_embed_src(url: &str) -> Option { let host = host.to_lowercase(); let video_id = if host == "youtu.be" || host == "www.youtu.be" { - clean_media_id(tail.split(['?', '#']).next().unwrap_or(tail).trim_matches('/')) - } else if matches!(host.as_str(), "youtube.com" | "www.youtube.com" | "m.youtube.com" | "music.youtube.com") { + clean_media_id( + tail.split(['?', '#']) + .next() + .unwrap_or(tail) + .trim_matches('/'), + ) + } else if matches!( + host.as_str(), + "youtube.com" | "www.youtube.com" | "m.youtube.com" | "music.youtube.com" + ) { let path = format!("/{}", tail.split('#').next().unwrap_or(tail)); if path.starts_with("/watch") { clean_media_id(&query_param(url, "v")?) @@ -745,10 +793,7 @@ pub(super) fn render_linkified_with_embeds_in_scope( /// CLI strings are embedded in a single-quoted JS literal; they must never need escaping. fn assert_cli_panel_cmd_js_single_quote_safe(s: &str) { assert!( - !s.contains('\\') - && !s.contains('\'') - && !s.contains('\n') - && !s.contains('\r'), + !s.contains('\\') && !s.contains('\'') && !s.contains('\n') && !s.contains('\r'), "cli_panel cmd must not contain `\\`, `'`, or newlines (got {s:?})" ); } @@ -805,15 +850,40 @@ mod linkify_title_tests { let mut bodies = HashMap::new(); let key = ItemId::parse(&slug_types::canonicalize_item("~/foo/bar")).unwrap(); bodies.insert(key, "Hello world\nline".to_string()); - let html = linkify_slugs_with_prefix( - "see ~/foo/bar ok", - "/r/x/~", - Some(&bodies), - ); + let html = linkify_slugs_with_prefix("see ~/foo/bar ok", "/r/x/~", Some(&bodies)); assert!(html.contains("title=\"Hello world line\"")); assert!(html.contains("href=\"/r/x/~/foo/bar\"")); } + #[test] + fn raw_url_links_to_public_external_garden_page() { + let html = linkify_slugs_with_prefix("see https://example.com/z.", "/~", None); + assert!(html + .contains(r#"https://example.com/z."#)); + } + + #[test] + fn dash_ref_links_to_room_external_garden_page_with_title() { + let mut bodies = HashMap::new(); + let key = ItemId::parse(&slug_types::canonicalize_item("-/example.com/z")).unwrap(); + bodies.insert(key, "External body\npreview".to_string()); + let html = + linkify_slugs_with_prefix("see -/example.com/z", "/r/9ab12cdroom/~", Some(&bodies)); + assert!(html.contains(r#"href="/r/9ab12cdroom/-/example.com/z""#)); + assert!(html.contains(r#"title="External body preview""#)); + } + + #[test] + fn code_fence_urls_are_not_linkified() { + let html = linkify_slugs_with_prefix( + "```json\n{\"url\":\"https://example.com/z\"}\n```\nthen https://example.com/a", + "/~", + None, + ); + assert!(!html.contains(r#"href="/-/example.com/z""#)); + assert!(html.contains(r#"href="/-/example.com/a""#)); + } + #[test] fn no_title_when_body_missing_or_empty() { let html = linkify_slugs_with_prefix("x ~/a/b y", "/~", Some(&HashMap::new()));