This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
devopscritical PriorityID: opentelemetry-carrier-immutability-enforcement

OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage

Enforces strict immutability across OpenTelemetry propagation carriers and baggage dictionaries over asynchronous task boundaries. Prevents shared mutable state from corrupting distributed trace parentage, span link integrity, and downstream context values during concurrent outbound dispatch. Guarantees that injected carrier payloads and baggage entries use copy-on-write isolation across async event loops and promise continuations.

Prohibited Anti-Pattern: In-Place Mutation of Shared Trace Propagation Carriers and Baggage Across Concurrent Task Boundaries

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage - Enforces strict immutability across OpenTelemetry propagation carriers and baggage dictionaries over asynchronous task boundaries. Prevents shared mutable state from corrupting distributed trace parentage, span link integrity, and downstream context values during concurrent outbound dispatch. Guarantees that injected carrier payloads and baggage entries use copy-on-write isolation across async event loops and promise continuations.
globs: src/**/*.ts, src/**/*.js, src/telemetry/**/*.ts, src/middleware/**/*.ts, src/tracing/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/opentelemetry-carrier-immutability-enforcement"
---

# OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage

> **Severity**: CRITICAL | **Category**: devops
> **Anti-Pattern Warning**: In-Place Mutation of Shared Trace Propagation Carriers and Baggage Across Concurrent Task Boundaries

## System Constraints
You are a Principal Software Architect enforcing production-grade OpenTelemetry Trace Context propagation. Distributed tracing systems fail catastrophically when propagation carriers or baggage objects are mutated in place across concurrent asynchronous boundaries, resulting in race conditions, detached span parentage, and trace data contamination across concurrent downstream RPCs. Mandate read-only carriers, defensive shallow copies, immutable context transforms, and isolated propagation injection.

## Strict Guidelines
- Engineers MUST ALWAYS treat incoming context carriers (e.g., HTTP request headers, messaging metadata) as read-only data structures.
- Engineers MUST NOT inject new trace contexts into existing shared request headers or carrier objects directly in-place.
- Services MUST instantiate a fresh, isolated carrier dictionary before invoking OpenTelemetry propagation APIs (e.g., `propagation.inject()`).
- Engineers MUST ALWAYS treat `Baggage` as immutable; all modifications MUST be performed using `context.with()` and copy-on-write `propagation.setBaggage()` patterns.
- Async operations dispatched in parallel (e.g., `Promise.all`, scatter-gather RPCs) MUST NEVER share a mutable outbound carrier instance.
- Code SHOULD enforce `Readonly<Record<string, string>>` or equivalent type-level immutability boundaries on all carrier extract and inject boundaries.

## Bad vs Good Implementations
### Outbound HTTP Carrier Injection Across Concurrent Requests

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Mutating `sharedHeaders` directly across parallel `Promise.all` branches creates a data race where parent context, baggage, and W3C traceparent headers can overwrite or corrupt one another between concurrent microtask ticks.
```typescript
import { propagation, context } from '@opentelemetry/api';

export async function dispatchBatchNotifications(sharedHeaders: Record<string, string>, targets: string[]): Promise<void> {
  // ANTI-PATTERN: Mutating a shared headers reference in-place causes trace context corruption across concurrent tasks
  await Promise.all(
    targets.map(async (targetUrl) => {
      propagation.inject(context.active(), sharedHeaders);
      await fetch(targetUrl, { headers: sharedHeaders });
    })
  );
}
```

#### Production Standard (Correct)
> **Standard**: Each asynchronous branch constructs a clean, isolated carrier copy prior to context injection and freezes the outbound structure, guaranteeing trace parentage isolation and preventing cross-request pollution.
```typescript
import { propagation, context } from '@opentelemetry/api';

export async function dispatchBatchNotifications(baseHeaders: Readonly<Record<string, string>>, targets: string[]): Promise<void> {
  // PRODUCTION INVARIANT: Defensive shallow clone creates an isolated carrier per async boundary
  await Promise.all(
    targets.map(async (targetUrl) => {
      const isolatedCarrier: Record<string, string> = { ...baseHeaders };
      propagation.inject(context.active(), isolatedCarrier);
      await fetch(targetUrl, {
        headers: Object.freeze(isolatedCarrier),
      });
    })
  );
}
```

