|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Programmatic Tool Calling (PTC) Server for nsjail sandbox execution. |
| 3 | +
|
| 4 | +This script runs INSIDE the nsjail sandbox and provides a Python execution |
| 5 | +environment where code can call externally-defined tools. Tool calls are |
| 6 | +serialized as JSON over stdin/stdout, allowing the host process to fulfill |
| 7 | +them and send results back. |
| 8 | +
|
| 9 | +Protocol: |
| 10 | +1. Host sends initial request via stdin: |
| 11 | + {"code": "...", "tools": [{"name": "...", "description": "...", "parameters": {...}}]} |
| 12 | +
|
| 13 | +2. Code executes. When a tool stub is called, PTC server writes to stdout: |
| 14 | + {"type": "tool_calls", "calls": [{"id": "...", "name": "...", "input": {...}}]} |
| 15 | +
|
| 16 | +3. Host reads tool_calls, fulfills them, and writes results to stdin: |
| 17 | + {"type": "tool_results", "results": [{"call_id": "...", "result": ..., "is_error": false}]} |
| 18 | +
|
| 19 | +4. Code continues. On completion, PTC server writes: |
| 20 | + {"type": "completed", "stdout": "...", "stderr": "..."} |
| 21 | +
|
| 22 | +5. On error, PTC server writes: |
| 23 | + {"type": "error", "error": "..."} |
| 24 | +""" |
| 25 | + |
| 26 | +import asyncio |
| 27 | +import json |
| 28 | +import os |
| 29 | +import sys |
| 30 | +import traceback |
| 31 | +import uuid |
| 32 | +from io import StringIO |
| 33 | + |
| 34 | +DELIMITER = "\n---PTC_END---\n" |
| 35 | + |
| 36 | +# Keep references to the REAL stdin/stdout for protocol communication. |
| 37 | +# User code's print() will be redirected to a StringIO capture buffer. |
| 38 | +_real_stdin = sys.stdin |
| 39 | +_real_stdout = sys.stdout |
| 40 | +_real_stderr = sys.stderr |
| 41 | + |
| 42 | + |
| 43 | +def _write_message(msg: dict) -> None: |
| 44 | + """Write a JSON message to the host via the real stdout.""" |
| 45 | + data = json.dumps(msg) + DELIMITER |
| 46 | + _real_stdout.write(data) |
| 47 | + _real_stdout.flush() |
| 48 | + |
| 49 | + |
| 50 | +def _read_message() -> dict: |
| 51 | + """Read a JSON message from the host via the real stdin.""" |
| 52 | + buf = "" |
| 53 | + while True: |
| 54 | + line = _real_stdin.readline() |
| 55 | + if not line: |
| 56 | + raise EOFError("stdin closed") |
| 57 | + buf += line |
| 58 | + if DELIMITER in buf: |
| 59 | + json_part = buf.split(DELIMITER)[0] |
| 60 | + return json.loads(json_part) |
| 61 | + |
| 62 | + |
| 63 | +# Pending tool calls collected during async execution |
| 64 | +_pending_calls = [] |
| 65 | +_tool_results_map = {} # call_id -> result |
| 66 | + |
| 67 | + |
| 68 | +def _make_tool_stub(tool_name: str) -> callable: |
| 69 | + """Create an async function stub for a tool.""" |
| 70 | + |
| 71 | + async def tool_stub(**kwargs): |
| 72 | + call_id = uuid.uuid4().hex[:12] |
| 73 | + call_info = { |
| 74 | + "id": call_id, |
| 75 | + "name": tool_name, |
| 76 | + "input": kwargs, |
| 77 | + } |
| 78 | + _pending_calls.append(call_info) |
| 79 | + |
| 80 | + # Wait for result - the main loop will flush calls and read results |
| 81 | + while call_id not in _tool_results_map: |
| 82 | + await asyncio.sleep(0.01) |
| 83 | + |
| 84 | + result_info = _tool_results_map.pop(call_id) |
| 85 | + if result_info.get("is_error"): |
| 86 | + raise RuntimeError( |
| 87 | + result_info.get("error_message", "Tool call failed") |
| 88 | + ) |
| 89 | + return result_info.get("result") |
| 90 | + |
| 91 | + tool_stub.__name__ = tool_name |
| 92 | + tool_stub.__qualname__ = tool_name |
| 93 | + return tool_stub |
| 94 | + |
| 95 | + |
| 96 | +async def _execute_with_tools( |
| 97 | + code: str, tools: list, user_stdout: StringIO, user_stderr: StringIO |
| 98 | +) -> dict: |
| 99 | + """Execute code with tool stubs, capturing user output.""" |
| 100 | + global _pending_calls, _tool_results_map |
| 101 | + |
| 102 | + _pending_calls = [] |
| 103 | + _tool_results_map = {} |
| 104 | + |
| 105 | + # Build namespace with tool stubs |
| 106 | + namespace = {"__builtins__": __builtins__, "__name__": "__main__"} |
| 107 | + |
| 108 | + try: |
| 109 | + import json as _json |
| 110 | + |
| 111 | + namespace["json"] = _json |
| 112 | + except ImportError: |
| 113 | + pass |
| 114 | + |
| 115 | + for tool in tools: |
| 116 | + namespace[tool["name"]] = _make_tool_stub(tool["name"]) |
| 117 | + |
| 118 | + # Wrap user code in async function |
| 119 | + indented_code = "\n".join(" " + line for line in code.split("\n")) |
| 120 | + wrapped_code = f"async def __ptc_main__():\n{indented_code}\n" |
| 121 | + |
| 122 | + try: |
| 123 | + compiled = compile(wrapped_code, "<ptc_code>", "exec") |
| 124 | + exec(compiled, namespace) |
| 125 | + except SyntaxError as e: |
| 126 | + return {"type": "error", "error": f"SyntaxError: {e}"} |
| 127 | + |
| 128 | + main_func = namespace["__ptc_main__"] |
| 129 | + main_task = asyncio.ensure_future(main_func()) |
| 130 | + |
| 131 | + try: |
| 132 | + while not main_task.done(): |
| 133 | + # Let the task run briefly to accumulate batched calls |
| 134 | + await asyncio.sleep(0.05) |
| 135 | + |
| 136 | + if _pending_calls and not main_task.done(): |
| 137 | + calls_to_send = list(_pending_calls) |
| 138 | + _pending_calls.clear() |
| 139 | + |
| 140 | + _write_message({ |
| 141 | + "type": "tool_calls", |
| 142 | + "calls": calls_to_send, |
| 143 | + }) |
| 144 | + |
| 145 | + # Wait for results from host |
| 146 | + response = _read_message() |
| 147 | + |
| 148 | + if response.get("type") != "tool_results": |
| 149 | + return { |
| 150 | + "type": "error", |
| 151 | + "error": f"Expected tool_results, got " |
| 152 | + f"{response.get('type')}", |
| 153 | + } |
| 154 | + |
| 155 | + for result in response.get("results", []): |
| 156 | + _tool_results_map[result["call_id"]] = result |
| 157 | + |
| 158 | + # Task completed |
| 159 | + main_task.result() |
| 160 | + return {"type": "completed"} |
| 161 | + |
| 162 | + except Exception as e: |
| 163 | + tb = traceback.format_exc() |
| 164 | + return { |
| 165 | + "type": "error", |
| 166 | + "error": str(e), |
| 167 | + "stderr_extra": tb, |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | +def main(): |
| 172 | + """Main entry point for PTC server.""" |
| 173 | + try: |
| 174 | + os.chdir("/mnt/data") |
| 175 | + except OSError: |
| 176 | + pass |
| 177 | + |
| 178 | + # Read initial request |
| 179 | + try: |
| 180 | + request = _read_message() |
| 181 | + except Exception as e: |
| 182 | + _write_message({ |
| 183 | + "type": "error", |
| 184 | + "error": f"Failed to read initial request: {e}", |
| 185 | + }) |
| 186 | + return |
| 187 | + |
| 188 | + code = request.get("code", "") |
| 189 | + tools = request.get("tools", []) |
| 190 | + |
| 191 | + if not code: |
| 192 | + _write_message({"type": "error", "error": "No code provided"}) |
| 193 | + return |
| 194 | + |
| 195 | + # Redirect sys.stdout and sys.stderr so user print() calls |
| 196 | + # are captured, not mixed with our protocol messages. |
| 197 | + user_stdout = StringIO() |
| 198 | + user_stderr = StringIO() |
| 199 | + sys.stdout = user_stdout |
| 200 | + sys.stderr = user_stderr |
| 201 | + |
| 202 | + try: |
| 203 | + result = asyncio.run( |
| 204 | + _execute_with_tools(code, tools, user_stdout, user_stderr) |
| 205 | + ) |
| 206 | + except Exception as e: |
| 207 | + result = { |
| 208 | + "type": "error", |
| 209 | + "error": str(e), |
| 210 | + } |
| 211 | + |
| 212 | + # Restore real stdout for final message |
| 213 | + sys.stdout = _real_stdout |
| 214 | + sys.stderr = _real_stderr |
| 215 | + |
| 216 | + # Attach captured user output |
| 217 | + result["stdout"] = user_stdout.getvalue() |
| 218 | + stderr_val = user_stderr.getvalue() |
| 219 | + if result.get("stderr_extra"): |
| 220 | + stderr_val += result.pop("stderr_extra") |
| 221 | + result["stderr"] = stderr_val |
| 222 | + |
| 223 | + _write_message(result) |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == "__main__": |
| 227 | + main() |
0 commit comments