{
  "schema_version": "1.3.0",
  "slug": "under-what-conditions-must-an-instance-emit-a-terminal-agent-turn-span-before-its-tool-call-child-spans-have-been-exported-and-what-breaks-when-it-doe",
  "title": "Under what conditions must an instance emit a terminal agent-turn span before its tool-call child spans have been exported, and what breaks when it does",
  "date_of_record": "2026 November 27",
  "scope": "Applies to any LLM agent process that emits OpenTelemetry GenAI spans (`gen_ai.*`) using a batching `SpanProcessor` and treats the agent-turn span as the parent of tool-call child spans. Does not apply to synchronous export processors or to single-shot model-invocation spans with no tool children.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-emit-a-terminal-agent-turn-span-before-its-tool-call-child-spans-have-been-exported-and-what-breaks-when-it-doe.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-emit-a-terminal-agent-turn-span-before-its-tool-call-child-spans-have-been-exported-and-what-breaks-when-it-doe.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-emit-a-terminal-agent-turn-span-before-its-tool-call-child-spans-have-been-exported-and-what-breaks-when-it-doe.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1761,
    "python_slice_tokens": 375,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define a span *hierarchy* (`invoke_agent` → `chat` → `execute_tool`) and a set of normalized attributes (`gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.tool.name`, `gen_ai.agent.turn_id`). They do **not** define an export *ordering* invariant. The OpenTelemetry SDK guarantees that a child ...",
    "what_was_observed": "A multi-tool agent loop (planner → `chat` → `execute_tool` × N → `chat` → final) emitted telemetry under a `BatchSpanProcessor` with `schedule_delay_millis=5000` and `max_export_batch_size=512`. The turn span was ended in a `finally` clause immediately after the final `chat` span returned. Tool-call spans for the *last* tool invocation were still open because their result post-processing (a large JSON parse + token-c...",
    "condition_of_validity": "The invariant \"a turn span's tool children are present in the same export batch as the turn span\" holds only when **all** of the following are true: 1. Every `execute_tool` span is `end()`-ed strictly before the enclosing `invoke_agent` span's `end()` is called — i.e., tool span finalization is *synchronous* with the turn's control flow, not deferred to a ca...",
    "decision_boundary": "The boundary is crossed the moment the agent's control flow allows a tool span's `end()` to be scheduled after the turn span's `end()`. Concretely, the decision rule is: - **If** tool-call span finalization (including token annotation, result hashing, error classification) is performed inline in the same coroutine/task that will call `agent_turn.end()`, **th...",
    "operational_effect": "Downstream, the orphaned-tree signature is not benign: - **Cost attribution** keyed on turn spans sees `gen_ai.usage.*` totals that include tool round-trip tokens but no `execute_tool` children to distribute them to, so per-tool cost is undercounted and per-turn cost is correct — a silent skew that only surfaces when reconciling against the provider bill. - ..."
  },
  "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": "3401c7b27fc5bc97df316b943aa62a14f75cfdd29af6d433342d03c46bd3d628",
    "verified_classes": [
      "OrphanedTurnSpanError",
      "SpanHandle",
      "TurnSpan"
    ],
    "verified_functions": [
      "run_tool_call",
      "run_turn",
      "end",
      "register_child",
      "child_ended",
      "end"
    ],
    "verified_functions_count": 6,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "OrphanedTurnSpanError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "asyncio",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
