bitfab 0.76.9 → 0.78.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9f05a1092d7d258248586f1d91e0549b41a141b60265d290f50dcfca0ea1c84
4
- data.tar.gz: 7bc1763c6c852c0189f8e0be06a55228c6844602cc17380ff62edf11250726dd
3
+ metadata.gz: 8df415900dea564b2faa8d2ecc01c2538bf44ad2d88d16a669c54439cba933b6
4
+ data.tar.gz: 262a531fc1566e6e276ec4f1b7136f09327fe6a2a97089f1e7f9fb067710937b
5
5
  SHA512:
6
- metadata.gz: d12922006705f3887077d240bf69367aa065cbdc841736ade2b2a250f69de7c52e42bdeaf97585332ae4458fbb8b4c8c42dbcd35606107a4ba74c7153be80401
7
- data.tar.gz: b39ad8194a05792ab00e8dcde80fe47b0781c7ecbc349e4fa688791285fa94b2973fd3323c7794982c2ee19e88cb20dc31ef4a51c71338db6cca08222d22d41b
6
+ metadata.gz: 05ead01bb8ece0e612217c20bd59f49233699735896f401db844430f0664c5bff188169185092f34bf7a01e2ba3d717f11738fc5e26066287e698cf87039e409
7
+ data.tar.gz: 7ddc3c3294263875bdf80b9edb7ffc1eeadcda7e699f6b77c51135e606b5efcb950169584effdd303027aa1012de72ecd2699ac380c39ce1ad6784da56c23345
data/README.md CHANGED
@@ -336,18 +336,22 @@ fn.wrap(OpenAI::Client, :embeddings, name: "Embed", type: "llm")
336
336
 
337
337
  ### Replay with Mock Strategies
338
338
 
339
- Replay reruns historical traces through your code so you can compare outputs after an iteration. By default, spans tagged with `mock_on_replay: true` return their historical output and other child spans run real code. Three strategies control whether child spans return their historical output instead of executing:
339
+ Replay reruns historical traces through your code so you can compare outputs after an iteration. By default, spans tagged with `mock_on_replay: true` return their historical output and other child spans run real code. Three strategies control whether child spans return their historical output instead of executing.
340
+
341
+ Every replay that is not a dry run needs an experiment group: one series of experiments testing one thing, named after what is being tested. Pass the id of an existing group when the run tests the same thing as that group, or a new UUID with `experiment_group_name:` to start one.
340
342
 
341
343
  ```ruby
344
+ group = {experiment_group_id: SecureRandom.uuid, experiment_group_name: "Shorter summary prompt"}
345
+
342
346
  # "marked" (default): only spans tagged with `mock_on_replay: true` return historical output
343
- client.replay(pipeline, :process, trace_function_key: "my-fn")
344
- client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "marked")
347
+ client.replay(pipeline, :process, trace_function_key: "my-fn", **group)
348
+ client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "marked", **group)
345
349
 
346
350
  # "none": everything runs real code
347
- client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "none")
351
+ client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "none", **group)
348
352
 
349
353
  # "all": every matched recorded child returns history; missing occurrences fail closed
350
- client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "all")
354
+ client.replay(pipeline, :process, trace_function_key: "my-fn", mock: "all", **group)
351
355
  ```
352
356
 
353
357
  Tag the spans you want mocked at definition time:
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bitfab
4
+ # The id of the coding agent session that launched a replay, recorded on the
5
+ # experiment so a run can be traced back to that session.
6
+ module AgentSession
7
+ # Environment variables read, in order, when no session id is passed.
8
+ ENV_VARS = %w[CLAUDE_CODE_SESSION_ID CODEX_THREAD_ID AGENT_SESSION_ID].freeze
9
+ # The longest session id the server accepts.
10
+ MAX_LENGTH = 255
11
+
12
+ module_function
13
+
14
+ # Returns the trimmed explicit id when one is given, otherwise the first
15
+ # non-empty value of ENV_VARS, otherwise nil. Raises ArgumentError when an
16
+ # explicit id is longer than MAX_LENGTH.
17
+ def resolve(explicit = nil, env: ENV)
18
+ given = text(explicit)
19
+ if given
20
+ if given.length > MAX_LENGTH
21
+ raise ArgumentError, "agent_session_id must be at most #{MAX_LENGTH} characters (got #{given.length})."
22
+ end
23
+ return given
24
+ end
25
+
26
+ ENV_VARS.each do |name|
27
+ value = text(env[name])
28
+ return value if value && value.length <= MAX_LENGTH
29
+ end
30
+ nil
31
+ end
32
+
33
+ def text(value)
34
+ trimmed = value&.to_s&.strip
35
+ (trimmed.nil? || trimmed.empty?) ? nil : trimmed
36
+ end
37
+ end
38
+ end
data/lib/bitfab/client.rb CHANGED
@@ -132,7 +132,7 @@ module Bitfab
132
132
  # Passed alongside dataset_id or dataset_ids it pins which members of that
133
133
  # selection replay, and the server rejects any ID none of those datasets
134
134
  # contains
135
- # @param name [String, nil] What this run is testing, in a few words, such
135
+ # @param name [String, nil] What this run changes, in a few words, such
136
136
  # as 'baseline' or 'shorter system prompt'. Bitfab records the commit,
137
137
  # branch, tree state, datasets, and who ran it with every experiment, so
138
138
  # do not repeat them here.
