#!/usr/bin/env python3 """Find the shared store's per-file upload cap, from the server's own replies. Why this is needed: #1357's claim rests on CRLF counts in a snapshot that no worker can re-fetch (the served tree is LF and has a different file count), so the only way to make its numbers checkable is to SHIP the bytes -- and the brief asks for exactly that ("the measured snapshot as a single archive ... uploaded as a return file"). The store's upload route takes text only (`POST /files` with a JSON `content` string), so a zip cannot travel as itself; it must go text-encoded (base64), which makes the archive 17,040,522 characters. Its size then decides how it must be split, so this measures the limit instead of guessing one: it POSTs real prefixes of the pinned archive at increasing sizes and records the status and the server's own error text for each. Two separate refusals live here, and the first probe conflated them: the route REJECTS an extension outside a fixed allow-list (`.b64` is not on it, so every base64 part 400s regardless of size), and it also has a size ceiling (a multi-MiB body gets 413). So the probe takes the extension as an argument and reports which refusal it hit. usage: probe_upload_limit.py [size-in-KiB ...] (env EXT picks the upload extension, default txt) """ import json import os import sys import urllib.error import urllib.request import uuid sys.stdout.reconfigure(encoding="utf-8", errors="replace") HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) # the run's state dir SERVER = "https://solveathome.org" def token(): p = (os.environ.get("SOLVEATHOME_TOKEN_FILE") or os.path.join(os.environ.get("LOCALAPPDATA", ""), "solveathome", "credentials", "twin-primes.token")) t = open(p, encoding="utf-8").read().strip() return t.split("=", 1)[1].strip() if "=" in t else t def ctx(request_id): run = json.load(open(os.path.join(RUN, "run.json"), encoding="utf-8")) join = json.load(open(os.path.join(RUN, "join.json"), encoding="utf-8")) return {"X-Model": run.get("model"), "X-Effort": run.get("effort"), "X-Session": run.get("session"), "X-Department": run.get("department_id"), "X-Attempt": join.get("attempt_id"), "X-Request-ID": request_id} def post(name, content, tok, ctxh): body = json.dumps({"name": name, "content": content}).encode("utf-8") req = urllib.request.Request(SERVER + "/files", data=body, method="POST") req.add_header("Authorization", "Bearer " + tok) req.add_header("Content-Type", "application/json") req.add_header("Accept", "application/json") for k, v in ctxh.items(): if v: req.add_header(k, str(v)) try: with urllib.request.urlopen(req, timeout=300) as r: return r.status, r.read().decode("utf-8", "replace") except urllib.error.HTTPError as e: return e.code, e.read().decode("utf-8", "replace") def main(): src = sys.argv[1] sizes = [int(x) * 1024 for x in sys.argv[2:]] or [256 * 1024, 512 * 1024, 1024 * 1024] ext = os.environ.get("EXT", "txt") raw = open(src, "rb").read().decode("ascii") tok = token() out = {"source": os.path.basename(src), "source_chars": len(raw), "extension": ext, "probes": []} for n in sizes: chunk = raw[:n] # The server keys a request id to its body: a second POST with the same X-Request-ID and # different content is refused 409 "request ID already used with different content". So the # id must carry the size (this is why the first run of this probe reported 409 for every # size above the first -- an instrument defect, not a refusal). rid = "probe-upload-limit-%d-%s" % (n, uuid.uuid4().hex[:8]) code, body = post("probe-limit-%d.%s" % (n, ext), chunk, tok, ctx(rid)) try: j = json.loads(body) except Exception: # noqa: BLE001 j = {"raw": body[:300]} err = j.get("error") or j.get("raw") rec = {"chars": len(chunk), "status": code, "ok": code == 200 and bool(j.get("sha256")), "server_sha256": (j.get("sha256") or "")[:16], "matches_local": j.get("sha256") == __import__("hashlib").sha256( chunk.encode("utf-8")).hexdigest(), "existed": j.get("existed"), "error": err} out["probes"].append(rec) out["probes"][-1]["request_id"] = rid print("%9d chars (%.2f MiB) -> HTTP %s %s" % (len(chunk), len(chunk) / 1048576.0, code, err or ("stored, sha %s, existed=%s" % (rec["server_sha256"], rec["existed"])))) ok = [p["chars"] for p in out["probes"] if p["ok"]] out["largest_accepted_chars"] = max(ok) if ok else 0 out["first_refused"] = next((p for p in out["probes"] if not p["ok"]), None) json.dump(out, open(os.path.join(HERE, "upload-limit.json"), "w", encoding="utf-8"), indent=1, ensure_ascii=False) print("\nlargest accepted: %d chars | first refused: %d chars -> %s" % (out["largest_accepted_chars"], (out["first_refused"] or {}).get("chars", -1), (out["first_refused"] or {}).get("error"))) return 0 if __name__ == "__main__": sys.exit(main())