Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions changelog.d/nz-reference-ageing-factors.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Add declaration-driven, exact feed-derived reference ageing factors with certified
root bounds, once-only rounding, selected evidence identities and authenticated
compiler replay. Declare the reviewed NZ ageing profiles and their explicit money
and count companions while keeping the income references parked for measurement
closure.

Retain authenticated decimal tokens only with explicit lexical-float loading for
selected factors; ordinary loads keep plain floats and their serializer and memory
behavior. Bind factor application to the selected money/count roster and preserve
count operations. Certify exact-root ties in the independent oracle and bound
supported schedules so exact receipts remain serializable. Float selector pins
match token-retaining floats while keeping booleans, integers and floats distinct.
Scan extra feed-identity fields for RuleSpec commits. Bind factor evidence to
reviewed feed identities while allowing prose-only pin edits; refuse replay when
the source commit cannot be authenticated.
62 changes: 58 additions & 4 deletions packages/microcosm-build/src/microcosm/build/ledger_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"CHRONICLE_FEED_RESOURCE",
"ChronicleFeedPinMismatchError",
"LedgerConsumerArtifact",
"LexicalFloat",
"add_ledger_artifact_args",
"chronicle_feed_identity",
"load_ledger_consumer_artifact",
"resolve_ledger_artifact",
"validate_chronicle_feed_pin",
Expand All @@ -78,6 +80,51 @@
ALLOWED_LEDGER_ASSERTIONS = frozenset(("observation", "source_projection"))
DEFAULT_LEDGER_ASSERTION = "observation"
CHRONICLE_FEED_RESOURCE = "chronicle_feed.json"
_FEED_IDENTITY_FIELDS = (
"source_commit",
"facts_sha256",
"manifest_sha256",
"artifact_schema_version",
"consumer_fact_schema_versions",
"consumer_fact_schema_sha256",
"fact_row_count",
)


def chronicle_feed_identity(feed_pin: Mapping[str, Any]) -> dict[str, Any]:
"""Select reviewed source commit and loader-verified fields, excluding prose.

The source commit is a caller declaration unless the authenticated manifest
also carries it; the artifact loader verifies the remaining six fields.
"""
missing = sorted(set(_FEED_IDENTITY_FIELDS) - feed_pin.keys())
if missing:
raise ValueError(
f"Chronicle factor feed identity is missing fields: {missing}."
)
if not isinstance(feed_pin["source_commit"], str) or not feed_pin["source_commit"]:
raise ValueError("Chronicle factor feed identity requires a source commit.")
return {field: feed_pin[field] for field in _FEED_IDENTITY_FIELDS}


class LexicalFloat(float):
"""An opt-in float retaining its authenticated JSON numeric token.

Request ``lexical_floats=True`` when loading operands for exact arithmetic.
The default loader returns plain floats, avoiding the instance and token
overhead and preserving compatibility with serializers requiring exact
float types. Standard JSON serialization retains the same numeric bytes.
"""

__slots__ = ("lexical_token",)

def __new__(cls, token: str):
instance = super().__new__(cls, token)
instance.lexical_token = token
return instance

def __reduce__(self):
return type(self), (self.lexical_token,)


class ChronicleFeedPinMismatchError(ValueError):
Expand Down Expand Up @@ -250,6 +297,7 @@ def load_ledger_consumer_artifact(
expected_facts_sha256: str | None = None,
expected_manifest_sha256: str | None = None,
feed_pin: Mapping[str, Any] | None = None,
lexical_floats: bool = False,
) -> LedgerConsumerArtifact:
"""Load a Ledger consumer artifact directory or bare consumer-facts file.

Expand All @@ -259,6 +307,8 @@ def load_ledger_consumer_artifact(
reviewed against; any mismatch raises before facts are used. ``feed_pin``
additionally checks all six reviewed artifact fields, without replacing
the manifest integrity checks or either optional hash check.
``lexical_floats=True`` retains numeric tokens for factor verification;
otherwise JSON decimals load as plain ``float`` instances.
"""
artifact_path = Path(path)
if not artifact_path.exists():
Expand Down Expand Up @@ -314,7 +364,7 @@ def load_ledger_consumer_artifact(
if feed_pin is not None:
artifact = LedgerConsumerArtifact(
path=artifact_path,
facts=_load_fact_rows(facts_path),
facts=_load_fact_rows(facts_path, lexical_floats=lexical_floats),
facts_sha256=facts_sha256,
manifest=manifest,
manifest_sha256=manifest_sha256,
Expand All @@ -340,7 +390,7 @@ def load_ledger_consumer_artifact(
if artifact is None:
artifact = LedgerConsumerArtifact(
path=artifact_path,
facts=_load_fact_rows(facts_path),
facts=_load_fact_rows(facts_path, lexical_floats=lexical_floats),
facts_sha256=facts_sha256,
manifest=manifest,
manifest_sha256=manifest_sha256,
Expand Down Expand Up @@ -389,15 +439,19 @@ def resolve_ledger_artifact(args: Namespace) -> LedgerConsumerArtifact | None:
)


def _load_fact_rows(path: Path) -> tuple[dict[str, Any], ...]:
def _load_fact_rows(
path: Path, *, lexical_floats: bool = False
) -> tuple[dict[str, Any], ...]:
rows: list[dict[str, Any]] = []
with path.open() as file:
for line_number, line in enumerate(file, start=1):
stripped = line.strip()
if not stripped:
continue
try:
row = json.loads(stripped)
row = json.loads(
stripped, parse_float=LexicalFloat if lexical_floats else float
)
except json.JSONDecodeError as exc:
raise ValueError(
f"Invalid Ledger facts JSONL row {line_number}: {exc.msg}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from types import MappingProxyType
from typing import Any

from microcosm.build.ledger_artifact import LexicalFloat
from microcosm.calibrate import (
CalibrationHierarchy,
CalibrationHierarchySeed,
Expand Down Expand Up @@ -5058,7 +5059,9 @@ def _dimension_values_match(fact: object, expected: object) -> bool:


def _strict_scalar_equal(actual: object, expected: object) -> bool:
return type(actual) is type(expected) and actual == expected
actual_type = float if type(actual) is LexicalFloat else type(actual)
expected_type = float if type(expected) is LexicalFloat else type(expected)
return actual_type is expected_type and actual == expected


def _measure_concepts(fact: object) -> tuple[str, ...]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
"kind": "legacy_json",
"schema_id": "legacy_json"
},
{
"path": "reference_aging.json",
"kind": "legacy_json",
"schema_id": "legacy_json"
},
{
"path": "reference_inventory.json",
"kind": "legacy_json",
Expand Down
Loading
Loading