{"version":3,"sources":["../../src/_internal/env.ts","../../src/_internal/redaction.ts","../../src/_internal/diagnostics.ts","../../src/_internal/otel-provider.browser.ts","../../src/_internal/revision.ts","../../src/_internal/spans.ts","../../src/_internal/agent-name.ts","../../src/_internal/attribution.ts","../../src/_internal/tracing.ts","../../src/propagation.ts"],"names":["readEnv","mod","warned","setRedactionConfig","out","timer","_resetForTesting"],"mappings":";;;AAoDO,IAAM,gBAAA,GAAmB;AAWhC,SAAS,QAAQ,GAAA,EAAiC;AAEhD,EAAA,IAAI,OAAO,OAAA,KAAY,WAAA,IAAe,OAAA,CAAQ,GAAA,EAAK;AACjD,IAAA,MAAM,CAAA,GAAI,OAAA,CAAQ,GAAA,CAAI,GAAG,CAAA;AACzB,IAAA,IAAI,OAAO,CAAA,KAAM,QAAA,EAAU,OAAO,CAAA;AAAA,EACpC;AAGA,EAAA,MAAM,aAAc,UAAA,CAAmB,IAAA;AAGvC,EAAA,IAAI,UAAA,EAAY,KAAK,GAAA,EAAK;AACxB,IAAA,IAAI;AACF,MAAA,MAAM,CAAA,GAAI,UAAA,CAAW,GAAA,CAAI,GAAA,CAAI,GAAG,CAAA;AAChC,MAAA,IAAI,OAAO,CAAA,KAAM,QAAA,EAAU,OAAO,CAAA;AAAA,IACpC,CAAA,CAAA,MAAQ;AAAA,IAER;AAAA,EACF;AACA,EAAA,OAAO,MAAA;AACT;AAEA,SAAS,OAAO,KAAA,EAAoC;AAClD,EAAA,IAAI,CAAC,OAAO,OAAO,KAAA;AACnB,EAAA,OAAO,CAAC,GAAA,EAAK,MAAA,EAAQ,KAAA,EAAO,IAAI,CAAA,CAAE,QAAA,CAAS,KAAA,CAAM,IAAA,EAAK,CAAE,WAAA,EAAa,CAAA;AACvE;AAEO,SAAS,SAAA,GAAgC;AAC9C,EAAA,MAAM,GAAA,GAAM,QAAQ,eAAe,CAAA;AACnC,EAAA,IAAI,CAAC,KAAK,OAAO,MAAA;AACjB,EAAA,MAAM,OAAA,GAAU,IAAI,IAAA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AAA,IAAI,OAAO,CAAA,KAAM,QAAA,EAAU,OAAO,CAAA;AAClC,MAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA,IAAK,EAAE,MAAA,GAAS,CAAA,IAAK,OAAO,CAAA,CAAE,CAAC,CAAA,KAAM,QAAA,EAAU,OAAO,EAAE,CAAC,CAAA;AAC5E,MAAA,OAAO,IAAA;AAAA,IACT;AAAA,EACF;AACA,EAAA,OAAO,IAAA;AACT;AAEA,SAAS,SAAA,CAAU,OAAA,EAAsB,IAAA,EAAc,KAAA,EAAqB;AAC1E,EAAA,IAAI,mBAAmB,OAAA,EAAS;AAC9B,IAAA,OAAA,CAAQ,GAAA,CAAI,MAAM,KAAK,CAAA;AACvB,IAAA;AAAA,EACF;AAEA,EAAA,MAAM,WAAY,OAAA,CAAgB,GAAA;AAClC,EAAA,IAAI,OAAO,aAAa,UAAA,EAAY;AAClC,IAAA,QAAA,CAAS,IAAA,CAAK,OAAA,EAAS,IAAA,EAAM,KAAK,CAAA;AAClC,IAAA;AAAA,EACF;AACA,EAAC,OAAA,CAAmC,IAAI,CAAA,GAAI,KAAA;AAC9C","file":"chunk-6UD56LGU.cjs","sourcesContent":["/**\n * Environment variable resolution for SDK auto-init.\n *\n * The V2 TS SDK supports zero-setup integration: the customer sets\n * `MORSE_API_KEY` in their process environment (Node) or supplies\n * it via build-time replacement (browser bundler), and the SDK\n * auto-initializes on first use. This module centralises the env-var\n * contract so the rules stay in one place.\n *\n * Env vars (all optional unless noted) — must mirror the Python SDK's\n * `_internal/env.py`:\n *\n * - `MORSE_API_KEY`              — triggers auto-init when set; absent = SDK dormant\n * - `MORSE_ENDPOINT`             — override default ingestion URL\n * - `MORSE_FLUSH_INTERVAL_SECONDS` — override default 2.0s flush interval\n * - `MORSE_DISABLED`             — \"1\"/\"true\" makes all SDK calls no-op\n * - `MORSE_SDK_DEBUG`            — \"1\"/\"true\" raises internal-error log level\n * - `MORSE_SDK_DISABLE_TELEMETRY` — \"1\"/\"true\" suppresses out-of-band SDK\n *   self-diagnostics (errors + delivery counters). Local logging is\n *   unaffected — see SDKDIAG-10.\n * - `MORSE_DISABLE_AUTO_DETECT`   — (MHQ-482, mirrors Python's `MORSE_DISABLE_AUTO_DETECT`)\n *                                    \"1\"/\"true\" skips the zero-config adapter auto-detect scan\n * - `MORSE_SAMPLE_RATE`           — (MHQ-482, mirrors Python MHQ-668/671) deterministic\n *                                    trace-ID head sampling rate in [0.0, 1.0], default 1.0.\n * - `MORSE_MAX_FIELD_BYTES`       — (MHQ-482) byte cap applied to input_data / output_data /\n *                                    context_segments / custom metadata at attribute-set\n *                                    time, default 65536.\n * - `MORSE_MAX_QUEUE_SIZE`        — (MHQ-482) OTel BatchSpanProcessor queue size, default\n *                                    2048 (OTel SDK default).\n * - `MORSE_MAX_BATCH_SIZE`        — (MHQ-482) OTel BatchSpanProcessor max export batch\n *                                    size, default 512 (OTel SDK default).\n * - `MORSE_LEGACY_TRANSPORT`      — (MHQ-482) \"1\"/\"true\" reverts run()/span()/runTraced()\n *                                    to the pre-OTel JSON batch transport instead of OTLP.\n *                                    Escape hatch for the migration window only;\n *                                    record()/log() always use the JSON path.\n * - `MORSE_CAPTURE_DB` / `MORSE_CAPTURE_HTTP` / `MORSE_CAPTURE_QUEUE` — (INFRACAP-01/02)\n *                                    zero-config `db` / `http` / queue span capture is ON\n *                                    by default since 0.7.0, inside an active trace only.\n *                                    \"0\"/\"false\"/\"no\"/\"off\" opts out of that one capture.\n * - `MORSE_CAPTURE_INFRA`         — \"0\"/\"false\" opts out of all three at once; a specific\n *                                    flag above still wins.\n * - `MORSE_CAPTURE_HTTP_EXCLUDE_HOSTS` — comma-separated extra hosts HTTP capture skips.\n * - `MORSE_CAPTURE_QUEUE_CONSUMER` — \"1\"/\"true\" opts in to consumer-side trace continuation.\n */\n\n/**\n * Default ingestion endpoint.\n *\n * Phase 6c targets the Railway dev URL until the custom domain is wired up\n * in Phase 6d. Mirrors the Python SDK constant in\n * `apps/sdk/python/src/morse/client.py`.\n */\nexport const DEFAULT_ENDPOINT = \"https://api.morsehq.dev/api/v1/traces/batch\";\n\n/**\n * Read an env var in a runtime-agnostic way.\n *\n * Handles Node (`process.env`), Deno (`Deno.env`), and browser\n * environments where `process` is undefined. In the browser the value\n * typically comes from a bundler-time replacement (Next.js inlines\n * `process.env.MORSE_API_KEY` at build time when the var is\n * exposed via `next.config.ts`).\n */\nfunction readEnv(key: string): string | undefined {\n  // Node / Next.js (server / build-time replaced)\n  if (typeof process !== \"undefined\" && process.env) {\n    const v = process.env[key];\n    if (typeof v === \"string\") return v;\n  }\n  // Deno\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const denoGlobal = (globalThis as any).Deno as\n    | { env: { get(k: string): string | undefined } }\n    | undefined;\n  if (denoGlobal?.env?.get) {\n    try {\n      const v = denoGlobal.env.get(key);\n      if (typeof v === \"string\") return v;\n    } catch {\n      // Permission denied — fall through.\n    }\n  }\n  return undefined;\n}\n\nfunction truthy(value: string | undefined): boolean {\n  if (!value) return false;\n  return [\"1\", \"true\", \"yes\", \"on\"].includes(value.trim().toLowerCase());\n}\n\nexport function getApiKey(): string | undefined {\n  const raw = readEnv(\"MORSE_API_KEY\");\n  if (!raw) return undefined;\n  const trimmed = raw.trim();\n  return trimmed || undefined;\n}\n\nexport function getEndpoint(): string | undefined {\n  const raw = readEnv(\"MORSE_ENDPOINT\");\n  if (!raw) return undefined;\n  const trimmed = raw.trim();\n  return trimmed || undefined;\n}\n\nexport function getFlushIntervalSeconds(defaultSeconds = 2.0): number {\n  const raw = readEnv(\"MORSE_FLUSH_INTERVAL_SECONDS\");\n  if (!raw) return defaultSeconds;\n  const parsed = Number.parseFloat(raw.trim());\n  if (!Number.isFinite(parsed) || parsed <= 0) return defaultSeconds;\n  return parsed;\n}\n\nexport function isDisabled(): boolean {\n  return truthy(readEnv(\"MORSE_DISABLED\"));\n}\n\nexport function isDebug(): boolean {\n  return truthy(readEnv(\"MORSE_SDK_DEBUG\"));\n}\n\nexport function isTelemetryDisabled(): boolean {\n  return truthy(readEnv(\"MORSE_SDK_DISABLE_TELEMETRY\"));\n}\n\n/**\n * True when `MORSE_DISABLE_AUTO_DETECT=1` — the zero-config adapter scan\n * (`_internal/auto-detect.ts`) is skipped entirely. Mirrors the Python SDK's\n * `is_auto_detect_disabled()`. Customers can still call the adapter\n * `wrapX()` / `instrumentX()` functions directly.\n */\nexport function isAutoDetectDisabled(): boolean {\n  return truthy(readEnv(\"MORSE_DISABLE_AUTO_DETECT\"));\n}\n\n/**\n * Resource-level \"source\" label for Morse's own dogfood emitters.\n *\n * Mirrors the Python SDK's `get_internal_source()` (PR #90). Only the\n * accepted whitelist values are honoured — any other value is treated\n * as unset so a typo in a customer's environment never accidentally\n * tags their data as internal.\n */\nconst VALID_INTERNAL_SOURCES = new Set([\"copilot\", \"investigator\", \"refagent\", \"ui-bot\"]);\n\nexport function getInternalSource(): string | undefined {\n  const raw = readEnv(\"MORSE_INTERNAL_SOURCE\");\n  if (!raw) return undefined;\n  const trimmed = raw.trim().toLowerCase();\n  if (!trimmed) return undefined;\n  return VALID_INTERNAL_SOURCES.has(trimmed) ? trimmed : undefined;\n}\n\nexport function isValidInternalSource(value: string | null | undefined): boolean {\n  return typeof value === \"string\" && VALID_INTERNAL_SOURCES.has(value);\n}\n\n/**\n * Deterministic trace-ID head sampling rate for the OTel export pipeline\n * (MHQ-482, mirrors Python `_internal/env.py::get_sample_rate`). Out-of-range\n * or unparseable values fall back to `default`.\n */\nexport function getSampleRate(defaultRate = 1.0): number {\n  const raw = readEnv(\"MORSE_SAMPLE_RATE\");\n  if (!raw) return defaultRate;\n  const parsed = Number.parseFloat(raw.trim());\n  if (!Number.isFinite(parsed) || parsed < 0.0 || parsed > 1.0) return defaultRate;\n  return parsed;\n}\n\n/** Byte cap for input_data/output_data/context_segments/metadata attribute values. */\nexport function getMaxFieldBytes(defaultBytes = 65536): number {\n  const raw = readEnv(\"MORSE_MAX_FIELD_BYTES\");\n  if (!raw) return defaultBytes;\n  const parsed = Number.parseInt(raw.trim(), 10);\n  return Number.isFinite(parsed) && parsed > 0 ? parsed : defaultBytes;\n}\n\n/** OTel BatchSpanProcessor queue size (OTel SDK default: 2048). */\nexport function getMaxQueueSize(defaultSize = 2048): number {\n  const raw = readEnv(\"MORSE_MAX_QUEUE_SIZE\");\n  if (!raw) return defaultSize;\n  const parsed = Number.parseInt(raw.trim(), 10);\n  return Number.isFinite(parsed) && parsed > 0 ? parsed : defaultSize;\n}\n\n/** OTel BatchSpanProcessor max export batch size (OTel SDK default: 512). */\nexport function getMaxBatchSize(defaultSize = 512): number {\n  const raw = readEnv(\"MORSE_MAX_BATCH_SIZE\");\n  if (!raw) return defaultSize;\n  const parsed = Number.parseInt(raw.trim(), 10);\n  return Number.isFinite(parsed) && parsed > 0 ? parsed : defaultSize;\n}\n\n/**\n * Escape hatch: `run()`/`span()`/`runTraced()` revert to the pre-OTel JSON\n * batch transport instead of OTLP when set. `record()`/`log()` always use\n * the JSON path regardless of this flag.\n */\nexport function isLegacyTransport(): boolean {\n  return truthy(readEnv(\"MORSE_LEGACY_TRANSPORT\"));\n}\n\nconst FALSY = [\"0\", \"false\", \"no\", \"off\"];\n\n/** true / false for a recognised value, undefined for unset, empty or garbage. */\nfunction flag(key: string): boolean | undefined {\n  const raw = readEnv(key)?.trim().toLowerCase();\n  if (!raw) return undefined;\n  if ([\"1\", \"true\", \"yes\", \"on\"].includes(raw)) return true;\n  if (FALSY.includes(raw)) return false;\n  return undefined;\n}\n\nexport type CaptureFlag = \"MORSE_CAPTURE_DB\" | \"MORSE_CAPTURE_HTTP\" | \"MORSE_CAPTURE_QUEUE\";\n\n/**\n * Default-on infra capture (INFRACAP-01/02, mirrors Python's\n * `infra_capture.py::capture_enabled`). A specific flag wins over\n * `MORSE_CAPTURE_INFRA`; with neither set, capture is ON. Capture only ever\n * records inside an active Morse trace.\n */\nexport function isCaptureEnabled(specific: CaptureFlag): boolean {\n  const v = flag(specific) ?? flag(\"MORSE_CAPTURE_INFRA\");\n  return v ?? true;\n}\n\n/** DB auto-capture switch. Default ON since 0.7.0; `MORSE_CAPTURE_DB=0` opts out. */\nexport function isDbCaptureEnabled(): boolean {\n  return isCaptureEnabled(\"MORSE_CAPTURE_DB\");\n}\n\n/**\n * Queue consumer-side trace continuation (INFRACAP-08). Opt-in: needs an\n * explicit truthy `MORSE_CAPTURE_QUEUE_CONSUMER`.\n */\nexport function isQueueConsumerEnabled(): boolean {\n  return truthy(readEnv(\"MORSE_CAPTURE_QUEUE_CONSUMER\"));\n}\n\n/** Extra hosts to leave out of HTTP capture (`MORSE_CAPTURE_HTTP_EXCLUDE_HOSTS=a.com,b.com`). */\nexport function getHttpExcludeHosts(): string[] {\n  const raw = readEnv(\"MORSE_CAPTURE_HTTP_EXCLUDE_HOSTS\");\n  if (!raw) return [];\n  return raw\n    .split(\",\")\n    .map((h) => h.trim().toLowerCase())\n    .filter(Boolean);\n}\n\n/*\n * Every Morse key is `mhq_` + optional segments (`prod_`, `pk_dev_`, …) + 64\n * hex characters (apps/api `api_keys/models.py::generate_key`). A cheap local\n * shape check, so a truncated or mis-pasted key is named at init instead of\n * only as a 401 at the first flush (offshore QA round 2, MHQ-1838). Warning\n * only: no network call, the key is still used exactly as given.\n */\nconst API_KEY_SHAPE = /^mhq_(?:[A-Za-z0-9-]+_)*[0-9a-fA-F]{64}$/;\n\n/** Why `apiKey` does not look like a Morse key, or `null` if it does. Never echoes the key. */\nexport function apiKeyShapeProblem(apiKey: string): string | null {\n  if (API_KEY_SHAPE.test(apiKey)) return null;\n  if (/^[\"']|[\"']$/.test(apiKey)) return \"it is wrapped in quotes\";\n  if (apiKey.trim() !== apiKey) return \"it has leading or trailing spaces\";\n  if (!apiKey.startsWith(\"mhq_\")) return \"it does not start with mhq_\";\n  return \"it looks truncated or mistyped\";\n}\n","/**\n * Client-side PII redaction for the TypeScript SDK.\n *\n * Phase 6f Wave B G-02. Mirrors:\n *   - `apps/sdk/python/src/morse/redaction.py` (Python SDK)\n *   - `apps/api/apps/core/redaction/patterns.py` (server-side floor)\n *\n * The eight pattern catalogue is canonical across all three implementations.\n * The parity corpus at\n *   `openspec/changes/phase-6f-g02-pii-redaction/tests/parity_corpus.json`\n * pins identical input → output behaviour on every row.\n *\n * Redaction runs default-on inside the SDK process so that sensitive strings\n * never leave the customer's runtime. Customers can:\n *   - disable the whole SDK layer (`redaction.enabled = false`),\n *   - turn off non-mandatory built-ins (`disabledPatterns`),\n *   - inject custom regexes (`extraPatterns`).\n *\n * The four mandatory keys (`api_key_prefixed`, `jwt`, `credit_card`,\n * `ssn_us`) cannot be disabled at the SDK — attempting to do so throws at\n * `init()`. Disabling everything at the SDK still leaves the server-side\n * mandatory floor in place.\n */\n\n/* ------------------------------------------------------------------------ */\n/*  Pattern catalogue                                                        */\n/* ------------------------------------------------------------------------ */\n\n/**\n * Canonical pattern keys. The redactor walks them in this order; once a\n * substring is replaced by a `[REDACTED:...]` marker, later patterns see\n * the replacement and don't re-match digit/word fragments inside it.\n *\n * Order MUST match `apps/api/apps/core/redaction/patterns.py` and\n * `apps/sdk/python/.../redaction.py` so the parity corpus produces\n * identical output across implementations.\n */\nexport const PATTERN_ORDER: readonly string[] = [\n  \"api_key_prefixed\",\n  \"jwt\",\n  \"credit_card\",\n  \"email\",\n  \"phone_us\",\n  \"phone_e164\",\n  \"ssn_us\",\n  \"ip_address\",\n  // Last: the value-shaped patterns above are more specific, so a JWT inside\n  // `authorization: Bearer eyJ...` gets its own tag before this one runs.\n  \"secret_assignment\",\n] as const;\n\n/** Patterns that customers cannot disable at the SDK layer. */\nexport const MANDATORY_PATTERNS: ReadonlySet<string> = new Set([\n  \"api_key_prefixed\",\n  \"jwt\",\n  \"credit_card\",\n  \"ssn_us\",\n]);\n\n/** Patterns that ship disabled by default (opt-in). */\nexport const DEFAULT_OFF_PATTERNS: ReadonlySet<string> = new Set([\"ip_address\"]);\n\n/**\n * Short replacement keys. The replacement format is `[REDACTED:<short>]`.\n * `phone_us` and `phone_e164` collapse to `phone`.\n */\nconst SHORT_KEYS: Record<string, string> = {\n  api_key_prefixed: \"api_key\",\n  jwt: \"jwt\",\n  credit_card: \"credit_card\",\n  email: \"email\",\n  phone_us: \"phone\",\n  phone_e164: \"phone\",\n  ssn_us: \"ssn\",\n  ip_address: \"ip\",\n  secret_assignment: \"secret\", // pragma: allowlist secret — a label, not a value\n};\n\n/**\n * Compiled regexes for each pattern. The `g` flag is required so the\n * `String.replace(regex, ...)` call walks the whole string. `i` flag is\n * applied where the canonical Python form is case-insensitive (email,\n * ip_address) for lowercase IPv6 + uppercase email parity.\n *\n * NB: the Luhn-validating credit_card pattern returns *candidate* matches;\n * `redactString` runs Luhn before deciding to replace.\n */\n/**\n * Source for the `secret_assignment` pattern, exported so the diagnostics\n * channel can compile its own instance and apply it unconditionally (MHQ-973).\n *\n * Exported as a STRING, not a RegExp: a `g`-flagged RegExp carries `lastIndex`\n * state, and sharing one instance across call sites is a well-known way to get\n * intermittently-skipped matches.\n */\nexport const SECRET_ASSIGNMENT_SOURCE =\n  // Compound keys accept `=` or `:`; bare `secret` requires `=`, because with\n  // `:` it is more often a marker (`k8s-secret: mounted`) than an assignment.\n  \"(?:(?:password|passwd|pwd|client[-_]?secret|secret[-_]?key|\" +\n  \"private[-_]?key|access[-_]?key|api[-_]?key|auth[-_]?token|\" +\n  \"access[-_]?token|refresh[-_]?token|id[-_]?token|session[-_]?token|\" +\n  \"authorization|credentials?)[\\\"']?\\\\s*[=:]\" +\n  \"|\\\\bsecret[\\\"']?\\\\s*=)\" +\n  \"\\\\s*(?:bearer\\\\s+)?(?:\\\"[^\\\"]*\\\"|'[^']*'|\\\\S+)\";\n\nconst PATTERNS: Record<string, RegExp> = {\n  api_key_prefixed: new RegExp(\n    \"\\\\b(?:\" +\n      \"sk-[A-Za-z0-9_-]{20,}\" +\n      \"|sk_(?:live|test)_[A-Za-z0-9]{20,}\" +\n      // WP-12 parity with the server: Stripe restricted keys and webhook\n      // secrets, GitHub fine-grained PATs and Resend keys (`re_<id>_<secret>`,\n      // which must carry a digit and an uppercase letter so snake_case\n      // identifiers never match). `sk-ant-` / `sk-proj-` are covered by `sk-`.\n      \"|rk_(?:live|test)_[A-Za-z0-9]{20,}\" +\n      \"|whsec_[A-Za-z0-9]{24,}\" +\n      \"|github_pat_[A-Za-z0-9_]{30,}\" +\n      \"|re_(?=[A-Za-z0-9_]{24,})(?=[A-Za-z0-9_]*\\\\d)(?=[A-Za-z0-9_]*[A-Z])\" +\n      \"[A-Za-z0-9]+(?:_[A-Za-z0-9]+)?\" +\n      \"|(?:mhq|av)_(?:pk_)?[\\\\s\\\\S]{0,48}_[A-Fa-f0-9]{64}\" +\n      \"|mhq_[A-Za-z0-9]{20,}\" +\n      \"|av_[A-Za-z0-9]{20,}\" +\n      \"|ghp_[A-Za-z0-9]{36}\" +\n      \"|gho_[A-Za-z0-9]{36}\" +\n      \"|ghu_[A-Za-z0-9]{36}\" +\n      \"|ghs_[A-Za-z0-9]{36}\" +\n      \"|ghr_[A-Za-z0-9]{36}\" +\n      \"|xox[bopsa]-[0-9A-Za-z\\\\-]{10,}\" +\n      \"|(?:AKIA|ASIA|AGPA|AIDA|AROA|AIPA|ANPA|ANVA|ASCA)[0-9A-Z]{16}\" +\n      \")\\\\b\",\n    \"g\",\n  ),\n  jwt: new RegExp(\n    \"\\\\beyJ[A-Za-z0-9_=-]{16,}\\\\.eyJ[A-Za-z0-9_=-]{16,}\\\\.[A-Za-z0-9_=-]{16,}\\\\b\",\n    \"g\",\n  ),\n  credit_card: new RegExp(\"\\\\b(?:\\\\d[ -]?){12,18}\\\\d\\\\b\", \"g\"),\n  email: new RegExp(\"\\\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\\\.[A-Za-z]{2,}\\\\b\", \"gi\"),\n  // The leading `(?<!\\d)` and trailing `(?!\\d)` lookarounds prevent\n  // the phone matcher from carving a 10-digit substring out of a longer\n  // continuous digit run (e.g., a 16-digit credit-card-shaped number that\n  // failed Luhn). Without them, `1234567890123456` would emit a\n  // `[REDACTED:phone]` for digits 7-16 even though the whole run was\n  // never a real phone number. The canonical Python regex needs the same\n  // shim — see redaction.py.\n  phone_us: new RegExp(\n    \"(?<!\\\\d)(?:\\\\+?1[ \\\\-.])?\\\\(?[2-9]\\\\d{2}\\\\)?[ \\\\-.]?\\\\d{3}[ \\\\-.]?\\\\d{4}(?!\\\\d)\",\n    \"g\",\n  ),\n  phone_e164: new RegExp(\"(?<!\\\\d)\\\\+(?:[0-9][ \\\\-.]?){8,14}[0-9](?!\\\\d)\", \"g\"),\n  ssn_us: new RegExp(\n    \"\\\\b(?!000|666|9\\\\d{2})\\\\d{3}[ \\\\-.](?!00)\\\\d{2}[ \\\\-.](?!0000)\\\\d{4}\\\\b\",\n    \"g\",\n  ),\n  ip_address: new RegExp(\n    \"\\\\b(?:\" +\n      \"(?:25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)\" +\n      \"(?:\\\\.(?:25[0-5]|2[0-4]\\\\d|1\\\\d\\\\d|[1-9]?\\\\d)){3}\" +\n      \"|(?:[A-F0-9]{1,4}:){2,7}[A-F0-9]{1,4}\" +\n      \")\\\\b\",\n    \"gi\",\n  ),\n  // MHQ-973 — key-context secret assignment. Every pattern above is\n  // VALUE-shaped: it recognises a credential by its own format. That cannot\n  // catch `password=hunter2` — `hunter2` is an ordinary word, and only the KEY\n  // marks it as a secret.\n  //\n  // Deliberately excludes a bare `token`: this is an LLM-observability SDK, and\n  // `input_tokens=100` / `max_tokens=4096` are core telemetry. Only unambiguous\n  // `*_token` compounds match. No leading `\\\\b` either, so `PGPASSWORD=` and\n  // `MYSQL_PWD=` are caught; the `[=:]` anchor is what prevents false positives.\n  secret_assignment: new RegExp(SECRET_ASSIGNMENT_SOURCE, \"gi\"),\n};\n\n/* ------------------------------------------------------------------------ */\n/*  Configuration                                                            */\n/* ------------------------------------------------------------------------ */\n\n/** Public SDK option. Mirrors `redaction` in `init()`. */\nexport interface RedactionOptions {\n  /** Master switch. Default: `true`. */\n  enabled?: boolean;\n  /** Pattern keys to selectively disable (mandatory keys rejected). */\n  disabledPatterns?: string[];\n  /** Customer-supplied regex strings (ECMAScript syntax). */\n  extraPatterns?: string[];\n}\n\n/** Validated, frozen config fed into `redactString` / `redactValue`. */\nexport interface RedactionConfig {\n  enabled: boolean;\n  /** Keys actually applied, in stable order. */\n  enabledPatterns: ReadonlySet<string>;\n  /** Custom compiled regexes — applied last, with `[REDACTED:custom]`. */\n  extraCompiled: readonly RegExp[];\n}\n\n/** Hard caps mirroring server-side validation. */\nconst EXTRA_PATTERN_CAP = 10;\nconst EXTRA_PATTERN_CHAR_CAP = 256;\n\n/**\n * Validate raw user options and return a normalised config. Throws on:\n *   - disabling a mandatory pattern,\n *   - more than 10 extra patterns,\n *   - any extra pattern over 256 chars,\n *   - any extra pattern that fails to compile via `new RegExp(...)`.\n *\n * `enabled = false` short-circuits — no patterns are applied.\n */\nexport function buildRedactionConfig(options?: RedactionOptions): RedactionConfig {\n  const opts = options ?? {};\n  const enabled = opts.enabled !== false;\n\n  if (!enabled) {\n    return {\n      enabled: false,\n      enabledPatterns: new Set<string>(),\n      extraCompiled: [],\n    };\n  }\n\n  // Mandatory-pattern lock-down.\n  const disabled = new Set(opts.disabledPatterns ?? []);\n  for (const key of disabled) {\n    if (MANDATORY_PATTERNS.has(key)) {\n      throw new Error(`redaction pattern '${key}' cannot be disabled (server-side mandatory)`);\n    }\n  }\n\n  // Active set: every catalogue key NOT in default-off NOT in disabled.\n  const active = new Set<string>();\n  for (const key of PATTERN_ORDER) {\n    if (DEFAULT_OFF_PATTERNS.has(key)) continue;\n    if (disabled.has(key)) continue;\n    active.add(key);\n  }\n  // Re-add any default-off pattern the user explicitly opted in to via a\n  // `disabledPatterns` membership check is the wrong direction — the only\n  // way to enable `ip_address` from the SDK today is through a future\n  // `enabledPatterns` option. The spec keeps it default-off; tests pass\n  // an explicit `enabledPatterns` set via the lower-level `redactString`.\n\n  // Custom regex validation.\n  const extraInput = opts.extraPatterns ?? [];\n  if (extraInput.length > EXTRA_PATTERN_CAP) {\n    throw new Error(\n      `too many redaction extra patterns: ${extraInput.length} (max ${EXTRA_PATTERN_CAP})`,\n    );\n  }\n  const extraCompiled: RegExp[] = [];\n  for (const raw of extraInput) {\n    if (typeof raw !== \"string\") {\n      throw new Error(`redaction extra pattern must be a string, got ${typeof raw}`);\n    }\n    if (raw.length > EXTRA_PATTERN_CHAR_CAP) {\n      throw new Error(`redaction extra pattern exceeds ${EXTRA_PATTERN_CHAR_CAP} chars`);\n    }\n    let compiled: RegExp;\n    try {\n      compiled = new RegExp(raw, \"g\");\n    } catch (err) {\n      const msg = err instanceof Error ? err.message : String(err);\n      throw new Error(`redaction extra pattern failed to compile: ${JSON.stringify(raw)} — ${msg}`);\n    }\n    extraCompiled.push(compiled);\n  }\n\n  return {\n    enabled: true,\n    enabledPatterns: active,\n    extraCompiled,\n  };\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Luhn check                                                               */\n/* ------------------------------------------------------------------------ */\n\n/** Standard mod-10 Luhn check on the digits of `s` (ignores spaces/dashes). */\nfunction luhnValid(s: string): boolean {\n  let sum = 0;\n  let alt = false;\n  let digits = 0;\n  for (let i = s.length - 1; i >= 0; i--) {\n    const ch = s.charCodeAt(i);\n    if (ch < 48 || ch > 57) continue; // skip non-digits (spaces, dashes)\n    let n = ch - 48;\n    if (alt) {\n      n *= 2;\n      if (n > 9) n -= 9;\n    }\n    sum += n;\n    alt = !alt;\n    digits++;\n  }\n  if (digits < 13 || digits > 19) return false;\n  return sum % 10 === 0;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  String redactor                                                          */\n/* ------------------------------------------------------------------------ */\n\n/**\n * Internal: compute the active enabled set when callers pass an explicit\n * `enabledPatterns` (parity-corpus tests do this) vs. defaults.\n */\nfunction resolveEnabled(\n  config: RedactionConfig,\n  override?: ReadonlySet<string>,\n): ReadonlySet<string> {\n  if (override !== undefined) return override;\n  return config.enabledPatterns;\n}\n\n// MHQ-1412: identifiers are not PII. A UUID or a long hex id (trace/span ids,\n// vector-store document ids) often contains a 10+ digit run, which the phone\n// and card patterns carved out of the middle (`bc41d39b-...-0c[REDACTED:phone]`),\n// corrupting ids that exist to be joined on. Id-shaped tokens are set aside\n// while the digit-run patterns run and restored afterwards. A hex id must\n// contain at least one letter, so an all-digit run (a card number) is never\n// shielded; API-key/JWT/secret patterns always see the full text. Mirrors\n// `apps/sdk/python/src/morse_ai/_internal/redaction.py` and the server.\nconst ID_SHIELDED_PATTERNS: ReadonlySet<string> = new Set([\n  \"credit_card\",\n  \"phone_us\",\n  \"phone_e164\",\n  \"ssn_us\",\n]);\nconst ID_TOKEN = new RegExp(\n  \"(?<![\\\\w@.])(?:\" +\n    \"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\" +\n    \"|(?=[0-9a-fA-F]*[a-fA-F])(?=[0-9a-fA-F]*[0-9])[0-9a-fA-F]{16,64}\" +\n    \")(?![0-9A-Za-z])(?![\\\\w.+\\\\-]*@)(?!\\\\.[A-Za-z]{2,})\",\n  \"g\",\n);\nconst ID_PLACEHOLDER_RE = /\\uE000morse-id-(\\d+)\\uE000/g;\n\nfunction shieldIds(s: string, out: string[]): string {\n  if (s.includes(\"\\uE000\")) return s;\n  ID_TOKEN.lastIndex = 0;\n  return s.replace(ID_TOKEN, (m) => {\n    out.push(m);\n    return `\\uE000morse-id-${out.length - 1}\\uE000`;\n  });\n}\n\nfunction unshieldIds(s: string, ids: readonly string[]): string {\n  ID_PLACEHOLDER_RE.lastIndex = 0;\n  return s.replace(ID_PLACEHOLDER_RE, (_m, i: string) => ids[Number(i)] ?? _m);\n}\n\n/**\n * Core redactor: walk patterns in canonical order, replace matches.\n *\n * `enabledOverride` allows tests (and the parity corpus) to specify an\n * exact enabled set, bypassing the config's default-on/off decisions.\n *\n * Customers calling through the SDK never see the override path — they\n * always pass through `RedactionConfig.enabledPatterns`.\n */\nexport function redactString(\n  s: string,\n  config: RedactionConfig,\n  enabledOverride?: ReadonlySet<string>,\n): string {\n  if (!config.enabled && enabledOverride === undefined) {\n    return s;\n  }\n  if (s.length === 0) return s;\n\n  const enabled = resolveEnabled(config, enabledOverride);\n  let out = s;\n\n  for (const key of PATTERN_ORDER) {\n    if (!enabled.has(key)) continue;\n    const re = PATTERNS[key];\n    if (re === undefined) continue;\n    // Reset `lastIndex` defensively (shared global regex state).\n    re.lastIndex = 0;\n    const replacement = `[REDACTED:${SHORT_KEYS[key]}]`;\n    // Only the digit-run patterns are kept off identifiers (MHQ-1412).\n    const protectedIds: string[] = [];\n    let text = ID_SHIELDED_PATTERNS.has(key) ? shieldIds(out, protectedIds) : out;\n    if (key === \"credit_card\") {\n      text = text.replace(re, (m) => (luhnValid(m) ? replacement : m));\n    } else {\n      text = text.replace(re, replacement);\n    }\n    out = protectedIds.length > 0 ? unshieldIds(text, protectedIds) : text;\n  }\n\n  // Custom patterns last.\n  for (const re of config.extraCompiled) {\n    re.lastIndex = 0;\n    out = out.replace(re, \"[REDACTED:custom]\");\n  }\n\n  return out;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  JSON walker                                                              */\n/* ------------------------------------------------------------------------ */\n\n/**\n * Maximum nesting depth walked. Matches the server\n * (`apps.core.redaction.services.MAX_JSON_DEPTH`) and the ingest limit: a\n * top-level value is depth 0 and a string inside 64 nested containers is depth\n * 64, which is still walked.\n */\nexport const MAX_DEPTH = 64;\n/** Replaces a node nested deeper than `MAX_DEPTH` (same marker as the server). */\nexport const DEPTH_MARKER = \"[REDACTED:depth]\";\n\n/**\n * Recursively redact string leaves inside an arbitrary JSON-like value.\n * Booleans, numbers, nulls, undefined are passed through untouched.\n * Recursion is bounded at `MAX_DEPTH` to defend against pathological\n * payloads — anything nested deeper becomes `DEPTH_MARKER` (fail closed, like\n * the server) instead of being passed through unredacted.\n *\n * Arrays return new arrays; plain objects return new shallow-cloned\n * objects; non-plain objects (Date, Map, etc.) are returned as-is so we\n * don't accidentally serialise customer-internal types.\n */\nexport function redactValue(value: unknown, config: RedactionConfig, depth = 0): unknown {\n  if (!config.enabled) return value;\n  if (depth > MAX_DEPTH) return DEPTH_MARKER;\n  if (value === null || value === undefined) return value;\n  if (typeof value === \"string\") return redactString(value, config);\n  if (typeof value === \"number\" || typeof value === \"boolean\") return value;\n\n  if (Array.isArray(value)) {\n    return value.map((item) => redactValue(item, config, depth + 1));\n  }\n\n  // Only walk plain objects (prototype is Object.prototype or null).\n  if (typeof value === \"object\") {\n    const proto = Object.getPrototypeOf(value);\n    if (proto === Object.prototype || proto === null) {\n      const out: Record<string, unknown> = {};\n      for (const [k, v] of Object.entries(value as Record<string, unknown>)) {\n        out[k] = redactValue(v, config, depth + 1);\n      }\n      return out;\n    }\n  }\n\n  return value;\n}\n","/**\n * SDK self-diagnostics — Morse-owned internal error reporting (MHQ-908).\n *\n * Mirrors the Python SDK's `_internal/diagnostics.py`. Replaces the Sentry\n * reporter: when the SDK absorbs an internal exception (per the non-invasive\n * guarantee in `_internal/errors.ts`), it reports to Morse's own\n * `/api/v1/diagnostics/sdk` rather than to a third party the customer may not\n * have installed.\n *\n * Two channels, both silent on a healthy install:\n *\n *   - **errors** — one absorbed exception with a *Morse-scoped* stack. Frames\n *     inside this package go in full; every run of frames outside it collapses\n *     to a count. No local variables, at any verbosity level.\n *   - **counters** (SDKDIAG-09) — aggregate `{reason, category, quantity}` for\n *     telemetry we discarded, adopted from Sentry's client reports, which is\n *     what that vendor chose to send *instead of* SDK stack traces.\n *\n * **Transport is deliberately its own** (founder decision, 2026-08-03, Option\n * B): a bounded buffer plus a timer, created on the FIRST report and never\n * before. Routing through the shared transport would have been a smaller diff,\n * but that queue is bounded and drops, so a diagnostic storm would evict the\n * customer's telemetry. Lazy creation matters more in JS than in Python: an\n * armed timer keeps the Node event loop alive, so a healthy process must not\n * arm one. The timer is `unref()`d where available for the same reason.\n *\n * **Nothing here is private from the host.** Everything transmitted is also\n * written to the console, rate-limited, whether or not transmission succeeds\n * and even when the customer has opted out entirely (SDKDIAG-10).\n *\n * **Never throws.** A failure anywhere is swallowed — never retried, never\n * reported about itself (SDKDIAG-06).\n */\n\nimport { getApiKey, getEndpoint, isDebug, isDisabled, isTelemetryDisabled } from \"./env\";\nimport { type RedactionConfig, SECRET_ASSIGNMENT_SOURCE, redactValue } from \"./redaction\";\n\n// Build-time define, same as transport.ts. Declared rather than imported so\n// this module stays a leaf — transport imports us for the drop counters, and a\n// cycle here would be resolved at module-init time on the error path.\ndeclare const __SDK_VERSION__: string | undefined;\nconst SDK_VERSION = typeof __SDK_VERSION__ === \"string\" ? __SDK_VERSION__ : \"0.0.0-dev\";\n\nexport const LEVEL_OFF = \"off\";\nexport const LEVEL_ERROR = \"error\";\nexport const LEVEL_DEBUG = \"debug\";\nconst VALID_LEVELS: ReadonlySet<string> = new Set([LEVEL_OFF, LEVEL_ERROR, LEVEL_DEBUG]);\n\n/**\n * Learned from the `X-Morse-Diagnostics` response header on traffic the SDK is\n * already sending. `error` until a header says otherwise — matching the server\n * model's default, so behaviour before the first response matches after.\n */\nlet level: string = LEVEL_ERROR;\n\nconst RATE_WINDOW_MS = 60_000;\nconst RATE_LIMIT_PER_WINDOW = 100;\n/** `debug` relaxes the send budget; it never relaxes what may be sent. */\nconst RATE_LIMIT_PER_WINDOW_DEBUG = 1000;\nconst rateBuckets: Map<string, number[]> = new Map();\n\n/**\n * Local emission is limited separately from transmission so our send budget is\n * never the host's visibility ceiling (SDKDIAG-10).\n */\nconst LOCAL_LOG_WINDOW_MS = 60_000;\nexport const LOCAL_LOG_LIMIT_PER_WINDOW = 10;\nconst localLogBuckets: Map<string, number[]> = new Map();\n\n// Payload caps — mirror apps/api/apps/diagnostics/schemas.py.\nexport const MAX_STACK_ENTRIES = 64;\nexport const MAX_MESSAGE_CHARS = 4096;\nexport const MAX_BATCH_EVENTS = 32;\nconst ALLOWED_EXTRA_KEYS: ReadonlySet<string> = new Set([\n  \"adapter\",\n  \"module\",\n  \"function\",\n  \"operation\",\n  \"endpoint_host\",\n]);\n\nconst DIAGNOSTICS_PATH = \"/api/v1/diagnostics/sdk\";\nconst MAX_PENDING = 100;\nconst FLUSH_INTERVAL_MS = 5000;\nconst BREAKER_THRESHOLD = 3;\nconst BREAKER_COOLDOWN_MS = 300_000;\n\n// --- Delivery counters (SDKDIAG-09) ----------------------------------------\n\nexport const REASON_QUEUE_FULL = \"queue_full\";\n/** Retryable failure, attempts made and used up (3, per MHQ-1001). */\nexport const REASON_RETRY_EXHAUSTED = \"retry_exhausted\";\nexport const REASON_QUOTA_REJECTED = \"quota_rejected\";\n/** A genuine serialisation failure — JSON.stringify threw. */\nexport const REASON_ENCODE_FAILED = \"encode_failed\";\nexport const REASON_SHUTDOWN_DROP = \"shutdown_drop\";\n/** 401/403 — key revoked, expired, or missing a scope. */\nexport const REASON_AUTH_REJECTED = \"auth_rejected\";\n/** Other non-429 4xx — wrong URL, or a payload the server will never accept. */\nexport const REASON_ENDPOINT_REJECTED = \"endpoint_rejected\";\n/** A send failed and the batch was dropped without further attempts. */\nexport const REASON_SEND_FAILED = \"send_failed\";\n// No breaker-open reason: the customer-telemetry transport has no circuit\n// breaker, and the breaker that does exist — this module's own — may not\n// report on itself (SDKDIAG-06). A constant here would be unreachable by\n// construction. See SDKDIAG-09.\n\n/**\n * Plain-language cause for the local warning, so a wrong or revoked key reads as that and not as\n * an OpenTelemetry internal (offshore QA 2026-11-12). Unknown reasons keep the generic text.\n */\nconst REASON_HINTS: Record<string, string> = {\n  [REASON_AUTH_REJECTED]:\n    \"Morse rejected your API key (wrong, revoked or expired); check MORSE_API_KEY\",\n  [REASON_QUOTA_REJECTED]:\n    \"Morse is rate limiting or over quota for this key; telemetry is being lost\",\n  [REASON_ENDPOINT_REJECTED]: \"Morse rejected the request; check the endpoint URL and SDK version\",\n  [REASON_RETRY_EXHAUSTED]:\n    \"could not reach the Morse API after retrying; check your network and endpoint\",\n  [REASON_SEND_FAILED]: \"could not reach the Morse API; telemetry is being lost\",\n};\n\n/**\n * What the transport knows about a drop beyond its reason code, so the local warning can name the\n * cause in plain words (offshore QA round 2, MHQ-1838). Optional: callers that know nothing more\n * get the generic hint above. Changes only the text of the warning — never counting or delivery.\n */\nexport interface DropDetail {\n  /** HTTP status the server answered with (401/403 for a rejected key). */\n  status?: number;\n  /** Host the SDK was sending to, e.g. `api.morsehq.dev`. */\n  host?: string;\n  /** The send failed at the network layer (DNS, refused, reset, timeout), not with an HTTP answer. */\n  unreachable?: boolean;\n}\n\nconst NETWORK_ERROR_PATTERN =\n  /ENOTFOUND|EAI_AGAIN|ECONNREFUSED|ECONNRESET|ETIMEDOUT|EHOSTUNREACH|ENETUNREACH|EPIPE|socket hang up|fetch failed|timed? ?out|timeout|aborted/i;\n\n/**\n * True when `err` is a network-layer failure (DNS, connection refused or reset, timeout) rather\n * than an HTTP answer. Looks at `code`, `message` and one level of `cause` (undici's `fetch failed`\n * carries the real errno there). Never throws.\n */\nexport function isNetworkError(err: unknown): boolean {\n  // forceFlush rejects with an array of the processors' errors (MHQ-1838).\n  if (Array.isArray(err)) return err.length > 0 && err.every(isNetworkError);\n  try {\n    const seen = [err, (err as { cause?: unknown } | null | undefined)?.cause];\n    for (const e of seen) {\n      if (e == null) continue;\n      const code = (e as { code?: unknown }).code;\n      if (typeof code === \"string\" && NETWORK_ERROR_PATTERN.test(code)) return true;\n      const message = e instanceof Error ? e.message : typeof e === \"string\" ? e : \"\";\n      if (message && NETWORK_ERROR_PATTERN.test(message)) return true;\n    }\n  } catch {\n    // Classification is best-effort.\n  }\n  return false;\n}\n\n/** True when `err` carries an HTTP 401/403 as its `code` (the `OTLPExporterError` shape), or is an array of such errors. */\nexport function isAuthError(err: unknown): boolean {\n  if (Array.isArray(err)) return err.length > 0 && err.every(isAuthError);\n  const code = (err as { code?: unknown } | null | undefined)?.code;\n  return code === 401 || code === 403;\n}\n\nfunction plural(category: string): string {\n  return `${category.charAt(0).toUpperCase()}${category.slice(1)}s`;\n}\n\n/** The one-line local warning for a drop. Exported for tests. */\nexport function dropWarning(\n  reason: string,\n  category: string,\n  quantity: number,\n  detail?: DropDetail,\n): string {\n  const count = `${quantity} ${category}(s)`;\n  if (reason === REASON_AUTH_REJECTED) {\n    const status = detail?.status ?? \"401/403\";\n    return `morse: Morse rejected the API key (${status}): ${count} dropped. Check MORSE_API_KEY or get a new key in Settings → API keys.`;\n  }\n  if (detail?.unreachable && (reason === REASON_RETRY_EXHAUSTED || reason === REASON_SEND_FAILED)) {\n    const host = detail.host ?? \"the Morse API\";\n    return `morse: Morse unreachable (${host}): ${count} dropped after retries. ${plural(category)} are not buffered across outages.`;\n  }\n  return `morse: dropped ${quantity} ${category} (${reason}) — ${REASON_HINTS[reason] ?? \"telemetry is being lost\"}`;\n}\n\n/* One \"retrying\" line per outage, so the wait before a drop is not silent. Reset by the next\n * successful send, so a later outage says so again. */\nlet retryingNoticeShown = false;\n\n/** Say once per outage that the SDK is retrying. Text only; never throws. */\nexport function noteRetrying(host: string | undefined, notResponding = false): void {\n  try {\n    if (retryingNoticeShown) return;\n    retryingNoticeShown = true;\n    const where = host ?? \"the Morse API\";\n    console.warn(\n      notResponding\n        ? `morse: Morse not responding (${where}), retrying…`\n        : `morse: Morse unreachable (${where}), retrying…`,\n    );\n  } catch {\n    // Never break a send.\n  }\n}\n\n/** A send succeeded: the next outage gets its own \"retrying\" line. */\nexport function noteDelivered(): void {\n  retryingNoticeShown = false;\n}\n\ninterface CounterEntry {\n  reason: string;\n  category: string;\n  quantity: number;\n}\n\nconst counters: Map<string, CounterEntry> = new Map();\n\ninterface DiagnosticEvent {\n  kind: \"error\" | \"counters\";\n  fingerprint: string;\n  error_type?: string;\n  message?: string;\n  stack?: Array<Record<string, unknown>>;\n  counters?: CounterEntry[];\n  sdk_language: string;\n  sdk_version: string;\n  runtime_version?: string;\n  platform?: string;\n  extra?: Record<string, string>;\n}\n\n/**\n * Active redaction config, PUSHED here by `tracing.setRedactionConfig`.\n *\n * Push rather than pull so this module imports nothing but `env` and\n * `redaction`. A static import of `tracing` would close a cycle\n * (transport → diagnostics → tracing → transport) that resolves at module-init\n * time, and the error path is the worst place to discover a half-initialised\n * module.\n */\nlet activeRedactionConfig: RedactionConfig | null = null;\n\nexport function setRedactionConfig(config: RedactionConfig | null): void {\n  activeRedactionConfig = config;\n}\n\nlet pending: DiagnosticEvent[] = [];\nlet timer: ReturnType<typeof setInterval> | null = null;\nlet consecutiveFailures = 0;\nlet breakerOpenUntil = 0;\n/** Re-entrancy fence (SDKDIAG-06). */\nlet reporting = false;\n\nexport function setLevel(raw: string | null | undefined): void {\n  if (!raw) return;\n  const value = String(raw).trim().toLowerCase();\n  // Unrecognised values are ignored rather than treated as `off` — a future\n  // server value must not silently mute an old SDK.\n  if (!VALID_LEVELS.has(value)) return;\n  level = value;\n}\n\nexport function getLevel(): string {\n  return level;\n}\n\n/**\n * True when this process may put diagnostics on the wire. The customer's kill\n * switch beats every server-side setting — that ordering is the whole point of\n * having a client-side switch.\n */\nexport function transmissionAllowed(): boolean {\n  if (isTelemetryDisabled() || isDisabled()) return false;\n  return level !== LEVEL_OFF;\n}\n\nfunction checkBucket(\n  buckets: Map<string, number[]>,\n  key: string,\n  limit: number,\n  windowMs: number,\n): boolean {\n  const now = Date.now();\n  const bucket = buckets.get(key) ?? [];\n  const cutoff = now - windowMs;\n  while (bucket.length > 0 && (bucket[0] as number) < cutoff) bucket.shift();\n  if (bucket.length >= limit) {\n    buckets.set(key, bucket);\n    return false;\n  }\n  bucket.push(now);\n  buckets.set(key, bucket);\n  return true;\n}\n\nexport function checkRateLimit(fingerprint: string): boolean {\n  const limit = level === LEVEL_DEBUG ? RATE_LIMIT_PER_WINDOW_DEBUG : RATE_LIMIT_PER_WINDOW;\n  return checkBucket(rateBuckets, fingerprint, limit, RATE_WINDOW_MS);\n}\n\n// --- Morse-scoped stack (SDKDIAG-02) ---------------------------------------\n\n/**\n * Frames mentioning this package are ours; everything else is the host's.\n *\n * Matching on the module path is a heuristic (a bundled build flattens paths),\n * and it fails CLOSED: an unrecognised frame is treated as the host's and\n * elided. Over-eliding costs us a debugging detail; under-eliding would ship\n * the customer's file layout.\n *\n * **Both package names must be listed.** The in-repo `package.json` name is\n * `@morsehq/sdk` (the pnpm workspace identifier), but `release-typescript.yml`\n * rewrites it to `@morsehq-dev/sdk` — the real npm scope — immediately before\n * publish. So EVERY customer install lives at `@morsehq-dev/sdk/...` and the\n * in-repo name never appears in a production stack trace.\n *\n * Listing only the in-repo name is what shipped through 0.5.0-rc.4: no frame\n * in any published build was recognised as ours, so every stack collapsed to\n * `[{elided: n}]` and diagnostics could not say where the SDK broke. Verified\n * against the published rc4 artifact on 2026-08-07.\n *\n * Matched as exact scoped-package substrings rather than a bare `morsehq`, to\n * keep the fail-closed property: a customer directory that merely contains our\n * name must not cause their frames to be transmitted in full.\n */\nconst MORSE_PATH_MARKERS = [\"@morsehq/sdk\", \"@morsehq-dev/sdk\", \"morse-sdk\", \"/morse/\", \"morse_ai\"];\n\nfunction isMorseFrame(line: string): boolean {\n  return MORSE_PATH_MARKERS.some((marker) => line.includes(marker));\n}\n\n/** Parse `at fn (file:line:col)` into its parts, best-effort. */\nfunction parseFrame(line: string): { file?: string; lineNo?: number; fn?: string } {\n  const withParens = /^\\s*at\\s+(.+?)\\s+\\((.+?):(\\d+):(\\d+)\\)\\s*$/.exec(line);\n  if (withParens) {\n    return {\n      fn: withParens[1],\n      file: withParens[2],\n      lineNo: Number(withParens[3]),\n    };\n  }\n  const bare = /^\\s*at\\s+(.+?):(\\d+):(\\d+)\\s*$/.exec(line);\n  if (bare) return { file: bare[1], lineNo: Number(bare[2]) };\n  return {};\n}\n\nexport function buildScopedStack(error: unknown): Array<Record<string, unknown>> {\n  const frames: Array<Record<string, unknown>> = [];\n  try {\n    const stack = error instanceof Error ? (error.stack ?? \"\") : \"\";\n    if (!stack) return frames;\n    let elided = 0;\n    for (const raw of stack.split(\"\\n\")) {\n      if (!raw.trim().startsWith(\"at \")) continue; // the message line\n      if (isMorseFrame(raw)) {\n        if (elided > 0) {\n          frames.push({ elided });\n          elided = 0;\n        }\n        const { file, lineNo, fn } = parseFrame(raw);\n        const frame: Record<string, unknown> = {};\n        // Keep only the tail of the path: an absolute path leaks the host's\n        // directory layout (usernames, deploy paths).\n        if (file) frame.file = file.split(/[\\\\/]/).slice(-3).join(\"/\");\n        if (typeof lineNo === \"number\" && !Number.isNaN(lineNo)) frame.line = lineNo;\n        if (fn) frame.function = fn.slice(0, 255);\n        frames.push(frame);\n      } else {\n        elided += 1;\n      }\n    }\n    if (elided > 0) frames.push({ elided });\n  } catch {\n    // A partial stack beats no diagnostic.\n  }\n  return frames.length > MAX_STACK_ENTRIES ? frames.slice(-MAX_STACK_ENTRIES) : frames;\n}\n\n/**\n * Exception message, redacted and capped.\n *\n * An exception message is not automatically safe — it can name a customer key\n * or echo content — so it goes through the same redaction the telemetry path\n * uses, and then through the secret-assignment pattern a second time,\n * unconditionally. A redactor fault returns a placeholder rather than the raw\n * string: failing closed is the only acceptable direction here.\n *\n * The second pass is the point (MHQ-973). `secret_assignment` is disableable,\n * because an org may legitimately want raw `password=` values in *their own*\n * spans. This channel is different: the published disclosure (SDKDIAG-02)\n * promises Morse receives no customer data, and that promise must not be\n * defeatable by someone's telemetry preference. Over-redacting our own error\n * text costs a little debuggability; under-redacting leaks a credential to us.\n */\nfunction redactedMessage(error: unknown): string {\n  let raw: string;\n  try {\n    raw = error instanceof Error ? error.message : String(error);\n  } catch {\n    return \"\";\n  }\n  try {\n    if (activeRedactionConfig) {\n      const out = redactValue(raw, activeRedactionConfig);\n      raw = typeof out === \"string\" ? out : String(out);\n    }\n    // Fresh RegExp per call: a `g`-flagged instance carries `lastIndex`.\n    raw = raw.replace(new RegExp(SECRET_ASSIGNMENT_SOURCE, \"gi\"), \"[REDACTED:secret]\");\n  } catch {\n    return \"<redaction unavailable>\";\n  }\n  return raw.slice(0, MAX_MESSAGE_CHARS);\n}\n\nexport function scrubExtra(extra: Record<string, unknown> | undefined): Record<string, string> {\n  const out: Record<string, string> = {};\n  if (!extra) return out;\n  for (const [key, value] of Object.entries(extra)) {\n    if (ALLOWED_EXTRA_KEYS.has(key) && value != null) {\n      out[key] = String(value).slice(0, 255);\n    }\n  }\n  return out;\n}\n\n/**\n * Count telemetry we discarded. Never throws, never sends immediately.\n *\n * Delivery loss is what the customer's on-call most needs to know, so it is a\n * `console.warn` locally even though it is only a counter on the wire.\n */\nexport function recordDrop(\n  reason: string,\n  category: string,\n  quantity = 1,\n  detail?: DropDetail,\n): void {\n  try {\n    if (quantity <= 0) return;\n    const key = `${reason}:${category}`;\n    const existing = counters.get(key);\n    if (existing) existing.quantity += quantity;\n    else counters.set(key, { reason, category, quantity });\n\n    if (\n      checkBucket(localLogBuckets, `drop:${key}`, LOCAL_LOG_LIMIT_PER_WINDOW, LOCAL_LOG_WINDOW_MS)\n    ) {\n      console.warn(dropWarning(reason, category, quantity, detail));\n    }\n    // MHQ-1030: arm the flush timer so the count goes out on the normal\n    // cadence. Counters alone used to arm nothing, so a process that lost\n    // telemetry but never threw reported it only at `shutdown()` — i.e. never,\n    // for a long-lived server or one killed outright. The timer is `unref`'d,\n    // so this still cannot hold the process open.\n    if (transmissionAllowed()) ensureTimer();\n  } catch {\n    // Counter bookkeeping must never break a caller.\n  }\n}\n\n/* MHQ-1787 (offshore QA round 1, finding 06): with no MORSE_API_KEY and no\n * `init()`, `morse.run()` still hands back a real-looking trace id (kept on\n * purpose, `traceId` semantics are unchanged) while nothing is ever sent. The\n * one-time \"SDK calls are silent no-ops\" warning at first use is easy to miss,\n * so count those runs and say so loudly, once, when the process exits. No\n * public attribute: the count lives here and only feeds the warning. */\nlet unsentNoKeyRuns = 0;\nlet unsentExitHookRegistered = false;\n\n/** Count a run that cannot be sent because no API key is set. Never throws. */\nexport function recordUnsentRunNoKey(): void {\n  try {\n    unsentNoKeyRuns += 1;\n    if (!unsentExitHookRegistered && typeof process !== \"undefined\" && process.once) {\n      unsentExitHookRegistered = true;\n      // \"exit\" (not \"beforeExit\") also fires on `process.exit()`; console.warn\n      // is synchronous, which is all an exit handler may do.\n      process.once(\"exit\", warnUnsentRunsAtExit);\n    }\n  } catch {\n    // Counter bookkeeping must never break a caller.\n  }\n}\n\n/** Warn once that runs were not sent for lack of an API key. Never throws. */\nexport function warnUnsentRunsAtExit(): void {\n  try {\n    const count = unsentNoKeyRuns;\n    unsentNoKeyRuns = 0;\n    if (count > 0) {\n      console.warn(\n        `morse: ${count} run(s) not sent: no API key (set MORSE_API_KEY or call init())`,\n      );\n    }\n  } catch {\n    // Never break process exit.\n  }\n}\n\nexport function drainCounters(): CounterEntry[] {\n  if (counters.size === 0) return [];\n  const drained = [...counters.values()];\n  counters.clear();\n  return drained;\n}\n\nexport interface ReportOptions {\n  fingerprint: string;\n  extra?: Record<string, unknown>;\n  /**\n   * Print the raw local line only under `MORSE_SDK_DEBUG=1`. For failures a plain drop warning\n   * already explained (rejected key, unreachable endpoint), so the host is not shown an\n   * OpenTelemetry internal on top of it (MHQ-1838). Transmission is unaffected.\n   */\n  localOnlyInDebug?: boolean;\n}\n\n/**\n * Report an absorbed internal SDK error. Never throws.\n *\n * Signature is unchanged from the Sentry reporter it replaces, so every\n * `absorbErrors` call site keeps working untouched.\n */\nexport function report(error: unknown, options: ReportOptions): void {\n  if (reporting) return; // SDKDIAG-06\n  reporting = true;\n  try {\n    const errClass = error instanceof Error ? error.name : \"Error\";\n    const errMessage = error instanceof Error ? error.message : String(error);\n\n    // SDKDIAG-10: local FIRST, gated only by its own limiter. Transmission may\n    // be off, blocked, or failing — the host still sees it.\n    if (\n      (!options.localOnlyInDebug || isDebug()) &&\n      checkBucket(\n        localLogBuckets,\n        options.fingerprint,\n        LOCAL_LOG_LIMIT_PER_WINDOW,\n        LOCAL_LOG_WINDOW_MS,\n      )\n    ) {\n      const line = `morse internal error [${options.fingerprint}]: ${errClass}: ${errMessage}`;\n      if (isDebug()) console.error(line, error);\n      else console.warn(line);\n    }\n\n    if (!transmissionAllowed()) return;\n    if (!checkRateLimit(options.fingerprint)) return;\n\n    enqueue({\n      kind: \"error\",\n      fingerprint: options.fingerprint.slice(0, 255),\n      error_type: errClass.slice(0, 255),\n      message: redactedMessage(error),\n      stack: buildScopedStack(error),\n      sdk_language: \"typescript\",\n      sdk_version: sdkVersion(),\n      runtime_version: runtimeVersion(),\n      platform: platformLabel(),\n      extra: scrubExtra(options.extra),\n    });\n  } catch {\n    // Absolute last-resort: nothing here may propagate.\n  } finally {\n    reporting = false;\n  }\n}\n\nfunction sdkVersion(): string {\n  return SDK_VERSION;\n}\n\nfunction runtimeVersion(): string {\n  try {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const proc = (globalThis as any).process;\n    return proc?.version ? String(proc.version) : \"\";\n  } catch {\n    return \"\";\n  }\n}\n\nfunction platformLabel(): string {\n  try {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const proc = (globalThis as any).process;\n    if (proc?.platform) return `${proc.platform}-${proc.arch ?? \"\"}`.slice(0, 128);\n    return \"browser\";\n  } catch {\n    return \"\";\n  }\n}\n\nfunction enqueue(event: DiagnosticEvent): void {\n  // Oldest-first eviction: during an incident the newest diagnostics describe\n  // what is happening now; a buffer of stale ones is worth less.\n  pending.push(event);\n  while (pending.length > MAX_PENDING) pending.shift();\n  ensureTimer();\n}\n\n/**\n * Arm the flush timer on first use — never at import.\n *\n * Option B's lazy half, and it matters more here than in Python: an armed\n * `setInterval` keeps the Node event loop alive, so a healthy process that\n * armed one at import would refuse to exit. `unref()` where available means\n * even an armed timer never holds the process open.\n */\nfunction ensureTimer(): void {\n  if (timer !== null) return;\n  try {\n    timer = setInterval(() => {\n      void flushOnce();\n    }, FLUSH_INTERVAL_MS);\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    (timer as any)?.unref?.();\n  } catch {\n    timer = null;\n  }\n}\n\nfunction breakerBlocks(): boolean {\n  return breakerOpenUntil > 0 && Date.now() < breakerOpenUntil;\n}\n\nfunction recordResult(ok: boolean): void {\n  if (ok) {\n    consecutiveFailures = 0;\n    breakerOpenUntil = 0;\n    return;\n  }\n  consecutiveFailures += 1;\n  if (consecutiveFailures >= BREAKER_THRESHOLD) {\n    // Open the breaker; when it lapses exactly one probe goes out (the counter\n    // is not reset, so a failed probe re-opens it immediately).\n    breakerOpenUntil = Date.now() + BREAKER_COOLDOWN_MS;\n  }\n}\n\nexport async function flushOnce(): Promise<void> {\n  try {\n    const batch = pending.splice(0, MAX_BATCH_EVENTS);\n    const drained = drainCounters();\n    if (drained.length > 0) {\n      batch.push({\n        kind: \"counters\",\n        fingerprint: \"morse.transport.delivery\",\n        counters: drained,\n        sdk_language: \"typescript\",\n        sdk_version: sdkVersion(),\n      });\n    }\n    // SDKDIAG-01/09: a healthy interval transmits nothing.\n    if (batch.length === 0) return;\n    if (breakerBlocks() || !transmissionAllowed()) return;\n\n    const target = resolveTarget();\n    if (target === null) return;\n\n    let response: Response;\n    try {\n      response = await fetch(target.url, {\n        method: \"POST\",\n        headers: {\n          Authorization: `Bearer ${target.apiKey}`,\n          \"Content-Type\": \"application/json\",\n        },\n        body: JSON.stringify({ events: batch }),\n      });\n    } catch {\n      // Network fault. No retry — a retry loop here would amplify exactly the\n      // situation we are reporting about (SDKDIAG-06).\n      recordResult(false);\n      return;\n    }\n    setLevel(response.headers.get(\"X-Morse-Diagnostics\"));\n    recordResult(response.ok);\n  } catch {\n    // The flush loop must outlive any single batch.\n  }\n}\n\n// MHQ-927: the live client's configuration, pushed in by `init()`.\n//\n// Python's `_resolve_target` reads `morse_ai._client` first and only falls\n// back to the environment. TS read the environment ONLY, so a consumer who\n// configured the SDK programmatically — `morse.init({ apiKey, endpoint })`,\n// the idiomatic and documented form — had no resolvable target and every\n// diagnostic was silently dropped. Traces still worked, because the transport\n// gets its config from `init()` directly.\n//\n// Pushed rather than pulled, mirroring `setDiagnosticsRedactionConfig`, to\n// keep diagnostics free of an import cycle back into `index.ts`.\nlet clientConfig: { apiKey?: string; endpoint?: string } | null = null;\n\nexport function setDiagnosticsClientConfig(\n  config: { apiKey?: string; endpoint?: string } | null,\n): void {\n  clientConfig = config;\n}\n\nfunction resolveTarget(): { apiKey: string; url: string } | null {\n  try {\n    const apiKey = clientConfig?.apiKey || getApiKey();\n    if (!apiKey) return null; // No authenticated destination — local-log-only.\n    const url = deriveDiagnosticsUrl(String(clientConfig?.endpoint || getEndpoint() || \"\"));\n    if (!url) return null;\n    return { apiKey: String(apiKey), url };\n  } catch {\n    return null;\n  }\n}\n\nexport function deriveDiagnosticsUrl(tracesEndpoint: string): string | null {\n  try {\n    const parsed = new URL(tracesEndpoint);\n    return `${parsed.protocol}//${parsed.host}${DIAGNOSTICS_PATH}`;\n  } catch {\n    return null;\n  }\n}\n\n/**\n * Test-only reset hook.\n *\n * @internal\n */\nexport function _resetForTesting(): void {\n  clientConfig = null;\n  rateBuckets.clear();\n  localLogBuckets.clear();\n  counters.clear();\n  unsentNoKeyRuns = 0;\n  retryingNoticeShown = false;\n  pending = [];\n  if (timer !== null) {\n    clearInterval(timer);\n    timer = null;\n  }\n  consecutiveFailures = 0;\n  breakerOpenUntil = 0;\n  reporting = false;\n  level = LEVEL_ERROR;\n  activeRedactionConfig = null;\n}\n\n/**\n * True when the flush timer is armed. Exported so tests can assert the\n * central Option B claim: a healthy process arms nothing.\n *\n * @internal\n */\nexport function _hasTimer(): boolean {\n  return timer !== null;\n}\n","/**\n * Browser replacement for `otel-provider.ts` (MHQ-482 follow-up).\n *\n * Why this file exists\n * --------------------\n * `otel-provider.ts` is Node-only by construction — it statically imports\n * `@opentelemetry/sdk-trace-node`, `@opentelemetry/context-async-hooks`\n * (`async_hooks`), `@opentelemetry/instrumentation` (node `module`) and the\n * proto exporter's Node build. Because `index.ts` -> `tracing.ts` ->\n * `otel-provider.ts` is a static chain, ANY bundler targeting the browser\n * pulled those Node built-ins into the browser graph and failed to resolve\n * them. `tracing.ts`'s own docstring flagged this as a deferred decision\n * (\"a future browser-targeted build would need ... to keep the JSON path as\n * the browser default\"); this module is that decision, taken.\n *\n * The build swaps this module in for `otel-provider.ts` via the esbuild\n * plugin in `tsup.config.ts`, and `package.json`'s `\"browser\"` export\n * condition points browser bundlers at the resulting `dist/browser/*`. No\n * consumer configuration is involved — webpack, Turbopack, Vite, Rollup and\n * esbuild all honour the condition automatically.\n *\n * Behaviour, not a black hole\n * ---------------------------\n * Tracing still WORKS in the browser: `isOtelAvailable()` returns false, so\n * `tracing.ts::sendTrace` routes traces down the legacy JSON transport\n * (the same path `MORSE_LEGACY_TRANSPORT=1` selects in Node) instead of\n * dropping them. What is genuinely unavailable in a browser is the OTLP\n * span pipeline and DB-driver instrumentation; `noteBrowserRuntime()` says\n * so once, out loud, rather than leaving an integrator to wonder why their\n * spans look different from the Node docs.\n */\n\n/** Public surface mirrors `otel-provider.ts` exactly — see that module. */\nexport interface OtelEngineConfigureOptions {\n  apiKey?: string | undefined;\n  endpoint?: string | undefined;\n  sampleRate?: number | undefined;\n  maxFieldBytes?: number | undefined;\n  redaction?: unknown;\n  [key: string]: unknown;\n}\n\nexport interface TraceEntryLike {\n  trace: Record<string, unknown>;\n  spans: Record<string, unknown>[];\n}\n\nlet warned = false;\n\n/**\n * Emit the one-time \"you are on the browser build\" notice.\n *\n * Deliberately `console.info`, not `warn`: nothing is broken and nothing\n * needs fixing — a warning here would train integrators to ignore the\n * console. Fires only once per bundle, and only when the SDK is actually\n * configured (i.e. the app really did initialize Morse), so a page that\n * merely imports the SDK stays silent.\n */\nfunction noteBrowserRuntime(): void {\n  if (warned) return;\n  warned = true;\n  try {\n    console.info(\n      \"[morse] Browser build active: OpenTelemetry span export and database \" +\n        \"capture are Node-only, so traces are sent via Morse's JSON transport \" +\n        \"instead. Everything else — run()/span()/track() and W3C traceparent \" +\n        \"propagation — behaves identically. Server-side code (Next.js route \" +\n        \"handlers, RSC, Node services) automatically gets the full OTel pipeline.\",\n    );\n  } catch {\n    // A console-less runtime is not a reason to fail a customer's app.\n  }\n}\n\n/**\n * Browser stand-in for `OtelEngine`. Every method is a safe no-op with the\n * same signature, so `index.ts` needs no `typeof window` branches.\n */\nexport class OtelEngine {\n  // Params are declared (not dropped) so this class stays signature-compatible\n  // with the real `OtelEngine` — the build swaps the modules, but anything\n  // type-checking against this one must see the same shape. `void x` marks the\n  // deliberate non-use for @typescript-eslint/no-unused-vars.\n  configure(opts: OtelEngineConfigureOptions): void {\n    void opts;\n    noteBrowserRuntime();\n  }\n\n  shutdown(): void {}\n\n  resetForTesting(): void {\n    warned = false;\n  }\n\n  async flush(): Promise<void> {}\n\n  resolveSampled(traceIdHex: string): boolean {\n    void traceIdHex;\n    // Sampling in the browser is decided by the JSON transport path, which\n    // applies its own rate — claiming \"sampled\" here would double-apply it.\n    return true;\n  }\n\n  /**\n   * No OTLP pipeline in a browser, so there is no tracer to hand out —\n   * `null` is the same \"not available\" answer the Node engine gives before\n   * `configure()` (MHQ-1216: `morseTelemetry()` reports it and no-ops).\n   */\n  getTracer(): null {\n    return null;\n  }\n\n  registerInstrumentations(\n    instrumentations: unknown[],\n    wrapProvider?: unknown,\n  ): (() => void) | null {\n    void instrumentations;\n    void wrapProvider;\n    // DB-driver instrumentation requires `require-in-the-middle`; there is\n    // nothing to hook in a browser.\n    return null;\n  }\n}\n\nconst engine = new OtelEngine();\n\nexport function getEngine(): OtelEngine {\n  return engine;\n}\n\n/**\n * False in the browser build — `tracing.ts::sendTrace` reads this to pick\n * the JSON transport instead of the (absent) OTLP pipeline.\n */\nexport function isOtelAvailable(): boolean {\n  return false;\n}\n\n/**\n * Never called: `sendTrace` checks `isOtelAvailable()` first. Kept so the\n * module's shape matches `otel-provider.ts` (a shape mismatch would only\n * surface as a bundler error in a customer's build), and defensive — if it\n * ever IS reached, dropping the trace silently is the one outcome that must\n * not happen, so it reports rather than returning quietly.\n */\nexport function emitTraceEntry(entry: TraceEntryLike): void {\n  void entry;\n  noteBrowserRuntime();\n  throw new Error(\"morse: OTLP export is unavailable in the browser build\");\n}\n\n/** No live OTel context to enter in a browser — run `fn` unchanged. */\nexport function withOtelSpanContext<T>(traceIdHex: string, spanIdHex: string, fn: () => T): T {\n  void traceIdHex;\n  void spanIdHex;\n  return fn();\n}\n","/**\n * Deployment identity detected once at `init()` (ATTR-06, MHQ-1275): the git\n * revision the process runs, and the `service.version` an operator declared.\n *\n * Both ride the trace `resource` on the JSON path (`git.revision`,\n * `service.version`) and the OTel `Resource` on the OTLP path, so a trace can\n * be tied to the code that produced it without any per-call API.\n *\n * Git revision, first match wins:\n *\n *   1. `MORSE_GIT_REVISION`      — explicit override; the Docker-image answer\n *   2. `GITHUB_SHA`              — GitHub Actions\n *   3. `CI_COMMIT_SHA`           — GitLab CI\n *   4. `RAILWAY_GIT_COMMIT_SHA`  — Railway\n *   5. `VERCEL_GIT_COMMIT_SHA`   — Vercel\n *   6. `RENDER_GIT_COMMIT`       — Render\n *   7. `SOURCE_VERSION`          — Heroku\n *   8. `git rev-parse HEAD`      — Node only, and only if `child_process` is reachable\n *\n * Why the last step is loaded the way it is: this module ships in the browser\n * and edge bundles too, where a static `child_process` import is a build error\n * in the customer's bundler (`scripts/check-sdk-browser-bundle.mjs` fails the\n * release on exactly that). So the builtin is never named in an `import` or\n * `require(\"...\")` — it is fetched at run time through\n * `process.getBuiltinModule` (Node 20.16+/22.3+, identical in ESM and CJS),\n * falling back to a run-time-eval'd `require` for older CJS. Same technique as\n * `tracing.ts::tryLoadAls`. Anywhere neither exists (browsers, edge runtimes,\n * ESM on an old Node) the step is skipped and the revision is simply unknown.\n *\n * Nothing here ever throws into the host application.\n */\n\n/** Env vars consulted for the revision, in precedence order. */\nexport const GIT_REVISION_ENV_VARS: readonly string[] = [\n  \"MORSE_GIT_REVISION\",\n  \"GITHUB_SHA\",\n  \"CI_COMMIT_SHA\",\n  \"RAILWAY_GIT_COMMIT_SHA\",\n  \"VERCEL_GIT_COMMIT_SHA\",\n  \"RENDER_GIT_COMMIT\",\n  \"SOURCE_VERSION\",\n];\n\n/** The server strips then truncates to this; matching it avoids sending bytes it will drop. */\nconst MAX_REVISION_LENGTH = 40;\n/** Matches the server's `Trace.service_version` column cap. */\nconst MAX_SERVICE_VERSION_LENGTH = 128;\n/** `git rev-parse HEAD` is local and instant; anything slower is a hung filesystem. */\nconst GIT_TIMEOUT_MS = 500;\n\nfunction readEnv(name: string): string | undefined {\n  try {\n    if (typeof process !== \"undefined\" && process.env) {\n      const v = process.env[name];\n      if (typeof v === \"string\") return v;\n    }\n  } catch {\n    // Some sandboxes make `process.env` access throw.\n  }\n  return undefined;\n}\n\nfunction cleanRevision(raw: string | undefined): string | undefined {\n  if (raw === undefined) return undefined;\n  const v = raw.trim().slice(0, MAX_REVISION_LENGTH);\n  return v.length > 0 ? v : undefined;\n}\n\n/** The revision named by the environment, or undefined. Re-read on every call (cheap). */\nexport function revisionFromEnv(): string | undefined {\n  for (const name of GIT_REVISION_ENV_VARS) {\n    const v = cleanRevision(readEnv(name));\n    if (v !== undefined) return v;\n  }\n  return undefined;\n}\n\ntype ChildProcessLike = {\n  execFileSync(\n    file: string,\n    args: readonly string[],\n    options: Record<string, unknown>,\n  ): string | Uint8Array;\n};\n\nfunction defaultLoadChildProcess(): ChildProcessLike | null {\n  try {\n    if (typeof process === \"undefined\" || !process.versions?.node) return null;\n    try {\n      const getBuiltin = (process as unknown as { getBuiltinModule?: (m: string) => unknown })\n        .getBuiltinModule;\n      if (typeof getBuiltin === \"function\") {\n        const mod = getBuiltin.call(process, \"node:child_process\") as ChildProcessLike | undefined;\n        if (mod && typeof mod.execFileSync === \"function\") return mod;\n      }\n    } catch {\n      // Fall through to the require path.\n    }\n    // Run-time-eval'd so no bundler sees a `require(\"child_process\")` literal.\n    const dynamicRequire = Function(\"m\", \"return require(m)\") as (m: string) => unknown;\n    const mod = dynamicRequire(\"node:child_process\") as ChildProcessLike | undefined;\n    if (mod && typeof mod.execFileSync === \"function\") return mod;\n  } catch {\n    // child_process unavailable (browser, edge, ESM on old Node) — skip the step.\n  }\n  return null;\n}\n\nlet loadChildProcess: () => ChildProcessLike | null = defaultLoadChildProcess;\n\n/** `undefined` = not tried yet; `null` = tried, nothing found. Cached for the process. */\nlet gitCache: string | null | undefined;\n\nfunction revisionFromGit(): string | undefined {\n  if (gitCache !== undefined) return gitCache ?? undefined;\n  let found: string | null = null;\n  try {\n    const cp = loadChildProcess();\n    if (cp !== null) {\n      const out = cp.execFileSync(\"git\", [\"rev-parse\", \"HEAD\"], {\n        timeout: GIT_TIMEOUT_MS,\n        // stdin and stderr ignored: git must never prompt or write noise into the host's terminal.\n        stdio: [\"ignore\", \"pipe\", \"ignore\"],\n        encoding: \"utf8\",\n        windowsHide: true,\n      });\n      const text = typeof out === \"string\" ? out : new TextDecoder().decode(out);\n      const sha = cleanRevision(text);\n      // Only a real object id; a wrapper script printing a banner must not become the revision.\n      if (sha !== undefined && /^[0-9a-f]{7,64}$/i.test(sha)) found = sha;\n    }\n  } catch {\n    // Not a git checkout, git not installed, timed out — all mean \"unknown\".\n  }\n  gitCache = found;\n  return found ?? undefined;\n}\n\n/**\n * Parse `OTEL_RESOURCE_ATTRIBUTES` (`k1=v1,k2=v2`, values percent-encoded per the\n * OTel spec). Malformed pairs are skipped; a value that is not valid percent\n * encoding is kept verbatim rather than dropped.\n */\nexport function parseOtelResourceAttributes(raw: string | undefined): Record<string, string> {\n  const out: Record<string, string> = {};\n  if (!raw) return out;\n  for (const pair of raw.split(\",\")) {\n    const eq = pair.indexOf(\"=\");\n    if (eq <= 0) continue;\n    const key = pair.slice(0, eq).trim();\n    if (key === \"\") continue;\n    const rawValue = pair.slice(eq + 1).trim();\n    let value = rawValue;\n    try {\n      value = decodeURIComponent(rawValue);\n    } catch {\n      // keep verbatim\n    }\n    out[key] = value;\n  }\n  return out;\n}\n\n/** `service.version` from `OTEL_RESOURCE_ATTRIBUTES`; there is deliberately no init option. */\nexport function serviceVersionFromEnv(): string | undefined {\n  const v = parseOtelResourceAttributes(readEnv(\"OTEL_RESOURCE_ATTRIBUTES\"))[\"service.version\"];\n  if (v === undefined) return undefined;\n  const trimmed = v.trim().slice(0, MAX_SERVICE_VERSION_LENGTH);\n  return trimmed.length > 0 ? trimmed : undefined;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Process-level detected values, written by init()                         */\n/* ------------------------------------------------------------------------ */\n\nlet detectedGitRevision: string | undefined;\nlet detectedServiceVersion: string | undefined;\n\n/**\n * Resolve both values and remember them for the trace builders. Called from\n * `init()` only: the `git` subprocess (capped at 500ms, cached) must never run\n * on the per-trace hot path. Returns what it found. Never throws.\n */\nexport function detectDeploymentIdentity(): { gitRevision?: string; serviceVersion?: string } {\n  try {\n    detectedGitRevision = revisionFromEnv() ?? revisionFromGit();\n    detectedServiceVersion = serviceVersionFromEnv();\n  } catch {\n    detectedGitRevision = undefined;\n    detectedServiceVersion = undefined;\n  }\n  const out: { gitRevision?: string; serviceVersion?: string } = {};\n  if (detectedGitRevision !== undefined) out.gitRevision = detectedGitRevision;\n  if (detectedServiceVersion !== undefined) out.serviceVersion = detectedServiceVersion;\n  return out;\n}\n\nexport function getGitRevision(): string | undefined {\n  return detectedGitRevision;\n}\n\nexport function getServiceVersion(): string | undefined {\n  return detectedServiceVersion;\n}\n\n/**\n * Test hook — forget the detected values but KEEP the cached git result, so a suite\n * of hundreds of `init()` calls spawns `git` once per test file, not once per test.\n */\nexport function _clearDetectedIdentityForTesting(): void {\n  detectedGitRevision = undefined;\n  detectedServiceVersion = undefined;\n}\n\n/** Test hook — forget detected values and the cached git result. */\nexport function _resetDeploymentIdentityForTesting(): void {\n  detectedGitRevision = undefined;\n  detectedServiceVersion = undefined;\n  gitCache = undefined;\n  loadChildProcess = defaultLoadChildProcess;\n}\n\n/** Test hook — substitute the `child_process` loader (`() => null` simulates edge/browser). */\nexport function _setChildProcessLoaderForTesting(loader: () => ChildProcessLike | null): void {\n  loadChildProcess = loader;\n  gitCache = undefined;\n}\n","/**\n * Span model — the wire format the SDK sends to Morse.\n *\n * Mirrors `apps/sdk/python/src/morse/spans.py` field-for-field. The\n * backend's `SpanType` choices are the source of truth.\n */\n\nexport const SpanType = {\n  AGENT: \"agent\",\n  LLM: \"llm\",\n  TOOL: \"tool\",\n  HTTP: \"http\",\n  DB: \"db\",\n  // MHQ-1404: auto-instrumented broker publish spans. Infra like db/http: non-billable\n  // (ADR-033 D1) and never sets trace status. Matches Python `SpanType.QUEUE`.\n  QUEUE: \"queue\",\n  CUSTOM: \"custom\",\n  MEMORY: \"memory\",\n  // MHQ-482: matches Python's `SpanType.RETRIEVER` (apps/sdk/python/src/morse_ai/spans.py).\n  // @beta — retriever span type is subject to change before SDK v1.0 (see\n  // the plain-LangChain adapter's on_retriever_* callbacks in src/langchain/).\n  RETRIEVER: \"retriever\",\n  // MHQ-1025: matches Python's `SpanType.HANDOFF`. One span per routing\n  // decision between two agents, with `from_agent` / `to_agent` metadata.\n  HANDOFF: \"handoff\",\n} as const;\n\nexport type SpanType = (typeof SpanType)[keyof typeof SpanType];\n\nexport type SpanStatus = \"completed\" | \"failed\" | \"timed_out\";\n\n/**\n * Wire-format shape of one context segment carried on an LLM span.\n *\n * Mirrors the Python `morse.context.ContextSegmentDict` — keep both\n * sides in lockstep with the backend `ContextSegmentIn` Pydantic schema.\n *\n * - `tokens` is intentionally optional on the wire; the server tokenizes\n *   when omitted to keep client-side cost and dependency surface small.\n * - `source` accepts either underscored DB enum values (`memory_vector`)\n *   or the dotted public-API form (`memory.vector`); the server\n *   `normalize_segment_source` helper translates dots → underscores.\n */\nexport interface ContextSegment {\n  position: number;\n  source: string;\n  content_hash: string;\n  content?: string;\n  content_truncated?: boolean;\n  tokens?: number;\n  tokens_estimated?: boolean;\n  cache_state?: \"cold\" | \"cache_write\" | \"cache_hit\";\n  attrs?: Record<string, unknown>;\n  producer_span_id?: string;\n}\n\nexport interface SpanData {\n  trace_id: string;\n  span_id: string;\n  type: SpanType;\n  name: string;\n  parent_span_id?: string;\n  started_at: string;\n  ended_at?: string;\n  duration_ms?: number;\n  status: SpanStatus;\n  // LLM-specific\n  model?: string;\n  input_tokens?: number;\n  output_tokens?: number;\n  cached_input_tokens?: number;\n  reasoning_tokens?: number;\n  cache_write_tokens?: number;\n  total_tokens?: number;\n  cost_usd?: number;\n  // Payload\n  input_data?: unknown;\n  output_data?: unknown;\n  error_message?: string;\n  /** Class name of the thrown error that failed this span (failure grouping). */\n  exception_type?: string;\n  metadata?: Record<string, unknown>;\n  /**\n   * Pre-segmented context profile (Anthropic / LangGraph adapters populate this).\n   * `undefined` → server runs its messages-array classifier.\n   * `[]`        → SDK explicitly says no segments (server skips classifier).\n   * Populated   → server skips classification and persists as-is.\n   */\n  context_segments?: ContextSegment[];\n}\n\nexport interface TraceFields {\n  trace_id: string;\n  /** Backend `TracePayloadIn.agent` — matches Python SDK + Pydantic schema. */\n  agent: string;\n  status: SpanStatus;\n  started_at: string;\n  ended_at?: string;\n  duration_ms?: number;\n  error_message?: string;\n  success?: boolean;\n  outcome?: string;\n}\n\nexport interface TraceEntry {\n  trace: TraceFields;\n  spans: SpanData[];\n}\n\n/** 32-char hex (128-bit). Compatible with W3C trace_id format. */\nexport function generateTraceId(): string {\n  return randomHex(32);\n}\n\n/**\n * 32-char hex (UUID4-style, 128-bit). Matches the backend's UUID-typed\n * `span_id` column and the sibling Python SDK (`uuid.uuid4().hex`).\n *\n * When the SDK emits a W3C traceparent header (see `src/propagation.ts`),\n * the first 16 hex chars are sliced off to fit the W3C `span_id` slot —\n * that interop is handled at the header-formatting layer, not here.\n *\n * Was 16 hex chars historically (W3C span_id format) until Phase 13\n * Option A dogfood surfaced silent backend rejection: the dev ingest\n * `TracePayloadIn` schema requires UUID-shaped span_ids and was tossing\n * every TS-emitted span with `Invalid span_id format (expected UUID)`.\n */\nexport function generateSpanId(): string {\n  return randomHex(32);\n}\n\nfunction randomHex(chars: number): string {\n  const bytes = new Uint8Array(Math.ceil(chars / 2));\n  // Use Web Crypto when available (Node 19+, all browsers).\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const cryptoObj = (globalThis as any).crypto as Crypto | undefined;\n  if (cryptoObj?.getRandomValues) {\n    cryptoObj.getRandomValues(bytes);\n  } else {\n    // Fallback: Math.random — non-cryptographic but acceptable for trace IDs.\n    for (let i = 0; i < bytes.length; i++) {\n      bytes[i] = Math.floor(Math.random() * 256);\n    }\n  }\n  let hex = \"\";\n  for (const b of bytes) {\n    hex += b.toString(16).padStart(2, \"0\");\n  }\n  return hex.slice(0, chars);\n}\n\nexport function nowIso(): string {\n  return new Date().toISOString();\n}\n\n/** Serialize a SpanData, dropping undefined fields, matching Python's to_dict(). */\nexport function spanToWire(span: SpanData): Record<string, unknown> {\n  const out: Record<string, unknown> = {};\n  for (const [k, v] of Object.entries(span)) {\n    if (v !== undefined && v !== null) {\n      out[k] = v;\n    }\n  }\n  return out;\n}\n\nexport function buildTraceEntry(\n  fields: TraceFields,\n  spans: SpanData[],\n): { trace: TraceFields; spans: Record<string, unknown>[] } {\n  return {\n    trace: fields,\n    spans: spans.map(spanToWire),\n  };\n}\n","/**\n * Agent-name resolution + process service-name capture (MHQ-1100).\n *\n * Morse ingest treats a real OTel `service.name` as the agent name, so the\n * SDK must never let an environment-derived `service.name`\n * (`OTEL_SERVICE_NAME`, `OTEL_RESOURCE_ATTRIBUTES`) reach the resource as\n * identity. The agent name travels as the root-span attribute `morse.agent`;\n * the process name is preserved as `morse.process.service_name`.\n *\n * Mirror of the Python SDK's resolver. Tiers, first non-empty wins:\n *   1. explicit name from customer code\n *   2. framework-native name the integration knows\n *   3. env service name (`OTEL_SERVICE_NAME` / `service.name` in\n *      `OTEL_RESOURCE_ATTRIBUTES`)\n *   4. entry-script basename without extension (`process.argv[1]`), skipping\n *      generic names\n *   5. `\"default-agent\"`\n *\n * Hardcoded placeholder defaults inside integrations count as NOT explicit:\n * integrations pass `undefined` for the explicit tier in that case.\n */\n\nexport const DEFAULT_AGENT_NAME = \"default-agent\";\n\n// AID-01: also framework launcher scripts (Next.js `start-server`), which name no app.\nconst GENERIC_SCRIPT_NAMES: ReadonlySet<string> = new Set([\n  \"node\",\n  \"index\",\n  \"main\",\n  \"server\",\n  \"start-server\",\n  \"next\",\n  \"next-server\",\n  \"ts-node\",\n  \"tsx\",\n]);\n\n/**\n * AID-02/AID-03: names frameworks give their own plumbing. Never an agent identity,\n * and adapters record these as chain spans, not agent spans. Mirrors the API denylist\n * (`apps/api/apps/agents/framework_nodes.py`).\n */\nconst FRAMEWORK_NODE_NAMES: ReadonlySet<string> = new Set([\n  \"chain\",\n  \"__start__\",\n  \"__end__\",\n  \"tools\",\n  \"agent\",\n  \"route\",\n  \"model\",\n  \"langgraph\",\n  \"langchain\",\n  \"runnablesequence\",\n  \"runnablelambda\",\n  \"runnableparallel\",\n  \"runnablepassthrough\",\n  \"chatprompttemplate\",\n  \"prompttemplate\",\n  \"stroutputparser\",\n  \"jsonoutputparser\",\n  \"toolnode\",\n]);\n\nexport function isFrameworkNodeName(name: unknown): boolean {\n  const n = clean(name);\n  if (!n) return false;\n  const bare = n.replace(/\\s*\\[(langchain|langgraph)\\]\\s*$/i, \"\").toLowerCase();\n  return FRAMEWORK_NODE_NAMES.has(bare) || /^\\[(langchain|langgraph)\\]$/i.test(n);\n}\n\n/** `MORSE_AGENT_IDENTITY=legacy` restores pre-0.7 (0.6.0) naming (AID-09). Read per call. */\nexport function isLegacyIdentity(env: NodeJS.ProcessEnv = process.env): boolean {\n  return clean(env?.MORSE_AGENT_IDENTITY)?.toLowerCase() === \"legacy\";\n}\n\nexport type IdentitySource = \"run\" | \"framework\" | \"service_name\" | \"entry_script\" | \"default\";\n\nfunction clean(value: unknown): string | undefined {\n  if (typeof value !== \"string\") return undefined;\n  const trimmed = value.trim();\n  return trimmed.length > 0 ? trimmed : undefined;\n}\n\n/** `service.name` from `OTEL_SERVICE_NAME`, else `OTEL_RESOURCE_ATTRIBUTES`. */\nexport function readEnvServiceName(env: NodeJS.ProcessEnv = process.env): string | undefined {\n  const direct = clean(env.OTEL_SERVICE_NAME);\n  if (direct) return direct;\n  const attrs = clean(env.OTEL_RESOURCE_ATTRIBUTES);\n  if (!attrs) return undefined;\n  for (const pair of attrs.split(\",\")) {\n    const eq = pair.indexOf(\"=\");\n    if (eq < 0) continue;\n    if (pair.slice(0, eq).trim() !== \"service.name\") continue;\n    let v = pair.slice(eq + 1).trim();\n    try {\n      v = decodeURIComponent(v);\n    } catch {\n      // keep raw value\n    }\n    const c = clean(v);\n    if (c) return c;\n  }\n  return undefined;\n}\n\n/** Entry script basename without extension, or undefined if generic/empty. */\nexport function entryScriptName(argv: readonly string[] = process.argv): string | undefined {\n  const script = clean(argv[1]);\n  if (!script) return undefined;\n  const base = script.split(/[\\\\/]/).pop() ?? \"\";\n  const name = clean(base.replace(/\\.[^./\\\\]+$/, \"\"));\n  if (!name || GENERIC_SCRIPT_NAMES.has(name.toLowerCase())) return undefined;\n  return name;\n}\n\nexport interface ResolveAgentNameOptions {\n  /** Name passed by customer code. */\n  explicit?: string | null;\n  /** Name a framework integration natively knows (graph name, agent name). */\n  framework?: string | null;\n  env?: NodeJS.ProcessEnv;\n  argv?: readonly string[];\n}\n\n/** The ladder with the rung that won, so unnamed agents can be flagged (AID-01). */\nexport function resolveAgentIdentity(opts: ResolveAgentNameOptions = {}): {\n  name: string;\n  source: IdentitySource;\n} {\n  const explicit = clean(opts.explicit);\n  if (explicit) return { name: explicit, source: \"run\" };\n  const framework = clean(opts.framework);\n  if (framework) return { name: framework, source: \"framework\" };\n  const service = readEnvServiceName(opts.env);\n  if (service) return { name: service, source: \"service_name\" };\n  const script = entryScriptName(opts.argv);\n  if (script) return { name: script, source: \"entry_script\" };\n  return { name: DEFAULT_AGENT_NAME, source: \"default\" };\n}\n\nexport function resolveAgentName(opts: ResolveAgentNameOptions = {}): string {\n  return resolveAgentIdentity(opts).name;\n}\n","/**\n * Client-side normalisation for the attribution facts (ATTR-01/04, MHQ-1275):\n * record keys, declared agent version, and prompt references.\n *\n * The server re-validates everything with the same rules, so this module exists\n * to keep bad input off the wire and to give the developer a reason when a value\n * is dropped. It follows the SDK's non-invasive rule: every function here\n * returns \"dropped\" instead of throwing, and none calls a customer's\n * `toString`/`valueOf` — only plain strings and numbers are accepted, so there is\n * no customer code to run.\n */\n\nexport const MAX_RECORD_KEYS = 20;\nexport const MAX_PROMPT_REFS = 20;\nexport const MAX_AGENT_VERSION_LENGTH = 128;\nexport const MAX_PROMPT_NAME_LENGTH = 255;\nexport const MAX_RECORD_KEY_VALUE_LENGTH = 256;\n\nconst RECORD_KEY_TYPE_RE = /^[a-z0-9][a-z0-9_.-]{0,63}$/;\n\nexport interface PromptRef {\n  name: string;\n  version?: number;\n  /** Set only when `setPrompt()` ran inside a child span; absent means \"the trace\". */\n  span_id?: string;\n  /**\n   * UP-09: `\"fetch\"` when `getPrompt()` created the reference by linking the active run.\n   * Absent for `setPrompt()` (the server reads absent as \"declared\").\n   */\n  link_source?: \"fetch\";\n}\n\nconst warned = new Set<string>();\n\n/** One `console.warn` per kind per process — these sit in request paths. Never includes the value (it may be an id or PII). */\nfunction warnOnce(kind: string, message: string): void {\n  if (warned.has(kind)) return;\n  warned.add(kind);\n  try {\n    console.warn(`morse: ${message} (this warning prints once per process)`);\n  } catch {\n    // a hostile console must not break the host\n  }\n}\n\n/** Test hook — re-arm the once-per-process warnings. */\nexport function _resetAttributionWarningsForTesting(): void {\n  warned.clear();\n}\n\n/** Accepts string or finite number; anything else (objects, null, NaN) is dropped without being stringified. */\nfunction plainToString(v: unknown): string | null {\n  if (typeof v === \"string\") return v;\n  if (typeof v === \"number\" && Number.isFinite(v)) return String(v);\n  if (typeof v === \"bigint\") return v.toString();\n  return null;\n}\n\n/**\n * Normalise one record-key pair, or return null when invalid.\n * Type: lower-cased, `^[a-z0-9][a-z0-9_.-]{0,63}$`. Value: string/number, trimmed, 1–256 chars.\n */\nexport function normalizeRecordKey(keyType: unknown, keyValue: unknown): [string, string] | null {\n  if (typeof keyType !== \"string\") return null;\n  const type = keyType.trim().toLowerCase();\n  if (!RECORD_KEY_TYPE_RE.test(type)) return null;\n  const raw = plainToString(keyValue);\n  if (raw === null) return null;\n  const value = raw.trim();\n  if (value.length < 1 || value.length > MAX_RECORD_KEY_VALUE_LENGTH) return null;\n  return [type, value];\n}\n\n/**\n * Add `pairs` to `target`, enforcing validation and the 20-key cap. Re-setting an\n * existing type replaces its value (and never counts against the cap). Returns\n * how many pairs were dropped. Never throws.\n */\nexport function mergeRecordKeys(\n  target: Record<string, string>,\n  pairs: Iterable<readonly [unknown, unknown]>,\n): number {\n  let dropped = 0;\n  for (const [t, v] of pairs) {\n    const norm = normalizeRecordKey(t, v);\n    if (norm === null) {\n      dropped += 1;\n      continue;\n    }\n    const [type, value] = norm;\n    if (!(type in target) && Object.keys(target).length >= MAX_RECORD_KEYS) {\n      dropped += 1;\n      continue;\n    }\n    target[type] = value;\n  }\n  if (dropped > 0) {\n    warnOnce(\n      \"record_keys\",\n      `dropped ${dropped} invalid record key(s). A key type must match ^[a-z0-9][a-z0-9_.-]{0,63}$ ` +\n        `(lower-cased for you), a value must be a string or number of 1-256 characters, ` +\n        `and a trace keeps at most ${MAX_RECORD_KEYS} keys`,\n    );\n  }\n  return dropped;\n}\n\n/** Entries of an arbitrary object without ever throwing (hostile getters, non-objects). */\nexport function safeEntries(input: unknown): Array<[string, unknown]> {\n  if (input === null || typeof input !== \"object\") return [];\n  const out: Array<[string, unknown]> = [];\n  try {\n    for (const k of Object.keys(input as object)) {\n      try {\n        out.push([k, (input as Record<string, unknown>)[k]]);\n      } catch {\n        // an unreadable value costs that key, not the rest\n      }\n    }\n  } catch {\n    // unreadable object\n  }\n  return out;\n}\n\n/** Trim and truncate to 128; empty or non-string yields undefined. */\nexport function normalizeAgentVersion(v: unknown): string | undefined {\n  const raw = plainToString(v);\n  if (raw === null) return undefined;\n  const s = raw.trim().slice(0, MAX_AGENT_VERSION_LENGTH);\n  return s.length > 0 ? s : undefined;\n}\n\n/**\n * Normalise one prompt reference. A name is required (trimmed, ≤255). A version\n * must be an integer ≥1; any other version is dropped but the name is kept, which\n * is the same rule the server applies.\n */\nexport function normalizePromptRef(\n  name: unknown,\n  version?: unknown,\n  spanId?: string,\n): PromptRef | null {\n  if (typeof name !== \"string\") return null;\n  const n = name.trim().slice(0, MAX_PROMPT_NAME_LENGTH);\n  if (n.length === 0) return null;\n  const ref: PromptRef = { name: n };\n  const v = coercePromptVersion(version);\n  if (v !== undefined) ref.version = v;\n  else if (version !== undefined && version !== null) {\n    warnOnce(\n      \"prompt_version\",\n      \"a setPrompt() version that is not a whole number >= 1 was dropped and only the \" +\n        'prompt name recorded; pass the registry version number (e.g. 3 or \"3\")',\n    );\n  }\n  if (spanId) ref.span_id = spanId;\n  return ref;\n}\n\n/**\n * MHQ-1415: `setPrompt(\"triage\", \"3\")` used to drop the version silently. A\n * whole-number string (`\"3\"`, `\"v3\"`) is now read as that version; anything\n * else that is not an integer ≥1 is still dropped (the server keeps integer\n * versions only), but with a one-time warning.\n */\nexport function coercePromptVersion(version: unknown): number | undefined {\n  if (typeof version === \"number\") {\n    return Number.isInteger(version) && version >= 1 ? version : undefined;\n  }\n  if (typeof version === \"string\") {\n    const m = /^v?(\\d{1,9})$/i.exec(version.trim());\n    if (m) {\n      const n = Number(m[1]);\n      return n >= 1 ? n : undefined;\n    }\n  }\n  return undefined;\n}\n\n/**\n * Append a ref unless it duplicates one already held or the 20-ref cap is reached. Returns whether it was added.\n *\n * Dedupe key is (name, version, span_id). When a `setPrompt()` ref and a `getPrompt()` link\n * collide, ONE ref is kept and it is tagged `link_source: \"fetch\"` whichever came first:\n * \"fetch\" is the stronger provenance (the SDK saw the text), so it wins over \"declared\".\n */\nexport function addPromptRef(list: PromptRef[], ref: PromptRef): boolean {\n  const existing = list.find(\n    (p) => p.name === ref.name && p.version === ref.version && p.span_id === ref.span_id,\n  );\n  if (existing) {\n    if (ref.link_source === \"fetch\") existing.link_source = \"fetch\";\n    return false;\n  }\n  if (list.length >= MAX_PROMPT_REFS) {\n    warnOnce(\n      \"prompt_refs\",\n      `a trace keeps at most ${MAX_PROMPT_REFS} prompt references; further setPrompt() calls are ignored`,\n    );\n    return false;\n  }\n  list.push(ref);\n  return true;\n}\n\nexport function warnPromptRejected(): void {\n  warnOnce(\n    \"prompt_name\",\n    \"setPrompt() needs a non-empty name of at most 255 characters; the call was ignored\",\n  );\n}\n\nexport function warnPromptSpanShared(): void {\n  warnOnce(\n    \"prompt_span_shared\",\n    \"several prompt references were set on one span; OTLP carries one per span, so only the last \" +\n      \"is exported. Call setPrompt() inside separate morse.span() blocks to record each\",\n  );\n}\n\n/** One prompt reference as OTLP carries it (`morse.prompt.*` span attributes). */\nexport interface OtlpPromptRef {\n  name: string;\n  version?: number;\n  link_source?: \"fetch\";\n}\n\n/**\n * OTLP carries a single `morse.prompt.name`/`.version` pair per span. Assign each\n * ref to the span it was set on (refs with no span, or whose span never finished,\n * go to the root span). When several land on one span the LAST wins (matching the\n * Python SDK); the earlier ones are\n * reported by the caller via `warnPromptSpanShared()`. The JSON path keeps them all.\n */\nexport function assignPromptRefsToSpans(\n  refs: unknown,\n  knownSpanIds: ReadonlySet<string>,\n  rootSpanId: string | undefined,\n): { bySpan: Map<string, OtlpPromptRef>; overflow: number } {\n  const bySpan = new Map<string, OtlpPromptRef>();\n  let overflow = 0;\n  if (!Array.isArray(refs)) return { bySpan, overflow };\n  for (const r of refs) {\n    if (r === null || typeof r !== \"object\") continue;\n    const {\n      name,\n      version,\n      span_id: spanId,\n      link_source: linkSource,\n    } = r as Record<string, unknown>;\n    if (typeof name !== \"string\" || name === \"\") continue;\n    let target: string | undefined;\n    if (typeof spanId === \"string\" && knownSpanIds.has(spanId)) target = spanId;\n    else target = rootSpanId;\n    if (target === undefined) continue;\n    if (bySpan.has(target)) overflow += 1;\n    const entry: OtlpPromptRef = { name };\n    if (linkSource === \"fetch\") entry.link_source = \"fetch\";\n    if (typeof version === \"number\" && Number.isInteger(version) && version >= 1) {\n      entry.version = version;\n    }\n    bySpan.set(target, entry);\n  }\n  return { bySpan, overflow };\n}\n","/**\n * SDK V2 tracing core — TraceState, run(), span() context-manager-likes.\n *\n * Mirrors `apps/sdk/python/src/morse/tracing.py`. Key differences:\n *\n * - Python uses `contextvars.ContextVar` for async/coroutine isolation.\n *   In Node we use `node:async_hooks.AsyncLocalStorage` when available\n *   (server-side, including Next.js API routes). On the browser there\n *   is no equivalent; we fall back to a module-global slot. This is\n *   acceptable because in the browser there is exactly one event loop\n *   and one user — no parallel traces.\n *\n * - \"Context manager\" idioms in TS use callback-style: `run(name, fn)`\n *   and `span(name, type, fn)` accept a function and invoke it with\n *   the active state on the stack. We keep an explicit `start/end`\n *   pair on the public surface for callers who can't use callbacks\n *   (e.g., when the work spans an SSE stream).\n *\n * - MHQ-482: `run()`/`span()`/`runTraced()`'s emission path now goes\n *   through `./otel-provider` (real OTel spans over OTLP, Node-only —\n *   mirrors the Python SDK's MHQ-668 `otel_provider.py`). This is a\n *   deliberate Node-only deviation from this module's own browser\n *   fallback for `AsyncLocalStorage` above: `otel-provider.ts` statically\n *   imports `@opentelemetry/sdk-trace-node`, which pulls in Node built-ins\n *   (`node:crypto`, `node:http`/`https`) that don't exist in a browser\n *   bundle. A future browser-targeted build would need either a\n *   browser-flavored OTel exporter swapped in at this call site or to\n *   keep `MORSE_LEGACY_TRANSPORT=1`'s JSON path as the browser default —\n *   out of scope for this port (see the PR report's deviation notes).\n *\n * - MHQ-482 (db-capture follow-up): `runSync`/`runAsync`/`spanSync`/\n *   `spanAsync` now ALSO wrap `fn()` in a live OTel context via\n *   `otel-provider.ts::withOtelSpanContext` (Node only — a no-op\n *   passthrough everywhere else, including the browser fallback path,\n *   since that helper itself no-ops when the OTel engine isn't\n *   configured). This is what lets community DB-driver instrumentation\n *   (`./db-capture.ts`) — which reads OTel's *global*\n *   `context.active()`, not this module's `TraceState` — see the right\n *   trace id and nest under the right parent span id when a query runs\n *   inside an active `morse.span()`/`morse.run()` block. It's a\n *   deliberately separate mechanism from `emitTraceEntry`'s\n *   synthetic-parent trick: that one runs once at trace-close time over\n *   already-built `SpanData`; this one runs live, for spans this module\n *   never sees (they're built entirely inside the instrumentation\n *   package and exported directly to the same `NodeTracerProvider`).\n */\n\nimport {\n  isFrameworkNodeName,\n  isLegacyIdentity,\n  resolveAgentIdentity,\n  type IdentitySource,\n} from \"./agent-name\";\nimport {\n  addPromptRef,\n  mergeRecordKeys,\n  normalizeAgentVersion,\n  normalizePromptRef,\n  safeEntries,\n  warnPromptRejected,\n  type PromptRef,\n} from \"./attribution\";\nimport { isLegacyTransport, isValidInternalSource } from \"./env\";\nimport { emitTraceEntry, isOtelAvailable, withOtelSpanContext } from \"./otel-provider\";\nimport { report, setRedactionConfig as setDiagnosticsRedactionConfig } from \"./diagnostics\";\nimport { buildRedactionConfig, type RedactionConfig, redactValue } from \"./redaction\";\nimport { getGitRevision, getServiceVersion } from \"./revision\";\nimport type { SpanData, SpanStatus, SpanType } from \"./spans\";\nimport { generateSpanId, generateTraceId, nowIso } from \"./spans\";\nimport { extractTraceparent } from \"../propagation\";\n\n/* ------------------------------------------------------------------------ */\n/*  Internal state                                                           */\n/* ------------------------------------------------------------------------ */\n\nexport interface TraceState {\n  traceId: string;\n  agentName: string;\n  // AID-01: which ladder rung produced `agentName`. Absent under MORSE_AGENT_IDENTITY=legacy.\n  identitySource?: IdentitySource;\n  // AID-04: root agent an adapter discovered (graph / starting-agent name). First wins.\n  frameworkRoot?: string;\n  spanStack: SpanData[];\n  completedSpans: SpanData[];\n  // Mutable outcome fields — set by `run()` callers via the returned handle.\n  success?: boolean;\n  outcome?: string;\n  errorMessage?: string;\n  // Multi-turn attribution (MHQ-748). threadId groups traces in the same\n  // conversation; userId is the app-layer end-user identifier. Both\n  // optional — mirrors Python's `_TraceState.thread_id`/`user_id`.\n  threadId?: string;\n  userId?: string;\n  // Cost-attribution dimensions — set via `run({ userMetadata })` at\n  // trace-creation time, or mutated mid-trace via `setContext()` (MHQ-751).\n  // Keys/values are always strings at the SDK boundary — mirrors Python's\n  // `_TraceState.user_metadata`.\n  userMetadata?: Record<string, string>;\n  // Per-trace override of the resource-level `morse_ai.source` tag. Wins\n  // over the process-level `_source` when set — mirrors Python's\n  // `_TraceState.source_override`.\n  sourceOverride?: string;\n  // MHQ-859: framework recorded by whichever adapter fired first inside\n  // this trace (`_internal/active-framework.ts`). Lifted onto the trace\n  // resource as `morse_ai.framework` — mirrors Python's\n  // `_TraceState.framework`. Absence is the signal; never defaulted.\n  framework?: string;\n  // INFRACAP-04: per-trace counters for default-on infra capture and the one\n  // summary span per kind that absorbs calls past the cap. Owned by\n  // `infra-shared.ts`; mirrors Python's `_TraceState.infra_counts/summaries`.\n  infraCounts?: Record<string, number>;\n  infraSummaries?: Record<string, SpanData>;\n  // ATTR-01/04 (MHQ-1275): business-record keys, declared agent version and\n  // prompt references. Already normalised (see `attribution.ts`) — buildTraceDict\n  // and the OTLP mapper copy them out verbatim.\n  recordKeys?: Record<string, string>;\n  agentVersion?: string;\n  prompts?: PromptRef[];\n  // AID-04: the name passed to `run()` when it differs from the discovered root.\n  appName?: string;\n  // MHQ-1407: open holds on this trace (a streamed LLM response handed back to\n  // the framework before it is read). The trace is finalised when the last\n  // hold is released, not when the `run()` callback returns. See `holdTraceOpen`.\n  openHolds?: number;\n  // Set by `endTrace` while it waits for the holds; the last release calls it.\n  onHoldsReleased?: () => void;\n}\n\ntype AlsLike<T> = {\n  getStore(): T | undefined;\n  run<R>(value: T, fn: () => R): R;\n};\n\nlet als: AlsLike<TraceState> | null = null;\nlet alsLoaded = false;\nlet browserState: TraceState | null = null;\n\nfunction tryLoadAls(): AlsLike<TraceState> | null {\n  if (alsLoaded) return als;\n  alsLoaded = true;\n  // Skip in the browser.\n  if (typeof process === \"undefined\" || !process.versions?.node) {\n    als = null;\n    return als;\n  }\n  // MHQ-924: two resolution paths, tried in order. `require` alone is NOT\n  // enough — it does not exist in ESM, so every `.mjs` / `\"type\": \"module\"`\n  // consumer silently fell through to the browser fallback below. That\n  // fallback is a module-global, which survives synchronous code but is lost\n  // across the first `await`, so EVERY child span was dropped with \"no active\n  // trace\" while the root span still persisted. Node consumers were never\n  // getting AsyncLocalStorage at all.\n  //\n  // Order matters: getBuiltinModule first, because it is the only one of the\n  // two that works in both module systems.\n  //\n  //   1. `process.getBuiltinModule` — Node 22.3+, synchronous, and identical\n  //      in ESM and CJS. Not a bundler-visible specifier, so the browser\n  //      build still cannot try to resolve `node:async_hooks`.\n  //   2. runtime-eval'd `require` — CJS, and older Node versions.\n  //\n  // A dynamic `import()` is not an option: this function is synchronous and\n  // is called on the hot path by getActiveTraceState().\n  try {\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const getBuiltin = (process as any)?.getBuiltinModule as ((m: string) => unknown) | undefined;\n    if (typeof getBuiltin === \"function\") {\n      // eslint-disable-next-line @typescript-eslint/no-explicit-any\n      const mod = getBuiltin(\"node:async_hooks\") as any;\n      if (mod?.AsyncLocalStorage) {\n        als = new mod.AsyncLocalStorage() as AlsLike<TraceState>;\n        return als;\n      }\n    }\n  } catch {\n    // Fall through to the require path.\n  }\n  try {\n    // Use a runtime-eval'd require so the browser bundler doesn't try\n    // to resolve `node:async_hooks`.\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const dynamicRequire = Function(\"m\", \"return require(m)\") as any as (m: string) => unknown;\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    const mod = dynamicRequire(\"node:async_hooks\") as any;\n    if (mod?.AsyncLocalStorage) {\n      als = new mod.AsyncLocalStorage() as AlsLike<TraceState>;\n      return als;\n    }\n  } catch {\n    // Fall through to browser fallback.\n  }\n  // Genuinely no ALS: browser, or a Node old enough to have neither path.\n  // The module-global fallback below is correct there — a browser has no\n  // concurrent async contexts to confuse it.\n  als = null;\n  return als;\n}\n\nexport function getActiveTraceState(): TraceState | null {\n  const store = tryLoadAls();\n  if (store) {\n    return store.getStore() ?? null;\n  }\n  return browserState;\n}\n\nfunction setActiveTraceState(state: TraceState | null): () => void {\n  // Returns a \"reset\" callback. Used only on the browser path. On Node,\n  // `als.run()` handles the lifecycle.\n  const previous = browserState;\n  browserState = state;\n  return () => {\n    browserState = previous;\n  };\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Transport injection                                                      */\n/* ------------------------------------------------------------------------ */\n\nexport interface TransportLike {\n  track(event: { traces: unknown[] } | Record<string, unknown>): boolean;\n}\n\nlet transport: TransportLike | null = null;\n\nexport function setTransport(t: TransportLike | null): void {\n  transport = t;\n}\n\nexport function getTransport(): TransportLike | null {\n  return transport;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Redaction config — module-level, replaceable on init()                   */\n/* ------------------------------------------------------------------------ */\n\n// Default-on with the canonical catalogue. Replaced when `init()` runs\n// with explicit redaction options. Customers calling SDK helpers before\n// `init()` still get the default redaction floor.\nlet redactionConfig: RedactionConfig = buildRedactionConfig();\n\nexport function setRedactionConfig(config: RedactionConfig): void {\n  redactionConfig = config;\n  // Keep the diagnostics reporter's view in sync so an exception MESSAGE gets\n  // the same redaction as span content (SDKDIAG-02). Pushed rather than pulled\n  // to keep diagnostics free of a cycle back into tracing.\n  setDiagnosticsRedactionConfig(config);\n}\n\nexport function getRedactionConfig(): RedactionConfig {\n  return redactionConfig;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Resource-level \"source\" label (dogfood emitter discrimination)           */\n/* ------------------------------------------------------------------------ */\n\n// Process-level source tag. Lifted into each outgoing trace's `resource`\n// dict so the backend can discriminate Morse's own dogfood emitters\n// from customer traces. Mirrors the Python SDK's module-level `_source`.\nlet _source: string | null = null;\n\nexport function setSource(value: string | null | undefined): void {\n  _source = value && value.length > 0 ? value : null;\n}\n\nexport function getSource(): string | null {\n  return _source;\n}\n\n// Process-level default `agent_version` from `init({ agentVersion })`; a per-run\n// `RunOptions.agentVersion` overrides it. Normalised before storing.\nlet _agentVersionDefault: string | undefined;\n\nexport function setAgentVersionDefault(value: unknown): void {\n  _agentVersionDefault = normalizeAgentVersion(value);\n}\n\n// SETUP-10: `init({ captureEndUser: true })` opt-in. Off by default: adapters\n// never read the provider's `user` / `metadata.user_id` unless this is set.\nlet _captureEndUser = false;\n\nexport function setCaptureEndUser(value: unknown): void {\n  _captureEndUser = value === true;\n}\n\nexport function isCaptureEndUser(): boolean {\n  return _captureEndUser;\n}\n\nlet lastTrace: { trace: unknown; spans: unknown[] } | null = null;\n\nexport function getLastTrace(): { trace: unknown; spans: unknown[] } | null {\n  return lastTrace;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Trace lifecycle helpers                                                  */\n/* ------------------------------------------------------------------------ */\n\nfunction nowMonotonic(): number {\n  // performance.now() is available in Node >= 16 globally and in all browsers.\n  if (typeof performance !== \"undefined\" && typeof performance.now === \"function\") {\n    return performance.now();\n  }\n  return Date.now();\n}\n\nfunction elapsedMs(startMonotonic: number): number {\n  return Math.max(0, Math.round(nowMonotonic() - startMonotonic));\n}\n\nfunction finalizeSpan(\n  span: SpanData,\n  startMono: number,\n  status: SpanStatus = \"completed\",\n  errorMessage?: string,\n  thrown?: unknown,\n): void {\n  span.ended_at = nowIso();\n  span.duration_ms = elapsedMs(startMono);\n  span.status = status;\n  if (errorMessage !== undefined) {\n    span.error_message = errorMessage;\n  }\n  const exceptionType = errToTypeName(thrown);\n  if (exceptionType !== undefined) {\n    span.exception_type = exceptionType;\n  }\n}\n\n/**\n * Class name of a thrown Error, preferring a custom subclass name over the\n * generic `Error` that `.name` reports for most subclasses. Non-Error throws\n * (strings, objects) have no type, so they group on message alone.\n */\nexport function errToTypeName(e: unknown): string | undefined {\n  if (!(e instanceof Error)) return undefined;\n  const ctor = e.constructor?.name;\n  if (e.name && e.name !== \"Error\") return e.name;\n  return ctor && ctor !== \"Object\" ? ctor : \"Error\";\n}\n\n/**\n * Field allowlist for span-level redaction. Identifiers, timestamps,\n * counts, and types pass through untouched; user-supplied strings and\n * JSON blobs route through `redactValue`.\n */\nconst SPAN_REDACTABLE_FIELDS: ReadonlySet<string> = new Set([\n  \"name\",\n  \"input_data\",\n  \"output_data\",\n  \"error_message\",\n  \"metadata\",\n]);\n\nconst TRACE_REDACTABLE_FIELDS: ReadonlySet<string> = new Set([\"agent\", \"outcome\", \"error_message\"]);\n\nfunction redactSpanDict(\n  raw: Record<string, unknown>,\n  config: RedactionConfig,\n): Record<string, unknown> {\n  const out: Record<string, unknown> = {};\n  // MHQ-947: guarded PER FIELD. Span metadata is USER data, and reading or\n  // redacting it runs arbitrary customer code — a getter, a `toString`. That\n  // threw straight through `redactValue` -> `buildTraceDict` -> `endTrace` and\n  // terminated the host process: instrumentation taking down the application\n  // it observes, which is the one thing the non-invasive guarantee exists to\n  // prevent. Confirmed reproducible on 0.5.0-rc.5.\n  //\n  // A bad field costs that field, not the trace. Losing one metadata key beats\n  // losing the span, and losing the span beats crashing the customer.\n  //\n  // Note this no longer early-returns when redaction is disabled: the read\n  // itself can throw, so the guard has to run either way.\n  for (const k of Object.keys(raw)) {\n    try {\n      const v = raw[k];\n      out[k] = config.enabled && SPAN_REDACTABLE_FIELDS.has(k) ? redactValue(v, config) : v;\n    } catch (err) {\n      report(err, { fingerprint: \"morse.tracing.span_field_unreadable\" });\n      out[k] = \"<unserializable>\";\n    }\n  }\n  return out;\n}\n\nfunction redactTraceDict(\n  raw: Record<string, unknown>,\n  config: RedactionConfig,\n): Record<string, unknown> {\n  if (!config.enabled) return raw;\n  const out: Record<string, unknown> = {};\n  for (const [k, v] of Object.entries(raw)) {\n    if (TRACE_REDACTABLE_FIELDS.has(k)) {\n      out[k] = redactValue(v, config);\n    } else {\n      out[k] = v;\n    }\n  }\n  return out;\n}\n\nfunction buildTraceDict(\n  state: TraceState,\n  rootSpan: SpanData,\n): {\n  trace: Record<string, unknown>;\n  spans: Record<string, unknown>[];\n} {\n  const trace: Record<string, unknown> = {\n    trace_id: state.traceId,\n    // Wire-format field is `agent` (matches backend TracePayloadIn schema +\n    // sibling Python SDK). Was `agent_name` historically — the mismatch\n    // caused dev ingest to reject every TS-emitted trace with a 422\n    // until Phase 13 Option A dogfood surfaced it.\n    agent: state.agentName,\n    status: rootSpan.status,\n    started_at: rootSpan.started_at,\n  };\n  if (rootSpan.ended_at) trace.ended_at = rootSpan.ended_at;\n  if (rootSpan.duration_ms !== undefined) trace.duration_ms = rootSpan.duration_ms;\n  if (rootSpan.error_message) trace.error_message = rootSpan.error_message;\n  if (state.success !== undefined) trace.success = state.success;\n  if (state.outcome !== undefined) trace.outcome = state.outcome;\n  // MHQ-748/MHQ-751: multi-turn + cost-attribution dimensions. Field names\n  // (`thread_id`/`user_id`/`user_metadata`) match the backend's\n  // `TracePayloadIn` schema exactly (apps/api/apps/traces/schemas.py) and\n  // the sibling Python SDK's `_build_trace_dict` — do not camelCase these.\n  if (state.threadId !== undefined) trace.thread_id = state.threadId;\n  if (state.userId !== undefined) trace.user_id = state.userId;\n  if (state.userMetadata && Object.keys(state.userMetadata).length > 0) {\n    trace.user_metadata = state.userMetadata;\n  }\n  // ATTR-01/04: omitted entirely when empty, so an old server and a trace that\n  // declared nothing see the exact payload they always did.\n  if (state.recordKeys && Object.keys(state.recordKeys).length > 0) {\n    trace.record_keys = { ...state.recordKeys };\n  }\n  if (state.agentVersion) trace.agent_version = state.agentVersion;\n  if (state.prompts && state.prompts.length > 0) {\n    trace.prompts = state.prompts.map((p) => ({ ...p }));\n  }\n  // AID-01/AID-04: trace metadata keys `identity_source` and `morse_app` (Wave 1 contract).\n  if (state.identitySource !== undefined || state.appName !== undefined) {\n    trace.metadata = {\n      ...(state.identitySource !== undefined ? { identity_source: state.identitySource } : {}),\n      ...(state.appName !== undefined ? { morse_app: state.appName } : {}),\n    };\n  }\n\n  const allSpans = [rootSpan, ...state.completedSpans];\n  const spans = allSpans.map((s) => {\n    const out: Record<string, unknown> = {};\n    for (const [k, v] of Object.entries(s)) {\n      if (v !== undefined && v !== null) {\n        out[k] = v;\n      }\n    }\n    return out;\n  });\n\n  const config = redactionConfig;\n  const out = {\n    trace: redactTraceDict(trace, config),\n    spans: spans.map((s) => redactSpanDict(s, config)),\n  };\n  // Per-trace `sourceOverride` wins over the process-level `_source` —\n  // mirrors Python's `_build_trace_dict`'s `effective_source = state.source_override or _source`.\n  const effectiveSource = state.sourceOverride || _source;\n  // MHQ-859: `morse_ai.framework` rides the same resource envelope as\n  // `morse_ai.source` — both are per-trace values a process-wide OTel\n  // Resource cannot express, and `otel-provider.ts` already lifts both onto\n  // the root span as `morse.source` / `morse.framework`. Python has stamped the\n  // framework since AGE-240; TS never did, so every TS trace landed with\n  // `framework` NULL.\n  const resource: Record<string, unknown> = {};\n  if (effectiveSource) {\n    // Lift the source label onto the trace envelope so the backend can\n    // route dogfood-emitter traces to the internal dashboards. Mirrors\n    // Python's `resource[\"morse_ai.source\"]`.\n    resource[\"morse_ai.source\"] = effectiveSource;\n  }\n  if (state.framework) {\n    resource[\"morse_ai.framework\"] = state.framework;\n  }\n  // ATTR-06: process-level deployment identity, detected once at init().\n  const gitRevision = getGitRevision();\n  if (gitRevision) resource[\"git.revision\"] = gitRevision;\n  const serviceVersion = getServiceVersion();\n  if (serviceVersion) resource[\"service.version\"] = serviceVersion;\n  if (Object.keys(resource).length > 0) {\n    (out as Record<string, unknown>).resource = resource;\n  }\n  return out;\n}\n\n/**\n * `run()` / `span()` / `runTraced()`'s emission path: real OTel spans over\n * OTLP (MHQ-482, mirrors Python's `tracing.py::_finish_trace`, MHQ-668).\n * Always updates `lastTrace` (test/debug introspection — same bookkeeping\n * regardless of which pipeline actually ships the trace), then hands the\n * dict to `otel-provider.ts::emitTraceEntry()` — unless\n * `MORSE_LEGACY_TRANSPORT=1`, in which case it falls back to the old JSON\n * queue path instead (rollback escape hatch; never both, to avoid\n * double-ingesting the same trace).\n *\n * Exported (MHQ-746) so `index.ts::record()` can submit a synthetic\n * single-span trace through this same pipeline instead of the defunct\n * `runs`-shaped `state.transport.track()` path — mirrors the Python SDK's\n * `client.py::record()`, which builds a trace + span dict and calls this\n * module's `_send_trace()` directly rather than going through `track()`.\n */\nexport function sendTrace(entry: {\n  trace: Record<string, unknown>;\n  spans: Record<string, unknown>[];\n}): void {\n  lastTrace = entry;\n\n  // `!isOtelAvailable()` is the browser build (see `otel-provider.browser.ts`):\n  // the OTLP pipeline is Node-only, so browser traces take the same JSON path\n  // the legacy escape hatch uses rather than being dropped. Still never both.\n  if (isLegacyTransport() || !isOtelAvailable()) {\n    if (transport !== null) {\n      try {\n        transport.track({ traces: [entry] });\n      } catch {\n        // never propagate\n      }\n    }\n    return;\n  }\n\n  try {\n    emitTraceEntry(entry);\n  } catch {\n    // never propagate — mirrors the rest of the SDK's non-invasive guarantee\n  }\n}\n\n/**\n * Run `fn` with the OTel-global active context set to a synthetic\n * `SpanContext` for `spanIdHex` under `traceId` (MHQ-482 db-capture\n * correlation — see the module docstring). No-op passthrough if the OTel\n * engine isn't configured (browser, uninitialized SDK, or\n * `otel-provider.ts` import failure) — never throws, mirrors the rest of\n * this module's non-invasive guarantee.\n */\nfunction withDbCaptureContext<T>(traceId: string, spanIdHex: string, fn: () => T): T {\n  try {\n    // OTel SpanId is 64-bit (16 hex chars); Morse's span_id is 128-bit\n    // (32 hex chars) — same truncation convention `spans.ts::generateSpanId`\n    // documents for W3C traceparent interop.\n    return withOtelSpanContext(traceId, spanIdHex.slice(0, 16), fn);\n  } catch {\n    return fn();\n  }\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Public-ish run() and span() — wrapped at the module surface              */\n/* ------------------------------------------------------------------------ */\n\nexport interface RunOptions {\n  /**\n   * Explicit agent name from customer code. Optional (MHQ-1100): when\n   * omitted, the name resolves from the framework-native name, the env\n   * service name, the entry script, then `\"default-agent\"` — see\n   * `_internal/agent-name.ts`.\n   */\n  agentName?: string;\n  /**\n   * Alias of `agentName` (MHQ-1415): `run({ name })` is what the runbooks and the\n   * Python-style call shape suggest, and it used to be silently ignored.\n   */\n  name?: string;\n  /** Framework-native name an integration knows (graph/agent name). Ranks below `agentName`. */\n  frameworkAgentName?: string;\n  /** W3C traceparent value to continue an upstream trace, when available. */\n  incomingTraceparent?: string | null;\n  /**\n   * Application-layer conversation/thread identifier (MHQ-748). Groups all\n   * traces from the same multi-turn session so the dashboard can show the\n   * full conversation history for a thread. Mirrors Python's\n   * `run(thread_id=...)`.\n   */\n  threadId?: string;\n  /**\n   * Application-layer end-user identifier (e.g. a user primary key or\n   * email) (MHQ-748). Enables per-user filtering in the dashboard. Mirrors\n   * Python's `run(user_id=...)`.\n   */\n  userId?: string;\n  /**\n   * Cost-attribution dimensions attached to this trace at creation time\n   * (e.g. `customer_id`, `feature`, `env`, `team`) (MHQ-748). Values are\n   * coerced to strings. Can also be set/merged mid-trace via\n   * `setContext()` (MHQ-751). Mirrors Python's `run(user_metadata=...)`.\n   */\n  userMetadata?: Record<string, unknown>;\n  /**\n   * INTERNAL USE ONLY — per-trace override for the resource-level\n   * `morse_ai.source` tag. Wins over the process-level source set at\n   * `init({ source })`. Accepted values: `\"copilot\" | \"investigator\" |\n   * \"refagent\" | \"ui-bot\"`; anything else is silently ignored and the\n   * process-level source is used. Customer code should leave this unset.\n   * Mirrors Python's `run(source=...)`.\n   */\n  source?: string;\n  /**\n   * Business-record keys to link this trace to, e.g. `{ zendesk_ticket: \"4812\" }`\n   * (ATTR-01). Key types are lower-cased and must match\n   * `^[a-z0-9][a-z0-9_.-]{0,63}$`; values are strings or numbers of 1-256\n   * characters. Invalid pairs are dropped, never thrown; at most 20 per trace.\n   * Use opaque ids, not personal data. More can be added mid-trace with\n   * `linkRecord()`. Mirrors Python's `run(record_keys=...)`.\n   */\n  recordKeys?: Record<string, string | number>;\n  /**\n   * Version of the agent this run belongs to (ATTR-04), e.g. a release tag.\n   * Overrides the `init({ agentVersion })` default for this run. Truncated to\n   * 128 characters. Mirrors Python's `run(agent_version=...)`.\n   */\n  agentVersion?: string;\n}\n\nexport interface RunHandle {\n  setOutcome(success: boolean, outcome?: string): void;\n  setError(message: string): void;\n  /** Internal — set by `runSync`/`runAsync`. */\n  _state: TraceState;\n}\n\nfunction buildRunHandle(state: TraceState): RunHandle {\n  return {\n    setOutcome(success, outcome) {\n      state.success = success;\n      if (outcome !== undefined) state.outcome = outcome;\n    },\n    setError(message) {\n      state.errorMessage = message;\n      state.success = false;\n    },\n    _state: state,\n  };\n}\n\nfunction startTrace(opts: RunOptions): {\n  state: TraceState;\n  rootSpan: SpanData;\n  startMono: number;\n} {\n  let traceId: string | undefined;\n  if (opts.incomingTraceparent) {\n    // Static import (was a lazy `require()`, which does not exist in the ESM\n    // build, so `run({ incomingTraceparent })` threw there). The import cycle\n    // with propagation.ts is harmless: both sides only call each other at run\n    // time, never while the modules are evaluating.\n    const ctx = extractTraceparent(opts.incomingTraceparent);\n    if (ctx) {\n      traceId = ctx.traceId;\n    }\n  }\n  if (!traceId) traceId = generateTraceId();\n\n  const identity = resolveAgentIdentity({\n    explicit: opts.agentName ?? opts.name,\n    framework: opts.frameworkAgentName,\n  });\n  const agentName = identity.name;\n  const state: TraceState = {\n    traceId,\n    agentName,\n    spanStack: [],\n    completedSpans: [],\n  };\n  if (!isLegacyIdentity()) state.identitySource = identity.source;\n  if (opts.threadId !== undefined) state.threadId = opts.threadId;\n  if (opts.userId !== undefined) state.userId = opts.userId;\n  if (opts.userMetadata) {\n    state.userMetadata = coerceDims(opts.userMetadata, \"morse.tracing.run.user_metadata\");\n  }\n  // Validate against the same whitelist as `init({ source })` — an invalid\n  // value falls through to whatever the process has set (never throws).\n  if (isValidInternalSource(opts.source)) {\n    state.sourceOverride = opts.source;\n  }\n  // ATTR-01/04. Both are normalised without ever throwing, so a bad value costs\n  // that value, not the callback the trace wraps.\n  try {\n    if (opts.recordKeys !== undefined) {\n      const keys: Record<string, string> = {};\n      mergeRecordKeys(keys, safeEntries(opts.recordKeys));\n      if (Object.keys(keys).length > 0) state.recordKeys = keys;\n    }\n    const agentVersion = normalizeAgentVersion(opts.agentVersion) ?? _agentVersionDefault;\n    if (agentVersion !== undefined) state.agentVersion = agentVersion;\n  } catch (err) {\n    report(err, { fingerprint: \"morse.tracing.run.attribution\" });\n  }\n  const rootSpan: SpanData = {\n    trace_id: traceId,\n    span_id: generateSpanId(),\n    type: \"agent\" as SpanType,\n    name: agentName,\n    started_at: nowIso(),\n    status: \"completed\",\n  };\n  state.spanStack.push(rootSpan);\n  return { state, rootSpan, startMono: nowMonotonic() };\n}\n\n/**\n * Longest a trace is held open by `holdTraceOpen` after its `run()` callback\n * returned. A stream nobody reads never ends; past this the trace is sent with\n * what it has.\n */\nexport const TRACE_HOLD_MAX_MS = 10 * 60 * 1000;\n\n/**\n * MHQ-1407: keep the active trace open until the returned release is called.\n *\n * A streamed route returns the response before the stream is read, so\n * `run(() => { const r = streamText(...); return r.toUIMessageStreamResponse(); })`\n * used to close the trace in milliseconds and drop the llm span, its usage and\n * every span the stream's tools made. Adapters take a hold when a stream starts\n * and release it when the stream ends; `run()` still returns as soon as its\n * callback does, and only the trace's finalisation waits (bounded by\n * `TRACE_HOLD_MAX_MS`). Releasing is synchronous, so a stream fully read inside\n * the callback ends the trace exactly as before. Returns null when no trace is\n * active. The release is idempotent and never throws.\n */\nexport function holdTraceOpen(): (() => void) | null {\n  try {\n    const state = getActiveTraceState();\n    if (state === null) return null;\n    state.openHolds = (state.openHolds ?? 0) + 1;\n    let released = false;\n    return () => {\n      if (released) return;\n      released = true;\n      try {\n        state.openHolds = Math.max(0, (state.openHolds ?? 1) - 1);\n        if (state.openHolds === 0 && state.onHoldsReleased) {\n          const resume = state.onHoldsReleased;\n          state.onHoldsReleased = undefined;\n          resume();\n        }\n      } catch (err) {\n        report(err, { fingerprint: \"morse.tracing.release_hold\" });\n      }\n    };\n  } catch {\n    return null;\n  }\n}\n\nfunction endTrace(\n  state: TraceState,\n  rootSpan: SpanData,\n  startMono: number,\n  threwError: unknown,\n): void {\n  if (!state.openHolds) {\n    endTraceNow(state, rootSpan, startMono, threwError);\n    return;\n  }\n  let ended = false;\n  const finish = (): void => {\n    if (ended) return;\n    ended = true;\n    clearTimeout(timer);\n    state.onHoldsReleased = undefined;\n    try {\n      endTraceNow(state, rootSpan, startMono, threwError);\n    } catch (err) {\n      report(err, { fingerprint: \"morse.tracing.end_trace_deferred\" });\n    }\n  };\n  const timer = setTimeout(finish, TRACE_HOLD_MAX_MS);\n  (timer as { unref?: () => void }).unref?.();\n  state.onHoldsReleased = finish;\n}\n\nfunction endTraceNow(\n  state: TraceState,\n  rootSpan: SpanData,\n  startMono: number,\n  threwError: unknown,\n): void {\n  // Remove the root itself, not whatever is on top: a span that is still open\n  // (a stream nobody finished reading) must not leave the root on the stack.\n  const rootIdx = state.spanStack.lastIndexOf(rootSpan);\n  if (rootIdx >= 0) state.spanStack.splice(rootIdx, 1);\n  else state.spanStack.pop();\n  if (threwError !== undefined) {\n    const msg = state.errorMessage ?? errToString(threwError);\n    finalizeSpan(rootSpan, startMono, \"failed\", msg, threwError);\n    // A crash is a fact, not a guess — failure metrics filter on success=false.\n    if (state.success === undefined) state.success = false;\n  } else if (state.errorMessage !== undefined) {\n    finalizeSpan(rootSpan, startMono, \"failed\", state.errorMessage);\n    // setError() already set state.success = false.\n  } else {\n    finalizeSpan(rootSpan, startMono);\n  }\n  applyFrameworkRoot(state, rootSpan);\n  // MHQ-1157: a clean exit is not proof of success, so it sends no `success`\n  // (the server would store it as provenance 'developer').\n  const dict = buildTraceDict(state, rootSpan);\n  sendTrace(dict);\n}\n\n/**\n * AID-01/AID-04: fold the root an adapter discovered into the trace identity.\n *\n * A framework-native root outranks the service name and the entry script, so it\n * replaces them. `run(\"name\")` outranks it as a label only: the name moves to\n * `appName` (`morse.app`) and the discovered root stays the agent, so the topology\n * keeps one root instead of forking a second agent above it. No-op in legacy mode.\n */\nfunction applyFrameworkRoot(state: TraceState, rootSpan: SpanData): void {\n  const root = state.frameworkRoot;\n  if (!root || state.identitySource === undefined || root === state.agentName) return;\n  if (state.identitySource === \"run\") state.appName = state.agentName;\n  state.agentName = root;\n  state.identitySource = \"framework\";\n  rootSpan.name = root;\n}\n\n/**\n * Record the root agent a framework adapter found (graph name, starting agent) on the\n * active trace. First call wins; framework plumbing names (`LangGraph`, `chain`) never\n * count. Never throws.\n */\nexport function adoptFrameworkRoot(name: unknown): void {\n  const state = getActiveTraceState();\n  if (state === null || state.frameworkRoot !== undefined) return;\n  if (typeof name !== \"string\" || name.trim() === \"\" || isFrameworkNodeName(name)) return;\n  state.frameworkRoot = name.trim();\n}\n\n/** The root an adapter adopted on the active trace, if any. */\nexport function getFrameworkRoot(): string | undefined {\n  return getActiveTraceState()?.frameworkRoot;\n}\n\nlet pendingParentSpanId: string | undefined;\n\n/**\n * AID-05: run `fn` with `startSpan` parenting its span to `parentSpanId` instead of the\n * top of the span stack. Callback frameworks (LangGraph, LangChain) run branches\n * concurrently, so \"top of stack\" is whichever sibling started last. A span parented\n * this way is not pushed on the stack either, so siblings cannot adopt it.\n * Synchronous only: the override is consumed by the first span `fn` starts.\n */\nexport function withParentSpan<T>(parentSpanId: string | undefined, fn: () => T): T {\n  if (parentSpanId === undefined) return fn();\n  pendingParentSpanId = parentSpanId;\n  try {\n    return fn();\n  } finally {\n    pendingParentSpanId = undefined;\n  }\n}\n\nfunction errToString(e: unknown): string {\n  if (e instanceof Error) return e.message;\n  return String(e);\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Sync + async run() wrappers                                              */\n/* ------------------------------------------------------------------------ */\n\n/** True for anything await-able, without assuming a real Promise. */\nfunction isThenable(v: unknown): v is PromiseLike<unknown> {\n  return (\n    v !== null &&\n    (typeof v === \"object\" || typeof v === \"function\") &&\n    typeof (v as { then?: unknown }).then === \"function\"\n  );\n}\n\nexport function runSync<T>(opts: RunOptions, fn: (handle: RunHandle) => T): T | undefined {\n  const { state, rootSpan, startMono } = startTrace(opts);\n  const handle = buildRunHandle(state);\n  const store = tryLoadAls();\n\n  const runFn = (): T => withDbCaptureContext(state.traceId, rootSpan.span_id, () => fn(handle));\n\n  // MHQ-924 (defect B): if `fn` is async, closing the trace here would send it\n  // before the body has run, so `state.completedSpans` is still empty and\n  // EVERY child span is lost. The root span still lands, so the trace looks\n  // healthy — it is just silently truncated. `run()` accepts an async callback\n  // (its signature cannot refuse one), so \"use runTraced() instead\" was a\n  // documented footgun rather than a safeguard: three adapters and the pino\n  // integration each landed exactly one span in live testing because of it.\n  //\n  // Settling first keeps the sync path byte-identical — a non-thenable result\n  // still ends the trace inline — while making the async path correct.\n  const settle = (result: T, onDone: () => void): T => {\n    if (!isThenable(result)) {\n      endTrace(state, rootSpan, startMono, undefined);\n      onDone();\n      return result;\n    }\n    return (result as PromiseLike<unknown>).then(\n      (value) => {\n        endTrace(state, rootSpan, startMono, undefined);\n        onDone();\n        return value;\n      },\n      (err) => {\n        endTrace(state, rootSpan, startMono, err);\n        onDone();\n        throw err;\n      },\n    ) as unknown as T;\n  };\n\n  if (store) {\n    try {\n      return settle(store.run(state, runFn), () => {});\n    } catch (err) {\n      endTrace(state, rootSpan, startMono, err);\n      throw err;\n    }\n  } else {\n    // Browser fallback: the reset must not fire until an async body has\n    // settled, or the module-global is cleared out from under it.\n    const reset = setActiveTraceState(state);\n    try {\n      return settle(runFn(), reset);\n    } catch (err) {\n      endTrace(state, rootSpan, startMono, err);\n      reset();\n      throw err;\n    }\n  }\n}\n\nexport async function runAsync<T>(\n  opts: RunOptions,\n  fn: (handle: RunHandle) => Promise<T>,\n): Promise<T | undefined> {\n  const { state, rootSpan, startMono } = startTrace(opts);\n  const handle = buildRunHandle(state);\n  const store = tryLoadAls();\n\n  const runFn = (): Promise<T> =>\n    withDbCaptureContext(state.traceId, rootSpan.span_id, () => fn(handle));\n\n  if (store) {\n    try {\n      const result = await store.run(state, runFn);\n      endTrace(state, rootSpan, startMono, undefined);\n      return result;\n    } catch (err) {\n      endTrace(state, rootSpan, startMono, err);\n      throw err;\n    }\n  } else {\n    const reset = setActiveTraceState(state);\n    try {\n      const result = await runFn();\n      endTrace(state, rootSpan, startMono, undefined);\n      return result;\n    } catch (err) {\n      endTrace(state, rootSpan, startMono, err);\n      throw err;\n    } finally {\n      reset();\n    }\n  }\n}\n\n/**\n * Attach cost-attribution dimensions to the active trace (MHQ-751).\n *\n * Call once per request (e.g. in Express/Next.js middleware) to propagate\n * `customer_id`, `feature`, `env`, `team`, etc. to all child spans\n * automatically. Mirrors Python's `morse_ai.set_context(**dims)`.\n *\n *     morse.setContext({ customerId: request.customerId, feature: \"doc-qa\" });\n *\n * Values are coerced to strings via `String(v)` — mirrors Python's `str()`\n * coercion. Merges into any dimensions already set (e.g. via\n * `run({ userMetadata })`) rather than replacing them. No-op outside an\n * active trace — never throws.\n */\nexport function setContext(dims: Record<string, unknown>): void {\n  const state = getActiveTraceState();\n  if (state === null) return;\n  if (state.userMetadata === undefined) state.userMetadata = {};\n  Object.assign(state.userMetadata, coerceDims(dims, \"morse.tracing.set_context\"));\n}\n\n/**\n * Declare the outcome of the active trace. Same effect as\n * `handle.setOutcome()`, without needing the handle. No-op outside a trace.\n */\nexport function setOutcome(success: boolean, outcome?: string): void {\n  const state = getActiveTraceState();\n  if (state === null) return;\n  state.success = success;\n  if (outcome !== undefined) state.outcome = outcome;\n}\n\n/**\n * Link the active trace to a business record (ATTR-01): a ticket, order, PR,\n * anything an outcome can later be reported against by id.\n *\n *     morse.linkRecord(\"zendesk_ticket\", ticket.id);\n *\n * `keyType` is lower-cased and must match `^[a-z0-9][a-z0-9_.-]{0,63}$`;\n * `keyValue` is a string or number of 1-256 characters after trimming. Calls\n * accumulate (the same type again replaces its value); a trace keeps at most 20.\n * Invalid input is dropped with a one-time warning, never thrown. Use opaque\n * ids, not personal data such as an email address. No-op outside a trace.\n */\nexport function linkRecord(keyType: string, keyValue: string | number): void {\n  try {\n    const state = getActiveTraceState();\n    if (state === null) return;\n    if (state.recordKeys === undefined) state.recordKeys = {};\n    mergeRecordKeys(state.recordKeys, [[keyType, keyValue]]);\n  } catch (err) {\n    report(err, { fingerprint: \"morse.tracing.link_record\" });\n  }\n}\n\n/**\n * Declare that the active trace used a prompt (ATTR-04). Inside a\n * `morse.span()` block the reference is pinned to that span; at the top level of\n * a run it belongs to the trace. `version` is optional — teams that keep prompts\n * in code can omit it and rely on `agentVersion` or the git revision. Anything\n * that is not an integer ≥1 is dropped and the name kept. A trace keeps at most\n * 20 references. Never throws; no-op outside a trace.\n *\n * Over OTLP a span carries one reference, so when several are set on the same\n * span the last is exported (the legacy JSON path keeps all).\n */\nexport function setPrompt(name: string, version?: number | string): void {\n  try {\n    const state = getActiveTraceState();\n    if (state === null) return;\n    const top = state.spanStack[state.spanStack.length - 1];\n    // spanStack[0] is the root span: a call made directly in run() is trace-level.\n    const spanId = state.spanStack.length > 1 ? top?.span_id : undefined;\n    const ref = normalizePromptRef(name, version, spanId);\n    if (ref === null) {\n      warnPromptRejected();\n      return;\n    }\n    if (state.prompts === undefined) state.prompts = [];\n    addPromptRef(state.prompts, ref);\n  } catch (err) {\n    report(err, { fingerprint: \"morse.tracing.set_prompt\" });\n  }\n}\n\n/**\n * UP-09: link the run that is active NOW to a prompt `getPrompt()` just resolved.\n * Same path as `setPrompt` (span-pinned inside `span()`, trace-level otherwise) but the\n * ref is tagged `link_source: \"fetch\"`. Returns true when a run was active and the\n * reference was recorded (or upgraded from a `setPrompt` ref). Never throws.\n *\n * @internal\n */\nexport function linkFetchedPrompt(name: string, version: number): boolean {\n  try {\n    const state = getActiveTraceState();\n    if (state === null) return false;\n    const top = state.spanStack[state.spanStack.length - 1];\n    const spanId = state.spanStack.length > 1 ? top?.span_id : undefined;\n    const ref = normalizePromptRef(name, version, spanId);\n    if (ref === null) return false;\n    ref.link_source = \"fetch\";\n    if (state.prompts === undefined) state.prompts = [];\n    addPromptRef(state.prompts, ref);\n    return true;\n  } catch (err) {\n    report(err, { fingerprint: \"morse.tracing.link_fetched_prompt\" });\n    return false;\n  }\n}\n\n/**\n * Set `threadId` on the active trace when nothing has claimed it yet.\n *\n * MHQ-1029 (the TypeScript half of MHQ-1022): no adapter populated `threadId`,\n * so every auto-opened trace landed with `thread_id: null` and a five-turn\n * conversation showed up as five unrelated traces. Each framework keeps\n * conversation identity somewhere different — an openai-agents `session` /\n * run context, a LangGraph `configurable.thread_id`, the Agent SDK's session id\n * — so extraction stays per-adapter and this is the one place that writes it,\n * which is where precedence gets decided once.\n *\n * An explicit `run({ threadId })` from app code always wins; adapters only fill\n * the gap. Never throws: `String()` runs customer `toString`/`valueOf` code.\n */\nexport function adoptThreadId(value: unknown): void {\n  const state = getActiveTraceState();\n  if (state === null || state.threadId !== undefined || value === null || value === undefined) {\n    return;\n  }\n  try {\n    const s = String(value);\n    if (s !== \"\") state.threadId = s;\n  } catch (err) {\n    report(err, { fingerprint: \"morse.tracing.adopt_thread_id\" });\n  }\n}\n\n/**\n * `String()` every value, dropping any that throws. Never throws. (MHQ-947)\n *\n * Cost-attribution dimensions are USER values, and the SDK is the one choosing\n * to stringify them — so a bare `String(v)` runs arbitrary customer code\n * (`toString`, `valueOf`) on the telemetry path. An exception there propagated\n * into the caller: `setContext()` threw into host code, and\n * `run({ userMetadata })` threw at trace START, taking the whole callback with\n * it. Both confirmed reproducible on 0.5.0-rc.5.\n *\n * The trigger is not exotic — an ORM object whose `toString` lazily hits a\n * dead connection is a realistic, non-malicious source.\n *\n * Guarded PER KEY rather than per call: wrapping the entry point would discard\n * every dimension because one was bad, and losing a customer's `customerId`\n * because their `feature` object misbehaved is the worse answer. Mirrors the\n * Python SDK's `coerce_dims`.\n */\nexport function coerceDims(\n  dims: Record<string, unknown>,\n  fingerprint: string,\n): Record<string, string> {\n  const out: Record<string, string> = {};\n  for (const k of Object.keys(dims)) {\n    try {\n      out[k] = String(dims[k]);\n    } catch (err) {\n      report(err, { fingerprint });\n    }\n  }\n  return out;\n}\n\n/* ------------------------------------------------------------------------ */\n/*  span() child-span helpers                                                */\n/* ------------------------------------------------------------------------ */\n\nexport interface SpanOptions {\n  name: string;\n  type?: SpanType;\n  metadata?: Record<string, unknown>;\n}\n\nexport interface SpanHandle {\n  /** Mutate the underlying span data (e.g., set tokens / cost). */\n  span: SpanData;\n  end(error?: unknown): void;\n}\n\n// MHQ-806. Latches the \"no active trace\" warning to once per process — see\n// startSpan for why it warns at all, and why not on every call.\nlet spanOutsideTraceWarningEmitted = false;\n\n/** Test hook — clears the once-per-process latch. */\nexport function _resetSpanOutsideTraceWarning(): void {\n  spanOutsideTraceWarningEmitted = false;\n}\n\n/** Start a child span under the active trace, returning a handle to close it. */\nexport function startSpan(opts: SpanOptions): SpanHandle | null {\n  const state = getActiveTraceState();\n  if (state === null) {\n    // No active trace — returns null rather than throwing, because library\n    // code may be instrumented but reached from an untraced path.\n    //\n    // The null is visible in the type here, unlike Python's context-manager\n    // form, but callers routinely ignore it (`span({...}, () => { ... })`\n    // never touches the handle), so the outcome is the same: no error, no\n    // spans, nothing distinguishing it from working instrumentation.\n    //\n    // Warned once per process, matching the existing not-initialized warning\n    // in index.ts — this sits inside request and agent loops, so per-call\n    // logging would bury the signal. This is the single choke point for\n    // startSpan / spanSync / spanAsync.\n    if (!spanOutsideTraceWarningEmitted) {\n      spanOutsideTraceWarningEmitted = true;\n      console.warn(\n        `morse: span(${JSON.stringify(opts.name)}) was called with no active trace, so nothing ` +\n          `was recorded. Wrap the work in morse.run('<agent-name>', () => { ... }) — run() opens ` +\n          `the trace that span() attaches to. This warning prints once per process.`,\n      );\n    }\n    return null;\n  }\n  const explicitParent = pendingParentSpanId;\n  pendingParentSpanId = undefined;\n  const parent = state.spanStack[state.spanStack.length - 1];\n  const span: SpanData = {\n    trace_id: state.traceId,\n    span_id: generateSpanId(),\n    type: opts.type ?? \"custom\",\n    name: opts.name,\n    parent_span_id: explicitParent ?? parent?.span_id,\n    started_at: nowIso(),\n    status: \"completed\",\n    metadata: opts.metadata,\n  };\n  const startMono = nowMonotonic();\n  if (explicitParent === undefined) state.spanStack.push(span);\n  return {\n    span,\n    end(error?: unknown): void {\n      // Pop only if still on top — defensive against out-of-order ends.\n      if (state.spanStack[state.spanStack.length - 1] === span) {\n        state.spanStack.pop();\n      }\n      if (error !== undefined) {\n        finalizeSpan(span, startMono, \"failed\", errToString(error), error);\n      } else {\n        finalizeSpan(span, startMono);\n      }\n      state.completedSpans.push(span);\n    },\n  };\n}\n\nexport interface DetachedSpan {\n  span: SpanData;\n  /** Close the span and record it on its trace. Safe to call from any async context. */\n  end(error?: unknown): void;\n}\n\n/**\n * Start a span under the active trace WITHOUT putting it on the span stack\n * (INFRACAP). Infra captures (an HTTP request, a queue publish) start in one\n * callback and finish in another, often after the caller has moved on; a\n * stacked span would adopt unrelated siblings as children and, if `end` never\n * ran, corrupt parentage for the rest of the trace. A detached span cannot do\n * either: its parent is whatever was on top when it started, and an `end` that\n * never comes simply means the span is never recorded.\n *\n * Returns null when no trace is active.\n */\nexport function startDetachedSpan(opts: SpanOptions): DetachedSpan | null {\n  const state = getActiveTraceState();\n  if (state === null) return null;\n  const parent = state.spanStack[state.spanStack.length - 1];\n  const span: SpanData = {\n    trace_id: state.traceId,\n    span_id: generateSpanId(),\n    type: opts.type ?? \"custom\",\n    name: opts.name,\n    parent_span_id: parent?.span_id,\n    started_at: nowIso(),\n    status: \"completed\",\n    metadata: opts.metadata,\n  };\n  const startMono = nowMonotonic();\n  let ended = false;\n  return {\n    span,\n    end(error?: unknown): void {\n      if (ended) return;\n      ended = true;\n      if (error !== undefined) {\n        finalizeSpan(span, startMono, \"failed\", errToString(error));\n      } else {\n        finalizeSpan(span, startMono);\n      }\n      state.completedSpans.push(span);\n    },\n  };\n}\n\n/** Sync callback wrapper for `startSpan` / `end`. No-op if no active trace. */\nexport function spanSync<T>(opts: SpanOptions, fn: (handle: SpanHandle | null) => T): T {\n  const handle = startSpan(opts);\n  try {\n    const result = handle\n      ? withDbCaptureContext(handle.span.trace_id, handle.span.span_id, () => fn(handle))\n      : fn(handle);\n    handle?.end();\n    return result;\n  } catch (err) {\n    handle?.end(err);\n    throw err;\n  }\n}\n\n/** Async callback wrapper for `startSpan` / `end`. No-op if no active trace. */\nexport async function spanAsync<T>(\n  opts: SpanOptions,\n  fn: (handle: SpanHandle | null) => Promise<T>,\n): Promise<T> {\n  const handle = startSpan(opts);\n  try {\n    const result = handle\n      ? await withDbCaptureContext(handle.span.trace_id, handle.span.span_id, () => fn(handle))\n      : await fn(handle);\n    handle?.end();\n    return result;\n  } catch (err) {\n    handle?.end(err);\n    throw err;\n  }\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Test helpers                                                             */\n/* ------------------------------------------------------------------------ */\n\nexport function _resetForTesting(): void {\n  browserState = null;\n  lastTrace = null;\n  _agentVersionDefault = undefined;\n  _captureEndUser = false;\n  redactionConfig = buildRedactionConfig();\n  // We do not reset `als` because re-loading it is expensive and the\n  // store is process-scoped anyway.\n}\n","/**\n * W3C Trace Context propagation — inject and extract `traceparent` headers.\n *\n * W3C traceparent format: `{version}-{trace_id}-{span_id}-{flags}`\n *   - version  : \"00\" (fixed)\n *   - trace_id : 32 lowercase hex chars (128-bit)\n *   - span_id  : 16 lowercase hex chars (64-bit)\n *   - flags    : \"01\" (sampled, always set for Morse)\n *\n * Example: `00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01`\n *\n * References:\n *   https://www.w3.org/TR/trace-context/\n */\n\nimport { getActiveTraceState } from \"./_internal/tracing\";\n\nexport const W3C_TRACEPARENT_HEADER = \"traceparent\";\nexport const W3C_VERSION = \"00\";\nexport const W3C_FLAGS_SAMPLED = \"01\";\n\nconst TRACE_ID_LEN = 32;\nconst SPAN_ID_LEN = 16;\n\n/**\n * Build a W3C traceparent value from explicit IDs.\n *\n * Useful when you have a trace_id / span_id from somewhere other than\n * the active context (e.g., re-emitting an upstream trace's IDs).\n */\nexport function formatTraceparent(traceId: string, spanId: string): string {\n  // We trust the caller; the \"build then validate\" assumption is documented.\n  const tid = traceId.slice(0, TRACE_ID_LEN).padStart(TRACE_ID_LEN, \"0\").toLowerCase();\n  const sid = spanId.slice(0, SPAN_ID_LEN).padStart(SPAN_ID_LEN, \"0\").toLowerCase();\n  return `${W3C_VERSION}-${tid}-${sid}-${W3C_FLAGS_SAMPLED}`;\n}\n\n/**\n * Inject the W3C `traceparent` header derived from the active trace\n * context into the supplied headers object. Mutates and returns the\n * object for convenience.\n *\n * No-op (returns headers unchanged) if no trace is active.\n *\n * Accepts both `Headers` (from fetch) and a plain dict.\n */\nexport function injectTraceparent<T extends HeadersLike>(headers: T): T {\n  const state = getActiveTraceState();\n  if (state === null) return headers;\n\n  const traceId = state.traceId;\n  const span =\n    state.spanStack.length > 0\n      ? state.spanStack[state.spanStack.length - 1]\n      : state.completedSpans[state.completedSpans.length - 1];\n  const rawSpanId = span?.span_id ?? traceId;\n  const spanId = rawSpanId.slice(0, SPAN_ID_LEN);\n\n  const value = formatTraceparent(traceId, spanId);\n  setHeader(headers, W3C_TRACEPARENT_HEADER, value);\n  return headers;\n}\n\nexport interface ExtractedContext {\n  traceId: string;\n  parentSpanId: string;\n}\n\n/**\n * Parse a W3C traceparent header and return `{ traceId, parentSpanId }`,\n * or `null` if the header is missing or malformed.\n *\n * Validation per W3C §3.2.3:\n *   - exactly 4 dash-separated fields\n *   - version === \"00\"\n *   - trace_id is 32 lowercase hex chars (and not all-zero)\n *   - span_id  is 16 lowercase hex chars (and not all-zero)\n */\nexport function extractTraceparent(\n  headers: HeadersLike | string | null | undefined,\n): ExtractedContext | null {\n  if (headers === null || headers === undefined) return null;\n  const raw = typeof headers === \"string\" ? headers : getHeader(headers, W3C_TRACEPARENT_HEADER);\n  if (!raw) return null;\n  const parts = raw.split(\"-\");\n  if (parts.length !== 4) return null;\n  const [version, traceId, spanId] = parts as [string, string, string, string];\n  if (version !== W3C_VERSION) return null;\n  if (traceId.length !== TRACE_ID_LEN) return null;\n  if (spanId.length !== SPAN_ID_LEN) return null;\n  if (!/^[0-9a-f]+$/.test(traceId)) return null;\n  if (!/^[0-9a-f]+$/.test(spanId)) return null;\n  if (/^0+$/.test(traceId)) return null;\n  if (/^0+$/.test(spanId)) return null;\n  return { traceId, parentSpanId: spanId };\n}\n\n/**\n * Convenience for Next.js API routes / generic HTTP servers: pull the\n * traceparent off an incoming Request (or a header bag).\n */\nexport function extractFromRequest(req: { headers: HeadersLike }): ExtractedContext | null {\n  return extractTraceparent(req.headers);\n}\n\n/**\n * Convenience for outbound `fetch`: build a fresh `Headers` object with\n * the traceparent injected, layered on top of the caller's input.\n */\nexport function injectIntoRequestInit(init?: RequestInit): RequestInit {\n  const merged = new Headers(init?.headers ?? {});\n  injectTraceparent(merged);\n  return { ...(init ?? {}), headers: merged };\n}\n\n/* ------------------------------------------------------------------------ */\n/*  Header polymorphism helpers                                              */\n/* ------------------------------------------------------------------------ */\n\nexport type HeadersLike =\n  | Headers\n  | Record<string, string | string[] | undefined>\n  | { get(name: string): string | null }\n  | { [key: string]: unknown };\n\nfunction getHeader(headers: HeadersLike, name: string): string | null {\n  if (headers instanceof Headers) {\n    return headers.get(name);\n  }\n  // Object that exposes get() — e.g., Node's IncomingMessage doesn't, but\n  // Next.js's headers() does.\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const maybeGet = (headers as any).get;\n  if (typeof maybeGet === \"function\") {\n    const v = maybeGet.call(headers, name);\n    if (typeof v === \"string\") return v;\n    if (v === null || v === undefined) return null;\n  }\n  // Plain dict: case-insensitive lookup.\n  const lower = name.toLowerCase();\n  for (const [k, v] of Object.entries(headers as Record<string, unknown>)) {\n    if (k.toLowerCase() === lower) {\n      if (typeof v === \"string\") return v;\n      if (Array.isArray(v) && v.length > 0 && typeof v[0] === \"string\") return v[0] as string;\n      return null;\n    }\n  }\n  return null;\n}\n\nfunction setHeader(headers: HeadersLike, name: string, value: string): void {\n  if (headers instanceof Headers) {\n    headers.set(name, value);\n    return;\n  }\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any\n  const maybeSet = (headers as any).set;\n  if (typeof maybeSet === \"function\") {\n    maybeSet.call(headers, name, value);\n    return;\n  }\n  (headers as Record<string, string>)[name] = value;\n}\n"]}