open-standards/wai/live-conformance/timeliness_verify.py
Transaction Science e6a38c6b82 wai: timeliness fallback ladder, and the reference sink scheduler that runs §7 plans
An entry's fallback is an ordered ladder: null, one rung as an object, or two to eight rungs as an
array in strictly falling value, no object named twice (§4.2 rule 3.4, timeliness_ladder_invalid).
One rung has one form, the object, so an entry with one fallback object is a ladder of one rung and
reads, judges and plans as it did; the cache-authority corpus regenerates unchanged.

§5.3 states which rung applies: at by_ms the primary held, else the first rung held; holding
neither, the sink has passed every rung, states `passed`, presents the primary late at its value at
arrival, and at the end, having presented nothing, abandons and states `ended`. §6.3 and §7.3 take
the ladder, and a plan's choice names its rung.

§7.5 and timeliness::scheduler run a sink's plans against the instants it first held each object:
fetch, abandon, present, passed and ended by exact steps, a function of those inputs alone. A later
plan replaces the one in force wholly. plans_invalid and arrivals_invalid refuse a run's inputs.

The corpus gains the group E, ladder claims, verdicts and plans, and runs, each outcome declared in
the generator. timeliness_verify.py writes the ladder and the run from the text, with mutations and
weakenings for both, and claim_verify.py holds rule 3.4.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 11:05:46 -05:00

1138 lines
49 KiB
Python

