feat(consume): a byte budget instrument that validates before it reports

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-07 09:11:54 +02:00
commit b8c43198b5
2 changed files with 125 additions and 0 deletions

View file

@ -22,6 +22,7 @@ house pattern rather than new inventions:
from __future__ import annotations
import hashlib
import json
import os
import sys
from pathlib import Path
@ -253,3 +254,58 @@ def test_the_block_form_case_is_real_in_the_fixture_and_not_only_in_the_unit_tes
frontmatter = parse_frontmatter(FIXTURE / "dyp" / "nivaa" / "blokkform-verifisert.md")
assert frontmatter["verified"] == ""
assert okf_consume.trust_tier(frontmatter["verified"]) is None
# --- Step 4: the budget instrument -------------------------------------------
CONTRACT = PROJECT_ROOT / "docs" / "consumption-contract.md"
def test_measure_counts_bytes_and_not_characters() -> None:
# The exact conflation the brief records itself making once: a chars/token
# ratio quoted where a bytes/token one was needed. `æøå` is three
# characters and six bytes, and the two only differ outside ASCII.
assert okf_consume.measure("æøå") == len('"æøå"'.encode())
assert okf_consume.measure("æøå") != len("æøå")
def test_measure_counts_the_encoded_form_the_payload_actually_costs() -> None:
# `json.dumps` defaults to `ensure_ascii=True`, which inflates this corpus
# by 7.1 %. A gate measuring one form while the knapsack weighs the other
# disagrees by more than the headroom.
norwegian = "årlig kontroll av anlegget"
assert okf_consume.measure(norwegian) == len(
json.dumps(norwegian, ensure_ascii=False).encode("utf-8")
)
assert okf_consume.measure(norwegian) < len(
json.dumps(norwegian, ensure_ascii=True).encode("utf-8")
)
def test_the_known_positive_is_reproduced_by_the_gates_own_instrument() -> None:
case, expected, measured = okf_consume.known_positive()
assert case
assert expected == measured, "SS 7.4: the instrument has not been shown to count"
def test_the_known_positive_is_not_the_raw_byte_count_of_the_same_file() -> None:
# Validating one instrument while gating with another is the SS 7.4 failure
# the rule exists to prevent. The delta is derivable by a second, wholly
# independent route (`wc -c`) and moves the moment `measure` changes what it
# counts -- which is what keeps `expected == measured` from being vacuous.
_, expected, _ = okf_consume.known_positive()
raw = len(CONTRACT.read_bytes())
assert expected != raw
assert expected - raw == okf_consume.KNOWN_POSITIVE_ENCODING_DELTA
def test_the_default_limit_admits_a_concept_the_size_of_the_price_form() -> None:
# Measured during planning: at the drafted 60 000 B default the SC6 gold
# concept (101 313 B encoded) falls to the "cannot fit alone" pre-exclusion,
# so SC1 and SC6 were mutually unsatisfiable on a CORRECT implementation.
assert okf_consume.DEFAULT_LIMIT >= 101_313
def test_the_budget_unit_and_instrument_are_named_rather_than_implied() -> None:
assert "byte" in okf_consume.BUDGET_UNIT
assert "ensure_ascii=False" in okf_consume.BUDGET_INSTRUMENT