### Copy-on-Write Baggage Augmentation on Task Execution Boundary

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly mutating map-based context containers violates the OpenTelemetry immutable context model and risks propagating stale or invalid entries to concurrent child spans.
```typescript
import { propagation, context } from '@opentelemetry/api';

export function executeWorkerTask(rawBaggage: Map<string, string>, taskKey: string, taskValue: string) {
  // ANTI-PATTERN: Mutating raw baggage collections directly breaches OTel API immutability contracts
  rawBaggage.set(taskKey, taskValue);
  
  const activeBaggage = propagation.getBaggage(context.active());
  // Attempting to mutate or pass raw baggage references down the pipeline
  return activeBaggage;
}
```

#### Production Standard (Correct)
> **Standard**: Uses standard OpenTelemetry immutable baggage mutation APIs, producing a new distinct Context snapshot without modifying parent execution state.
```typescript
import { propagation, context, Context } from '@opentelemetry/api';

export function executeWorkerTask(parentCtx: Context, taskKey: string, taskValue: string): Context {
  // PRODUCTION INVARIANT: Use immutable OTel Baggage builder APIs to return a copy-on-write context
  const currentBaggage = propagation.getBaggage(parentCtx) ?? propagation.createBaggage();
  const updatedBaggage = currentBaggage.setEntry(taskKey, {
    value: encodeURIComponent(taskValue),
  });

  return propagation.setBaggage(parentCtx, updatedBaggage);
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage

OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage defines an industrial-grade behavioral standard for devops engineering. Enforces strict immutability across OpenTelemetry propagation carriers and baggage dictionaries over asynchronous task boundaries. Prevents shared mutable state from corrupting distributed trace parentage, span link integrity, and downstream context values during concurrent outbound dispatch. Guarantees that injected carrier payloads and baggage entries use copy-on-write isolation across async event loops and promise continuations. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: In-Place Mutation of Shared Trace Propagation Carriers and Baggage Across Concurrent Task Boundaries Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftIn-Place Mutation of Shared Trace Propagation Carriers and Baggage Across Concurrent Task BoundariesCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS treat incoming context carriers (e.g., HTTP request headers, messaging metadata) as read-only data structures.

Engineers MUST ALWAYS treat incoming context carriers (e.g., HTTP request headers, messaging metadata) as read-only data structures.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT inject new trace contexts into existing shared request headers or carrier objects directly in-place.

Engineers MUST NOT inject new trace contexts into existing shared request headers or carrier objects directly in-place.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Services MUST instantiate a fresh, isolated carrier dictionary before invoking OpenTelemetry propagation APIs (e.g., `propagation.inject()`).

Services MUST instantiate a fresh, isolated carrier dictionary before invoking OpenTelemetry propagation APIs (e.g., `propagation.inject()`).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Engineers MUST ALWAYS treat `Baggage` as immutable

Engineers MUST ALWAYS treat `Baggage` as immutable; all modifications MUST be performed using `context.with()` and copy-on-write `propagation.setBaggage()` patterns.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Async operations dispatched in parallel (e.g., `Promise.all`, scatter-gather RPCs) MUST NEVER share a mutable outbound carrier instance.

Async operations dispatched in parallel (e.g., `Promise.all`, scatter-gather RPCs) MUST NEVER share a mutable outbound carrier instance.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Code SHOULD enforce `Readonly<Record<string, string>>` or equivalent type-level immutability boundaries on all carrier extract and inject boundaries.

Code SHOULD enforce `Readonly<Record<string, string>>` or equivalent type-level immutability boundaries on all carrier extract and inject boundaries.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is OpenTelemetry Trace Context Carrier Immutability and Copy-on-Write Baggage critical for AI coding assistants?

Enforces strict immutability across OpenTelemetry propagation carriers and baggage dictionaries over asynchronous task boundaries. Prevents shared mutable state from corrupting distributed trace parentage, span link integrity, and downstream context values during concurrent outbound dispatch. Guarantees that injected carrier payloads and baggage entries use copy-on-write isolation across async event loops and promise continuations. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (In-Place Mutation of Shared Trace Propagation Carriers and Baggage Across Concurrent Task Boundaries) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

OpenTelemetry