M1: make the first story turn work with no Internet

Phase 0B ran the upstream application on a network with no route out and
the first turn died in tiktoken, which downloads its BPE table the first
time anything counts a token. The browser separately fetched three font
families from Google on every page load. Neither is visible on a machine
that has been online once, which is why both now have tests.

The tokenizer table is vendored at
backend/app/context/vendor/cl100k_base.tiktoken and
backend/app/context/encoding.py builds the encoding from it directly,
verifying its SHA-256 against the digest tiktoken itself pins for that
URL. No code path in the tokenizer can reach the network any more —
not a warm cache, not an environment variable a deployment could forget.
The encoding was checked token for token against tiktoken's own.

The three font families are self-hosted as variable fonts under
frontend/public/fonts/ (343 KiB, Latin and Latin Extended), declared in
frontend/src/styles/fonts.css, and re-vendored by
frontend/tools/vendor_fonts.py. Their OFL licences ship beside them.
With no remote asset left, the CSP drops both Google hosts and gains
object-src, base-uri and form-action; woff2 also gets its real media
type, which Python's table lacks on a slim image.

A trusted-LAN Ollama turned out not to work at all over HTTPS. httpx
verifies against the certifi bundle, so an endpoint whose certificate
comes from a CA the user installed on their own machines — a StartOS
server's Ollama, for one — was refused with CERTIFICATE_VERIFY_FAILED
while curl and the browser on the same host accepted it.
app/tlstrust.py builds one context that unions the platform CA store
with certifi's, and all four outbound clients use it. A union rather
than a swap, so an image with an empty system store cannot start failing
on endpoints that worked before. Verification itself is untouched:
CERT_REQUIRED, hostname checking on, and no insecure escape hatch.

The storyteller listener is now loopback by explicit statement rather
than by inheriting uvicorn's default: start.sh, start.ps1, and
docker-compose.yml, which publishes to 127.0.0.1 rather than every
interface. Reaching an Ollama on another machine is outbound and needs
none of that inbound exposure.

backend/requirements.lock pins the exact tested closure;
requirements.txt keeps the ranges. DEVELOPMENT.md covers setup, the
same-host and trusted-LAN Ollama configurations, and how to re-run the
offline proof. PROVENANCE.md records the upstream commit, the MIT terms,
and both vendored assets.

Verified, not just compiled. On an --internal Docker network with
1.1.1.1 unreachable and no name resolving, a campaign was created and
played for six turns through same-host Ollama, restarted, and resumed.
A second run played ten turns through Ollama on a separate physical
machine on the LAN over verified HTTPS, summaries and embeddings
included, with the storyteller's default route deleted so the LAN was
reachable and the Internet was not. Its capture: 893 packets to the
approved host, 730 loopback, zero anywhere else, and zero DNS queries.
Two induced model failures left the accepted story bit-identical. The
inherited SPA was opened in a browser and a campaign read back from it.
Evidence is in planning/reports/M1-BASELINE-REPORT.md, along with the
findings that did not belong in this change.

