@bitfab/sdk 0.36.9 → 0.36.10

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/index.d.cts CHANGED
@@ -353,6 +353,12 @@ interface DeliveryReport {
353
353
  closed: boolean;
354
354
  /** Every carrier submitted under this trace came back accepted. */
355
355
  delivered: boolean;
356
+ /**
357
+ * The server's assigned `traces.id`, read back from the OTLP ingest response.
358
+ * Absent when talking to a server that predates this field, or before any
359
+ * carrier for the trace has been acked.
360
+ */
361
+ serverTraceId?: string;
356
362
  }
357
363
  declare class HttpClient {
358
364
  private readonly apiKey;
@@ -390,6 +396,12 @@ declare class HttpClient {
390
396
  * that was never tracked, so ordinary tracing costs no bookkeeping at all.
391
397
  */
392
398
  trackTraceDeliveries(traceIds: string[]): void;
399
+ /**
400
+ * The server's assigned `traces.id` for a tracked trace if it has already
401
+ * been read back off an ingest response, without stopping tracking. Lets a
402
+ * replay surface the id mid-run for items whose spans already landed.
403
+ */
404
+ peekServerTraceId(traceId: string): string | undefined;
393
405
  /** Whether any tracked trace has had its closing carrier submitted. */
394
406
  hasClosedDeliveries(traceIds: string[]): boolean;
395
407
  /**
@@ -410,6 +422,12 @@ declare class HttpClient {
410
422
  * delivered ref is proof its row exists: the same fact the replay status
411
423
  * endpoint would report, already in hand.
412
424
  */
425
+ /**
426
+ * Record the server's assigned `traces.id` for each tracked source trace,
427
+ * read back from the OTLP ingest response. Keyed by source trace id, the same
428
+ * key the delivery ledger uses. Untracked ids are ignored.
429
+ */
430
+ private recordServerTraceIds;
413
431
  private recordDeliveredCarriers;
414
432
  /**
415
433
  * Track deferred span work so this client's own lifecycle waits for it, and
@@ -2557,7 +2575,7 @@ declare class BitfabFunction {
2557
2575
  /**
2558
2576
  * SDK version from package.json (injected at build time)
2559
2577
  */
2560
- declare const __version__ = "0.36.9";
2578
+ declare const __version__ = "0.36.10";
2561
2579
 
2562
2580
  /**
2563
2581
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -353,6 +353,12 @@ interface DeliveryReport {
353
353
  closed: boolean;
354
354
  /** Every carrier submitted under this trace came back accepted. */
355
355
  delivered: boolean;
356
+ /**
357
+ * The server's assigned `traces.id`, read back from the OTLP ingest response.
358
+ * Absent when talking to a server that predates this field, or before any
359
+ * carrier for the trace has been acked.
360
+ */
361
+ serverTraceId?: string;
356
362
  }
357
363
  declare class HttpClient {
358
364
  private readonly apiKey;
@@ -390,6 +396,12 @@ declare class HttpClient {
390
396
  * that was never tracked, so ordinary tracing costs no bookkeeping at all.
391
397
  */
392
398
  trackTraceDeliveries(traceIds: string[]): void;
399
+ /**
400
+ * The server's assigned `traces.id` for a tracked trace if it has already
401
+ * been read back off an ingest response, without stopping tracking. Lets a
402
+ * replay surface the id mid-run for items whose spans already landed.
403
+ */
404
+ peekServerTraceId(traceId: string): string | undefined;
393
405
  /** Whether any tracked trace has had its closing carrier submitted. */
394
406
  hasClosedDeliveries(traceIds: string[]): boolean;
395
407
  /**
@@ -410,6 +422,12 @@ declare class HttpClient {
410
422
  * delivered ref is proof its row exists: the same fact the replay status
411
423
  * endpoint would report, already in hand.
412
424
  */
425
+ /**
426
+ * Record the server's assigned `traces.id` for each tracked source trace,
427
+ * read back from the OTLP ingest response. Keyed by source trace id, the same
428
+ * key the delivery ledger uses. Untracked ids are ignored.
429
+ */
430
+ private recordServerTraceIds;
413
431
  private recordDeliveredCarriers;
414
432
  /**
415
433
  * Track deferred span work so this client's own lifecycle waits for it, and
@@ -2557,7 +2575,7 @@ declare class BitfabFunction {
2557
2575
  /**
2558
2576
  * SDK version from package.json (injected at build time)
2559
2577
  */
2560
- declare const __version__ = "0.36.9";
2578
+ declare const __version__ = "0.36.10";
2561
2579
 
2562
2580
  /**
2563
2581
  * Constants for the Bitfab SDK.
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  getCurrentReplayBranch,
12
12
  getCurrentSpan,
13
13
  getCurrentTrace
14
- } from "./chunk-TJAFKZ4X.js";
14
+ } from "./chunk-RSM2TLKG.js";
15
15
  import {
16
16
  BITFAB_PROGRESS_PREFIX,
17
17
  BitfabError,
@@ -23,7 +23,7 @@ import {
23
23
  flushTraces,
24
24
  reportReplayProgress,
25
25
  serializeReplayResult
26
- } from "./chunk-MBKY42QC.js";
26
+ } from "./chunk-AAMRJGZJ.js";
27
27
  export {
28
28
  BITFAB_PROGRESS_PREFIX,
29
29
  Bitfab,
package/dist/node.cjs CHANGED
@@ -88,7 +88,7 @@ var __version__;
88
88
  var init_version_generated = __esm({
89
89
  "src/version.generated.ts"() {
90
90
  "use strict";
91
- __version__ = "0.36.9";
91
+ __version__ = "0.36.10";
92
92
  }
93
93
  });
94
94
 
@@ -1442,6 +1442,10 @@ var init_http = __esm({
1442
1442
  ...error instanceof BitfabError && error.retryAfterMs !== void 0 ? { retryAfterMs: error.retryAfterMs } : {}
1443
1443
  });
1444
1444
  }
1445
+ const serverTraceIds = asPayloadRecord(response?.traceIds);
1446
+ if (serverTraceIds !== void 0) {
1447
+ this.recordServerTraceIds(serverTraceIds);
1448
+ }
1445
1449
  const partialSuccess = asPayloadRecord(response?.partialSuccess);
1446
1450
  const rejected = partialSuccess?.rejectedSpans;
1447
1451
  if (rejected !== void 0 && rejected !== "0" && rejected !== 0) {
@@ -1466,6 +1470,14 @@ var init_http = __esm({
1466
1470
  }
1467
1471
  }
1468
1472
  }
1473
+ /**
1474
+ * The server's assigned `traces.id` for a tracked trace if it has already
1475
+ * been read back off an ingest response, without stopping tracking. Lets a
1476
+ * replay surface the id mid-run for items whose spans already landed.
1477
+ */
1478
+ peekServerTraceId(traceId) {
1479
+ return this.traceDeliveries.get(traceId)?.serverTraceId;
1480
+ }
1469
1481
  /** Whether any tracked trace has had its closing carrier submitted. */
1470
1482
  hasClosedDeliveries(traceIds) {
1471
1483
  return traceIds.some((traceId) => this.traceDeliveries.get(traceId)?.closed);
@@ -1492,7 +1504,8 @@ var init_http = __esm({
1492
1504
  closed: delivery.closed,
1493
1505
  delivered: delivery.closingAcked && [...delivery.submittedSpanIds].every(
1494
1506
  (spanId) => delivery.ackedSpanIds.has(spanId)
1495
- )
1507
+ ),
1508
+ serverTraceId: delivery.serverTraceId
1496
1509
  };
1497
1510
  }
1498
1511
  return reports;
@@ -1521,6 +1534,23 @@ var init_http = __esm({
1521
1534
  * delivered ref is proof its row exists: the same fact the replay status
1522
1535
  * endpoint would report, already in hand.
1523
1536
  */
1537
+ /**
1538
+ * Record the server's assigned `traces.id` for each tracked source trace,
1539
+ * read back from the OTLP ingest response. Keyed by source trace id, the same
1540
+ * key the delivery ledger uses. Untracked ids are ignored.
1541
+ */
1542
+ recordServerTraceIds(map) {
1543
+ for (const [sourceTraceId, serverTraceId] of Object.entries(map)) {
1544
+ if (typeof serverTraceId !== "string") {
1545
+ continue;
1546
+ }
1547
+ const delivery = this.traceDeliveries.get(sourceTraceId);
1548
+ if (delivery === void 0) {
1549
+ continue;
1550
+ }
1551
+ delivery.serverTraceId = serverTraceId;
1552
+ }
1553
+ }
1524
1554
  recordDeliveredCarriers(refs) {
1525
1555
  for (const ref of refs) {
1526
1556
  const delivery = this.traceDeliveries.get(ref.traceId);
@@ -2658,13 +2688,17 @@ async function waitForReplayPersistence(httpClient, testRunId, replayedTraceIds)
2658
2688
  }
2659
2689
  if (!httpClient.hasClosedDeliveries(replayedTraceIds)) {
2660
2690
  httpClient.takeTraceDeliveries(replayedTraceIds);
2661
- return;
2691
+ return {};
2662
2692
  }
2663
2693
  const flushed = await flushTraces(REPLAY_PERSISTENCE_TIMEOUT_MS);
2664
2694
  const deliveries = httpClient.takeTraceDeliveries(replayedTraceIds);
2665
2695
  const expectedSpanCounts = {};
2696
+ const readBackTraceIds = {};
2666
2697
  let allDelivered = true;
2667
2698
  for (const [traceId, delivery] of Object.entries(deliveries)) {
2699
+ if (delivery.serverTraceId !== void 0) {
2700
+ readBackTraceIds[traceId] = delivery.serverTraceId;
2701
+ }
2668
2702
  if (!delivery.closed) {
2669
2703
  continue;
2670
2704
  }
@@ -2672,7 +2706,7 @@ async function waitForReplayPersistence(httpClient, testRunId, replayedTraceIds)
2672
2706
  allDelivered = allDelivered && delivery.delivered;
2673
2707
  }
2674
2708
  if (allDelivered) {
2675
- return;
2709
+ return readBackTraceIds;
2676
2710
  }
2677
2711
  const deadline = Date.now() + REPLAY_PERSISTENCE_TIMEOUT_MS;
2678
2712
  let missing = Object.keys(expectedSpanCounts).length;
@@ -2686,7 +2720,7 @@ async function waitForReplayPersistence(httpClient, testRunId, replayedTraceIds)
2686
2720
  (traceId) => ready[traceId] === void 0
2687
2721
  ).length;
2688
2722
  if (missing === 0) {
2689
- return;
2723
+ return readBackTraceIds;
2690
2724
  }
2691
2725
  if (Date.now() >= deadline) {
2692
2726
  break;
@@ -2714,7 +2748,7 @@ async function mapWithConcurrency2(tasks, maxConcurrency, onSettled, onStarted)
2714
2748
  onStarted?.(index);
2715
2749
  const result = await tasks[index]();
2716
2750
  results[index] = result;
2717
- onSettled?.(result, index);
2751
+ await onSettled?.(result, index);
2718
2752
  }
2719
2753
  }
2720
2754
  const workers = Array.from(
@@ -2804,10 +2838,36 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
2804
2838
  let started = 0;
2805
2839
  let succeeded = 0;
2806
2840
  let errored = 0;
2841
+ let flushInFlight = null;
2842
+ let flushPending = false;
2843
+ const flushFinishedItemTrace = () => {
2844
+ flushPending = true;
2845
+ if (!flushInFlight) {
2846
+ flushInFlight = (async () => {
2847
+ try {
2848
+ while (flushPending) {
2849
+ flushPending = false;
2850
+ await httpClient.settleDeferredWork(REPLAY_PERSISTENCE_TIMEOUT_MS);
2851
+ await flushTraces(REPLAY_PERSISTENCE_TIMEOUT_MS);
2852
+ }
2853
+ } finally {
2854
+ flushInFlight = null;
2855
+ }
2856
+ })();
2857
+ }
2858
+ return flushInFlight;
2859
+ };
2807
2860
  const resultItems = await mapWithConcurrency2(
2808
2861
  tasks,
2809
2862
  maxConcurrency,
2810
- (item) => {
2863
+ async (item, index) => {
2864
+ let serverTraceId = null;
2865
+ try {
2866
+ await flushFinishedItemTrace();
2867
+ serverTraceId = httpClient.peekServerTraceId(replayedTraceIds[index]) ?? null;
2868
+ } catch {
2869
+ }
2870
+ item.traceId = serverTraceId;
2811
2871
  completed += 1;
2812
2872
  if (item.error === null) {
2813
2873
  succeeded += 1;
@@ -2822,11 +2882,12 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
2822
2882
  succeeded,
2823
2883
  errored,
2824
2884
  item: {
2825
- // The server replay trace id isn't known until completeReplay runs
2826
- // (below), so it can't be reported mid-run and we never emit the
2827
- // client-side placeholder. originalTraceId (the historical trace)
2828
- // is known now and is what a UI keys on to identify what settled.
2829
- traceId: null,
2885
+ // The server's assigned traces.id, read back off the ingest
2886
+ // response and surfaced as this item finishes. Null only if the
2887
+ // per-item flush could not confirm delivery in time; the end-of-run
2888
+ // barrier then fills the returned ReplayItem. The client-side
2889
+ // placeholder is never surfaced.
2890
+ traceId: serverTraceId,
2830
2891
  originalTraceId: item.originalTraceId ?? null,
2831
2892
  originalSpanId: item.originalSpanId ?? null,
2832
2893
  // Deprecated aliases for originalTraceId/originalSpanId.
@@ -2872,12 +2933,19 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
2872
2933
  }
2873
2934
  } : void 0
2874
2935
  );
2875
- await preserveReplayFailure(
2936
+ const deliveredTraceIds = await preserveReplayFailure(
2876
2937
  () => waitForReplayPersistence(httpClient, testRunId, replayedTraceIds),
2877
2938
  resultItems,
2878
2939
  testRunId,
2879
2940
  fullTestRunUrl
2880
2941
  );
2942
+ for (let index = 0; index < resultItems.length; index += 1) {
2943
+ const localId = replayedTraceIds[index];
2944
+ const readBack = localId ? deliveredTraceIds[localId] : void 0;
2945
+ if (readBack !== void 0) {
2946
+ resultItems[index].traceId = readBack;
2947
+ }
2948
+ }
2881
2949
  const completeResult = await preserveReplayFailure(
2882
2950
  () => httpClient.completeReplay(testRunId),
2883
2951
  resultItems,
@@ -2893,7 +2961,7 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
2893
2961
  const item = resultItems[index];
2894
2962
  const localId = replayedTraceIds[index];
2895
2963
  const mapped = localId ? serverTraceIds[localId] : void 0;
2896
- item.traceId = mapped ?? null;
2964
+ item.traceId = item.traceId ?? mapped ?? null;
2897
2965
  if (item.error === null) {
2898
2966
  completedCount += 1;
2899
2967
  if (mapped === void 0) {