@bitfab/sdk 0.23.3 → 0.24.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.
package/dist/node.cjs CHANGED
@@ -91,6 +91,54 @@ var init_errors = __esm({
91
91
  }
92
92
  });
93
93
 
94
+ // src/warnOnce.ts
95
+ function warnOnce(key, message) {
96
+ if (warned.has(key)) {
97
+ return;
98
+ }
99
+ warned.add(key);
100
+ try {
101
+ console.warn(`[bitfab] ${message}`);
102
+ } catch {
103
+ }
104
+ }
105
+ var warned;
106
+ var init_warnOnce = __esm({
107
+ "src/warnOnce.ts"() {
108
+ "use strict";
109
+ warned = /* @__PURE__ */ new Set();
110
+ }
111
+ });
112
+
113
+ // src/randomUuid.ts
114
+ function randomUuid() {
115
+ const globalCrypto = globalThis.crypto;
116
+ if (typeof globalCrypto?.randomUUID === "function") {
117
+ try {
118
+ return globalCrypto.randomUUID();
119
+ } catch {
120
+ }
121
+ }
122
+ warnOnce(
123
+ "crypto-unavailable",
124
+ "global crypto.randomUUID is unavailable; using a non-cryptographic fallback for trace/span ids. Tracing works normally (ids are correlation-only, not security-sensitive)."
125
+ );
126
+ return fallbackUuidV4();
127
+ }
128
+ function fallbackUuidV4() {
129
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (char) => {
130
+ const rand = Math.random() * 16 | 0;
131
+ const value = char === "x" ? rand : rand & 3 | 8;
132
+ return value.toString(16);
133
+ });
134
+ }
135
+ var init_randomUuid = __esm({
136
+ "src/randomUuid.ts"() {
137
+ "use strict";
138
+ init_warnOnce();
139
+ }
140
+ });
141
+
94
142
  // src/serialize.ts