@@ -163,18 +163,29 @@ module Bitfab
163
163
  # against when code_change is "ref", and only then, such as "dev",
164
164
  # "release/2.3", or a commit SHA. A branch that exists only on the remote
165
165
  # is read as origin/<name>. A name git cannot resolve raises ArgumentError.
166
- # @param experiment_group_id [String, nil] optional UUID grouping multiple
167
- # replay runs into a single experiment batch. The run joins that group,
168
- # and an id no group has yet creates it
166
+ # @param experiment_group_id [String, nil] UUID of the experiment group
167
+ # this run belongs to: one series of experiments testing one thing.
168
+ # Required except for a dry run or skip_experiment_group; leaving it out raises ArgumentError
169
+ # before any request. The run joins that group, and an id no group has
170
+ # yet creates it
169
171
  # @param experiment_group_name [String, nil] optional display name for the
170
- # experiment group, applied when this replay creates the group or the group
171
- # has no name yet. A group that already has a name keeps it. Requires
172
- # experiment_group_id; passing it alone raises ArgumentError
172
+ # experiment group, named after what the run tests, applied when this
173
+ # replay creates the group or the group has no name yet. A group that
174
+ # already has a name keeps it. Requires experiment_group_id; passing it
175
+ # alone raises ArgumentError
173
176
  # @param baseline_experiment_id [String, nil] optional UUID of an earlier
174
177
  # experiment this run is compared against. It takes precedence over the
175
178
  # first finished experiment in the group and over the original traces.
176
179
  # It must belong to the caller's organization or the server rejects the
177
180
  # replay
181
+ # @param skip_experiment_group [Boolean] runs without an experiment group,
182
+ # for scripts and schedules whose runs should not be grouped. Cannot be
183
+ # combined with experiment_group_id or experiment_group_name; doing so
184
+ # raises ArgumentError before any request
185
+ # @param agent_session_id [String, nil] the coding agent session that
186
+ # launched this run, stored on the experiment. Defaults to the first
187
+ # non-empty of CLAUDE_CODE_SESSION_ID, CODEX_THREAD_ID, and
188
+ # AGENT_SESSION_ID
178
189
  # @param dataset_ids [Array<String>, nil] optional UUIDs of the datasets this
179
190
  # replay runs against, for benchmarking one method against several corpora
180
191
  # in a single run; mutually exclusive with dataset_id. Passing trace_ids
@@ -187,9 +198,15 @@ module Bitfab
187
198
  # directly to this experiment, graded as the union with the dataset's
188
199
  # runnable graders at completion; each must be an active/live grader in the
189
200
  # same org and trace function or the server rejects the replay
201
+ # @param only_drafts [Boolean] replay only the selected traces that
202
+ # carry at least one draft assertion: not confirmed, not
203
+ # rejected, and not archived. Narrows whatever limit, trace_ids or
204
+ # dataset_ids selected, the way only_with_assertions does. With
205
+ # dataset_ids the experiment is recorded as a subset of the dataset. A
206
+ # selection that narrows to nothing replays nothing.
190
207
  # @param category_ids [Array<String>, nil] optional category UUIDs. With
191
208
  # trace_ids, dataset_id or dataset_ids, only the selected traces carrying
192
- # an approved assertion in one of these categories replay. It changes
209
+ # a confirmed assertion in one of these categories replay. It changes
193
210
  # which traces replay, not which assertions are judged. Raises
194
211
  # ArgumentError when empty or passed without a trace ID or dataset
195
212
  # selection.
@@ -239,7 +256,7 @@ module Bitfab
239
256
  # @return [Hash] with :items, :experiment_id, :experiment_url, and the
240
257
  # deprecated aliases :test_run_id and :test_run_url carrying the same values
241
258
  def replay(receiver, method_name = nil, trace_function_key:, limit: nil, trace_ids: nil, concurrency: nil, max_concurrency: ReplayConcurrency::UNSET,
242
- name: nil, notes: nil, metadata: nil, code_change: nil, code_change_description: nil, code_change_files: nil, code_change_base: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil, dataset_id: nil, dataset_ids: nil, grader_ids: nil, mock: "marked", attempts: ReplayConcurrency::UNSET, only_with_assertions: false, category_ids: nil, skip_assertion_judging: false, judge_assertions: nil, dry_run: false,
259
+ name: nil, notes: nil, metadata: nil, code_change: nil, code_change_description: nil, code_change_files: nil, code_change_base: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil, skip_experiment_group: false, agent_session_id: nil, dataset_id: nil, dataset_ids: nil, grader_ids: nil, mock: "marked", attempts: ReplayConcurrency::UNSET, only_with_assertions: false, only_drafts: false, category_ids: nil, skip_assertion_judging: false, judge_assertions: nil, dry_run: false,
243
260
  adapt_inputs: nil, mock_override: nil, db_branch: nil, on_item_start: nil, on_item_finish: nil, on_progress: nil,
244
261
  on_experiment_start: nil, experimental_selective_replay: nil)
