feat: validator, bundle builder and release checks for 0.1.0

- Add tools/validate.py: validates packages, and with --correction runs
  proposed corrections through the gate against a package without
  writing anything (spec/11 §11.4). The gate now refuses a proposal that
  fails the correction schema, rule correction.schema.
- tools/load.py validates every package before loading, and with
  --bundle --built-from builds a scienting-bundle/1 with the FTS5 search
  index and bundle_meta (spec/13 §13.5). Search rows are defined per
  unit (sentence, cell, equation outside a sentence, figure-text) with
  the parent kind as context.
- Roster keys carry a role, and a key with the role owner may revoke any
  receipt (spec/08 §8.7, spec/20); roster schema, store table and the
  fixture roster updated. The checker signs a trial revocation to prove
  the rule both ways.
- A qa: receipt raises the nodes it sampled except those in its errors
  (spec/08 §8.2); the example paper's fidelity counts become 27 F3,
  13 F2, 4 F1 and 1 F0.
- The destination chooses the keyed scheme, not the text policy:
  hmac-team between the team's machines, hkdf-pages outside the team
  (spec/05 §5.8, spec/12 §12.5).
- A TextQuoteSelector must repeat the anchor's plain quote and is absent
  for hashed or keyed anchors (spec/18 §18.3).
- Add fixtures/records/suspect.json, a scienting-suspect/1 record.
- Oracle: hashes are kept incrementally and checked after every
  correction against a full recomputation; load and write refuse unsafe
  manifest paths; Ed25519, HKDF and schema validation come from the
  cryptography and jsonschema packages; canonical JSON uses the built-in
  encoder with an exact RFC 8785 fallback.
- The tools need Python 3.14 with cryptography and jsonschema; the py310
  CI environment is dropped and the workflow runs the default only.
- Resolve every [CHECK] note in the docs, READMEs and context; add
  CONTRIBUTING.md; every tool answers --help and refuses unknown options.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
alexmandClaude committed 2026-10-14 18:58:00 -04:00
1 parent b357af84d8
commit 8c8b51cc3f
89 files changed
+1141 -513

No files matched your search

