doukui 0.0.0-stage → 0.3.0
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.en.md +42 -0
- package/README.md +147 -2
- package/bin/commands/build.js +178 -0
- package/bin/commands/config.js +88 -0
- package/bin/commands/help.js +83 -0
- package/bin/commands/init.js +194 -0
- package/bin/commands/install.js +202 -0
- package/bin/commands/interactive.js +352 -0
- package/bin/commands/list.js +108 -0
- package/bin/commands/loop-server.js +357 -0
- package/bin/commands/loop.js +1398 -0
- package/bin/commands/migrate.js +120 -0
- package/bin/commands/setup-global.js +81 -0
- package/bin/commands/status.js +140 -0
- package/bin/commands/team-cleanup.js +190 -0
- package/bin/commands/validate-config.js +35 -0
- package/bin/commands/validate.js +21 -0
- package/bin/commands/version.js +22 -0
- package/bin/context.js +143 -0
- package/bin/doukui.js +216 -0
- package/package.json +47 -4
- package/plugins/contracts/capabilities.js +268 -0
- package/plugins/contracts/checkresult-schema.json +75 -0
- package/plugins/contracts/config-schema.json +179 -0
- package/plugins/contracts/plugin-interface.js +262 -0
- package/plugins/contracts/plugin-schema.json +104 -0
- package/plugins/core/hook-session-start/index.js +281 -0
- package/plugins/core/hook-session-start/plugin.json +7 -0
- package/plugins/core/hook-skill-gate/index.js +661 -0
- package/plugins/core/hook-skill-gate/plugin.json +12 -0
- package/plugins/core/provider-loop-engine/index.js +1397 -0
- package/plugins/core/provider-loop-engine/plugin.json +15 -0
- package/plugins/core/provider-memory-store/index.js +510 -0
- package/plugins/core/provider-memory-store/plugin.json +9 -0
- package/plugins/core/provider-role-registry/index.js +376 -0
- package/plugins/core/provider-role-registry/plugin.json +9 -0
- package/plugins/core/provider-state-store/index.js +244 -0
- package/plugins/core/provider-state-store/plugin.json +9 -0
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +32 -0
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +676 -0
- package/plugins/core/provider-watchdog/assets/watchdog.js +754 -0
- package/plugins/core/provider-watchdog/index.js +151 -0
- package/plugins/core/provider-watchdog/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +214 -0
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +249 -0
- package/plugins/core/skill-agent-dispatcher/plugin.json +13 -0
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +253 -0
- package/plugins/core/skill-agent-dispatcher/skill.md +1291 -0
- package/plugins/core/skill-agentic-loop/plugin.json +20 -0
- package/plugins/core/skill-agentic-loop/skill.md +572 -0
- package/plugins/core/skill-batch-task-creator/plugin.json +15 -0
- package/plugins/core/skill-batch-task-creator/skill.md +620 -0
- package/plugins/core/skill-checklist/plugin.json +10 -0
- package/plugins/core/skill-checklist/skill.md +171 -0
- package/plugins/core/skill-completion-report/plugin.json +10 -0
- package/plugins/core/skill-completion-report/skill.md +331 -0
- package/plugins/core/skill-doukui-plan/plugin.json +15 -0
- package/plugins/core/skill-doukui-plan/skill.md +233 -0
- package/plugins/core/skill-doukui-sync/plugin.json +10 -0
- package/plugins/core/skill-doukui-sync/skill.md +76 -0
- package/plugins/core/skill-experience-logger/plugin.json +10 -0
- package/plugins/core/skill-experience-logger/skill.md +235 -0
- package/plugins/core/skill-human-checkpoint/plugin.json +10 -0
- package/plugins/core/skill-human-checkpoint/skill.md +194 -0
- package/plugins/core/skill-progress-tracker/plugin.json +10 -0
- package/plugins/core/skill-progress-tracker/skill.md +228 -0
- package/plugins/core/skill-role-manager/plugin.json +10 -0
- package/plugins/core/skill-role-manager/skill.md +252 -0
- package/plugins/core/skill-split-work-package/plugin.json +15 -0
- package/plugins/core/skill-split-work-package/skill.md +441 -0
- package/plugins/core/skill-task-archive/plugin.json +10 -0
- package/plugins/core/skill-task-archive/skill.md +116 -0
- package/plugins/core/skill-task-creator/plugin.json +15 -0
- package/plugins/core/skill-task-creator/skill.md +886 -0
- package/plugins/core/skill-team-cleanup/plugin.json +10 -0
- package/plugins/core/skill-team-cleanup/skill.md +266 -0
- package/plugins/core/skill-watchdog-manager/plugin.json +10 -0
- package/plugins/core/skill-watchdog-manager/skill.md +143 -0
- package/plugins/core/skill-workflow-orchestrator/plugin.json +13 -0
- package/plugins/core/skill-workflow-orchestrator/skill.md +273 -0
- package/plugins/core/validator-doc-sync/index.js +248 -0
- package/plugins/core/validator-doc-sync/plugin.json +12 -0
- package/plugins/core/validator-work-package/index.js +300 -0
- package/plugins/core/validator-work-package/plugin.json +12 -0
- package/plugins/plugin-registry.json +186 -0
- package/plugins/runtime/agent-def-generator.js +150 -0
- package/plugins/runtime/audit-logger.js +311 -0
- package/plugins/runtime/build-cli.js +89 -0
- package/plugins/runtime/checkresult-validator.js +131 -0
- package/plugins/runtime/claude-md-injector.js +159 -0
- package/plugins/runtime/cleanup-utils.js +293 -0
- package/plugins/runtime/config-manager.js +317 -0
- package/plugins/runtime/config-validator.js +320 -0
- package/plugins/runtime/context-window.js +81 -0
- package/plugins/runtime/event-bus.js +207 -0
- package/plugins/runtime/executor-claude.js +828 -0
- package/plugins/runtime/executor-default.js +448 -0
- package/plugins/runtime/executor-local.js +178 -0
- package/plugins/runtime/executor-sdk.js +390 -0
- package/plugins/runtime/harness-build.js +1037 -0
- package/plugins/runtime/hook-dispatcher.js +315 -0
- package/plugins/runtime/logger.js +192 -0
- package/plugins/runtime/loop-actuator.js +292 -0
- package/plugins/runtime/loop-coordinator.js +347 -0
- package/plugins/runtime/loop-defaults.js +29 -0
- package/plugins/runtime/loop-dispatch-batch.js +247 -0
- package/plugins/runtime/loop-executor.js +120 -0
- package/plugins/runtime/loop-report.js +327 -0
- package/plugins/runtime/loop-server-core.js +750 -0
- package/plugins/runtime/loop-snapshot.js +581 -0
- package/plugins/runtime/loop-trace.js +242 -0
- package/plugins/runtime/loop-worktree.js +404 -0
- package/plugins/runtime/manifest-resolver.js +457 -0
- package/plugins/runtime/multi-window-coordinator.js +560 -0
- package/plugins/runtime/objective-verifier.js +136 -0
- package/plugins/runtime/plan-reader.js +763 -0
- package/plugins/runtime/plugin-loader.js +798 -0
- package/plugins/runtime/plugin-trust.js +182 -0
- package/plugins/runtime/plugin-validator.js +491 -0
- package/plugins/runtime/provider-failover.js +114 -0
- package/plugins/runtime/provider-resolver.js +342 -0
- package/plugins/runtime/reflection-evaluator.js +540 -0
- package/plugins/runtime/resolve-plugin-path.js +297 -0
- package/plugins/runtime/safe-path.js +119 -0
- package/plugins/runtime/sandbox-context.js +235 -0
- package/plugins/runtime/sandbox-manager.js +609 -0
- package/plugins/runtime/sandbox-worker.js +215 -0
- package/plugins/runtime/settings-merger.js +172 -0
- package/plugins/runtime/state-store.js +527 -0
- package/plugins/runtime/team-cleanup.js +497 -0
- package/plugins/runtime/text-tokenize.js +25 -0
- package/plugins/runtime/validator-pipeline.js +475 -0
- package/plugins/runtime/yaml-parser.js +692 -0
- package/templates/harness-config.yaml +388 -0
|
@@ -0,0 +1,763 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan Reader — Agentic Loop 计划入口解析器(WP-177-1-impl-a)
|
|
3
|
+
*
|
|
4
|
+
* @module plan-reader
|
|
5
|
+
*
|
|
6
|
+
* 读取 brainstorming/plan 阶段生成的 `.claude/plan.md`,按其结构解析为
|
|
7
|
+
* skill-agentic-loop 可消费的工作包集合,替代「从用户原始需求 P0 重拆」。
|
|
8
|
+
*
|
|
9
|
+
* 解析策略(纯字符串处理,无外部 markdown 依赖):
|
|
10
|
+
* - `##`/`###` section、`Step N:` 步骤 → 每个可执行 section/step 映射为一个 WP
|
|
11
|
+
* - `- [ ]` / `- [x]` markdown 任务项 → WP 的 checklist(每项带稳定 id)
|
|
12
|
+
* - 「依赖」/「depends on」/「先完成 X」语义 → dependencyGraph
|
|
13
|
+
*
|
|
14
|
+
* 输出结构:
|
|
15
|
+
* {
|
|
16
|
+
* goal: { wpIds: ['WP-...'], checklistSpec, successCriteria },
|
|
17
|
+
* workPackages: [{ wpId, title, checklist:[{id,category,item}], dependencies:['WP-...'] }],
|
|
18
|
+
* dependencyGraph: { ... }, // 供 dispatcher 拓扑排序
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* 容错约定(与 loop-snapshot 一致):
|
|
22
|
+
* - plan.md 不存在/为空/无法解析出任何 WP → 返回
|
|
23
|
+
* `{ goal:{wpIds:[]}, workPackages:[], dependencyGraph:{...}, error:'...' }`,
|
|
24
|
+
* **不抛异常**(供 skill.md 判断「是否退化提示」)。
|
|
25
|
+
* - 唯一例外:解析出循环依赖时抛 Error(依赖图无法拓扑排序,属于计划本身缺陷,
|
|
26
|
+
* 调用方应捕获并提示用户修正 plan.md)。
|
|
27
|
+
*
|
|
28
|
+
* 设计依据:docs/wp/WP-177.md(入口不匹配根因 + plan-reader 输出契约)、
|
|
29
|
+
* docs/wp/WP-177-1-impl-a.md
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
'use strict';
|
|
33
|
+
|
|
34
|
+
var fs = require('fs');
|
|
35
|
+
var path = require('path');
|
|
36
|
+
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// 默认配置
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 默认 plan.md 路径(相对项目根,与 loop-snapshot.resolveProjectRoot 一致口径)。
|
|
43
|
+
*/
|
|
44
|
+
var DEFAULT_PLAN_RELATIVE = path.join('.claude', 'plan.md');
|
|
45
|
+
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
// 工具函数
|
|
48
|
+
// ---------------------------------------------------------------------------
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* ISO 时间戳。
|
|
52
|
+
* @returns {string}
|
|
53
|
+
*/
|
|
54
|
+
function nowIso() {
|
|
55
|
+
return new Date().toISOString();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 解析项目根目录(仿 loop-snapshot.resolveProjectRoot:向上找 task.md / .claude)。
|
|
60
|
+
* @param {string} [startCwd]
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
function resolveProjectRoot(startCwd) {
|
|
64
|
+
var dir = startCwd || process.cwd();
|
|
65
|
+
for (var i = 0; i < 10; i++) {
|
|
66
|
+
if (fs.existsSync(path.join(dir, 'task.md'))) return dir;
|
|
67
|
+
if (fs.existsSync(path.join(dir, '.claude'))) return dir;
|
|
68
|
+
var parent = path.dirname(dir);
|
|
69
|
+
if (parent === dir) break;
|
|
70
|
+
dir = parent;
|
|
71
|
+
}
|
|
72
|
+
return startCwd || process.cwd();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 解析默认 plan.md 绝对路径。优先用 opts.planFilePath;否则拼到项目根下
|
|
77
|
+
* `.claude/plan.md`。
|
|
78
|
+
* @param {object} [opts]
|
|
79
|
+
* @param {string} [opts.planFilePath] 绝对或相对路径(相对则按 cwd 解析)
|
|
80
|
+
* @param {string} [opts.projectRoot] 项目根覆盖(测试用)
|
|
81
|
+
* @returns {string}
|
|
82
|
+
*/
|
|
83
|
+
function resolvePlanPath(opts) {
|
|
84
|
+
opts = opts || {};
|
|
85
|
+
if (opts.planFilePath) {
|
|
86
|
+
return path.isAbsolute(opts.planFilePath)
|
|
87
|
+
? opts.planFilePath
|
|
88
|
+
: path.resolve(opts.planFilePath);
|
|
89
|
+
}
|
|
90
|
+
var root = opts.projectRoot || resolveProjectRoot();
|
|
91
|
+
return path.join(root, DEFAULT_PLAN_RELATIVE);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 把任意字符串归一化为稳定 slug(用于 wpId 备选 / checklist id 前缀)。
|
|
96
|
+
* 规则:小写 → 非字母数字下划线连字符替换为 '-' → 折叠连续 '-' → 去首尾 '-'。
|
|
97
|
+
* 空串返回 'wp'。
|
|
98
|
+
* @param {string} raw
|
|
99
|
+
* @returns {string}
|
|
100
|
+
*/
|
|
101
|
+
function slugify(raw) {
|
|
102
|
+
if (!raw) return 'wp';
|
|
103
|
+
var s = String(raw)
|
|
104
|
+
.toLowerCase()
|
|
105
|
+
.replace(/[^a-z0-9_-]+/g, '-')
|
|
106
|
+
.replace(/-+/g, '-')
|
|
107
|
+
.replace(/^-+|-+$/g, '');
|
|
108
|
+
return s || 'wp';
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 从 section 标题行提取标题文本(去掉前导的 # / Step N: / 序号)。
|
|
113
|
+
* 例:`## Step 1: 实现解析模块` → `实现解析模块`
|
|
114
|
+
* `### 数据模型设计` → `数据模型设计`
|
|
115
|
+
* @param {string} headerLine 形如 `## ...` / `### Step 2: ...`
|
|
116
|
+
* @returns {string}
|
|
117
|
+
*/
|
|
118
|
+
function extractTitle(headerLine) {
|
|
119
|
+
if (!headerLine) return '';
|
|
120
|
+
// 去掉前导 # 与空白
|
|
121
|
+
var s = String(headerLine).replace(/^#+\s*/, '').trim();
|
|
122
|
+
// 去掉 "Step N:" / "步骤 N:" 前缀
|
|
123
|
+
s = s.replace(/^(?:step|步骤)\s*\d+\s*[::、\-]\s*/i, '');
|
|
124
|
+
return s.trim();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 提取 section 标题里的显式 WP 引用(如 `## WP-5: 数据模型` / `## WP-A: ...` /
|
|
129
|
+
* `## WP-feature-x: ...`)。
|
|
130
|
+
*
|
|
131
|
+
* WP-186(放宽):正则从 `WP-?(\d+)` 放宽到 `WP-?([A-Za-z0-9][\w-]*)`,支持
|
|
132
|
+
* 字母 / 混合编号(WP-A、WP-175、WP-feature-x),与 loop-snapshot.parseProgressMarkdown
|
|
133
|
+
* 已放宽的 `WP-?[\w-]+` 口径对齐 —— 否则字母编号 section 经 assignWpIds 派生为数字,
|
|
134
|
+
* 真实链路(plan-reader→driver→PROGRESS.md)永不产字母编号。
|
|
135
|
+
*
|
|
136
|
+
* 边界设计:
|
|
137
|
+
* - 必须以字母/数字开头(`[A-Za-z0-9]`),排除 `WP--` / `WP-` 空编号;
|
|
138
|
+
* - 后续允许 `[\w-]*`(连字符、字母、数字、下划线),与 loop-snapshot 一致;
|
|
139
|
+
* - 外层 `\b` 确保整体是单词边界,`[\w-]+` 含连字符但不会跨词贪婪(见测试)。
|
|
140
|
+
*
|
|
141
|
+
* 归一化:统一规整为 `WP-<原token>`(编号 token 保留原样,大小写不变 —— 与
|
|
142
|
+
* loop-snapshot 的 `'WP-' + m[1].replace(/^WP-?/i,'')` 一致)。
|
|
143
|
+
*
|
|
144
|
+
* @param {string} headerLine
|
|
145
|
+
* @returns {string|null}
|
|
146
|
+
*/
|
|
147
|
+
function extractExplicitWpId(headerLine) {
|
|
148
|
+
if (!headerLine) return null;
|
|
149
|
+
var m = String(headerLine).match(/\bWP-?([A-Za-z0-9][\w-]*)\b/i);
|
|
150
|
+
if (!m) return null;
|
|
151
|
+
return 'WP-' + m[1];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 判断一行是否为 section 标题(## / ###)。
|
|
156
|
+
* 注意:`#` 顶级标题视为文档标题,不作为独立 WP。
|
|
157
|
+
* @param {string} line
|
|
158
|
+
* @returns {boolean}
|
|
159
|
+
*/
|
|
160
|
+
function isSectionHeader(line) {
|
|
161
|
+
return /^(?:##|###)\s+\S/.test(line);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 判断一行是否为顶层 `Step N:` 行(在没显式 section 时作为可执行单元兜底)。
|
|
166
|
+
* 形如 `Step 1: xxx` / `步骤 1:xxx`。
|
|
167
|
+
* @param {string} line
|
|
168
|
+
* @returns {boolean}
|
|
169
|
+
*/
|
|
170
|
+
function isStepLine(line) {
|
|
171
|
+
return /^\s*(?:step|步骤)\s*\d+\s*[::]/i.test(String(line).trim());
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 解析一行 markdown 任务项,返回 { checked, text } 或 null(非任务项)。
|
|
176
|
+
* 支持:`- [ ]`、`- [x]`、`- [X]`、`* [✓]`、`- [x]`(全角对勾)。
|
|
177
|
+
* @param {string} line
|
|
178
|
+
* @returns {{checked:boolean, text:string}|null}
|
|
179
|
+
*/
|
|
180
|
+
function parseTaskItem(line) {
|
|
181
|
+
if (!line) return null;
|
|
182
|
+
// 仅匹配任务项:[ ] 或 [xX✓✔✗×] 等勾选标记
|
|
183
|
+
var m = String(line).match(/^\s*[-*+]\s*\[([ xX✓✔✗×])\]\s+(.+)$/);
|
|
184
|
+
if (!m) return null;
|
|
185
|
+
var mark = m[1].toLowerCase();
|
|
186
|
+
// B7: ✗/× are "failed / not done" markers — they must NOT count as checked.
|
|
187
|
+
// Only x/X/✓/✔ (positive check marks) are checked. This was previously
|
|
188
|
+
// treating ✗ and × as checked:true, semantically inverting failures to done.
|
|
189
|
+
var checked = mark === 'x' || mark === '✓' || mark === '✔';
|
|
190
|
+
return { checked: checked, text: m[2].trim() };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 从一段文本(section body / 整个 plan)抽取 WP 依赖引用。
|
|
195
|
+
* 识别语义:「依赖 WP-X」/「depends on WP-X」/「先完成 WP-X」/「after WP-X」/
|
|
196
|
+
* 「需要 WP-X」/「requires WP-X」。
|
|
197
|
+
* 仅返回当前已识别 wpId 集合内的引用(避免把无关的 WP-NNNN 文档号误当依赖)。
|
|
198
|
+
* 若 knownIds 非空,做白名单过滤;为空则不过滤(全部 WP 引用视为依赖)。
|
|
199
|
+
*
|
|
200
|
+
* WP-186(放宽):捕获组从 `WP-?(\d+)` 放宽到 `WP-?([A-Za-z0-9][\w-]*)`,与
|
|
201
|
+
* extractExplicitWpId / loop-snapshot 口径一致,支持字母/混合编号依赖(如
|
|
202
|
+
* `依赖 WP-A`)。
|
|
203
|
+
*
|
|
204
|
+
* @param {string} text
|
|
205
|
+
* @param {string[]} [knownIds] 已分配的合法 wpId 白名单
|
|
206
|
+
* @returns {string[]} 去重后的依赖 wpId 列表(首次出现顺序)
|
|
207
|
+
*/
|
|
208
|
+
function extractDependencyRefs(text, knownIds) {
|
|
209
|
+
if (!text) return [];
|
|
210
|
+
var refs = [];
|
|
211
|
+
var seen = {};
|
|
212
|
+
// 依赖语义锚词(中英),后跟 WP-<字母/数字编号>
|
|
213
|
+
var re = /(?:依赖|depends?\s*on|先完成|需要|requires?|after|前置)\s*:?\s*(WP-?([A-Za-z0-9][\w-]*))/gi;
|
|
214
|
+
var m;
|
|
215
|
+
while ((m = re.exec(text)) !== null) {
|
|
216
|
+
var wid = 'WP-' + m[2];
|
|
217
|
+
if (seen[wid]) continue;
|
|
218
|
+
if (knownIds && knownIds.length && knownIds.indexOf(wid) === -1) continue;
|
|
219
|
+
// 自引用排除:依赖文本里恰好提到自己(由调用方传入时已隔离,这里二次保险)
|
|
220
|
+
seen[wid] = true;
|
|
221
|
+
refs.push(wid);
|
|
222
|
+
}
|
|
223
|
+
return refs;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// Section 切分
|
|
228
|
+
// ---------------------------------------------------------------------------
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 把 plan.md 文本切分为若干 section。
|
|
232
|
+
* 切分规则(优先级从高到低):
|
|
233
|
+
* 1) `##` / `###` 标题行作为 section 起点,吸纳其后到下一个同级或更高级标题的内容;
|
|
234
|
+
* 2) 若无任何 section 标题,但存在顶层 `Step N:` 行,则按 Step 行切分;
|
|
235
|
+
* 3) 兜底:整篇视为单个 section(标题取首个 `#` 文档标题或空)。
|
|
236
|
+
*
|
|
237
|
+
* 每个 section:{ headerLine, level, title, body, explicitWpId }
|
|
238
|
+
* level: 2 = `##`,3 = `###`,0 = Step 兜底 / 整篇兜底。
|
|
239
|
+
*
|
|
240
|
+
* @param {string} content plan.md 全文
|
|
241
|
+
* @returns {Array<{headerLine:string, level:number, title:string, body:string, explicitWpId:string|null}>}
|
|
242
|
+
*/
|
|
243
|
+
function splitSections(content) {
|
|
244
|
+
if (!content) return [];
|
|
245
|
+
var lines = String(content).split(/\r?\n/);
|
|
246
|
+
var sections = [];
|
|
247
|
+
|
|
248
|
+
// 先扫描显式 section 标题(## / ###)
|
|
249
|
+
var headerIdx = [];
|
|
250
|
+
for (var i = 0; i < lines.length; i++) {
|
|
251
|
+
if (isSectionHeader(lines[i])) headerIdx.push(i);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (headerIdx.length > 0) {
|
|
255
|
+
for (var h = 0; h < headerIdx.length; h++) {
|
|
256
|
+
var start = headerIdx[h];
|
|
257
|
+
var level = lines[start].indexOf('###') === 0 ? 3 : 2;
|
|
258
|
+
// 该 section body 延伸到下一个「任意 section 标题」前。
|
|
259
|
+
// `##`(2) 与 `###`(3) 都视为独立可执行单元:父 section 的 body 在其下一个
|
|
260
|
+
// 子标题(`###`)处即截断,子标题独立成 section,避免父吞并子的任务项。
|
|
261
|
+
var end = lines.length;
|
|
262
|
+
if (h + 1 < headerIdx.length) {
|
|
263
|
+
end = headerIdx[h + 1];
|
|
264
|
+
}
|
|
265
|
+
var headerLine = lines[start];
|
|
266
|
+
sections.push({
|
|
267
|
+
headerLine: headerLine,
|
|
268
|
+
level: level,
|
|
269
|
+
title: extractTitle(headerLine),
|
|
270
|
+
body: lines.slice(start + 1, end).join('\n'),
|
|
271
|
+
explicitWpId: extractExplicitWpId(headerLine),
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
return sections;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// 无 section 标题 → 按 Step 行切分
|
|
278
|
+
var stepIdx = [];
|
|
279
|
+
for (var k = 0; k < lines.length; k++) {
|
|
280
|
+
if (isStepLine(lines[k])) stepIdx.push(k);
|
|
281
|
+
}
|
|
282
|
+
if (stepIdx.length > 0) {
|
|
283
|
+
for (var s = 0; s < stepIdx.length; s++) {
|
|
284
|
+
var sStart = stepIdx[s];
|
|
285
|
+
var sEnd = s + 1 < stepIdx.length ? stepIdx[s + 1] : lines.length;
|
|
286
|
+
var sLine = lines[sStart];
|
|
287
|
+
sections.push({
|
|
288
|
+
headerLine: sLine,
|
|
289
|
+
level: 0,
|
|
290
|
+
title: extractTitle(sLine),
|
|
291
|
+
body: lines.slice(sStart + 1, sEnd).join('\n'),
|
|
292
|
+
explicitWpId: extractExplicitWpId(sLine),
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return sections;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 兜底:整篇一个 section(仅当含任务项时才有意义,由调用方决定)
|
|
299
|
+
var docTitle = '';
|
|
300
|
+
for (var d = 0; d < lines.length; d++) {
|
|
301
|
+
var hm = lines[d].match(/^#\s+(.+)$/);
|
|
302
|
+
if (hm) { docTitle = hm[1].trim(); break; }
|
|
303
|
+
}
|
|
304
|
+
sections.push({
|
|
305
|
+
headerLine: '',
|
|
306
|
+
level: 0,
|
|
307
|
+
title: docTitle,
|
|
308
|
+
body: content,
|
|
309
|
+
explicitWpId: null,
|
|
310
|
+
});
|
|
311
|
+
return sections;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 从 section body 提取 checklist 任务项。
|
|
316
|
+
* 每项 { id, category, item, checked },id 形如 `{sectionSlug}-{序号}`(跨轮稳定)。
|
|
317
|
+
* 仅提取 section 内直接出现的顶层任务项(缩进 ≤ 2 空格的子项也计入,保持宽松)。
|
|
318
|
+
* @param {object} section splitSections 的产物
|
|
319
|
+
* @param {number} sectionIndex 该 section 在切分序列中的序号(用于 slug 区分)
|
|
320
|
+
* @returns {Array<{id:string, category:string, item:string, checked:boolean}>}
|
|
321
|
+
*/
|
|
322
|
+
function extractChecklist(section, sectionIndex) {
|
|
323
|
+
var slug = section.explicitWpId
|
|
324
|
+
? slugify(section.explicitWpId)
|
|
325
|
+
: slugify(section.title || ('section-' + (sectionIndex + 1)));
|
|
326
|
+
var items = [];
|
|
327
|
+
var bodyLines = String(section.body).split(/\r?\n/);
|
|
328
|
+
var seq = 0;
|
|
329
|
+
for (var i = 0; i < bodyLines.length; i++) {
|
|
330
|
+
var parsed = parseTaskItem(bodyLines[i]);
|
|
331
|
+
if (!parsed) continue;
|
|
332
|
+
seq += 1;
|
|
333
|
+
// category 从任务项文本里尽力抽取(如「[acceptance] xxx」前缀),默认 'check'
|
|
334
|
+
var cat = 'check';
|
|
335
|
+
var text = parsed.text;
|
|
336
|
+
var cm = text.match(/^\[([a-zA-Z][\w-]*)\]\s*(.+)$/);
|
|
337
|
+
if (cm) {
|
|
338
|
+
cat = cm[1].toLowerCase();
|
|
339
|
+
text = cm[2].trim();
|
|
340
|
+
}
|
|
341
|
+
// WP-S5:验收项可声明客观验证命令 —— 格式「- [ ] 事项 [verify: command]」。
|
|
342
|
+
// verify 透传到 executor CheckResult items[].verify,objective-verifier 实跑,
|
|
343
|
+
// 命令可验证的项零 LLM 自由判断(省 token + 消解过度自检)。
|
|
344
|
+
var verify = null;
|
|
345
|
+
var vm = text.match(/\s*\[verify:\s*([^\]]+)\]\s*$/i);
|
|
346
|
+
if (vm) {
|
|
347
|
+
verify = { command: vm[1].trim(), expectExit: 0 };
|
|
348
|
+
text = text.replace(vm[0], '').trim();
|
|
349
|
+
}
|
|
350
|
+
items.push({
|
|
351
|
+
id: slug + '-' + seq,
|
|
352
|
+
category: cat,
|
|
353
|
+
item: text,
|
|
354
|
+
checked: parsed.checked,
|
|
355
|
+
verify: verify,
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
return items;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* 判定一个 section 是否「可执行」(应映射为 WP)。
|
|
363
|
+
* 规则:含任务项,或标题/内容非空且非纯说明(含 Step/实现/创建/修改等动词)。
|
|
364
|
+
* 用以过滤「背景」「目标」之类的纯叙述 section。
|
|
365
|
+
* @param {object} section
|
|
366
|
+
* @param {Array} checklistItems 该 section 抽出的 checklist
|
|
367
|
+
* @returns {boolean}
|
|
368
|
+
*/
|
|
369
|
+
function isExecutableSection(section, checklistItems) {
|
|
370
|
+
if (checklistItems && checklistItems.length > 0) return true;
|
|
371
|
+
// 无任务项时,看标题/body 是否含执行性关键词
|
|
372
|
+
var hay = (section.title + '\n' + section.body).toLowerCase();
|
|
373
|
+
var execKeywords = /\b(?:实现|创建|新增|修改|添加|编写|完成|deploy|implement|create|add|build|fix|refactor)\b/;
|
|
374
|
+
if (execKeywords.test(hay)) return true;
|
|
375
|
+
return false;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// ---------------------------------------------------------------------------
|
|
379
|
+
// WP 分配与依赖图
|
|
380
|
+
// ---------------------------------------------------------------------------
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* 为每个 section 分配稳定 wpId。
|
|
384
|
+
* 策略:
|
|
385
|
+
* - 若 section 标题含显式 WP-NNN → 直接用之(去重:重复则降级为序号派生);
|
|
386
|
+
* - 否则 → 按 section 顺序 `WP-{startSeq + i}` 派生(startSeq 来自 task.md 最大编号+1,
|
|
387
|
+
* 无 task.md 时从 1 起)。
|
|
388
|
+
*
|
|
389
|
+
* @param {Array} sections 可执行 section 列表(已过滤)
|
|
390
|
+
* @param {number} startSeq 派生起点(task.md 最大编号 + 1)
|
|
391
|
+
* @returns {Array<{wpId:string, section:object, derived:boolean}>}
|
|
392
|
+
*/
|
|
393
|
+
function assignWpIds(sections, startSeq) {
|
|
394
|
+
var assigned = [];
|
|
395
|
+
var usedIds = {};
|
|
396
|
+
var seq = startSeq;
|
|
397
|
+
for (var i = 0; i < sections.length; i++) {
|
|
398
|
+
var sec = sections[i];
|
|
399
|
+
var wpId = null;
|
|
400
|
+
if (sec.explicitWpId && !usedIds[sec.explicitWpId]) {
|
|
401
|
+
wpId = sec.explicitWpId;
|
|
402
|
+
usedIds[wpId] = true;
|
|
403
|
+
} else {
|
|
404
|
+
// 派生:保证唯一(跳过已被显式占用的编号)
|
|
405
|
+
while (usedIds['WP-' + seq]) seq += 1;
|
|
406
|
+
wpId = 'WP-' + seq;
|
|
407
|
+
usedIds[wpId] = true;
|
|
408
|
+
seq += 1;
|
|
409
|
+
}
|
|
410
|
+
assigned.push({ wpId: wpId, section: sec, derived: wpId !== sec.explicitWpId });
|
|
411
|
+
}
|
|
412
|
+
return assigned;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 从 task.md 解析当前最大 WP 编号(用于新 WP 派生起点)。
|
|
417
|
+
* 扫描 `WP-NNN` 形式引用,取最大值;无 task.md / 无匹配 → 0。
|
|
418
|
+
* @param {string} projectRoot
|
|
419
|
+
* @returns {number}
|
|
420
|
+
*/
|
|
421
|
+
function readMaxWpNumber(projectRoot) {
|
|
422
|
+
var taskPath = path.join(projectRoot || resolveProjectRoot(), 'task.md');
|
|
423
|
+
if (!fs.existsSync(taskPath)) return 0;
|
|
424
|
+
var content;
|
|
425
|
+
try {
|
|
426
|
+
content = fs.readFileSync(taskPath, 'utf8');
|
|
427
|
+
} catch (e) {
|
|
428
|
+
return 0;
|
|
429
|
+
}
|
|
430
|
+
var max = 0;
|
|
431
|
+
var re = /WP-(\d+)/gi;
|
|
432
|
+
var m;
|
|
433
|
+
while ((m = re.exec(content)) !== null) {
|
|
434
|
+
var n = parseInt(m[1], 10);
|
|
435
|
+
if (!isNaN(n) && n > max) max = n;
|
|
436
|
+
}
|
|
437
|
+
return max;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 构建 dependencyGraph(邻接表 + 反向 + 拓扑层)。
|
|
442
|
+
* 结构:
|
|
443
|
+
* {
|
|
444
|
+
* nodes: { [wpId]: { wpId, dependencies:[], dependents:[] } },
|
|
445
|
+
* edges: [{ from, to }], // from 依赖 to(to 先完成)
|
|
446
|
+
* order: [wpId, ...], // 拓扑顺序(入度 0 优先,稳定)
|
|
447
|
+
* hasCycle: boolean,
|
|
448
|
+
* cycle: [wpId, ...] // 检测到的环(若 hasCycle)
|
|
449
|
+
* }
|
|
450
|
+
*
|
|
451
|
+
* 检测到环时返回 hasCycle=true 并附 cycle 路径(不抛异常;由 parsePlanToGoal 决定
|
|
452
|
+
* 是否升级为错误)。
|
|
453
|
+
* @param {Array<{wpId:string, dependencies:string[]}>} wpDeps 每个 WP 的依赖列表
|
|
454
|
+
* @returns {object}
|
|
455
|
+
*/
|
|
456
|
+
function buildDependencyGraph(wpDeps) {
|
|
457
|
+
var nodes = {};
|
|
458
|
+
var validIds = {};
|
|
459
|
+
for (var i = 0; i < wpDeps.length; i++) {
|
|
460
|
+
validIds[wpDeps[i].wpId] = true;
|
|
461
|
+
}
|
|
462
|
+
// 初始化节点
|
|
463
|
+
for (var j = 0; j < wpDeps.length; j++) {
|
|
464
|
+
var wid = wpDeps[j].wpId;
|
|
465
|
+
nodes[wid] = { wpId: wid, dependencies: [], dependents: [] };
|
|
466
|
+
}
|
|
467
|
+
// 建边:from 依赖 to(只保留落在合法 wpId 集合内的依赖引用)
|
|
468
|
+
var edges = [];
|
|
469
|
+
for (var k = 0; k < wpDeps.length; k++) {
|
|
470
|
+
var from = wpDeps[k].wpId;
|
|
471
|
+
var deps = wpDeps[k].dependencies || [];
|
|
472
|
+
for (var d = 0; d < deps.length; d++) {
|
|
473
|
+
var to = deps[d];
|
|
474
|
+
if (!validIds[to]) continue; // 越界依赖忽略
|
|
475
|
+
if (to === from) continue; // 自环忽略
|
|
476
|
+
// 去重
|
|
477
|
+
if (nodes[from].dependencies.indexOf(to) !== -1) continue;
|
|
478
|
+
nodes[from].dependencies.push(to);
|
|
479
|
+
nodes[to].dependents.push(from);
|
|
480
|
+
edges.push({ from: from, to: to });
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
// Kahn 拓扑排序 + 环检测
|
|
484
|
+
var inDegree = {};
|
|
485
|
+
for (var id in nodes) {
|
|
486
|
+
if (!Object.prototype.hasOwnProperty.call(nodes, id)) continue;
|
|
487
|
+
inDegree[id] = nodes[id].dependencies.length;
|
|
488
|
+
}
|
|
489
|
+
var queue = [];
|
|
490
|
+
// 入度为 0 入队(按 wpId 字典序稳定)
|
|
491
|
+
var allIds = Object.keys(nodes).sort();
|
|
492
|
+
for (var q = 0; q < allIds.length; q++) {
|
|
493
|
+
if (inDegree[allIds[q]] === 0) queue.push(allIds[q]);
|
|
494
|
+
}
|
|
495
|
+
var order = [];
|
|
496
|
+
while (queue.length > 0) {
|
|
497
|
+
var cur = queue.shift();
|
|
498
|
+
order.push(cur);
|
|
499
|
+
var dependents = nodes[cur].dependents;
|
|
500
|
+
var newlyReady = [];
|
|
501
|
+
for (var r = 0; r < dependents.length; r++) {
|
|
502
|
+
inDegree[dependents[r]] -= 1;
|
|
503
|
+
if (inDegree[dependents[r]] === 0) newlyReady.push(dependents[r]);
|
|
504
|
+
}
|
|
505
|
+
// 同批新就绪按字典序稳定入队
|
|
506
|
+
newlyReady.sort();
|
|
507
|
+
for (var s = 0; s < newlyReady.length; s++) queue.push(newlyReady[s]);
|
|
508
|
+
}
|
|
509
|
+
var hasCycle = order.length < Object.keys(nodes).length;
|
|
510
|
+
var cycle = [];
|
|
511
|
+
if (hasCycle) {
|
|
512
|
+
// 收集仍在环中的节点(inDegree > 0)
|
|
513
|
+
for (var c in inDegree) {
|
|
514
|
+
if (!Object.prototype.hasOwnProperty.call(inDegree, c)) continue;
|
|
515
|
+
if (inDegree[c] > 0) cycle.push(c);
|
|
516
|
+
}
|
|
517
|
+
cycle.sort();
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
nodes: nodes,
|
|
521
|
+
edges: edges,
|
|
522
|
+
order: order,
|
|
523
|
+
hasCycle: hasCycle,
|
|
524
|
+
cycle: cycle,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// ---------------------------------------------------------------------------
|
|
529
|
+
// 公开 API
|
|
530
|
+
// ---------------------------------------------------------------------------
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* 解析 plan.md 为 loop 可消费的目标 + 工作包集合。
|
|
534
|
+
*
|
|
535
|
+
* @param {string} [planFilePath] plan.md 路径(绝对或相对);省略则用默认 `.claude/plan.md`
|
|
536
|
+
* @param {object} [opts]
|
|
537
|
+
* @param {string} [opts.planFilePath] 同上(参数化注入便于测试)
|
|
538
|
+
* @param {string} [opts.projectRoot] 项目根覆盖(默认自动探测)
|
|
539
|
+
* @param {boolean} [opts.throwOnCycle=true] 检测到循环依赖时是否抛异常;
|
|
540
|
+
* false 则在返回值的 error/cycle 字段体现,不抛
|
|
541
|
+
* @returns {object} { goal, workPackages, dependencyGraph, error? }
|
|
542
|
+
*/
|
|
543
|
+
function parsePlanToGoal(planFilePath, opts) {
|
|
544
|
+
// 兼容两种调用:parsePlanToGoal(path) / parsePlanToGoal(opts)
|
|
545
|
+
if (planFilePath && typeof planFilePath === 'object') {
|
|
546
|
+
opts = planFilePath;
|
|
547
|
+
planFilePath = opts.planFilePath;
|
|
548
|
+
}
|
|
549
|
+
opts = opts || {};
|
|
550
|
+
var throwOnCycle = opts.throwOnCycle !== false;
|
|
551
|
+
var projectRoot = opts.projectRoot || resolveProjectRoot();
|
|
552
|
+
|
|
553
|
+
var planPath = planFilePath
|
|
554
|
+
? (path.isAbsolute(planFilePath) ? planFilePath : path.resolve(projectRoot, planFilePath))
|
|
555
|
+
: path.join(projectRoot, DEFAULT_PLAN_RELATIVE);
|
|
556
|
+
|
|
557
|
+
var emptyResult = function (errMsg) {
|
|
558
|
+
return {
|
|
559
|
+
goal: { wpIds: [], checklistSpec: null, successCriteria: [] },
|
|
560
|
+
workPackages: [],
|
|
561
|
+
dependencyGraph: { nodes: {}, edges: [], order: [], hasCycle: false, cycle: [] },
|
|
562
|
+
planFilePath: planPath,
|
|
563
|
+
error: errMsg || null,
|
|
564
|
+
parsedAt: nowIso(),
|
|
565
|
+
};
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// 1) 读取文件(缺失/读失败 → 降级)
|
|
569
|
+
if (!fs.existsSync(planPath)) {
|
|
570
|
+
return emptyResult('plan-not-found');
|
|
571
|
+
}
|
|
572
|
+
var content;
|
|
573
|
+
try {
|
|
574
|
+
content = fs.readFileSync(planPath, 'utf8');
|
|
575
|
+
} catch (e) {
|
|
576
|
+
return emptyResult('plan-read-error: ' + (e && e.message));
|
|
577
|
+
}
|
|
578
|
+
if (!content || !content.trim()) {
|
|
579
|
+
return emptyResult('plan-empty');
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// 2) 切分 section
|
|
583
|
+
var sections = splitSections(content);
|
|
584
|
+
if (sections.length === 0) {
|
|
585
|
+
return emptyResult('plan-no-sections');
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// 3) 过滤可执行 section + 抽 checklist
|
|
589
|
+
var candidates = [];
|
|
590
|
+
for (var i = 0; i < sections.length; i++) {
|
|
591
|
+
var sec = sections[i];
|
|
592
|
+
var checklist = extractChecklist(sec, i);
|
|
593
|
+
if (!isExecutableSection(sec, checklist)) continue;
|
|
594
|
+
candidates.push({ section: sec, checklist: checklist });
|
|
595
|
+
}
|
|
596
|
+
if (candidates.length === 0) {
|
|
597
|
+
return emptyResult('plan-no-executable-sections');
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// 4) 分配 wpId(task.md 最大编号 +1 起派生;显式 WP-NNN 优先)
|
|
601
|
+
var startSeq = readMaxWpNumber(projectRoot) + 1;
|
|
602
|
+
var bare = candidates.map(function (c) { return c.section; });
|
|
603
|
+
var assigned = assignWpIds(bare, startSeq);
|
|
604
|
+
|
|
605
|
+
// 5) 构建 workPackages + 收集依赖
|
|
606
|
+
var workPackages = [];
|
|
607
|
+
var wpDepList = [];
|
|
608
|
+
// 已分配 id 集合(用于依赖引用白名单过滤)
|
|
609
|
+
var knownIds = assigned.map(function (a) { return a.wpId; });
|
|
610
|
+
for (var a = 0; a < assigned.length; a++) {
|
|
611
|
+
var cand = candidates[a];
|
|
612
|
+
var asg = assigned[a];
|
|
613
|
+
var sectionText = asg.section.headerLine + '\n' + asg.section.body;
|
|
614
|
+
var deps = extractDependencyRefs(sectionText, knownIds);
|
|
615
|
+
// 排除自引用(标题里显式 WP-NNN 与派生 id 相同的情况)
|
|
616
|
+
deps = deps.filter(function (d) { return d !== asg.wpId; });
|
|
617
|
+
workPackages.push({
|
|
618
|
+
wpId: asg.wpId,
|
|
619
|
+
title: asg.section.title || asg.wpId,
|
|
620
|
+
checklist: cand.checklist,
|
|
621
|
+
dependencies: deps,
|
|
622
|
+
_derived: asg.derived,
|
|
623
|
+
});
|
|
624
|
+
wpDepList.push({ wpId: asg.wpId, dependencies: deps });
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// 6) 依赖图 + 循环检测
|
|
628
|
+
var dependencyGraph = buildDependencyGraph(wpDepList);
|
|
629
|
+
if (dependencyGraph.hasCycle) {
|
|
630
|
+
var cycleErr = 'plan-cyclic-dependency: ' + dependencyGraph.cycle.join(' <- ');
|
|
631
|
+
if (throwOnCycle) {
|
|
632
|
+
var err = new Error(cycleErr);
|
|
633
|
+
err.code = 'PLAN_CYCLIC_DEPENDENCY';
|
|
634
|
+
err.cycle = dependencyGraph.cycle;
|
|
635
|
+
err.dependencyGraph = dependencyGraph;
|
|
636
|
+
err.planFilePath = planPath;
|
|
637
|
+
throw err;
|
|
638
|
+
}
|
|
639
|
+
// 不抛时也返回结构,但带 error 字段
|
|
640
|
+
return {
|
|
641
|
+
goal: {
|
|
642
|
+
wpIds: knownIds,
|
|
643
|
+
checklistSpec: null,
|
|
644
|
+
successCriteria: [],
|
|
645
|
+
},
|
|
646
|
+
workPackages: workPackages,
|
|
647
|
+
dependencyGraph: dependencyGraph,
|
|
648
|
+
planFilePath: planPath,
|
|
649
|
+
error: cycleErr,
|
|
650
|
+
parsedAt: nowIso(),
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// 7) goal 装配
|
|
655
|
+
var goal = {
|
|
656
|
+
wpIds: knownIds,
|
|
657
|
+
checklistSpec: buildChecklistSpec(workPackages),
|
|
658
|
+
successCriteria: extractSuccessCriteria(content),
|
|
659
|
+
// WP-S3 风险分级:plan 顶层声明「风险分级: low」/「risk: low」→ 人审豁免信号。
|
|
660
|
+
// 显式声明才为 true;未声明 = 默认高风险(人审照常)。
|
|
661
|
+
riskLow: /^\s*(?:风险分级|risk)\s*[::]\s*low\s*$/im.test(content),
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
return {
|
|
665
|
+
goal: goal,
|
|
666
|
+
workPackages: workPackages,
|
|
667
|
+
dependencyGraph: dependencyGraph,
|
|
668
|
+
planFilePath: planPath,
|
|
669
|
+
error: null,
|
|
670
|
+
riskLow: goal.riskLow,
|
|
671
|
+
parsedAt: nowIso(),
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* 从 workPackages 聚合 checklistSpec(供 reflection-evaluator / dispatcher 引用)。
|
|
677
|
+
* 每个 WP 的 checklist 项带稳定 id;spec 列出全部项的扁平视图。
|
|
678
|
+
* @param {Array} workPackages
|
|
679
|
+
* @returns {{total:number, byWp:object, items:Array}}
|
|
680
|
+
*/
|
|
681
|
+
function buildChecklistSpec(workPackages) {
|
|
682
|
+
var byWp = {};
|
|
683
|
+
var items = [];
|
|
684
|
+
for (var i = 0; i < workPackages.length; i++) {
|
|
685
|
+
var wp = workPackages[i];
|
|
686
|
+
byWp[wp.wpId] = wp.checklist;
|
|
687
|
+
for (var j = 0; j < wp.checklist.length; j++) {
|
|
688
|
+
var it = wp.checklist[j];
|
|
689
|
+
items.push({
|
|
690
|
+
id: it.id,
|
|
691
|
+
wpId: wp.wpId,
|
|
692
|
+
category: it.category,
|
|
693
|
+
item: it.item,
|
|
694
|
+
// WP-S5:verify 声明透传(无标记项不带字段,保持投影干净)
|
|
695
|
+
verify: it.verify || undefined,
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return { total: items.length, byWp: byWp, items: items };
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* 从 plan.md 全文抽取「成功标准 / 验收标准」语义段(非任务项)。
|
|
704
|
+
* 识别 `## 成功标准` / `## 验收标准` / `## Success Criteria` section 下的要点行。
|
|
705
|
+
* 抽不到则返回空数组(非阻断)。
|
|
706
|
+
* @param {string} content
|
|
707
|
+
* @returns {string[]}
|
|
708
|
+
*/
|
|
709
|
+
function extractSuccessCriteria(content) {
|
|
710
|
+
if (!content) return [];
|
|
711
|
+
var lines = String(content).split(/\r?\n/);
|
|
712
|
+
var inSection = false;
|
|
713
|
+
var out = [];
|
|
714
|
+
var reSectionStart = /^#{1,4}\s*(?:成功标准|验收标准|success\s*criteria|acceptance\s*criteria)\s*$/i;
|
|
715
|
+
for (var i = 0; i < lines.length; i++) {
|
|
716
|
+
var line = lines[i];
|
|
717
|
+
if (reSectionStart.test(line.trim())) {
|
|
718
|
+
inSection = true;
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
if (inSection) {
|
|
722
|
+
// 进入下一个同级/更高级 section 则结束
|
|
723
|
+
if (/^#{1,3}\s+\S/.test(line) && !reSectionStart.test(line.trim())) {
|
|
724
|
+
inSection = false;
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
// 收集任务项与普通要点
|
|
728
|
+
var taskParsed = parseTaskItem(line);
|
|
729
|
+
if (taskParsed) {
|
|
730
|
+
out.push(taskParsed.text);
|
|
731
|
+
} else {
|
|
732
|
+
var bullet = String(line).match(/^\s*[-*]\s+(.+)$/);
|
|
733
|
+
if (bullet && bullet[1].trim()) out.push(bullet[1].trim());
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
return out;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
module.exports = {
|
|
741
|
+
parsePlanToGoal: parsePlanToGoal,
|
|
742
|
+
resolvePlanPath: resolvePlanPath,
|
|
743
|
+
resolveProjectRoot: resolveProjectRoot,
|
|
744
|
+
// WP-198-rest:公开 primitive——依赖图 Kahn 拓扑构建。
|
|
745
|
+
// 输入 [{wpId, dependencies[]}],返回 {nodes,edges,order,hasCycle,cycle}。
|
|
746
|
+
// 供 agent-dispatcher 纯 prompt 路径 / 文档示例 / 未来图成分 WP 显式 require 调用。
|
|
747
|
+
buildDependencyGraph: buildDependencyGraph,
|
|
748
|
+
|
|
749
|
+
// 内部工具(暴露供单元测试,遵循 loop-snapshot/reflection-evaluator 惯例)
|
|
750
|
+
_slugify: slugify,
|
|
751
|
+
_extractTitle: extractTitle,
|
|
752
|
+
_extractExplicitWpId: extractExplicitWpId,
|
|
753
|
+
_parseTaskItem: parseTaskItem,
|
|
754
|
+
_extractDependencyRefs: extractDependencyRefs,
|
|
755
|
+
_splitSections: splitSections,
|
|
756
|
+
_extractChecklist: extractChecklist,
|
|
757
|
+
_isExecutableSection: isExecutableSection,
|
|
758
|
+
_assignWpIds: assignWpIds,
|
|
759
|
+
_readMaxWpNumber: readMaxWpNumber,
|
|
760
|
+
_buildChecklistSpec: buildChecklistSpec,
|
|
761
|
+
_extractSuccessCriteria: extractSuccessCriteria,
|
|
762
|
+
_DEFAULT_PLAN_RELATIVE: DEFAULT_PLAN_RELATIVE,
|
|
763
|
+
};
|