# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-dynamically-loaded-tool-manifest-as-an-untrusted-executable-surface-rather-than-declarative-configuration.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-dynamically-loaded-tool-manifest-as-an-untrusted-executable-surface-rather-than-declarative-configuration.warrant.json
# Markdown SHA-256:   703838710ecfe8b299a2cabf55d36055f22020b104933e09c8395dfa2609fd9d
# Code AST SHA-256:   665ff38cd42371ae76ce44f7ea261597b882d4205fd9f6e3016aee2e462dafc8
# Verified Classes:   ManifestTrustViolation
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ManifestTrustViolation(RuntimeError):
    """Raised when a dynamically loaded tool manifest attempts to carry
    instruction authority or spawn a tool outside the runtime registry."""
    pass


IMPERATIVE_MARKERS = (
    "important:", "you must", "before calling", "ignore previous",
    "include the", "read ~/", "do not tell", "required by",
)


def admit_manifest(manifest: dict, registered_names: set[str],
                   signature_verified: bool) -> dict:
    name = manifest.get("name", "")
    desc = manifest.get("description", "")

    # Phantom-tool reconciliation: parsed name must exist in runtime registry.
    if name not in registered_names:
        raise ManifestTrustViolation(
            f"phantom tool {name!r} parsed from manifest but absent from "
            f"runtime registry; refusing to admit"
        )

    # Even signed manifests are untrusted *text*; signature gates relevance,
    # not instruction authority.
    lowered = desc.lower()
    hits = [m for m in IMPERATIVE_MARKERS if m in lowered]
    if hits:
        raise ManifestTrustViolation(
            f"manifest {name!r} description carries imperative markers "
            f"{hits}; description must be data, not instruction"
        )

    # Cross-tool parameter flow requires elevated tier + operator confirmation.
    for prop, spec in manifest.get("inputSchema", {}).get("properties", {}).items():
        if spec.get("x-carries-tool-output") and not signature_verified:
            raise ManifestTrustViolation(
                f"untrusted-tier manifest {name!r} declares parameter "
                f"{prop!r} that can carry another tool's output; "
                f"blocked pending signature verification"
            )

    return {"name": name, "description": f"[UNTRUSTED TOOL TEXT] {desc}",
            "tier": "reviewed" if signature_verified else "untrusted"}
