constitution · epochs · watch · epoch 3

comparison

c_5cd3e5917d2f (tommy-mor) vs c_45659f04aa7c (tommy-mor)

download prompt · raw event · cmp_3f16b625881aec

council reasoning

~anthropic/claude-sonnet-latest · winner B · 6:4 · permalink

B introduces a substantial, well-tested subsystem (deterministic multi-repository git discovery with patch-identity dedup, replay-safety, adversarial hypothesis tests, and integration tests using real local git repos) replacing a brittle GitHub-API-dependent ranking flow, delivering real lasting architectural value despite lockfile noise. A is a solid, focused refactor (removing EntityStore, adding TTL eviction for ephemeral Reddit content) that is correct and useful but narrower in scope and impact than B's rewrite of the core attribution/discovery pipeline.

~x-ai/grok-latest · winner B · 1:4 · permalink

B replaces brittle GitHub API polling with multi-repo git mirroring, first-reachability snapshots, patch-id cherry-pick dedup, and adversarial/replay guards (force-push, replace refs, locks), plus deep unit/stateful/integration coverage—core lasting attribution infrastructure. A is a sound but narrower redesign: drop EntityImported/EntityStore, cache Reddit display data ephemerally with TTL eviction, and log only NodeEnsured structure.

openai/gpt-chat-latest · winner B · 3:2 · permalink

Side B replaces GitHub API-based commit discovery with deterministic multi-repository Git snapshotting, including mirror verification, reachability tracking, patch-identity deduplication, concurrency-safe discovery, replayable ledger snapshots, and extensive real-Git/stateful tests. Side A makes a meaningful architectural change by removing persistent Reddit payloads from the event log and introducing ephemeral projection storage with TTL eviction, but B establishes a broader and more durable correctness foundation for contribution attribution across repositories.

sides

A — c_5cd3e5917d2f (tommy-mor)

message

