checkdelivery.py
What a reader downloads, and whether it caches
117 lines. This is the file the build actually runs, copied verbatim at build time.
"""What a reader actually downloads, and whether it stays downloaded.
Two things this site assumes and has never checked:
* The tokenizer bundles are ~2 MB raw. If they are not compressed in transit,
every reader pays four times what they need to. Nothing in the build knows
whether compression actually happens — that is the CDN's decision, not mine.
* Asset URLs carry a content hash (/style.css?v=abc123) specifically so they
can be cached forever. That is pointless if the cache headers do not say so,
and worse than pointless if HTML is cached too, because then corrections
never reach anyone.
python3 checkdelivery.py
"""
import os
import re
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, ".."))
BASE = "https://sweedworks.com"
ASSETS = [
"/vendor/gpt-tokenizer/o200k_base.js",
"/vendor/gpt-tokenizer/cl100k_base.js",
"/attention/model.js",
"/style.css",
"/learn/mlp.js",
"/feed.xml",
]
PAGES = ["/", "/tokens/", "/about/"]
BIG = 50_000 # worth compressing
LONG_CACHE = 86_400 # a day, in seconds
def head(url, compressed):
"""Headers plus the number of bytes curl actually pulled down.
content-length is absent on compressed responses, so it cannot be used to
measure transfer. %{size_download} is what really crossed the wire.
"""
cmd = ["curl", "-sS", "-o", "/dev/null", "-D", "-",
"-w", "\n__size__:%{size_download}", BASE + url]
if compressed:
cmd.insert(1, "--compressed")
out = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
headers = {}
for line in out.stdout.splitlines():
if line.startswith("__size__:"):
headers["__size__"] = line.split(":", 1)[1].strip()
elif ":" in line:
k, v = line.split(":", 1)
headers[k.strip().lower()] = v.strip()
return headers
def max_age(cache_control):
m = re.search(r"max-age=(\d+)", cache_control or "")
return int(m.group(1)) if m else None
def main():
problems, rows = [], []
for url in ASSETS:
path = os.path.join(ROOT, url.lstrip("/"))
raw = os.path.getsize(path) if os.path.exists(path) else 0
h = head(url, compressed=True)
enc = h.get("content-encoding", "none")
sent = int(h.get("__size__", 0) or 0)
cc = h.get("cache-control", "")
age = max_age(cc)
rows.append((url, raw, sent, enc, cc or "—"))
if raw >= BIG and sent and sent > 0.9 * raw:
problems.append(
f"{url}: {raw:,} bytes on disk, {sent:,} received — barely "
f"compressed, readers are paying for the whole file")
if "?v=" not in url and age is not None and age > LONG_CACHE:
# These are unhashed URLs; a long cache means corrections stick.
problems.append(f"{url}: cached for {age:,}s but has no version in "
f"the URL, so a fix cannot reach anyone who has it")
print(f"{'asset':<40} {'on disk':>10} {'sent':>10} encoding cache-control")
for url, raw, sent, enc, cc in rows:
ratio = f"{100 * sent / raw:.0f}%" if raw and sent else ""
print(f" {url:<38} {raw:>10,} {sent:>10,} {enc:<10} {cc[:36]} {ratio}")
print()
for url in PAGES:
h = head(url, compressed=True)
cc = h.get("cache-control", "")
age = max_age(cc)
enc = h.get("content-encoding", "none")
print(f" page {url:<12} encoding {enc:<8} cache-control: {cc or '—'}")
if age and age > 3600:
problems.append(f"{url}: HTML cached for {age:,}s — a correction "
f"would not reach readers for that long")
print()
if problems:
print(f"{len(problems)} problem(s):")
for p in problems:
print(f" - {p}")
return 1
print("Assets are compressed and cacheable; HTML is not cached long enough "
"to trap a correction.")
return 0
if __name__ == "__main__":
sys.exit(main())