# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-enforce-a-federated-policy-invariant-when-mcp-servers-disagree-about-the-schema-of-the-same-capability.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-enforce-a-federated-policy-invariant-when-mcp-servers-disagree-about-the-schema-of-the-same-capability.warrant.json
# Markdown SHA-256:   7b040e568168a53cbb9bbaf331db448ae18f272ba9de5d74764fa347bbb524d1
# Code AST SHA-256:   a34e7e00dc524c8d8bbc803e2659e11d7cf0b885691850bcd3f79c1a674ae4c6
# Verified Classes:   SchemaProjectionError
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class SchemaProjectionError(RuntimeError):
    """Raised when a federated server's inputSchema cannot be losslessly
    projected onto the canonical capability schema for policy-constrained fields."""


CANONICAL_WRITE_FILE = {
    "path": "string:relative-sandboxed",
    "content": "string",
    "mode": "enum:append|create",
}
POLICY_CONSTRAINED_FIELDS = {"path", "mode"}


def project_schema(server_name: str, advertised: dict) -> dict:
    """Map an advertised server schema onto the canonical schema.
    Refuses (does not coerce) if a policy-constrained field has no
    1:1 syntactic+semantic counterpart in the advertised schema."""
    projection = {}
    for field, kind in CANONICAL_WRITE_FILE.items():
        if field in advertised:
            projection[field] = field
            continue
        if field in POLICY_CONSTRAINED_FIELDS:
            raise SchemaProjectionError(
                f"server={server_name!r} field={field!r} is policy-constrained "
                f"but absent from advertised schema {sorted(advertised)}; "
                f"refusing federation (no lossless projection, no coercion)"
            )
    return projection


def federate(servers: dict[str, dict]) -> dict[str, dict]:
    """Register servers only if every policy-constrained field projects.
    Fails closed at registration, before any tool call executes."""
    registry = {}
    for name, advertised in servers.items():
        registry[name] = project_schema(name, advertised)
    return registry


def evaluate_call(server: str, registry: dict, args: dict) -> None:
    """Evaluate a call against the canonical policy. Unmodeled fields are
    denied, not default-allowed; the adaptation loop cannot smuggle a
    renamed field past the evaluator."""
    if server not in registry:
        raise SchemaProjectionError(f"server {server!r} not federated")
    canonical = registry[server]
    for key in args:
        if key not in canonical:
            raise SchemaProjectionError(
                f"call to {server!r} carries unmodeled field {key!r}; "
                f"denied (default-deny on unmodeled fields)"
            )
    path = args.get("path", "")
    if ".." in path or path.startswith("/"):
        raise SchemaProjectionError(f"path {path!r} escapes sandbox")
    if args.get("mode") not in {"append", "create"}:
        raise SchemaProjectionError(f"mode {args.get('mode')!r} not allowlisted")
