bitfab 0.76.7 → 0.76.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72b685fa78be09c364e02ada044f2b0a7f3f5080fd2a85158481c1159a2abf05
4
- data.tar.gz: a68a729e616ae128f97ffcb52479724b1e2d58e0de3cd4e8b52f13a459b24346
3
+ metadata.gz: dd1e830fb9af8fd421f465e0be57b7cd3136eaf098cc39ae7a403edce55c3695
4
+ data.tar.gz: 07033b21b9f0f0cdcdc8d66cb794833c1e688aacc6f1bae5ae9f761ec2333456
5
5
  SHA512:
6
- metadata.gz: 619206241cb6210f3bf5d236587fefa41aef49ad53c330b37d6aae716cbe3a5e09bcefc2e9a8cc04353d5d8d5e7128f7386bd6b250badbd71b1b354f09e26d82
7
- data.tar.gz: 38f2cc6e2d31f99e1528086a4721ace25a114de284d774f3474ca3a75c769f2faf4f4b9dc2d3409e892ef1b52ab8ad8919928b1692418fa4afc2cee3705d5f5a
6
+ metadata.gz: 36fc4df83664eacaf63aa24aa7385fbf346654115ca39ac66bc0c0c59bb3002d4347aedb40bab0757c5a9586a3985ff1548f90eba157a825515cf3272e33d419
7
+ data.tar.gz: fc07dfaea53359dde2645caa9f196469899d4af720410fb6c94d08d9f19f7da54f0aeac3b03ee87b4b595c895f3ea52ea539afbe960e1bc0bcaac8ac0b15b99d
data/lib/bitfab/client.rb CHANGED
@@ -1060,7 +1060,7 @@ module Bitfab
1060
1060
  span_name:, span_type:, function_name:, contexts:, prompt:, args:, kwargs:, result:, error:,
1061
1061
  started_at:, ended_at:, metadata: nil, experiment_id: nil, input_source_span_id: nil, mocked: false,
1062
1062
  mock_target: nil, mock_source: nil, span_data: {}, instrumentation: "span", declared_node: false,
1063
- nearest_captured_ancestor_span_id: nil)
1063
+ nearest_captured_ancestor_span_id: nil, on_payload_bytes: nil)
1064
1064
  extra_span_data = span_data
1065
1065
  # If drop() was called on this trace, suppress the span PAYLOAD upload for
1066
1066
  # every span that completes after the flag was set. The trace completion
@@ -1082,8 +1082,9 @@ module Bitfab
1082
1082
  span_data.merge!(extra_span_data.except(*SimulationPlan::CONTENT_KEYS))
1083
1083
  span_data["content_off_by_simulation_plan"] = true
1084
1084
  else
1085
- human_inputs, input_dropped, (kept_args, kept_kwargs) = Serialize.serialize_inputs_with_report(args, kwargs)
1086
- human_output, output_dropped = Serialize.serialize_value_with_report(result)
1085
+ human_inputs, input_dropped, (kept_args, kept_kwargs), input_size = Serialize.serialize_inputs_with_report(args, kwargs)
1086
+ human_output, output_dropped, output_size = Serialize.serialize_value_with_report(result)
1087
+ on_payload_bytes&.call(input_size + output_size)
1087
1088
  kept_result = Serialize.too_large?(output_dropped) ? human_output : result
1088
1089
  dropped = input_dropped + output_dropped
1089
1090
 
@@ -1370,9 +1371,16 @@ module Bitfab
1370
1371
  end
1371
1372
 
1372
1373
  # Experimentally trace an existing method and its first-party call subtree.
1374
+ # @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
1375
+ # inputs and outputs that one automatically captured function may record
1376
+ # within a single call of the root. Defaults to 250,000,000. Once a function
1377
+ # has recorded this much, its later calls beneath that root call are not
1378
+ # recorded, so a helper called in a loop with a large argument cannot fill
1379
+ # memory. Nodes declared with bitfab_node and LLM calls do not count toward it.
1373
1380
  def trace(klass, method_name, name: nil, type: "custom",
1374
1381
  max_depth: Subtree::DEFAULT_MAX_DEPTH,
1375
1382
  max_captured_subtree_spans: Subtree::DEFAULT_MAX_CAPTURED_SUBTREE_SPANS,
1383
+ max_payload_bytes_per_function: Subtree::DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION,
1376
1384
  exclude: [], include_wrappers: false, mock_on_replay_default: false)
