@walvka-code/proj-doc-builder 0.0.0-stage → 4.5.2
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.
- package/LICENSE +21 -0
- package/README.md +290 -3
- package/README.zh-CN.md +258 -0
- package/cordis.patch.yml +8 -0
- package/icon.svg +11 -0
- package/index.js +164 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
- package/skills/proj-doc-builder/SKILL.md +412 -0
- package/skills/proj-doc-builder/references/audit_script.md +103 -0
- package/skills/proj-doc-builder/references/batch-generation.md +55 -0
- package/skills/proj-doc-builder/references/checklist.md +90 -0
- package/skills/proj-doc-builder/references/exclusions.md +79 -0
- package/skills/proj-doc-builder/references/linking.md +83 -0
- package/skills/proj-doc-builder/references/migration.md +32 -0
- package/skills/proj-doc-builder/references/phase4.md +200 -0
- package/skills/proj-doc-builder/references/templates.md +212 -0
- package/skills/proj-doc-builder/tests/claims.json +76 -0
- package/skills/proj-doc-builder/tests/conftest.py +23 -0
- package/skills/proj-doc-builder/tests/test_claims_registry.py +81 -0
- package/skills/proj-doc-builder/tests/test_claude_doc_index.py +157 -0
- package/skills/proj-doc-builder/tests/test_e2e_fresh_project.py +156 -0
- package/skills/proj-doc-builder/tests/test_link_tools.py +279 -0
- package/skills/proj-doc-builder/tests/test_load_docs.py +353 -0
- package/skills/proj-doc-builder/tests/test_normalize_markers.py +120 -0
- package/skills/proj-doc-builder/tests/test_phase4_exit.py +3895 -0
- package/skills/proj-doc-builder/tests/test_project_docs_contract.py +812 -0
- package/skills/proj-doc-builder/tests/test_scan_dirs.py +1159 -0
- package/skills/proj-doc-builder/tests/test_stage1_stage2_upgrades.py +395 -0
- package/skills/proj-doc-builder/tests/test_sync_project_docs.py +2169 -0
- package/skills/proj-doc-builder/tests/test_v41_upgrades.py +614 -0
- package/skills/proj-doc-builder/tests/test_v42_upgrades.py +422 -0
- package/skills/proj-doc-builder/tests/test_v43_upgrades.py +198 -0
- package/skills/proj-doc-builder/tests/test_v44_upgrades.py +361 -0
- package/skills/proj-doc-builder/tests/test_v45_upgrades.py +212 -0
- package/skills/proj-doc-builder/tests/test_validate_doc.py +712 -0
- package/skills/proj-doc-builder/tools/_paths.py +39 -0
- package/skills/proj-doc-builder/tools/core/claude_doc_index.py +504 -0
- package/skills/proj-doc-builder/tools/core/compute_link.py +239 -0
- package/skills/proj-doc-builder/tools/core/proj_doc_config.py +460 -0
- package/skills/proj-doc-builder/tools/core/project_docs_contract.py +2725 -0
- package/skills/proj-doc-builder/tools/phase4/__init__.py +82 -0
- package/skills/proj-doc-builder/tools/phase4/cas_storage.py +25 -0
- package/skills/proj-doc-builder/tools/phase4/engine.py +35 -0
- package/skills/proj-doc-builder/tools/phase4/journal.py +29 -0
- package/skills/proj-doc-builder/tools/phase4/platform_handles.py +27 -0
- package/skills/proj-doc-builder/tools/phase4_exit.py +8565 -0
- package/skills/proj-doc-builder/tools/pipeline/gen_docs_from_scaffold.py +789 -0
- package/skills/proj-doc-builder/tools/pipeline/sync_project_docs.py +5531 -0
- package/skills/proj-doc-builder/tools/reader/load_docs.py +654 -0
- package/skills/proj-doc-builder/tools/scan/link_check.py +203 -0
- package/skills/proj-doc-builder/tools/scan/normalize_markers.py +211 -0
- package/skills/proj-doc-builder/tools/scan/scan_dirs.py +2735 -0
- package/skills/proj-doc-builder/tools/validate/validate_doc.py +1911 -0
|
@@ -0,0 +1,712 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Focused tests for ``tools/validate/validate_doc.py``."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import json
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
import validate_doc as validator
|
|
12
|
+
|
|
13
|
+
STANDARD_OVERVIEW = (
|
|
14
|
+
"本目录属于测试子系统中的算法模块,定位为接收协议向量并产出验证结论的执行单元。"
|
|
15
|
+
"它从上游协议层 `proto` 接收结构化测试向量与配置参数。"
|
|
16
|
+
"核心处理由 `PublicApi` 与 `run_case` 协作完成确定性计算。"
|
|
17
|
+
"处理结果以 `Result` 结构交付给下游验证组件 `verifier`。"
|
|
18
|
+
"本目录不负责测试调度与报告生成,仅承担单用例的执行。"
|
|
19
|
+
"失败路径通过 `DEFAULT_MODE` 切换降级,并在异常时回滚中间产物。"
|
|
20
|
+
"与同级 `adapter` 目录共享测试上下文总线但无写竞争。"
|
|
21
|
+
"文件清单含 `__init__.py` 与 `core.py` 两个直接文件。"
|
|
22
|
+
)
|
|
23
|
+
STANDARD_ROLE = (
|
|
24
|
+
"本目录位于测试子系统的数据层,作为算法模块接收上游协议栈传入的测试向量。"
|
|
25
|
+
"它依赖核心接口完成处理,并把输出结果传递给下游验证组件,同时与同级适配器"
|
|
26
|
+
"协作保持数据格式一致。"
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _write(path: Path, content: str) -> None:
|
|
31
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
32
|
+
path.write_text(content, encoding="utf-8")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _standard_document(*, overview: str = STANDARD_OVERVIEW) -> str:
|
|
36
|
+
return f"""# module — 标准测试模块
|
|
37
|
+
|
|
38
|
+
**导航**: src > **module**
|
|
39
|
+
|
|
40
|
+
## 概述
|
|
41
|
+
|
|
42
|
+
{overview}
|
|
43
|
+
|
|
44
|
+
## 文件清单
|
|
45
|
+
|
|
46
|
+
| 文件 | 用途 | 关键内容 |
|
|
47
|
+
|------|------|---------|
|
|
48
|
+
| `__init__.py` | 包入口 | 导出公共接口 |
|
|
49
|
+
| `core.py` | 核心实现 | 定义执行流程 |
|
|
50
|
+
|
|
51
|
+
## 关键符号
|
|
52
|
+
|
|
53
|
+
| 符号 | 类型 | 定义位置 | 说明 |
|
|
54
|
+
|------|------|---------|------|
|
|
55
|
+
| `PublicApi` | class | `core.py:L10` | 公共接口 |
|
|
56
|
+
| `run_case` | function | `core.py:L20` | 执行用例 |
|
|
57
|
+
| `DEFAULT_MODE` | constant | `core.py:L5` | 默认模式 |
|
|
58
|
+
|
|
59
|
+
## 子目录
|
|
60
|
+
|
|
61
|
+
| 子目录 | 用途 | 文档 |
|
|
62
|
+
|--------|------|------|
|
|
63
|
+
| `child/` | 子模块实现 | [child.md](module/child.md) |
|
|
64
|
+
|
|
65
|
+
## 依赖关系
|
|
66
|
+
|
|
67
|
+
| 方向 | 模块 | 关系 | 文档 |
|
|
68
|
+
|------|------|------|------|
|
|
69
|
+
| ⬆ 上游 | parent | 依赖父模块提供的核心接口定义 | [parent.md](parent.md) |
|
|
70
|
+
|
|
71
|
+
## 系统角色
|
|
72
|
+
|
|
73
|
+
{STANDARD_ROLE}
|
|
74
|
+
|
|
75
|
+
## 数据与状态
|
|
76
|
+
|
|
77
|
+
本目录读取 `core.py` 中的 `DEFAULT_MODE` 常量作为运行期开关,不写入持久化文件。
|
|
78
|
+
跨进程状态由上层 `project_docs` 契约管理,本目录不持有独立锁或 journal。
|
|
79
|
+
|
|
80
|
+
## 性能与资源特征
|
|
81
|
+
|
|
82
|
+
符号提取对 `core.py` 做单次 AST 解析,复杂度与文件数线性相关,当前量级为 O(2)。
|
|
83
|
+
无并发 worker 常驻,单次校验内存占用与文档字数成正比。
|
|
84
|
+
|
|
85
|
+
## 变更与迁移注意
|
|
86
|
+
|
|
87
|
+
本文档遵循 doc_contract_version=4.2,接口 `PublicApi.run_case` 自 v4.1 起保持签名不变。
|
|
88
|
+
契约升级时通过 `--upgrade-plan` 报告差异,再以 `--migrate-manifest` 迁移。
|
|
89
|
+
|
|
90
|
+
## 状态
|
|
91
|
+
|
|
92
|
+
✅ 完整 — 标准模块已经实现。
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _make_standard_tree(
|
|
97
|
+
tmp_path: Path,
|
|
98
|
+
*,
|
|
99
|
+
overview: str = STANDARD_OVERVIEW,
|
|
100
|
+
) -> tuple[Path, Path, Path, Path]:
|
|
101
|
+
project_root = tmp_path / "project"
|
|
102
|
+
docs_root = project_root / "project_docs"
|
|
103
|
+
source_dir = project_root / "src" / "module"
|
|
104
|
+
source_dir.mkdir(parents=True)
|
|
105
|
+
(source_dir / "child").mkdir()
|
|
106
|
+
_write(source_dir / "__init__.py", "# -*- coding: utf-8 -*-\n")
|
|
107
|
+
_write(
|
|
108
|
+
source_dir / "core.py",
|
|
109
|
+
"# -*- coding: utf-8 -*-\n"
|
|
110
|
+
"DEFAULT_MODE = 'test'\n\n"
|
|
111
|
+
"class PublicApi:\n"
|
|
112
|
+
" pass\n\n"
|
|
113
|
+
"def run_case():\n"
|
|
114
|
+
" pass\n",
|
|
115
|
+
)
|
|
116
|
+
_write(docs_root / "src" / "parent.md", "# parent\n")
|
|
117
|
+
_write(docs_root / "src" / "module" / "child.md", "# child\n")
|
|
118
|
+
doc_path = docs_root / "src" / "module.md"
|
|
119
|
+
_write(doc_path, _standard_document(overview=overview))
|
|
120
|
+
return project_root, docs_root, source_dir, doc_path
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _validate_standard(
|
|
124
|
+
project_root: Path,
|
|
125
|
+
docs_root: Path,
|
|
126
|
+
source_dir: Path,
|
|
127
|
+
doc_path: Path,
|
|
128
|
+
**kwargs: object,
|
|
129
|
+
) -> dict:
|
|
130
|
+
return validator.validate_doc(
|
|
131
|
+
doc_path,
|
|
132
|
+
source_dir,
|
|
133
|
+
project_docs_dir=docs_root,
|
|
134
|
+
project_root=project_root,
|
|
135
|
+
**kwargs,
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _check(result: dict, check_id: int) -> dict:
|
|
140
|
+
return next(item for item in result["checks"] if item["id"] == check_id)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def test_standard_profile_passes_exact_inventory_links_and_content(
|
|
144
|
+
tmp_path: Path,
|
|
145
|
+
) -> None:
|
|
146
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
147
|
+
|
|
148
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
149
|
+
|
|
150
|
+
assert result["profile"] == validator.PROFILE_STANDARD
|
|
151
|
+
assert result["qualified"] is True
|
|
152
|
+
assert result["ok"] is True
|
|
153
|
+
assert result["failed"] == 0
|
|
154
|
+
assert result["critical_failed"] == 0
|
|
155
|
+
assert len(result["checks"]) == 12
|
|
156
|
+
assert _check(result, 1)["result"] == "PASS"
|
|
157
|
+
assert _check(result, 4)["result"] == "PASS"
|
|
158
|
+
assert _check(result, 9)["result"] == "PASS"
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@pytest.mark.parametrize(
|
|
162
|
+
"overview, sentence_count",
|
|
163
|
+
[
|
|
164
|
+
("本目录属于测试模块。它向下游输出结果。", 2),
|
|
165
|
+
(
|
|
166
|
+
"本目录属于测试模块。它接收输入。它执行处理。它输出结果。它记录状态。"
|
|
167
|
+
"它读取配置。它校验参数。",
|
|
168
|
+
7,
|
|
169
|
+
),
|
|
170
|
+
],
|
|
171
|
+
)
|
|
172
|
+
def test_standard_overview_requires_at_least_eight_sentences(
|
|
173
|
+
tmp_path: Path,
|
|
174
|
+
overview: str,
|
|
175
|
+
sentence_count: int,
|
|
176
|
+
) -> None:
|
|
177
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(
|
|
178
|
+
tmp_path,
|
|
179
|
+
overview=overview,
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
183
|
+
|
|
184
|
+
overview_check = _check(result, 2)
|
|
185
|
+
assert overview_check["result"] == "FAIL"
|
|
186
|
+
assert f"{sentence_count} 句话" in overview_check["detail"]
|
|
187
|
+
assert overview_check["severity"] == "advisory"
|
|
188
|
+
assert result["critical_failed"] == 0
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def test_standard_role_and_dependency_must_be_substantive(tmp_path: Path) -> None:
|
|
192
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
193
|
+
content = doc_path.read_text(encoding="utf-8")
|
|
194
|
+
content = content.replace(
|
|
195
|
+
STANDARD_ROLE,
|
|
196
|
+
"这里记录了一段长度足够但没有架构位置和协作含义的普通说明文字,"
|
|
197
|
+
"只是反复描述当前目录已经存在并包含若干内容,不说明实际数据关系。",
|
|
198
|
+
).replace(
|
|
199
|
+
"依赖父模块提供的核心接口定义",
|
|
200
|
+
"待补充",
|
|
201
|
+
)
|
|
202
|
+
_write(doc_path, content)
|
|
203
|
+
|
|
204
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
205
|
+
|
|
206
|
+
role_check = _check(result, 3)
|
|
207
|
+
dependency_check = _check(result, 9)
|
|
208
|
+
assert role_check["result"] == "FAIL"
|
|
209
|
+
assert "所属子系统、协议层或模块" in role_check["detail"]
|
|
210
|
+
assert dependency_check["result"] == "FAIL"
|
|
211
|
+
assert "关系说明不够具体" in dependency_check["detail"]
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_placeholder_profile_does_not_require_three_symbols_or_long_role(
|
|
215
|
+
tmp_path: Path,
|
|
216
|
+
) -> None:
|
|
217
|
+
project_root = tmp_path / "project"
|
|
218
|
+
docs_root = project_root / "project_docs"
|
|
219
|
+
source_dir = project_root / "src" / "empty"
|
|
220
|
+
source_dir.mkdir(parents=True)
|
|
221
|
+
_write(source_dir / "__init__.py", "# -*- coding: utf-8 -*-\n")
|
|
222
|
+
_write(docs_root / "src.md", "# src\n")
|
|
223
|
+
doc_path = docs_root / "src" / "empty.md"
|
|
224
|
+
_write(
|
|
225
|
+
doc_path,
|
|
226
|
+
"""# empty — 占位包
|
|
227
|
+
|
|
228
|
+
**导航**: src > **empty**
|
|
229
|
+
|
|
230
|
+
## 概述
|
|
231
|
+
|
|
232
|
+
本目录当前为占位。待后续实现。
|
|
233
|
+
|
|
234
|
+
## 文件清单
|
|
235
|
+
|
|
236
|
+
| 文件 | 用途 |
|
|
237
|
+
|------|------|
|
|
238
|
+
| `__init__.py` | 包初始化文件 |
|
|
239
|
+
|
|
240
|
+
## 系统角色
|
|
241
|
+
|
|
242
|
+
> 参见 [父目录文档](../src.md)。
|
|
243
|
+
|
|
244
|
+
## 状态
|
|
245
|
+
|
|
246
|
+
⬜ 占位 — 目录已建立,待实现。
|
|
247
|
+
""",
|
|
248
|
+
)
|
|
249
|
+
|
|
250
|
+
result = validator.validate_doc(
|
|
251
|
+
doc_path,
|
|
252
|
+
source_dir,
|
|
253
|
+
project_docs_dir=docs_root,
|
|
254
|
+
project_root=project_root,
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
assert result["profile"] == validator.PROFILE_PLACEHOLDER
|
|
258
|
+
assert result["qualified"] is True
|
|
259
|
+
assert _check(result, 2)["result"] == "PASS"
|
|
260
|
+
assert _check(result, 3)["result"] == "PASS"
|
|
261
|
+
assert _check(result, 6)["result"] == "N/A"
|
|
262
|
+
assert _check(result, 9)["result"] == "N/A"
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def test_file_manifest_reports_missing_extra_and_duplicate_rows(tmp_path: Path) -> None:
|
|
266
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
267
|
+
content = doc_path.read_text(encoding="utf-8")
|
|
268
|
+
content = content.replace(
|
|
269
|
+
"| `core.py` | 核心实现 | 定义执行流程 |",
|
|
270
|
+
"| `ghost.py` | 错误条目 | 不存在 |\n" "| `ghost.py` | 重复错误条目 | 不存在 |",
|
|
271
|
+
)
|
|
272
|
+
_write(doc_path, content)
|
|
273
|
+
|
|
274
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
275
|
+
|
|
276
|
+
file_check = _check(result, 1)
|
|
277
|
+
assert file_check["result"] == "FAIL"
|
|
278
|
+
assert "遗漏文件: core.py" in file_check["detail"]
|
|
279
|
+
assert "多余文件: ghost.py" in file_check["detail"]
|
|
280
|
+
assert "重复文件: ghost.py" in file_check["detail"]
|
|
281
|
+
assert result["critical_failed"] >= 1
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def test_subdirectory_manifest_requires_exact_existing_markdown_target(
|
|
285
|
+
tmp_path: Path,
|
|
286
|
+
) -> None:
|
|
287
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
288
|
+
content = doc_path.read_text(encoding="utf-8").replace(
|
|
289
|
+
"[child.md](module/child.md)",
|
|
290
|
+
"[wrong.md](parent.md)",
|
|
291
|
+
)
|
|
292
|
+
_write(doc_path, content)
|
|
293
|
+
|
|
294
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
295
|
+
|
|
296
|
+
subdir_check = _check(result, 4)
|
|
297
|
+
assert subdir_check["result"] == "FAIL"
|
|
298
|
+
assert "链接目标错误" in subdir_check["detail"]
|
|
299
|
+
assert "src/module/child.md" in subdir_check["detail"]
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
def test_unsafe_markdown_link_is_a_critical_error(tmp_path: Path) -> None:
|
|
303
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
304
|
+
content = doc_path.read_text(encoding="utf-8").replace(
|
|
305
|
+
STANDARD_ROLE,
|
|
306
|
+
STANDARD_ROLE + " [越界链接](../../../outside.md)",
|
|
307
|
+
)
|
|
308
|
+
_write(doc_path, content)
|
|
309
|
+
|
|
310
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
311
|
+
|
|
312
|
+
assert result["qualified"] is False
|
|
313
|
+
assert result["critical"] is True
|
|
314
|
+
assert result["critical_failed"] >= 1
|
|
315
|
+
assert result["error_code"] == "unsafe-markdown-link"
|
|
316
|
+
assert "target-outside-docs-root" in result["error"]
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def test_document_and_source_paths_cannot_escape_trusted_roots(tmp_path: Path) -> None:
|
|
320
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
321
|
+
outside_doc = tmp_path / "outside.md"
|
|
322
|
+
_write(outside_doc, _standard_document())
|
|
323
|
+
outside_source = tmp_path / "outside-source"
|
|
324
|
+
outside_source.mkdir()
|
|
325
|
+
|
|
326
|
+
escaped_doc = validator.validate_doc(
|
|
327
|
+
outside_doc,
|
|
328
|
+
source_dir,
|
|
329
|
+
project_docs_dir=docs_root,
|
|
330
|
+
project_root=project_root,
|
|
331
|
+
)
|
|
332
|
+
escaped_source = validator.validate_doc(
|
|
333
|
+
doc_path,
|
|
334
|
+
outside_source,
|
|
335
|
+
project_docs_dir=docs_root,
|
|
336
|
+
project_root=project_root,
|
|
337
|
+
)
|
|
338
|
+
unbounded_doc = validator.validate_doc(outside_doc, source_dir)
|
|
339
|
+
|
|
340
|
+
assert escaped_doc["error_code"] == "path-or-input-error"
|
|
341
|
+
assert "越界" in escaped_doc["error"]
|
|
342
|
+
assert escaped_source["error_code"] == "path-or-input-error"
|
|
343
|
+
assert "越过项目根目录" in escaped_source["error"]
|
|
344
|
+
assert unbounded_doc["error_code"] == "path-or-input-error"
|
|
345
|
+
assert "project_docs" in unbounded_doc["error"]
|
|
346
|
+
assert escaped_doc["critical_failed"] == 1
|
|
347
|
+
assert escaped_source["critical_failed"] == 1
|
|
348
|
+
assert unbounded_doc["critical_failed"] == 1
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def test_validate_index_doc_against_root(tmp_path: Path) -> None:
|
|
352
|
+
project_root = tmp_path / "project"
|
|
353
|
+
docs_root = project_root / "project_docs"
|
|
354
|
+
docs_root.mkdir(parents=True)
|
|
355
|
+
(project_root / "main.py").write_text("def main():\n pass\n", encoding="utf-8")
|
|
356
|
+
(project_root / "src").mkdir()
|
|
357
|
+
(docs_root / "src.md").write_text("# src\n", encoding="utf-8")
|
|
358
|
+
|
|
359
|
+
index_content = """# index — 项目全息总览
|
|
360
|
+
|
|
361
|
+
**导航**: **项目全息总览**
|
|
362
|
+
|
|
363
|
+
## 概述
|
|
364
|
+
|
|
365
|
+
系统提供统一端到端量化分析与自动交易能力,定位为顶层总控与架构拓扑中枢。
|
|
366
|
+
微内核分层架构解耦数据采集、因子计算与执行撮合,由 `main` 入口统一调度。
|
|
367
|
+
上游从数据中台接收行情快照与配置参数,经事件总线分发到研判与执行子系统。
|
|
368
|
+
核心处理由 `微内核` 与 `动态研判` 协作完成策略生成与风控校验。
|
|
369
|
+
交付产物为交易指令与研报,向 `交易执行` 子系统输出。
|
|
370
|
+
本目录不承担单用例测试与日志归档,仅做顶层编排。
|
|
371
|
+
失败路径通过事件总线回滚未完成事务并降级为只读研报模式。
|
|
372
|
+
顶层直接文件 `main.py` 与子目录 `src/` 共同构成运行入口。
|
|
373
|
+
|
|
374
|
+
## 文件清单
|
|
375
|
+
|
|
376
|
+
| 文件 | 用途 | 关键内容 |
|
|
377
|
+
|---|---|---|
|
|
378
|
+
| `main.py` | 统一主入口 | `main` |
|
|
379
|
+
|
|
380
|
+
## 子目录
|
|
381
|
+
|
|
382
|
+
| 子目录 | 用途 | 文档 |
|
|
383
|
+
|---|---|---|
|
|
384
|
+
| `src/` | 核心源码 | [src.md](src.md) |
|
|
385
|
+
|
|
386
|
+
## 关键符号
|
|
387
|
+
|
|
388
|
+
| 符号 | 类型 | 定义位置 | 说明 |
|
|
389
|
+
|---|---|---|---|
|
|
390
|
+
| `main` | function | `main.py:L1` | 启动入口 |
|
|
391
|
+
|
|
392
|
+
## 依赖关系
|
|
393
|
+
|
|
394
|
+
| 方向 | 模块 | 关系 | 文档 |
|
|
395
|
+
|---|---|---|---|
|
|
396
|
+
| ⬇ 下游 | `src` | 驱动核心计算流水线 | [src.md](src.md) |
|
|
397
|
+
|
|
398
|
+
## 系统角色
|
|
399
|
+
|
|
400
|
+
本模块作为系统的顶层总控与架构拓扑中枢,统一调用底层微内核、数据中台、动态研判与交易执行交付子系统,为下游提供统一服务接口。
|
|
401
|
+
|
|
402
|
+
## 数据与状态
|
|
403
|
+
|
|
404
|
+
顶层目录不直接持有持久化数据,运行期状态由事件总线在内存中流转。
|
|
405
|
+
跨进程协调通过 `project_docs` 契约定义的路径约定,无独立锁。
|
|
406
|
+
|
|
407
|
+
## 性能与资源特征
|
|
408
|
+
|
|
409
|
+
单次启动按子目录线性分发任务,开销与子目录数成正比(当前量级 O(2))。
|
|
410
|
+
无并发 worker 常驻,顶层编排延迟主要来自下游子系统的处理时长。
|
|
411
|
+
|
|
412
|
+
## 变更与迁移注意
|
|
413
|
+
|
|
414
|
+
本文档遵循 doc_contract_version=4.2,`main` 入口签名自首版保持不变。
|
|
415
|
+
契约升级时通过 `--upgrade-plan` 报告差异,再以 `--migrate-manifest` 迁移。
|
|
416
|
+
|
|
417
|
+
## 状态
|
|
418
|
+
|
|
419
|
+
✅ 完整
|
|
420
|
+
"""
|
|
421
|
+
index_doc = docs_root / "index.md"
|
|
422
|
+
_write(index_doc, index_content)
|
|
423
|
+
|
|
424
|
+
result = validator.validate_doc(
|
|
425
|
+
index_doc,
|
|
426
|
+
project_root,
|
|
427
|
+
project_docs_dir=docs_root,
|
|
428
|
+
project_root=project_root,
|
|
429
|
+
)
|
|
430
|
+
assert result["qualified"] is True
|
|
431
|
+
assert result["failed"] == 0
|
|
432
|
+
assert result["critical_failed"] == 0
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def test_symbol_table_rejects_hallucinated_symbol(tmp_path: Path) -> None:
|
|
437
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
438
|
+
content = doc_path.read_text(encoding="utf-8").replace(
|
|
439
|
+
"| `PublicApi` | class | `core.py:L10` | 公共接口 |",
|
|
440
|
+
"| `HallucinatedClass` | class | `core.py:L10` | 虚构的类 |",
|
|
441
|
+
)
|
|
442
|
+
_write(doc_path, content)
|
|
443
|
+
|
|
444
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
445
|
+
symbol_check = _check(result, 6)
|
|
446
|
+
assert symbol_check["result"] == "FAIL"
|
|
447
|
+
assert "未在 core.py 的 AST 定义中找到" in symbol_check["detail"]
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def test_banned_boilerplate_phrases(tmp_path: Path) -> None:
|
|
451
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(tmp_path)
|
|
452
|
+
content = doc_path.read_text(encoding="utf-8").replace(
|
|
453
|
+
"定义执行流程",
|
|
454
|
+
"核心业务逻辑与基础设施功能",
|
|
455
|
+
)
|
|
456
|
+
_write(doc_path, content)
|
|
457
|
+
|
|
458
|
+
result = _validate_standard(project_root, docs_root, source_dir, doc_path)
|
|
459
|
+
banned_check = _check(result, 7)
|
|
460
|
+
assert banned_check["result"] == "FAIL"
|
|
461
|
+
assert "核心业务逻辑与基础设施功能" in banned_check["detail"]
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
def test_missing_and_invalid_utf8_documents_return_complete_stable_results(
|
|
466
|
+
tmp_path: Path,
|
|
467
|
+
) -> None:
|
|
468
|
+
project_root = tmp_path / "project"
|
|
469
|
+
docs_root = project_root / "project_docs"
|
|
470
|
+
docs_root.mkdir(parents=True)
|
|
471
|
+
source_dir = project_root / "src" / "module"
|
|
472
|
+
source_dir.mkdir(parents=True)
|
|
473
|
+
missing = docs_root / "src" / "missing.md"
|
|
474
|
+
bad_utf8 = docs_root / "src" / "bad.md"
|
|
475
|
+
bad_utf8.parent.mkdir(parents=True)
|
|
476
|
+
bad_utf8.write_bytes(b"# title\n\xff")
|
|
477
|
+
|
|
478
|
+
missing_result = validator.validate_doc(
|
|
479
|
+
missing,
|
|
480
|
+
source_dir,
|
|
481
|
+
project_docs_dir=docs_root,
|
|
482
|
+
project_root=project_root,
|
|
483
|
+
)
|
|
484
|
+
bad_result = validator.validate_doc(
|
|
485
|
+
bad_utf8,
|
|
486
|
+
source_dir,
|
|
487
|
+
project_docs_dir=docs_root,
|
|
488
|
+
project_root=project_root,
|
|
489
|
+
)
|
|
490
|
+
|
|
491
|
+
expected_keys = {
|
|
492
|
+
"schema_version",
|
|
493
|
+
"file",
|
|
494
|
+
"source_dir",
|
|
495
|
+
"requested_profile",
|
|
496
|
+
"profile",
|
|
497
|
+
"checks",
|
|
498
|
+
"passed",
|
|
499
|
+
"failed",
|
|
500
|
+
"na",
|
|
501
|
+
"known_issue_count",
|
|
502
|
+
"known_issues",
|
|
503
|
+
"advisories",
|
|
504
|
+
"critical_failed",
|
|
505
|
+
"critical",
|
|
506
|
+
"qualified",
|
|
507
|
+
"ok",
|
|
508
|
+
"error_code",
|
|
509
|
+
"error",
|
|
510
|
+
"errors",
|
|
511
|
+
}
|
|
512
|
+
assert set(missing_result) == expected_keys
|
|
513
|
+
assert set(bad_result) == expected_keys
|
|
514
|
+
assert len(missing_result["checks"]) == 12
|
|
515
|
+
assert len(bad_result["checks"]) == 12
|
|
516
|
+
assert missing_result["na"] == 12
|
|
517
|
+
assert bad_result["na"] == 12
|
|
518
|
+
assert missing_result["error_code"] == "document-not-found"
|
|
519
|
+
assert bad_result["error_code"] == "invalid-utf8"
|
|
520
|
+
assert missing_result["qualified"] is False
|
|
521
|
+
assert bad_result["qualified"] is False
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
def test_cli_json_is_pure_and_missing_file_exits_critical(
|
|
525
|
+
tmp_path: Path,
|
|
526
|
+
capsys: pytest.CaptureFixture[str],
|
|
527
|
+
) -> None:
|
|
528
|
+
project_root = tmp_path / "project"
|
|
529
|
+
docs_root = project_root / "project_docs"
|
|
530
|
+
docs_root.mkdir(parents=True)
|
|
531
|
+
source_dir = project_root / "src" / "module"
|
|
532
|
+
source_dir.mkdir(parents=True)
|
|
533
|
+
missing = docs_root / "src" / "missing.md"
|
|
534
|
+
|
|
535
|
+
exit_code = validator.main(
|
|
536
|
+
[
|
|
537
|
+
str(missing),
|
|
538
|
+
"--source-dir",
|
|
539
|
+
str(source_dir),
|
|
540
|
+
"--project-docs",
|
|
541
|
+
str(docs_root),
|
|
542
|
+
"--project-root",
|
|
543
|
+
str(project_root),
|
|
544
|
+
"--json",
|
|
545
|
+
]
|
|
546
|
+
)
|
|
547
|
+
captured = capsys.readouterr()
|
|
548
|
+
payload = json.loads(captured.out)
|
|
549
|
+
|
|
550
|
+
assert exit_code == validator.EXIT_CRITICAL_ERROR
|
|
551
|
+
assert payload["error_code"] == "document-not-found"
|
|
552
|
+
assert payload["qualified"] is False
|
|
553
|
+
assert captured.err == ""
|
|
554
|
+
assert captured.out.lstrip().startswith("{")
|
|
555
|
+
assert captured.out.rstrip().endswith("}")
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
def test_cli_exit_policy_distinguishes_advisory_strict_and_critical(
|
|
559
|
+
tmp_path: Path,
|
|
560
|
+
capsys: pytest.CaptureFixture[str],
|
|
561
|
+
) -> None:
|
|
562
|
+
project_root, docs_root, source_dir, doc_path = _make_standard_tree(
|
|
563
|
+
tmp_path,
|
|
564
|
+
overview="本目录属于测试模块。它向下游输出结果。",
|
|
565
|
+
)
|
|
566
|
+
args = [
|
|
567
|
+
str(doc_path),
|
|
568
|
+
"--source_dir",
|
|
569
|
+
str(source_dir),
|
|
570
|
+
"--project-docs",
|
|
571
|
+
str(docs_root),
|
|
572
|
+
"--project-root",
|
|
573
|
+
str(project_root),
|
|
574
|
+
"--json",
|
|
575
|
+
]
|
|
576
|
+
|
|
577
|
+
advisory_exit = validator.main(args)
|
|
578
|
+
advisory_output = capsys.readouterr()
|
|
579
|
+
strict_exit = validator.main([*args, "--strict"])
|
|
580
|
+
strict_output = capsys.readouterr()
|
|
581
|
+
|
|
582
|
+
assert advisory_exit == validator.EXIT_OK
|
|
583
|
+
assert strict_exit == validator.EXIT_VALIDATION_FAILED
|
|
584
|
+
assert json.loads(advisory_output.out)["failed"] == 1
|
|
585
|
+
assert json.loads(strict_output.out)["failed"] == 1
|
|
586
|
+
assert advisory_output.err == ""
|
|
587
|
+
assert strict_output.err == ""
|
|
588
|
+
|
|
589
|
+
content = doc_path.read_text(encoding="utf-8").replace(
|
|
590
|
+
"| `core.py` | 核心实现 | 定义执行流程 |",
|
|
591
|
+
"| `ghost.py` | 错误条目 | 不存在 |",
|
|
592
|
+
)
|
|
593
|
+
_write(doc_path, content)
|
|
594
|
+
critical_exit = validator.main(args)
|
|
595
|
+
critical_output = capsys.readouterr()
|
|
596
|
+
|
|
597
|
+
assert critical_exit == validator.EXIT_VALIDATION_FAILED
|
|
598
|
+
assert json.loads(critical_output.out)["critical_failed"] >= 1
|
|
599
|
+
assert critical_output.err == ""
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
def _rich_section_doc(section: str, body: str) -> str:
|
|
603
|
+
"""Minimal standard-profile document carrying one v4.5 rich section."""
|
|
604
|
+
|
|
605
|
+
return (
|
|
606
|
+
"# 模块文档\n\n## 概述\n\n"
|
|
607
|
+
+ STANDARD_OVERVIEW
|
|
608
|
+
+ "\n\n## 数据与状态\n\n"
|
|
609
|
+
+ (body if section == "数据与状态" else "状态落在 `_manifest.json`,无进程内缓存。\n进程退出后由磁盘文件恢复。")
|
|
610
|
+
+ "\n\n## 性能与资源特征\n\n"
|
|
611
|
+
+ (body if section == "性能与资源特征" else "按目录数线性扫描,复杂度 O(n)。\n单次运行内存占用与文档数成正比。")
|
|
612
|
+
+ "\n\n## 变更与迁移注意\n\n"
|
|
613
|
+
+ (body if section == "变更与迁移注意" else "契约版本变化时先跑 `--upgrade-plan` 查看差异。\n再用 `--migrate-manifest` 迁移旧清单。")
|
|
614
|
+
+ "\n"
|
|
615
|
+
)
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
@pytest.mark.parametrize(
|
|
619
|
+
("check", "section"),
|
|
620
|
+
[
|
|
621
|
+
(validator.check_10_data_state, "数据与状态"),
|
|
622
|
+
(validator.check_11_perf_resources, "性能与资源特征"),
|
|
623
|
+
(validator.check_12_change_migration, "变更与迁移注意"),
|
|
624
|
+
],
|
|
625
|
+
)
|
|
626
|
+
def test_rich_sections_accept_declared_not_applicable_with_reason(check, section) -> None:
|
|
627
|
+
content = _rich_section_doc(section, "本节不适用:该目录只做纯函数计算,不持有任何持久化数据。")
|
|
628
|
+
|
|
629
|
+
ok, detail = check(content)
|
|
630
|
+
|
|
631
|
+
assert ok is True, detail
|
|
632
|
+
assert "不适用" in detail
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
@pytest.mark.parametrize(
|
|
636
|
+
("check", "section"),
|
|
637
|
+
[
|
|
638
|
+
(validator.check_10_data_state, "数据与状态"),
|
|
639
|
+
(validator.check_11_perf_resources, "性能与资源特征"),
|
|
640
|
+
(validator.check_12_change_migration, "变更与迁移注意"),
|
|
641
|
+
],
|
|
642
|
+
)
|
|
643
|
+
def test_rich_sections_reject_not_applicable_without_reason(check, section) -> None:
|
|
644
|
+
content = _rich_section_doc(section, "本节不适用:")
|
|
645
|
+
|
|
646
|
+
ok, detail = check(content)
|
|
647
|
+
|
|
648
|
+
assert ok is False
|
|
649
|
+
assert "未给出具体理由" in detail
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
def test_rich_section_reason_strips_whitespace_not_letters() -> None:
|
|
653
|
+
"""Regression: the not-applicable reason stripper must trim whitespace and
|
|
654
|
+
punctuation, never the ASCII letters n/t (a prior build used ``\\n\\t``
|
|
655
|
+
in the source, which Python evaluated to backslash+n and stripped letters)."""
|
|
656
|
+
|
|
657
|
+
reason = "nnnnnn"
|
|
658
|
+
assert len(reason) == 6
|
|
659
|
+
content = _rich_section_doc("数据与状态", f"本节不适用:{reason}")
|
|
660
|
+
|
|
661
|
+
ok, detail = validator.check_10_data_state(content)
|
|
662
|
+
|
|
663
|
+
assert ok is True, detail
|
|
664
|
+
assert "不适用" in detail
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
def test_rich_section_reason_trims_trailing_newline() -> None:
|
|
668
|
+
"""A reason followed by stray whitespace must still be measured by its text."""
|
|
669
|
+
|
|
670
|
+
content = _rich_section_doc(
|
|
671
|
+
"数据与状态",
|
|
672
|
+
"本节不适用:该模块不落盘任何运行期状态\n\n",
|
|
673
|
+
)
|
|
674
|
+
|
|
675
|
+
ok, detail = validator.check_10_data_state(content)
|
|
676
|
+
|
|
677
|
+
assert ok is True, detail
|
|
678
|
+
|
|
679
|
+
|
|
680
|
+
@pytest.mark.parametrize(
|
|
681
|
+
("check", "section"),
|
|
682
|
+
[
|
|
683
|
+
(validator.check_10_data_state, "数据与状态"),
|
|
684
|
+
(validator.check_11_perf_resources, "性能与资源特征"),
|
|
685
|
+
(validator.check_12_change_migration, "变更与迁移注意"),
|
|
686
|
+
],
|
|
687
|
+
)
|
|
688
|
+
def test_rich_sections_reject_short_body(check, section) -> None:
|
|
689
|
+
content = _rich_section_doc(section, "只有一句话。")
|
|
690
|
+
|
|
691
|
+
ok, detail = check(content)
|
|
692
|
+
|
|
693
|
+
assert ok is False
|
|
694
|
+
assert "过短" in detail or "2 句" in detail
|
|
695
|
+
|
|
696
|
+
|
|
697
|
+
def test_rich_sections_are_skipped_for_placeholder_profile() -> None:
|
|
698
|
+
context = validator._ValidationContext(
|
|
699
|
+
doc_path=Path("placeholder.md"),
|
|
700
|
+
docs_root=Path("project_docs"),
|
|
701
|
+
doc_relative="placeholder.md",
|
|
702
|
+
project_root=Path("."),
|
|
703
|
+
source_dir=None,
|
|
704
|
+
source_files=None,
|
|
705
|
+
source_subdirs=None,
|
|
706
|
+
profile=validator.PROFILE_PLACEHOLDER,
|
|
707
|
+
)
|
|
708
|
+
|
|
709
|
+
ok, detail = validator.check_10_data_state("# 占位\n", context=context)
|
|
710
|
+
|
|
711
|
+
assert ok is True
|
|
712
|
+
assert "placeholder" in detail
|