bitfab 0.57.8 → 0.58.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/index.cjs CHANGED
@@ -51,7 +51,7 @@ var __version__, __packageName__;
51
51
  var init_version_generated = __esm({
52
52
  "src/version.generated.ts"() {
53
53
  "use strict";
54
- __version__ = "0.57.8";
54
+ __version__ = "0.58.0";
55
55
  __packageName__ = "bitfab";
56
56
  }
57
57
  });
@@ -1048,7 +1048,14 @@ function isTraceRootSpan(payload) {
1048
1048
  const parentId = rawSpanOf(payload)?.parent_id;
1049
1049
  return parentId === void 0 || parentId === null;
1050
1050
  }
1051
- var SIMULATION_PLAN_REFRESH_MS, SIMULATION_PLAN_RETRY_MS, SIMULATION_PLAN_READ_TIMEOUT_MS, SIMULATION_PLAN_MAX_HELD, DISABLE_SIMULATION_PLAN_ENV, ROOT_TRACE_FUNCTION_KEY_FIELD, CONTENT_OFF_KEY, CONTENT_KEYS, SimulationPlan;
1051
+ function declaredInCode(payload) {
1052
+ return payload[DECLARED_NODE_FIELD] === true;
1053
+ }
1054
+ function recordedError(payload) {
1055
+ const error = spanDataOf(payload)?.error;
1056
+ return error !== void 0 && error !== null;
1057
+ }
1058
+ var SIMULATION_PLAN_REFRESH_MS, SIMULATION_PLAN_RETRY_MS, SIMULATION_PLAN_READ_TIMEOUT_MS, SIMULATION_PLAN_MAX_HELD, DISABLE_SIMULATION_PLAN_ENV, ROOT_TRACE_FUNCTION_KEY_FIELD, DECLARED_NODE_FIELD, CONTENT_OFF_KEY, CONTENT_KEYS, SimulationPlan;
1052
1059
  var init_simulationPlan = __esm({
1053
1060
  "src/simulationPlan.ts"() {
1054
1061
  "use strict";
@@ -1063,6 +1070,7 @@ var init_simulationPlan = __esm({
1063
1070
  SIMULATION_PLAN_MAX_HELD = 1e3;
1064
1071
  DISABLE_SIMULATION_PLAN_ENV = "BITFAB_DISABLE_SIM_PLAN";
1065
1072
  ROOT_TRACE_FUNCTION_KEY_FIELD = "rootTraceFunctionKey";
1073
+ DECLARED_NODE_FIELD = "declaredNode";
1066
1074
  CONTENT_OFF_KEY = "content_off_by_simulation_plan";
1067
1075
  CONTENT_KEYS = [
1068
1076
  "input",
@@ -1234,7 +1242,7 @@ var init_simulationPlan = __esm({
1234
1242
  }
1235
1243
  }
1236
1244
  payloadToSend(entry) {
1237
- if (entry.traceFunctionKey === void 0) {
1245
+ if (entry.traceFunctionKey === void 0 || declaredInCode(entry.payload)) {
1238
1246
  return entry.payload;
1239
1247
  }
1240
1248
  if (this.contentOffByKey !== void 0) {
@@ -1246,6 +1254,10 @@ var init_simulationPlan = __esm({
1246
1254
  return stripContent(entry.payload);
1247
1255
  }
1248
1256
  submitEntry(entry) {
1257
+ if (entry.traceFunctionKey !== void 0 && this.skipsSpan(entry.payload, entry.traceFunctionKey)) {
1258
+ entry.drop?.();
1259
+ return;
1260
+ }
1249
1261
  try {
1250
1262
  entry.submit(this.payloadToSend(entry));
1251
1263
  } catch (error) {
@@ -1308,8 +1320,12 @@ var init_simulationPlan = __esm({
1308
1320
  contentOffNow(traceFunctionKey, spanName) {
1309
1321
  return !this.stopped && !this.disabled() && this.contentOff(traceFunctionKey, spanName);
1310
1322
  }
1323
+ skipsSpan(payload, traceFunctionKey) {
1324
+ const name = spanNameOf(payload);
1325
+ return name !== void 0 && this.contentOff(traceFunctionKey, name) && !isTraceRootSpan(payload) && !declaredInCode(payload) && spanInstrumentationOf(payload) !== void 0 && !recordedByFramework(payload) && !recordedError(payload);
1326
+ }
1311
1327
  apply(payload, traceFunctionKey) {
1312
- if (recordedByFramework(payload)) {
1328
+ if (recordedByFramework(payload) || declaredInCode(payload)) {
1313
1329
  return payload;
1314
1330
  }
1315
1331
  const name = spanNameOf(payload);
@@ -2588,7 +2604,6 @@ var init_http = __esm({
2588
2604
  this.closed = true;
2589
2605
  this.externalSpanTransform = void 0;
2590
2606
  this.externalTraceTransform = void 0;
2591
- this.frameworkSpanStart = void 0;
2592
2607
  this.releaseHeldExternalSpans = void 0;
2593
2608
  this.stopSimulationPlan = void 0;
2594
2609
  const transport = this.traceTransport;
@@ -2771,16 +2786,6 @@ var init_http = __esm({
2771
2786
  carrierMeta("internal_trace", body, void 0)
2772
2787
  );
2773
2788
  }
2774
- reserveFrameworkSpan(traceId, spanId, parentSpanId) {
2775
- try {
2776
- this.frameworkSpanStart?.(traceId, spanId, parentSpanId ?? null);
2777
- } catch (error) {
2778
- warnOnce(
2779
- "framework-span-reservation-failed",
2780
- `a framework span could not be counted toward its trace's span limit: ${describeError(error)}`
2781
- );
2782
- }
2783
- }
2784
2789
  sendExternalSpan(payload) {
2785
2790
  const ref2 = carrierRef(payload);
2786
2791
  if (ref2?.spanId !== void 0) {
@@ -2809,6 +2814,7 @@ var init_http = __esm({
2809
2814
  sdkVersion: __version__
2810
2815
  };
2811
2816
  delete body[ROOT_TRACE_FUNCTION_KEY_FIELD];
2817
+ delete body[DECLARED_NODE_FIELD];
2812
2818
  this.getTraceTransport()?.submit(
2813
2819
  "external_span",
2814
2820
  body,
@@ -4685,11 +4691,6 @@ var BitfabClaudeAgentHandler = class {
4685
4691
  }
4686
4692
  this.runToSpan.set(spanId, spanInfo);
4687
4693
  this.httpClient.traceCompletion.start(spanInfo.traceId, spanInfo.spanId);
4688
- this.httpClient.reserveFrameworkSpan(
4689
- spanInfo.traceId,
4690
- spanInfo.spanId,
4691
- spanInfo.parentId
4692
- );
4693
4694
  return spanInfo;
4694
4695
  }
4695
4696
  completeSpan(spanId, output, error, extraContexts) {
@@ -4969,11 +4970,6 @@ var BitfabClaudeAgentHandler = class {
4969
4970
  this.conversationHistory.push(...this.pendingMessages);
4970
4971
  this.pendingMessages = [];
4971
4972
  this.currentLlmSpanId = randomUuid();
4972
- this.httpClient.reserveFrameworkSpan(
4973
- this.ensureTrace(),
4974
- this.currentLlmSpanId,
4975
- this.getParentId()
4976
- );
4977
4973
  this.currentLlmMessageId = messageId ?? null;
4978
4974
  this.currentLlmContent = [];
4979
4975
  this.currentLlmModel = inner.model ?? null;
@@ -6384,11 +6380,6 @@ var BitfabLangGraphCallbackHandler = class {
6384
6380
  this.runToSpan.set(runId, spanInfo);
6385
6381
  if (spanInfo.hidden !== true) {
6386
6382
  this.httpClient.traceCompletion.start(spanInfo.traceId, spanInfo.spanId);
6387
- this.httpClient.reserveFrameworkSpan(
6388
- spanInfo.traceId,
6389
- spanInfo.spanId,
6390
- spanInfo.parentId
6391
- );
6392
6383
  }
6393
6384
  if (isRootInvocation) {
6394
6385
  this.sendTraceStart(spanInfo);
@@ -7937,11 +7928,6 @@ var BitfabOpenAITracingProcessor = class {
7937
7928
  const mapping = this.activeSpanMappings[span.traceId];
7938
7929
  const traceId = mapping?.traceId ?? span.traceId;
7939
7930
  this.httpClient.traceCompletion.start(traceId, span.spanId);
7940
- this.httpClient.reserveFrameworkSpan(
7941
- traceId,
7942
- span.spanId,
7943
- span.parentId ?? mapping?.spanId
7944
- );
7945
7931
  }
7946
7932
  }
7947
7933
  /**
@@ -8330,56 +8316,9 @@ var BitfabVercelAiHandler = class {
8330
8316
  init_warnOnce();
8331
8317
  var TRUNCATION_LIMIT_ORDER = [
8332
8318
  "max_depth",
8333
- "max_spans",
8334
8319
  "max_captured_subtree_spans"
8335
8320
  ];
8336
8321
  var activeTraceStates = /* @__PURE__ */ new Map();
8337
- var REFUSED_FRAMEWORK_SPAN_TRACES_RETAINED = 1024;
8338
- var refusedFrameworkSpans = /* @__PURE__ */ new Map();
8339
- function frameworkSpanRefused(traceId, spanId) {
8340
- return refusedFrameworkSpans.get(traceId)?.has(spanId) === true;
8341
- }
8342
- function refuseFrameworkSpan(traceId, spanId) {
8343
- const refused = refusedFrameworkSpans.get(traceId);
8344
- if (refused !== void 0) {
8345
- refused.add(spanId);
8346
- return;
8347
- }
8348
- refusedFrameworkSpans.set(traceId, /* @__PURE__ */ new Set([spanId]));
8349
- while (refusedFrameworkSpans.size > REFUSED_FRAMEWORK_SPAN_TRACES_RETAINED) {
8350
- const oldest = refusedFrameworkSpans.keys().next();
8351
- if (oldest.done) {
8352
- return;
8353
- }
8354
- refusedFrameworkSpans.delete(oldest.value);
8355
- }
8356
- }
8357
- function reserveFrameworkSpan(traceId, spanId, parentSpanId) {
8358
- const spanBudget = activeTraceStates.get(traceId)?.spanBudget;
8359
- const parentRefused = parentSpanId !== null && frameworkSpanRefused(traceId, parentSpanId);
8360
- if (!parentRefused && (spanBudget === void 0 || spanBudget.spansUsed < spanBudget.maxSpans)) {
8361
- if (spanBudget !== void 0) {
8362
- spanBudget.spansUsed += 1;
8363
- }
8364
- return;
8365
- }
8366
- refuseFrameworkSpan(traceId, spanId);
8367
- spanBudget?.truncate("max_spans");
8368
- }
8369
- function isRefusedFrameworkSpan(payload) {
8370
- if (!recordedByFramework(payload)) {
8371
- return false;
8372
- }
8373
- const rawSpan = payload.rawSpan;
8374
- const spanId = rawSpan?.id;
8375
- if (typeof spanId !== "string") {
8376
- return false;
8377
- }
8378
- const traceIds = [payload.sourceTraceId, traceIdOf(payload)];
8379
- return traceIds.some(
8380
- (traceId) => typeof traceId === "string" && frameworkSpanRefused(traceId, spanId)
8381
- );
8382
- }
8383
8322
  function truncationMetadata(truncation) {
8384
8323
  if (truncation === void 0 || truncation.limits.size === 0) {
8385
8324
  return void 0;
@@ -8769,7 +8708,6 @@ function readBooleanEnv(name) {
8769
8708
  return void 0;
8770
8709
  }
8771
8710
  var DEFAULT_AUTO_TRACE_MAX_DEPTH = 30;
8772
- var DEFAULT_AUTO_TRACE_MAX_SPANS = 1e4;
8773
8711
  var DEFAULT_AUTO_TRACE_MAX_CAPTURED_SUBTREE_SPANS = 500;
8774
8712
  function spanOwnerName(receiver) {
8775
8713
  if (typeof receiver === "function") {
@@ -8837,7 +8775,6 @@ var Bitfab = class {
8837
8775
  config.simulationPlan ?? true
8838
8776
  );
8839
8777
  this.httpClient.externalSpanTransform = (payload, submit, drop) => this.applySimulationPlan(payload, submit, drop);
8840
- this.httpClient.frameworkSpanStart = reserveFrameworkSpan;
8841
8778
  this.httpClient.externalTraceTransform = (payload, submit) => this.simulationPlan.sendTrace(payload, submit);
8842
8779
  this.httpClient.releaseHeldExternalSpans = (timeoutMs) => this.simulationPlan.release(timeoutMs);
8843
8780
  this.httpClient.stopSimulationPlan = () => this.simulationPlan.stop();
@@ -9000,21 +8937,9 @@ var Bitfab = class {
9000
8937
  options.maxDepth,
9001
8938
  DEFAULT_AUTO_TRACE_MAX_DEPTH
9002
8939
  );
9003
- if (options.maxSpans !== void 0 && options.maxSpans < 1) {
9004
- throw new BitfabError(
9005
- `maxSpans must be at least 1 because the root span counts toward it (got ${options.maxSpans})`
9006
- );
9007
- }
9008
- const maxSpans = autoTraceLimit(
9009
- options.maxSpans,
9010
- DEFAULT_AUTO_TRACE_MAX_SPANS
9011
- );
9012
- const maxCapturedSubtreeSpans = Math.min(
9013
- autoTraceLimit(
9014
- options.maxCapturedSubtreeSpans,
9015
- DEFAULT_AUTO_TRACE_MAX_CAPTURED_SUBTREE_SPANS
9016
- ),
9017
- maxSpans
8940
+ const maxCapturedSubtreeSpans = autoTraceLimit(
8941
+ options.maxCapturedSubtreeSpans,
8942
+ DEFAULT_AUTO_TRACE_MAX_CAPTURED_SUBTREE_SPANS
9018
8943
  );
9019
8944
  const rootIsAsync = fn.constructor.name === "AsyncFunction";
9020
8945
  const excluded = new Set(options.exclude ?? []);
@@ -9045,19 +8970,11 @@ var Bitfab = class {
9045
8970
  truncation.droppedSpans += 1;
9046
8971
  warnOnce(
9047
8972
  `auto-trace-truncated:${traceFunctionKey}`,
9048
- `"${traceFunctionKey}" hit an automatic subtree capture limit (maxDepth=${maxDepth}, maxCapturedSubtreeSpans=${maxCapturedSubtreeSpans}, maxSpans=${maxSpans}); its trace is incomplete. Raise the limits, declare the calls you need with node(), or narrow the subtree with exclude.`
8973
+ `"${traceFunctionKey}" hit an automatic subtree capture limit (maxDepth=${maxDepth}, maxCapturedSubtreeSpans=${maxCapturedSubtreeSpans}); its trace is incomplete. Raise the limits, declare the calls you need with node(), or narrow the subtree with exclude.`
9049
8974
  );
9050
8975
  };
9051
- const spanBudget = {
9052
- spansUsed: 1,
9053
- maxSpans,
9054
- truncate
9055
- };
9056
8976
  if (rootTraceState !== void 0) {
9057
8977
  rootTraceState.truncation = truncation;
9058
- if (rootTraceState.spanBudget === void 0) {
9059
- rootTraceState.spanBudget = spanBudget;
9060
- }
9061
8978
  }
9062
8979
  const spanStackOf = (entry) => entry.spanStack;
9063
8980
  const openChild = (entry, childOptions, inputs, isAsync, invokeFn) => {
@@ -9093,16 +9010,16 @@ var Bitfab = class {
9093
9010
  return innermost === void 0 ? traceFunctionKey : rootTraceFunctionKeyOfTrace(innermost.traceId, traceFunctionKey);
9094
9011
  };
9095
9012
  const recordsSpan = (entry, spanName, declared) => {
9096
- const countsTowardCapturedSubtree = !declared && !self.simulationPlan.contentOffNow(
9013
+ const contentOff = !declared && self.simulationPlan.contentOffNow(
9097
9014
  rootTraceFunctionKeyOfEntry(entry),
9098
9015
  spanName
9099
9016
  );
9100
- const refusedBy = entry.depth >= maxDepth ? "max_depth" : spanBudget.spansUsed >= maxSpans ? "max_spans" : countsTowardCapturedSubtree && capturedSubtreeCount >= maxCapturedSubtreeSpans ? "max_captured_subtree_spans" : void 0;
9017
+ const countsTowardCapturedSubtree = !declared && !contentOff;
9018
+ const refusedBy = entry.depth >= maxDepth ? "max_depth" : countsTowardCapturedSubtree && capturedSubtreeCount >= maxCapturedSubtreeSpans ? "max_captured_subtree_spans" : void 0;
9101
9019
  if (refusedBy !== void 0) {
9102
9020
  truncate(refusedBy);
9103
9021
  return false;
9104
9022
  }
9105
- spanBudget.spansUsed += 1;
9106
9023
  if (countsTowardCapturedSubtree) {
9107
9024
  capturedSubtreeCount += 1;
9108
9025
  }
@@ -9121,7 +9038,8 @@ var Bitfab = class {
9121
9038
  return clearAutoTraceNodeConfiguration(invokeFn);
9122
9039
  }
9123
9040
  const spanName = nodeConfiguration?.name ?? definition.name;
9124
- if (!recordsSpan(entry, spanName, nodeConfiguration !== void 0)) {
9041
+ const declaredNode = nodeConfiguration !== void 0;
9042
+ if (!recordsSpan(entry, spanName, declaredNode)) {
9125
9043
  return invokeWithoutNode();
9126
9044
  }
9127
9045
  const mockOnReplay = nodeConfiguration?.mockOnReplay ?? options.mockOnReplayDefault;
@@ -9134,6 +9052,7 @@ var Bitfab = class {
9134
9052
  functionId: definition.id,
9135
9053
  captureContent: true,
9136
9054
  autoTraceDefinition: definition,
9055
+ ...declaredNode && { declaredNode },
9137
9056
  ...nodeConfiguration?.testRunId !== void 0 && {
9138
9057
  testRunId: nodeConfiguration.testRunId
9139
9058
  },
@@ -9163,6 +9082,7 @@ var Bitfab = class {
9163
9082
  surface: "opt-out",
9164
9083
  instrumentation: "trace",
9165
9084
  captureContent: true,
9085
+ declaredNode: true,
9166
9086
  ...link !== void 0 && { nestedTrace: link }
9167
9087
  };
9168
9088
  return openChild(entry, childOptions, inputs, false, invokeFn);
@@ -9234,10 +9154,6 @@ var Bitfab = class {
9234
9154
  return autoTraceRoot;
9235
9155
  }
9236
9156
  applySimulationPlan(payload, submit, drop) {
9237
- if (isRefusedFrameworkSpan(payload)) {
9238
- drop();
9239
- return;
9240
- }
9241
9157
  this.simulationPlan.send(
9242
9158
  payload,
9243
9159
  rootTraceFunctionKeyOf(payload),
@@ -9912,9 +9828,6 @@ var Bitfab = class {
9912
9828
  registeredTraceId = traceId;
9913
9829
  }
9914
9830
  self.httpClient.traceCompletion.start(traceId, spanId);
9915
- if (isFrameworkInstrumentation(options.instrumentation)) {
9916
- reserveFrameworkSpan(traceId, spanId, parentSpanId);
9917
- }
9918
9831
  startedSpan = { traceId, spanId };
9919
9832
  const functionName = fn.name !== "" ? fn.name : void 0;
9920
9833
  const rootTraceFunctionKey = rootTraceFunctionKeyOfTrace(
@@ -9936,6 +9849,7 @@ var Bitfab = class {
9936
9849
  functionId: options.functionId,
9937
9850
  captureContent: options.captureContent ?? true,
9938
9851
  autoTraceDefinition: options.autoTraceDefinition,
9852
+ declaredNode: options.declaredNode === true,
9939
9853
  nestedTrace: options.nestedTrace,
9940
9854
  enclosingTrace: options.enclosingTrace
9941
9855
  };
@@ -10461,7 +10375,7 @@ var Bitfab = class {
10461
10375
  * Queued on the client's span transport; delivery is the transport's job.
10462
10376
  */
10463
10377
  sendWrapperSpan(params) {
10464
- const contentOff = this.contentOffBySimulationPlan(params);
10378
+ const contentOff = params.declaredNode !== true && this.contentOffBySimulationPlan(params);
10465
10379
  const buildsContent = params.captureContent && !contentOff;
10466
10380
  const serializedInputs = buildsContent ? serializeValue(params.inputs) : void 0;
10467
10381
  const serializedResult = buildsContent ? serializeValue(params.result) : void 0;
@@ -10535,6 +10449,7 @@ var Bitfab = class {
10535
10449
  ...params.rootTraceFunctionKey !== void 0 && {
10536
10450
  [ROOT_TRACE_FUNCTION_KEY_FIELD]: params.rootTraceFunctionKey
10537
10451
  },
10452
+ ...params.declaredNode === true && { [DECLARED_NODE_FIELD]: true },
10538
10453
  rawSpan: externalSpan,
10539
10454
  ...params.testRunId && { testRunId: params.testRunId },
10540
10455
  ...params.mocked && { mocked: true },