245
262
  Replay.run(
@@ -256,6 +273,7 @@ module Bitfab
256
273
  max_concurrency:,
257
274
  attempts:,
258
275
  only_with_assertions:,
276
+ only_drafts:,
259
277
  category_ids:,
260
278
  skip_assertion_judging:,
261
279
  judge_assertions:,
@@ -267,6 +285,8 @@ module Bitfab
267
285
  experiment_group_id:,
268
286
  experiment_group_name:,
269
287
  baseline_experiment_id:,
288
+ skip_experiment_group:,
289
+ agent_session_id:,
270
290
  dataset_id:,
271
291
  dataset_ids:,
272
292
  grader_ids:,
@@ -14,13 +14,14 @@ module Bitfab
14
14
  end
15
15
 
16
16
  def list(dataset_id: nil, trace_function_key: nil, git_branch: nil, experiment_group_id: nil, status: nil,
17
- metadata: nil, created_after: nil, created_before: nil, cursor: nil, limit: nil)
17
+ metadata: nil, agent_session_id: nil, created_after: nil, created_before: nil, cursor: nil, limit: nil)
18
18
  params = {}
19
19
  params["datasetId"] = dataset_id unless dataset_id.nil?
20
20
  params["traceFunctionKey"] = trace_function_key unless trace_function_key.nil?
21
21
  params["gitBranch"] = git_branch unless git_branch.nil?
22
22
  params["experimentGroupId"] = experiment_group_id unless experiment_group_id.nil?
23
23
  params["status"] = status unless status.nil?
24
+ params["agentSessionId"] = agent_session_id unless agent_session_id.nil?
24
25
  params["metadata"] = JSON.generate(metadata) unless metadata.nil?
25
26
  params["createdAfter"] = iso8601(created_after) unless created_after.nil?
26
27
  params["createdBefore"] = iso8601(created_before) unless created_before.nil?
@@ -5,6 +5,7 @@ require "json"
5
5
  require "uri"
6
6
 
7
7
  require_relative "git_state"
8
+ require_relative "agent_session"
8
9
  require_relative "replay_invocation"
9
10
  require_relative "compress"
10
11
  require_relative "connection_pool"
@@ -311,6 +312,12 @@ module Bitfab
311
312
  # has no name yet; only sent together with experiment_group_id
312
313
  # @param baseline_experiment_id [String, nil] optional UUID of an earlier
313
314
  # experiment this run is compared against, sent as baselineExperimentId
315
+ # @param require_experiment_group [Boolean] asks the server to refuse a
316
+ # replay with no experiment_group_id; false leaves the field out
317
+ # @param agent_session_id [String, nil] the coding agent session that
318
+ # launched this replay. Defaults to the first non-empty of
319
+ # CLAUDE_CODE_SESSION_ID, CODEX_THREAD_ID, and AGENT_SESSION_ID, and is
320
+ # left out when none is set
314
321
  # @param name [String, nil] optional display name for the resulting
315
322
  # experiment
316
323
  # @param dataset_ids [Array<String>, nil] optional UUIDs of the datasets this
@@ -322,18 +329,23 @@ module Bitfab
322
329
  # directly to this experiment, graded as the union with the dataset's
323
330
  # runnable graders at completion; each must be an active/live grader in the
324
331
  # same org and trace function or the server rejects the replay
332
+ # @param only_drafts [Boolean] replay only the selected traces that
333
+ # carry at least one draft assertion: not confirmed, not
334
+ # rejected, and not archived. Narrows whatever limit, trace_ids or
335
+ # dataset_ids selected, the way only_with_assertions does. With
336
+ # dataset_ids the experiment is recorded as a subset of the dataset. A
337
+ # selection that narrows to nothing replays nothing.
325
338
  # @param category_ids [Array<String>, nil] optional category UUIDs. With
326
339
  # trace_ids, dataset_id or dataset_ids, only the selected traces carrying
327
- # an approved assertion in one of these categories replay. It changes
340
+ # a confirmed assertion in one of these categories replay. It changes
328
341
  # which traces replay, not which assertions are judged. Raises
329
342
  # ArgumentError when empty or passed without a trace ID or dataset
330
343
  # selection.
331
344
  def start_replay(trace_function_key, limit, trace_ids: nil, code_change_type: nil, code_change_description: nil,
332
- code_change_files: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil, name: nil, notes: nil, metadata: nil, include_db_branch_lease: false, dataset_ids: nil,
333
- grader_ids: nil, db_branch_settings: nil, attempts: 1, only_with_assertions: false, category_ids: nil, skip_assertion_judging: false, include_original_metadata: false, experimental_selective_replay: nil, dry_run: false, invocation: nil)
334
- payload = {
335
- "traceFunctionKey" => trace_function_key
336
- }
345
+ code_change_files: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil, require_experiment_group: true, name: nil, notes: nil, metadata: nil, include_db_branch_lease: false, dataset_ids: nil,
346
+ grader_ids: nil, db_branch_settings: nil, attempts: 1, only_with_assertions: false, only_drafts: false, category_ids: nil, skip_assertion_judging: false, include_original_metadata: false, experimental_selective_replay: nil, dry_run: false, invocation: nil, agent_session_id: nil)
347
+ payload = {"traceFunctionKey" => trace_function_key}
348
+ payload["requireExperimentGroup"] = true if require_experiment_group
337
349
  # limit is only meaningful without trace_ids (an explicit ID list
338
350
  # already determines the count), so it's omitted when nil.
339
351
  payload["limit"] = limit unless limit.nil?
@@ -347,6 +359,8 @@ module Bitfab
347
359
  payload["experimentGroupId"] = experiment_group_id unless experiment_group_id.nil?
348
360
  payload["experimentGroupName"] = experiment_group_name unless experiment_group_name.nil?
