Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.
Rust agent-to-agentai-agentsclaude-codecodexcoding-agentscursorgrokirohmcpmcp-servermulti-agentpeer-to-peerreason
Relay list: no rollback, a hard size cap, no .curlrc, a quick first start
Lars Talian01e69b8b573f
diff --git a/src/relays.rs b/src/relays.rsindex 470eac2..3be52b4 100644--- a/src/relays.rs+++ b/src/relays.rs@@ -5,7 +5,8 @@//! The list steers every such machine's relay traffic. That traffic is end-to-end encrypted, but a list of someone//! else's relays could cut machines off or show who talks to whom, so it is taken only signed with Reason's proof key//! (`typ` `reason-network-relays`, its own `iss`: neither a sign-in proof nor anything else signed with that key passes-//! for one) and fetched only over HTTPS from [`URL`], following no redirect. Any fault rejects the whole list.+//! for one) and fetched only over HTTPS from [`URL`], following no redirect. Any fault rejects the whole list, and so+//! does one issued before the list this machine has (no rolling back to an older one).//!//! The last good list is kept in the state folder (`relays.json`, the document as served) and used from then on, even//! once it has expired, until a newer one comes. Without one, [`net::DEFAULT_RELAYS`](crate::net::DEFAULT_RELAYS).@@ -15,7 +16,10 @@use anyhow::{Result, anyhow, bail};use iroh::RelayUrl;use serde::Deserialize;-use std::{path::Path, time::Duration};+use std::{+ path::{Path, PathBuf},+ time::Duration,+};pub const URL: &str = "https://reasonmachines.com/network/relays.json";const TYPE: &str = "reason-network-relays";@@ -26,7 +30,12 @@ const MAX_BYTES: usize = 64 << 10;const MAX_RELAYS: usize = 16;/// A list fetched this long after it expired is still taken (clocks differ).const LEEWAY_SECS: i64 = 300;+/// A list issued further ahead than this is refused (a broken clock, or worse): it would hold off every real one.+const MAX_SKEW_SECS: i64 = 86_400;pub const EVERY: Duration = Duration::from_secs(6 * 3600);+/// How long a machine's first network start waits for the list. A quick answer (a list, or a refusal) is taken at+/// once; a slow fetch carries on in the background, for the next start.+const FIRST_WAIT: Duration = Duration::from_secs(3);/// What is served: the list for people to read (ignored here), and the same list signed, which is what counts.#[derive(Deserialize)]@@ -38,6 +47,7 @@ struct Served {struct Claims {iss: String,aud: String,+ iat: i64,exp: i64,relays: Vec<Entry>,}@@ -47,8 +57,16 @@ struct Entry {url: String,}-/// The relays in a served (or cached) document, checked; `now` (seconds) also checks it hasn't expired.-fn parse(doc: &[u8], now: Option<i64>) -> Result<Vec<RelayUrl>> {+/// A checked list.+#[derive(Debug)]+struct List {+ iat: i64,+ relays: Vec<RelayUrl>,+}++/// The list in a served (or cached) document, checked as of `now` (seconds); one `fetched` just now mustn't have+/// expired either.+fn parse(doc: &[u8], now: i64, fetched: bool) -> Result<List> {if doc.len() > MAX_BYTES {bail!("the relay list is over {MAX_BYTES} bytes");}@@ -58,33 +76,40 @@ fn parse(doc: &[u8], now: Option<i64>) -> Result<Vec<RelayUrl>> {if c.iss != ISSUER || c.aud != AUDIENCE {bail!("the relay list isn't Reason Network's");}- if now.is_some_and(|now| c.exp.saturating_add(LEEWAY_SECS) <= now) {+ if c.iat > now.saturating_add(MAX_SKEW_SECS) {+ bail!("the relay list is dated in the future (is this computer's clock right?)");+ }+ if fetched && c.exp.saturating_add(LEEWAY_SECS) <= now {bail!("the relay list expired");}if c.relays.is_empty() || c.relays.len() > MAX_RELAYS {bail!("the relay list has {} relays (1 to {MAX_RELAYS} expected)", c.relays.len());}- let mut out: Vec<RelayUrl> = Vec::new();+ let mut relays: Vec<RelayUrl> = Vec::new();for e in c.relays {let url: RelayUrl = e.url.parse().map_err(|_| anyhow!("not a relay url: {}", e.url))?;if url.scheme() != "https" || url.host_str().is_none() {bail!("not an https relay: {url}");}- if out.contains(&url) {+ if relays.contains(&url) {bail!("{url} is in the relay list twice");}- out.push(url);+ relays.push(url);}- Ok(out)+ Ok(List { iat: c.iat, relays })+}++fn now() -> i64 {+ crate::util::now_ms() / 1000}-fn cache(home: &Path) -> std::path::PathBuf {+fn cache(home: &Path) -> PathBuf {home.join("relays.json")}/// The cached list, if there is a good one (expired or not).-fn cached(home: &Path) -> Option<Vec<RelayUrl>> {- parse(&std::fs::read(cache(home)).ok()?, None).ok()+fn cached(home: &Path) -> Option<List> {+ parse(&std::fs::read(cache(home)).ok()?, now(), false).ok()}/// The relays to use: the cached list, else the built-in default.@@ -93,17 +118,25 @@ pub fn current() -> Vec<RelayUrl> {}fn current_in(home: &Path) -> Vec<RelayUrl> {- cached(home).unwrap_or_else(crate::net::default_relays)+ cached(home).map_or_else(crate::net::default_relays, |l| l.relays)}-/// The same, fetching the list first (briefly) if none is cached yet: a machine's first network start.+/// The same, fetching the list first if none is cached yet (a machine's first network start), for at most+/// `FIRST_WAIT`.pub async fn current_or_fetch() -> Vec<RelayUrl> {- let home = crate::paths::home();+ current_or_fetch_in(crate::paths::home(), url()).await+}++async fn current_or_fetch_in(home: PathBuf, url: String) -> Vec<RelayUrl> {if cached(&home).is_none() {- match tokio::time::timeout(Duration::from_secs(10), refresh_in(&home, &url())).await {- Ok(Ok(())) => {}- Ok(Err(e)) => log(&format!("{e:#}")),- Err(_) => log("fetching the relay list timed out"),+ let h = home.clone();+ let fetching = tokio::spawn(async move {+ if let Err(e) = refresh_in(&h, &url).await {+ log(&format!("{e:#}; using the default relay"));+ }+ });+ if tokio::time::timeout(FIRST_WAIT, fetching).await.is_err() {+ log("the relay list is slow to come: the default relay until the next start");}}current_in(&home)@@ -122,7 +155,12 @@ fn log(msg: &str) {async fn refresh_in(home: &Path, url: &str) -> Result<()> {let doc = fetch(url).await?;- parse(&doc, Some(crate::util::now_ms() / 1000))?;+ let list = parse(&doc, now(), true)?;+ if let Some(had) = cached(home)+ && list.iat < had.iat+ {+ bail!("the relay list is older than the one this machine has");+ }if std::fs::read(cache(home)).ok().as_deref() == Some(doc.as_slice()) {return Ok(());}@@ -141,23 +179,38 @@ fn url() -> String {URL.to_string()}-/// The body at `url`, over HTTPS only (or plain HTTP for a debug build's test URL), following no redirect, of at most-/// `MAX_BYTES`.-async fn fetch(url: &str) -> Result<Vec<u8>> {+/// How curl fetches `url`: ignoring any `.curlrc` (`-q`, which only counts first), over HTTPS only (or plain HTTP+/// for a debug build's test URL), following no redirect, the status after the body.+fn curl_args(url: &str) -> Vec<String> {let proto = if cfg!(debug_assertions) && url != URL && !url.starts_with("https:") { "=http" } else { "=https" };- let max = MAX_BYTES.to_string();- let out = tokio::process::Command::new("curl")- .args(["-fsS", "--proto", proto, "--max-redirs", "0", "--max-time", "30", "--max-filesize", &max, "-w"])- .args(["%{response_code}", url])+ ["-q", "-fsS", "--proto", proto, "--max-redirs", "0", "--max-time", "30", "-w", "%{response_code}", url]+ .map(String::from)+ .to_vec()+}++/// The body at `url`, if a 200 of at most `MAX_BYTES`: curl is stopped as soon as more comes, however it is sent.+async fn fetch(url: &str) -> Result<Vec<u8>> {+ use tokio::io::AsyncReadExt;+ let mut curl = tokio::process::Command::new("curl")+ .args(curl_args(url)).stdin(std::process::Stdio::null())+ .stdout(std::process::Stdio::piped())+ .stderr(std::process::Stdio::piped()).kill_on_drop(true)- .output();- let out = tokio::time::timeout(Duration::from_secs(40), out).await??;+ .spawn()?;+ let stdout = curl.stdout.take().expect("piped");+ // The body and the 3-digit status, and a byte more to tell if there's too much.+ let mut body = Vec::new();+ let mut capped = stdout.take((MAX_BYTES + 4) as u64);+ tokio::time::timeout(Duration::from_secs(40), capped.read_to_end(&mut body)).await??;+ if body.len() > MAX_BYTES + 3 {+ bail!("fetching {url}: stopped past {MAX_BYTES} bytes");+ }+ let out = tokio::time::timeout(Duration::from_secs(10), curl.wait_with_output()).await??;if !out.status.success() {bail!("fetching {url}: {}", String::from_utf8_lossy(&out.stderr).trim());}- // The status comes last: anything but 200 (a redirect, which isn't followed) is no list.- let mut body = out.stdout;+ // Anything but 200 (a redirect, which isn't followed) is no list.let code = body.split_off(body.len().saturating_sub(3));if code != b"200" {bail!("fetching {url}: HTTP {}", String::from_utf8_lossy(&code));@@ -171,10 +224,6 @@ mod tests {use crate::proof::tests::{KID, forger, sign_with, signer};use serde_json::{Value, json};- fn now() -> i64 {- crate::util::now_ms() / 1000- }-fn region(r: &str) -> String {format!("https://relay-{r}.compute.reasonmachines.com")}@@ -206,7 +255,7 @@ mod tests {#[test]fn a_signed_list_gives_its_relays_in_order() {- let got = parse(&doc(claims(&four())), Some(now())).unwrap();+ let got = parse(&doc(claims(&four())), now(), true).unwrap().relays;assert_eq!(got.iter().map(|u| u.to_string()).collect::<Vec<_>>(),four().iter().map(|u| format!("{u}/")).collect::<Vec<_>>()@@ -215,7 +264,7 @@ mod tests {#[test]fn any_fault_rejects_the_whole_list() {- let bad = |d: Vec<u8>| parse(&d, Some(now())).unwrap_err().to_string();+ let bad = |d: Vec<u8>| parse(&d, now(), true).unwrap_err().to_string();let with = |f: &dyn Fn(&mut Value)| {let mut c = claims(&four());f(&mut c);@@ -228,7 +277,7 @@ mod tests {assert!(bad(doc(claims(&[]))).contains("has 0 relays"));let many: Vec<String> = (0..17).map(|i| region(&format!("r{i}"))).collect();assert!(bad(doc(claims(&many))).contains("has 17 relays"));- assert!(parse(&doc(claims(&many[..16])), Some(now())).is_ok(), "16 is fine");+ assert!(parse(&doc(claims(&many[..16])), now(), true).is_ok(), "16 is fine");let dup = [region("sfo1"), region("iad1"), format!("{}/", region("sfo1"))];assert!(bad(doc(claims(&dup))).contains("twice"));assert!(bad(b"{not json".to_vec()).contains("isn't one"));@@ -237,6 +286,8 @@ mod tests {big.splice(1..1, std::iter::repeat_n(b' ', MAX_BYTES));assert!(bad(big).contains("over"));assert!(bad(with(&|c| c["exp"] = json!(now() - LEEWAY_SECS - 1))).contains("expired"));+ assert!(bad(with(&|c| c["iat"] = json!(now() + MAX_SKEW_SECS + 60))).contains("in the future"));+ assert!(bad(with(&|c| c.as_object_mut().unwrap().remove("iat").map(drop).unwrap())).contains("damaged"));assert!(bad(with(&|c| c["iss"] = json!("https://reasonmachines.com/network"))).contains("isn't Reason Network's"));@@ -251,7 +302,7 @@ mod tests {let me = iroh::SecretKey::generate().public();let proof = crate::proof::tests::proof("u_1", "alex", &me);let as_list = serde_json::to_vec(&json!({"signed": proof})).unwrap();- assert!(parse(&as_list, Some(now())).unwrap_err().to_string().contains("not a Reason Network relay list"));+ assert!(parse(&as_list, now(), true).unwrap_err().to_string().contains("not a Reason Network relay list"));// A relay list's signed part isn't a proof, even with a proof's claims in it; and those claims (the proof's// issuer) make no relay list.let mut c = crate::proof::tests::claims("u_1", "alex", &me);@@ -259,11 +310,11 @@ mod tests {let list = sign_with(signer(), json!({"alg": "RS256", "kid": KID, "typ": TYPE}), c);let err = crate::proof::verify(&list, &me).unwrap_err().to_string();assert!(err.contains("not a Reason Network sign-in proof"), "{err}");- let err = parse(&serde_json::to_vec(&json!({"signed": list})).unwrap(), Some(now())).unwrap_err();+ let err = parse(&serde_json::to_vec(&json!({"signed": list})).unwrap(), now(), true).unwrap_err();assert!(err.to_string().contains("isn't Reason Network's"), "{err}");// Nor does a list signed as a proof pass as one.let typ_proof = doc_with(json!({"alg": "RS256", "kid": KID, "typ": "reason-network-proof"}), claims(&four()));- assert!(parse(&typ_proof, Some(now())).unwrap_err().to_string().contains("not a Reason Network relay list"));+ assert!(parse(&typ_proof, now(), true).unwrap_err().to_string().contains("not a Reason Network relay list"));}#[test]@@ -279,7 +330,8 @@ mod tests {let _ = std::fs::remove_dir_all(&h);}- /// Serves `responses` in turn on a local port, one connection each, as HTTP/1.0; its URL.+ /// Serves `responses` in turn on a local port, one connection each, as HTTP/1.1 (a status of `chunked` sends a+ /// 200 in chunks, with no length); its URL.async fn serve(responses: Vec<(&'static str, Vec<u8>)>) -> String {use tokio::io::{AsyncReadExt, AsyncWriteExt};let l = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();@@ -288,8 +340,19 @@ mod tests {for (status, body) in responses {let (mut s, _) = l.accept().await.unwrap();let _ = s.read(&mut [0; 4096]).await;+ if status == "chunked" {+ let _ = s.write_all(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n").await;+ for chunk in body.chunks(8192) {+ let framed = [format!("{:x}\r\n", chunk.len()).as_bytes(), chunk, b"\r\n"].concat();+ if s.write_all(&framed).await.is_err() {+ break;+ }+ }+ let _ = s.write_all(b"0\r\n\r\n").await;+ continue;+ }let head = format!(- "HTTP/1.0 {status}\r\nContent-Length: {}\r\nLocation: https://example.com/\r\n\r\n",+ "HTTP/1.1 {status}\r\nContent-Length: {}\r\nLocation: https://example.com/\r\nConnection: close\r\n\r\n",body.len());let _ = s.write_all(&[head.as_bytes(), &body].concat()).await;@@ -310,6 +373,8 @@ mod tests {("301 Moved", good.clone()),("200 OK", doc(claims(&[region("sfo1"), "http://relay.example.com".into()]))),("200 OK", doc(only_fra)),+ ("chunked", vec![b' '; 4 << 20]),+ ("chunked", good.clone()),]).await;refresh_in(&h, &url).await.unwrap();@@ -319,9 +384,53 @@ mod tests {assert!(refresh_in(&h, &url).await.unwrap_err().to_string().contains("HTTP 301"), "no redirect followed");assert!(refresh_in(&h, &url).await.unwrap_err().to_string().contains("https"), "a bad list");assert!(refresh_in(&h, &url).await.unwrap_err().to_string().contains("expired"), "an expired one, fresh");+ let err = refresh_in(&h, &url).await.unwrap_err().to_string();+ assert!(err.contains("stopped past 65536 bytes"), "too much, with no length to tell beforehand: {err}");+ refresh_in(&h, &url).await.unwrap();assert!(refresh_in(&h, &url).await.is_err(), "nobody there");assert_eq!(std::fs::read(cache(&h)).unwrap(), good, "the last good list stays");assert_eq!(std::fs::read_dir(&h).unwrap().count(), 1, "nothing but the list left behind");let _ = std::fs::remove_dir_all(&h);}++ #[tokio::test]+ async fn an_older_list_never_replaces_a_newer_one() {+ let h = home();+ let (mut newer, mut older) = (claims(&four()), claims(&[region("sfo1")]));+ newer["iat"] = json!(now());+ older["iat"] = json!(now() - 20 * 86_400);+ let url = serve(vec![("200 OK", doc(newer.clone())), ("200 OK", doc(older)), ("200 OK", doc(newer))]).await;+ refresh_in(&h, &url).await.unwrap();+ let err = refresh_in(&h, &url).await.unwrap_err().to_string();+ assert!(err.contains("older than the one this machine has"), "{err}");+ assert_eq!(current_in(&h).len(), 4);+ refresh_in(&h, &url).await.unwrap();+ let _ = std::fs::remove_dir_all(&h);+ }++ #[test]+ fn curl_reads_no_curlrc_and_only_https() {+ let args = curl_args(URL);+ assert_eq!(args[0], "-q", "only first does it count");+ assert!(args.windows(2).any(|w| w == ["--proto", "=https"]));+ assert!(args.windows(2).any(|w| w == ["--max-redirs", "0"]));+ assert!(!args.iter().any(|a| a == "-L" || a == "--location"));+ }++ #[tokio::test]+ async fn the_first_start_does_not_wait_on_a_failed_fetch() {+ let h = home();+ let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();+ let start = std::time::Instant::now();+ let got = current_or_fetch_in(h.clone(), format!("http://127.0.0.1:{port}/relays.json")).await;+ assert!(start.elapsed() < Duration::from_secs(1), "{:?}", start.elapsed());+ assert_eq!(got, crate::net::default_relays());+ // A server that answers with nothing for long: the default, after a short wait.+ let silent = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let url = format!("http://{}/relays.json", silent.local_addr().unwrap());+ let start = std::time::Instant::now();+ assert_eq!(current_or_fetch_in(h.clone(), url).await, crate::net::default_relays());+ assert!(start.elapsed() < FIRST_WAIT + Duration::from_secs(1), "{:?}", start.elapsed());+ let _ = std::fs::remove_dir_all(&h);+ }}diff --git a/tests/e2e/mixed_versions.py b/tests/e2e/mixed_versions.pyindex 489df19..06b1e78 100644--- a/tests/e2e/mixed_versions.py+++ b/tests/e2e/mixed_versions.py@@ -7,7 +7,7 @@ Needs internet (iroh relays/discovery)."""import json, os, re, subprocess, sys, tempfile, timesys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))-from proofs import TRUST, proof+from proofs import TRUST, proof, serve_relay_listsROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))NEW = os.path.join(ROOT, "target", "debug", "reason-network")@@ -15,6 +15,8 @@ OLD = os.environ["REASON_NETWORK_OLD_BIN"]D = tempfile.mkdtemp(prefix="reason-network-mixed-")FAKE_BIN = os.path.join(D, "bin")UNEXPECTED = os.path.join(D, "unexpected.log")+# Reason's relay list, served here for this build (debug): never the real one. The older build has no list.+RELAYS_URL = serve_relay_lists({"all": ["sfo1", "iad1", "fra1", "sin1"]}) + "/all.json"# alex and nina run this build, olga the older one.WHO = {"alex": (NEW, "laptop"), "olga": (OLD, "oldbox"), "nina": (NEW, "desk"), "nina2": (NEW, "pad"), "nina3": (NEW, "tab"), "mal": (NEW, "evil")}results = []@@ -30,7 +32,7 @@ def env(who):e.update(HOME=os.path.join(D, "home"), PATH=FAKE_BIN + os.pathsep + os.environ["PATH"], CLAUDE_CONFIG_DIR=os.path.join(D, "claude"),REASON_NETWORK_HOME=os.path.join(D, who), REASON_NETWORK_MACHINE=WHO[who][1], REASON_NETWORK_NO_MDNS="1",REASON_NETWORK_NO_SERVICE="1", REASON_NETWORK_NO_UPDATE="1", REASON_NETWORK_NO_TRAY="1",- REASON_NETWORK_TRUST_KEYS=TRUST)+ REASON_NETWORK_TRUST_KEYS=TRUST, REASON_NETWORK_RELAYS_URL=RELAYS_URL)return ediff --git a/tests/e2e/proofs.py b/tests/e2e/proofs.pyindex ad8dba8..0b1c498 100644--- a/tests/e2e/proofs.py+++ b/tests/e2e/proofs.py@@ -1,7 +1,7 @@-"""Reason sign-in proofs for the e2e tests: a stand-in for Reason's signing key, trusted through+"""Reason sign-in proofs and relay lists for the e2e tests: a stand-in for Reason's signing key, trusted throughREASON_NETWORK_TRUST_KEYS (debug builds). RS256 (RSASSA-PKCS1-v1_5 with SHA-256) in plain Python, so the tests neednothing installed: keys are made fresh for each run."""-import base64, hashlib, json, secrets, time+import base64, hashlib, http.server, json, secrets, threading, time# DER of the DigestInfo for SHA-256, which PKCS#1 v1.5 puts before the hash (RFC 8017, 9.2).SHA256_INFO = bytes.fromhex("3031300d060960864801650304020105000420")@@ -70,3 +70,38 @@ def proof(sub, name, endpoint, key=None, **claims):head = {"alg": "RS256", "kid": KID, "typ": "reason-network-proof"}signing = f"{b64(json.dumps(head).encode())}.{b64(json.dumps(body).encode())}"return f"{signing}.{b64(sign(key or REASON, signing.encode()))}"+++RELAY = {r: f"https://relay-{r}.compute.reasonmachines.com" for r in ("sfo1", "iad1", "fra1", "sin1")}+++def relay_list(regions):+ """relays.json as Reason serves it: the list, and the same list signed."""+ relays = [{"url": RELAY[r], "region": r} for r in regions]+ now = int(time.time())+ claims = {"iss": "https://reasonmachines.com/network/relays", "aud": "reason-network", "relays": relays,+ "iat": now, "exp": now + 30 * 86400}+ head = {"alg": "RS256", "kid": KID, "typ": "reason-network-relays"}+ signing = f"{b64(json.dumps(head).encode())}.{b64(json.dumps(claims).encode())}"+ return json.dumps({"relays": relays, "signed": f"{signing}.{b64(sign(REASON, signing.encode()))}"}).encode()+++def serve_relay_lists(lists):+ """Serves `lists` ({name: regions}) at /<name>.json on a local port, for REASON_NETWORK_RELAYS_URL (debug builds),+ so tests never fetch Reason's real list; the server's base URL."""+ docs = {f"/{name}.json": relay_list(regions) for name, regions in lists.items()}++ class Lists(http.server.BaseHTTPRequestHandler):+ def do_GET(self):+ body = docs.get(self.path, b"")+ self.send_response(200 if body else 404)+ self.send_header("Content-Length", str(len(body)))+ self.end_headers()+ self.wfile.write(body)++ def log_message(self, *args):+ pass++ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Lists)+ threading.Thread(target=server.serve_forever, daemon=True).start()+ return f"http://127.0.0.1:{server.server_port}"diff --git a/tests/e2e/regional_relays.py b/tests/e2e/regional_relays.pyindex c46845d..d603a62 100644--- a/tests/e2e/regional_relays.py+++ b/tests/e2e/regional_relays.py@@ -6,18 +6,18 @@ debug builds honor), so alex knows only sfo1 and bea only fra1.Needs internet (Reason's live relays, n0's address lookup)."""-import base64, http.server, json, os, re, subprocess, sys, tempfile, threading, time+import base64, os, re, subprocess, sys, tempfile, timesys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))-from proofs import KID, REASON, TRUST, b64, proof, sign+from proofs import RELAY, TRUST, proof, serve_relay_listsROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))BIN = os.environ.get("REASON_NETWORK_TEST_BIN", os.path.join(ROOT, "target", "debug", "reason-network"))D = tempfile.mkdtemp(prefix="reason-network-relays-")FAKE_BIN = os.path.join(D, "bin")UNEXPECTED = os.path.join(D, "unexpected.log")-RELAY = {r: f"https://relay-{r}.compute.reasonmachines.com" for r in ("sfo1", "iad1", "fra1", "sin1")}# Who knows which relays; carl's team runs its own (sin1), which must win over his list (fra1).WHO = {"alex": ("laptop", ["sfo1"]), "bea": ("desk", ["fra1"]), "carl": ("box", ["fra1"])}+LISTS = serve_relay_lists({w: regions for w, (_, regions) in WHO.items()})results = []@@ -26,42 +26,12 @@ def check(name, ok, detail=""):print(f"{'PASS' if ok else 'FAIL'}: {name}" + ("" if ok else f"\n {detail}"))-def relay_list(regions):- """relays.json as Reason serves it: the list, and the same list signed."""- relays = [{"url": RELAY[r], "region": r} for r in regions]- now = int(time.time())- claims = {"iss": "https://reasonmachines.com/network/relays", "aud": "reason-network", "relays": relays,- "iat": now, "exp": now + 30 * 86400}- head = {"alg": "RS256", "kid": KID, "typ": "reason-network-relays"}- signing = f"{b64(json.dumps(head).encode())}.{b64(json.dumps(claims).encode())}"- return json.dumps({"relays": relays, "signed": f"{signing}.{b64(sign(REASON, signing.encode()))}"}).encode()---LISTS = {f"/{w}.json": relay_list(regions) for w, (_, regions) in WHO.items()}---class Lists(http.server.BaseHTTPRequestHandler):- def do_GET(self):- body = LISTS.get(self.path)- self.send_response(200 if body else 404)- self.send_header("Content-Length", str(len(body or b"")))- self.end_headers()- self.wfile.write(body or b"")-- def log_message(self, *args):- pass---server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Lists)-threading.Thread(target=server.serve_forever, daemon=True).start()--def env(who):e = {k: v for k, v in os.environ.items() if not k.startswith("CLAUDE")}e.update(HOME=os.path.join(D, "home"), PATH=FAKE_BIN + os.pathsep + os.environ["PATH"], CLAUDE_CONFIG_DIR=os.path.join(D, "claude"),REASON_NETWORK_HOME=os.path.join(D, who), REASON_NETWORK_MACHINE=WHO[who][0], REASON_NETWORK_NO_MDNS="1",REASON_NETWORK_NO_SERVICE="1", REASON_NETWORK_NO_UPDATE="1", REASON_NETWORK_NO_TRAY="1",- REASON_NETWORK_TRUST_KEYS=TRUST, REASON_NETWORK_RELAYS_URL=f"http://127.0.0.1:{server.server_port}/{who}.json")+ REASON_NETWORK_TRUST_KEYS=TRUST, REASON_NETWORK_RELAYS_URL=f"{LISTS}/{who}.json")return e@@ -137,7 +107,6 @@ try:finally:for w in WHO:subprocess.run([BIN, "stop"], env=env(w), capture_output=True, timeout=30)- server.shutdown()log = open(UNEXPECTED).read() if os.path.exists(UNEXPECTED) else ""check("nothing opened a browser or reached the real notifications", log == "", log)print(f"\n{sum(results)}/{len(results)} checks passed (state in {D})")diff --git a/tests/e2e/two_machines.py b/tests/e2e/two_machines.pyindex b3f4788..e898ede 100644--- a/tests/e2e/two_machines.py+++ b/tests/e2e/two_machines.py@@ -7,7 +7,7 @@ test messages into that real session (they say "ignore this")."""import base64, json, os, re, subprocess, sys, tempfile, timesys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))-from proofs import TRUST, keygen, proof+from proofs import TRUST, keygen, proof, serve_relay_listsROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))BIN = os.path.join(ROOT, "target", "debug", "reason-network")@@ -22,6 +22,8 @@ IN_CLAUDE = "CLAUDE_CODE_MESSAGING_SOCKET" in os.environ# First on PATH: nothing here may open a browser or reach this machine's real notifications.FAKE_BIN = os.path.join(D, "bin")UNEXPECTED = os.path.join(D, "unexpected.log")+# Reason's relay list, served here: never the real one.+RELAYS_URL = serve_relay_lists({"all": ["sfo1", "iad1", "fra1", "sin1"]}) + "/all.json"results = []@@ -33,7 +35,7 @@ def check(name, ok, detail=""):def env(who, strip_claude=True):e = dict(os.environ, REASON_NETWORK_HOME=H[who], REASON_NETWORK_MACHINE=MACHINE[who], REASON_NETWORK_NO_MDNS="1", REASON_NETWORK_NO_SERVICE="1", REASON_NETWORK_CODEX_BIN=FAKE_CODEX)e.update({"HOME": os.path.join(D, "home"), "PATH": FAKE_BIN + os.pathsep + os.environ["PATH"], "REASON_NETWORK_NO_UPDATE": "1",- "REASON_NETWORK_NO_TRAY": "1", "REASON_NETWORK_TRUST_KEYS": TRUST})+ "REASON_NETWORK_NO_TRAY": "1", "REASON_NETWORK_TRUST_KEYS": TRUST, "REASON_NETWORK_RELAYS_URL": RELAYS_URL})e.update({"REASON_NETWORK_OSASCRIPT_BIN": FAKE_NOTIFY, "REASON_NETWORK_NOTIFY-SEND_BIN": FAKE_NOTIFY, "DBUS_SESSION_BUS_ADDRESS": "test","NOTIFY_LOG": os.path.join(D, f"notify-{who}.log")})if strip_claude: