verifiers 0.3.2.dev23__py3-none-any.whl → 0.3.2.dev24__py3-none-any.whl

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.
verifiers/v1/graph.py CHANGED
@@ -312,13 +312,85 @@ def _head_index(trace: Trace) -> dict[tuple[int | None, str], int]:
312
312
  return trace._head_index
313
313
 
314
314
 
315
+ def _matching_node(
316
+ trace: Trace,
317
+ parent: int | None,
318
+ message: Message,
319
+ token_ids: list[int] | None = None,
320
+ ) -> int | None:
321
+ """Find an existing child, optionally requiring its exact physical token span.
322
+
323
+ The head index deliberately points at only the latest content-equivalent child. Token-level
324
+ prefix breaks can leave older physical variants under the same key, so a token mismatch falls
325
+ back to a reverse scan rather than materializing a duplicate of an already-existing variant.
326
+ """
327
+ key = (parent, message_hash(message))
328
+ indexed = _head_index(trace).get(key)
329
+ if indexed is not None and (
330
+ token_ids is None or trace.nodes[indexed].token_ids == token_ids
331
+ ):
332
+ return indexed
333
+ if token_ids is None:
334
+ return indexed
335
+ for node_id in range(len(trace.nodes) - 1, -1, -1):
336
+ if node_id == indexed:
337
+ continue
338
+ node = trace.nodes[node_id]
339
+ if (
340
+ node.parent == parent
341
+ and node.token_ids == token_ids
342
+ and message_hash(node.message) == key[1]
343
+ ):
344
+ return node_id
345
+ return None
346
+
347
+
348
+ def _matching_prefix_node(
349
+ trace: Trace,
350
+ parent: int | None,
351
+ message: Message,
352
+ prompt_ids: list[int],
353
+ start: int,
354
+ stop: int,
355
+ ) -> int | None:
356
+ """Find the longest content-equivalent child matching inside `[start, stop]`.
357
+
358
+ Renderers may leave a prompt-supplied assistant unattributed (`message_spans[i] is None`).
359
+ Its existing sampled node still owns physical tokens, so use those tokens to recover the
360
+ otherwise-missing message boundary. The next attributed message's start bounds the match:
361
+ a sampled variant must never consume tokens that the renderer assigned to that message.
362
+ """
363
+ key = (parent, message_hash(message))
364
+ indexed = _head_index(trace).get(key)
365
+ candidates: list[int] = []
366
+ if indexed is not None:
367
+ candidates.append(indexed)
368
+ candidates.extend(
369
+ node_id
370
+ for node_id in range(len(trace.nodes) - 1, -1, -1)
371
+ if node_id != indexed
372
+ and trace.nodes[node_id].parent == parent
373
+ and message_hash(trace.nodes[node_id].message) == key[1]
374
+ )
375
+ matches = [
376
+ node_id
377
+ for node_id in candidates
378
+ if start + len(trace.nodes[node_id].token_ids) <= stop
379
+ and prompt_ids[start : start + len(trace.nodes[node_id].token_ids)]
380
+ == trace.nodes[node_id].token_ids
381
+ ]
382
+ return max(
383
+ matches, key=lambda node_id: len(trace.nodes[node_id].token_ids), default=None
384
+ )
385
+
386
+
315
387
  @dataclass(frozen=True)
316
388
  class PendingTurn:
317
389
  """A resolved prompt waiting on model inference.
318
390
 
319
- `prepare_turn` does the one canonical graph prefix walk. Training clients use the resolved
320
- prefix for renderer bridging before inference, and `commit` uses the same prefix after
321
- inference to add only the prompt tail plus the sampled assistant response.
391
+ `prepare_turn` resolves the graph prefix used for renderer bridging before inference. `commit`
392
+ preserves that inference-producing prefix, extends it with any matching nodes committed while
393
+ inference was in flight, then adds only the remaining prompt tail and sampled response.
322
394
  """
323
395
 
324
396
  trace: Trace
@@ -390,6 +462,10 @@ class PendingTurn:
390
462
  parent = self.prefix_node_ids[-1] if self.prefix_node_ids else None
391
463
  index = _head_index(self.trace)
392
464
  for message in self.tail:
465
+ existing = _matching_node(self.trace, parent, message)
466
+ if existing is not None:
467
+ parent = existing
468
+ continue
393
469
  previous = parent
394
470
  self.trace.nodes.append(MessageNode(parent=parent, message=message))
395
471
  parent = len(self.trace.nodes) - 1
@@ -575,6 +651,45 @@ def _commit_turn(turn: PendingTurn, response: Response) -> int:
575
651
  keep += 1
576
652
  prefix = prefix[:keep]
577
653
  path_len = off