349
361
  payload["baselineExperimentId"] = baseline_experiment_id unless baseline_experiment_id.nil?
362
+ resolved_agent_session_id = AgentSession.resolve(agent_session_id)
363
+ payload["agentSessionId"] = resolved_agent_session_id if resolved_agent_session_id
350
364
  payload["includeDbBranchLease"] = true if include_db_branch_lease
351
365
  payload["lazyDbBranchLease"] = true if include_db_branch_lease
352
366
  unless dataset_ids.nil?
@@ -359,6 +373,7 @@ module Bitfab
359
373
  payload["graderIds"] = grader_ids unless grader_ids.nil?
360
374
  payload["attempts"] = attempts if attempts > 1
361
375
  payload["onlyWithAssertions"] = true if only_with_assertions
376
+ payload["onlyDrafts"] = true if only_drafts
362
377
  payload["categoryIds"] = category_ids unless category_ids.nil?
363
378
  payload["judgeAssertions"] = true unless skip_assertion_judging
364
379
  payload["dryRun"] = true if dry_run
data/lib/bitfab/replay.rb CHANGED
@@ -148,6 +148,13 @@ module Bitfab
148
148
  CODE_CHANGE_CHOICES = '"working-tree" (uncommitted changes vs HEAD), "last-branch" (the diff against the ' \
149
149
  'branch this one was cut from), "default-branch" (the diff against the default branch), "ref" (the diff ' \
150
150
  'against a branch or commit you name), "skip" (no code change), or "manual" (pass the files yourself)'
151
+ # The error a replay raises when it names no experiment group.
152
+ EXPERIMENT_GROUP_REQUIRED = "experiment_group_id is required. An experiment group is one series of experiments " \
153
+ "testing one thing. Reuse the id of the group whose runs test the same thing, or pass a new UUID with " \
154
+ "experiment_group_name, named after what this run tests, to start a group. To run without a group, " \
155
+ "pass skip_experiment_group: true."
156
+ SKIP_EXPERIMENT_GROUP_CONFLICT = "skip_experiment_group runs without an experiment group and cannot be combined " \
157
+ "with experiment_group_id or experiment_group_name. Pass one or the other."
151
158
  UNSET_DEFAULT = Object.new.freeze
152
159
  OPTION_DEFAULTS = {mock: "marked"}.freeze
153
160
  PERSISTENCE_TIMEOUT_SECONDS = 30.0
@@ -219,7 +226,7 @@ module Bitfab
219
226
  # already determines how many traces replay. Supplying trace_ids also
220
227
  # emits a warning.
221
228
  # @param trace_ids [Array<String>, nil] optional list of trace IDs to replay (max 100)
222
- # @param name [String, nil] What this run is testing, in a few words, such
229
+ # @param name [String, nil] What this run changes, in a few words, such
223
230
  # as 'baseline' or 'shorter system prompt'. Bitfab records the commit,
224
231
  # branch, tree state, datasets, and who ran it with every experiment, so
225
232
  # do not repeat them here.
@@ -250,18 +257,29 @@ module Bitfab
250
257
  # against when code_change is "ref", and only then, such as "dev",
251
258
  # "release/2.3", or a commit SHA. A branch that exists only on the remote
252
259
  # is read as origin/<name>. A name git cannot resolve raises ArgumentError.
253
- # @param experiment_group_id [String, nil] optional UUID grouping multiple
254
- # replay runs into a single experiment batch. The run joins that group,
255
- # and an id no group has yet creates it
260
+ # @param experiment_group_id [String, nil] UUID of the experiment group
261
+ # this run belongs to: one series of experiments testing one thing.
262
+ # Required except for a dry run or skip_experiment_group; leaving it out raises ArgumentError
263
+ # before any request. The run joins that group, and an id no group has
264
+ # yet creates it
256
265
  # @param experiment_group_name [String, nil] optional display name for the
257
- # experiment group, applied when this replay creates the group or the group
258
- # has no name yet. A group that already has a name keeps it. Requires
259
- # experiment_group_id; passing it alone raises ArgumentError
266
+ # experiment group, named after what the run tests, applied when this
267
+ # replay creates the group or the group has no name yet. A group that
268
+ # already has a name keeps it. Requires experiment_group_id; passing it
269
+ # alone raises ArgumentError
260
270
  # @param baseline_experiment_id [String, nil] optional UUID of an earlier
261
271
  # experiment this run is compared against. It takes precedence over the
262
272
  # first finished experiment in the group and over the original traces.
263
273
  # It must belong to the caller's organization or the server rejects the
264
274
  # replay
275
+ # @param skip_experiment_group [Boolean] runs without an experiment group,
276
+ # for scripts and schedules whose runs should not be grouped. Cannot be
277
+ # combined with experiment_group_id or experiment_group_name; doing so
278
+ # raises ArgumentError before any request
279
+ # @param agent_session_id [String, nil] the coding agent session that
280
+ # launched this run, stored on the experiment. Defaults to the first
281
+ # non-empty of CLAUDE_CODE_SESSION_ID, CODEX_THREAD_ID, and
282
+ # AGENT_SESSION_ID
265
283
  # @param dataset_id [String, nil] optional UUID of the dataset this replay
266
284
  # runs against, stored on the resulting experiment for durable attribution;
267
285
  # mutually exclusive with dataset_ids. Alone it replays the dataset's full