#!/usr/bin/env python3
"""Independent verifier of the timeliness corpus (extensions/timeliness.md).
Written from the extension's text. It shares no code with wai-rs, which generates the corpus,
and never runs it. It takes from the second implementations of the texts this extension builds
on what they define: group and object receipts from the JWP corpus's
(`../jwp-conformance/jwp_verify.py`, jwp-receipts §2.2, §3); a claim's steps 1 to 5 from the
claim corpus's (`../claim-conformance/claim_verify.py`, jwp-receipts §3.1), whose step 4 holds
§4.2's rules; a transmission claim's step 6, a schedule's, and a receiver's passes from the
carousel corpus's (`carousel_verify.py`: bearer-receipts §4.3, carousel §4.3 and §7); a holdings
record's form, signature and holdings from the holdings corpus's (`holdings_verify.py`, holdings
§4.4, §5.4); and a WAI1 envelope's manifest from the staged corpus's reader (SPEC §2, §3).
Everything else this extension adds is written here:
- §4.3: a timeliness claim against its group, the object receipts and envelopes held, and each
rung's group and object receipt;
- §5: an entry's value at an instant, and its end;
- §6: the intents that stand, the records that stand and the held-at statements they give
(`held_from_records`, with §2's `before_group`), each intent's lines (`lines`), and a
statement offered;
- §7: the plan's inputs, each source's arrival, and the choice, the ladder's rung included;
- §7.5: a sink's run of its plans against its clock (`run`).
For every case it requires the outcome the corpus states, field for field; that every reason
of §9's table, every outcome of §6.3's and every action §7.5.3 states appears in the corpus; then
it mutates copies of the
corpus, one defect each, and requires every copy to fail; and it drops each of its own checks in
turn and requires the corpus to catch the weakened verifier. A bound no vector carries is checked
directly.
python3 timeliness_verify.py <live-conformance/timeliness>
Exit status 0 only if every case verifies, every mutated copy fails, every weakened verifier is
caught and every direct check holds. Python 3.8 or later; the `cryptography` package, which the
claim verifier imports.
"""
import copy
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
for d in ("claim-conformance", "jwp-conformance"):
sys.path.insert(0, os.path.join(HERE, "..", d))
import claim_verify as CV # noqa: E402
import jwp_verify as JV # noqa: E402
import carousel_verify as KV # noqa: E402
import holdings_verify as HV # noqa: E402
SV = CV.SV # the staged corpus's strict WAI1 reader, which the claim verifier loads
EXTENSION = os.path.join(HERE, "..", "extensions", "timeliness.md")
SPEC = os.path.join(HERE, "..", "SPEC.md")
JWP_RECEIPTS = os.path.join(HERE, "..", "extensions", "jwp-receipts.md")
TELEMETRY = os.path.join(HERE, "..", "extensions", "delivery-telemetry.md")
FULL = 1_000_000 # §2: a value in parts per million of the full value
LAST_INSTANT = 253_402_300_799_999 # §2: 9999-12-31T23:59:59.999Z
MAX_INT = (1 << 53) - 1
ACTIONS = ("fetch", "abandon", "present", "passed", "ended") # §7.5.3
# A weakened run drops one of these checks (see `WEAKENINGS`); None checks everything.
DROPPED = None
def drops(name):
return DROPPED == name
def text_of(path):
with open(path, encoding="utf-8") as f:
return f.read()
def table_rows(text, heading):
"""The first backticked cell of each row of the table under `heading`, up to the next heading
of its level or above."""
at = text.index("\n" + heading)
level = heading.split(" ")[0]
ends = [text.find("\n" + h + " ", at + 1) for h in ("#" * n for n in range(2, len(level) + 1))]
ends = [e for e in ends if e >= 0]
rows = text[at:min(ends) if ends else len(text)].splitlines()
return [r.split("`")[1] for r in rows if r.startswith("| `")]
def reasons_table():
"""§9's codes, in order, each with the section the table says raises it."""
text = text_of(EXTENSION)
at = text.index("\n## 9. Reasons")
end = text.find("\n## ", at + 1)
rows = text[at:end].splitlines()
return [(r.split("`")[1], r.split("|")[2].strip()) for r in rows if r.startswith("| `")]
def outcomes_table():
"""§6.3's outcomes, in order."""
return table_rows(text_of(EXTENSION), "### 6.3 Lines")
def refinement_only():
"""SPEC §5, *Refinement capabilities*: the capabilities that are refinement-only."""
return set(table_rows(text_of(SPEC), "### Refinement capabilities (staged delivery)"))
REFINEMENT_ONLY = refinement_only()
def need(ok, reason):
"""jwp-receipts §3.1 step 6."""
if not ok:
raise CV.Reject(6, reason)
# ------------------------------------------------------------------ what the verifier holds
# Steps 1 to 4 of a claim, a group's steps 1 and 2 with its objects', and a record's reading are
# pure in their bytes: each is computed once and kept, which nothing a weakened run drops reads.
_CLAIMS, _GROUPS, _RECORDS = {}, {}, {}
def claim_steps(wire):
"""jwp-receipts §3.1 steps 1 to 4 (claim_verify.py). Returns the claim; raises CV.Reject."""
if wire not in _CLAIMS:
try:
_CLAIMS[wire] = CV.verify_claim(wire)
except CV.Reject as r:
_CLAIMS[wire] = r
v = _CLAIMS[wire]
if isinstance(v, CV.Reject):
raise v
return v
def read_record(b):
"""holdings §4.4 (holdings_verify.py). Returns the record; raises HV.Refused."""
if b not in _RECORDS:
try:
_RECORDS[b] = HV.read(b)
except HV.Refused as r:
_RECORDS[b] = r
v = _RECORDS[b]
if isinstance(v, HV.Refused):
raise v
return v
class Group:
"""A group given, after jwp-receipts §3 steps 1 and 2, with its object receipts and envelopes."""
def __init__(self, item):
self.receipt = JV.read_group(item["group"])
JV.verify_group(self.receipt)
self.link = JV.link(self.receipt).hex()
self.count = self.receipt["leaf_count"]
self.signer = self.receipt["signer_pubkey"]
self.start = JV.seconds(self.receipt["group_start"])
self.verdicts = [] # bearer-receipts §6.2 step 2, one for each object receipt given
self.pairs = [] # [(object receipt, envelope or None)] for each that reads
self.included = {} # position -> content hash of each object receipt that passes step 5
self.envelopes = {} # content hash -> envelope bytes held
for o in item["objects"]:
try:
ob = JV.read_object(o["receipt"])
except JV.Refuse as r:
self.verdicts.append("reject reason=" + r.reason)
continue
env = None if o["envelope"] is None else bytes.fromhex(o["envelope"])
self.pairs.append((ob, env))
try:
if env is None:
JV.inclusion(ob, self.receipt)
self.verdicts.append("inclusion-verified")
else:
JV.accept(ob, self.receipt, env)
self.verdicts.append("delivered-verified")
except JV.Refuse as r:
self.verdicts.append("reject reason=" + r.reason)
try:
JV.inclusion(ob, self.receipt)
self.included[ob["object_id"]] = ob["content_hash"].hex()
except JV.Refuse:
pass
if env is not None:
self.envelopes[JV.blake3(JV.OBJECT + env).hex()] = env
def read_groups(items):
"""§6.2 step 1: each group under jwp-receipts §3 steps 1 and 2, and each of its object receipts
as bearer-receipts §6.2 step 2 checks it. Raises JV.Refuse for a group that fails."""
out = []
for item in items:
key = json.dumps(item, sort_keys=True)
if key not in _GROUPS:
try:
_GROUPS[key] = Group(item)
except JV.Refuse as r:
_GROUPS[key] = r
g = _GROUPS[key]
if isinstance(g, JV.Refuse):
raise g
out.append(g)
return out
def manifest(env):
"""A WAI1 envelope's manifest (SPEC §2, §3), or None for bytes that are not one."""
try:
m, _, _ = SV.read_wai1(env)
except SV.NotWai1:
return None
return m
def rungs(entry):
"""§4.1: an entry's ladder, in its order: none for `null`, the one object, or the array's."""
fb = entry["fallback"]
return [] if fb is None else fb if isinstance(fb, list) else [fb]
def checked_rungs(entry, check):
"""The rungs §4.3 reads, every one; a weakened run reads only the first."""
return rungs(entry)[:1] if drops(check) else rungs(entry)
def is_layer(env):
"""§4.3 step 6: a WAI1 whose capability is refinement-only (SPEC §5)."""
m = manifest(env)
return m is not None and m["model_requirement"]["capability"] in REFINEMENT_ONLY
def derived_from(env, motif):
"""§4.3 step 8: a WAI1 whose manifest carries `derived_from` (SPEC §7.1, *Derived renditions*:
its four members in their forms) with `motif` the primary's content hash. A WAI2 is none."""
m = manifest(env)
df = None if m is None else m.get("derived_from")
return (isinstance(df, dict) and all(isinstance(df.get(k), str) and df[k] for k in
("motif", "motif_capability", "renderer", "settings"))
and CV.is_hex(df["motif"], 32) and CV.is_hex(df["settings"], 32) and df["motif"] == motif)
# ------------------------------------------------------------------ §2: the held-at statement
def before_group(word_s, start):
"""§2: a held-at statement is before its group when the whole seconds of the time its word
states, any fraction dropped (`word_s`), are below the seconds jwp-receipts §2.4.3 reads from
the group's `group_start` (`start`, None where it is not a date-time, which nothing is before).
§4.3 step 4 reads an entry's `from_ms` by the same rule."""
if start is None:
return False
return word_s <= start if drops("same_second") else word_s < start
# ------------------------------------------------------------------ §4.3: step 6
def check_timeliness(c, g, groups):
"""§4.3 against the group `g` the claim is checked against, which passed jwp-receipts §3 steps
1 and 2, and every group given (`groups`, by link hash) with the object receipts and envelopes
held under it."""
need(g.start is not None, "context_invalid")
entries = c["body"]["objects"]
envelopes = {}
for x in groups.values():
envelopes.update(x.envelopes)
need(drops("subject") or g.link == c["subject"], "timeliness_subject_mismatch")
need(drops("signer") or g.signer == c["signer_pubkey"], "timeliness_signer_mismatch")
need(drops("range") or all(e["leaf_index"] < g.count for e in entries), "timeliness_leaf_out_of_range")
need(drops("before") or not any(before_group(e["from_ms"] // 1000, g.start) for e in entries),
"timeliness_before_group")
need(drops("content") or all(g.included.get(e["leaf_index"], e["content_hash"]) == e["content_hash"]
for e in entries), "timeliness_content_mismatch")
named = [e["content_hash"] for e in entries] + [f["content_hash"] for e in entries for f in checked_rungs(e, "rung_layer")]
need(drops("layer") or not any(h in envelopes and is_layer(envelopes[h]) for h in named), "timeliness_on_layer")
for f in (f for e in entries for f in checked_rungs(e, "rung_position")):
fg = groups.get(f["group"])
if fg is not None and not drops("fallback_position"):
need(f["leaf_index"] < fg.count and fg.included.get(f["leaf_index"], f["content_hash"]) == f["content_hash"],
"timeliness_fallback_mismatch")
for e in entries:
for f in checked_rungs(e, "rung_derived"):
if f["content_hash"] in envelopes and not drops("derived"):
need(derived_from(envelopes[f["content_hash"]], e["content_hash"]), "timeliness_fallback_not_derived")
# ------------------------------------------------------------------ §5: value over time
def value(entry, h):
"""§5.1: the entry's value at the instant h, floored."""
by = entry["by_ms"]
if h <= by:
return FULL
pts = [(by, FULL)] + [(p["at_ms"], p["value_ppm"]) for p in entry["decay"]]
for (ta, va), (tb, vb) in zip(pts, pts[1:]):
if ta < h <= tb:
num, den = va * (tb - h) + vb * (h - ta), tb - ta
return (2 * num + den) // (2 * den) if drops("floor") else num // den
return 0
def end(entry):
"""§5.2: the `at_ms` of its last point."""
return entry["decay"][-1]["at_ms"]
# ------------------------------------------------------------------ §6: judging delivery
def claims_step(wires, groups, plan):
"""§6.2 step 2, and §7.2's (`plan`): each claim under jwp-receipts §3.1 steps 1 to 5, each
signer's previous claim the last of its claims held before it, then step 6 against the group
whose link hash is its subject, or the first group. Returns each claim's label, and each
claim accepted (None for one refused)."""
by_link = {}
for g in groups:
by_link.setdefault(g.link, g)
held, labels, accepted = {}, [], []
for wire in wires:
try:
c = claim_steps(wire)
except CV.Reject as r:
labels.append(f"reject step={r.step} reason={r.reason}")
accepted.append(None)
continue
previous = held.get(c["signer_pubkey"])
held[c["signer_pubkey"]] = c
try:
CV.check_chain(c, previous)
g = by_link.get(c["subject"], groups[0] if groups else None)
if c["class"] == "timeliness":
need(g is not None, "timeliness_subject_mismatch")
check_timeliness(c, g, by_link)
elif plan and c["class"] == "transmission" and g is not None:
KV.check_transmission(c, g.receipt)
elif plan and c["class"] == "schedule" and g is not None:
KV.check_schedule(c, g.receipt, g.pairs)
elif not drops("context"):
raise CV.Reject(6, "context_mismatch")
except CV.Reject as r:
labels.append(f"reject step={r.step} reason={r.reason}")
accepted.append(None)
continue
labels.append("accepted")
accepted.append(c)
return labels, accepted
def intents_step(accepted, groups):
"""§6.2 step 3: the entries of the accepted claims by group and `leaf_index`, in the order of
the groups given and then of `leaf_index`: [(group, leaf_index, entry, or None when set
aside)]."""
stated = {}
for c in accepted:
if c is not None and c["class"] == "timeliness":
for e in c["body"]["objects"]:
stated.setdefault((c["subject"], e["leaf_index"]), []).append(e)
out, seen = [], set()
for g in groups:
if g.link in seen:
continue
seen.add(g.link)
for i in sorted(i for link, i in stated if link == g.link):
es = stated[(g.link, i)]
stands = drops("intent_contradiction") or all(e == es[0] for e in es)
out.append((g.link, i, es[0] if stands else None))
return out
def records_step(records, groups):
"""§6.2 step 4: each record as holdings §4.4 reads and verifies it, refused whole when its
entry for a group given misstates the leaf count or is before the group, two of one holder at
one `as_of_ms` that differ set aside. Returns each record's label and the records that are
not refused (index -> record)."""
by_link = {g.link: g for g in groups}
labels, kept = [], {}
for k, hexed in enumerate(records):
try:
rec = read_record(bytes.fromhex(hexed))
mine = [(by_link[g.hex()], n) for g, n, _ in rec[3] if g.hex() in by_link]
if not drops("leaf_count") and any(g.count != n for g, n in mine):
raise HV.Refused("holdings_leaf_count_mismatch")
if not drops("held_before") and any(before_group(rec[1] // 1000, g.start) for g, _ in mine):
raise HV.Refused("held_before_group")
kept[k] = rec
labels.append("stands")
except HV.Refused as r:
labels.append("reject reason=" + r.reason)
at = {}
for k, rec in kept.items():
at.setdefault((rec[0], rec[1]), []).append(k)
for ks in at.values():
if len({HV.digest(kept[k]) for k in ks}) > 1 and not drops("record_contradiction"):
for k in ks:
labels[k] = "set-aside reason=holdings_contradicted"
return labels, kept
def held_from_records(standing, group, index):
"""§6.2 step 5: the held-at statements the records that stand give for object `index` of
`group` (a link hash in hexadecimal): (holder, group, index, instant) for each record that holds
it (holdings §5.4), its instant the record's `as_of_ms`."""
g = bytes.fromhex(group)
return [(rec[0].hex(), group, index, rec[1]) for rec in standing if HV.holds(rec, g, index) is None]
def has_entry(rec, group):
return any(g.hex() == group for g, _, _ in rec[3])
def line_holders(group, entry, standing):
"""§6.3: the holders with a record that stands and has an entry for the intent's group or a
rung's group, in the order of their first such record."""
rs = checked_rungs(entry, "rung_holders")
out = []
for rec in standing:
h = rec[0].hex()
if h not in out and (drops("line_holders") or has_entry(rec, group)
or any(has_entry(rec, r["group"]) for r in rs)):
out.append(h)
return out
def grade(entry, p, fs):
"""§6.3's table, the first outcome that holds, for a holder whose least instants for the
primary and for each rung of the ladder are p and fs (None where none)."""
by = entry["by_ms"]
if p is not None and (p < by if drops("deadline_inclusive") else p <= by):
return "on_time", FULL
# The first rung, in the ladder's order, held by the deadline.
in_time = [r for r, f in zip(rungs(entry), fs) if f is not None and (f <= by or drops("fallback_deadline"))]
if in_time:
r = in_time[-1] if drops("rung_order") else in_time[0]
# §5.3: what arrived after the deadline grades nothing, so a later primary of more value
# does not displace a rung held by then.
if not (drops("sink_rule") and p is not None and value(entry, p) > r["value_ppm"]):
return "replaced", r["value_ppm"]
if p is not None:
return "after_deadline", value(entry, p)
return "not_shown", 0
def lines(entry, holders, primary, fallback):
"""§6.3: an intent's line for each of `holders` (keys in hexadecimal), in increasing order of
key bytes, from that holder's own held-at statements for its primary and for the rungs of its
fallback, each statement naming its object. *f* is one instant for a ladder of one rung, and
the list of them for an array."""
def least(statements, holder, latest=False):
instants = [s[3] for s in statements if drops("own_statements") or s[0] == holder]
if not instants:
return None
return max(instants) if latest or drops("least") else min(instants)
out = []
for h in (holders if drops("key_order") else sorted(holders, key=bytes.fromhex)):
p = least(primary, h)
fs = [least([s for s in fallback if (s[1], s[2]) == (r["group"], r["leaf_index"])], h, drops("least_fallback"))
for r in rungs(entry)]
outcome, v = grade(entry, p, fs)
f = fs if isinstance(entry["fallback"], list) else (fs[0] if fs else None)
out.append({"holder": h, "held_ms": p, "fallback_ms": f, "outcome": outcome, "value_ppm": v})
return out
def intent_lines(link, entry, standing):
primary = held_from_records(standing, link, entry["leaf_index"])
fallback = [s for r in rungs(entry) for s in held_from_records(standing, r["group"], r["leaf_index"])]
return lines(entry, line_holders(link, entry, standing), primary, fallback)
def verdict(case):
"""§6: the verdict, or `refuse reason=R` for a group that fails."""
try:
groups = read_groups(case["groups"])
except JV.Refuse as r:
return "refuse reason=" + r.reason
labels, accepted = claims_step(case["claims"], groups, plan=False)
intents = intents_step(accepted, groups)
rlabels, kept = records_step(case["records"], groups)
standing = [kept[k] for k in sorted(kept) if rlabels[k] == "stands"]
out, found = [], {}
for link, i, entry in intents:
ls = [] if entry is None else intent_lines(link, entry, standing)
found[(link, i)] = (entry, ls)
out.append({"group": link, "leaf_index": i,
"intent": "stands" if entry is not None else "set-aside reason=timeliness_contradicted",
"end_ms": None if entry is None else end(entry), "lines": ls})
statement = None
s = case["statement"]
if s is not None:
entry, ls = found.get((s["group"], s["index"]), (None, []))
if entry is None and not drops("no_intent"):
statement = "reject reason=deadline_not_stated"
else:
line = next((x for x in ls if x["holder"] == s["holder"]), None)
statement = ({"outcome": "not_shown", "value_ppm": 0} if line is None
else {"outcome": line["outcome"], "value_ppm": line["value_ppm"]})
return {"objects": [g.verdicts for g in groups], "claims": labels, "records": rlabels, "intents": out,
"statement": statement}
# ------------------------------------------------------------------ §7: planning
def sources_valid(sources, loss, h):
"""§7.1: the sources, each exactly a bearer instance, a holder or the origin."""
if not isinstance(sources, list) or (not sources and not drops("sources_empty")):
return False
bearers, holders, origins = set(), set(), 0
for s in sources:
if not isinstance(s, dict):
return False
kind = s.get("kind")
fetch = s.get("fetch_ms")
if kind != "bearer" and not (drops("sources_fetch") or (CV.is_int(fetch) and fetch >= 1)):
return False
if kind == "bearer":
if set(s) != {"kind", "bearer_id"} or not (drops("sources_uri") or CV.is_uri(s["bearer_id"])):
return False
if s["bearer_id"] in bearers and not drops("sources_twice"):
return False
bearers.add(s["bearer_id"])
elif kind == "holder":
if set(s) != {"kind", "holder", "fetch_ms"} or not (drops("sources_key") or CV.is_hex(s["holder"], 32)):
return False
if s["holder"] in holders and not drops("sources_twice"):
return False
holders.add(s["holder"])
elif kind == "origin":
if set(s) != {"kind", "fetch_ms"}:
return False
origins += 1
elif not drops("sources_kind"):
return False
if origins > 1 and not drops("sources_twice"):
return False
return not (bearers and (loss is None or h is None)) or drops("sources_loss")
def plan_inputs(loss, h, t, sources):
"""§7.1's checks, in their order: the reason the plan is refused, or None."""
if loss is not None and not drops("loss_bound") and not (CV.is_int(loss) and loss <= 999_999):
return "loss_bound_invalid"
if h is not None and not drops("overhead") and not (CV.is_int(h) and h <= 2):
return "overhead_invalid"
if t != "from_ms" and not drops("instant") and not (CV.is_int(t) and t <= LAST_INSTANT):
return "instant_invalid"
if not sources_valid(sources, loss, h):
return "sources_invalid"
return None
def schedules_step(labels, accepted):
"""§7.2: the declarations (bearer-receipts §6.2 step 4) and the schedules (carousel §6.2 step
4), each of its group. Relabels the claims; returns the schedules that stand, by (group,
bearer_id)."""
def key(c):
return c["body"]["bearer_id"] if drops("per_group") else (c["subject"], c["body"]["bearer_id"])
def by_instance(cls):
out = {}
for i, c in enumerate(accepted):
if c is not None and c["class"] == cls:
out.setdefault(key(c), []).append(i)
return out
declared, contradicted = {}, set()
for k, idx in by_instance("transmission").items():
bodies = [accepted[i]["body"] for i in idx]
same = all(b == bodies[0] for b in bodies)
if same:
declared[k] = bodies[0]["bearer"]
else:
contradicted.add(k)
for i in idx:
labels[i] = "declared" if same else "set-aside reason=bearer_contradicted"
schedules = {}
for k, idx in by_instance("schedule").items():
bodies = [accepted[i]["body"] for i in idx]
if any(b != bodies[0] for b in bodies):
aside = "schedule_contradicted"
elif k in contradicted:
aside = "bearer_contradicted"
elif k not in declared and not drops("undeclared"):
aside = "bearer_not_declared"
elif declared.get(k) == "bidirectional" and not drops("bidirectional"):
aside = "schedule_on_bidirectional"
else:
aside = None
for i in idx:
labels[i] = "stands" if aside is None else "set-aside reason=" + aside
if aside is None:
schedules[(accepted[i]["subject"], accepted[i]["body"]["bearer_id"])] = bodies[0]
return schedules
def standing_at(kept, rlabels, s):
"""§7.2: the records that stand at s, by holdings §5.1 to §5.3 over those §6.2 step 4 did not
refuse, and not set aside there."""
live = [rec for k, rec in kept.items() if drops("record_at_s") or rec[1] <= s < rec[2]]
out = []
for holder in {rec[0] for rec in live}:
mine = [rec for rec in live if rec[0] == holder]
latest = max(rec[1] for rec in mine)
top = [rec for rec in mine if rec[1] == latest]
if len({HV.digest(rec) for rec in top}) == 1:
out.append(top[0])
aside = {HV.digest(kept[k]) for k in kept if rlabels[k] != "stands"}
return [rec for rec in out if HV.digest(rec) not in aside]
def arrival(src, group, index, s, ctx):
"""§7.2: a source's arrival of object `index` of `group` planned from s, or None."""
kind = src["kind"]
if kind == "origin":
return s + src["fetch_ms"]
if kind == "holder":
recs = standing_at(ctx["kept"], ctx["rlabels"], s)
held = any((drops("holder_record") or rec[0].hex() == src["holder"])
and HV.holds(rec, bytes.fromhex(group), index) is None for rec in recs)
return s + src["fetch_ms"] if held else None
if kind != "bearer":
return None
body = ctx["schedules"].get((group, src["bearer_id"]))
e = None if body is None else next((x for x in body["objects"] if x["leaf_index"] == index), None)
if e is None:
return None
blocks = sum(n for _, n in CV.schedule_blocks(e))
p = KV.plan({"entry": e, "loss_ppm": ctx["loss"], "overhead": ctx["h"], "at_ms": s, "held": [0] * blocks})
return p["complete_ms"]
def choose(entry, primary, ladder):
"""§7.3 for an object whose intent stands, from each source's arrivals of the primary and,
for each rung of the ladder in its order, of that rung."""
by = entry["by_ms"]
def choice(outcome, obj, r, i, arrived, v):
return {"outcome": outcome, "object": obj, "rung": r, "source": i, "arrival_ms": arrived, "value_ppm": v}
timely = [i for i, a in enumerate(primary) if a is not None and a <= by]
if timely:
i = min(timely, key=lambda j: (primary[j], j)) if drops("plan_order") else timely[0]
return choice("on_time", "primary", None, i, primary[i], FULL)
late = [(value(entry, a), i) for i, a in enumerate(primary) if a is not None]
best = max((v for v, _ in late), default=0)
# Rule 2: the first rung of at least the best late value that arrives by the deadline, from the
# first such source in the asker's order.
order = list(enumerate(rungs(entry)))
if drops("plan_rung_first"):
order = order[:1]
if drops("plan_rung_order"):
order.reverse()
for r, fb in order:
at_least = fb["value_ppm"] > best if drops("fallback_tie") else fb["value_ppm"] >= best
if not (at_least or drops("fallback_value")):
continue
arrivals = ladder[r]
in_time = [i for i, a in enumerate(arrivals)
if a is not None and (a < by if drops("plan_fallback_deadline") else a <= by)]
if in_time and drops("fallback_order"):
in_time.sort(key=lambda j: (arrivals[j], j))
if in_time:
return choice("replaced", "fallback", r, in_time[0], arrivals[in_time[0]], fb["value_ppm"])
if late and (best >= 1 or drops("late_zero")):
ties = [i for v, i in late if v == best]
i = ties[-1] if drops("late_tie") else ties[0]
return choice("after_deadline", "primary", None, i, primary[i], best)
return choice("no_source", None, None, None, None, 0)
def plan(case):
"""§7: the plan, or `refuse reason=R`."""
loss, h, t, sources = case["loss_ppm"], case["overhead"], case["at_ms"], case["sources"]
bad = plan_inputs(loss, h, t, sources)
if bad:
return "refuse reason=" + bad
try:
groups = read_groups(case["groups"])
except JV.Refuse as r:
return "refuse reason=" + r.reason
labels, accepted = claims_step(case["claims"], groups, plan=True)
intents = {(link, i): entry for link, i, entry in intents_step(accepted, groups)}
rlabels, kept = records_step(case["records"], groups)
ctx = {"kept": kept, "rlabels": rlabels, "schedules": schedules_step(labels, accepted), "loss": loss, "h": h}
choices = []
for w in case["wanted"]:
entry = intents.get((w["group"], w["index"]))
if entry is None:
choices.append("reject reason=deadline_not_stated")
continue
s = entry["from_ms"] if t == "from_ms" else t
primary = [arrival(src, w["group"], w["index"], s, ctx) for src in sources]
ladder = [[arrival(src, r["group"], r["leaf_index"], s, ctx) for src in sources] for r in rungs(entry)]
choices.append(choose(entry, primary, ladder))
return {"claims": labels, "records": rlabels, "choices": choices}
# ------------------------------------------------------------------ §7.5: running a plan
def run_inputs(case):
"""§7.5.1's checks, in their order: the reason the run is refused, or None."""
plans = case["plans"]
if not plans and not drops("run_plans_none"):
return "plans_invalid"
if any(p["at_ms"] == "from_ms" for p in plans) and not drops("run_plans_from_ms"):
return "plans_invalid"
instants = [p["at_ms"] for p in plans]
if not drops("run_plans_order") and any(a >= b for a, b in zip(instants, instants[1:])):
return "plans_invalid"
if not drops("run_plans_twice"):
for p in plans:
objects = [(w["group"], w["index"]) for w in p["wanted"]]
if len(set(objects)) != len(objects):
return "plans_invalid"
for p in plans:
bad = plan_inputs(case["loss_ppm"], case["overhead"], p["at_ms"], p["sources"])
if bad:
return bad
arrivals = case["arrivals"]
if not drops("run_arrivals_last") and any(not CV.is_int(a["at_ms"]) or a["at_ms"] > LAST_INSTANT for a in arrivals):
return "arrivals_invalid"
if not drops("run_arrivals_order") and any(a["at_ms"] > b["at_ms"] for a, b in zip(arrivals, arrivals[1:])):
return "arrivals_invalid"
if not drops("run_arrivals_twice") and len({(a["group"], a["index"]) for a in arrivals}) != len(arrivals):
return "arrivals_invalid"
return None
def same_source(a, b):
"""§7.5.2: both name one bearer instance or one holder's key, or both are the origin, whatever
their `fetch_ms`."""
if a["kind"] != b["kind"]:
return False
if drops("run_same_fetch_ms") and a.get("fetch_ms") != b.get("fetch_ms"):
return False
return a.get("bearer_id") == b.get("bearer_id") and a.get("holder") == b.get("holder")
class Fetch:
"""§7.5.2: the object a fetch takes, its source, and the plan it serves."""
def __init__(self, obj, rung, takes, plan, index, source):
self.obj, self.rung, self.takes, self.plan, self.index, self.source = obj, rung, takes, plan, index, source
def takes(key, entry, choice):
"""The object a choice takes: the primary, or the rung it names."""
if choice["object"] == "primary":
return key
r = rungs(entry)[choice["rung"]]
return (r["group"], r["leaf_index"])
def run(case):
"""§7.5: the run of a sink's plans against its arrivals, or `refuse reason=R`."""
bad = run_inputs(case)
if bad:
return "refuse reason=" + bad
# Each plan derived by §7.2 and §7.3 from the shared inputs with its own instant, sources and
# wanted objects; a group that fails refuses the run as it refuses a plan.
plans = []
for p in case["plans"]:
derived = plan(dict(case, at_ms=p["at_ms"], sources=p["sources"], wanted=p["wanted"]))
if isinstance(derived, str):
return derived
plans.append(derived["choices"])
groups = read_groups(case["groups"])
_, accepted = claims_step(case["claims"], groups, plan=True)
intents = {(link, i): entry for link, i, entry in intents_step(accepted, groups)}
def objects_run(k):
"""§7.5.2: what plan k wants whose choice is not refused, in its wanted order, each with its
choice and its entry."""
return [((w["group"], w["index"]), c, intents[(w["group"], w["index"])])
for w, c in zip(case["plans"][k]["wanted"], plans[k]) if isinstance(c, dict)]
instants = {p["at_ms"] for p in case["plans"]} | {a["at_ms"] for a in case["arrivals"]}
for k in range(len(plans)):
for _, _, e in objects_run(k):
instants |= {e["by_ms"], end(e)}
held, settled, passed, fetches, force, out = {}, set(), set(), {}, None, []
def decide(n, k, action, key, obj=None, rung=None, source=None, v=None):
out.append({"at_ms": n, "plan": k, "action": action, "group": key[0], "index": key[1],
"object": obj, "rung": rung, "source": source, "value_ppm": v})
def abandon(n, key):
f = fetches.pop(key)
decide(n, f.plan, "abandon", key, f.obj, f.rung, f.index)
for n in sorted(instants):
# Step 1.
for a in case["arrivals"]:
if a["at_ms"] == n:
arrived = (a["group"], a["index"])
held[arrived] = n
if not drops("run_arrival_completes"):
for key in [key for key, f in fetches.items() if f.takes == arrived]:
del fetches[key]
# Step 2.
new = next((k for k, p in enumerate(case["plans"]) if p["at_ms"] == n), None)
if new is not None:
sources = case["plans"][new]["sources"]
choices = {key: (c, e) for key, c, e in objects_run(new)}
def continues(key, f):
c, e = choices.get(key, (None, None))
if c is None or c["object"] is None or drops("run_continue"):
return False
return takes(key, e, c) == f.takes and same_source(sources[c["source"]], f.source)
if force is not None:
for key, _, _ in objects_run(force):
if key not in settled and key in fetches and not continues(key, fetches[key]):
if drops("run_drop_abandon") and key not in choices:
continue
abandon(n, key)
force = new
for key, c, e in objects_run(new):
if (key in settled and not drops("run_settled_refetch")) or c["object"] is None:
continue
if takes(key, e, c) in held and not drops("run_held_fetch"):
continue
f = fetches.get(key)
if f is not None and continues(key, f):
if not drops("run_continued_plan"):
f.plan, f.index = new, c["source"]
continue
fetches[key] = Fetch(c["object"], c["rung"], takes(key, e, c), new, c["source"], sources[c["source"]])
decide(n, new, "fetch", key, c["object"], c["rung"], c["source"])
if force is None:
continue
current = objects_run(force)
# Step 3, by what the sink holds by `by_ms`.
for key, _, e in current:
if key in settled or key in passed or e["by_ms"] > n:
continue
limit = n if drops("run_by_deadline") else e["by_ms"]
holds = [(r, f) for r, f in enumerate(rungs(e))
if held.get((f["group"], f["leaf_index"]), limit + 1) <= limit]
if held.get(key, limit + 1) <= limit:
settled.add(key)
decide(n, force, "present", key, "primary", v=FULL)
elif holds:
r, f = holds[-1] if drops("run_rung_order") else holds[0]
settled.add(key)
decide(n, force, "present", key, "fallback", r, v=f["value_ppm"])
else:
passed.add(key)
decide(n, force, "passed", key)
if key in fetches:
after_present = key in settled and not drops("run_present_abandon")
after_passed = key in passed and fetches[key].obj == "fallback" and not drops("run_passed_abandon")
if after_present or after_passed:
abandon(n, key)
# Step 4, at the primary's value at its arrival.
for key, _, e in current:
if key in passed and key not in settled and key in held:
v = value(e, n if drops("run_late_value") else held[key])
if v >= 1 or drops("run_late_zero"):
settled.add(key)
decide(n, force, "present", key, "primary", v=v)
# Step 5.
for key, _, e in current:
if key in passed and key not in settled and end(e) <= n:
if key in fetches and not drops("run_end_abandon"):
abandon(n, key)
settled.add(key)
if not drops("run_ended"):
decide(n, force, "ended", key)
return {"plans": plans, "decisions": out}
# ------------------------------------------------------------------ the corpus
def claim_outcome(c):
try:
claim_steps(c["claim"])
return "accept"
except CV.Reject as r:
return f"reject step={r.step} reason={r.reason}"
def value_outcome(c):
"""§5 for an entry read alone, as §3.1 steps 2 and 4 read an entry."""
e = c["entry"]
CV.timeliness_entry_body(e)
CV.timeliness_entry_rules(e, None)
return {"value_ppm": value(e, c["at_ms"]), "end_ms": end(e)}
RUNS = (("claims", claim_outcome), ("values", value_outcome), ("verdicts", verdict), ("plans", plan), ("runs", run))
KINDS = tuple(k for k, _ in RUNS)
def strings(v, out):
if isinstance(v, str):
out.append(v)
elif isinstance(v, list):
for x in v:
strings(x, out)
elif isinstance(v, dict):
for x in v.values():
strings(x, out)
def check(doc, quiet=False):
"""The failures of a corpus document: each case's outcome, every reason of §9, every outcome of
§6.3 and every action of §7.5.3."""
fails = []
for kind, run in RUNS:
for c in doc[kind]:
try:
got = run(c)
except CV.Reject as r:
got = {"error": f"reject step={r.step} reason={r.reason}"}
except (KeyError, TypeError, ValueError, IndexError, AttributeError, StopIteration,
ZeroDivisionError) as e:
got = {"error": repr(e)}
if got != c["outcome"]:
fails.append(c["name"])
if not quiet:
print(f"FAIL {c['name']}\n stated {json.dumps(c['outcome'])}\n got {json.dumps(got)}")
stated = []
for kind in KINDS:
for c in doc[kind]:
strings(c["outcome"], stated)
for code, _ in reasons_table():
if not any(s == code or s.endswith("reason=" + code) for s in stated):
fails.append("no case gives " + code)
for outcome in outcomes_table():
if outcome not in stated:
fails.append("no case gives the outcome " + outcome)
for action in ACTIONS:
if action not in stated:
fails.append("no case gives the action " + action)
return fails
def mutations(doc):
"""Copies of the corpus, one defect each, every one of which must fail."""
def case(d, name):
return next(c for k in KINDS for c in d[k] if c["name"] == name)
def m_instant(d):
# A millisecond before the end is 75; at the end, 0.
case(d, "example_last_ms")["at_ms"] += 1
def m_end(d):
case(d, "hard_one_ms")["outcome"]["end_ms"] -= 1
def m_floor(d):
case(d, "step_mid")["outcome"]["value_ppm"] += 1
def m_accept(d):
case(d, "decay_near_collinear")["outcome"] = "reject step=4 reason=timeliness_decay_invalid"
def m_line(d):
case(d, "holder_one_ms_after")["outcome"]["intents"][0]["lines"][0]["value_ppm"] = 999_801
def m_earliest(d):
c = case(d, "earliest_record_counts")
c["records"] = c["records"][1:]
def m_statement(d):
case(d, "statement_line")["statement"]["holder"] = d["keys"]["holder_c"]
def m_envelope(d):
c = case(d, "on_layer")
c["groups"][0]["objects"][3]["envelope"] = None
def m_order(d):
c = case(d, "order_over_earliest")
c["sources"] = list(reversed(c["sources"]))
def m_deadline(d):
case(d, "on_time_at_deadline")["at_ms"] += 1
def m_fetch(d):
# Both late sources at 300,000: the first in order, the origin.
case(d, "late_choice_by_value")["sources"][1]["fetch_ms"] = 66_000
def m_loss(d):
# 70%: 3 symbols a pass of the 12 needed, a fourth pass.
case(d, "every_pass_from_first")["loss_ppm"] = 700_000
def m_from_ms(d):
case(d, "publisher_check_fails")["at_ms"] = d["plans"][0]["at_ms"]
def m_object(d):
case(d, "fallback_from_carousel")["outcome"]["choices"][0]["object"] = "primary"
def m_reason_gone(d):
d["verdicts"].remove(case(d, "record_before_group"))
def m_rung_value(d):
# The lower rung presented at the higher's value.
case(d, "ladder_lower_rung_alone")["outcome"]["intents"][0]["lines"][0]["value_ppm"] = 700_000
def m_rung_instant(d):
# The lower rung a millisecond late: nothing held by the deadline.
r = case(d, "ladder_lower_rung_alone")
r["outcome"]["intents"][0]["lines"][0]["fallback_ms"][1] += 1
def m_rung_form(d):
# A ladder's line reports its list.
r = case(d, "ladder_first_rung_replaces")
r["outcome"]["intents"][0]["lines"][0]["fallback_ms"] = r["outcome"]["intents"][0]["lines"][0]["fallback_ms"][0]
def m_plan_rung(d):
case(d, "ladder_lower_rung")["outcome"]["choices"][0]["rung"] = 0
def m_arrival_late(d):
# The trace a millisecond later is a late object at another value.
case(d, "run_late_object")["arrivals"][0]["at_ms"] += 1
def m_arrival_gone(d):
case(d, "run_on_time")["arrivals"] = []
def m_decision_value(d):
case(d, "run_late_object")["outcome"]["decisions"][-1]["value_ppm"] += 1
def m_decision_action(d):
case(d, "run_every_rung_passed_no_primary")["outcome"]["decisions"][1]["action"] = "ended"
def m_plan_dropped(d):
c = case(d, "run_replaced_changes_source")
c["plans"] = c["plans"][:1]
def m_plan_instant(d):
case(d, "run_replaced_at_deadline")["plans"][1]["at_ms"] += 1
def m_decision_order(d):
c = case(d, "run_in_wanted_order")
c["outcome"]["decisions"][0], c["outcome"]["decisions"][1] = c["outcome"]["decisions"][1], c["outcome"]["decisions"][0]
def m_reason_gone_run(d):
d["runs"] = [c for c in d["runs"] if c["outcome"] != "refuse reason=arrivals_invalid"]
return [m_instant, m_end, m_floor, m_accept, m_line, m_earliest, m_statement, m_envelope, m_order, m_deadline,
m_fetch, m_loss, m_from_ms, m_object, m_reason_gone, m_rung_value, m_rung_instant, m_rung_form, m_plan_rung,
m_arrival_late, m_arrival_gone, m_decision_value, m_decision_action, m_plan_dropped, m_plan_instant,
m_decision_order, m_reason_gone_run]
WEAKENINGS = ["subject", "signer", "range", "before", "same_second", "content", "layer", "fallback_position", "derived",
"context", "intent_contradiction", "leaf_count", "held_before", "record_contradiction", "line_holders",
"least", "least_fallback", "own_statements", "key_order", "deadline_inclusive", "fallback_deadline",
"sink_rule", "floor", "no_intent", "loss_bound",
"overhead", "instant", "sources_empty", "sources_kind", "sources_uri", "sources_key", "sources_fetch",
"sources_twice", "sources_loss", "per_group", "undeclared", "bidirectional", "record_at_s",
"holder_record", "plan_order", "fallback_value", "fallback_tie", "fallback_order",
"plan_fallback_deadline", "late_zero", "late_tie",
"rung_layer", "rung_position", "rung_derived", "rung_holders", "rung_order",
"plan_rung_first", "plan_rung_order",
"run_plans_none", "run_plans_from_ms", "run_plans_order", "run_plans_twice", "run_arrivals_last",
"run_arrivals_order", "run_arrivals_twice", "run_same_fetch_ms", "run_arrival_completes", "run_continue",
"run_drop_abandon", "run_settled_refetch", "run_held_fetch", "run_continued_plan", "run_by_deadline",
"run_rung_order", "run_present_abandon", "run_passed_abandon", "run_late_value", "run_late_zero",
"run_end_abandon", "run_ended"]
def direct_checks():
"""What no vector carries, and what this text states of the texts beside it."""
fails = []
# §7.1: the greatest fetch estimate is admitted; the corpus carries only the one past it.
if not sources_valid([{"kind": "origin", "fetch_ms": MAX_INT}], None, None):
fails.append("a fetch_ms of 2^53 - 1 is refused")
# §4.2 rule 3.2 where the products pass 2^64: three points on one line are refused, and a
# part per million off it is not. A float of 64 bits cannot tell them apart.
half = (LAST_INSTANT - 1) // 2
for v1, ok in ((500_000, False), (500_001, True)):
e = {"leaf_index": 0, "content_hash": "00" * 32, "from_ms": 0, "by_ms": 0, "fallback": None,
"decay": [{"at_ms": half, "value_ppm": v1}, {"at_ms": 2 * half, "value_ppm": 0}]}
try:
CV.timeliness_entry_rules(e, None)
got = True
except CV.Reject:
got = False
if got != ok:
fails.append(f"a point of {v1} at {half} is {'refused' if ok else 'accepted'}")
# §7.5.3 states each action a run decides.
decisions = text_of(EXTENSION)
decisions = decisions[decisions.index("\n#### 7.5.3 Decisions"):decisions.index("\n#### 7.5.4 ")]
for action in ACTIONS:
if f"`{action}`" not in decisions:
fails.append(f"§7.5.3 does not state {action}")
# jwp-receipts §3.1 lists §4.2's codes at step 4 and §4.3's at step 6; delivery-telemetry §5.1
# lists §8.6's switch reason.
jwp = text_of(JWP_RECEIPTS)
for code, where in reasons_table():
step = {"§4.2": 4, "§4.3": 6}.get(where.split(" ")[0])
if step is not None and f"| {step} | `{code}` |" not in jwp:
fails.append(f"jwp-receipts §3.1 does not list {code} at step {step}")
if "- `not-held-by-deadline`:" not in text_of(TELEMETRY):
fails.append("delivery-telemetry §5.1 does not list not-held-by-deadline")
if REFINEMENT_ONLY != {"wai.image.int_refine", "wai.video.int_refine", "wai.audio.int_refine",
"wai.splat.int_refine"}:
fails.append(f"SPEC §5's refinement capabilities read as {sorted(REFINEMENT_ONLY)}")
return fails
def main(argv):
global DROPPED
if len(argv) != 2:
print(__doc__)
return 2
doc = json.load(open(os.path.join(argv[1], "vectors.json"), encoding="utf-8"))
extra = sorted(set(os.listdir(argv[1])) - {"vectors.json", "README.md"})
fails = check(doc)
if extra:
fails.append(f"files the corpus does not hold: {extra}")
fails += direct_checks()
print(f"{sum(len(doc[k]) for k in KINDS)} cases ({', '.join(f'{len(doc[k])} {k}' for k in KINDS)}): "
f"{len(fails)} failure(s)")
caught = 0
for m in mutations(doc):
d = copy.deepcopy(doc)
m(d)
if check(d, quiet=True):
caught += 1
else:
fails.append(f"mutation {m.__name__} was not caught")
print(f"{caught} of {len(mutations(doc))} mutated copies fail")
weak = 0
for w in WEAKENINGS:
DROPPED = w
if check(doc, quiet=True):
weak += 1
else:
fails.append(f"a verifier without its {w} check passes the corpus")
DROPPED = None
print(f"{weak} of {len(WEAKENINGS)} weakened verifiers are caught")
for f in fails:
print("FAIL", f)
return 1 if fails else 0
if __name__ == "__main__":
sys.exit(main(sys.argv))