# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-workload-identity-attestation-has-silently-decoupled-from-agent-behavior-in-aaif-governed-autonomous-agents.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-workload-identity-attestation-has-silently-decoupled-from-agent-behavior-in-aaif-governed-autonomous-agents.warrant.json
# Markdown SHA-256:   bf20b2535fda2834c1da5700f58be4519b892889ddc2528ff74467f101e5429a
# Code AST SHA-256:   296bee669a612c7fa7f6615227e4f39c3af6313a4767c6548eb1cdb5c0df333a
# Verified Classes:   AttestationBehaviorDecoupling
# Defensive Guards:   5 fail-closed invariants (zero external deps)

class AttestationBehaviorDecoupling(RuntimeError):
    """Raised when a valid AAIF attestation no longer describes the acting agent's behavior."""
    pass


def authorize_call(
    token,                 # verified AAIF attestation: sub, iat, exp, scopes, task_class
    call,                  # (tool, endpoint, resource)
    provenance,            # "root_prompt" | "untrusted_ingested" | "peer_agent"
    exercised_surface,     # set of (tool, endpoint, resource) seen since iat
    surface_budget,        # int, calibrated at issuance
    task_class_similarity, # float in [0,1], current root prompt vs token.task_class
    now,
    cold_scope_k=5,
    task_class_floor=0.55,
):
    if not (token.iat <= now <= token.exp):
        raise AttestationBehaviorDecoupling(
            f"token outside validity window: iat={token.iat} exp={token.exp} now={now}"
        )
    if call not in token.scopes:
        raise AttestationBehaviorDecoupling(
            f"call {call} not in attested scopes {token.scopes}"
        )

    # Cold-scope rule: a scope first exercised late under untrusted provenance is suspect.
    if provenance == "untrusted_ingested" and call not in exercised_surface:
        calls_since_iat = len(exercised_surface)
        if calls_since_iat > cold_scope_k:
            raise AttestationBehaviorDecoupling(
                f"cold scope {call} first exercised at call #{calls_since_iat} "
                f"under untrusted provenance; token minted for {token.task_class}"
            )

    # Surface-expansion budget: drift shows as monotonic surface growth under a fixed token.
    proposed_surface = exercised_surface | {call}
    if len(proposed_surface) > surface_budget:
        raise AttestationBehaviorDecoupling(
            f"surface expansion {len(proposed_surface)} > budget {surface_budget} "
            f"under token {token.sub}; re-attest required"
        )

    # Task-class match: the token's issuance context must still describe the acting agent.
    if task_class_similarity < task_class_floor:
        raise AttestationBehaviorDecoupling(
            f"task-class similarity {task_class_similarity:.2f} < floor {task_class_floor}; "
            f"agent intent has decoupled from issuance context {token.task_class}"
        )

    return True  # token_valid AND provenance_ok AND surface_within_budget AND task_class_match