[1d14ff09] Ephemeral Reddit content; log structure only (#46)

* Keep Reddit content ephemeral; log structure only

Remove EntityImported and EntityStore. Reddit fetches write display
content directly to the projection with a fetched_at timestamp, while
the event log records NodeEnsured for discovered identities only.

A background task evicts cached display content after 48 hours. Votes,
tree structure, and ItemIds remain in the log and projection.

Co-authored-by: tommy <thmorriss@gmail.com>

* Fix reddit import test assertions and Clojure syntax

Co-authored-by: tommy <thmorriss@gmail.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>

diff preview

diff --git a/server/src/bin/storage_bench.rs b/server/src/bin/storage_bench.rs
index 4762d0d23bc8e2df439afe09a3235ba7f72bd486..3d988416ad36b27a7d3dc84280cfdbdcafa43e69 100644
--- a/server/src/bin/storage_bench.rs
+++ b/server/src/bin/storage_bench.rs
@@ -6,8 +6,8 @@ use std::{
 };
 
 use sorter2_server::{
-    entity_store::EntityStore, event_log::EventLog, events::Event, journal::JournalClient,
-    projection_apply, projection_store::ProjectionStore,
+    event_log::EventLog, events::Event, journal::JournalClient, projection_apply,
+    projection_store::ProjectionStore,
 };
 
 #[tokio::main]
@@ -18,12 +18,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
     let data_dir = opts.data_dir.to_string_lossy().into_owned();
     let event_log = Arc::new(EventLog::new(format!("{data_dir}/events.jsonl")));
     let db = durable::Db::open(opts.data_dir.join("store"))?;
-    let entity_store = EntityStore::from_db(&db)?;
     let projection_store = ProjectionStore::from_db(&db)?;
 
     let journal = JournalClient::spawn(
         event_log.clone(),
-        entity_store.clone(),
         projection_store.clone(),
         event_log.last_sequence().await? + 1,
     );
@@ -46,12 +44,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
     drop(journal);
 
     let rebuild_start = Instant::now();
-    entity_store.reset()?;
     projection_store.reset()?;
     let rebuild = event_log
-        .replay(|record| {
-            projection_apply::apply_records(&projection_store, &entity_store, &[record])
-        })
+        .replay(|record| projection_apply::apply_records(&projection_store, &[record]))
         .await?;
     let rebuild_elapsed = rebuild_start.elapsed();
 
diff --git a/server/src/entity_store.rs b/server/src/entity_store.rs
deleted file mode 100644
index d29f39aecca6f12cdcf263cf77c3654eb4ee6cfa..0000000000000000000000000000000000000000
--- a/server/src/entity_store.rs
+++ /dev/null
@@ -1,134 +0,0 @@
-//! Off-heap storage for full entity payloads (Reddit API JSON).
-//!
-//! Derived [`crate::reducer::EntityData`] is stored on the node; the raw JSON
-//! lives here, in the shared durable [`Store`] schema.
-
-use std::path::Path;
-
-use durable::{Batch, Db, Durability};
-use serde_json::Value;
-
-use crate::{
-    path_types::ItemId,
-    storage_dto::{decode_entity_payload, encode_entity_payload},
-    storage_schema::{Store, StoreFields},
-};
-
-const ENTITY_SCHEMA_KEY: &str = "schema_version";
-const ENTITY_SCHEMA_VERSION: u64 = 2;
-
-#[derive(Debug, thiserror::Error)]
-pub enum EntityStoreError {
-    #[error("durable error: {0}")]
-    Durable(#[from] durable::Error),
-    #[error("json error: {0}")]
-    Json(#[from] serde_json::Error),
-    #[error("storage decode error: {0}")]
-    Storage(String),
-    #[error("io error: {0}")]
-    Io(#[from] std::io::Error),
-}
-
-/// Disk-backed map of entity id → raw JSON payload.
-#[derive(Clone)]
-pub struct EntityStore {
-    db: Db,
-}
-
-impl EntityStore {
-    /// Open (or create) the entity database under `dir`.
-    pub fn open(dir: &Path) -> Result<Self, EntityStoreError> {
-        std::fs::create_dir_all(dir)?;
-        let db = Db::open(dir)?;
-        Self::from_db(&db)
-    }
-
-    /// Create an entity store backed by an already-open database.
-    pub fn from_db(db: &Db) -> Result<Self, EntityStoreError> {
-        let store = Self { db: db.clone() };
-        let version = Store::root()
-            .entity_meta()
-            .key(&ENTITY_SCHEMA_KEY.to_string())
-            .get(db)?;
-        if version != Some(ENTITY_SCHEMA_VERSION) {
-            store.reset()?;
-        }
-        Ok(store)
-    }
-
-    /// Clear rebuildable entity payloads and reset storage schema metadata.
-    pub fn reset(&self) -> Result<(), EntityStoreError> {
-        let root = Store::root();
-        self.db.apply(
-            &[root.entities().clear(), root.entity_meta().clear()],
-            Durability::SyncWal,
-        )?;
-        self.db.run(
-            root.entity_meta()
-                .key(&ENTITY_SCHEMA_KEY.to_string())
-                .set(&ENTITY_SCHEMA_VERSION),
-            Durability::SyncWal,
-        )?;
-        Ok(())
-    }
-
-    /// Persist a payload for `id` (overwrites any existing entry).
-    pub fn put(&self, id: &ItemId, payload: &Value) -> Result<(), EntityStoreError> {
-        self.db.run(
-            Store::root()
-                .entities()
-                .key(&id.as_str().to_string())
-                .set(&encode_entity_payload(payload)),
-            Durability::SyncWal,
-        )?;
-        Ok(())
-    }
-
-    /// Add a payload write to the caller's batch.
-    pub fn put_in_batch(
-        &self,
-        batch: &mut Batch,
-        id: &ItemId,
-        payload: &Value,
-    ) -> Result<(), EntityStoreError> {
-        batch.write(
-            Store::root()
-                .entities()
-                .key(&id.as_str().to_string())
-                .set(&encode_entity_payload(payload)),
-        );
-        Ok(())
-    }
-
-    /// Load a stored payload, if present.
-    pub fn get(&self, id: &ItemId) -> Result<Option<Value>, EntityStoreError> {
-        match Store::root()
-            .entities()
-            .key(&id.as_str().to_string())
-            .get(&self.db)?
-        {
-            Some(record) => decode_entity_payload(record)
-                .map(Some)
-                .map_err(EntityStoreError::Storage),
-            None => Ok(None),
-        }
-    }
-}
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-    use serde_json::json;
-
-    #[test]
-    fn round_trip_payload() {
-        let tmp = tempfile::tempdir().unwrap();
-        let store = EntityStore::open(tmp.path()).unwrap();
-        let id = ItemId::from_url("https://reddit.com/r/rust").unwrap();
-        let payload = json!({"kind": "t5", "data": {"display_name": "rust"}});
-
-        store.put(&id, &payload).unwrap();
-        let loaded = store.get(&id).unwrap().unwrap();
-        assert_eq!(loaded, payload);
-    }
-}
diff --git a/server/src/events.rs b/server/src/events.rs
index a3d88e285c645a96d33e6f0aed1b487b843aceb8..d76c3bb4277216b0d39c9422ba7a50db10a95e05 100644
--- a/server/src/events.rs
+++ b/server/src/events.rs
@@ -1,5 +1,4 @@
 use serde::{Deserialize, Serialize};
-use serde_json::Value;
 
 /// Schema version for JSONL log records. Bump when event semantics change.
 pub const CURRENT_LOG_SCHEMA: u32 = 1;
@@ -30,7 +29,7 @@ pub type ViewRecord = LogRecord<ViewEvent>;
 /// Wall-clock timestamp carried on the log envelope for domain events.
 pub fn event_timestamp(event: &Event) -> i64 {
     match event {
-        Event::VoteRecorded { ts, .. } | Event::EntityImported { ts, .. } => *ts,
+        Event::VoteRecorded { ts, .. } => *ts,
         Event::NodeEnsured { .. } => crate::fetch::now_ms(),
     }
 }
@@ -57,6 +56,4 @@ pub enum Event {
     },
     /// Register a node path in the fractal tree (no external fetch).
     NodeEnsured { id: String },
-    /// Full upstream API payload for a node (domain-specific view derived at replay/render time).
-    EntityImported { id: String, ts: i64, payload: Value },
 }
diff --git a/server/src/journal.rs b/server/src/journal.rs
index 50bc89f976edb82b7b0e49e954a8eccbbe82bf87..d50023aca7c3e74068de988449b11baee32d1ecf 100644
--- a/server/src/journal.rs
+++ b/server/src/journal.rs
@@ -5,7 +5,6 @@ use std::sync::Arc;
 use tokio::sync::{mpsc, oneshot};
 
 use crate::{
-    entity_store::EntityStore,
     event_log::EventLog,
     events::{event_timestamp, Event, EventRecord},
     projection_apply,
@@ -25,7 +24,6 @@ pub struct JournalClient {
 impl JournalClient {
     pub fn spawn(
         event_log: Arc<EventLog>,
-        entity_store: EntityStore,
         projection_store: ProjectionStore,
         next_seq: u64,
     ) -> Self {
@@ -33,7 +31,6 @@ impl JournalClient {
         tokio::spawn(journal_worker(
             rx,
             event_log,
-            entity_store,
             projection_store,
             next_seq,
         ));
@@ -62,7 +59,6 @@ impl JournalClient {
 async fn journal_worker(
     mut rx: mpsc::Receiver<JournalCommand>,
     event_log: Arc<EventLog>,
-    entity_store: EntityStore,
     projection_store: ProjectionStore,
     mut next_seq: u64,
 ) {
@@ -75,7 +71,6 @@ async fn journal_worker(
         let result = append_and_project_batch(
             &event_log,
             &projection_store,
-            &entity_store,
             &mut next_seq,
             &batch,
         )
@@ -99,7 +94,6 @@ async fn journal_worker(
 async fn append_and_project_batch(
     event_log: &EventLog,
     projection_store: &ProjectionStore,
-    entity_store: &EntityStore,
     next_seq: &mut u64,
     commands: &[JournalCommand],
 ) -> Result<(), String> {
@@ -117,7 +111,7 @@ async fn append_and_project_batch(
         .await
         .map_err(|e| e.to_string())?;
     *next_seq = seq;
-    projection_apply::apply_records(projection_store, entity_store, &records)
+    projection_apply::apply_records(projection_store, &records)
         .map_err(|e| format!("projection apply failed after durable append: {e}"))
 }
 
@@ -132,10 +126,9 @@ mod tests {
         let log_path = tmp.path().join("events.jsonl");
         let event_log = Arc::new(EventLog::new(log_path));
         let db = durable::Db::open(tmp.path().join("store")).unwrap();
-        let entity_store = EntityStore::from_db(&db).unwrap();
         let projection_store = ProjectionStore::from_db(&db).unwrap();
 
-        let journal = JournalClient::spawn(event_log, entity_store, projection_store.clone(), 1);
+        let journal = JournalClient::spawn(event_log, projection_store.clone(), 1);
 
         let j1 = journal.clone();
         let j2 = journal.clone();
@@ -177,11 +170,9 @@ mod tests {
             .unwrap();
 
         let db = durable::Db::open(tmp.path().join("store")).unwrap();
-        let entity_store = EntityStore::from_db(&db).unwrap();
         let projection_store = ProjectionStore::from_db(&db).unwrap();
         projection_apply::apply_records(
             &projection_store,
-            &entity_store,
             &[EventRecord::new(
                 1,
                 1,
@@ -196,7 +187,6 @@ mod tests {
 
         let journal = JournalClient::spawn(
             event_log.clone(),
-            entity_store,
             projection_store.clone(),
             next_seq,
         );
@@ -219,10 +209,9 @@ mod tests {
         let log_path = tmp.path().join("events.jsonl");
         let event_log = Arc::new(EventLog::new(log_path));
         let db = durable::Db::open(tmp.path().join("store")).unwrap();
-        let entity_store = EntityStore::from_db(&db).unwrap();
         let projection_store = ProjectionStore::from_db(&db).unwrap();
         let journal =
-            JournalClient::spawn(event_log.clone(), entity_store, projection_store.clone(), 1);
+            JournalClient::spawn(event_log.clone(), projection_store.clone(), 1);
 
         journal
             .append_many(vec![
diff --git a/server/src/lib.rs b/server/src/lib.rs
index 5c02c8e704e4664453bad75d819df8a067668176..3dfc7c8acb8ed61bb73ade63e72768e402042cc5 100644
--- a/server/src/lib.rs
+++ b/server/src/lib.rs
@@ -1,5 +1,4 @@
 pub mod api;
-pub mod entity_store;
 pub mod event_log;
 pub mod events;
 pub mod fetch;
diff --git a/server/src/projection_apply.rs b/server/src/projection_apply.rs
index 5644557a41b3e9497c7421b444155ae629fa79f1..9c8990a8af927f35d3344c8d0872a516aba56b86 100644
--- a/server/src/projection_apply.rs
+++ b/server/src/projection_apply.rs
@@ -1,22 +1,20 @@
 //! Apply event-log records to the durable projection as precise point updates.
 //!
 //! Each batch of records lowers to reified durable writes (edge merges, child
-//! links, voted-pair flags, recent-vote pushes, entity payloads) plus a cursor
-//! advance, all committed in one atomic `DisableWal` batch. The cursor moving in
-//! the same batch as the (non-idempotent) edge merges guarantees e

… preview truncated; 33,427 characters omitted

download full diff A

B — c_45659f04aa7c (tommy-mor)

message

[1efb7222] multi-repository git discovery

Make contribution discovery deterministic across repository and branch DAGs, with replayable attribution and adversarial coverage.

Co-authored-by: Cursor <cursoragent@cursor.com>

diff preview

diff --git a/constitution.py b/constitution.py
index b535faad658c553b3b9046e2401333618e212150..4bee9f83663ab7fb36db95129b92b64b4ef57258 100644
--- a/constitution.py
+++ b/constitution.py
@@ -6,7 +6,7 @@
 #   "uvicorn",
 #   "httpx",
 #   "tenacity",
-#   "evaleval>=0.2.6",
+#   "evaleval==0.2.7",
 #   "authlib",
 #   "itsdangerous",
 #   "starlette",
@@ -29,7 +29,7 @@ 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
+import json, time, os, asyncio, httpx, pathlib, subprocess, hashlib, re, fcntl
 import sympy as sp  # type: ignore[reportMissingImports]
 from tenacity import retry, retry_if_exception, stop_after_attempt, wait_exponential
 from evaleval import (
@@ -118,11 +118,35 @@ JSONL_PATH = pathlib.Path(os.environ.get("JSONL_PATH", "/data/ledger.jsonl"))
 
 GITHUB_CLIENT_ID = os.environ.get("GITHUB_CLIENT_ID", "")
 GITHUB_CLIENT_SECRET = os.environ.get("GITHUB_CLIENT_SECRET", "")
-REPO = os.environ.get("REPO", "tommy-mor/slug")
 
 OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY", "")
 OPENROUTER_BASE_URL = os.environ.get("OPENROUTER_BASE_URL", "https://openrouter.ai").rstrip("/")
-GITHUB_API_BASE_URL = os.environ.get("GITHUB_API_BASE_URL", "https://api.github.com").rstrip("/")
+
+# Repositories, branches, and contributor identities are constitutional inputs.
+# A ref pattern matches a complete Git ref: * stays within one path component,
+# while ** crosses slashes, so refs/heads/** includes branches on branches.
+# Environment overrides exist for deterministic integration tests and deployments
+# using the exact same source; their normalized values are committed to every
+# discovery event.
+DEFAULT_REPOSITORIES = [
+    {
+        "id": "slug",
+        "url": "https://github.com/tommy-mor/slug.git",
+        "refs": ["refs/heads/**"],
+    },
+]
+DEFAULT_CONTRIBUTORS = {
+    "tommy-mor": ["thmorriss@gmail.com"],
+}
+
+REPOSITORIES = json.loads(
+    os.environ.get("REPOSITORIES_JSON", json.dumps(DEFAULT_REPOSITORIES))
+)
+CONTRIBUTORS = json.loads(
+    os.environ.get("CONTRIBUTORS_JSON", json.dumps(DEFAULT_CONTRIBUTORS))
+)
+GIT_MIRROR_DIR = pathlib.Path(os.environ.get("GIT_MIRROR_DIR", "/data/git"))
+GIT_TIMEOUT_SECONDS = int(os.environ.get("GIT_TIMEOUT_SECONDS", "120"))
 
 # 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("/")
@@ -146,6 +170,7 @@ class Emission:
     distributions: dict   # author -> amount str
     ranking: dict         # author -> score str
     models_used: list
+    discovery_snapshot_id: str = ""  # empty only for pre-discovery ledger history
 
 
 @event
@@ -163,6 +188,20 @@ class Redemption:
     amount: str
 
 
+@event
+class GitDiscovery:
+    schema_version: int
+    epoch: int
+    snapshot_id: str
+    timestamp_ms: int
+    config_digest: str
+    initial_snapshot: bool
+    configuration: dict
+    repositories: list
+    observations: list
+    commits: list
+
+
 store = JsonlStore(JSONL_PATH)
 
 
@@ -532,44 +571,426 @@ Side B — unified diffs (full patches):
         return json.loads(content)
 
 
-def _github_headers():
-    h = {"Accept": "application/vnd.github+json"}
-    tok = os.environ.get("GITHUB_TOKEN", "")
-    if tok:
-        h["Authorization"] = f"Bearer {tok}"
-    return h
+# ===========================================================================
+# §4b. GIT DISCOVERY — immutable reachability snapshots across repositories
+# ===========================================================================
+#
+# Git timestamps cannot prove when a branch first reached a commit. The first
+# snapshot therefore bootstraps history by committer time at GENESIS_MS. Every
+# later snapshot uses the stronger rule: a commit enters exactly once, when it
+# first becomes reachable from the union of configured refs.
+#
+# OIDs are deduplicated globally, then equivalent cherry-picks are deduplicated
+# by Git's stable patch identity. Merges and empty commits are graph structure,
+# not separately priced contributions. Discovery is all-or-nothing: if any
+# repository cannot be mirrored and verified, no snapshot is appended.
+
+GIT_DISCOVERY_SCHEMA_VERSION = 1
+PATCH_IDENTITY_VERSION = "git-patch-id-stable-v1"
+_DISCOVERY_LOCK = asyncio.Lock()
+
+
+def _normalized_discovery_config() -> dict:
+    repositories = []
+    seen_ids = set()
+    for raw in REPOSITORIES:
+        repo_id = str(raw.get("id", ""))
+        url = str(raw.get("url", ""))
+        refs = sorted(set(str(x) for x in raw.get("refs", [])))
+        if not re.fullmatch(r"[A-Za-z0-9._-]+", repo_id):
+            raise ValueError(f"invalid repository id: {repo_id!r}")
+        if repo_id in seen_ids:
+            raise ValueError(f"duplicate repository id: {repo_id}")
+        if not url or not refs or any(not r.startswith("refs/") for r in refs):
+            raise ValueError(f"repository {repo_id} requires a URL and full ref patterns")
+        seen_ids.add(repo_id)
+        repositories.append({"id": repo_id, "url": url, "refs": refs})
+
+    email_to_contributor = {}
+    contributors = {}
+    for contributor, emails in sorted(CONTRIBUTORS.items()):
+        contributor = str(contributor)
+        normalized = sorted(set(str(e).strip().lower() for e in emails))
+        if not contributor or not normalized:
+            raise ValueError("contributors require an id and at least one email")
+        for email in normalized:
+            if email in email_to_contributor:
+                raise ValueError(f"email belongs to multiple contributors: {email}")
+            email_to_contributor[email] = contributor
+        contributors[contributor] = normalized
+
+    repositories.sort(key=lambda r: r["id"])
+    return {"repositories": repositories, "contributors": contributors}
+
+
+def _config_digest(config: dict) -> str:
+    encoded = json.dumps(config, sort_keys=True, separators=(",", ":")).encode()
+    return hashlib.sha256(encoded).hexdigest()
+
+
+def _ref_pattern_regex(pattern: str) -> re.Pattern:
+    out = ""
+    i = 0
+    while i < len(pattern):
+        if pattern[i:i + 2] == "**":
+            out += ".*"
+            i += 2
+        elif pattern[i] == "*":
+            out += "[^/]*"
+            i += 1
+        elif pattern[i] == "?":
+            out += "[^/]"
+            i += 1
+        else:
+            out += re.escape(pattern[i])
+            i += 1
+    return re.compile(f"^{out}$")
+
+
+def _git(repo: pathlib.Path | None, *args: str, input_bytes: bytes | None = None) -> bytes:
+    command = [
+        "git",
+        "--no-replace-objects",
+        "-c", "core.quotepath=true",
+        "-c", "core.attributesFile=/dev/null",
+        "-c", "diff.external=",
+        "-c", "diff.renames=false",
+        "-c", "diff.algorithm=myers",
+        "-c", "diff.context=3",
+    ]
+    if repo is not None:
+        command += ["-C", str(repo)]
+    command += list(args)
+    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",
+            },
+            timeout=GIT_TIMEOUT_SECONDS,
+            check=False,
+        )
+    except subprocess.TimeoutExpired as exc:
+        raise RuntimeError(f"git command timed out: {args[0]}") from exc
+    if result.returncode:
+        error = result.stderr.decode("utf-8", "replace").strip()
+        raise RuntimeError(f"git {args[0]} failed: {error}")
+    return result.stdout
+
+
+def _ensure_mirror(repo: dict) -> pathlib.Path:
+    GIT_MIRROR_DIR.mkdir(parents=True, exist_ok=True)
+    mirror = GIT_MIRROR_DIR / f"{repo['id']}.git"
+    if not mirror.exists():
+        _git(None, "clone", "--mirror", "--", repo["url"], str(mirror))
+    else:
+        actual_url = _git(mirror, "remote", "get-url", "origin").decode().strip()
+        if actual_url != repo["url"]:
+            raise RuntimeError(
+                f"mirror URL mismatch for {repo['id']}: {actual_url!r}"
+            )
+    _git(mirror, "fetch", "--prune", "origin", "+refs/*:refs/*")
+    _git(mirror, "fsck", "--connectivity-only", "--no-dangling")
+    return mirror
+
+
+def _matching_refs(mirror: pathlib.Path, patterns: list[str]) -> list[dict]:
+    regexes = [_ref_pattern_regex(p) for p in patterns]
+    lines = _git(
+        mirror, "for-each-ref", "--format=%(refname)%00%(objectname)"
+    ).decode("utf-8", "replace").splitlines()
+    selected = []
+    for line in lines:
+        if not line:
+            continue
+        ref_name, direct_oid = line.split("\x00", 1)
+        if not any(r.fullmatch(ref_name) for r in regexes):
+            continue
+        commit_oid = _git(
+            mirror, "rev-parse", "--verify", f"{ref_name}^{{commit}}"
+        ).decode().strip()
+        selected.append({
+            "name": ref_name,
+            "direct_oid": direct_oid,
+            "commit_oid": commit_oid,
+        })
+    if not selected:
+        raise RuntimeError(f"no refs matched patterns {patterns!r}")
+    return sorted(selected, key=lambda r: r["name"])
+
+
+def _commit_metadata(mirror: pathlib.Path, oid: str) -> dict:
+    raw = _git(
+        mirror,
+        "show",
+        "-s",
+        "--format=%H%x00%T%x00%P%x00%an%x00%ae%x00%at%x00%cn%x00%ce%x00%ct%x00%B",
+        oid,
+    ).decode("utf-8", "replace")
+    fields = raw.split("\x00", 9)
+    if len(fields) != 10:
+        raise RuntimeError(f"could not parse commit metadata for {oid}")
+    return {
+        "oid": fields[0],
+        "tree_oid": fields[1],
+        "parent_oids": fields[2].split() if fields[2] else [],
+        "author_name": fields[3],
+        "author_email": fields[4].strip().lower(),
+        "author_timestamp_ms": int(fields[5]) * 1000,
+        "committer_name": fields[6],
+        "committer_email": fields[7].strip().lower(),
+        "committer_timestamp_ms": int(fields[8]) * 1000,
+        "message": fields[9].rstrip("\n"),
+    }
 
 
-def unified_diff_from_commit_payload(payload: dict) -> str:
-    parts = []
-    for f in payload.get("files") or []:
-        name = f.get("filename", "?")
-        patch = f.get("patch")
-        if patch:
-            parts.append(f"--- {name}\n{patch}")
-        else:
-            parts.append(f"--- {name}\n[no textual patch: binary, submodule, or too large]\n")
-    return "\n\n".join(parts) if parts else "[no files in API response]"
+def _commit_patch(mirror: pathlib.Path, metadata: dict) -> tuple[str, str | None]:
+    parents = metadata["parent_oids"]
+    if len(parents) > 1:
+        return "", None
+    if parents:
+        args = ("diff", "--patch", "--binary", "--full-index", "--no-renames",
+                "--no-ext-diff", "--no-textconv", "--src-prefix=a/",
+                "--dst-prefix=b/", parents[0], metadata["oid"], "--")
+    else:
+        args = ("diff-tree", "--root", "--patch", "--binary", "--full-index",
+                "--no-renames", "--no-ext-diff", "--no-textconv",
+                "--src-prefix=a/", "--dst-prefix=b/", "--no-commit-id",
+                metadata["oid"], "--")
+    patch_bytes = _git(mirror, *args)
+    if not patch_bytes.strip():
+        return "", None
+    # Run patch-id outside the repository so SHA-1 and SHA-256 repositories use
+    # the same canonical patch hash algorithm.
+    patch_id_out = _git(
+        None, "patch-id", "--stable", input_bytes=patch_bytes
+    ).decode().strip()
+    if patch_id_out:
+        st

… preview truncated; 120,051 characters omitted

download full diff B

Hardlinks — judgments / attempts / prompt

prompt download

judgments

attempts

Prompt text is loaded only by the download route.