bitfab 0.38.3 → 0.38.4

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
@@ -2059,6 +2059,8 @@ interface SpanOptions {
2059
2059
  * real child.
2060
2060
  */
2061
2061
  mockOnReplay?: boolean;
2062
+ /** Optional test run ID included on the span and on the trace it starts. */
2063
+ testRunId?: string;
2062
2064
  /**
2063
2065
  * Record a serializable view of a non-serializable result (e.g. a live
2064
2066
  * stream object) as the span output.
@@ -2100,6 +2102,15 @@ interface SpanMethodDecoratorContext<TThis, TValue> {
2100
2102
  }
2101
2103
  /** A standard ECMAScript method decorator produced by {@link Bitfab.span}. */
2102
2104
  type SpanMethodDecorator = <TThis, TArgs extends unknown[], TReturn>(originalMethod: (this: TThis, ...args: TArgs) => TReturn, context: SpanMethodDecoratorContext<TThis, (this: TThis, ...args: TArgs) => TReturn>) => (this: TThis, ...args: TArgs) => TReturn;
2105
+ /** Trace-owned configuration for a function discovered beneath `trace()`. */
2106
+ interface NodeOptions extends Omit<SpanOptions, "captureWhen"> {
2107
+ /** Whether the enclosing trace captures this call. Defaults to true. */
2108
+ capture?: boolean;
2109
+ }
2110
+ type StandardNodeMethodDecorator = SpanMethodDecorator;
2111
+ type LegacyNodeMethodDecorator = <TThis, TArgs extends unknown[], TReturn>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(this: TThis, ...args: TArgs) => TReturn>) => void;
2112
+ /** A method decorator produced by {@link Bitfab.node}. */
2113
+ type NodeMethodDecorator = StandardNodeMethodDecorator & LegacyNodeMethodDecorator;
2103
2114
  /** Options for experimental automatic subtree tracing. */
