Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6f958ded28 | |||
| 3bc15d1241 | |||
| c67079fb9e |
@@ -1,4 +1,4 @@
|
|||||||
# ── 运行模式 / adapter 档位(见 references/ADAPTER.md)──
|
# ── 运行模式 / adapter 档位(见 development/ADAPTER.md)──
|
||||||
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
OPENCLAW_TEST_TARGET=mock # mock | simulator_rpa | real_api | real_rpa
|
||||||
|
|
||||||
# ── 目标系统 ──
|
# ── 目标系统 ──
|
||||||
@@ -12,7 +12,7 @@ OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
|||||||
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
# ── 存证 ──
|
# ── 存证 ──
|
||||||
OPENCLAW_RECORD_VIDEO=1
|
OPENCLAW_RECORD_VIDEO=0 # 0=默认不录屏;1=启用录屏+字幕+旁白+背景音+MP4
|
||||||
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
# ── 节流 / 超时 ──
|
# ── 节流 / 超时 ──
|
||||||
|
|||||||
8
SKILL.md
8
SKILL.md
@@ -1,12 +1,12 @@
|
|||||||
---
|
---
|
||||||
name: 技能开发模板(通用业务版)
|
name: 技能开发模板(通用业务版)
|
||||||
description: "OpenClaw 通用业务技能开发模板,供复制后定制新业务 skill。定制步骤见 development/DEVELOPMENT.md。"
|
description: "OpenClaw 通用业务技能开发模板,供复制后定制新业务 skill。定制步骤见 development/DEVELOPMENT.md。"
|
||||||
version: 1.0.32
|
version: 1.0.38
|
||||||
author: 深圳匠厂科技有限公司
|
author: 深圳匠厂科技有限公司
|
||||||
metadata:
|
metadata:
|
||||||
openclaw:
|
openclaw:
|
||||||
slug: your-skill-slug
|
slug: your-skill-slug
|
||||||
platform_kit_min_version: "1.0.17"
|
platform_kit_min_version: "1.2.0"
|
||||||
emoji: "📦"
|
emoji: "📦"
|
||||||
category: "通用"
|
category: "通用"
|
||||||
developer_ids:
|
developer_ids:
|
||||||
@@ -70,10 +70,10 @@ python {baseDir}/scripts/main.py version
|
|||||||
## 运行依赖
|
## 运行依赖
|
||||||
|
|
||||||
- Python 运行环境由匠厂宿主注入**共享 runtime**:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
- Python 运行环境由匠厂宿主注入**共享 runtime**:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
- 公共能力来自共享 runtime 安装的 `jiangchang-platform-kit>=1.0.17`(`jiangchang_skill_core` 包);**不要 vendor** `scripts/jiangchang_skill_core/`,新技能不得在仓库内保留该目录副本。
|
- 公共能力来自共享 runtime 安装的 `jiangchang-platform-kit>=1.2.0`(`jiangchang_skill_core` 包);**不要 vendor** `scripts/jiangchang_skill_core/`,新技能不得在仓库内保留该目录副本。
|
||||||
- config、logging、runtime_env、rpa、media-assets、video_session、runtime_diagnostics 等均从共享 venv 的 `jiangchang_skill_core` import,而非技能目录副本。
|
- config、logging、runtime_env、rpa、media-assets、video_session、runtime_diagnostics 等均从共享 venv 的 `jiangchang_skill_core` import,而非技能目录副本。
|
||||||
- 根目录 `requirements.txt` **只声明技能特有** Python 三方依赖;`jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
- 根目录 `requirements.txt` **只声明技能特有** Python 三方依赖;`jiangchang-platform-kit`、`playwright` 等公共能力由宿主共享 runtime 提供,**不要**写入技能 requirements。
|
||||||
- `metadata.openclaw.platform_kit_min_version`(当前 `1.0.17`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是**技能 pip 依赖声明。
|
- `metadata.openclaw.platform_kit_min_version`(当前 `1.2.0`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是**技能 pip 依赖声明。
|
||||||
- Skill 代码**不要**自行 `pip install`;系统级依赖(如 VC++ Runtime)仅在 `health` / preflight 中提示用户安装。
|
- Skill 代码**不要**自行 `pip install`;系统级依赖(如 VC++ Runtime)仅在 `health` / preflight 中提示用户安装。
|
||||||
- `health` 使用 `collect_runtime_diagnostics` 输出统一 runtime 诊断(只读,不下载/修复 media-assets)。
|
- `health` 使用 `collect_runtime_diagnostics` 输出统一 runtime 诊断(只读,不下载/修复 media-assets)。
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ OPENCLAW_BROWSER_HEADLESS=0 # 0=有头(默认) 1=无头(CI)
|
|||||||
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
OPENCLAW_PLAYWRIGHT_STEALTH=1 # 1=开启反检测指纹(默认)
|
||||||
|
|
||||||
# ── 录屏与失败存证 ──
|
# ── 录屏与失败存证 ──
|
||||||
OPENCLAW_RECORD_VIDEO=1 # 1=录屏+字幕+旁白+背景音+MP4(RPA 默认开,见 RPA.md)
|
OPENCLAW_RECORD_VIDEO=0 # 0=默认不录屏;1=启用录屏+字幕+旁白+背景音+MP4(见 RPA.md)
|
||||||
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
OPENCLAW_ARTIFACTS_ON_FAILURE=1 # 1=失败截图(默认)
|
||||||
|
|
||||||
# ── 节流 / 超时 ──
|
# ── 节流 / 超时 ──
|
||||||
@@ -42,6 +42,13 @@ HUMAN_WAIT_TIMEOUT=180 # 滑块/验证码/2FA 等人工超时(
|
|||||||
|
|
||||||
**业务专属配置必须带技能前缀**(如 `DEMO_XXX`、`MY_SKILL_XXX`),**不要污染全局命名空间**(禁止 `SCRAPE_1688_*`、`RECEIVE_ORDER_*` 等跨技能前缀写入模板)。
|
**业务专属配置必须带技能前缀**(如 `DEMO_XXX`、`MY_SKILL_XXX`),**不要污染全局命名空间**(禁止 `SCRAPE_1688_*`、`RECEIVE_ORDER_*` 等跨技能前缀写入模板)。
|
||||||
|
|
||||||
|
### 录屏开关(`OPENCLAW_RECORD_VIDEO`)
|
||||||
|
|
||||||
|
- **模板默认 `OPENCLAW_RECORD_VIDEO=0`**,降低本地开发 / 调试成本(不启 ffmpeg、不生成 MP4)。
|
||||||
|
- 需要**生产留证、合规记录、客户现场复现或排障**时,可在用户 `.env` 或进程环境变量中设为 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
|
- **默认关闭不代表可以删除录屏接入**:RPA / 长任务代码仍须保留 `RpaVideoSession`、`video.add_step` 与 video artifact 写入 `result_summary`(见 `RPA.md` §5.3)。
|
||||||
|
- **已落盘的用户 `.env` 不会被模板整体覆盖**;老用户保留原值,技能升级时仅通过 `merge_missing_env_keys` **追加** `.env.example` 中新增的 key。
|
||||||
|
|
||||||
## 🚫 红线:敏感信息不进 `.env`
|
## 🚫 红线:敏感信息不进 `.env`
|
||||||
|
|
||||||
`.env` 只放**非敏感运行参数**(模式、地址、开关、超时、账号标识)。密码、密钥、动态口令、token——
|
`.env` 只放**非敏感运行参数**(模式、地址、开关、超时、账号标识)。密码、密钥、动态口令、token——
|
||||||
|
|||||||
@@ -152,11 +152,13 @@ scripts/
|
|||||||
- `service/`
|
- `service/`
|
||||||
作用:核心业务逻辑
|
作用:核心业务逻辑
|
||||||
比如任务编排、调用兄弟技能、外部 API、可选浏览器自动化
|
比如任务编排、调用兄弟技能、外部 API、可选浏览器自动化
|
||||||
|
**负责业务关键节点日志**(`task_start` / 外部调用 / `task_failed` 等,见 [`LOGGING.md`](LOGGING.md))
|
||||||
|
|
||||||
- `util/`
|
- `util/`
|
||||||
作用:常量、日志、路径、时间工具、通用帮助函数
|
作用:常量、日志、路径、时间工具、通用帮助函数
|
||||||
|
`util/logging_config.py` 是 `jiangchang_skill_core.unified_logging` 的**薄封装**,业务代码应通过它获取 logger
|
||||||
|
|
||||||
公共能力(config、logging、runtime_env、rpa、media_assets、video_session、runtime_diagnostics)从共享 runtime 的 `jiangchang-platform-kit>=1.0.17` import,**不得**在 `scripts/` 下保留 `jiangchang_skill_core/` 副本。
|
公共能力(config、logging、runtime_env、rpa、media_assets、video_session、runtime_diagnostics、activity/SRCP)从共享 runtime 的 `jiangchang-platform-kit>=1.2.0` import,**不得**在 `scripts/` 下保留 `jiangchang_skill_core/` 副本。
|
||||||
|
|
||||||
## 3.2 开发 RPA 类 skill
|
## 3.2 开发 RPA 类 skill
|
||||||
|
|
||||||
@@ -193,7 +195,7 @@ scripts/
|
|||||||
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能**特有** Python 三方依赖。
|
技能根目录的 `requirements.txt` 是**标准文件**,用于声明本技能**特有** Python 三方依赖。
|
||||||
|
|
||||||
- **公共依赖**(`jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等)由**宿主共享 runtime** 提供,**不要**写入技能 `requirements.txt`。
|
- **公共依赖**(`jiangchang-platform-kit`、`playwright`、config、runtime diagnostics、RPA 公共能力等)由**宿主共享 runtime** 提供,**不要**写入技能 `requirements.txt`。
|
||||||
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.0.17`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。
|
- `SKILL.md` 的 `metadata.openclaw.platform_kit_min_version`(当前 `1.2.0`)是运行契约/兼容性声明,供宿主安装与启用时校验,**不是** pip 依赖声明。
|
||||||
- 匠厂宿主安装/更新技能后,会将技能 `requirements.txt` 安装到共享 venv:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
- 匠厂宿主安装/更新技能后,会将技能 `requirements.txt` 安装到共享 venv:`{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。
|
||||||
- **不要**在业务代码中 `subprocess` / `pip install`;缺依赖由 `health` 报错,由宿主负责安装。
|
- **不要**在业务代码中 `subprocess` / `pip install`;缺依赖由 `health` 报错,由宿主负责安装。
|
||||||
- **版本约束尽量收窄**,降低多技能共享 venv 时的冲突风险。推荐范围写法:
|
- **版本约束尽量收窄**,降低多技能共享 venv 时的冲突风险。推荐范围写法:
|
||||||
@@ -421,6 +423,7 @@ metadata:
|
|||||||
- `REQUIREMENTS.md` — 需求与验收
|
- `REQUIREMENTS.md` — 需求与验收
|
||||||
- `DEVELOPMENT.md` — 开发教程(本文档)
|
- `DEVELOPMENT.md` — 开发教程(本文档)
|
||||||
- `TESTING.md` — 测试分层与隔离(详见 [`TESTING.md`](TESTING.md))
|
- `TESTING.md` — 测试分层与隔离(详见 [`TESTING.md`](TESTING.md))
|
||||||
|
- `LOGGING.md` — 日志分层、必打节点、敏感信息红线
|
||||||
- `ADAPTER.md`、`RPA.md`、`CONFIG.md`、`RUNTIME.md` — 技术规范
|
- `ADAPTER.md`、`RPA.md`、`CONFIG.md`、`RUNTIME.md` — 技术规范
|
||||||
|
|
||||||
## 8. `assets/` 应该放什么
|
## 8. `assets/` 应该放什么
|
||||||
@@ -478,6 +481,16 @@ metadata:
|
|||||||
- `entitlement_service.py`
|
- `entitlement_service.py`
|
||||||
放鉴权逻辑
|
放鉴权逻辑
|
||||||
|
|
||||||
|
### 日志要求(`cmd_run` / 核心业务)
|
||||||
|
|
||||||
|
`service` 层是**关键业务日志**的落点,须遵循 [`LOGGING.md`](LOGGING.md):
|
||||||
|
|
||||||
|
- `cmd_run` 及主任务入口必须:`log.info("task_start ...")`、`log.info("task_log_saved ...")`;失败路径必须 `log.exception("task_failed ...")`。
|
||||||
|
- 外部 API、兄弟技能、RPA 操作须记录**开始 / 结束 / 耗时 / 状态**(`external_call_start` / `external_call_done elapsed_ms=...`)。
|
||||||
|
- 长任务 / RPA 须配合 `activity.emit` 或 `RpaVideoSession.add_step` 输出用户可见进度;`cmd_run` 须 `job_context` + 各出口 `finish`(见 [`LOGGING.md`](LOGGING.md) §2.5)。
|
||||||
|
- 使用 `from util.logging_config import get_skill_logger`,**不要**用 `print` 替代 logger 做排障日志。
|
||||||
|
- **禁止**在日志中写入 password、token、cookie 等敏感明文(见 `LOGGING.md` §敏感信息红线)。
|
||||||
|
|
||||||
### 一个很重要的原则
|
### 一个很重要的原则
|
||||||
|
|
||||||
不要把所有逻辑都堆进一个文件。
|
不要把所有逻辑都堆进一个文件。
|
||||||
@@ -602,6 +615,14 @@ python scripts/main.py logs
|
|||||||
python scripts/main.py log-get 1
|
python scripts/main.py log-get 1
|
||||||
```
|
```
|
||||||
|
|
||||||
|
除 `health` / `version` / `logs` 外,执行一次 `run`(或你的主任务命令)后,应确认**统一日志文件**已生成并可检索:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log
|
||||||
|
```
|
||||||
|
|
||||||
|
其中应能看到 `cli_start`、`task_start` 等带 `trace_id` / `skill_slug` 的行。路径与分层说明见 [`LOGGING.md`](LOGGING.md)。
|
||||||
|
|
||||||
### 4. 最后再验证真实业务
|
### 4. 最后再验证真实业务
|
||||||
|
|
||||||
比如:
|
比如:
|
||||||
@@ -768,6 +789,8 @@ uses: client-jiangchang/jiangchang-platform-kit/.github/workflows/reusable-relea
|
|||||||
- [ ] `release.ps1` 存在(发布前会自动跑 `python tests/run_tests.py -v`;失败不得打 tag)
|
- [ ] `release.ps1` 存在(发布前会自动跑 `python tests/run_tests.py -v`;失败不得打 tag)
|
||||||
- [ ] `.github/workflows/release_skill.yaml` 存在
|
- [ ] `.github/workflows/release_skill.yaml` 存在
|
||||||
- [ ] `python tests/run_tests.py -v` 全部通过(含 `test_development_policy_guard.py`)
|
- [ ] `python tests/run_tests.py -v` 全部通过(含 `test_development_policy_guard.py`)
|
||||||
|
- [ ] 主任务入口(如 `cmd_run`)有关键日志:`task_start`、`task_log_saved`;失败路径使用 `logger.exception`
|
||||||
|
- [ ] 日志不泄露 password / token / cookie 等敏感明文(见 [`LOGGING.md`](LOGGING.md))
|
||||||
- [ ] 新增 hard 规范已写入 `development/POLICY_MATRIX.md` 并有对应自动检测
|
- [ ] 新增 hard 规范已写入 `development/POLICY_MATRIX.md` 并有对应自动检测
|
||||||
- [ ] `scripts/**/*.py` 单文件 **< 1000 行**(PyArmor 试用/免费模式限制;推荐 < 900 行)
|
- [ ] `scripts/**/*.py` 单文件 **< 1000 行**(PyArmor 试用/免费模式限制;推荐 < 900 行)
|
||||||
- [ ] 源码/文档/配置为 **UTF-8 without BOM**(Python 文件不得含 `U+FEFF`)
|
- [ ] 源码/文档/配置为 **UTF-8 without BOM**(Python 文件不得含 `U+FEFF`)
|
||||||
|
|||||||
309
development/LOGGING.md
Normal file
309
development/LOGGING.md
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
# 日志规范
|
||||||
|
|
||||||
|
本文件是 skill 日志规范的**权威入口**,面向技术人员与 AI 编程代理。涉及长任务、RPA、外部系统对接的技能,**必须**在实现前通读本文,并在 code review 时对照「必打日志节点清单」人工检查。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. 为什么日志是必需的
|
||||||
|
|
||||||
|
OpenClaw 技能常在客户电脑上异步执行,且大量依赖 RPA、浏览器、ERP、微信等外部系统。这类任务具备以下特征:
|
||||||
|
|
||||||
|
- **耗时长**:单次 run 可能数分钟甚至更久,stdout 长时间无输出不代表卡死。
|
||||||
|
- **失败点多**:网络抖动、页面改版、登录态失效、验证码、U 盾等都会导致中途失败。
|
||||||
|
- **环境差异大**:客户 OS、浏览器版本、代理、杀毒软件、屏幕分辨率各不相同,无法复现开发机行为。
|
||||||
|
- **必须离线排查**:技术支持往往无法远程登录客户电脑,只能依赖本地落盘的日志与 artifacts。
|
||||||
|
|
||||||
|
日志用于定位:
|
||||||
|
|
||||||
|
| 维度 | 说明 |
|
||||||
|
|------|------|
|
||||||
|
| 失败阶段 | 任务停在鉴权、取号、打开页面、提交还是等待结果 |
|
||||||
|
| 输入范围 | `task_type`、`target_id`、`input_id`、`batch_id` |
|
||||||
|
| 外部系统状态 | HTTP 状态、RPA 步骤、兄弟技能返回码 |
|
||||||
|
| 耗时 | `elapsed_ms`、批量 `current/total` |
|
||||||
|
| 重试与人工介入 | 第几次重试、是否等待验证码 / U 盾 |
|
||||||
|
| 存证路径 | 截图、录屏、`video_log`、失败 artifacts |
|
||||||
|
|
||||||
|
**没有关键节点日志 = 客户现场不可排障。** 不要把 `print` 当作日志;stdout 面向用户与 Agent,文件日志与 Run Journal 面向运维。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2. 日志分层
|
||||||
|
|
||||||
|
技能日志分四层,职责互不替代:
|
||||||
|
|
||||||
|
### 2.1 统一文件日志(开发 / 运维排查)
|
||||||
|
|
||||||
|
**来源**:`jiangchang_skill_core.unified_logging`(技能内通过 `util.logging_config` 薄封装 import)。
|
||||||
|
|
||||||
|
| API | 用途 |
|
||||||
|
|-----|------|
|
||||||
|
| `setup_skill_logging` | CLI 入口启动时初始化(`cli/app.py` 的 `main()`) |
|
||||||
|
| `get_skill_logger` | 获取带 `skill_slug` 的 logger |
|
||||||
|
| `subprocess_env_with_trace` | 子进程 / 兄弟技能调用时传递 `trace_id` |
|
||||||
|
|
||||||
|
**路径**:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log
|
||||||
|
```
|
||||||
|
|
||||||
|
**特征**:每行带 `trace_id`、`skill_slug`;适合 grep、离线打包发给技术支持。
|
||||||
|
|
||||||
|
### 2.2 Activity / Run Journal(宿主 UI 与用户可见进度)
|
||||||
|
|
||||||
|
**来源**:`jiangchang_skill_core.activity`
|
||||||
|
|
||||||
|
| API | 用途 |
|
||||||
|
|-----|------|
|
||||||
|
| `emit` | 推送用户可读进度(不写 stdout);进入步骤前自动步骤闸门(SRCP) |
|
||||||
|
| `step` | 结构化步骤 |
|
||||||
|
| `finish` | 任务结束;**唯一**写单行 result JSON 到 stdout 的场景 |
|
||||||
|
| `job_context` | 包裹 `cmd_run`;未捕获异常 / `JobStopped` 时自动 `finish` |
|
||||||
|
| `rpa_step` | RPA 专用步骤:闸门 + ▶/✓ emit |
|
||||||
|
| `interruptible_sleep` | RPA 等待(替代裸 `asyncio.sleep`),可暂停/停止 |
|
||||||
|
| `checkpoint` | 长循环内 consult control(一般由 kit 自动调用) |
|
||||||
|
|
||||||
|
**路径**:
|
||||||
|
|
||||||
|
```text
|
||||||
|
{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl
|
||||||
|
```
|
||||||
|
|
||||||
|
**约定**:
|
||||||
|
|
||||||
|
- `emit` **不写 stdout**;长任务应持续 emit,避免用户以为卡死。
|
||||||
|
- `finish` 才输出单行 result JSON 供宿主解析。
|
||||||
|
- RPA 类技能:`RpaVideoSession.add_step` 会自动同步 activity(无需重复手写每步 emit)。
|
||||||
|
- **禁止**自建 `scripts/util/progress.py` 或向 stdout 打印 `type:progress` JSON;进度只走 Run Journal。
|
||||||
|
- 宿主通过 `{job_id}.control.json` 下发暂停/继续/停止;技能侧由 platform-kit 在步骤闸门自动处理(见 §2.5)。
|
||||||
|
|
||||||
|
### 2.5 步骤控制(SRCP v1,platform-kit >= 1.2.0)
|
||||||
|
|
||||||
|
**权威行为定义在本文**;platform-kit README 为实现细节参考。
|
||||||
|
|
||||||
|
| 通道 | 方向 | 载体 |
|
||||||
|
|------|------|------|
|
||||||
|
| 进度 | 技能 → 宿主 | Run Journal(`emit` / `rpa_step` / `add_step`) |
|
||||||
|
| 结果 | 技能 → 宿主 | `finish()` → Journal 终态 + stdout 一行 result |
|
||||||
|
| 控制 | 宿主 → 技能 | `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.control.json` |
|
||||||
|
|
||||||
|
`control.json` 的 `command` 只认:`none` | `pause` | `resume` | `stop`。
|
||||||
|
|
||||||
|
技能作者三件事:
|
||||||
|
|
||||||
|
1. `cmd_run` 外包 `with job_context(skill=SKILL_SLUG):`
|
||||||
|
2. 关键步骤 `emit(...)` 或 `@rpa_step` / `video.add_step`
|
||||||
|
3. 每个出口 `finish(status=..., message=..., skill=SKILL_SLUG, **fields)` — **不要**手写多行 JSON 结果
|
||||||
|
|
||||||
|
RPA 等待用 `interruptible_sleep`,**不要**裸 `asyncio.sleep`(否则暂停响应滞后)。
|
||||||
|
|
||||||
|
暂停在**步骤边界**生效(当前 `@rpa_step` / `emit` 执行完后、下一步开始前),与影刀「当前指令完成后暂停」一致。
|
||||||
|
|
||||||
|
Journal 会写入 `type: lifecycle` 事件(`paused` / `resumed` / `stopping`),供宿主任务中心显示状态。
|
||||||
|
|
||||||
|
|
||||||
|
### 2.3 `task_logs`(任务结果审计)
|
||||||
|
|
||||||
|
**来源**:`db/task_logs_repository.save_task_log`
|
||||||
|
|
||||||
|
- 适合 `python scripts/main.py logs` / `log-get <id>` 查询。
|
||||||
|
- 记录任务最终状态、`result_summary`(可含 `video_path`、`video_log` 等)。
|
||||||
|
- 面向「这次任务成功还是失败、摘要是什么」,不是逐步 trace。
|
||||||
|
- **鉴权失败与未捕获异常也必须写入 `task_logs`**,保证 `logs` / `log-get` 排查链不断(见模板 `cmd_run` 示范)。
|
||||||
|
|
||||||
|
表结构与字段见 [`references/SCHEMA.md`](../references/SCHEMA.md)。
|
||||||
|
|
||||||
|
### 2.4 RPA video step / artifacts(用户可见动作与存证)
|
||||||
|
|
||||||
|
**来源**:`RpaVideoSession`、`rpa-artifacts/` 目录
|
||||||
|
|
||||||
|
- video artifact 是**标准可启用能力**,**不保证默认每次生成 MP4**(模板默认 `OPENCLAW_RECORD_VIDEO=0`)。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=0`**:不生成最终录屏,但 RPA / 长任务仍须保留 `RpaVideoSession`、`video.add_step` 与 Activity 进度同步。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=1`**:`result_summary` 应包含 `video_path`、`raw_video`、`video_log`、`video_warnings` 等字段;CLI 可打印录屏路径与诊断。
|
||||||
|
- 用户可见的中文步骤(「打开登录页」「点击提交」)。
|
||||||
|
- 失败截图:`{数据目录}/rpa-artifacts/{batch_id}/...`(受 `OPENCLAW_ARTIFACTS_ON_FAILURE` 控制,默认开)
|
||||||
|
|
||||||
|
详见 [`RPA.md`](RPA.md) §5 存证与录屏规范。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3. 必打日志节点清单
|
||||||
|
|
||||||
|
以下节点应在**生产代码**中有对应 `log.info` / `log.warning` / `log.exception`(或 Activity / RPA step)。复制模板后按业务增删,但**不可整体省略**。
|
||||||
|
|
||||||
|
| 阶段 | 建议日志 / emit |
|
||||||
|
|------|-----------------|
|
||||||
|
| CLI 入口启动 | `cli_start`(`cli/app.py`,已由模板提供) |
|
||||||
|
| 参数解析完成 | `run_args target_id=... input_id=...` |
|
||||||
|
| 鉴权开始 | `entitlement_check_start` |
|
||||||
|
| 鉴权成功 | `entitlement_ok` |
|
||||||
|
| 鉴权失败 | `entitlement_failed`(`warning`) |
|
||||||
|
| 任务开始 | `task_start task_type=... target_id=... input_id=... batch_id=...` + `emit("开始处理任务")` |
|
||||||
|
| adapter / test target 选择 | `adapter_selected adapter=... system=...` |
|
||||||
|
| 账号 / profile / lease 获取 | `account_acquired account_id=...` |
|
||||||
|
| 账号 / lease 释放 | `account_released account_id=...` |
|
||||||
|
| 外部 API 开始 | `external_call_start system=... operation=...` |
|
||||||
|
| 外部 API 结束 | `external_call_done system=... operation=... elapsed_ms=... status=...` |
|
||||||
|
| 兄弟技能调用开始 / 结束 | 同上,或 `sibling_call_start/done skill_slug=...` |
|
||||||
|
| RPA 操作开始 / 结束 | `rpa_start` / `rpa_done` + `video.add_step` 中文步骤 |
|
||||||
|
| 打开页面 | step:「打开 xxx 页面」 |
|
||||||
|
| 检查登录 | step + log |
|
||||||
|
| 定位输入框 / 点击提交 | step + log |
|
||||||
|
| 等待结果 | step + log(含 timeout 预期) |
|
||||||
|
| 批量循环 | `batch_progress current=... total=...` |
|
||||||
|
| 重试 | `retry_attempt n=... reason=...`(`warning`) |
|
||||||
|
| 跳过 / 部分失败 | `skipped` / `partial_failure`(`warning`) |
|
||||||
|
| 人工介入 | `human_intervention type=captcha|login|ukey|otp`(`warning`) |
|
||||||
|
| 失败截图 / 视频 | `artifact_saved artifact_path=...` / `video_path=...` |
|
||||||
|
| 任务成功 | `task_success ...` + `task_log_saved status=success` |
|
||||||
|
| 任务失败 | `task_failed ...`(`exception` 保留 traceback)+ `task_log_saved status=failed` |
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4. 字段规范
|
||||||
|
|
||||||
|
推荐在 `log.info` 等消息中使用 **key=value** 结构化字段,便于 grep:
|
||||||
|
|
||||||
|
| 字段 | 含义 |
|
||||||
|
|------|------|
|
||||||
|
| `task_type` | 任务类型(与 `task_logs.task_type` 对齐) |
|
||||||
|
| `target_id` | 目标标识(平台、店铺、报表类型等) |
|
||||||
|
| `input_id` | 输入记录 ID |
|
||||||
|
| `batch_id` | 批次 / 录屏批次 |
|
||||||
|
| `stage` | 阶段:`auth` / `run` / `cleanup` |
|
||||||
|
| `system` | 外部系统名:`erp` / `wechat` / `1688` |
|
||||||
|
| `operation` | 操作名:`submit_order` / `fetch_report` |
|
||||||
|
| `adapter` | adapter 档位:`mock` / `simulator_api` / `real_api` |
|
||||||
|
| `elapsed_ms` | 耗时毫秒 |
|
||||||
|
| `status` | `ok` / `failed` / `timeout` / `skipped` |
|
||||||
|
| `error_code` | 业务错误码(非 Python 异常名) |
|
||||||
|
| `artifact_path` | 截图 / 附件路径 |
|
||||||
|
| `video_path` | 录屏成品路径 |
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
```text
|
||||||
|
external_call_done system=erp operation=download_report elapsed_ms=4523 status=ok
|
||||||
|
batch_progress current=7 total=20 target_id=store-001
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5. 日志级别规范
|
||||||
|
|
||||||
|
| 级别 | 使用场景 |
|
||||||
|
|------|----------|
|
||||||
|
| **info** | 关键阶段、成功路径、外部调用开始/结束、批量进度、`task_log_saved` |
|
||||||
|
| **warning** | 可恢复问题、重试、跳过、人工介入、部分失败、鉴权失败(业务可预期) |
|
||||||
|
| **exception** | 异常路径;**必须**用 `log.exception(...)` 保留 traceback |
|
||||||
|
| **debug** | 低频诊断;**默认不依赖 debug 才能排查问题** |
|
||||||
|
|
||||||
|
原则:**生产排障应主要靠 info + warning + exception**,不要把 debug 当作唯一线索。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6. 敏感信息红线
|
||||||
|
|
||||||
|
### 禁止记录原始值
|
||||||
|
|
||||||
|
以下字段**不得**以明文出现在日志、Activity、task_logs 的 `error_msg` / `result_summary`、stdout:
|
||||||
|
|
||||||
|
- `password`、`token`、`cookie`、`secret`、`api_key`、`authorization`
|
||||||
|
- 验证码、动态口令全文
|
||||||
|
- 身份证、银行卡、手机号**完整值**
|
||||||
|
|
||||||
|
### 建议记录的安全替代
|
||||||
|
|
||||||
|
| 替代字段 | 示例 |
|
||||||
|
|----------|------|
|
||||||
|
| `credential_ref` | vault / account-manager 引用 ID |
|
||||||
|
| `account_id` | 账号表主键 |
|
||||||
|
| `masked_user` | `138****8000` |
|
||||||
|
| `domain` | `erp.example.com` |
|
||||||
|
| `last4` | 卡号后四位 |
|
||||||
|
| `hash` / `short_id` | 内容摘要 |
|
||||||
|
|
||||||
|
变量名可以叫 `token`,但日志里不能出现 `token=eyJhbG...` 或 f-string 拼接明文。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7. 推荐代码示例(service 层)
|
||||||
|
|
||||||
|
```python
|
||||||
|
from util.logging_config import get_skill_logger
|
||||||
|
from jiangchang_skill_core.activity import emit, finish, job_context, rpa_step, interruptible_sleep
|
||||||
|
|
||||||
|
from util.constants import SKILL_SLUG
|
||||||
|
|
||||||
|
log = get_skill_logger()
|
||||||
|
|
||||||
|
def cmd_run(target=None, input_id=None):
|
||||||
|
task_type = "your_task"
|
||||||
|
with job_context(skill=SKILL_SLUG):
|
||||||
|
log.info(
|
||||||
|
"task_start task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type, target, input_id,
|
||||||
|
)
|
||||||
|
emit("开始处理任务", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
ok, reason = check_entitlement(SKILL_SLUG)
|
||||||
|
if not ok:
|
||||||
|
log.warning("entitlement_failed task_type=%s reason=%s", task_type, reason)
|
||||||
|
finish(status="failed", message=reason, skill=SKILL_SLUG, error_code="ENTITLEMENT_DENIED")
|
||||||
|
return 1
|
||||||
|
|
||||||
|
t0 = time.monotonic()
|
||||||
|
# ... 外部调用 / RPA:@rpa_step 或 video.add_step;等待用 interruptible_sleep ...
|
||||||
|
elapsed_ms = int((time.monotonic() - t0) * 1000)
|
||||||
|
log.info(
|
||||||
|
"external_call_done system=%s operation=%s elapsed_ms=%d status=%s",
|
||||||
|
"erp", "submit", elapsed_ms, "ok",
|
||||||
|
)
|
||||||
|
|
||||||
|
tlr.save_task_log(..., status="success", ...)
|
||||||
|
log.info("task_log_saved task_type=%s status=success", task_type)
|
||||||
|
finish(status="success", message="任务完成", skill=SKILL_SLUG)
|
||||||
|
return 0
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type, target, input_id,
|
||||||
|
)
|
||||||
|
emit("任务失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="run")
|
||||||
|
tlr.save_task_log(..., status="failed", error_msg="...", ...)
|
||||||
|
finish(status="failed", message="任务执行异常", skill=SKILL_SLUG)
|
||||||
|
return 1
|
||||||
|
```
|
||||||
|
|
||||||
|
模板示范见 [`scripts/service/task_service.py`](../scripts/service/task_service.py) 的 `cmd_run`。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8. 错误排查顺序
|
||||||
|
|
||||||
|
客户现场或测试机排障,建议按以下顺序(由快到慢、由用户可见到运维细节):
|
||||||
|
|
||||||
|
1. **CLI stdout** — 是否有 `ERROR:<CODE>:`、`HINT:`、模板提示文案。
|
||||||
|
2. **`python scripts/main.py logs`** — 最近任务是否写入 `task_logs`、status 与 `error_msg`。
|
||||||
|
3. **`python scripts/main.py log-get <id>`** — 单条 JSON,含 `result_summary`(video 路径等)。
|
||||||
|
4. **统一日志文件** — `{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log`(grep `trace_id`、`task_failed`)。
|
||||||
|
5. **Run Journal** — `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl`(宿主 UI 进度来源)。
|
||||||
|
6. **RPA artifacts** — `rpa-artifacts/` 失败截图;若 `OPENCLAW_RECORD_VIDEO=1` 还有录屏 MP4、`video_log`。未开启录屏时可能只有截图或日志,需要完整录屏请在用户 `.env` 中设置 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
|
|
||||||
|
长时间无 stdout **不代表卡死**;先看 Run Journal / Activity,再看统一日志。
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9. 与现有文档关系
|
||||||
|
|
||||||
|
| 文档 | 关系 |
|
||||||
|
|------|------|
|
||||||
|
| [`RUNTIME.md`](RUNTIME.md) | 统一日志能力来源、`util.logging_config` 薄封装、数据根约定 |
|
||||||
|
| [`TESTING.md`](TESTING.md) | 日志相关 hard policy 测试(`test_development_policy_guard.py`) |
|
||||||
|
| [`RPA.md`](RPA.md) | RPA step、`RpaVideoSession`、video / artifacts 标准 |
|
||||||
|
| [`SCHEMA.md`](../references/SCHEMA.md) | `task_logs` 表结构与查询命令 |
|
||||||
|
| [`CLI.md`](../references/CLI.md) | `logs` / `log-get` 命令与手工排查入口 |
|
||||||
|
| [`CONFIG.md`](CONFIG.md) | 敏感配置不进日志;`.env` 边界 |
|
||||||
|
| [`DEVELOPMENT.md`](DEVELOPMENT.md) | `service/` 层日志职责与发布前检查清单 |
|
||||||
|
| [`POLICY_MATRIX.md`](POLICY_MATRIX.md) | 可自动检测的 logging hard policy 索引 |
|
||||||
@@ -15,9 +15,18 @@
|
|||||||
| POLICY-TESTING-002 | `tests/integration/` 下 Python 测试文件若存在,默认应为 `.sample`,不得进入默认套件 | development/TESTING.md §7 真实联调测试的安全约束、§8 新 skill 的最小测试清单 | hard | 检查 `tests/integration/*.py` 无 `.sample` 后缀 | `tests/test_development_policy_guard.py::TestPolicyTesting002` |
|
| POLICY-TESTING-002 | `tests/integration/` 下 Python 测试文件若存在,默认应为 `.sample`,不得进入默认套件 | development/TESTING.md §7 真实联调测试的安全约束、§8 新 skill 的最小测试清单 | hard | 检查 `tests/integration/*.py` 无 `.sample` 后缀 | `tests/test_development_policy_guard.py::TestPolicyTesting002` |
|
||||||
| POLICY-RPA-001 | 不得 import/use account-manager 内部 `rpa_helpers` / `inject_account_manager_scripts_path` / `get_account_credential` | development/ADAPTER.md §兄弟依赖;development/RPA.md §1.7 | hard | 扫描 `scripts/**/*.py` 禁止模式 | `tests/test_development_policy_guard.py::TestPolicyRpa001`(另见 `tests/test_no_rpa_helpers_import.py`) |
|
| POLICY-RPA-001 | 不得 import/use account-manager 内部 `rpa_helpers` / `inject_account_manager_scripts_path` / `get_account_credential` | development/ADAPTER.md §兄弟依赖;development/RPA.md §1.7 | hard | 扫描 `scripts/**/*.py` 禁止模式 | `tests/test_development_policy_guard.py::TestPolicyRpa001`(另见 `tests/test_no_rpa_helpers_import.py`) |
|
||||||
| POLICY-RPA-002 | RPA 交付代码不得直接执行 ffmpeg 拼 MP4;应使用 `RpaVideoSession`(允许 `ffmpeg_path` 等诊断字段) | development/RPA.md §5.3 录屏成片标准 | hard | 扫描 `scripts/**/*.py` 禁止 `subprocess` / `os.system` / `os.popen` 直接调用 ffmpeg(`task_run_support.py` 保留豁免) | `tests/test_development_policy_guard.py::TestPolicyRpa002` |
|
| POLICY-RPA-002 | RPA 交付代码不得直接执行 ffmpeg 拼 MP4;应使用 `RpaVideoSession`(允许 `ffmpeg_path` 等诊断字段) | development/RPA.md §5.3 录屏成片标准 | hard | 扫描 `scripts/**/*.py` 禁止 `subprocess` / `os.system` / `os.popen` 直接调用 ffmpeg(`task_run_support.py` 保留豁免) | `tests/test_development_policy_guard.py::TestPolicyRpa002` |
|
||||||
|
| POLICY-RPA-003 | 即使默认 `OPENCLAW_RECORD_VIDEO=0`,RPA / 长任务模板也必须保留录屏能力:`RpaVideoSession`、`video.add_step`、video artifact merge 到 `result_summary` | development/RPA.md §5.3;development/LOGGING.md;development/CONFIG.md | hard | 扫描 `scripts/service/*.py`(service 层整体须含上述接入) | `tests/test_development_policy_guard.py::TestPolicyRpa003` |
|
||||||
| POLICY-PACKAGING-001 | `scripts/**/*.py` 单文件 < 1000 行 | development/DEVELOPMENT.md §3.4 发布打包约束;development/RUNTIME.md §发布打包约束 | hard | 行数统计 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_scripts_py_files_under_pyarmor_line_limit` |
|
| POLICY-PACKAGING-001 | `scripts/**/*.py` 单文件 < 1000 行 | development/DEVELOPMENT.md §3.4 发布打包约束;development/RUNTIME.md §发布打包约束 | hard | 行数统计 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_scripts_py_files_under_pyarmor_line_limit` |
|
||||||
| POLICY-PACKAGING-002 | 文本文件 UTF-8 without BOM,无 `U+FEFF` | development/DEVELOPMENT.md §3.4;development/RUNTIME.md §编码与输出 | hard | BOM / 解码检查 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_text_files_are_utf8_without_bom` |
|
| POLICY-PACKAGING-002 | 文本文件 UTF-8 without BOM,无 `U+FEFF` | development/DEVELOPMENT.md §3.4;development/RUNTIME.md §编码与输出 | hard | BOM / 解码检查 | **已有测试覆盖**:`tests/test_release_packaging_constraints.py::test_text_files_are_utf8_without_bom` |
|
||||||
| POLICY-DOCS-001 | 本矩阵存在且包含上述全部 `policy_id` | 本次规范化约定 | hard | 解析 `development/POLICY_MATRIX.md` | `tests/test_development_policy_guard.py::TestPolicyDocs001` |
|
| POLICY-DOCS-001 | 本矩阵存在且包含上述全部 `policy_id` | 本次规范化约定 | hard | 解析 `development/POLICY_MATRIX.md` | `tests/test_development_policy_guard.py::TestPolicyDocs001` |
|
||||||
|
| POLICY-LOGGING-001 | CLI 入口必须调用 `setup_skill_logging` | development/LOGGING.md;development/RUNTIME.md | hard | 扫描 `scripts/cli/app.py` 含 `setup_skill_logging` 与 logger info | `tests/test_development_policy_guard.py::TestPolicyLogging001` |
|
||||||
|
| POLICY-LOGGING-002 | service 主任务入口必须使用 `get_skill_logger` | development/LOGGING.md;development/DEVELOPMENT.md | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyLogging002` |
|
||||||
|
| POLICY-LOGGING-003 | 主任务入口须含 `log.info`/`logger.info`、`log.exception`/`logger.exception`,并写入 `save_task_log` | development/LOGGING.md | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyLogging003` |
|
||||||
|
| POLICY-LOGGING-004 | 长任务 / RPA 模板须使用 Activity 或 RPA video step 输出进度 | development/LOGGING.md;development/RPA.md | hard | 扫描 `scripts/service/task_service.py` 含 `emit(` / `activity.emit` / `video.add_step` | `tests/test_development_policy_guard.py::TestPolicyLogging004` |
|
||||||
|
| POLICY-LOGGING-005 | 交付代码不得在 logging 调用行以 `key=value` 形式记录明显敏感字段 | development/LOGGING.md;development/CONFIG.md | hard | 扫描 `scripts/**/*.py` 的 logging 调用行(不含注释) | `tests/test_development_policy_guard.py::TestPolicyLogging005` |
|
||||||
|
| POLICY-CONTROL-001 | 不得存在 `scripts/util/progress.py` 或自建 stdout 进度模块 | development/LOGGING.md §2.5;development/RPA.md §0.1 | hard | 文件不存在检查 | `tests/test_development_policy_guard.py::TestPolicyControl001` |
|
||||||
|
| POLICY-CONTROL-002 | `task_service.py` 须使用 `job_context(` 与 `finish(` | development/LOGGING.md §2.5、§7 | hard | 扫描 `scripts/service/task_service.py` | `tests/test_development_policy_guard.py::TestPolicyControl002` |
|
||||||
|
| POLICY-CONTROL-003 | `scripts/service/*.py` 不得裸 `asyncio.sleep`(RPA 等待须 `interruptible_sleep`) | development/RPA.md §0.1 | hard | 扫描 service 层 Python | `tests/test_development_policy_guard.py::TestPolicyControl003` |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -36,4 +45,4 @@
|
|||||||
| RPA 拟人操作、选择器纪律、HITL 超时 | development/RPA.md §0–§1 | 行为与 DOM 质量,无法静态扫描 |
|
| RPA 拟人操作、选择器纪律、HITL 超时 | development/RPA.md §0–§1 | 行为与 DOM 质量,无法静态扫描 |
|
||||||
| adapter 四档契约测试覆盖 timeout/unauthorized 等 | development/ADAPTER.md §contract tests | 需业务实现后人工补测 |
|
| adapter 四档契约测试覆盖 timeout/unauthorized 等 | development/ADAPTER.md §contract tests | 需业务实现后人工补测 |
|
||||||
| `SKILL.md` / `constants.SKILL_SLUG` 一致性 | development/DEVELOPMENT.md §16 | 已有 `tests/test_skill_metadata.py` |
|
| `SKILL.md` / `constants.SKILL_SLUG` 一致性 | development/DEVELOPMENT.md §16 | 已有 `tests/test_skill_metadata.py` |
|
||||||
| platform_kit_min_version >= 1.0.17 | development/RUNTIME.md | 已有 `tests/test_platform_import.py` |
|
| platform_kit_min_version >= 1.2.0 | development/RUNTIME.md | 已有 `tests/test_platform_import.py` |
|
||||||
|
|||||||
@@ -6,10 +6,11 @@
|
|||||||
2. [`NAMING.md`](NAMING.md) — slug / 仓库名命名规范(复制模板前必读)
|
2. [`NAMING.md`](NAMING.md) — slug / 仓库名命名规范(复制模板前必读)
|
||||||
3. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
3. [`DEVELOPMENT.md`](DEVELOPMENT.md) — 完整开发步骤与目录规范
|
||||||
4. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
4. [`TESTING.md`](TESTING.md) — 测试分层、隔离数据根与档位开关
|
||||||
5. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
5. [`LOGGING.md`](LOGGING.md) — 日志分层、必打节点、敏感信息红线(**涉及长任务、RPA、外部系统时必读**)
|
||||||
6. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
6. [`ADAPTER.md`](ADAPTER.md) — 涉及外部系统对接时
|
||||||
7. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
7. [`RPA.md`](RPA.md) — 涉及浏览器 / 桌面 / 手机自动化时
|
||||||
8. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径、发布打包与编码约定
|
8. [`CONFIG.md`](CONFIG.md) — `.env` 规范与 bootstrap 机制
|
||||||
|
9. [`RUNTIME.md`](RUNTIME.md) — 共享 runtime、数据路径、发布打包与编码约定
|
||||||
|
|
||||||
脚手架与 Git 防串库:[`../tools/README.md`](../tools/README.md)(`scaffold_skill.ps1`)。
|
脚手架与 Git 防串库:[`../tools/README.md`](../tools/README.md)(`scaffold_skill.ps1`)。
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,30 @@
|
|||||||
| **失败存证** | 失败必截图,合规场景全程录屏,统一存 `{数据目录}/rpa-artifacts/{batch_id}/{tag}_{ts}.png` |
|
| **失败存证** | 失败必截图,合规场景全程录屏,统一存 `{数据目录}/rpa-artifacts/{batch_id}/{tag}_{ts}.png` |
|
||||||
| **选择器纪律** | 语义选择器优先(id/name/text/aria);**F12 确认后再写,严禁凭记忆猜 DOM** |
|
| **选择器纪律** | 语义选择器优先(id/name/text/aria);**F12 确认后再写,严禁凭记忆猜 DOM** |
|
||||||
| **统一错误码** | `ERROR:REQUIRE_LOGIN` / `ERROR:CAPTCHA_NEED_HUMAN` / `ERROR:RATE_LIMITED` / `ERROR:LOGIN_TIMEOUT` 等,见下方错误码表 |
|
| **统一错误码** | `ERROR:REQUIRE_LOGIN` / `ERROR:CAPTCHA_NEED_HUMAN` / `ERROR:RATE_LIMITED` / `ERROR:LOGIN_TIMEOUT` 等,见下方错误码表 |
|
||||||
|
| **步骤可控制** | 每条 RPA 指令 = 一个 `@rpa_step` 或 `video.add_step`;等待用 `interruptible_sleep`;宿主通过 control.json 暂停/继续/停止(SRCP,见 [`LOGGING.md`](LOGGING.md) §2.5) |
|
||||||
| **幂等 / 断点续跑** | 批量操作记录"已处理到第几条",崩溃后能续跑、不重复提交 |
|
| **幂等 / 断点续跑** | 批量操作记录"已处理到第几条",崩溃后能续跑、不重复提交 |
|
||||||
|
|
||||||
> 三端各自实现一个会话抽象 `RpaSession`(launch / login / act / screenshot / close),上层 skill 不感知是浏览器还是手机。
|
> 三端各自实现一个会话抽象 `RpaSession`(launch / login / act / screenshot / close),上层 skill 不感知是浏览器还是手机。
|
||||||
|
|
||||||
|
### 0.1 步骤控制与进度(SRCP,platform-kit >= 1.2.0)
|
||||||
|
|
||||||
|
- **禁止** `scripts/util/progress.py` 或 `print({"type":"progress",...})`;进度只走 `emit` / Run Journal。
|
||||||
|
- **禁止** RPA 主路径裸 `asyncio.sleep`;用 `jiangchang_skill_core.activity.interruptible_sleep`。
|
||||||
|
- RPA 函数用 `@rpa_step("中文步骤名")` 拆分;或在 `RpaVideoSession` 内 `video.add_step("中文步骤")`。
|
||||||
|
- `cmd_run` 须 `with job_context(skill=SKILL_SLUG):`,所有出口 `finish(...)`。
|
||||||
|
- 暂停在步骤边界生效;浏览器会话暂停时不强制关闭,恢复后同进程继续。
|
||||||
|
|
||||||
|
```python
|
||||||
|
from jiangchang_skill_core.activity import emit, finish, job_context, interruptible_sleep, rpa_step
|
||||||
|
|
||||||
|
@rpa_step("打开登录页")
|
||||||
|
async def open_login(page):
|
||||||
|
await page.goto("https://example.com")
|
||||||
|
await interruptible_sleep(1.5)
|
||||||
|
```
|
||||||
|
|
||||||
|
规范权威定义:[`LOGGING.md`](LOGGING.md) §2.5。金样代码:[`scripts/service/task_service.py`](../scripts/service/task_service.py)。
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 1. 浏览器(标准已成熟)
|
## 1. 浏览器(标准已成熟)
|
||||||
@@ -51,7 +71,7 @@
|
|||||||
6. **可以** `ignore_default_args=["--enable-automation"]`(platform-kit `launch_persistent_browser` 已处理)。
|
6. **可以** `ignore_default_args=["--enable-automation"]`(platform-kit `launch_persistent_browser` 已处理)。
|
||||||
7. **强风控平台**:优先真实点击、键盘、鼠标、地址栏、持久 profile;**不要**直接拼接搜索结果 URL 或 DOM 注入。
|
7. **强风控平台**:优先真实点击、键盘、鼠标、地址栏、持久 profile;**不要**直接拼接搜索结果 URL 或 DOM 注入。
|
||||||
|
|
||||||
指纹淡化(stealth)典型项:`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages` 等。共享实现见 `jiangchang_skill_core.rpa`(platform-kit **>= 1.0.17**)。
|
指纹淡化(stealth)典型项:`navigator.webdriver=undefined`、`chrome.runtime`、`permissions.query`、`plugins`、`languages` 等。共享实现见 `jiangchang_skill_core.rpa`(platform-kit **>= 1.2.0**)。
|
||||||
|
|
||||||
**拟人操作**(必做):
|
**拟人操作**(必做):
|
||||||
|
|
||||||
@@ -124,7 +144,7 @@ from jiangchang_skill_core.rpa import (
|
|||||||
from jiangchang_skill_core.rpa.stealth import stealth_enabled, STEALTH_INIT_SCRIPT
|
from jiangchang_skill_core.rpa.stealth import stealth_enabled, STEALTH_INIT_SCRIPT
|
||||||
```
|
```
|
||||||
|
|
||||||
- `RpaVideoSession` 来自 platform-kit **>= 1.0.17**;ffmpeg、背景音乐、media-assets 由 platform-kit 统一解析;已提供前置/后置缓冲、字幕、TTS 旁白、背景音乐循环、结尾淡出。
|
- `RpaVideoSession` 来自 platform-kit **>= 1.2.0**;ffmpeg、背景音乐、media-assets 由 platform-kit 统一解析;已提供前置/后置缓冲、字幕、TTS 旁白、背景音乐循环、结尾淡出。
|
||||||
- `health` 对上述资源做只读诊断,不下载、不修复。
|
- `health` 对上述资源做只读诊断,不下载、不修复。
|
||||||
|
|
||||||
### 1.5 真实浏览器 RPA 示例(必读)
|
### 1.5 真实浏览器 RPA 示例(必读)
|
||||||
@@ -283,13 +303,16 @@ skill 退出/抛错统一用 `ERROR:` 前缀 + 稳定码,方便宿主与上层
|
|||||||
|
|
||||||
### 5.3 录屏成片标准
|
### 5.3 录屏成片标准
|
||||||
|
|
||||||
- RPA skill 默认 `OPENCLAW_RECORD_VIDEO=1`。
|
- **skill-template 默认 `OPENCLAW_RECORD_VIDEO=0`**(开发调试更快,不启 ffmpeg)。
|
||||||
|
- **`OPENCLAW_RECORD_VIDEO=1` 时启用录屏**:字幕、旁白、背景音、MP4 成片由 platform-kit 统一处理。
|
||||||
- 使用 platform-kit 的 **`RpaVideoSession`**;**skill 不要自行合成视频**(不要自己调 ffmpeg 拼 MP4)。
|
- 使用 platform-kit 的 **`RpaVideoSession`**;**skill 不要自行合成视频**(不要自己调 ffmpeg 拼 MP4)。
|
||||||
- `OPENCLAW_RECORD_VIDEO=0` 时 session 无副作用(不启 ffmpeg、不写字幕文件)。
|
- **默认关闭 ≠ 可以删除录屏接入**:RPA / 长任务 **service 层**(`scripts/service/*.py`)**必须保留** `RpaVideoSession`、`video.add_step`、video artifact merge 到 `result_summary`(见 `POLICY-RPA-003`)。
|
||||||
|
- `OPENCLAW_RECORD_VIDEO=0` 时 session **无副作用**(不启 ffmpeg、不写字幕文件、不生成最终 MP4),但 **`RpaVideoSession.add_step` 仍可同步 Activity 进度**。
|
||||||
|
- 生产留证 / 合规 / 客户现场复现时,由用户在 `.env` 或进程环境变量中开启 `OPENCLAW_RECORD_VIDEO=1`。
|
||||||
- **ffmpeg 是唯一录屏器**(Windows:`gdigrab` + `desktop`)。
|
- **ffmpeg 是唯一录屏器**(Windows:`gdigrab` + `desktop`)。
|
||||||
- **最终视频**:`{skill_data_dir}/videos/{skill_slug}_{yyyyMMdd_HHmmss}_{batch_id}.mp4`
|
- **最终视频**(`OPENCLAW_RECORD_VIDEO=1` 时):`{skill_data_dir}/videos/{skill_slug}_{yyyyMMdd_HHmmss}_{batch_id}.mp4`
|
||||||
- **中间产物**:`rpa-artifacts/{batch_id}/capture.mp4`、`subtitles/`、`logs/` 等。
|
- **中间产物**:`rpa-artifacts/{batch_id}/capture.mp4`、`subtitles/`、`logs/` 等。
|
||||||
- 任务完成后 CLI / `result_summary` 应包含:`video_path`、`raw_video`、`video_log`、`video_warnings`、`music_path`、`voiceover_path`、`audio_warnings`(见 `scripts/service/task_run_support.py`)。
|
- 任务完成后 CLI / `result_summary` 应包含 video 字段(开启录屏时有路径;关闭时仍保留 `video` 结构与诊断占位):`video_path`、`raw_video`、`video_log`、`video_warnings`、`music_path`、`voiceover_path`、`audio_warnings`(见 `scripts/service/task_run_support.py`)。
|
||||||
|
|
||||||
模板最小示范见 `scripts/service/task_service.py` 的 `_run_template_demo()`。
|
模板最小示范见 `scripts/service/task_service.py` 的 `_run_template_demo()`。
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## 共享 Python Runtime
|
## 共享 Python Runtime
|
||||||
|
|
||||||
**skill-template** 及复制出的新技能,公共能力均来自宿主匠厂安装的共享 Python Runtime(`jiangchang-platform-kit>=1.0.17` 及其传递依赖,含 `playwright`)。`jiangchang_skill_core` **不得**在技能仓库内 vendored,应由共享 venv 的 site-packages 提供。
|
**skill-template** 及复制出的新技能,公共能力均来自宿主匠厂安装的共享 Python Runtime(`jiangchang-platform-kit>=1.2.0` 及其传递依赖,含 `playwright`)。`jiangchang_skill_core` **不得**在技能仓库内 vendored,应由共享 venv 的 site-packages 提供。
|
||||||
|
|
||||||
技能根目录 `requirements.txt` **只声明技能特有依赖**;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。`SKILL.md` 的 `platform_kit_min_version` 是运行契约,**不是** pip 依赖声明。
|
技能根目录 `requirements.txt` **只声明技能特有依赖**;**不要**重复声明 `jiangchang-platform-kit` 或 `playwright`。`SKILL.md` 的 `platform_kit_min_version` 是运行契约,**不是** pip 依赖声明。
|
||||||
|
|
||||||
@@ -24,7 +24,7 @@ Windows:
|
|||||||
|
|
||||||
`<shared-python>` 通常位于 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。数据根由宿主注入;开发模式下也可能通过 `JIANGCHANG_DATA_ROOT` 解析(见 `jiangchang_skill_core.runtime_env`)。
|
`<shared-python>` 通常位于 `{JIANGCHANG_DATA_ROOT}/python-runtime/.venv`。数据根由宿主注入;开发模式下也可能通过 `JIANGCHANG_DATA_ROOT` 解析(见 `jiangchang_skill_core.runtime_env`)。
|
||||||
|
|
||||||
## Runtime 诊断(platform-kit 1.0.17+)
|
## Runtime 诊断(platform-kit 1.2.0+)
|
||||||
|
|
||||||
`health` 命令通过 **`jiangchang_skill_core.collect_runtime_diagnostics`** 输出共享 runtime 诊断,**不在技能内重复实现**。典型字段:
|
`health` 命令通过 **`jiangchang_skill_core.collect_runtime_diagnostics`** 输出共享 runtime 诊断,**不在技能内重复实现**。典型字段:
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ Windows:
|
|||||||
- 用户实际 `.env`:`{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill_slug}/.env`。
|
- 用户实际 `.env`:`{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/{skill_slug}/.env`。
|
||||||
- `scripts/main.py` 与 `cli.app.main()` 启动时调用 `util.config_bootstrap.bootstrap_skill_config()`。
|
- `scripts/main.py` 与 `cli.app.main()` 启动时调用 `util.config_bootstrap.bootstrap_skill_config()`。
|
||||||
- 配置优先级:**进程环境变量** > **用户 `.env`** > **`.env.example` 默认值**。
|
- 配置优先级:**进程环境变量** > **用户 `.env`** > **`.env.example` 默认值**。
|
||||||
- 公共 `config` / `merge_missing_env_keys` 来自共享 runtime 的 `jiangchang-platform-kit>=1.0.17`,**不得** vendored `scripts/jiangchang_skill_core/`。
|
- 公共 `config` / `merge_missing_env_keys` 来自共享 runtime 的 `jiangchang-platform-kit>=1.2.0`,**不得** vendored `scripts/jiangchang_skill_core/`。
|
||||||
|
|
||||||
## media-assets / ffmpeg / 背景音乐
|
## media-assets / ffmpeg / 背景音乐
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
必跑套件要像一个紧张的守门员:**快、确定、离线**。典型覆盖:
|
必跑套件要像一个紧张的守门员:**快、确定、离线**。典型覆盖:
|
||||||
|
|
||||||
- CLI:导入 [`cli.app`](../scripts/cli/app.py) 走解析链路、`health`(runtime diagnostics)/ `version` / `logs` / `log-get` 冒烟;
|
- CLI:导入 [`cli.app`](../scripts/cli/app.py) 走解析链路、`health`(runtime diagnostics)/ `version` / `logs` / `log-get` 冒烟;
|
||||||
- 架构守护:无 `scripts/jiangchang_skill_core/`、`platform-kit>=1.0.17` 导入来源、文档/runtime 标准(见 `test_platform_import.py` 等);
|
- 架构守护:无 `scripts/jiangchang_skill_core/`、`platform-kit>=1.2.0` 导入来源、文档/runtime 标准(见 `test_platform_import.py` 等);
|
||||||
- **真实 subprocess**:[`tests/test_entrypoint_subprocess.py`](../tests/test_entrypoint_subprocess.py) 再调用一遍 `python scripts/main.py`,防路径漂移;
|
- **真实 subprocess**:[`tests/test_entrypoint_subprocess.py`](../tests/test_entrypoint_subprocess.py) 再调用一遍 `python scripts/main.py`,防路径漂移;
|
||||||
- 运行时:`runtime_paths` 与 **`JIANGCHANG_*` 隔离**;
|
- 运行时:`runtime_paths` 与 **`JIANGCHANG_*` 隔离**;
|
||||||
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
- `SKILL.md` YAML slug vs [`constants.SKILL_SLUG`](../scripts/util/constants.py);
|
||||||
@@ -191,6 +191,17 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
- `POLICY-RPA-002`:只禁止 `subprocess` / `os.system` / `os.popen` **直接执行** ffmpeg,不误伤 `ffmpeg_path` 等诊断字段。
|
- `POLICY-RPA-002`:只禁止 `subprocess` / `os.system` / `os.popen` **直接执行** ffmpeg,不误伤 `ffmpeg_path` 等诊断字段。
|
||||||
- 与已有测试分工:`test_release_packaging_constraints.py` 继续守护 PyArmor 行数与 UTF-8 BOM;`test_no_rpa_helpers_import.py` 与 policy guard 的 `POLICY-RPA-001` 语义一致,二者并存防回归。
|
- 与已有测试分工:`test_release_packaging_constraints.py` 继续守护 PyArmor 行数与 UTF-8 BOM;`test_no_rpa_helpers_import.py` 与 policy guard 的 `POLICY-RPA-001` 语义一致,二者并存防回归。
|
||||||
|
|
||||||
|
### 日志规范测试
|
||||||
|
|
||||||
|
[`tests/test_development_policy_guard.py`](../tests/test_development_policy_guard.py) 中的 `POLICY-LOGGING-*` 规则会检查 logging **hard policy**(见 [`POLICY_MATRIX.md`](POLICY_MATRIX.md)):
|
||||||
|
|
||||||
|
- CLI 是否调用 `setup_skill_logging`(`POLICY-LOGGING-001`)
|
||||||
|
- `task_service.py` 是否使用 `get_skill_logger`、含 `log.info` / `log.exception`、写入 `save_task_log`(`POLICY-LOGGING-002` / `003`)
|
||||||
|
- 长任务模板是否使用 Activity 或 RPA video step(`POLICY-LOGGING-004`)
|
||||||
|
- 交付代码是否在 logging 调用中拼接明显敏感字段(`POLICY-LOGGING-005`)
|
||||||
|
|
||||||
|
**局限**:自动检测只覆盖可静态判断的**底线**,**不**判断业务日志是否足够完整。复制新 skill 后,仍须人工对照 [`LOGGING.md`](LOGGING.md) §必打日志节点清单做 code review。
|
||||||
|
|
||||||
新增或调整开发规范时:**先更新 `POLICY_MATRIX.md`,再补/改 `test_development_policy_guard.py`(或注明已有测试覆盖)**。
|
新增或调整开发规范时:**先更新 `POLICY_MATRIX.md`,再补/改 `test_development_policy_guard.py`(或注明已有测试覆盖)**。
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -225,10 +236,11 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
|
|
||||||
## 12. RPA / video 测试标准
|
## 12. RPA / video 测试标准
|
||||||
|
|
||||||
- `RpaVideoSession` 调用**不跑真实 ffmpeg**:单测中使用 `unittest.mock` patch session 或设 `OPENCLAW_RECORD_VIDEO=0`。
|
- 模板 `.env.example` 默认 **`OPENCLAW_RECORD_VIDEO=0`**;默认必跑测试**不应启动真实 ffmpeg**(patch session 或显式设 `OPENCLAW_RECORD_VIDEO=0`)。
|
||||||
|
- **但测试必须守护录屏接入能力**(`POLICY-RPA-003` 扫描 `scripts/service/*.py`):`RpaVideoSession` 被构造、`video.add_step` 被调用、video artifact 结构 merge 进 `result_summary`(`video` / `video_path` 等字段),与默认是否录屏无关。
|
||||||
- 断言 `title` / `closing_title` 是**中文**业务文案。
|
- 断言 `title` / `closing_title` 是**中文**业务文案。
|
||||||
- 断言 video artifact 会进入 `result_summary`(`video_path`、`raw_video`、`video_log` 等)。
|
|
||||||
- 断言 step 文案贴近用户动作,不是技术日志(如「准备执行示例任务」而非「enter cmd_run」)。
|
- 断言 step 文案贴近用户动作,不是技术日志(如「准备执行示例任务」而非「enter cmd_run」)。
|
||||||
|
- 若需测试**真实录屏**(ffmpeg 成片),必须显式设 `OPENCLAW_RECORD_VIDEO=1`,并放在 `tests/integration/` 或手动触发场景,**不得**进入默认 unittest 套件。
|
||||||
|
|
||||||
参考 `tests/test_video_service.py`。
|
参考 `tests/test_video_service.py`。
|
||||||
|
|
||||||
@@ -248,7 +260,7 @@ Golden fixture 流程同理([`tests/samples/test_golden_cases.py.sample`](../t
|
|||||||
|
|
||||||
- [ ] `requirements.txt` **不含** `jiangchang-platform-kit` / `playwright`
|
- [ ] `requirements.txt` **不含** `jiangchang-platform-kit` / `playwright`
|
||||||
- [ ] 无 `scripts/jiangchang_skill_core/` vendored 副本
|
- [ ] 无 `scripts/jiangchang_skill_core/` vendored 副本
|
||||||
- [ ] `platform_kit_min_version` **>= 1.0.17**(`SKILL.md` + `constants.py`)
|
- [ ] `platform_kit_min_version` **>= 1.2.0**(`SKILL.md` + `constants.py`)
|
||||||
- [ ] `health` 能输出 `platform_kit_version_ok`(或等价诊断行)
|
- [ ] `health` 能输出 `platform_kit_version_ok`(或等价诊断行)
|
||||||
- [ ] `config-path` 可输出用户 `.env` 路径 JSON
|
- [ ] `config-path` 可输出用户 `.env` 路径 JSON
|
||||||
- [ ] `pytest.ini` 存在且 `python_files` 只收集 `test_*.py` / `*_test.py`
|
- [ ] `pytest.ini` 存在且 `python_files` 只收集 `test_*.py` / `*_test.py`
|
||||||
|
|||||||
@@ -16,8 +16,15 @@ python {baseDir}/scripts/main.py version
|
|||||||
- **`health`**:只读 runtime 诊断,**不下载、不修复 media-assets,不执行业务动作**;不输出敏感值。
|
- **`health`**:只读 runtime 诊断,**不下载、不修复 media-assets,不执行业务动作**;不输出敏感值。
|
||||||
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`、`example_path`。
|
- **`config-path`**:输出 JSON,包含 `skill`、`env_path`、`example_path`。
|
||||||
- **`version`**:输出 JSON(`version`、`skill`)。
|
- **`version`**:输出 JSON(`version`、`skill`)。
|
||||||
- **`run`**:长时间无 stdout **不代表卡死**;应通过 `logs` / `log-get` 排查。
|
- **`run`**:长时间无 stdout **不代表卡死**;RPA / 外部调用期间应通过 Activity 或 Run Journal 看进度。
|
||||||
- **任务完成后**若有 video artifact,CLI 应打印录屏路径、录屏日志、视频/音频诊断(见 `task_run_support._print_video_summary`)。
|
- **排查顺序**(由快到慢):
|
||||||
|
1. `python {baseDir}/scripts/main.py logs`
|
||||||
|
2. `python {baseDir}/scripts/main.py log-get <log_id>`
|
||||||
|
3. 统一日志文件 `{JIANGCHANG_DATA_ROOT}/{JIANGCHANG_USER_ID}/logs/jiangchang.log`(含 `trace_id`、`task_failed` 等)
|
||||||
|
4. Run Journal `{JIANGCHANG_DATA_ROOT}/.jiangchang/runs/{job_id}.jsonl`
|
||||||
|
5. RPA artifacts / `video_log` / 失败截图(见 `development/RPA.md`);未开启录屏时可能只有截图或日志
|
||||||
|
- **录屏默认关闭**:模板 `.env.example` 为 `OPENCLAW_RECORD_VIDEO=0`;若未开启录屏,任务**可能不会**打印录屏路径。
|
||||||
|
- 需要录屏时在用户 `.env` 或进程环境变量中设置 **`OPENCLAW_RECORD_VIDEO=1`**;开启后任务完成时 CLI 应打印录屏路径、录屏日志、视频/音频诊断(见 `task_run_support._print_video_summary`),且 `result_summary` 含 video 字段。
|
||||||
|
|
||||||
## config-path(配置路径)
|
## config-path(配置路径)
|
||||||
|
|
||||||
|
|||||||
@@ -13,20 +13,30 @@ import uuid
|
|||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from jiangchang_skill_core import collect_runtime_diagnostics, config, format_runtime_health_lines
|
from jiangchang_skill_core import collect_runtime_diagnostics, config, format_runtime_health_lines
|
||||||
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
from jiangchang_skill_core.activity import JobStopped, emit, finish, job_context
|
||||||
|
|
||||||
from db import task_logs_repository as tlr
|
from db import task_logs_repository as tlr
|
||||||
|
from jiangchang_skill_core.rpa.video_session import RpaVideoSession
|
||||||
from service.entitlement_service import check_entitlement
|
from service.entitlement_service import check_entitlement
|
||||||
from service.task_run_support import (
|
from service.task_run_support import (
|
||||||
_print_video_summary,
|
_print_video_summary,
|
||||||
build_video_info,
|
build_video_info,
|
||||||
merge_video_into_result_summary,
|
merge_video_into_result_summary,
|
||||||
)
|
)
|
||||||
from util.constants import PLATFORM_KIT_MIN_VERSION, SKILL_SLUG, SKILL_VERSION
|
from util.constants import LOG_LOGGER_NAME, PLATFORM_KIT_MIN_VERSION, SKILL_SLUG, SKILL_VERSION
|
||||||
|
from util.logging_config import get_skill_logger, setup_skill_logging
|
||||||
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
from util.runtime_paths import get_skill_data_dir, get_skill_root
|
||||||
from util.timeutil import unix_to_iso
|
from util.timeutil import unix_to_iso
|
||||||
|
|
||||||
|
|
||||||
|
def _get_task_logger():
|
||||||
|
try:
|
||||||
|
return get_skill_logger()
|
||||||
|
except RuntimeError:
|
||||||
|
setup_skill_logging(SKILL_SLUG, LOG_LOGGER_NAME)
|
||||||
|
return get_skill_logger()
|
||||||
|
|
||||||
|
|
||||||
async def _run_template_demo(target: Optional[str], input_id: Optional[str]) -> tuple[int, dict[str, Any]]:
|
async def _run_template_demo(target: Optional[str], input_id: Optional[str]) -> tuple[int, dict[str, Any]]:
|
||||||
"""最小 RpaVideoSession 示范:不启动浏览器、不执行业务,仅演示录屏包裹范式。"""
|
"""最小 RpaVideoSession 示范:不启动浏览器、不执行业务,仅演示录屏包裹范式。"""
|
||||||
batch_id = uuid.uuid4().hex[:12]
|
batch_id = uuid.uuid4().hex[:12]
|
||||||
@@ -53,34 +63,129 @@ async def _run_template_demo(target: Optional[str], input_id: Optional[str]) ->
|
|||||||
|
|
||||||
def cmd_run(target: Optional[str] = None, input_id: Optional[str] = None) -> int:
|
def cmd_run(target: Optional[str] = None, input_id: Optional[str] = None) -> int:
|
||||||
"""通用任务执行入口模板。复制后请实现真实业务逻辑。"""
|
"""通用任务执行入口模板。复制后请实现真实业务逻辑。"""
|
||||||
ok, reason = check_entitlement(SKILL_SLUG)
|
log = _get_task_logger()
|
||||||
if not ok:
|
task_type = "demo"
|
||||||
print(f"❌ {reason}")
|
|
||||||
return 1
|
|
||||||
|
|
||||||
rc, video_info = asyncio.run(_run_template_demo(target, input_id))
|
with job_context(skill=SKILL_SLUG):
|
||||||
_print_video_summary(video_info)
|
log.info(
|
||||||
|
"task_start task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
emit("开始处理任务", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
ok, reason = check_entitlement(SKILL_SLUG)
|
||||||
|
if not ok:
|
||||||
|
log.warning("entitlement_failed task_type=%s reason=%s", task_type, reason)
|
||||||
|
emit("鉴权失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="auth")
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg=reason,
|
||||||
|
result_summary=json.dumps({"stage": "auth", "error": reason}, ensure_ascii=False),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s stage=auth status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message=reason,
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
error_code="ENTITLEMENT_DENIED",
|
||||||
|
stage="auth",
|
||||||
|
)
|
||||||
|
print(f"❌ {reason}")
|
||||||
|
return 1
|
||||||
|
|
||||||
summary_payload = merge_video_into_result_summary(
|
log.info("task_run_demo_start target_id=%s input_id=%s", target, input_id)
|
||||||
{
|
rc, video_info = asyncio.run(_run_template_demo(target, input_id))
|
||||||
"template_demo": True,
|
log.info("task_run_demo_done target_id=%s input_id=%s status=failed", target, input_id)
|
||||||
"target": target,
|
_print_video_summary(video_info)
|
||||||
"input_id": input_id,
|
|
||||||
},
|
|
||||||
video_info,
|
|
||||||
)
|
|
||||||
tlr.save_task_log(
|
|
||||||
task_type="demo",
|
|
||||||
target_id=target,
|
|
||||||
input_id=input_id,
|
|
||||||
input_title="模板示例任务",
|
|
||||||
status="failed",
|
|
||||||
error_msg="模板仓库未实现真实业务",
|
|
||||||
result_summary=json.dumps(summary_payload, ensure_ascii=False),
|
|
||||||
)
|
|
||||||
|
|
||||||
print("❌ 这是模板仓库,请复制后在 scripts/service/task_service.py 中实现 cmd_run 的真实业务逻辑。")
|
summary_payload = merge_video_into_result_summary(
|
||||||
return rc
|
{
|
||||||
|
"template_demo": True,
|
||||||
|
"target": target,
|
||||||
|
"input_id": input_id,
|
||||||
|
},
|
||||||
|
video_info,
|
||||||
|
)
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg="模板仓库未实现真实业务",
|
||||||
|
result_summary=json.dumps(summary_payload, ensure_ascii=False),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message="模板仓库未实现真实业务",
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
template_demo=True,
|
||||||
|
target=target,
|
||||||
|
input_id=input_id,
|
||||||
|
)
|
||||||
|
print("❌ 这是模板仓库,请复制后在 scripts/service/task_service.py 中实现 cmd_run 的真实业务逻辑。")
|
||||||
|
return rc
|
||||||
|
except JobStopped:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
emit("任务失败,已记录诊断信息", type="warn", skill=SKILL_SLUG, stage="run")
|
||||||
|
try:
|
||||||
|
tlr.save_task_log(
|
||||||
|
task_type=task_type,
|
||||||
|
target_id=target,
|
||||||
|
input_id=input_id,
|
||||||
|
input_title="模板示例任务",
|
||||||
|
status="failed",
|
||||||
|
error_msg="任务执行异常,详见统一日志",
|
||||||
|
result_summary=json.dumps(
|
||||||
|
{"stage": "run", "error": "unexpected_exception"},
|
||||||
|
ensure_ascii=False,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
log.info(
|
||||||
|
"task_log_saved task_type=%s target_id=%s input_id=%s stage=run status=failed",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
log.exception(
|
||||||
|
"task_log_save_failed task_type=%s target_id=%s input_id=%s",
|
||||||
|
task_type,
|
||||||
|
target,
|
||||||
|
input_id,
|
||||||
|
)
|
||||||
|
finish(
|
||||||
|
status="failed",
|
||||||
|
message="任务执行异常,详见统一日志",
|
||||||
|
skill=SKILL_SLUG,
|
||||||
|
stage="run",
|
||||||
|
)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
def cmd_logs(
|
def cmd_logs(
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
"""技能标识、版本与平台公共库约束(复制后请修改 slug/version/logger)。"""
|
||||||
|
|
||||||
SKILL_SLUG = "your-skill-slug"
|
SKILL_SLUG = "your-skill-slug"
|
||||||
SKILL_VERSION = "1.0.32"
|
SKILL_VERSION = "1.0.38"
|
||||||
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
LOG_LOGGER_NAME = "openclaw.skill.your_skill_slug"
|
||||||
PLATFORM_KIT_MIN_VERSION = "1.0.17"
|
PLATFORM_KIT_MIN_VERSION = "1.2.0"
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ def get_skill_root() -> str:
|
|||||||
return _SKILL_ROOT
|
return _SKILL_ROOT
|
||||||
|
|
||||||
|
|
||||||
def platform_kit_version_patch(version: str = "1.0.17"):
|
def platform_kit_version_patch(version: str = "1.2.0"):
|
||||||
"""Mock installed jiangchang-platform-kit version for health/diagnostics tests."""
|
"""Mock installed jiangchang-platform-kit version for health/diagnostics tests."""
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
|||||||
@@ -32,9 +32,18 @@ POLICY_IDS = (
|
|||||||
"POLICY-TESTING-002",
|
"POLICY-TESTING-002",
|
||||||
"POLICY-RPA-001",
|
"POLICY-RPA-001",
|
||||||
"POLICY-RPA-002",
|
"POLICY-RPA-002",
|
||||||
|
"POLICY-RPA-003",
|
||||||
"POLICY-PACKAGING-001",
|
"POLICY-PACKAGING-001",
|
||||||
"POLICY-PACKAGING-002",
|
"POLICY-PACKAGING-002",
|
||||||
"POLICY-DOCS-001",
|
"POLICY-DOCS-001",
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
"POLICY-LOGGING-002",
|
||||||
|
"POLICY-LOGGING-003",
|
||||||
|
"POLICY-LOGGING-004",
|
||||||
|
"POLICY-LOGGING-005",
|
||||||
|
"POLICY-CONTROL-001",
|
||||||
|
"POLICY-CONTROL-002",
|
||||||
|
"POLICY-CONTROL-003",
|
||||||
)
|
)
|
||||||
|
|
||||||
STRUCTURE_PATHS = (
|
STRUCTURE_PATHS = (
|
||||||
@@ -104,6 +113,29 @@ RPA_002_FFMPEG_PATTERNS = (
|
|||||||
|
|
||||||
RPA_002_EXEMPT_REL = "scripts/service/task_run_support.py"
|
RPA_002_EXEMPT_REL = "scripts/service/task_run_support.py"
|
||||||
|
|
||||||
|
RPA_003_SOURCE = (
|
||||||
|
"development/RPA.md §5.3; development/TESTING.md §12; development/POLICY_MATRIX.md"
|
||||||
|
)
|
||||||
|
|
||||||
|
LOGGING_001_SOURCE = "development/LOGGING.md; development/RUNTIME.md"
|
||||||
|
LOGGING_002_SOURCE = "development/LOGGING.md; development/DEVELOPMENT.md"
|
||||||
|
LOGGING_003_SOURCE = "development/LOGGING.md"
|
||||||
|
LOGGING_004_SOURCE = "development/LOGGING.md; development/RPA.md"
|
||||||
|
LOGGING_005_SOURCE = "development/LOGGING.md; development/CONFIG.md"
|
||||||
|
|
||||||
|
LOGGING_CALL_PATTERN = re.compile(
|
||||||
|
r"\b(log|logger|get_skill_logger\s*\(\s*\))\s*\.\s*(info|warning|error|debug|exception|critical)\s*\(",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
LOGGING_CALL_GET_LOGGER_PATTERN = re.compile(
|
||||||
|
r"get_skill_logger\s*\(\s*\)\s*\.\s*(info|warning|error|debug|exception|critical)\s*\(",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
SENSITIVE_LOG_ASSIGNMENT_PATTERN = re.compile(
|
||||||
|
r"(password|token|cookie|secret|api_key|authorization)\s*=",
|
||||||
|
re.IGNORECASE,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _policy_msg(policy_id: str, source: str, detail: str) -> str:
|
def _policy_msg(policy_id: str, source: str, detail: str) -> str:
|
||||||
return f"{policy_id} [{source}]: {detail}"
|
return f"{policy_id} [{source}]: {detail}"
|
||||||
@@ -446,6 +478,209 @@ class TestPolicyRpa002(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyRpa003(unittest.TestCase):
|
||||||
|
def test_service_layer_retains_video_session_integration(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
service_files = _walk_files(skill_root, "scripts/service", suffix=".py")
|
||||||
|
self.assertTrue(service_files, msg="scripts/service/*.py not found")
|
||||||
|
combined = "\n".join(_read_text(path) for path in service_files)
|
||||||
|
missing: list[str] = []
|
||||||
|
if "RpaVideoSession" not in combined:
|
||||||
|
missing.append("RpaVideoSession")
|
||||||
|
if "video.add_step" not in combined and ".add_step(" not in combined:
|
||||||
|
missing.append("video.add_step or .add_step(")
|
||||||
|
if "merge_video_into_result_summary" not in combined:
|
||||||
|
missing.append("merge_video_into_result_summary")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-RPA-003",
|
||||||
|
RPA_003_SOURCE,
|
||||||
|
"scripts/service/*.py missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _scan_sensitive_logging_assignments(skill_root: str) -> list[str]:
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
for lineno, line in enumerate(_read_text(path).splitlines(), 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
if not LOGGING_CALL_PATTERN.search(line):
|
||||||
|
continue
|
||||||
|
if SENSITIVE_LOG_ASSIGNMENT_PATTERN.search(line):
|
||||||
|
offenders.append(f"{rel}:{lineno}: {stripped}")
|
||||||
|
return offenders
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging001(unittest.TestCase):
|
||||||
|
def test_cli_app_sets_up_unified_logging(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "cli", "app.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
self.assertIn(
|
||||||
|
"setup_skill_logging",
|
||||||
|
text,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
LOGGING_001_SOURCE,
|
||||||
|
f"{rel} missing setup_skill_logging",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
has_logger_info = bool(
|
||||||
|
LOGGING_CALL_GET_LOGGER_PATTERN.search(text)
|
||||||
|
or re.search(r"\bget_skill_logger\s*\(\s*\)\s*\.\s*info\s*\(", text)
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
has_logger_info,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-001",
|
||||||
|
LOGGING_001_SOURCE,
|
||||||
|
f"{rel} missing get_skill_logger().info (or equivalent)",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging002(unittest.TestCase):
|
||||||
|
def test_task_service_uses_get_skill_logger(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
self.assertRegex(
|
||||||
|
text,
|
||||||
|
r"get_skill_logger",
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-002",
|
||||||
|
LOGGING_002_SOURCE,
|
||||||
|
f"{rel} must import/use get_skill_logger",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging003(unittest.TestCase):
|
||||||
|
def test_task_service_logs_start_failure_and_task_logs(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
missing: list[str] = []
|
||||||
|
if not re.search(r"\b(log|logger)\.info\s*\(", text):
|
||||||
|
missing.append("log.info or logger.info")
|
||||||
|
if not re.search(r"\b(log|logger)\.exception\s*\(", text):
|
||||||
|
missing.append("log.exception or logger.exception")
|
||||||
|
if "save_task_log" not in text:
|
||||||
|
missing.append("save_task_log")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-003",
|
||||||
|
LOGGING_003_SOURCE,
|
||||||
|
f"{rel} missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging004(unittest.TestCase):
|
||||||
|
def test_task_service_emits_progress_for_long_tasks(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
has_progress = bool(
|
||||||
|
re.search(r"\bemit\s*\(", text)
|
||||||
|
or re.search(r"\bactivity\.emit\s*\(", text)
|
||||||
|
or re.search(r"\bvideo\.add_step\s*\(", text)
|
||||||
|
)
|
||||||
|
self.assertTrue(
|
||||||
|
has_progress,
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-004",
|
||||||
|
LOGGING_004_SOURCE,
|
||||||
|
f"{rel} must use emit( / activity.emit / video.add_step",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyLogging005(unittest.TestCase):
|
||||||
|
def test_scripts_do_not_log_sensitive_assignments(self) -> None:
|
||||||
|
offenders = _scan_sensitive_logging_assignments(get_skill_root())
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-LOGGING-005",
|
||||||
|
LOGGING_005_SOURCE,
|
||||||
|
"\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl001(unittest.TestCase):
|
||||||
|
def test_no_util_progress_module(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "util", "progress.py")
|
||||||
|
self.assertFalse(
|
||||||
|
os.path.isfile(path),
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-001",
|
||||||
|
"development/LOGGING.md §2.5; development/RPA.md §0.1",
|
||||||
|
"scripts/util/progress.py must not exist",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl002(unittest.TestCase):
|
||||||
|
def test_task_service_uses_job_context_and_finish(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
path = os.path.join(skill_root, "scripts", "service", "task_service.py")
|
||||||
|
text = _read_text(path)
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
missing: list[str] = []
|
||||||
|
if "job_context(" not in text:
|
||||||
|
missing.append("job_context(")
|
||||||
|
if "finish(" not in text:
|
||||||
|
missing.append("finish(")
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-002",
|
||||||
|
"development/LOGGING.md §2.5、§7",
|
||||||
|
f"{rel} missing: " + ", ".join(missing),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPolicyControl003(unittest.TestCase):
|
||||||
|
def test_service_layer_has_no_bare_asyncio_sleep(self) -> None:
|
||||||
|
skill_root = get_skill_root()
|
||||||
|
offenders: list[str] = []
|
||||||
|
for path in _walk_files(skill_root, "scripts/service", suffix=".py"):
|
||||||
|
rel = _rel(skill_root, path)
|
||||||
|
for lineno, line in enumerate(_read_text(path).splitlines(), 1):
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
if "asyncio.sleep" in stripped:
|
||||||
|
offenders.append(f"{rel}:{lineno}: {stripped}")
|
||||||
|
self.assertEqual(
|
||||||
|
offenders,
|
||||||
|
[],
|
||||||
|
msg=_policy_msg(
|
||||||
|
"POLICY-CONTROL-003",
|
||||||
|
"development/RPA.md §0.1",
|
||||||
|
"use interruptible_sleep instead:\n" + "\n".join(offenders),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestPolicyDocs001(unittest.TestCase):
|
class TestPolicyDocs001(unittest.TestCase):
|
||||||
def test_policy_matrix_exists_and_lists_policy_ids(self) -> None:
|
def test_policy_matrix_exists_and_lists_policy_ids(self) -> None:
|
||||||
skill_root = get_skill_root()
|
skill_root = get_skill_root()
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ class TestDocsStandards(unittest.TestCase):
|
|||||||
self.assertIn("--no-sandbox", text)
|
self.assertIn("--no-sandbox", text)
|
||||||
self.assertIn("--disable-blink-features=AutomationControlled", text)
|
self.assertIn("--disable-blink-features=AutomationControlled", text)
|
||||||
self.assertIn("RpaVideoSession", text)
|
self.assertIn("RpaVideoSession", text)
|
||||||
self.assertIn("1.0.17", text)
|
self.assertIn("1.2.0", text)
|
||||||
|
|
||||||
def test_rpa_md_forbids_rpa_helpers_import(self) -> None:
|
def test_rpa_md_forbids_rpa_helpers_import(self) -> None:
|
||||||
text = self._read("development/RPA.md")
|
text = self._read("development/RPA.md")
|
||||||
|
|||||||
@@ -83,16 +83,16 @@ class TestPlatformImportSource(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_platform_kit_min_version_is_1_0_14(self) -> None:
|
def test_platform_kit_min_version_is_1_2_0(self) -> None:
|
||||||
from jiangchang_skill_core import version_ge
|
from jiangchang_skill_core import version_ge
|
||||||
from util.constants import PLATFORM_KIT_MIN_VERSION
|
from util.constants import PLATFORM_KIT_MIN_VERSION
|
||||||
|
|
||||||
self.assertEqual(PLATFORM_KIT_MIN_VERSION, "1.0.17")
|
self.assertEqual(PLATFORM_KIT_MIN_VERSION, "1.2.0")
|
||||||
|
|
||||||
md_path = os.path.join(get_skill_root(), "SKILL.md")
|
md_path = os.path.join(get_skill_root(), "SKILL.md")
|
||||||
with open(md_path, encoding="utf-8") as f:
|
with open(md_path, encoding="utf-8") as f:
|
||||||
md = f.read()
|
md = f.read()
|
||||||
self.assertEqual(_parse_platform_kit_min_version(md), "1.0.17")
|
self.assertEqual(_parse_platform_kit_min_version(md), "1.2.0")
|
||||||
|
|
||||||
req_path = os.path.join(get_skill_root(), "requirements.txt")
|
req_path = os.path.join(get_skill_root(), "requirements.txt")
|
||||||
with open(req_path, encoding="utf-8") as f:
|
with open(req_path, encoding="utf-8") as f:
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ FORBIDDEN_PHRASES = (
|
|||||||
|
|
||||||
POSITIVE_MARKERS = (
|
POSITIVE_MARKERS = (
|
||||||
"jiangchang-platform-kit",
|
"jiangchang-platform-kit",
|
||||||
"1.0.17",
|
"1.2.0",
|
||||||
"共享 runtime",
|
"共享 runtime",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,46 @@ from _support import IsolatedDataRoot, get_skill_root
|
|||||||
from jiangchang_skill_core import config
|
from jiangchang_skill_core import config
|
||||||
|
|
||||||
|
|
||||||
|
def _active_env_value(text: str, key: str) -> str | None:
|
||||||
|
"""Return the active (non-comment) KEY=value line from .env-style text."""
|
||||||
|
prefix = f"{key}="
|
||||||
|
for line in text.splitlines():
|
||||||
|
stripped = line.strip()
|
||||||
|
if not stripped or stripped.startswith("#"):
|
||||||
|
continue
|
||||||
|
active = stripped.split("#", 1)[0].strip()
|
||||||
|
if active.startswith(prefix):
|
||||||
|
return active
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _combined_service_source(skill_root: str | None = None) -> str:
|
||||||
|
"""Merge scripts/service/*.py source (excludes __pycache__)."""
|
||||||
|
root = skill_root or get_skill_root()
|
||||||
|
service_dir = os.path.join(root, "scripts", "service")
|
||||||
|
parts: list[str] = []
|
||||||
|
for dirpath, dirnames, filenames in os.walk(service_dir):
|
||||||
|
dirnames[:] = [d for d in dirnames if d != "__pycache__"]
|
||||||
|
for name in sorted(filenames):
|
||||||
|
if not name.endswith(".py"):
|
||||||
|
continue
|
||||||
|
path = os.path.join(dirpath, name)
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
parts.append(f.read())
|
||||||
|
return "\n".join(parts)
|
||||||
|
|
||||||
|
|
||||||
|
def _missing_video_integration_tokens(service_text: str) -> list[str]:
|
||||||
|
missing: list[str] = []
|
||||||
|
if "RpaVideoSession" not in service_text:
|
||||||
|
missing.append("RpaVideoSession")
|
||||||
|
if "video.add_step" not in service_text and ".add_step(" not in service_text:
|
||||||
|
missing.append("video.add_step or .add_step(")
|
||||||
|
if "merge_video_into_result_summary" not in service_text:
|
||||||
|
missing.append("merge_video_into_result_summary")
|
||||||
|
return missing
|
||||||
|
|
||||||
|
|
||||||
class TestEnvExampleVideoDefaults(unittest.TestCase):
|
class TestEnvExampleVideoDefaults(unittest.TestCase):
|
||||||
def test_env_example_contains_required_keys(self) -> None:
|
def test_env_example_contains_required_keys(self) -> None:
|
||||||
path = os.path.join(get_skill_root(), ".env.example")
|
path = os.path.join(get_skill_root(), ".env.example")
|
||||||
@@ -22,13 +62,45 @@ class TestEnvExampleVideoDefaults(unittest.TestCase):
|
|||||||
text = f.read()
|
text = f.read()
|
||||||
for key in (
|
for key in (
|
||||||
"OPENCLAW_" + "TEST_TARGET=mock",
|
"OPENCLAW_" + "TEST_TARGET=mock",
|
||||||
"OPENCLAW_RECORD_VIDEO=1",
|
"OPENCLAW_RECORD_VIDEO=0",
|
||||||
"OPENCLAW_ARTIFACTS_ON_FAILURE=1",
|
"OPENCLAW_ARTIFACTS_ON_FAILURE=1",
|
||||||
"OPENCLAW_BROWSER_HEADLESS=0",
|
"OPENCLAW_BROWSER_HEADLESS=0",
|
||||||
"OPENCLAW_PLAYWRIGHT_STEALTH=1",
|
"OPENCLAW_PLAYWRIGHT_STEALTH=1",
|
||||||
):
|
):
|
||||||
self.assertIn(key, text, msg=f"missing {key!r} in .env.example")
|
self.assertIn(key, text, msg=f"missing {key!r} in .env.example")
|
||||||
|
|
||||||
|
def test_env_example_default_off_but_template_keeps_video_integration(self) -> None:
|
||||||
|
path = os.path.join(get_skill_root(), ".env.example")
|
||||||
|
with open(path, encoding="utf-8") as f:
|
||||||
|
text = f.read()
|
||||||
|
self.assertEqual(
|
||||||
|
_active_env_value(text, "OPENCLAW_RECORD_VIDEO"),
|
||||||
|
"OPENCLAW_RECORD_VIDEO=0",
|
||||||
|
)
|
||||||
|
|
||||||
|
service_text = _combined_service_source()
|
||||||
|
missing = _missing_video_integration_tokens(service_text)
|
||||||
|
self.assertEqual(
|
||||||
|
missing,
|
||||||
|
[],
|
||||||
|
msg=(
|
||||||
|
"default OPENCLAW_RECORD_VIDEO=0 does not allow removing video integration; "
|
||||||
|
f"scripts/service/*.py missing: {', '.join(missing)}"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_env_example_allows_record_video_one_in_comments_only(self) -> None:
|
||||||
|
sample = "\n".join(
|
||||||
|
[
|
||||||
|
"# example: OPENCLAW_RECORD_VIDEO=1 for production",
|
||||||
|
"OPENCLAW_RECORD_VIDEO=0",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
_active_env_value(sample, "OPENCLAW_RECORD_VIDEO"),
|
||||||
|
"OPENCLAW_RECORD_VIDEO=0",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TestPrintVideoSummary(unittest.TestCase):
|
class TestPrintVideoSummary(unittest.TestCase):
|
||||||
def test_cli_prints_video_path_when_enabled(self) -> None:
|
def test_cli_prints_video_path_when_enabled(self) -> None:
|
||||||
@@ -157,5 +229,78 @@ class TestTemplateRunVideoSession(unittest.TestCase):
|
|||||||
self.assertIn("video_path", summary)
|
self.assertIn("video_path", summary)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCmdRunTaskLogCoverage(unittest.TestCase):
|
||||||
|
def test_cmd_run_entitlement_failure_writes_task_log(self) -> None:
|
||||||
|
with IsolatedDataRoot(user_id="_entitlement_fail"):
|
||||||
|
from service import task_service
|
||||||
|
|
||||||
|
with patch.object(task_service, "check_entitlement", return_value=(False, "mock denied")):
|
||||||
|
buf = io.StringIO()
|
||||||
|
with redirect_stdout(buf), redirect_stderr(io.StringIO()):
|
||||||
|
rc = task_service.cmd_run(target="t1", input_id="99")
|
||||||
|
|
||||||
|
self.assertEqual(rc, 1)
|
||||||
|
from db import task_logs_repository as tlr
|
||||||
|
|
||||||
|
rows = tlr.list_task_logs(1)
|
||||||
|
self.assertTrue(rows)
|
||||||
|
_rid, _ttype, tid, iid, _ititle, status, err, rsum, _cat, _uat = rows[0]
|
||||||
|
self.assertEqual(status, "failed")
|
||||||
|
self.assertEqual(tid, "t1")
|
||||||
|
self.assertEqual(iid, "99")
|
||||||
|
self.assertIn("mock denied", err or "")
|
||||||
|
summary = json.loads(rsum or "{}")
|
||||||
|
self.assertEqual(summary.get("stage"), "auth")
|
||||||
|
|
||||||
|
def test_cmd_run_exception_writes_task_log(self) -> None:
|
||||||
|
with IsolatedDataRoot(user_id="_exception_run"):
|
||||||
|
os.environ["OPENCLAW_RECORD_VIDEO"] = "0"
|
||||||
|
config.reset_cache()
|
||||||
|
|
||||||
|
from service import task_service
|
||||||
|
|
||||||
|
async def _boom(*_args, **_kwargs):
|
||||||
|
raise RuntimeError("boom")
|
||||||
|
|
||||||
|
with patch.object(task_service, "check_entitlement", return_value=(True, "")):
|
||||||
|
with patch.object(task_service, "_run_template_demo", side_effect=_boom):
|
||||||
|
buf = io.StringIO()
|
||||||
|
with redirect_stdout(buf), redirect_stderr(io.StringIO()):
|
||||||
|
rc = task_service.cmd_run(target="t2", input_id="88")
|
||||||
|
|
||||||
|
self.assertEqual(rc, 1)
|
||||||
|
from db import task_logs_repository as tlr
|
||||||
|
|
||||||
|
rows = tlr.list_task_logs(1)
|
||||||
|
self.assertTrue(rows)
|
||||||
|
_rid, _ttype, tid, iid, _ititle, status, err, rsum, _cat, _uat = rows[0]
|
||||||
|
self.assertEqual(status, "failed")
|
||||||
|
self.assertEqual(tid, "t2")
|
||||||
|
self.assertEqual(iid, "88")
|
||||||
|
self.assertIn("任务执行异常", err or "")
|
||||||
|
summary = json.loads(rsum or "{}")
|
||||||
|
self.assertEqual(summary.get("stage"), "run")
|
||||||
|
self.assertEqual(summary.get("error"), "unexpected_exception")
|
||||||
|
|
||||||
|
def test_get_task_logger_fallback_when_not_initialized(self) -> None:
|
||||||
|
from service import task_service
|
||||||
|
|
||||||
|
mock_logger = MagicMock()
|
||||||
|
responses = [RuntimeError("logging not initialized"), mock_logger]
|
||||||
|
|
||||||
|
def _fake_get_skill_logger():
|
||||||
|
item = responses.pop(0)
|
||||||
|
if isinstance(item, Exception):
|
||||||
|
raise item
|
||||||
|
return item
|
||||||
|
|
||||||
|
with patch.object(task_service, "get_skill_logger", side_effect=_fake_get_skill_logger):
|
||||||
|
with patch.object(task_service, "setup_skill_logging") as mock_setup:
|
||||||
|
logger = task_service._get_task_logger()
|
||||||
|
|
||||||
|
mock_setup.assert_called_once_with(task_service.SKILL_SLUG, task_service.LOG_LOGGER_NAME)
|
||||||
|
self.assertIs(logger, mock_logger)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user