mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 19:13:55 +01:00
216 lines
9.1 KiB
Python
216 lines
9.1 KiB
Python
"""Public Wave 2 service for offline checkpoint structural inspection."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from typing import Literal
|
|
|
|
from obliteratus.checkpoint_capabilities import AdapterRegistry
|
|
from obliteratus.checkpoint_inspection import (
|
|
CheckpointInspection,
|
|
InspectionLimits,
|
|
inspect_checkpoint,
|
|
)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CheckpointComponent:
|
|
format: Literal['safetensors', 'pytorch_pickle']
|
|
files: int
|
|
trust_required: bool
|
|
validation: str = 'Filename classification only; tensor structure and integrity unverified.'
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CheckpointConfiguration:
|
|
model_type: str | None
|
|
dtype: str | None
|
|
hidden_size: int | None
|
|
layers: int | None
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class BoundedCheckpointDescriptor:
|
|
files: int
|
|
directories: int
|
|
total_bytes: int
|
|
tensor_files: int
|
|
primary_format: Literal['safetensors', 'pytorch_pickle', 'mixed', 'unknown']
|
|
components: list[CheckpointComponent]
|
|
configuration: CheckpointConfiguration | None
|
|
schema_id: str = 'obliteratus.mcp.checkpoint-descriptor'
|
|
schema_version: int = 1
|
|
inspection: str = 'Bounded directory metadata and recognized root config.json fields only.'
|
|
descriptor_available: bool = True
|
|
payloads_read: bool = False
|
|
tensor_headers_read: bool = False
|
|
integrity_verified: bool = False
|
|
loadability_verified: bool = False
|
|
support_decision: str = 'not_assessed'
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class CheckpointService:
|
|
"""Expose the common safe plane without trusted readers or producer adapters."""
|
|
|
|
inspection_limits: InspectionLimits = field(default_factory=InspectionLimits)
|
|
adapter_registry: AdapterRegistry = field(default_factory=AdapterRegistry)
|
|
|
|
def inspect(
|
|
self, source: Path | str, *, mode: Literal['full', 'bounded_metadata'] = 'full',
|
|
) -> CheckpointInspection | BoundedCheckpointDescriptor:
|
|
"""Return a bounded structure-only descriptor for one local source."""
|
|
if mode == 'bounded_metadata':
|
|
return self._bounded_descriptor(Path(source))
|
|
if mode != 'full':
|
|
raise ValueError('invalid inspection mode')
|
|
return inspect_checkpoint(
|
|
source,
|
|
limits=self.inspection_limits,
|
|
adapter_registry=self.adapter_registry,
|
|
)
|
|
|
|
def _bounded_descriptor(self, source: Path) -> BoundedCheckpointDescriptor:
|
|
import json
|
|
import os
|
|
import re
|
|
import stat
|
|
|
|
from obliteratus.read_boundary import child_directory, open_directory, read_json_bytes
|
|
|
|
limits = self.inspection_limits
|
|
counts = {'files': 0, 'directories': 0, 'total_bytes': 0, 'tensor_files': 0}
|
|
formats = {'safetensors': 0, 'pytorch_pickle': 0}
|
|
configuration = None
|
|
|
|
def regular(name, info):
|
|
if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1:
|
|
raise ValueError('checkpoint links and special files denied')
|
|
counts['files'] += 1
|
|
counts['total_bytes'] += info.st_size
|
|
if counts['files'] > min(limits.max_files, 1000) or counts['total_bytes'] > limits.max_total_bytes:
|
|
raise ValueError('checkpoint inventory limit')
|
|
suffix = Path(name).suffix
|
|
if suffix in {'.safetensors', '.bin', '.pt', '.pth'}:
|
|
counts['tensor_files'] += 1
|
|
formats['safetensors' if suffix == '.safetensors' else 'pytorch_pickle'] += 1
|
|
|
|
def config(fd):
|
|
nonlocal configuration
|
|
raw = read_json_bytes(fd, 'config.json', min(limits.max_json_bytes, 16384))
|
|
|
|
def unique(pairs):
|
|
value = {}
|
|
for key, item in pairs:
|
|
if key in value:
|
|
raise ValueError('duplicate configuration key')
|
|
value[key] = item
|
|
return value
|
|
|
|
def nonfinite(_):
|
|
raise ValueError('nonfinite JSON')
|
|
|
|
value = json.loads(raw, object_pairs_hook=unique, parse_constant=nonfinite)
|
|
if not isinstance(value, dict):
|
|
raise ValueError('invalid model configuration')
|
|
|
|
def label(key):
|
|
item = value.get(key)
|
|
return item if type(item) is str and re.fullmatch('[A-Za-z0-9_-]{1,64}', item) else None
|
|
|
|
def number(key):
|
|
item = value.get(key)
|
|
return item if type(item) is int and 0 < item <= 10**9 else None
|
|
|
|
dtype = label('torch_dtype') or label('dtype')
|
|
if dtype not in {'float32', 'float16', 'bfloat16', 'int8', 'int4'}:
|
|
dtype = None
|
|
configuration = CheckpointConfiguration(label('model_type'), dtype,
|
|
number('hidden_size'), number('num_hidden_layers'))
|
|
|
|
def visit(fd, depth):
|
|
if depth > 32:
|
|
raise ValueError('checkpoint depth limit')
|
|
counts['directories'] += 1
|
|
if counts['directories'] > min(limits.max_directories, 100):
|
|
raise ValueError('checkpoint directory limit')
|
|
# Every entry increments either files or directories below. Their
|
|
# independently enforced limits also bound the traversal as a whole.
|
|
with os.scandir(fd) as iterator:
|
|
for entry in iterator:
|
|
info = entry.stat(follow_symlinks=False)
|
|
if stat.S_ISDIR(info.st_mode):
|
|
with child_directory(fd, entry.name) as child:
|
|
visit(child, depth + 1)
|
|
else:
|
|
regular(entry.name, info)
|
|
if depth == 0 and entry.name == 'config.json':
|
|
config(fd)
|
|
|
|
try:
|
|
with open_directory(source) as fd:
|
|
visit(fd, 0)
|
|
except NotADirectoryError:
|
|
with open_directory(source.parent) as parent:
|
|
regular(source.name, os.stat(source.name, dir_fd=parent, follow_symlinks=False))
|
|
if source.name == 'config.json':
|
|
config(parent)
|
|
components = [CheckpointComponent(kind, count, kind == 'pytorch_pickle')
|
|
for kind, count in formats.items() if count]
|
|
primary = 'mixed' if len(components) > 1 else components[0].format if components else 'unknown'
|
|
return BoundedCheckpointDescriptor(**counts, primary_format=primary,
|
|
components=components, configuration=configuration)
|
|
|
|
def inspect_metadata(self, source: Path | str) -> dict[str, int]:
|
|
"""Count regular files and bytes without opening contents or tensors.
|
|
|
|
A directory inventory is deliberately weaker than ``inspect``: it makes
|
|
no format, integrity, trust, or loadability claim. Descriptor-relative
|
|
traversal also prevents parent-directory replacement from escaping.
|
|
"""
|
|
import os
|
|
import stat
|
|
|
|
from obliteratus.read_boundary import child_directory, open_directory
|
|
|
|
limits = self.inspection_limits
|
|
counts = {"files": 0, "directories": 0, "total_bytes": 0, "tensor_files": 0}
|
|
|
|
def visit(fd: int, depth: int) -> None:
|
|
if depth > 32:
|
|
raise ValueError("checkpoint depth limit exceeded")
|
|
counts["directories"] += 1
|
|
if counts["directories"] > limits.max_directories:
|
|
raise ValueError("checkpoint directory limit exceeded")
|
|
with os.scandir(fd) as entries:
|
|
for entry in entries:
|
|
# Classification increments a bounded counter or aborts;
|
|
# no entry can bypass both the file and directory ceilings.
|
|
info = entry.stat(follow_symlinks=False)
|
|
if stat.S_ISDIR(info.st_mode):
|
|
with child_directory(fd, entry.name) as child:
|
|
visit(child, depth + 1)
|
|
elif stat.S_ISREG(info.st_mode):
|
|
counts["files"] += 1
|
|
counts["total_bytes"] += info.st_size
|
|
if counts["files"] > limits.max_files or counts["total_bytes"] > limits.max_total_bytes:
|
|
raise ValueError("checkpoint inventory limit exceeded")
|
|
if Path(entry.name).suffix in {".safetensors", ".bin", ".pt", ".pth"}:
|
|
counts["tensor_files"] += 1
|
|
else:
|
|
raise ValueError("checkpoint contains a link or special file")
|
|
|
|
source = Path(source)
|
|
try:
|
|
with open_directory(source) as fd:
|
|
visit(fd, 0)
|
|
except NotADirectoryError:
|
|
with open_directory(source.parent) as parent:
|
|
info = os.stat(source.name, dir_fd=parent, follow_symlinks=False)
|
|
if not stat.S_ISREG(info.st_mode) or info.st_size > limits.max_total_bytes:
|
|
raise ValueError("invalid checkpoint file")
|
|
counts = {"files": 1, "directories": 0, "total_bytes": info.st_size,
|
|
"tensor_files": int(source.suffix in {".safetensors", ".bin", ".pt", ".pth"})}
|
|
return counts
|