+25 -146
View File
@@ -1,13 +1,15 @@
"""A JSON Schema (draft 2020-12) validator for the subset of keywords the spec's schemas use.
"""JSON Schema (draft 2020-12) validation with the `jsonschema` package, and the lint of the keyword subset.
Non-normative.
The spec's schemas are restricted to these keywords (spec/22-conformance.md) so that any 2020-12 validator, and
this one, agree; CI also runs the `jsonschema` package when it is installed.
The spec's schemas are restricted to the keywords below (spec/22-conformance.md) so that every 2020-12
validator reads them alike; `lint` refuses any schema that leaves the subset.
"""
import json
import os
import re
from jsonschema import Draft202012Validator
from referencing import Registry, Resource
KEYWORDS = {
"$schema", "$id", "$ref", "$defs", "$comment", "title", "description", "examples", "default", "deprecated",
@@ -24,9 +26,25 @@ class SchemaError(Exception):
class Store:
"""The schemas of one folder, linted on load, with a compiled validator per schema."""
def __init__(self, root):
self.root = root
self.docs = {}
self._registry = None
self._validators = {}
def registry(self):
if self._registry is None:
names = sorted(f for f in os.listdir(self.root) if f.endswith(".schema.json"))
self._registry = Registry().with_resources(
[(self.load(f)["$id"], Resource.from_contents(self.load(f))) for f in names])
return self._registry
def validator(self, name):
if name not in self._validators:
self._validators[name] = Draft202012Validator(self.load(name), registry=self.registry())
return self._validators[name]
def load(self, name):
if name not in self.docs:
@@ -65,146 +83,7 @@ def lint(schema, where):
lint(s, "%s/%s/%d" % (where, k, i))
def _type_ok(t, x):
if t == "null":
return x is None
if t == "boolean":
return isinstance(x, bool)
if t == "integer":
return isinstance(x, int) and not isinstance(x, bool)
if t == "number":
return isinstance(x, (int, float)) and not isinstance(x, bool)
if t == "string":
return isinstance(x, str)
if t == "array":
return isinstance(x, list)
if t == "object":
return isinstance(x, dict)
raise SchemaError("unknown type " + t)
def _equal(a, b):
if isinstance(a, bool) or isinstance(b, bool):
return type(a) is type(b) and a == b
return a == b
def validate(store, name, instance):
"""Return a list of error strings; empty when the instance is valid."""
schema = store.load(name)
errors = []
_v(store, name, schema, instance, "", errors)
return errors
def _v(store, base, s, x, path, errors):
if s is True:
return
if s is False:
errors.append(path + ": not allowed")
return
if "$ref" in s:
b2, target = store.deref(base, s["$ref"])
_v(store, b2, target, x, path, errors)
if "type" in s:
ts = s["type"] if isinstance(s["type"], list) else [s["type"]]
if not any(_type_ok(t, x) for t in ts):
errors.append("%s: expected %s" % (path or "/", "/".join(ts)))
return
if "const" in s and not _equal(s["const"], x):
errors.append("%s: expected %r" % (path or "/", s["const"]))
if "enum" in s and not any(_equal(e, x) for e in s["enum"]):
errors.append("%s: %r is not one of %r" % (path or "/", x, s["enum"]))
if isinstance(x, str):
if "minLength" in s and len(x) < s["minLength"]:
errors.append("%s: shorter than %d" % (path, s["minLength"]))
if "maxLength" in s and len(x) > s["maxLength"]:
errors.append("%s: longer than %d" % (path, s["maxLength"]))
if "pattern" in s and not re.search(s["pattern"], x):
errors.append("%s: %r does not match %s" % (path, x, s["pattern"]))
if isinstance(x, (int, float)) and not isinstance(x, bool):
if "minimum" in s and x < s["minimum"]:
errors.append("%s: below %s" % (path, s["minimum"]))
if "maximum" in s and x > s["maximum"]:
errors.append("%s: above %s" % (path, s["maximum"]))
if "exclusiveMinimum" in s and x <= s["exclusiveMinimum"]:
errors.append("%s: not above %s" % (path, s["exclusiveMinimum"]))
if "exclusiveMaximum" in s and x >= s["exclusiveMaximum"]:
errors.append("%s: not below %s" % (path, s["exclusiveMaximum"]))
if isinstance(x, list):
if "minItems" in s and len(x) < s["minItems"]:
errors.append("%s: fewer than %d items" % (path, s["minItems"]))
if "maxItems" in s and len(x) > s["maxItems"]:
errors.append("%s: more than %d items" % (path, s["maxItems"]))
if s.get("uniqueItems"):
seen = [json.dumps(i, sort_keys=True) for i in x]
if len(seen) != len(set(seen)):
errors.append("%s: items are not unique" % path)
pre = s.get("prefixItems", [])
for i, item in enumerate(x):
if i < len(pre):
_v(store, base, pre[i], item, "%s/%d" % (path, i), errors)
elif "items" in s:
_v(store, base, s["items"], item, "%s/%d" % (path, i), errors)
if "contains" in s:
if not any(not _sub(store, base, s["contains"], i) for i in x):
errors.append("%s: no item matches `contains`" % path)
if isinstance(x, dict):
if "minProperties" in s and len(x) < s["minProperties"]:
errors.append("%s: fewer than %d properties" % (path, s["minProperties"]))
if "maxProperties" in s and len(x) > s["maxProperties"]:
errors.append("%s: more than %d properties" % (path, s["maxProperties"]))
for r in s.get("required", []):
if r not in x:
errors.append("%s: missing %s" % (path or "/", r))
for k, deps in s.get("dependentRequired", {}).items():
if k in x:
for d in deps:
if d not in x:
errors.append("%s: %s requires %s" % (path or "/", k, d))
props = s.get("properties", {})
pprops = s.get("patternProperties", {})
for k, v in x.items():
matched = False
if k in props:
matched = True
_v(store, base, props[k], v, path + "/" + k, errors)
for pat, ps in pprops.items():
if re.search(pat, k):
matched = True
_v(store, base, ps, v, path + "/" + k, errors)
if not matched and "additionalProperties" in s:
ap = s["additionalProperties"]
if ap is False:
errors.append("%s: unexpected property %s" % (path or "/", k))
else:
_v(store, base, ap, v, path + "/" + k, errors)
if "propertyNames" in s:
_v(store, base, s["propertyNames"], k, path + "/" + k, errors)
for sub in s.get("allOf", []):
_v(store, base, sub, x, path, errors)
if "anyOf" in s:
if all(_sub(store, base, sub, x) for sub in s["anyOf"]):
errors.append("%s: matches none of anyOf" % (path or "/"))
if "oneOf" in s:
ok = [sub for sub in s["oneOf"] if not _sub(store, base, sub, x)]
if len(ok) != 1:
detail = ""
if not ok:
best = min((_sub(store, base, sub, x) for sub in s["oneOf"]), key=len)
detail = " (closest: %s)" % "; ".join(best[:3])
errors.append("%s: matches %d of oneOf%s" % (path or "/", len(ok), detail))
if "not" in s and not _sub(store, base, s["not"], x):
errors.append("%s: matches `not`" % (path or "/"))
if "if" in s:
if not _sub(store, base, s["if"], x):
if "then" in s:
_v(store, base, s["then"], x, path, errors)
elif "else" in s:
_v(store, base, s["else"], x, path, errors)
def _sub(store, base, s, x):
e = []
_v(store, base, s, x, "", e)
return e
"""Return a list of error strings, `path: message`, in path order; empty when the instance is valid."""
errors = sorted(store.validator(name).iter_errors(instance), key=lambda e: [str(x) for x in e.absolute_path])
return ["%s: %s" % ("/" + "/".join(str(x) for x in e.absolute_path), e.message) for e in errors]