{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-tool-call-span-re-parenting-from-silently-corrupting-agent-turn-attribution-under-opentelemetry-genai-semantic-conventions",
  "title": "Which invariants prevent tool-call span re-parenting from silently corrupting agent turn attribution under OpenTelemetry GenAI semantic conventions",
  "date_of_record": "2026 November 26",
  "scope": "Applies to agents that emit OTel spans per tool invocation under the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`/`output_tokens`) and attribute each turn to a parent agent span. Does not cover pure tracing of non-agentic LLM calls or metrics-only pipelines.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-re-parenting-from-silently-corrupting-agent-turn-attribution-under-opentelemetry-genai-semantic-conventions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-re-parenting-from-silently-corrupting-agent-turn-attribution-under-opentelemetry-genai-semantic-conventions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-re-parenting-from-silently-corrupting-agent-turn-attribution-under-opentelemetry-genai-semantic-conventions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1733,
    "python_slice_tokens": 515,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways to model an agent turn in OTel. The **turn-as-parent** model makes each agent reasoning turn a span that owns its tool-call child spans, so `parent_span_id` encodes causality and token aggregation rolls up naturally. The **turn-as-attribute** model emits tool-call spans flat under a long-lived session/root span and tags each with `gen_ai.a...",
    "what_was_observed": "A two-tier agent (orchestrator + retrieval sub-agent) ran 40 turns. Each orchestrator turn opened a `chat` span with `gen_ai.usage.input_tokens`. Tool calls (`gen_ai.operation.name=\"execute_tool\"`, `gen_ai.tool.name=\"vector_search\"`) were emitted as children. On turn 12 the sub-agent's tool executor was invoked through an async task pool that captured the *ambient* context at pool creation, not at dispatch. The `vect...",
    "condition_of_validity": "The turn-as-parent invariant is only enforceable when three conditions hold simultaneously: 1. **Context propagation is dispatch-time, not creation-time.** The span must read `contextvars` at the moment the tool is invoked, not when the worker/task/pool object is constructed. A pool captured at construction carries the context of whatever turn created the po...",
    "decision_boundary": "The boundary is *whether to trust the turn rollup or the session rollup* when they diverge. The decision rule: - If `session_total - sum(turn_totals) > 0` and the residual equals a whole number of tool-call spans with `parent_span_id == session_root`, the divergence is a **re-parenting leak** — trust the session total, flag the orphaned spans, and re-attribu...",
    "operational_effect": "The practical consequence is that token-budget enforcement, which typically reads the turn rollup to decide whether to truncate context or halt the loop, under-counts on re-parenting leaks and over-counts on retries. An agent enforcing a 100k-token turn budget will silently permit turns that actually consumed 104k, because the leaked tool tokens never entere..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "223994816eb79666f70885c997da37520c9dc6af025d1fd13338de00f1e4036c",
    "verified_classes": [
      "TurnAttributionError",
      "Span",
      "Trace"
    ],
    "verified_functions": [
      "assert_turn_attribution",
      "reconcile_rollups",
      "detect_divergence",
      "is_tool",
      "is_chat",
      "by_id",
      "chat_spans",
      "tool_spans"
    ],
    "verified_functions_count": 8,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TurnAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