95
143
  function describeValue(value) {
96
144
  try {
@@ -103,6 +151,10 @@ function describeValue(value) {
103
151
  return typeof value;
104
152
  }
105
153
  function unserializableStub(value, reason) {
154
+ warnOnce(
155
+ `serialize:${reason.replace(/\d+/g, "N")}`,
156
+ `a value could not be fully serialized for a span (${reason}); it was replaced with a placeholder. The span still ships, but its captured input/output is incomplete.`
157
+ );
106
158
  let summary;
107
159
  try {
108
160
  summary = `<unserializable: ${describeValue(value)} (${reason})>`;
@@ -142,7 +194,19 @@ function deserializeValue(serialized) {
142
194
  });
143
195
  }
144
196
  function toJsonSafe(value) {
145
- return toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
197
+ const safe = toJsonSafeInner(value, 0, /* @__PURE__ */ new WeakSet());
198
+ try {
199
+ const size = JSON.stringify(safe)?.length ?? 0;
200
+ if (size > MAX_FRAMEWORK_SERIALIZED_BYTES) {
201
+ warnOnce(
202
+ "toJsonSafe:too_large",
203
+ `a framework payload exceeded ${MAX_FRAMEWORK_SERIALIZED_BYTES} bytes and was replaced with a placeholder so the span still ships. The captured state for this span is incomplete.`
204
+ );
205
+ return `<unserializable: too_large_${size}_bytes>`;
206
+ }
207
+ } catch {
208
+ }
209
+ return safe;
146
210
  }
147
211
  function toJsonSafeInner(value, depth, seen) {
148
212
  if (value === null || value === void 0) {
@@ -195,12 +259,14 @@ function toJsonSafeInner(value, depth, seen) {
195
259
  seen.delete(value);
196
260
  return result;
197
261
  }
198
- var import_superjson, MAX_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
262
+ var import_superjson, MAX_SERIALIZED_BYTES, MAX_FRAMEWORK_SERIALIZED_BYTES, MAX_SAFE_DEPTH;
199
263
  var init_serialize = __esm({
200
264
  "src/serialize.ts"() {
201
265
  "use strict";
202
266
  import_superjson = __toESM(require("superjson"), 1);
267
+ init_warnOnce();
203
268
  MAX_SERIALIZED_BYTES = 512e3;
269
+ MAX_FRAMEWORK_SERIALIZED_BYTES = 2e6;
204
270
  MAX_SAFE_DEPTH = 6;
205
271
  }
206
272
  });
@@ -286,7 +352,7 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
286
352
  let originalOutput;
287
353
  let result;
288
354
  let error = null;
289
- const replayedTraceId = crypto.randomUUID();
355
+ const replayedTraceId = randomUuid();
290
356
  const pendingPersistence = [];
291
357
  try {
292
358
  const span = await httpClient.getExternalSpan(serverItem.externalSpanId);
@@ -470,6 +536,7 @@ var init_replay = __esm({
470
536
  "src/replay.ts"() {
471
537
  "use strict";
472
538
  init_errors();
539
+ init_randomUuid();
473
540
  init_replayContext();
474
541
  init_serialize();
475
542
  }
@@ -506,13 +573,24 @@ registerAsyncLocalStorageClass(
506
573
  );
507
574
 
508
575
  // src/version.generated.ts
509
- var __version__ = "0.23.3";
576
+ var __version__ = "0.24.0";
510
577
 
511
578
  // src/constants.ts
512
579
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
513
580
 
514
581
  // src/http.ts
515
582
  init_errors();
583
+
584
+ // src/unrefTimer.ts
585
+ function unrefTimer(timer) {
586
+ const handle = timer;
587
+ if (typeof handle.unref === "function") {
588
+ handle.unref();
589
+ }
590
+ }
591
+
592
+ // src/http.ts
593
+ init_warnOnce();
516
594
  function serializePayloadBody(payload) {
517
595
  try {
518
596
  return { body: JSON.stringify(payload), dropped: [] };
@@ -557,11 +635,20 @@ function serializePayloadBody(payload) {
557
635
  result = `<unserializable: ${className}>`;
558
636
  }
559
637
  } else {
560
- const out = {};
561
- for (const [k, v] of Object.entries(obj)) {
562
- out[k] = sanitize(v, seen);
638
+ try {
639
+ const out = {};
640
+ for (const [k, v] of Object.entries(obj)) {
641
+ out[k] = sanitize(v, seen);
642
+ }
643
+ result = out;
644
+ } catch {
645
+ warnOnce(
646
+ "payload:field-getter-threw",
647
+ "a value with a throwing getter/proxy could not be serialized into a span payload; it was replaced with a placeholder. The span still ships with its other fields intact."
648
+ );
649
+ dropped.push(className);
650
+ result = `<unserializable: ${className}>`;
563
651
  }
564
- result = out;
565
652
  }
566
653
  seen.delete(obj);
567
654
  return result;
@@ -606,10 +693,20 @@ async function flushTraces(timeoutMs = 5e3) {
606
693
  if (pendingTracePromises.size === 0) {
607
694
  return;
608
695
  }
609
- await Promise.race([
610
- Promise.allSettled(Array.from(pendingTracePromises)),
611
- new Promise((resolve) => setTimeout(resolve, timeoutMs))
612
- ]);
696
+ let timer;
697
+ try {
698
+ await Promise.race([
699
+ Promise.allSettled(Array.from(pendingTracePromises)),
700
+ new Promise((resolve) => {
701
+ timer = setTimeout(resolve, timeoutMs);
702
+ unrefTimer(timer);
703
+ })
704
+ ]);
705
+ } finally {
706
+ if (timer) {
707
+ clearTimeout(timer);
708
+ }
709
+ }
613
710
  }
614
711
  if (typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
615
712
  let isFlushing = false;
@@ -919,6 +1016,7 @@ var HttpClient = class {
919
1016
  };
920
1017
 
921
1018
  // src/claudeAgentSdk.ts
1019
+ init_randomUuid();
922
1020
  init_serialize();
923
1021
  function nowIso() {
924
1022
  return (/* @__PURE__ */ new Date()).toISOString();
@@ -1006,7 +1104,7 @@ var BitfabClaudeAgentHandler = class {
1006
1104
  if (this.activeContext) {
1007
1105
  this.traceId = this.activeContext.traceId;
1008
1106
  } else {
1009
- this.traceId = crypto.randomUUID();
1107
+ this.traceId = randomUuid();
1010
1108
  }
1011
1109
  this.traceStartedAt = nowIso();
1012
1110
  return this.traceId;
@@ -1031,7 +1129,7 @@ var BitfabClaudeAgentHandler = class {
1031
1129
  if (this.activeContext !== null) {
1032
1130
  return;
1033
1131
  }
1034
- const spanId = crypto.randomUUID();
1132
+ const spanId = randomUuid();
1035
1133
  this.startSpan(spanId, this.traceFunctionKey, "agent", this.rootInput, null);
1036
1134
  this.rootSpanId = spanId;
1037
1135
  }
@@ -1145,7 +1243,7 @@ var BitfabClaudeAgentHandler = class {
1145
1243
  // ── hook callbacks ───────────────────────────────────────────
1146
1244
  async preToolUseHook(inputData, toolUseId, _context) {
1147
1245
  try {
1148
- const sid = inputData.tool_use_id ?? toolUseId ?? crypto.randomUUID();
1246
+ const sid = inputData.tool_use_id ?? toolUseId ?? randomUuid();
1149
1247
  const toolName = inputData.tool_name ?? "tool";
1150
1248
  const toolInput = inputData.tool_input ?? {};
1151
1249
  const agentId = inputData.agent_id;
@@ -1175,10 +1273,10 @@ var BitfabClaudeAgentHandler = class {
1175
1273
  }
1176
1274
  async subagentStartHook(inputData, _toolUseId, _context) {
1177
1275
  try {
1178
- const agentId = inputData.agent_id ?? crypto.randomUUID();
1276
+ const agentId = inputData.agent_id ?? randomUuid();
1179
1277
  const agentType = inputData.agent_type ?? "subagent";
1180
1278
  const parentId = this.getParentId();
1181
- const spanId = crypto.randomUUID();
1279
+ const spanId = randomUuid();
1182
1280
  this.activeSubagentSpans.set(agentId, spanId);
1183
1281
  this.startSpan(
1184
1282
  spanId,
@@ -1319,7 +1417,7 @@ var BitfabClaudeAgentHandler = class {
1319
1417
  this.flushLlmTurn();
1320
1418
  this.conversationHistory.push(...this.pendingMessages);
1321
1419
  this.pendingMessages = [];
1322
- this.currentLlmSpanId = crypto.randomUUID();
1420
+ this.currentLlmSpanId = randomUuid();
1323
1421
  this.currentLlmMessageId = messageId ?? null;
1324
1422
  this.currentLlmContent = [];
1325
1423
  this.currentLlmModel = inner.model ?? null;
@@ -1763,6 +1861,7 @@ function buildSnapshotRef(config, sdkWallClockBeforeFn) {
1763
1861
  }
1764
1862
 
1765
1863
  // src/langgraph.ts
1864
+ init_randomUuid();
1766
1865
  init_serialize();
1767
1866
  var LANGSMITH_HIDDEN_TAG = "langsmith:hidden";
1768
1867
  var LANGGRAPH_METADATA_KEYS = [
@@ -2011,7 +2110,7 @@ var BitfabLangGraphCallbackHandler = class {
2011
2110
  } else {
2012
2111
  const activeContext = this.getActiveSpanContext?.() ?? null;
2013
2112
  invocation = {
2014
- traceId: activeContext ? activeContext.traceId : crypto.randomUUID(),
2113
+ traceId: activeContext ? activeContext.traceId : randomUuid(),
2015
2114
  activeContext,
2016
2115
  rootRunId: runId
2017
2116
  };
@@ -2306,6 +2405,20 @@ var BitfabOpenAIAgentHandler = class {
2306
2405
  this.withSpanFn = config.withSpan;
2307
2406
  this.getActiveSpanContext = config.getActiveSpanContext;
2308
2407
  }
2408
+ /**
2409
+ * Drop-in replacement for the OpenAI Agents SDK's `run()` that records a
2410
+ * replayable root `agent` span.
2411
+ *
2412
+ * The `input` is captured as the root span's input (as a single positional
2413
+ * argument, so `replay(key, fn)` re-feeds it), and the run's `finalOutput`
2414
+ * is recorded as the root output. For streaming runs (`{ stream: true }`),
2415
+ * the result is handed back immediately and the final output is recorded
2416
+ * once the stream completes — first-byte latency is untouched.
2417
+ *
2418
+ * The process-wide tracing processor (`getOpenAiTracingProcessor`) must still
2419
+ * be registered: it captures the LLM/tool/handoff spans that nest beneath
2420
+ * this root.
2421
+ */
2309
2422
  async wrapRun(agent, input, options) {
2310
2423
  const { run } = await importOptionalPeer([
2311
2424
  "@openai",
@@ -2344,6 +2457,7 @@ var BitfabOpenAIAgentHandler = class {
2344
2457
  };
2345
2458
 
2346
2459
  // src/client.ts
2460
+ init_randomUuid();
2347
2461
  init_replayContext();
2348
2462
 
2349
2463
  // src/replayEnvironment.ts
@@ -2763,6 +2877,7 @@ var BitfabVercelAiHandler = class {
2763
2877
  };
2764
2878
 
2765
2879
  // src/client.ts
2880
+ init_warnOnce();
2766
2881
  var activeTraceStates = /* @__PURE__ */ new Map();
2767
2882
  var pendingSpanPromises = /* @__PURE__ */ new Map();
2768
2883
  var asyncLocalStorage = null;
@@ -3121,7 +3236,20 @@ var Bitfab = class {
3121
3236
  functionVersion.providers,
3122
3237
  this.envVars
3123
3238
  );
3124
- const resultStr = typeof executionResult.result === "string" ? executionResult.result : JSON.stringify(executionResult.result);
3239
+ let resultStr;
3240
+ if (typeof executionResult.result === "string") {
3241
+ resultStr = executionResult.result;
3242
+ } else {
3243
+ try {
3244
+ resultStr = JSON.stringify(executionResult.result) ?? String(executionResult.result);
3245
+ } catch {
3246
+ warnOnce(
3247
+ "call-result-serialize",
3248
+ "a local execution result could not be JSON-serialized; storing its String() form instead. The call still returns its real value."
3249
+ );
3250
+ resultStr = String(executionResult.result);
3251
+ }
3252
+ }
3125
3253
  this.httpClient.sendInternalTrace(functionVersion.id, {
3126
3254
  result: resultStr,
3127
3255
  source: "typescript-sdk",
@@ -3370,11 +3498,26 @@ var Bitfab = class {
3370
3498
  return await bamlClient[methodName](...args);
3371
3499
  }
3372
3500
  const collector = new CollectorClass("bitfab-baml-tracing");
3373
- const trackedClient = bamlClient.withOptions({ collector });
3374
- const trackedMethod = trackedClient[methodName];
3375
- const result = await trackedMethod.bind(
3376
- trackedClient
3377
- )(...args);
3501
+ let trackedClient;
3502
+ let trackedMethod;
3503
+ try {
3504
+ trackedClient = bamlClient.withOptions({ collector });
3505
+ const method2 = trackedClient[methodName];
3506
+ if (typeof method2 !== "function") {
3507
+ throw new BitfabError(
3508
+ "bamlClient.withOptions did not return the wrapped method"
3509
+ );
3510
+ }
3511
+ trackedMethod = method2;
3512
+ } catch {
3513
+ warnOnce(
3514
+ `wrapBAML-setup:${methodName}`,
3515
+ `BAML tracing setup failed for "${methodName}" (incompatible bamlClient or BAML version); calling it untraced. The call still runs; no span is recorded.`
3516
+ );
3517
+ wrappedFn.collector = null;
3518
+ return await bamlClient[methodName](...args);
3519
+ }
3520
+ const result = await trackedMethod.bind(trackedClient)(...args);
3378
3521
  wrappedFn.collector = collector;
3379
3522
  try {
3380
3523
  const prompt = extractPromptFromCollector(collector);
@@ -3450,200 +3593,229 @@ var Bitfab = class {
3450
3593
  () => wrappedFn.apply(this, args)
3451
3594
  );
3452
3595
  }
3453
- const currentStack = getSpanStack();
3454
- const parentContext = currentStack[currentStack.length - 1];
3455
- const replayCtxForTraceId = parentContext ? null : getReplayContext();
3456
- const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? crypto.randomUUID();
3457
- const spanId = crypto.randomUUID();
3458
- const parentSpanId = parentContext?.spanId ?? null;
3459
- const isRootSpan = parentSpanId === null;
3460
- const newContext = { traceId, spanId, contexts: [] };
3461
- const newStack = [...currentStack, newContext];
3462
- const inputs = args;
3463
- const startedAt = (/* @__PURE__ */ new Date()).toISOString();
3464
- if (isRootSpan && !activeTraceStates.has(traceId)) {
3465
- const replayCtxAtRoot = getReplayContext();
3466
- const dbSnapshotRef = buildSnapshotRef(self.dbSnapshot, startedAt);
3467
- activeTraceStates.set(traceId, {
3596
+ let newStack;
3597
+ let executeWithContext;
3598
+ let registeredTraceId;
3599
+ try {
3600
+ const currentStack = getSpanStack();
3601
+ const parentContext = currentStack[currentStack.length - 1];
3602
+ const replayCtxForTraceId = parentContext ? null : getReplayContext();
3603
+ const traceId = parentContext?.traceId ?? replayCtxForTraceId?.traceId ?? randomUuid();
3604
+ const spanId = randomUuid();
3605
+ const parentSpanId = parentContext?.spanId ?? null;
3606
+ const isRootSpan = parentSpanId === null;
3607
+ const newContext = { traceId, spanId, contexts: [] };
3608
+ newStack = [...currentStack, newContext];
3609
+ const inputs = args;
3610
+ const startedAt = (/* @__PURE__ */ new Date()).toISOString();
3611
+ if (isRootSpan && !activeTraceStates.has(traceId)) {
3612
+ const replayCtxAtRoot = getReplayContext();
3613
+ const dbSnapshotRef = buildSnapshotRef(self.dbSnapshot, startedAt);
3614
+ activeTraceStates.set(traceId, {
3615
+ traceId,
3616
+ startedAt,
3617
+ contexts: [],
3618
+ ...replayCtxAtRoot?.testRunId && {
3619
+ testRunId: replayCtxAtRoot.testRunId
3620
+ },
3621
+ ...replayCtxAtRoot?.inputSourceTraceId && {
3622
+ inputSourceTraceId: replayCtxAtRoot.inputSourceTraceId
3623
+ },
3624
+ dbSnapshotRef
3625
+ });
3626
+ pendingSpanPromises.set(traceId, []);
3627
+ registeredTraceId = traceId;
3628
+ }
3629
+ const functionName = fn.name !== "" ? fn.name : void 0;
3630
+ const baseSpanParams = {
3631
+ traceFunctionKey,
3632
+ functionName,
3633
+ spanName: options.name ?? functionName ?? traceFunctionKey,
3468
3634
  traceId,
3635
+ spanId,
3636
+ parentSpanId,
3637
+ inputs,
3469
3638
  startedAt,
3470
- contexts: [],
3471
- ...replayCtxAtRoot?.testRunId && {
3472
- testRunId: replayCtxAtRoot.testRunId
3473
- },
3474
- ...replayCtxAtRoot?.inputSourceTraceId && {
3475
- inputSourceTraceId: replayCtxAtRoot.inputSourceTraceId
3476
- },
3477
- dbSnapshotRef
3478
- });
3479
- pendingSpanPromises.set(traceId, []);
3480
- }
3481
- const functionName = fn.name !== "" ? fn.name : void 0;
3482
- const baseSpanParams = {
3483
- traceFunctionKey,
3484
- functionName,
3485
- spanName: options.name ?? functionName ?? traceFunctionKey,
3486
- traceId,
3487
- spanId,
3488
- parentSpanId,
3489
- inputs,
3490
- startedAt,
3491
- spanType: options.type ?? "custom"
3492
- };
3493
- const sendSpan = async (params, spanOpts) => {
3494
- const replayCtx = getReplayContext();
3495
- const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3496
- let resolvePersistence;
3497
- if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
3498
- persistenceCollector.push(
3499
- new Promise((resolve) => {
3500
- resolvePersistence = resolve;
3501
- })
3502
- );
3503
- }
3504
- try {
3505
- const endedAt = (/* @__PURE__ */ new Date()).toISOString();
3506
- const spanPromise = self.sendWrapperSpan({
3507
- ...baseSpanParams,
3508
- ...params,
3509
- contexts: newContext.contexts,
3510
- prompt: newContext.prompt,
3511
- endedAt,
3512
- ...replayCtx?.testRunId && { testRunId: replayCtx.testRunId },
3513
- ...replayCtx?.inputSourceSpanId && {
3514
- inputSourceSpanId: replayCtx.inputSourceSpanId
3515
- }
3516
- });
3517
- if (isRootSpan) {
3518
- const pending = pendingSpanPromises.get(traceId) ?? [];
3519
- pending.push(spanPromise);
3520
- if (persistenceCollector) {
3521
- await Promise.allSettled(pending);
3522
- } else {
3523
- await Promise.race([
3524
- Promise.allSettled(pending),
3525
- new Promise((resolve) => setTimeout(resolve, 5e3))
3526
- ]);
3527
- }
3528
- pendingSpanPromises.delete(traceId);
3529
- const traceState = activeTraceStates.get(traceId);
3530
- const completionPromise = self.sendTraceCompletion({
3531
- traceFunctionKey,
3532
- traceId,
3533
- startedAt: traceState?.startedAt ?? startedAt,
3639
+ spanType: options.type ?? "custom"
3640
+ };
3641
+ const sendSpan = async (params, spanOpts) => {
3642
+ const replayCtx = getReplayContext();
3643
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3644
+ let resolvePersistence;
3645
+ if (persistenceCollector && !spanOpts?.skipPersistenceRegistration) {
3646
+ persistenceCollector.push(
3647
+ new Promise((resolve) => {
3648
+ resolvePersistence = resolve;
3649
+ })
3650
+ );
3651
+ }
3652
+ try {
3653
+ const endedAt = (/* @__PURE__ */ new Date()).toISOString();
3654
+ const spanPromise = self.sendWrapperSpan({
3655
+ ...baseSpanParams,
3656
+ ...params,
3657
+ contexts: newContext.contexts,
3658
+ prompt: newContext.prompt,
3534
3659
  endedAt,
3535
- sessionId: traceState?.sessionId,
3536
- metadata: traceState?.metadata,
3537
- contexts: traceState?.contexts ?? [],
3538
- testRunId: traceState?.testRunId,
3539
- inputSourceTraceId: traceState?.inputSourceTraceId,
3540
- dbSnapshotRef: traceState?.dbSnapshotRef,
3541
- // Built AFTER the wrapped fn finished, so `accessed` reflects
3542
- // whether customer code obtained the branch URL during this
3543
- // item. Omitted entirely when no lease was attached, so the
3544
- // server can distinguish "no branch" from "branch ignored".
3545
- ...replayCtx?.dbBranchLease && {
3546
- dbSnapshotUsage: {
3547
- neonBranchId: replayCtx.dbBranchLease.neonBranchId,
3548
- snapshotTimestamp: replayCtx.dbBranchLease.snapshotTimestamp,
3549
- sourceTraceId: replayCtx.sourceBitfabTraceId,
3550
- accessed: replayCtx.dbSnapshotAccessed === true
3551
- }
3660
+ ...replayCtx?.testRunId && { testRunId: replayCtx.testRunId },
3661
+ ...replayCtx?.inputSourceSpanId && {
3662
+ inputSourceSpanId: replayCtx.inputSourceSpanId
3552
3663
  }
3553
3664
  });
3554
- activeTraceStates.delete(traceId);
3555
- if (persistenceCollector) {
3556
- await completionPromise;
3557
- }
3558
- } else {
3559
- const pending = pendingSpanPromises.get(traceId);
3560
- if (pending) {
3665
+ if (isRootSpan) {
3666
+ const pending = pendingSpanPromises.get(traceId) ?? [];
3561
3667
  pending.push(spanPromise);
3668
+ if (persistenceCollector) {
3669
+ await Promise.allSettled(pending);
3670
+ } else {
3671
+ let raceTimer;
3672
+ try {
3673
+ await Promise.race([
3674
+ Promise.allSettled(pending),
3675
+ new Promise((resolve) => {
3676
+ raceTimer = setTimeout(resolve, 5e3);
3677
+ unrefTimer(raceTimer);
3678
+ })
3679
+ ]);
3680
+ } finally {
3681
+ if (raceTimer) {
3682
+ clearTimeout(raceTimer);
3683
+ }
3684
+ }
3685
+ }
3686
+ pendingSpanPromises.delete(traceId);
3687
+ const traceState = activeTraceStates.get(traceId);
3688
+ const completionPromise = self.sendTraceCompletion({
3689
+ traceFunctionKey,
3690
+ traceId,
3691
+ startedAt: traceState?.startedAt ?? startedAt,
3692
+ endedAt,
3693
+ sessionId: traceState?.sessionId,
3694
+ metadata: traceState?.metadata,
3695
+ contexts: traceState?.contexts ?? [],
3696
+ testRunId: traceState?.testRunId,
3697
+ inputSourceTraceId: traceState?.inputSourceTraceId,
3698
+ dbSnapshotRef: traceState?.dbSnapshotRef,
3699
+ // Built AFTER the wrapped fn finished, so `accessed` reflects
3700
+ // whether customer code obtained the branch URL during this
3701
+ // item. Omitted entirely when no lease was attached, so the
3702
+ // server can distinguish "no branch" from "branch ignored".
3703
+ ...replayCtx?.dbBranchLease && {
3704
+ dbSnapshotUsage: {
3705
+ neonBranchId: replayCtx.dbBranchLease.neonBranchId,
3706
+ snapshotTimestamp: replayCtx.dbBranchLease.snapshotTimestamp,
3707
+ sourceTraceId: replayCtx.sourceBitfabTraceId,
3708
+ accessed: replayCtx.dbSnapshotAccessed === true
3709
+ }
3710
+ }
3711
+ });
3712
+ activeTraceStates.delete(traceId);
3713
+ if (persistenceCollector) {
3714
+ await completionPromise;
3715
+ }
3562
3716
  } else {
3563
- pendingSpanPromises.set(traceId, [spanPromise]);
3717
+ const pending = pendingSpanPromises.get(traceId);
3718
+ if (pending) {
3719
+ pending.push(spanPromise);
3720
+ } else {
3721
+ pendingSpanPromises.set(traceId, [spanPromise]);
3722
+ }
3564
3723
  }
3724
+ } catch {
3725
+ } finally {
3726
+ resolvePersistence?.();
3565
3727
  }
3566
- } catch {
3567
- } finally {
3568
- resolvePersistence?.();
3569
- }
3570
- };
3571
- const replayCtxForMock = getReplayContext();
3572
- if (replayCtxForMock?.mockTree && !isRootSpan) {
3573
- const counters = replayCtxForMock.callCounters;
3574
- const counterKey = `${traceFunctionKey}:${baseSpanParams.spanName}`;
3575
- const callIndex = counters.get(counterKey) ?? 0;
3576
- counters.set(counterKey, callIndex + 1);
3577
- const shouldMock = replayCtxForMock.mockStrategy === "all" || replayCtxForMock.mockStrategy === "marked" && options.mockOnReplay === true;
3578
- if (shouldMock) {
3579
- const mockKey = `${counterKey}:${callIndex}`;
3580
- const mockSpan = replayCtxForMock.mockTree.spans.get(mockKey);
3581
- if (mockSpan) {
3582
- let output = mockSpan.output;
3583
- if (mockSpan.outputMeta !== void 0 && mockSpan.outputMeta !== null) {
3584
- output = deserializeValue({
3585
- json: mockSpan.output,
3586
- meta: mockSpan.outputMeta
3587
- });
3588
- }
3589
- void sendSpan({ result: output });
3590
- if (fnReturnsPromise) {
3591
- return Promise.resolve(output);
3728
+ };
3729
+ const replayCtxForMock = getReplayContext();
3730
+ if (replayCtxForMock?.mockTree && !isRootSpan) {
3731
+ const counters = replayCtxForMock.callCounters;
3732
+ const counterKey = `${traceFunctionKey}:${baseSpanParams.spanName}`;
3733
+ const callIndex = counters.get(counterKey) ?? 0;
3734
+ counters.set(counterKey, callIndex + 1);
3735
+ const shouldMock = replayCtxForMock.mockStrategy === "all" || replayCtxForMock.mockStrategy === "marked" && options.mockOnReplay === true;
3736
+ if (shouldMock) {
3737
+ const mockKey = `${counterKey}:${callIndex}`;
3738
+ const mockSpan = replayCtxForMock.mockTree.spans.get(mockKey);
3739
+ if (mockSpan) {
3740
+ let output = mockSpan.output;
3741
+ if (mockSpan.outputMeta !== void 0 && mockSpan.outputMeta !== null) {
3742
+ output = deserializeValue({
3743
+ json: mockSpan.output,
3744
+ meta: mockSpan.outputMeta
3745
+ });
3746
+ }
3747
+ void sendSpan({ result: output });
3748
+ if (fnReturnsPromise) {
3749
+ return Promise.resolve(output);
3750
+ }
3751
+ return output;
3592
3752
  }
3593
- return output;
3594
3753
  }
3595
3754
  }
3596
- }
3597
- const recordSpan = (result) => {
3598
- if (options.finalize) {
3599
- const replayCtx = getReplayContext();
3600
- const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3601
- let resolvePersistence;
3602
- if (persistenceCollector) {
3603
- persistenceCollector.push(
3604
- new Promise((resolve) => {
3605
- resolvePersistence = resolve;
3606
- })
3607
- );
3755
+ const recordSpan = (result) => {
3756
+ if (options.finalize) {
3757
+ const replayCtx = getReplayContext();
3758
+ const persistenceCollector = isRootSpan ? replayCtx?.pendingPersistence : void 0;
3759
+ let resolvePersistence;
3760
+ if (persistenceCollector) {
3761
+ persistenceCollector.push(
3762
+ new Promise((resolve) => {
3763
+ resolvePersistence = resolve;
3764
+ })
3765
+ );
3766
+ }
3767
+ void Promise.resolve().then(() => options.finalize(result)).then(
3768
+ (output) => sendSpan(
3769
+ { result: output },
3770
+ { skipPersistenceRegistration: true }
3771
+ )
3772
+ ).catch(
3773
+ (error) => sendSpan(
3774
+ {
3775
+ result: void 0,
3776
+ error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
3777
+ },
3778
+ { skipPersistenceRegistration: true }
3779
+ )
3780
+ ).finally(() => resolvePersistence?.());
3781
+ } else {
3782
+ void sendSpan({ result });
3608
3783
  }
3609
- void Promise.resolve().then(() => options.finalize(result)).then(
3610
- (output) => sendSpan(
3611
- { result: output },
3612
- { skipPersistenceRegistration: true }
3613
- )
3614
- ).catch(
3615
- (error) => sendSpan(
3616
- {
3784
+ };
3785
+ executeWithContext = () => {
3786
+ const result = fn(...args);
3787
+ if (result instanceof Promise) {
3788
+ return result.then((resolvedResult) => {
3789
+ recordSpan(resolvedResult);
3790
+ return resolvedResult;
3791
+ }).catch((error) => {
3792
+ void sendSpan({
3617
3793
  result: void 0,
3618
- error: error instanceof Error ? `finalize failed: ${error.message}` : `finalize failed: ${String(error)}`
3619
- },
3620
- { skipPersistenceRegistration: true }
3621
- )
3622
- ).finally(() => resolvePersistence?.());
3623
- } else {
3624
- void sendSpan({ result });
3625
- }
3626
- };
3627
- const executeWithContext = () => {
3628
- const result = fn(...args);
3629
- if (result instanceof Promise) {
3630
- return result.then((resolvedResult) => {
3631
- recordSpan(resolvedResult);
3632
- return resolvedResult;
3633
- }).catch((error) => {
3634
- void sendSpan({
3635
- result: void 0,
3636
- error: error instanceof Error ? error.message : String(error)
3794
+ error: error instanceof Error ? error.message : String(error)
3795
+ });
3796
+ throw error;
3637
3797
  });
3638
- throw error;
3639
- });
3798
+ }
3799
+ if (isAsyncGenerator(result)) {
3800
+ return wrapAsyncGenerator(result, newStack, sendSpan);
3801
+ }
3802
+ recordSpan(result);
3803
+ return result;
3804
+ };
3805
+ } catch (setupError) {
3806
+ if (registeredTraceId) {
3807
+ activeTraceStates.delete(registeredTraceId);
3808
+ pendingSpanPromises.delete(registeredTraceId);
3640
3809
  }
3641
- if (isAsyncGenerator(result)) {
3642
- return wrapAsyncGenerator(result, newStack, sendSpan);
3810
+ if (getReplayContext()) {
3811
+ throw setupError;
3643
3812
  }
3644
- recordSpan(result);
3645
- return result;
3646
- };
3813
+ warnOnce(
3814
+ `withSpan-setup:${traceFunctionKey}`,
3815
+ `tracing setup failed for "${traceFunctionKey}"; running it untraced. The function still runs and returns normally; no span is recorded.`
3816
+ );
3817
+ return fn(...args);
3818
+ }
3647
3819
  return runWithSpanStack(newStack, executeWithContext);
3648
3820
  };
3649
3821
  Object.defineProperty(wrappedFn, "_bitfabTraceFunctionKey", {