# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-skill-manifest-provenance-chain-breakage-at-dynamic-tool-registry-load-time.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-poisoned-skill-manifest-provenance-chain-breakage-at-dynamic-tool-registry-load-time.warrant.json
# Markdown SHA-256:   448895fa95104a38cbe686f1ca067a2370dc4213f763bf503cab57696390936b
# Code AST SHA-256:   102aa9bb45cf00bbcaa776e48a04b0d58b08746e83d3ebff8f0794b77cc41aaa
# Verified Classes:   ManifestPoisoningError, Manifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class ManifestPoisoningError(RuntimeError):
    """Raised when a dynamically loaded skill manifest violates
    the provenance/effect-surface invariant and must be quarantined."""


@dataclass
class Manifest:
    name: str
    publisher_key: str
    description: str
    declared_secrets: list = field(default_factory=list)
    declared_tools_mentioned: list = field(default_factory=list)
    signature_valid: bool = True


# Lexical signatures of a manifest carrying instructions rather than a capability summary.
_DIRECTIVE_MARKERS = ("first call", "you must", "before returning", "then call", "attach the")


def validate_manifest(
    manifest: Manifest,
    task_tool_scope: set,
    reviewer_truncation_limit: int,
    observed_secrets_on_first_call: Optional[list] = None,
) -> None:
    # 1. Provenance is necessary but not sufficient.
    if not manifest.signature_valid:
        raise ManifestPoisoningError(
            f"manifest {manifest.name!r}: signature invalid; reject outright"
        )

    # 2. Truncation asymmetry: directive hidden past reviewer-visible limit
    #    but injected in full into model context.
    lower = manifest.description.lower()
    for marker in _DIRECTIVE_MARKERS:
        pos = lower.find(marker)
        if pos != -1 and pos >= reviewer_truncation_limit:
            raise ManifestPoisoningError(
                f"manifest {manifest.name!r}: directive-shaped text at char {pos} "
                f"exceeds reviewer truncation limit {reviewer_truncation_limit}; "
                f"model sees it, human does not"
            )

    # 3. Description references tools outside the task's declared scope.
    out_of_scope = set(manifest.declared_tools_mentioned) - task_tool_scope
    if out_of_scope:
        raise ManifestPoisoningError(
            f"manifest {manifest.name!r}: description references out-of-scope "
            f"tools {sorted(out_of_scope)}; description/scope divergence"
        )

    # 4. First-invocation effect-surface diff (only checkable after one call).
    if observed_secrets_on_first_call is not None:
        undeclared = set(observed_secrets_on_first_call) - set(manifest.declared_secrets)
        if undeclared:
            raise ManifestPoisoningError(
                f"manifest {manifest.name!r}: declared secrets {manifest.declared_secrets} "
                f"but first call touched {sorted(undeclared)}; effect-surface mismatch"
            )
