risicare 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +282 -63
  2. package/dist/frameworks/instructor.cjs +790 -5
  3. package/dist/frameworks/instructor.cjs.map +1 -1
  4. package/dist/frameworks/instructor.js +780 -5
  5. package/dist/frameworks/instructor.js.map +1 -1
  6. package/dist/frameworks/langchain.cjs +790 -5
  7. package/dist/frameworks/langchain.cjs.map +1 -1
  8. package/dist/frameworks/langchain.d.cts +3 -2
  9. package/dist/frameworks/langchain.d.ts +3 -2
  10. package/dist/frameworks/langchain.js +780 -5
  11. package/dist/frameworks/langchain.js.map +1 -1
  12. package/dist/frameworks/langgraph.cjs +790 -5
  13. package/dist/frameworks/langgraph.cjs.map +1 -1
  14. package/dist/frameworks/langgraph.js +780 -5
  15. package/dist/frameworks/langgraph.js.map +1 -1
  16. package/dist/frameworks/llamaindex.cjs +801 -5
  17. package/dist/frameworks/llamaindex.cjs.map +1 -1
  18. package/dist/frameworks/llamaindex.d.cts +3 -2
  19. package/dist/frameworks/llamaindex.d.ts +3 -2
  20. package/dist/frameworks/llamaindex.js +791 -5
  21. package/dist/frameworks/llamaindex.js.map +1 -1
  22. package/dist/index.cjs +1113 -176
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +234 -42
  25. package/dist/index.d.ts +234 -42
  26. package/dist/index.js +1113 -176
  27. package/dist/index.js.map +1 -1
  28. package/dist/providers/anthropic/index.cjs +1601 -36
  29. package/dist/providers/anthropic/index.cjs.map +1 -1
  30. package/dist/providers/anthropic/index.d.cts +3 -1
  31. package/dist/providers/anthropic/index.d.ts +3 -1
  32. package/dist/providers/anthropic/index.js +1591 -36
  33. package/dist/providers/anthropic/index.js.map +1 -1
  34. package/dist/providers/bedrock/index.cjs +850 -32
  35. package/dist/providers/bedrock/index.cjs.map +1 -1
  36. package/dist/providers/bedrock/index.js +840 -32
  37. package/dist/providers/bedrock/index.js.map +1 -1
  38. package/dist/providers/cerebras/index.cjs +1534 -30
  39. package/dist/providers/cerebras/index.cjs.map +1 -1
  40. package/dist/providers/cerebras/index.js +1524 -30
  41. package/dist/providers/cerebras/index.js.map +1 -1
  42. package/dist/providers/cohere/index.cjs +1557 -102
  43. package/dist/providers/cohere/index.cjs.map +1 -1
  44. package/dist/providers/cohere/index.js +1547 -102
  45. package/dist/providers/cohere/index.js.map +1 -1
  46. package/dist/providers/google/index.cjs +1562 -60
  47. package/dist/providers/google/index.cjs.map +1 -1
  48. package/dist/providers/google/index.js +1552 -60
  49. package/dist/providers/google/index.js.map +1 -1
  50. package/dist/providers/groq/index.cjs +1534 -30
  51. package/dist/providers/groq/index.cjs.map +1 -1
  52. package/dist/providers/groq/index.js +1524 -30
  53. package/dist/providers/groq/index.js.map +1 -1
  54. package/dist/providers/huggingface/index.cjs +1622 -25
  55. package/dist/providers/huggingface/index.cjs.map +1 -1
  56. package/dist/providers/huggingface/index.d.cts +0 -9
  57. package/dist/providers/huggingface/index.d.ts +0 -9
  58. package/dist/providers/huggingface/index.js +1612 -25
  59. package/dist/providers/huggingface/index.js.map +1 -1
  60. package/dist/providers/mistral/index.cjs +1535 -54
  61. package/dist/providers/mistral/index.cjs.map +1 -1
  62. package/dist/providers/mistral/index.js +1525 -54
  63. package/dist/providers/mistral/index.js.map +1 -1
  64. package/dist/providers/ollama/index.cjs +1559 -93
  65. package/dist/providers/ollama/index.cjs.map +1 -1
  66. package/dist/providers/ollama/index.js +1549 -93
  67. package/dist/providers/ollama/index.js.map +1 -1
  68. package/dist/providers/openai/index.cjs +1598 -61
  69. package/dist/providers/openai/index.cjs.map +1 -1
  70. package/dist/providers/openai/index.d.cts +6 -1
  71. package/dist/providers/openai/index.d.ts +6 -1
  72. package/dist/providers/openai/index.js +1588 -61
  73. package/dist/providers/openai/index.js.map +1 -1
  74. package/dist/providers/together/index.cjs +1534 -30
  75. package/dist/providers/together/index.cjs.map +1 -1
  76. package/dist/providers/together/index.js +1524 -30
  77. package/dist/providers/together/index.js.map +1 -1
  78. package/dist/providers/vercel-ai/index.cjs +1288 -17
  79. package/dist/providers/vercel-ai/index.cjs.map +1 -1
  80. package/dist/providers/vercel-ai/index.d.cts +0 -6
  81. package/dist/providers/vercel-ai/index.d.ts +0 -6
  82. package/dist/providers/vercel-ai/index.js +1278 -17
  83. package/dist/providers/vercel-ai/index.js.map +1 -1
  84. package/package.json +1 -1
