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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +105 -28
  3. package/bin/doctor.mjs +148 -0
  4. package/botscreen/requirements-offline.txt +11 -0
  5. package/client.js +1 -2
  6. package/cordis.patch.yml +1 -1
  7. package/host/api.js +0 -4
  8. package/host/remote.js +2 -140
  9. package/host/screen.js +85 -21
  10. package/market/index.json +41 -0
  11. package/package.json +32 -13
  12. package/vendor/wheels/any/annotated_doc-0.0.7-py3-none-any.whl +0 -0
  13. package/vendor/wheels/any/annotated_types-0.8.0-py3-none-any.whl +0 -0
  14. package/vendor/wheels/any/anyio-4.16.0-py3-none-any.whl +0 -0
  15. package/vendor/wheels/any/certifi-2026.11.20-py3-none-any.whl +0 -0
  16. package/vendor/wheels/any/click-8.5.1-py3-none-any.whl +0 -0
  17. package/vendor/wheels/any/fastapi-0.165.0-py3-none-any.whl +0 -0
  18. package/vendor/wheels/any/h11-0.16.0-py3-none-any.whl +0 -0
  19. package/vendor/wheels/any/httpcore-1.0.9-py3-none-any.whl +0 -0
  20. package/vendor/wheels/any/httpx-0.29.0-py3-none-any.whl +0 -0
  21. package/vendor/wheels/any/idna-3.20-py3-none-any.whl +0 -0
  22. package/vendor/wheels/any/mss-10.2.0-py3-none-any.whl +0 -0
  23. package/vendor/wheels/any/opentelemetry_api-1.45.2-py3-none-any.whl +0 -0
  24. package/vendor/wheels/any/pydantic-2.14.4-py3-none-any.whl +0 -0
  25. package/vendor/wheels/any/python_multipart-0.0.34-py3-none-any.whl +0 -0
  26. package/vendor/wheels/any/python_xlib-0.33-py2.py3-none-any.whl +0 -0
  27. package/vendor/wheels/any/six-1.17.0-py2.py3-none-any.whl +0 -0
  28. package/vendor/wheels/any/starlette-1.10.0-py3-none-any.whl +0 -0
  29. package/vendor/wheels/any/typing_extensions-4.16.0-py3-none-any.whl +0 -0
  30. package/vendor/wheels/any/typing_inspection-0.4.5-py3-none-any.whl +0 -0
  31. package/vendor/wheels/any/uvicorn-0.54.0-py3-none-any.whl +0 -0
  32. package/vendor/wheels/linux-aarch64/pillow-12.2.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
  33. package/vendor/wheels/linux-aarch64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl +0 -0
  34. package/vendor/wheels/linux-aarch64/websockets-17.4-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl +0 -0
  35. package/vendor/wheels/linux-x86_64/pillow-12.2.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
  36. package/vendor/wheels/linux-x86_64/pydantic_core-2.50.5-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl +0 -0
  37. 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
  38. package/botscreen/botscreen/__init__.py +0 -2
  39. package/botscreen/botscreen/__main__.py +0 -5
  40. package/botscreen/botscreen/artifacts.py +0 -388
  41. package/botscreen/botscreen/audit.py +0 -94
  42. package/botscreen/botscreen/cgroup.py +0 -293
  43. package/botscreen/botscreen/config.py +0 -320
  44. package/botscreen/botscreen/desktop/assets/chrome.png +0 -0
  45. package/botscreen/botscreen/desktop/assets/codex.png +0 -0
  46. package/botscreen/botscreen/desktop/assets/drive.png +0 -0
  47. package/botscreen/botscreen/desktop/assets/folder.png +0 -0
  48. package/botscreen/botscreen/desktop/assets/search.png +0 -0
  49. package/botscreen/botscreen/desktop/assets/terminal.png +0 -0
  50. package/botscreen/botscreen/desktop/assets/trash.png +0 -0
  51. package/botscreen/botscreen/desktop/assets/wallpaper.png +0 -0
  52. package/botscreen/botscreen/desktop.py +0 -244
  53. package/botscreen/botscreen/driver.py +0 -832
  54. package/botscreen/botscreen/gateway.py +0 -2089
  55. package/botscreen/botscreen/iconlayer.py +0 -841
  56. package/botscreen/botscreen/mcp_server.py +0 -435
  57. package/botscreen/botscreen/resources.py +0 -494
  58. package/botscreen/botscreen/screen.py +0 -1282
  59. package/botscreen/botscreen/session.py +0 -706
  60. package/botscreen/botscreen/state.py +0 -330
  61. package/botscreen/botscreen/streamer.py +0 -388
  62. package/remote/bin/dsh-remote.mjs +0 -418
  63. package/remote/package.json +0 -27
  64. package/remote/src/config.mjs +0 -183
  65. package/remote/src/crypto.mjs +0 -220
  66. package/remote/src/keys.mjs +0 -226
  67. package/remote/src/proxy.mjs +0 -1028
  68. package/remote/src/sessions.mjs +0 -163
  69. package/remote/src/shim.js +0 -838
  70. package/remote/src/supervisor.mjs +0 -207
  71. package/remote/src/tunnel.mjs +0 -489
  72. package/remote/src/upstream.mjs +0 -372
  73. package/remote/src/util.mjs +0 -139
  74. package/remote/src/ws.mjs +0 -313
  75. package/requirements.txt +0 -8
