"""Test-only seams. Nothing in the production code path may import this module. Startup admits a node only on a capability report produced by a *real* forward through the loaded shard (see :mod:`meshnet_node.admission`). Tests run against fake or stub backends that cannot perform one, so they pass an explicit validator from here instead — the honest statement being "this test asserts capability it never proved", which is a thing a test may do and a node may not. `capability_stub` builds the deliberately-wrong reports the fail-closed tests need: a failed one, one for another model or shard, one that has aged out. """ from __future__ import annotations import time from typing import Any from .admission import CapabilityContext, CapabilityValidator from . import __version__ as _PACKAGE_VERSION from .capability import STATUS_PASSED, CapabilityReport, build_capability_report from .gguf_ownership import authoritative_dense_llama_ownership from .runtime_recipe import build_runtime_recipe_identity def capability_report_for( context: CapabilityContext, *, status: str = STATUS_PASSED, model_id: str | None = None, shard_start: int | None = None, shard_end: int | None = None, recipe_id: str | None = None, recipe_version: str | None = None, backend_id: str | None = None, device: str | None = None, artifact_hash: str | None = None, activation_dtype: str | None = None, compute_dtype: str | None = None, kv_dtype: str | None = None, kv_layout: str | None = None, tokenizer_revision: str | None = None, architecture_adapter: str | None = None, boundary_schema_version: int = 1, cache_layout: str | None = None, validated_at: float | None = None, age_seconds: float = 0.0, diagnostics: Any = None, duration_ms: int = 0, ) -> CapabilityReport: """A report describing `context`, with any field bent away from the truth.""" now = time.time() if validated_at is None else validated_at backend = getattr(context, "backend", None) model_config = getattr(getattr(backend, "model", None), "config", None) model_config_payload = ( model_config.to_dict() if hasattr(model_config, "to_dict") else model_config ) resolved_cache_layout = ( "stateless" if getattr(backend, "supports_kv_cache", False) is False else "local-hot-kv" ) ownership = authoritative_dense_llama_ownership(backend, context.selection) runtime_recipe = build_runtime_recipe_identity( model_id=context.selection.model_id, revision=getattr(getattr(backend, "model", None), "revision", None), model_config=model_config_payload, recipe_params=context.recipe.params, weight_quantization=context.selection.quantization, backend_id=context.recipe.backend_id, runtime_version=_PACKAGE_VERSION, activation_dtype=activation_dtype, compute_dtype=compute_dtype, kv_dtype=kv_dtype, kv_layout=kv_layout or _backend_kv_layout(backend), tokenizer_revision=tokenizer_revision, architecture_adapter=architecture_adapter, boundary_schema_version=boundary_schema_version, cache_layout=cache_layout or resolved_cache_layout, ) return build_capability_report( model_id=model_id or context.selection.model_id, shard_start=ownership.start_layer if shard_start is None else shard_start, shard_end=ownership.end_layer if shard_end is None else shard_end, recipe_id=recipe_id or context.recipe.id, recipe_version=recipe_version or context.recipe.version, catalogue_version=context.manifest.catalogue_version, backend_id=backend_id or context.recipe.backend_id, device=device or context.device, quantization=context.selection.quantization, runtime=_runtime_versions(), artifact_hash=artifact_hash, runtime_recipe=runtime_recipe, owns_embedding=ownership.owns_embedding, owns_final_head=ownership.owns_final_head, status=status, duration_ms=duration_ms, diagnostics=diagnostics, validated_at=now - age_seconds, ) def assume_capability(context: CapabilityContext) -> CapabilityReport: """Assert the selection works, without proving it. Tests only.""" return capability_report_for(context) def capability_stub(**overrides: Any) -> CapabilityValidator: """A validator producing a report that deviates from `context` as named.""" def validator(context: CapabilityContext) -> CapabilityReport: return capability_report_for(context, **overrides) return validator def _runtime_versions() -> dict[str, str]: versions: dict[str, str] = {} for name in ("torch", "transformers"): try: module = __import__(name) except Exception: continue version = getattr(module, "__version__", None) if version: versions[name] = str(version) return versions def _backend_kv_layout(backend: Any) -> str: return "session-cache" if getattr(backend, "supports_kv_cache", False) else "stateless"