# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-skill-injection-bypass-before-manifest-signature-verification-completes.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-skill-injection-bypass-before-manifest-signature-verification-completes.warrant.json
# Markdown SHA-256:   44f971eee51c85fc340a9a83556920205b995686c194b05bf947a79068a8b2a9
# Code AST SHA-256:   9a731a977ed79689790b3094bd5aba462b4c7a3dbae5b8f04a23edad39540487
# Verified Classes:   CapabilityDeltaError, SignedManifest, LoadedSkill
# Defensive Guards:   2 fail-closed invariants (zero external deps)

"""Load-time guard enforcing effective_capabilities ⊆ signed_capabilities.

Signature validity is necessary but NOT sufficient. A validly signed manifest
that under-declares capabilities must still be rejected.
"""

from dataclasses import dataclass, field


class CapabilityDeltaError(RuntimeError):
    """Raised when a live skill exceeds its signed capability declaration."""


@dataclass(frozen=True)
class SignedManifest:
    publisher: str
    digest: str
    capabilities: frozenset[str]


@dataclass
class LoadedSkill:
    manifest: SignedManifest
    # Capabilities observed during the sandboxed probation window.
    observed: set[str] = field(default_factory=set)


def verify_signature(manifest: SignedManifest, trusted_keys: dict[str, str]) -> bool:
    # Placeholder for real Ed25519 verification over the canonical digest.
    return manifest.publisher in trusted_keys


def attest_and_admit(skill: LoadedSkill, trusted_keys: dict[str, str]) -> None:
    if not verify_signature(skill.manifest, trusted_keys):
        raise CapabilityDeltaError(
            f"signature invalid for publisher {skill.manifest.publisher!r}"
        )

    signed = skill.manifest.capabilities
    effective = frozenset(skill.observed)
    excess = effective - signed

    if excess:
        # Signature passed, but the live object exceeds its declaration.
        # This is the injection boundary: fail closed, quarantine.
        raise CapabilityDeltaError(
            "effective capabilities exceed signed manifest: "
            f"undeclared={sorted(excess)} "
            f"signed={sorted(signed)} "
            f"digest={skill.manifest.digest}"
        )

    if effective < signed:
        # Benign under-use, but log: conditional capabilities can hide here.
        print(
            f"[warn] skill under-uses grant: unused={sorted(signed - effective)}"
        )
    # effective == signed or effective < signed: admit.


# Example: a validly signed manifest whose live object opened an egress hook.
_manifest = SignedManifest(
    publisher="acme",
    digest="sha256:deadbeef",
    capabilities=frozenset({"read:workspace"}),
)
_skill = LoadedSkill(manifest=_manifest, observed={"read:workspace", "net:egress"})

try:
    attest_and_admit(_skill, trusted_keys={"acme": "pubkey-material"})
except CapabilityDeltaError as exc:
    # Quarantine: do not instantiate against real state.
    print(f"QUARANTINED: {exc}")
