@bitfab/sdk 0.52.5 → 0.52.6

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 (36) hide show
  1. package/dist/autoTrace.cjs +84 -34
  2. package/dist/autoTrace.cjs.map +1 -1
  3. package/dist/autoTrace.d.cts +19 -4
  4. package/dist/autoTrace.d.ts +19 -4
  5. package/dist/autoTrace.js +9 -3
  6. package/dist/{chunk-BEAGKWXY.js → chunk-3TL4SATS.js} +136 -31
  7. package/dist/chunk-3TL4SATS.js.map +1 -0
  8. package/dist/{chunk-IHAB7SPF.js → chunk-7KIRLN47.js} +2 -2
  9. package/dist/{chunk-IHAB7SPF.js.map → chunk-7KIRLN47.js.map} +1 -1
  10. package/dist/{chunk-ITIMJ24H.js → chunk-A6KY2IFA.js} +3 -3
  11. package/dist/{chunk-ZUD7OFYB.js → chunk-E5BLYL5X.js} +81 -34
  12. package/dist/chunk-E5BLYL5X.js.map +1 -0
  13. package/dist/{chunk-VAIXHJ7O.js → chunk-RC6XLP6M.js} +2 -2
  14. package/dist/{chunk-VAIXHJ7O.js.map → chunk-RC6XLP6M.js.map} +1 -1
  15. package/dist/{chunk-THKLMOHS.js → chunk-WYC4QRK3.js} +2 -2
  16. package/dist/{http-KZU56HZA.js → http-EL26HG5B.js} +2 -2
  17. package/dist/{http-6Y3IQQ2Z.js → http-YMBQ4XJO.js} +2 -2
  18. package/dist/index.cjs +182 -48
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -1
  21. package/dist/index.d.ts +1 -1
  22. package/dist/index.js +4 -4
  23. package/dist/node.cjs +182 -48
  24. package/dist/node.cjs.map +1 -1
  25. package/dist/node.js +4 -4
  26. package/dist/{replay-LZ7DIY5L.js → replay-DXDWFFJK.js} +3 -3
  27. package/dist/replayCli.js +2 -2
  28. package/dist/seedCli.js +2 -2
  29. package/package.json +1 -1
  30. package/dist/chunk-BEAGKWXY.js.map +0 -1
  31. package/dist/chunk-ZUD7OFYB.js.map +0 -1
  32. /package/dist/{chunk-ITIMJ24H.js.map → chunk-A6KY2IFA.js.map} +0 -0
  33. /package/dist/{chunk-THKLMOHS.js.map → chunk-WYC4QRK3.js.map} +0 -0
  34. /package/dist/{http-6Y3IQQ2Z.js.map → http-EL26HG5B.js.map} +0 -0
  35. /package/dist/{http-KZU56HZA.js.map → http-YMBQ4XJO.js.map} +0 -0
  36. /package/dist/{replay-LZ7DIY5L.js.map → replay-DXDWFFJK.js.map} +0 -0
