# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-opentelemetry-genai-span-attributes-as-unverifiable-until-the-tool-call-result-is-reconciled-against-the-reco.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-opentelemetry-genai-span-attributes-as-unverifiable-until-the-tool-call-result-is-reconciled-against-the-reco.warrant.json
# Markdown SHA-256:   b1bbfe88b0a834989f08e5f9f74a281fe162541fdaedb514e98a4b39a0499e90
# Code AST SHA-256:   85869d0edabe200fecb7cf46235fe69a510be409d2475bca5c54556c997a8c2d
# Verified Classes:   UnverifiableToolCallSpan, GenAISpan
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class UnverifiableToolCallSpan(RuntimeError):
    """Raised when a GenAI tool-call span cannot be reconciled against its token ledger."""
    pass


@dataclass
class GenAISpan:
    name: str
    output_tokens: int
    input_tokens: int
    context_ceiling: int
    has_tool_call: bool
    trace_flags: str
    tool_result_count: int


def reconcile_tool_call_span(span: GenAISpan, parent_trace_flags: str) -> None:
    if span.has_tool_call and span.output_tokens == 0:
        raise UnverifiableToolCallSpan(
            f"span {span.name!r}: tool call recorded with output_tokens=0; "
            "cannot distinguish saturation artifact from genuine call"
        )
    if span.input_tokens >= span.context_ceiling:
        raise UnverifiableToolCallSpan(
            f"span {span.name!r}: input_tokens={span.input_tokens} at context ceiling "
            f"{span.context_ceiling}; tool arguments may be degenerate"
        )
    if span.trace_flags != parent_trace_flags:
        raise UnverifiableToolCallSpan(
            f"span {span.name!r}: trace_flags transitioned "
            f"{parent_trace_flags!r}->{span.trace_flags!r}; downstream accounting voided"
        )
    if span.has_tool_call and span.tool_result_count == 0 and span.output_tokens == 0:
        raise UnverifiableToolCallSpan(
            f"span {span.name!r}: empty result with zero generation is unverified"
        )
