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: [4e327784] deploy live constitution dashboard Expose auditable progress and event streaming, configure the production roots and runtime, and make tested main-branch commits the deployment authority. Co-authored-by: Cursor Side A — unified diff (full patch): diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..c9d63a722beba0a0297fc853089332c460ab78dd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +.git +.venv +.hypothesis +__pycache__ +tests +*.json +*.bsp diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000000000000000000000000000000000000..76dcdf82d53177c1e47d86b23a54523239d232a6 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,49 @@ +name: Test and deploy + +on: + push: + branches: [main] + +concurrency: + group: production + cancel-in-progress: false + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: astral-sh/setup-uv@v6 + with: + enable-cache: true + + - name: Run Python tests + run: uv run pytest -q + + - name: Install Babashka + run: | + curl -fsSL https://raw.githubusercontent.com/babashka/babashka/master/install \ + | sudo bash -s -- --dir /usr/local/bin + + - name: Run process integration tests + run: bb TEST.sh + + deploy: + needs: test + runs-on: ubuntu-latest + environment: + name: production + url: https://token.slug.social + steps: + - uses: actions/checkout@v4 + + - uses: superfly/flyctl-actions/setup-flyctl@master + + - name: Deploy to Fly + run: flyctl deploy --remote-only + env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..c9a5c00782371c19ad5ab5c58cf6f5a8ffec0141 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM ghcr.io/astral-sh/uv:python3.11-bookworm-slim + +RUN apt-get update \ + && apt-get install -y --no-install-recommends git ca-certificates \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +COPY pyproject.toml uv.lock ./ +RUN uv sync --frozen --no-install-project + +COPY constitution.py ./ + +ENV PATH="/app/.venv/bin:${PATH}" \ + PYTHONUNBUFFERED="1" + +EXPOSE 8080 +CMD ["python", "constitution.py"] diff --git a/constitution.py b/constitution.py index 4bee9f83663ab7fb36db95129b92b64b4ef57258..a58257e1881b21d1d6fa8e68a3faa222e4f661ef 100644 --- a/constitution.py +++ b/constitution.py @@ -24,12 +24,12 @@ A daily GitHub Action backs up the JSONL ledger to the same repo. Run: uv run constitution.py """ -from decimal import Decimal, getcontext +from decimal import Decimal, getcontext, DefaultContext from datetime import datetime, timezone from fastapi import FastAPI, Request, Response from fastapi.responses import PlainTextResponse, HTMLResponse from starlette.middleware.sessions import SessionMiddleware -import json, time, os, asyncio, httpx, pathlib, subprocess, hashlib, re, fcntl +import json, time, os, asyncio, httpx, pathlib, subprocess, hashlib, re, fcntl, base64 import sympy as sp # type: ignore[reportMissingImports] from tenacity import retry, retry_if_exception, stop_after_attempt, wait_exponential from evaleval import ( @@ -37,6 +37,7 @@ from evaleval import ( exec_event, One, Two, Three, Selector, MORPH, PREPEND, ) +DefaultContext.prec = 50 getcontext().prec = 50 app = FastAPI() @@ -129,14 +130,47 @@ OPENROUTER_BASE_URL = os.environ.get("OPENROUTER_BASE_URL", "https://openrouter. # using the exact same source; their normalized values are committed to every # discovery event. DEFAULT_REPOSITORIES = [ + { + "id": "constitution", + "url": "https://github.com/sortersocial/constitution.git", + "refs": ["refs/heads/**"], + }, { "id": "slug", - "url": "https://github.com/tommy-mor/slug.git", + "url": "https://github.com/sortersocial/slug.git", + "refs": ["refs/heads/**"], + }, + { + "id": "sorter", + "url": "https://github.com/sorterisntonline/sorter.git", + "refs": ["refs/heads/**"], + }, + { + "id": "sorter2", + "url": "https://github.com/sortersocial/sorter2.git", + "refs": ["refs/heads/**"], + }, + { + "id": "sorter-oldest", + "url": "https://github.com/tommy-mor/sorter.git", "refs": ["refs/heads/**"], }, ] DEFAULT_CONTRIBUTORS = { "tommy-mor": ["thmorriss@gmail.com"], + "christopher-whitman": [ + "chris@cwwhitman.com", + "7566903+cwwhitman@users.noreply.github.com", + ], + "jake-chvatal": [ + "jake+github@uln.industries", + "jakechvatal@gmail.com", + "jake@isnt.online", + ], + "lara": ["me@lara.lv"], + "nat-reid": ["nathanielreid@gmail.com"], + "zod": ["jason.p.mcel@gmail.com", "me@zod.tf"], + "jovan": ["jovan@slug.social", "jovan@getcivicai.com"], } REPOSITORIES = json.loads( @@ -147,6 +181,7 @@ CONTRIBUTORS = json.loads( ) GIT_MIRROR_DIR = pathlib.Path(os.environ.get("GIT_MIRROR_DIR", "/data/git")) GIT_TIMEOUT_SECONDS = int(os.environ.get("GIT_TIMEOUT_SECONDS", "120")) +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "") # Council model IDs: slug.social garden rank under this parent (bodies = OpenRouter URLs), then top-up from OpenRouter list. SLUG_SOCIAL_BASE_URL = os.environ.get("SLUG_SOCIAL_BASE_URL", "https://slug.social").rstrip("/") @@ -661,20 +696,30 @@ def _git(repo: pathlib.Path | None, *args: str, input_bytes: bytes | None = None if repo is not None: command += ["-C", str(repo)] command += list(args) + git_env = { + **os.environ, + "GIT_CONFIG_NOSYSTEM": "1", + "GIT_CONFIG_GLOBAL": os.devnull, + "GIT_NO_REPLACE_OBJECTS": "1", + "LC_ALL": "C", + "TZ": "UTC", + } + if GITHUB_TOKEN: + credential = base64.b64encode( + f"x-access-token:{GITHUB_TOKEN}".encode() + ).decode() + git_env.update({ + "GIT_CONFIG_COUNT": "1", + "GIT_CONFIG_KEY_0": "http.https://github.com/.extraHeader", + "GIT_CONFIG_VALUE_0": f"Authorization: Basic {credential}", + }) try: result = subprocess.run( command, input=input_bytes, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - env={ - **os.environ, - "GIT_CONFIG_NOSYSTEM": "1", - "GIT_CONFIG_GLOBAL": os.devnull, - "GIT_NO_REPLACE_OBJECTS": "1", - "LC_ALL": "C", - "TZ": "UTC", - }, + env=git_env, timeout=GIT_TIMEOUT_SECONDS, check=False, ) @@ -844,9 +889,15 @@ def _build_discovery(epoch_n: int, boundary_ms: int, events: list) -> GitDiscove canonical_location = min( locations[qualified_oid], key=lambda x: (x[0], x[1]) ) + # One commit may be reachable from dozens of refs in the same mirror. + # Verify its object once per repository, not once per source ref. + object_locations = { + (str(m), raw_oid): (m, raw_oid) + for _, _, m, raw_oid in locations[qualified_oid] + } object_hashes = { hashlib.sha256(_git(m, "cat-file", "commit", raw_oid)).hexdigest() - for _, _, m, raw_oid in locations[qualified_oid] + for m, raw_oid in object_locations.values() } if len(object_hashes) != 1: raise RuntimeError(f"conflicting Git objects share OID {qualified_oid}") @@ -994,11 +1045,55 @@ async def discover_repositories(epoch_n: int, boundary_ms: int) -> GitDiscovery: SSE_CLIENTS = [] +AUDIT_HISTORY = [] +AUDIT_SEQUENCE = 0 +PROCESS_STATE = { + "running": False, + "phase": "idle", + "progress": 100, + "message": "Waiting for the next epoch", +} + + +def _sse_event(event_name: str, payload: dict) -> str: + return ( + f"event: {event_name}\n" + f"data: {json.dumps(payload, separators=(',', ':'))}\n\n" + ) + + +async def broadcast_audit( + kind: str, + message: str, + *, + progress: int | None = None, + phase: str | None = None, +) -> dict: + global AUDIT_SEQUENCE + AUDIT_SEQUENCE += 1 + if progress is not None: + PROCESS_STATE["progress"] = max(0, min(100, int(progress))) + if phase is not None: + PROCESS_STATE["phase"] = phase + PROCESS_STATE["message"] = message + payload = { + "id": AUDIT_SEQUENCE, + "timestamp_ms": int(time.time() * 1000), + "kind": kind, + "message": message, + **PROCESS_STATE, + } + AUDIT_HISTORY.append(payload) + del AUDIT_HISTORY[:-200] + wire = _sse_event("audit", payload) + for queue in list(SSE_CLIENTS): + await queue.put(wire) + return payload async def broadcast_js(js: str): """Send a JS snippet to all connected SSE clients.""" - for queue in SSE_CLIENTS: + for queue in list(SSE_CLIENTS): await queue.put(js) @@ -1006,10 +1101,29 @@ async def rank_commits(commits: list[dict]): if not commits: return {}, [] - models = await fetch_top_models(n=3) contributors = sorted(set(c["contributor"] for c in commits)) - if len(contributors) > 1 and not models: + if len(contributors) == 1: + await broadcast_audit( + "ranking", + f"Only {contributors[0]} is eligible; rank is 1.0", + progress=90, + phase="finalizing", + ) + return {contributors[0]: Decimal("1")}, [] + if not (OPENROUTER_API_KEY or "").strip(): + raise RuntimeError( + "OPENROUTER_API_KEY is required when multiple contributors need ranking" + ) + + models = await fetch_top_models(n=3) + if not models: raise RuntimeError("no council models available for contributor ranking") + await broadcast_audit( + "council", + f"Council selected: {', '.join(models)}", + progress=35, + phase="ranking", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-council", f"Council: {', '.join(models)} — {len(commits)} commits"] ])) @@ -1035,6 +1149,11 @@ async def rank_commits(commits: list[dict]): async def compare_fn(i, j): a1, a2 = authors[i], authors[j] + await broadcast_audit( + "comparison", + f"Comparing {a1} with {a2}", + phase="ranking", + ) await broadcast_js(exec_event(Three[Selector("#emission-status")][MORPH][ ["div#emission-status", f"Comparing {a1} vs {a2}…"] ])) @@ -1050,6 +1169,11 @@ async def rank_commits(commits: list[dict]): if winner_weight <= 0 or loser_weight <= 0: raise ValueError("ratio weights must be positive") results.append((w, l, winner_weight, loser_weight)) + await broadcast_audit( + "vote", + f"{model}: {authors[w]} over {authors[l]} ({result['ratio']})", + phase="ranking", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-vote", ["span.model", model], " — ", @@ -1059,6 +1183,11 @@ async def rank_commits(commits: list[dict]): ] ])) except Exception as e: + await broadcast_audit( + "error", + f"{model} failed: {e}", + phase="error", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-error", f"⚠ {model}: {e}"] ])) @@ -1068,8 +1197,13 @@ async def rank_commits(commits: list[dict]): async def progress_fn(ev): if ev["phase"] == "spanning_tree": label = f"Spanning tree: {ev['step']}/{ev['total']}" + percent = 35 + round(35 * ev["step"] / max(ev["total"], 1)) else: label = f"Zip pass {ev['pass']}: {ev['step']}/{ev['total']}" + percent = 70 + round(20 * ev["step"] / max(ev["total"], 1)) + await broadcast_audit( + "progress", label, progress=percent, phase="ranking" + ) await broadcast_js(exec_event(Three[Selector("#emission-status")][MORPH][ ["div#emission-status", label] ])) @@ -1083,6 +1217,12 @@ async def rank_commits(commits: list[dict]): scores = rank_centrality(pairs) ranking = {authors[i]: Decimal(str(scores[i])) for i in range(len(authors))} ranking_rows = sorted(ranking.items(), key=lambda x: x[1], reverse=True) + await broadcast_audit( + "ranking", + "Ranking: " + ", ".join(f"{a} {s:.4f}" for a, s in ranking_rows), + progress=90, + phase="finalizing", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-ranking", ["b", "Ranking: "], @@ -1104,11 +1244,33 @@ def pool_remaining(events: list) -> Decimal: async def run_emission(epoch_n, boundary_ms): + PROCESS_STATE["running"] = True + await broadcast_audit( + "start", + f"Epoch {epoch_n} emission started", + progress=2, + phase="starting", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-start", f"⚡ Epoch {epoch_n} emission started"] ])) + await broadcast_audit( + "discovery", + "Fetching configured repositories and snapshotting refs", + progress=8, + phase="discovery", + ) discovery = await discover_repositories(epoch_n, boundary_ms) + await broadcast_audit( + "discovery", + ( + f"Discovered {len(discovery.observations)} new commits; " + f"{len(discovery.commits)} are eligible" + ), + progress=30, + phase="discovery", + ) ranking, models = await rank_commits(discovery.commits) def make_emission(events): @@ -1146,6 +1308,13 @@ async def run_emission(epoch_n, boundary_ms): entry = await store.atomic(make_emission) if entry: + PROCESS_STATE["running"] = False + await broadcast_audit( + "complete", + f"Epoch {entry.epoch} complete; emitted {entry.total_emitted} SLG", + progress=100, + phase="idle", + ) await broadcast_js(exec_event(Three[Selector("#emission-log")][PREPEND][ ["div.log-amount", f"Pool {entry.pool_before} → emit {entry.total_emitted} → {entry.pool_after}"] @@ -1189,13 +1358,24 @@ async def distribute_usdc(holdings, treasury_balance): async def epoch_loop(): while True: epoch_n, current_start, next_boundary = current_epoch() + processed = {e.epoch for e in store.read() if isinstance(e, Emission)} + if epoch_n >= 0 and epoch_n not in processed: + try: + await run_emission(epoch_n, current_start) + except Exception as exc: + PROCESS_STATE["running"] = False + await broadcast_audit( + "error", + f"Epoch {epoch_n} failed: {exc}; retrying in 60 seconds", + phase="error", + ) + print(f"epoch {epoch_n} emission failed: {exc}", flush=True) + await asyncio.sleep(60) + continue + now = int(time.time() * 1000) wait_ms = next_boundary - now - if wait_ms <= 0: - processed = {e.epoch for e in store.read() if isinstance(e, Emission)} - if epoch_n not in processed and epoch_n >= 0: - await run_emission(epoch_n, current_start) await asyncio.sleep(60) elif wait_ms < 86_400_000: await broadcast_js(exec_event(Three[Selector("#emission-status")][MORPH][ @@ -1243,6 +1423,36 @@ async def get_ranking(): return {"ranking": latest.ranking, "epoch": latest.epoch} +@app.get("/api/status") +async def get_status(): + events = store.read() + discoveries = [e for e in events if isinstance(e, GitDiscovery)] + emissions = [e for e in events if isinstance(e, Emission)] + return { + **PROCESS_STATE, + "epoch": current_epoch()[0], + "openrouter_configured": bool((OPENROUTER_API_KEY or "").strip()), + "sse_clients": len(SSE_CLIENTS), + "latest_discovery": ( + { + "epoch": discoveries[-1].epoch, + "snapshot_id": discoveries[-1].snapshot_id, + "observations": len(discoveries[-1].observations), + "eligible_commits": len(discoveries[-1].commits), + } + if discoveries else None + ), + "latest_emission": ( + { + "epoch": emissions[-1].epoch, + "total_emitted": emissions[-1].total_emitted, + "ranking": emissions[-1].ranking, + } + if emissions else None + ), + } + + @app.get("/api/contributor/{github_username}") async def get_contributor(github_username: str): history = [ @@ -1306,13 +1516,6 @@ async def test_emit(): # =========================================================================== # §9. SSE — live audit stream of the pairwise voting process -# -# TODO: the /sse emission audit page needs a real SSE-driven UI. votes arrive -# incrementally during rank_commits(), and the client should show a live -# progress bar and per-vote results as they stream in. this requires a -# dedicated page that connects to /sse and updates the DOM on each event -# (council, comparing, vote, ranking, emission_complete). defer until we -# have playwright tests to cover it — the incremental rendering is fiddly. # =========================================================================== @app.get("/sse") @@ -1322,6 +1525,13 @@ async def sse_stream(request: Request): async def generate(): try: + yield _sse_event("audit", { + "id": AUDIT_SEQUENCE, + "timestamp_ms": int(time.time() * 1000), + "kind": "connection", + "message": f"Connected to epoch {current_epoch()[0]}", + **PROCESS_STATE, + }) yield exec_event(Three[Selector("#emission-status")][MORPH][ ["div#emission-status", f"Connected — epoch {current_epoch()[0]}"] ]) @@ -1334,10 +1544,15 @@ async def sse_stream(request: Request): except asyncio.TimeoutError: yield ": keepalive\n\n" finally: - SSE_CLIENTS.remove(queue) + if queue in SSE_CLIENTS: + SSE_CLIENTS.remove(queue) from starlette.responses import StreamingResponse - return StreamingResponse(generate(), media_type="text/event-stream") + return StreamingResponse( + generate(), + media_type="text/event-stream", + headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"}, + ) # =========================================================================== @@ -1359,6 +1574,7 @@ def _page(title: str, body: list) -> HTMLResponse: ["meta", {"charset": "utf-8"}], ["meta", {"name": "viewport", "content": "width=device-width, initial-scale=1"}], ["title", title], + ["style", RawContent(_WATCH_CSS)], ], ["body", body, @@ -1367,6 +1583,387 @@ def _page(title: str, body: list) -> HTMLResponse: ])) +_WATCH_CSS = """ +/* ================================================================ + ZIGGURAT — bevel-first dark theme + --spread (0→1) controls bevel depth. 0 = flat. 1 = full relief. + Light source: top-left. Shadow: bottom-right. + Platforms nest. Each level is raised. Nothing is rounded. + ================================================================ */ + +:root { + color-scheme: dark; + --spread: 1; + + --g0: #080808; + --g1: #131313; + --g2: #1c1c1c; + --g3: #252525; + --g4: #2e2e2e; + --g5: #383838; + + --hi: #5e5e5e; + --lo: #050505; + --bv: calc(var(--spread) * 4px + 1px); + --bv-lg: calc(var(--spread) * 6px + 2px); + + --signal: #f0f0f0; + --prose: #c2c2c2; + --ui: #888; + --meta: #4a4a4a; + --link: #8899ee; + --code-fg: #c8dda0; + + --font-prose: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif; + --font-ui: system-ui, -apple-system, sans-serif; + --font-code: ui-monospace, "Cascadia Code", "SF Mono", Menlo, monospace; +} + +*, *::before, *::after { box-sizing: border-box; } +html, body { margin: 0; padding: 0; } + +body { + background: var(--g0); + color: var(--prose); + font-family: var(--font-ui); + font-size: 14px; + line-height: 1.6; + margin: 0 auto; + max-width: 560px; + min-height: 100vh; + padding: 0 16px 48px; +} +main { width: 100%; padding: 18px 0 48px; } + +h1, h2, h3 { + color: var(--signal); + font-size: 11px; + font-weight: bold; + letter-spacing: 0.12em; + margin: 14px 0 6px; + text-transform: uppercase; +} +a { color: var(--link); text-decoration: none; } +a:hover { color: var(--signal); } +.eyebrow { + background: var(--g2); + border: var(--bv) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + color: var(--ui); + font-size: 11px; + letter-spacing: 0.12em; + padding: 4px 10px; + text-transform: uppercase; + width: fit-content; +} + +/* Every dashboard section is a raised platform. */ +.panel { + background: var(--g2); + border: var(--bv-lg) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + margin: 8px 0; + padding: 10px; + width: 100%; +} +.status-row { + align-items: center; + display: flex; + flex-wrap: wrap; + gap: 8px; + justify-content: space-between; +} +#process-status { color: var(--signal); font-family: var(--font-code); font-weight: bold; } +.badge { + align-items: center; + background: var(--g3); + border: var(--bv) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + color: var(--ui); + display: inline-flex; + font-size: 11px; + gap: 7px; + padding: 3px 8px; +} +.dot { background: var(--meta); height: 8px; width: 8px; } +.live .dot { background: #7acc7a; } +.warn .dot { background: #cc9955; } + +/* The progress track is inset; its signal is raised inside it. */ +.progress-shell { + background: var(--g1); + border: var(--bv-lg) solid; + border-color: var(--lo) var(--hi) var(--hi) var(--lo); + height: 58px; + margin: 14px 0 10px; + overflow: hidden; + position: relative; +} +#progress-fill { + background: var(--link); + border: var(--bv) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + height: 100%; + transition: width .35s steps(8, end); + width: 0; +} +#progress-label { + color: var(--signal); + display: grid; + font-family: var(--font-code); + font-size: 18px; + font-weight: bold; + inset: 0; + place-items: center; + position: absolute; + text-shadow: 1px 1px var(--lo); +} + +.controls { align-items: center; display: flex; flex-wrap: wrap; gap: 8px; } +button { + background: var(--g5); + border: var(--bv) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + color: var(--signal); + cursor: pointer; + font: inherit; + font-size: 12px; + padding: 4px 10px; +} +button:hover { background: #404040; } +button:active { + background: var(--g4); + border-color: var(--lo) var(--hi) var(--hi) var(--lo); + transform: translate(1px, 1px); +} +button:disabled { cursor: default; opacity: .4; } +.note { color: var(--meta); font-size: 11px; margin: 4px 0; } + +.feed-head { align-items: baseline; display: flex; justify-content: space-between; } +#audit-feed { + background: var(--g1); + border: var(--bv) solid; + border-color: var(--lo) var(--hi) var(--hi) var(--lo); + display: flex; + flex-direction: column; + gap: 5px; + margin-top: 8px; + padding: 6px; +} +.event { + background: var(--g3); + border: var(--bv) solid; + border-color: var(--hi) var(--lo) var(--lo) var(--hi); + display: grid; + gap: 6px; + grid-template-columns: 82px 88px 1fr; + padding: 5px 8px; +} +.event[data-kind="error"] { border-left-color: #cc5555; } +.event[data-kind="complete"], .event[data-kind="ranking"] { border-left-color: #7acc7a; } +.event[data-kind="vote"] { border-left-color: var(--link); } +.event time, .event-kind { color: var(--meta); font-family: var(--font-code); font-size: 10px; } +.event-kind { text-transform: uppercase; } +.event-message { color: var(--prose); font-family: var(--font-prose); } + +code { + background: var(--g1); + border: 2px solid; + border-color: var(--lo) var(--hi) var(--hi) var(--lo); + color: var(--code-fg); + font-family: var(--font-code); + font-size: 12px; + padding: 1px 4px; +} + +@media (max-width: 520px) { + .event { grid-template-columns: 72px 1fr; } + .event-message { grid-column: 1 / -1; } +} +""" + + +def _watch_initial_state() -> dict: + events = store.read() + feed = [] + for event_ in events[-40:]: + if isinstance(event_, GitDiscovery): + feed.append({ + "id": f"discovery-{event_.snapshot_id}", + "timestamp_ms": event_.timestamp_ms, + "kind": "discovery", + "message": ( + f"Epoch {event_.epoch}: observed {len(event_.observations)} commits; " + f"{len(event_.commits)} eligible" + ), + }) + elif isinstance(event_, Emission): + feed.append({ + "id": f"emission-{event_.epoch}", + "timestamp_ms": event_.timestamp_ms, + "kind": "complete", + "message": ( + f"Epoch {event_.epoch}: emitted {event_.total_emitted} SLG; " + f"ranking {event_.ranking}" + ), + }) + feed.extend(AUDIT_HISTORY) + return { + "process": dict(PROCESS_STATE), + "openrouter_configured": bool((OPENROUTER_API_KEY or "").strip()), + "epoch": current_epoch()[0], + "feed": feed[-200:], + } + + +_WATCH_JS = """ +const initial = __INITIAL__; +const feed = document.querySelector('#audit-feed'); +const processStatus = document.querySelector('#process-status'); +const connection = document.querySelector('#connection-status'); +const fill = document.querySelector('#progress-fill'); +const progressLabel = document.querySelector('#progress-label'); +const play = document.querySelector('#play'); +const pause = document.querySelector('#pause'); +const seen = new Set(); +let source = null; + +function setProgress(value) { + const n = Math.max(0, Math.min(100, Number(value ?? 0))); + fill.style.width = `${n}%`; + progressLabel.textContent = `${Math.round(n)}%`; + document.querySelector('.progress-shell').setAttribute('aria-valuenow', String(n)); +} + +function addEvent(event) { + const id = String(event.id); + if (seen.has(id)) return; + seen.add(id); + const row = document.createElement('div'); + row.className = 'event'; + row.dataset.kind = event.kind || 'event'; + const when = document.createElement('time'); + when.dateTime = new Date(event.timestamp_ms).toISOString(); + when.textContent = new Date(event.timestamp_ms).toLocaleTimeString(); + const kind = document.createElement('span'); + kind.className = 'event-kind'; + kind.textContent = event.kind || 'event'; + const message = document.createElement('span'); + message.className = 'event-message'; + message.textContent = event.message; + row.append(when, kind, message); + feed.prepend(row); + while (feed.children.length > 200) feed.lastElementChild.remove(); +} + +function applyState(event) { + processStatus.textContent = event.message || 'Waiting for the next epoch'; + setProgress(event.progress); + if (event.kind !== 'connection') addEvent(event); +} + +function connect() { + if (source) return; + source = new EventSource('/sse'); + connection.classList.remove('warn'); + connection.classList.add('live'); + connection.querySelector('span:last-child').textContent = 'connecting'; + play.disabled = true; + pause.disabled = false; + source.onopen = () => { + connection.querySelector('span:last-child').textContent = 'live'; + }; + source.addEventListener('audit', event => applyState(JSON.parse(event.data))); + source.onerror = () => { + connection.classList.remove('live'); + connection.classList.add('warn'); + connection.querySelector('span:last-child').textContent = 'reconnecting'; + }; +} + +function disconnect() { + if (source) source.close(); + source = null; + connection.classList.remove('live'); + connection.classList.add('warn'); + connection.querySelector('span:last-child').textContent = 'paused locally'; + play.disabled = false; + pause.disabled = true; +} + +play.addEventListener('click', connect); +pause.addEventListener('click', disconnect); +initial.feed.forEach(addEvent); +processStatus.textContent = initial.process.message; +setProgress(initial.process.progress); +connect(); +""" + + +@app.get("/watch") +async def watch(): + initial = json.dumps( + _watch_initial_state(), separators=(",", ":") + ).replace(" 0") - ;; 9. SSE connects and sends initial event + ;; 9. watch UI exposes progress, controls, readiness, and live SSE + (println "\nchecking /watch UI…") + (bind watch-html (slurp (str base-url "/watch"))) + (assert! (str/includes? watch-html "role=\"progressbar\"") + "watch page has progress bar") + (assert! (str/includes? watch-html "id=\"play\"") + "watch page has play control") + (assert! (str/includes? watch-html "id=\"pause\"") + "watch page has pause control") + (assert! (str/includes? watch-html "OpenRouter configured") + "watch page reports council readiness") + (bind status-resp (get-json base-url "/api/status")) + (assert! (true? (:openrouter_configured status-resp)) + "status API reports OpenRouter configuration") + + ;; 10. SSE connects and sends initial event (println "\nchecking /sse initial event…") (bind sse-events (read-sse-events (str base-url "/sse") 1 5000)) (assert! (= 1 (count sse-events)) "received 1 SSE event") (assert! (not (str/blank? (first sse-events))) "initial SSE event contains executable audit data") - ;; 10. POST /test/emit — full ranking pipeline hits mocks + ;; 11. POST /test/emit — full ranking pipeline hits mocks (println "\ntriggering /test/emit (epoch 1)…") (bind emit-resp (post-json! base-url "/test/emit")) (assert! (= "emission" (:type emit-resp)) "emit response type is emission") @@ -503,7 +518,7 @@ (bind rank-after (get-json base-url "/api/ranking")) (assert! (= 1 (:epoch rank-after)) "latest ranking is epoch 1") - ;; 11. kill and restart — prove replay determinism + ;; 12. kill and restart — prove replay determinism (println "\nkilling server for replay test…") (.destroyForcibly (:proc server)) (deref server) diff --git a/tests/test_git_discovery.py b/tests/test_git_discovery.py index 0dd31bc42a19bc8c59842dc61f193c595c474659..5a9e167e16ad2ffb25988af85820f6f19e8910cd 100644 --- a/tests/test_git_discovery.py +++ b/tests/test_git_discovery.py @@ -393,7 +393,9 @@ def test_empty_epoch_records_zero_emission_without_burning_pool( monkeypatch.setattr(c, "store", c.JsonlStore(discovery_config / "ledger.jsonl")) async def discover(_epoch, _boundary): - return SimpleNamespace(commits=[], snapshot_id="empty-snapshot") + return SimpleNamespace( + observations=[], commits=[], snapshot_id="empty-snapshot" + ) async def rank(_commits): return {}, [] @@ -412,7 +414,11 @@ def test_emission_distribution_sums_exactly_to_total( monkeypatch.setattr(c, "store", c.JsonlStore(discovery_config / "ledger.jsonl")) async def discover(_epoch, _boundary): - return SimpleNamespace(commits=[{"x": 1}], snapshot_id="ranked-snapshot") + return SimpleNamespace( + observations=[{"x": 1}], + commits=[{"x": 1}], + snapshot_id="ranked-snapshot", + ) async def rank(_commits): return { @@ -454,6 +460,7 @@ def test_any_council_failure_aborts_ranking(monkeypatch): monkeypatch.setattr(c, "fetch_top_models", models) monkeypatch.setattr(c, "llm_pairwise_compare", compare) + monkeypatch.setattr(c, "OPENROUTER_API_KEY", "test-key") commits = [ { "contributor": contributor, @@ -465,3 +472,54 @@ def test_any_council_failure_aborts_ranking(monkeypatch): ] with pytest.raises(RuntimeError, match="council model failed"): asyncio.run(c.rank_commits(commits)) + + +def test_contested_ranking_requires_openrouter_key(monkeypatch): + monkeypatch.setattr(c, "OPENROUTER_API_KEY", "") + commits = [ + { + "contributor": contributor, + "oid": "sha1:" + char * 40, + "message": contributor, + "patch": "patch", + } + for contributor, char in [("alice", "a"), ("bob", "b")] + ] + with pytest.raises(RuntimeError, match="OPENROUTER_API_KEY"): + asyncio.run(c.rank_commits(commits)) + + +def test_watch_page_has_live_controls_progress_and_key_warning( + discovery_config, monkeypatch +): + monkeypatch.setattr(c, "store", c.JsonlStore(discovery_config / "ledger.jsonl")) + monkeypatch.setattr(c, "OPENROUTER_API_KEY", "") + monkeypatch.setattr(c, "current_epoch", lambda: (3, 0, 1)) + response = asyncio.run(c.watch()) + html = response.body.decode() + assert 'role="progressbar"' in html + assert 'id="play"' in html + assert 'id="pause"' in html + assert "new EventSource('/sse')" in html + assert "OpenRouter key missing" in html + + +def test_audit_events_are_json_sse_and_update_process_state(monkeypatch): + clients = [] + history = [] + monkeypatch.setattr(c, "SSE_CLIENTS", clients) + monkeypatch.setattr(c, "AUDIT_HISTORY", history) + queue = asyncio.Queue() + clients.append(queue) + + async def emit(): + event = await c.broadcast_audit( + "progress", "halfway", progress=50, phase="ranking" + ) + return event, await queue.get() + + event, wire = asyncio.run(emit()) + assert event["progress"] == 50 + assert event["phase"] == "ranking" + assert wire.startswith("event: audit\ndata: {") + assert '"message":"halfway"' in wire Side B — contributor: tommy-mor Side B — 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 B — 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)