{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-of-a-genai-agent-span-is-closed-before-its-tool-call-child-spans-the-opentelemetry-genai-semantic-convention-invariant-t",
  "title": "What happens when an instance of a GenAI agent span is closed before its tool-call child spans: the OpenTelemetry GenAI semantic-convention invariant that tool-call spans must never outlive their enclosing agent invocation",
  "date_of_record": "2026 November 14",
  "scope": "Applies to traces emitted under the OpenTelemetry GenAI semantic conventions (`gen_ai.*`) where an agent loop (`gen_ai.operation.name` = `invoke_agent` or `chat`) emits tool-call spans (`execute_tool`) as descendants. Does not apply to flat, single-shot LLM inference spans with no tool execution, nor to non-GenAI spans that merely carry a `gen_ai.*` attribute for correlation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-of-a-genai-agent-span-is-closed-before-its-tool-call-child-spans-the-opentelemetry-genai-semantic-convention-invariant-t.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-of-a-genai-agent-span-is-closed-before-its-tool-call-child-spans-the-opentelemetry-genai-semantic-convention-invariant-t.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-of-a-genai-agent-span-is-closed-before-its-tool-call-child-spans-the-opentelemetry-genai-semantic-convention-invariant-t.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1954,
    "python_slice_tokens": 472,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions model an agent loop as a *parent* span (`invoke_agent`) that encloses zero or more *chat* spans (`chat`), each of which may enclose one or more *tool execution* spans (`execute_tool`). The non-obvious invariant is **temporal nesting, not just structural nesting**: the `execute_tool` child span must both *start after* and *end b...",
    "what_was_observed": "In a production agent runtime using an async tool executor, the following sequence reproduced a class of broken traces: 1. The `invoke_agent` span was started with `start_as_current_span(\"invoke_agent\")` and entered via context manager. 2. The agent issued a `chat` span, received a tool-call request, and dispatched the tool as an `asyncio.create_task(...)` so the loop could continue. 3. The tool task captured the *cu...",
    "condition_of_validity": "The invariant \"every `execute_tool` span is temporally contained in its enclosing `invoke_agent` span\" holds if and only if the agent span is ended *after* all tool-call futures it spawned have resolved. Concretely, the agent loop must: - Track every tool-call task it creates (e.g., a `set[asyncio.Task]`), and - `await asyncio.gather(*outstanding, return_exc...",
    "decision_boundary": "When you detect a tool span ending after its agent parent, the correct remediation depends on which of two regimes you are in: - **Regime A — the agent genuinely finished before the tool.** This is the async-dispatch bug above. The fix is to make the agent span's lifetime encompass the tool's: `await` outstanding tools before `span.end()`. Do **not** \"fix\" i...",
    "operational_effect": "The downstream consequences of getting this wrong are quiet and compounding: 1. **Latency attribution drift.** Backends that compute p99 agent-turn latency as `agent.end - agent.start` will under-report by exactly the tool tail — the portion of tool execution after the agent span closed. In the observed case, p99 was under-reported by 11% for turns that disp..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "e8ab3c92d3cc6d9cbeaebb04cf09ea02a87e1da7cb823e83c4ff358ae107c4a6",
    "verified_classes": [
      "TemporalContainmentError",
      "SpanInterval"
    ],
    "verified_functions": [
      "assert_temporal_containment",
      "validate_agent_trace"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "TemporalContainmentError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