648 backend tests pass, up from the inherited 632; frontend lint and
build are clean; the image builds. No M2 work is included: the hosted,
cloud, analytics, Postgres and scripting surfaces are untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017foPNqFjAJa2Ngebf5mEfL
This commit is contained in:
JesseMarkowitz
2026-09-02 02:40:28 -04:00
co-authored by Claude Opus 5
parent 7f182a86e9
commit c1a73b3d77
35 changed files with 102102 additions and 23 deletions
+137
View File
@@ -0,0 +1,137 @@
#!/usr/bin/env python3
"""Re-vendor the web fonts the SPA uses into `frontend/public/fonts/`.
The upstream `index.html` linked Google Fonts, so every page load fetched a
stylesheet from `fonts.googleapis.com` and font files from `fonts.gstatic.com`.
That is a runtime Internet dependency and a third party learning when the story
is being read, both of which the local-only requirement rules out (acceptance
tests A01, H01 and H11).
This script downloads the same faces once, at development time, and writes
`frontend/src/styles/fonts.css` pointing at the local copies. It needs the
Internet; the application never does. Run it only when a family, weight, or
subset needs to change, and commit what it produces.
python3 frontend/tools/vendor_fonts.py
Variable fonts are requested on purpose: one file per family per subset covers
every weight the design uses, instead of one file per weight.
"""
import re
import sys
import urllib.request
from pathlib import Path
# Chrome's UA: the Google Fonts CSS API serves woff2 only to a client it
# believes supports it, and returns older formats to anything it doesn't
# recognize.
UA = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/140.0.0.0 Safari/537.36"
)
CSS_URL = (
"https://fonts.googleapis.com/css2"
"?family=Cinzel:wght@400..900"
"&family=Crimson+Pro:ital,wght@0,200..900;1,200..900"
"&family=Inter:wght@100..900"
"&display=swap"
)
# Latin and Latin Extended cover the alphabets this interface and its prose are
# written in. Greek/Cyrillic/Vietnamese subsets are skipped rather than
# vendored unused; text in them falls back to the system stack in
# `tokens.css`. Add the subset here if that stops being acceptable.
SUBSETS = ("latin", "latin-ext")
ROOT = Path(__file__).resolve().parent.parent
FONT_DIR = ROOT / "public" / "fonts"
CSS_OUT = ROOT / "src" / "styles" / "fonts.css"
BLOCK_RE = re.compile(
r"/\* (?P<subset>[a-z-]+) \*/\s*@font-face \{(?P<body>.*?)\}", re.S
)
def _field(body: str, name: str) -> str:
match = re.search(rf"{name}:\s*(.*?);", body, re.S)
if not match:
raise SystemExit(f"no {name} in @font-face block:\n{body}")
return match.group(1).strip()
def _get(url: str) -> bytes:
request = urllib.request.Request(url, headers={"User-Agent": UA})
with urllib.request.urlopen(request, timeout=60) as response:
return response.read()
def main() -> int:
css = _get(CSS_URL).decode()
FONT_DIR.mkdir(parents=True, exist_ok=True)
faces = []
for block in BLOCK_RE.finditer(css):
subset = block.group("subset")
if subset not in SUBSETS:
continue
body = block.group("body")
family = _field(body, "font-family").strip("'\"")
style = _field(body, "font-style")
url = re.search(r"url\((.*?)\)", _field(body, "src")).group(1)
slug = family.lower().replace(" ", "-")
name = f"{slug}-{style}-{subset}.woff2"
(FONT_DIR / name).write_bytes(_get(url))
faces.append(
{
"family": family,
"style": style,
"weight": _field(body, "font-weight"),
"range": _field(body, "unicode-range"),
"file": name,
"source": url,
}
)
if not faces:
raise SystemExit("no @font-face blocks matched the requested subsets")
lines = [
"/* Generated by frontend/tools/vendor_fonts.py — do not hand-edit.",
"",
" Self-hosted so the SPA makes no request to fonts.googleapis.com or",
" fonts.gstatic.com at runtime. See the script for why, and for how to",
" change a family, weight, or subset.",
"",
" Files live in frontend/public/fonts/ and are served by the app itself",
" at /fonts/..., which is what the `font-src 'self'` CSP in",
" backend/app/main.py allows.",
"*/",
"",
]
for face in faces:
lines += [
f"/* {face['family']} {face['style']} — from {face['source']} */",
"@font-face {",
f" font-family: '{face['family']}';",
f" font-style: {face['style']};",
f" font-weight: {face['weight']};",
" font-display: swap;",
f" src: url('/fonts/{face['file']}') format('woff2');",
f" unicode-range: {face['range']};",
"}",
"",
]
CSS_OUT.write_text("\n".join(lines))
total = sum((FONT_DIR / f["file"]).stat().st_size for f in faces)
print(f"vendored {len(faces)} faces, {total / 1024:.0f} KiB, into {FONT_DIR}")
print(f"wrote {CSS_OUT}")
return 0
if __name__ == "__main__":
sys.exit(main())