{"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[8d8230d1] reddit\n\nSide A — unified diff (full patch):\ndiff --git a/.gitignore b/.gitignore\nindex 4c7073f9fac0c30fd2050d79a60ef447af58ebeb..ada462e900d24a3a6d08165d158c80f79c35a5a5 100644\n--- a/.gitignore\n+++ b/.gitignore\n@@ -7,3 +7,4 @@\n data/\n repomix-output.xml\n dev-data/\n+.env\ndiff --git a/server/Cargo.toml b/server/Cargo.toml\nindex 7906a8547d56b8e6a48ef59c37aa82a8510fdee9..4677fedcb45292eebebe7e9cf6ce2f5738f18ddf 100644\n--- a/server/Cargo.toml\n+++ b/server/Cargo.toml\n@@ -16,6 +16,7 @@ tower = \"0.5\"\n tower-http = { version = \"0.5\", features = [\"trace\"] }\n tracing = \"0.1\"\n tracing-subscriber = { version = \"0.3\", features = [\"env-filter\"] }\n+reqwest = { version = \"0.12\", features = [\"json\"] }\n \n [dev-dependencies]\n reqwest = { version = \"0.12\", features = [\"json\"] }\ndiff --git a/server/src/html/mod.rs b/server/src/html/mod.rs\nindex 2864407ed6e8ec284a1dc663acf1805534566b62..df6505021d9f446c2b453e20e3eb3cf696a111f9 100644\n--- a/server/src/html/mod.rs\n+++ b/server/src/html/mod.rs\n@@ -272,5 +272,16 @@ pub async fn home(State(state): State, uri: Uri) -> impl IntoResponse\n \n pub async fn browse(State(state): State, uri: Uri) -> impl IntoResponse {\n let item = ItemId::from_browse_uri(uri.path()).unwrap_or(ItemId::root());\n+ if item.as_str().starts_with(\"reddit.com\") {\n+ let needs_fetch = {\n+ let tree = state.tree.read().await;\n+ tree.get(&item)\n+ .map(|n| n.data.is_none())\n+ .unwrap_or(true)\n+ };\n+ if needs_fetch {\n+ state.reddit.request_fetch(item.clone());\n+ }\n+ }\n item_page(state, uri, item).await\n }\ndiff --git a/server/src/reddit.rs b/server/src/reddit.rs\nindex d203dca09245daf869b3aa942898447700ae69fb..90053ad03b1d7c8e94f325dd4ee64c2b4f7da900 100644\n--- a/server/src/reddit.rs\n+++ b/server/src/reddit.rs\n@@ -1,4 +1,12 @@\n-//! Reddit API import (async, decoupled from UI request path).\n+//! Reddit API import via a single background worker (rate limits, dedup, backoff).\n+\n+use std::collections::{HashMap, HashSet};\n+use std::sync::Arc;\n+use std::time::{Duration, Instant};\n+\n+use reqwest::{header, Client, StatusCode};\n+use serde::Deserialize;\n+use tokio::sync::{mpsc, RwLock};\n \n use crate::{\n path_types::ItemId,\n@@ -10,12 +18,401 @@ pub fn ensure_partial_tree(tree: &mut GlobalTree, id: &ItemId) {\n tree.ensure_path(id);\n }\n \n-/// Placeholder for Reddit JSON import. Returns entity data when implemented.\n-pub async fn fetch_reddit_entity(_id: &ItemId) -> Option {\n- None\n+pub struct RedditCommand {\n+ pub id: ItemId,\n+}\n+\n+#[derive(Clone)]\n+pub struct RedditBroker {\n+ tx: mpsc::Sender,\n+}\n+\n+#[derive(Clone)]\n+struct RedditCredentials {\n+ client_id: String,\n+ client_secret: String,\n+}\n+\n+struct OAuthToken {\n+ access_token: String,\n+ expires_at: Instant,\n+}\n+\n+impl RedditBroker {\n+ pub fn spawn(tree: Arc>, user_agent: &str) -> Self {\n+ let (tx, rx) = mpsc::channel(100);\n+\n+ let mut headers = header::HeaderMap::new();\n+ headers.insert(\n+ header::USER_AGENT,\n+ header::HeaderValue::from_str(user_agent).expect(\"valid user agent\"),\n+ );\n+\n+ let client = Client::builder()\n+ .default_headers(headers)\n+ .timeout(Duration::from_secs(15))\n+ .build()\n+ .expect(\"reqwest client\");\n+\n+ let creds = RedditCredentials::from_env();\n+ tokio::spawn(reddit_worker(rx, tree, client, creds));\n+\n+ Self { tx }\n+ }\n+\n+ /// Fire-and-forget: queue a fetch; worker updates the tree when done.\n+ pub fn request_fetch(&self, id: ItemId) {\n+ let _ = self.tx.try_send(RedditCommand { id });\n+ }\n+}\n+\n+impl RedditCredentials {\n+ fn from_env() -> Option {\n+ let client_id = std::env::var(\"REDDIT_CLIENT_ID\").ok()?;\n+ let client_secret = std::env::var(\"REDDIT_CLIENT_SECRET\").ok()?;\n+ if client_id.is_empty() || client_secret.is_empty() {\n+ return None;\n+ }\n+ Some(Self {\n+ client_id,\n+ client_secret,\n+ })\n+ }\n+}\n+\n+pub fn default_user_agent() -> String {\n+ std::env::var(\"REDDIT_USER_AGENT\").unwrap_or_else(|_| {\n+ \"web:sorter2.social:v0.0.1 (by /u/sorter2)\".to_string()\n+ })\n }\n \n-/// Apply fetched entity data to a node (called from async worker).\n-pub fn apply_entity(tree: &mut GlobalTree, id: &ItemId, data: EntityData) {\n- tree.set_entity_data(id, data);\n+async fn reddit_worker(\n+ mut rx: mpsc::Receiver,\n+ tree: Arc>,\n+ client: Client,\n+ creds: Option,\n+) {\n+ let mut in_flight = HashSet::new();\n+ let mut recently_fetched: HashMap = HashMap::new();\n+ let mut current_delay = Duration::from_secs(1);\n+ let mut oauth: Option = None;\n+ let cache_ttl = Duration::from_secs(300);\n+\n+ while let Some(cmd) = rx.recv().await {\n+ let now = Instant::now();\n+ recently_fetched.retain(|_, t| now.duration_since(*t) < cache_ttl);\n+\n+ if in_flight.contains(&cmd.id) || recently_fetched.contains_key(&cmd.id) {\n+ continue;\n+ }\n+\n+ in_flight.insert(cmd.id.clone());\n+ let fetch_id = cmd.id.clone();\n+\n+ tokio::time::sleep(current_delay).await;\n+\n+ if let Some(c) = &creds {\n+ oauth = ensure_oauth_token(&client, c, oauth.take()).await;\n+ }\n+\n+ let token = oauth.as_ref().map(|t| t.access_token.as_str());\n+ let use_oauth = token.is_some();\n+\n+ match do_fetch(&client, &fetch_id, use_oauth, token).await {\n+ Ok(FetchOutcome::Entity(data)) => {\n+ let mut w = tree.write().await;\n+ w.set_entity_data(&fetch_id, data);\n+ recently_fetched.insert(fetch_id.clone(), Instant::now());\n+ current_delay = Duration::from_millis(600);\n+ }\n+ Ok(FetchOutcome::NotFound) => {\n+ recently_fetched.insert(fetch_id.clone(), Instant::now());\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+ );\n+ tokio::time::sleep(wait).await;\n+ current_delay = (current_delay * 2).min(Duration::from_secs(60));\n+ }\n+ Err(e) => {\n+ tracing::warn!(\"Reddit fetch failed for {}: {}\", fetch_id, e);\n+ current_delay = (current_delay * 2).min(Duration::from_secs(60));\n+ }\n+ }\n+\n+ in_flight.remove(&fetch_id);\n+ }\n+}\n+\n+enum FetchOutcome {\n+ Entity(EntityData),\n+ NotFound,\n+ RateLimited { reset_secs: u64 },\n+}\n+\n+async fn ensure_oauth_token(\n+ client: &Client,\n+ creds: &RedditCredentials,\n+ existing: Option,\n+) -> Option {\n+ if let Some(t) = existing {\n+ if Instant::now() < t.expires_at - Duration::from_secs(60) {\n+ return Some(t);\n+ }\n+ }\n+\n+ let resp = client\n+ .post(\"https://www.reddit.com/api/v1/access_token\")\n+ .basic_auth(&creds.client_id, Some(&creds.client_secret))\n+ .form(&[(\"grant_type\", \"client_credentials\")])\n+ .send()\n+ .await;\n+\n+ let resp = match resp {\n+ Ok(r) => r,\n+ Err(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+ return None;\n+ }\n+\n+ #[derive(Deserialize)]\n+ struct TokenResponse {\n+ access_token: String,\n+ expires_in: u64,\n+ }\n+\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+ return None;\n+ }\n+ };\n+\n+ Some(OAuthToken {\n+ access_token: body.access_token,\n+ expires_at: Instant::now() + Duration::from_secs(body.expires_in),\n+ })\n+}\n+\n+async fn do_fetch(\n+ client: &Client,\n+ id: &ItemId,\n+ use_oauth: bool,\n+ bearer: Option<&str>,\n+) -> Result {\n+ let url = map_item_to_reddit_api(id, use_oauth);\n+ if url.is_empty() {\n+ return Ok(FetchOutcome::NotFound);\n+ }\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+\n+ if resp.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+ }\n+\n+ if !resp.status().is_success() {\n+ return Ok(FetchOutcome::NotFound);\n+ }\n+\n+ if rate_limit_remaining(&resp) == Some(0) {\n+ let reset = rate_limit_reset_secs(&resp);\n+ return Ok(FetchOutcome::RateLimited { reset_secs: reset });\n+ }\n+\n+ let bytes = resp.bytes().await.map_err(|e| e.to_string())?;\n+ Ok(parse_reddit_json(id, &bytes)\n+ .map(FetchOutcome::Entity)\n+ .unwrap_or(FetchOutcome::NotFound))\n+}\n+\n+fn rate_limit_remaining(resp: &reqwest::Response) -> Option {\n+ resp.headers()\n+ .get(\"x-ratelimit-remaining\")\n+ .and_then(|v| v.to_str().ok())\n+ .and_then(|s| s.parse::().ok())\n+ .map(|f| f.floor() as u64)\n+}\n+\n+fn rate_limit_reset_secs(resp: &reqwest::Response) -> u64 {\n+ resp.headers()\n+ .get(\"x-ratelimit-reset\")\n+ .and_then(|v| v.to_str().ok())\n+ .and_then(|s| s.parse::().ok())\n+ .map(|f| f.ceil() as u64)\n+ .unwrap_or(5)\n+}\n+\n+/// Map canonical item id to Reddit JSON API URL.\n+pub fn map_item_to_reddit_api(id: &ItemId, oauth: bool) -> String {\n+ let path = id.as_str();\n+ if !path.starts_with(\"reddit.com/\") && path != \"reddit.com\" {\n+ return String::new();\n+ }\n+\n+ let base = if oauth {\n+ \"https://oauth.reddit.com\"\n+ } else {\n+ \"https://www.reddit.com\"\n+ };\n+\n+ let segments: Vec<&str> = path.split('/').collect();\n+\n+ if let Some(i) = segments.iter().position(|&p| p == \"comments\") {\n+ if segments.len() > i + 1 {\n+ let api_path = segments[1..=i + 1].join(\"/\");\n+ return format!(\"{base}/{api_path}.json?raw_json=1\");\n+ }\n+ }\n+\n+ if segments.len() == 3 && segments[1] == \"r\" {\n+ return format!(\"{base}/r/{}/about.json?raw_json=1\", segments[2]);\n+ }\n+\n+ String::new()\n+}\n+\n+fn parse_reddit_json(id: &ItemId, bytes: &[u8]) -> Option {\n+ let v: serde_json::Value = serde_json::from_slice(bytes).ok()?;\n+ let segments: Vec<&str> = id.as_str().split('/').collect();\n+\n+ if segments.iter().any(|&p| p == \"comments\") {\n+ parse_post_listing(&v)\n+ } else {\n+ parse_subreddit_about(&v)\n+ }\n+}\n+\n+fn parse_subreddit_about(v: &serde_json::Value) -> Option {\n+ let data = v.get(\"data\")?;\n+ let title = data\n+ .get(\"title\")\n+ .or_else(|| data.get(\"display_name\"))\n+ .and_then(|t| t.as_str())?\n+ .to_string();\n+ let body_html = data\n+ .get(\"public_description_html\")\n+ .or_else(|| data.get(\"public_description\"))\n+ .and_then(|t| t.as_str())\n+ .map(|s| s.to_string());\n+ let thumb_url = data\n+ .get(\"icon_img\")\n+ .or_else(|| data.get(\"community_icon\"))\n+ .and_then(|t| t.as_str())\n+ .filter(|s| !s.is_empty())\n+ .map(|s| s.to_string());\n+\n+ Some(EntityData {\n+ title,\n+ author: None,\n+ body_html,\n+ thumb_url,\n+ })\n+}\n+\n+fn parse_post_listing(v: &serde_json::Value) -> Option {\n+ let listing = v.as_array()?.first()?;\n+ let child = listing\n+ .pointer(\"/data/children/0/data\")?;\n+ let title = child.get(\"title\")?.as_str()?.to_string();\n+ let author = child\n+ .get(\"author\")\n+ .and_then(|a| a.as_str())\n+ .filter(|a| *a != \"[deleted]\")\n+ .map(|s| s.to_string());\n+ let body_html = child\n+ .get(\"selftext_html\")\n+ .and_then(|t| t.as_str())\n+ .filter(|s| !s.is_empty())\n+ .map(|s| s.to_string());\n+ let thumb_url = child\n+ .get(\"thumbnail\")\n+ .and_then(|t| t.as_str())\n+ .filter(|s| s.starts_with(\"http\"))\n+ .map(|s| s.to_string());\n+\n+ Some(EntityData {\n+ title,\n+ author,\n+ body_html,\n+ thumb_url,\n+ })\n+}\n+\n+#[cfg(test)]\n+mod tests {\n+ use super::*;\n+\n+ #[test]\n+ fn map_subreddit_about_url() {\n+ let id = ItemId::parse(\"reddit.com/r/rust\").unwrap();\n+ assert_eq!(\n+ map_item_to_reddit_api(&id, false),\n+ \"https://www.reddit.com/r/rust/about.json?raw_json=1\"\n+ );\n+ assert_eq!(\n+ map_item_to_reddit_api(&id, true),\n+ \"https://oauth.reddit.com/r/rust/about.json?raw_json=1\"\n+ );\n+ }\n+\n+ #[test]\n+ fn map_post_url() {\n+ let id =\n+ ItemId::parse(\"reddit.com/r/amitheasshole/comments/1trnvdl\").unwrap();\n+ assert_eq!(\n+ map_item_to_reddit_api(&id, false),\n+ \"https://www.reddit.com/r/amitheasshole/comments/1trnvdl.json?raw_json=1\"\n+ );\n+ }\n+\n+ #[test]\n+ fn map_non_reddit_empty() {\n+ let id = ItemId::opaque(\"example.com/foo\");\n+ assert!(map_item_to_reddit_api(&id, false).is_empty());\n+ }\n+\n+ #[test]\n+ fn parse_subreddit_fixture() {\n+ let json = r#\"{\"kind\":\"t5\",\"data\":{\"title\":\"Rust\",\"display_name\":\"rust\",\"public_description\":\"systems\"}}\"#;\n+ let entity = parse_reddit_json(\n+ &ItemId::parse(\"reddit.com/r/rust\").unwrap(),\n+ json.as_bytes(),\n+ )\n+ .unwrap();\n+ assert_eq!(entity.title, \"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 entity = parse_reddit_json(\n+ &ItemId::parse(\"reddit.com/r/x/comments/abc\").unwrap(),\n+ json.as_bytes(),\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 cc1722f5a5bf4d415f2327ea585c488a15a75592..8c03aa60c15aee803a534439400b69935b1a3d84 100644\n--- a/server/src/state.rs\n+++ b/server/src/state.rs\n@@ -5,9 +5,10 @@ use tokio::sync::RwLock;\n use crate::{\n event_log::EventLog,\n events::Event,\n+ journal::JournalClient,\n path_types::ItemId,\n+ reddit::{default_user_agent, RedditBroker},\n reducer::{GlobalTree, VoteData},\n- journal::JournalClient,\n views::ViewStore,\n };\n \n@@ -73,6 +74,7 @@ pub struct AppState {\n pub views: ViewStore,\n pub tree: Arc>,\n journal: JournalClient,\n+ pub reddit: RedditBroker,\n }\n \n impl AppState {\n@@ -112,6 +114,7 @@ impl AppState {\n \n let tree = Arc::new(RwLock::new(tree));\n let journal = JournalClient::spawn(tree.clone(), event_log.clone());\n+ let reddit = RedditBroker::spawn(tree.clone(), &default_user_agent());\n \n Self {\n cfg: Arc::new(cfg),\n@@ -119,6 +122,7 @@ impl AppState {\n views,\n tree,\n journal,\n+ reddit,\n }\n }\n \n@@ -127,8 +131,11 @@ impl AppState {\n id: id.as_str().to_string(),\n };\n self.event_log.append(&event).await.map_err(|e| e.to_string())?;\n- let mut w = self.tree.write().await;\n- w.ensure_path(id);\n+ {\n+ let mut w = self.tree.write().await;\n+ w.ensure_path(id);\n+ }\n+ self.reddit.request_fetch(id.clone());\n Ok(())\n }\n \ndiff --git a/todo b/todo\nnew file mode 100644\nindex 0000000000000000000000000000000000000000..d196e8cb4cc80ccb95eeff01c73607d520e13212\n--- /dev/null\n+++ b/todo\n@@ -0,0 +1,7 @@\n+reddit import (only on explicit request)\n+reddit rendering\n+vote redering\n+pair chosing\n+nsfw gate\n+\n+logins (uuid user, two sides, oauths, and pseudonyms)\n\n\nSide B — contributor: tommy-mor\nSide B — commit message:\n[8f69c309] Require Reddit OAuth when credentials are set and refresh on 401/403.\n\nAvoid falling back to the public www.reddit.com API from cloud IPs, which\nreturns Reddit's network-security block page. Also pin SORTER2_BASE_URL in fly.toml.\n\nCo-authored-by: Cursor \n\nSide B — unified diff (full patch):\ndiff --git a/fly.toml b/fly.toml\nindex f0c6a39f643c204987debc177234d95a8ef44b65..ca7e0088a7efd7d58d29d808f8d89080b2bff233 100644\n--- a/fly.toml\n+++ b/fly.toml\n@@ -5,6 +5,7 @@ primary_region = \"iad\"\n dockerfile = \"Dockerfile\"\n \n [env]\n+ SORTER2_BASE_URL = \"https://reddit.sorter.social\"\n SORTER2_DATA_DIR = \"/data\"\n SORTER2_EVENT_LOG = \"/data/events.jsonl\"\n PORT = \"8080\"\ndiff --git a/server/src/reddit.rs b/server/src/reddit.rs\nindex a874814f8927192ee62cab2d0db1efd27dcd57b7..f409764c1e1f36216f1b08107043c2eab905694c 100644\n--- a/server/src/reddit.rs\n+++ b/server/src/reddit.rs\n@@ -283,26 +283,35 @@ async fn reddit_worker(\n );\n tokio::time::sleep(current_delay).await;\n \n- if let Some(c) = &creds {\n- oauth = ensure_oauth_token(&client, &oauth_token_base, c, oauth.take()).await;\n- }\n-\n- let token = oauth.as_ref().map(|t| t.access_token.as_str());\n- let fetch_base = if token.is_some() {\n- tracing::debug!(\n- item = %fetch_id,\n- base = %oauth_api_base,\n- \"reddit fetch using OAuth bearer\"\n- );\n- &oauth_api_base\n- } else {\n- &api_base\n- };\n- let url = match kind {\n- FetchKind::SelfEntity => map_item_to_reddit_api(&fetch_id, fetch_base),\n- FetchKind::Children => map_children_url(&fetch_id, fetch_base),\n+ let outcome = match &creds {\n+ Some(c) => {\n+ // OAuth is required when credentials are configured — never fall\n+ // back to the public www.reddit.com JSON endpoints (cloud IPs\n+ // get blocked with a 403 HTML interstitial).\n+ fetch_with_oauth(\n+ &client,\n+ &oauth_token_base,\n+ &oauth_api_base,\n+ c,\n+ &mut oauth,\n+ &fetch_id,\n+ kind,\n+ )\n+ .await\n+ }\n+ None => {\n+ let url = match kind {\n+ FetchKind::SelfEntity => map_item_to_reddit_api(&fetch_id, &api_base),\n+ FetchKind::Children => map_children_url(&fetch_id, &api_base),\n+ };\n+ match do_fetch(&client, &url, &fetch_id, None).await {\n+ Ok(FetchOutcome::AuthRejected { status, detail }) => {\n+ Err(format!(\"Reddit API {status}: {detail}\"))\n+ }\n+ other => other,\n+ }\n+ }\n };\n- let outcome = do_fetch(&client, &url, &fetch_id, token).await;\n \n match outcome {\n Ok(FetchOutcome::Payload(payload)) => {\n@@ -342,6 +351,12 @@ async fn reddit_worker(\n current_delay = (current_delay * 2).min(Duration::from_secs(60));\n notify(done, FetchJobResult::RateLimited { reset_secs });\n }\n+ Ok(FetchOutcome::AuthRejected { status, detail }) => {\n+ let e = format!(\"Reddit API {status}: {detail}\");\n+ tracing::warn!(item = %fetch_id, err = %e, \"reddit fetch auth rejected\");\n+ current_delay = (current_delay * 2).min(Duration::from_secs(60));\n+ notify(done, FetchJobResult::Failed(e));\n+ }\n Err(e) => {\n tracing::warn!(item = %fetch_id, err = %e, \"reddit fetch failed\");\n current_delay = (current_delay * 2).min(Duration::from_secs(60));\n@@ -357,6 +372,60 @@ enum FetchOutcome {\n Payload(Value),\n NotFound,\n RateLimited { reset_secs: u64 },\n+ /// Bearer rejected — caller should drop the cached token and retry once.\n+ AuthRejected { status: StatusCode, detail: String },\n+}\n+\n+async fn fetch_with_oauth(\n+ client: &Client,\n+ oauth_token_base: &str,\n+ oauth_api_base: &str,\n+ creds: &RedditCredentials,\n+ oauth: &mut Option,\n+ fetch_id: &ItemId,\n+ kind: FetchKind,\n+) -> Result {\n+ for attempt in 0..2 {\n+ let force_refresh = attempt > 0;\n+ *oauth = Some(\n+ ensure_oauth_token(client, oauth_token_base, creds, oauth.take(), force_refresh)\n+ .await?,\n+ );\n+ let token = oauth\n+ .as_ref()\n+ .expect(\"token set above\")\n+ .access_token\n+ .clone();\n+\n+ tracing::debug!(\n+ item = %fetch_id,\n+ base = %oauth_api_base,\n+ attempt,\n+ \"reddit fetch using OAuth bearer\"\n+ );\n+\n+ let url = match kind {\n+ FetchKind::SelfEntity => map_item_to_reddit_api(fetch_id, oauth_api_base),\n+ FetchKind::Children => map_children_url(fetch_id, oauth_api_base),\n+ };\n+ match do_fetch(client, &url, fetch_id, Some(&token)).await? {\n+ FetchOutcome::AuthRejected { status, detail } if attempt == 0 => {\n+ tracing::warn!(\n+ item = %fetch_id,\n+ %status,\n+ %detail,\n+ \"reddit OAuth rejected; refreshing token and retrying\"\n+ );\n+ *oauth = None;\n+ continue;\n+ }\n+ FetchOutcome::AuthRejected { status, detail } => {\n+ return Err(format!(\"Reddit API {status}: {detail}\"));\n+ }\n+ other => return Ok(other),\n+ }\n+ }\n+ unreachable!(\"loop always returns\")\n }\n \n async fn ensure_oauth_token(\n@@ -364,35 +433,35 @@ async fn ensure_oauth_token(\n oauth_base: &str,\n creds: &RedditCredentials,\n existing: Option,\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+ force_refresh: bool,\n+) -> Result {\n+ if !force_refresh {\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 Ok(t);\n+ }\n }\n }\n \n let url = format!(\"{}/api/v1/access_token\", oauth_base.trim_end_matches('/'));\n- tracing::debug!(%url, \"reddit OAuth token request\");\n+ tracing::debug!(%url, force_refresh, \"reddit OAuth token request\");\n \n let resp = client\n .post(&url)\n .basic_auth(&creds.client_id, Some(&creds.client_secret))\n .form(&[(\"grant_type\", \"client_credentials\")])\n .send()\n- .await;\n-\n- let resp = match resp {\n- Ok(r) => r,\n- Err(e) => {\n- tracing::warn!(\"reddit OAuth token request failed: {e}\");\n- return None;\n- }\n- };\n+ .await\n+ .map_err(|e| format!(\"Reddit OAuth token request failed: {e}\"))?;\n \n if !resp.status().is_success() {\n- tracing::warn!(\"reddit OAuth token HTTP {}\", resp.status());\n- return None;\n+ let status = resp.status();\n+ let body = resp.text().await.unwrap_or_default();\n+ return Err(format!(\n+ \"Reddit OAuth token HTTP {status}: {}\",\n+ truncate_for_error(&body)\n+ ));\n }\n \n #[derive(Deserialize)]\n@@ -401,21 +470,40 @@ async fn ensure_oauth_token(\n expires_in: u64,\n }\n \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- return None;\n- }\n- };\n+ let body: TokenResponse = resp\n+ .json()\n+ .await\n+ .map_err(|e| format!(\"Reddit OAuth token parse failed: {e}\"))?;\n \n- tracing::debug!(expires_in = body.expires_in, \"reddit OAuth token acquired\");\n- Some(OAuthToken {\n+ tracing::info!(expires_in = body.expires_in, \"reddit OAuth token acquired\");\n+ Ok(OAuthToken {\n access_token: body.access_token,\n expires_at: Instant::now() + Duration::from_secs(body.expires_in),\n })\n }\n \n+fn truncate_for_error(body: &str) -> String {\n+ let compact: String = body.split_whitespace().collect::>().join(\" \");\n+ if compact.is_empty() {\n+ return \"(empty body)\".into();\n+ }\n+ // Prefer the human-readable block message over dumping Reddit's CSS.\n+ if let Some(idx) = compact.find(\"You've been blocked\") {\n+ let slice: String = compact.chars().skip(idx).take(160).collect();\n+ return if compact.chars().count() > idx + 160 {\n+ format!(\"{slice}…\")\n+ } else {\n+ slice\n+ };\n+ }\n+ let chars: String = compact.chars().take(200).collect();\n+ if compact.chars().count() > 200 {\n+ format!(\"{chars}…\")\n+ } else {\n+ chars\n+ }\n+}\n+\n async fn do_fetch(\n client: &Client,\n url: &str,\n@@ -460,15 +548,16 @@ async fn do_fetch(\n \n if !status.is_success() {\n let body = resp.text().await.unwrap_or_default();\n+ let detail = truncate_for_error(&body);\n tracing::debug!(\n item = %id,\n %status,\n body_len = body.len(),\n- body_prefix = %body.chars().take(240).collect::(),\n+ %detail,\n \"reddit non-success body\"\n );\n if status == StatusCode::FORBIDDEN || status == StatusCode::UNAUTHORIZED {\n- return Err(format!(\"Reddit API {status}: {body}\"));\n+ return Ok(FetchOutcome::AuthRejected { status, detail });\n }\n return Ok(FetchOutcome::NotFound);\n }\n@@ -716,6 +805,15 @@ fn reddit_direct_image_url(url: &str) -> bool {\n mod tests {\n use super::*;\n \n+ #[test]\n+ fn truncate_error_prefers_block_message() {\n+ let html = r#\"
You've been blocked by network security. To continue, log in
\"#;\n+ let msg = truncate_for_error(html);\n+ assert!(msg.starts_with(\"You've been blocked\"));\n+ assert!(msg.len() < 200);\n+ assert!(!msg.contains(\".x{color\"));\n+ }\n+\n #[test]\n fn map_subreddit_about_url() {\n let id = ItemId::from_url(\"https://reddit.com/r/rust\").unwrap();\n","role":"user"}],"model":"~anthropic/claude-sonnet-latest"}