2104
2115
  interface TraceOptions {
2105
2116
  /** Root span name. Defaults to the decorated or wrapped function name. */
@@ -2182,6 +2193,36 @@ declare class Bitfab {
2182
2193
  */
2183
2194
  withTrace<This, TArgs extends unknown[], TReturn>(traceFunctionKey: string, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2184
2195
  withTrace<This, TArgs extends unknown[], TReturn>(traceFunctionKey: string, options: TraceOptions, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2196
+ /**
2197
+ * Configure a transformed class method when it is discovered beneath a
2198
+ * {@link Bitfab.trace} root.
2199
+ *
2200
+ * The decorator creates no span or trace by itself. Beneath an active trace,
2201
+ * it can rename or retype the discovered call, capture its contents, mark it
2202
+ * for recorded-output replay, finalize its output, or omit it while leaving
2203
+ * captured descendants attached to the nearest captured parent.
2204
+ *
2205
+ * @param options - Trace-owned call configuration.
2206
+ * @experimental Automatic child-call instrumentation is experimental.
2207
+ */
2208
+ node(options?: NodeOptions): NodeMethodDecorator;
2209
+ /**
2210
+ * Configure a transformed standalone function when it is discovered beneath
2211
+ * a {@link Bitfab.trace} or {@link Bitfab.withTrace} root.
2212
+ *
2213
+ * This is the function-oriented equivalent of {@link Bitfab.node}. Without
2214
+ * an active automatic trace, the returned function runs normally and never
2215
+ * creates a span or trace. The function must be named so configuration can
2216
+ * be bound to its transformed definition without leaking to a descendant.
2217
+ *
2218
+ * @param optionsOrFn - Node options or the function to configure.
2219
+ * @param maybeFn - Function to configure when options are provided.
2220
+ * @experimental Automatic child-call instrumentation is experimental.
2221
+ */
2222
+ withNode<This, TArgs extends unknown[], TReturn>(fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2223
+ withNode<This, TArgs extends unknown[], TReturn>(options: NodeOptions, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2224
+ private resolveNodeConfiguration;
2225
+ private createAutoTraceNode;
2185
2226
  private createAutoTraceRoot;
2186
2227
  private refreshAutoTraceCapturePolicy;
2187
2228
  /**
@@ -2718,7 +2759,7 @@ declare class BitfabFunction {
2718
2759
  /**
2719
2760
  * SDK version from package.json (injected at build time)
2720
2761
  */
2721
- declare const __version__ = "0.38.3";
2762
+ declare const __version__ = "0.38.4";
2722
2763
 
2723
2764
  /**
2724
2765
  * Constants for the Bitfab SDK.
@@ -2821,4 +2862,4 @@ type ReplayRegistry = Record<string, ReplayRegistration>;
2821
2862
  */
2822
2863
  declare function defineReplayRegistry<TRegistry extends ReplayRegistry>(registry: TRegistry): TRegistry;
2823
2864
 
2824
- export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, BITFAB_PROGRESS_PREFIX, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CaptureWhen, type CapturedSpan, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbBranchOptions, DbBranchReplayError, type DbBranchTimings, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, HttpClient, type MockOverride, type MockOverrideCtx, type MockStrategy, type MockValue, type NodeMatcher, type ProviderDefinition, ReplayBranch, ReplayError, type ReplayItem, type ReplayItemFinishProgress, type ReplayItemStartProgress, type ReplayOptions, type ReplayOptionsFactory, type ReplayProgress, type ReplayProgressItem, type ReplayRegistration, type ReplayRegistry, type ReplayRegistryContext, type ReplayRegistryOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanLookup, type SpanMethodDecorator, type SpanMethodDecoratorContext, type SpanNodeMeta, type SpanOccurrence, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, defineReplayRegistry, finalizers, flushTraces, getCurrentReplayBranch, getCurrentSpan, getCurrentTrace, reportReplayProgress, serializeReplayResult };
2865
+ export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, BITFAB_PROGRESS_PREFIX, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CaptureWhen, type CapturedSpan, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbBranchOptions, DbBranchReplayError, type DbBranchTimings, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, HttpClient, type MockOverride, type MockOverrideCtx, type MockStrategy, type MockValue, type NodeMatcher, type NodeMethodDecorator, type NodeOptions, type ProviderDefinition, ReplayBranch, ReplayError, type ReplayItem, type ReplayItemFinishProgress, type ReplayItemStartProgress, type ReplayOptions, type ReplayOptionsFactory, type ReplayProgress, type ReplayProgressItem, type ReplayRegistration, type ReplayRegistry, type ReplayRegistryContext, type ReplayRegistryOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanLookup, type SpanMethodDecorator, type SpanMethodDecoratorContext, type SpanNodeMeta, type SpanOccurrence, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, defineReplayRegistry, finalizers, flushTraces, getCurrentReplayBranch, getCurrentSpan, getCurrentTrace, reportReplayProgress, serializeReplayResult };
package/dist/index.d.ts CHANGED
@@ -2059,6 +2059,8 @@ interface SpanOptions {
2059
2059
  * real child.
2060
2060
  */
2061
2061
  mockOnReplay?: boolean;
2062
+ /** Optional test run ID included on the span and on the trace it starts. */
2063
+ testRunId?: string;
2062
2064
  /**
2063
2065
  * Record a serializable view of a non-serializable result (e.g. a live
2064
2066
  * stream object) as the span output.
@@ -2100,6 +2102,15 @@ interface SpanMethodDecoratorContext<TThis, TValue> {
2100
2102
  }
2101
2103
  /** A standard ECMAScript method decorator produced by {@link Bitfab.span}. */
2102
2104
  type SpanMethodDecorator = <TThis, TArgs extends unknown[], TReturn>(originalMethod: (this: TThis, ...args: TArgs) => TReturn, context: SpanMethodDecoratorContext<TThis, (this: TThis, ...args: TArgs) => TReturn>) => (this: TThis, ...args: TArgs) => TReturn;
2105
+ /** Trace-owned configuration for a function discovered beneath `trace()`. */
2106
+ interface NodeOptions extends Omit<SpanOptions, "captureWhen"> {
2107
+ /** Whether the enclosing trace captures this call. Defaults to true. */
2108
+ capture?: boolean;
2109
+ }
2110
+ type StandardNodeMethodDecorator = SpanMethodDecorator;
2111
+ type LegacyNodeMethodDecorator = <TThis, TArgs extends unknown[], TReturn>(target: object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<(this: TThis, ...args: TArgs) => TReturn>) => void;
2112
+ /** A method decorator produced by {@link Bitfab.node}. */
2113
+ type NodeMethodDecorator = StandardNodeMethodDecorator & LegacyNodeMethodDecorator;
2103
2114
  /** Options for experimental automatic subtree tracing. */
2104
2115
  interface TraceOptions {
2105
2116
  /** Root span name. Defaults to the decorated or wrapped function name. */
@@ -2182,6 +2193,36 @@ declare class Bitfab {
2182
2193
  */
2183
2194
  withTrace<This, TArgs extends unknown[], TReturn>(traceFunctionKey: string, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2184
2195
  withTrace<This, TArgs extends unknown[], TReturn>(traceFunctionKey: string, options: TraceOptions, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2196
+ /**
2197
+ * Configure a transformed class method when it is discovered beneath a
2198
+ * {@link Bitfab.trace} root.
2199
+ *
2200
+ * The decorator creates no span or trace by itself. Beneath an active trace,
2201
+ * it can rename or retype the discovered call, capture its contents, mark it
2202
+ * for recorded-output replay, finalize its output, or omit it while leaving
2203
+ * captured descendants attached to the nearest captured parent.
2204
+ *
2205
+ * @param options - Trace-owned call configuration.
2206
+ * @experimental Automatic child-call instrumentation is experimental.
2207
+ */
2208
+ node(options?: NodeOptions): NodeMethodDecorator;
2209
+ /**
2210
+ * Configure a transformed standalone function when it is discovered beneath
2211
+ * a {@link Bitfab.trace} or {@link Bitfab.withTrace} root.
2212
+ *
2213
+ * This is the function-oriented equivalent of {@link Bitfab.node}. Without
2214
+ * an active automatic trace, the returned function runs normally and never
2215
+ * creates a span or trace. The function must be named so configuration can
2216
+ * be bound to its transformed definition without leaking to a descendant.
2217
+ *
2218
+ * @param optionsOrFn - Node options or the function to configure.
2219
+ * @param maybeFn - Function to configure when options are provided.
2220
+ * @experimental Automatic child-call instrumentation is experimental.
2221
+ */
2222
+ withNode<This, TArgs extends unknown[], TReturn>(fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2223
+ withNode<This, TArgs extends unknown[], TReturn>(options: NodeOptions, fn: (this: This, ...args: TArgs) => TReturn): (this: This, ...args: TArgs) => TReturn;
2224
+ private resolveNodeConfiguration;
2225
+ private createAutoTraceNode;
2185
2226
  private createAutoTraceRoot;
2186
2227
  private refreshAutoTraceCapturePolicy;
2187
2228
  /**
@@ -2718,7 +2759,7 @@ declare class BitfabFunction {
2718
2759
  /**
2719
2760
  * SDK version from package.json (injected at build time)
2720
2761
  */
2721
- declare const __version__ = "0.38.3";
2762
+ declare const __version__ = "0.38.4";
2722
2763
 
2723
2764
  /**
2724
2765
  * Constants for the Bitfab SDK.
@@ -2821,4 +2862,4 @@ type ReplayRegistry = Record<string, ReplayRegistration>;
2821
2862
  */
2822
2863
  declare function defineReplayRegistry<TRegistry extends ReplayRegistry>(registry: TRegistry): TRegistry;
2823
2864
 
2824
- export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, BITFAB_PROGRESS_PREFIX, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CaptureWhen, type CapturedSpan, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbBranchOptions, DbBranchReplayError, type DbBranchTimings, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, HttpClient, type MockOverride, type MockOverrideCtx, type MockStrategy, type MockValue, type NodeMatcher, type ProviderDefinition, ReplayBranch, ReplayError, type ReplayItem, type ReplayItemFinishProgress, type ReplayItemStartProgress, type ReplayOptions, type ReplayOptionsFactory, type ReplayProgress, type ReplayProgressItem, type ReplayRegistration, type ReplayRegistry, type ReplayRegistryContext, type ReplayRegistryOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanLookup, type SpanMethodDecorator, type SpanMethodDecoratorContext, type SpanNodeMeta, type SpanOccurrence, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, defineReplayRegistry, finalizers, flushTraces, getCurrentReplayBranch, getCurrentSpan, getCurrentTrace, reportReplayProgress, serializeReplayResult };
2865
+ export { type ActiveSpanContext, type AdaptContext, type AdaptInputsFn, type AllowedEnvVars, BITFAB_PROGRESS_PREFIX, type BamlExecutionResult, Bitfab, BitfabClaudeAgentHandler, type BitfabConfig, BitfabError, BitfabFunction, BitfabLangGraphCallbackHandler as BitfabLangChainCallbackHandler, BitfabLangGraphCallbackHandler, type BitfabLanguageModelMiddleware, BitfabOpenAIAgentHandler, BitfabOpenAITracingProcessor, BitfabVercelAiHandler, type CaptureWhen, type CapturedSpan, type CodeChangeFile, type CurrentSpan, type CurrentTrace, DEFAULT_SERVICE_URL, type DbBranchOptions, DbBranchReplayError, type DbBranchTimings, type DbSnapshotConfig, type DbSnapshotProvider, type DbSnapshotRef, type DetachedTrace, HttpClient, type MockOverride, type MockOverrideCtx, type MockStrategy, type MockValue, type NodeMatcher, type NodeMethodDecorator, type NodeOptions, type ProviderDefinition, ReplayBranch, ReplayError, type ReplayItem, type ReplayItemFinishProgress, type ReplayItemStartProgress, type ReplayOptions, type ReplayOptionsFactory, type ReplayProgress, type ReplayProgressItem, type ReplayRegistration, type ReplayRegistry, type ReplayRegistryContext, type ReplayRegistryOptions, type ReplayResult, SUPPORTED_PROVIDERS, type SpanLookup, type SpanMethodDecorator, type SpanMethodDecoratorContext, type SpanNodeMeta, type SpanOccurrence, type SpanOptions, type SpanType, type TokenUsage, type TraceResponse, type TracingProcessor, type VercelCallParams, type VercelGenerateResult, type VercelStreamResult, type WrapBAMLOptions, type WrappedBamlFn, __version__, defineReplayRegistry, finalizers, flushTraces, getCurrentReplayBranch, getCurrentSpan, getCurrentTrace, reportReplayProgress, serializeReplayResult };
package/dist/index.js CHANGED
@@ -21,8 +21,8 @@ import {
21
21
  getCurrentReplayBranch,
22
22
  getCurrentSpan,
23
23
  getCurrentTrace
24
- } from "./chunk-GSTRSZRO.js";
25
- import "./chunk-GFBQ2AMO.js";
24
+ } from "./chunk-PXAL7PPD.js";
25
+ import "./chunk-J47KPS77.js";
26
26
  import {
27
27
  BITFAB_PROGRESS_PREFIX,
28
28
  BitfabError,
@@ -34,7 +34,7 @@ import {
34
34
  flushTraces,
35
35
  reportReplayProgress,
36
36
  serializeReplayResult
37
- } from "./chunk-QNPM27RY.js";
37
+ } from "./chunk-LHFNHB2J.js";
38
38
  import "./chunk-H6LZRFMN.js";
39
39
  export {
40
40
  BITFAB_PROGRESS_PREFIX,
package/dist/node.cjs CHANGED
@@ -97,7 +97,7 @@ var __version__, __packageName__;
97
97
  var init_version_generated = __esm({
98
98
  "src/version.generated.ts"() {
99
99
  "use strict";
100
- __version__ = "0.38.3";
100
+ __version__ = "0.38.4";
101
101
  __packageName__ = "bitfab";
102
102
  }
103
103
  });
@@ -3811,6 +3811,29 @@ function runWithAutoTraceContext(context, fn, depth = 0) {
3811
3811
  autoTraceState.browserScope = previous;
3812
3812
  }
3813
3813
  }
3814
+ function runWithAutoTraceNodeConfiguration(nodeConfiguration, fn) {
3815
+ const scope = currentAutoTraceScope();
3816
+ if (!scope) {
3817
+ return fn();
3818
+ }
3819
+ const configuredScope = { ...scope, nodeConfiguration };
3820
+ let result;
3821
+ if (autoTraceState.storage) {
3822
+ result = autoTraceState.storage.run(configuredScope, fn);
3823
+ } else {
3824
+ const previous = autoTraceState.browserScope;
3825
+ autoTraceState.browserScope = configuredScope;
3826
+ try {
3827
+ result = fn();
3828
+ } finally {
3829
+ autoTraceState.browserScope = previous;
3830
+ }
3831
+ }
3832
+ if (isAutoTraceAsyncGenerator(result)) {
3833
+ return wrapAutoTraceNodeAsyncGenerator(nodeConfiguration, result);
3834
+ }
3835
+ return result;
3836
+ }
3814
3837
  function runWithAutoTraceRootContext(context, fn) {
3815
3838
  autoTraceState.activeRoots += 1;
3816
3839
  let result;
@@ -3849,6 +3872,29 @@ function wrapAutoTraceAsyncGenerator(context, source) {
3849
3872
  };
3850
3873
  return wrapped;
3851
3874
  }
3875
+ function wrapAutoTraceNodeAsyncGenerator(nodeConfiguration, source) {
3876
+ const step = (method, value) => runWithAutoTraceNodeConfiguration(
3877
+ nodeConfiguration,
3878
+ () => source[method](value)
3879
+ );
3880
+ const wrapped = {
3881
+ next: (value) => step("next", value),
3882
+ return: (value) => step("return", value),
3883
+ throw: (error) => step("throw", error),
3884
+ [Symbol.asyncIterator]: () => wrapped
3885
+ };
3886
+ return wrapped;
3887
+ }
3888
+ function __bitfabAutoTraceActive() {
3889
+ if (autoTraceState.activeRoots === 0) {
3890
+ return false;
3891
+ }
3892
+ return currentAutoTraceScope() !== void 0;
3893
+ }
3894
+ function currentAutoTraceScope() {
3895
+ initializeAutoTraceStorage();
3896
+ return autoTraceState.storage?.getStore() ?? autoTraceState.browserScope;
3897
+ }
3852
3898
  function __setBitfabAutoTraceCapturePolicy(client, traceFunctionKey, functionIds) {
3853
3899
  const policies = autoTraceState.capturePolicies.get(client) ?? /* @__PURE__ */ new Map();
3854
3900
  policies.set(traceFunctionKey, new Set(functionIds));
@@ -5701,6 +5747,102 @@ var Bitfab = class {
5701
5747
  const name = fn.name !== "" ? fn.name : traceFunctionKey;
5702
5748
  return this.createAutoTraceRoot(traceFunctionKey, name, options, fn);
5703
5749
  }
5750
+ /**
5751
+ * Configure a transformed class method when it is discovered beneath a
5752
+ * {@link Bitfab.trace} root.
5753
+ *
5754
+ * The decorator creates no span or trace by itself. Beneath an active trace,
5755
+ * it can rename or retype the discovered call, capture its contents, mark it
5756
+ * for recorded-output replay, finalize its output, or omit it while leaving
5757
+ * captured descendants attached to the nearest captured parent.
5758
+ *
5759
+ * @param options - Trace-owned call configuration.
5760
+ * @experimental Automatic child-call instrumentation is experimental.
5761
+ */
5762
+ node(options = {}) {
5763
+ const configuration = this.resolveNodeConfiguration(options);
5764
+ const decorator = (...args) => {
5765
+ if (args.length === 3) {
5766
+ const descriptor = args[2];
5767
+ if (!descriptor || typeof descriptor.value !== "function") {
5768
+ throw new BitfabError("@bitfab.node can only decorate methods");
5769
+ }
5770
+ if (!this.explicitlyEnabled) {
5771
+ return;
5772
+ }
5773
+ descriptor.value = this.createAutoTraceNode(
5774
+ configuration,
5775
+ descriptor.value,
5776
+ String(args[1])
5777
+ );
5778
+ return;
5779
+ }
5780
+ const method = args[0];
5781
+ const context = args[1];
5782
+ if (typeof method !== "function" || context?.kind !== "method") {
5783
+ throw new BitfabError("@bitfab.node can only decorate methods");
5784
+ }
5785
+ if (!this.explicitlyEnabled) {
5786
+ return method;
5787
+ }
5788
+ return this.createAutoTraceNode(
5789
+ configuration,
5790
+ method,
5791
+ String(context.name)
5792
+ );
5793
+ };
5794
+ return decorator;
5795
+ }
5796
+ withNode(optionsOrFn, maybeFn, internalFunctionName) {
5797
+ const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
5798
+ const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
5799
+ if (!fn) {
5800
+ throw new BitfabError("bitfab.withNode requires a function");
5801
+ }
5802
+ const configuration = this.resolveNodeConfiguration(options);
5803
+ if (!this.explicitlyEnabled) {
5804
+ return fn;
5805
+ }
5806
+ const functionName = internalFunctionName ?? fn.name;
5807
+ if (functionName === "") {
5808
+ throw new BitfabError(
5809
+ "bitfab.withNode requires a named function so the subtree transform can bind its configuration to the correct call."
5810
+ );
5811
+ }
5812
+ return this.createAutoTraceNode(configuration, fn, functionName);
5813
+ }
5814
+ resolveNodeConfiguration(options) {
5815
+ const capture = options.capture ?? true;
5816
+ if (!capture && options.mockOnReplay === true) {
5817
+ throw new BitfabError(
5818
+ "bitfab.node({ capture: false }) cannot use mockOnReplay: true because an uncaptured node has no recorded output."
5819
+ );
5820
+ }
5821
+ return {
5822
+ capture,
5823
+ type: options.type ?? "custom",
5824
+ ...options.name !== void 0 && { name: options.name },
5825
+ ...options.testRunId !== void 0 && {
5826
+ testRunId: options.testRunId
5827
+ },
5828
+ ...options.mockOnReplay !== void 0 && {
5829
+ mockOnReplay: options.mockOnReplay
5830
+ },
5831
+ ...options.finalize !== void 0 && { finalize: options.finalize }
5832
+ };
5833
+ }
5834
+ createAutoTraceNode(configuration, fn, functionName) {
5835
+ const nodeConfiguration = { ...configuration, functionName };
5836
+ return function(...args) {
5837
+ if (!__bitfabAutoTraceActive()) {
5838
+ return fn.apply(this, args);
5839
+ }
5840
+ return runWithAutoTraceNodeConfiguration(
5841
+ nodeConfiguration,
5842
+ () => fn.apply(this, args)
5843
+ );
5844
+ };
5845
+ }
5704
5846
  createAutoTraceRoot(traceFunctionKey, name, options, fn) {
5705
5847
  const self = this;
5706
5848
  const maxDepth = autoTraceLimit(
@@ -5739,29 +5881,51 @@ var Bitfab = class {
5739
5881
  );
5740
5882
  };
5741
5883
  const autoTraceContext = {
5742
- invoke(definition, inputs, invokeFn, depth) {
5884
+ invoke(definition, inputs, invokeFn, depth, nodeConfiguration) {
5743
5885
  const nameParts = definition.name.split(".");
5744
5886
  const simpleName = nameParts[nameParts.length - 1];
5745
- if (excluded.has(definition.name) || simpleName !== void 0 && excluded.has(simpleName) || definition.wrapper === true && !includeWrappers) {
5746
- return invokeFn();
5887
+ const invokeWithoutNode = () => nodeConfiguration === void 0 ? invokeFn() : runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
5888
+ if (excluded.has(definition.name) || simpleName !== void 0 && excluded.has(simpleName) || nodeConfiguration === void 0 && definition.wrapper === true && !includeWrappers) {
5889
+ return invokeWithoutNode();
5890
+ }
5891
+ if (nodeConfiguration?.capture === false) {
5892
+ return runWithAutoTraceContext(autoTraceContext, invokeFn, depth);
5747
5893
  }
5748
5894
  if (depth >= maxDepth || spansUsed >= maxSpans) {
5749
5895
  warnTruncated();
5750
- return invokeFn();
5896
+ return invokeWithoutNode();
5751
5897
  }
5752
5898
  spansUsed += 1;
5753
5899
  const childOptions = {
5754
- name: definition.name,
5755
- type: "function",
5900
+ name: nodeConfiguration?.name ?? definition.name,
5901
+ type: nodeConfiguration?.type ?? "function",
5756
5902
  captureWhen: "nested",
5757
5903
  functionId: definition.id,
5758
- captureContent: capturePolicy.has(definition.id),
5759
- autoTraceDefinition: definition
5904
+ captureContent: nodeConfiguration !== void 0 || capturePolicy.has(definition.id),
5905
+ autoTraceDefinition: definition,
5906
+ ...nodeConfiguration?.testRunId !== void 0 && {
5907
+ testRunId: nodeConfiguration.testRunId
5908
+ },
5909
+ ...nodeConfiguration?.mockOnReplay !== void 0 && {
5910
+ mockOnReplay: nodeConfiguration.mockOnReplay
5911
+ },
5912
+ ...nodeConfiguration?.finalize !== void 0 && {
5913
+ finalize: nodeConfiguration.finalize
5914
+ }
5760
5915
  };
5916
+ const invokeWithAutoTraceContext = () => runWithAutoTraceContext(autoTraceContext, invokeFn, depth + 1);
5917
+ if (definition.async === true) {
5918
+ const tracedAsyncChild = self.withSpan(
5919
+ traceFunctionKey,
5920
+ childOptions,
5921
+ async (..._inputs) => await invokeWithAutoTraceContext()
5922
+ );
5923
+ return tracedAsyncChild(...inputs);
5924
+ }
5761
5925
  const tracedChild = self.withSpan(
5762
5926
  traceFunctionKey,
5763
5927
  childOptions,
5764
- (..._inputs) => runWithAutoTraceContext(autoTraceContext, invokeFn, depth + 1)
5928
+ (..._inputs) => invokeWithAutoTraceContext()
5765
5929
  );
5766
5930
  return tracedChild(...inputs);
5767
5931
  }
@@ -6331,18 +6495,17 @@ var Bitfab = class {
6331
6495
  newStack = [...currentStack, newContext];
6332
6496
  const inputs = args;
6333
6497
  const startedAt = (/* @__PURE__ */ new Date()).toISOString();
6498
+ const replayCtxAtStart = getReplayContext();
6499
+ const testRunId = replayCtxAtStart?.testRunId ?? options.testRunId;
6334
6500
  if (isRootSpan && !activeTraceStates.has(traceId)) {
6335
- const replayCtxAtRoot = getReplayContext();
6336
6501
  const dbSnapshotRef = buildSnapshotRef(self.dbSnapshot, startedAt);
6337
6502
  activeTraceStates.set(traceId, {
6338
6503
  traceId,
6339
6504
  startedAt,
6340
6505
  contexts: [],
6341
- ...replayCtxAtRoot?.testRunId && {
6342
- testRunId: replayCtxAtRoot.testRunId
6343
- },
6344
- ...replayCtxAtRoot?.inputSourceTraceId && {
6345
- inputSourceTraceId: replayCtxAtRoot.inputSourceTraceId
6506
+ ...testRunId !== void 0 && { testRunId },
6507
+ ...replayCtxAtStart?.inputSourceTraceId && {
6508
+ inputSourceTraceId: replayCtxAtStart.inputSourceTraceId
6346
6509
  },
6347
6510
  dbSnapshotRef
6348
6511
  });
@@ -6375,9 +6538,7 @@ var Bitfab = class {
6375
6538
  contexts: newContext.contexts,
6376
6539
  prompt: newContext.prompt,
6377
6540
  endedAt,
6378
- ...replayCtx?.testRunId && {
6379
- testRunId: replayCtx.testRunId
6380
- },
6541
+ ...testRunId !== void 0 && { testRunId },
6381
6542
  ...replayCtx?.inputSourceSpanId && {
6382
6543
  inputSourceSpanId: replayCtx.inputSourceSpanId
6383
6544
  }