1377
1385
  Bitfab::Traceable.trace(
1378
1386
  klass, method_name,
@@ -1381,6 +1389,7 @@ module Bitfab
1381
1389
  type:,
1382
1390
  max_depth:,
1383
1391
  max_captured_subtree_spans:,
1392
+ max_payload_bytes_per_function:,
1384
1393
  exclude:,
1385
1394
  include_wrappers:,
1386
1395
  mock_on_replay_default:,
@@ -58,7 +58,8 @@ module Bitfab
58
58
  end
59
59
 
60
60
  # Like serialize_value, but also reports what could not be faithfully
61
- # captured. Returns [result, dropped] where dropped lists the class name (or
61
+ # captured. Returns [result, dropped, size] where size is the byte length of
62
+ # result's JSON encoding and dropped lists the class name (or
62
63
  # "max_depth"/"too_large") behind every placeholder the walk had to emit. A
63
64
  # non-empty dropped means the capture is lossy, so the send boundary marks
64
65
  # the span serialization_degraded (non-replayable) instead of shipping it as
@@ -74,17 +75,29 @@ module Bitfab
74
75
  dropped = []
75
76
  budget = Budget.new(1, 1)
76
77
  result = catch(budget) { serialize_value_inner(value, 0, dropped, budget) }
77
- reason = if budget.used > MAX_SERIALIZED_BYTES
78
- "too_large_over_#{MAX_SERIALIZED_BYTES}_bytes"
79
- elsif oversized?(result)
80
- "too_large"
81
- end
82
- return [result, dropped] unless reason
78
+ if budget.used > MAX_SERIALIZED_BYTES
79
+ reason = "too_large_over_#{MAX_SERIALIZED_BYTES}_bytes"
80
+ else
81
+ size = encoded_size(result)
82
+ return [result, dropped, size] if size && size <= MAX_SERIALIZED_BYTES
83
83
 
84
+ reason = "too_large"
85
+ end
84
86
  dropped << TOO_LARGE
85
- [unserializable_stub(value, reason), dropped]
87
+ sized_stub(value, reason, dropped)
86
88
  rescue StandardError, SystemStackError
87
- [unserializable_stub(value, "unexpected_error"), [class_name(value)]]
89
+ sized_stub(value, "unexpected_error", [class_name(value)])
90
+ end
91
+
92
+ def sized_stub(value, reason, dropped)
93
+ stub = unserializable_stub(value, reason)
94
+ [stub, dropped, stub.bytesize + 2]
95
+ end
96
+
97
+ def encoded_size(value)
98
+ JSON.dump(value).bytesize
99
+ rescue
100
+ nil
88
101
  end
89
102
 
90
103
  # `budget`, when given, counts the bytes this call's output needs beyond the
@@ -228,14 +241,6 @@ module Bitfab
228
241
  "<unserializable: #{class_name(value)} (#{reason})>"
229
242
  end
230
243
 
231
- def oversized?(value)
232
- JSON.dump(value).bytesize > MAX_SERIALIZED_BYTES
233
- rescue
234
- # If JSON.dump can't handle it, the wire path can't either, so treat as
235
- # oversized to force the stub fallback.
236
- true
237
- end
238
-
239
244
  # Serialize function inputs (args + kwargs) for span data (human-readable).
240
245
  def serialize_inputs(args, kwargs = {})
241
246
  serialize_inputs_with_report(args, kwargs).first
@@ -245,12 +250,16 @@ module Bitfab
245
250
  # send boundary can mark a lossy capture non-replayable. The third element
246
251
  # is [args, kwargs] with every value past MAX_SERIALIZED_BYTES swapped for
247
252
  # its placeholder (the originals when none is), so the type-preserving
248
- # capture never copies a value the readable capture already gave up on.
253
+ # capture never copies a value the readable capture already gave up on. The
254
+ # fourth element is the byte length of the readable capture's JSON encoding.
249
255
  def serialize_inputs_with_report(args, kwargs = {})
250
256
  dropped = []
251
257
  kept_args = args
258
+ elements = args.size + (kwargs.empty? ? 0 : 1)
259
+ size = 2 + [elements - 1, 0].max
252
260
  serialized = args.each_with_index.map do |arg, index|
253
- result, arg_dropped = serialize_value_with_report(arg)
261
+ result, arg_dropped, arg_size = serialize_value_with_report(arg)
262
+ size += arg_size
254
263
  dropped.concat(arg_dropped)
255
264
  if too_large?(arg_dropped)
256
265
  kept_args = kept_args.dup if kept_args.equal?(args)
@@ -260,8 +269,10 @@ module Bitfab
260
269
  end
261
270
  kept_kwargs = kwargs
262
271
  unless kwargs.empty?
272
+ size += 2 * kwargs.size + 1
263
273
  kw = kwargs.each_with_object({}) do |(k, v), acc|
264
- result, v_dropped = serialize_value_with_report(v)
274
+ result, v_dropped, v_size = serialize_value_with_report(v)
275
+ size += (encoded_size(safe_to_s(k)) || 2) + v_size
265
276
  dropped.concat(v_dropped)
266
277
  if too_large?(v_dropped)
267
278
  kept_kwargs = kept_kwargs.dup if kept_kwargs.equal?(kwargs)
@@ -271,7 +282,7 @@ module Bitfab
271
282
  end
272
283
  serialized << kw
273
284
  end
274
- [serialized, dropped, [kept_args, kept_kwargs]]
285
+ [serialized, dropped, [kept_args, kept_kwargs], size]
275
286
  end
276
287
 
277
288
  # Whether a dropped list from serialize_value_with_report means the value
@@ -11,7 +11,8 @@ module Bitfab
11
11
  module Subtree
12
12
  DEFAULT_MAX_DEPTH = 30
13
13
  DEFAULT_MAX_CAPTURED_SUBTREE_SPANS = 500
14
- LIMIT_ORDER = %i[max_depth max_captured_subtree_spans].freeze
14
+ DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION = 250_000_000
15
+ LIMIT_ORDER = %i[max_depth max_captured_subtree_spans max_payload_bytes_per_function].freeze
15
16
  SESSION_KEY = :bitfab_subtree_session
16
17
  DISPATCHER_KEY = :bitfab_subtree_dispatcher
17
18
  FIBER_CONTEXT_KEY = :bitfab_subtree_fiber_context
@@ -30,6 +31,10 @@ module Bitfab
30
31
  :skip_next_call
31
32
  )
