ohos-devloop 0.1.2__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.
- ohos_devloop/__init__.py +2 -0
- ohos_devloop/_version.py +24 -0
- ohos_devloop/agents/skills/collab-delegate/SKILL.md +79 -0
- ohos_devloop/agents/skills/loop-iterate/SKILL.md +54 -0
- ohos_devloop/agents/skills/openharmony-ci/SKILL.md +71 -0
- ohos_devloop/agents/skills/openharmony-ci/agents/openai.yaml +4 -0
- ohos_devloop/agents/skills/openharmony-ci/scripts/openharmony_ci.py +520 -0
- ohos_devloop/agents/skills/pool-status/SKILL.md +47 -0
- ohos_devloop/config/e2e-cases.yaml +31 -0
- ohos_devloop/config/e2e-hap.yaml +54 -0
- ohos_devloop/config/inventory-template.yaml +35 -0
- ohos_devloop/config/inventory.yaml +68 -0
- ohos_devloop/config/report-templates/analysis-report.md +48 -0
- ohos_devloop/config/report-templates/impl-report.md +47 -0
- ohos_devloop/config/task-template.md +21 -0
- ohos_devloop/config/tdd-board.yaml +31 -0
- ohos_devloop/config/tdd-coverage.yaml +47 -0
- ohos_devloop/config/tdd-emulator.yaml +36 -0
- ohos_devloop/config/tdd-host.yaml +26 -0
- ohos_devloop/config/tdd-suites.yaml +34 -0
- ohos_devloop/config/templates/agents-user.md +11 -0
- ohos_devloop/config/templates/quickstart.md +82 -0
- ohos_devloop/config/verify.yaml +45 -0
- ohos_devloop/examples/anchor-e2e.yaml +55 -0
- ohos_devloop/examples/assert-smoke.yaml +33 -0
- ohos_devloop/examples/diffcov-fixture/cov.json +6688 -0
- ohos_devloop/examples/diffcov-fixture/diff.txt +24 -0
- ohos_devloop/examples/diffcov-smoke.yaml +32 -0
- ohos_devloop/examples/e2e-emu-smoke.yaml +45 -0
- ohos_devloop/examples/layout-fixture.norm.json +1 -0
- ohos_devloop/examples/smoke.yaml +27 -0
- ohos_devloop/ohos_cli.py +131 -0
- ohos_devloop/scripts/agent-run-v3.sh +149 -0
- ohos_devloop/scripts/apply.py +1448 -0
- ohos_devloop/scripts/check_env.py +284 -0
- ohos_devloop/scripts/collab.py +303 -0
- ohos_devloop/scripts/diffcov.py +211 -0
- ohos_devloop/scripts/flash.py +511 -0
- ohos_devloop/scripts/mount_ws.py +192 -0
- ohos_devloop/scripts/release.py +2645 -0
- ohos_devloop/scripts/release_audit.py +260 -0
- ohos_devloop/scripts/release_metadata.py +397 -0
- ohos_devloop/scripts/release_smoke.py +287 -0
- ohos_devloop/scripts/report_draft.py +234 -0
- ohos_devloop/scripts/server_init.py +310 -0
- ohos_devloop/scripts/sgrep.py +82 -0
- ohos_devloop/scripts/sign_hap.py +256 -0
- ohos_devloop/scripts/telemetry.py +260 -0
- ohos_devloop/scripts/ui_probe.py +396 -0
- ohos_devloop/scripts/verify_frame_diff.py +138 -0
- ohos_devloop/scripts/ws_init.py +131 -0
- ohos_devloop/scripts/wsroot.py +192 -0
- ohos_devloop-0.1.2.dist-info/METADATA +177 -0
- ohos_devloop-0.1.2.dist-info/RECORD +58 -0
- ohos_devloop-0.1.2.dist-info/WHEEL +5 -0
- ohos_devloop-0.1.2.dist-info/entry_points.txt +2 -0
- ohos_devloop-0.1.2.dist-info/licenses/LICENSE +202 -0
- ohos_devloop-0.1.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""环境诊断 —— clone 本仓后第一件事:逐项检查本机工具链/构建服务器/开发板就绪度。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
ohos-devloop check-env # 全量检查(含 ssh 外联与板卡探测)
|
|
7
|
+
ohos-devloop check-env --offline # 跳过 ssh/板卡等外联项
|
|
8
|
+
输出 [PASS]/[WARN]/[FAIL] + 修复提示;有 FAIL 时退出码 1。检查项与 README「环境安装」一一对应。
|
|
9
|
+
"""
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
import shutil
|
|
13
|
+
import subprocess
|
|
14
|
+
import sys
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
import yaml
|
|
18
|
+
|
|
19
|
+
import wsroot # inventory/注册表读源(工作区优先、包回落;安装态读包缺省)
|
|
20
|
+
|
|
21
|
+
PASS = WARN = FAIL = 0
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def report(tag: str, item: str, msg: str = "") -> None:
|
|
25
|
+
global PASS, WARN, FAIL
|
|
26
|
+
suffix = f" — {msg}" if msg else ""
|
|
27
|
+
print(f"[{tag}] {item}{suffix}")
|
|
28
|
+
if tag == "PASS":
|
|
29
|
+
PASS += 1
|
|
30
|
+
elif tag == "WARN":
|
|
31
|
+
WARN += 1
|
|
32
|
+
else:
|
|
33
|
+
FAIL += 1
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def run(args: list[str], timeout: int = 30) -> tuple[int, str]:
|
|
37
|
+
# npm/devecocli 等是 .cmd shim,CreateProcess 不认 PATHEXT —— 先 shutil.which 解析全路径
|
|
38
|
+
cmd = shutil.which(args[0]) or args[0]
|
|
39
|
+
try:
|
|
40
|
+
p = subprocess.run([cmd, *args[1:]], capture_output=True, text=True,
|
|
41
|
+
encoding="utf-8", errors="replace", timeout=timeout)
|
|
42
|
+
return p.returncode, (p.stdout or "") + (p.stderr or "")
|
|
43
|
+
except subprocess.TimeoutExpired:
|
|
44
|
+
return 124, ""
|
|
45
|
+
except FileNotFoundError:
|
|
46
|
+
return 127, ""
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def ver_ge(cur: str, minv: str) -> bool:
|
|
50
|
+
def key(s):
|
|
51
|
+
return tuple(int(x) for x in re.findall(r"\d+", s)[:3])
|
|
52
|
+
return key(cur) >= key(minv)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def main() -> int:
|
|
56
|
+
offline = "--offline" in sys.argv
|
|
57
|
+
print("== 本机基础 ==")
|
|
58
|
+
|
|
59
|
+
rc, out = run(["python", "--version"])
|
|
60
|
+
pyv = re.search(r"(\d+\.\d+)", out)
|
|
61
|
+
if rc == 0 and pyv and ver_ge(pyv.group(1), "3.10"):
|
|
62
|
+
if run(["python", "-c", "import yaml"])[0] == 0:
|
|
63
|
+
report("PASS", "Python + PyYAML", f"python {pyv.group(1)}")
|
|
64
|
+
else:
|
|
65
|
+
report("FAIL", "PyYAML", "未安装 → pip install pyyaml(调度器依赖)")
|
|
66
|
+
else:
|
|
67
|
+
report("FAIL", "Python", "未找到或版本 < 3.10 → 安装 Python 3.10+ 并加入 PATH")
|
|
68
|
+
|
|
69
|
+
rc, out = run(["node", "--version"])
|
|
70
|
+
nodev = out.strip().lstrip("v")
|
|
71
|
+
if rc == 0 and ver_ge(nodev, "22"):
|
|
72
|
+
report("PASS", "Node.js", f"v{nodev}(deveco-cli 要求 ≥22)")
|
|
73
|
+
else:
|
|
74
|
+
report("FAIL", "Node.js", "未找到或 < 22 → https://nodejs.org 安装 22+")
|
|
75
|
+
rc, out = run(["npm", "--version"])
|
|
76
|
+
if rc == 0:
|
|
77
|
+
report("PASS", "npm", out.strip())
|
|
78
|
+
else:
|
|
79
|
+
report("FAIL", "npm", "未找到(随 Node.js 安装)")
|
|
80
|
+
|
|
81
|
+
rc, out = run(["devecocli", "--version"])
|
|
82
|
+
if rc == 0:
|
|
83
|
+
report("PASS", "deveco-cli", f"devecocli {out.strip()}")
|
|
84
|
+
else:
|
|
85
|
+
report("WARN", "deveco-cli", "未安装 → npm install -g @deveco/deveco-cli(测试应用编译/装板)")
|
|
86
|
+
|
|
87
|
+
# assert frame_diff 断言依赖(缺了其余断言种类仍可用,降 WARN)
|
|
88
|
+
rc, _ = run(["python", "-c", "import PIL"])
|
|
89
|
+
report(("PASS" if rc == 0 else "WARN"), "Pillow",
|
|
90
|
+
"在位" if rc == 0 else "未安装 → pip install Pillow(assert 步骤 frame_diff 需要)")
|
|
91
|
+
|
|
92
|
+
# hap 离线签名链(hap_sign 步骤依赖;材料缺失时 sign 步骤会给出精确提示)
|
|
93
|
+
studio = Path(os.environ.get("DEVECO_CLI_STUDIO_PATH", "C:/Program Files/Huawei/DevEco Studio"))
|
|
94
|
+
lib = studio / "sdk/default/openharmony/toolchains/lib"
|
|
95
|
+
sign_deps = [studio / "jbr/bin/java.exe", lib / "hap-sign-tool.jar",
|
|
96
|
+
lib / "OpenHarmony.p12", lib / "UnsgnedDebugProfileTemplate.json"]
|
|
97
|
+
missing_sign = [str(p) for p in sign_deps if not p.is_file()]
|
|
98
|
+
if not missing_sign:
|
|
99
|
+
report("PASS", "hap 离线签名链", "java/hap-sign-tool/p12/模板 齐备(profile 材料首签时自动生成)")
|
|
100
|
+
else:
|
|
101
|
+
report("WARN", "hap 离线签名链", "缺 " + ";".join(missing_sign) + "(hap_sign 步骤不可用)")
|
|
102
|
+
|
|
103
|
+
studio = Path(os.environ.get("DEVECO_CLI_STUDIO_PATH", "C:/Program Files/Huawei/DevEco Studio"))
|
|
104
|
+
info = studio / "product-info.json"
|
|
105
|
+
if info.exists():
|
|
106
|
+
try:
|
|
107
|
+
sv = re.search(r'"version":\s*"([^"]+)"', info.read_text(encoding="utf-8")).group(1)
|
|
108
|
+
except Exception:
|
|
109
|
+
sv = "?"
|
|
110
|
+
if (studio / "sdk/default/openharmony").is_dir():
|
|
111
|
+
report("PASS", "DevEco Studio + OpenHarmony SDK", f"{sv}(SDK 目录在位;与板端基线偏差大时走 deveco-sdk-update 流程)")
|
|
112
|
+
else:
|
|
113
|
+
report("WARN", "OpenHarmony SDK 目录", f"Studio {sv} 已装但 sdk/default/openharmony 缺失")
|
|
114
|
+
else:
|
|
115
|
+
report("WARN", "DevEco Studio", "默认路径未找到(应用编译/调试工具链;纯 CLI 流可后装)")
|
|
116
|
+
|
|
117
|
+
print("== 本仓配置 ==")
|
|
118
|
+
inv_path = wsroot.inventory_path() # 工作区优先、包回落(PACK-02:安装态读自己的 inventory 而非包缺省)
|
|
119
|
+
if not inv_path.exists():
|
|
120
|
+
report("FAIL", "config/inventory.yaml", "缺失(资源清单唯一事实源)")
|
|
121
|
+
inv = {}
|
|
122
|
+
else:
|
|
123
|
+
try:
|
|
124
|
+
inv = yaml.safe_load(inv_path.read_text(encoding="utf-8")) or {}
|
|
125
|
+
except Exception as e:
|
|
126
|
+
report("FAIL", "inventory.yaml", f"解析失败: {e}")
|
|
127
|
+
inv = {}
|
|
128
|
+
else:
|
|
129
|
+
servers = inv.get("servers") or {}
|
|
130
|
+
if servers:
|
|
131
|
+
name, s = next(iter(servers.items()))
|
|
132
|
+
origin = "工作区" if inv_path != wsroot.PKG / "config" / "inventory.yaml" else "包缺省"
|
|
133
|
+
report("PASS", "inventory.yaml 可解析",
|
|
134
|
+
f"server {name}: {s.get('user')}@{s.get('host')}:{s.get('port')}(读自{origin})")
|
|
135
|
+
else:
|
|
136
|
+
report("FAIL", "inventory.yaml", "无 servers 条目 → 编辑 servers/boards/local 三段")
|
|
137
|
+
|
|
138
|
+
# e2e/TDD 注册表与报告模板(M1-M3 资产;缺项对应功能降级但不挡基础链路 → WARN)
|
|
139
|
+
for rel, why in (("config/e2e-cases.yaml", "hap e2e 用例注册(hap_* 步骤)"),
|
|
140
|
+
("config/tdd-suites.yaml", "TDD 套件别名(tdd_* 步骤)"),
|
|
141
|
+
("config/report-templates/impl-report.md", "功能实现报告模板"),
|
|
142
|
+
("config/report-templates/analysis-report.md", "问题分析报告模板")):
|
|
143
|
+
p = wsroot.config_path(rel.removeprefix("config/")) # 工作区优先、包回落
|
|
144
|
+
if p.is_file():
|
|
145
|
+
report("PASS", rel)
|
|
146
|
+
else:
|
|
147
|
+
report("WARN", rel, f"缺失({why})")
|
|
148
|
+
|
|
149
|
+
print("== 构建服务器 ==")
|
|
150
|
+
srv = next(iter((inv.get("servers") or {}).values()), {})
|
|
151
|
+
host, port, user = srv.get("host"), str(srv.get("port", 22)), srv.get("user")
|
|
152
|
+
placeholder_host = isinstance(host, str) and "<" in host # 占位串 truthy(Pitfall 6):裸探测会误导性 FAIL
|
|
153
|
+
if offline:
|
|
154
|
+
report("WARN", "SSH 连通性", "--offline 跳过")
|
|
155
|
+
elif placeholder_host:
|
|
156
|
+
report("FAIL", "SSH 免密",
|
|
157
|
+
"inventory 未配置(host 仍是 <...> 占位串)→ 先编辑 config/inventory.yaml 填你的服务器"
|
|
158
|
+
"(工作区覆写位或包缺省;维护者源码仓形态填仓根 config/inventory.yaml)")
|
|
159
|
+
elif host:
|
|
160
|
+
# 远程命令以单元素传入;探测失败不能拖垮整体退出码(; true 兜底)
|
|
161
|
+
rc, out = run(["ssh", "-o", "ConnectTimeout=15", "-o", "BatchMode=yes",
|
|
162
|
+
"-p", port, f"{user}@{host}",
|
|
163
|
+
"echo ok; ls -d /srv/workspace/openharmony_master_default_*/code 2>/dev/null | head -5; true"], timeout=30)
|
|
164
|
+
if out.strip().splitlines()[:1] == ["ok"]:
|
|
165
|
+
ws = [l for l in out.splitlines()[1:] if l.strip()]
|
|
166
|
+
if ws:
|
|
167
|
+
report("PASS", "SSH 免密 + 大仓工作区", f"{host}({len(ws)} 个工作区在位)")
|
|
168
|
+
else:
|
|
169
|
+
report("WARN", "大仓工作区", "SSH 通但未发现 openharmony_master_default_* 工作区 → sync-repo/编译不可用")
|
|
170
|
+
else:
|
|
171
|
+
report("FAIL", "SSH 免密", f"连不上 {user}@{host}:{port} → 配置 ssh 免密(需要 ssh 客户端:Windows 内置 OpenSSH 或 Git for Windows)")
|
|
172
|
+
|
|
173
|
+
print("== 远端 code-worker 契约 ==")
|
|
174
|
+
if offline:
|
|
175
|
+
report("WARN", "code-worker 契约", "--offline 跳过")
|
|
176
|
+
elif placeholder_host:
|
|
177
|
+
report("WARN", "code-worker 契约",
|
|
178
|
+
"inventory 未配置(host 占位),跳过探测——先填 config/inventory.yaml")
|
|
179
|
+
elif host:
|
|
180
|
+
rc, out = run(["ssh", "-o", "ConnectTimeout=15", "-o", "BatchMode=yes",
|
|
181
|
+
"-p", port, f"{user}@{host}",
|
|
182
|
+
"test -x ~/collab/bin/agent-run-v3.sh && echo SCRIPT_OK; "
|
|
183
|
+
"test -x /root/.local/bin/opencode && echo OPENCODE_OK; true"], timeout=30)
|
|
184
|
+
flags = out.split()
|
|
185
|
+
if "SCRIPT_OK" in flags and "OPENCODE_OK" in flags:
|
|
186
|
+
report("PASS", "collab 契约", f"{host}: agent-run-v3.sh + opencode 在位(collab.py 任务通道可用)")
|
|
187
|
+
else:
|
|
188
|
+
missing = []
|
|
189
|
+
if "SCRIPT_OK" not in flags:
|
|
190
|
+
missing.append("~/collab/bin/agent-run-v3.sh(scp scripts/agent-run-v3.sh 上去并 chmod +x)")
|
|
191
|
+
if "OPENCODE_OK" not in flags:
|
|
192
|
+
missing.append("/root/.local/bin/opencode")
|
|
193
|
+
report("FAIL", "collab 契约", f"缺 {';'.join(missing)}")
|
|
194
|
+
|
|
195
|
+
print("== W: 应急通道(SSHFS,可选) ==")
|
|
196
|
+
if offline:
|
|
197
|
+
report("WARN", "W: 应急通道", "--offline 跳过")
|
|
198
|
+
else:
|
|
199
|
+
sshfs = r"C:\Program Files\SSHFS-Win\bin\sshfs-win.exe"
|
|
200
|
+
if Path(sshfs).is_file():
|
|
201
|
+
rc, out = run(["cmd", "/c", "dir W:\\ >nul 2>&1"])
|
|
202
|
+
if rc == 0:
|
|
203
|
+
report("PASS", "W: 挂载", "W:\\ 可访问(应急人工通道;改码主通道 = 远端 code-worker)")
|
|
204
|
+
else:
|
|
205
|
+
report("WARN", "W: 挂载", "未挂载(可选应急通道,不影响改码)→ python scripts/mount_ws.py W:")
|
|
206
|
+
else:
|
|
207
|
+
report("WARN", "sshfs-win.exe", f"未找到(可选应急通道,改码不依赖它): {sshfs}")
|
|
208
|
+
|
|
209
|
+
print("== 开发板 ==")
|
|
210
|
+
if offline:
|
|
211
|
+
report("WARN", "板卡探测", "--offline 跳过")
|
|
212
|
+
else:
|
|
213
|
+
hdc = ((inv.get("local") or {}).get("hdc")
|
|
214
|
+
or "C:/Program Files/oh_toolchain/toolchains/hdc.exe")
|
|
215
|
+
if not Path(hdc).is_file():
|
|
216
|
+
report("FAIL", "hdc", f"未找到 {hdc} → 装 DevEco Studio 或 oh_toolchain,路径写进 inventory.yaml local.hdc")
|
|
217
|
+
else:
|
|
218
|
+
rc, out = run([hdc, "list", "targets"])
|
|
219
|
+
targets = [l for l in out.splitlines() if l.strip() and "ERROR" not in l and "[" not in l]
|
|
220
|
+
if targets:
|
|
221
|
+
report("PASS", "hdc + 板卡", f"{hdc}({len(targets)} 台在线)")
|
|
222
|
+
rc, out = run([hdc, "shell", "param get const.product.software.version"])
|
|
223
|
+
ver = out.strip()
|
|
224
|
+
if ver:
|
|
225
|
+
expects = [str(b.get("software_version") or "") for b in (inv.get("boards") or {}).values()]
|
|
226
|
+
if any(e and e in ver for e in expects):
|
|
227
|
+
report("PASS", "板端系统版本", f"{ver}(与 inventory 期望一致)")
|
|
228
|
+
elif any(e for e in expects):
|
|
229
|
+
report("WARN", "板端系统版本",
|
|
230
|
+
f"板上 {ver} 不含 inventory 期望 {expects}(混基线会被 push/preflight 拒绝;"
|
|
231
|
+
"要么换固件要么更新 inventory)")
|
|
232
|
+
else:
|
|
233
|
+
report("PASS", "板端系统版本", ver)
|
|
234
|
+
else:
|
|
235
|
+
report("WARN", "hdc", f"{hdc} 在位但无在线设备 → 检查 USB 连接/授权")
|
|
236
|
+
|
|
237
|
+
print("== 本机数据目录 ==")
|
|
238
|
+
# 目录清单来自 inventory local 段(PUB-01 占位化后不再内嵌个人路径);
|
|
239
|
+
# <...> 占位值 = 未配置 → WARN「inventory 未配置」而非误导性 FAIL/裸探测(Pitfall 6)
|
|
240
|
+
for key in ("download_dir", "images_dir", "board_backup"):
|
|
241
|
+
v = str((inv.get("local") or {}).get(key) or "").strip()
|
|
242
|
+
if not v or v.startswith("<"):
|
|
243
|
+
report("WARN", f"local.{key}",
|
|
244
|
+
"inventory 未配置(占位值)→ 编辑 config/inventory.yaml 的 local 段填真实目录")
|
|
245
|
+
elif Path(v).is_dir():
|
|
246
|
+
report("PASS", f"目录 {v}")
|
|
247
|
+
else:
|
|
248
|
+
report("WARN", f"目录 {v}", "不存在(flash/推库备份用到;首次用到时创建即可)")
|
|
249
|
+
|
|
250
|
+
print("== Git ==")
|
|
251
|
+
rc, out = run(["git", "--version"])
|
|
252
|
+
if rc == 0:
|
|
253
|
+
report("PASS", "git", out.strip())
|
|
254
|
+
else:
|
|
255
|
+
report("FAIL", "git", "未安装")
|
|
256
|
+
rc, out = run(["git", "config", "--get", "user.name"])
|
|
257
|
+
rc2, out2 = run(["git", "config", "--get", "user.email"])
|
|
258
|
+
if rc == 0 and rc2 == 0 and out.strip() and out2.strip():
|
|
259
|
+
report("PASS", "git 身份", f"{out.strip()} <{out2.strip()}>")
|
|
260
|
+
else:
|
|
261
|
+
report("WARN", "git 身份", "未配置 → git config --global user.name/user.email")
|
|
262
|
+
|
|
263
|
+
print()
|
|
264
|
+
print(f"汇总: PASS {PASS} / WARN {WARN} / FAIL {FAIL}")
|
|
265
|
+
# onboarding 漏斗遥测(M6):只报三级计数 + offline 标志,不报具体检查项结果
|
|
266
|
+
try:
|
|
267
|
+
import telemetry
|
|
268
|
+
telemetry.emit("check_env_done", offline=offline,
|
|
269
|
+
pass_n=PASS, warn_n=WARN, fail_n=FAIL)
|
|
270
|
+
except Exception:
|
|
271
|
+
pass
|
|
272
|
+
if FAIL:
|
|
273
|
+
print("先解决 FAIL 项(按各条提示),WARN 可带病运行。")
|
|
274
|
+
return 1
|
|
275
|
+
print("就绪。下一步: 编辑 config/inventory.yaml 指向你自己的服务器/板卡 → ohos-devloop apply examples/smoke.yaml run")
|
|
276
|
+
return 0
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
if __name__ == "__main__":
|
|
280
|
+
try:
|
|
281
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
282
|
+
except Exception:
|
|
283
|
+
pass
|
|
284
|
+
sys.exit(main())
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""远端 code-worker 任务契约生命周期(v3,本机 = 设备侧驱动方)。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
ohos-devloop collab new <task-id> # 建任务(本地 + 服务器目录);repo 自动回填工作区池
|
|
7
|
+
ohos-devloop collab push <task-id> # 推 task.md + context/
|
|
8
|
+
ohos-devloop collab run <task-id> [model] # 异步调服务器 code-worker(默认 bigmodel/glm-5.3)
|
|
9
|
+
ohos-devloop collab status <task-id> # 运行状态(.running/.exit 标记 + status.json 摘要 + 日志尾)
|
|
10
|
+
ohos-devloop collab log <task-id> # 最近一次运行日志尾部
|
|
11
|
+
ohos-devloop collab fetch <task-id> # 取 report.md / status.json / artifacts.txt
|
|
12
|
+
ohos-devloop collab artifacts <task-id> # 取产物目录(TDD stripped 二进制等)
|
|
13
|
+
ohos-devloop collab close <task-id> # 设备侧验证通过:记 device_verified + 释放工作区 + 自动汇编报告 draft
|
|
14
|
+
ohos-devloop collab stats # 收敛遥测:任务迭代轮数/结果分布(ROADMAP M3)
|
|
15
|
+
|
|
16
|
+
架构(2026-11-09 回切):改码主通道 = 服务器 code-worker(opencode,代码与构建同机);
|
|
17
|
+
本机主 Agent 负责取证/推板/板端 TDD/判定/PR 操作,W: SSHFS 降级为应急通道。
|
|
18
|
+
run 是异步的(服务器 nohup,断 ssh 不死),用 status/log 轮询。
|
|
19
|
+
服务器取自 config/inventory.yaml 首个 servers 条目(COLLAB_SERVER/COLLAB_PORT 环境变量可覆盖)。
|
|
20
|
+
协议细节见 RUNBOOK「远端 code-worker 任务契约」。
|
|
21
|
+
"""
|
|
22
|
+
import argparse
|
|
23
|
+
import datetime
|
|
24
|
+
import json
|
|
25
|
+
import os
|
|
26
|
+
import re
|
|
27
|
+
import shlex
|
|
28
|
+
import subprocess
|
|
29
|
+
import sys
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
import yaml
|
|
33
|
+
|
|
34
|
+
import wsroot # 工作区解析(M6):任务留档/inventory/pool 归工作区,模板与工具脚本归正本仓
|
|
35
|
+
|
|
36
|
+
BASE = wsroot.PKG # 工具正本仓(包资产)
|
|
37
|
+
TID_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$") # task-id 白名单,防 shell 注入
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def stage_root() -> Path:
|
|
41
|
+
"""任务留档根(写锚点):调用点经 require_ws() 求值,绝不 import 期——
|
|
42
|
+
安装态无工作区时 WorkspaceRequiredError 引导 init(02-02 惰性锚点模式)。"""
|
|
43
|
+
return wsroot.require_ws() / "tasks"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def die(msg: str) -> int:
|
|
47
|
+
print(f"ERROR: {msg}", file=sys.stderr)
|
|
48
|
+
return 2
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def sh(args: list[str], timeout: int | None = None) -> tuple[int, str, str]:
|
|
52
|
+
"""list 参数起子进程(不做 shell 字符串拼接,绕开 Git Bash 引号/路径转换问题)。"""
|
|
53
|
+
try:
|
|
54
|
+
p = subprocess.run(args, capture_output=True, text=True, encoding="utf-8",
|
|
55
|
+
errors="replace", timeout=timeout)
|
|
56
|
+
return p.returncode, p.stdout or "", p.stderr or ""
|
|
57
|
+
except subprocess.TimeoutExpired:
|
|
58
|
+
return 124, "", f"timeout after {timeout}s"
|
|
59
|
+
except FileNotFoundError as e:
|
|
60
|
+
return 127, "", str(e)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def server() -> tuple[str, str]:
|
|
64
|
+
"""(user@host, port)。环境变量优先,缺省读 inventory.yaml 首个 servers 条目。"""
|
|
65
|
+
target = os.environ.get("COLLAB_SERVER")
|
|
66
|
+
port = os.environ.get("COLLAB_PORT")
|
|
67
|
+
if not target or not port:
|
|
68
|
+
inv = yaml.safe_load(wsroot.inventory_path().read_text(encoding="utf-8"))
|
|
69
|
+
s = next(iter((inv.get("servers") or {}).values()), {})
|
|
70
|
+
target = target or f"{s.get('user')}@{s.get('host')}"
|
|
71
|
+
port = port or str(s.get("port", 22))
|
|
72
|
+
return target, port
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def ssh(cmd: str, timeout: int = 60) -> tuple[int, str]:
|
|
76
|
+
"""返回 (rc, 合并后的输出)。"""
|
|
77
|
+
tgt, port = server()
|
|
78
|
+
rc, out, err = sh(["ssh", "-o", "ConnectTimeout=15", "-p", port, tgt, cmd], timeout=timeout)
|
|
79
|
+
merged = out + (f"\n[stderr] {err}" if err.strip() else "")
|
|
80
|
+
return rc, merged
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def scp(args: list[str], ignore_fail: bool = False) -> bool:
|
|
84
|
+
tgt, port = server()
|
|
85
|
+
rc, _, err = sh(["scp", "-o", "ConnectTimeout=15", "-P", port, *args], timeout=300)
|
|
86
|
+
ok = rc == 0
|
|
87
|
+
if not ok and not ignore_fail:
|
|
88
|
+
print(err.strip()[:400], file=sys.stderr)
|
|
89
|
+
return ok
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def pool_repo_for(tid: str) -> str:
|
|
93
|
+
"""工作区池里 task==tid 的行 → 服务器 code 根路径;没有则空串
|
|
94
|
+
(读语义 None 守卫:安装态无工作区等价「无池登记」)。"""
|
|
95
|
+
ws = wsroot.ws_root()
|
|
96
|
+
pool = (ws / "workspace_pool.yaml") if ws else None
|
|
97
|
+
if pool is None or not pool.is_file():
|
|
98
|
+
return ""
|
|
99
|
+
data = yaml.safe_load(pool.read_text(encoding="utf-8")) or {}
|
|
100
|
+
for row in data.get("workspaces") or []:
|
|
101
|
+
if row.get("task") == tid:
|
|
102
|
+
return str(row.get("path", "")).strip()
|
|
103
|
+
return ""
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def main() -> int:
|
|
107
|
+
ap = argparse.ArgumentParser(description="远端 code-worker 任务契约生命周期")
|
|
108
|
+
sub = ap.add_subparsers(dest="cmd", required=True)
|
|
109
|
+
for name in ("new", "push", "run", "status", "log", "fetch", "artifacts", "close"):
|
|
110
|
+
sp = sub.add_parser(name)
|
|
111
|
+
sp.add_argument("tid")
|
|
112
|
+
if name == "run":
|
|
113
|
+
sp.add_argument("model", nargs="?", default="")
|
|
114
|
+
sub.add_parser("stats", help="收敛遥测:任务迭代轮数/结果分布汇总")
|
|
115
|
+
args = ap.parse_args()
|
|
116
|
+
if args.cmd == "stats":
|
|
117
|
+
return cmd_stats()
|
|
118
|
+
tid = args.tid
|
|
119
|
+
if not TID_RE.match(tid):
|
|
120
|
+
return die(f"非法 task-id: {tid!r}(只允许字母数字._-)")
|
|
121
|
+
T = stage_root() / tid
|
|
122
|
+
tgt, port = server()
|
|
123
|
+
|
|
124
|
+
if args.cmd == "new":
|
|
125
|
+
(T / "context").mkdir(parents=True, exist_ok=True)
|
|
126
|
+
body = wsroot.config_path("task-template.md").read_text(encoding="utf-8").replace("TASK_ID", tid)
|
|
127
|
+
repo = pool_repo_for(tid)
|
|
128
|
+
body = body.replace("REPO_PATH_PLACEHOLDER", repo or "/srv/workspace/<工作区>/code(手工填写,或先 flash.py sync-repo 领取工作区)")
|
|
129
|
+
T.joinpath("task.md").write_text(body, encoding="utf-8", newline="\n")
|
|
130
|
+
rc, out = ssh(f"mkdir -p ~/collab/tasks/{shlex.quote(tid)}/context")
|
|
131
|
+
if rc != 0:
|
|
132
|
+
return die(f"服务器建目录失败(ssh 不通?)\n{out}")
|
|
133
|
+
print(f"created: {T/'task.md'} (edit goal/现象/证据, put logs in {T/'context'}/, then: ohos-devloop collab push {tid})")
|
|
134
|
+
if repo:
|
|
135
|
+
print(f"repo 已从工作区池回填: {repo}")
|
|
136
|
+
|
|
137
|
+
elif args.cmd == "push":
|
|
138
|
+
if not (T / "task.md").is_file():
|
|
139
|
+
return die(f"no {T/'task.md'} — run: ohos-devloop collab new {tid}")
|
|
140
|
+
if not scp([str(T / "task.md"), f"{tgt}:~/collab/tasks/{tid}/task.md"]):
|
|
141
|
+
return die("push task.md 失败")
|
|
142
|
+
if (T / "context").is_dir() and any((T / "context").iterdir()):
|
|
143
|
+
if not scp(["-r", str(T / "context"), f"{tgt}:~/collab/tasks/{tid}/"]):
|
|
144
|
+
return die("push context/ 失败")
|
|
145
|
+
print("pushed task.md + context/")
|
|
146
|
+
|
|
147
|
+
elif args.cmd == "run":
|
|
148
|
+
model = args.model or "bigmodel/glm-5.3"
|
|
149
|
+
if not (T / "task.md").is_file():
|
|
150
|
+
return die(f"no {T/'task.md'} — run: ohos-devloop collab new {tid}")
|
|
151
|
+
# 异步:服务器 nohup 整体 detach(断 ssh/本机休眠不陪葬);.running/.exit 标记供 status 轮询
|
|
152
|
+
inner = (f"bash ~/collab/bin/agent-run-v3.sh {shlex.quote(tid)} {shlex.quote(model)} "
|
|
153
|
+
f"> ~/collab/runs/{tid}/launch.log 2>&1; "
|
|
154
|
+
f"echo $? > ~/collab/runs/{tid}/.exit; rm -f ~/collab/runs/{tid}/.running")
|
|
155
|
+
# mkdir/标记同步做完,只把 nohup 段后台化——否则 & 会把整条链丢后台,ssh 立即退出有竞态
|
|
156
|
+
cmd = (f"mkdir -p ~/collab/runs/{tid} && "
|
|
157
|
+
f"rm -f ~/collab/runs/{tid}/.running ~/collab/runs/{tid}/.exit && "
|
|
158
|
+
f"date +'%F %T' > ~/collab/runs/{tid}/.running && "
|
|
159
|
+
f"{{ nohup bash -c {shlex.quote(inner)} >/dev/null 2>&1 & }}; echo LAUNCHED pid=$!")
|
|
160
|
+
rc, out = ssh(cmd)
|
|
161
|
+
if rc != 0:
|
|
162
|
+
return die(f"启动失败\n{out}")
|
|
163
|
+
print((out.strip() or "LAUNCHED").splitlines()[-1])
|
|
164
|
+
print(f"轮询: ohos-devloop collab status {tid} 日志: ohos-devloop collab log {tid}")
|
|
165
|
+
|
|
166
|
+
elif args.cmd == "status":
|
|
167
|
+
cmd = (f"if [ ! -d ~/collab/runs/{tid} ]; then echo NO_RUN_YET; exit 0; fi; "
|
|
168
|
+
f"cd ~/collab/runs/{tid}; "
|
|
169
|
+
f"if [ -f .running ]; then echo STATE=RUNNING since $(cat .running); "
|
|
170
|
+
f"elif [ -f .exit ]; then echo STATE=FINISHED exit=$(cat .exit); "
|
|
171
|
+
f"else echo STATE=IDLE; fi; "
|
|
172
|
+
f"latest=$(ls -t *.log 2>/dev/null | head -1); "
|
|
173
|
+
f"[ -n \"$latest\" ] && echo \"--- log tail: $latest ---\" && tail -6 \"$latest\"; "
|
|
174
|
+
f"echo '--- status.json ---'; cat ~/collab/tasks/{tid}/status.json 2>/dev/null || echo '(尚未产出)'")
|
|
175
|
+
rc, out = ssh(cmd)
|
|
176
|
+
print(out)
|
|
177
|
+
if rc != 0:
|
|
178
|
+
return die("status 查询失败(ssh 不通?)")
|
|
179
|
+
|
|
180
|
+
elif args.cmd == "log":
|
|
181
|
+
rc, out = ssh(f"ls -t ~/collab/runs/{tid}/*.log 2>/dev/null | head -1 | xargs -r tail -80")
|
|
182
|
+
print(out)
|
|
183
|
+
if rc != 0:
|
|
184
|
+
return die("log 查询失败")
|
|
185
|
+
|
|
186
|
+
elif args.cmd == "fetch":
|
|
187
|
+
T.mkdir(parents=True, exist_ok=True)
|
|
188
|
+
for f, must in (("report.md", False), ("report.prev.md", False),
|
|
189
|
+
("status.json", True), ("artifacts.txt", False)):
|
|
190
|
+
if scp([f"{tgt}:~/collab/tasks/{tid}/{f}", str(T / f)], ignore_fail=not must) and (T / f).is_file():
|
|
191
|
+
if must:
|
|
192
|
+
print((T / "status.json").read_text(encoding="utf-8"))
|
|
193
|
+
elif f == "artifacts.txt":
|
|
194
|
+
print(f"artifacts.txt -> {T/'artifacts.txt'}(apply.py --params artifacts= 可直接引用)")
|
|
195
|
+
elif must:
|
|
196
|
+
return die(f"取 {f} 失败(任务还没 run 过?)")
|
|
197
|
+
|
|
198
|
+
elif args.cmd == "artifacts":
|
|
199
|
+
(T / "artifacts").mkdir(parents=True, exist_ok=True)
|
|
200
|
+
if not scp(["-r", f"{tgt}:~/collab/tasks/{tid}/artifacts", str(T)]):
|
|
201
|
+
return die(f"取 artifacts 失败(服务器上无产物?)")
|
|
202
|
+
print(f"artifacts -> {T/'artifacts'}")
|
|
203
|
+
|
|
204
|
+
elif args.cmd == "close":
|
|
205
|
+
code = (
|
|
206
|
+
"import json, datetime, os\n"
|
|
207
|
+
f"p = os.path.expanduser('~/collab/tasks/{tid}/status.json')\n"
|
|
208
|
+
"d = json.load(open(p))\n"
|
|
209
|
+
'd["device_verified"] = True\n'
|
|
210
|
+
'd["verified_at"] = datetime.datetime.now().astimezone().isoformat(timespec="seconds")\n'
|
|
211
|
+
'json.dump(d, open(p, "w"), ensure_ascii=False, indent=2)\n'
|
|
212
|
+
'print("closed:", json.dumps(d, ensure_ascii=False))\n'
|
|
213
|
+
)
|
|
214
|
+
rc, out = ssh(f"python3 -c {shlex.quote(code)}")
|
|
215
|
+
if rc != 0:
|
|
216
|
+
return die(f"close 失败(status.json 不存在?先 run/fetch)\n{out}")
|
|
217
|
+
print(out.strip())
|
|
218
|
+
with open(stage_root() / "closed.log", "a", encoding="utf-8", newline="\n") as f:
|
|
219
|
+
f.write(f"closed {tid} {datetime.datetime.now():%F %T}\n")
|
|
220
|
+
# 收敛遥测(M6):worker 侧数据经 status.json 由本机带回上报,worker 不直连外网
|
|
221
|
+
try:
|
|
222
|
+
import telemetry
|
|
223
|
+
sj = T / "status.json"
|
|
224
|
+
s = json.loads(sj.read_text(encoding="utf-8")) if sj.is_file() else {}
|
|
225
|
+
telemetry.emit("task_closed", task=tid, result=s.get("status") or "?",
|
|
226
|
+
iterations=s.get("iterations"))
|
|
227
|
+
except Exception:
|
|
228
|
+
pass
|
|
229
|
+
# 任务完成即释放其占用的大仓工作区(登记见 workspace_pool.yaml)
|
|
230
|
+
try:
|
|
231
|
+
from flash import load_pool, save_pool
|
|
232
|
+
pool = load_pool()
|
|
233
|
+
freed = [r for r in pool if r.get("task") == tid]
|
|
234
|
+
for r in freed:
|
|
235
|
+
r["task"] = ""
|
|
236
|
+
print(f"released workspace: {r['path']}")
|
|
237
|
+
if freed:
|
|
238
|
+
save_pool(pool)
|
|
239
|
+
else:
|
|
240
|
+
print("(该任务未占用工作区,跳过释放)")
|
|
241
|
+
except OSError as e:
|
|
242
|
+
print(f"WARN: 工作区释放失败(可手工 ohos-devloop flash pool release): {e}", file=sys.stderr)
|
|
243
|
+
# 交付三件套之一:报告 draft 自动汇编(非致命;runs.txt 缺失时验证节留 TODO)
|
|
244
|
+
rc2, out2, _ = sh([sys.executable, str(BASE / "scripts" / "report_draft.py"), tid])
|
|
245
|
+
print((out2.strip() or "(report-draft 跳过)").splitlines()[-1] if rc2 == 0
|
|
246
|
+
else f"WARN: report-draft 汇编失败(可手工 ohos-devloop report {tid})",
|
|
247
|
+
file=sys.stdout if rc2 == 0 else sys.stderr)
|
|
248
|
+
|
|
249
|
+
return 0
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def cmd_stats() -> int:
|
|
253
|
+
"""收敛遥测(ROADMAP M3):closed.log + 各任务 status.json → 迭代轮数/结果分布。"""
|
|
254
|
+
closed: dict[str, str] = {}
|
|
255
|
+
legacy = 0
|
|
256
|
+
cl = stage_root() / "closed.log"
|
|
257
|
+
if cl.is_file():
|
|
258
|
+
for ln in cl.read_text(encoding="utf-8").splitlines():
|
|
259
|
+
m = re.match(r"^closed\s+(\S+)\s+(\d{4}-\d{2}-\d{2})", ln)
|
|
260
|
+
if m and not re.match(r"^\d{4}-\d{2}-\d{2}$", m.group(1)):
|
|
261
|
+
closed[m.group(1)] = m.group(2)
|
|
262
|
+
elif ln.strip():
|
|
263
|
+
legacy += 1
|
|
264
|
+
rows, dist, iters = [], {}, []
|
|
265
|
+
for d in sorted(T for T in stage_root().iterdir() if T.is_dir()):
|
|
266
|
+
sj = d / "status.json"
|
|
267
|
+
if not sj.is_file():
|
|
268
|
+
continue
|
|
269
|
+
try:
|
|
270
|
+
s = json.loads(sj.read_text(encoding="utf-8"))
|
|
271
|
+
except ValueError:
|
|
272
|
+
continue
|
|
273
|
+
st = str(s.get("status") or "?")
|
|
274
|
+
dist[st] = dist.get(st, 0) + 1
|
|
275
|
+
it = s.get("iterations")
|
|
276
|
+
if isinstance(it, (int, float)):
|
|
277
|
+
iters.append(it)
|
|
278
|
+
rows.append((d.name, st, it, "closed" if d.name in closed else ""))
|
|
279
|
+
print(f"任务留档(含 status.json):{len(rows)} 个|closed.log:{len(closed)} 个(另有 {legacy} 条旧格式无 tid)")
|
|
280
|
+
if iters:
|
|
281
|
+
print(f"迭代轮数:均值 {sum(iters)/len(iters):.1f}|最大 {max(iters)}|分布 "
|
|
282
|
+
+ " ".join(f"{k}轮×{v}" for k, v in sorted(
|
|
283
|
+
{n: iters.count(n) for n in set(iters)}.items())))
|
|
284
|
+
print("结果分布:" + "|".join(f"{k} {v}" for k, v in sorted(dist.items())))
|
|
285
|
+
print(f"{'task-id':<44}{'status':<10}{'iter':>5} closed")
|
|
286
|
+
for name, st, it, cl_flag in rows:
|
|
287
|
+
print(f"{name:<44}{st:<10}{str(it if it is not None else '-'):>5} {cl_flag}")
|
|
288
|
+
print("反哺:连续 BLOCKED/NEED_INFO 多的任务 → 检查 task.md 证据是否充足;迭代 >3 的 → worker prompt 是否欠约束")
|
|
289
|
+
return 0
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
if __name__ == "__main__":
|
|
293
|
+
try:
|
|
294
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
295
|
+
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
|
|
296
|
+
except Exception:
|
|
297
|
+
pass
|
|
298
|
+
try:
|
|
299
|
+
rc = main()
|
|
300
|
+
except wsroot.WorkspaceRequiredError as e: # 安装态无工作区:引导 init,rc==2(02-02 错误出口契约)
|
|
301
|
+
print(f"ERROR: {e}", file=sys.stderr)
|
|
302
|
+
rc = 2
|
|
303
|
+
sys.exit(rc)
|