package/dist/index.cjs CHANGED
@@ -134,6 +134,29 @@ function getDebug() {
134
134
  function setDebugFlag(enabled) {
135
135
  G[PREFIX + "debug"] = enabled;
136
136
  }
137
+ var PROVIDER_SPANS = /* @__PURE__ */ Symbol.for("risicare.providerSpans");
138
+ function usableProviderSpanState(s) {
139
+ if (!s || typeof s !== "object") return false;
140
+ const { version, open, registry } = s;
141
+ const o = open;
142
+ return version === 1 && !!o && typeof o.add === "function" && typeof o.delete === "function" && typeof o[Symbol.iterator] === "function" && (registry === void 0 || typeof registry.register === "function");
143
+ }
144
+ function getProviderSpanState() {
145
+ const found = G[PROVIDER_SPANS];
146
+ if (found === void 0) {
147
+ const s = {
148
+ version: 1,
149
+ open: /* @__PURE__ */ new Set(),
150
+ registry: typeof FinalizationRegistry === "function" ? new FinalizationRegistry((o) => o.release()) : void 0
151
+ };
152
+ G[PROVIDER_SPANS] = s;
153
+ return s;
154
+ }
155
+ if (!usableProviderSpanState(found)) {
156
+ throw new TypeError("the shared provider span state is not version 1");
157
+ }
158
+ return found;
159
+ }
137
160
 
138
161
  // src/utils/log.ts
139
162
  function setDebug(enabled) {
@@ -150,6 +173,73 @@ function warn(msg) {
150
173
  `);
151
174
  }
152
175
 
176
+ // src/utils/text.ts
177
+ function cutCodePoints(text, max) {
178
+ if (text.length <= max) return text;
179
+ let units = 0;
180
+ for (let points = 0; points < max && units < text.length; points++) {
181
+ units += text.codePointAt(units) > 65535 ? 2 : 1;
182
+ }
183
+ return text.slice(0, units);
184
+ }
185
+
186
+ // src/utils/scrub.ts
187
+ var KEY_MARKER = "[REDACTED-KEY]";
188
+ var CONTROL = new RegExp("[\\u0000-\\u001f\\u007f-\\u009f\\u2028\\u2029]", "g");
189
+ function escapeRegExp(text) {
190
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
191
+ }
192
+ function percentDecode(text) {
193
+ try {
194
+ return decodeURIComponent(text);
195
+ } catch {
196
+ return text.replace(/%([0-9A-Fa-f]{2})/g, (_m, hex) => String.fromCharCode(parseInt(hex, 16)));
197
+ }
198
+ }
199
+ function keyPattern(keys) {
200
+ const values = /* @__PURE__ */ new Set();
201
+ for (const k of [...keys, process.env.RISICARE_API_KEY]) if (k) values.add(k);
202
+ if (values.size === 0) return null;
203
+ const ordered = [...values].sort((a, b) => b.length - a.length);
204
+ return new RegExp(ordered.map(escapeRegExp).join("|"), "gi");
205
+ }
206
+ function scrubText(text, keys, limit = 200) {
207
+ let out;
208
+ try {
209
+ out = text instanceof Error ? `${text.name}: ${text.message}` : String(text);
210
+ } catch {
211
+ out = typeof text;
212
+ }
213
+ const pattern = keyPattern(keys);
214
+ if (pattern !== null) {
215
+ if (out.includes("%")) {
216
+ const decoded = percentDecode(out);
217
+ pattern.lastIndex = 0;
218
+ if (decoded !== out && pattern.test(decoded)) out = decoded;
219
+ }
220
+ pattern.lastIndex = 0;
221
+ out = out.replace(pattern, KEY_MARKER);
222
+ }
223
+ return cutCodePoints(out.replace(CONTROL, " "), limit);
224
+ }
225
+ function safeUrl(url) {
226
+ try {
227
+ const u = new URL(url);
228
+ const path = u.pathname === "/" && !/^[a-z][a-z0-9+.-]*:\/\/[^/?#]*\//i.test(url) ? "" : u.pathname;
229
+ return `${u.protocol}//${u.host}${path}`;
230
+ } catch {
231
+ return url.split("?")[0].split("#")[0].replace(/\/\/[^/@]*@/, "//");
232
+ }
233
+ }
234
+ function hasCredentials(url) {
235
+ try {
236
+ const u = new URL(url);
237
+ return u.username !== "" || u.password !== "";
238
+ } catch {
239
+ return false;
240
+ }
241
+ }
242
+
153
243
  // src/masking.ts
154
244
  var MASK_ERROR_MARKER = "<risicare:mask-error>";
155
245
  var MASK_BUDGET_MS_PER_SPAN = 1;
@@ -229,6 +319,9 @@ var CONTENT_KEYS_EXACT = /* @__PURE__ */ new Set([
229
319
  "gen_ai.output.messages",
230
320
  "gen_ai.system_instructions",
231
321
  "gen_ai.tool.call.arguments",
322
+ // Corpus v2: the list of tool definitions of a request. The names,
323
+ // descriptions and schemas in it are text that the customer wrote.
324
+ "gen_ai.tool.definitions",
232
325
  "gen_ai.tool.call.result",
233
326
  "input.value",
234
327
  // OpenInference
@@ -392,7 +485,13 @@ function applyMaskBatch(spans, mask, onError) {
392
485
  }
393
486
  const elapsedMs = Date.now() - started;
394
487
  if (errors > 0 && firstKey !== void 0) {
395
- debug(`mask threw on ${errors} field(s); first: key=${firstKey} ${String(firstError)}`);
488
+ let detail;
489
+ try {
490
+ detail = String(firstError);
491
+ } catch {
492
+ detail = typeof firstError;
493
+ }
494
+ debug(`mask threw on ${errors} field(s); first: key=${firstKey} ${detail}`);
396
495
  }
397
496
  return { errors, elapsedMs };
398
497
  }
@@ -408,8 +507,37 @@ function warnMaskSlow(spanCount, elapsedMs) {
408
507
  }
409
508
 
410
509
  // src/exporters/batch.ts
510
+ var DEADLINE = /* @__PURE__ */ Symbol("deadline");
511
+ async function raceDeadline(p, ms) {
512
+ let timer;
513
+ const deadline = new Promise((resolve) => {
514
+ timer = setTimeout(() => resolve(DEADLINE), Math.max(0, ms));
515
+ });
516
+ try {
517
+ return await Promise.race([p, deadline]);
518
+ } finally {
519
+ clearTimeout(timer);
520
+ }
521
+ }
522
+ function describeThrown(e) {
523
+ return scrubText(e, []);
524
+ }
525
+ async function shutdownExporter(exporter, ms) {
526
+ try {
527
+ const outcome = await raceDeadline(Promise.resolve().then(() => exporter.shutdown()), ms);
528
+ if (outcome === DEADLINE) debug(`${exporter.name}.shutdown() did not finish within ${ms} ms`);
529
+ } catch (e) {
530
+ debug(`Error shutting down ${exporter.name}: ${describeThrown(e)}`);
531
+ }
532
+ }
411
533
  var SHUTDOWN_RESERVE_MS = 100;
412
534
  var SHUTDOWN_BUDGET_MS = 5e3;
535
+ var MAX_TIMEOUT_MS = 2 ** 31 - 1;
536
+ function normalizeTimeoutMs(value) {
537
+ if (typeof value !== "number" || !Number.isFinite(value)) return SHUTDOWN_BUDGET_MS;
538
+ if (value < 0) return 0;
539
+ return Math.min(value, MAX_TIMEOUT_MS);
540
+ }
413
541
  var REFUSAL_WARN_WINDOW_MS = 3e5;
414
542
  var DEFAULT_BATCH_SIZE = 500;
415
543
  var MAX_ATTRIBUTE_PAIRS_PER_BATCH = 24e3;
@@ -498,11 +626,18 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
498
626
  _timer = null;
499
627
  _started = false;
500
628
  _flushing = false;
629
+ _inFlight = null;
501
630
  _beforeExitHandler = null;
502
631
  // Retry tracking for failed batches (Audit #5)
503
632
  _retryCounts = /* @__PURE__ */ new Map();
504
633
  static MAX_RETRIES = 3;
505
634
  _lastRefusalWarnAt = Number.NEGATIVE_INFINITY;
635
+ _lastUnackWarnAt = Number.NEGATIVE_INFINITY;
636
+ /**
637
+ * The loss total (dropped + rejected) that the last finished `flush()` saw.
638
+ * A flush reports a loss that is new since then, one time.
639
+ */
640
+ _lossMark = 0;
506
641
  // ── Masking (see `../masking.ts`) ──────────────────────────────────────────
507
642
  _mask;
508
643
  // ── Content capture gate (see `../masking.ts`) ─────────────────────────────
@@ -524,9 +659,15 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
524
659
  droppedSpansByReason = {
525
660
  queue_full: 0,
526
661
  transport_refused: 0,
527
- shutdown_residue: 0
662
+ shutdown_residue: 0,
663
+ unacknowledged: 0
528
664
  };
529
665
  exportedSpans = 0;
666
+ /**
667
+ * Spans that the backend acknowledged as REJECTED (the ack's `rejected`). They
668
+ * are not stored and are not retried. An exporter without an ack adds nothing.
669
+ */
670
+ rejectedSpans = 0;
530
671
  /** Export ATTEMPTS, not a span count. */
531
672
  failedExports = 0;
532
673
  /**
@@ -611,13 +752,32 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
611
752
  * NO progress and abandons the queue exactly as before — measured at 900 of
612
753
  * 1,000. We await a tick instead, the shape `flush()` already models.
613
754
  *
614
- * Anything still queued when the loop stops is COUNTED as `shutdown_residue`
615
- * and announced with a WARNING. The warning is the primary signal, not the
616
- * counter: a counter incremented inside `shutdown()` is written to memory
617
- * about to be freed and has no reader (RULING §2 / §8.2).
755
+ * The deadline is a hard wall-clock bound. Each round is raced against the
756
+ * budget that remains. At the deadline, `shutdown()` stops the round in flight
757
+ * through the exporter's optional `abort()`. That stopped round changes no
758
+ * counter, does not move the circuit breaker and re-queues nothing. Through
759
+ * 0.7.1 the deadline was checked only between rounds, so one round against a
760
+ * silent server (3 attempts x 5 s) held a 5,000 ms shutdown for 15 s.
761
+ *
762
+ * Anything still queued when the loop stops, AND a batch whose export was
763
+ * stopped, is COUNTED as `shutdown_residue` and announced with ONE WARNING
764
+ * that gives both numbers. The server may have received the spans of the
765
+ * stopped export. The warning is the primary signal, not the counter: a
766
+ * counter incremented inside `shutdown()` is written to memory about to be
767
+ * freed and has no reader (RULING §2 / §8.2).
768
+ *
769
+ * Each exporter's own `shutdown()` gets the time that is left of the budget.
770
+ * An exporter that does not finish in that time is not awaited.
771
+ *
772
+ * Limit: the abort does not stop a TLS handshake that stalls. Measured, such a
773
+ * handshake kept the process alive about 10 s after `shutdown()` returned.
774
+ *
775
+ * @param timeoutMs - The budget (default 5000). See `normalizeTimeoutMs` for
776
+ * the values that are not a finite number.
618
777
  */
619
778
  async shutdown(timeoutMs = SHUTDOWN_BUDGET_MS) {
620
779
  if (!this._started) return;
780
+ timeoutMs = normalizeTimeoutMs(timeoutMs);
621
781
  this._started = false;
622
782
  if (this._timer) {
623
783
  clearInterval(this._timer);
@@ -628,10 +788,13 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
628
788
  this._beforeExitHandler = null;
629
789
  }
630
790
  const reserveMs = Math.min(SHUTDOWN_RESERVE_MS, timeoutMs / 2);
631
- const drainDeadline = Date.now() + Math.max(0, timeoutMs - reserveMs);
791
+ const shutdownDeadline = Date.now() + timeoutMs;
792
+ const drainDeadline = shutdownDeadline - reserveMs;
632
793
  while (this._queue.length > 0 || this._flushing) {
633
- if (Date.now() >= drainDeadline) break;
634
- const outcome = await this._exportBatch();
794
+ const remaining = drainDeadline - Date.now();
795
+ if (remaining <= 0) break;
796
+ const outcome = await raceDeadline(this._exportBatch(), remaining);
797
+ if (outcome === DEADLINE) break;
635
798
  if (outcome === 3 /* Busy */) {
636
799
  await new Promise((r) => setTimeout(r, 1));
637
800
  continue;
@@ -640,20 +803,29 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
640
803
  break;
641
804
  }
642
805
  }
643
- const residue = this._queue.length;
806
+ const queued = this._queue.length;
807
+ const flight = this._inFlight;
808
+ const inFlight = flight !== null && !flight.abandoned ? flight.size : 0;
809
+ if (flight !== null && !flight.abandoned) {
810
+ flight.abandoned = true;
811
+ for (const exporter of this._exporters) {
812
+ try {
813
+ exporter.abort?.(flight.spans);
814
+ } catch (e) {
815
+ debug(`Error aborting ${exporter.name}: ${describeThrown(e)}`);
816
+ }
817
+ }
818
+ }
819
+ const residue = queued + inFlight;
644
820
  if (residue > 0) {
645
821
  this._queue.length = 0;
646
822
  this._recordDropped("shutdown_residue", residue);
647
823
  warn(
648
- `shutdown() gave up with ${residue} span(s) still queued (reason=shutdown_residue) \u2014 they were NOT delivered. The ${timeoutMs} ms shutdown budget expired or the transport stopped accepting. Raise the shutdown timeout, or your container's termination grace period, to lose less.`
824
+ `shutdown() gave up with ${residue} span(s) (reason=shutdown_residue): ${queued} still queued and ${inFlight} in an export that shutdown() stopped. ` + (inFlight > 0 ? `The server may have received the ${inFlight} span(s) of the stopped export. ` : "") + `All ${residue} were not confirmed as delivered, and are counted as dropped. The ${timeoutMs} ms shutdown budget expired or the transport stopped accepting. Pass a larger timeout to shutdown(timeoutMs), or raise your container's termination grace period, to lose less.`
649
825
  );
650
826
  }
651
827
  for (const exporter of this._exporters) {
652
- try {
653
- await exporter.shutdown();
654
- } catch (e) {
655
- debug(`Error shutting down ${exporter.name}: ${e}`);
656
- }
828
+ await shutdownExporter(exporter, Math.max(0, shutdownDeadline - Date.now()));
657
829
  }
658
830
  this._retryCounts.clear();
659
831
  debug(
@@ -677,7 +849,8 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
677
849
  /**
678
850
  * Flush pending spans.
679
851
  *
680
- * @returns `true` only if everything queued at entry was DELIVERED.
852
+ * @returns `true` only if everything queued at entry was DELIVERED and no
853
+ * span was lost since the previous flush returned.
681
854
  *
682
855
  * RULING §6. This used to answer a different question — "is the queue
683
856
  * empty?" — and those two diverge in exactly one case: when spans left the
@@ -686,35 +859,76 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
686
859
  * everything right; silence lets them discover the problem, a false
687
860
  * affirmative forecloses it.
688
861
  *
689
- * Returns `false` when (a) the deadline expires with spans still queued,
690
- * (b) any span was discarded during the window — breaker refusal or retry
691
- * exhaustion — or (c) it is called after `shutdown()` and the shutdown
692
- * residue was non-zero.
862
+ * A LOSS IS REPORTED BY THE NEXT `flush()`, ONE TIME (design section 0,
863
+ * third revision). Returns `true` only when, at the moment it returns, the
864
+ * queue is empty, no batch is in flight, and no span was discarded or
865
+ * rejected since the previous `flush()` returned (or since start, for the
866
+ * first call). Through 0.7.1 only a loss during the call counted: a script
867
+ * whose spans the batch timer had discarded against a 401 sink got `true`
868
+ * with nothing delivered. A failure that a retry repairs does not make it
869
+ * `false`.
870
+ *
871
+ * Mechanics: the processor keeps a loss mark, the loss total (dropped +
872
+ * rejected) that the last finished flush saw. A flush compares the total
873
+ * with the mark it read at its start, then raises the mark to the total
874
+ * (never lowers it). Two flushes that overlap both answer `false` for one
875
+ * loss. The flush after that answers `true` when nothing new was lost.
876
+ *
877
+ * Returns `false` in these cases:
878
+ * (a) the deadline expires with spans still queued or in flight;
879
+ * (b) a span was discarded since the previous flush returned: queue full,
880
+ * breaker refusal, retry exhaustion, or an ack that did not account
881
+ * for it or a span that could not be serialized (`unacknowledged`);
882
+ * (c) the backend rejected a span since the previous flush returned;
883
+ * (d) it is called after `shutdown()`, and the shutdown residue was not 0.
884
+ *
885
+ * The deadline is a hard wall-clock bound on the WAIT. At the deadline
886
+ * `flush()` returns `false`. Through 0.7.1 the deadline was checked only
887
+ * between rounds.
888
+ *
889
+ * ⚠ `flush()` never stops a request that is already sent. The export round in
890
+ * flight continues and ends by its own rules: its request timeout, its
891
+ * retries, the circuit breaker. A short flush budget therefore costs no retry,
892
+ * moves no breaker and sends nothing twice. Its result still counts.
893
+ *
894
+ * Limit: that round can keep the process alive after `flush()` returns
895
+ * `false`, until its own timeouts end. Measured on a silent server: 22 s. To
896
+ * end a process within a bound, call `await shutdown(timeoutMs)`: it stops
897
+ * the round in flight.
693
898
  *
694
899
  * ⚠ (b) reads the flat total, so a concurrent `queue_full` drop of a span
695
- * that arrived DURING the window also answers `false`. That is the
900
+ * that arrived during the call also answers `false`. That is the
696
901
  * conservative direction on purpose: a false negative costs a caller one
697
902
  * more flush, a false positive is the defect being fixed.
698
903
  */
699
904
  async flush(timeoutMs = SHUTDOWN_BUDGET_MS) {
905
+ timeoutMs = normalizeTimeoutMs(timeoutMs);
700
906
  if (!this._started) {
701
907
  return this._queue.length === 0 && this.droppedSpansByReason.shutdown_residue === 0;
702
908
  }
703
- const start = Date.now();
704
- const discardedAtEntry = this.droppedSpans;
909
+ const deadline = Date.now() + timeoutMs;
910
+ const mark = this._lossMark;
911
+ const finish = (settled) => {
912
+ const lost = this.droppedSpans + this.rejectedSpans;
913
+ this._lossMark = Math.max(this._lossMark, lost);
914
+ return settled && lost === mark;
915
+ };
705
916
  while (this._queue.length > 0 || this._flushing) {
917
+ const remaining = deadline - Date.now();
918
+ if (remaining <= 0) return finish(false);
706
919
  if (!this._flushing && this._queue.length > 0) {
707
- await this._exportBatch();
920
+ const outcome = await raceDeadline(this._exportBatch(), remaining);
921
+ if (outcome === DEADLINE) return finish(false);
708
922
  } else {
709
923
  await new Promise((r) => setTimeout(r, 1));
710
924
  }
711
- if (Date.now() - start > timeoutMs) return false;
712
925
  }
713
- return this.droppedSpans === discardedAtEntry;
926
+ return finish(true);
714
927
  }
715
928
  getMetrics() {
716
929
  return {
717
930
  exportedSpans: this.exportedSpans,
931
+ rejectedSpans: this.rejectedSpans,
718
932
  droppedSpans: this.droppedSpans,
719
933
  droppedSpansByReason: { ...this.droppedSpansByReason },
720
934
  failedExports: this.failedExports,
@@ -724,10 +938,56 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
724
938
  queueUtilization: this._queue.length / this._maxQueueSize
725
939
  };
726
940
  }
941
+ /**
942
+ * Count a delivered batch of `n` spans from the exporter's ack.
943
+ *
944
+ * The ack is not trusted. With r = min(rejected, n), a = min(accepted, n - r)
945
+ * and u = n - a - r: `exportedSpans` rises by a, `rejectedSpans` by r, and the
946
+ * u spans that no ack accounts for are dropped as `unacknowledged`, with one
947
+ * rate-limited WARNING. Without an ack (console, custom exporter), the whole
948
+ * batch counts as exported, as before. Python twin: the same clamp.
949
+ */
950
+ _countDelivered(n, ack) {
951
+ if (ack === null) {
952
+ this.exportedSpans += n;
953
+ return;
954
+ }
955
+ const rejected = Math.min(ack.rejected, n);
956
+ const accepted = Math.min(ack.accepted, n - rejected);
957
+ const unacknowledged = n - accepted - rejected;
958
+ this.exportedSpans += accepted;
959
+ this.rejectedSpans += rejected;
960
+ if (unacknowledged > 0) {
961
+ this._recordDropped("unacknowledged", unacknowledged);
962
+ const now = Date.now();
963
+ if (now - this._lastUnackWarnAt > REFUSAL_WARN_WINDOW_MS) {
964
+ this._lastUnackWarnAt = now;
965
+ warn(
966
+ `Risicare: the backend's ack accounts for ${accepted + rejected} of ${n} span(s) in this batch. The other ${unacknowledged} are counted as dropped (reason=unacknowledged): they may not be stored. ${this.droppedSpansByReason.unacknowledged} span(s) counted this way so far in this process.`
967
+ );
968
+ }
969
+ }
970
+ }
971
+ /** The requests in one export round, when every exporter that delivers states it. */
972
+ _requestsPerRound() {
973
+ const delivering = this._exporters.filter((e) => e.countsAsDelivery !== false);
974
+ if (delivering.length !== 1) return void 0;
975
+ const n = delivering[0].attemptsPerExport;
976
+ return typeof n === "number" && Number.isFinite(n) && n > 0 ? n : void 0;
977
+ }
978
+ /**
979
+ * One export round.
980
+ *
981
+ * Which results decide delivery: an exporter that only shows spans locally
982
+ * (`countsAsDelivery === false`, the console exporter) decides only when no
983
+ * other kind of exporter is present. Among the exporters that deliver, ANY
984
+ * success counts the batch as delivered, once.
985
+ */
727
986
  async _exportBatch() {
728
987
  if (this._flushing) return 3 /* Busy */;
729
988
  if (this._queue.length === 0) return 0 /* Empty */;
730
989
  this._flushing = true;
990
+ let flight = null;
731
991
  try {
732
992
  let take = 0;
733
993
  let pairs = 0;
@@ -746,6 +1006,8 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
746
1006
  }
747
1007
  const batch = this._queue.splice(0, take);
748
1008
  if (batch.length === 0) return 0 /* Empty */;
1009
+ flight = { spans: batch, size: batch.length, abandoned: false };
1010
+ this._inFlight = flight;
749
1011
  if (!this._traceContent) {
750
1012
  const stripped = stripContentBatch(batch);
751
1013
  if (stripped > 0) {
@@ -759,12 +1021,19 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
759
1021
  }
760
1022
  debug(`Exporting batch of ${batch.length} spans`);
761
1023
  let batchExported = false;
1024
+ const anyDelivers = this._exporters.some((e) => e.countsAsDelivery !== false);
1025
+ const unserializable = /* @__PURE__ */ new Set();
762
1026
  for (const exporter of this._exporters) {
1027
+ const decides = !anyDelivers || exporter.countsAsDelivery !== false;
763
1028
  try {
764
1029
  const result = await exporter.export(batch);
1030
+ if (flight.abandoned) return 2 /* Failed */;
1031
+ if (!decides) continue;
1032
+ for (const span of exporter.takeUnserializable?.(batch) ?? []) unserializable.add(span);
765
1033
  if (result === "success" /* SUCCESS */) {
1034
+ const ack = exporter.takeAck?.(batch) ?? null;
766
1035
  if (!batchExported) {
767
- this.exportedSpans += batch.length;
1036
+ this._countDelivered(batch.length - unserializable.size, ack);
768
1037
  batchExported = true;
769
1038
  for (const span of batch) {
770
1039
  this._retryCounts.delete(span.spanId);
@@ -774,13 +1043,17 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
774
1043
  this.failedExports++;
775
1044
  }
776
1045
  } catch (e) {
1046
+ if (flight.abandoned) return 2 /* Failed */;
1047
+ if (!decides) continue;
777
1048
  this.failedExports++;
778
- debug(`Export to ${exporter.name} failed: ${e}`);
1049
+ debug(`Export to ${exporter.name} failed: ${describeThrown(e)}`);
779
1050
  }
780
1051
  }
1052
+ if (unserializable.size > 0) this._recordDropped("unacknowledged", unserializable.size);
781
1053
  if (!batchExported) {
782
1054
  let dropped = 0;
783
1055
  const retryable = batch.filter((span) => {
1056
+ if (unserializable.has(span)) return false;
784
1057
  const count = (this._retryCounts.get(span.spanId) ?? 0) + 1;
785
1058
  if (count > _BatchSpanProcessor.MAX_RETRIES) {
786
1059
  this._retryCounts.delete(span.spanId);
@@ -799,14 +1072,17 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
799
1072
  const now = Date.now();
800
1073
  if (now - this._lastRefusalWarnAt > REFUSAL_WARN_WINDOW_MS) {
801
1074
  this._lastRefusalWarnAt = now;
1075
+ const rounds = _BatchSpanProcessor.MAX_RETRIES + 1;
1076
+ const perRound = this._requestsPerRound();
802
1077
  warn(
803
- `${dropped} span(s) DISCARDED (reason=transport_refused) after ${_BatchSpanProcessor.MAX_RETRIES} failed export attempts each \u2014 the endpoint is not accepting spans and the retry budget is spent. ${this.droppedSpansByReason.transport_refused} span(s) discarded this way so far in this process. This data is gone; check the apiKey and the endpoint.`
1078
+ `${dropped} span(s) DISCARDED (reason=transport_refused) after ${rounds} failed export rounds each` + (perRound !== void 0 ? ` (up to ${rounds * perRound} HTTP requests: ${perRound} in each round)` : "") + ` \u2014 the endpoint is not accepting spans and the retry budget is spent. ${this.droppedSpansByReason.transport_refused} span(s) discarded this way so far in this process. This data is gone; check the apiKey and the endpoint.`
804
1079
  );
805
1080
  }
806
1081
  }
807
1082
  }
808
1083
  return batchExported ? 1 /* Delivered */ : 2 /* Failed */;
809
1084
  } finally {
1085
+ if (this._inFlight === flight) this._inFlight = null;
810
1086
  this._flushing = false;
811
1087
  }
812
1088
  }
@@ -815,18 +1091,46 @@ var BatchSpanProcessor = class _BatchSpanProcessor {
815
1091
  // src/config.ts
816
1092
  var DEFAULT_ENDPOINT = "https://ingest.risicare.ai";
817
1093
  var DEFAULT_API_ENDPOINT = "https://api.risicare.ai";
1094
+ var ASCII_WS_EDGES = /^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g;
1095
+ function given(value) {
1096
+ if (typeof value !== "string") return void 0;
1097
+ const trimmed = value.replace(ASCII_WS_EDGES, "");
1098
+ return trimmed === "" ? void 0 : trimmed;
1099
+ }
1100
+ var DEFAULT_INGEST_PATTERN = /^https:\/\/ingest\.risicare\.ai(?::443)?\/*$/i;
1101
+ function isDefaultIngest(value) {
1102
+ return DEFAULT_INGEST_PATTERN.test(value);
1103
+ }
1104
+ function resolveEndpoints(endpointOption, apiEndpointOption, envEndpoint, envApiEndpoint) {
1105
+ const e = given(endpointOption);
1106
+ const a = given(apiEndpointOption);
1107
+ const ee = given(envEndpoint);
1108
+ const ea = given(envApiEndpoint);
1109
+ const endpoint = e ?? ee ?? DEFAULT_ENDPOINT;
1110
+ let apiEndpoint;
1111
+ if (a !== void 0) apiEndpoint = a;
1112
+ else if (e !== void 0 && !isDefaultIngest(e)) apiEndpoint = e;
1113
+ else if (ea !== void 0) apiEndpoint = ea;
1114
+ else if (e === void 0 && ee !== void 0 && !isDefaultIngest(ee)) apiEndpoint = ee;
1115
+ else apiEndpoint = DEFAULT_API_ENDPOINT;
1116
+ return { endpoint, apiEndpoint };
1117
+ }
818
1118
  function resolveConfig(config) {
819
1119
  const env = process.env;
820
1120
  const apiKey = config?.apiKey ?? env.RISICARE_API_KEY;
821
- const endpoint = config?.endpoint ?? env.RISICARE_ENDPOINT ?? DEFAULT_ENDPOINT;
822
- const apiEndpoint = config?.apiEndpoint ?? env.RISICARE_API_ENDPOINT ?? config?.endpoint ?? env.RISICARE_ENDPOINT ?? DEFAULT_API_ENDPOINT;
1121
+ const { endpoint, apiEndpoint } = resolveEndpoints(
1122
+ config?.endpoint,
1123
+ config?.apiEndpoint,
1124
+ env.RISICARE_ENDPOINT,
1125
+ env.RISICARE_API_ENDPOINT
1126
+ );
823
1127
  const projectId = config?.projectId ?? env.RISICARE_PROJECT_ID;
824
1128
  if (config?.projectId || env.RISICARE_PROJECT_ID) {
825
1129
  console.warn(
826
1130
  "[risicare] projectId is deprecated and ignored by the gateway. Your API key determines the project. Use serviceName and environment for within-project organization. projectId will be removed in v1.0."
827
1131
  );
828
1132
  }
829
- const environment = config?.environment ?? env.RISICARE_ENVIRONMENT ?? "development";
1133
+ const environment = config?.environment || env.RISICARE_ENVIRONMENT || "development";
830
1134
  const serviceName = config?.serviceName ?? env.RISICARE_SERVICE_NAME;
831
1135
  const serviceVersion = config?.serviceVersion ?? env.RISICARE_SERVICE_VERSION;
832
1136
  const traceContent = config?.traceContent ?? parseBool(env.RISICARE_TRACE_CONTENT, false);
@@ -847,14 +1151,15 @@ function resolveConfig(config) {
847
1151
  const batchTimeoutMs = validatePositiveInt("batchTimeoutMs", batchTimeoutMsRaw, 1e3, 1, 6e5);
848
1152
  const maxQueueSize = validatePositiveInt("maxQueueSize", maxQueueSizeRaw, 1e4, 1, 1e6);
849
1153
  let enabled;
1154
+ const tracingEnv = config?.enabled === void 0 ? tracingFromEnv(env.RISICARE_TRACING) : void 0;
850
1155
  if (config?.enabled !== void 0) {
851
1156
  enabled = config.enabled;
852
- } else if (env.RISICARE_TRACING !== void 0) {
853
- enabled = parseBool(env.RISICARE_TRACING, false);
1157
+ } else if (tracingEnv !== void 0) {
1158
+ enabled = tracingEnv;
854
1159
  } else {
855
1160
  enabled = !!apiKey;
856
1161
  }
857
- return {
1162
+ return hideKeyFromInspect({
858
1163
  apiKey: apiKey ?? "",
859
1164
  endpoint,
860
1165
  apiEndpoint,
@@ -874,13 +1179,36 @@ function resolveConfig(config) {
874
1179
  metadata: config?.metadata ?? {},
875
1180
  // No env fallback — see the field's note on RisicareConfig.
876
1181
  mask: config?.mask
877
- };
1182
+ });
1183
+ }
1184
+ function hideKeyFromInspect(config) {
1185
+ Object.defineProperty(config, /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom"), {
1186
+ enumerable: false,
1187
+ value(_depth, options, inspect) {
1188
+ const { apiKey, ...rest } = this;
1189
+ return `RisicareConfig ${inspect({ ...rest, apiKey: apiKey ? "<redacted>" : "" }, options)}`;
1190
+ }
1191
+ });
1192
+ return config;
878
1193
  }
879
1194
  function parseBool(value, defaultValue) {
880
1195
  if (value === void 0) return defaultValue;
881
1196
  const lower = value.toLowerCase();
882
1197
  return lower === "true" || lower === "1" || lower === "yes";
883
1198
  }
1199
+ var TRACING_ON = /* @__PURE__ */ new Set(["true", "1", "yes", "on"]);
1200
+ var TRACING_OFF = /* @__PURE__ */ new Set(["false", "0", "no", "off"]);
1201
+ function tracingFromEnv(value) {
1202
+ if (value === void 0) return void 0;
1203
+ const v = value.trim().toLowerCase();
1204
+ if (v === "") return void 0;
1205
+ if (TRACING_ON.has(v)) return true;
1206
+ if (TRACING_OFF.has(v)) return false;
1207
+ warn(
1208
+ `RISICARE_TRACING=${JSON.stringify(value.slice(0, 40))} is not a recognised value, so tracing is OFF. Use one of: true, 1, yes, on (tracing on) or false, 0, no, off (tracing off). Leave it unset or empty to use the default.`
1209
+ );
1210
+ return false;
1211
+ }
884
1212
  function parseNumericEnv(value, defaultValue) {
885
1213
  if (value === void 0) return defaultValue;
886
1214
  const n = Number(value);
@@ -1486,12 +1814,31 @@ var CANONICAL_ERROR_CODES = /* @__PURE__ */ new Set([
1486
1814
  "TOOL.RESULT.UNEXPECTED_FORMAT",
1487
1815
  "TOOL.RESULT.UNPARSEABLE"
1488
1816
  ]);
1817
+ var CORE_RULES = [
1818
+ { re: new RegExp("json", "u"), code: "PERCEPTION.PARSING.JSON_INVALID" },
1819
+ { re: new RegExp("encoding", "u"), code: "PERCEPTION.INPUT.ENCODING" },
1820
+ { re: new RegExp("injection", "u"), code: "PERCEPTION.INPUT.INJECTION" },
1821
+ { re: new RegExp("malformed", "u"), code: "PERCEPTION.INPUT.MALFORMED" },
1822
+ { re: new RegExp("hallucin", "u"), code: "REASONING.HALLUCINATION.FACTUAL" },
1823
+ { re: new RegExp("contradict", "u"), code: "REASONING.LOGIC.CONTRADICTION" },
1824
+ { re: new RegExp("timeout", "u"), code: "TOOL.EXECUTION.TIMEOUT" },
1825
+ { re: new RegExp("rate[^\\n]?limit", "u"), code: "TOOL.EXECUTION.RATE_LIMITED" },
1826
+ { re: new RegExp("permission", "u"), code: "TOOL.INVOCATION.PERMISSION_DENIED" },
1827
+ { re: new RegExp("context[^\\n]+overflow", "u"), code: "MEMORY.CONTEXT_WINDOW.EXCEEDED" },
1828
+ { re: new RegExp("state[^\\n]+corrupt", "u"), code: "MEMORY.STATE.CORRUPTED" },
1829
+ { re: new RegExp("pii", "u"), code: "OUTPUT.SAFETY.PII_LEAK" },
1830
+ { re: new RegExp("deadlock", "u"), code: "COORDINATION.WORKFLOW.DEADLOCK" },
1831
+ { re: new RegExp("undeliverable", "u"), code: "COMMUNICATION.DELIVERY.UNDELIVERABLE" },
1832
+ { re: new RegExp("crash", "u"), code: "ORCHESTRATION.LIFECYCLE.CRASHED" },
1833
+ { re: new RegExp("quorum", "u"), code: "CONSENSUS.AGREEMENT.QUORUM_FAILED" },
1834
+ { re: new RegExp("quota[^\\n]+exceed", "u"), code: "RESOURCES.QUOTA.EXCEEDED" }
1835
+ ];
1489
1836
  var SUPPLEMENTAL_RULES = [
1490
- { re: new RegExp("timeout|timedout|aborted", "i"), code: "TOOL.EXECUTION.TIMEOUT" },
1491
- { re: new RegExp("rate.?limit|\\b429\\b|too many requests", "i"), code: "TOOL.EXECUTION.RATE_LIMITED" },
1492
- { re: new RegExp("connection|econnrefused|econnreset|enotfound|fetch failed|unreachable|broken pipe", "i"), code: "TOOL.EXECUTION.DEPENDENCY_FAILED" },
1493
- { re: new RegExp("\\bauth|unauthorized|forbidden|permission|\\b401\\b|\\b403\\b", "i"), code: "TOOL.INVOCATION.PERMISSION_DENIED" },
1494
- { re: new RegExp("json|syntaxerror|unexpected token|decodeerror", "i"), code: "PERCEPTION.PARSING.JSON_INVALID" }
1837
+ { re: new RegExp("timeout|timedout|aborted", "u"), code: "TOOL.EXECUTION.TIMEOUT" },
1838
+ { re: new RegExp("rate[^\\n]?limit|(?<![\\p{L}\\p{N}_])429(?![\\p{L}\\p{N}_])|too many requests", "u"), code: "TOOL.EXECUTION.RATE_LIMITED" },
1839
+ { re: new RegExp("connection|econnrefused|econnreset|enotfound|fetch failed|unreachable|broken pipe", "u"), code: "TOOL.EXECUTION.DEPENDENCY_FAILED" },
1840
+ { re: new RegExp("(?<![\\p{L}\\p{N}_])auth|unauthorized|forbidden|permission|(?<![\\p{L}\\p{N}_])401(?![\\p{L}\\p{N}_])|(?<![\\p{L}\\p{N}_])403(?![\\p{L}\\p{N}_])", "u"), code: "TOOL.INVOCATION.PERMISSION_DENIED" },
1841
+ { re: new RegExp("json|syntaxerror|unexpected token|decodeerror", "u"), code: "PERCEPTION.PARSING.JSON_INVALID" }
1495
1842
  ];
1496
1843
  var GENERIC_FALLBACK = "TOOL.EXECUTION.CRASHED";
1497
1844
 
@@ -1508,6 +1855,16 @@ var GuardRejectedError = class extends Error {
1508
1855
  this.phase = phase;
1509
1856
  }
1510
1857
  };
1858
+ function guardText(message) {
1859
+ if (!message || typeof message !== "object") return null;
1860
+ const content = message.content;
1861
+ if (typeof content === "string") return content === "" ? null : content;
1862
+ if (Array.isArray(content)) {
1863
+ const texts = content.filter((p) => !!p && typeof p === "object" && p.type === "text" && typeof p.text === "string").map((p) => p.text);
1864
+ if (texts.length > 0) return texts.join("\n");
1865
+ }
1866
+ return null;
1867
+ }
1511
1868
  function createInterceptContext(operationType, operationName, options) {
1512
1869
  return {
1513
1870
  operationType,
@@ -1567,8 +1924,8 @@ var DefaultFixInterceptor = class {
1567
1924
  }
1568
1925
  if (modifiedMessages && this._config.enabled) {
1569
1926
  for (const msg of modifiedMessages) {
1570
- const content = msg.content;
1571
- if (typeof content === "string") {
1927
+ const content = guardText(msg);
1928
+ if (content !== null) {
1572
1929
  for (const fix of this._cache.getAll()) {
1573
1930
  if (fix.fixType === "guard") {
1574
1931
  const { passed, result } = this._applier.applyGuardFix(
@@ -1713,7 +2070,13 @@ var DefaultFixInterceptor = class {
1713
2070
  }
1714
2071
  };
1715
2072
  function classifyError(error) {
1716
- const haystack = `${error?.constructor?.name ?? "Error"} ${error?.message ?? ""}`;
2073
+ const message = String(error?.message ?? "").toLowerCase();
2074
+ for (const rule of CORE_RULES) {
2075
+ if (rule.re.test(message)) {
2076
+ return CANONICAL_ERROR_CODES.has(rule.code) ? rule.code : GENERIC_FALLBACK;
2077
+ }
2078
+ }
2079
+ const haystack = `${error?.constructor?.name ?? "Error"} ${String(error?.message ?? "")}`.toLowerCase();
1717
2080
  for (const rule of SUPPLEMENTAL_RULES) {
1718
2081
  if (rule.re.test(haystack)) {
1719
2082
  return CANONICAL_ERROR_CODES.has(rule.code) ? rule.code : GENERIC_FALLBACK;
@@ -1729,6 +2092,9 @@ function storage() {
1729
2092
  function getContext() {
1730
2093
  return storage().getStore() ?? {};
1731
2094
  }
2095
+ function hasContextScope() {
2096
+ return storage().getStore() !== void 0;
2097
+ }
1732
2098
  function runWithContext(overrides, fn) {
1733
2099
  const parent = getContext();
1734
2100
  const merged = { ...parent, ...overrides };
@@ -1785,7 +2151,8 @@ function getCurrentContext() {
1785
2151
  // src/tracer.ts
1786
2152
  function attachErrorCode(span, error) {
1787
2153
  try {
1788
- const err = error instanceof Error ? error : new Error(String(error));
2154
+ const ownMessage = error !== null && typeof error === "object" ? error.message : void 0;
2155
+ const err = error instanceof Error ? error : new Error(typeof ownMessage === "string" ? ownMessage : String(error));
1789
2156
  const code = classifyError(err);
1790
2157
  if (code) {
1791
2158
  span.setAttribute("error.code", code);
@@ -1887,6 +2254,7 @@ var Tracer = class {
1887
2254
  (error) => {
1888
2255
  span.recordException(error instanceof Error ? error : String(error));
1889
2256
  attachErrorCode(span, error);
2257
+ markCancel(span, error);
1890
2258
  span.end();
1891
2259
  throw error;
1892
2260
  }
@@ -1897,6 +2265,7 @@ var Tracer = class {
1897
2265
  } catch (error) {
1898
2266
  span.recordException(error instanceof Error ? error : String(error));
1899
2267
  attachErrorCode(span, error);
2268
+ markCancel(span, error);
1900
2269
  span.end();
1901
2270
  throw error;
1902
2271
  }
@@ -1916,7 +2285,7 @@ var Tracer = class {
1916
2285
  const session2 = getCurrentSession();
1917
2286
  const agent2 = getCurrentAgent();
1918
2287
  const phase = getCurrentPhase();
1919
- const traceId = opts.traceId ?? parentSpan?.traceId ?? generateTraceId();
2288
+ const traceId = opts.traceId ?? parentSpan?.traceId ?? getContext()._rootTraceId ?? generateTraceId();
1920
2289
  if (!parentSpan) {
1921
2290
  if (getContext()._traceSampled === false) {
1922
2291
  return NOOP_SPAN;
@@ -1952,9 +2321,15 @@ var Tracer = class {
1952
2321
  );
1953
2322
  }
1954
2323
  };
2324
+ function markCancel(span, error) {
2325
+ if (error instanceof Error && error.name === "AbortError") {
2326
+ span.setAttribute("risicare.cancelled", true);
2327
+ span.setAttribute("risicare.cancel_type", "AbortError");
2328
+ }
2329
+ }
1955
2330
 
1956
2331
  // src/version.ts
1957
- var SDK_VERSION = "0.7.0";
2332
+ var SDK_VERSION = "0.8.0";
1958
2333
 
1959
2334
  // src/exporters/http.ts
1960
2335
  var CAPACITY_REFUSAL_STATUSES = /* @__PURE__ */ new Set([429, 503]);
@@ -1962,6 +2337,8 @@ var RETRY_AFTER_CEILING_MS = 1e4;
1962
2337
  var REFUSAL_BACKOFF_BASE_MS = 1e3;
1963
2338
  var REFUSAL_NO_HINT = -1;
1964
2339
  var MISROUTE_WARN_WINDOW_MS = 3e5;
2340
+ var AUTH_REFUSAL_STATUSES = /* @__PURE__ */ new Set([401, 403]);
2341
+ var FAILURE_WARN_WINDOW_MS = 3e5;
1965
2342
  var HttpExporter = class {
1966
2343
  name = "http";
1967
2344
  _endpoint;
@@ -1977,21 +2354,24 @@ var HttpExporter = class {
1977
2354
  // cooldown gives the gateway time to come back before the half-open
1978
2355
  // probe fires. Lower numbers (was 3@30s) tripped on transient blips.
1979
2356
  _consecutiveFailures = 0;
2357
+ _lastMisrouteWarnAt = -Infinity;
2358
+ _lastFailureWarnAt = -Infinity;
1980
2359
  /**
1981
- * F-SDK503-001. Set by `_sendRequest` when a response is a CAPACITY REFUSAL, read by the
1982
- * retry loop and the breaker. `null` = not a refusal; `REFUSAL_NO_HINT` = refused with no
1983
- * `Retry-After`; otherwise the honoured delay in ms.
1984
- *
1985
- * ⚠ Node has ONE thread and `export()` is `async`, so two concurrent exports interleave on
1986
- * this field at every `await`. It is written immediately after the awaited `fetch` resolves
1987
- * and read on the very next statement, with no `await` between — so the value cannot be
1988
- * clobbered mid-decision. The Python twin carries the same per-call state on `self` for the
1989
- * same reason (its `_send_request` signature is pin-locked).
2360
+ * The kind of failure that the last outage WARNING named ('auth', 'http', 'none',
2361
+ * 'unknown'). `null` after a success. A new kind in the same outage writes one more
2362
+ * WARNING, also inside the window.
1990
2363
  */
1991
- _lastRefusalAfterMs = null;
1992
- /** Set by `_sendRequest` on a MISROUTE: a short description of the answer. `null` = none. */
1993
- _lastMisroute = null;
1994
- _lastMisrouteWarnAt = -Infinity;
2364
+ _failureKind = null;
2365
+ /** The endpoint as a log line may show it: no userinfo, no query (see `safeUrl`). */
2366
+ _safeEndpoint;
2367
+ /** The endpoint holds userinfo. `fetch` refuses such a URL. */
2368
+ _endpointHasCredentials;
2369
+ /** The calls in progress, by their batch, so that `abort(spans)` stops the right one. */
2370
+ _calls = /* @__PURE__ */ new Map();
2371
+ /** The ack of each finished call, by its batch. Read once by `takeAck(spans)`. */
2372
+ _acks = /* @__PURE__ */ new WeakMap();
2373
+ /** The spans of each finished call that could not be serialized, by its batch. */
2374
+ _unserializable = /* @__PURE__ */ new WeakMap();
1995
2375
  _circuitOpenUntil = 0;
1996
2376
  _circuitBreakerThreshold = 5;
1997
2377
  _circuitBreakerCooldownMs = 6e4;
@@ -2003,9 +2383,105 @@ var HttpExporter = class {
2003
2383
  this._timeoutMs = options.timeoutMs ?? 5e3;
2004
2384
  this._maxRetries = options.maxRetries ?? 3;
2005
2385
  this._compress = options.compress ?? false;
2386
+ this._safeEndpoint = safeUrl(this._endpoint);
2387
+ this._endpointHasCredentials = hasCredentials(this._endpoint);
2388
+ }
2389
+ /**
2390
+ * Network or error text for a log line: the endpoint as `safeUrl` gives it, and no
2391
+ * form of the API key (`utils/scrub.ts`).
2392
+ */
2393
+ _scrub(text, limit = 200) {
2394
+ let out;
2395
+ try {
2396
+ out = text instanceof Error ? `${text.name}: ${text.message}` : String(text);
2397
+ } catch {
2398
+ out = typeof text;
2399
+ }
2400
+ if (this._safeEndpoint !== this._endpoint) out = out.split(this._endpoint).join(this._safeEndpoint);
2401
+ return scrubText(out, [this._apiKey], limit);
2402
+ }
2403
+ /** Hide the key from `util.inspect` and `console.log` of the exporter. */
2404
+ [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
2405
+ return `HttpExporter { endpoint: '${this._safeEndpoint}' }`;
2406
+ }
2407
+ /** The most requests that one `export()` call sends (`maxRetries`). */
2408
+ get attemptsPerExport() {
2409
+ return this._maxRetries;
2006
2410
  }
2411
+ /**
2412
+ * Returns the counts of the ack that the `export()` call for `spans` received, and clears
2413
+ * them. The answer is kept per batch, not on the instance: two processors that share this
2414
+ * exporter cannot read each other's ack across an `await`.
2415
+ */
2416
+ takeAck(spans) {
2417
+ const ack = this._acks.get(spans) ?? null;
2418
+ this._acks.delete(spans);
2419
+ return ack;
2420
+ }
2421
+ /** Returns the spans of `spans` that could not be serialized and were not sent, and clears them. */
2422
+ takeUnserializable(spans) {
2423
+ const bad = this._unserializable.get(spans) ?? [];
2424
+ this._unserializable.delete(spans);
2425
+ return bad;
2426
+ }
2427
+ /**
2428
+ * Cancel the `export()` call for `spans` (all calls when `spans` is not given): its
2429
+ * request, its retries and its backoff sleep. The batch processor calls it when
2430
+ * `shutdown()` abandons the batch, so that the call does not keep the process alive. The
2431
+ * cancelled call returns FAILURE, changes no counter (the circuit breaker included) and
2432
+ * logs no WARNING.
2433
+ */
2434
+ abort(spans) {
2435
+ if (spans !== void 0) this._calls.get(spans)?.cancel.abort();
2436
+ else for (const call of this._calls.values()) call.cancel.abort();
2437
+ }
2438
+ /**
2439
+ * Send one batch. The limits are the request timeout and `maxRetries`.
2440
+ *
2441
+ * ⚠ NO CALLER BUDGET. `flush(timeoutMs)` only stops WAITING at its deadline; it never
2442
+ * cuts a request that is already sent. A short flush budget that cut the request made a
2443
+ * healthy, slow server look like a failure: the span used a retry, the breaker moved, and
2444
+ * the server stored the same spans again. Only `abort()` stops the call, and only
2445
+ * `shutdown()` calls it.
2446
+ */
2007
2447
  async export(spans) {
2008
2448
  if (spans.length === 0) return "success" /* SUCCESS */;
2449
+ const call = {
2450
+ cancel: new AbortController(),
2451
+ refusalAfterMs: null,
2452
+ misroute: null,
2453
+ noRetry: false,
2454
+ status: null,
2455
+ error: null
2456
+ };
2457
+ this._calls.set(spans, call);
2458
+ try {
2459
+ return await this._export(spans, call);
2460
+ } finally {
2461
+ this._calls.delete(spans);
2462
+ }
2463
+ }
2464
+ async _export(spans, call) {
2465
+ const cancelled = call.cancel.signal;
2466
+ const parts = [];
2467
+ const bad = [];
2468
+ let errorClass = "";
2469
+ for (const span of spans) {
2470
+ try {
2471
+ parts.push(JSON.stringify(span.toPayload()));
2472
+ } catch (e) {
2473
+ bad.push(span);
2474
+ errorClass ||= e instanceof Error ? e.name : typeof e;
2475
+ }
2476
+ }
2477
+ if (bad.length > 0) {
2478
+ this._unserializable.set(spans, bad);
2479
+ warn(
2480
+ `Risicare: ${bad.length} of ${spans.length} span(s) in this batch could not be serialized (${errorClass}), for example an attribute with a BigInt or a circular reference. They are not sent and are counted as dropped (reason=unacknowledged).`
2481
+ );
2482
+ }
2483
+ if (parts.length === 0) return "success" /* SUCCESS */;
2484
+ const payload = `{"spans":[${parts.join(",")}]` + (this._projectId ? `,"projectId":${JSON.stringify(this._projectId)}` : "") + (this._environment ? `,"environment":${JSON.stringify(this._environment)}` : "") + "}";
2009
2485
  const now = Date.now();
2010
2486
  let isHalfOpen = false;
2011
2487
  if (this._consecutiveFailures >= this._circuitBreakerThreshold) {
@@ -2014,44 +2490,64 @@ var HttpExporter = class {
2014
2490
  }
2015
2491
  isHalfOpen = true;
2016
2492
  }
2017
- const body = {
2018
- spans: spans.map((s) => s.toPayload())
2019
- };
2020
- if (this._projectId) body.projectId = this._projectId;
2021
- if (this._environment) body.environment = this._environment;
2022
2493
  const maxAttempts = isHalfOpen ? 1 : this._maxRetries;
2494
+ let attempts = 0;
2023
2495
  for (let attempt = 0; attempt < maxAttempts; attempt++) {
2024
- this._lastRefusalAfterMs = null;
2025
- this._lastMisroute = null;
2026
- const result = await this._sendRequest(body);
2496
+ call.refusalAfterMs = null;
2497
+ call.misroute = null;
2498
+ call.noRetry = false;
2499
+ call.status = null;
2500
+ call.error = null;
2501
+ attempts++;
2502
+ const result = await this._sendRequest(payload, parts.length, call, spans);
2503
+ if (cancelled.aborted) {
2504
+ this._acks.delete(spans);
2505
+ return "failure" /* FAILURE */;
2506
+ }
2027
2507
  if (result === "success" /* SUCCESS */) {
2028
2508
  this._consecutiveFailures = 0;
2509
+ this._failureKind = null;
2029
2510
  return result;
2030
2511
  }
2031
- if (this._lastMisroute !== null) break;
2512
+ if (call.misroute !== null || call.noRetry) break;
2513
+ if (call.status !== null && AUTH_REFUSAL_STATUSES.has(call.status)) break;
2032
2514
  if (attempt < maxAttempts - 1) {
2033
- const refusal = this._lastRefusalAfterMs;
2515
+ const refusal = call.refusalAfterMs;
2034
2516
  const delay = refusal === null ? 100 * Math.pow(2, attempt) : refusal === REFUSAL_NO_HINT ? REFUSAL_BACKOFF_BASE_MS * Math.pow(2, attempt) : refusal;
2035
- await sleep(delay);
2517
+ await sleep(delay, cancelled);
2518
+ if (cancelled.aborted) return "failure" /* FAILURE */;
2036
2519
  }
2037
2520
  }
2038
- const refusedForCapacity = this._lastRefusalAfterMs !== null;
2039
- this._lastRefusalAfterMs = null;
2040
- if (refusedForCapacity) {
2521
+ if (call.refusalAfterMs !== null) {
2041
2522
  warn(
2042
- `Risicare: the ingest endpoint refused this batch for capacity (HTTP 429/503) after ${maxAttempts} attempt(s). This is a healthy server shedding load, not an outage \u2014 the batch is dropped but the exporter keeps sending. Reduce batch size or export rate if it persists.`
2523
+ `Risicare: the ingest endpoint refused this batch for capacity (HTTP 429/503) after ${attempts} attempt(s). This is a healthy server shedding load, not an outage. This export of the batch failed; the circuit breaker does not count it. Reduce batch size or export rate if it persists.`
2043
2524
  );
2044
2525
  return "failure" /* FAILURE */;
2045
2526
  }
2046
- const misroute = this._lastMisroute;
2047
- this._lastMisroute = null;
2527
+ const misroute = call.misroute;
2048
2528
  if (misroute !== null && Date.now() - this._lastMisrouteWarnAt > MISROUTE_WARN_WINDOW_MS) {
2049
2529
  this._lastMisrouteWarnAt = Date.now();
2050
2530
  warn(
2051
- `Risicare: spans are NOT reaching ${this._endpoint}. It answered POST /v1/spans with ${misroute}. That is not the Risicare ingest gateway, so the endpoint is probably wrong (for example the dashboard URL instead of the ingest gateway URL). The SDK does not follow redirects, and this batch is counted as NOT delivered. Set endpoint or RISICARE_ENDPOINT to the ingest gateway URL.`
2531
+ `Risicare: spans are NOT reaching ${this._safeEndpoint}. It answered POST /v1/spans with ${misroute}. That is not the Risicare ingest gateway, so the endpoint is probably wrong (for example the dashboard URL instead of the ingest gateway URL). The SDK does not follow redirects, and this batch is counted as NOT delivered. Set endpoint or RISICARE_ENDPOINT to the ingest gateway URL.`
2052
2532
  );
2053
2533
  }
2054
2534
  this._consecutiveFailures++;
2535
+ if (misroute === null && !call.noRetry) {
2536
+ const status = call.status;
2537
+ const kind = status !== null && AUTH_REFUSAL_STATUSES.has(status) ? "auth" : status !== null ? "http" : call.error !== null ? "none" : "unknown";
2538
+ let write = false;
2539
+ if (this._consecutiveFailures === 1 || this._failureKind === null) {
2540
+ if (Date.now() - this._lastFailureWarnAt > FAILURE_WARN_WINDOW_MS) {
2541
+ this._lastFailureWarnAt = Date.now();
2542
+ write = true;
2543
+ }
2544
+ this._failureKind = kind;
2545
+ } else if (kind !== this._failureKind) {
2546
+ this._failureKind = kind;
2547
+ write = true;
2548
+ }
2549
+ if (write) this._warnOutage(kind, status, call.error);
2550
+ }
2055
2551
  if (this._consecutiveFailures >= this._circuitBreakerThreshold) {
2056
2552
  this._circuitOpenUntil = Date.now() + this._circuitBreakerCooldownMs;
2057
2553
  warn(
@@ -2060,10 +2556,36 @@ var HttpExporter = class {
2060
2556
  }
2061
2557
  return "failure" /* FAILURE */;
2062
2558
  }
2559
+ /** The outage WARNING for one kind of failure. */
2560
+ _warnOutage(kind, status, error) {
2561
+ const where = `Risicare: spans are NOT reaching ${this._safeEndpoint}`;
2562
+ const tail = "Tracing continues, but the SDK drops the spans that it cannot send.";
2563
+ if (kind === "auth") {
2564
+ warn(
2565
+ `${where}: it answered HTTP ${status}. The endpoint refused the API key. Check the apiKey (or RISICARE_API_KEY) and the project it belongs to. ${tail}`
2566
+ );
2567
+ } else if (kind === "http") {
2568
+ warn(
2569
+ `${where}: it answered HTTP ${status}. The server did not take the batch. Check the endpoint and the server. ${tail} Set debug: true to see each attempt.`
2570
+ );
2571
+ } else if (kind === "none" && this._endpointHasCredentials) {
2572
+ warn(
2573
+ `${where}: no answer (${error}). The endpoint URL holds credentials (a user name or a password before the host), and fetch does not send a request to such a URL. Remove the credentials from the endpoint URL. ${tail}`
2574
+ );
2575
+ } else if (kind === "none") {
2576
+ warn(
2577
+ `${where}: no answer (${error}). The endpoint cannot be reached, or it did not answer in time. Check the endpoint and the network. ${tail} Set debug: true to see each attempt.`
2578
+ );
2579
+ } else {
2580
+ warn(
2581
+ `${where}: the export failed. Common causes: an invalid apiKey or an unreachable endpoint. ${tail} Set debug: true to see each attempt.`
2582
+ );
2583
+ }
2584
+ }
2063
2585
  /** Record a MISROUTE for the retry loop and the breaker. Always returns FAILURE. */
2064
- _noteMisroute(answer) {
2065
- this._lastMisroute = answer.slice(0, 300);
2066
- debug(`Ingest misroute: ${this._lastMisroute}`);
2586
+ _noteMisroute(call, answer) {
2587
+ call.misroute = this._scrub(answer, 300);
2588
+ debug(`Ingest misroute: ${call.misroute}`);
2067
2589
  return "failure" /* FAILURE */;
2068
2590
  }
2069
2591
  /**
@@ -2073,7 +2595,7 @@ var HttpExporter = class {
2073
2595
  * HTTP-date); anything else falls back to our own backoff rather than guessing at a date
2074
2596
  * parse.
2075
2597
  */
2076
- _noteRefusal(status, retryAfterRaw) {
2598
+ _noteRefusal(call, status, retryAfterRaw) {
2077
2599
  if (!CAPACITY_REFUSAL_STATUSES.has(status)) return false;
2078
2600
  let delay = REFUSAL_NO_HINT;
2079
2601
  if (retryAfterRaw) {
@@ -2082,11 +2604,13 @@ var HttpExporter = class {
2082
2604
  delay = Math.min(secs * 1e3, RETRY_AFTER_CEILING_MS);
2083
2605
  }
2084
2606
  }
2085
- this._lastRefusalAfterMs = delay;
2086
- debug(`Ingest refused for capacity: HTTP ${status} (Retry-After=${retryAfterRaw})`);
2607
+ call.refusalAfterMs = delay;
2608
+ debug(`Ingest refused for capacity: HTTP ${status} (Retry-After=${this._scrub(retryAfterRaw)})`);
2087
2609
  return true;
2088
2610
  }
2089
- async _sendRequest(body) {
2611
+ async _sendRequest(body, spansSent, call, spans) {
2612
+ const cancelled = call.cancel.signal;
2613
+ const timeoutMs = this._timeoutMs;
2090
2614
  const url = `${this._endpoint}/v1/spans`;
2091
2615
  const headers = {
2092
2616
  "Content-Type": "application/json",
@@ -2101,19 +2625,22 @@ var HttpExporter = class {
2101
2625
  if (this._apiKey) {
2102
2626
  headers["Authorization"] = `Bearer ${this._apiKey}`;
2103
2627
  }
2104
- let payload = JSON.stringify(body);
2628
+ let payload = body;
2105
2629
  if (this._compress && payload.length > 1024) {
2106
2630
  try {
2107
2631
  const { gzipSync } = await import("zlib");
2108
2632
  payload = gzipSync(Buffer.from(payload));
2109
2633
  headers["Content-Encoding"] = "gzip";
2110
2634
  } catch (e) {
2111
- debug(`Gzip compression failed, sending uncompressed: ${e}`);
2635
+ debug(`Gzip compression failed, sending uncompressed: ${this._scrub(e)}`);
2112
2636
  }
2113
2637
  }
2638
+ const controller = new AbortController();
2639
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
2640
+ const onCancel = () => controller.abort();
2641
+ cancelled.addEventListener("abort", onCancel, { once: true });
2642
+ if (cancelled.aborted) controller.abort();
2114
2643
  try {
2115
- const controller = new AbortController();
2116
- const timeoutId = setTimeout(() => controller.abort(), this._timeoutMs);
2117
2644
  const response = await fetch(url, {
2118
2645
  method: "POST",
2119
2646
  headers,
@@ -2123,40 +2650,89 @@ var HttpExporter = class {
2123
2650
  // the 3xx itself; a browser-like runtime returns type 'opaqueredirect' with status 0.
2124
2651
  redirect: "manual"
2125
2652
  });
2126
- clearTimeout(timeoutId);
2653
+ call.status = response.status;
2127
2654
  const redirected = response.type === "opaqueredirect";
2128
2655
  if (redirected || response.status >= 300 && response.status < 400) {
2129
2656
  const location = response.headers?.get?.("Location");
2130
- const target = location ? location.slice(0, 200) : "no Location header";
2131
- return this._noteMisroute(`HTTP ${response.status || "3xx"}, a redirect to ${target}`);
2657
+ const target = location ? this._scrub(location, 1e3) : "no Location header";
2658
+ return this._noteMisroute(call, `HTTP ${response.status || "3xx"}, a redirect to ${target}`);
2132
2659
  }
2133
2660
  if (response.status >= 200 && response.status < 300) {
2134
- const ack = await checkAck(response);
2135
- return ack === null ? this._noteMisroute(
2136
- `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
2137
- ) : ack;
2661
+ const ack = await checkAck(response, spansSent, (t) => this._scrub(t, 1e3));
2662
+ if (ack === null) {
2663
+ if (controller.signal.aborted) {
2664
+ call.status = null;
2665
+ call.error = `timeout after ${timeoutMs} ms`;
2666
+ debug(`HTTP export timeout: the ack body did not arrive within ${timeoutMs} ms`);
2667
+ return "timeout" /* TIMEOUT */;
2668
+ }
2669
+ return this._noteMisroute(
2670
+ call,
2671
+ `HTTP ${response.status} and a body that is not a Risicare ack (content-type: ${response.headers?.get?.("Content-Type") || "none"})`
2672
+ );
2673
+ }
2674
+ if (ack.result === "success" /* SUCCESS */) this._acks.set(spans, ack.counts);
2675
+ if (ack.final) call.noRetry = true;
2676
+ return ack.result;
2138
2677
  } else if (response.status === 408 || response.status === 504) {
2139
2678
  debug(`HTTP export timeout: ${response.status}`);
2140
2679
  return "timeout" /* TIMEOUT */;
2141
2680
  }
2142
- this._noteRefusal(response.status, response.headers.get("Retry-After"));
2681
+ this._noteRefusal(call, response.status, response.headers.get("Retry-After"));
2143
2682
  debug(`HTTP export failed: ${response.status}`);
2144
2683
  return "failure" /* FAILURE */;
2145
2684
  } catch (e) {
2146
- debug(`HTTP export error: ${e}`);
2685
+ const cause = e instanceof Error ? e.cause : void 0;
2686
+ call.error = controller.signal.aborted ? `timeout after ${timeoutMs} ms` : typeof cause?.code === "string" ? cause.code : e instanceof Error ? e.name : "Error";
2687
+ debug(`HTTP export error: ${this._scrub(e)}`);
2147
2688
  return "failure" /* FAILURE */;
2689
+ } finally {
2690
+ clearTimeout(timeoutId);
2691
+ cancelled.removeEventListener("abort", onCancel);
2148
2692
  }
2149
2693
  }
2150
2694
  shutdown() {
2151
2695
  }
2152
2696
  };
2153
- function sleep(ms) {
2154
- return new Promise((resolve) => setTimeout(resolve, ms));
2697
+ function sleep(ms, signal) {
2698
+ return new Promise((resolve) => {
2699
+ if (signal?.aborted) return resolve();
2700
+ const done = () => {
2701
+ clearTimeout(timer);
2702
+ signal?.removeEventListener("abort", done);
2703
+ resolve();
2704
+ };
2705
+ const timer = setTimeout(done, ms);
2706
+ signal?.addEventListener("abort", done, { once: true });
2707
+ });
2155
2708
  }
2156
2709
  function asCount(v) {
2157
2710
  return typeof v === "number" && Number.isInteger(v) && v >= 0 ? v : void 0;
2158
2711
  }
2159
- async function checkAck(response) {
2712
+ var MAX_ACK_REASONS = 3;
2713
+ var MAX_ACK_REASON_CHARS = 200;
2714
+ function oneLine(text) {
2715
+ return text.replace(/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g, " ");
2716
+ }
2717
+ function ackReasons(body) {
2718
+ const reasons = [];
2719
+ const errors = body.errors;
2720
+ if (Array.isArray(errors)) {
2721
+ for (const entry of errors) {
2722
+ const text = entry?.error;
2723
+ if (typeof text !== "string" || text.length === 0) continue;
2724
+ const cut = oneLine(text).slice(0, MAX_ACK_REASON_CHARS);
2725
+ if (!reasons.includes(cut)) reasons.push(cut);
2726
+ if (reasons.length === MAX_ACK_REASONS) break;
2727
+ }
2728
+ }
2729
+ if (reasons.length === 0) {
2730
+ const message = typeof body.errorMessage === "string" && body.errorMessage || typeof body.error_message === "string" && body.error_message || "";
2731
+ if (message) reasons.push(oneLine(message).slice(0, MAX_ACK_REASON_CHARS));
2732
+ }
2733
+ return reasons.length === 0 ? "No reason was returned." : `Reasons: ${reasons.join("; ")}`;
2734
+ }
2735
+ async function checkAck(response, spansSent, scrub) {
2160
2736
  let payload;
2161
2737
  try {
2162
2738
  payload = await response.json();
@@ -2170,25 +2746,35 @@ async function checkAck(response) {
2170
2746
  const accepted = asCount(body.accepted);
2171
2747
  const rejected = asCount(body.rejected);
2172
2748
  if (accepted === void 0 || rejected === void 0) return null;
2173
- if (rejected === 0) return "success" /* SUCCESS */;
2174
- const errors = body.errors;
2175
- const detail = typeof body.errorMessage === "string" && body.errorMessage || typeof body.error_message === "string" && body.error_message || (Array.isArray(errors) && errors.length > 0 ? JSON.stringify(errors[0]) : "") || "No reason was returned.";
2749
+ const counts = { accepted, rejected };
2750
+ if (accepted === 0 && rejected === 0 && spansSent > 0) {
2751
+ warn(
2752
+ `Risicare: the backend acknowledged 0 of ${spansSent} span(s): its ack counts no span as accepted or rejected. This batch is counted as NOT delivered.`
2753
+ );
2754
+ return { result: "failure" /* FAILURE */, counts, final: true };
2755
+ }
2756
+ if (rejected === 0) return { result: "success" /* SUCCESS */, counts, final: false };
2176
2757
  const total = accepted + rejected;
2177
2758
  if (accepted === 0) {
2178
2759
  warn(
2179
- `Risicare: the backend accepted 0 of ${total} span(s) and rejected ${rejected} \u2014 this batch was NOT stored. ${detail}`
2760
+ `Risicare: the backend accepted 0 of ${total} span(s) and rejected ${rejected} \u2014 this batch was NOT stored. Rejected spans are permanently lost and are NOT retried. ${scrub(ackReasons(body))}`
2180
2761
  );
2181
- return "failure" /* FAILURE */;
2762
+ return { result: "success" /* SUCCESS */, counts, final: false };
2182
2763
  }
2183
2764
  warn(
2184
- `Risicare: the backend rejected ${rejected} of ${total} span(s); the remaining ${accepted} were stored. Those ${rejected} are permanently lost and are NOT retried. ${detail}`
2765
+ `Risicare: the backend rejected ${rejected} of ${total} span(s); the remaining ${accepted} were stored. Those ${rejected} are permanently lost and are NOT retried. ${scrub(ackReasons(body))}`
2185
2766
  );
2186
- return "success" /* SUCCESS */;
2767
+ return { result: "success" /* SUCCESS */, counts, final: false };
2187
2768
  }
2188
2769
 
2189
2770
  // src/exporters/console.ts
2190
2771
  var ConsoleExporter = class {
2191
2772
  name = "console";
2773
+ /**
2774
+ * It only shows spans locally, so its success is not a delivery. When another
2775
+ * exporter is present, the batch processor ignores this exporter's result.
2776
+ */
2777
+ countsAsDelivery = false;
2192
2778
  export(spans) {
2193
2779
  for (const span of spans) {
2194
2780
  const payload = span.toPayload();
@@ -2202,6 +2788,17 @@ var ConsoleExporter = class {
2202
2788
  }
2203
2789
  };
2204
2790
 
2791
+ // src/utils/stream-proxy.ts
2792
+ function endOpenProviderSpans() {
2793
+ let open;
2794
+ try {
2795
+ open = [...getProviderSpanState().open];
2796
+ } catch {
2797
+ return;
2798
+ }
2799
+ for (const o of open) o.end("shutdown");
2800
+ }
2801
+
2205
2802
  // src/runtime/cache.ts
2206
2803
  var FixCache = class {
2207
2804
  _ttlMs;
@@ -2377,6 +2974,94 @@ function emptyResult(fix, fixType) {
2377
2974
  modifications: {}
2378
2975
  };
2379
2976
  }
2977
+ var SAME_ESCAPES = "bBdDsSwWfnrtv";
2978
+ var SAME_ESCAPES_IN_CLASS = "bdDsSwWfnrtv";
2979
+ var LEADING_INLINE_FLAGS = /^\(\?([ims]+)\)/;
2980
+ var PY_NAMED_GROUP = /^\(\?P<([A-Za-z_][A-Za-z0-9_]*)>/;
2981
+ var PY_NAMED_BACKREF = /^\(\?P=([A-Za-z_][A-Za-z0-9_]*)\)/;
2982
+ var JS_NAMED_GROUP = /^\(\?<([A-Za-z_][A-Za-z0-9_]*)>/;
2983
+ function translatePythonPattern(pattern) {
2984
+ let flags = "i";
2985
+ let rest = pattern;
2986
+ const m = LEADING_INLINE_FLAGS.exec(rest);
2987
+ if (m) {
2988
+ for (const f of m[1]) if (!flags.includes(f)) flags += f;
2989
+ rest = rest.slice(m[0].length);
2990
+ }
2991
+ const names = /* @__PURE__ */ new Set();
2992
+ const addName = (name) => {
2993
+ if (names.has(name)) throw new Error("a group name occurs two times");
2994
+ names.add(name);
2995
+ };
2996
+ let source = "";
2997
+ let inClass = false;
2998
+ for (let i = 0; i < rest.length; i++) {
2999
+ const c = rest[i];
3000
+ if (c === "\\" && i + 1 < rest.length) {
3001
+ const next = rest[i + 1];
3002
+ if (/[A-Za-z]/.test(next)) {
3003
+ if (!inClass && next === "A") {
3004
+ source += "(?<![\\s\\S])";
3005
+ i++;
3006
+ continue;
3007
+ }
3008
+ if (!inClass && next === "Z") {
3009
+ source += "(?![\\s\\S])";
3010
+ i++;
3011
+ continue;
3012
+ }
3013
+ if (next === "x" || next === "u") {
3014
+ const n = next === "x" ? 2 : 4;
3015
+ const hex = rest.slice(i + 2, i + 2 + n);
3016
+ if (!new RegExp(`^[0-9A-Fa-f]{${n}}$`).test(hex)) throw new Error("short hex escape");
3017
+ source += c + next + hex;
3018
+ i += 1 + n;
3019
+ continue;
3020
+ }
3021
+ if (!(inClass ? SAME_ESCAPES_IN_CLASS : SAME_ESCAPES).includes(next)) {
3022
+ throw new Error("an escape that Python and JavaScript read differently");
3023
+ }
3024
+ }
3025
+ source += c + next;
3026
+ i++;
3027
+ continue;
3028
+ }
3029
+ if (inClass) {
3030
+ if (c === "]") inClass = false;
3031
+ source += c;
3032
+ continue;
3033
+ }
3034
+ if (c === "[") {
3035
+ inClass = true;
3036
+ source += c;
3037
+ continue;
3038
+ }
3039
+ const tail = rest.slice(i);
3040
+ let g;
3041
+ if ((g = PY_NAMED_GROUP.exec(tail)) || (g = JS_NAMED_GROUP.exec(tail))) {
3042
+ addName(g[1]);
3043
+ source += `(?<${g[1]}>`;
3044
+ i += g[0].length - 1;
3045
+ continue;
3046
+ }
3047
+ if (g = PY_NAMED_BACKREF.exec(tail)) {
3048
+ source += `\\k<${g[1]}>`;
3049
+ i += g[0].length - 1;
3050
+ continue;
3051
+ }
3052
+ source += c;
3053
+ }
3054
+ return { source, flags };
3055
+ }
3056
+ var warnedGuardPatterns = /* @__PURE__ */ new Set();
3057
+ function warnUnusablePattern(fixId, index) {
3058
+ const key = `${fixId ?? "unknown"}\0${index}`;
3059
+ if (warnedGuardPatterns.has(key)) return;
3060
+ warnedGuardPatterns.add(key);
3061
+ warn(
3062
+ index < 0 ? `Guard fix ${fixId ?? "unknown"}: blocked_patterns is not a list. The guard rejects every call.` : `Guard fix ${fixId ?? "unknown"}: blocked pattern ${index} is not usable (empty, not a string, longer than 500 characters, or it does not compile). The guard rejects every call.`
3063
+ );
3064
+ }
2380
3065
  var FixApplier = class {
2381
3066
  _config;
2382
3067
  _cache;
@@ -2634,6 +3319,26 @@ var FixApplier = class {
2634
3319
  * - content_filter: Regex-based blocked patterns
2635
3320
  * - format_check: JSON.parse() validity
2636
3321
  * - input_validation / output_validation: min/max length
3322
+ *
3323
+ * `content_filter` searches the content for each entry of `blocked_patterns`
3324
+ * (case-insensitive, after `translatePythonPattern`). It fails CLOSED: when
3325
+ * `blocked_patterns` is not a list, or an entry is empty, is not a string,
3326
+ * is longer than 500 characters or cannot be used, the guard does not pass.
3327
+ * One WARNING names the fix id and the pattern index, once for each pair in
3328
+ * the process. The reason names the pattern index, never the pattern or the
3329
+ * content.
3330
+ *
3331
+ * Limits:
3332
+ * - The interceptor runs only on the OpenAI chat patch (input guards) and on
3333
+ * `interceptCall` / `interceptResponse`. It does not run when a framework
3334
+ * integration suppresses provider instrumentation.
3335
+ * - Each SDK uses its own regular-expression engine. These constructs can
3336
+ * give a different result in JavaScript and Python: `$` before a final
3337
+ * newline, Unicode case folding, `\d` and `\w` on non-ASCII text,
3338
+ * `\N{...}` and `\U........` (JavaScript fails closed), and a `]` first
3339
+ * in a class.
3340
+ * - A pattern has no time bound. A pattern with nested repeats can take a
3341
+ * very long time on a long input.
2637
3342
  */
2638
3343
  applyGuardFix(fix, content, isInput = true) {
2639
3344
  try {
@@ -2654,13 +3359,25 @@ var FixApplier = class {
2654
3359
  let passed = true;
2655
3360
  let reason;
2656
3361
  if (guardType === "content_filter") {
2657
- const blockedPatterns = guardConfig.blocked_patterns ?? guardConfig.blockedPatterns ?? [];
2658
- for (const pattern of blockedPatterns) {
2659
- const regex = this._safeCompileRegex(pattern);
2660
- if (regex && regex.test(content)) {
2661
- passed = false;
2662
- reason = `blocked pattern matched: ${pattern.slice(0, 80)}`;
2663
- break;
3362
+ const blockedPatterns = "blocked_patterns" in guardConfig ? guardConfig.blocked_patterns : "blockedPatterns" in guardConfig ? guardConfig.blockedPatterns : [];
3363
+ if (!Array.isArray(blockedPatterns)) {
3364
+ warnUnusablePattern(fix.fixId, -1);
3365
+ passed = false;
3366
+ reason = "blocked_patterns is not a list";
3367
+ } else {
3368
+ for (let index = 0; index < blockedPatterns.length; index++) {
3369
+ const regex = this._safeCompileRegex(blockedPatterns[index]);
3370
+ if (!regex) {
3371
+ warnUnusablePattern(fix.fixId, index);
3372
+ passed = false;
3373
+ reason = `blocked pattern ${index} is not usable`;
3374
+ break;
3375
+ }
3376
+ if (regex.test(content)) {
3377
+ passed = false;
3378
+ reason = `blocked pattern matched (pattern ${index})`;
3379
+ break;
3380
+ }
2664
3381
  }
2665
3382
  }
2666
3383
  } else if (guardType === "format_check") {
@@ -2721,11 +3438,17 @@ var FixApplier = class {
2721
3438
  /**
2722
3439
  * Safely compile a regex pattern from untrusted input.
2723
3440
  * Returns null if invalid or too long.
3441
+ *
3442
+ * F-029: the fix patterns are written for Python `re`. Before it compiles
3443
+ * the pattern, this function translates the Python forms that have an exact
3444
+ * JavaScript equivalent (see `translatePythonPattern`). The `i` flag is
3445
+ * always on, as in the Python applier.
2724
3446
  */
2725
3447
  _safeCompileRegex(pattern, maxLength = 500) {
2726
- if (!pattern || pattern.length > maxLength) return null;
3448
+ if (typeof pattern !== "string" || !pattern || pattern.length > maxLength) return null;
2727
3449
  try {
2728
- return new RegExp(pattern, "i");
3450
+ const { source, flags } = translatePythonPattern(pattern);
3451
+ return new RegExp(source, flags);
2729
3452
  } catch {
2730
3453
  debug(`Invalid regex pattern (length=${pattern.length})`);
2731
3454
  return null;
@@ -2801,6 +3524,8 @@ var FixLoader = class _FixLoader {
2801
3524
  _consecutiveFailures = 0;
2802
3525
  _circuitOpenUntil = 0;
2803
3526
  _onLoadCallbacks = [];
3527
+ // The HTTP status of the last load that got a non-2xx answer; null otherwise.
3528
+ _lastStatus = null;
2804
3529
  // Circuit breaker settings
2805
3530
  static CIRCUIT_BREAKER_THRESHOLD = 3;
2806
3531
  static CIRCUIT_BREAKER_COOLDOWN_MS = 3e4;
@@ -2808,6 +3533,43 @@ var FixLoader = class _FixLoader {
2808
3533
  this._config = resolveFixRuntimeConfig(config);
2809
3534
  this._cache = cache;
2810
3535
  }
3536
+ /** Error text for a log line: one line, the api endpoint as `safeUrl` gives it, no key. */
3537
+ _scrub(error) {
3538
+ let out;
3539
+ try {
3540
+ out = error instanceof Error ? `${error.name}: ${error.message}` : String(error);
3541
+ } catch {
3542
+ out = typeof error;
3543
+ }
3544
+ const endpoint = (this._config.apiEndpoint ?? "").replace(/\/+$/, "");
3545
+ if (endpoint && safeUrl(endpoint) !== endpoint) out = out.split(endpoint).join(safeUrl(endpoint));
3546
+ return scrubText(out, [this._config.apiKey]);
3547
+ }
3548
+ /**
3549
+ * The WARNING for a failed first load: the request URL with no userinfo, no
3550
+ * query and no key, the HTTP status or the error class, and that tracing is
3551
+ * not affected. Never the error text: it can echo the URL with its query.
3552
+ */
3553
+ _firstLoadWarning(error) {
3554
+ let cause;
3555
+ if (this._lastStatus !== null) {
3556
+ cause = `HTTP ${this._lastStatus}`;
3557
+ } else {
3558
+ cause = error instanceof Error ? error.name : typeof error;
3559
+ const code = error?.cause?.code;
3560
+ if (typeof code === "string" && /^[A-Z][A-Z0-9_]*$/.test(code)) cause += ` (${code})`;
3561
+ }
3562
+ const endpoint = (this._config.apiEndpoint ?? "").replace(/\/+$/, "");
3563
+ return scrubText(
3564
+ `The first load of the fixes from ${safeUrl(endpoint)}/v1/fixes/active failed (${cause}). No fix is applied until a later load succeeds. Tracing is not affected.`,
3565
+ [this._config.apiKey],
3566
+ 500
3567
+ );
3568
+ }
3569
+ /** Hide the key from `util.inspect` and `console.log` of the loader. */
3570
+ [/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")]() {
3571
+ return "FixLoader {}";
3572
+ }
2811
3573
  /** Timestamp of last successful load. */
2812
3574
  get lastLoadTime() {
2813
3575
  return this._lastLoadTime;
@@ -2828,13 +3590,13 @@ var FixLoader = class _FixLoader {
2828
3590
  return;
2829
3591
  }
2830
3592
  this.loadSync().catch((e) => {
2831
- debug(`Initial fix load failed: ${e}`);
3593
+ warn(this._firstLoadWarning(e));
2832
3594
  });
2833
3595
  if (this._config.autoRefresh) {
2834
3596
  this._startRefreshInterval();
2835
3597
  }
2836
3598
  } catch (e) {
2837
- debug(`FixLoader.start error: ${e}`);
3599
+ debug(`FixLoader.start error: ${this._scrub(e)}`);
2838
3600
  }
2839
3601
  }
2840
3602
  /** Stop the loader: clear the refresh interval. */
@@ -2845,7 +3607,7 @@ var FixLoader = class _FixLoader {
2845
3607
  this._refreshTimer = null;
2846
3608
  }
2847
3609
  } catch (e) {
2848
- debug(`FixLoader.stop error: ${e}`);
3610
+ debug(`FixLoader.stop error: ${this._scrub(e)}`);
2849
3611
  }
2850
3612
  }
2851
3613
  /**
@@ -2869,6 +3631,7 @@ var FixLoader = class _FixLoader {
2869
3631
  debug("Fix loader circuit breaker open \u2014 skipping load");
2870
3632
  return this._cache.getAll();
2871
3633
  }
3634
+ this._lastStatus = null;
2872
3635
  try {
2873
3636
  const endpoint = this._config.apiEndpoint.replace(/\/+$/, "");
2874
3637
  const url = `${endpoint}/v1/fixes/active`;
@@ -2888,6 +3651,7 @@ var FixLoader = class _FixLoader {
2888
3651
  });
2889
3652
  clearTimeout(timeoutId);
2890
3653
  if (!response.ok) {
3654
+ this._lastStatus = response.status;
2891
3655
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
2892
3656
  }
2893
3657
  const data = await response.json();
@@ -2914,7 +3678,7 @@ var FixLoader = class _FixLoader {
2914
3678
  fixes.push(activeFixFromApiResponse(item));
2915
3679
  }
2916
3680
  } catch (e) {
2917
- debug(`Failed to parse fix item: ${e}`);
3681
+ debug(`Failed to parse fix item: ${this._scrub(e)}`);
2918
3682
  }
2919
3683
  }
2920
3684
  return fixes;
@@ -2927,7 +3691,7 @@ var FixLoader = class _FixLoader {
2927
3691
  try {
2928
3692
  callback(fixes);
2929
3693
  } catch (e) {
2930
- debug(`Load callback error: ${e}`);
3694
+ debug(`Load callback error: ${this._scrub(e)}`);
2931
3695
  }
2932
3696
  }
2933
3697
  debug(`Loaded ${fixes.length} active fixes`);
@@ -2941,14 +3705,14 @@ var FixLoader = class _FixLoader {
2941
3705
  );
2942
3706
  } else {
2943
3707
  debug(
2944
- `Fix load failed (attempt ${this._consecutiveFailures}): ${error.message}`
3708
+ `Fix load failed (attempt ${this._consecutiveFailures}): ${this._scrub(error)}`
2945
3709
  );
2946
3710
  }
2947
3711
  }
2948
3712
  _startRefreshInterval() {
2949
3713
  const tick = () => {
2950
3714
  this.loadSync().catch((e) => {
2951
- debug(`Background fix refresh failed: ${e}`);
3715
+ debug(`Background fix refresh failed: ${this._scrub(e)}`);
2952
3716
  });
2953
3717
  };
2954
3718
  this._refreshTimer = setInterval(
@@ -3216,6 +3980,78 @@ var TERMINATING_SIGNALS = [
3216
3980
  ];
3217
3981
  var BACKSTOP_MARGIN_MS = 500;
3218
3982
  var _finalMetrics;
3983
+ var SCORE_TIMEOUT_MS = 2e3;
3984
+ var _pendingScores = /* @__PURE__ */ new Set();
3985
+ var _failedScores = 0;
3986
+ var _failedScoresFlushMark = 0;
3987
+ var SCORE_MAX_IN_FLIGHT = 50;
3988
+ var SCORE_MAX_WAITING = 1e3;
3989
+ var SCORE_CAP_REASON = `not sent: ${SCORE_MAX_WAITING} scores wait for a free slot`;
3990
+ var SCORE_DEADLINE_REASON = "not sent before the deadline";
3991
+ var _scoresInFlight = 0;
3992
+ var _scoreWaiters = [];
3993
+ function _startWaitingScores() {
3994
+ while (_scoresInFlight < SCORE_MAX_IN_FLIGHT && _scoreWaiters.length > 0) {
3995
+ _scoreWaiters.shift().start();
3996
+ }
3997
+ }
3998
+ function _failWaitingScores(reason) {
3999
+ for (const waiter of _scoreWaiters.splice(0)) waiter.fail(reason);
4000
+ }
4001
+ var _scoreWarnWindowMs = 1e4;
4002
+ var SCORE_NAME_MAX = 200;
4003
+ var _scoreWarnedAt;
4004
+ var _scoreHeld = 0;
4005
+ var _scoreHeldReason = "";
4006
+ function _resetScoreFailures() {
4007
+ _failedScores = 0;
4008
+ _failedScoresFlushMark = 0;
4009
+ _scoreWarnedAt = void 0;
4010
+ _scoreHeld = 0;
4011
+ _scoreHeldReason = "";
4012
+ }
4013
+ function _heldText() {
4014
+ return ` ${_scoreHeld} more scores failed after the last warning. The last reason: ${_scoreHeldReason}.`;
4015
+ }
4016
+ function _scoreFailed(name, reason) {
4017
+ _failedScores += 1;
4018
+ const now = Date.now();
4019
+ if (_scoreWarnedAt !== void 0 && now - _scoreWarnedAt < _scoreWarnWindowMs) {
4020
+ _scoreHeld += 1;
4021
+ _scoreHeldReason = reason;
4022
+ return;
4023
+ }
4024
+ let message = `score ${JSON.stringify(name.slice(0, SCORE_NAME_MAX))} failed: ${reason}.`;
4025
+ if (_scoreHeld > 0) message += _heldText();
4026
+ _scoreWarnedAt = now;
4027
+ _scoreHeld = 0;
4028
+ _scoreHeldReason = "";
4029
+ warn(message);
4030
+ }
4031
+ function _warnHeldScores() {
4032
+ if (_scoreHeld === 0) return;
4033
+ const message = _heldText().trimStart();
4034
+ _scoreWarnedAt = Date.now();
4035
+ _scoreHeld = 0;
4036
+ _scoreHeldReason = "";
4037
+ warn(message);
4038
+ }
4039
+ async function _waitForPendingScores(timeoutMs) {
4040
+ if (_pendingScores.size === 0) return true;
4041
+ let timer;
4042
+ const expired = new Promise((resolve) => {
4043
+ timer = setTimeout(() => resolve(false), Math.max(0, timeoutMs));
4044
+ });
4045
+ const settled = (async () => {
4046
+ while (_pendingScores.size > 0) await Promise.all([..._pendingScores]);
4047
+ return true;
4048
+ })();
4049
+ try {
4050
+ return await Promise.race([settled, expired]);
4051
+ } finally {
4052
+ clearTimeout(timer);
4053
+ }
4054
+ }
3219
4055
  var RisicareClient = class {
3220
4056
  config;
3221
4057
  processor;
@@ -3228,8 +4064,17 @@ var RisicareClient = class {
3228
4064
  _shutdownHandlers = [];
3229
4065
  constructor(config) {
3230
4066
  this.config = resolveConfig(config);
3231
- if (this.config.apiKey && !this.config.apiKey.startsWith("rsk-")) {
3232
- debug('Warning: API key should start with "rsk-". Got: ' + this.config.apiKey.slice(0, 4) + "...");
4067
+ const hasKnownPrefix = this.config.apiKey?.startsWith("rsk-") || this.config.apiKey?.startsWith("rski_") || this.config.apiKey?.startsWith("rsks_");
4068
+ if (this.config.apiKey && !hasKnownPrefix) {
4069
+ warn(
4070
+ 'the API key does not look like a Risicare key: it should start with "rsk-", "rski_" or "rsks_". Tracing continues with this key.'
4071
+ );
4072
+ }
4073
+ const tracingSetExplicitly = config?.enabled !== void 0 || (process.env.RISICARE_TRACING ?? "").trim() !== "";
4074
+ if (!this.config.apiKey && !this.config.enabled && !tracingSetExplicitly) {
4075
+ warn(
4076
+ "Risicare found no API key. Tracing is off and the SDK sends no span. Pass apiKey to init() or set RISICARE_API_KEY."
4077
+ );
3233
4078
  }
3234
4079
  let exporter;
3235
4080
  if (this.config.debug && !this.config.apiKey) {
@@ -3293,21 +4138,23 @@ var RisicareClient = class {
3293
4138
  });
3294
4139
  } catch (e) {
3295
4140
  const errName = e instanceof Error ? e.name : "Error";
3296
- const errMsg = e instanceof Error ? e.message : String(e);
4141
+ const errMsg = scrubText(e instanceof Error ? e.message : String(e), [this.config.apiKey]);
3297
4142
  try {
3298
4143
  console.warn(
3299
- `[risicare] FixRuntime initialization failed \u2014 auto-fix is OFFLINE for this process. endpoint=${this.config.endpoint} error=${errName}: ${errMsg}. Tracing + reporting still work; only fix application is disabled. Pass debug:true in init() options for the full traceback.`
4144
+ `[risicare] FixRuntime initialization failed \u2014 auto-fix is OFFLINE for this process. apiEndpoint=${safeUrl(this.config.apiEndpoint)} error=${errName}: ${errMsg}. Tracing + reporting still work; only fix application is disabled. Pass debug:true in init() options for the full traceback.`
3300
4145
  );
3301
4146
  } catch {
3302
4147
  }
3303
4148
  if (this.config.debug) {
3304
- debug(`FixRuntime init traceback: ${e instanceof Error && e.stack ? e.stack : String(e)}`);
4149
+ debug(
4150
+ `FixRuntime init traceback: ${scrubText(e instanceof Error && e.stack ? e.stack : String(e), [this.config.apiKey], 2e3)}`
4151
+ );
3305
4152
  }
3306
4153
  }
3307
4154
  }
3308
4155
  this._registerShutdownHooks();
3309
4156
  setDebug(this.config.debug);
3310
- debug(`Initialized: enabled=${this.config.enabled}, endpoint=${this.config.endpoint}`);
4157
+ debug(`Initialized: enabled=${this.config.enabled}, endpoint=${safeUrl(this.config.endpoint)}`);
3311
4158
  if (!this.config.fixRuntime) {
3312
4159
  debug(
3313
4160
  "FixRuntime is off: nothing polls /fixes/active and no fix is applied. Opt in with fixRuntime: true or RISICARE_FIX_RUNTIME=true."
@@ -3321,18 +4168,20 @@ var RisicareClient = class {
3321
4168
  this.tracer.enabled = value;
3322
4169
  }
3323
4170
  // Audit #6: Promise-based shutdown dedup (fixes TOCTOU race condition)
3324
- async shutdown() {
4171
+ async shutdown(timeoutMs) {
3325
4172
  if (this._shutdownPromise) return this._shutdownPromise;
3326
- this._shutdownPromise = this._doShutdown();
4173
+ this._shutdownPromise = this._doShutdown(timeoutMs);
3327
4174
  return this._shutdownPromise;
3328
4175
  }
3329
- async _doShutdown() {
4176
+ async _doShutdown(timeoutMs) {
3330
4177
  debug("Shutting down...");
3331
4178
  for (const { signal, handler } of this._shutdownHandlers) {
3332
4179
  process.removeListener(signal, handler);
3333
4180
  }
3334
4181
  this._shutdownHandlers = [];
3335
- await this.processor.shutdown();
4182
+ endOpenProviderSpans();
4183
+ await this.processor.shutdown(timeoutMs);
4184
+ _failWaitingScores(SCORE_DEADLINE_REASON);
3336
4185
  this.isShutdown = true;
3337
4186
  _finalMetrics = this.processor.getMetrics();
3338
4187
  }
@@ -3347,8 +4196,8 @@ var RisicareClient = class {
3347
4196
  * false one — both leave the customer unable to tell *drained* from
3348
4197
  * *discarded*.
3349
4198
  */
3350
- async flush() {
3351
- return this.processor.flush();
4199
+ async flush(timeoutMs) {
4200
+ return this.processor.flush(timeoutMs);
3352
4201
  }
3353
4202
  /**
3354
4203
  * SDK-RELEASE-RULING.md §3 — who decides whether the process dies.
@@ -3446,25 +4295,39 @@ function init(config) {
3446
4295
  return;
3447
4296
  }
3448
4297
  _finalMetrics = void 0;
4298
+ _resetScoreFailures();
3449
4299
  const client = new RisicareClient(config);
3450
4300
  setClient(client);
3451
4301
  setTracer(client.tracer);
3452
4302
  }
3453
- async function shutdown() {
4303
+ async function shutdown(timeoutMs) {
3454
4304
  try {
3455
4305
  shutdownFixRuntime();
3456
4306
  } catch {
3457
4307
  }
3458
4308
  const client = getClient();
3459
4309
  if (!client) return;
3460
- await client.shutdown();
4310
+ await client.shutdown(timeoutMs);
3461
4311
  setClient(void 0);
3462
4312
  setTracer(void 0);
3463
4313
  }
3464
- async function flush() {
4314
+ async function flush(timeoutMs) {
4315
+ const budgetMs = normalizeTimeoutMs(timeoutMs);
4316
+ const deadline = Date.now() + budgetMs;
4317
+ const scoreMark = _failedScoresFlushMark;
3465
4318
  const client = getClient();
3466
- if (!client) return true;
3467
- return client.flush();
4319
+ let spansOk;
4320
+ if (!client) {
4321
+ spansOk = (_finalMetrics?.droppedSpansByReason.shutdown_residue ?? 0) === 0;
4322
+ } else {
4323
+ spansOk = await client.flush(timeoutMs);
4324
+ }
4325
+ const scoresDone = await _waitForPendingScores(deadline - Date.now());
4326
+ if (!scoresDone) _failWaitingScores(SCORE_DEADLINE_REASON);
4327
+ _warnHeldScores();
4328
+ const failedNow = _failedScores;
4329
+ if (failedNow > _failedScoresFlushMark) _failedScoresFlushMark = failedNow;
4330
+ return spansOk && scoresDone && failedNow <= scoreMark;
3468
4331
  }
3469
4332
  function enable() {
3470
4333
  const client = getClient();
@@ -3488,22 +4351,30 @@ function getTraceContent() {
3488
4351
  function getMetrics() {
3489
4352
  const client = getClient();
3490
4353
  if (client && !client.isShutdown) {
3491
- return { ...client.processor.getMetrics(), sdkState: "running" };
4354
+ return {
4355
+ ...client.processor.getMetrics(),
4356
+ failedScores: _failedScores,
4357
+ sdkState: "running"
4358
+ };
3492
4359
  }
3493
4360
  if (_finalMetrics) {
3494
4361
  return {
3495
4362
  ..._finalMetrics,
4363
+ failedScores: _failedScores,
3496
4364
  droppedSpansByReason: { ..._finalMetrics.droppedSpansByReason },
3497
4365
  sdkState: "shutdown"
3498
4366
  };
3499
4367
  }
3500
4368
  return {
3501
4369
  exportedSpans: 0,
4370
+ rejectedSpans: 0,
4371
+ failedScores: _failedScores,
3502
4372
  droppedSpans: 0,
3503
4373
  droppedSpansByReason: {
3504
4374
  queue_full: 0,
3505
4375
  transport_refused: 0,
3506
- shutdown_residue: 0
4376
+ shutdown_residue: 0,
4377
+ unacknowledged: 0
3507
4378
  },
3508
4379
  failedExports: 0,
3509
4380
  queueSize: 0,
@@ -3548,8 +4419,8 @@ function reportError(error, options) {
3548
4419
  span.setStatus("error" /* ERROR */, err.message);
3549
4420
  span.setAttribute("error", true);
3550
4421
  span.setAttribute("error.type", err.constructor.name);
3551
- span.setAttribute("error.message", err.message.slice(0, 2e3));
3552
- if (err.stack) span.setAttribute("error.stack", err.stack.slice(0, 4e3));
4422
+ span.setAttribute("error.message", cutCodePoints(err.message, 2e3));
4423
+ if (err.stack) span.setAttribute("error.stack", cutCodePoints(err.stack, 4e3));
3553
4424
  span.setAttribute("risicare.reported_error", true);
3554
4425
  attachErrorCode(span, err);
3555
4426
  if (options?.attributes) {
@@ -3564,12 +4435,22 @@ function reportError(error, options) {
3564
4435
  }
3565
4436
  function score(traceId, name, value, options) {
3566
4437
  try {
3567
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1) {
3568
- debug(`score: value must be in [0.0, 1.0], got ${value}. Score not sent.`);
4438
+ const raw = value;
4439
+ if (typeof raw === "boolean") value = raw ? 1 : 0;
4440
+ if (typeof value !== "number") {
4441
+ warn(`score: the value must be a number, not ${typeof raw}. The SDK does not send this score.`);
4442
+ return;
4443
+ }
4444
+ if (!Number.isFinite(value) || value < 0 || value > 1) {
4445
+ warn(`score: the value must be in [0.0, 1.0], not ${value}. The SDK does not send this score.`);
3569
4446
  return;
3570
4447
  }
3571
- if (!traceId || !name) {
3572
- debug("score: traceId and name are required");
4448
+ if (typeof traceId !== "string" || given(traceId) === void 0) {
4449
+ warn("score: traceId must be a non-empty string. The SDK does not send this score.");
4450
+ return;
4451
+ }
4452
+ if (typeof name !== "string" || given(name) === void 0) {
4453
+ warn("score: name must be a non-empty string. The SDK does not send this score.");
3573
4454
  return;
3574
4455
  }
3575
4456
  const client = getClient();
@@ -3588,21 +4469,71 @@ function score(traceId, name, value, options) {
3588
4469
  ...options?.spanId && { span_id: options.spanId },
3589
4470
  ...comment && { comment }
3590
4471
  });
3591
- fetch(url, {
3592
- method: "POST",
3593
- headers: {
3594
- "Content-Type": "application/json",
3595
- "Authorization": `Bearer ${client.config.apiKey}`,
3596
- // F-SDKVER-001 — see the Python twin. `score()` reaches the FastAPI
3597
- // server under /v1/, which `main.py::audit_logging_middleware` audits
3598
- // into `audit_logs.user_agent`; ingest reaches the Rust gateway, which
3599
- // discards it. This is the one client-identity visible with no gateway
3600
- // change.
3601
- "User-Agent": `risicare-js/${SDK_VERSION} node/${process.version}`,
3602
- "X-Risicare-SDK-Version": SDK_VERSION
3603
- },
3604
- body
3605
- }).catch((err) => debug(`score: send failed: ${err}`));
4472
+ if (_scoresInFlight >= SCORE_MAX_IN_FLIGHT && _scoreWaiters.length >= SCORE_MAX_WAITING) {
4473
+ _scoreFailed(name, SCORE_CAP_REASON);
4474
+ return;
4475
+ }
4476
+ let settle;
4477
+ const request = new Promise((resolve) => settle = resolve);
4478
+ const send = async () => {
4479
+ const controller = new AbortController();
4480
+ const timer = setTimeout(() => controller.abort(), SCORE_TIMEOUT_MS);
4481
+ try {
4482
+ const response = await fetch(url, {
4483
+ method: "POST",
4484
+ headers: {
4485
+ "Content-Type": "application/json",
4486
+ "Authorization": `Bearer ${client.config.apiKey}`,
4487
+ // F-SDKVER-001 — see the Python twin. `score()` reaches the FastAPI
4488
+ // server under /v1/, which `main.py::audit_logging_middleware` audits
4489
+ // into `audit_logs.user_agent`; ingest reaches the Rust gateway, which
4490
+ // discards it. This is the one client-identity visible with no gateway
4491
+ // change.
4492
+ "User-Agent": `risicare-js/${SDK_VERSION} node/${process.version}`,
4493
+ "X-Risicare-SDK-Version": SDK_VERSION
4494
+ },
4495
+ body,
4496
+ // Never follow: a redirect is a misroute, not a route. Through 0.7.1
4497
+ // fetch followed a 307 and re-POSTed the score to the login page.
4498
+ redirect: "manual",
4499
+ signal: controller.signal
4500
+ });
4501
+ if (response.type === "opaqueredirect" || response.status >= 300) {
4502
+ _scoreFailed(name, `HTTP ${response.status || "3xx"}`);
4503
+ }
4504
+ try {
4505
+ await response.body?.cancel();
4506
+ } catch {
4507
+ }
4508
+ } catch (err) {
4509
+ const cause = err instanceof Error ? err.cause : void 0;
4510
+ const code = typeof cause?.code === "string" ? cause.code : void 0;
4511
+ const reason = controller.signal.aborted ? `no answer within ${SCORE_TIMEOUT_MS} ms` : code ?? (err instanceof Error ? err.name : "Error");
4512
+ _scoreFailed(name, reason);
4513
+ } finally {
4514
+ clearTimeout(timer);
4515
+ _scoresInFlight--;
4516
+ settle();
4517
+ _startWaitingScores();
4518
+ }
4519
+ };
4520
+ const start = () => {
4521
+ _scoresInFlight++;
4522
+ void send();
4523
+ };
4524
+ if (_scoresInFlight < SCORE_MAX_IN_FLIGHT) {
4525
+ start();
4526
+ } else {
4527
+ _scoreWaiters.push({
4528
+ start,
4529
+ fail: (reason) => {
4530
+ _scoreFailed(name, reason);
4531
+ settle();
4532
+ }
4533
+ });
4534
+ }
4535
+ _pendingScores.add(request);
4536
+ void request.finally(() => _pendingScores.delete(request));
3606
4537
  } catch {
3607
4538
  debug("score failed");
3608
4539
  }
@@ -3631,9 +4562,9 @@ var LIFECYCLE_STATUS = "risicare.lifecycle.status";
3631
4562
  var STATUS_MESSAGE_MAX = 500;
3632
4563
  function excSummary(error) {
3633
4564
  if (error instanceof Error) {
3634
- return `${error.constructor.name}: ${error.message}`.slice(0, STATUS_MESSAGE_MAX);
4565
+ return cutCodePoints(`${error.constructor.name}: ${error.message}`, STATUS_MESSAGE_MAX);
3635
4566
  }
3636
- return String(error).slice(0, STATUS_MESSAGE_MAX);
4567
+ return cutCodePoints(String(error), STATUS_MESSAGE_MAX);
3637
4568
  }
3638
4569
  function stampEndMarker(span, entity, error) {
3639
4570
  try {
@@ -3720,9 +4651,10 @@ function withSessionEndMarker(body) {
3720
4651
 
3721
4652
  // src/decorators/agent.ts
3722
4653
  function _wrapAgent(options, fn) {
3723
- const spanName = `agent:${options.name ?? "agent"}`;
3724
- const agentId = options.agentId ?? generateAgentId(options.name ?? "agent");
3725
- const boundOptions = { ...options, agentId };
4654
+ const agentName = options.name ?? (fn.name.replace(/^(bound )+/, "") || "agent");
4655
+ const spanName = `agent:${agentName}`;
4656
+ const agentId = options.agentId ?? generateAgentId(agentName);
4657
+ const boundOptions = { ...options, name: agentName, agentId };
3726
4658
  return (...args) => {
3727
4659
  const tracer = getTracer2();
3728
4660
  if (!tracer) {
@@ -3775,6 +4707,10 @@ function withPhase(phase, fn) {
3775
4707
  }
3776
4708
 
3777
4709
  // src/decorators/phase.ts
4710
+ function bareName(phase, fn) {
4711
+ const fnName = typeof fn === "function" ? fn.name.replace(/^(bound )+/, "") : "";
4712
+ return fnName ? `${phase}:${fnName}` : `phase:${phase}`;
4713
+ }
3778
4714
  function phaseWrapper(phase, kind, name, fn) {
3779
4715
  return (...args) => {
3780
4716
  const tracer = getTracer2();
@@ -3790,22 +4726,22 @@ function phaseWrapper(phase, kind, name, fn) {
3790
4726
  };
3791
4727
  }
3792
4728
  function traceThink(fnOrName, maybeFn) {
3793
- const name = typeof fnOrName === "string" ? fnOrName : `phase:think`;
4729
+ const name = typeof fnOrName === "string" ? fnOrName : bareName("think", fnOrName);
3794
4730
  const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
3795
4731
  return phaseWrapper("think" /* THINK */, "think" /* THINK */, name, fn);
3796
4732
  }
3797
4733
  function traceDecide(fnOrName, maybeFn) {
3798
- const name = typeof fnOrName === "string" ? fnOrName : `phase:decide`;
4734
+ const name = typeof fnOrName === "string" ? fnOrName : bareName("decide", fnOrName);
3799
4735
  const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
3800
4736
  return phaseWrapper("decide" /* DECIDE */, "decide" /* DECIDE */, name, fn);
3801
4737
  }
3802
4738
  function traceAct(fnOrName, maybeFn) {
3803
- const name = typeof fnOrName === "string" ? fnOrName : `phase:act`;
4739
+ const name = typeof fnOrName === "string" ? fnOrName : bareName("act", fnOrName);
3804
4740
  const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
3805
4741
  return phaseWrapper("act" /* ACT */, "tool_call" /* TOOL_CALL */, name, fn);
3806
4742
  }
3807
4743
  function traceObserve(fnOrName, maybeFn) {
3808
- const name = typeof fnOrName === "string" ? fnOrName : `phase:observe`;
4744
+ const name = typeof fnOrName === "string" ? fnOrName : bareName("observe", fnOrName);
3809
4745
  const fn = typeof fnOrName === "function" ? fnOrName : maybeFn;
3810
4746
  return phaseWrapper("observe" /* OBSERVE */, "observe" /* OBSERVE */, name, fn);
3811
4747
  }
@@ -3894,7 +4830,7 @@ function getTraceContext() {
3894
4830
  traceId = ctx._rootTraceId;
3895
4831
  } else {
3896
4832
  traceId = generateTraceId();
3897
- if (ctx.session || ctx.agent) {
4833
+ if (hasContextScope()) {
3898
4834
  ctx._rootTraceId = traceId;
3899
4835
  }
3900
4836
  }
@@ -3931,7 +4867,8 @@ function extractTraceContext(headers) {
3931
4867
  const result = {};
3932
4868
  const normalizedHeaders = {};
3933
4869
  for (const [key, value] of Object.entries(headers)) {
3934
- normalizedHeaders[key.toLowerCase()] = value;
4870
+ const first = Array.isArray(value) ? value[0] : value;
4871
+ if (typeof first === "string") normalizedHeaders[key.toLowerCase()] = first;
3935
4872
  }
3936
4873
  const traceparent = normalizedHeaders[TRACEPARENT_HEADER] ?? "";
3937
4874
  if (traceparent) {