verifiers 0.4.2.dev96__py3-none-any.whl → 0.4.2.dev107__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.
@@ -2,14 +2,19 @@
2
2
 
3
3
  import argparse
4
4
  import asyncio
5
+ import fcntl
5
6
  import json
6
7
  import logging
7
8
  import os
9
+ import select
8
10
  import selectors
11
+ import struct
9
12
  import subprocess
13
+ import termios
14
+ import threading
10
15
  import time
11
16
  import traceback
12
- from contextlib import AsyncExitStack
17
+ from contextlib import AsyncExitStack, suppress
13
18
  from pathlib import Path
14
19
  from typing import TYPE_CHECKING
15
20
 
@@ -199,25 +204,94 @@ class _BashOutput:
199
204
  self.size += len(chunk)
200
205
 
201
206
 
207
+ BASH_TIMEOUT = 3600
208
+
209
+ EXIT_GRACE = 0.5
210
+ """Seconds to keep reading after bash exits, e.g. for a process substitution."""
211
+
212
+ BACKGROUND_NOTE = "[a process started by this command is still running and holds its output; later output is not shown]"
213
+
214
+
215
+ def _pending(fd: int) -> int:
216
+ """Bytes waiting in a pipe right now."""
217
+ return struct.unpack("i", fcntl.ioctl(fd, termios.FIONREAD, b"\0" * 4))[0]
218
+
219
+
220
+ def _at_end(fd: int) -> bool:
221
+ """Whether every writer has closed the pipe; late bytes read here are dropped."""
222
+ poller = select.poll() # select.select rejects fds >= 1024
223
+ poller.register(fd, select.POLLIN)
224
+ if not poller.poll(0):
225
+ return False
226
+ return not os.read(fd, 65536)
227
+
228
+
229
+ def _discard_until_eof(fd: int) -> None:
230
+ """Read a held pipe to EOF, so its writer neither blocks nor gets SIGPIPE."""
231
+ with open(fd, "rb", buffering=0) as pipe:
232
+ while pipe.read(65536):
233
+ pass
234
+
235
+
236
+ def _drain_in_background(fd: int) -> None:
237
+ """Drain a held pipe on a daemon thread; never raises."""
238
+ try:
239
+ copy = os.dup(fd)
240
+ except OSError:
241
+ return
242
+ try:
243
+ threading.Thread(target=_discard_until_eof, args=(copy,), daemon=True).start()
244
+ except Exception: # noqa: BLE001 - e.g. no more threads
245
+ with suppress(OSError):
246
+ os.close(copy)
247
+
248
+
202
249
  def run_bash(command: str) -> str:
250
+ held_open = False
203
251
  try:
204
252
  with subprocess.Popen(
205
253
  ["bash", "-c", command],
254
+ stdin=subprocess.DEVNULL,
206
255
  stdout=subprocess.PIPE,
207
256
  stderr=subprocess.PIPE,
208
257
  ) as process:
209
258
  stdout, stderr = _BashOutput(), _BashOutput()
210
- deadline = time.monotonic() + 3600
259
+ deadline = time.monotonic() + BASH_TIMEOUT
260
+ exited = None
211
261
  try:
212
262
  with selectors.DefaultSelector() as selector:
213
263
  assert process.stdout is not None and process.stderr is not None
214
264
  selector.register(process.stdout, selectors.EVENT_READ, stdout)
215
265
  selector.register(process.stderr, selectors.EVENT_READ, stderr)
216
266
  while selector.get_map():
217
- remaining = deadline - time.monotonic()
218
- if remaining <= 0:
219
- raise subprocess.TimeoutExpired(process.args, 3600)
220
- for key, _ in selector.select(remaining):
267
+ now = time.monotonic()
268
+ if exited is None and process.poll() is not None:
269
+ exited = now
270
+ if exited is None:
271
+ if now >= deadline:
272
+ raise subprocess.TimeoutExpired(
273
+ process.args, BASH_TIMEOUT
274
+ )
275
+ wait = min(deadline - now, 0.1)
276
+ elif now - exited < EXIT_GRACE:
277
+ # A process bash did not wait for may still be writing.
278
+ wait = exited + EXIT_GRACE - now
279
+ else:
280
+ # A pipe still open is held by a job left running.
281
+ for key in list(selector.get_map().values()):
282
+ pending = _pending(key.fd)
283
+ while pending > 0:
284
+ chunk = os.read(key.fd, min(pending, 65536))
285
+ if not chunk:
286
+ break
287
+ pending -= len(chunk)
288
+ key.data.add(chunk)
289
+ selector.unregister(key.fileobj)
290
+ if not _at_end(key.fd):
291
+ held_open = True
292
+ _drain_in_background(key.fd)
293
+ break
294
+ for key, _ in selector.select(wait):
221
295
  chunk = os.read(key.fd, 8192)
222
296
  if chunk:
223
297
  key.data.add(chunk)
@@ -230,19 +304,29 @@ def run_bash(command: str) -> str:
230
304
  process.kill()
231
305
  process.wait()
232
306
  size = stdout.size + stderr.size