32
33
 
34
+ DroppedCall = Struct.new(:identity, :error) do
35
+ def span_id = nil
36
+ end
37
+
33
38
  MethodMetadata = Struct.new(
34
39
  :identity,
35
40
  :lineno,
@@ -182,7 +187,8 @@ module Bitfab
182
187
  end
183
188
 
184
189
  def capture(client:, trace_function_key:, config:, max_depth:, include_wrappers:,
185
- root_receiver:, root_method_name:, max_captured_subtree_spans: DEFAULT_MAX_CAPTURED_SUBTREE_SPANS, mock_on_replay_default: false)
190
+ root_receiver:, root_method_name:, max_captured_subtree_spans: DEFAULT_MAX_CAPTURED_SUBTREE_SPANS,
191
+ max_payload_bytes_per_function: DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION, mock_on_replay_default: false)
186
192
  return yield unless config.root
187
193
 
188
194
  span = SpanContext.current || ReplayContext.current_span
@@ -200,6 +206,7 @@ module Bitfab
200
206
  config:,
201
207
  max_depth:,
202
208
  max_captured_subtree_spans:,
209
+ max_payload_bytes_per_function:,
203
210
  include_wrappers:,
204
211
  mock_on_replay_default:,
205
212
  root_receiver:,
@@ -424,7 +431,8 @@ module Bitfab
424
431
 
425
432
  def initialize(client:, trace_function_key:, trace_id:, root_span_id:, experiment_id:,
426
433
  input_source_span_id:, config:, max_depth:, include_wrappers:,
427
- root_receiver:, root_method_name:, max_captured_subtree_spans: DEFAULT_MAX_CAPTURED_SUBTREE_SPANS, mock_on_replay_default: false)
434
+ root_receiver:, root_method_name:, max_captured_subtree_spans: DEFAULT_MAX_CAPTURED_SUBTREE_SPANS,
435
+ max_payload_bytes_per_function: DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION, mock_on_replay_default: false)
428
436
  @mock_on_replay_default = mock_on_replay_default
429
437
  @client = client
430
438
  @trace_function_key = trace_function_key
@@ -439,6 +447,8 @@ module Bitfab
439
447
  @skip_root_call = true
440
448
  @max_depth = max_depth
441
449
  @max_captured_subtree_spans = max_captured_subtree_spans
450
+ @max_payload_bytes_per_function = max_payload_bytes_per_function
451
+ @payload_bytes_by_function = Hash.new(0)
442
452
  @exclude = config.exclude
443
453
  @include_wrappers = include_wrappers
444
454
  @stacks = {}
@@ -661,6 +671,11 @@ module Bitfab
661
671
  stop_discovery(fiber_id) unless content_off_names?
662
672
  return
663
673
  end
674
+ if !content_off && !metadata.jev && @payload_bytes_by_function[metadata.name] >= @max_payload_bytes_per_function
675
+ drop(:max_payload_bytes_per_function)
676
+ stack << DroppedCall.new(metadata.identity)
677
+ return
678
+ end
664
679
 