654
+
655
+ # A parallel request may have committed more of this prompt after `prepare_turn` resolved
656
+ # the inference prefix. Reconcile that still-uncommitted tail now, one whole message at a
657
+ # time. Exact token-span equality preserves intentional renderer-level forks; tokenless relay
658
+ # turns use message identity. Because commit is synchronous, once this loop stops the rest of
659
+ # the tail can be appended without another request interleaving.
660
+ prefix = list(prefix)
661
+ while len(prefix) < len(prompt):
662
+ i = len(prefix)
663
+ span = spans[i] if spans and i < len(spans) else None
664
+ end = span[1] if span else path_len
665
+ parent = prefix[-1] if prefix else None
666
+ if tokens is not None and span is None:
667
+ next_start = next(
668
+ (
669
+ later_span[0]
670
+ for later_span in (spans[i + 1 :] if spans else [])
671
+ if later_span is not None
672
+ ),
673
+ len(prompt_ids),
674
+ )
675
+ existing = _matching_prefix_node(
676
+ trace, parent, prompt[i], prompt_ids, path_len, next_start
677
+ )
678
+ if existing is not None:
679
+ end += len(trace.nodes[existing].token_ids)
680
+ else:
681
+ node_tokens = prompt_ids[path_len:end]
682
+ existing = _matching_node(
683
+ trace,
684
+ parent,
685
+ prompt[i],
686
+ node_tokens if tokens is not None else None,
687
+ )
688
+ if existing is None:
689
+ break
690
+ prefix.append(existing)
691
+ path_len = end
692
+
578
693
  num_reused = len(prefix)
579
694
  parent = prefix[-1] if prefix else None
580
695
  # cursor: in prompt_ids, the end of the previous *new* message's tokens
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: verifiers
3
- Version: 0.3.2.dev23
3
+ Version: 0.3.2.dev24
4
4
  Summary: Verifiers: Environments for LLM Reinforcement Learning
5
5
  Project-URL: Homepage, https://github.com/primeintellect-ai/verifiers
6
6
  Project-URL: Documentation, https://github.com/primeintellect-ai/verifiers
@@ -4,7 +4,7 @@ verifiers/v1/agent.py,sha256=AfjyYmcGpVaOlZkElIz1JZeNRQuZsE_KMZ-LV3uLvzA,31644
4
4
  verifiers/v1/env.py,sha256=CEhzjojhg5jqbOYruUnc5U7TtgDHDSx8NiiFA0qfbt8,18234
5
5
  verifiers/v1/episode.py,sha256=uStvI4WqgJT50hmHIB55m5B-ASqqLz-fZ-ynBz9uzEY,5474
6
6
  verifiers/v1/errors.py,sha256=uQySmCUrjDYFkBIii1vBFPiEcPFPQZ9EGpas_6HvcQI,5279
7
- verifiers/v1/graph.py,sha256=vjxIrE0e-IXeb8fIY0pRGfwrQlGsIAe9IjwHNZXqizk,30652
7
+ verifiers/v1/graph.py,sha256=W5JkeUQwtYwQY8pyI2YN6Ci7G8D4PFktfKf2PxzhuwE,34996
8
8
  verifiers/v1/harness.py,sha256=ziECqEBWV34UWbLlxrkhY6MwkbVYkoXBOoaY7mZxwTs,14753
9
9
  verifiers/v1/judge.py,sha256=Pvr0C41ah1qkNSZ0WXdkDvBMfv5YSfY1REHncwlJ8cg,9346
10
10
  verifiers/v1/rollout.py,sha256=EnF3RuBfMyKNjJIs0LZaImkSvfe9LYLXc8dYP-1a2Ew,23628
@@ -183,8 +183,8 @@ verifiers/v1/utils/prime.py,sha256=UTYRjp9cbjNb6CVmBHda-1wWZAIIfxNOmTyNuT7_wL4,9
183
183
  verifiers/v1/utils/retries.py,sha256=Y2ZgrAjn-qNkRKZP_RVNL_7EK0iaRcZeCa404lpGYi4,5417
184
184
  verifiers/v1/utils/score.py,sha256=493yJVMw8teCu9JxapxMFPFzI0hNUqdo0Y2nGW4kckk,6200
185
185
  verifiers/v1/utils/version.py,sha256=-obEo_-l9-D8FLef4hYxncOe-uJpxrM1g2Hig_37Sgs,1607
186
- verifiers-0.3.2.dev23.dist-info/METADATA,sha256=PdLAJi_T-NSJykGDmb4tj-piqBOVCf4s8z5-Fau2S98,4144
187
- verifiers-0.3.2.dev23.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
188
- verifiers-0.3.2.dev23.dist-info/entry_points.txt,sha256=uqQje0TMsr7k6nXlWxlPs13Si0evb3pVCEDF6c-YL80,241
189
- verifiers-0.3.2.dev23.dist-info/licenses/LICENSE,sha256=v0RrUsdV3IDoZhrRce297IXS3xMHNJ-_LdLpFAUWb9k,1072
190
- verifiers-0.3.2.dev23.dist-info/RECORD,,
186
+ verifiers-0.3.2.dev24.dist-info/METADATA,sha256=ubhsm2wGxJo5JbBqs2ZHTZU6MMbo60B1ZTC3bHcd0pU,4144
187
+ verifiers-0.3.2.dev24.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
188
+ verifiers-0.3.2.dev24.dist-info/entry_points.txt,sha256=uqQje0TMsr7k6nXlWxlPs13Si0evb3pVCEDF6c-YL80,241
189
+ verifiers-0.3.2.dev24.dist-info/licenses/LICENSE,sha256=v0RrUsdV3IDoZhrRce297IXS3xMHNJ-_LdLpFAUWb9k,1072
190
+ verifiers-0.3.2.dev24.dist-info/RECORD,,