@@ -1,330 +0,0 @@
1
- """每张虚拟屏的状态机 + 人机互斥锁(方案三),带跨进程落盘租约。
2
-
3
- 状态迁移:
4
- STOPPED ──start──▶ AGENT_ACTIVE ──idle──▶ FROZEN ──agent/human──▶ AGENT_ACTIVE
5
- │ ▲
6
- ├──human input──▶ HUMAN_TAKEOVER ──release──┘
7
- └──request_help──▶ NEED_HUMAN ──takeover──▶ HUMAN_TAKEOVER
8
-
9
- 租约(lease)语义参照 Hermes `tools/bot_desktop/lease.py`:
10
- * 权威落盘在 `runtime/lease.json`,写时 `flock` + 原子 `os.replace`,读不存在的内存态不参与判定;
11
- * `epoch` 每次迁移 +1:一个在某次租约下被放行的动作,可以据此发现自己执行期间控制权已切换;
12
- * **fail-closed**:租约文件存在但读不出来/内容非法时,一律视为「人类持有」,绝不放行 Agent;
13
- * holder 只有 `agent` / `human` 两个值,键鼠与截图的判定都从这里派生。
14
- """
15
- from __future__ import annotations
16
-
17
- import errno
18
- import fcntl
19
- import json
20
- import os
21
- import tempfile
22
- import time
23
- from enum import Enum
24
- from typing import Optional
25
-
26
- AGENT = "agent"
27
- HUMAN = "human"
28
-
29
-
30
- class State(str, Enum):
31
- STOPPED = "STOPPED"
32
- STARTING = "STARTING"
33
- FROZEN = "FROZEN"
34
- AGENT_ACTIVE = "AGENT_ACTIVE"
35
- HUMAN_TAKEOVER = "HUMAN_TAKEOVER"
36
- NEED_HUMAN = "NEED_HUMAN"
37
-
38
-
39
- #: Agent 允许操控的状态(STOPPED / FROZEN 会按需自动拉起或唤醒)
40
- AGENT_ALLOWED = {State.STOPPED, State.STARTING, State.AGENT_ACTIVE, State.FROZEN}
41
- #: 允许推流给人类的状态
42
- STREAMABLE = {State.AGENT_ACTIVE, State.HUMAN_TAKEOVER, State.NEED_HUMAN}
43
-
44
-
45
- class HumanInControl(Exception):
46
- """人类正在接管,Agent 请求被拒绝 → HTTP 423。"""
47
-
48
- def __init__(self, bot_id: str, state: str, what: str = "control") -> None:
49
- super().__init__(f"{bot_id} locked: {state}")
50
- self.bot_id = bot_id
51
- self.state = state
52
- self.what = what
53
-
54
-
55
- class StaleLease(Exception):
56
- """调用方按旧 epoch 放行的动作已经过期 → HTTP 409。"""
57
-
58
- def __init__(self, bot_id: str, expected: int, actual: int) -> None:
59
- super().__init__(f"{bot_id} lease epoch changed: {expected} -> {actual}")
60
- self.bot_id = bot_id
61
- self.expected = expected
62
- self.actual = actual
63
-
64
-
65
- class ScreenLock:
66
- """单屏状态机。所有迁移都走这里,保证互斥语义唯一。"""
67
-
68
- def __init__(self, bot_id: str, lease_path: Optional[str] = None) -> None:
69
- self.bot_id = bot_id
70
- self.state = State.STOPPED
71
- self.epoch = 0
72
- self.frozen_at: Optional[float] = None
73
- self.last_agent_call = 0.0
74
- self.last_human_input = 0.0
75
- self.viewers = 0
76
- self.human_held_since: Optional[float] = None
77
- self.help_reason: Optional[str] = None
78
- self.transitions: list[dict] = []
79
- self.lease_path = lease_path
80
- self.lease_corrupt: Optional[str] = None
81
-
82
- self._op_lock = None # 懒加载 asyncio.Lock(构造可能发生在无事件循环的线程里)
83
- self._released = None
84
-
85
- # ---- 异步原语懒加载 ---------------------------------------------------
86
- def _lazy(self) -> None:
87
- import asyncio
88
-
89
- if self._op_lock is None:
90
- self._op_lock = asyncio.Lock()
91
- if self._released is None:
92
- self._released = asyncio.Event()
93
- self._released.set()
94
-
95
- @property
96
- def released_event(self):
97
- self._lazy()
98
- return self._released
99
-
100
- @property
101
- def op_lock(self):
102
- self._lazy()
103
- return self._op_lock
104
-
105
- # ---- holder / 租约 ----------------------------------------------------
106
- @property
107
- def holder(self) -> str:
108
- return HUMAN if self.state is State.HUMAN_TAKEOVER else AGENT
109
-
110
- def _persist(self) -> None:
111
- """把租约写到磁盘。失败不影响内存态(内存态仍是权威),但要可观测。"""
112
- if not self.lease_path:
113
- return
114
- payload = {
115
- "bot_id": self.bot_id,
116
- "holder": self.holder,
117
- "state": self.state.value,
118
- "epoch": self.epoch,
119
- "since": self.human_held_since or time.time(),
120
- "updated_at": time.time(),
121
- "reason": self.transitions[-1]["reason"] if self.transitions else "",
122
- "pid": os.getpid(),
123
- }
124
- directory = os.path.dirname(self.lease_path)
125
- try:
126
- os.makedirs(directory, exist_ok=True)
127
- lock_path = self.lease_path + ".lock"
128
- with open(lock_path, "a+") as lock_fh:
129
- try:
130
- fcntl.flock(lock_fh.fileno(), fcntl.LOCK_EX)
131
- except OSError:
132
- pass
133
- fd, tmp = tempfile.mkstemp(dir=directory, prefix=".lease-")
134
- try:
135
- with os.fdopen(fd, "w", encoding="utf-8") as fh:
136
- json.dump(payload, fh, ensure_ascii=False)
137
- fh.flush()
138
- os.fsync(fh.fileno())
139
- os.replace(tmp, self.lease_path)
140
- except BaseException:
141
- try:
142
- os.unlink(tmp)
143
- except OSError:
144
- pass
145
- raise
146
- try:
147
- fcntl.flock(lock_fh.fileno(), fcntl.LOCK_UN)
148
- except OSError:
149
- pass
150
- except OSError as exc:
151
- if exc.errno not in (errno.ENOSPC, errno.EACCES):
152
- raise
153
-
154
- def restore_from_disk(self) -> Optional[dict]:
155
- """启动/接管时读租约。**fail-closed**:读不出来就当人类持有。"""
156
- if not self.lease_path:
157
- return None
158
- try:
159
- with open(self.lease_path, encoding="utf-8") as fh:
160
- data = json.load(fh)
161
- except FileNotFoundError:
162
- return None
163
- except (OSError, json.JSONDecodeError) as exc:
164
- self.lease_corrupt = f"unreadable lease: {exc}"
165
- self.human_held_since = time.time()
166
- self._to(State.HUMAN_TAKEOVER, "lease unreadable (fail-closed)")
167
- return {"holder": HUMAN, "corrupt": str(exc)}
168
-
169
- if not isinstance(data, dict) or data.get("holder") not in (AGENT, HUMAN):
170
- self.lease_corrupt = "lease has invalid holder"
171
- self.human_held_since = time.time()
172
- self._to(State.HUMAN_TAKEOVER, "lease corrupt (fail-closed)")
173
- return {"holder": HUMAN, "corrupt": "invalid holder"}
174
-
175
- self.epoch = max(self.epoch, int(data.get("epoch") or 0))
176
- if data["holder"] == HUMAN:
177
- self.human_held_since = float(data.get("since") or time.time())
178
- self._to(State.HUMAN_TAKEOVER, str(data.get("reason") or "restored human lease"))
179
- return data
180
-
181
- # ---- 内部 -------------------------------------------------------------
182
- def _to(self, new: State, reason: str) -> None:
183
- if new is self.state:
184
- return
185
- self.transitions.append(
186
- {
187
- "ts": round(time.time(), 3),
188
- "from": self.state.value,
189
- "to": new.value,
190
- "reason": reason,
191
- "epoch": self.epoch + 1,
192
- }
193
- )
194
- del self.transitions[:-50]
195
- self.state = new
196
- self.epoch += 1
197
- if new is State.FROZEN:
198
- self.frozen_at = time.time()
199
- elif new is not State.FROZEN:
200
- self.frozen_at = None
201
- self._persist()
202
-
203
- # ---- 控制面 -----------------------------------------------------------
204
- async def begin_start(self) -> None:
205
- self._to(State.STARTING, "start")
206
- self.released_event.set()
207
-
208
- def mark_active(self, reason: str = "ready") -> None:
209
- # 启动即刷新活动计时基准,否则刚拉起的屏会被 idle_reaper 立刻判定为闲置
210
- self.last_agent_call = time.time()
211
- self._to(State.AGENT_ACTIVE, reason)
212
-
213
- def mark_stopped(self, reason: str = "stop") -> None:
214
- self._to(State.STOPPED, reason)
215
- self.human_held_since = None
216
- self.help_reason = None
217
- self.released_event.set()
218
- self._persist()
219
-
220
- def mark_frozen(self) -> None:
221
- self._to(State.FROZEN, "idle")
222
-
223
- def mark_thawed(self) -> None:
224
- if self.state is State.FROZEN:
225
- self.last_agent_call = time.time()
226
- self._to(State.AGENT_ACTIVE, "thaw")
227
-
228
- async def takeover(self, reason: str = "human") -> None:
229
- async with self.op_lock:
230
- self.human_held_since = time.time()
231
- self.released_event.clear()
232
- self._to(State.HUMAN_TAKEOVER, reason)
233
-
234
- async def request_help(self, reason: str) -> None:
235
- async with self.op_lock:
236
- self.help_reason = reason
237
- self.released_event.clear()
238
- self._to(State.NEED_HUMAN, "agent request_help")
239
-
240
- async def release(self) -> None:
241
- async with self.op_lock:
242
- self.human_held_since = None
243
- self.help_reason = None
244
- self.released_event.set()
245
- if self.state in (State.HUMAN_TAKEOVER, State.NEED_HUMAN):
246
- self.last_agent_call = time.time()
247
- self._to(State.AGENT_ACTIVE, "human release")
248
-
249
- def touch_agent(self) -> None:
250
- self.last_agent_call = time.time()
251
-
252
- def touch_human(self) -> bool:
253
- """人类输入信号(VNC 键鼠帧)。返回是否真的触发了抢占。"""
254
- self.last_human_input = time.time()
255
- if self.state in (State.AGENT_ACTIVE, State.NEED_HUMAN):
256
- self.human_held_since = time.time()
257
- self.released_event.clear()
258
- self._to(State.HUMAN_TAKEOVER, "human input detected")
259
- return True
260
- return False
261
-
262
- # ---- Agent 侧守卫 -----------------------------------------------------
263
- def guard_agent(self, screenshot: bool = False) -> None:
264
- """Agent 调用前检查。
265
-
266
- * `HUMAN_TAKEOVER`:键鼠和截图**全部**拒绝(fail-closed:人可能正在输密码)。
267
- * `NEED_HUMAN`:键鼠拒绝,但允许截图(Agent 需要看人是否已处理完)。
268
- * `STOPPED` / `STARTING` / `FROZEN`:放行,由后续 `ensure_awake()` 按需拉起并恢复现场。
269
- """
270
- if self.state is State.HUMAN_TAKEOVER:
271
- raise HumanInControl(self.bot_id, self.state.value, "screenshot" if screenshot else "control")
272
- if self.state is State.NEED_HUMAN and not screenshot:
273
- raise HumanInControl(self.bot_id, self.state.value, "control")
274
-
275
- def check_epoch(self, expected: Optional[int]) -> None:
276
- """调用方带上 X-Expected-Epoch 时,控制权必须在它以为的这一次租约上。"""
277
- if expected is None:
278
- return
279
- if int(expected) != self.epoch:
280
- raise StaleLease(self.bot_id, int(expected), self.epoch)
281
-
282
- async def wait_for_release(self, timeout: float = 60.0) -> bool:
283
- import asyncio
284
-
285
- try:
286
- await asyncio.wait_for(self.released_event.wait(), timeout)
287
- return True
288
- except asyncio.TimeoutError:
289
- return False
290
-
291
- # ---- 闲置判定(双信号) ------------------------------------------------
292
- def idle_seconds(self) -> float:
293
- refs = [self.last_agent_call, self.last_human_input]
294
- return time.time() - max(refs) if any(refs) else float("inf")
295
-
296
- def should_freeze(self, idle_timeout: float) -> bool:
297
- return (
298
- self.state is State.AGENT_ACTIVE
299
- and self.viewers == 0
300
- and self.idle_seconds() >= idle_timeout
301
- )
302
-
303
- def should_idle_stop(self, idle_timeout: float) -> bool:
304
- return (
305
- self.state is State.AGENT_ACTIVE
306
- and self.viewers == 0
307
- and self.idle_seconds() >= idle_timeout
308
- )
309
-
310
- def long_freeze(self, threshold: float) -> bool:
311
- return self.frozen_at is not None and (time.time() - self.frozen_at) >= threshold
312
-
313
- # ---- 视图 -------------------------------------------------------------
314
- def snapshot(self) -> dict:
315
- return {
316
- "bot_id": self.bot_id,
317
- "state": self.state.value,
318
- "holder": self.holder,
319
- "epoch": self.epoch,
320
- "lease_corrupt": self.lease_corrupt,
321
- "frozen_at": self.frozen_at,
322
- "idle_seconds": None
323
- if self.idle_seconds() == float("inf")
324
- else round(self.idle_seconds(), 1),
325
- "viewers": self.viewers,
326
- "help_reason": self.help_reason,
327
- "human_held_since": self.human_held_since,
328
- "last_agent_call": self.last_agent_call or None,
329
- "transitions": self.transitions[-8:],
330
- }