@@ -276,9 +294,15 @@ module Bitfab
276
294
  # directly to this experiment, graded as the union with the dataset's
277
295
  # runnable graders at completion; each must be an active/live grader in the
278
296
  # same org and trace function or the server rejects the replay
297
+ # @param only_drafts [Boolean] replay only the selected traces that
298
+ # carry at least one draft assertion: not confirmed, not
299
+ # rejected, and not archived. Narrows whatever limit, trace_ids or
300
+ # dataset_ids selected, the way only_with_assertions does. With
301
+ # dataset_ids the experiment is recorded as a subset of the dataset. A
302
+ # selection that narrows to nothing replays nothing.
279
303
  # @param category_ids [Array<String>, nil] optional category UUIDs. With
280
304
  # trace_ids, dataset_id or dataset_ids, only the selected traces carrying
281
- # an approved assertion in one of these categories replay. It changes
305
+ # a confirmed assertion in one of these categories replay. It changes
282
306
  # which traces replay, not which assertions are judged. Raises
283
307
  # ArgumentError when empty or passed without a trace ID or dataset
284
308
  # selection.
@@ -327,8 +351,8 @@ module Bitfab
327
351
  # The experiment fields are nil for a dry run, which creates no experiment,
328
352
  # and progress events then carry a nil :experiment_id too.
329
353
  def run(client, receiver, method_name, trace_function_key:, limit: nil, trace_ids: nil, name: nil, notes: nil, metadata: nil,
330
- concurrency: nil, max_concurrency: ReplayConcurrency::UNSET, code_change: nil, code_change_description: nil, code_change_files: nil, code_change_base: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil,
331
- dataset_id: nil, dataset_ids: nil, grader_ids: nil, mock: "marked", attempts: ReplayConcurrency::UNSET, only_with_assertions: false, category_ids: nil, skip_assertion_judging: false, judge_assertions: nil, dry_run: false,
354
+ concurrency: nil, max_concurrency: ReplayConcurrency::UNSET, code_change: nil, code_change_description: nil, code_change_files: nil, code_change_base: nil, experiment_group_id: nil, experiment_group_name: nil, baseline_experiment_id: nil, skip_experiment_group: false, agent_session_id: nil,
355
+ dataset_id: nil, dataset_ids: nil, grader_ids: nil, mock: "marked", attempts: ReplayConcurrency::UNSET, only_with_assertions: false, only_drafts: false, category_ids: nil, skip_assertion_judging: false, judge_assertions: nil, dry_run: false,
332
356
  adapt_inputs: nil, mock_override: nil, db_branch: nil, on_item_start: nil, on_item_finish: nil, on_progress: nil,
333
357
  on_experiment_start: nil, experimental_selective_replay: nil)
334
358
  validate_code_change(code_change, code_change_description, code_change_files, code_change_base)
@@ -352,9 +376,15 @@ module Bitfab
352
376
  unless attempts.is_a?(Integer) && attempts.between?(1, 100)
353
377
  raise ArgumentError, "attempts must be an integer from 1 to 100."
354
378
  end
379
+ if skip_experiment_group && !(experiment_group_id.nil? && experiment_group_name.nil?)
380
+ raise ArgumentError, SKIP_EXPERIMENT_GROUP_CONFLICT
381
+ end
355
382
  if experiment_group_name && experiment_group_id.nil?
356
383
  raise ArgumentError, "experiment_group_name only applies with experiment_group_id. Pass it together with experiment_group_id."
357
384
  end
385
+ if experiment_group_id.to_s.strip.empty? && !dry_run && !skip_experiment_group
386
+ raise ArgumentError, EXPERIMENT_GROUP_REQUIRED
387
+ end
358
388
  item_finish_callback = on_item_finish || on_progress
359
389
  if trace_ids
360
390
  raise ArgumentError, "trace_ids must contain at least one trace ID." if trace_ids.empty?
@@ -427,11 +457,14 @@ module Bitfab
427
457
  experiment_group_id:,
428
458
  experiment_group_name:,
429
459
  baseline_experiment_id:,
460
+ require_experiment_group: !skip_experiment_group,
461
+ agent_session_id:,
430
462
  include_db_branch_lease:,
431
463
  dataset_ids: resolved_dataset_ids,
432
464
  grader_ids:,
433
465
  attempts:,
434
466
  only_with_assertions:,
467
+ only_drafts:,
435
468
  category_ids:,
436
469
  skip_assertion_judging:,
437
470
  include_original_metadata: !adapt_inputs.nil?,
@@ -48,7 +48,10 @@ module Bitfab
48
48
  def check_arguments(argv)
49
49
  _, replay_argv = parse(argv)
50
50
  args = ReplayCli.parse(nil, replay_argv)
51
- ReplayCli.require_code_change(args) unless args[:seed] || args[:from_trace]
51
+ unless args[:seed] || args[:from_trace]
52
+ ReplayCli.require_code_change(args)
53
+ ReplayCli.require_experiment_group(args)
54
+ end
52
55
  0
53
56
  end
54
57
 
@@ -19,8 +19,8 @@ module Bitfab
19
19
  # Project-owned registry of production replay roots.
20
20
  class ReplayRegistry
