Coverage for src/hypra/native/worker.py: 51.67%

48 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-26 22:12 +0000

1"""Optional killable native computation process; exact JSON, no source evaluation.""" 

2 

3from __future__ import annotations 

4 

5import json 

6import subprocess 

7import sys 

8from dataclasses import asdict 

9 

10from .._json import strict_json_loads 

11from ..backend import BackendProtocolError, BackendTimeoutError 

12from ..errors import IncompleteExpansionError, UnsupportedExpressionError 

13 

14 

15def run_bounded(request, config): 

16 settings = asdict(config) 

17 timeout = settings.pop("timeout_seconds") 

18 payload = json.dumps({"request": request, "config": settings}) 

19 try: 

20 completed = subprocess.run( 

21 [sys.executable, "-m", "hypra.native.worker"], 

22 input=payload, 

23 capture_output=True, 

24 text=True, 

25 timeout=timeout, 

26 check=False, 

27 ) 

28 except subprocess.TimeoutExpired as exc: 

29 raise BackendTimeoutError(f"Native expansion exceeded {timeout:g} seconds") from exc 

30 if completed.returncode != 0: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true

31 raise BackendProtocolError("Native computation process failed without a valid result") 

32 try: 

33 envelope = strict_json_loads(completed.stdout) 

34 if not isinstance(envelope, dict): 34 ↛ 35line 34 didn't jump to line 35 because the condition on line 34 was never true

35 raise BackendProtocolError("Native worker response is not an object") 

36 if "error" in envelope: 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true

37 kind = envelope["error"] 

38 message = envelope.get("message", "Native worker failed") 

39 if not isinstance(message, str): 

40 raise BackendProtocolError("Invalid native worker error message") 

41 classes = { 

42 "UnsupportedExpressionError": UnsupportedExpressionError, 

43 "IncompleteExpansionError": IncompleteExpansionError, 

44 } 

45 raise classes.get(kind, BackendProtocolError)(message) 

46 response = envelope["response"] 

47 if not isinstance(response, dict): 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 raise BackendProtocolError("Invalid native worker response") 

49 return response 

50 except (KeyError, json.JSONDecodeError) as exc: 

51 raise BackendProtocolError("Invalid native worker response") from exc 

52 

53 

54def main(): 

55 from ..native import NativeBackend, NativeConfig 

56 

57 try: 

58 payload = strict_json_loads(sys.stdin.read()) 

59 settings = payload["config"] 

60 settings["timeout_seconds"] = None 

61 response = NativeBackend(NativeConfig(**settings)).expand(payload["request"]) 

62 result = {"response": response} 

63 except Exception as exc: # noqa: BLE001 - child-process exception boundary 

64 result = {"error": type(exc).__name__, "message": str(exc)} 

65 print(json.dumps(result)) 

66 

67 

68if __name__ == "__main__": 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 main()