# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-emit-a-gen-ai-tool-call-span-orphaned-tool-span-attribution-under-concurrent-multi-turn-replay.md # Remnant Warrant: https://corpusfetch.com/c/when-must-an-instance-refuse-to-emit-a-gen-ai-tool-call-span-orphaned-tool-span-attribution-under-concurrent-multi-turn-replay.warrant.json # Markdown SHA-256: f3c40a1a5edc33131e33257424d7eb51b425915a46f6a4017d50abe2f8b9db81 # Code AST SHA-256: f10905003c5ec87cbb2e4f463cc8402f28a18a43133b10b4192f7505b4e4e3a1 # Verified Classes: OrphanedToolCallError, ToolCallEmission # Defensive Guards: 3 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 from dataclasses import dataclass from typing import Optional class OrphanedToolCallError(RuntimeError): """Raised when a tool span cannot be attributed to exactly one committed side effect.""" @dataclass(frozen=True) class ToolCallEmission: call_id: str # gen_ai.tool.call.id, minted by the model this emission conversation_id: str # gen_ai.conversation.id turn_index: int # monotonic per conversation checkpoint_generation: int # generation at which this emission is being made first_minted_generation: Optional[int] # generation at which call_id was first seen, if known dedup_disclosure: Optional[str] # original_execution_id if tool layer reported a dedup hit is_concurrent_branch: bool has_merge_span: bool def validate_tool_span_attribution(em: ToolCallEmission) -> str: """ Returns the span disposition: 'normal' | 'degraded' | 'suppress'. Raises OrphanedToolCallError when the emission is in the ambiguous zone and the caller has not opted into degraded emission. """ is_replay = ( em.first_minted_generation is not None and em.first_minted_generation != em.checkpoint_generation ) # Case 1: replay with no dedup disclosure -> ambiguous zone. if is_replay and em.dedup_disclosure is None: raise OrphanedToolCallError( f"tool call {em.call_id!r} in conversation {em.conversation_id!r} " f"is a replay (minted gen {em.first_minted_generation}, " f"emitting gen {em.checkpoint_generation}) with no idempotency " f"disclosure; side effect attribution is unprovable. " f"Either recover the original call id or emit a degraded span " f"(status=ERROR, error.type=orphaned_tool_call)." ) # Case 2: replay with dedup disclosure -> attribute to original execution. if is_replay and em.dedup_disclosure is not None: return "normal" # caller must rewrite gen_ai.tool.call.id to dedup_disclosure # Case 3: concurrent branch with no merge span -> ordering unrecoverable. if em.is_concurrent_branch and not em.has_merge_span: raise OrphanedToolCallError( f"tool call {em.call_id!r} in conversation {em.conversation_id!r} " f"is on a concurrent branch with no merge span; causal ordering " f"within the conversation is unrecoverable from the trace." ) return "normal" def emit_tool_span(em: ToolCallEmission, allow_degraded: bool = False) -> str: try: return validate_tool_span_attribution(em) except OrphanedToolCallError: if allow_degraded: # Emit a degraded span: visible gap, no side-effect attribution. return "degraded" # status=ERROR, error.type="orphaned_tool_call" raise