Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ Loading is atomic: all pre-validation (file existence, JSON validity, conflict d

Once loaded, pipeline nodes persist in the Resource until `clear_pipeline_resources()` is called or the process exits.

### JSONC Support (issue #38)

MaaFramework's own pipeline parser (meojson `json::parsec`) accepts JSONC, so MaaMCP must too — otherwise the same file loads in MaaFramework but fails in MaaMCP.

[jsonc.py](maa_mcp/jsonc.py) re-implements that semantics in Python (zero new dependencies):

- Allowed: `//` line comments, `/* */` block comments, trailing commas in objects/arrays (a comma in an *empty* container — `{,}` / `[,]` — is still a syntax error, same as MaaFramework)
- Rejected (same as MaaFramework): unquoted keys, single-quoted strings, `NaN` / `Infinity`, duplicate keys, lone surrogates (e.g. `"\ud800"`)

Where it applies: pipeline files read by `load_pipeline` / `run_pipeline` / `benchmark_node`, the `pipeline_json` string passed to `save_pipeline` (output is still normalized standard JSON — comments are dropped), and `interface.json` read by the agent supervisor.

Implementation note: comments are replaced with equal-length whitespace (newlines preserved), then trailing commas are stripped, then the text goes to stdlib `json.loads`. Byte offsets are preserved, so stdlib's line/column error positions apply directly to the original file (a leading UTF-8 BOM shifts them by one character, since the BOM is stripped before parsing).

Three deliberate deviations from meojson:

1. Numbers that overflow to `inf` (`1e999`) are rejected — MaaMCP re-serializes the parsed result into strict JSON for `Resource.override_pipeline`, so meojson's literal round-trip would write back the invalid literal `Infinity`.
2. `loads` accepts any top-level value while meojson only accepts objects/arrays (callers still require a non-empty object).
3. Trailing residue __after__ the top-level value is reported as an error. meojson's `skip_whitespace` conflates "end of input" with "malformed comment start", so it silently accepts and discards a trailing `/x` or an unterminated block comment; MaaFramework inherits that. This direction is __stricter__ — MaaMCP never accepts a file MaaFramework rejects. An unterminated comment __inside__ the document is rejected by both.

`interface.json` caveat: MaaMCP tolerates comments there, and so does MFAAvalonia (Newtonsoft.Json skips comments). But MAAGC's own `agent/main.py` reads that file with stdlib `json.load` to detect the version — a commented `interface.json` makes it log an exception and fall back to version `"unknown"` (it only decides dev-mode vs. venv setup). Keep `interface.json` comment-free if you rely on MAAGC's version detection.

### Agent Subprocess Lifecycle (Custom Action Support)

For pipelines containing `action: "Custom"` nodes (e.g. MAAGC's `TaskProcessor` / `YearlyTaskProcessor`), MaaMCP spawns a Python agent subprocess to host the custom callbacks, following the MFAAvalonia pattern.
Expand Down Expand Up @@ -189,6 +210,12 @@ When a TemplateMatch / OCR / ColorMatch node isn't reliable, iterate this loop (

For MaaMCP-side pipeline infra testing, see `tests/test_dbg_pipeline.py` (gated by `@pytest.mark.integration`; skips if `MaaDbgControlUnit` DLL isn't shipped).

### Stress tests are opt-in — never auto-pick a window

`tests/test_performance_stress.py` drives __real__ devices/windows (clicks, swipes, typed text, key presses). It is skipped unless `MAA_STRESS=1` is set, and the target must be named explicitly via `MAA_STRESS_DEVICE` / `MAA_STRESS_WINDOW`.

Do not "fix" it by picking `find_window_list()[0]`: the first desktop window is usually the user's editor (VS Code), and the stress test's synthetic clicks/keystrokes land on it — this closed VS Code during a plain `pytest tests/` run. This mirrors the rule in [win32.py](maa_mcp/win32.py): never auto-select a window when several are available.

## Localization

- [CLAUDE_CN.md](CLAUDE_CN.md): Chinese version of this document
Expand Down
27 changes: 27 additions & 0 deletions CLAUDE_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ pytest tests/test_basic.py -v # 运行特定文件

节点一旦加载会持续驻留到 Resource,直到调用 `clear_pipeline_resources()` 或进程结束。

### JSONC 语法支持(issue #38)

MaaFramework 自带的 pipeline 解析器(meojson 的 `json::parsec`)接受 JSONC,因此 MaaMCP 也必须接受——否则同一份文件在 MaaFramework 能加载、在 MaaMCP 却报解析失败。

[jsonc.py](maa_mcp/jsonc.py) 在 Python 侧复刻了这套语义(不引入任何新依赖):

- 允许:`//` 行注释、`/* */` 块注释、对象/数组尾逗号(__空__容器里的逗号 `{,}` / `[,]` 仍是语法错误,与 MaaFramework 一致)
- 拒绝(与 MaaFramework 一致):无引号键、单引号字符串、`NaN` / `Infinity`、重复键、落单的代理字符(如 `"\ud800"`)

生效范围:`load_pipeline` / `run_pipeline` / `benchmark_node` 读取的 pipeline 文件、`save_pipeline` 的 `pipeline_json` 输入字符串(落盘仍是格式化后的标准 JSON,注释丢弃)、以及 agent supervisor 读取的 `interface.json`。

实现要点:先把注释替换成等长空白(保留换行),再剥掉尾逗号,最后交给标准库 `json.loads`。字节偏移保持不变,因此标准库自带的行列号报错可直接对到原文件(文件带 UTF-8 BOM 时位置会偏移一个字符——BOM 在解析前被剥掉)。

三处有意偏离 meojson:

1. 溢出成 `inf` 的数字(`1e999`)会被拒绝——MaaMCP 会把解析结果重新序列化成严格 JSON 再交给 `Resource.override_pipeline`,meojson 按字面量保存能原样往返,我们写回去会是非法的 `Infinity` 字面量。
2. `loads` 接受任意顶层值,而 meojson 只接受对象/数组(调用方仍要求顶层为非空对象)。
3. 顶层值__之后__的尾部残余按错误处理。meojson 的 `skip_whitespace` 把「已到输入末尾」与「注释起始符非法」混为一谈,会静默接受并丢弃尾部残余(如 `/x` 或未闭合的块注释),MaaFramework 继承了这一行为。这一处的方向是「更严」——MaaMCP 不会接受 MaaFramework 拒绝的文件;未闭合注释出现在文档__内部__时两边都拒绝。

`interface.json` 的注意事项:MaaMCP 这里容忍注释,MFAAvalonia 也容忍(Newtonsoft.Json 会跳过注释)。但 MAAGC 自己的 `agent/main.py` 用标准库 `json.load` 读同一个文件来检测版本——带注释的 `interface.json` 会让它记一条异常并回退到版本 `"unknown"`(该值只用来区分 dev 模式与 venv 准备)。依赖 MAAGC 版本检测时,`interface.json` 保持无注释。

### Agent 子进程生命周期(Custom Action 支持)

对于含 `action: "Custom"` 节点的 pipeline(如 MAAGC 的 `TaskProcessor` / `YearlyTaskProcessor`),MaaMCP 启动一个 Python agent 子进程来托管自定义回调,对齐 MFAAvalonia 模式。
Expand Down Expand Up @@ -188,6 +209,12 @@ TemplateMatch / OCR / ColorMatch 节点不稳定时,按这个循环迭代(is

MaaMCP 侧 pipeline infra 集成测试见 `tests/test_dbg_pipeline.py`(标 `@pytest.mark.integration`;缺 `MaaDbgControlUnit` DLL 时自动 skip)。

### 压力测试必须显式开启——绝不自动挑窗口

`tests/test_performance_stress.py` 会操作__真实__设备/窗口(点击、滑动、输入文本、按键)。默认整体跳过:需设置 `MAA_STRESS=1`,并用 `MAA_STRESS_DEVICE` / `MAA_STRESS_WINDOW` 显式指定目标。

不要用「取 `find_window_list()[0]`」来"修好"它:桌面窗口列表的第一项通常是用户正在用的编辑器(VS Code),压力测试合成的点击与按键会直接打上去——曾导致一次普通的 `pytest tests/` 把 VS Code 关掉。这与 [win32.py](maa_mcp/win32.py) 里的约定一致:多个窗口时绝不自动选择。

## 本地化

- [CLAUDE.md](CLAUDE.md):本文档的英文版
Expand Down
165 changes: 165 additions & 0 deletions docs/superpowers/specs/2026-11-08-jsonc-support-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# JSONC 支持设计(issue #38)

> 状态:已实现
> 关联 issue:https://github.com/MAA-AI/MaaMCP/issues/38
> 日期:2026-11-08

## 背景

MaaMCP 用标准库 `json` 读取 pipeline 文件与 `interface.json`。但 MaaFramework 自带的
pipeline 解析器(内部使用 [meojson](https://github.com/MistEO/meojson) 的 `json::parsec`)
是 JSONC 语法——允许注释与尾逗号。于是同一份文件 MaaFramework 能读、MaaMCP 报
`Expecting value`,`load_pipeline` / `run_pipeline` / `benchmark_node` / `save_pipeline`
全部受影响。

实测 MaaFramework 5.14.3(`Resource.post_pipeline`)的接受集合:

| 语法 | MaaFramework |
|---|---|
| `//` 行注释 | ✅ |
| `/* */` 块注释 | ✅ |
| 尾逗号(对象/数组) | ✅ |
| 无引号键 | ❌ |
| 单引号字符串 | ❌ |

MaaMCP 侧必须与之一致:比 MaaFramework 更宽松会让「MaaMCP 能跑、bundle 加载失败」,
更严格则会让 MaaFramework 能跑的文件在 MaaMCP 报错。

## 方案选型

### 为什么不是 meojson

meojson 是 header-only C++17 库,无 PyPI 包、无 Python binding(GitHub 全库搜索仅
C++ 仓库本身)。MaaMCP 是纯 Python pip 包,调用它需要:

1. 把头文件 vendor 进仓库;
2. 写 CPython 扩展;
3. 构建系统从 hatchling 换成 scikit-build-core;
4. 要么要求用户本机有 C++17 工具链,要么 CI 为 win/mac/linux 交叉编译 wheel。

为一个 JSON 解析功能付出这个分发代价不划算。

被否决的两条「借道 MaaFramework」的路:

- `Resource.post_pipeline()` 确实能原生解析 JSONC,但 `get_node_data()` 返回的是
MaaFramework **归一化后**的节点结构(未知字段被丢弃、默认值被填充、`next` 被改写成
对象数组),有损,不能当解析器用。
- `Resource.override_pipeline()` 走的是 meojson 的严格 `json::parse`(见 MaaFramework
仓库 `.agents/skills/meojson/SKILL.md`),所以 MaaMCP 交给它的必须是严格 JSON。

### 选定方案

在 Python 侧复刻 meojson `json::parsec` 的语义,零新依赖。

## 解析层:`maa_mcp/jsonc.py`

### 公开 API

```python
def loads(text: str) -> Any
```

- 入参:JSONC 文本(可含 BOM)。
- 返回:解析后的 Python 对象。
- 失败:抛 `json.JSONDecodeError` 子类(兼容现有 `except json.JSONDecodeError`)。

### 语义

| 语法 | 支持 |
|---|---|
| `//` 行注释、`/* */` 块注释 | ✅ |
| 对象/数组尾逗号 | ✅(空容器里的逗号 `{,}` / `[,]` 仍是语法错误,与 meojson 一致) |
| 无引号键、单引号字符串 | ❌ |
| `NaN` / `Infinity` / `-Infinity` | ❌(stdlib `json` 默认接受,需显式拦截) |
| 重复键 | ❌(meojson 同样报错) |
| 落单的代理字符(`"\ud800"`) | ❌(meojson 同样报错;stdlib 接受但会让后续编码崩溃) |
| 溢出成 `inf` 的数字(`1e999`) | ❌(见下方「有意偏离」) |
| UTF-8 BOM | ✅(自动剥离) |

### 实现

不写递归下降解析器,走「预处理 + 标准库」:

1. **剥注释**:扫描文本,跟踪字符串字面量与转义状态;注释内容替换成**等长空白**
(块注释内的换行保留),因此字节偏移与行号完全不变。
2. **拦非法字面量**:再扫一遍,识别 `NaN` / `Infinity` / `-Infinity` 并报错。
3. **剥尾逗号**:再扫一遍,把「其后(跳过空白)是 `}` 或 `]`」且「前面已经有过至少
一个成员」的 `,` 替换成空格——meojson 的解析循环必须先完成一个键值对才会收尾,
所以 `{,}`、`[,]` 是语法错误,必须同样拒绝。
4. **交给标准库**:`json.loads(cleaned, object_pairs_hook=_make_pairs_hook(doc),
parse_float=_make_parse_float(doc))`——前者拒绝重复键,后者拒绝溢出数字。
5. **修代理字符**:解析后递归处理字符串,把 `\uXXXX` 代理对还原成真实码点,
落单的代理字符直接报错。

因为偏移量没变,stdlib `JSONDecodeError` 自带的行列号(`line X column Y`)可以直接
用,不需要自己算位置(BOM 在解析前被剥掉,带 BOM 的文件位置会偏移一个字符)。

### 有意偏离 meojson 的三处

1. **溢出数字**:`1e999` 会被解析成 `inf`,序列化回去是非法的 `Infinity` 字面量。
meojson 把数字当字面量保存能原样往返,MaaMCP 不能,因此直接拒绝。
2. **顶层值类型**:meojson 只接受对象 / 数组,本模块 `loads` 接受任意顶层值
(是否合法交给调用方判断——pipeline 文件另行要求顶层为非空对象)。
3. **顶层值之后的尾部残余**:meojson 的 `skip_whitespace` 把「已到输入末尾」与
「注释起始符非法」混为一谈——顶层值之后出现 `/x` 或未闭合的块注释时,它会静默
接受并丢弃残余,MaaFramework 继承了这一行为。本模块按标准库语义报错。方向是
「更严」:MaaMCP 不会接受 MaaFramework 拒绝的文件,只会在这类病态的尾部残余上
报错。未闭合注释出现在文档**内部**时两边都拒绝,行为一致。

前两条源于 MaaMCP 与 MaaFramework 的调用方式不同——MaaMCP 要把解析结果重新序列化
成严格 JSON 再交给 `Resource.override_pipeline`。

已确认与 meojson **一致**(无需偏离):注释、尾逗号(含 `{,}` / `[,]` 这类空容器
逗号都拒绝)、重复键、NaN / Infinity、落单代理字符、顶层值之后非 `/` 开头的尾部
垃圾(两者都报错)、顶层值之后的尾部注释(两者都接受)。

### 错误信息

| 情况 | 报错 |
|---|---|
| 块注释未闭合 | `Unterminated comment: line L column C (char N)` |
| 非法 JSON | 复用 stdlib 的 msg,位置用**原文**重新抛出 |
| `NaN` / `Infinity` | `NaN is not valid JSON: line L column C` |
| 溢出数字 | `1e999 is not a finite number`(无位置) |
| 落单代理字符 | `lone surrogate in string`(无位置) |
| 重复键 | `Duplicate key 'X'`(无位置,stdlib hook 无法定位) |

## 接入点

| 位置 | 原实现 → 现实现 | 影响范围 |
|---|---|---|
| `pipeline_tools.py` `_read_and_validate_pipelines` | `json.load(fh)` → `jsonc_loads(fh.read())` | `load_pipeline` / `run_pipeline` / `benchmark_node`(三处共用该函数) |
| `pipeline_tools.py` `save_pipeline` | `json.loads(pipeline_json)` → `jsonc_loads(pipeline_json)` | 校验输入字符串;**写盘仍是 `json.dump(indent=2)` 的标准 JSON**,注释丢弃 |
| `agent_supervisor.py` `load_agent_config` | `json.loads(...)` → `jsonc_loads(...)` | `interface.json` 允许注释——与 MFAAvalonia(Newtonsoft.Json,容忍注释)对齐;MaaFramework 本身不读 `interface.json` |

## 兼容性

- 纯 JSON 是合法 JSONC → 现有行为不变,现有测试全绿。
- MaaMCP 交给 MaaFramework 的永远是**重新序列化出来的严格 JSON**(由 maafw 的
`override_pipeline` 内部做 `json.dumps`)→ MaaFramework 侧零风险。
- 新增的严格性:重复键报错、落单代理字符报错(这两条与 meojson 一致,能挡掉
「节点名写重了」「文件里有坏编码」这类真实问题),以及溢出数字报错
(这一条是上文的有意偏离,防止落盘写出非法的 `Infinity`)。

## 测试

新增 `tests/test_jsonc.py`(按仓库既有惯例整体标 `@pytest.mark.unit`):

- **解析器**:行/块注释、尾逗号、字符串内含 `//` 与 `/* */`、转义引号、未闭合块注释、
重复键、NaN/Infinity、溢出数字、代理对与落单代理、BOM、非 str 输入、错误行列号正确。
- **工具层**:带注释的 pipeline 文件能被 `_read_and_validate_pipelines` /
`load_pipeline` 读出;重复键与非 UTF-8 文件给出明确错误;`save_pipeline` 接受带注释
的输入且落盘为标准 JSON;`interface.json` 带注释时 agent 配置仍能读到,坏文件返回
None 而不抛异常。

## 文档

- `CLAUDE.md` / `CLAUDE_CN.md`:同步加一条「pipeline 文件与 interface.json 支持 JSONC」。
- `load_pipeline` / `save_pipeline` / `run_pipeline` / `benchmark_node` 的工具描述里
点明——工具描述就是 AI 读到的说明书。

## 不做的事

- 不改 `save_pipeline` 的输出格式(统一输出标准 JSON)。
- 不为 `.jsonc` 扩展名做特殊分流(MaaFramework 两种扩展名都收,MaaMCP 不按扩展名分流)。
- 不引入任何新依赖。
5 changes: 3 additions & 2 deletions maa_mcp/agent_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from maa.tasker import Tasker

from maa_mcp.core import object_registry
from maa_mcp.jsonc import loads as jsonc_loads

# ---------------------------------------------------------------------------
# 平台相关常量(Windows JobObject)
Expand Down Expand Up @@ -125,8 +126,8 @@ def load_agent_config(resource_path: Optional[str]) -> Optional[AgentConfig]:
if not cfg_path.is_file():
continue
try:
data = json.loads(cfg_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError) as e:
data = jsonc_loads(cfg_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, UnicodeDecodeError, OSError, RecursionError) as e:
logger.warning(f"解析 {cfg_path} 失败: {e}")
return None
if not isinstance(data, dict):
Expand Down
Loading
Loading