21
21
  OPTION_NAMES = %i[
22
- limit trace_ids name notes metadata concurrency max_concurrency experiment_group_id experiment_group_name baseline_experiment_id dataset_id dataset_ids grader_ids mock
23
- mock_override adapt_inputs db_branch attempts only_with_assertions category_ids skip_assertion_judging judge_assertions dry_run on_item_finish
22
+ limit trace_ids name notes metadata concurrency max_concurrency experiment_group_id experiment_group_name baseline_experiment_id skip_experiment_group agent_session_id dataset_id dataset_ids grader_ids mock
23
+ mock_override adapt_inputs db_branch attempts only_with_assertions only_drafts category_ids skip_assertion_judging judge_assertions dry_run on_item_finish
24
24
  ].freeze
25
25
 
26
26
  def initialize
@@ -88,48 +88,76 @@ module Bitfab
88
88
  module ReplayCli
89
89
  MAX_PINNED_TRACE_IDS = 100
90
90
  CLI_INVOCATION_SKIPPED = %i[pipeline seed run from_trace].freeze
91
+ EXPERIMENT_GROUP_REQUIRED = "--experiment-group-id is required. An experiment group is one series of experiments " \
92
+ "testing one thing. Reuse the id of the group whose runs test the same thing, or pass a new UUID with " \
93
+ "--experiment-group-name, named after what this run tests, to start a group. To run without a group, " \
94
+ "pass --skip-experiment-group."
95
+ SKIP_EXPERIMENT_GROUP_CONFLICT = "--skip-experiment-group runs without an experiment group and cannot be combined " \
96
+ "with --experiment-group-id or --experiment-group-name. Pass one or the other."
91
97
 
92
98
  class ItemsErroredError < StandardError; end
93
99
 
94
100
  module_function
95
101
 
96
- # get_assertions returns every state so a reviewer can see drafts, but only an
97
- # approved assertion is checked on a replay, so narrowing on anything else
102
+ # get_assertions returns every status so a reviewer can see drafts, but only a
103
+ # confirmed assertion is checked on a replay, so narrowing on anything else
98
104
  # would pick traces the run cannot be judged against. The server applies the
99
105
  # same rule when it revalidates the selection.
100
- def approved_assertion?(assertions, category_ids = nil)
106
+ def confirmed_assertion?(assertions, category_ids = nil)
101
107
  assertions.any? do |assertion|
102
- assertion["approvalState"] == "approved" &&
108
+ assertion["status"] == "confirmed" &&
103
109
  (category_ids.nil? || category_ids.include?(assertion["category_id"]))
104
110
  end
105
111
  end
106
112
 
107
- def bound_trace_ids(client, trace_ids, limit, only_with_assertions, category_ids = nil)
113
+ def draft_assertion?(assertions)
114
+ assertions.any? { |assertion| assertion["status"] == "draft" }
115
+ end
116
+
117
+ def narrows_to_confirmed_assertions?(narrowing)
118
+ narrowing[:only_with_assertions] || !narrowing[:category_ids].nil?
119
+ end
120
+
121
+ def keeps_trace?(assertions, narrowing)
122
+ if narrows_to_confirmed_assertions?(narrowing) && !confirmed_assertion?(assertions, narrowing[:category_ids])
123
+ return false
124
+ end
125
+
126
+ !narrowing[:only_drafts] || draft_assertion?(assertions)
127
+ end
128
+
129
+ def no_traces_matched_message(narrowing)
130
+ if narrowing[:only_drafts]
131
+ return "No traces with a draft assertion matched this replay selection."
132
+ end
133
+ if narrowing[:category_ids]
134
+ return "No traces with a confirmed assertion in the given categories matched this replay selection."
135
+ end
136
+
137
+ "No traces with confirmed assertions matched this replay selection."
138
+ end
139
+
140
+ def bound_trace_ids(client, trace_ids, limit, narrowing = {})
108
141
  unless limit.is_a?(Integer) && limit.positive?
109
142
  raise ArgumentError, "Replay limit must be a positive integer."
110
143
  end
111
- narrows = only_with_assertions || !category_ids.nil?
144
+ narrows = narrows_to_confirmed_assertions?(narrowing) || narrowing[:only_drafts]
112
145
  return trace_ids.first(limit) if !narrows || limit >= trace_ids.length
113
146
 
114
147
  selected = []
115
148
  trace_ids.each do |trace_id|
116
149
  result = client.traces.get_assertions(trace_id)
117
- if result.fetch("inheritedFrom").nil? && approved_assertion?(result.fetch("assertions"), category_ids)
150
+ if result.fetch("inheritedFrom").nil? && keeps_trace?(result.fetch("assertions"), narrowing)
118
151
  selected << trace_id
119
152
  break if selected.length == limit
120
153
  end
121
154
  end
122
- if selected.empty?
123
- if category_ids
124
- raise ArgumentError,
125
- "No traces with an approved assertion in the given categories matched this replay selection."
126
- end
127
- raise ArgumentError, "No traces with approved assertions matched this replay selection."
128
- end
155
+ raise ArgumentError, no_traces_matched_message(narrowing) if selected.empty?
156
+
129
157
  selected
130
158
  end
131
159
 
132
- def pinned_dataset_members(client, dataset_ids, limit, only_with_assertions = false, category_ids = nil)
160
+ def pinned_dataset_members(client, dataset_ids, limit, narrowing = {})
133
161
  members = dataset_ids.flat_map { |dataset_id|
134
162
  client.datasets.list_traces(dataset_id).fetch("traceIds")
135
163
  }.uniq.sort
@@ -142,13 +170,14 @@ module Bitfab
142
170
  "every selected dataset in full."
