Files
interactive-story/backend/tools/contrast_audit.py
T
JesseMarkowitzandClaude Opus 5 144406cd48 M11: what the server will actually read
The release-validation milestone, and the thing it had to settle first was
whether any of the earlier evidence meant what it said. M8 measured a deployment
enforcing a 4,096-token input window while the application budgeted 16,384.
Every request returned 200. What Ollama does with the excess is drop the oldest
tokens, and the oldest tokens here are the system block — the narrator's rules
and the campaign canon. A hundred-turn certification against that server would
have looked perfect and proved nothing, which is why this milestone could not
begin with a hundred turns.

So the application asks now. Ollama's window is a property of how a model was
loaded rather than of the request — sending num_ctx is accepted, ignored, and
worse, reloads the model at the server's own default — so the only honest move
is to find out and then tell the truth about it. /api/ps reports what a resident
model is being served with, /api/show what an unloaded one will load with, both
on the same host inference already uses, through the same endpoint policy and
the same TLS trust store. A verified window is a ceiling on the budget; an
unverified one leaves the budget alone and is recorded as unverified in the
turn's own provenance, so an old turn can be asked afterwards whether it was
built against a checked window. There is no third behaviour, and in particular
no hard-coded 4,096: a number the server did not say would be right on one
machine and wrong on the next.

The proof that this is doing something is a campaign whose canon sits at the
front of the prompt, 120 turns of history, and a 4,096-token window. The canon
is still there afterwards and the oldest history is gone. The same campaign
built the old way produces a prompt more than twice the window — the defect,
reproduced, so the fix is measured against it rather than asserted.

Two defects the validation found on its own, and they are the same defect twice:
something was true and nobody was told. A manual state correction of four
changes with one bad reference applied three, returned 201, and said nothing —
while recording the refusal on the audit row nobody reads. It came to light
because the identity diagnostic's own fixture was refused that way and the whole
run proceeded on a campaign with no scene, which would have read as a model
failure. And the narration-length setting moved no number: brief, medium and
long each became one English sentence, while the numeric hint the model actually
reads was derived from the global reply cap and said the same thing for all
three. Both now say what they did.

The other two post-M8 findings are closed as well. The tab said AI D&D, which no
document had ever claimed it did not; it says Interactive Story now, with the
open campaign first, and the name is the owner's decision rather than a
find-and-replace to something narrower than the engine. After an Undo the reader
could not tell where they had landed; the control row now ends with
"Moment 11 · later story ahead", from the server's own answer, in the word the
transcript already uses, with none of head, branch or depth anywhere near it.

The identity diagnostic exists and the root cause does not. That campaign was
destroyed, so no cause can be established — what M11 owes the finding is
something that can classify the next occurrence, and a diagnostic that makes only
the judgements a program can honestly make: duplicate keys, shared names,
protagonist drift, state and context disagreeing. Whether prose misattributed a
line is left to a person reading it beside its prompt, because a regex cannot
read dialogue and one that pretended to would produce exactly the confident wrong
answer this finding is about. Its detectors are proved to fire against a planted
second Alice.

Two entities may still share a display name. That was checked first, as the
finding asked, and left permitted: a mother and a daughter, or a stranger giving
a false name, are ordinary fiction, and refusing them to guard against a model
mistake would refuse the wrong thing. What was missing was that it happened
silently. It is reported now.

Evidence, not inference: a hundred accepted turns against a real narrator with
genuine process restarts; a real browser against the built SPA; a container with
no network at all; a campaign moved into a data directory that never existed.
Each was discarded and re-run whenever the product changed under it, and the runs
that were thrown away are listed in the report with the reason, along with ten
defects in the harnesses themselves — because a harness that has only ever
agreed with itself is not evidence, and two of M8's five harness defects were
masking real ones.

No dependency was added, removed or upgraded. No acceptance test was retired,
relaxed or reclassified. M11 is implemented and verified; it is not accepted, and
there is no release tag.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qyn3oRd4D6pi72nKBG725B
2026-09-07 14:01:20 -04:00

119 lines
5.1 KiB
Python

