bitfab 0.33.1 → 0.33.3

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.cjs CHANGED
@@ -324,14 +324,25 @@ function runWithReplayContext(ctx, fn) {
324
324
  }
325
325
  return fn();
326
326
  }
327
- var replayContextStorage, replayContextReady;
327
+ var replayContextStorage, REPLAY_CONTEXT_STORAGE_SYMBOL, replayContextReady;
328
328
  var init_replayContext = __esm({
329
329
  "src/replayContext.ts"() {
330
330
  "use strict";
331
331
  init_asyncStorage();
332
332
  replayContextStorage = null;
333
+ REPLAY_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.replayContextStorage");
333
334
  replayContextReady = asyncStorageReady.then(() => {
334
- replayContextStorage = createAsyncLocalStorage();
335
+ const shared = globalThis;
336
+ const existing = shared[REPLAY_CONTEXT_STORAGE_SYMBOL];
337
+ if (existing) {
338
+ replayContextStorage = existing;
339
+ return;
340
+ }
341
+ const created = createAsyncLocalStorage();
342
+ if (created) {
343
+ shared[REPLAY_CONTEXT_STORAGE_SYMBOL] = created;
344
+ replayContextStorage = created;
345
+ }
335
346
  });
336
347
  }
337
348
  });
@@ -612,9 +623,10 @@ function buildMockTree(rootNode) {
612
623
  }
613
624
  return { spans };
614
625
  }