143
171
  end
144
172
 
145
- bound_trace_ids(client, members, limit, only_with_assertions, category_ids)
173
+ bound_trace_ids(client, members, limit, narrowing)
146
174
  end
147
175
 
148
176
  def run(registry, argv: ARGV, stdout: $stdout, stderr: $stderr)
149
177
  args = parse(registry, argv.dup)
150
178
  return SeedCli.run(registry, argv:, stdout:, stderr:) if args[:seed] || args[:from_trace]
151
179
  require_code_change(args)
180
+ require_experiment_group(args)
152
181
  stderr.puts "Bitfab: #{Replay::JUDGE_ASSERTIONS_DEPRECATION}" if args[:judge_assertions]
153
182
 
154
183
  registration = registry.fetch(args.fetch(:pipeline))
@@ -180,8 +209,12 @@ module Bitfab
180
209
  options.delete(:trace_ids)
181
210
  end
182
211
 
183
- only_with_assertions = args[:only_with_assertions] || options[:only_with_assertions]
184
212
  category_ids = args[:category_ids] || options[:category_ids]
213
+ narrowing = {
214
+ only_with_assertions: args[:only_with_assertions] || options[:only_with_assertions],
215
+ only_drafts: args[:only_drafts] || options[:only_drafts],
216
+ category_ids:
217
+ }
185
218
  bound = args[:limit] || options[:limit]
186
219
  options.delete(:limit)
187
220
  bounded_dataset_ids = args[:dataset_ids] || options[:dataset_ids]
@@ -193,20 +226,18 @@ module Bitfab
193
226
 
194
227
  if options[:trace_ids]
195
228
  if bound
196
- options[:trace_ids] = bound_trace_ids(registration.client, options[:trace_ids], bound, only_with_assertions,
197
- category_ids)
229
+ options[:trace_ids] = bound_trace_ids(registration.client, options[:trace_ids], bound, narrowing)
198
230
  end
199
231
  elsif bounded_dataset_ids
200
232
  if bound
201
- pinned = pinned_dataset_members(registration.client, bounded_dataset_ids, bound, only_with_assertions,
202
- category_ids)
233
+ pinned = pinned_dataset_members(registration.client, bounded_dataset_ids, bound, narrowing)
203
234
  options[:trace_ids] = pinned if pinned
204
235
  end
205
236
  else
206
237
  options[:limit] = bound || 10
207
238
  end
208
239
 
209
- %i[name notes metadata max_concurrency experiment_group_id experiment_group_name baseline_experiment_id dataset_ids grader_ids mock attempts only_with_assertions category_ids skip_assertion_judging dry_run].each do |key|
240
+ %i[name notes metadata max_concurrency experiment_group_id experiment_group_name baseline_experiment_id skip_experiment_group agent_session_id dataset_ids grader_ids mock attempts only_with_assertions only_drafts category_ids skip_assertion_judging dry_run].each do |key|
210
241
  options[key] = args[key] unless args[key].nil?
211
242
  end
212
243
 
@@ -328,12 +359,16 @@ module Bitfab
328
359
  "How many traces to replay. It bounds a trace ID or dataset selection instead of replacing it") do |value|
329
360
  args[:limit] = positive_integer("--limit", value)
330
361
  end
331
- options.on("--trace-ids IDS") { |value| args[:trace_ids] = comma_separated("--trace-ids", value) }
362
+ options.on("--trace-ids IDS",
363
+ "Traces to replay. With --dataset-ids it replays only these members of the datasets, " \
364
+ "recorded on the experiment as a subset of them") do |value|
365
+ args[:trace_ids] = comma_separated("--trace-ids", value)
366
+ end
332
367
  options.on("--seed PATH", "--cases PATH") { |value| args[:seed] = value }
333
368
  options.on("--run") { args[:run] = true }
334
369
  options.on("--from-trace IDS") { |value| args[:from_trace] = comma_separated("--from-trace", value) }
335
370
  options.on("--name NAME",
336
- "What this run is testing, in a few words, such as 'baseline' or 'shorter system prompt'. " \
371
+ "What this run changes, in a few words, such as 'baseline' or 'shorter system prompt'. " \
337
372
  "Bitfab records the commit, branch, tree state, datasets, and who ran it with every experiment, " \
338
373
  "so do not repeat them here.") { |value| args[:name] = value }
339
374
  options.on("--notes TEXT",
@@ -371,24 +406,35 @@ module Bitfab
371
406
  options.on("--code-change-base BRANCH|COMMIT",
372
407
  "With --code-change ref: the branch or commit to diff against.") { |value| args[:code_change_base] = value }
373
408
  options.on("--experiment-group-id UUID",
374
- "Add this run to an experiment group. A UUID no group has yet creates the group.") { |value| args[:experiment_group_id] = value }
409
+ "Required unless --dry-run or --skip-experiment-group: the experiment group this run belongs to, one series of experiments " \
410
+ "testing one thing. Reuse the id of the group whose runs test the same thing, or pass a new UUID " \
411
+ "with --experiment-group-name, named after what this run tests, to start a group.") { |value| args[:experiment_group_id] = value }
375
412
  options.on("--experiment-group-name NAME",
376
- "Name the experiment group when this run creates it or it has no name yet. " \
413
+ "Name the experiment group after what this run tests, when this run creates it or it has no name yet. " \
377
414
  "A group that already has a name keeps it. Requires --experiment-group-id.") { |value| args[:experiment_group_name] = value }
