pyddock 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyddock/__init__.py ADDED
@@ -0,0 +1,8 @@
1
+ """pyddock — policy-controlled Python execution via MCP."""
2
+
3
+ __version__ = "0.1.0"
4
+
5
+ # Filename used for compile() when executing agent snippets.
6
+ # Used by executor (compile source), runtime (stack inspection to identify
7
+ # agent code vs trusted library code), and tests.
8
+ SNIPPET_FILENAME = "<snippet>"
@@ -0,0 +1,88 @@
1
+ """Shared process utilities for pyddock executors.
2
+
3
+ Contains helpers used by both executor.py (run_python) and shell_executor.py (run_shell)
4
+ to avoid circular imports.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import os
10
+ import subprocess
11
+
12
+
13
+ def make_child_env() -> dict[str, str]:
14
+ """Build an environment dict for child processes.
15
+
16
+ Starts from os.environ, ensures essential Windows variables are present
17
+ (some launchers like uv run strip them), and removes pyddock internals
18
+ that shouldn't leak into child processes.
19
+ """
20
+ env = os.environ.copy()
21
+ env["PYTHONIOENCODING"] = "utf-8"
22
+ if os.name == "nt":
23
+ _temp_dir = os.environ.get("TEMP", os.environ.get("TMP", r"C:\Windows\Temp"))
24
+ _win_defaults = {
25
+ "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL",
26
+ "SystemRoot": r"C:\Windows",
27
+ "SYSTEMROOT": r"C:\Windows",
28
+ "TMP": _temp_dir,
29
+ "TEMP": _temp_dir,
30
+ "COMSPEC": r"C:\Windows\system32\cmd.exe",
31
+ "NUMBER_OF_PROCESSORS": str(os.cpu_count() or 1),
32
+ "OS": "Windows_NT",
33
+ }
34
+ for key, default in _win_defaults.items():
35
+ if key not in env:
36
+ env[key] = default
37
+ env.pop("VIRTUAL_ENV", None)
38
+ env.pop("VIRTUAL_ENV_PROMPT", None)
39
+ env.pop("PYTHONPATH", None)
40
+ return env
41
+
42
+
43
+ def get_startupinfo() -> subprocess.STARTUPINFO:
44
+ """Create STARTUPINFO that hides the process window (Windows only)."""
45
+ si = subprocess.STARTUPINFO()
46
+ si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
47
+ si.wShowWindow = 0 # SW_HIDE
48
+ return si
49
+
50
+
51
+ # 64 KB ≈ 16K tokens — enough for useful output without dominating agent context
52
+ MAX_OUTPUT_BYTES = 65_536
53
+
54
+
55
+ def truncate_output(text: str, label: str = "output", limit: int = MAX_OUTPUT_BYTES) -> str:
56
+ """Truncate text to *limit* characters, appending a notice if trimmed."""
57
+ if len(text) <= limit:
58
+ return text
59
+ return (
60
+ text[:limit]
61
+ + f"\n\n[truncated: {label} exceeded 64 KB. "
62
+ f"For large results, write to a file instead of printing.]"
63
+ )
64
+
65
+
66
+ def kill_and_drain(proc: subprocess.Popen) -> tuple[str, str]:
67
+ """Kill a timed-out process tree and return its buffered stdout/stderr (truncated).
68
+
69
+ Performs a forceful kill (taskkill on Windows, SIGKILL on Unix), then drains
70
+ whatever output was already buffered in the OS pipes before the process died.
71
+ """
72
+ if os.name == "nt":
73
+ subprocess.run(
74
+ ["taskkill", "/F", "/T", "/PID", str(proc.pid)],
75
+ capture_output=True,
76
+ )
77
+ else:
78
+ import signal
79
+ os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
80
+ # Drain any partial output buffered in the pipes
81
+ stdout_bytes, stderr_bytes = proc.communicate()
82
+ stdout = truncate_output(
83
+ stdout_bytes.decode("utf-8", errors="replace").replace("\r\n", "\n"), "output"
84
+ )
85
+ stderr = truncate_output(
86
+ stderr_bytes.decode("utf-8", errors="replace").replace("\r\n", "\n"), "stderr"
87
+ )
88
+ return stdout, stderr