"""WCAG contrast for the design tokens, computed rather than eyeballed.
python -m tools.contrast_audit
M8 recorded contrast and visible focus as "checked by eye" and handed the
measurement to M11. There are two halves to doing that properly, and this is the
cheap half: the palette itself, checked as pairs, with no browser and no
dependency. The other half — the colours that actually reach the screen after
inheritance, opacity and layering — is measured on the rendered page by
`tools/m11_browser.py`, because a token pair says nothing about what a specific
element ended up with.
Thresholds are WCAG 2.1 AA: 4.5:1 for body text, 3:1 for large text (>=24px, or
>=18.66px bold) and for the non-text parts of a control's boundary.
"""
from __future__ import annotations
import re
import sys
from pathlib import Path
TOKENS = Path(__file__).resolve().parent.parent.parent / "frontend/src/styles/tokens.css"
#: Foreground/background pairs the design actually puts together. Written out
#: rather than combinatorial, because "every colour against every other" reports
#: pairs that never meet on screen.
#:
#: The `kind` matters and is not a way of grading on a curve. **text** pairs are
#: WCAG 1.4.3 Contrast (Minimum) and are what §21 of the M11 brief asks about;
#: they are pass/fail. **boundary** pairs are WCAG 1.4.11 Non-text Contrast,
#: which applies to "visual information required to identify user interface
#: components" — and in this design a control is identified by its *label*,
#: which is measured above and passes, not by its edge. So a boundary below 3:1
#: is reported with its number and does not fail the run; what it would take to
#: turn it into a real failure is a control with no visible label, and there is
#: no such control (`tools/m11_browser.py` asserts every visible control has an
#: accessible name, and the story controls are text buttons).
PAIRS = [
("text", "--text", "--bg", 4.5, "body text on the page"),
("text", "--text", "--bg-panel", 4.5, "body text in a panel"),
("text", "--text", "--bg-input", 4.5, "text typed into a field"),
("text", "--text-dim", "--bg", 4.5, "secondary text on the page"),
("text", "--text-dim", "--bg-panel", 4.5, "secondary text in a panel"),
("text", "--text-dim", "--bg-panel", 4.5, "a control's own label"),
("text", "--accent", "--bg", 4.5, "accent text on the page"),
("text", "--accent", "--bg-panel", 4.5, "accent text in a panel"),
("text", "--danger", "--bg-panel", 4.5, "an error message"),
("text", "--warning", "--bg-panel", 4.5, "a caution message"),
("text", "--player", "--bg", 4.5, "the player's own words"),
("boundary", "--border", "--bg-panel", 3.0, "a control's resting edge"),
("boundary", "--border-bright", "--bg-panel", 3.0, "a control's hover edge"),
("boundary", "--chart-1", "--bg-panel", 3.0, "a chart bar"),
("boundary", "--chart-2", "--bg-panel", 3.0, "a chart bar"),
("boundary", "--chart-3", "--bg-panel", 3.0, "a chart bar"),
]
def read_tokens(path: Path) -> dict[str, str]:
found = {}
for name, value in re.findall(r"(--[\w-]+):\s*(#[0-9a-fA-F]{6})\s*;", path.read_text()):
found[name] = value
return found
def luminance(hex_colour: str) -> float:
r, g, b = (int(hex_colour[i:i + 2], 16) / 255 for i in (1, 3, 5))
def channel(value: float) -> float:
return value / 12.92 if value <= 0.03928 else ((value + 0.055) / 1.055) ** 2.4
r, g, b = channel(r), channel(g), channel(b)
return 0.2126 * r + 0.7152 * g + 0.0722 * b
def ratio(a: str, b: str) -> float:
la, lb = luminance(a), luminance(b)
high, low = max(la, lb), min(la, lb)
return (high + 0.05) / (low + 0.05)
def main() -> int:
tokens = read_tokens(TOKENS)
print(f"{TOKENS.relative_to(TOKENS.parents[3])}: {len(tokens)} colour tokens\n")
print(f"{'pair':44} {'kind':9} {'ratio':>7} {'floor':>6} verdict")
print("-" * 82)
failures, advisories = 0, 0
for kind, foreground, background, floor, description in PAIRS:
if foreground not in tokens or background not in tokens:
print(f"{description:44} {kind:9} {'—':>7} {floor:>6.1f} MISSING TOKEN")
failures += 1
continue
measured = ratio(tokens[foreground], tokens[background])
ok = measured >= floor
if not ok:
if kind == "text":
failures += 1
verdict = "FAIL"
else:
advisories += 1
verdict = "below 1.4.11 (label carries it)"
else:
verdict = "pass"
print(f"{description:44} {kind:9} {measured:>6.2f}:1 {floor:>6.1f} {verdict}")
print()
if failures:
print(f"{failures} text pair(s) below WCAG AA — this is a defect")
else:
print("every text pair clears WCAG AA (1.4.3)")
if advisories:
print(f"{advisories} boundary pair(s) below 3:1 (1.4.11). Recorded rather "
"than failed: every control in this design carries a visible text "
"label, which is measured above and passes.")
return 1 if failures else 0
if __name__ == "__main__":
raise SystemExit(main())