378
415
  options.on("--baseline-experiment-id UUID",
379
416
  "Compare this run against an earlier experiment instead of the group's first finished run " \
380
417
  "or the original traces.") { |value| args[:baseline_experiment_id] = value }
418
+ options.on("--skip-experiment-group",
419
+ "Run without an experiment group, for scripts and schedules whose runs should not be grouped. " \
420
+ "Cannot be combined with --experiment-group-id or --experiment-group-name.") { args[:skip_experiment_group] = true }
421
+ options.on("--agent-session-id ID",
422
+ "The coding agent session that launched this run, stored on the experiment. Defaults to " \
423
+ "CLAUDE_CODE_SESSION_ID, CODEX_THREAD_ID, or AGENT_SESSION_ID, the first one set.") { |value| args[:agent_session_id] = value }
381
424
  options.on("--dataset-ids IDS", "--dataset-id IDS") { |value| args[:dataset_ids] = comma_separated("--dataset-ids", value) }
382
425
  options.on("--grader-ids IDS") { |value| args[:grader_ids] = comma_separated("--grader-ids", value) }
383
426
  options.on("--attempts N", Integer) { |value| args[:attempts] = positive_integer("--attempts", value) }
384
427
  options.on("--only-with-assertions") { args[:only_with_assertions] = true }
428
+ options.on("--only-drafts",
429
+ "Replay only traces carrying a draft assertion (not yet confirmed or rejected); " \
430
+ "with --dataset-ids the run is recorded as a subset of the dataset") { args[:only_drafts] = true }
385
431
  options.on("--category-ids id1,id2",
386
- "With --trace-ids or --dataset-ids: replay only the traces carrying an approved assertion in one of " \
432
+ "With --trace-ids or --dataset-ids: replay only the traces carrying a confirmed assertion in one of " \
387
433
  "these categories; which assertions are judged stays the same") do |value|
388
434
  args[:category_ids] = comma_separated("--category-ids", value)
389
435
  end
390
436
  options.on("--skip-assertion-judging",
391
- "Don't judge approved assertions on each replay; judging is on by default and costs model calls") { args[:skip_assertion_judging] = true }
437
+ "Don't judge confirmed assertions on each replay; judging is on by default and costs model calls") { args[:skip_assertion_judging] = true }
392
438
  options.on("--judge-assertions", "Deprecated: judging is on by default") { args[:judge_assertions] = true }
393
439
  options.on("--dry-run") { args[:dry_run] = true }
394
440
  options.on("--fail-on-error",
@@ -403,10 +449,6 @@ module Bitfab
403
449
  options.on("--param NAME=VALUE") { |value| (args[:param] ||= []) << value }
404
450
  end
405
451
  parser.parse!(argv)
406
- if args[:trace_ids] && args[:dataset_ids]
407
- raise OptionParser::InvalidArgument,
408
- "--trace-ids and --dataset-ids select different replay sources and cannot be used together"
409
- end
410
452
  pipeline = argv.shift
411
453
  known = pipeline && (registry.nil? || registry.names.include?(pipeline))
412
454
  raise OptionParser::MissingArgument, parser.banner unless known
@@ -431,6 +473,17 @@ module Bitfab
431
473
  "--code-change-base goes with --code-change ref, and --code-change ref needs --code-change-base BRANCH|COMMIT."
432
474
  end
433
475
 
476
+ def require_experiment_group(args)
477
+ if args[:skip_experiment_group]
478
+ raise ArgumentError, SKIP_EXPERIMENT_GROUP_CONFLICT unless args[:experiment_group_id].nil? && args[:experiment_group_name].nil?
479
+
480
+ return args
481
+ end
482
+ return args if args[:dry_run] || !args[:experiment_group_id].to_s.strip.empty?
483
+
484
+ raise ArgumentError, EXPERIMENT_GROUP_REQUIRED
485
+ end
486
+
434
487
  def registry_dataset_ids(options)
435
488
  dataset_id = options[:dataset_id]
436
489
  dataset_ids = options[:dataset_ids]
@@ -444,6 +497,15 @@ module Bitfab
444
497
  end
445
498
 
446
499
  def describe_selection(options)
500
+ selection = describe_selected_traces(options)
501
+ options[:only_drafts] ? "#{selection} with a draft assertion" : selection
502
+ end
503
+
504
+ def describe_selected_traces(options)
505
+ if options[:trace_ids] && options[:dataset_ids]
506
+ datasets = (options[:dataset_ids].length == 1) ? "the dataset" : "#{options[:dataset_ids].length} datasets"
507
+ return "#{options[:trace_ids].length} traces of #{datasets}"
508
+ end
447
509
  return "#{options[:trace_ids].length} traces" if options[:trace_ids]
448
510
  return "#{options[:limit]} traces" if options[:limit]
449
511
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bitfab
4
- VERSION = "0.76.9"
4
+ VERSION = "0.78.0"
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.9
4
+ version: 0.78.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harvest Team
@@ -169,6 +169,7 @@ files:
169
169
  - exe/bitfab-replay
170
170
  - exe/bitfab-seed
171
171
  - lib/bitfab.rb
172
+ - lib/bitfab/agent_session.rb
172
173
  - lib/bitfab/assertions.rb
173
174
  - lib/bitfab/baml.rb
174
175
  - lib/bitfab/categories.rb