615
- async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, resolvedOverrides, replayedTraceId, includeDbBranchLease, adaptInputs) {
616
- const lease = includeDbBranchLease ? serverItem.dbBranchLease : void 0;
617
- const leaseError = includeDbBranchLease ? serverItem.dbBranchLeaseError : void 0;
626
+ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, resolvedOverrides, replayedTraceId, includeDbBranchLease, dbBranchSettings, adaptInputs) {
627
+ let lease = includeDbBranchLease ? serverItem.dbBranchLease : void 0;
628
+ let leaseError = includeDbBranchLease ? serverItem.dbBranchLeaseError : void 0;
629
+ let dbSnapshotRef = serverItem.dbSnapshotRef;
618
630
  let inputs = [];
619
631
  let originalOutput;
620
632
  let result;
@@ -623,6 +635,16 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
623
635
  const originalTraceId = serverItem.originalTraceId ?? serverItem.sourceTraceId;
624
636
  const originalSpanId = serverItem.originalSpanId ?? serverItem.sourceSpanId;
625
637
  try {
638
+ if (includeDbBranchLease && !lease && !leaseError) {
639
+ const resolved = await httpClient.resolveDbBranchLease(
640
+ testRunId,
641
+ originalTraceId,
642
+ dbBranchSettings
643
+ );
644
+ lease = resolved.lease ?? void 0;
645
+ leaseError = resolved.leaseError ?? void 0;
646
+ dbSnapshotRef = resolved.dbSnapshotRef ?? dbSnapshotRef;
647
+ }
626
648
  if (leaseError) {
627
649
  throw new BitfabError(
628
650
  `Replay requested a database branch for trace ${originalTraceId} but it could not be resolved (${leaseError.code}): ${leaseError.message}. The function was not run, because replaying it against the live database would produce a result that looks valid but did not use the historical data you asked for.`
@@ -734,7 +756,7 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
734
756
  // Null here (and on older servers) means "replay tokens not known".
735
757
  tokens: null,
736
758
  model: serverItem.model ?? null,
737
- dbSnapshotRef: serverItem.dbSnapshotRef ?? null
759
+ dbSnapshotRef: dbSnapshotRef ?? null
738
760
  };
739
761
  }
740
762
  async function mapWithConcurrency(tasks, maxConcurrency, onSettled) {
@@ -777,11 +799,13 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
777
799
  await replayContextReady;
778
800
  let codeChangeDescription = options?.codeChangeDescription;
779
801
  let codeChangeFiles = options?.codeChangeFiles;
780
- if (codeChangeDescription === void 0 && codeChangeFiles === void 0) {
802
+ if (codeChangeFiles === void 0) {
781
803
  const captured = await resolveAutoCodeChange(options?.name);
782
804
  if (captured) {
783
- codeChangeDescription = captured.description;
784
805
  codeChangeFiles = captured.files;
806
+ if (codeChangeDescription === void 0) {
807
+ codeChangeDescription = captured.description;
808
+ }
785
809
  }
786
810
  }
787
811
  const {
@@ -821,6 +845,7 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
821
845
  resolvedOverrides,
822
846
  replayedTraceIds[index],
823
847
  options?.dbBranch !== void 0,
848
+ resolveDbBranchSettings(options?.dbBranch),
824
849
  options?.adaptInputs
825
850
  )
826
851
  );
@@ -989,7 +1014,7 @@ __export(index_exports, {
989
1014
  module.exports = __toCommonJS(index_exports);
990
1015
 
991
1016
  // src/version.generated.ts
992
- var __version__ = "0.33.1";
1017
+ var __version__ = "0.33.3";
993
1018
 
994
1019
  // src/constants.ts
995
1020
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -1007,6 +1032,7 @@ function unrefTimer(timer) {
1007
1032
 
1008
1033
  // src/http.ts
1009
1034
  init_warnOnce();
1035
+ var REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS = 3e5;
1010
1036
  function serializePayloadBody(payload) {
1011
1037
  try {
1012
1038
  return { body: JSON.stringify(payload), dropped: [] };
@@ -1367,6 +1393,7 @@ var HttpClient = class {
1367
1393
  }
1368
1394
  if (includeDbBranchLease) {
1369
1395
  payload.includeDbBranchLease = true;
1396
+ payload.lazyDbBranchLease = true;
1370
1397
  }
1371
1398
  if (experimentGroupId !== void 0) {
1372
1399
  payload.experimentGroupId = experimentGroupId;
@@ -1380,7 +1407,7 @@ var HttpClient = class {
1380
1407
  if (dbBranchSettings !== void 0) {
1381
1408
  payload.dbBranchSettings = dbBranchSettings;
1382
1409
  }
1383
- const timeout = includeDbBranchLease ? 3e5 : 3e4;
1410
+ const timeout = includeDbBranchLease ? REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS : 3e4;
1384
1411
  return this.request("/api/sdk/replay/start", payload, {
1385
1412
  timeout
1386
1413
  });
@@ -1479,11 +1506,11 @@ var HttpClient = class {
1479
1506
  * snapshot + preview branch and polls operations to readiness, which
1480
1507
  * can take seconds.
1481
1508
  */
1482
- async resolveDbBranchLease(testRunId, traceId, dbSnapshotRef) {
1509
+ async resolveDbBranchLease(testRunId, traceId, dbBranchSettings) {
1483
1510
  return this.request(
1484
1511
  "/api/sdk/replay/resolveDbBranchLease",
1485
- { testRunId, traceId, dbSnapshotRef },
1486
- { timeout: 9e4 }
1512
+ { testRunId, traceId, dbBranchSettings },
1513
+ { timeout: REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS }
1487
1514
  );
1488
1515
  }
1489
1516
  /** Release a previously-resolved DB branch by deleting its Neon branch. Idempotent server-side. */
@@ -3480,8 +3507,22 @@ init_warnOnce();
3480
3507
  var activeTraceStates = /* @__PURE__ */ new Map();
3481
3508
  var pendingSpanPromises = /* @__PURE__ */ new Map();
3482
3509
  var asyncLocalStorage = null;
3510
+ var SPAN_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.spanContextStorage");
3483
3511
  var initializeAsyncContext = () => {
3484
- asyncLocalStorage ?? (asyncLocalStorage = createAsyncLocalStorage());
3512
+ if (asyncLocalStorage) {
3513
+ return;
3514
+ }
3515
+ const shared = globalThis;
3516
+ const existing = shared[SPAN_CONTEXT_STORAGE_SYMBOL];
3517
+ if (existing) {
3518
+ asyncLocalStorage = existing;
3519
+ return;
3520
+ }
3521
+ const created = createAsyncLocalStorage();
3522
+ if (created) {
3523
+ shared[SPAN_CONTEXT_STORAGE_SYMBOL] = created;
3524
+ asyncLocalStorage = created;
3525
+ }
3485
3526
  };
3486
3527
  var asyncLocalStorageReady = asyncStorageReady.then(() => {
3487
3528
  initializeAsyncContext();