# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-agent-spans-with-a-missing-or-reset-trace-id-across-a-mid-task-checkpoint-boundary.md # Remnant Warrant: https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-agent-spans-with-a-missing-or-reset-trace-id-across-a-mid-task-checkpoint-boundary.warrant.json # Markdown SHA-256: 4a79073b1bcd0c1b5654a1ddcab802185a0946d3950eb9b222f6b5ecd914a6ed # Code AST SHA-256: 4712991234549718f36d8174bc05d87238c92e7155e1197cc9099a0daf0a9520 # Verified Classes: TraceContinuityViolation, SpanContext, Checkpoint, AgentRuntime # 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, field from typing import Any class TraceContinuityViolation(RuntimeError): """Raised when a resumed agent instance cannot prove trace continuity across a checkpoint boundary, or when replayed history is emitted as novel tool-call spans.""" @dataclass class SpanContext: trace_id: str span_id: str trace_flags: int = 1 @dataclass class Checkpoint: agent_id: str messages: list[dict[str, Any]] span_context: SpanContext | None = None # must be persisted for continuity replayed_tool_call_ids: set[str] = field(default_factory=set) @dataclass class AgentRuntime: agent_id: str def resume(self, cp: Checkpoint) -> SpanContext: if cp.agent_id != self.agent_id: raise TraceContinuityViolation( f"checkpoint agent_id {cp.agent_id!r} != runtime {self.agent_id!r}" ) if cp.span_context is None: # Span-context-blind checkpoint: we cannot join to the prior trace. # Refuse to silently fork a new trace without an explicit link marker. raise TraceContinuityViolation( "checkpoint lacks SpanContext; resumed root would fork a new trace_id " "and sever causal continuity. Persist SpanContext or set an explicit " "gen_ai.agent.resume_of link before proceeding." ) return cp.span_context def emit_tool_span( self, ctx: SpanContext, tool_call_id: str, replayed: bool ) -> dict[str, Any]: if replayed and tool_call_id not in ctx.__dict__.get("_seen", set()): raise TraceContinuityViolation( f"tool_call_id {tool_call_id!r} emitted as novel work but flagged " f"replayed; cost aggregation would double-count it" ) return { "trace_id": ctx.trace_id, "parent_span_id": ctx.span_id, "gen_ai.tool.call.id": tool_call_id, "gen_ai.tool.call.replayed": replayed, } def resume_agent(runtime: AgentRuntime, cp: Checkpoint) -> SpanContext: ctx = runtime.resume(cp) # Guard: replayed history must not be re-spanned as new work. for tc_id in cp.replayed_tool_call_ids: runtime.emit_tool_span(ctx, tc_id, replayed=True) return ctx