# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-a-resumed-mcp-streamable-http-session-against-the-last-event-id-replay-watermark-before-trusting-tool-call-results.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-a-resumed-mcp-streamable-http-session-against-the-last-event-id-replay-watermark-before-trusting-tool-call-results.warrant.json
# Markdown SHA-256:   7ae4682fe2e96b9afeba610ca7166c6f026cb135d95f2261c3559caa37340798
# Code AST SHA-256:   12c5d5ae4aa75e88ad6d0171349f23b79897cd04275348030d31c6442bb17c59
# Verified Classes:   ResumptionReconciliationError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ResumptionReconciliationError(RuntimeError):
    """Raised when a resumed MCP session cannot prove request completion."""
    pass


def reconcile_resumed_session(
    last_event_id: int,
    replay_buffer_floor: int,
    in_flight_ids: set[int],
    completed_ledger: dict[int, object],
    task_status_lookup,  # callable(id) -> "done"|"running"|"unknown"|None
) -> dict[int, object]:
    """
    Decide, for each in-flight request, whether its result is trusted,
    must be re-fetched, or must be failed. Never infers from stream order.
    """
    trusted: dict[int, object] = {}

    gap_exists = last_event_id < replay_buffer_floor
    if gap_exists:
        # Cannot trust stream continuity for ANY unresolved request.
        for req_id in in_flight_ids:
            if req_id in completed_ledger:
                trusted[req_id] = completed_ledger[req_id]
                continue
            status = task_status_lookup(req_id)
            if status == "done":
                # Must fetch authoritative result; cannot reconstruct from stream.
                raise ResumptionReconciliationError(
                    f"request {req_id} completed server-side but result event "
                    f"is below replay floor ({replay_buffer_floor}); "
                    f"re-fetch via tasks/get before proceeding"
                )
            elif status == "running":
                raise ResumptionReconciliationError(
                    f"request {req_id} still running across gap; "
                    f"re-attach with idempotency key, do not re-issue blindly"
                )
            else:
                raise ResumptionReconciliationError(
                    f"request {req_id} state unknown after replay gap; "
                    f"fail explicitly rather than trust partial events"
                )
        return trusted

    # No gap: route strictly by id, but only trust ledger entries.
    for req_id in in_flight_ids:
        if req_id not in completed_ledger:
            raise ResumptionReconciliationError(
                f"request {req_id} has no terminal event at or before "
                f"Last-Event-ID {last_event_id}; cannot assume completion"
            )
        trusted[req_id] = completed_ledger[req_id]
    return trusted
