{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-span-orphaning-from-silently-corrupting-tool-call-latency-attribution-in-agent-trajectory-traces",
  "title": "Which invariants prevent span-orphaning from silently corrupting tool-call latency attribution in agent trajectory traces",
  "date_of_record": "2026 November 29",
  "scope": "Applies to agents whose runtime emits OpenTelemetry-style span trees (agent-loop span → LLM-turn span → tool-call span) and where latency attribution is computed by parent-child span subtraction. Does not apply to flat log-only tracing or to systems where tool latency is measured in-band by the tool executor itself.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-orphaning-from-silently-corrupting-tool-call-latency-attribution-in-agent-trajectory-traces.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-orphaning-from-silently-corrupting-tool-call-latency-attribution-in-agent-trajectory-traces.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-orphaning-from-silently-corrupting-tool-call-latency-attribution-in-agent-trajectory-traces.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1787,
    "python_slice_tokens": 383,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"a slow tool.\" It is a **span-orphaning discontinuity**: a tool-call span that begins while its parent LLM-turn span has already been closed (or is closed by a *different* coroutine than the one that opened it), so the child's `start_time` falls outside the parent's `[start, end]` interval. Naive attribution logic computes `llm_latency = p...",
    "what_was_observed": "In a ReAct-style loop with parallel tool dispatch (`asyncio.gather` over N tool calls per turn), the following trace pattern recurred under load: 1. LLM-turn span `T_k` opens, records `gen_ai.usage.completion_tokens`, and the model emits a batch of `tool_use` blocks. 2. The runtime closes `T_k` *before* awaiting the tool futures — a common pattern where the \"thinking\" span is scoped to the model call only. 3. Each to...",
    "condition_of_validity": "The invariant that must hold for latency attribution to be conservative (sum of attributed ≤ wall clock, with residual ≥ 0) is: > **Containment invariant:** for every tool-call span `C`, `C.parent_span_id` is set at span *creation* to the LLM-turn span that *emitted* the tool_use block, and `C.start_time ≥ parent.start_time` and `C.end_time ≤ parent.end_time...",
    "decision_boundary": "The boundary is where you choose between two mutually exclusive designs: - **Turn-scoped span (invariant-preserving):** turn span opens before model call, closes after all tool futures resolve. Attribution is a clean subtraction; orphans are impossible by construction because the parent cannot close before its children. Cost: the turn span's duration include...",
    "operational_effect": "After enforcing the containment invariant (turn span held open across tool await, exporter configured `reconcile_parent=false`, malformed orphans rejected with a counter increment), the observed effects were: (1) the residual `wall_clock − Σ attributed` dropped from a bimodal distribution with a negative tail to a tight positive band, restoring the anomaly d..."
  },
  "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": "88fef4daacc975d749c681aa9ef94acf04fed49d418ff383ef08b158f4f1acec",
    "verified_classes": [
      "SpanContainmentViolation",
      "Span"
    ],
    "verified_functions": [
      "attribute_turn_latency",
      "simulate_orphan_race",
      "duration"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "SpanContainmentViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
