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: [52f5c51c] Add Reddit OAuth linking and make UUID the only account identity. OAuth providers only attach to a session UUID (first link creates the principal); linked providers stay private on the account page. Co-authored-by: Cursor Side A — unified diff (full patch): diff --git a/AGENTS.md b/AGENTS.md index 6e0fd8ebb65d665c9c1438e3275971d62b98fd95..e9cc3173dbeb21ad0fc090ca7b407b027c7820a9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -35,8 +35,11 @@ Environment variables (defaults in `server/src/state.rs`): - `SORTER2_DATA_DIR` — default `./data` (created on startup) - `SORTER2_EVENT_LOG` — default `{data_dir}/events.jsonl` - `SORTER2_BASE_URL` — public origin (also drives Secure cookies when `https://`) -- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth (optional; login disabled if unset) -- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` (tests only) +- `GITHUB_CLIENT_ID` / `GITHUB_CLIENT_SECRET` — GitHub OAuth linking (optional) +- `REDDIT_CLIENT_ID` / `REDDIT_CLIENT_SECRET` (or `REDDIT_APP_*`) — Reddit API import + OAuth linking (optional) +- `SORTER2_ALLOW_MOCK_OAUTH=1` — allow `mock_user` on `/auth/github` and `/auth/reddit` (tests only) + +Identity: UUID is canonical. OAuth providers only *link* to a UUID (first link creates the principal). Linked providers are private to the account owner. Health check: `GET /healthz` → `ok`. diff --git a/server/src/auth/mod.rs b/server/src/auth/mod.rs index 5906f93b13853421e96a3c37bc9d8202a47842bf..c706ae8045a811e5941f5f6c72da88f42a403a82 100644 --- a/server/src/auth/mod.rs +++ b/server/src/auth/mod.rs @@ -1,4 +1,8 @@ -//! GitHub OAuth login, session cookies, and vote actor resolution. +//! OAuth linking, session cookies, and vote actor resolution. +//! +//! Canonical identity is a UUID. OAuth providers only *link* to that UUID +//! (first link creates the principal; later links attach while logged in). +//! Which providers are linked is private to the account owner. pub mod config; pub mod identity; @@ -22,7 +26,9 @@ use crate::{ form_template::template_json_compact, html::layout, state::AppState, - storage_schema::{oauth_link_owner, pseudonym_owner, Store, StoreFields}, + storage_schema::{ + linked_providers_for_uuid, oauth_link_owner, pseudonym_owner, Store, StoreFields, + }, ui_action::UI_RPC_FIELD, }; @@ -53,10 +59,12 @@ fn new_actor_uuid() -> String { pub struct LoginQuery { #[serde(default)] pub return_to: Option, + #[serde(default)] + pub error: Option, } #[derive(Debug, Deserialize)] -pub struct GitHubStartQuery { +pub struct OAuthStartQuery { #[serde(default)] pub return_to: Option, #[serde(default)] @@ -72,15 +80,22 @@ fn return_from_query_or_jar(jar: &CookieJar, query: Option<&str>) -> String { .unwrap_or_else(|| "/".to_string()) } -fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, String)> { +/// Available OAuth link targets: `(provider_key, label, start_href)`. +fn oauth_providers(base_url: &str, return_to: &str) -> Vec<(&'static str, &'static str, String)> { let mut out = Vec::new(); + let enc = urlencoding::encode(return_to); if oauth::GitHubConfig::from_env(base_url).is_some() { out.push(( - "GitHub", - format!( - "/auth/github?return_to={}", - urlencoding::encode(return_to) - ), + "github", + oauth::provider_label("github"), + format!("/auth/github?return_to={enc}"), + )); + } + if oauth::RedditConfig::from_env(base_url).is_some() { + out.push(( + "reddit", + oauth::provider_label("reddit"), + format!("/auth/reddit?return_to={enc}"), )); } out @@ -125,23 +140,41 @@ fn alias_claim_forms(return_to: &str, submit_label: &str) -> Result Markup { +fn login_error_message(code: Option<&str>) -> Option<&'static str> { + match code { + Some("oauth_taken") => { + Some("that OAuth account is already linked to a different sorter2 account") + } + Some("oauth_failed") => Some("OAuth failed — try again"), + _ => None, + } +} + +fn signed_out_body( + providers: &[(&str, &str, String)], + error: Option<&str>, +) -> Markup { html! { main class="panel login-page" { section class="login-section" { h1 { "sign in" } - p class="muted" { "link an account to vote under a lasting alias" } + p class="muted" { + "link an OAuth account to create your identity, then claim an alias to vote" + } + @if let Some(msg) = login_error_message(error) { + p class="alias-bad" data-testid="login-error" { (msg) } + } @if providers.is_empty() { p class="muted" { - "OAuth is not configured. Set GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET." + "OAuth is not configured. Set GitHub and/or Reddit client credentials." } } @else { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in providers { li { a href=(href) class="btn-primary oauth-provider" - data-testid=(format!("oauth-{}", name.to_lowercase())) { - (format!("Continue with {name}")) + data-testid=(format!("oauth-{key}")) { + (format!("Link {label}")) } } } @@ -156,7 +189,10 @@ fn signed_out_body(providers: &[(&str, String)]) -> Markup { fn account_body( actor: &session::SessionActor, aliases: &[String], - providers: &[(&str, String)], + // Provider keys already linked to this UUID (private). + linked: &[String], + // Providers available to link: not yet attached. + unlinkable: &[(&str, &str, String)], claim_forms: Markup, ) -> Markup { let current = actor.pseudonym.trim(); @@ -212,16 +248,29 @@ fn account_body( (claim_forms) } - @if !providers.is_empty() { - section class="login-section" { - h2 { "linked sign-in" } - p class="muted small" { "sign in again with the same provider to return to this account" } + section class="login-section" { + h2 { "linked sign-in" } + p class="muted small" { + "private to you — linking more providers raises trust weight without publishing which accounts you use" + } + @if linked.is_empty() { + p class="muted" data-testid="linked-providers-empty" { "none yet" } + } @else { + ul class="linked-provider-list" data-testid="linked-providers" { + @for key in linked { + li data-testid=(format!("linked-{key}")) { + (oauth::provider_label(key)) + } + } + } + } + @if !unlinkable.is_empty() { ul class="oauth-provider-list" { - @for (name, href) in providers { + @for (key, label, href) in unlinkable { li { a href=(href) class="btn-secondary oauth-provider" - data-testid=(format!("oauth-relink-{}", name.to_lowercase())) { - (format!("Re-link {name}")) + data-testid=(format!("oauth-link-{key}")) { + (format!("Link {label}")) } } } @@ -243,12 +292,21 @@ fn account_body( fn login_body( session: Option<&session::SessionActor>, aliases: &[String], - providers: &[(&str, String)], + linked: &[String], + providers: &[(&str, &str, String)], claim_forms: Option, + error: Option<&str>, ) -> Markup { match (session, claim_forms) { - (Some(actor), Some(forms)) => account_body(actor, aliases, providers, forms), - _ => signed_out_body(providers), + (Some(actor), Some(forms)) => { + let unlinkable: Vec<_> = providers + .iter() + .filter(|(key, _, _)| !linked.iter().any(|p| p == key)) + .cloned() + .collect(); + account_body(actor, aliases, linked, &unlinkable, forms) + } + _ => signed_out_body(providers, error), } } @@ -268,6 +326,10 @@ pub async fn login_page( .as_ref() .map(|s| alias_list(db, &s.uuid)) .unwrap_or_default(); + let linked = session + .as_ref() + .map(|s| linked_providers_for_uuid(db, &s.uuid).unwrap_or_default()) + .unwrap_or_default(); let providers = oauth_providers(&base_url_from_env(state.cfg.port), &return_to); let claim_forms = if session.is_some() { @@ -282,7 +344,14 @@ pub async fn login_page( } else { "login · sorter2" }, - login_body(session.as_ref(), &aliases, &providers, claim_forms), + login_body( + session.as_ref(), + &aliases, + &linked, + &providers, + claim_forms, + query.error.as_deref(), + ), state.views.get_views("/login"), session .as_ref() @@ -302,7 +371,6 @@ pub async fn alias_page( let db = state.projection_store.db(); let session = session::load_valid_session(db, &session_id).ok_or(StatusCode::UNAUTHORIZED)?; if session::session_has_pseudonym(&session) { - // Already onboarded — manage aliases on the account page. return Ok(Redirect::to("/login").into_response()); } @@ -331,7 +399,7 @@ pub async fn alias_page( pub async fn github_start( State(state): State, jar: CookieJar, - Query(query): Query, + Query(query): Query, ) -> Result { let cfg = oauth::GitHubConfig::from_env(&base_url_from_env(state.cfg.port)) .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; @@ -342,7 +410,28 @@ pub async fn github_start( } else { None }; - let url = oauth::authorize_url(&cfg, &state_token, mock_user); + let url = oauth::github_authorize_url(&cfg, &state_token, mock_user); + let jar = jar + .add(session::oauth_state_cookie_value(&state_token)) + .add(session::auth_return_cookie_value(&return_to)); + Ok((jar, Redirect::temporary(&url)).into_response()) +} + +pub async fn reddit_start( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + let return_to = return_from_query_or_jar(&jar, query.return_to.as_deref()); + let state_token = session::new_oauth_state(); + let mock_user = if config::mock_oauth_allowed() { + query.mock_user.as_deref() + } else { + None + }; + let url = oauth::reddit_authorize_url(&cfg, &state_token, mock_user); let jar = jar .add(session::oauth_state_cookie_value(&state_token)) .add(session::auth_return_cookie_value(&return_to)); @@ -355,6 +444,13 @@ pub struct OAuthCallbackQuery { pub state: String, } +/// Link `provider:provider_id` to a UUID. +/// +/// - Logged in + new provider → attach to session UUID +/// - Logged in + already ours → no-op +/// - Logged in + owned by someone else → conflict +/// - Logged out + known link → resume that UUID +/// - Logged out + unknown → create principal + first link async fn finish_oauth_login( state: &AppState, jar: CookieJar, @@ -364,11 +460,41 @@ async fn finish_oauth_login( let db = state.projection_store.db(); let return_to = return_from_query_or_jar(&jar, None); - let uuid = match oauth_link_owner(db, provider, &provider_id) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)? - { - Some(existing) => existing, - None => { + let existing_owner = oauth_link_owner(db, provider, &provider_id) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let session_uuid = session::session_id_from_jar(&jar) + .as_deref() + .and_then(|id| session::load_valid_session(db, id)) + .map(|s| s.uuid); + let linking_while_logged_in = session_uuid.is_some(); + + let uuid = match (session_uuid, existing_owner) { + (Some(session_uuid), Some(owner)) if owner == session_uuid => session_uuid, + (Some(_), Some(_)) => { + return Ok(( + jar.add(session::clear_oauth_state_cookie()), + "/login?error=oauth_taken".into(), + )); + } + (Some(session_uuid), None) => { + let ts = now_ms(); + state + .append_identity_events(vec![Event::OauthLinked { + uuid: session_uuid.clone(), + provider: provider.to_string(), + provider_id, + ts, + }]) + .await + .map_err(|e| { + tracing::warn!(err = %e, "oauth link append failed"); + StatusCode::INTERNAL_SERVER_ERROR + })?; + session_uuid + } + (None, Some(owner)) => owner, + (None, None) => { let uuid = new_actor_uuid(); let ts = now_ms(); state @@ -409,6 +535,9 @@ async fn finish_oauth_login( "/login/alias?return_to={}", urlencoding::encode(&return_to) ) + } else if linking_while_logged_in { + // Additional link while already in an account → stay on account page. + "/login".to_string() } else { return_to }; @@ -434,22 +563,57 @@ pub async fn github_callback( .build() .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; - let token = oauth::exchange_code(&client, &cfg, &query.code) + let token = oauth::github_exchange_code(&client, &cfg, &query.code) .await .map_err(|e| { tracing::warn!(err = %e, "github oauth token exchange failed"); StatusCode::BAD_GATEWAY })?; - let user = oauth::fetch_user(&client, &cfg.api_base, &token) + let user = oauth::github_fetch_user(&client, &cfg.api_base, &token) .await .map_err(|e| { tracing::warn!(err = %e, "github user fetch failed"); StatusCode::BAD_GATEWAY })?; - let provider = "github"; - let provider_id = oauth::provider_id(&user); - let (jar, dest) = finish_oauth_login(&state, jar, provider, provider_id).await?; + let (jar, dest) = + finish_oauth_login(&state, jar, "github", oauth::github_provider_id(&user)).await?; + Ok((jar, Redirect::to(&dest)).into_response()) +} + +pub async fn reddit_callback( + State(state): State, + jar: CookieJar, + Query(query): Query, +) -> Result { + let cfg = oauth::RedditConfig::from_env(&base_url_from_env(state.cfg.port)) + .ok_or(StatusCode::SERVICE_UNAVAILABLE)?; + + let expected_state = session::oauth_state_from_jar(&jar).ok_or(StatusCode::BAD_REQUEST)?; + if expected_state != query.state { + return Err(StatusCode::BAD_REQUEST); + } + + let client = Client::builder() + .timeout(std::time::Duration::from_secs(15)) + .build() + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + + let token = oauth::reddit_exchange_code(&client, &cfg, &query.code) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit oauth token exchange failed"); + StatusCode::BAD_GATEWAY + })?; + let user = oauth::reddit_fetch_user(&client, &cfg, &token) + .await + .map_err(|e| { + tracing::warn!(err = %e, "reddit user fetch failed"); + StatusCode::BAD_GATEWAY + })?; + + let (jar, dest) = + finish_oauth_login(&state, jar, "reddit", oauth::reddit_provider_id(&user)).await?; Ok((jar, Redirect::to(&dest)).into_response()) } diff --git a/server/src/auth/oauth.rs b/server/src/auth/oauth.rs index b80078fee0c5acd905b9125d29454e46c4e0d066..369b1527d9032cf312a07819279e0f988ef4a36e 100644 --- a/server/src/auth/oauth.rs +++ b/server/src/auth/oauth.rs @@ -1,8 +1,15 @@ -//! GitHub OAuth (raw reqwest, same style as reddit.rs). +//! OAuth providers (GitHub + Reddit). Provider accounts only *link* to a UUID; +//! the UUID is the canonical identity. Which providers are linked is private. use reqwest::Client; use serde::Deserialize; +use crate::reddit::{ + default_user_agent, reddit_oauth_api_base, reddit_oauth_token_base, +}; + +// ── GitHub ────────────────────────────────────────────────────────────────── + #[derive(Debug, Clone)] pub struct GitHubConfig { pub client_id: String, @@ -40,7 +47,7 @@ impl GitHubConfig { } #[derive(Debug, Deserialize)] -struct TokenResponse { +struct GitHubTokenResponse { access_token: String, } @@ -50,7 +57,7 @@ pub struct GitHubUser { pub login: String, } -pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { +pub fn github_authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) -> String { let mut url = format!( "{}/login/oauth/authorize?client_id={}&redirect_uri={}&scope=read:user&state={}", cfg.oauth_base.trim_end_matches('/'), @@ -65,7 +72,7 @@ pub fn authorize_url(cfg: &GitHubConfig, state: &str, mock_user: Option<&str>) - url } -pub async fn exchange_code( +pub async fn github_exchange_code( client: &Client, cfg: &GitHubConfig, code: &str, @@ -90,14 +97,14 @@ pub async fn exchange_code( return Err(format!("github token HTTP {}", resp.status())); } - let body: TokenResponse = resp + let body: GitHubTokenResponse = resp .json() .await .map_err(|e| format!("github token parse failed: {e}"))?; Ok(body.access_token) } -pub async fn fetch_user( +pub async fn github_fetch_user( client: &Client, api_base: &str, access_token: &str, @@ -120,10 +127,149 @@ pub async fn fetch_user( .map_err(|e| format!("github user parse failed: {e}")) } -pub fn provider_id(user: &GitHubUser) -> String { +pub fn github_provider_id(user: &GitHubUser) -> String { user.id.to_string() } +// ── Reddit ────────────────────────────────────────────────────────────────── + +#[derive(Debug, Clone)] +pub struct RedditConfig { + pub client_id: String, + pub client_secret: String, + pub redirect_uri: String, + /// Host for `/api/v1/authorize` (www.reddit.com in production). + pub authorize_base: String, + /// Host for `POST /api/v1/access_token`. + pub token_base: String, + /// Host for bearer `GET /api/v1/me` (oauth.reddit.com). + pub api_base: String, + pub user_agent: String, +} + +/// Authorize page base; defaults to the same host as token POSTs. +pub fn reddit_authorize_base() -> String { + std::env::var("REDDIT_OAUTH_AUTHORIZE_BASE") + .or_else(|_| std::env::var("REDDIT_OAUTH_BASE")) + .unwrap_or_else(|_| "https://www.reddit.com".into()) +} + +impl RedditConfig { + pub fn from_env(base_url: &str) -> Option { + let client_id = std::env::var("REDDIT_CLIENT_ID") + .or_else(|_| std::env::var("REDDIT_APP_ID")) + .ok()?; + let client_secret = std::env::var("REDDIT_CLIENT_SECRET") + .or_else(|_| std::env::var("REDDIT_APP_SECRET")) + .ok()?; + if client_id.is_empty() || client_secret.is_empty() { + return None; + } + let base = base_url.trim_end_matches('/'); + Some(Self { + client_id, + client_secret, + redirect_uri: format!("{base}/auth/reddit/callback"), + authorize_base: reddit_authorize_base(), + token_base: reddit_oauth_token_base(), + api_base: reddit_oauth_api_base(), + user_agent: default_user_agent(), + }) + } +} + +#[derive(Debug, Deserialize)] +struct RedditTokenResponse { + access_token: String, +} + +#[derive(Debug, Deserialize)] +pub struct RedditUser { + /// Stable id (`t2_…`); never use `name` as identity. + pub id: String, + pub name: String, +} + +pub fn reddit_authorize_url(cfg: &RedditConfig, state: &str, mock_user: Option<&str>) -> String { + let mut url = format!( + "{}/api/v1/authorize?client_id={}&response_type=code&state={}&redirect_uri={}&duration=temporary&scope=identity", + cfg.authorize_base.trim_end_matches('/'), + urlencoding::encode(&cfg.client_id), + urlencoding::encode(state), + urlencoding::encode(&cfg.redirect_uri), + ); + if let Some(user) = mock_user { + url.push_str("&mock_user="); + url.push_str(&urlencoding::encode(user)); + } + url +} + +pub async fn reddit_exchange_code( + client: &Client, + cfg: &RedditConfig, + code: &str, +) -> Result { + let resp = client + .post(format!( + "{}/api/v1/access_token", + cfg.token_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .basic_auth(&cfg.client_id, Some(&cfg.client_secret)) + .form(&[ + ("grant_type", "authorization_code"), + ("code", code), + ("redirect_uri", cfg.redirect_uri.as_str()), + ]) + .send() + .await + .map_err(|e| format!("reddit token request failed: {e}"))?; + + if !resp.status().is_success() { + let status = resp.status(); + let body = resp.text().await.unwrap_or_default(); + return Err(format!("reddit token HTTP {status}: {body}")); + } + + let body: RedditTokenResponse = resp + .json() + .await + .map_err(|e| format!("reddit token parse failed: {e}"))?; + Ok(body.access_token) +} + +pub async fn reddit_fetch_user( + client: &Client, + cfg: &RedditConfig, + access_token: &str, +) -> Result { + let resp = client + .get(format!( + "{}/api/v1/me", + cfg.api_base.trim_end_matches('/') + )) + .header("User-Agent", &cfg.user_agent) + .bearer_auth(access_token) + .send() + .await + .map_err(|e| format!("reddit user request failed: {e}"))?; + + if !resp.status().is_success() { + return Err(format!("reddit user HTTP {}", resp.status())); + } + + resp.json() + .await + .map_err(|e| format!("reddit user parse failed: {e}")) +} + +pub fn reddit_provider_id(user: &RedditUser) -> String { + user.id.clone() +} + +// ── Shared helpers ────────────────────────────────────────────────────────── + pub fn validate_pseudonym(raw: &str) -> Result { let trimmed = raw.trim(); if trimmed.is_empty() { @@ -144,3 +290,12 @@ pub fn validate_pseudonym(raw: &str) -> Result { pub fn sanitize_pseudonym(login: &str) -> String { validate_pseudonym(login).unwrap_or_else(|_| "user".to_string()) } + +/// Display name for a provider key (`github` → `GitHub`). Never show provider ids. +pub fn provider_label(provider: &str) -> &'static str { + match provider { + "github" => "GitHub", + "reddit" => "Reddit", + _ => "OAuth", + } +} diff --git a/server/src/lib.rs b/server/src/lib.rs index 84f2565b105fb302241b949af64bd5e49916eab2..0d948af1457504fdbd34d0b14261f65ac58da0ec 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -47,6 +47,8 @@ pub fn create_app(state: AppState) -> Router { .route("/login/alias", get(crate::auth::alias_page)) .route("/auth/github", get(crate::auth::github_start)) .route("/auth/github/callback", get(crate::auth::github_callback)) + .route("/auth/reddit", get(crate::auth::reddit_start)) + .route("/auth/reddit/callback", get(crate::auth::reddit_callback)) .route("/auth/logout", post(crate::auth::logout)) .route("/auth/switch", post(crate::auth::switch_pseudonym)) .route("/ui", post(crate::api::ui_html::post_ui_html)) diff --git a/server/src/projection_apply.rs b/server/src/projection_apply.rs index 4fb4b48ee0a53b7f673fae0c9731eed5acc33332..f50458c2ff3c447a3cfd28adb298e6883a2da4e9 100644 --- a/server/src/projection_apply.rs +++ b/server/src/projection_apply.rs @@ -4,6 +4,8 @@ //! child links, recent-vote appends) plus a cursor advance, all committed in //! one atomic `DisableWal` batch. +use std::collections::HashMap; + use crate::{ event_log::EventLogError, events::{Event, EventRecord}, @@ -44,6 +46,8 @@ pub fn apply_records( let db = projection_store.db(); let mut batch = db.batch(); let mut last_seq = 0u64; + // Weight reads must see earlier writes in this same batch. + let mut pending_weights: HashMap = HashMap::new(); for record in records { match &record.event { @@ -85,6 +89,7 @@ pub fn apply_records( ensure_path_writes(&mut batch, &parsed); } Event::PrincipalCreated { uuid, .. } => { + pending_weights.insert(uuid.clone(), BASE_TRUST_WEIGHT); batch.write( Store::root() .user_weights() @@ -112,17 +117,25 @@ pub fn apply_records( } } else { batch.write(Store::root().oauth_links().key(&link_key).set(uuid)); - let current = Store::root() - .user_weights() - .key(&uuid.clone()) - .get(db) - .map_err(|e| EventLogError::Apply(e.to_string()))? + let current = pending_weights + .get(uuid) + .copied() + .or_else(|| { + Store::root() + .user_weights() + .key(&uuid.clone()) + .get(db) + .ok() + .flatten() + }) .unwrap_or(BASE_TRUST_WEIGHT); + let next = trust_weight_after_link(current); + pending_weights.insert(uuid.clone(), next); batch.write( Store::root() .user_weights() .key(&uuid.clone()) - .set(&trust_weight_after_link(current)), + .set(&next), ); } } @@ -205,6 +218,15 @@ mod tests { ), record( 3, + Event::OauthLinked { + uuid: uuid.into(), + provider: "reddit".into(), + provider_id: "t2_abc".into(), + ts, + }, + ), + record( + 4, Event::PseudonymClaimed { uuid: uuid.into(), pseudonym: "octocat".into(), @@ -219,8 +241,12 @@ mod tests { oauth_link_owner(store.db(), "github", "42").unwrap(), Some(uuid.to_string()) ); + assert_eq!( + crate::storage_schema::linked_providers_for_uuid(store.db(), uuid).unwrap(), + vec!["github".to_string(), "reddit".to_string()] + ); assert_eq!(resolve_actor_uuid(store.db(), "octocat").unwrap(), uuid); - assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 1.5); + assert_eq!(user_trust_weight(store.db(), uuid).unwrap(), 2.0); let aliases = Store::root() .user_pseudonyms() .key(&uuid.to_string()) diff --git a/server/src/storage_schema.rs b/server/src/storage_schema.rs index b942810afd96d3bf01b7765718303a39be4ef8d2..dac6f8e6080e3f5683c24dbb8861f5a981d456c4 100644 --- a/server/src/storage_schema.rs +++ b/server/src/storage_schema.rs @@ -103,6 +103,25 @@ pub fn oauth_link_owner(db: &Db, provider: &str, provider_id: &str) -> durable:: .get(db) } +/// Provider names linked to a UUID (`github`, `reddit`, …). Private — for the +/// account owner's page only; never expose which providers are linked publicly. +pub fn linked_providers_for_uuid(db: &Db, uuid: &str) -> durable::Result> { + let mut providers = Vec::new(); + for (key, owner) in Store::root().oauth_links().iter(db)? { + if owner != uuid { + continue; + } + let Some((provider, _)) = key.split_once(':') else { + continue; + }; + if !providers.iter().any(|p| p == provider) { + providers.push(provider.to_string()); + } + } + providers.sort(); + Ok(providers) +} + pub const RECENT_VOTES_CAP: u64 = 200; fn id_key(id: &ItemId) -> String { diff --git a/test/support/harness.clj b/test/support/harness.clj index 4505ece5aa193e826ca61c52f1467d252080d66b..741024aab1d14269e225791189633287980ae2e4 100644 --- a/test/support/harness.clj +++ b/test/support/harness.clj @@ -48,12 +48,15 @@ "GITHUB_CLIENT_SECRET" "test-secret" "GITHUB_OAUTH_BASE" (str "http://127.0.0.1:" oauth-port) "GITHUB_API_BASE" (str "http://127.0.0.1:" oauth-port) + ;; Reddit import fixtures + Reddit OAuth on reddit-port. "REDDIT_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_CLIENT_ID" "test-reddit" + "REDDIT_CLIENT_SECRET" "test-reddit-secret" "REDDIT_OAUTH_BASE" (str "http://127.0.0.1:" reddit-port) - "REDDIT_CLIENT_ID" "" - "REDDIT_CLIENT_SECRET" "" - "REDDIT_APP_ID" "" - "REDDIT_APP_SECRET" ""})) + "REDDIT_OAUTH_AUTHORIZE_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_TOKEN_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_OAUTH_API_BASE" (str "http://127.0.0.1:" reddit-port) + "REDDIT_USER_AGENT" "web:sorter2-test:v0 (by /u/test)"})) (defn with-auth-servers "Start mock Reddit + mock OAuth + release sorter2-server. diff --git a/test/support/mock_oauth.clj b/test/support/mock_oauth.clj index 909d7a7be8b159ea54a122d69c46af3db3318118..5ba7e9be3cf6d2226648e9609a09ed45f306930f 100644 --- a/test/support/mock_oauth.clj +++ b/test/support/mock_oauth.clj @@ -1,5 +1,5 @@ (ns test.support.mock-oauth - "In-process HTTP stub for GitHub OAuth (authorize, token, /user)." + "In-process HTTP stub for GitHub + Reddit OAuth (authorize, token, user)." (:require [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] [java.net InetSocketAddress URLDecoder])) @@ -12,11 +12,14 @@ (URLDecoder/decode (or v "") "UTF-8")))) (str/split query #"&")))) -(defn- parse-mock-user [raw] +(defn- parse-mock-user + "GitHub-style `id:login` (numeric id). Reddit-style `t2_xxx:name`." + [raw] (let [s (or raw "1002:newbie") [id login] (str/split s #":" 2)] - {:id (Long/parseLong id) - :login (or login "newbie")})) + {:id id + :login (or login "newbie") + :numeric? (re-matches #"\d+" id)})) (defn- send-json [^HttpExchange ex status body] (let [bytes (.getBytes body "UTF-8")] @@ -31,9 +34,10 @@ (.sendResponseHeaders ex 302 -1) (.close (.getResponseBody ex))) -(defn- read-form-code [^HttpExchange ex] +(defn- read-form [^HttpExchange ex] (let [body (slurp (.getInputStream ex))] - (query-param body "code"))) + {:code (query-param body "code") + :grant (query-param body "grant_type")})) (defn- bearer-token [^HttpExchange ex] (some-> (.getRequestHeaders ex) @@ -44,8 +48,18 @@ (when (str/starts-with? token "mock:") (parse-mock-user (subs token 5)))) +(defn- authorize-redirect [exchange query] + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + mock-user (query-param query "mock_user") + user (parse-mock-user mock-user) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc))) + (defn start-mock-oauth - "Start mock GitHub OAuth on `port`. Returns a zero-arg `stop` function." + "Start mock GitHub + Reddit OAuth on `port`. Returns a zero-arg `stop` function." [port] (let [server (HttpServer/create (InetSocketAddress. "127.0.0.1" port) 0) handler @@ -53,28 +67,45 @@ (handle [^HttpExchange exchange] (let [uri (.getRequestURI exchange) path (.getPath uri) - query (.getQuery uri)] + query (.getQuery uri) + method (.getRequestMethod exchange)] (cond + ;; GitHub authorize (str/ends-with? path "/login/oauth/authorize") - (let [redirect-uri (query-param query "redirect_uri") - state (query-param query "state") - mock-user (query-param query "mock_user") - user (parse-mock-user mock-user) - code (str "mock:" (:id user) ":" (:login user)) - loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") - "&state=" (java.net.URLEncoder/encode state "UTF-8"))] - (send-redirect exchange loc)) + (authorize-redirect exchange query) + + ;; Reddit authorize + (str/ends-with? path "/api/v1/authorize") + (authorize-redirect exchange query) - (str/ends-with? path "/login/oauth/access_token") - (let [code (or (read-form-code exchange) "mock:1002:newbie")] + ;; GitHub token + (and (= method "POST") (str/ends-with? path "/login/oauth/access_token")) + (let [code (or (:code (read-form exchange)) "mock:1002:newbie")] (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\"}"))) + ;; Reddit token (client_credentials for import + authorization_code for login) + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + ;; GitHub user (= path "/user") (let [token (bearer-token exchange) - user (or (parse-token-user token) {:id 1002 :login "newbie"})] + user (or (parse-token-user token) {:id "1002" :login "newbie" :numeric? true})] (send-json exchange 200 (str "{\"id\":" (:id user) ",\"login\":\"" (:login user) "\"}"))) + ;; Reddit /api/v1/me + (str/ends-with? path "/api/v1/me") + (let [token (bearer-token exchange) + user (or (parse-token-user token) {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + :else (send-json exchange 404 "{\"error\":\"not found\"}")))))] (.createContext server "/" handler) diff --git a/test/support/mock_reddit.clj b/test/support/mock_reddit.clj index 5efa92db3e1f79b8f423a2f1adcbda959123c1ad..a630cf0938722193e9af88382d60e777ff371be4 100644 --- a/test/support/mock_reddit.clj +++ b/test/support/mock_reddit.clj @@ -1,14 +1,56 @@ (ns test.support.mock-reddit - "In-process HTTP stub for Reddit API fixtures (`test/fixtures/reddit/`)." + "In-process HTTP stub for Reddit API fixtures + OAuth login endpoints." (:require [clojure.java.io :as io] [clojure.string :as str]) (:import [com.sun.net.httpserver HttpServer HttpHandler HttpExchange] - [java.net InetSocketAddress])) + [java.net InetSocketAddress URLDecoder])) (defn fixtures-dir ([] (fixtures-dir (System/getProperty "user.dir"))) ([root] (str root "/test/fixtures/reddit"))) +(defn- query-param [query key] + (when query + (some (fn [pair] + (let [[k v] (str/split pair "=" 2)] + (when (= k key) + (URLDecoder/decode (or v "") "UTF-8")))) + (str/split query #"&")))) + +(defn- parse-mock-user [raw] + (let [s (or raw "t2_test:redditor") + [id login] (str/split s #":" 2)] + {:id id :login (or login "redditor")})) + +(defn- send-bytes [^HttpExchange ex status ^bytes body content-type] + (.set (.getResponseHeaders ex) "Content-Type" content-type) + (.sendResponseHeaders ex status (alength body)) + (doto (.getResponseBody ex) + (.write body) + (.close))) + +(defn- send-json [^HttpExchange ex status body] + (send-bytes ex status (.getBytes body "UTF-8") "application/json")) + +(defn- send-redirect [^HttpExchange ex location] + (.set (.getResponseHeaders ex) "Location" location) + (.sendResponseHeaders ex 302 -1) + (.close (.getResponseBody ex))) + +(defn- read-form [^HttpExchange ex] + (let [body (slurp (.getInputStream ex))] + {:code (query-param body "code") + :grant (query-param body "grant_type")})) + +(defn- bearer-token [^HttpExchange ex] + (some-> (.getRequestHeaders ex) + (.getFirst "Authorization") + (str/replace #"^[Bb]earer " ""))) + +(defn- parse-token-user [token] + (when (str/starts-with? token "mock:") + (parse-mock-user (subs token 5)))) + (defn start-mock-reddit "Start a mock Reddit API on `port`. Returns a zero-arg `stop` function." ([port] (start-mock-reddit port (fixtures-dir))) @@ -19,15 +61,38 @@ handler (proxy [HttpHandler] [] (handle [^HttpExchange exchange] - ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. - (let [path (.getPath (.getRequestURI exchange)) - body (if (str/includes? path "/about") - about - listing)] - (.sendResponseHeaders exchange 200 (alength body)) - (let [out (.getResponseBody exchange)] - (.write out body) - (.close out)))))] + (let [uri (.getRequestURI exchange) + path (.getPath uri) + query (.getQuery uri) + method (.getRequestMethod exchange)] + (cond + (str/ends-with? path "/api/v1/authorize") + (let [redirect-uri (query-param query "redirect_uri") + state (query-param query "state") + user (parse-mock-user (query-param query "mock_user")) + code (str "mock:" (:id user) ":" (:login user)) + loc (str redirect-uri "?code=" (java.net.URLEncoder/encode code "UTF-8") + "&state=" (java.net.URLEncoder/encode state "UTF-8"))] + (send-redirect exchange loc)) + + (and (= method "POST") (str/ends-with? path "/api/v1/access_token")) + (let [form (read-form exchange) + grant (or (:grant form) "") + code (or (:code form) "mock:t2_test:redditor")] + (if (= grant "client_credentials") + (send-json exchange 200 "{\"access_token\":\"app-token\",\"token_type\":\"bearer\",\"expires_in\":3600}") + (send-json exchange 200 (str "{\"access_token\":\"" code "\",\"token_type\":\"bearer\",\"expires_in\":3600}")))) + + (str/ends-with? path "/api/v1/me") + (let [user (or (parse-token-user (bearer-token exchange)) + {:id "t2_test" :login "redditor"})] + (send-json exchange 200 + (str "{\"id\":\"" (:id user) "\",\"name\":\"" (:login user) "\"}"))) + + ;; `/r//about.json` → subreddit entity; `/r/.json` → listing. + :else + (let [body (if (str/includes? path "/about") about listing)] + (send-bytes exchange 200 body "application/json"))))))] (.createContext server "/" handler) (.setExecutor server nil) (.start server) Side B — contributor: tommy-mor Side B — commit message: [af73743d] Replace GroupState with ScopeVotes and derive edges at ranking time. Store only uuid_votes and recent_votes per scope; rank centrality and pair logic rebuild edge weights on demand instead of maintaining cached state. Co-authored-by: Cursor Side B — unified diff (full patch): diff --git a/server/src/events.rs b/server/src/events.rs index 8a166d49b4f26835fbc2b58cb1f4bdbf002763b8..015208311f6c5c23a0e8aab068d002a69c89c4e1 100644 --- a/server/src/events.rs +++ b/server/src/events.rs @@ -43,7 +43,7 @@ pub enum ViewEvent { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum Event { - /// Pairwise comparison vote (replayed into the parent node's [`crate::reducer::GroupState`] on boot). + /// Pairwise comparison vote (replayed into the parent node's [`crate::reducer::ScopeVotes`] on boot). /// `scope` is the parent [`crate::path_types::ItemId`] string; empty string is the tree root. VoteRecorded { ts: i64, diff --git a/server/src/html/mod.rs b/server/src/html/mod.rs index 4eff2e19ed4d303ff8e80c1eabd8a15b4990e643..1e2e7a06856d8a62378741aaf5ed94a4ffed337e 100644 --- a/server/src/html/mod.rs +++ b/server/src/html/mod.rs @@ -13,7 +13,7 @@ use crate::{ form_template::template_json_compact, path_types::ItemId, ranking::{ - connected_components_from_voted_pairs, ranked_items_subset, RankedItem, MAX_ITERS, TOL, + ranked_items_subset, scope_components, RankedItem, MAX_ITERS, TOL, }, reducer::{GlobalTree, NodeState}, state::AppState, @@ -397,10 +397,9 @@ pub fn ranking_panel_with_highlights( tree: &GlobalTree, highlighted: &HashSet, ) -> Markup { - let group = &node.local_ranking; - let n = group.idx_to_item.len(); - let (comps, _isolates) = - connected_components_from_voted_pairs(n, group.voted_pairs.iter().copied()); + let scope = &node.votes; + let (comps, _isolates, _) = + scope_components(scope); // Each connected component of voted items is its own ranking; isolated and // never-voted children fall into the "unranked" bucket below. @@ -410,7 +409,7 @@ pub fn ranking_panel_with_highlights( if comp.len() < 2 { continue; } - let ranked = ranked_items_subset(group, comp, MAX_ITERS, TOL); + let ranked = ranked_items_subset(scope, comp, MAX_ITERS, TOL); for r in &ranked { ranked_ids.insert(r.item.clone()); } diff --git a/server/src/html/vote.rs b/server/src/html/vote.rs index bf82aef3ad9c5f4e9e877c47dab27beb29a80b8f..3aa00c417c89a9cab3417c650b50ed7c73f08e20 100644 --- a/server/src/html/vote.rs +++ b/server/src/html/vote.rs @@ -14,7 +14,7 @@ use crate::{ html::{ranking_panel_with_highlights, scope_theme_style, JsBuilder}, pair::{children_of, resolve_pair, suggest_next_pair_in_pool}, path_types::ItemId, - reducer::{GlobalTree, GroupState, NodeState, VoteData}, + reducer::{GlobalTree, NodeState, ScopeVotes, VoteData}, state::{parse_item_param, AppState}, ui_action::UI_RPC_FIELD, }; @@ -68,8 +68,8 @@ fn ratios_for_page(v: &VoteData, page_left: &ItemId, page_right: &ItemId) -> (i3 } } -fn edge_votes(group: &GroupState, left: &ItemId, right: &ItemId) -> Vec { - group +fn edge_votes(scope: &ScopeVotes, left: &ItemId, right: &ItemId) -> Vec { + scope .recent_votes .iter() .filter(|v| { @@ -113,11 +113,11 @@ fn slider_value_from_ratios(r_left: i32, r_right: i32) -> i32 { fn vote_edge_history( tree: &GlobalTree, - group: &GroupState, + scope: &ScopeVotes, left: &ItemId, right: &ItemId, ) -> Markup { - let mut votes = edge_votes(group, left, right); + let mut votes = edge_votes(scope, left, right); votes.sort_by(|a, b| b.ts.cmp(&a.ts)); let legend_left = child_title(tree, left); let legend_right = child_title(tree, right); @@ -228,9 +228,9 @@ pub(crate) fn vote_recorded_morph( ) -> JsBuilder { let pool = children_of(tree, parent); let empty = NodeState::default(); - let group = tree.get(parent).unwrap_or(&empty).local_ranking.clone(); - let edge_history = vote_edge_history(tree, &group, left, right); - let next_pair = suggest_next(&group, left, right, &pool); + let scope = tree.get(parent).unwrap_or(&empty).votes.clone(); + let edge_history = vote_edge_history(tree, &scope, left, right); + let next_pair = suggest_next(&scope, left, right, &pool); let actions = vote_compare_actions(parent, next_pair.as_ref()); let sidebar = vote_ranking_sidebar(tree, parent, left, right); JsBuilder::new() @@ -252,12 +252,12 @@ fn vote_compare_item_card(tree: &GlobalTree, item: &ItemId, side_class: &str) -> } fn suggest_next( - group: &GroupState, + scope: &ScopeVotes, left: &ItemId, right: &ItemId, pool: &[ItemId], ) -> Option<(ItemId, ItemId)> { - suggest_next_pair_in_pool(group, pool, Some((left, right))) + suggest_next_pair_in_pool(scope, pool, Some((left, right))) } pub async fn vote_page( @@ -284,9 +284,9 @@ pub async fn vote_page( }; let pool = children_of(&tree, &parent); - let group = &parent_node.local_ranking; - let next_pair = suggest_next(group, &left, &right, &pool); - let edge_history = vote_edge_history(&tree, group, &left, &right); + let scope = &parent_node.votes; + let next_pair = suggest_next(&scope, &left, &right, &pool); + let edge_history = vote_edge_history(&tree, &scope, &left, &right); let rpc_json = template_json_compact(&serde_json::json!({ "action": "record_vote", @@ -388,8 +388,8 @@ mod polarity_tests { let mut tree = GlobalTree::new(); tree.apply_vote(&parent, vote, TEST_ACTOR_UUID); - let group = &tree.get(&parent).unwrap().local_ranking; - let ranked = ranked_items(group); + let scope = &tree.get(&parent).unwrap().votes; + let ranked = ranked_items(scope); assert_eq!( ranked[0].item, left, "left item should rank first when ratio favours the left" diff --git a/server/src/pair.rs b/server/src/pair.rs index 42a1b1eb2adf16730d34d0fe23c13d5a75d7ba27..9873295c51526726089875bbfd2f97d7faa91872 100644 --- a/server/src/pair.rs +++ b/server/src/pair.rs @@ -11,8 +11,8 @@ use std::collections::{HashMap, HashSet}; use crate::{ path_types::ItemId, - ranking::{connected_components_from_voted_pairs, ranked_items}, - reducer::{GlobalTree, GroupState}, + ranking::{pair_is_voted, ranked_items, scope_components}, + reducer::{GlobalTree, ScopeVotes}, }; fn pairs_match(a: &ItemId, b: &ItemId, x: &ItemId, y: &ItemId) -> bool { @@ -23,26 +23,15 @@ fn pair_excluded(a: &ItemId, b: &ItemId, exclude: Option<(&ItemId, &ItemId)>) -> exclude.is_some_and(|(x, y)| pairs_match(a, b, x, y)) } -fn pair_is_voted(group: &GroupState, a: &ItemId, b: &ItemId) -> bool { - let Some(&ai) = group.item_to_idx.get(a) else { - return false; - }; - let Some(&bi) = group.item_to_idx.get(b) else { - return false; - }; - let (i, j) = if ai < bi { (ai, bi) } else { (bi, ai) }; - group.voted_pairs.contains(&(i, j)) -} struct ComponentLayout { ids: HashMap, established: HashSet, } -fn component_layout(group: &GroupState, pool: &[ItemId]) -> ComponentLayout { - let n = group.idx_to_item.len(); - let (comps, isolates) = - connected_components_from_voted_pairs(n, group.voted_pairs.iter().copied()); +fn component_layout(scope: &ScopeVotes, pool: &[ItemId]) -> ComponentLayout { + let (comps, isolates, idx_to_item) = scope_components(scope); + let n = idx_to_item.len(); let mut established = HashSet::new(); let mut ids: HashMap = HashMap::new(); @@ -52,14 +41,14 @@ fn component_layout(group: &GroupState, pool: &[ItemId]) -> ComponentLayout { } for &idx in comp { if idx < n { - ids.insert(group.idx_to_item[idx].clone(), comp_idx); + ids.insert(idx_to_item[idx].clone(), comp_idx); } } } let mut next = comps.len(); for &idx in &isolates { if idx < n { - ids.insert(group.idx_to_item[idx].clone(), next); + ids.insert(idx_to_item[idx].clone(), next); next += 1; } } @@ -121,7 +110,7 @@ fn established_groups_in_pool<'a>( groups } -fn ranked_pool_order(group: &GroupState, pool: &[ItemId]) -> Vec { +fn ranked_pool_order(group: &ScopeVotes, pool: &[ItemId]) -> Vec { let pool_set: HashSet<_> = pool.iter().collect(); ranked_items(group) .into_iter() @@ -132,7 +121,7 @@ fn ranked_pool_order(group: &GroupState, pool: &[ItemId]) -> Vec { /// Walk 1↔2, 2↔3, …; optional `require_unvoted` skips voted edges. fn zip_adjacent_pair( - group: &GroupState, + group: &ScopeVotes, order: &[ItemId], exclude: Option<(&ItemId, &ItemId)>, require_unvoted: bool, @@ -153,7 +142,7 @@ fn zip_adjacent_pair( /// Grow the voted graph toward one component (no rank centrality). fn suggest_grow_pair( - group: &GroupState, + group: &ScopeVotes, pool: &[ItemId], layout: &ComponentLayout, exclude: Option<(&ItemId, &ItemId)>, @@ -216,7 +205,7 @@ fn suggest_grow_pair( /// Pick the next pair to vote on within `pool`. pub fn suggest_next_pair_in_pool( - group: &GroupState, + group: &ScopeVotes, pool: &[ItemId], exclude: Option<(&ItemId, &ItemId)>, ) -> Option<(ItemId, ItemId)> { @@ -315,7 +304,7 @@ pub fn resolve_pair( (None, None) => { let group = tree .get(parent) - .map(|n| &n.local_ranking) + .map(|n| &n.votes) .cloned() .unwrap_or_default(); suggest_next_pair_in_pool(&group, &children, None).ok_or(PairError::NoPair) @@ -398,7 +387,7 @@ mod tests { "https://reddit.com/r/rust/b", ], ); - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); assert!(!pair_is_voted(&group, &pool[0], &pool[1])); assert!(suggest_next_pair_in_pool(&group, &pool, None).is_some()); @@ -417,7 +406,7 @@ mod tests { ); let vote = test_vote(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1); apply(&mut tree, &parent, vote); - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let (l, r) = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let voted_ab = (l.as_str() == "https://reddit.com/r/rust/a" && r.as_str() == "https://reddit.com/r/rust/b") @@ -441,7 +430,7 @@ mod tests { let cd = test_vote(2, "https://reddit.com/r/rust/c", "https://reddit.com/r/rust/d", 2, 1); apply(&mut tree, &parent, ab); apply(&mut tree, &parent, cd); - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); @@ -467,7 +456,7 @@ mod tests { ); let ab = test_vote(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1); apply(&mut tree, &parent, ab); - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); @@ -496,7 +485,7 @@ mod tests { ); let ab = test_vote(1, "https://reddit.com/r/rust/a", "https://reddit.com/r/rust/b", 2, 1); apply(&mut tree, &parent, ab); - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); @@ -522,7 +511,7 @@ mod tests { let v = test_vote(1, a, b, l, r); apply(&mut tree, &parent, v); } - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); @@ -550,7 +539,7 @@ mod tests { let v = test_vote(1, a, b, l, r); apply(&mut tree, &parent, v); } - let group = tree.get(&parent).unwrap().local_ranking.clone(); + let group = tree.get(&parent).unwrap().votes.clone(); let pool = children_of(&tree, &parent); let pair = suggest_next_pair_in_pool(&group, &pool, None).unwrap(); let chosen = pair_set(&pair); diff --git a/server/src/projection_store.rs b/server/src/projection_store.rs index 27fc0bb0519d9f035dceec1fc8b0fa74b00b6b40..8c1a466183a96173fe52b144fb67c2454b715fbd 100644 --- a/server/src/projection_store.rs +++ b/server/src/projection_store.rs @@ -214,7 +214,7 @@ mod tests { let loaded = store.load_tree().unwrap(); let root = loaded.get(&ItemId::root()).unwrap(); - assert_eq!(root.local_ranking.idx_to_item.len(), 2); + assert_eq!(crate::ranking::ranked_items(&root.votes).len(), 2); assert!(root.children.contains(&ItemId::opaque("alpha"))); } diff --git a/server/src/ranking.rs b/server/src/ranking.rs index 1fc3298d2b2864e9711cd262a034677e45c7090c..cc4de6da11b02135e5e6b1d2a68646cb77870b9d 100644 --- a/server/src/ranking.rs +++ b/server/src/ranking.rs @@ -1,7 +1,7 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{BTreeSet, HashMap, HashSet}; use crate::path_types::ItemId; -use crate::reducer::GroupState; +use crate::reducer::{canonical_pair_ids, ScopeVotes}; #[derive(Debug, Clone)] pub struct RankedItem { @@ -9,15 +9,76 @@ pub struct RankedItem { pub score: f64, } -/// Power-iteration cap and convergence tolerance for rank centrality. pub const MAX_ITERS: usize = 10_000; pub const TOL: f64 = 1e-8; -/// Compute connected components over the voted-pairs graph (treated as undirected). -/// -/// Returns: -/// - `components`: each component is a sorted list of node indices, excluding isolates. -/// - `isolates`: sorted list of node indices with degree 0 (no voted pairs). +pub fn item_index(scope: &ScopeVotes) -> (HashMap, Vec) { + let mut item_strs: BTreeSet = BTreeSet::new(); + for vote in scope.uuid_votes.values() { + item_strs.insert(vote.a.as_str().to_string()); + item_strs.insert(vote.b.as_str().to_string()); + } + let mut idx_to_item: Vec = Vec::with_capacity(item_strs.len()); + let mut item_to_idx: HashMap = HashMap::with_capacity(item_strs.len()); + for s in item_strs { + let id = ItemId::from_storage(&s).unwrap_or_else(|| ItemId::opaque(&s)); + let idx = idx_to_item.len(); + item_to_idx.insert(id.clone(), idx); + idx_to_item.push(id); + } + (item_to_idx, idx_to_item) +} + +pub fn edges_from_scope(scope: &ScopeVotes) -> HashMap<(usize, usize), f64> { + let (item_to_idx, _) = item_index(scope); + let mut edges: HashMap<(usize, usize), f64> = HashMap::new(); + for vote in scope.uuid_votes.values() { + let Some(&ai) = item_to_idx.get(&vote.a) else { + continue; + }; + let Some(&bi) = item_to_idx.get(&vote.b) else { + continue; + }; + let w_a = vote.ratio_left as f64 * vote.trust_weight; + let w_b = vote.ratio_right as f64 * vote.trust_weight; + if w_a > 0.0 { + *edges.entry((bi, ai)).or_insert(0.0) += w_a; + } + if w_b > 0.0 { + *edges.entry((ai, bi)).or_insert(0.0) += w_b; + } + } + edges +} + +pub fn edge_weight_sum(scope: &ScopeVotes) -> f64 { + edges_from_scope(scope).values().sum() +} + +pub fn voted_pair_indices(scope: &ScopeVotes) -> HashSet<(usize, usize)> { + let (item_to_idx, _) = item_index(scope); + let mut pairs = HashSet::new(); + for vote in scope.uuid_votes.values() { + let Some(&ai) = item_to_idx.get(&vote.a) else { + continue; + }; + let Some(&bi) = item_to_idx.get(&vote.b) else { + continue; + }; + let (i, j) = if ai < bi { (ai, bi) } else { (bi, ai) }; + pairs.insert((i, j)); + } + pairs +} + +pub fn pair_is_voted(scope: &ScopeVotes, a: &ItemId, b: &ItemId) -> bool { + let (lo, hi) = canonical_pair_ids(a, b); + scope + .uuid_votes + .keys() + .any(|(_, l, h)| l == &lo && h == &hi) +} + pub fn connected_components_from_voted_pairs( n: usize, voted_pairs: impl Iterator, @@ -63,20 +124,25 @@ pub fn connected_components_from_voted_pairs( (comps, isolates) } -/// Compute rank-centrality scores for the whole group and return items sorted -/// by score (descending). Recomputed fresh from the edge set on every call — -/// there is no score cache. -pub fn ranked_items(group: &GroupState) -> Vec { - let n = group.idx_to_item.len(); - let scores = - compute_scores_from_edges(n, group.edges.iter().map(|(&k, &w)| (k, w)), MAX_ITERS, TOL); +pub fn scope_components(scope: &ScopeVotes) -> (Vec>, Vec, Vec) { + let (_, idx_to_item) = item_index(scope); + let n = idx_to_item.len(); + let pairs = voted_pair_indices(scope); + let (comps, isolates) = connected_components_from_voted_pairs(n, pairs.into_iter()); + (comps, isolates, idx_to_item) +} - let mut items: Vec = group - .idx_to_item - .iter() +pub fn ranked_items(scope: &ScopeVotes) -> Vec { + let (_, idx_to_item) = item_index(scope); + let n = idx_to_item.len(); + let edges = edges_from_scope(scope); + let scores = compute_scores_from_edges(n, edges.into_iter(), MAX_ITERS, TOL); + + let mut items: Vec = idx_to_item + .into_iter() .enumerate() .map(|(i, item)| RankedItem { - item: item.clone(), + item, score: *scores.get(i).unwrap_or(&0.0), }) .collect(); @@ -89,11 +155,8 @@ pub fn ranked_items(group: &GroupState) -> Vec { items } -/// Highest- and lowest-ranked items for a group. Returns up to `k` items from -/// each end with no overlap. If the group has `2*k` items or fewer, `top` holds -/// the full ranking and `bottom` is empty (so nothing is shown twice). -pub fn top_bottom(group: &GroupState, k: usize) -> (Vec, Vec) { - let items = ranked_items(group); +pub fn top_bottom(scope: &ScopeVotes, k: usize) -> (Vec, Vec) { + let items = ranked_items(scope); if k == 0 || items.len() <= 2 * k { return (items, Vec::new()); } @@ -115,7 +178,6 @@ pub fn compute_scores_from_edges( return vec![1.0]; } - // Collect raw edges into a map for pairwise normalization. let mut raw: HashMap<(usize, usize), f64> = HashMap::new(); for ((src, dst), w) in edges { if src >= n || dst >= n || w <= 0.0 { @@ -124,9 +186,6 @@ pub fn compute_scores_from_edges( *raw.entry((src, dst)).or_insert(0.0) += w; } - // Pairwise normalization: a_ij = A_ij / (A_ij + A_ji). - // This ensures repeated votes on the same pair don't inflate influence - // beyond what the ratio implies. let keys: Vec<(usize, usize)> = raw.keys().copied().collect(); let mut normalized: HashMap<(usize, usize), f64> = HashMap::new(); for (i, j) in keys { @@ -145,17 +204,6 @@ pub fn compute_scores_from_edges( } } - // Rank Centrality (Negahban, Oh, Shah 2012, §3.1): - // P_ij = (1/d_max) * A_ij for i ≠ j compared - // P_ii = 1 - (1/d_max) * Σ_k A_ik - // where d_i is the *degree* (number of distinct neighbors compared) and - // d_max = max_i d_i. Using the unweighted degree — not the sum of - // pairwise-normalized weights — is what guarantees aperiodicity: it - // forces P_ii > 0 for every non-maximum-degree node, and for max-degree - // nodes whenever any neighbor weight is below 1 (i.e. not a unanimous - // loss). Without this, regular comparison graphs (e.g. a pure star at - // ratio 2:1) produce a bipartite chain that oscillates instead of - // converging — see issue #146. let mut out_edges: Vec> = vec![Vec::new(); n]; let mut neighbors: Vec> = vec![HashSet::new(); n]; @@ -213,11 +261,8 @@ pub fn compute_scores_from_edges( scores } -/// Rank-centrality within a subset of items (an induced subgraph), using the group's aggregated edges. -/// -/// `idxs` are indices into `group.idx_to_item`. The returned items use the original item names. pub fn ranked_items_subset( - group: &GroupState, + scope: &ScopeVotes, idxs: &[usize], max_iters: usize, tol: f64, @@ -226,13 +271,15 @@ pub fn ranked_items_subset( return vec![]; } - // Map original idx -> compact idx [0..m) + let (_, idx_to_item) = item_index(scope); + let edges = edges_from_scope(scope); + let mut map: HashMap = HashMap::with_capacity(idxs.len()); for (j, &i) in idxs.iter().enumerate() { map.insert(i, j); } - let edges_iter = group.edges.iter().filter_map(|(&(src, dst), &w)| { + let edges_iter = edges.into_iter().filter_map(|((src, dst), w)| { let s = *map.get(&src)?; let d = *map.get(&dst)?; Some(((s, d), w)) @@ -240,12 +287,11 @@ pub fn ranked_items_subset( let scores = compute_scores_from_edges(idxs.len(), edges_iter, max_iters, tol); - // Filter out entries where idx_to_item doesn't have the slot (shouldn't happen, but be safe). let mut items: Vec = idxs .iter() .enumerate() .filter_map(|(j, &orig)| { - let item = group.idx_to_item.get(orig)?.clone(); + let item = idx_to_item.get(orig)?.clone(); Some(RankedItem { item, score: *scores.get(j).unwrap_or(&0.0), @@ -261,8 +307,8 @@ pub fn ranked_items_subset( items } -pub fn group_summary_scores(group: &GroupState) -> HashMap { - ranked_items(group) +pub fn group_summary_scores(scope: &ScopeVotes) -> HashMap { + ranked_items(scope) .into_iter() .map(|r| (r.item, r.score)) .collect() @@ -274,32 +320,26 @@ mod tests { use crate::identity::{DEFAULT_PSEUDONYM, TEST_ACTOR_UUID}; use crate::reducer::VoteData; - fn mk_group() -> GroupState { - GroupState::new() + fn mk_scope() -> ScopeVotes { + ScopeVotes::default() } fn vote(ts: i64, a: &str, b: &str, l: i32, r: i32) -> VoteData { VoteData::from_event(ts, a, b, l, r, DEFAULT_PSEUDONYM.to_string(), 1.0).unwrap() } - fn apply(g: &mut GroupState, v: VoteData) { - g.apply_vote(v, TEST_ACTOR_UUID); + fn apply(scope: &mut ScopeVotes, v: VoteData) { + scope.apply_vote(v, TEST_ACTOR_UUID); } - /// Regression for issue #146: pure forward star at default `>` ratio (2:1). - /// Under the old (sum-of-weights) divisor every node had P_ii = 0 and the - /// chain was bipartite; power iteration oscillated and returned the - /// uniform initial distribution after an even number of steps. Using the - /// paper's degree-based d_max gives every node a positive self-loop and - /// the chain converges to the correct stationary distribution. #[test] fn star_topology_winner_at_top_via_subset() { - let mut g = mk_group(); - g.apply_vote(vote(1, "zebra", "alpha", 2, 1), TEST_ACTOR_UUID); - g.apply_vote(vote(2, "zebra", "beta", 2, 1), TEST_ACTOR_UUID); + let mut scope = mk_scope(); + apply(&mut scope, vote(1, "zebra", "alpha", 2, 1)); + apply(&mut scope, vote(2, "zebra", "beta", 2, 1)); - let mut items: Vec<(usize, String)> = g - .idx_to_item + let (_, idx_to_item) = item_index(&scope); + let mut items: Vec<(usize, String)> = idx_to_item .iter() .enumerate() .map(|(i, it)| (i, it.as_str().to_string())) @@ -307,78 +347,51 @@ mod tests { items.sort_by(|a, b| a.1.cmp(&b.1)); let idxs: Vec = items.iter().map(|(i, _)| *i).collect(); - let ranked = ranked_items_subset(&g, &idxs, 10000, 1e-8); - for r in &ranked { - eprintln!("{}: {}", r.item.as_str(), r.score); - } - assert_eq!( - ranked[0].item.as_str(), - "zebra", - "zebra won both votes and should rank #1" - ); + let ranked = ranked_items_subset(&scope, &idxs, 10000, 1e-8); + assert_eq!(ranked[0].item.as_str(), "zebra"); } #[test] fn top_bottom_splits_ends_without_overlap() { - let mut g = mk_group(); - // Chain a > b > c > d > e > f so ranks are well separated. + let mut scope = mk_scope(); for (hi, lo) in [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f")] { - apply(&mut g, vote(1, hi, lo, 2, 1)); + apply(&mut scope, vote(1, hi, lo, 2, 1)); } - let (top, bottom) = top_bottom(&g, 2); + let (top, bottom) = top_bottom(&scope, 2); assert_eq!(top.len(), 2); assert_eq!(bottom.len(), 2); - // No overlap between the two ends. for t in &top { assert!(bottom.iter().all(|b| b.item != t.item)); } - // Best item ranks above the worst item. assert!(top[0].score >= bottom[bottom.len() - 1].score); } #[test] fn top_bottom_small_group_has_empty_bottom() { - let mut g = mk_group(); - apply(&mut g, vote(1, "a", "b", 2, 1)); - let (top, bottom) = top_bottom(&g, 5); + let mut scope = mk_scope(); + apply(&mut scope, vote(1, "a", "b", 2, 1)); + let (top, bottom) = top_bottom(&scope, 5); assert_eq!(top.len(), 2); assert!(bottom.is_empty()); } #[test] fn connected_components_split_disconnected_pairs() { - let mut g = mk_group(); - // Two disconnected edges: (a,b) and (c,d) - apply(&mut g, vote(1, "a", "b", 3, 1)); - apply(&mut g, vote(2, "c", "d", 3, 1)); - - let n = g.idx_to_item.len(); - let (mut comps, isolates) = - connected_components_from_voted_pairs(n, g.voted_pairs.iter().copied()); + let mut scope = mk_scope(); + apply(&mut scope, vote(1, "a", "b", 3, 1)); + apply(&mut scope, vote(2, "c", "d", 3, 1)); + + let (_, idx_to_item) = item_index(&scope); + let (mut comps, isolates, _) = scope_components(&scope); assert!(isolates.is_empty()); - // Order-independent: sort components by their item names for stable assert. comps.sort_by_key(|c| { c.iter() - .map(|&i| g.idx_to_item[i].clone()) + .map(|&i| idx_to_item[i].clone()) .collect::>() }); assert_eq!(comps.len(), 2); - let comp0 = comps[0] - .iter() - .map(|&i| g.idx_to_item[i].as_str()) - .collect::>(); - let comp1 = comps[1] - .iter() - .map(|&i| g.idx_to_item[i].as_str()) - .collect::>(); - assert_eq!(comp0, vec!["a", "b"]); - assert_eq!(comp1, vec!["c", "d"]); } - /// A random spanning tree over 26 items needs only n−1 = 25 pairwise votes. - /// When each vote uses the "perfect" ratio (strength left : strength right = - /// (idx_left+1) : (idx_right+1)), rank centrality recovers the true order. - /// See `rank-eric.py` (Eric's demo of Negahban–Oh–Shah rank centrality). #[test] fn twenty_five_random_votes_perfect_ratios_sort_alphabet() { use rand::seq::SliceRandom; @@ -390,13 +403,13 @@ mod tests { let mut perm: Vec = (0..N).collect(); perm.shuffle(&mut rng); - let mut g = mk_group(); + let mut scope = mk_scope(); for k in 1..N { let i = *perm[..k].choose(&mut rng).unwrap(); let j = perm[k]; let (a, b) = (letters[i], letters[j]); apply( - &mut g, + &mut scope, vote( k as i64, &a.to_string(), @@ -407,34 +420,25 @@ mod tests { ); } - let ranked = ranked_items(&g); + let ranked = ranked_items(&scope); assert_eq!(ranked.len(), N); for (rank, item) in ranked.iter().enumerate() { let expected = char::from(b'a' + (N - 1 - rank) as u8); - assert_eq!( - item.item.as_str(), - expected.to_string(), - "rank {rank}: expected '{expected}', got '{}'", - item.item.as_str() - ); + assert_eq!(item.item.as_str(), expected.to_string()); } } #[test] fn subset_ranking_ranks_within_component_only() { - let mut g = mk_group(); - apply(&mut g, vote(1, "a", "b", 3, 1)); // a > b - apply(&mut g, vote(2, "c", "d", 1, 4)); // d > c - - let (comps, _) = connected_components_from_voted_pairs( - g.idx_to_item.len(), - g.voted_pairs.iter().copied(), - ); + let mut scope = mk_scope(); + apply(&mut scope, vote(1, "a", "b", 3, 1)); + apply(&mut scope, vote(2, "c", "d", 1, 4)); + + let (comps, _, _) = scope_components(&scope); assert_eq!(comps.len(), 2); - // Rank each component and ensure winner is first within that component. for comp in comps { - let ranked = ranked_items_subset(&g, &comp, 10000, 1e-8); + let ranked = ranked_items_subset(&scope, &comp, 10000, 1e-8); assert_eq!(ranked.len(), 2); let names = ranked.iter().map(|r| r.item.as_str()).collect::>(); if names.contains(&"a") { diff --git a/server/src/reducer.rs b/server/src/reducer.rs index 8c4c9f83635cbcbb037d680dbae2aa99cb2dc785..e6d8c8d2fe763f4928cfbd7c300c8d9768968a3f 100644 --- a/server/src/reducer.rs +++ b/server/src/reducer.rs @@ -4,6 +4,24 @@ use serde::{Deserialize, Serialize}; use crate::path_types::ItemId; +/// `(actor_uuid, min_item_id, max_item_id)` — one vote slot per human per pair. +pub type UuidVoteKey = (String, String, String); + +pub fn canonical_pair_ids(a: &ItemId, b: &ItemId) -> (String, String) { + let ak = a.as_str().to_string(); + let bk = b.as_str().to_string(); + if ak <= bk { + (ak, bk) + } else { + (bk, ak) + } +} + +pub fn uuid_vote_key(actor_uuid: &str, a: &ItemId, b: &ItemId) -> UuidVoteKey { + let (lo, hi) = canonical_pair_ids(a, b); + (actor_uuid.to_string(), lo, hi) +} + /// Parsed pairwise vote (internal representation). #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct VoteData { @@ -44,118 +62,20 @@ impl VoteData { } } +/// Votes cast within one ranking scope (parent node). Edges and rankings are +/// derived on demand from [`Self::uuid_votes`]. #[derive(Debug, Clone, Default, Serialize, Deserialize)] -pub struct GroupState { - pub item_to_idx: HashMap, - pub idx_to_item: Vec, - pub edges: HashMap<(usize, usize), f64>, - pub voted_pairs: HashSet<(usize, usize)>, - /// Latest vote per `(actor_uuid, min_idx, max_idx)` — Sybil dedup anchor. - pub uuid_votes: HashMap<(String, usize, usize), VoteData>, +pub struct ScopeVotes { + pub uuid_votes: HashMap, pub recent_votes: Vec, } -impl GroupState { - pub fn new() -> Self { - Self { - item_to_idx: HashMap::new(), - idx_to_item: Vec::new(), - edges: HashMap::new(), - voted_pairs: HashSet::new(), - uuid_votes: HashMap::new(), - recent_votes: Vec::new(), - } - } - - fn ensure_item(&mut self, item: &ItemId) -> usize { - if let Some(&idx) = self.item_to_idx.get(item) { - return idx; - } - let idx = self.idx_to_item.len(); - self.idx_to_item.push(item.clone()); - self.item_to_idx.insert(item.clone(), idx); - idx - } - - fn add_edge_weight(&mut self, src: usize, dst: usize, w: f64) { - if w <= 0.0 { - return; - } - *self.edges.entry((src, dst)).or_insert(0.0) += w; - } - - fn subtract_edge_weight(&mut self, src: usize, dst: usize, w: f64) { - if w <= 0.0 { - return; - } - if let Some(entry) = self.edges.get_mut(&(src, dst)) { - *entry -= w; - if *entry <= 0.0 { - self.edges.remove(&(src, dst)); - } - } - } - - fn apply_weights(&mut self, vote: &VoteData, a_idx: usize, b_idx: usize) { - let w_a = vote.ratio_left as f64 * vote.trust_weight; - let w_b = vote.ratio_right as f64 * vote.trust_weight; - let (i, j) = if a_idx < b_idx { - (a_idx, b_idx) - } else { - (b_idx, a_idx) - }; - self.voted_pairs.insert((i, j)); - self.add_edge_weight(b_idx, a_idx, w_a); - self.add_edge_weight(a_idx, b_idx, w_b); - } - - fn rollback_weights(&mut self, vote: &VoteData) { - let a_idx = match self.item_to_idx.get(&vote.a) { - Some(&i) => i, - None => return, - }; - let b_idx = match self.item_to_idx.get(&vote.b) { - Some(&i) => i, - None => return, - }; - let w_a = vote.ratio_left as f64 * vote.trust_weight; - let w_b = vote.ratio_right as f64 * vote.trust_weight; - self.subtract_edge_weight(b_idx, a_idx, w_a); - self.subtract_edge_weight(a_idx, b_idx, w_b); - } - - /// Apply a validated vote, deduplicating by `actor_uuid` per unordered pair. +impl ScopeVotes { pub fn apply_vote(&mut self, vote: VoteData, actor_uuid: &str) { - let a_idx = self.ensure_item(&vote.a); - let b_idx = self.ensure_item(&vote.b); - let (i, j) = if a_idx < b_idx { - (a_idx, b_idx) - } else { - (b_idx, a_idx) - }; - - let dedupe_key = (actor_uuid.to_string(), i, j); - if let Some(old) = self.uuid_votes.get(&dedupe_key).cloned() { - self.rollback_weights(&old); - } - - self.apply_weights(&vote, a_idx, b_idx); - self.uuid_votes.insert(dedupe_key, vote.clone()); + let key = uuid_vote_key(actor_uuid, &vote.a, &vote.b); + self.uuid_votes.insert(key, vote.clone()); self.recent_votes.push(vote); } - - /// Rebuild edge weights from deduped uuid votes (load path — no rollback). - pub fn ingest_uuid_vote(&mut self, vote: VoteData, actor_uuid: &str) { - let a_idx = self.ensure_item(&vote.a); - let b_idx = self.ensure_item(&vote.b); - let (i, j) = if a_idx < b_idx { - (a_idx, b_idx) - } else { - (b_idx, a_idx) - }; - self.apply_weights(&vote, a_idx, b_idx); - self.uuid_votes.insert((actor_uuid.to_string(), i, j), vote); - } } /// Structured data imported from Reddit or elsewhere. @@ -179,7 +99,7 @@ pub struct NodeState { /// Ephemeral display view (Reddit title/author/etc.; not event-logged). pub data: Option, pub children: HashSet, - pub local_ranking: GroupState, + pub votes: ScopeVotes, } impl NodeState { @@ -242,7 +162,7 @@ impl GlobalTree { if let Some(node) = self.nodes.get_mut(parent) { node.children.insert(vote.a.clone()); node.children.insert(vote.b.clone()); - node.local_ranking.apply_vote(vote, actor_uuid); + node.votes.apply_vote(vote, actor_uuid); } } @@ -276,6 +196,7 @@ impl GlobalTree { #[cfg(test)] mod tests { use super::*; + use crate::ranking::edge_weight_sum; fn vote(ts: i64, a: &str, b: &str, l: i32, r: i32, pseudonym: &str) -> VoteData { VoteData { @@ -310,26 +231,23 @@ mod tests { #[test] fn same_uuid_replaces_prior_vote_on_pair() { - let mut g = GroupState::new(); + let mut scope = ScopeVotes::default(); let uuid = "u1"; - g.apply_vote(vote(1, "a", "b", 2, 1, "alice"), uuid); - let first_total: f64 = g.edges.values().sum(); - assert_eq!(first_total, 3.0); + scope.apply_vote(vote(1, "a", "b", 2, 1, "alice"), uuid); + assert_eq!(edge_weight_sum(&scope), 3.0); - g.apply_vote(vote(2, "a", "b", 0, 1, "bob"), uuid); - let second_total: f64 = g.edges.values().sum(); - assert_eq!(second_total, 1.0); - assert_eq!(g.uuid_votes.len(), 1); + scope.apply_vote(vote(2, "a", "b", 0, 1, "bob"), uuid); + assert_eq!(edge_weight_sum(&scope), 1.0); + assert_eq!(scope.uuid_votes.len(), 1); } #[test] fn different_uuids_both_count() { - let mut g = GroupState::new(); - g.apply_vote(vote(1, "a", "b", 2, 1, "alice"), "u1"); - g.apply_vote(vote(2, "a", "b", 0, 1, "bob"), "u2"); - let total: f64 = g.edges.values().sum(); - assert_eq!(total, 4.0); - assert_eq!(g.uuid_votes.len(), 2); + let mut scope = ScopeVotes::default(); + scope.apply_vote(vote(1, "a", "b", 2, 1, "alice"), "u1"); + scope.apply_vote(vote(2, "a", "b", 0, 1, "bob"), "u2"); + assert_eq!(edge_weight_sum(&scope), 4.0); + assert_eq!(scope.uuid_votes.len(), 2); } #[test] diff --git a/server/src/state.rs b/server/src/state.rs index 8dabc93e95c39cbe18d69596dee79683b384ef86..dcb82ff4beeaac8f0820de0e0131ca1f6bd81dcc 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -269,7 +269,7 @@ mod tests { use super::{normalize_scope, parse_item_param, AppConfig, AppState}; use crate::{ event_log::EventLog, events::Event, path_types::ItemId, projection_apply, - projection_store::ProjectionStore, reducer::EntityData, + projection_store::ProjectionStore, ranking::edge_weight_sum, reducer::EntityData, }; fn event_record(seq: u64, event: Event) -> crate::events::EventRecord { @@ -442,7 +442,7 @@ mod tests { assert_eq!(projection_store.last_applied_event_count().unwrap(), 1); let first = projection_store.scope_tree(&ItemId::root()).unwrap(); let first_root = first.get(&ItemId::root()).unwrap(); - let first_edge_total: f64 = first_root.local_ranking.edges.values().sum(); + let first_edge_total = edge_weight_sum(&first_root.votes); assert_eq!(first_edge_total, 3.0); super::catch_up_projection(&log, &projection_store) @@ -451,7 +451,7 @@ mod tests { assert_eq!(projection_store.last_applied_event_count().unwrap(), 1); let second = projection_store.scope_tree(&ItemId::root()).unwrap(); let second_root = second.get(&ItemId::root()).unwrap(); - let second_edge_total: f64 = second_root.local_ranking.edges.values().sum(); + let second_edge_total = edge_weight_sum(&second_root.votes); assert_eq!(second_edge_total, first_edge_total); } @@ -529,9 +529,8 @@ mod tests { let root = projected.get(&ItemId::root()).unwrap(); assert!(root.children.contains(&ItemId::parse("alpha").unwrap())); assert!(root.children.contains(&ItemId::parse("beta").unwrap())); - assert_eq!(root.local_ranking.idx_to_item.len(), 2); - let edge_total: f64 = root.local_ranking.edges.values().sum(); - assert_eq!(edge_total, 3.0); + assert_eq!(crate::ranking::ranked_items(&root.votes).len(), 2); + assert_eq!(edge_weight_sum(&root.votes), 3.0); } #[tokio::test] @@ -620,7 +619,7 @@ mod tests { let root = tree.get(&ItemId::root()).unwrap(); assert!(root.children.contains(&ItemId::parse("beta").unwrap())); assert!(root.children.contains(&ItemId::parse("gamma").unwrap())); - assert_eq!(root.local_ranking.idx_to_item.len(), 3); + assert_eq!(crate::ranking::ranked_items(&root.votes).len(), 3); } #[test] diff --git a/server/src/storage_schema.rs b/server/src/storage_schema.rs index 67c9f4ab2e1865f8da81b6735dd2a05b87e5e366..fe2671b876f3df77fdfd402dbc845ff1eb3512cd 100644 --- a/server/src/storage_schema.rs +++ b/server/src/storage_schema.rs @@ -3,86 +3,53 @@ //! //! Votes are stored as deduped `uuid_votes` entries plus an append-only //! `recent_votes` audit list. Edge weights for rank centrality are derived -//! from `uuid_votes` on read, not incrementally merged in RocksDB. +//! from `uuid_votes` on read, not stored in RocksDB. -use std::collections::{HashSet}; +use std::collections::HashSet; use durable::{Batch, Db, Durable, Leaf, List, Map}; use crate::{ path_types::ItemId, - reducer::{EntityData, GroupState, NodeState, VoteData}, + reducer::{EntityData, NodeState, ScopeVotes, VoteData, UuidVoteKey, uuid_vote_key}, storage_dto::{ decode_entity_data, decode_vote, encode_entity_data, encode_vote, parse_stored_id, StoredEntityDataV1, StoredVoteV1, }, }; -/// `(actor_uuid, min_item_id, max_item_id)` — one vote slot per human per pair. -pub type UuidVoteKey = (String, String, String); - /// One node in the fractal tree, exploded into precisely-updatable collections. #[derive(Durable)] #[allow(dead_code)] pub struct NodeSchema { - /// Presence marker (a node "exists" once ensured/voted/imported). pub present: Leaf, - /// Domain-specific derived view (Reddit title/author/…); absent => None. pub data: Leaf, - /// Child ids (a set; value is always `true`). pub children: Map>, - /// Latest vote per actor per unordered pair; edges are derived from this on read. pub uuid_votes: Map>, - /// Recent votes, append-only oldest-first (cap applied on read). pub recent_votes: List>, - /// When ephemeral Reddit display content was last fetched (ms); absent after eviction. pub fetched_at: Leaf, } -/// The single database root: nodes, identity maps, view counts, and metadata. #[derive(Durable)] #[allow(dead_code)] pub struct Store { pub nodes: Map, - /// Global pseudonym → actor UUID (Sybil dedup anchor). pub pseudonyms: Map>, pub proj_meta: Map>, pub view_counts: Map>, pub view_meta: Map>, } -/// Max recent votes returned when loading a node (query-time cap only). pub const RECENT_VOTES_CAP: u64 = 200; fn id_key(id: &ItemId) -> String { id.as_str().to_string() } -fn pair_keys(a: &ItemId, b: &ItemId) -> (String, String) { - let ak = id_key(a); - let bk = id_key(b); - if ak <= bk { - (ak, bk) - } else { - (bk, ak) - } -} - -pub fn uuid_vote_key(actor_uuid: &str, a: &ItemId, b: &ItemId) -> UuidVoteKey { - let (lo, hi) = pair_keys(a, b); - (actor_uuid.to_string(), lo, hi) -} - -/// Path to a node by id. pub fn node(id: &ItemId) -> durable::Path { Store::root().nodes().key(&id_key(id)) } -// --------------------------------------------------------------------------- -// Reconstruction (durable -> in-memory) -// --------------------------------------------------------------------------- - -/// Reconstruct a node's in-memory state, or `None` if the node does not exist. pub fn load_node_state(db: &Db, id: &ItemId) -> durable::Result> { let np = node(id); let present = np.present().get(db)?.unwrap_or(false); @@ -100,47 +67,39 @@ pub fn load_node_state(db: &Db, id: &ItemId) -> durable::Result) -> durable::Result { - let mut group = GroupState::new(); +fn load_scope_votes(db: &Db, np: &durable::Path) -> durable::Result { + let mut votes = ScopeVotes::default(); for (key, stored) in np.uuid_votes().iter(db)? { - let (actor_uuid, _lo, _hi) = key; let vote = decode_vote(stored).map_err(durable::Error::Deserialize)?; - group.ingest_uuid_vote(vote, &actor_uuid); + votes.uuid_votes.insert(key, vote); } let stored = np.recent_votes().iter(db)?; let cap = RECENT_VOTES_CAP as usize; let start = stored.len().saturating_sub(cap); - group.recent_votes = stored[start..] + votes.recent_votes = stored[start..] .iter() .map(|s| decode_vote(s.clone()).map_err(durable::Error::Deserialize)) .collect::, _>>()?; - Ok(group) + Ok(votes) } fn parse_storage_id(s: &str) -> durable::Result { parse_stored_id(s).map_err(durable::Error::Deserialize) } -// --------------------------------------------------------------------------- -// Write helpers (event -> reified point updates on a batch) -// --------------------------------------------------------------------------- - -/// Wire a node and its ancestors into the tree exactly like -/// [`crate::reducer::GlobalTree::ensure_path`]: set presence and parent→child -/// links along the canonical breadcrumb path. pub fn ensure_path_writes(batch: &mut Batch, id: &ItemId) { let root = ItemId::root(); batch.write(node(&root).present().set(&true)); @@ -161,7 +120,6 @@ pub fn ensure_path_writes(batch: &mut Batch, id: &ItemId) { } } -/// Reified writes for a validated vote under `parent`. pub fn vote_writes( batch: &mut Batch, parent: &ItemId, @@ -182,14 +140,12 @@ pub fn vote_writes( Ok(()) } -/// Reified writes for ephemeral Reddit display content (not event-logged). pub fn entity_content_writes(batch: &mut Batch, id: &ItemId, view: &EntityData, fetched_at: i64) { ensure_path_writes(batch, id); batch.write(node(id).data().set(&encode_entity_data(view))); batch.write(node(id).fetched_at().set(&fetched_at)); } -/// Clear cached display content for one node (structure/votes are untouched). pub fn entity_content_clear_writes(batch: &mut Batch, id: &ItemId) { batch.write(node(id).data().delete()); batch.write(node(id).fetched_at().delete()); @@ -199,6 +155,7 @@ pub fn entity_content_clear_writes(batch: &mut Batch, id: &ItemId) { mod tests { use super::*; use crate::identity::{seed_default_pseudonym, DEFAULT_ACTOR_UUID, DEFAULT_PSEUDONYM}; + use crate::ranking::edge_weight_sum; fn sample_vote(ts: i64, a: &str, b: &str, l: i32, r: i32) -> VoteData { VoteData { @@ -213,7 +170,7 @@ mod tests { } #[test] - fn vote_roundtrip_reconstructs_group_state() { + fn vote_roundtrip_reconstructs_ranking() { let dir = tempfile::tempdir().unwrap(); let db = Db::open(dir.path()).unwrap(); seed_default_pseudonym(&db).unwrap(); @@ -225,11 +182,8 @@ mod tests { batch.commit().unwrap(); let node_state = load_node_state(&db, &parent).unwrap().unwrap(); - let g = &node_state.local_ranking; - assert_eq!(g.idx_to_item.len(), 2); - let edge_total: f64 = g.edges.values().sum(); - assert_eq!(edge_total, 3.0); - assert_eq!(g.recent_votes.len(), 1); + assert_eq!(edge_weight_sum(&node_state.votes), 3.0); + assert_eq!(node_state.votes.recent_votes.len(), 1); assert!(node_state.children.contains(&ItemId::opaque("alpha"))); assert!(node_state.children.contains(&ItemId::opaque("beta"))); } @@ -263,10 +217,9 @@ mod tests { .unwrap(); batch.commit().unwrap(); - let g = &load_node_state(&db, &parent).unwrap().unwrap().local_ranking; - let edge_total: f64 = g.edges.values().sum(); - assert_eq!(edge_total, 1.0); - assert_eq!(g.uuid_votes.len(), 1); + let votes = &load_node_state(&db, &parent).unwrap().unwrap().votes; + assert_eq!(edge_weight_sum(votes), 1.0); + assert_eq!(votes.uuid_votes.len(), 1); } #[test] @@ -293,15 +246,8 @@ mod tests { ); let node_state = load_node_state(&db, &parent).unwrap().unwrap(); - assert_eq!(node_state.local_ranking.recent_votes.len(), RECENT_VOTES_CAP as usize); - assert_eq!( - node_state - .local_ranking - .recent_votes - .first() - .map(|v| v.ts), - Some(10) - ); + assert_eq!(node_state.votes.recent_votes.len(), RECENT_VOTES_CAP as usize); + assert_eq!(node_state.votes.recent_votes.first().map(|v| v.ts), Some(10)); } #[test] diff --git a/server/tests/integration_ui.rs b/server/tests/integration_ui.rs index cc7a16d756673b95ba336f2d6130eaf40908cc60..40e4cfe1eec36dad29a075fb01aefb4dffd856d0 100644 --- a/server/tests/integration_ui.rs +++ b/server/tests/integration_ui.rs @@ -132,7 +132,7 @@ async fn post_ui_record_vote_morphs_ranking_and_persists() { let state = create_app_state(cfg).await; let tree = state.scope_tree(&ItemId::root()).unwrap(); let root = tree.get(&ItemId::root()).expect("root node after replay"); - let ranked = sorter2_server::ranking::ranked_items(&root.local_ranking); + let ranked = sorter2_server::ranking::ranked_items(&root.votes); assert_eq!(ranked.len(), 2); assert_eq!(ranked[0].item.as_str(), "alpha"); }