File size: 4,995 Bytes
1049942
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""Actual OpenEnv rollout/capture smoke test with scripted endpoint and oracle.

No paid model, model capability score, or training token/logprob validation.
"""

import argparse
import asyncio
import json
import socket
import threading
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path

from harbor.agents.factory import AgentFactory
from harbor.models.agent.name import AgentName
from openenv.core.harness.capture import CaptureServer
from openenv.harbor import seams
from openenv.harbor.rollout import run_rollout
from openenv.harbor.tasks import resolve_task_dirs


class ScriptedEndpoint(BaseHTTPRequestHandler):
    def do_POST(self):
        json.loads(self.rfile.read(int(self.headers["Content-Length"])))
        result = {
            "id": "chatcmpl-validation",
            "object": "chat.completion",
            "created": 1,
            "model": "validation-oracle",
            "choices": [
                {
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": "Validation acknowledged.",
                    },
                    "finish_reason": "stop",
                }
            ],
            "usage": {"prompt_tokens": 10, "completion_tokens": 3, "total_tokens": 13},
        }
        body = json.dumps(result).encode()
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

    def log_message(self, *_):
        pass


async def main(args):
    root = Path(__file__).resolve().parents[1]
    paths = resolve_task_dirs(str(args.dataset))
    if args.task_ids:
        paths = [path for path in paths if path.name in args.task_ids]
        assert len(paths) == len(args.task_ids)
    scripted = ThreadingHTTPServer(("127.0.0.1", 0), ScriptedEndpoint)
    thread = threading.Thread(target=scripted.serve_forever, daemon=True)
    thread.start()
    with socket.socket() as reservation:
        reservation.bind(("127.0.0.1", 0))
        port = reservation.getsockname()[1]
    capture = CaptureServer(
        llm_url=f"http://127.0.0.1:{scripted.server_port}/v1",
        model="validation-oracle",
        port=port,
        capture_level="text",
        admin_key=uuid.uuid4().hex,
    )
    seams.SEAMS["oracle"] = seams.Seam(
        name="oracle",
        dialect="openai_chat",
        kwargs=lambda base, session, model: {
            "validation_base_url": base,
            "validation_session": session,
        },
        notes="Validation only: scripted capture call followed by oracle",
    )
    original = AgentFactory._AGENT_MAP[AgentName.ORACLE]
    AgentFactory._AGENT_MAP[AgentName.ORACLE] = (
        "agentescape_port.testing:CaptureOracleAgent"
    )
    results = []
    capture.start()
    try:
        for path in paths:
            result = await run_rollout(
                task_dir=path,
                harness="oracle",
                sandbox="docker",
                registry=capture.registry,
                intercept_url=f"http://127.0.0.1:{port}",
                model="openai/validation-oracle",
                trials_dir=root / "jobs/openenv-smoke",
                dataset=str(args.dataset),
                capture_level="text",
                purpose="eval",
            )
            results.append(result.model_dump(mode="json"))
    finally:
        capture.stop()
        scripted.shutdown()
        scripted.server_close()
        AgentFactory._AGENT_MAP[AgentName.ORACLE] = original
    args.report.write_text(
        json.dumps(
            {
                "scope": "Real OpenEnv task discovery, run_rollout and text capture using a scripted localhost endpoint, followed by public-action oracle. No paid model or token/logprob validation.",
                "results": results,
            },
            indent=2,
        )
    )
    for result in results:
        print(
            json.dumps(
                {
                    key: result.get(key)
                    for key in (
                        "task_name",
                        "reward",
                        "ok",
                        "error",
                        "n_turns",
                        "findings",
                    )
                }
            )
        )
    assert results and all(
        result["ok"] and result["reward"] == 1.0 and result["n_turns"] >= 1
        for result in results
    )


if __name__ == "__main__":
    root = Path(__file__).resolve().parents[1]
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--dataset", type=Path, default=root / "pilot-tasks")
    parser.add_argument("--task-ids", nargs="*")
    parser.add_argument(
        "--report", type=Path, default=root / "reports/openenv-smoke.json"
    )
    asyncio.run(main(parser.parse_args()))