@bitfab/sdk 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
@@ -315,14 +315,25 @@ function runWithReplayContext(ctx, fn) {
315
315
  }
316
316
  return fn();
317
317
  }
318
- var replayContextStorage, replayContextReady;
318
+ var replayContextStorage, REPLAY_CONTEXT_STORAGE_SYMBOL, replayContextReady;
319
319
  var init_replayContext = __esm({
320
320
  "src/replayContext.ts"() {
321
321
  "use strict";
322
322
  init_asyncStorage();
323
323
  replayContextStorage = null;
324
+ REPLAY_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.replayContextStorage");
324
325
  replayContextReady = asyncStorageReady.then(() => {
325
- replayContextStorage = createAsyncLocalStorage();
326
+ const shared = globalThis;
327
+ const existing = shared[REPLAY_CONTEXT_STORAGE_SYMBOL];
328
+ if (existing) {
329
+ replayContextStorage = existing;
330
+ return;
331
+ }
332
+ const created = createAsyncLocalStorage();
333
+ if (created) {
334
+ shared[REPLAY_CONTEXT_STORAGE_SYMBOL] = created;
335
+ replayContextStorage = created;
336
+ }
326
337
  });
327
338
  }
328
339
  });
@@ -603,9 +614,10 @@ function buildMockTree(rootNode) {
603
614
  }
604
615
  return { spans };
605
616
  }
