跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

mobile-cli-supervisor-smoke.py

61 行 · 2.5 KB历史下载
1# Linux guest check. Each fixture creates only its own short-lived descendants.
2import json
3import os
4from pathlib import Path
5import signal
6import subprocess
7import sys
8import tempfile
9import time
10
11def until(condition, message, timeout=5):
12 deadline = time.monotonic() + timeout
13 while time.monotonic() < deadline:
14 if condition():
15 return
16 time.sleep(0.025)
17 raise AssertionError(message)
18
19def verify(supervisor, crash):
20 with tempfile.TemporaryDirectory(prefix="cage-supervisor-") as directory:
21 root = Path(directory)
22 child = (
23 "import os,time,pathlib;"
24 "pathlib.Path('tool.pid').write_text(str(os.getpid()));"
25 "time.sleep(1.8);pathlib.Path('late-write').write_text('unexpected')"
26 )
27 middle = (
28 "import sys,subprocess; "
29 f"p=subprocess.Popen([sys.executable,'-c',{child!r}],start_new_session=True);p.wait()"
30 )
31 cli = (
32 "import os,sys,subprocess,pathlib;sys.stdin.read();"
33 "pathlib.Path('cli.pid').write_text(str(os.getpid()));"
34 f"p=subprocess.Popen([sys.executable,'-c',{middle!r}],start_new_session=True);p.wait()"
35 )
36 proc = subprocess.Popen(
37 [sys.executable, "-I", supervisor, directory, sys.executable, "-c", cli],
38 stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
39 with proc:
40 proc.stdin.write(json.dumps("synthetic approved command").encode() + b"\n")
41 proc.stdin.flush()
42 until(lambda: (root / "tool.pid").is_file(), "tool did not start")
43 if crash:
44 os.kill(int((root / "cli.pid").read_text()), signal.SIGKILL)
45 else:
46 proc.stdin.close()
47 code = proc.wait(timeout=6)
48 if code == 0:
49 raise AssertionError("interrupted CLI reported success")
50 until(lambda: not Path("/proc", (root / "tool.pid").read_text()).exists(),
51 "detached tool survived supervision")
52 time.sleep(1.9)
53 if (root / "late-write").exists():
54 raise AssertionError("tool changed project after termination")
55
56if __name__ == "__main__":
57 if not sys.platform.startswith("linux"):
58 raise SystemExit("run inside an isolated Linux guest")
59 for crash in (False, True):
60 verify(str(Path(sys.argv[1]).resolve()), crash)
61 print("CLI crash: PASS" if crash else "SSH disconnect: PASS")