mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
(cherry picked from commit 37f407850916ebe76575d73112c4d26fe08bf421) Ported for #244 (part 6/6). Internal-only content from the archived commit was dropped: .aiwg records and the internal security-docs test. Doc and test conflicts keep upstream wording and add only the topology references; internal tracker numbers were replaced with upstream framing. Marked experimental in user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
805 lines
31 KiB
Python
805 lines
31 KiB
Python
"""Bounded private evidence records for one exact distributed model load.
|
|
|
|
This module deliberately depends only on Python mappings. Model loading and
|
|
manifest construction remain separate trust boundaries: their validated output
|
|
is converted to the closed record below, persisted privately, and represented
|
|
to collectives by a compact digest-only summary.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import os
|
|
import re
|
|
import secrets
|
|
import stat
|
|
from collections.abc import Mapping, Sequence
|
|
from dataclasses import asdict, dataclass
|
|
from pathlib import Path
|
|
from typing import TypeAlias
|
|
|
|
from obliteratus.distributed.contracts import ContractError
|
|
from obliteratus.distributed.model_topology import GPU_CANDIDATE_EVIDENCE_TIERS
|
|
|
|
|
|
JSONScalar: TypeAlias = str | int | bool | None
|
|
JSONValue: TypeAlias = JSONScalar | list["JSONValue"] | dict[str, "JSONValue"]
|
|
|
|
MAX_PRIVATE_LOAD_EVIDENCE_BYTES = 16 * 1024 * 1024
|
|
MAX_LOAD_SUMMARY_BYTES = 4 * 1024
|
|
MAX_MANIFEST_RECORDS = 16_384
|
|
MAX_JSON_ITEMS = 1_048_576
|
|
MAX_JSON_DEPTH = 16
|
|
MAX_TEXT_BYTES = 512
|
|
|
|
_RUN_ID_RE = re.compile(r"^[0-9a-f]{32}$")
|
|
_DIGEST_RE = re.compile(r"^[0-9a-f]{64}$")
|
|
_KEY_RE = re.compile(r"^[a-z][a-z0-9_]{0,63}$")
|
|
_SAFE_TEXT_RE = re.compile(r"^[A-Za-z0-9_.:@+*-]+$")
|
|
_ERROR_RE = re.compile(r"^LMS_[A-Z0-9_]{1,59}$")
|
|
|
|
_PRIVATE_RECORD_FIELDS = frozenset(
|
|
{
|
|
"schema_version",
|
|
"record_type",
|
|
"result",
|
|
"run_id",
|
|
"rank",
|
|
"world_size",
|
|
"evidence_tier",
|
|
"bindings",
|
|
"manifest",
|
|
"error_code",
|
|
}
|
|
)
|
|
_MANIFEST_REQUIRED_FIELDS = frozenset(
|
|
{"schema_version", "adapter_id", "rank", "world_size", "parameters", "hooks"}
|
|
)
|
|
_MANIFEST_OPTIONAL_FIELDS = frozenset({"buffers"})
|
|
LOAD_EVIDENCE_BINDING_FIELDS = frozenset(
|
|
{
|
|
"api_digest",
|
|
"artifact_manifest_digest",
|
|
"code_digest",
|
|
"config_digest",
|
|
"control_group_digest",
|
|
"data_group_digest",
|
|
"deployment_layout_digest",
|
|
"hardware_digest",
|
|
"identity_digest",
|
|
"image_digest",
|
|
"lock_digest",
|
|
"logical_manifest_digest",
|
|
"model_config_digest",
|
|
"model_digest",
|
|
"package_digest",
|
|
"plan_digest",
|
|
"profile_digest",
|
|
"runtime_digest",
|
|
"source_digest",
|
|
"tokenizer_digest",
|
|
"topology_digest",
|
|
"torch_wheel_digest",
|
|
}
|
|
)
|
|
_MANIFEST_REQUIRED_COLLECTIONS = ("parameters", "hooks")
|
|
_EVIDENCE_TIERS = frozenset({"protocol_cpu", *GPU_CANDIDATE_EVIDENCE_TIERS})
|
|
_RESULTS = frozenset({"loaded", "failed", "quarantined"})
|
|
_FORBIDDEN_MANIFEST_KEYS = frozenset(
|
|
{
|
|
"address",
|
|
"access_key",
|
|
"api_key",
|
|
"argv",
|
|
"authorization",
|
|
"bearer",
|
|
"cookie",
|
|
"credential",
|
|
"credentials",
|
|
"device_uuid",
|
|
"environment",
|
|
"exception",
|
|
"host",
|
|
"hostname",
|
|
"ip",
|
|
"model_path",
|
|
"password",
|
|
"path",
|
|
"payload",
|
|
"prompt",
|
|
"secret",
|
|
"token_value",
|
|
"traceback",
|
|
"url",
|
|
"value",
|
|
"values",
|
|
}
|
|
)
|
|
|
|
# These codes are the only failure details retained by this boundary. Callers
|
|
# must translate framework exceptions before building a record.
|
|
LOAD_ERROR_CODES = frozenset(
|
|
{
|
|
"LMS_CLEANUP_INCOMPLETE",
|
|
"LMS_DEPENDENCY_DRIFT",
|
|
"LMS_DIAGNOSTIC_REDACTION_FAILED",
|
|
"LMS_LOAD_EVIDENCE_UNAVAILABLE",
|
|
"LMS_LOAD_MANIFEST_INCOMPLETE",
|
|
"LMS_LOAD_MANIFEST_INVALID",
|
|
"LMS_LOAD_PROFILE_MISMATCH",
|
|
"LMS_LOAD_STATE_UNCERTAIN",
|
|
"LMS_LOAD_TIMEOUT",
|
|
"LMS_MODEL_ARTIFACT_UNSUPPORTED",
|
|
"LMS_MODEL_CONFIG_UNSUPPORTED",
|
|
"LMS_MODEL_LOAD_FAILED",
|
|
"LMS_SOURCE_CHANGED",
|
|
"LMS_TP_PLAN_DRIFT",
|
|
"LMS_TP_PLAN_UNSUPPORTED",
|
|
}
|
|
)
|
|
_QUARANTINE_CODES = frozenset({"LMS_CLEANUP_INCOMPLETE", "LMS_LOAD_STATE_UNCERTAIN"})
|
|
|
|
|
|
def _require_int(value: object, name: str, *, minimum: int, maximum: int) -> int:
|
|
if isinstance(value, bool) or not isinstance(value, int) or not minimum <= value <= maximum:
|
|
raise ContractError(f"{name} must be an integer between {minimum} and {maximum}")
|
|
return value
|
|
|
|
|
|
def _require_text(
|
|
value: object,
|
|
name: str,
|
|
*,
|
|
pattern: re.Pattern[str] | None = None,
|
|
) -> str:
|
|
if not isinstance(value, str) or not value:
|
|
raise ContractError(f"{name} must be non-empty text")
|
|
try:
|
|
encoded = value.encode("utf-8", errors="strict")
|
|
except UnicodeError as exc:
|
|
raise ContractError(f"{name} must be strict UTF-8") from exc
|
|
if len(encoded) > MAX_TEXT_BYTES:
|
|
raise ContractError(f"{name} exceeds its text bound")
|
|
if pattern is not None and pattern.fullmatch(value) is None:
|
|
raise ContractError(f"{name} has an invalid format")
|
|
return value
|
|
|
|
|
|
def _require_digest(value: object, name: str) -> str:
|
|
return _require_text(value, name, pattern=_DIGEST_RE)
|
|
|
|
|
|
def _normalize_json(
|
|
value: object, *, depth: int = 0, counter: list[int] | None = None
|
|
) -> JSONValue:
|
|
"""Copy one bounded deterministic JSON value and reject unsafe text surfaces."""
|
|
|
|
if counter is None:
|
|
counter = [0]
|
|
if depth > MAX_JSON_DEPTH:
|
|
raise ContractError("load evidence exceeds its JSON depth bound")
|
|
counter[0] += 1
|
|
if counter[0] > MAX_JSON_ITEMS:
|
|
raise ContractError("load evidence exceeds its JSON item bound")
|
|
|
|
if value is None or isinstance(value, bool):
|
|
return value
|
|
if isinstance(value, int):
|
|
return _require_int(value, "manifest integer", minimum=-(2**63), maximum=2**63 - 1)
|
|
if isinstance(value, float):
|
|
raise ContractError("load evidence cannot contain floating-point values")
|
|
if isinstance(value, str):
|
|
return _require_text(value, "manifest text", pattern=_SAFE_TEXT_RE)
|
|
if isinstance(value, Mapping):
|
|
normalized: dict[str, JSONValue] = {}
|
|
for key, item in value.items():
|
|
checked_key = _require_text(key, "manifest key", pattern=_KEY_RE)
|
|
if checked_key in _FORBIDDEN_MANIFEST_KEYS:
|
|
raise ContractError("load evidence contains a forbidden private-data field")
|
|
if checked_key in normalized:
|
|
raise ContractError("load evidence contains a duplicate field")
|
|
normalized[checked_key] = _normalize_json(item, depth=depth + 1, counter=counter)
|
|
return normalized
|
|
if isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
|
|
return [_normalize_json(item, depth=depth + 1, counter=counter) for item in value]
|
|
raise ContractError("load evidence contains a non-JSON value")
|
|
|
|
|
|
def _canonical_bytes(value: Mapping[str, object], *, maximum: int) -> bytes:
|
|
normalized = _normalize_json(value)
|
|
if not isinstance(normalized, dict):
|
|
raise ContractError("load evidence must be a JSON object")
|
|
encoded = (json.dumps(normalized, sort_keys=True, separators=(",", ":")) + "\n").encode("utf-8")
|
|
if not 1 <= len(encoded) <= maximum:
|
|
raise ContractError("load evidence exceeds its byte bound")
|
|
return encoded
|
|
|
|
|
|
def _strict_json(payload: bytes, *, maximum: int) -> dict[str, JSONValue]:
|
|
if (
|
|
not isinstance(payload, bytes)
|
|
or not 1 <= len(payload) <= maximum
|
|
or not payload.endswith(b"\n")
|
|
):
|
|
raise ContractError("load evidence has an invalid byte envelope")
|
|
|
|
def unique_object(pairs: list[tuple[str, object]]) -> dict[str, object]:
|
|
result: dict[str, object] = {}
|
|
for key, value in pairs:
|
|
if key in result:
|
|
raise ContractError("load evidence contains a duplicate field")
|
|
result[key] = value
|
|
return result
|
|
|
|
def bounded_integer(text: str) -> int:
|
|
if len(text.lstrip("-")) > 19:
|
|
raise ContractError("load evidence integer exceeds its bound")
|
|
return int(text)
|
|
|
|
def reject_constant(_text: str) -> object:
|
|
raise ContractError("load evidence cannot contain a non-finite value")
|
|
|
|
try:
|
|
decoded = json.loads(
|
|
payload[:-1].decode("utf-8", errors="strict"),
|
|
object_pairs_hook=unique_object,
|
|
parse_int=bounded_integer,
|
|
parse_constant=reject_constant,
|
|
)
|
|
except ContractError:
|
|
raise
|
|
except (UnicodeError, json.JSONDecodeError, RecursionError, ValueError) as exc:
|
|
raise ContractError("load evidence is not strict UTF-8 JSON") from exc
|
|
normalized = _normalize_json(decoded)
|
|
if not isinstance(normalized, dict):
|
|
raise ContractError("load evidence must be a JSON object")
|
|
if payload != _canonical_bytes(normalized, maximum=maximum):
|
|
raise ContractError("load evidence is not canonical")
|
|
return normalized
|
|
|
|
|
|
def _normalize_bindings(value: object) -> dict[str, JSONValue]:
|
|
if not isinstance(value, Mapping) or set(value) != LOAD_EVIDENCE_BINDING_FIELDS:
|
|
raise ContractError("load evidence bindings do not match the closed schema")
|
|
return {
|
|
key: _require_digest(value[key], f"bindings.{key}")
|
|
for key in sorted(LOAD_EVIDENCE_BINDING_FIELDS)
|
|
}
|
|
|
|
|
|
def _manifest_counts(value: object) -> tuple[dict[str, JSONValue], int, int, int]:
|
|
if not isinstance(value, Mapping):
|
|
raise ContractError("successful load evidence requires a manifest object")
|
|
normalized = _normalize_json(value)
|
|
if not isinstance(normalized, dict):
|
|
raise ContractError("load manifest must be an object")
|
|
if not _MANIFEST_REQUIRED_FIELDS.issubset(normalized) or not set(normalized).issubset(
|
|
_MANIFEST_REQUIRED_FIELDS | _MANIFEST_OPTIONAL_FIELDS
|
|
):
|
|
raise ContractError("load manifest fields do not match the closed schema")
|
|
if normalized.get("schema_version") != 1:
|
|
raise ContractError("load manifest schema_version must be 1")
|
|
_require_text(normalized.get("adapter_id"), "manifest adapter_id", pattern=_SAFE_TEXT_RE)
|
|
counts: dict[str, int] = {}
|
|
for name in _MANIFEST_REQUIRED_COLLECTIONS:
|
|
records = normalized.get(name)
|
|
if not isinstance(records, list):
|
|
raise ContractError(f"load manifest {name} must be a list")
|
|
counts[name] = len(records)
|
|
buffers = normalized.get("buffers", [])
|
|
if not isinstance(buffers, list):
|
|
raise ContractError("load manifest buffers must be a list when present")
|
|
if buffers:
|
|
raise ContractError("the exact initial model-load profile admits no persistent buffers")
|
|
counts["buffers"] = len(buffers)
|
|
if sum(counts.values()) > MAX_MANIFEST_RECORDS:
|
|
raise ContractError("load manifest exceeds its record-count bound")
|
|
return normalized, counts["parameters"], counts["buffers"], counts["hooks"]
|
|
|
|
|
|
def normalize_rank_load_evidence(record: Mapping[str, object]) -> dict[str, JSONValue]:
|
|
"""Validate and detach one private per-rank evidence mapping."""
|
|
|
|
if not isinstance(record, Mapping) or set(record) != _PRIVATE_RECORD_FIELDS:
|
|
raise ContractError("rank load evidence fields do not match the closed schema")
|
|
if record["schema_version"] != 1:
|
|
raise ContractError("rank load evidence schema_version must be 1")
|
|
if record["record_type"] != "lms_private_rank_load_evidence":
|
|
raise ContractError("rank load evidence record_type is invalid")
|
|
result = _require_text(record["result"], "result", pattern=_SAFE_TEXT_RE)
|
|
if result not in _RESULTS:
|
|
raise ContractError("rank load evidence result is invalid")
|
|
run_id = _require_text(record["run_id"], "run_id", pattern=_RUN_ID_RE)
|
|
world_size = _require_int(record["world_size"], "world_size", minimum=2, maximum=4096)
|
|
rank = _require_int(record["rank"], "rank", minimum=0, maximum=world_size - 1)
|
|
evidence_tier = _require_text(record["evidence_tier"], "evidence_tier", pattern=_SAFE_TEXT_RE)
|
|
if evidence_tier not in _EVIDENCE_TIERS:
|
|
raise ContractError("rank load evidence tier is invalid")
|
|
bindings = _normalize_bindings(record["bindings"])
|
|
raw_error_code = record["error_code"]
|
|
error_code = (
|
|
None
|
|
if raw_error_code is None
|
|
else _require_text(raw_error_code, "error_code", pattern=_ERROR_RE)
|
|
)
|
|
manifest: dict[str, JSONValue] | None
|
|
if result == "loaded":
|
|
if error_code is not None:
|
|
raise ContractError("successful load evidence cannot contain an error code")
|
|
manifest, _parameters, _buffers, _hooks = _manifest_counts(record["manifest"])
|
|
if manifest["rank"] != rank or manifest["world_size"] != world_size:
|
|
raise ContractError("load manifest rank identity disagrees with its evidence envelope")
|
|
else:
|
|
if record["manifest"] is not None:
|
|
raise ContractError("unsuccessful load evidence cannot retain a partial manifest")
|
|
if error_code not in LOAD_ERROR_CODES or _ERROR_RE.fullmatch(str(error_code)) is None:
|
|
raise ContractError("unsuccessful load evidence requires a stable load error code")
|
|
if (result == "quarantined") != (error_code in _QUARANTINE_CODES):
|
|
raise ContractError("quarantine result and error code disagree")
|
|
manifest = None
|
|
return {
|
|
"bindings": bindings,
|
|
"error_code": error_code,
|
|
"evidence_tier": evidence_tier,
|
|
"manifest": manifest,
|
|
"rank": rank,
|
|
"record_type": "lms_private_rank_load_evidence",
|
|
"result": result,
|
|
"run_id": run_id,
|
|
"schema_version": 1,
|
|
"world_size": world_size,
|
|
}
|
|
|
|
|
|
def build_rank_load_evidence(
|
|
*,
|
|
result: str,
|
|
run_id: str,
|
|
rank: int,
|
|
world_size: int,
|
|
evidence_tier: str,
|
|
bindings: Mapping[str, object],
|
|
manifest: Mapping[str, object] | None = None,
|
|
error_code: str | None = None,
|
|
) -> dict[str, JSONValue]:
|
|
"""Build a detached closed record without retaining raw implementation objects."""
|
|
|
|
return normalize_rank_load_evidence(
|
|
{
|
|
"schema_version": 1,
|
|
"record_type": "lms_private_rank_load_evidence",
|
|
"result": result,
|
|
"run_id": run_id,
|
|
"rank": rank,
|
|
"world_size": world_size,
|
|
"evidence_tier": evidence_tier,
|
|
"bindings": bindings,
|
|
"manifest": manifest,
|
|
"error_code": error_code,
|
|
}
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class LoadEvidenceSummary:
|
|
"""A compact, digest-only per-rank record suitable for bounded consensus."""
|
|
|
|
schema_version: int
|
|
record_type: str
|
|
result: str
|
|
run_id: str
|
|
rank: int
|
|
world_size: int
|
|
evidence_tier: str
|
|
bindings_digest: str
|
|
private_record_digest: str
|
|
manifest_digest: str | None
|
|
parameter_count: int
|
|
buffer_count: int
|
|
hook_count: int
|
|
error_code: str | None
|
|
|
|
def __post_init__(self) -> None:
|
|
if self.schema_version != 1 or self.record_type != "lms_rank_load_summary":
|
|
raise ContractError("load summary schema identity is invalid")
|
|
if self.result not in _RESULTS:
|
|
raise ContractError("load summary result is invalid")
|
|
_require_text(self.run_id, "summary run_id", pattern=_RUN_ID_RE)
|
|
_require_int(self.world_size, "summary world_size", minimum=2, maximum=4096)
|
|
_require_int(self.rank, "summary rank", minimum=0, maximum=self.world_size - 1)
|
|
if self.evidence_tier not in _EVIDENCE_TIERS:
|
|
raise ContractError("load summary tier is invalid")
|
|
_require_digest(self.bindings_digest, "summary bindings_digest")
|
|
_require_digest(self.private_record_digest, "summary private_record_digest")
|
|
counts = (
|
|
_require_int(
|
|
self.parameter_count, "parameter_count", minimum=0, maximum=MAX_MANIFEST_RECORDS
|
|
),
|
|
_require_int(
|
|
self.buffer_count, "buffer_count", minimum=0, maximum=MAX_MANIFEST_RECORDS
|
|
),
|
|
_require_int(self.hook_count, "hook_count", minimum=0, maximum=MAX_MANIFEST_RECORDS),
|
|
)
|
|
if sum(counts) > MAX_MANIFEST_RECORDS:
|
|
raise ContractError("load summary counts exceed the manifest bound")
|
|
if self.result == "loaded":
|
|
if self.manifest_digest is None:
|
|
raise ContractError("successful load summary requires a manifest digest")
|
|
_require_digest(self.manifest_digest, "summary manifest_digest")
|
|
if self.error_code is not None:
|
|
raise ContractError("successful load summary cannot contain an error code")
|
|
else:
|
|
if self.manifest_digest is not None or any(counts):
|
|
raise ContractError("unsuccessful load summary cannot describe a partial manifest")
|
|
if self.error_code not in LOAD_ERROR_CODES:
|
|
raise ContractError("unsuccessful load summary requires a stable load error code")
|
|
if (self.result == "quarantined") != (self.error_code in _QUARANTINE_CODES):
|
|
raise ContractError("load summary quarantine state is inconsistent")
|
|
|
|
def to_bytes(self) -> bytes:
|
|
return _canonical_bytes(asdict(self), maximum=MAX_LOAD_SUMMARY_BYTES)
|
|
|
|
@classmethod
|
|
def from_bytes(cls, payload: bytes) -> "LoadEvidenceSummary":
|
|
value = _strict_json(payload, maximum=MAX_LOAD_SUMMARY_BYTES)
|
|
if set(value) != set(cls.__dataclass_fields__):
|
|
raise ContractError("load summary fields do not match the closed schema")
|
|
raw_error_code = value.get("error_code")
|
|
error_code = (
|
|
None
|
|
if raw_error_code is None
|
|
else _require_text(raw_error_code, "summary error_code", pattern=_ERROR_RE)
|
|
)
|
|
result = cls(
|
|
schema_version=_require_int(
|
|
value.get("schema_version"), "summary schema_version", minimum=1, maximum=1
|
|
),
|
|
record_type=_require_text(
|
|
value.get("record_type"), "summary record_type", pattern=_SAFE_TEXT_RE
|
|
),
|
|
result=_require_text(value.get("result"), "summary result", pattern=_SAFE_TEXT_RE),
|
|
run_id=_require_text(value.get("run_id"), "summary run_id", pattern=_RUN_ID_RE),
|
|
rank=_require_int(value.get("rank"), "summary rank", minimum=0, maximum=4095),
|
|
world_size=_require_int(
|
|
value.get("world_size"), "summary world_size", minimum=2, maximum=4096
|
|
),
|
|
evidence_tier=_require_text(
|
|
value.get("evidence_tier"), "summary evidence tier", pattern=_SAFE_TEXT_RE
|
|
),
|
|
bindings_digest=_require_digest(
|
|
value.get("bindings_digest"), "summary bindings_digest"
|
|
),
|
|
private_record_digest=_require_digest(
|
|
value.get("private_record_digest"), "summary private record digest"
|
|
),
|
|
manifest_digest=(
|
|
None
|
|
if value.get("manifest_digest") is None
|
|
else _require_digest(value.get("manifest_digest"), "summary manifest digest")
|
|
),
|
|
parameter_count=_require_int(
|
|
value.get("parameter_count"),
|
|
"summary parameter count",
|
|
minimum=0,
|
|
maximum=MAX_MANIFEST_RECORDS,
|
|
),
|
|
buffer_count=_require_int(
|
|
value.get("buffer_count"),
|
|
"summary buffer count",
|
|
minimum=0,
|
|
maximum=MAX_MANIFEST_RECORDS,
|
|
),
|
|
hook_count=_require_int(
|
|
value.get("hook_count"),
|
|
"summary hook count",
|
|
minimum=0,
|
|
maximum=MAX_MANIFEST_RECORDS,
|
|
),
|
|
error_code=error_code,
|
|
)
|
|
if payload != result.to_bytes():
|
|
raise ContractError("load summary is not canonical")
|
|
return result
|
|
|
|
|
|
def _record_bytes(record: Mapping[str, object]) -> tuple[dict[str, JSONValue], bytes]:
|
|
normalized = normalize_rank_load_evidence(record)
|
|
return normalized, _canonical_bytes(normalized, maximum=MAX_PRIVATE_LOAD_EVIDENCE_BYTES)
|
|
|
|
|
|
def _record_summary(record: Mapping[str, object], payload: bytes) -> LoadEvidenceSummary:
|
|
normalized = normalize_rank_load_evidence(record)
|
|
bindings = normalized["bindings"]
|
|
assert isinstance(bindings, dict)
|
|
bindings_payload = _canonical_bytes(bindings, maximum=MAX_LOAD_SUMMARY_BYTES)
|
|
manifest = normalized["manifest"]
|
|
if manifest is None:
|
|
manifest_digest = None
|
|
parameter_count = buffer_count = hook_count = 0
|
|
else:
|
|
manifest, parameter_count, buffer_count, hook_count = _manifest_counts(manifest)
|
|
manifest_payload = _canonical_bytes(manifest, maximum=MAX_PRIVATE_LOAD_EVIDENCE_BYTES)
|
|
manifest_digest = hashlib.sha256(manifest_payload[:-1]).hexdigest()
|
|
return LoadEvidenceSummary(
|
|
schema_version=1,
|
|
record_type="lms_rank_load_summary",
|
|
result=str(normalized["result"]),
|
|
run_id=str(normalized["run_id"]),
|
|
rank=_require_int(normalized["rank"], "rank", minimum=0, maximum=4095),
|
|
world_size=_require_int(normalized["world_size"], "world_size", minimum=2, maximum=4096),
|
|
evidence_tier=str(normalized["evidence_tier"]),
|
|
bindings_digest=hashlib.sha256(bindings_payload[:-1]).hexdigest(),
|
|
private_record_digest=hashlib.sha256(payload).hexdigest(),
|
|
manifest_digest=manifest_digest,
|
|
parameter_count=parameter_count,
|
|
buffer_count=buffer_count,
|
|
hook_count=hook_count,
|
|
error_code=None if normalized["error_code"] is None else str(normalized["error_code"]),
|
|
)
|
|
|
|
|
|
def _record_filename(run_id: str, rank: int) -> str:
|
|
_require_text(run_id, "run_id", pattern=_RUN_ID_RE)
|
|
_require_int(rank, "rank", minimum=0, maximum=4095)
|
|
return f"load-evidence-{run_id}-rank-{rank:04d}.json"
|
|
|
|
|
|
def _open_private_directory(directory: str | Path) -> int:
|
|
target = Path(directory)
|
|
if not target.is_absolute() or any(part in {"", ".", ".."} for part in target.parts[1:]):
|
|
raise ContractError("load evidence directory must be an absolute normalized path")
|
|
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0)
|
|
try:
|
|
descriptor = os.open("/", flags)
|
|
except OSError:
|
|
raise ContractError("load evidence sink is unavailable") from None
|
|
try:
|
|
for component in target.parts[1:]:
|
|
next_descriptor = os.open(component, flags, dir_fd=descriptor)
|
|
os.close(descriptor)
|
|
descriptor = next_descriptor
|
|
metadata = os.fstat(descriptor)
|
|
effective_uid = getattr(os, "geteuid", lambda: metadata.st_uid)()
|
|
if not stat.S_ISDIR(metadata.st_mode):
|
|
raise ContractError("load evidence destination must be a directory")
|
|
if metadata.st_uid != effective_uid or metadata.st_mode & 0o077:
|
|
raise ContractError("load evidence directory must be private and worker-owned")
|
|
return descriptor
|
|
except ContractError:
|
|
os.close(descriptor)
|
|
raise
|
|
except OSError:
|
|
os.close(descriptor)
|
|
raise ContractError("load evidence sink is unavailable") from None
|
|
|
|
|
|
def _exclusive_atomic_write(directory_fd: int, filename: str, payload: bytes) -> None:
|
|
temporary = f".{filename}.tmp.{os.getpid()}.{secrets.token_hex(16)}"
|
|
descriptor: int | None = None
|
|
try:
|
|
descriptor = os.open(
|
|
temporary,
|
|
os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0),
|
|
0o600,
|
|
dir_fd=directory_fd,
|
|
)
|
|
offset = 0
|
|
while offset < len(payload):
|
|
written = os.write(descriptor, payload[offset:])
|
|
if written <= 0:
|
|
raise ContractError("load evidence write made no progress")
|
|
offset += written
|
|
os.fsync(descriptor)
|
|
metadata = os.fstat(descriptor)
|
|
if not stat.S_ISREG(metadata.st_mode) or metadata.st_nlink != 1 or metadata.st_mode & 0o077:
|
|
raise ContractError("load evidence temporary file is not private and regular")
|
|
os.close(descriptor)
|
|
descriptor = None
|
|
os.link(
|
|
temporary,
|
|
filename,
|
|
src_dir_fd=directory_fd,
|
|
dst_dir_fd=directory_fd,
|
|
follow_symlinks=False,
|
|
)
|
|
os.unlink(temporary, dir_fd=directory_fd)
|
|
os.fsync(directory_fd)
|
|
except FileExistsError:
|
|
raise ContractError("rank load evidence already exists") from None
|
|
except OSError:
|
|
raise ContractError("rank load evidence sink is unavailable") from None
|
|
finally:
|
|
if descriptor is not None:
|
|
os.close(descriptor)
|
|
try:
|
|
os.unlink(temporary, dir_fd=directory_fd)
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
|
|
def _read_stable(directory_fd: int, filename: str) -> bytes:
|
|
descriptor: int | None = None
|
|
try:
|
|
descriptor = os.open(
|
|
filename,
|
|
os.O_RDONLY | os.O_NOFOLLOW | getattr(os, "O_CLOEXEC", 0),
|
|
dir_fd=directory_fd,
|
|
)
|
|
before = os.fstat(descriptor)
|
|
effective_uid = getattr(os, "geteuid", lambda: before.st_uid)()
|
|
if (
|
|
not stat.S_ISREG(before.st_mode)
|
|
or before.st_uid != effective_uid
|
|
or before.st_nlink != 1
|
|
or before.st_mode & 0o077
|
|
or not 1 <= before.st_size <= MAX_PRIVATE_LOAD_EVIDENCE_BYTES
|
|
):
|
|
raise ContractError("rank load evidence is not private, owned, regular, and bounded")
|
|
chunks: list[bytes] = []
|
|
remaining = before.st_size
|
|
while remaining:
|
|
chunk = os.read(descriptor, min(remaining, 1024 * 1024))
|
|
if not chunk:
|
|
raise ContractError("rank load evidence was truncated while reading")
|
|
chunks.append(chunk)
|
|
remaining -= len(chunk)
|
|
if os.read(descriptor, 1):
|
|
raise ContractError("rank load evidence grew while reading")
|
|
after = os.fstat(descriptor)
|
|
observed = (
|
|
before.st_dev,
|
|
before.st_ino,
|
|
before.st_mode,
|
|
before.st_uid,
|
|
before.st_nlink,
|
|
before.st_size,
|
|
before.st_mtime_ns,
|
|
before.st_ctime_ns,
|
|
)
|
|
confirmed = (
|
|
after.st_dev,
|
|
after.st_ino,
|
|
after.st_mode,
|
|
after.st_uid,
|
|
after.st_nlink,
|
|
after.st_size,
|
|
after.st_mtime_ns,
|
|
after.st_ctime_ns,
|
|
)
|
|
if observed != confirmed:
|
|
raise ContractError("rank load evidence changed while reading")
|
|
return b"".join(chunks)
|
|
except FileNotFoundError:
|
|
raise ContractError("rank load evidence is unavailable") from None
|
|
except OSError:
|
|
raise ContractError("rank load evidence sink is unavailable") from None
|
|
finally:
|
|
if descriptor is not None:
|
|
os.close(descriptor)
|
|
|
|
|
|
def publish_rank_load_evidence(
|
|
directory: str | Path,
|
|
record: Mapping[str, object],
|
|
) -> LoadEvidenceSummary:
|
|
"""Create one private per-rank record and return its compact digest summary."""
|
|
|
|
normalized, payload = _record_bytes(record)
|
|
run_id = str(normalized["run_id"])
|
|
rank = _require_int(normalized["rank"], "rank", minimum=0, maximum=4095)
|
|
directory_fd = _open_private_directory(directory)
|
|
try:
|
|
filename = _record_filename(run_id, rank)
|
|
_exclusive_atomic_write(directory_fd, filename, payload)
|
|
confirmed = _read_stable(directory_fd, filename)
|
|
if confirmed != payload:
|
|
raise ContractError("rank load evidence readback disagrees")
|
|
finally:
|
|
os.close(directory_fd)
|
|
return _record_summary(normalized, payload)
|
|
|
|
|
|
def read_rank_load_evidence(
|
|
directory: str | Path,
|
|
*,
|
|
run_id: str,
|
|
rank: int,
|
|
) -> dict[str, JSONValue]:
|
|
"""Read one canonical private record through the same descriptor boundary."""
|
|
|
|
directory_fd = _open_private_directory(directory)
|
|
try:
|
|
payload = _read_stable(directory_fd, _record_filename(run_id, rank))
|
|
finally:
|
|
os.close(directory_fd)
|
|
value = _strict_json(payload, maximum=MAX_PRIVATE_LOAD_EVIDENCE_BYTES)
|
|
return normalize_rank_load_evidence(value)
|
|
|
|
|
|
def verify_rank_load_evidence(
|
|
directory: str | Path,
|
|
summary: LoadEvidenceSummary,
|
|
) -> dict[str, JSONValue]:
|
|
"""Verify a private record against its compact summary before relying on it."""
|
|
|
|
if not isinstance(summary, LoadEvidenceSummary):
|
|
raise ContractError("load evidence summary has an invalid type")
|
|
directory_fd = _open_private_directory(directory)
|
|
try:
|
|
payload = _read_stable(
|
|
directory_fd,
|
|
_record_filename(summary.run_id, summary.rank),
|
|
)
|
|
finally:
|
|
os.close(directory_fd)
|
|
value = _strict_json(payload, maximum=MAX_PRIVATE_LOAD_EVIDENCE_BYTES)
|
|
normalized = normalize_rank_load_evidence(value)
|
|
if _record_summary(normalized, payload) != summary:
|
|
raise ContractError("private load evidence does not match its summary")
|
|
return normalized
|
|
|
|
|
|
def validate_loaded_summary_set(summaries: Sequence[LoadEvidenceSummary]) -> str:
|
|
"""Validate one complete fixed-rank loaded set and return its aggregate digest."""
|
|
|
|
if not isinstance(summaries, Sequence) or isinstance(summaries, (str, bytes, bytearray)):
|
|
raise ContractError("load summaries must be a fixed-rank sequence")
|
|
if not summaries or any(not isinstance(item, LoadEvidenceSummary) for item in summaries):
|
|
raise ContractError("load summaries contain an invalid record")
|
|
world_size = summaries[0].world_size
|
|
if len(summaries) != world_size or [item.rank for item in summaries] != list(range(world_size)):
|
|
raise ContractError("load summaries do not cover the fixed world in rank order")
|
|
first = summaries[0]
|
|
shared = (
|
|
first.run_id,
|
|
first.world_size,
|
|
first.evidence_tier,
|
|
first.bindings_digest,
|
|
first.parameter_count,
|
|
first.buffer_count,
|
|
first.hook_count,
|
|
)
|
|
if any(
|
|
item.result != "loaded"
|
|
or (
|
|
item.run_id,
|
|
item.world_size,
|
|
item.evidence_tier,
|
|
item.bindings_digest,
|
|
item.parameter_count,
|
|
item.buffer_count,
|
|
item.hook_count,
|
|
)
|
|
!= shared
|
|
for item in summaries
|
|
):
|
|
raise ContractError("load summaries do not unanimously describe one loaded world")
|
|
if len({item.private_record_digest for item in summaries}) != world_size:
|
|
raise ContractError("load summaries reuse a private rank record")
|
|
|
|
digest = hashlib.sha256(b"lms-loaded-summary-set-v1\x00")
|
|
for item in summaries:
|
|
payload = item.to_bytes()
|
|
digest.update(len(payload).to_bytes(4, byteorder="big"))
|
|
digest.update(payload)
|
|
return digest.hexdigest()
|
|
|
|
|
|
__all__ = [
|
|
"LOAD_ERROR_CODES",
|
|
"LOAD_EVIDENCE_BINDING_FIELDS",
|
|
"MAX_JSON_DEPTH",
|
|
"MAX_JSON_ITEMS",
|
|
"MAX_LOAD_SUMMARY_BYTES",
|
|
"MAX_MANIFEST_RECORDS",
|
|
"MAX_PRIVATE_LOAD_EVIDENCE_BYTES",
|
|
"LoadEvidenceSummary",
|
|
"build_rank_load_evidence",
|
|
"normalize_rank_load_evidence",
|
|
"publish_rank_load_evidence",
|
|
"read_rank_load_evidence",
|
|
"verify_rank_load_evidence",
|
|
"validate_loaded_summary_set",
|
|
]
|