606
- async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, resolvedOverrides, replayedTraceId, includeDbBranchLease, adaptInputs) {
607
- const lease = includeDbBranchLease ? serverItem.dbBranchLease : void 0;
608
- const leaseError = includeDbBranchLease ? serverItem.dbBranchLeaseError : void 0;
617
+ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy, resolvedOverrides, replayedTraceId, includeDbBranchLease, dbBranchSettings, adaptInputs) {
618
+ let lease = includeDbBranchLease ? serverItem.dbBranchLease : void 0;
619
+ let leaseError = includeDbBranchLease ? serverItem.dbBranchLeaseError : void 0;
620
+ let dbSnapshotRef = serverItem.dbSnapshotRef;
609
621
  let inputs = [];
610
622
  let originalOutput;
611
623
  let result;
@@ -614,6 +626,16 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
614
626
  const originalTraceId = serverItem.originalTraceId ?? serverItem.sourceTraceId;
615
627
  const originalSpanId = serverItem.originalSpanId ?? serverItem.sourceSpanId;
616
628
  try {
629
+ if (includeDbBranchLease && !lease && !leaseError) {
630
+ const resolved = await httpClient.resolveDbBranchLease(
631
+ testRunId,
632
+ originalTraceId,
633
+ dbBranchSettings
634
+ );
635
+ lease = resolved.lease ?? void 0;
636
+ leaseError = resolved.leaseError ?? void 0;
637
+ dbSnapshotRef = resolved.dbSnapshotRef ?? dbSnapshotRef;
638
+ }
617
639
  if (leaseError) {
618
640
  throw new BitfabError(
619
641
  `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.`
@@ -725,7 +747,7 @@ async function processItem(httpClient, serverItem, fn, testRunId, mockStrategy,
725
747
  // Null here (and on older servers) means "replay tokens not known".
726
748
  tokens: null,
727
749
  model: serverItem.model ?? null,
728
- dbSnapshotRef: serverItem.dbSnapshotRef ?? null
750
+ dbSnapshotRef: dbSnapshotRef ?? null
729
751
  };
730
752
  }
731
753
  async function mapWithConcurrency(tasks, maxConcurrency, onSettled) {
@@ -768,11 +790,13 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
768
790
  await replayContextReady;
769
791
  let codeChangeDescription = options?.codeChangeDescription;
770
792
  let codeChangeFiles = options?.codeChangeFiles;
771
- if (codeChangeDescription === void 0 && codeChangeFiles === void 0) {
793
+ if (codeChangeFiles === void 0) {
772
794
  const captured = await resolveAutoCodeChange(options?.name);
773
795
  if (captured) {
774
- codeChangeDescription = captured.description;
775
796
  codeChangeFiles = captured.files;
797
+ if (codeChangeDescription === void 0) {
798
+ codeChangeDescription = captured.description;
799
+ }
776
800
  }
777
801
  }
778
802
  const {
@@ -812,6 +836,7 @@ async function replay(httpClient, serviceUrl, traceFunctionKey, fn, options, reg
812
836
  resolvedOverrides,
813
837
  replayedTraceIds[index],
814
838
  options?.dbBranch !== void 0,
839
+ resolveDbBranchSettings(options?.dbBranch),
815
840
  options?.adaptInputs
816
841
  )
817
842
  );
@@ -980,7 +1005,7 @@ __export(index_exports, {
980
1005
  module.exports = __toCommonJS(index_exports);
981
1006
 
982
1007
  // src/version.generated.ts
983
- var __version__ = "0.33.1";
1008
+ var __version__ = "0.33.3";
984
1009
 
985
1010
  // src/constants.ts
986
1011
  var DEFAULT_SERVICE_URL = "https://bitfab.ai";
@@ -998,6 +1023,7 @@ function unrefTimer(timer) {
998
1023
 
999
1024
  // src/http.ts
1000
1025
  init_warnOnce();
1026
+ var REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS = 3e5;
1001
1027
  function serializePayloadBody(payload) {
1002
1028
  try {
1003
1029
  return { body: JSON.stringify(payload), dropped: [] };
@@ -1358,6 +1384,7 @@ var HttpClient = class {
1358
1384
  }
1359
1385
  if (includeDbBranchLease) {
1360
1386
  payload.includeDbBranchLease = true;
1387
+ payload.lazyDbBranchLease = true;
1361
1388
  }
1362
1389
  if (experimentGroupId !== void 0) {
1363
1390
  payload.experimentGroupId = experimentGroupId;
@@ -1371,7 +1398,7 @@ var HttpClient = class {
1371
1398
  if (dbBranchSettings !== void 0) {
1372
1399
  payload.dbBranchSettings = dbBranchSettings;
1373
1400
  }
1374
- const timeout = includeDbBranchLease ? 3e5 : 3e4;
1401
+ const timeout = includeDbBranchLease ? REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS : 3e4;
1375
1402
  return this.request("/api/sdk/replay/start", payload, {
1376
1403
  timeout
1377
1404
  });
@@ -1470,11 +1497,11 @@ var HttpClient = class {
1470
1497
  * snapshot + preview branch and polls operations to readiness, which
1471
1498
  * can take seconds.
1472
1499
  */
1473
- async resolveDbBranchLease(testRunId, traceId, dbSnapshotRef) {
1500
+ async resolveDbBranchLease(testRunId, traceId, dbBranchSettings) {
1474
1501
  return this.request(
1475
1502
  "/api/sdk/replay/resolveDbBranchLease",
1476
- { testRunId, traceId, dbSnapshotRef },
1477
- { timeout: 9e4 }
1503
+ { testRunId, traceId, dbBranchSettings },
1504
+ { timeout: REPLAY_DB_BRANCH_REQUEST_TIMEOUT_MS }
1478
1505
  );
1479
1506
  }
1480
1507
  /** Release a previously-resolved DB branch by deleting its Neon branch. Idempotent server-side. */
@@ -3471,8 +3498,22 @@ init_warnOnce();
3471
3498
  var activeTraceStates = /* @__PURE__ */ new Map();
3472
3499
  var pendingSpanPromises = /* @__PURE__ */ new Map();
3473
3500
  var asyncLocalStorage = null;
3501
+ var SPAN_CONTEXT_STORAGE_SYMBOL = /* @__PURE__ */ Symbol.for("bitfab.spanContextStorage");
3474
3502
  var initializeAsyncContext = () => {
3475
- asyncLocalStorage ?? (asyncLocalStorage = createAsyncLocalStorage());
3503
+ if (asyncLocalStorage) {
3504
+ return;
3505
+ }
3506
+ const shared = globalThis;
3507
+ const existing = shared[SPAN_CONTEXT_STORAGE_SYMBOL];
3508
+ if (existing) {
3509
+ asyncLocalStorage = existing;
3510
+ return;
3511
+ }
3512
+ const created = createAsyncLocalStorage();
3513
+ if (created) {
3514
+ shared[SPAN_CONTEXT_STORAGE_SYMBOL] = created;
3515
+ asyncLocalStorage = created;
3516
+ }
3476
3517
  };
3477
3518
  var asyncLocalStorageReady = asyncStorageReady.then(() => {
3478
3519
  initializeAsyncContext();