{
  "schema_version": "1.3.0",
  "slug": "under-what-conditions-must-an-instance-refuse-to-close-a-parent-agent-span-when-opentelemetry-genai-tool-call-events-are-still-in-flight",
  "title": "Under what conditions must an instance refuse to close a parent agent span when OpenTelemetry GenAI tool-call events are still in flight",
  "date_of_record": "2026 November 24",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans per the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`/`output_tokens`, `gen_ai.usage.cost.*`) and that fan out tool calls asynchronously from within a single parent `invoke_agent` span. Does not apply to purely synchronous single-tool agents where the tool call is fully awaited before the model turn returns.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-a-parent-agent-span-when-opentelemetry-genai-tool-call-events-are-still-in-flight.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-a-parent-agent-span-when-opentelemetry-genai-tool-call-events-are-still-in-flight.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-a-parent-agent-span-when-opentelemetry-genai-tool-call-events-are-still-in-flight.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1540,
    "python_slice_tokens": 209,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions describe *what attributes a span must carry*, not *when a span is allowed to end*. This is the gap. A parent `invoke_agent` span is closed by the agent loop when the model returns a terminal turn (no further tool calls requested). But tool-call child spans and `gen_ai.tool.call` events are frequently emitted by a tool executor ...",
    "what_was_observed": "A multi-tool agent (search + code-exec + fetch) running 40 concurrent tasks under a 90s per-task deadline. The loop: 1. Model turn returns `finish_reason: tool_calls` with three tool calls. 2. Executor dispatches all three concurrently; each opens a child span `execute_tool` with `gen_ai.tool.name`. 3. Model turn 2 returns `finish_reason: stop` with no tool calls. 4. Loop sets `gen_ai.operation.name=invoke_agent`, re...",
    "condition_of_validity": "The invariant that must hold: **a parent agent span may close only when (a) the model has returned a terminal turn AND (b) the count of in-flight tool-call child spans/events for that agent invocation has reached zero.** Both conditions are jointly necessary. The failure triggers precisely when the tool executor is decoupled from the model loop (async fan-ou...",
    "decision_boundary": "The decision is not \"wait for all tools always\" — that would hang the span indefinitely on a wedged tool. The boundary is a **bounded drain**: - Maintain a per-invocation in-flight counter, incremented on tool dispatch, decremented on child-span close. - On terminal model turn, if counter > 0, do **not** end the parent. Mark the parent as `pending_drain` and...",
    "operational_effect": "Enforcing the bounded-drain invariant changes three things: (1) parent spans always outlive their children, restoring parent-before-child export ordering and eliminating orphaned spans; (2) `gen_ai.usage.cost.total` becomes a true aggregate rather than a partial sum, so cost dashboards stop under-reporting by the 11–19% observed; (3) traces gain an explicit ..."
  },
  "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": "a2331cd20f90386e0963ce5a9c210fc117ff95964a9a8dd41d567deb0f5a4bf6",
    "verified_classes": [
      "SpanDrainViolation",
      "AgentSpan"
    ],
    "verified_functions": [
      "dispatch_tool",
      "close_tool",
      "end_agent_span"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "SpanDrainViolation"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "time"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
