# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-skill-manifest-capability-attestation-failure-at-the-registry-to-execution-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-skill-manifest-capability-attestation-failure-at-the-registry-to-execution-boundary.warrant.json
# Markdown SHA-256:   259c1d7d2c4c37d3a617532322a0e89e9c23a704234e381d65fd96c320883353
# Code AST SHA-256:   12eb2ac172f96699091260a56b410f4b23387d8dfc39568fa7d51afed71a4bc2
# Verified Classes:   CapabilityAttestationError
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class CapabilityAttestationError(RuntimeError):
    """Raised when a manifest's prose references a surface its attestation disclaims."""
    pass


def bind_skill(manifest: dict, known_tools: set[str], known_creds: set[str]) -> dict:
    declared = set(manifest.get("capabilities", []))
    declared_params = set(manifest.get("parameters", {}).keys())
    prose = " ".join(
        str(manifest.get(f, "")) for f in ("description", "usage", "notes")
    )

    # Structural signal: any known identifier named in prose but absent from
    # the declared capability/parameter surface is a cross-surface reference.
    referenced = {
        tok for tok in _tokenize(prose)
        if tok in known_tools or tok in known_creds
    }
    undeclared = referenced - declared - declared_params

    if undeclared:
        raise CapabilityAttestationError(
            f"manifest '{manifest.get('name')}' references undeclared surface "
            f"{sorted(undeclared)}; declared={sorted(declared)}. "
            f"Quarantine: prose reaches beyond attestation."
        )
    return manifest


def _tokenize(text: str) -> list[str]:
    import re
    return re.findall(r"[A-Za-z_][A-Za-z0-9_]*", text)
