dsh-quay-bot 0.7.4 → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +105 -28
- package/bin/doctor.mjs +148 -0
- package/botscreen/requirements-offline.txt +11 -0
- package/client.js +1 -2
- package/cordis.patch.yml +1 -1
- package/host/api.js +0 -4
- package/host/remote.js +2 -140
- package/host/screen.js +85 -21
- package/market/index.json +41 -0
- package/package.json +32 -13
- package/vendor/wheels/any/annotated_doc-0.0.7-py3-none-any.whl +0 -0
- package/vendor/wheels/any/annotated_types-0.8.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/anyio-4.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/certifi-2026.11.20-py3-none-any.whl +0 -0
- package/vendor/wheels/any/click-8.5.1-py3-none-any.whl +0 -0
- package/vendor/wheels/any/fastapi-0.165.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/h11-0.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/httpcore-1.0.9-py3-none-any.whl +0 -0
- package/vendor/wheels/any/httpx-0.29.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/idna-3.20-py3-none-any.whl +0 -0
- package/vendor/wheels/any/mss-10.2.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/opentelemetry_api-1.45.2-py3-none-any.whl +0 -0
- package/vendor/wheels/any/pydantic-2.14.4-py3-none-any.whl +0 -0
- package/vendor/wheels/any/python_multipart-0.0.34-py3-none-any.whl +0 -0
- package/vendor/wheels/any/python_xlib-0.33-py2.py3-none-any.whl +0 -0
- package/vendor/wheels/any/six-1.17.0-py2.py3-none-any.whl +0 -0
- package/vendor/wheels/any/starlette-1.10.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/typing_extensions-4.16.0-py3-none-any.whl +0 -0
- package/vendor/wheels/any/typing_inspection-0.4.5-py3-none-any.whl +0 -0
- package/vendor/wheels/any/uvicorn-0.54.0-py3-none-any.whl +0 -0
- package/vendor/wheels/linux-aarch64/pillow-12.2.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
- package/vendor/wheels/linux-aarch64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
- package/vendor/wheels/linux-aarch64/websockets-17.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl +0 -0
- package/vendor/wheels/linux-x86_64/pillow-12.2.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
- package/vendor/wheels/linux-x86_64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
- package/vendor/wheels/linux-x86_64/websockets-17.4-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl +0 -0
- package/botscreen/botscreen/__init__.py +0 -2
- package/botscreen/botscreen/__main__.py +0 -5
- package/botscreen/botscreen/artifacts.py +0 -388
- package/botscreen/botscreen/audit.py +0 -94
- package/botscreen/botscreen/cgroup.py +0 -293
- package/botscreen/botscreen/config.py +0 -320
- package/botscreen/botscreen/desktop/assets/chrome.png +0 -0
- package/botscreen/botscreen/desktop/assets/codex.png +0 -0
- package/botscreen/botscreen/desktop/assets/drive.png +0 -0
- package/botscreen/botscreen/desktop/assets/folder.png +0 -0
- package/botscreen/botscreen/desktop/assets/search.png +0 -0
- package/botscreen/botscreen/desktop/assets/terminal.png +0 -0
- package/botscreen/botscreen/desktop/assets/trash.png +0 -0
- package/botscreen/botscreen/desktop/assets/wallpaper.png +0 -0
- package/botscreen/botscreen/desktop.py +0 -244
- package/botscreen/botscreen/driver.py +0 -832
- package/botscreen/botscreen/gateway.py +0 -2089
- package/botscreen/botscreen/iconlayer.py +0 -841
- package/botscreen/botscreen/mcp_server.py +0 -435
- package/botscreen/botscreen/resources.py +0 -494
- package/botscreen/botscreen/screen.py +0 -1282
- package/botscreen/botscreen/session.py +0 -706
- package/botscreen/botscreen/state.py +0 -330
- package/botscreen/botscreen/streamer.py +0 -388
- package/remote/bin/dsh-remote.mjs +0 -418
- package/remote/package.json +0 -27
- package/remote/src/config.mjs +0 -183
- package/remote/src/crypto.mjs +0 -220
- package/remote/src/keys.mjs +0 -226
- package/remote/src/proxy.mjs +0 -1028
- package/remote/src/sessions.mjs +0 -163
- package/remote/src/shim.js +0 -838
- package/remote/src/supervisor.mjs +0 -207
- package/remote/src/tunnel.mjs +0 -489
- package/remote/src/upstream.mjs +0 -372
- package/remote/src/util.mjs +0 -139
- package/remote/src/ws.mjs +0 -313
- package/requirements.txt +0 -8
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
"""cgroup v2 管理:进程归组、freeze/unfreeze、内存读数。
|
|
2
|
-
|
|
3
|
-
所有写操作都通过 `sudo -n tee` 落盘,网关本身可以以普通用户运行;
|
|
4
|
-
freezer 是内核级暂停,进程树 CPU 归零但窗口/表单/标签页完整保留(方案四.2)。
|
|
5
|
-
"""
|
|
6
|
-
from __future__ import annotations
|
|
7
|
-
|
|
8
|
-
import os
|
|
9
|
-
import shutil
|
|
10
|
-
import subprocess
|
|
11
|
-
import time
|
|
12
|
-
from typing import Dict, Iterable, List
|
|
13
|
-
|
|
14
|
-
from .config import CGROUP_ROOT
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class CgroupError(RuntimeError):
|
|
18
|
-
pass
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def _run(cmd: List[str], stdin_text: str | None = None) -> str:
|
|
22
|
-
proc = subprocess.run(
|
|
23
|
-
cmd,
|
|
24
|
-
input=stdin_text,
|
|
25
|
-
text=True,
|
|
26
|
-
capture_output=True,
|
|
27
|
-
check=False,
|
|
28
|
-
)
|
|
29
|
-
if proc.returncode != 0:
|
|
30
|
-
raise CgroupError(
|
|
31
|
-
f"{' '.join(cmd)} failed rc={proc.returncode}: "
|
|
32
|
-
f"{(proc.stderr or proc.stdout).strip()[:300]}"
|
|
33
|
-
)
|
|
34
|
-
return proc.stdout
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class CgroupManager:
|
|
38
|
-
def __init__(self, root: str = CGROUP_ROOT) -> None:
|
|
39
|
-
self.root = root
|
|
40
|
-
self.available = True
|
|
41
|
-
self._reclaim_procs: Dict[str, subprocess.Popen] = {}
|
|
42
|
-
|
|
43
|
-
# ---- 基础写入 ---------------------------------------------------------
|
|
44
|
-
def _write(self, path: str, value: str) -> None:
|
|
45
|
-
try:
|
|
46
|
-
with open(path, "w") as fh:
|
|
47
|
-
fh.write(value)
|
|
48
|
-
return
|
|
49
|
-
except PermissionError:
|
|
50
|
-
pass
|
|
51
|
-
except FileNotFoundError:
|
|
52
|
-
raise CgroupError(f"missing cgroup file: {path}")
|
|
53
|
-
_run(["sudo", "-n", "tee", path], value)
|
|
54
|
-
|
|
55
|
-
def _mkdir(self, path: str) -> None:
|
|
56
|
-
try:
|
|
57
|
-
os.makedirs(path, exist_ok=True)
|
|
58
|
-
return
|
|
59
|
-
except PermissionError:
|
|
60
|
-
pass
|
|
61
|
-
_run(["sudo", "-n", "mkdir", "-p", path])
|
|
62
|
-
|
|
63
|
-
@staticmethod
|
|
64
|
-
def _read(path: str, default: str = "") -> str:
|
|
65
|
-
try:
|
|
66
|
-
with open(path) as fh:
|
|
67
|
-
return fh.read().strip()
|
|
68
|
-
except OSError:
|
|
69
|
-
return default
|
|
70
|
-
|
|
71
|
-
# ---- 目录结构 ---------------------------------------------------------
|
|
72
|
-
def ensure_root(self) -> None:
|
|
73
|
-
"""创建 /sys/fs/cgroup/botscreen 并放开所需控制器;无权限时尝试用户委托 slice 或降级关闭。"""
|
|
74
|
-
try:
|
|
75
|
-
self._mkdir(self.root)
|
|
76
|
-
ctrls = self._read(os.path.join(self.root, "cgroup.controllers")).split()
|
|
77
|
-
wanted = [c for c in ("memory", "pids", "cpu", "io") if c in ctrls]
|
|
78
|
-
if wanted:
|
|
79
|
-
self._write(
|
|
80
|
-
os.path.join(self.root, "cgroup.subtree_control"),
|
|
81
|
-
" ".join(f"+{c}" for c in wanted),
|
|
82
|
-
)
|
|
83
|
-
self.available = True
|
|
84
|
-
return
|
|
85
|
-
except (OSError, CgroupError):
|
|
86
|
-
pass
|
|
87
|
-
|
|
88
|
-
# 回退到当前用户 systemd 委托的 cgroup 子目录
|
|
89
|
-
try:
|
|
90
|
-
with open("/proc/self/cgroup", encoding="utf-8") as fh:
|
|
91
|
-
for line in fh:
|
|
92
|
-
parts = line.strip().split(":", 2)
|
|
93
|
-
if len(parts) == 3 and parts[2].startswith("/"):
|
|
94
|
-
rel = parts[2].lstrip("/")
|
|
95
|
-
parent = os.path.join("/sys/fs/cgroup", os.path.dirname(rel), "botscreen")
|
|
96
|
-
os.makedirs(parent, exist_ok=True)
|
|
97
|
-
self.root = parent
|
|
98
|
-
self.available = True
|
|
99
|
-
return
|
|
100
|
-
except OSError:
|
|
101
|
-
pass
|
|
102
|
-
self.available = False
|
|
103
|
-
|
|
104
|
-
def bot_path(self, bot_id: str) -> str:
|
|
105
|
-
return os.path.join(self.root, bot_id)
|
|
106
|
-
|
|
107
|
-
def ensure_bot(self, bot_id: str) -> str:
|
|
108
|
-
path = self.bot_path(bot_id)
|
|
109
|
-
if not self.available:
|
|
110
|
-
return path
|
|
111
|
-
try:
|
|
112
|
-
self._mkdir(path)
|
|
113
|
-
except (OSError, CgroupError):
|
|
114
|
-
self.available = False
|
|
115
|
-
return path
|
|
116
|
-
|
|
117
|
-
def destroy(self, bot_id: str) -> None:
|
|
118
|
-
"""解冻并尝试移除子组。
|
|
119
|
-
|
|
120
|
-
注意:这里**不**杀 cgroup 内的进程。cgroup 成员是全局可读的,
|
|
121
|
-
若旧网关实例在退出清理时盲杀成员,会误杀新实例刚拉起的同名屏。
|
|
122
|
-
进程终止一律由 ScreenWorker 针对自己记录的 PID 完成。
|
|
123
|
-
"""
|
|
124
|
-
path = self.bot_path(bot_id)
|
|
125
|
-
if not os.path.isdir(path):
|
|
126
|
-
return
|
|
127
|
-
try:
|
|
128
|
-
self.freeze(bot_id, False)
|
|
129
|
-
except CgroupError:
|
|
130
|
-
pass
|
|
131
|
-
if self.pids(bot_id):
|
|
132
|
-
# 仍有成员 → 保留子组,下次启动复用,避免误伤
|
|
133
|
-
return
|
|
134
|
-
try:
|
|
135
|
-
os.rmdir(path)
|
|
136
|
-
except OSError:
|
|
137
|
-
try:
|
|
138
|
-
_run(["sudo", "-n", "rmdir", path])
|
|
139
|
-
except CgroupError:
|
|
140
|
-
pass
|
|
141
|
-
|
|
142
|
-
# ---- 进程归组 ---------------------------------------------------------
|
|
143
|
-
def add_pid(self, bot_id: str, pid: int) -> None:
|
|
144
|
-
path = self.ensure_bot(bot_id)
|
|
145
|
-
self._write(os.path.join(path, "cgroup.procs"), str(pid))
|
|
146
|
-
|
|
147
|
-
def add_pids(self, bot_id: str, pids: Iterable[int]) -> int:
|
|
148
|
-
moved = 0
|
|
149
|
-
for pid in pids:
|
|
150
|
-
try:
|
|
151
|
-
self.add_pid(bot_id, pid)
|
|
152
|
-
moved += 1
|
|
153
|
-
except (CgroupError, ProcessLookupError):
|
|
154
|
-
continue
|
|
155
|
-
return moved
|
|
156
|
-
|
|
157
|
-
def pids(self, bot_id: str) -> List[int]:
|
|
158
|
-
raw = self._read(os.path.join(self.bot_path(bot_id), "cgroup.procs"))
|
|
159
|
-
return [int(x) for x in raw.split() if x.isdigit()]
|
|
160
|
-
|
|
161
|
-
def descendants(self, root_pid: int) -> List[int]:
|
|
162
|
-
"""按 /proc 扫描某进程的全部后代(用于把 fork 出去的窗口管理器归组)。"""
|
|
163
|
-
children: dict[int, List[int]] = {}
|
|
164
|
-
for entry in os.listdir("/proc"):
|
|
165
|
-
if not entry.isdigit():
|
|
166
|
-
continue
|
|
167
|
-
pid = int(entry)
|
|
168
|
-
try:
|
|
169
|
-
with open(f"/proc/{pid}/stat") as fh:
|
|
170
|
-
fields = fh.read().split()
|
|
171
|
-
ppid = int(fields[3])
|
|
172
|
-
except (OSError, ValueError, IndexError):
|
|
173
|
-
continue
|
|
174
|
-
children.setdefault(ppid, []).append(pid)
|
|
175
|
-
out: List[int] = []
|
|
176
|
-
stack = list(children.get(root_pid, []))
|
|
177
|
-
while stack:
|
|
178
|
-
pid = stack.pop()
|
|
179
|
-
out.append(pid)
|
|
180
|
-
stack.extend(children.get(pid, []))
|
|
181
|
-
return out
|
|
182
|
-
|
|
183
|
-
def reap(self, bot_id: str, root_pid: int) -> int:
|
|
184
|
-
"""把 root_pid 的整棵进程树塞进该 bot 的 cgroup。"""
|
|
185
|
-
if not self.available:
|
|
186
|
-
return 0
|
|
187
|
-
pids = [root_pid] + self.descendants(root_pid)
|
|
188
|
-
return self.add_pids(bot_id, pids)
|
|
189
|
-
|
|
190
|
-
# ---- 冻结 / 唤醒 ------------------------------------------------------
|
|
191
|
-
def _prune_reclaims(self) -> None:
|
|
192
|
-
for bid, proc in list(self._reclaim_procs.items()):
|
|
193
|
-
if proc.poll() is not None:
|
|
194
|
-
self._reclaim_procs.pop(bid, None)
|
|
195
|
-
|
|
196
|
-
def _cancel_reclaim(self, bot_id: str) -> None:
|
|
197
|
-
proc = self._reclaim_procs.pop(bot_id, None)
|
|
198
|
-
if proc is None or proc.poll() is not None:
|
|
199
|
-
return
|
|
200
|
-
try:
|
|
201
|
-
proc.terminate()
|
|
202
|
-
except OSError:
|
|
203
|
-
pass
|
|
204
|
-
|
|
205
|
-
def freeze(self, bot_id: str, frozen: bool = True) -> None:
|
|
206
|
-
if not self.available:
|
|
207
|
-
return
|
|
208
|
-
if not frozen:
|
|
209
|
-
self._cancel_reclaim(bot_id)
|
|
210
|
-
path = os.path.join(self.ensure_bot(bot_id), "cgroup.freeze")
|
|
211
|
-
self._write(path, "1" if frozen else "0")
|
|
212
|
-
# 必须等 freeze 真正生效:内核写 cgroup.freeze=1 是异步的
|
|
213
|
-
deadline = time.time() + 2.0
|
|
214
|
-
while time.time() < deadline:
|
|
215
|
-
if self.is_frozen(bot_id) == frozen:
|
|
216
|
-
return
|
|
217
|
-
time.sleep(0.01)
|
|
218
|
-
raise CgroupError(f"freeze state did not settle for {bot_id}")
|
|
219
|
-
|
|
220
|
-
def is_frozen(self, bot_id: str) -> bool:
|
|
221
|
-
return self._read(os.path.join(self.bot_path(bot_id), "cgroup.freeze")) == "1"
|
|
222
|
-
|
|
223
|
-
def frozen_events(self, bot_id: str) -> int:
|
|
224
|
-
raw = self._read(os.path.join(self.bot_path(bot_id), "cgroup.events"))
|
|
225
|
-
for line in raw.splitlines():
|
|
226
|
-
if line.startswith("frozen"):
|
|
227
|
-
return int(line.split()[1])
|
|
228
|
-
return -1
|
|
229
|
-
|
|
230
|
-
def reclaim(self, bot_id: str, amount: int) -> int:
|
|
231
|
-
"""同步回收(会阻塞到内核回收够为止),仅用于手动调用/测量。"""
|
|
232
|
-
if amount <= 0:
|
|
233
|
-
return 0
|
|
234
|
-
path = os.path.join(self.bot_path(bot_id), "memory.reclaim")
|
|
235
|
-
if not os.path.exists(path):
|
|
236
|
-
return 0
|
|
237
|
-
before = self.memory_current(bot_id)
|
|
238
|
-
try:
|
|
239
|
-
self._write(path, str(amount))
|
|
240
|
-
except CgroupError:
|
|
241
|
-
return 0
|
|
242
|
-
return max(0, before - self.memory_current(bot_id))
|
|
243
|
-
|
|
244
|
-
def reclaim_async(self, bot_id: str, amount: int) -> bool:
|
|
245
|
-
"""异步触发回收:内核在可回收页不足时会**同步阻塞**写入方,
|
|
246
|
-
因此做最小驻留阈值过滤、去重防并发、限流与超时收割,避免永久卡在 D 状态吃满 CPU。"""
|
|
247
|
-
self._prune_reclaims()
|
|
248
|
-
if amount < 8 * 1024 * 1024:
|
|
249
|
-
return False
|
|
250
|
-
current = self.memory_current(bot_id)
|
|
251
|
-
if current < 32 * 1024 * 1024:
|
|
252
|
-
return False
|
|
253
|
-
safe_amount = min(amount, current // 2)
|
|
254
|
-
if safe_amount < 8 * 1024 * 1024:
|
|
255
|
-
return False
|
|
256
|
-
existing = self._reclaim_procs.get(bot_id)
|
|
257
|
-
if existing is not None and existing.poll() is None:
|
|
258
|
-
return False
|
|
259
|
-
path = os.path.join(self.bot_path(bot_id), "memory.reclaim")
|
|
260
|
-
if not os.path.exists(path):
|
|
261
|
-
return False
|
|
262
|
-
timeout_bin = shutil.which("timeout")
|
|
263
|
-
cmd = ["sudo", "-n", "tee", path]
|
|
264
|
-
if timeout_bin:
|
|
265
|
-
cmd = [timeout_bin, "-k", "1", "8", *cmd]
|
|
266
|
-
try:
|
|
267
|
-
proc = subprocess.Popen(
|
|
268
|
-
cmd,
|
|
269
|
-
stdin=subprocess.PIPE,
|
|
270
|
-
stdout=subprocess.DEVNULL,
|
|
271
|
-
stderr=subprocess.DEVNULL,
|
|
272
|
-
text=True,
|
|
273
|
-
start_new_session=True,
|
|
274
|
-
)
|
|
275
|
-
if proc.stdin is not None:
|
|
276
|
-
proc.stdin.write(str(safe_amount))
|
|
277
|
-
proc.stdin.close()
|
|
278
|
-
self._reclaim_procs[bot_id] = proc
|
|
279
|
-
return True
|
|
280
|
-
except (OSError, subprocess.SubprocessError):
|
|
281
|
-
return False
|
|
282
|
-
|
|
283
|
-
# ---- 观测 -------------------------------------------------------------
|
|
284
|
-
def memory_current(self, bot_id: str) -> int:
|
|
285
|
-
raw = self._read(os.path.join(self.bot_path(bot_id), "memory.current"), "0")
|
|
286
|
-
return int(raw or 0)
|
|
287
|
-
|
|
288
|
-
def cpu_usage_usec(self, bot_id: str) -> int:
|
|
289
|
-
raw = self._read(os.path.join(self.bot_path(bot_id), "cpu.stat"))
|
|
290
|
-
for line in raw.splitlines():
|
|
291
|
-
if line.startswith("usage_usec"):
|
|
292
|
-
return int(line.split()[1])
|
|
293
|
-
return 0
|
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
"""BotScreen 全局配置:路径、编号映射、动态槽位分配、环境常量。"""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import fcntl
|
|
5
|
-
import json
|
|
6
|
-
import os
|
|
7
|
-
import shutil
|
|
8
|
-
import tempfile
|
|
9
|
-
from dataclasses import dataclass
|
|
10
|
-
from typing import Dict, Optional
|
|
11
|
-
|
|
12
|
-
# ---- 底座路径 ---------------------------------------------------------------
|
|
13
|
-
def _default_data_root() -> str:
|
|
14
|
-
explicit = os.environ.get("BOTSCREEN_DATA_ROOT", "").strip()
|
|
15
|
-
if explicit:
|
|
16
|
-
return explicit
|
|
17
|
-
legacy = "/data/botscreen"
|
|
18
|
-
if os.path.isdir(legacy) and os.access(legacy, os.W_OK):
|
|
19
|
-
return legacy
|
|
20
|
-
return os.path.join(os.path.expanduser("~"), ".dsh", "botmode", "screens")
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
DATA_ROOT = _default_data_root()
|
|
24
|
-
CGROUP_ROOT = os.environ.get("BOTSCREEN_CGROUP_ROOT", "/sys/fs/cgroup/botscreen")
|
|
25
|
-
KASMVNC_WWW = os.environ.get("BOTSCREEN_KASMVNC_WWW", "/usr/share/kasmvnc/www")
|
|
26
|
-
#: 所有虚拟屏共用一份 Xauthority 文件(按 DISPLAY 分条目),
|
|
27
|
-
#: 这样网关单进程内的 mss 可以跨屏抓图而无需切换环境变量。
|
|
28
|
-
SHARED_XAUTHORITY = os.environ.get(
|
|
29
|
-
"BOTSCREEN_XAUTHORITY", os.path.join(DATA_ROOT, "Xauthority.shared")
|
|
30
|
-
)
|
|
31
|
-
WEB_ROOT = os.environ.get(
|
|
32
|
-
"BOTSCREEN_WEB_ROOT",
|
|
33
|
-
os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "web"),
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
# ---- 编号映射(方案二.1) ----------------------------------------------------
|
|
37
|
-
DISPLAY_BASE = int(os.environ.get("BOTSCREEN_DISPLAY_BASE", "100"))
|
|
38
|
-
WS_PORT_BASE = int(os.environ.get("BOTSCREEN_WS_PORT_BASE", "6100"))
|
|
39
|
-
|
|
40
|
-
#: 网关协议代数(DSH Host 用于校验网关接口兼容性并自动替换旧进程)
|
|
41
|
-
GATEWAY_API_VERSION = 4
|
|
42
|
-
#: 统一虚拟桌面规格代数(用于检测并淘汰旧版/错位桌面进程)
|
|
43
|
-
DESKTOP_SPEC_VERSION = 4
|
|
44
|
-
#: 顶部统一导航栏固定高度(px),Openbox margins 与 iconlayer 共享此常量
|
|
45
|
-
TOPBAR_HEIGHT = 34
|
|
46
|
-
#: 底部浮动 Dock 栏预留高度(px),保证 Dock 始终完整显示在屏幕底部不被窗口遮挡
|
|
47
|
-
DOCK_MARGIN_BOTTOM = 76
|
|
48
|
-
|
|
49
|
-
# ---- 虚拟屏固定几何(锁死最小够用分辨率,兼顾视觉识别与低带宽/低内存) ----------
|
|
50
|
-
SCREEN_WIDTH = int(os.environ.get("BOTSCREEN_WIDTH", "1280"))
|
|
51
|
-
SCREEN_HEIGHT = int(os.environ.get("BOTSCREEN_HEIGHT", "720"))
|
|
52
|
-
#: 色深:默认 24 位(亦支持 16 位进一步压缩帧缓冲带宽)。
|
|
53
|
-
SCREEN_DEPTH = int(os.environ.get("BOTSCREEN_DEPTH", "24"))
|
|
54
|
-
FRAME_RATE = int(os.environ.get("BOTSCREEN_FPS", "10"))
|
|
55
|
-
#: 按需增量推流:64×64 脏矩形网格 + 100ms 采样 + 断开 5 秒自动销毁抓屏会话
|
|
56
|
-
TILE_SIZE = int(os.environ.get("BOTSCREEN_TILE_SIZE", "64"))
|
|
57
|
-
STREAM_INTERVAL_MS = int(os.environ.get("BOTSCREEN_STREAM_MS", "100"))
|
|
58
|
-
CAPTURE_IDLE_HIBERNATE = float(os.environ.get("BOTSCREEN_CAPTURE_HIBERNATE", "5.0"))
|
|
59
|
-
|
|
60
|
-
# ---- 生命周期参数 ------------------------------------------------------------
|
|
61
|
-
#: 默认闲置策略:stop(自动保存桌面会话并彻底关闭进程,释放 100% 内存与 CPU);可选 freeze
|
|
62
|
-
IDLE_MODE = os.environ.get("BOTSCREEN_IDLE_MODE", "stop").strip().lower()
|
|
63
|
-
IDLE_STOP_SECONDS = float(os.environ.get("BOTSCREEN_IDLE_STOP", "120"))
|
|
64
|
-
IDLE_FREEZE_SECONDS = float(os.environ.get("BOTSCREEN_IDLE_FREEZE", str(int(IDLE_STOP_SECONDS))))
|
|
65
|
-
LONG_FREEZE_SECONDS = float(os.environ.get("BOTSCREEN_LONG_FREEZE", "300"))
|
|
66
|
-
WATCHDOG_INTERVAL = float(os.environ.get("BOTSCREEN_WATCHDOG_INTERVAL", "10"))
|
|
67
|
-
#: 冻结后主动向 swap/zram 换出的内存比例(0 = 关闭)。
|
|
68
|
-
#: cgroup.freeze 只暂停进程,不会自动换出匿名页;需要 memory.reclaim 触发。
|
|
69
|
-
RECLAIM_PCT = int(os.environ.get("BOTSCREEN_RECLAIM_PCT", "60"))
|
|
70
|
-
#: 单次回收的内存上限(MB)。没有上限时 200MB+ 的屏会在内核里等很久。
|
|
71
|
-
RECLAIM_MAX_MB = int(os.environ.get("BOTSCREEN_RECLAIM_MAX_MB", "96"))
|
|
72
|
-
|
|
73
|
-
# ---- 启动门禁 ---------------------------------------------------------------
|
|
74
|
-
#: 拉起一张屏前要求的最小可用内存(MB)。0 = 关闭门禁。
|
|
75
|
-
MIN_FREE_MB = int(os.environ.get("BOTSCREEN_MIN_FREE_MB", "1024"))
|
|
76
|
-
#: Xvfb 二进制(默认 PATH 里的;源码编译时用 BOTSCREEN_XVFB_BIN 指定绝对路径)
|
|
77
|
-
XVFB_BIN = os.environ.get("BOTSCREEN_XVFB_BIN", "Xvfb")
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
def _detect_x_backend() -> str:
|
|
81
|
-
raw = os.environ.get("BOTSCREEN_X_BACKEND", "").strip().lower()
|
|
82
|
-
if raw in ("xvfb", "kasmvnc"):
|
|
83
|
-
return raw
|
|
84
|
-
if shutil.which(XVFB_BIN) or os.path.isfile(XVFB_BIN):
|
|
85
|
-
return "xvfb"
|
|
86
|
-
if shutil.which("Xvnc"):
|
|
87
|
-
return "kasmvnc"
|
|
88
|
-
return "xvfb"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
#: X server 后端:默认极致轻量纯 Xvfb(无常驻 VNC 编码器);可选 kasmvnc
|
|
92
|
-
X_BACKEND = _detect_x_backend()
|
|
93
|
-
#: noVNC 静态前端目录(仅可选外部 VNC 模式使用)
|
|
94
|
-
VNC_WEB_DIR = os.environ.get("BOTSCREEN_VNC_WEB", "/usr/share/novnc")
|
|
95
|
-
#: x11vnc 按需 RFB 端口基址(5950 + 序号,仅按需拉起 -once 时使用)
|
|
96
|
-
RFB_PORT_BASE = int(os.environ.get("BOTSCREEN_RFB_PORT_BASE", "5950"))
|
|
97
|
-
|
|
98
|
-
#: 窗口管理器:统一轻量 openbox(带完整标题栏与最小化/最大化/关闭按钮 + 顶部 34px 导航栏留白)
|
|
99
|
-
WINDOW_MANAGER = os.environ.get("BOTSCREEN_WM", "openbox").strip().lower()
|
|
100
|
-
#: 默认开启统一轻量纯 Python 桌面壳(壁纸 + 桌面图标 + 顶部导航/任务栏,零 tint2/xfdesktop 错位)
|
|
101
|
-
DESKTOP_SHELL = os.environ.get("BOTSCREEN_DESKTOP", "1") == "1"
|
|
102
|
-
|
|
103
|
-
#: 人类持有时是否连 Agent 截图一并拒绝(Hermes 的 fail-closed 语义:人可能在输密码)。
|
|
104
|
-
SCREENSHOT_FAIL_CLOSED = os.environ.get("BOTSCREEN_SCREENSHOT_FAIL_CLOSED", "1") == "1"
|
|
105
|
-
BOOT_TIMEOUT = float(os.environ.get("BOTSCREEN_BOOT_TIMEOUT", "15"))
|
|
106
|
-
|
|
107
|
-
# ---- 网关自身 ---------------------------------------------------------------
|
|
108
|
-
GATEWAY_HOST = os.environ.get("BOTSCREEN_HOST", "127.0.0.1")
|
|
109
|
-
GATEWAY_PORT = int(os.environ.get("BOTSCREEN_PORT", "6200"))
|
|
110
|
-
|
|
111
|
-
# ---- 系统信息采集与网关上报配置 ----------------------------------------------
|
|
112
|
-
SYSTEM_CONFIG_FILE = os.environ.get(
|
|
113
|
-
"BOTSCREEN_SYSTEM_CONFIG", os.path.join(DATA_ROOT, "system_config.json")
|
|
114
|
-
)
|
|
115
|
-
DEFAULT_DISK_PATHS = [
|
|
116
|
-
p.strip()
|
|
117
|
-
for p in os.environ.get("BOTSCREEN_DISK_PATHS", "/").split(",")
|
|
118
|
-
if p.strip()
|
|
119
|
-
] or ["/"]
|
|
120
|
-
SYSTEM_STREAM_INTERVAL = float(os.environ.get("BOTSCREEN_SYSTEM_STREAM_INTERVAL", "2.0"))
|
|
121
|
-
SYSTEM_PUSH_URL = os.environ.get("BOTSCREEN_SYSTEM_PUSH_URL", "").strip()
|
|
122
|
-
SYSTEM_PUSH_INTERVAL = int(os.environ.get("BOTSCREEN_SYSTEM_PUSH_INTERVAL", "0"))
|
|
123
|
-
SYSTEM_PUSH_TOKEN = os.environ.get("BOTSCREEN_SYSTEM_PUSH_TOKEN", "").strip()
|
|
124
|
-
|
|
125
|
-
# ---- 审计 ------------------------------------------------------------------
|
|
126
|
-
AUDIT_ENABLED = os.environ.get("BOTSCREEN_AUDIT", "1") == "1"
|
|
127
|
-
THUMB_WIDTH = 320
|
|
128
|
-
|
|
129
|
-
# ---- Agent 回复产物传输与下载限制 -------------------------------------------
|
|
130
|
-
ARTIFACT_MAX_BYTES = int(os.environ.get("BOTSCREEN_ARTIFACT_MAX_BYTES", str(64 * 1024 * 1024)))
|
|
131
|
-
ARTIFACT_BUNDLE_MAX_BYTES = int(
|
|
132
|
-
os.environ.get("BOTSCREEN_ARTIFACT_BUNDLE_MAX_BYTES", str(128 * 1024 * 1024))
|
|
133
|
-
)
|
|
134
|
-
|
|
135
|
-
# 支持 BOTSCREEN_1..n 形式批量登记;默认 bot_01..bot_04
|
|
136
|
-
DEFAULT_BOTS = os.environ.get(
|
|
137
|
-
"BOTSCREEN_BOTS", "bot_01,bot_02,bot_03,bot_04"
|
|
138
|
-
).split(",")
|
|
139
|
-
|
|
140
|
-
SLOTS_FILE = os.environ.get("BOTSCREEN_SLOTS_FILE", os.path.join(DATA_ROOT, "slots.json"))
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
def _legacy_numeric_slot(bot_id: str) -> Optional[int]:
|
|
144
|
-
"""仅对纯数字后缀形式(如 bot_01..bot_999)保留原序号,避免影响已有屏。"""
|
|
145
|
-
prefix, _, suffix = bot_id.rpartition("_")
|
|
146
|
-
if prefix == "bot" and suffix.isdigit():
|
|
147
|
-
val = int(suffix)
|
|
148
|
-
if 1 <= val <= 999:
|
|
149
|
-
return val
|
|
150
|
-
return None
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
class SlotAllocator:
|
|
154
|
-
"""持久化 bot_id -> 正整数槽位映射,确保任意 UUID Bot ID 不撞 DISPLAY/端口。"""
|
|
155
|
-
|
|
156
|
-
def __init__(self, path: str = SLOTS_FILE) -> None:
|
|
157
|
-
self.path = path
|
|
158
|
-
self._slots: Dict[str, int] = {}
|
|
159
|
-
self._load()
|
|
160
|
-
|
|
161
|
-
def _load(self) -> None:
|
|
162
|
-
try:
|
|
163
|
-
with open(self.path, encoding="utf-8") as fh:
|
|
164
|
-
raw = json.load(fh)
|
|
165
|
-
if isinstance(raw, dict):
|
|
166
|
-
self._slots = {
|
|
167
|
-
str(k): int(v)
|
|
168
|
-
for k, v in raw.items()
|
|
169
|
-
if isinstance(v, int) and 1 <= v <= 999
|
|
170
|
-
}
|
|
171
|
-
except (OSError, json.JSONDecodeError, ValueError):
|
|
172
|
-
self._slots = {}
|
|
173
|
-
|
|
174
|
-
def _save(self) -> None:
|
|
175
|
-
directory = os.path.dirname(self.path)
|
|
176
|
-
os.makedirs(directory, exist_ok=True)
|
|
177
|
-
lock_path = self.path + ".lock"
|
|
178
|
-
with open(lock_path, "a+", encoding="utf-8") as lock_fh:
|
|
179
|
-
try:
|
|
180
|
-
fcntl.flock(lock_fh.fileno(), fcntl.LOCK_EX)
|
|
181
|
-
except OSError:
|
|
182
|
-
pass
|
|
183
|
-
fd, tmp = tempfile.mkstemp(dir=directory, prefix=".slots-")
|
|
184
|
-
try:
|
|
185
|
-
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
|
186
|
-
json.dump(self._slots, fh, ensure_ascii=False, indent=2)
|
|
187
|
-
fh.flush()
|
|
188
|
-
os.fsync(fh.fileno())
|
|
189
|
-
os.replace(tmp, self.path)
|
|
190
|
-
except BaseException:
|
|
191
|
-
try:
|
|
192
|
-
os.unlink(tmp)
|
|
193
|
-
except OSError:
|
|
194
|
-
pass
|
|
195
|
-
raise
|
|
196
|
-
finally:
|
|
197
|
-
try:
|
|
198
|
-
fcntl.flock(lock_fh.fileno(), fcntl.LOCK_UN)
|
|
199
|
-
except OSError:
|
|
200
|
-
pass
|
|
201
|
-
|
|
202
|
-
def all_slots(self) -> Dict[str, int]:
|
|
203
|
-
return dict(self._slots)
|
|
204
|
-
|
|
205
|
-
def allocate(self, bot_id: str) -> int:
|
|
206
|
-
if bot_id in self._slots:
|
|
207
|
-
return self._slots[bot_id]
|
|
208
|
-
used = set(self._slots.values())
|
|
209
|
-
legacy = _legacy_numeric_slot(bot_id)
|
|
210
|
-
if legacy is not None and legacy not in used:
|
|
211
|
-
slot = legacy
|
|
212
|
-
else:
|
|
213
|
-
slot = 1
|
|
214
|
-
while slot in used:
|
|
215
|
-
slot += 1
|
|
216
|
-
self._slots[bot_id] = slot
|
|
217
|
-
try:
|
|
218
|
-
self._save()
|
|
219
|
-
except OSError:
|
|
220
|
-
pass
|
|
221
|
-
return slot
|
|
222
|
-
|
|
223
|
-
def release(self, bot_id: str) -> None:
|
|
224
|
-
if bot_id in self._slots:
|
|
225
|
-
self._slots.pop(bot_id, None)
|
|
226
|
-
try:
|
|
227
|
-
self._save()
|
|
228
|
-
except OSError:
|
|
229
|
-
pass
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
def bot_index(bot_id: str, slot: Optional[int] = None) -> int:
|
|
233
|
-
"""优先使用显式分配的槽位;回退支持 bot_01 -> 1。"""
|
|
234
|
-
if slot is not None and slot >= 1:
|
|
235
|
-
return int(slot)
|
|
236
|
-
legacy = _legacy_numeric_slot(bot_id)
|
|
237
|
-
if legacy is not None:
|
|
238
|
-
return legacy
|
|
239
|
-
digits = "".join(ch for ch in bot_id if ch.isdigit())
|
|
240
|
-
return int(digits) if digits and int(digits) <= 999 else 1
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
def rfb_port_for(bot_id: str, slot: Optional[int] = None) -> int:
|
|
244
|
-
return RFB_PORT_BASE + bot_index(bot_id, slot=slot)
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
def display_for(bot_id: str, slot: Optional[int] = None) -> int:
|
|
248
|
-
return DISPLAY_BASE + bot_index(bot_id, slot=slot)
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
def ws_port_for(bot_id: str, slot: Optional[int] = None) -> int:
|
|
252
|
-
return WS_PORT_BASE + bot_index(bot_id, slot=slot)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
@dataclass(frozen=True)
|
|
256
|
-
class BotPaths:
|
|
257
|
-
"""单个 Bot 的目录规范(方案二.2)。"""
|
|
258
|
-
|
|
259
|
-
root: str
|
|
260
|
-
|
|
261
|
-
@property
|
|
262
|
-
def home(self) -> str:
|
|
263
|
-
return os.path.join(self.root, "home")
|
|
264
|
-
|
|
265
|
-
@property
|
|
266
|
-
def downloads(self) -> str:
|
|
267
|
-
return os.path.join(self.root, "downloads")
|
|
268
|
-
|
|
269
|
-
@property
|
|
270
|
-
def artifacts(self) -> str:
|
|
271
|
-
return os.path.join(self.root, "artifacts")
|
|
272
|
-
|
|
273
|
-
@property
|
|
274
|
-
def audit(self) -> str:
|
|
275
|
-
return os.path.join(self.root, "audit")
|
|
276
|
-
|
|
277
|
-
@property
|
|
278
|
-
def runtime(self) -> str:
|
|
279
|
-
return os.path.join(self.root, "runtime")
|
|
280
|
-
|
|
281
|
-
@property
|
|
282
|
-
def xauthority(self) -> str:
|
|
283
|
-
return os.path.join(self.runtime, "Xauthority")
|
|
284
|
-
|
|
285
|
-
@property
|
|
286
|
-
def token_file(self) -> str:
|
|
287
|
-
return os.path.join(self.runtime, "token.json")
|
|
288
|
-
|
|
289
|
-
@property
|
|
290
|
-
def lease_file(self) -> str:
|
|
291
|
-
"""跨进程可见的控制权租约(落盘 + flock),网关重启后可恢复。"""
|
|
292
|
-
return os.path.join(self.runtime, "lease.json")
|
|
293
|
-
|
|
294
|
-
@property
|
|
295
|
-
def session_file(self) -> str:
|
|
296
|
-
"""关闭前保存的桌面窗口与应用会话状态(重启虚拟屏时自动还原)。"""
|
|
297
|
-
return os.path.join(self.runtime, "session_state.json")
|
|
298
|
-
|
|
299
|
-
@property
|
|
300
|
-
def preview_file(self) -> str:
|
|
301
|
-
"""关闭前保存的最后桌面预览图(停止状态下零开销展示上次画面)。"""
|
|
302
|
-
return os.path.join(self.runtime, "last_preview.jpg")
|
|
303
|
-
|
|
304
|
-
@property
|
|
305
|
-
def spec_file(self) -> str:
|
|
306
|
-
"""当前运行虚拟屏的规格版本标记。"""
|
|
307
|
-
return os.path.join(self.runtime, "desktop_spec.json")
|
|
308
|
-
|
|
309
|
-
@property
|
|
310
|
-
def avatar_file(self) -> str:
|
|
311
|
-
"""当前 Bot 的自定义形象数据(Image-bot / BotAvatarPrefs 12 字段契约)。"""
|
|
312
|
-
return os.path.join(self.runtime, "avatar.json")
|
|
313
|
-
|
|
314
|
-
@property
|
|
315
|
-
def actions_log(self) -> str:
|
|
316
|
-
return os.path.join(self.audit, "actions.jsonl")
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
def paths_for(bot_id: str) -> BotPaths:
|
|
320
|
-
return BotPaths(root=os.path.join(DATA_ROOT, bot_id))
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|