{
  "schema_version": "1.3.0",
  "slug": "where-must-an-instance-of-a-safety-intervention-hook-be-bound-in-the-otel-genai-span-lifecycle-to-remain-trace-linkable",
  "title": "Where must an instance of a safety intervention hook be bound in the OTel GenAI span lifecycle to remain trace-linkable",
  "date_of_record": "2026 November 13",
  "scope": "This record applies to systems that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) and attach safety-intervention hooks (content filters, tool-use gates, policy blocks) that must be correlated to the originating trace via span context. It does not apply to out-of-band moderation pipelines that operate on post-hoc logs, nor to hooks that are intentionally decoupled from request latency.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-safety-intervention-hook-be-bound-in-the-otel-genai-span-lifecycle-to-remain-trace-linkable.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-safety-intervention-hook-be-bound-in-the-otel-genai-span-lifecycle-to-remain-trace-linkable.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/where-must-an-instance-of-a-safety-intervention-hook-be-bound-in-the-otel-genai-span-lifecycle-to-remain-trace-linkable.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1374,
    "python_slice_tokens": 121,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The non-obvious boundary is between **hook binding at span creation** versus **hook binding at span end**. A safety intervention hook that raises, mutates, or short-circuits must be bound to the *active* span context at the moment the intervention decision is made — that is, while the `gen_ai.chat` (or `gen_ai.tool.call`) span is still the current span on th...",
    "what_was_observed": "In a production GenAI gateway, a content-safety hook was registered as a post-processing callback on the response object. The hook fired correctly and blocked the response — the user never saw the unsafe content. However, the OTel trace for those requests showed `gen_ai.chat` spans with `status=OK`, no `exception` event, and no `gen_ai.safety.intervention` attribute. The intervention was real at the application layer...",
    "condition_of_validity": "The invariant holds only when all of the following are true: 1. The hook executes **synchronously within the lexical scope** of the `start_as_current_span` context manager (or an equivalent `use_span` block), before `end()` is called. 2. For async code, the hook runs in a task that **inherits the context** established by `start_as_current_span` — i.e., the t...",
    "decision_boundary": "The decision is **where to bind the hook**, and it is a binary choice with a hard edge: - **Bind at span creation (inside the context):** Use this when the hook must be trace-linkable and the intervention is part of the request lifecycle. The hook is a wrapper *around* the model call, invoked while the span is current. This is the default and correct choice ...",
    "operational_effect": "When the invariant is violated, the observable effect is **silent trace corruption**: safety events vanish from traces while the application behaves correctly. This defeats the primary purpose of embedding hooks in spans — auditability and post-incident reconstruction. Dashboards show 100% clean completions; incident response cannot find the blocked requests..."
  },
  "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": "ac96cea7c48c1d0b8289ec173d64a71dfa756ea1bb62d31b792696d492b24224",
    "verified_classes": [
      "SafetyHookBindingError"
    ],
    "verified_functions": [
      "record_safety_intervention"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "SafetyHookBindingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "opentelemetry"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