233
- if size <= TOOL_OUTPUT_MAX_BYTES:
307
+ # Leave room for the note.
308
+ budget = TOOL_OUTPUT_MAX_BYTES - (
309
+ len(BACKGROUND_NOTE.encode()) + 1 if held_open else 0
310
+ )
311
+ if size <= budget:
312
+ # A held pipe may be cut mid-character.
234
313
  text = "".join(
235
- output.head.decode().replace("\r\n", "\n").replace("\r", "\n")
314
+ output.head.decode(errors="replace")
315
+ .replace("\r\n", "\n")
316
+ .replace("\r", "\n")
236
317
  for output in (stdout, stderr)
237
318
  )
238
319
  else:
239
320
  marker = f"\n[... bash output truncated; {size} bytes total ...]\n"
240
- keep = (TOOL_OUTPUT_MAX_BYTES - len(marker.encode())) // 2
321
+ keep = (budget - len(marker.encode())) // 2
241
322
  # Preserve stdout-then-stderr ordering, including when just one is large.
242
323
  head = (stdout.head + stderr.head)[:keep].decode(errors="ignore")
243
324
  tail = (stdout.tail + stderr.tail)[-keep:].decode(errors="ignore")
244
325
  text = head + marker + tail
245
- return text.replace("\r\n", "\n").replace("\r", "\n")
326
+ text = text.replace("\r\n", "\n").replace("\r", "\n")
327
+ if held_open:
328
+ text += ("\n" if text and not text.endswith("\n") else "") + BACKGROUND_NOTE
329
+ return text
246
330
  except Exception as e: # noqa: BLE001 - tool failures are returned to the model
247
331
  return f"error: {e}"
248
332
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: verifiers
3
- Version: 0.4.2.dev96
3
+ Version: 0.4.2.dev107
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
@@ -112,7 +112,7 @@ verifiers/v1/harnesses/stirrup/harness.py,sha256=Bcm7m1IzFnseiAa6xRKHSFKYrqSRpvx
112
112
  verifiers/v1/harnesses/stirrup/program.py,sha256=0xj35Teph6BW_4ydW921J18MPCBRXNfI62J21nh0dFw,15148
113
113
  verifiers/v1/harnesses/utils/__init__.py,sha256=eTS-Ft2HwoLiYn5u1t_KueLmaZB_W12cdYmcVsE-G5A,364
114
114
  verifiers/v1/harnesses/utils/compaction.py,sha256=DeY6CYLEsv7-vJm26sT2mRaPHu3W2ywiZhvFr_pi6Hg,10495
115
- verifiers/v1/harnesses/utils/core.py,sha256=RBLi3R5yGrIt2YixvPrWlCbR5l4KmKLvSMU3D704w9U,23733
115
+ verifiers/v1/harnesses/utils/core.py,sha256=Q3xsnk3PtYYcA73p6by7QbjfcfaQVIhRyg3Qi6Ca-H4,26923
116
116
  verifiers/v1/harnesses/utils/install.py,sha256=EESYSh_e9L4gj6N4GvAbQY2kb4ckven7tmtlQEErPiQ,3563
117
117
  verifiers/v1/harnesses/utils/launch.py,sha256=2KuLnNruUqH6vcW2Ru661P86ffRzaqslNs-kDMn7B6U,5670
118
118
  verifiers/v1/harnesses/utils/mcp.py,sha256=EtDraHEqgsOB6xKvcF2z9C6HlQyKoST0ZDvmy47NWgM,7450
@@ -185,8 +185,8 @@ verifiers/v1/utils/scope.py,sha256=zbs4pj6AVk4jywlAJ3AX0Q0_kzB9dBB02nY-9H4ELdQ,8
185
185
  verifiers/v1/utils/score.py,sha256=493yJVMw8teCu9JxapxMFPFzI0hNUqdo0Y2nGW4kckk,6200
186
186
  verifiers/v1/utils/trace_store.py,sha256=7F6xMhYgl3qmylnCU8-ekqswH16fUat1uegeM1OR9QQ,2563
187
187
  verifiers/v1/utils/version.py,sha256=-obEo_-l9-D8FLef4hYxncOe-uJpxrM1g2Hig_37Sgs,1607
188
- verifiers-0.4.2.dev96.dist-info/METADATA,sha256=Ek7ScmcNBGK2Kw_JM160q2KHdc7F0mwkZd564ix2EXI,5117
189
- verifiers-0.4.2.dev96.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
190
- verifiers-0.4.2.dev96.dist-info/entry_points.txt,sha256=qdMg7ajiFpDsHf7tuPMRclJXizu1J6LTWawS5YS0B0I,137
191
- verifiers-0.4.2.dev96.dist-info/licenses/LICENSE,sha256=v0RrUsdV3IDoZhrRce297IXS3xMHNJ-_LdLpFAUWb9k,1072
192
- verifiers-0.4.2.dev96.dist-info/RECORD,,
188
+ verifiers-0.4.2.dev107.dist-info/METADATA,sha256=zVaqDyxV-PdyI5ahAnhEpvQJ7ss6GnQKmSwYky7C8wo,5118
189
+ verifiers-0.4.2.dev107.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
190
+ verifiers-0.4.2.dev107.dist-info/entry_points.txt,sha256=qdMg7ajiFpDsHf7tuPMRclJXizu1J6LTWawS5YS0B0I,137
191
+ verifiers-0.4.2.dev107.dist-info/licenses/LICENSE,sha256=v0RrUsdV3IDoZhrRce297IXS3xMHNJ-_LdLpFAUWb9k,1072
192
+ verifiers-0.4.2.dev107.dist-info/RECORD,,