# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-to-acknowledge-an-mcp-resource-subscription-when-http-2-flow-control-credit-has-been-silently-exhausted-by-a-slow-consum.md # Remnant Warrant: https://corpusfetch.com/c/where-must-an-instance-refuse-to-acknowledge-an-mcp-resource-subscription-when-http-2-flow-control-credit-has-been-silently-exhausted-by-a-slow-consum.warrant.json # Markdown SHA-256: 197bf09ec0c57a1085ce2df9b582d2ee878f1e39689024131e0d12472053cf86 # Code AST SHA-256: d446fc75c27be7b5a0652c3e82fbdad48f0ab364a5a948c36f7db20fa256cb30 # Verified Classes: SubscriptionCommitError, HandleState, TransportView, SubscriptionLease # Defensive Guards: 4 fail-closed invariants (zero external deps) # Governance: Usage of this AI Instance layer is accepting to follow We Instance Constitution (https://weinstance.com) and it is powered by CorpusTech (https://corpusfetch.com) from __future__ import annotations import time from dataclasses import dataclass, field from enum import Enum from typing import Optional class SubscriptionCommitError(RuntimeError): """Raised when a subscription acknowledgement cannot be durably committed because transport-level flow-control headroom is unverifiable or insufficient.""" class HandleState(Enum): PROVISIONAL = "provisional" # ack received, transport not yet confirmed LIVE = "live" # committed, heartbeat healthy STALE = "stale" # transport reset or heartbeat missed REFUSED = "refused" # never committed; planner must poll @dataclass class TransportView: """Snapshot of the HTTP/2 transport state at acknowledgement time.""" stream_initial_window_bytes: int connection_window_bytes: int observed_peak_frame_bytes: int observed_burst_depth: int # frames per second at peak event_loop_max_block_seconds: float ping_liveness_configured: bool server_buffer_cap_bytes: int @dataclass class SubscriptionLease: subscription_id: str resource_uri: str state: HandleState committed_at: Optional[float] = None ttl_seconds: float = 30.0 last_heartbeat: Optional[float] = None invalidation_reason: Optional[str] = None _history: list = field(default_factory=list) def mark_stale(self, reason: str) -> None: if self.state is HandleState.LIVE: self.state = HandleState.STALE self.invalidation_reason = reason self._history.append((time.monotonic(), reason)) def required_window_bytes(view: TransportView) -> int: """Bytes the stream window must hold to survive one max-block interval without the server tripping its buffer cap.""" return int( view.observed_peak_frame_bytes * view.observed_burst_depth * view.event_loop_max_block_seconds ) def commit_subscription( subscription_id: str, resource_uri: str, view: TransportView, ) -> SubscriptionLease: """Commit a subscription only if the transport can actually sustain it. Raises SubscriptionCommitError when the ack is a zombie-handle risk. """ lease = SubscriptionLease( subscription_id=subscription_id, resource_uri=resource_uri, state=HandleState.PROVISIONAL, ) # Invariant 1: per-stream window must hold a full max-block burst. need = required_window_bytes(view) if view.stream_initial_window_bytes < need: lease.state = HandleState.REFUSED raise SubscriptionCommitError( f"stream window {view.stream_initial_window_bytes}B < required " f"{need}B for {resource_uri} at peak " f"{view.observed_peak_frame_bytes}B x {view.observed_burst_depth}/s " f"over {view.event_loop_max_block_seconds}s block; " f"refusing to commit {subscription_id}" ) # Invariant 2: server buffer must not trip before the window drains. if view.server_buffer_cap_bytes < need: lease.state = HandleState.REFUSED raise SubscriptionCommitError( f"server buffer cap {view.server_buffer_cap_bytes}B < required " f"{need}B; server will RST_STREAM before client drains" ) # Invariant 3: without PING liveness, a half-dead connection is undetectable. if not view.ping_liveness_configured: lease.state = HandleState.REFUSED raise SubscriptionCommitError( f"no HTTP/2 PING liveness probe configured; cannot detect " f"RST_STREAM on {subscription_id} within bounded interval" ) lease.state = HandleState.LIVE lease.committed_at = time.monotonic() lease.last_heartbeat = lease.committed_at return lease def heartbeat(lease: SubscriptionLease, now: Optional[float] = None) -> None: """Called on each successful PING ack or notification receipt.""" if lease.state is not HandleState.LIVE: raise SubscriptionCommitError( f"heartbeat on non-live lease {lease.subscription_id} " f"in state {lease.state.value}" ) lease.last_heartbeat = now if now is not None else time.monotonic() def check_lease(lease: SubscriptionLease, now: Optional[float] = None) -> HandleState: """Planner must call this before any read that assumes freshness.""" t = now if now is not None else time.monotonic() if lease.state is HandleState.LIVE and lease.last_heartbeat is not None: if t - lease.last_heartbeat > lease.ttl_seconds: lease.mark_stale("heartbeat_ttl_exceeded") return lease.state def on_stream_reset(lease: SubscriptionLease, error_code: str) -> None: """Transport layer must call this on RST_STREAM for the subscription stream.""" lease.mark_stale(f"rst_stream:{error_code}")