{"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[c7ef287e] Rank every eligible commit with the LLM council.\n\nStop short-circuiting on a single contributor; pairwise-sort commits, roll scores up for emission payouts, and surface commit rankings on epoch pages.\n\nCo-authored-by: Cursor \n\nSide A — unified diff (full patch):\ndiff --git a/constitution.py b/constitution.py\nindex 26ba130e885e0e69fb7874ca5c3f07f42100a150..71fc46b4c9a4d7a9ea7bf319860b0db1acc4a673 100644\n--- a/constitution.py\n+++ b/constitution.py\n@@ -503,20 +503,22 @@ def _epochs_in_ledger() -> list[int]:\n \n \n def build_pairwise_prompt(side_a: dict, side_b: dict) -> str:\n- return f\"\"\"You are ranking contributions to an open source project.\n-Compare these two sides (each may be one or more commits). Decide which side contributed more.\n+ return f\"\"\"You are ranking individual git commits to an open source project.\n+Compare these two commits. Decide which commit contributed more.\n Return ONLY a JSON object: {{\"winner\": \"A\" or \"B\", \"ratio\": \"N:M\", \"explanation\": \"...\"}}\n \n-Side A — commit messages:\n+Side A — contributor: {side_a.get('contributor', '?')}\n+Side A — commit message:\n {side_a['message']}\n \n-Side A — unified diffs (full patches):\n+Side A — unified diff (full patch):\n {side_a['diff']}\n \n-Side B — commit messages:\n+Side B — contributor: {side_b.get('contributor', '?')}\n+Side B — commit message:\n {side_b['message']}\n \n-Side B — unified diffs (full patches):\n+Side B — unified diff (full patch):\n {side_b['diff']}\"\"\"\n \n \n@@ -1518,16 +1520,28 @@ async def broadcast_js(js: str):\n await queue.put(js)\n \n \n-def _author_side_for_llm(author: str, author_commits: dict) -> dict:\n- cs = author_commits[author]\n+def _commit_side_for_llm(row: dict) -> dict:\n+ oid = row[\"oid\"]\n+ short = oid.split(\":\", 1)[1][:8] if \":\" in oid else oid[:8]\n return {\n- \"message\": \"\\n\".join(f\"[{c['sha']}] {c['message']}\" for c in cs),\n- \"diff\": \"\\n\\n\".join(f\"=== {c['sha']} ===\\n{c['diff']}\" for c in cs),\n- \"commit_ids\": [c[\"commit_id\"] for c in cs],\n- \"contributor\": author,\n+ \"message\": f\"[{short}] {row['message']}\",\n+ \"diff\": row[\"patch\"] or \"\",\n+ \"commit_id\": commit_id_for_oid(oid),\n+ \"contributor\": row[\"contributor\"],\n+ \"oid\": oid,\n }\n \n \n+def _rollup_contributor_scores(\n+ ordered: list[dict], commit_scores: list[Decimal]\n+) -> dict[str, Decimal]:\n+ totals: dict[str, Decimal] = {}\n+ for row, score in zip(ordered, commit_scores):\n+ contributor = row[\"contributor\"]\n+ totals[contributor] = totals.get(contributor, Decimal(\"0\")) + score\n+ return totals\n+\n+\n def _find_judgment(comparison_id: str, model_id: str) -> dict | None:\n for e in evidence_by_kind(\"llm.judgment\"):\n p = e.payload\n@@ -1546,101 +1560,106 @@ def _find_ranking_models(ranking_run_id: str) -> list[str] | None:\n \n \n async def rank_commits(commits: list[dict], *, epoch: int = -1):\n+ \"\"\"Pairwise-rank every eligible commit; roll scores up to contributors.\"\"\"\n if not commits:\n return {}, [], {\"ranking_run_id\": \"\", \"ranking_event_id\": \"\"}\n \n- commit_ids = sorted(commit_id_for_oid(row[\"oid\"]) for row in commits)\n+ ordered = sorted(commits, key=lambda r: r[\"oid\"])\n+ commit_ids = [commit_id_for_oid(row[\"oid\"]) for row in ordered]\n ranking_run_id = _content_id(\"rank\", {\n \"epoch\": epoch,\n- \"commit_ids\": commit_ids,\n+ \"commit_ids\": sorted(commit_ids),\n })\n- contributors = sorted(set(c[\"contributor\"] for c in commits))\n+ contributors = sorted({c[\"contributor\"] for c in ordered})\n \n- if len(contributors) == 1:\n+ # Nothing to compare: a single commit (not a single contributor).\n+ if len(ordered) == 1:\n await append_evidence(epoch, \"ranking.started\", {\n \"ranking_run_id\": ranking_run_id,\n \"commit_ids\": commit_ids,\n \"contributors\": contributors,\n \"models\": [],\n- \"summary\": f\"ranking epoch {epoch}: single contributor\",\n+ \"summary\": f\"ranking epoch {epoch}: single commit\",\n })\n- ranking = {contributors[0]: Decimal(\"1\")}\n+ commit_ranking = {commit_ids[0]: \"1\"}\n+ contributor_ranking = {ordered[0][\"contributor\"]: Decimal(\"1\")}\n completed = await append_evidence(epoch, \"ranking.completed\", {\n \"ranking_run_id\": ranking_run_id,\n \"models\": [],\n- \"ranking\": {contributors[0]: \"1\"},\n+ \"commit_ranking\": commit_ranking,\n+ \"contributor_ranking\": {ordered[0][\"contributor\"]: \"1\"},\n+ \"ranking\": {ordered[0][\"contributor\"]: \"1\"},\n \"judgment_ids\": [],\n- \"summary\": f\"Only {contributors[0]} is eligible; rank is 1.0\",\n+ \"summary\": f\"Only one eligible commit; {ordered[0]['contributor']} rank 1.0\",\n })\n await broadcast_audit(\n \"ranking\",\n- f\"Only {contributors[0]} is eligible; rank is 1.0\",\n+ f\"Only one eligible commit; {ordered[0]['contributor']} rank 1.0\",\n progress=90,\n phase=\"finalizing\",\n evidence_event_id=completed.event_id,\n evidence_url=_evidence_url(\"event\", completed.event_id),\n links={\"epoch\": _evidence_url(\"epoch\", str(epoch))},\n )\n- return ranking, [], {\n+ return contributor_ranking, [], {\n \"ranking_run_id\": ranking_run_id,\n \"ranking_event_id\": completed.event_id,\n }\n \n if not (OPENROUTER_API_KEY or \"\").strip():\n raise RuntimeError(\n- \"OPENROUTER_API_KEY is required when multiple contributors need ranking\"\n+ \"OPENROUTER_API_KEY is required when multiple commits need ranking\"\n )\n \n models = _find_ranking_models(ranking_run_id)\n if models is None:\n models = await fetch_top_models(n=3)\n if not models:\n- raise RuntimeError(\"no council models available for contributor ranking\")\n+ raise RuntimeError(\"no council models available for commit ranking\")\n await append_evidence(epoch, \"ranking.started\", {\n \"ranking_run_id\": ranking_run_id,\n \"commit_ids\": commit_ids,\n \"contributors\": contributors,\n \"models\": models,\n- \"summary\": f\"Council selected: {', '.join(models)}\",\n+ \"summary\": (\n+ f\"Council selected: {', '.join(models)} — \"\n+ f\"{len(ordered)} commits\"\n+ ),\n })\n await broadcast_audit(\n \"council\",\n- f\"Council selected: {', '.join(models)}\",\n+ f\"Council selected: {', '.join(models)} — ranking {len(ordered)} commits\",\n progress=35,\n phase=\"ranking\",\n )\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n- [\"div.log-council\", f\"Council: {', '.join(models)} — {len(commits)} commits\"]\n+ [\"div.log-council\",\n+ f\"Council: {', '.join(models)} — {len(ordered)} commits\"]\n ]))\n \n- authors = contributors\n- author_commits = {a: [] for a in authors}\n- for row in sorted(commits, key=lambda r: r[\"oid\"]):\n- author_commits[row[\"contributor\"]].append({\n- \"message\": row[\"message\"],\n- \"sha\": row[\"oid\"].split(\":\", 1)[1][:8],\n- \"diff\": row[\"patch\"],\n- \"commit_id\": commit_id_for_oid(row[\"oid\"]),\n- })\n-\n+ sides = [_commit_side_for_llm(row) for row in ordered]\n judgment_ids: list[str] = []\n \n async def compare_fn(i, j):\n- a1, a2 = authors[i], authors[j]\n- side_a = _author_side_for_llm(a1, author_commits)\n- side_b = _author_side_for_llm(a2, author_commits)\n+ side_a, side_b = sides[i], sides[j]\n+ label_a = f\"{side_a['commit_id'][:16]} ({side_a['contributor']})\"\n+ label_b = f\"{side_b['commit_id'][:16]} ({side_b['contributor']})\"\n prompt = build_pairwise_prompt(side_a, side_b)\n comparison_material = {\n \"ranking_run_id\": ranking_run_id,\n \"side_a\": {\n- \"contributor\": a1,\n- \"commit_ids\": side_a[\"commit_ids\"],\n+ \"contributor\": side_a[\"contributor\"],\n+ \"commit_id\": side_a[\"commit_id\"],\n+ \"commit_ids\": [side_a[\"commit_id\"]],\n+ \"oid\": side_a[\"oid\"],\n \"message\": _bytes_blob(side_a[\"message\"]),\n \"diff\": _bytes_blob(side_a[\"diff\"]),\n },\n \"side_b\": {\n- \"contributor\": a2,\n- \"commit_ids\": side_b[\"commit_ids\"],\n+ \"contributor\": side_b[\"contributor\"],\n+ \"commit_id\": side_b[\"commit_id\"],\n+ \"commit_ids\": [side_b[\"commit_id\"]],\n+ \"oid\": side_b[\"oid\"],\n \"message\": _bytes_blob(side_b[\"message\"]),\n \"diff\": _bytes_blob(side_b[\"diff\"]),\n },\n@@ -1650,22 +1669,24 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n comparison_material = {\n **comparison_material,\n \"comparison_id\": comparison_id,\n- \"summary\": f\"Comparing {a1} with {a2}\",\n+ \"summary\": f\"Comparing {label_a} with {label_b}\",\n }\n cmp_ev = await append_evidence(epoch, \"comparison.input\", comparison_material)\n await broadcast_audit(\n \"comparison\",\n- f\"Comparing {a1} with {a2}\",\n+ f\"Comparing commits {label_a} vs {label_b}\",\n phase=\"ranking\",\n evidence_event_id=cmp_ev.event_id,\n evidence_url=_evidence_url(\"comparison\", comparison_id),\n links={\n \"comparison\": _evidence_url(\"comparison\", comparison_id),\n+ \"commit_a\": _evidence_url(\"commit\", side_a[\"commit_id\"]),\n+ \"commit_b\": _evidence_url(\"commit\", side_b[\"commit_id\"]),\n \"epoch\": _evidence_url(\"epoch\", str(epoch)),\n },\n )\n await broadcast_js(exec_event(Three[Selector(\"#emission-status\")][MORPH][\n- [\"div#emission-status\", f\"Comparing {a1} vs {a2}…\"]\n+ [\"div#emission-status\", f\"Comparing {label_a} vs {label_b}…\"]\n ]))\n results = []\n for model in models:\n@@ -1697,9 +1718,15 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n jud_id = (existing or _find_judgment(comparison_id, model) or {}).get(\n \"judgment_id\"\n )\n+ win_label = (\n+ f\"{sides[w]['commit_id'][:16]} ({sides[w]['contributor']})\"\n+ )\n+ lose_label = (\n+ f\"{sides[l]['commit_id'][:16]} ({sides[l]['contributor']})\"\n+ )\n await broadcast_audit(\n \"vote\",\n- f\"{model}: {authors[w]} over {authors[l]} ({result['ratio']})\",\n+ f\"{model}: {win_label} over {lose_label} ({result['ratio']})\",\n phase=\"ranking\",\n evidence_url=(\n _evidence_url(\"judgment\", jud_id) if jud_id else None\n@@ -1714,8 +1741,8 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n [\"div.log-vote\",\n [\"span.model\", model], \" — \",\n- [\"span.winner\", authors[w]], f\" beat \",\n- [\"span.loser\", authors[l]], f\" ({result['ratio']}) \",\n+ [\"span.winner\", win_label], f\" beat \",\n+ [\"span.loser\", lose_label], f\" ({result['ratio']}) \",\n [\"span.explanation\", result[\"explanation\"]],\n ]\n ]))\n@@ -1745,24 +1772,46 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n [\"div#emission-status\", label]\n ]))\n \n- pairs = await pairwise_rank(len(authors), compare_fn, progress_fn)\n+ pairs = await pairwise_rank(len(ordered), compare_fn, progress_fn)\n \n if not pairs:\n- ranking = {authors[0]: Decimal(\"1\")} if authors else {}\n+ commit_score_list = [Decimal(\"1\")]\n else:\n scores = rank_centrality(pairs)\n- ranking = {authors[i]: Decimal(str(scores[i])) for i in range(len(authors))}\n- ranking_rows = sorted(ranking.items(), key=lambda x: x[1], reverse=True)\n+ commit_score_list = [Decimal(str(scores[i])) for i in range(len(ordered))]\n+\n+ commit_ranking = {\n+ commit_ids[i]: str(commit_score_list[i]) for i in range(len(ordered))\n+ }\n+ contributor_totals = _rollup_contributor_scores(ordered, commit_score_list)\n+ contrib_rows = sorted(\n+ contributor_totals.items(), key=lambda x: x[1], reverse=True\n+ )\n+ commit_rows = sorted(\n+ ((commit_ids[i], commit_score_list[i], ordered[i][\"contributor\"])\n+ for i in range(len(ordered))),\n+ key=lambda x: x[1],\n+ reverse=True,\n+ )\n completed = await append_evidence(epoch, \"ranking.completed\", {\n \"ranking_run_id\": ranking_run_id,\n \"models\": models,\n- \"ranking\": {a: str(s) for a, s in ranking_rows},\n+ \"commit_ranking\": commit_ranking,\n+ \"contributor_ranking\": {a: str(s) for a, s in contrib_rows},\n+ \"ranking\": {a: str(s) for a, s in contrib_rows},\n \"judgment_ids\": judgment_ids,\n- \"summary\": \"Ranking: \" + \", \".join(f\"{a} {s:.4f}\" for a, s in ranking_rows),\n+ \"summary\": (\n+ \"Commit ranking: \"\n+ + \", \".join(\n+ f\"{cid[:16]}={float(s):.4f}\" for cid, s, _ in commit_rows[:12]\n+ )\n+ + (\"…\" if len(commit_rows) > 12 else \"\")\n+ ),\n })\n await broadcast_audit(\n \"ranking\",\n- \"Ranking: \" + \", \".join(f\"{a} {s:.4f}\" for a, s in ranking_rows),\n+ \"Contributor rollup: \"\n+ + \", \".join(f\"{a} {s:.4f}\" for a, s in contrib_rows),\n progress=90,\n phase=\"finalizing\",\n evidence_event_id=completed.event_id,\n@@ -1772,10 +1821,10 @@ async def rank_commits(commits: list[dict], *, epoch: int = -1):\n await broadcast_js(exec_event(Three[Selector(\"#emission-log\")][PREPEND][\n [\"div.log-ranking\",\n [\"b\", \"Ranking: \"],\n- *[[\"span.rank-entry\", f\"{a} {float(s):.3f} \"] for a, s in ranking_rows],\n+ *[[\"span.rank-entry\", f\"{a} {float(s):.3f} \"] for a, s in contrib_rows],\n ]\n ]))\n- return ranking, models, {\n+ return contributor_totals, models, {\n \"ranking_run_id\": ranking_run_id,\n \"ranking_event_id\": completed.event_id,\n }\n@@ -2352,6 +2401,23 @@ async def epoch_detail(epoch: int):\n \n ranking_nodes: list = []\n if ranking_completed:\n+ commit_ranking = ranking_completed.payload.get(\"commit_ranking\") or {}\n+ contrib_ranking = (\n+ ranking_completed.payload.get(\"contributor_ranking\")\n+ or ranking_completed.payload.get(\"ranking\")\n+ or {}\n+ )\n+ commit_rank_links = [\n+ (\n+ f\"{cid[:20]} = {score}\",\n+ _evidence_path(\"commit\", cid),\n+ )\n+ for cid, score in sorted(\n+ commit_ranking.items(),\n+ key=lambda kv: Decimal(str(kv[1])),\n+ reverse=True,\n+ )\n+ ]\n ranking_nodes = [\n [\"p\", ranking_completed.payload.get(\"summary\") or \"ranking completed\"],\n _dl_rows([\n@@ -2362,10 +2428,10 @@ async def epoch_detail(epoch: int):\n ranking_completed.event_id,\n )),\n ]),\n- [\"pre.blob\", json.dumps(\n- ranking_completed.payload.get(\"ranking\") or {},\n- indent=2, sort_keys=True,\n- )],\n+ [\"h3\", \"commit ranking\"],\n+ _link_list(commit_rank_links) if commit_rank_links else [\"p.note\", \"(none)\"],\n+ [\"h3\", \"contributor rollup\"],\n+ [\"pre.blob\", json.dumps(contrib_ranking, indent=2, sort_keys=True)],\n ]\n elif ranking_started:\n ranking_nodes = [[\"p.note\", f\"Ranking started: {ranking_started.event_id}\"]]\n@@ -2386,14 +2452,9 @@ async def epoch_detail(epoch: int):\n else:\n emission_node = [\"p.note\", \"No emission for this epoch.\"]\n \n- contributors = {\n- e.payload.get(\"contributor\")\n- for e in commit_evs\n- if e.payload.get(\"contributor\")\n- }\n no_comparisons_note = \"No comparisons.\"\n- if len(contributors) <= 1:\n- no_comparisons_note += \" Single-contributor — no LLM judgments.\"\n+ if len(commit_evs) <= 1:\n+ no_comparisons_note += \" Fewer than two eligible commits — nothing to pairwise-rank.\"\n \n body = [\n _evidence_nav(),\n@@ -2511,9 +2572,14 @@ async def comparison_detail(comparison_id: str):\n j.payload.get(\"summary\") or jid,\n _evidence_path(\"judgment\", jid),\n ))\n- commit_links = []\n- for cid in (side_a.get(\"commit_ids\") or []) + (side_b.get(\"commit_ids\") or []):\n- commit_links.append((cid, _evidence_path(\"commit\", cid)))\n+ commit_ids = []\n+ for side in (side_a, side_b):\n+ cid = side.get(\"commit_id\")\n+ if cid:\n+ commit_ids.append(cid)\n+ else:\n+ commit_ids.extend(side.get(\"commit_ids\") or [])\n+ commit_links = [(cid, _evidence_path(\"commit\", cid)) for cid in commit_ids]\n return _evidence_page(f\"comparison {comparison_id[:24]}\", [\n _evidence_nav(_a(_evidence_path(\"epoch\", str(ev.epoch)), f\"epoch {ev.epoch}\")),\n [\"div.eyebrow\", \"comparison\"],\n@@ -2522,8 +2588,8 @@ async def comparison_detail(comparison_id: str):\n (\"summary\", p.get(\"summary\")),\n (\"ranking_run_id\", p.get(\"ranking_run_id\")),\n (\"evidence_event\", _a(_evidence_path(\"event\", ev.event_id), ev.event_id)),\n- (\"side_a\", side_a.get(\"contributor\")),\n- (\"side_b\", side_b.get(\"contributor\")),\n+ (\"side_a\", f\"{side_a.get('commit_id', '?')} ({side_a.get('contributor', '?')})\"),\n+ (\"side_b\", f\"{side_b.get('commit_id', '?')} ({side_b.get('contributor', '?')})\"),\n ]),\n [\"p\", _a(f\"/comparisons/{comparison_id}/prompt\", \"download prompt\")],\n [\"h2\", \"commits\"],\n@@ -2534,12 +2600,12 @@ async def comparison_detail(comparison_id: str):\n _link_list(judgment_links),\n [\"h2\", \"prompt\"],\n _pre_blob(_blob_text(p.get(\"prompt\"))),\n- [\"h2\", f\"side A — {side_a.get('contributor', '?')}\"],\n+ [\"h2\", f\"side A — {side_a.get('commit_id', side_a.get('contributor', '?'))}\"],\n [\"h3\", \"message\"],\n _pre_blob(_blob_text(side_a.get(\"message\"))),\n [\"h3\", \"diff\"],\n _pre_blob(_blob_text(side_a.get(\"diff\"))),\n- [\"h2\", f\"side B — {side_b.get('contributor', '?')}\"],\n+ [\"h2\", f\"side B — {side_b.get('commit_id', side_b.get('contributor', '?'))}\"],\n [\"h3\", \"message\"],\n _pre_blob(_blob_text(side_b.get(\"message\"))),\n [\"h3\", \"diff\"],\n@@ -3156,9 +3222,9 @@ async def watch():\n ],\n [\"p.note\",\n (\n- \"Pairwise council voting is ready.\"\n+ \"Pairwise council ranks every eligible commit.\"\n if key_ok else\n- \"Single-contributor epochs can finalize, but contested rankings require OPENROUTER_API_KEY.\"\n+ \"Epochs with two or more eligible commits require OPENROUTER_API_KEY.\"\n )\n ],\n ],\ndiff --git a/tests/integration.clj b/tests/integration.clj\nindex 11140e95a8cd86768a661a30e6b29a3ddfc95fcc..39b2476cb5ddf80733769f61343c81ba7287a974 100644\n--- a/tests/integration.clj\n+++ b/tests/integration.clj\n@@ -507,7 +507,7 @@\n \n (bind or-state @(:state or-mock))\n (assert! (pos? (:model-requests or-state)) \"OpenRouter /models was called\")\n- (assert! (>= (:compare-requests or-state) 3) \"at least 3 pairwise LLM calls (2 authors × 3 models)\")\n+ (assert! (>= (:compare-requests or-state) 3) \"at least 3 pairwise LLM calls (2 commits × 3 models)\")\n \n (bind ledger2 (get-json base-url \"/api/ledger\"))\n (assert! (>= (count ledger2) 3)\n@@ -543,6 +543,8 @@\n \"epoch page links comparisons\")\n (assert! (str/includes? epoch-html \"/judgments/\")\n \"epoch page links judgments\")\n+ (assert! (str/includes? epoch-html \"commit ranking\")\n+ \"epoch page shows per-commit ranking\")\n (bind commit-href\n (second (re-find #\"/commits/(c_[a-f0-9]+)\" epoch-html)))\n (assert! (some? commit-href) \"found a commit id on epoch page\")\ndiff --git a/tests/test_git_discovery.py b/tests/test_git_discovery.py\nindex 00d26bfa85737b178c8822804e278211974e0efe..0f002a58bd9a122d41c5a85e32c16ed2b4404d2f 100644\n--- a/tests/test_git_discovery.py\n+++ b/tests/test_git_discovery.py\n@@ -434,7 +434,7 @@ def test_emission_distribution_sums_exactly_to_total(\n assert entry.discovery_snapshot_id == \"ranked-snapshot\"\n \n \n-def test_single_contributor_ranking_is_total_and_uses_no_pairwise_votes(\n+def test_single_commit_ranking_skips_pairwise(\n discovery_config, monkeypatch,\n ):\n monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n@@ -454,6 +454,65 @@ def test_single_contributor_ranking_is_total_and_uses_no_pairwise_votes(\n assert info[\"ranking_event_id\"]\n \n \n+def test_same_contributor_multiple_commits_runs_pairwise(\n+ discovery_config, monkeypatch,\n+):\n+ monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n+ calls = {\"n\": 0}\n+\n+ async def models(n=3):\n+ return [\"m1\", \"m2\", \"m3\"]\n+\n+ async def compare(model_id, side_a, side_b, **kwargs):\n+ calls[\"n\"] += 1\n+ assert \"commit_id\" in side_a and \"commit_id\" in side_b\n+ if kwargs.get(\"persist\"):\n+ attempt_id = c.attempt_id_for(kwargs[\"comparison_id\"], model_id, 1)\n+ await c.append_evidence(kwargs[\"epoch\"], \"llm.judgment\", {\n+ \"judgment_id\": c.judgment_id_for({\n+ \"attempt_id\": attempt_id,\n+ \"comparison_id\": kwargs[\"comparison_id\"],\n+ \"model_id\": model_id,\n+ \"winner\": \"A\",\n+ \"ratio\": \"2:1\",\n+ \"explanation\": \"ok\",\n+ }),\n+ \"attempt_id\": attempt_id,\n+ \"comparison_id\": kwargs[\"comparison_id\"],\n+ \"model_id\": model_id,\n+ \"winner\": \"A\",\n+ \"ratio\": \"2:1\",\n+ \"explanation\": \"ok\",\n+ \"summary\": \"ok\",\n+ })\n+ return {\"winner\": \"A\", \"ratio\": \"2:1\", \"explanation\": \"ok\"}\n+\n+ monkeypatch.setattr(c, \"fetch_top_models\", models)\n+ monkeypatch.setattr(c, \"llm_pairwise_compare\", compare)\n+ monkeypatch.setattr(c, \"OPENROUTER_API_KEY\", \"test-key\")\n+ commits = [\n+ {\n+ \"contributor\": \"alice\",\n+ \"oid\": \"sha1:\" + char * 40,\n+ \"message\": f\"msg-{char}\",\n+ \"patch\": f\"patch-{char}\",\n+ }\n+ for char in (\"a\", \"b\", \"c\")\n+ ]\n+ ranking, used, info = asyncio.run(c.rank_commits(commits, epoch=0))\n+ assert set(ranking) == {\"alice\"}\n+ assert ranking[\"alice\"] > 0\n+ assert used == [\"m1\", \"m2\", \"m3\"]\n+ assert calls[\"n\"] >= 3\n+ completed = next(\n+ e for e in c.store.read()\n+ if isinstance(e, c.Evidence) and e.kind == \"ranking.completed\"\n+ )\n+ assert len(completed.payload[\"commit_ranking\"]) == 3\n+ assert \"alice\" in completed.payload[\"contributor_ranking\"]\n+ assert info[\"ranking_event_id\"]\n+\n+\n def test_any_council_failure_aborts_ranking(discovery_config, monkeypatch):\n monkeypatch.setattr(c, \"store\", c.JsonlStore(discovery_config / \"ledger.jsonl\"))\n \n@@ -479,16 +538,16 @@ def test_any_council_failure_aborts_ranking(discovery_config, monkeypatch):\n asyncio.run(c.rank_commits(commits, epoch=0))\n \n \n-def test_contested_ranking_requires_openrouter_key(monkeypatch):\n+def test_multi_commit_ranking_requires_openrouter_key(monkeypatch):\n monkeypatch.setattr(c, \"OPENROUTER_API_KEY\", \"\")\n commits = [\n {\n- \"contributor\": contributor,\n+ \"contributor\": \"alice\",\n \"oid\": \"sha1:\" + char * 40,\n- \"message\": contributor,\n+ \"message\": char,\n \"patch\": \"patch\",\n }\n- for contributor, char in [(\"alice\", \"a\"), (\"bob\", \"b\")]\n+ for char in (\"a\", \"b\")\n ]\n with pytest.raises(RuntimeError, match=\"OPENROUTER_API_KEY\"):\n asyncio.run(c.rank_commits(commits))\n\n\nSide B — contributor: tommy-mor\nSide B — commit message:\n[8d8230d1] reddit\n\nSide B — 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","role":"user"}],"model":"openai/gpt-chat-latest"}