package/dist/index.d.cts CHANGED
@@ -3326,7 +3326,7 @@ interface CommitRef {
3326
3326
  /**
3327
3327
  * SDK version from package.json (injected at build time)
3328
3328
  */
3329
- declare const __version__ = "0.52.5";
3329
+ declare const __version__ = "0.52.6";
3330
3330
 
3331
3331
  /**
3332
3332
  * Constants for the Bitfab SDK.
package/dist/index.d.ts CHANGED
@@ -3326,7 +3326,7 @@ interface CommitRef {
3326
3326
  /**
3327
3327
  * SDK version from package.json (injected at build time)
3328
3328
  */
3329
- declare const __version__ = "0.52.5";
3329
+ declare const __version__ = "0.52.6";
3330
3330
 
3331
3331
  /**
3332
3332
  * Constants for the Bitfab SDK.
package/dist/index.js CHANGED
@@ -19,8 +19,8 @@ import {
19
19
  getCurrentTrace,
20
20
  reseedFromRegistry,
21
21
  seedFromRegistry
22
- } from "./chunk-BEAGKWXY.js";
23
- import "./chunk-ZUD7OFYB.js";
22
+ } from "./chunk-3TL4SATS.js";
23
+ import "./chunk-E5BLYL5X.js";
24
24
  import {
25
25
  BITFAB_PROGRESS_PREFIX,
26
26
  DbBranchReplayError,
@@ -28,7 +28,7 @@ import {
28
28
  ReplayError,
29
29
  reportReplayProgress,
30
30
  serializeReplayResult
31
- } from "./chunk-THKLMOHS.js";
31
+ } from "./chunk-WYC4QRK3.js";
32
32
  import {
33
33
  BitfabError,
34
34
  DEFAULT_SERVICE_URL,
@@ -36,7 +36,7 @@ import {
36
36
  MixedTracingError,
37
37
  __version__,
38
38
  flushTraces
39
- } from "./chunk-IHAB7SPF.js";
39
+ } from "./chunk-7KIRLN47.js";
40
40
  import "./chunk-H6LZRFMN.js";
41
41
  export {
42
42
  BITFAB_PROGRESS_PREFIX,
package/dist/node.cjs CHANGED
@@ -88,7 +88,7 @@ var __version__, __packageName__;
88
88
  var init_version_generated = __esm({
89
89
  "src/version.generated.ts"() {
90
90
  "use strict";
91
- __version__ = "0.52.5";
91
+ __version__ = "0.52.6";
92
92
  __packageName__ = "@bitfab/sdk";
93
93
  }
94
94
  });
@@ -4212,19 +4212,18 @@ init_asyncStorage();
4212
4212
  // src/autoTrace.ts
4213
4213
  init_asyncStorage();
4214
4214
  var autoTraceGlobal = globalThis;
4215
- var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV3 ?? {
4215
+ var autoTraceState = autoTraceGlobal.__bitfabAutoTraceStateV4 ?? {
4216
4216
  storage: null,
4217
4217
  browserScope: void 0,
4218
4218
  capturePolicies: /* @__PURE__ */ new WeakMap(),
4219
4219
  activeRoots: 0
4220
4220
  };
4221
- autoTraceGlobal.__bitfabAutoTraceStateV3 = autoTraceState;
4221
+ autoTraceGlobal.__bitfabAutoTraceStateV4 = autoTraceState;
4222
4222
  function initializeAutoTraceStorage() {
4223
4223
  autoTraceState.storage ?? (autoTraceState.storage = createAsyncLocalStorage());
4224
4224
  }