665
680
  active_parent = SpanContext.current || ReplayContext.current_span
666
681
  explicit_receiver = active_parent && active_parent[:explicit_span_receiver]
@@ -715,13 +730,22 @@ module Bitfab
715
730
  return unless stack
716
731
 
717
732
  entry = stack.last
733
+ return close_dropped(stack, fiber_id) if entry.is_a?(DroppedCall) && matching_identity?(entry.identity, event, resolved_path)
718
734
  return unless entry.is_a?(OpenSpan) && matching_identity?(entry.identity, event, resolved_path)
719
735
 
720
736
  stack.pop
721
737
  record_jev_reply(entry, event.return_value) if entry.jev_started_at
722
738
  emit(entry, result: event.return_value)
723
739
  parent = stack.last
724
- parent.error ||= entry.error if entry.error && parent.is_a?(OpenSpan)
740
+ parent.error ||= entry.error if entry.error && (parent.is_a?(OpenSpan) || parent.is_a?(DroppedCall))
741
+ @stacks.delete(fiber_id) if stack.empty?
742
+ @drained = true if @exhausted && captured_calls_drained?
743
+ end
744
+
745
+ def close_dropped(stack, fiber_id)
746
+ entry = stack.pop
747
+ parent = stack.last
748
+ parent.error ||= entry.error if entry.error && (parent.is_a?(OpenSpan) || parent.is_a?(DroppedCall))
725
749
  @stacks.delete(fiber_id) if stack.empty?
726
750
  @drained = true if @exhausted && captured_calls_drained?
727
751
  end
@@ -739,7 +763,7 @@ module Bitfab
739
763
 
740
764
  def record_error(event)
741
765
  entry = @stacks[Fiber.current.object_id]&.last
742
- return unless entry.is_a?(OpenSpan)
766
+ return unless entry.is_a?(OpenSpan) || entry.is_a?(DroppedCall)
743
767
 
744
768
  exception = event.raised_exception
745
769
  entry.error = "#{exception.class}: #{ErrorChain.describe(exception)}"
@@ -747,14 +771,18 @@ module Bitfab
747
771
 
748
772
  def clear_rescued_error(event)
749
773
  entry = @stacks[Fiber.current.object_id]&.last
750
- return unless entry.is_a?(OpenSpan)
774
+ return unless entry.is_a?(OpenSpan) || entry.is_a?(DroppedCall)
751
775
 
752
776
  entry.error = nil
753
777
  end
754
778
 
755
779
  def emit(entry, result:)
756
- # Recording a lazy return value must not execute the caller's iteration.
757
780
  result = Serialize.unserializable_stub(result, "lazy_enumerator") if result.is_a?(Enumerator)
781
+ payload_bytes = {}
782
+ if !entry.declared_node && entry.span_type != "llm"
783
+ name = entry.name
784
+ payload_bytes[:on_payload_bytes] = ->(bytes) { @payload_bytes_by_function[name] += bytes }
785
+ end
758
786
  @client.send(
759
787
  :send_span,
760
788
  trace_function_key: @trace_function_key,
@@ -777,7 +805,8 @@ module Bitfab
777
805
  started_at: entry.started_at,
778
806
  ended_at: now,
779
807
  experiment_id: entry.experiment_id || @experiment_id,
780
- input_source_span_id: @input_source_span_id
808
+ input_source_span_id: @input_source_span_id,
809
+ **payload_bytes
781
810
  )
782
811
  rescue Exception # rubocop:disable Lint/RescueException
783
812
  nil
@@ -921,7 +950,7 @@ module Bitfab
921
950
 
922
951
  def local_depth(stack)
923
952
  stack.count do |entry|
924
- !entry.is_a?(Boundary) || entry.counts_toward_depth
953
+ entry.is_a?(OpenSpan) || (entry.is_a?(Boundary) && entry.counts_toward_depth)
925
954
  end
926
955
  end
927
956
 
@@ -995,7 +1024,8 @@ module Bitfab
995
1024
  Bitfab.warn_once(
996
1025
  "subtree-truncated:#{@trace_function_key}",
997
1026
  "'#{@trace_function_key}' hit a subtree capture limit (max_depth=#{@max_depth}, " \
998
- "max_captured_subtree_spans=#{@max_captured_subtree_spans}) and its trace is incomplete. " \
1027
+ "max_captured_subtree_spans=#{@max_captured_subtree_spans}, " \
1028
+ "max_payload_bytes_per_function=#{@max_payload_bytes_per_function}) and its trace is incomplete. " \
999
1029
  "Raise the limits, add bitfab_node to the calls that matter, or narrow what is traced with exclude: [...]."
1000
1030
  )
