File size: 4,179 Bytes
387a4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Run model-written functions with an isolated filesystem and no network.

Bubblewrap namespaces protect the host; a small Python interface also gives the
selector stable seeded randomness, bounded output and documented operations.
https://github.com/containers/bubblewrap
"""

from __future__ import annotations

import functools
import json
import os
import subprocess

DRIVER = """
import ast, json, math, random, resource, sys
resource.setrlimit(resource.RLIMIT_CPU, (2, 2))
resource.setrlimit(resource.RLIMIT_AS, (128*1024**2, 128*1024**2))
resource.setrlimit(resource.RLIMIT_FSIZE, (0, 0))
resource.setrlimit(resource.RLIMIT_NPROC, (0, 0))
p=json.load(sys.stdin)
tree=ast.parse(p['source'])
for n in ast.walk(tree):
    if isinstance(n, (ast.Import, ast.ImportFrom, ast.Global, ast.Nonlocal, ast.ClassDef)):
        raise ValueError('Use the supplied math module and rng object')
    if isinstance(n,ast.Attribute) and n.attr.startswith('_') or isinstance(n,ast.Name) and n.id.startswith('_'):
        raise ValueError('Private attributes are unavailable')
env={'__builtins__':{k: __builtins__.__dict__[k] for k in ('abs','all','any','bool','dict','enumerate','float','int','len','list','max','min','pow','range','reversed','round','set','sorted','str','sum','tuple','zip','isinstance')},'math':math}
exec(compile(tree,'<workspace>','exec'),env)
fn=env[p['function']]
args=[p['context']]
if p['function']=='select_parent': args.append(random.Random(p['seed']))
result=fn(*args)
if not isinstance(result,str) or not 0<len(result)<=8000: raise ValueError('Expected a nonempty bounded string')
print(json.dumps(result,ensure_ascii=True))
"""


class Worker:
    """Expose one JSON-in/string-out invocation to the host orchestrator."""

    def call(self, source: str, function: str, context: dict, seed: int = 0) -> str:
        if len(source) > 16000:
            raise ValueError("Workspace source exceeds the worker limit")
        prefix = (
            ["wsl", "-d", os.getenv("RQGM_WSL_DISTRO", "Ubuntu"), "--exec"]
            if os.name == "nt"
            else []
        )
        # Mount runtime binaries alone. The Windows workspace, home and dataset
        # are absent, as are host credentials and network interfaces.
        command = prefix + [
            "bwrap",
            "--unshare-all",
            "--clearenv",
            "--new-session",
            "--die-with-parent",
            "--ro-bind",
            "/usr",
            "/usr",
            "--symlink",
            "usr/lib",
            "/lib",
            "--symlink",
            "usr/lib64",
            "/lib64",
            "--symlink",
            "usr/bin",
            "/bin",
            "--tmpfs",
            "/tmp",
            "--dev",
            "/dev",
            "--proc",
            "/proc",
            "--chdir",
            "/tmp",
            "--setenv",
            "PYTHONIOENCODING",
            "utf-8",
            "/usr/bin/python3",
            "-I",
            "-S",
            "-c",
            DRIVER,
        ]
        payload = json.dumps(
            dict(source=source, function=function, context=context, seed=seed),
            ensure_ascii=True,
        )
        try:
            result = subprocess.run(
                command,
                input=payload.encode(),
                capture_output=True,
                timeout=12,
                check=False,
            )
        except subprocess.TimeoutExpired as exc:
            raise ValueError("Worker timed out") from exc
        if result.returncode:
            raise ValueError(
                result.stderr.decode("utf-8", "replace")[-500:]
                or f"Worker exited {result.returncode}"
            )
        try:
            return json.loads(result.stdout)
        except (ValueError, UnicodeError) as exc:
            raise ValueError("Worker did not return valid JSON") from exc

    @functools.lru_cache(maxsize=1024)
    def instruction(self, source: str, role: str, route: str = "") -> str:
        """Memoize only deterministic instruction calls with identical public context."""
        return self.call(source, "instruct", {"role": role, "route": route})