# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-trust-anchor-epoch-skew-during-cross-org-capability-exchange-before-policy-translation-silently-widens-scope.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-trust-anchor-epoch-skew-during-cross-org-capability-exchange-before-policy-translation-silently-widens-scope.warrant.json
# Markdown SHA-256:   6680a03aad24247e38571017c96be7879c740daa43cfcdbb4056938b7a6da101
# Code AST SHA-256:   9e0ce68d589eb238e63439e871c82f93bf52540888cdc485d389911d826cb488
# Verified Classes:   EpochSkewViolation, EpochManifest
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Literal


class EpochSkewViolation(RuntimeError):
    """Raised when a credential signed under epoch e is translated by a
    translator whose semantics diverge from e in a way that widens scope."""


@dataclass(frozen=True)
class EpochManifest:
    epoch: str
    default_verb: Literal["deny", "allow", "inherit-from-parent"]
    scope_arity: int  # 1 = flat string, 2 = structured object


def translate_capability(
    credential_epoch: str,
    translator_epoch: str,
    manifests: dict[str, EpochManifest],
    credential_scope: object,
) -> dict:
    if credential_epoch not in manifests or translator_epoch not in manifests:
        raise EpochSkewViolation(
            f"missing manifest for epoch {credential_epoch!r} or {translator_epoch!r}"
        )

    src = manifests[credential_epoch]
    dst = manifests[translator_epoch]

    # The boundary: default flips or arity changes forbid shimmed translation.
    if src.default_verb != dst.default_verb or src.scope_arity != dst.scope_arity:
        raise EpochSkewViolation(
            f"epoch skew {credential_epoch}->{translator_epoch} touches "
            f"default_verb ({src.default_verb}->{dst.default_verb}) or "
            f"scope_arity ({src.scope_arity}->{dst.scope_arity}); "
            f"refuse shimmed translation, pin to {credential_epoch} semantics"
        )

    # Safe path: epochs are semantically compatible; translate and tag.
    return {
        "scope": credential_scope,
        "epoch": credential_epoch,  # tag, not translator_epoch
        "translated_by": translator_epoch,
    }
