# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-checkpoint-a-partial-a2a-artifact-stream-to-survive-a-mid-transfer-peer-eviction-without-corrupting-the-resumable-cursor.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-checkpoint-a-partial-a2a-artifact-stream-to-survive-a-mid-transfer-peer-eviction-without-corrupting-the-resumable-cursor.warrant.json
# Markdown SHA-256:   280c7e3e2b28f5f6959bc630def4ad77049d2b6562ef6e05191458cf57d70f2d
# Code AST SHA-256:   eb22b22c7b65f161f25d13e0f5604320ac92c78d250ffe911235c370c6b20836
# Verified Classes:   ResumeCursorViolation, ResumeWindowExhausted
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ResumeCursorViolation(RuntimeError):
    """Raised when a resume negotiation would silently skip or duplicate chunks."""
    pass


class ResumeWindowExhausted(ResumeCursorViolation):
    """Producer cannot re-emit from the requested cursor; gap is unrecoverable."""
    pass


def negotiate_resume(
    requested_cursor: int,
    producer_durable_offset: int,
    producer_retention_floor: int,
    consumer_durable_checkpoint: int,
) -> int:
    """
    Resolve the authoritative resume cursor under the min-cursor invariant.

    Invariants enforced:
      - cursor == min(producer_durable_offset, consumer_durable_checkpoint)
      - cursor >= producer_retention_floor (else chunks are gone)
      - neither side may unilaterally raise the cursor past the other's durable state
    """
    if requested_cursor > producer_durable_offset:
        # Consumer claims to be ahead of what the producer ever emitted durably.
        # This is the eviction-reorder signature: emission-coupled offset advanced
        # past the chunk the consumer actually holds. Never honor the forward jump.
        raise ResumeCursorViolation(
            f"requested_cursor={requested_cursor} exceeds "
            f"producer_durable_offset={producer_durable_offset}; "
            f"refusing forward seek (would skip unflushed chunks)"
        )

    if requested_cursor > consumer_durable_checkpoint:
        # Consumer is asking to resume from a point it has not durably checkpointed.
        # Accepting would mean replaying into a non-idempotent reducer gap.
        raise ResumeCursorViolation(
            f"requested_cursor={requested_cursor} exceeds "
            f"consumer_durable_checkpoint={consumer_durable_checkpoint}; "
            f"consumer must not claim unflushed state"
        )

    cursor = min(producer_durable_offset, consumer_durable_checkpoint, requested_cursor)

    if cursor < producer_retention_floor:
        # The gap between cursor and retention floor has been GC'd.
        # Fast-forwarding silently is the original bug; fail loudly instead.
        raise ResumeWindowExhausted(
            f"cursor={cursor} below retention_floor={producer_retention_floor}; "
            f"chunks [{cursor}, {producer_retention_floor}) unrecoverable — "
            f"artifact segment must be marked LOSSY"
        )

    return cursor