4225
- function runWithAutoTraceContext(context, fn, depth = 0) {
4225
+ function runWithAutoTraceScope(scope, fn) {
4226
4226
  initializeAutoTraceStorage();
4227
- const scope = { context, depth };
4228
4227
  if (autoTraceState.storage) {
4229
4228
  return autoTraceState.storage.run(scope, fn);
4230
4229
  }
@@ -4236,41 +4235,65 @@ function runWithAutoTraceContext(context, fn, depth = 0) {
4236
4235
  autoTraceState.browserScope = previous;
4237
4236
  }
4238
4237
  }
4238
+ function currentAutoTraceEntries() {
4239
+ return currentAutoTraceScope()?.entries ?? [];
4240
+ }
4241
+ function replaceEntry(entries, previous, updated) {
4242
+ return entries.map((entry) => entry === previous ? updated : entry);
4243
+ }
4244
+ function runWithAutoTraceEntry(previous, updated, fn) {
4245
+ const result = runWithAutoTraceScope(
4246
+ { entries: replaceEntry(currentAutoTraceEntries(), previous, updated) },
4247
+ fn
4248
+ );
4249
+ if (isAutoTraceAsyncGenerator(result)) {
4250
+ return wrapAutoTraceEntryAsyncGenerator(previous, updated, result);
4251
+ }
4252
+ return result;
4253
+ }
4254
+ function wrapAutoTraceEntryAsyncGenerator(previous, updated, source) {
4255
+ const step = (method, value) => runWithAutoTraceScope(
4256
+ { entries: replaceEntry(currentAutoTraceEntries(), previous, updated) },
4257
+ () => source[method](value)
4258
+ );
4259
+ const wrapped = {
4260
+ next: (value) => step("next", value),
4261
+ return: (value) => step("return", value),
4262
+ throw: (error) => step("throw", error),
4263
+ [Symbol.asyncIterator]: () => wrapped
4264
+ };
4265
+ return wrapped;
4266
+ }
4267
+ function clearAutoTraceNodeConfiguration(fn) {
4268
+ return runWithAutoTraceScope({ entries: currentAutoTraceEntries() }, fn);
4269
+ }
4239
4270
  function runWithAutoTraceNodeConfiguration(nodeConfiguration, fn) {
4240
4271
  const scope = currentAutoTraceScope();
4241
- if (!scope) {
4272
+ if (!scope || scope.entries.length === 0) {
4242
4273
  return fn();
4243
4274
  }
4244
- const configuredScope = { ...scope, nodeConfiguration };
4245
- let result;
4246
- if (autoTraceState.storage) {
4247
- result = autoTraceState.storage.run(configuredScope, fn);
4248
- } else {
4249
- const previous = autoTraceState.browserScope;
4250
- autoTraceState.browserScope = configuredScope;
4251
- try {
4252
- result = fn();
4253
- } finally {
4254
- autoTraceState.browserScope = previous;
4255
- }
4256
- }
4275
+ const result = runWithAutoTraceScope({ ...scope, nodeConfiguration }, fn);
4257
4276
  if (isAutoTraceAsyncGenerator(result)) {
4258
4277
  return wrapAutoTraceNodeAsyncGenerator(nodeConfiguration, result);
4259
4278
  }
4260
4279
  return result;
4261
4280
  }
4262
- function runWithAutoTraceRootContext(context, fn) {
4281
+ function runWithAutoTraceRootContext(context, spanStack, fn) {
4263
4282
  autoTraceState.activeRoots += 1;
4283
+ const entries = [
4284
+ ...currentAutoTraceEntries(),
4285
+ { context, depth: 0, spanStack }
4286
+ ];
4264
4287
  let result;
4265
4288
  try {
4266
- result = runWithAutoTraceContext(context, fn);
4289
+ result = runWithAutoTraceScope({ entries }, fn);
4267
4290
  } catch (error) {
4268
4291
  autoTraceState.activeRoots -= 1;
4269
4292
  throw error;
4270
4293
  }
4271
4294
  if (isAutoTraceAsyncGenerator(result)) {
4272
4295
  autoTraceState.activeRoots -= 1;
4273
- return wrapAutoTraceAsyncGenerator(context, result);
4296
+ return wrapAutoTraceAsyncGenerator(context, spanStack, result);
4274
4297
  }
4275
4298
  if (result instanceof Promise) {
4276
4299
  return result.finally(() => {
@@ -4280,6 +4303,15 @@ function runWithAutoTraceRootContext(context, fn) {
4280
4303
  autoTraceState.activeRoots -= 1;
4281
4304
  return result;
4282
4305
  }
4306
+ function invokeThroughAutoTraceEntries(entries, step, fn) {
4307
+ let run = fn;
4308
+ for (let index = entries.length - 1; index >= 0; index -= 1) {
4309
+ const entry = entries[index];
4310
+ const next = run;
4311
+ run = () => step(entry, next);
4312
+ }
4313
+ return run();
4314
+ }
4283
4315
  function isAutoTraceAsyncGenerator(value) {
4284
4316
  if (value === null || typeof value !== "object") {
4285
4317
  return false;
@@ -4287,8 +4319,8 @@ function isAutoTraceAsyncGenerator(value) {
4287
4319
  const candidate = value;
4288
4320
  return typeof candidate.next === "function" && typeof candidate.return === "function" && typeof candidate.throw === "function" && typeof candidate[Symbol.asyncIterator] === "function";
4289
4321
  }
4290
- function wrapAutoTraceAsyncGenerator(context, source) {
4291
- const step = (method, value) => runWithAutoTraceRootContext(context, () => source[method](value));
4322
+ function wrapAutoTraceAsyncGenerator(context, spanStack, source) {
4323
+ const step = (method, value) => runWithAutoTraceRootContext(context, spanStack, () => source[method](value));
4292
4324
  const wrapped = {
4293
4325
  next: (value) => step("next", value),
4294
4326
  return: (value) => step("return", value),
@@ -4314,7 +4346,7 @@ function __bitfabAutoTraceActive() {
4314
4346
  if (autoTraceState.activeRoots === 0) {
4315
4347
  return false;
4316
4348
  }
4317
- return currentAutoTraceScope() !== void 0;
4349
+ return currentAutoTraceEntries().length > 0;
4318
4350
  }
4319
4351
  function currentAutoTraceScope() {
4320
4352
  initializeAutoTraceStorage();
@@ -7067,11 +7099,15 @@ var Bitfab = class {
7067
7099
  type: options.type ?? "custom",
7068
7100
  surface: "opt-out"
7069
7101
  };
7070
- const tracedRoot = this.withSpan(
7102
+ const rootName = options.name ?? name;
7103
+ const buildTracedRoot = (spanOptions) => this.withSpan(
7071
7104
  traceFunctionKey,
7072
- rootOptions,
7105
+ spanOptions,
7073
7106
  function(...args) {
7074
- const capturePolicy = getAutoTraceCapturePolicy(self, traceFunctionKey);
7107
+ const capturePolicy = getAutoTraceCapturePolicy(
7108
+ self,
7109
+ traceFunctionKey
7110
+ );
7075
7111
  self.refreshAutoTraceCapturePolicy(traceFunctionKey);
7076
7112
  let spansUsed = 0;
7077
7113
  let truncated = false;
@@ -7092,19 +7128,54 @@ var Bitfab = class {
7092
7128
  `"${traceFunctionKey}" hit an automatic subtree capture limit (maxDepth=${maxDepth}, maxSpans=${maxSpans}); its trace is incomplete. Raise the limits or narrow the subtree with exclude.`
7093
7129
  );
7094
7130
  };
7131
+ const spanStackOf = (entry) => entry.spanStack;
7132
+ const openChild = (entry, childOptions, inputs, isAsync, invokeFn) => {
7133
+ const runInside = () => runWithAutoTraceEntry(
7134
+ entry,
7135
+ {
7136
+ context: autoTraceContext,
7137
+ depth: entry.depth + 1,
7138
+ spanStack: getSpanStack()
7139
+ },
7140
+ invokeFn
7141
+ );
7142
+ return runWithSpanStack(spanStackOf(entry), () => {
7143
+ if (isAsync) {
7144
+ const tracedAsyncChild = self.withSpan(
7145
+ traceFunctionKey,
7146
+ childOptions,
7147
+ async (..._inputs) => await runInside()
7148
+ );
7149
+ return tracedAsyncChild(...inputs);
7150
+ }
7151
+ const tracedChild = self.withSpan(
7152
+ traceFunctionKey,
7153
+ childOptions,
7154
+ (..._inputs) => runInside()
7155
+ );
7156
+ return tracedChild(...inputs);
7157
+ });
7158
+ };
7159
+ const overBudget = (entry) => {
7160
+ if (entry.depth < maxDepth && spansUsed < maxSpans) {
7161
+ return false;
7162
+ }
7163
+ runWithSpanStack(spanStackOf(entry), warnTruncated);
7164
+ return true;
7165
+ };
7095
7166
  const autoTraceContext = {
7096
- invoke(definition, inputs, invokeFn, depth, nodeConfiguration) {
7167
+ traceFunctionKey,
7168
+ invoke(definition, inputs, invokeFn, entry, nodeConfiguration) {
7097
7169
  const nameParts = definition.name.split(".");
7098
7170
  const simpleName = nameParts[nameParts.length - 1];
7099
- const invokeWithoutNode = () => nodeConfiguration === void 0 ? invokeFn() : runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
7171
+ const invokeWithoutNode = () => nodeConfiguration === void 0 ? invokeFn() : clearAutoTraceNodeConfiguration(invokeFn);
7100
7172
  if (excluded.has(definition.name) || simpleName !== void 0 && excluded.has(simpleName) || nodeConfiguration === void 0 && definition.wrapper === true && !includeWrappers) {
7101
7173
  return invokeWithoutNode();
7102
7174
  }
7103
7175
  if (nodeConfiguration?.capture === false) {
7104
- return runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
7176
+ return clearAutoTraceNodeConfiguration(invokeFn);
7105
7177
  }
7106
- if (depth >= maxDepth || spansUsed >= maxSpans) {
7107
- warnTruncated();
7178
+ if (overBudget(entry)) {
7108
7179
  return invokeWithoutNode();
7109
7180
  }
7110
7181
  spansUsed += 1;
@@ -7127,34 +7198,81 @@ var Bitfab = class {
7127
7198
  finalize: nodeConfiguration.finalize
7128
7199
  }
7129
7200
  };
7130
- const invokeWithAutoTraceContext = () => runWithAutoTraceContext(autoTraceContext, invokeFn, depth + 1);
7131
- if (definition.async === true) {
7132
- const tracedAsyncChild = self.withSpan(
7133
- traceFunctionKey,
7134
- childOptions,
7135
- async (..._inputs) => await invokeWithAutoTraceContext()
7136
- );
7137
- return tracedAsyncChild(...inputs);
7138
- }
7139
- const tracedChild = self.withSpan(
7140
- traceFunctionKey,
7201
+ return openChild(
7202
+ entry,
7141
7203
  childOptions,
7142
- (..._inputs) => invokeWithAutoTraceContext()
7204
+ inputs,
7205
+ definition.async === true,
7206
+ invokeFn
7143
7207
  );
7144
- return tracedChild(...inputs);
7208
+ },
7209
+ invokeNestedRoot(nestedName, nestedFunctionName, inputs, invokeFn, entry, link) {
7210
+ if (excluded.has(nestedName) || excluded.has(nestedFunctionName) || overBudget(entry)) {
7211
+ return invokeFn();
7212
+ }
7213
+ spansUsed += 1;
7214
+ const childOptions = {
7215
+ name: nestedName,
7216
+ type: "function",
7217
+ captureWhen: "nested",
7218
+ surface: "opt-out",
7219
+ captureContent: true,
7220
+ ...link !== void 0 && { nestedTrace: link }
7221
+ };
7222
+ return openChild(entry, childOptions, inputs, false, invokeFn);
7145
7223
  }
7146
7224
  };
7147
7225
  return runWithAutoTraceRootContext(
7148
7226
  autoTraceContext,
7227
+ getSpanStack(),
7149
7228
  () => fn.apply(this, args)
7150
7229
  );
7151
7230
  }
7152
7231
  );
7232
+ const tracedRoot = buildTracedRoot(rootOptions);
7153
7233
  const autoTraceRoot = function(...args) {
7154
7234
  if (!self.shouldRecord()) {
7155
7235
  return fn.apply(this, args);
7156
7236
  }
7157
- return tracedRoot.apply(this, args);
7237
+ const enclosing = currentAutoTraceEntries();
7238
+ if (enclosing.length === 0) {
7239
+ return tracedRoot.apply(this, args);
7240
+ }
7241
+ const link = getReplayContext() || inSeedScope() ? void 0 : { traceFunctionKey };
7242
+ return invokeThroughAutoTraceEntries(
7243
+ enclosing,
7244
+ (entry, next) => entry.context.invokeNestedRoot(
7245
+ rootName,
7246
+ name,
7247
+ args,
7248
+ next,
7249
+ entry,
7250
+ link
7251
+ ),
7252
+ () => {
7253
+ if (link === void 0) {
7254
+ return fn.apply(this, args);
7255
+ }
7256
+ const innermost = enclosing[enclosing.length - 1];
7257
+ const innermostNow = currentAutoTraceEntries().find(
7258
+ (entry) => entry.context === innermost.context
7259
+ ) ?? innermost;
7260
+ const innermostStack = innermostNow.spanStack;
7261
+ const copy = innermostStack[innermostStack.length - 1];
7262
+ const detachedRoot = buildTracedRoot({
7263
+ ...rootOptions,
7264
+ ...copy !== void 0 && {
7265
+ enclosingTrace: {
7266
+ traceId: copy.traceId,
7267
+ spanId: copy.spanId,
7268
+ traceFunctionKey: innermost.context.traceFunctionKey,
7269
+ link
7270
+ }
7271
+ }
7272
+ });
7273
+ return runWithSpanStack([], () => detachedRoot.apply(this, args));
7274
+ }
7275
+ );
7158
7276
  };
7159
7277
  Object.defineProperty(autoTraceRoot, "_bitfabTraceFunctionKey", {
7160
7278
  value: traceFunctionKey
@@ -7743,6 +7861,10 @@ var Bitfab = class {
7743
7861
  const spanId = randomUuid();
7744
7862
  const parentSpanId = parentContext?.spanId ?? null;
7745
7863
  const isRootSpan = parentSpanId === null;
7864
+ if (options.enclosingTrace !== void 0) {
7865
+ options.enclosingTrace.link.traceId = traceId;
7866
+ options.enclosingTrace.link.rootSpanId = spanId;
7867
+ }
7746
7868
  const requestedSurface = options.surface ?? DEFAULT_SURFACE;
7747
7869
  const surface = resolveSurface(requestedSurface, parentContext?.surface);
7748
7870
  assertSurfacesCompatible(
@@ -7792,7 +7914,9 @@ var Bitfab = class {
7792
7914
  spanType: options.type ?? "custom",
7793
7915
  functionId: options.functionId,
7794
7916
  captureContent: options.captureContent ?? true,
7795
- autoTraceDefinition: options.autoTraceDefinition
7917
+ autoTraceDefinition: options.autoTraceDefinition,
7918
+ nestedTrace: options.nestedTrace,
7919
+ enclosingTrace: options.enclosingTrace
7796
7920
  };
7797
7921
  const sendSpan = async (params) => {
7798
7922
  const replayCtx = getReplayContext();
@@ -8302,6 +8426,16 @@ var Bitfab = class {
8302
8426
  function_line: params.autoTraceDefinition.line,
8303
8427
  function_column: params.autoTraceDefinition.column
8304
8428
  },
8429
+ ...params.nestedTrace?.traceId !== void 0 && {
8430
+ nested_trace_id: params.nestedTrace.traceId,
8431
+ nested_trace_function_key: params.nestedTrace.traceFunctionKey,
8432
+ nested_root_span_id: params.nestedTrace.rootSpanId
8433
+ },
8434
+ ...params.enclosingTrace !== void 0 && {
8435
+ enclosing_trace_id: params.enclosingTrace.traceId,
8436
+ enclosing_trace_function_key: params.enclosingTrace.traceFunctionKey,
8437
+ enclosing_span_id: params.enclosingTrace.spanId
8438
+ },
8305
8439
  ...serializedInputs !== void 0 && {
8306
8440
  input: serializedInputs.json,
8307
8441
  ...serializedInputs.meta !== void 0 && {