{"messages":[{"content":"You are a constitutional council ranking individual git commits for ownership allocation.\n\nCompare these two commits. Decide which contributed more lasting value to the project.\n\nJudge substance, not spectacle:\n- Prefer correct, lasting design and real bugfixes over churn, formatting, renames, or generated noise.\n- Prefer clarity and necessity over sheer line count. A small precise change can beat a large diffuse one.\n- Do not favor a side merely because its patch is longer or noisier.\n- Weight what the change does for the project, not the contributor's name.\n\nReturn ONLY a JSON object: {\"winner\": \"A\" or \"B\", \"ratio\": \"N:M\", \"explanation\": \"...\"}\nThe explanation must cite concrete differences in the patches (1-3 sentences).\n\nSide A — contributor: tommy-mor\nSide A — commit message:\n[6d04afc2] refactor\n\nSide A — unified diff (full patch):\ndiff --git a/Cargo.lock b/Cargo.lock\nindex 2cea973082716e761ef6f5dd5886acc08ff9aac0..8c43fb75c472b102e6e1d3b837dce3355be898f2 100644\n--- a/Cargo.lock\n+++ b/Cargo.lock\n@@ -17,6 +17,28 @@ version = \"1.0.102\"\n source = \"registry+https://github.com/rust-lang/crates.io-index\"\n checksum = \"7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c\"\n \n+[[package]]\n+name = \"async-stream\"\n+version = \"0.3.6\"\n+source = \"registry+https://github.com/rust-lang/crates.io-index\"\n+checksum = \"0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476\"\n+dependencies = [\n+ \"async-stream-impl\",\n+ \"futures-core\",\n+ \"pin-project-lite\",\n+]\n+\n+[[package]]\n+name = \"async-stream-impl\"\n+version = \"0.3.6\"\n+source = \"registry+https://github.com/rust-lang/crates.io-index\"\n+checksum = \"c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d\"\n+dependencies = [\n+ \"proc-macro2\",\n+ \"quote\",\n+ \"syn\",\n+]\n+\n [[package]]\n name = \"async-trait\"\n version = \"0.1.89\"\n@@ -1242,9 +1264,11 @@ dependencies = [\n name = \"sorter2-server\"\n version = \"0.0.1\"\n dependencies = [\n+ \"async-stream\",\n \"axum\",\n \"axum-extra\",\n \"dotenvy\",\n+ \"futures-util\",\n \"maud\",\n \"reqwest\",\n \"serde\",\ndiff --git a/server/Cargo.toml b/server/Cargo.toml\nindex bd600138b613bd0f546bdec217a5334cdcb20aa5..c940acb687fb141d21760a3d6656172013cf6f41 100644\n--- a/server/Cargo.toml\n+++ b/server/Cargo.toml\n@@ -18,6 +18,8 @@ tracing = \"0.1\"\n tracing-subscriber = { version = \"0.3\", features = [\"env-filter\"] }\n reqwest = { version = \"0.12\", features = [\"json\"] }\n dotenvy = \"0.15\"\n+async-stream = \"0.3\"\n+futures-util = { version = \"0.3\", default-features = false, features = [\"std\"] }\n \n [dev-dependencies]\n reqwest = { version = \"0.12\", features = [\"json\"] }\ndiff --git a/server/src/api/ui_html.rs b/server/src/api/ui_html.rs\nindex b33a84e8bb5e817b26592868d88090e6d664d950..7af6527d03c483f33f3469ce6766c01a554c5fe3 100644\n--- a/server/src/api/ui_html.rs\n+++ b/server/src/api/ui_html.rs\n@@ -6,7 +6,8 @@ use axum::{\n use std::collections::HashMap;\n \n use crate::{\n- html::{entity_section, input_panel, js_string_literal, ranking_panel, JsBuilder},\n+ fetch,\n+ html::{input_panel, js_string_literal, ranking_panel, JsBuilder},\n parser::parse_reddit_url,\n path_types::ItemId,\n reddit::ensure_partial_tree,\n@@ -89,18 +90,8 @@ pub async fn post_ui_html(\n },\n HtmlUiAction::FetchEntity { item } => {\n let id = parse_item_param(&item);\n- if id.is_root() {\n- return ui_js_warn(\"nothing to fetch for the root\").into_response();\n- }\n- state.queue_entity_fetch(id.clone());\n- let tree = state.tree.read().await;\n- let empty = crate::reducer::NodeState::default();\n- let node = tree.get(&id).unwrap_or(&empty);\n- let panel = entity_section(&id, node, true);\n- JsBuilder::new()\n- .morph_selector(\"#entity-section\", panel)\n- .into_response()\n- },\n+ fetch::fetch_entity_stream(state, id).into_response()\n+ }\n }\n }\n \ndiff --git a/server/src/fetch/html.rs b/server/src/fetch/html.rs\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..63634508496e224c38b9ec0308b7a6086462f925\n--- /dev/null\n+++ b/server/src/fetch/html.rs\n@@ -0,0 +1,67 @@\n+//! Markup for entity import / “Fetch from Reddit” (`POST /ui`, SSE response).\n+\n+use maud::{html, Markup};\n+\n+use crate::{\n+ form_template::template_json_compact,\n+ path_types::ItemId,\n+ reddit::is_fetchable,\n+ reducer::NodeState,\n+ ui_action::UI_RPC_FIELD,\n+};\n+\n+fn entity_panel(node: &NodeState) -> Markup {\n+ html! {\n+ @if let Some(data) = &node.data {\n+ div id=\"entity-panel\" class=\"entity-card\" {\n+ h2 { (data.title) }\n+ @if let Some(author) = &data.author {\n+ p class=\"muted small\" { \"by \" (author) }\n+ }\n+ @if let Some(body) = &data.body_html {\n+ div class=\"entity-body\" { (maud::PreEscaped(body)) }\n+ }\n+ }\n+ }\n+ }\n+}\n+\n+/// Reddit/API import — `POST /ui` with `fetch_entity` returns an SSE stream.\n+pub fn fetch_entity_panel(item: &ItemId, has_data: bool, fetching: bool) -> Markup {\n+ if !is_fetchable(item) {\n+ return html! {};\n+ }\n+ let label = if fetching {\n+ \"Fetching…\"\n+ } else if has_data {\n+ \"Fetch more\"\n+ } else {\n+ \"Fetch from Reddit\"\n+ };\n+ let rpc = template_json_compact(&serde_json::json!({\n+ \"action\": \"fetch_entity\",\n+ \"item\": item.as_str(),\n+ }))\n+ .expect(\"fetch_entity rpc template\");\n+ html! {\n+ form method=\"post\" action=\"/ui\" id=\"fetch-entity-form\" class=\"fetch-entity-form\" {\n+ input type=\"hidden\" name=(UI_RPC_FIELD) value=(rpc);\n+ @if fetching {\n+ button type=\"submit\" class=\"btn-secondary\" disabled { (label) }\n+ } @else {\n+ button type=\"submit\" class=\"btn-secondary\" { (label) }\n+ }\n+ }\n+ }\n+}\n+\n+/// Entity card + fetch control (target `#entity-section` for Idiomorph / SSE).\n+pub fn entity_section(item: &ItemId, node: &NodeState, fetching: bool) -> Markup {\n+ let has_data = node.data.is_some();\n+ html! {\n+ section id=\"entity-section\" class=\"demo-panel\" {\n+ (entity_panel(node))\n+ (fetch_entity_panel(item, has_data, fetching))\n+ }\n+ }\n+}\ndiff --git a/server/src/fetch/mod.rs b/server/src/fetch/mod.rs\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..2290f9d3a0f1cbf1806c6339f82a4515c11cc3d3\n--- /dev/null\n+++ b/server/src/fetch/mod.rs\n@@ -0,0 +1,115 @@\n+//! Entity import over `POST /ui` as SSE (Reddit worker in [`crate::reddit`]).\n+\n+pub mod html;\n+\n+use std::convert::Infallible;\n+use std::time::Duration;\n+\n+use async_stream::stream;\n+use axum::response::sse::{Event, KeepAlive, Sse};\n+use futures_util::Stream;\n+use serde::Serialize;\n+use tokio::sync::oneshot;\n+\n+use crate::{\n+ path_types::ItemId,\n+ reddit::FetchJobResult,\n+ reducer::NodeState,\n+ state::AppState,\n+};\n+\n+pub fn now_ms() -> i64 {\n+ let t = std::time::SystemTime::now()\n+ .duration_since(std::time::UNIX_EPOCH)\n+ .unwrap_or_default();\n+ t.as_millis() as i64\n+}\n+\n+#[derive(Serialize)]\n+struct SseMorphPayload {\n+ selector: &'static str,\n+ html: String,\n+}\n+\n+fn morph_complete_event(html: maud::Markup) -> Event {\n+ let payload = SseMorphPayload {\n+ selector: \"#entity-section\",\n+ html: html.into_string(),\n+ };\n+ let data = serde_json::to_string(&payload).unwrap_or_else(|_| \"{}\".into());\n+ Event::default().event(\"complete\").data(data)\n+}\n+\n+/// Stream `fetching` → `complete` / `error` for [`crate::ui_action::HtmlUiAction::FetchEntity`].\n+pub fn fetch_entity_stream(\n+ state: AppState,\n+ id: ItemId,\n+) -> Sse>> {\n+ tracing::debug!(item = %id, \"fetch entity stream opened\");\n+\n+ let stream = stream! {\n+ if id.is_root() {\n+ yield Ok(Event::default().event(\"error\").data(\"{\\\"message\\\":\\\"nothing to fetch for the root\\\"}\"));\n+ return;\n+ }\n+\n+ if !crate::reddit::is_fetchable(&id) {\n+ tracing::debug!(item = %id, \"fetch stream: not fetchable\");\n+ yield Ok(Event::default().event(\"error\").data(\"{\\\"message\\\":\\\"this page cannot be fetched from Reddit\\\"}\"));\n+ return;\n+ }\n+\n+ let fetching_html = {\n+ let tree = state.tree.read().await;\n+ let empty = NodeState::default();\n+ let node = tree.get(&id).unwrap_or(&empty);\n+ html::entity_section(&id, node, true).into_string()\n+ };\n+ let fetching_payload = serde_json::json!({\n+ \"selector\": \"#entity-section\",\n+ \"html\": fetching_html,\n+ });\n+ yield Ok(Event::default().event(\"fetching\").data(fetching_payload.to_string()));\n+\n+ let (tx, rx) = oneshot::channel();\n+ state.reddit.request_fetch(id.clone(), true, Some(tx));\n+ tracing::debug!(item = %id, \"fetch stream: queued reddit job\");\n+\n+ let result = match rx.await {\n+ Ok(r) => r,\n+ Err(_) => {\n+ tracing::warn!(item = %id, \"fetch stream: worker dropped oneshot\");\n+ FetchJobResult::Failed(\"reddit worker stopped\".into())\n+ }\n+ };\n+\n+ tracing::debug!(item = %id, ?result, \"fetch stream: job finished\");\n+\n+ match result {\n+ FetchJobResult::Imported | FetchJobResult::NotFound => {\n+ let tree = state.tree.read().await;\n+ let empty = NodeState::default();\n+ let node = tree.get(&id).unwrap_or(&empty);\n+ yield Ok(morph_complete_event(html::entity_section(&id, node, false)));\n+ }\n+ FetchJobResult::SkippedCached | FetchJobResult::SkippedDuplicate => {\n+ let tree = state.tree.read().await;\n+ let empty = NodeState::default();\n+ let node = tree.get(&id).unwrap_or(&empty);\n+ yield Ok(morph_complete_event(html::entity_section(&id, node, false)));\n+ }\n+ FetchJobResult::RateLimited { reset_secs } => {\n+ yield Ok(Event::default().event(\"error\").data(\n+ serde_json::json!({\"message\": format!(\"Reddit rate limit — retry in {reset_secs}s\")}).to_string(),\n+ ));\n+ }\n+ FetchJobResult::Failed(msg) => {\n+ yield Ok(Event::default().event(\"error\").data(\n+ serde_json::json!({\"message\": msg}).to_string(),\n+ ));\n+ }\n+ }\n+ };\n+\n+ Sse::new(stream).keep_alive(KeepAlive::new().interval(Duration::from_secs(15)))\n+}\ndiff --git a/server/src/html/mod.rs b/server/src/html/mod.rs\nindex db5b4c7f06b0be64603981166835cde268234f67..9314a7556306ddab969b896dbf4126b542a46722 100644\n--- a/server/src/html/mod.rs\n+++ b/server/src/html/mod.rs\n@@ -7,10 +7,10 @@ use axum::{\n use maud::{html, Markup, DOCTYPE};\n \n use crate::{\n+ fetch::html::entity_section,\n form_template::template_json_compact,\n path_types::ItemId,\n ranking::{top_bottom, RankedItem},\n- reddit::is_fetchable,\n reducer::{GroupState, NodeState},\n state::AppState,\n ui_action::UI_RPC_FIELD,\n@@ -149,62 +149,6 @@ pub fn breadcrumb_path(item: &ItemId) -> Markup {\n }\n }\n \n-fn entity_panel(node: &NodeState) -> Markup {\n- html! {\n- @if let Some(data) = &node.data {\n- div id=\"entity-panel\" class=\"entity-card\" {\n- h2 { (data.title) }\n- @if let Some(author) = &data.author {\n- p class=\"muted small\" { \"by \" (author) }\n- }\n- @if let Some(body) = &data.body_html {\n- div class=\"entity-body\" { (maud::PreEscaped(body)) }\n- }\n- }\n- }\n- }\n-}\n-\n-/// Reddit/API import control — only shown on fetchable pages; never auto-fires.\n-pub fn fetch_entity_panel(item: &ItemId, has_data: bool, fetching: bool) -> Markup {\n- if !is_fetchable(item) {\n- return html! {};\n- }\n- let label = if fetching {\n- \"Fetching…\"\n- } else if has_data {\n- \"Fetch more\"\n- } else {\n- \"Fetch from Reddit\"\n- };\n- let rpc = template_json_compact(&serde_json::json!({\n- \"action\": \"fetch_entity\",\n- \"item\": item.as_str(),\n- }))\n- .expect(\"fetch_entity rpc template\");\n- html! {\n- form method=\"post\" action=\"/ui\" id=\"fetch-entity-form\" class=\"fetch-entity-form\" {\n- input type=\"hidden\" name=(UI_RPC_FIELD) value=(rpc);\n- @if fetching {\n- button type=\"submit\" class=\"btn-secondary\" disabled { (label) }\n- } @else {\n- button type=\"submit\" class=\"btn-secondary\" { (label) }\n- }\n- }\n- }\n-}\n-\n-/// Entity card + explicit fetch control (morphed as `#entity-section`).\n-pub fn entity_section(item: &ItemId, node: &NodeState, fetching: bool) -> Markup {\n- let has_data = node.data.is_some();\n- html! {\n- section id=\"entity-section\" class=\"demo-panel\" {\n- (entity_panel(node))\n- (fetch_entity_panel(item, has_data, fetching))\n- }\n- }\n-}\n-\n fn rank_list(label: &str, items: &[RankedItem], start_rank: usize) -> Markup {\n html! {\n @if !items.is_empty() {\ndiff --git a/server/src/lib.rs b/server/src/lib.rs\nindex 79b173f391a96ae5d0d96fd656e1e8d2dd070d09..0677363e0ed21d244bfa065f7ec728549ddd9e50 100644\n--- a/server/src/lib.rs\n+++ b/server/src/lib.rs\n@@ -1,6 +1,7 @@\n pub mod api;\n pub mod event_log;\n pub mod events;\n+pub mod fetch;\n pub mod form_template;\n pub mod html;\n pub mod parser;\ndiff --git a/server/src/reddit.rs b/server/src/reddit.rs\nindex ff0f01e57b18af878eb5be3efc47204a7673589d..0e6ce32720951a58456852b67fb76a85dd9f4aad 100644\n--- a/server/src/reddit.rs\n+++ b/server/src/reddit.rs\n@@ -7,12 +7,12 @@ use std::time::{Duration, Instant};\n use reqwest::{header, Client, StatusCode};\n use serde::Deserialize;\n use serde_json::Value;\n-use tokio::sync::{mpsc, RwLock};\n+use tokio::sync::{mpsc, oneshot, RwLock};\n \n use crate::{\n event_log::EventLog,\n events::Event,\n- html::now_ms,\n+ fetch::now_ms,\n path_types::ItemId,\n reducer::GlobalTree,\n };\n@@ -22,10 +22,21 @@ pub fn ensure_partial_tree(tree: &mut GlobalTree, id: &ItemId) {\n tree.ensure_path(id);\n }\n \n+#[derive(Debug, Clone, PartialEq, Eq)]\n+pub enum FetchJobResult {\n+ Imported,\n+ NotFound,\n+ SkippedDuplicate,\n+ SkippedCached,\n+ RateLimited { reset_secs: u64 },\n+ Failed(String),\n+}\n+\n pub struct RedditCommand {\n pub id: ItemId,\n /// User-initiated fetch bypasses the in-memory \"recently fetched\" cache.\n pub force: bool,\n+ pub done: Option>,\n }\n \n #[derive(Clone)]\n@@ -72,14 +83,29 @@ impl RedditBroker {\n .build()\n .expect(\"reqwest client\");\n \n+ tracing::debug!(\n+ api_base = %config.api_base,\n+ oauth_base = %config.oauth_base,\n+ oauth = config.creds.is_some(),\n+ \"reddit worker started\"\n+ );\n+\n tokio::spawn(reddit_worker(rx, tree, event_log, client, config));\n \n Self { tx }\n }\n \n /// Queue a fetch; drops when the channel is full (backpressure).\n- pub fn request_fetch(&self, id: ItemId, force: bool) {\n- let _ = self.tx.try_send(RedditCommand { id, force });\n+ pub fn request_fetch(\n+ &self,\n+ id: ItemId,\n+ force: bool,\n+ done: Option>,\n+ ) {\n+ match self.tx.try_send(RedditCommand { id: id.clone(), force, done }) {\n+ Ok(()) => tracing::debug!(item = %id, force, \"reddit fetch queued\"),\n+ Err(_) => tracing::warn!(item = %id, \"reddit fetch queue full, dropped\"),\n+ }\n }\n }\n \n@@ -95,8 +121,6 @@ impl RedditApiConfig {\n }\n \n impl RedditCredentials {\n- /// Reddit's OAuth docs call these \"client id\" and \"client secret\"; the app\n- /// registration UI often labels them \"app id\" / \"app secret\" — same values.\n fn from_env() -> Option {\n let client_id = std::env::var(\"REDDIT_CLIENT_ID\")\n .or_else(|_| std::env::var(\"REDDIT_APP_ID\"))\n@@ -128,12 +152,10 @@ pub fn default_user_agent() -> String {\n })\n }\n \n-/// True when this node can be loaded from the Reddit JSON API.\n pub fn is_fetchable(id: &ItemId) -> bool {\n !map_item_to_reddit_api(id, \"https://example.com\").is_empty()\n }\n \n-/// Derive UI-facing fields from a stored payload (Reddit-specific when under reddit.com).\n pub fn entity_view_from_payload(id: &ItemId, payload: &Value) -> Option {\n if id.as_str().starts_with(\"reddit.com\") {\n return parse_reddit_view(id, payload);\n@@ -141,12 +163,17 @@ pub fn entity_view_from_payload(id: &ItemId, payload: &Value) -> Option>, result: FetchJobResult) {\n+ if let Some(tx) = done {\n+ let _ = tx.send(result);\n+ }\n+}\n+\n async fn reddit_worker(\n mut rx: mpsc::Receiver,\n tree: Arc>,\n@@ -168,15 +195,25 @@ async fn reddit_worker(\n recently_fetched.retain(|_, t| now.duration_since(*t) < cache_ttl);\n \n if in_flight.contains(&cmd.id) {\n+ tracing::debug!(item = %cmd.id, \"reddit fetch skipped: already in flight\");\n+ notify(cmd.done, FetchJobResult::SkippedDuplicate);\n continue;\n }\n if !cmd.force && recently_fetched.contains_key(&cmd.id) {\n+ tracing::debug!(item = %cmd.id, \"reddit fetch skipped: recently fetched cache\");\n+ notify(cmd.done, FetchJobResult::SkippedCached);\n continue;\n }\n \n in_flight.insert(cmd.id.clone());\n let fetch_id = cmd.id.clone();\n+ let done = cmd.done;\n \n+ tracing::debug!(\n+ item = %fetch_id,\n+ delay_ms = current_delay.as_millis(),\n+ \"reddit fetch starting after delay\"\n+ );\n tokio::time::sleep(current_delay).await;\n \n if let Some(c) = &creds {\n@@ -184,8 +221,18 @@ async fn reddit_worker(\n }\n \n let token = oauth.as_ref().map(|t| t.access_token.as_str());\n+ if token.is_some() {\n+ tracing::debug!(item = %fetch_id, \"reddit fetch using OAuth bearer\");\n+ }\n+\n+ let fetch_base = if token.is_some() {\n+ &oauth_base\n+ } else {\n+ &api_base\n+ };\n+ let outcome = do_fetch(&client, fetch_base, &fetch_id, token).await;\n \n- match do_fetch(&client, &api_base, &fetch_id, token).await {\n+ match outcome {\n Ok(FetchOutcome::Payload(payload)) => {\n let ts = now_ms();\n let event = Event::EntityImported {\n@@ -193,30 +240,49 @@ async fn reddit_worker(\n ts,\n payload: payload.clone(),\n };\n- if let Err(e) = event_log.append(&event).await {\n- tracing::warn!(\"event log append failed for {}: {}\", fetch_id, e);\n- } else {\n- apply_entity_import(&mut *tree.write().await, &fetch_id, payload);\n- recently_fetched.insert(fetch_id.clone(), Instant::now());\n- current_delay = Duration::from_millis(600);\n+ tracing::debug!(\n+ item = %fetch_id,\n+ ts,\n+ payload_keys = ?payload.as_object().map(|o| o.len()),\n+ \"reddit fetch got JSON payload, appending event\"\n+ );\n+ match event_log.append(&event).await {\n+ Err(e) => {\n+ tracing::warn!(\n+ item = %fetch_id,\n+ err = %e,\n+ \"reddit event log append failed\"\n+ );\n+ notify(done, FetchJobResult::Failed(e.to_string()));\n+ }\n+ Ok(()) => {\n+ apply_entity_import(&mut *tree.write().await, &fetch_id, payload);\n+ recently_fetched.insert(fetch_id.clone(), Instant::now());\n+ current_delay = Duration::from_millis(600);\n+ tracing::info!(item = %fetch_id, \"reddit entity imported\");\n+ notify(done, FetchJobResult::Imported);\n+ }\n }\n }\n Ok(FetchOutcome::NotFound) => {\n+ tracing::debug!(item = %fetch_id, \"reddit fetch: not found (no event written)\");\n recently_fetched.insert(fetch_id.clone(), Instant::now());\n+ notify(done, FetchJobResult::NotFound);\n }\n Ok(FetchOutcome::RateLimited { reset_secs }) => {\n- let wait = Duration::from_secs(reset_secs.max(1));\n tracing::warn!(\n- \"Reddit rate limit for {}; sleeping {}s\",\n- fetch_id,\n- wait.as_secs()\n+ item = %fetch_id,\n+ reset_secs,\n+ \"reddit rate limited\"\n );\n- tokio::time::sleep(wait).await;\n+ tokio::time::sleep(Duration::from_secs(reset_secs.max(1))).await;\n current_delay = (current_delay * 2).min(Duration::from_secs(60));\n+ notify(done, FetchJobResult::RateLimited { reset_secs });\n }\n Err(e) => {\n- tracing::warn!(\"Reddit fetch failed for {}: {}\", fetch_id, e);\n+ tracing::warn!(item = %fetch_id, err = %e, \"reddit fetch failed\");\n current_delay = (current_delay * 2).min(Duration::from_secs(60));\n+ notify(done, FetchJobResult::Failed(e));\n }\n }\n \n@@ -238,6 +304,7 @@ async fn ensure_oauth_token(\n ) -> Option {\n if let Some(t) = existing {\n if Instant::now() < t.expires_at - Duration::from_secs(60) {\n+ tracing::debug!(\"reddit OAuth token still valid\");\n return Some(t);\n }\n }\n@@ -246,6 +313,7 @@ async fn ensure_oauth_token(\n \"{}/api/v1/access_token\",\n oauth_base.trim_end_matches('/')\n );\n+ tracing::debug!(%url, \"reddit OAuth token request\");\n \n let resp = client\n .post(&url)\n@@ -257,13 +325,13 @@ async fn ensure_oauth_token(\n let resp = match resp {\n Ok(r) => r,\n Err(e) => {\n- tracing::warn!(\"Reddit OAuth token request failed: {e}\");\n+ tracing::warn!(\"reddit OAuth token request failed: {e}\");\n return None;\n }\n };\n \n if !resp.status().is_success() {\n- tracing::warn!(\"Reddit OAuth token HTTP {}\", resp.status());\n+ tracing::warn!(\"reddit OAuth token HTTP {}\", resp.status());\n return None;\n }\n \n@@ -276,11 +344,12 @@ async fn ensure_oauth_token(\n let body: TokenResponse = match resp.json().await {\n Ok(b) => b,\n Err(e) => {\n- tracing::warn!(\"Reddit OAuth token parse failed: {e}\");\n+ tracing::warn!(\"reddit OAuth token parse failed: {e}\");\n return None;\n }\n };\n \n+ tracing::debug!(expires_in = body.expires_in, \"reddit OAuth token acquired\");\n Some(OAuthToken {\n access_token: body.access_token,\n expires_at: Instant::now() + Duration::from_secs(body.expires_in),\n@@ -295,35 +364,72 @@ async fn do_fetch(\n ) -> Result {\n let url = map_item_to_reddit_api(id, api_base);\n if url.is_empty() {\n+ tracing::debug!(item = %id, \"reddit do_fetch: no API URL for item\");\n return Ok(FetchOutcome::NotFound);\n }\n \n+ tracing::debug!(item = %id, %url, bearer = bearer.is_some(), \"reddit HTTP GET\");\n+\n let mut req = client.get(&url);\n if let Some(token) = bearer {\n req = req.bearer_auth(token);\n }\n \n- let resp = req.send().await.map_err(|e| e.to_string())?;\n+ let resp = req.send().await.map_err(|e| {\n+ tracing::debug!(item = %id, %url, err = %e, \"reddit HTTP transport error\");\n+ e.to_string()\n+ })?;\n+\n+ let status = resp.status();\n+ tracing::debug!(\n+ item = %id,\n+ %url,\n+ %status,\n+ remaining = ?rate_limit_remaining(&resp),\n+ reset = ?rate_limit_reset_secs(&resp),\n+ \"reddit HTTP response\"\n+ );\n \n- if resp.status() == StatusCode::TOO_MANY_REQUESTS {\n+ if status == StatusCode::TOO_MANY_REQUESTS {\n let reset = rate_limit_reset_secs(&resp);\n return Ok(FetchOutcome::RateLimited { reset_secs: reset });\n }\n \n- if resp.status() == StatusCode::SERVICE_UNAVAILABLE {\n- return Err(\"Reddit unavailable (503)\".to_string());\n+ if status == StatusCode::SERVICE_UNAVAILABLE {\n+ return Err(\"Reddit unavailable (503)\".into());\n }\n \n- if !resp.status().is_success() {\n+ if !status.is_success() {\n+ let body = resp.text().await.unwrap_or_default();\n+ tracing::debug!(\n+ item = %id,\n+ %status,\n+ body_len = body.len(),\n+ body_prefix = %body.chars().take(240).collect::(),\n+ \"reddit non-success body\"\n+ );\n return Ok(FetchOutcome::NotFound);\n }\n \n if rate_limit_remaining(&resp) == Some(0) {\n let reset = rate_limit_reset_secs(&resp);\n+ tracing::debug!(item = %id, reset_secs = reset, \"reddit headers: rate limit exhausted\");\n return Ok(FetchOutcome::RateLimited { reset_secs: reset });\n }\n \n- let payload: Value = resp.json().await.map_err(|e| e.to_string())?;\n+ let text = resp.text().await.map_err(|e| e.to_string())?;\n+ tracing::debug!(item = %id, bytes = text.len(), \"reddit response body received\");\n+\n+ let payload: Value = serde_json::from_str(&text).map_err(|e| {\n+ tracing::debug!(\n+ item = %id,\n+ err = %e,\n+ body_prefix = %text.chars().take(240).collect::(),\n+ \"reddit JSON parse failed\"\n+ );\n+ format!(\"invalid JSON: {e}\")\n+ })?;\n+\n Ok(FetchOutcome::Payload(payload))\n }\n \n@@ -344,7 +450,6 @@ fn rate_limit_reset_secs(resp: &reqwest::Response) -> u64 {\n .unwrap_or(5)\n }\n \n-/// Map canonical item id to a Reddit JSON API URL under `api_base`.\n pub fn map_item_to_reddit_api(id: &ItemId, api_base: &str) -> String {\n let path = id.as_str();\n if !path.starts_with(\"reddit.com/\") && path != \"reddit.com\" {\n@@ -445,26 +550,6 @@ mod tests {\n map_item_to_reddit_api(&id, \"https://www.reddit.com\"),\n \"https://www.reddit.com/r/rust/about.json?raw_json=1\"\n );\n- assert_eq!(\n- map_item_to_reddit_api(&id, \"http://127.0.0.1:9999\"),\n- \"http://127.0.0.1:9999/r/rust/about.json?raw_json=1\"\n- );\n- }\n-\n- #[test]\n- fn map_post_url() {\n- let id = ItemId::parse(\"reddit.com/r/amitheasshole/comments/1trnvdl\").unwrap();\n- assert_eq!(\n- map_item_to_reddit_api(&id, \"https://www.reddit.com\"),\n- \"https://www.reddit.com/r/amitheasshole/comments/1trnvdl.json?raw_json=1\"\n- );\n- }\n-\n- #[test]\n- fn is_fetchable_reddit_sub() {\n- let id = ItemId::parse(\"reddit.com/r/rust\").unwrap();\n- assert!(is_fetchable(&id));\n- assert!(!is_fetchable(&ItemId::opaque(\"example.com/x\")));\n }\n \n #[test]\n@@ -477,19 +562,5 @@ mod tests {\n )\n .unwrap();\n assert_eq!(entity.title, \"The Rust Programming Language\");\n- assert!(entity.body_html.as_ref().is_some_and(|b| b.contains(\"Rust\")));\n- }\n-\n- #[test]\n- fn parse_post_fixture() {\n- let json = r#\"[{\"kind\":\"Listing\",\"data\":{\"children\":[{\"kind\":\"t3\",\"data\":{\"title\":\"AITA\",\"author\":\"op\",\"selftext_html\":\"<p>hi</p>\",\"thumbnail\":\"https://b.thumbs.redditmedia.com/x.jpg\"}}]}}]\"#;\n- let v: Value = serde_json::from_str(json).unwrap();\n- let entity = entity_view_from_payload(\n- &ItemId::parse(\"reddit.com/r/x/comments/abc\").unwrap(),\n- &v,\n- )\n- .unwrap();\n- assert_eq!(entity.title, \"AITA\");\n- assert_eq!(entity.author.as_deref(), Some(\"op\"));\n }\n }\ndiff --git a/server/src/state.rs b/server/src/state.rs\nindex d71d1079a8f486cdab15795384aef0b81b32544d..e7ff9f663e45b5e168d6a3869948e3bd890966d4 100644\n--- a/server/src/state.rs\n+++ b/server/src/state.rs\n@@ -143,9 +143,9 @@ impl AppState {\n Ok(())\n }\n \n- /// User-initiated Reddit/API import (via \"Fetch more\" — never on paste or navigate).\n- pub fn queue_entity_fetch(&self, id: ItemId) {\n- self.reddit.request_fetch(id, true);\n+ /// User-initiated Reddit/API import (SSE / fetch module only).\n+ pub fn queue_entity_fetch(&self, id: ItemId, done: Option>) {\n+ self.reddit.request_fetch(id, true, done);\n }\n \n pub async fn record_vote(\ndiff --git a/server/src/ui_action.rs b/server/src/ui_action.rs\nindex 3d6a49a2a3fb950752827efe1f5a049308f09baf..ef9ac873fc0442e0b960f144309c8e91124d7884 100644\n--- a/server/src/ui_action.rs\n+++ b/server/src/ui_action.rs\n@@ -26,7 +26,7 @@ pub enum HtmlUiAction {\n ParseQuery {\n query: String,\n },\n- /// Fetch upstream entity data for the current page (explicit user action only).\n+ /// Import entity data; `POST /ui` responds with `text/event-stream` (not JS).\n FetchEntity {\n item: String,\n },\ndiff --git a/server/static/sorter_ui.js b/server/static/sorter_ui.js\nindex d9b016f197547f37ca4b2bcdd7ee6b673d0fb3f0..fe7bfc657c0fccae5c596d005b0e807abefec677 100644\n--- a/server/static/sorter_ui.js\n+++ b/server/static/sorter_ui.js\n@@ -1,5 +1,5 @@\n /**\n- * sorter2 web UI: fetch/eval for POST /ui. No product logic here.\n+ * sorter2 web UI: POST /ui returns JS (morph) or SSE (entity fetch).\n */\n (function () {\n function evalJs(js) {\n@@ -8,15 +8,98 @@\n }\n }\n \n+ function morphSelector(selector, html) {\n+ var el = document.querySelector(selector);\n+ if (el && typeof Idiomorph !== 'undefined') {\n+ Idiomorph.morph(el, html);\n+ }\n+ }\n+\n+ function handleSseEvent(eventType, data, form) {\n+ if (eventType === 'fetching' || eventType === 'complete') {\n+ try {\n+ var msg = JSON.parse(data);\n+ morphSelector(msg.selector || '#entity-section', msg.html);\n+ } catch (err) {\n+ console.warn('fetch morph parse', err);\n+ }\n+ }\n+ if (eventType === 'complete' || eventType === 'error') {\n+ var btn = form && form.querySelector('button[type=\"submit\"]');\n+ if (btn) btn.disabled = false;\n+ }\n+ if (eventType === 'error') {\n+ try {\n+ var err = JSON.parse(data);\n+ console.warn('fetch error:', err.message || data);\n+ } catch (_e) {\n+ console.warn('fetch error:', data);\n+ }\n+ }\n+ }\n+\n+ function consumeSseStream(response, form) {\n+ var reader = response.body.getReader();\n+ var decoder = new TextDecoder();\n+ var buffer = '';\n+ var eventType = '';\n+ var dataLines = [];\n+\n+ function dispatch() {\n+ if (!eventType && dataLines.length === 0) return;\n+ handleSseEvent(eventType || 'message', dataLines.join('\\n'), form);\n+ eventType = '';\n+ dataLines = [];\n+ }\n+\n+ function pump() {\n+ return reader.read().then(function (chunk) {\n+ if (chunk.done) {\n+ dispatch();\n+ return;\n+ }\n+ buffer += decoder.decode(chunk.value, { stream: true });\n+ var parts = buffer.split('\\n');\n+ buffer = parts.pop() || '';\n+ for (var i = 0; i < parts.length; i++) {\n+ var line = parts[i].replace(/\\r$/, '');\n+ if (line === '') {\n+ dispatch();\n+ } else if (line.indexOf('event:') === 0) {\n+ eventType = line.slice(6).trim();\n+ } else if (line.indexOf('data:') === 0) {\n+ dataLines.push(line.slice(5).trim());\n+ }\n+ }\n+ return pump();\n+ });\n+ }\n+\n+ return pump();\n+ }\n+\n function postUiForm(form) {\n+ var btn = form.querySelector('button[type=\"submit\"]');\n+ if (form.id === 'fetch-entity-form' && btn) {\n+ btn.disabled = true;\n+ }\n return fetch(form.action, {\n method: 'POST',\n body: new URLSearchParams(new FormData(form)),\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n credentials: 'same-origin',\n }).then(function (resp) {\n- return resp.text();\n- }).then(evalJs);\n+ var ct = resp.headers.get('content-type') || '';\n+ if (ct.indexOf('text/event-stream') !== -1) {\n+ return consumeSseStream(resp, form);\n+ }\n+ return resp.text().then(evalJs);\n+ }).catch(function (err) {\n+ if (form.id === 'fetch-entity-form' && btn) {\n+ btn.disabled = false;\n+ }\n+ console.warn('POST /ui failed', err);\n+ });\n }\n \n function initSorterUi() {\ndiff --git a/test/reddit_import.clj b/test/reddit_import.clj\nindex 54edaedeef08718c8f184d6aa468d8e6415068c7..17f2ee39e1498b0b6bb6a9d8a7e35608b0bd1b32 100644\n--- a/test/reddit_import.clj\n+++ b/test/reddit_import.clj\n@@ -44,10 +44,12 @@\n (do (Thread/sleep 200) (recur))\n false))))))\n \n-(defn- curl-post-ui [base rpc-json]\n+(defn- curl-fetch-ui-sse [base item]\n (process/shell {:out :string :err :string}\n- \"curl\" \"-sf\" \"-X\" \"POST\" (str base \"/ui\")\n- \"--data-urlencode\" (str \"__rpc__=\" rpc-json)))\n+ \"curl\" \"-sfN\" \"--max-time\" \"20\"\n+ \"-X\" \"POST\" (str base \"/ui\")\n+ \"--data-urlencode\"\n+ (str \"__rpc__={\\\"action\\\":\\\"fetch_entity\\\",\\\"item\\\":\\\"\" item \"\\\"}\")))\n \n (defn- wait-event-log [path ms]\n (let [deadline (+ (System/currentTimeMillis) ms)]\n@@ -98,11 +100,12 @@\n \"curl\" \"-sf\" browse-url))]\n (is (str/includes? before \"Fetch from Reddit\"))\n (is (not (str/includes? before \"The Rust Programming Language\")))\n- (let [rpc \"{\\\"action\\\":\\\"fetch_entity\\\",\\\"item\\\":\\\"reddit.com/r/rust\\\"}\"\n- post (curl-post-ui app-base rpc)\n- log-path (str data-dir \"/events.jsonl\")]\n- (is (zero? (:exit post)) \"fetch_entity POST succeeds\")\n- (is (wait-event-log log-path 10000) \"event log written\")\n+ (let [log-path (str data-dir \"/events.jsonl\")\n+ sse (curl-fetch-ui-sse app-base \"reddit.com/r/rust\")]\n+ (is (zero? (:exit sse)) \"POST /ui fetch_entity SSE succeeds\")\n+ (is (str/includes? (:out sse) \"event: complete\"))\n+ (is (str/includes? (:out sse) \"The Rust Programming Language\"))\n+ (is (wait-event-log log-path 2000) \"event log written\")\n (let [after (:out (process/shell {:out :string :err :string}\n \"curl\" \"-sf\" browse-url))\n log (slurp (io/file log-path))]\n\n\nSide B — contributor: tommy-mor\nSide B — commit message:\n[c7ef287e] Rank every eligible commit with the LLM council.\n\nStop short-circuiting on a single contributor; pairwise-sort commits, roll scores up for emission payouts, and surface commit rankings on epoch pages.\n\nCo-authored-by: Cursor \n\nSide B — unified diff (full patch):\ndiff --git a/constitution.py b/constitution.py\nindex 26ba130e885e0e69fb7874ca5c3f07f42100a150..71fc46b4c9a4d7a9ea7bf319860b0db1acc4a673 100644\n--- a/constitution.py\n+++ b/constitution.py\n@@ -503,20 +503,22 @@ def _epochs_in_ledger() -> list[int]:\n \n \n def build_pairwise_prompt(side_a: dict, side_b: dict) -> str:\n- return f\"\"\"You are ranking contributions to an open source project.\n-Compare these two sides (each may be one or more commits). Decide which side contributed more.\n+ return f\"\"\"You are ranking individual git commits to an open source project.\n+Compare these two commits. Decide which commit contributed more.\n Return ONLY a JSON object: {{\"winner\": \"A\" or \"B\", \"ratio\": \"N:M\", \"explanation\": \"...\"}}\n \n-Side A — commit messages:\n+Side A — contributor: {side_a.get('contributor', '?')}\n+Side A — commit message:\n {side_a['message']}\n \n-Side A — unified diffs (full patches):\n+Side A — unified diff (full patch):\n {side_a['diff']}\n \n-Side B — commit messages:\n+Side B — contributor: {side_b.get('contributor', '?')}\n+Side B — commit message:\n {side_b['message']}\n \n-Side B — unified diffs (full patches):\n+Side B — unified diff (full patch):\n {side_b['diff']}\"\"\"\n \n \n@@ -1518,16 +1520,28 @@ async def broadcast_js(js: str):\n await queue.put(js)\n \n \n-def _author_side_for_llm(author: str, author_commits: dict) -> dict:\n- cs = author_commits[author]\n+def _commit_side_for_llm(row: dict) -> dict:\n+ oid = row[\"oid\"]\n+ short = oid.split(\":\", 1)[1][:8] if \":\" in oid else oid[:8]\n return {\n- \"message\": \"\\n\".join(f\"[{c['sha']}] {c['message']}\" for c in cs),\n- \"diff\": \"\\n\\n\".join(f\"=== {c['sha']} ===\\n{c['diff']}\" for c in cs),\n- \"commit_ids\": [c[\"commit_id\"] for c in cs],\n- \"contributor\": author,\n+ \"message\": f\"[{short}] {row['message']}\",\n+ \"diff\": row[\"patch\"] or \"\",\n+ \"commit_id\": commit_id_for_oid(oid),\n+ \"contributor\": row[\"contributor\"],\n+ \"oid\": oid,\n }\n \n \n+def _rollup_contributor_scores(\n+ ordered: list[dict], commit_scores: list[Decimal]\n+) -> dict[str, Decimal]:\n+ totals: dict[str, Decimal] = {}\n+ for row, score in zip(ordered, commit_scores):\n+ contributor = row[\"contributor\"]\n+ totals[contributor] = totals.get(contributor, Decimal(\"0\")) + score\n+ return totals\n+\n+\n def _find_judgment(comparison_id: str, model_id: str) -> dict | None:\n for e in evidence_by_kind(\"llm.judgment\"):\n p = e.payload\n@@ -1546,101 +1560,106 @@ def _find_ranking_models(ranking_run_id: str) -> list[str] | None:\n \n \n async def rank_commits(commits: list[dict], *, epoch: int = -1):\n+ \"\"\"Pairwise-rank every eligible commit; roll scores up to contributors.\"\"\"\n if not commits:\n return {}, [], {\"ranking_run_id\": \"\", \"ranking_event_id\": \"\"}\n \n- commit_ids = sorted(commit_id_for_oid(row[\"oid\"]) for row in commits)\n+ ordered = sorted(commits, key=lambda r: r[\"oid\"])\n+ commit_ids = [commit_id_for_oid(row[\"oid\"]) for row in ordered]\n ranking_run_id = _content_id(\"rank\", {\n \"epoch\": epoch,\n- \"commit_ids\": commit_ids,\n+ \"commit_ids\": sorted(commit_ids),\n })\n- contributors = sorted(set(c[\"contributor\"] for c in commits))\n+ contributors = sorted({c[\"contributor\"] for c in ordered})\n \n- if len(contributors) == 1:\n+ # Nothing to compare: a single commit (not a single contributor).\n+ if len(ordered) == 1:\n await append_evidence(epoch, \"ranking.started\", {\n \"ranking_run_id\": ranking_run_id,\n \"commit_ids\": commit_ids,\n \"contributors\": contributors,\n \"models\": [],\n- \"summary\": f\"ranking epoch {epoch}: single contributor\",\n+ \"summary\": f\"ranking epoch {epoch}: single commit\",\n })\n- ranking = {contributors[0]: Decimal(\"1\")}\n+ commit_ranking = {commit_ids[0]: \"1\"}\n+ contributor_ranking = {ordered[0][\"contributor\"]: Decimal(\"1\")}\n completed = await append_evidence(epoch, \"ranking.completed\", {\n \"ranking_run_id\": ranking_run_id,\n \"models\": [],\n- \"ranking\": {contributors[0]: \"1\"},\n+ \"commit_ranking\": commit_ranking,\n+ \"contributor_ranking\": {ordered[0][\"contributor\"]: \"1\"},\n+ \"ranking\": {ordered[0][\"contributor\"]: \"1\"},\n \"judgment_ids\": [],\n- \"summary\": f\"Only {contributors[0]} is eligible; rank is 1.0\",\n+ \"summary\": f\"Only one eligible commit; {ordered[0]['contributor']} rank 1.0\",\n })\n await broadcast_audit(\n \"ranking\",\n- f\"Only {contributors[0]} is eligible; rank is 1.0\",\n+ f\"Only one eligible commit; {ordered[0]['contributor']} rank 1.0\",\n progress=90,\n phase=\"finalizing\",\n evidence_event_id=completed.event_id,\n evidence_url=_evidence_url(\"event\", completed.event_id),\n links={\"epoch\": _evidence_url(\"epoch\", str(epoch))},\n )\n- return ranking, [], {\n+ return contributor_ranking, [], {\n \"ranking_run_id\": ranking_run_id,\n \"ranking_event_id\": completed.event_id,\n }\n \n if not (OPENROUTER_API_KEY or \"\").strip():\n raise RuntimeError(\n- \"OPENROUTER_API_KEY is required when multiple contributors need ranking\"\n+ \"OPENROUTER_API_KEY is required when multiple commits need ranking\"\n )\n \n models = _find_ranking_models(ranking_run_id)\n if models is None:\n models = await fetch_top_models(n=3)\n if not models:\n- raise RuntimeError(\"no council models available for contributor ranking\")\n+ raise RuntimeError(\"no council models available for commit ranking\")\n await append_evidence(epoch, \"ranking.started\", {\n \"ranking_run_id\": ranking_run_id,\n \"commit_ids\": commit_ids,\n \"contributors\": contributors,\n \"models\": models,\n- \"summary\": f\"Council selected: {', '.join(models)}\",\n+ \"summary\": (\n+ f\"Council selected: {', '.join(models)} — \"\n+ f\"{len(ordered)} commits\"\n+ ),\n })\n await broadcast_audit(\n \"council\",\n- f\"Council selected: {', '.join(models)}\",\n+ f\"Council selected: {', '.join(models)} — ranking {len(ordered)} commits\",\n progress=35,\n phase=\"ranking\",\n )\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n- [\"div.log-council\", f\"Council: {', '.join(models)} — {len(commits)} commits\"]\n+ [\"div.log-council\",\n+ f\"Council: {', '.join(models)} — {len(ordered)} commits\"]\n ]))\n \n- authors = contributors\n- author_commits = {a: [] for a in authors}\n- for row in sorted(commits, key=lambda r: r[\"oid\"]):\n- author_commits[row[\"contributor\"]].append({\n- \"message\": row[\"message\"],\n- \"sha\": row[\"oid\"].split(\":\", 1)[1][:8],\n- \"diff\": row[\"patch\"],\n- \"commit_id\": commit_id_for_oid(row[\"oid\"]),\n- })\n-\n+ sides = [_commit_side_for_llm(row) for row in ordered]\n judgment_ids: list[str] = []\n \n async def compare_fn(i, j):\n- a1, a2 = authors[i], authors[j]\n- side_a = _author_side_for_llm(a1, author_commits)\n- side_b = _author_side_for_llm(a2, author_commits)\n+ side_a, side_b = sides[i], sides[j]\n+ label_a = f\"{side_a['commit_id'][:16]} ({side_a['contributor']})\"\n+ label_b = f\"{side_b['commit_id'][:16]} ({side_b['contributor']})\"\n prompt = build_pairwise_prompt(side_a, side_b)\n comparison_material = {\n \"ranking_run_id\": ranking_run_id,\n \"side_a\": {\n- \"contributor\": a1,\n- \"commit_ids\": side_a[\"commit_ids\"],\n+ \"contributor\": side_a[\"contributor\"],\n+ \"commit_id\": side_a[\"commit_id\"],\n+ \"commit_ids\": [side_a[\"commit_id\"]],\n+ \"oid\": side_a[\"oid\"],\n \"message\": _bytes_blob(side_a[\"message\"]),\n \"diff\": _bytes_blob(side_a[\"diff\"]),\n },\n \"side_b\": {\n- \"contributor\": a2,\n- \"commit_ids\": side_b[\"commit_ids\"],\n+ \"contributor\": side_b[\"contributor\"],\n+ \"commit_id\": side_b[\"commit_id\"],\n+ \"commit_ids\": [side_b[\"commit_id\"]],\n+ \"oid\": side_b[\"oid\"],\n \"message\": _bytes_blob(side_b[\"message\"]),\n \"diff\": _bytes_blob(side_b[\"diff\"]),\n },\n@@ -1650,22 +1669,24 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n comparison_material = {\n **comparison_material,\n \"comparison_id\": comparison_id,\n- \"summary\": f\"Comparing {a1} with {a2}\",\n+ \"summary\": f\"Comparing {label_a} with {label_b}\",\n }\n cmp_ev = await append_evidence(epoch, \"comparison.input\", comparison_material)\n await broadcast_audit(\n \"comparison\",\n- f\"Comparing {a1} with {a2}\",\n+ f\"Comparing commits {label_a} vs {label_b}\",\n phase=\"ranking\",\n evidence_event_id=cmp_ev.event_id,\n evidence_url=_evidence_url(\"comparison\", comparison_id),\n links={\n \"comparison\": _evidence_url(\"comparison\", comparison_id),\n+ \"commit_a\": _evidence_url(\"commit\", side_a[\"commit_id\"]),\n+ \"commit_b\": _evidence_url(\"commit\", side_b[\"commit_id\"]),\n \"epoch\": _evidence_url(\"epoch\", str(epoch)),\n },\n )\n await broadcast_js(exec_event(Three[Selector(\"#emission-status\")][MORPH][\n- [\"div#emission-status\", f\"Comparing {a1} vs {a2}…\"]\n+ [\"div#emission-status\", f\"Comparing {label_a} vs {label_b}…\"]\n ]))\n results = []\n for model in models:\n@@ -1697,9 +1718,15 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n jud_id = (existing or _find_judgment(comparison_id, model) or {}).get(\n \"judgment_id\"\n )\n+ win_label = (\n+ f\"{sides[w]['commit_id'][:16]} ({sides[w]['contributor']})\"\n+ )\n+ lose_label = (\n+ f\"{sides[l]['commit_id'][:16]} ({sides[l]['contributor']})\"\n+ )\n await broadcast_audit(\n \"vote\",\n- f\"{model}: {authors[w]} over {authors[l]} ({result['ratio']})\",\n+ f\"{model}: {win_label} over {lose_label} ({result['ratio']})\",\n phase=\"ranking\",\n evidence_url=(\n _evidence_url(\"judgment\", jud_id) if jud_id else None\n@@ -1714,8 +1741,8 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n [\"div.log-vote\",\n [\"span.model\", model], \" — \",\n- [\"span.winner\", authors[w]], f\" beat \",\n- [\"span.loser\", authors[l]], f\" ({result['ratio']}) \",\n+ [\"span.winner\", win_label], f\" beat \",\n+ [\"span.loser\", lose_label], f\" ({result['ratio']}) \",\n [\"span.explanation\", result[\"explanation\"]],\n ]\n ]))\n@@ -1745,24 +1772,46 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n [\"div#emission-status\", label]\n ]))\n \n- pairs = await pairwise_rank(len(authors), compare_fn, progress_fn)\n+ pairs = await pairwise_rank(len(ordered), compare_fn, progress_fn)\n \n if not pairs:\n- ranking = {authors[0]: Decimal(\"1\")} if authors else {}\n+ commit_score_list = [Decimal(\"1\")]\n else:\n scores = rank_centrality(pairs)\n- ranking = {authors[i]: Decimal(str(scores[i])) for i in range(len(authors))}\n- ranking_rows = sorted(ranking.items(), key=lambda x: x[1], reverse=True)\n+ commit_score_list = [Decimal(str(scores[i])) for i in range(len(ordered))]\n+\n+ commit_ranking = {\n+ commit_ids[i]: str(commit_score_list[i]) for i in range(len(ordered))\n+ }\n+ contributor_totals = _rollup_contributor_scores(ordered, commit_score_list)\n+ contrib_rows = sorted(\n+ contributor_totals.items(), key=lambda x: x[1], reverse=True\n+ )\n+ commit_rows = sorted(\n+ ((commit_ids[i], commit_score_list[i], ordered[i][\"contributor\"])\n+ for i in range(len(ordered))),\n+ key=lambda x: x[1],\n+ reverse=True,\n+ )\n completed = await append_evidence(epoch, \"ranking.completed\", {\n \"ranking_run_id\": ranking_run_id,\n \"models\": models,\n- \"ranking\": {a: str(s) for a, s in ranking_rows},\n+ \"commit_ranking\": commit_ranking,\n+ \"contributor_ranking\": {a: str(s) for a, s in contrib_rows},\n+ \"ranking\": {a: str(s) for a, s in contrib_rows},\n \"judgment_ids\": judgment_ids,\n- \"summary\": \"Ranking: \" + \", \".join(f\"{a} {s:.4f}\" for a, s in ranking_rows),\n+ \"summary\": (\n+ \"Commit ranking: \"\n+ + \", \".join(\n+ f\"{cid[:16]}={float(s):.4f}\" for cid, s, _ in commit_rows[:12]\n+ )\n+ + (\"…\" if len(commit_rows) > 12 else \"\")\n+ ),\n })\n await broadcast_audit(\n \"ranking\",\n- \"Ranking: \" + \", \".join(f\"{a} {s:.4f}\" for a, s in ranking_rows),\n+ \"Contributor rollup: \"\n+ + \", \".join(f\"{a} {s:.4f}\" for a, s in contrib_rows),\n progress=90,\n phase=\"finalizing\",\n evidence_event_id=completed.event_id,\n@@ -1772,10 +1821,10 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n [\"div.log-ranking\",\n [\"b\", \"Ranking: \"],\n- *[[\"span.rank-entry\", f\"{a} {float(s):.3f} \"] for a, s in ranking_rows],\n+ *[[\"span.rank-entry\", f\"{a} {float(s):.3f} \"] for a, s in contrib_rows],\n ]\n ]))\n- return ranking, models, {\n+ return contributor_totals, models, {\n \"ranking_run_id\": ranking_run_id,\n \"ranking_event_id\": completed.event_id,\n }\n@@ -2352,6 +2401,23 @@ async def epoch_detail(epoch: int):\n \n ranking_nodes: list = []\n if ranking_completed:\n+ commit_ranking = ranking_completed.payload.get(\"commit_ranking\") or {}\n+ contrib_ranking = (\n+ ranking_completed.payload.get(\"contributor_ranking\")\n+ or ranking_completed.payload.get(\"ranking\")\n+ or {}\n+ )\n+ commit_rank_links = [\n+ (\n+ f\"{cid[:20]} = {score}\",\n+ _evidence_path(\"commit\", cid),\n+ )\n+ for cid, score in sorted(\n+ commit_ranking.items(),\n+ key=lambda kv: Decimal(str(kv[1])),\n+ reverse=True,\n+ )\n+ ]\n ranking_nodes = [\n [\"p\", ranking_completed.payload.get(\"summary\") or \"ranking completed\"],\n _dl_rows([\n@@ -2362,10 +2428,10 @@ async def epoch_detail(epoch: int):\n ranking_completed.event_id,\n )),\n ]),\n- [\"pre.blob\", json.dumps(\n- ranking_completed.payload.get(\"ranking\") or {},\n- indent=2, sort_keys=True,\n- )],\n+ [\"h3\", \"commit ranking\"],\n+ _link_list(commit_rank_links) if commit_rank_links else [\"p.note\", \"(none)\"],\n+ [\"h3\", \"contributor rollup\"],\n+ [\"pre.blob\", json.dumps(contrib_ranking, indent=2, sort_keys=True)],\n ]\n elif ranking_started:\n ranking_nodes = [[\"p.note\", f\"Ranking started: {ranking_started.event_id}\"]]\n@@ -2386,14 +2452,9 @@ async def epoch_detail(epoch: int):\n else:\n emission_node = [\"p.note\", \"No emission for this epoch.\"]\n \n- contributors = {\n- e.payload.get(\"contributor\")\n- for e in commit_evs\n- if e.payload.get(\"contributor\")\n- }\n no_comparisons_note = \"No comparisons.\"\n- if len(contributors) <= 1:\n- no_comparisons_note += \" Single-contributor — no LLM judgments.\"\n+ if len(commit_evs) <= 1:\n+ no_comparisons_note += \" Fewer than two eligible commits — nothing to pairwise-rank.\"\n \n body = [\n _evidence_nav(),\n@@ -2511,9 +2572,14 @@ async def comparison_detail(comparison_id: str):\n j.payload.get(\"summary\") or jid,\n _evidence_path(\"judgment\", jid),\n ))\n- commit_links = []\n- for cid in (side_a.get(\"commit_ids\") or []) + (side_b.get(\"commit_ids\") or []):\n- commit_links.append((cid, _evidence_path(\"commit\", cid)))\n+ commit_ids = []\n+ for side in (side_a, side_b):\n+ cid = side.get(\"commit_id\")\n+ if cid:\n+ commit_ids.append(cid)\n+ else:\n+ commit_ids.extend(side.get(\"commit_ids\") or [])\n+ commit_links = [(cid, _evidence_path(\"commit\", cid)) for cid in commit_ids]\n return _evidence_page(f\"comparison {comparison_id[:24]}\", [\n _evidence_nav(_a(_evidence_path(\"epoch\", str(ev.epoch)), f\"epoch {ev.epoch}\")),\n [\"div.eyebrow\", \"comparison\"],\n@@ -2522,8 +2588,8 @@ async def comparison_detail(comparison_id: str):\n (\"summary\", p.get(\"summary\")),\n (\"ranking_run_id\", p.get(\"ranking_run_id\")),\n (\"evidence_event\", _a(_evidence_path(\"event\", ev.event_id), ev.event_id)),\n- (\"side_a\", side_a.get(\"contributor\")),\n- (\"side_b\", side_b.get(\"contributor\")),\n+ (\"side_a\", f\"{side_a.get('commit_id', '?')} ({side_a.get('contributor', '?')})\"),\n+ (\"side_b\", f\"{side_b.get('commit_id', '?')} ({side_b.get('contributor', '?')})\"),\n ]),\n [\"p\", _a(f\"/comparisons/{comparison_id}/prompt\", \"download prompt\")],\n [\"h2\", \"commits\"],\n@@ -2534,12 +2600,12 @@ async def comparison_detail(comparison_id: str):\n _link_list(judgment_links),\n [\"h2\", \"prompt\"],\n _pre_blob(_blob_text(p.get(\"prompt\"))),\n- [\"h2\", f\"side A — {side_a.get('contributor', '?')}\"],\n+ [\"h2\", f\"side A — {side_a.get('commit_id', side_a.get('contributor', '?'))}\"],\n [\"h3\", \"message\"],\n _pre_blob(_blob_text(side_a.get(\"message\"))),\n [\"h3\", \"diff\"],\n _pre_blob(_blob_text(side_a.get(\"diff\"))),\n- [\"h2\", f\"side B — {side_b.get('contributor', '?')}\"],\n+ [\"h2\", f\"side B — {side_b.get('commit_id', side_b.get('contributor', '?'))}\"],\n [\"h3\", \"message\"],\n _pre_blob(_blob_text(side_b.get(\"message\"))),\n [\"h3\", \"diff\"],\n@@ -3156,9 +3222,9 @@ async def watch():\n ],\n [\"p.note\",\n (\n- \"Pairwise council voting is ready.\"\n+ \"Pairwise council ranks every eligible commit.\"\n if key_ok else\n- \"Single-contributor epochs can finalize, but contested rankings require OPENROUTER_API_KEY.\"\n+ \"Epochs with two or more eligible commits require OPENROUTER_API_KEY.\"\n )\n ],\n ],\ndiff --git a/tests/integration.clj b/tests/integration.clj\nindex 11140e95a8cd86768a661a30e6b29a3ddfc95fcc..39b2476cb5ddf80733769f61343c81ba7287a974 100644\n--- a/tests/integration.clj\n+++ b/tests/integration.clj\n@@ -507,7 +507,7 @@\n \n (bind or-state @(:state or-mock))\n (assert! (pos? (:model-requests or-state)) \"OpenRouter /models was called\")\n- (assert! (>= (:compare-requests or-state) 3) \"at least 3 pairwise LLM calls (2 authors × 3 models)\")\n+ (assert! (>= (:compare-requests or-state) 3) \"at least 3 pairwise LLM calls (2 commits × 3 models)\")\n \n (bind ledger2 (get-json base-url \"/api/ledger\"))\n (assert! (>= (count ledger2) 3)\n@@ -543,6 +543,8 @@\n \"epoch page links comparisons\")\n (assert! (str/includes? epoch-html \"/judgments/\")\n \"epoch page links judgments\")\n+ (assert! (str/includes? epoch-html \"commit ranking\")\n+ \"epoch page shows per-commit ranking\")\n (bind commit-href\n (second (re-find #\"/commits/(c_[a-f0-9]+)\" epoch-html)))\n (assert! (some? commit-href) \"found a commit id on epoch page\")\ndiff --git a/tests/test_git_discovery.py b/tests/test_git_discovery.py\nindex 00d26bfa85737b178c8822804e278211974e0efe..0f002a58bd9a122d41c5a85e32c16ed2b4404d2f 100644\n--- a/tests/test_git_discovery.py\n+++ b/tests/test_git_discovery.py\n@@ -434,7 +434,7 @@ def test_emission_distribution_sums_exactly_to_total(\n assert entry.discovery_snapshot_id == \"ranked-snapshot\"\n \n \n-def test_single_contributor_ranking_is_total_and_uses_no_pairwise_votes(\n+def test_single_commit_ranking_skips_pairwise(\n discovery_config, monkeypatch,\n ):\n monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n@@ -454,6 +454,65 @@ def test_single_contributor_ranking_is_total_and_uses_no_pairwise_votes(\n assert info[\"ranking_event_id\"]\n \n \n+def test_same_contributor_multiple_commits_runs_pairwise(\n+ discovery_config, monkeypatch,\n+):\n+ monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n+ calls = {\"n\": 0}\n+\n+ async def models(n=3):\n+ return [\"m1\", \"m2\", \"m3\"]\n+\n+ async def compare(model_id, side_a, side_b, **kwargs):\n+ calls[\"n\"] += 1\n+ assert \"commit_id\" in side_a and \"commit_id\" in side_b\n+ if kwargs.get(\"persist\"):\n+ attempt_id = c.attempt_id_for(kwargs[\"comparison_id\"], model_id, 1)\n+ await c.append_evidence(kwargs[\"epoch\"], \"llm.judgment\", {\n+ \"judgment_id\": c.judgment_id_for({\n+ \"attempt_id\": attempt_id,\n+ \"comparison_id\": kwargs[\"comparison_id\"],\n+ \"model_id\": model_id,\n+ \"winner\": \"A\",\n+ \"ratio\": \"2:1\",\n+ \"explanation\": \"ok\",\n+ }),\n+ \"attempt_id\": attempt_id,\n+ \"comparison_id\": kwargs[\"comparison_id\"],\n+ \"model_id\": model_id,\n+ \"winner\": \"A\",\n+ \"ratio\": \"2:1\",\n+ \"explanation\": \"ok\",\n+ \"summary\": \"ok\",\n+ })\n+ return {\"winner\": \"A\", \"ratio\": \"2:1\", \"explanation\": \"ok\"}\n+\n+ monkeypatch.setattr(c, \"fetch_top_models\", models)\n+ monkeypatch.setattr(c, \"llm_pairwise_compare\", compare)\n+ monkeypatch.setattr(c, \"OPENROUTER_API_KEY\", \"test-key\")\n+ commits = [\n+ {\n+ \"contributor\": \"alice\",\n+ \"oid\": \"sha1:\" + char * 40,\n+ \"message\": f\"msg-{char}\",\n+ \"patch\": f\"patch-{char}\",\n+ }\n+ for char in (\"a\", \"b\", \"c\")\n+ ]\n+ ranking, used, info = asyncio.run(c.rank_commits(commits, epoch=0))\n+ assert set(ranking) == {\"alice\"}\n+ assert ranking[\"alice\"] > 0\n+ assert used == [\"m1\", \"m2\", \"m3\"]\n+ assert calls[\"n\"] >= 3\n+ completed = next(\n+ e for e in c.store.read()\n+ if isinstance(e, c.Evidence) and e.kind == \"ranking.completed\"\n+ )\n+ assert len(completed.payload[\"commit_ranking\"]) == 3\n+ assert \"alice\" in completed.payload[\"contributor_ranking\"]\n+ assert info[\"ranking_event_id\"]\n+\n+\n def test_any_council_failure_aborts_ranking(discovery_config, monkeypatch):\n monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n \n@@ -479,16 +538,16 @@ def test_any_council_failure_aborts_ranking(discovery_config, monkeypatch):\n asyncio.run(c.rank_commits(commits, epoch=0))\n \n \n-def test_contested_ranking_requires_openrouter_key(monkeypatch):\n+def test_multi_commit_ranking_requires_openrouter_key(monkeypatch):\n monkeypatch.setattr(c, \"OPENROUTER_API_KEY\", \"\")\n commits = [\n {\n- \"contributor\": contributor,\n+ \"contributor\": \"alice\",\n \"oid\": \"sha1:\" + char * 40,\n- \"message\": contributor,\n+ \"message\": char,\n \"patch\": \"patch\",\n }\n- for contributor, char in [(\"alice\", \"a\"), (\"bob\", \"b\")]\n+ for char in (\"a\", \"b\")\n ]\n with pytest.raises(RuntimeError, match=\"OPENROUTER_API_KEY\"):\n asyncio.run(c.rank_commits(commits))\n","role":"user"}],"model":"openai/gpt-chat-latest"}