1001
1031
  end
@@ -82,9 +82,16 @@ module Bitfab
82
82
  # Wrap an existing method with experimental whole-subtree tracing.
83
83
  # The root records with the requested type; first-party Ruby methods called
84
84
  # beneath it record automatically as function spans.
85
+ # @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
86
+ # inputs and outputs that one automatically captured function may record
87
+ # within a single call of the root. Defaults to 250,000,000. Once a function
88
+ # has recorded this much, its later calls beneath that root call are not
89
+ # recorded, so a helper called in a loop with a large argument cannot fill
90
+ # memory. Nodes declared with bitfab_node and LLM calls do not count toward it.
85
91
  def self.trace(klass, method_name, trace_function_key:, name: nil, type: "custom",
86
92
  max_depth: Subtree::DEFAULT_MAX_DEPTH,
87
93
  max_captured_subtree_spans: Subtree::DEFAULT_MAX_CAPTURED_SUBTREE_SPANS,
94
+ max_payload_bytes_per_function: Subtree::DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION,
88
95
  exclude: [], include_wrappers: false, mock_on_replay_default: false, client: nil)
89
96
  capture_config = Subtree.prepare(root_source: source_of(klass, method_name), exclude:)
90
97
  method_name_str = method_name.to_s
@@ -106,7 +113,8 @@ module Bitfab
106
113
  SpanContext.current[:surface] = :trace
107
114
  next Subtree.capture(
108
115
  client: target_client, trace_function_key:, config: capture_config,
109
- max_depth:, max_captured_subtree_spans:, include_wrappers:, mock_on_replay_default:,
116
+ max_depth:, max_captured_subtree_spans:, max_payload_bytes_per_function:, include_wrappers:,
117
+ mock_on_replay_default:,
110
118
  root_receiver: self, root_method_name: method_name
111
119
  ) { Traceable.send(:as_root_call, self, method_name) { super(*args, **kwargs, &block) } }
112
120
  end
@@ -120,7 +128,8 @@ module Bitfab
120
128
  around_body: call.method(:run)) do
121
129
  Subtree.capture(
122
130
  client: target_client, trace_function_key:, config: capture_config,
123
- max_depth:, max_captured_subtree_spans:, include_wrappers:, mock_on_replay_default:,
131
+ max_depth:, max_captured_subtree_spans:, max_payload_bytes_per_function:, include_wrappers:,
132
+ mock_on_replay_default:,
124
133
  root_receiver: self, root_method_name: method_name
125
134
  ) { Traceable.send(:as_root_call, self, method_name) { super(*args, **kwargs, &block) } }
126
135
  end
@@ -348,9 +357,16 @@ module Bitfab
348
357
 
349
358
  # EXPERIMENTAL. Trace a method and every first-party Ruby method it calls.
350
359
  # Descendants require no Bitfab declarations and record as function spans.
360
+ # @param max_payload_bytes_per_function [Integer] maximum serialized bytes of
361
+ # inputs and outputs that one automatically captured function may record
362
+ # within a single call of the root. Defaults to 250,000,000. Once a function
363
+ # has recorded this much, its later calls beneath that root call are not
364
+ # recorded, so a helper called in a loop with a large argument cannot fill
365
+ # memory. Nodes declared with bitfab_node and LLM calls do not count toward it.
351
366
  def bitfab_trace(method_name, trace_function_key: nil, name: nil, type: "custom",
352
367
  max_depth: Subtree::DEFAULT_MAX_DEPTH,
353
368
  max_captured_subtree_spans: Subtree::DEFAULT_MAX_CAPTURED_SUBTREE_SPANS,
369
+ max_payload_bytes_per_function: Subtree::DEFAULT_MAX_PAYLOAD_BYTES_PER_FUNCTION,
354
370
  exclude: [], include_wrappers: false, mock_on_replay_default: false)
355
371
  trace_function_key ||= @bitfab_function_key
356
372
  unless trace_function_key
@@ -364,6 +380,7 @@ module Bitfab
364
380
  type:,
365
381
  max_depth:,
366
382
  max_captured_subtree_spans:,
383
+ max_payload_bytes_per_function:,
367
384
  exclude:,
368
385
  include_wrappers:,
369
386
  mock_on_replay_default:
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bitfab
4
- VERSION = "0.76.7"
4
+ VERSION = "0.76.8"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitfab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.76.7
4
+ version: 0.76.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harvest Team