99
1010import argparse
1111import json
12- import os
1312import sys
14- import time
15- import uuid
1613from pathlib import Path
1714
15+ from benchmark .agent_spool import spool_request
16+
1817MAX_CERTIFICATE_BYTES = 1024 * 1024
1918DEFAULT_REQUEST_TIMEOUT_SECONDS = 300
20- POLL_INTERVAL_SECONDS = 0.02
2119
2220
2321def _read_certificate (path : str ) -> str :
@@ -32,67 +30,46 @@ def _read_certificate(path: str) -> str:
3230 return raw .decode ("utf-8" )
3331
3432
35- def _request (payload : dict ) -> dict :
36- channel = os .environ .get ("PRB_SUBMIT_DIR" )
37- if not channel :
38- raise RuntimeError ("submit is only available inside an active benchmark evaluation" )
39- channel_dir = Path (channel )
40- request_id = uuid .uuid4 ().hex
41- inbox = channel_dir / "inbox"
42- outbox = channel_dir / "outbox"
43- request_path = inbox / f"{ request_id } .json"
44- temp_path = inbox / f".{ request_id } .{ os .getpid ()} .tmp"
45- response_path = outbox / f"{ request_id } .json"
46-
47- envelope = {"request_id" : request_id , ** payload }
48- temp_path .write_text (json .dumps (envelope ), encoding = "utf-8" )
49- os .replace (temp_path , request_path )
50-
51- timeout = int (os .environ .get ("PRB_SUBMIT_TIMEOUT" , DEFAULT_REQUEST_TIMEOUT_SECONDS ))
52- deadline = time .monotonic () + timeout
53- while time .monotonic () < deadline :
54- try :
55- raw = response_path .read_text (encoding = "utf-8" )
56- except FileNotFoundError :
57- time .sleep (POLL_INTERVAL_SECONDS )
58- continue
59- response = json .loads (raw )
60- try :
61- response_path .unlink ()
62- except FileNotFoundError :
63- pass
64- return response
65- raise TimeoutError (f"submit service did not respond within { timeout } s" )
33+ def _request (payload : dict , request_id : str | None = None ) -> dict :
34+ return spool_request (
35+ payload ,
36+ channel_env = "PRB_SUBMIT_DIR" ,
37+ timeout_env = "PRB_SUBMIT_TIMEOUT" ,
38+ default_timeout_seconds = DEFAULT_REQUEST_TIMEOUT_SECONDS ,
39+ service_name = "submit service" ,
40+ request_id = request_id ,
41+ )
6642
6743
6844def main () -> None :
6945 parser = argparse .ArgumentParser (
7046 prog = "submit" ,
7147 description = "Submit one rule counterexample to the benchmark verifier. Every "
72- "submission, accepted or rejected, consumes one run-wide attempt." ,
48+ "submission, accepted or rejected, consumes one rule-scoped attempt." ,
7349 )
7450 parser .add_argument ("certificate" , nargs = "?" , help = "Certificate JSON file, or - for stdin" )
7551 parser .add_argument ("--status" , action = "store_true" , help = "Show remaining attempts (free)" )
52+ parser .add_argument ("--request-id" , help = argparse .SUPPRESS )
7653 args = parser .parse_args ()
7754
7855 try :
7956 if args .status :
8057 if args .certificate :
8158 parser .error ("--status does not take a certificate" )
82- response = _request ({"op" : "status" })
59+ response = _request ({"op" : "status" }, args . request_id )
8360 print (f"submit budget: { response ['used' ]} /{ response ['limit' ]} used, "
8461 f"{ response ['remaining' ]} remaining" )
8562 return
8663 if not args .certificate :
8764 parser .error ("a certificate JSON file is required (use - for stdin)" )
8865 try :
8966 text = _read_certificate (args .certificate )
90- response = _request ({"op" : "submit" , "certificate_text" : text })
67+ response = _request ({"op" : "submit" , "certificate_text" : text }, args . request_id )
9168 except (OSError , UnicodeError , ValueError ) as e :
9269 # File/encoding/size failures are still submission attempts; let the service
9370 # account for them instead of failing locally without consuming budget.
94- response = _request ({"op" : "submit" , "client_error" : str (e )})
95- except (OSError , ValueError , json .JSONDecodeError , RuntimeError ) as e :
71+ response = _request ({"op" : "submit" , "client_error" : str (e )}, args . request_id )
72+ except (OSError , ValueError , json .JSONDecodeError , RuntimeError , TimeoutError ) as e :
9673 print (f"submit unavailable: { e } " , file = sys .stderr )
9774 raise SystemExit (2 )
9875
@@ -102,9 +79,10 @@ def main() -> None:
10279 return
10380
10481 reason = response .get ("reason" ) or response .get ("error" ) or "rejected"
105- label = "BUDGET_EXHAUSTED" if response .get ("exhausted" ) else "REJECTED"
82+ label = ("INFRASTRUCTURE_ERROR" if response .get ("infrastructure_error" ) else
83+ "BUDGET_EXHAUSTED" if response .get ("exhausted" ) else "REJECTED" )
10684 print (f"{ label } attempt { used } /{ limit } ({ remaining } remaining): { reason } " , file = sys .stderr )
107- raise SystemExit (2 if response .get ("exhausted" ) else 1 )
85+ raise SystemExit (2 if response .get ("exhausted" ) or response . get ( "infrastructure_error" ) else 1 )
10886
10987
11088if __name__ == "__main__" :
0 commit comments