taskboard-workbench 0.0.0-stage → 0.2.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/CHANGELOG.md +132 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/cli/tb.mjs +314 -0
- package/cordis.patch.yml +5 -0
- package/index.js +633 -0
- package/lib/client.js +522 -0
- package/package.json +83 -4
- package/spec//345/233/242/351/230/237agent.md +171 -0
- package/src/core/advance.mjs +192 -0
- package/src/core/agent-teams.mjs +48 -0
- package/src/core/binding.mjs +44 -0
- package/src/core/config.mjs +22 -0
- package/src/core/debug-dispatch.mjs +94 -0
- package/src/core/delivery.mjs +93 -0
- package/src/core/deps.mjs +53 -0
- package/src/core/diagnostics.mjs +648 -0
- package/src/core/domain.mjs +235 -0
- package/src/core/empty-state.mjs +240 -0
- package/src/core/errors.mjs +139 -0
- package/src/core/executor.mjs +645 -0
- package/src/core/human-review.mjs +773 -0
- package/src/core/intake.mjs +116 -0
- package/src/core/kickoff.mjs +147 -0
- package/src/core/loop.mjs +498 -0
- package/src/core/memory.mjs +193 -0
- package/src/core/next-step.mjs +377 -0
- package/src/core/notify.mjs +183 -0
- package/src/core/owner.mjs +41 -0
- package/src/core/requirements.mjs +262 -0
- package/src/core/review.mjs +82 -0
- package/src/core/session-naming.mjs +64 -0
- package/src/core/split-ask.mjs +107 -0
- package/src/core/store.mjs +3103 -0
- package/src/core/team.mjs +170 -0
- package/src/core/verify.mjs +170 -0
- package/src/core/watchdog.mjs +144 -0
- package/src/host/ai-elaborator.mjs +26 -0
- package/src/host/team-engine.mjs +125 -0
- package/src/host/tool-surface.mjs +205 -0
- package/src/server/api.mjs +1974 -0
- package/src/server/events.mjs +32 -0
- package/src/server/kickoff.mjs +46 -0
- package/src/server/server.mjs +211 -0
- package/src/server/verify.mjs +112 -0
- package/tools/env-check.mjs +35 -0
- package/tools/seed.mjs +67 -0
- package/web/app.js +5237 -0
- package/web/index.html +15 -0
- package/web/tokens.css +1139 -0
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* loop.mjs —— 看板执行循环(FR-68/70)。
|
|
3
|
+
* 把"人工设为待执行(todo)且已归因"的任务,按活跃执行者引擎自动派发(建团队记录 + 任务级锁 + 转进行中)。
|
|
4
|
+
* 供 ①API 手动触发 ②插件定时器周期触发 共用。
|
|
5
|
+
*/
|
|
6
|
+
import { resolveCliExecutor, executeTaskWithCli, DEFAULT_TIMEOUT_MS } from "./executor.mjs";
|
|
7
|
+
import { isHumanReviewTask } from "./human-review.mjs";
|
|
8
|
+
import { normalizeQualityGates } from "./team.mjs";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 正在后台跑的 CLI 执行(任务 id → Promise)。用途:①防重复启动 ②供 API/UI 显示"执行中"。
|
|
12
|
+
*/
|
|
13
|
+
const cliRuns = new Map();
|
|
14
|
+
export function activeCliRuns() { return [...cliRuns.keys()]; }
|
|
15
|
+
function directiveFor(engine, { teamId, taskRef, requirementId, qualityGates = null }) {
|
|
16
|
+
const head = engine === "official"
|
|
17
|
+
? `[派发团队 · DSH官方自带] teamId=${teamId} 请作为 Lead 用 spawn_teammate 召集成员、team_task_create 建任务看板。`
|
|
18
|
+
: engine === "nanmicoder"
|
|
19
|
+
? `[派发团队 · @nanmicoder] teamId=${teamId}(agent_teams_create)`
|
|
20
|
+
: `[派发团队] teamId=${teamId}`;
|
|
21
|
+
// FR-50:质量门链按项目 `teamTemplate.qualityGates` 渲染(顺序 = 配置声明顺序);
|
|
22
|
+
// 未配置回落契约默认链,并在行内**注明来源**(配置 / 默认),与建队派发指令同口径。
|
|
23
|
+
const { gates, source } = normalizeQualityGates({ qualityGates });
|
|
24
|
+
return [
|
|
25
|
+
head,
|
|
26
|
+
`目标:${taskRef}`,
|
|
27
|
+
requirementId ? `需求:${requirementId}` : "",
|
|
28
|
+
`工作台任务标识:${taskRef}(执行/受阻请更新工作台状态:进行中→待验收;失败→受阻)`,
|
|
29
|
+
`质量门(${source === "configured" ? "配置" : "默认"}):${gates.join("→")}`,
|
|
30
|
+
"操作指引:按依赖就绪认领 → 执行 → 提交\"待验收\";不得置\"完成\"。",
|
|
31
|
+
].filter(Boolean).join("\n");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* FR-67:由「活跃执行者」团队(`teamTemplate.executionTeam`,形如 `<source>:<id>`)推导派发引擎。
|
|
36
|
+
* 口径(团队来源 → 引擎):
|
|
37
|
+
* · `official:*` → `official`(DSH 官方自带智能体团队,spawn_teammate / team_task_*)
|
|
38
|
+
* · `third-party:*` → `nanmicoder`(工作区 `.agent-teams/*`,@nanmicoder/dsh-agent-teams)
|
|
39
|
+
* 未选择 / 形态不合法(取不到已知 source)→ 返回 `null`,表示"不参与覆盖",回落项目引擎配置。
|
|
40
|
+
*/
|
|
41
|
+
export function engineOfExecutionTeam(executionTeam) {
|
|
42
|
+
const raw = String(executionTeam ?? "").trim();
|
|
43
|
+
if (!raw) return null;
|
|
44
|
+
const idx = raw.indexOf(":");
|
|
45
|
+
const source = idx >= 0 ? raw.slice(0, idx).trim() : raw;
|
|
46
|
+
if (source === "official") return "official";
|
|
47
|
+
if (source === "third-party") return "nanmicoder";
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* FR-67 + FR-68:**派发引擎解析** —— 活跃执行者团队优先,未选择时回落 `teamTemplate.engine`。
|
|
53
|
+
* 返回 `{ engine, source }`,`source` ∈ `execution-team` / `team-template` / `port`(供核对口径)。
|
|
54
|
+
*/
|
|
55
|
+
export function resolveEngine(store, projectId) {
|
|
56
|
+
const tpl = store.getProjectSettings(projectId).teamTemplate ?? {};
|
|
57
|
+
const fromTeam = engineOfExecutionTeam(tpl.executionTeam);
|
|
58
|
+
if (fromTeam) return { engine: fromTeam, source: "execution-team", executionTeam: String(tpl.executionTeam).trim() };
|
|
59
|
+
const fromTpl = String(tpl.engine ?? "").trim();
|
|
60
|
+
if (fromTpl) return { engine: fromTpl, source: "team-template", executionTeam: null };
|
|
61
|
+
return { engine: store.teamEnginePort?.mode ?? "instruction", source: "port", executionTeam: null };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function activeEngine(store, projectId) {
|
|
65
|
+
return resolveEngine(store, projectId).engine;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** 列表行标记(`- ` / `* ` / `1. ` / `1)` / `1、`)。 */
|
|
69
|
+
const LIST_MARKER = /^([-*]|\d+[.)、])\s+/;
|
|
70
|
+
/** 句末标点:出现即视为散文正文(非可拆解清单)。 */
|
|
71
|
+
const SENTENCE_END = /[。!?;]/;
|
|
72
|
+
/** 自动拆解的列表行数量上下限(含)与单行正文长度上限。 */
|
|
73
|
+
const MIN_LIST_LINES = 2;
|
|
74
|
+
const MAX_LIST_LINES = 6;
|
|
75
|
+
const MAX_LINE_BODY = 80;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 提取描述里的列表行正文(去掉列表标记后的文本)。
|
|
79
|
+
* 只做纯文本判定,不产生任何副作用。
|
|
80
|
+
*/
|
|
81
|
+
function listLineBodies(description) {
|
|
82
|
+
return String(description || "")
|
|
83
|
+
.split("\n")
|
|
84
|
+
.map((l) => l.trim())
|
|
85
|
+
.filter((l) => LIST_MARKER.test(l))
|
|
86
|
+
.map((l) => l.replace(LIST_MARKER, "").trim());
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 复杂任务判定(P0-7 收紧):只认**显式可拆解**。
|
|
91
|
+
* ① 带 `complex` / `复杂` 标签 → 复杂;
|
|
92
|
+
* ② 否则要求描述里的列表行数在 2–6 条之间(含),每条去标记后正文 ≤80 字符,
|
|
93
|
+
* 且不含句末标点(。!?;)——出现任一条即视为散文正文,不自动拆解。
|
|
94
|
+
*/
|
|
95
|
+
export function isComplexTask(task) {
|
|
96
|
+
if ((task.labels || []).some((l) => /complex|复杂/i.test(String(l)))) return true;
|
|
97
|
+
const bodies = listLineBodies(task.description);
|
|
98
|
+
if (bodies.length < MIN_LIST_LINES || bodies.length > MAX_LIST_LINES) return false;
|
|
99
|
+
return bodies.every((b) => b.length > 0 && b.length <= MAX_LINE_BODY && !SENTENCE_END.test(b));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function decomposeTitles(task) {
|
|
103
|
+
const lines = String(task.description || "").split("\n").map((l) => l.trim().replace(/^([-*]|\d+[.)、])\s+/, "").trim()).filter((l) => l && l.length <= 100);
|
|
104
|
+
return lines.length >= 2 ? lines : [`${task.title} — 实现`, `${task.title} — 验证`];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* FR-86:**共享文件**清单一——同轮并行改同一个文件即"共享文件冲突",必须显式串行。
|
|
109
|
+
* 只认这几个跨层共享文件(其余文件并行改动不构成必须串行的关键冲突)。
|
|
110
|
+
*/
|
|
111
|
+
export const SHARED_FILES = Object.freeze(["store.mjs", "api.mjs", "web/app.js", "index.js"]);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* FR-86:从任务**标题 + 描述**里解析"目标文件集合"。
|
|
115
|
+
* 判定口径:文本里出现上述共享文件名的**路径引用**(可带目录前缀,如 `src/core/store.mjs`)即算引用;
|
|
116
|
+
* **解析不到就视为无冲突**(空集合不参与冲突判定,绝不臆测)。
|
|
117
|
+
*/
|
|
118
|
+
export function targetFilesOf(task) {
|
|
119
|
+
const text = String(`${task?.title ?? ""}\n${task?.description ?? ""}`);
|
|
120
|
+
const found = [];
|
|
121
|
+
for (const file of SHARED_FILES) {
|
|
122
|
+
const escaped = file.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
123
|
+
// 前置:串首或分隔符(路径分隔 / 空白 / 引号括号等);后置:不得紧跟文件名字符(避免 store.mjsx / index.json 误命中)
|
|
124
|
+
const re = new RegExp(`(^|[\\s/\\\\"'\`(\\[=:,>])${escaped}(?![\\w.-])`);
|
|
125
|
+
if (re.test(text)) found.push(file);
|
|
126
|
+
}
|
|
127
|
+
return found;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 需求条目的可读编号(取不到就回落 null,绝不因缺编号而抛错)。 */
|
|
131
|
+
function requirementLabel(store, requirementId) {
|
|
132
|
+
if (!requirementId) return null;
|
|
133
|
+
try { return store.getRequirementItemById(requirementId)?.identifier ?? null; } catch { return null; }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* FR-86:**影响域**——该任务**自身及其下游**(沿依赖图可达的后继),排除已完结(done)/已取消(canceled)。
|
|
138
|
+
* 关键阻塞叫停只覆盖这个集合,**不得整批停**同需求其余任务。
|
|
139
|
+
*/
|
|
140
|
+
export function downstreamOf(store, task) {
|
|
141
|
+
const out = [];
|
|
142
|
+
const seen = new Set([task.id]);
|
|
143
|
+
const queue = [task.id];
|
|
144
|
+
while (queue.length) {
|
|
145
|
+
let edges = [];
|
|
146
|
+
try { edges = store.listTaskDependencies(queue.shift())?.blocks ?? []; } catch { edges = []; }
|
|
147
|
+
for (const edge of edges) {
|
|
148
|
+
if (seen.has(edge.id)) continue;
|
|
149
|
+
seen.add(edge.id);
|
|
150
|
+
const t = (() => { try { return store.getTaskById(edge.id); } catch { return null; } })();
|
|
151
|
+
if (t) out.push({ id: t.id, identifier: t.identifier, title: t.title, status: t.status, requirementId: t.requirementId ?? null });
|
|
152
|
+
if (!t || (t.status !== "done" && t.status !== "canceled")) queue.push(edge.id);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* FR-86 关键阻塞判定(**只读**,纯函数:不改库、不抛错)→ 返回 `null`(可派发)或 `{ reason, detail }`。
|
|
160
|
+
*
|
|
161
|
+
* 三种"关键阻塞"(其余一律不叫停——同需求其余任务照常并行):
|
|
162
|
+
* · `review-not-passed` FR-83 评审未过:`review_state=changes_requested` 的任务已完成过一轮评审门
|
|
163
|
+
* 却被退回,**不得**被当作"全新 todo"再自动派发。
|
|
164
|
+
* · `human-review-pending` FR-85 人工校验未结论:上游挂了人在环升级门且尚无结论,不得越过门推进。
|
|
165
|
+
* · `upstream-blocked` 上游依赖未就绪:前置处于**不会自行变 done** 的终态(受阻 / 人工停手 / 待办池 / 待验收)。
|
|
166
|
+
*
|
|
167
|
+
* 明确**不算**关键阻塞的情形:前置是普通未完成 todo —— 那些任务本身同轮就在派发队列里(FR-86 要的是最大并行,
|
|
168
|
+
* 不是串行等待),因此不因"上游还没 done"就叫停下游。
|
|
169
|
+
*/
|
|
170
|
+
export function keyBlockOf(store, task) {
|
|
171
|
+
if (task?.reviewState === "changes_requested") {
|
|
172
|
+
return {
|
|
173
|
+
reason: "review-not-passed",
|
|
174
|
+
detail: `第 ${Number(task.reviewRound ?? 0)} 轮返工未通过(已退回来源列 ${task.reviewSourceStatus ?? "in_progress"}),须返工后再提交评审`,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
let blockers = [];
|
|
178
|
+
try { blockers = store.listTaskDependencies(task.id)?.blockers ?? []; } catch { blockers = []; }
|
|
179
|
+
const unmet = blockers.filter((b) => b.status !== "done" && b.status !== "canceled");
|
|
180
|
+
if (!unmet.length) return null;
|
|
181
|
+
const gate = unmet.find((b) => {
|
|
182
|
+
const bt = store.getTaskById(b.id);
|
|
183
|
+
return bt && isHumanReviewTask(bt) && (bt.humanReviewState ?? "pending") === "pending";
|
|
184
|
+
});
|
|
185
|
+
if (gate) {
|
|
186
|
+
return { reason: "human-review-pending", detail: `上游人工校验门 ${gate.identifier} 尚无结论(FR-85),不得越过该门推进` };
|
|
187
|
+
}
|
|
188
|
+
return { reason: "upstream-blocked", detail: `上游依赖未就绪:${unmet.map((b) => `${b.identifier}(${b.status})`).join("、")}` };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* FR-86:同需求并行度汇总("同轮尽多派发"的可核对证据)。
|
|
193
|
+
* 每条带原因:该需求下被裁剪过则说明裁剪条数,否则说明只受并发上限约束。
|
|
194
|
+
*/
|
|
195
|
+
function summarizeParallel(store, dispatched, stopped, serialized) {
|
|
196
|
+
const groups = new Map();
|
|
197
|
+
for (const d of dispatched) {
|
|
198
|
+
const t = store.getTaskRef(d.task);
|
|
199
|
+
const key = t?.requirementId ?? null;
|
|
200
|
+
if (!groups.has(key)) groups.set(key, []);
|
|
201
|
+
groups.get(key).push(d.task);
|
|
202
|
+
}
|
|
203
|
+
return [...groups.entries()]
|
|
204
|
+
.map(([requirementId, tasks]) => {
|
|
205
|
+
let identifier = null;
|
|
206
|
+
try { identifier = requirementId ? (store.getRequirementItemById(requirementId)?.identifier ?? null) : null; } catch { identifier = null; }
|
|
207
|
+
const cut = stopped.filter((s) => s.requirementId === requirementId).length + serialized.filter((s) => s.requirementId === requirementId).length;
|
|
208
|
+
return {
|
|
209
|
+
requirement: identifier ?? requirementId,
|
|
210
|
+
requirementId: requirementId ?? null,
|
|
211
|
+
dispatched: tasks,
|
|
212
|
+
count: tasks.length,
|
|
213
|
+
reason: cut
|
|
214
|
+
? `同需求最大并行:同轮派发 ${tasks.length} 条;另有 ${cut} 条因关键阻塞/共享文件冲突被裁剪(未整批停)`
|
|
215
|
+
: `同需求最大并行:同轮派发 ${tasks.length} 条(仅受并发上限与任务级锁约束)`,
|
|
216
|
+
};
|
|
217
|
+
})
|
|
218
|
+
.sort((a, b) => b.count - a.count);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** FR-86:串行化让行时在看板**显式标注原因**(同因去重,避免定时循环反复刷屏)。 */
|
|
222
|
+
function markSerialized(store, task, holder, file) {
|
|
223
|
+
const body = `【FR-86 串行化】与任务 ${holder} 冲突于共享文件 ${file}:本轮让行(待其执行结束后才放行;同需求其余任务照常并行)。`;
|
|
224
|
+
try {
|
|
225
|
+
if (store.listComments(task.identifier).some((c) => c.body === body)) return;
|
|
226
|
+
store.addComment(task.identifier, { body });
|
|
227
|
+
} catch { /* 标注失败不影响裁剪结论 */ }
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** 派发单个任务:建团队记录 + 任务级锁 + 转"进行中"(把派发目标会话写进任务,供"是否仍在执行"跟踪)。 */
|
|
231
|
+
function dispatchOne(store, emit, task, engine, targetSessionId = null) {
|
|
232
|
+
if (store.getTaskLock(task.id)) return null;
|
|
233
|
+
const run = store.createTeamRun({ targetTask: task.id });
|
|
234
|
+
// 记录团队成员(来自项目团队模板;无则按任务归属角色/默认 engineer)——避免"成员为空"
|
|
235
|
+
const tpl = store.getProjectSettings(task.projectId).teamTemplate ?? {};
|
|
236
|
+
const roles = (tpl.roles || []).map((r) => r.role).filter(Boolean);
|
|
237
|
+
for (const role of (roles.length ? roles : [task.owner || "engineer"])) { try { store.addTeamMember({ teamId: run.id, role }); } catch { /* ignore */ } }
|
|
238
|
+
try { store.acquireTeamTask({ teamId: run.id, task: task.id, kind: "work" }); } catch { return null; }
|
|
239
|
+
store.startTaskExecution(task.id, { sessionId: targetSessionId ?? null });
|
|
240
|
+
emit("task.updated", { taskId: task.id, identifier: task.identifier, status: "in_progress" });
|
|
241
|
+
const directive = directiveFor(engine, { teamId: run.id, taskRef: task.identifier, requirementId: task.requirementId, qualityGates: tpl.qualityGates ?? null });
|
|
242
|
+
return { task: task.identifier, teamId: run.id, mode: engine, sessionId: targetSessionId ?? null, head: directive.split("\n")[0], directive };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 已有执行会话仍在执行且未返回结论 → 不得再次下发。
|
|
247
|
+
* 判定:该任务已记录执行会话,且宿主 agents 注册表显示该会话 `running`(或 inbox 仍有待消费消息)。
|
|
248
|
+
*/
|
|
249
|
+
function inflightOf(store, task) {
|
|
250
|
+
const state = store.taskSessionState(task);
|
|
251
|
+
if (!state?.sessionId) return null;
|
|
252
|
+
if (state.running || (state.live && (state.pending ?? 0) > 0)) {
|
|
253
|
+
return { sessionId: state.sessionId, status: state.status ?? null, pending: state.pending ?? 0 };
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 任务下发:把派发指令投递到项目**主会话**(真实闭环)。无绑定会话 / 宿主无 prompt 能力则如实标注降级。
|
|
260
|
+
*/
|
|
261
|
+
async function deliverDispatched(store, project, dispatched) {
|
|
262
|
+
const boundSessionId = project?.initialSessionId ?? null;
|
|
263
|
+
const port = store.hostSessionPort;
|
|
264
|
+
const result = { boundSessionId, delivered: 0 };
|
|
265
|
+
if (!dispatched.length) return result;
|
|
266
|
+
const reason = boundSessionId ? "prompt-unavailable" : "no-bound-session";
|
|
267
|
+
for (const d of dispatched) { d.delivered = false; d.deliveryReason = reason; }
|
|
268
|
+
if (!boundSessionId || typeof port?.sendPrompt !== "function") return result;
|
|
269
|
+
// 一轮只投递**一条**汇总指令(避免拆解出大量子任务时刷屏目标会话)。
|
|
270
|
+
const lines = [`[任务工作台下发] 本轮派发 ${dispatched.length} 条任务(请作为项目执行团队承接):`];
|
|
271
|
+
for (const d of dispatched) lines.push(`· ${d.task}(teamId=${d.teamId})`);
|
|
272
|
+
lines.push("操作指引:按依赖就绪逐个认领 → 执行 → 提交\"待验收\"(失败→受阻);不得置\"完成\"(人工确认)。");
|
|
273
|
+
lines.push("状态回写:任务看板 taskboard_update_status(进行中/待验收/受阻),完成由人工确认。");
|
|
274
|
+
let r = null;
|
|
275
|
+
try { r = await port.sendPrompt(boundSessionId, lines.join("\n")); } catch { r = null; }
|
|
276
|
+
const sent = Boolean(r?.sent);
|
|
277
|
+
for (const d of dispatched) {
|
|
278
|
+
d.delivered = sent;
|
|
279
|
+
d.deliveryReason = sent ? null : (r?.reason ?? "unknown");
|
|
280
|
+
if (sent) { try { store.addComment(store.requireTaskRef(d.task).id, { body: `已下发派发指令到主会话 ${boundSessionId}` }); } catch { /* ignore */ } }
|
|
281
|
+
}
|
|
282
|
+
if (sent) result.delivered = dispatched.length;
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* CLI 执行器派发("由看板侧真的调 claude CLI 干活")。
|
|
288
|
+
* 命中条件:项目设置里角色执行器是 `cli:*`(本仓库约定 engineer → `cli:claude`)。
|
|
289
|
+
* 语义:**后台启动**(一次真实开发可能几十分钟,不能阻塞 HTTP 请求)→ 立即把任务标为已投递,
|
|
290
|
+
* 执行结果由执行器自己回写看板(评论证据 + 尝试记录 + 状态回流)。
|
|
291
|
+
* 返回 `null` 表示该项目没有配 CLI 执行器(调用方回落原"投递主会话"路径)。
|
|
292
|
+
*/
|
|
293
|
+
function deliverViaCliExecutors(store, project, dispatched, options = {}) {
|
|
294
|
+
if (!dispatched.length) return null;
|
|
295
|
+
const settings = store.getProjectSettings(project.id);
|
|
296
|
+
const executor = resolveCliExecutor(settings);
|
|
297
|
+
if (!executor) return null;
|
|
298
|
+
const { spawnImpl, env, now, timeoutMs = DEFAULT_TIMEOUT_MS } = options;
|
|
299
|
+
for (const d of dispatched) {
|
|
300
|
+
const task = store.requireTaskRef(d.task);
|
|
301
|
+
if (cliRuns.has(task.id)) { d.delivered = false; d.deliveryReason = "cli-already-running"; continue; }
|
|
302
|
+
d.delivered = true; d.deliveryReason = null; d.via = executor.id;
|
|
303
|
+
const promise = executeTaskWithCli({ store, task, project, settings, spawnImpl, env, now, timeoutMs })
|
|
304
|
+
.catch((error) => ({ ran: false, reason: String(error?.message ?? error) }))
|
|
305
|
+
.finally(() => { cliRuns.delete(task.id); try { options.onFinish?.({ task: task.identifier }); } catch { /* ignore */ } });
|
|
306
|
+
cliRuns.set(task.id, promise);
|
|
307
|
+
}
|
|
308
|
+
const delivered = dispatched.filter((d) => d.delivered).length;
|
|
309
|
+
return { boundSessionId: project.initialSessionId ?? null, delivered, via: executor.id, executor: executor.id, background: true };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* 任务的"逻辑身份":同需求 + 同派生链(parentId 根)+ 同标题 → 视为**同一逻辑任务**。
|
|
314
|
+
* 用途:防重复派发(历史遗留的兄弟任务不得被再次拆解派发),与"共享文件冲突"无关。
|
|
315
|
+
*/
|
|
316
|
+
function logicalKeyOf(task, rootOf) {
|
|
317
|
+
return `${task.requirementId ?? ""}|${rootOf(task)}|${String(task.title ?? "").trim()}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export async function runExecutionLoop({ store, events, projectId, limit = 20, cliOptions = null } = {}) {
|
|
321
|
+
const project = store.requireProject(projectId);
|
|
322
|
+
// FR-67:派发引擎 = 活跃执行者团队优先(`executionTeam`)→ 回落项目引擎配置。
|
|
323
|
+
const { engine, source: engineSource, executionTeam } = resolveEngine(store, project.id);
|
|
324
|
+
const targetSessionId = project.initialSessionId ?? null; // 派发目标=项目主会话(也写入任务,供状态跟踪)
|
|
325
|
+
const emit = (type, payload) => { try { events?.publish?.(type, payload); } catch { /* ignore */ } };
|
|
326
|
+
const todos = store.listTasks({ projectId: project.id, status: "todo" }).filter((t) => t.requirementId);
|
|
327
|
+
const dispatched = []; const decomposed = []; const blockedByInflight = []; const blockedByGuard = []; let skipped = 0;
|
|
328
|
+
// FR-85b:人工校验任务**不是可派发的工作**(执行侧不得认领、不得置 done)。
|
|
329
|
+
// 一旦它出现在「可开工」列,这里必须整批跳过并如实给出原因,绝不投递给任何执行者。
|
|
330
|
+
const blockedByHumanReview = [];
|
|
331
|
+
// FR-86:被关键阻塞裁剪的任务(结构化,带原因与影响域)。
|
|
332
|
+
const stopped = []; const blockedByUpstream = [];
|
|
333
|
+
// FR-86:因共享文件冲突被显式串行化的任务(同轮只放行一条)。
|
|
334
|
+
const serialized = [];
|
|
335
|
+
|
|
336
|
+
// ---- FR-86 批级索引 ----
|
|
337
|
+
// ① 同轮已占用的共享文件(file → 已放行任务标识):同轮只放行一条,其余串行化。
|
|
338
|
+
const batchFiles = new Map();
|
|
339
|
+
// ② "逻辑任务"去重:同需求同派生链下已有同名任务(非本批)→ 本批同名 todo 视为历史遗留,不再派发。
|
|
340
|
+
const projectTasks = store.listTasks({ projectId: project.id });
|
|
341
|
+
const byId = new Map(projectTasks.map((t) => [t.id, t]));
|
|
342
|
+
// FR-86 跨轮(真机复现 2026-10-18):冲突表若只统计**本轮**放行的任务,被让行的那条会在下一跳
|
|
343
|
+
// 因前一条已离开 `todo`(转进行中)而被放行 —— 于是同一共享文件被两条任务**并行修改**
|
|
344
|
+
// (实测两条 claude 任务重叠执行约 105s)。故把**在途**任务的共享文件占用也并进本轮索引,
|
|
345
|
+
// 让"让行"判定跨轮成立:只要占用方仍在执行(有认领锁或未结束的尝试记录),本轮就不放行。
|
|
346
|
+
const occupying = (t) => {
|
|
347
|
+
if (t?.status !== "in_progress") return false;
|
|
348
|
+
if (t.claimLock) return true;
|
|
349
|
+
try { if (store.getCurrentRun(t)) return true; } catch { /* ignore */ }
|
|
350
|
+
// 会话投递路径不建尝试记录,但派发时必留下任务级执行锁(团队执行单元 open/running);
|
|
351
|
+
// 该锁在任务离开"进行中"时一并关闭,故可安全用作"仍在执行"的判据。
|
|
352
|
+
try { return Boolean(store.getTaskLock(t.id)); } catch { return false; }
|
|
353
|
+
};
|
|
354
|
+
for (const t of projectTasks) {
|
|
355
|
+
if (!occupying(t)) continue;
|
|
356
|
+
for (const f of targetFilesOf(t)) if (!batchFiles.has(f)) batchFiles.set(f, t.identifier);
|
|
357
|
+
}
|
|
358
|
+
const rootOf = (t) => {
|
|
359
|
+
let cur = t; const seen = new Set([t.id]);
|
|
360
|
+
while (cur?.parentId && byId.has(cur.parentId) && !seen.has(cur.parentId)) { seen.add(cur.parentId); cur = byId.get(cur.parentId); }
|
|
361
|
+
return cur?.id ?? t.id;
|
|
362
|
+
};
|
|
363
|
+
const batchIds = new Set(todos.map((t) => t.id));
|
|
364
|
+
const batchKeys = new Set(todos.map((t) => logicalKeyOf(t, rootOf)));
|
|
365
|
+
const substantive = new Set(["blocked", "in_review", "in_progress", "done", "canceled", "todo"]);
|
|
366
|
+
const dupKeys = new Set();
|
|
367
|
+
for (const t of projectTasks) {
|
|
368
|
+
if (batchIds.has(t.id)) continue;
|
|
369
|
+
if (!substantive.has(t.status)) continue;
|
|
370
|
+
const key = logicalKeyOf(t, rootOf);
|
|
371
|
+
if (batchKeys.has(key)) dupKeys.add(key);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// 派发槽位:共享文件冲突检查(显式串行)→ 建锁派发 → 占用文件。
|
|
375
|
+
let slotCount = 0;
|
|
376
|
+
const dispatchSlot = (task, files) => {
|
|
377
|
+
const holder = files.map((f) => ({ file: f, task: batchFiles.get(f) })).find((x) => x.task);
|
|
378
|
+
if (holder) {
|
|
379
|
+
serialized.push({
|
|
380
|
+
task: task.identifier, requirementId: task.requirementId, requirement: requirementLabel(store, task.requirementId),
|
|
381
|
+
reason: "shared-file-conflict", file: holder.file, heldBy: holder.task,
|
|
382
|
+
detail: `目标文件 ${holder.file} 与任务 ${holder.task} 冲突(该任务本轮已放行或仍在执行中):本轮只放行一条,其余显式串行`,
|
|
383
|
+
});
|
|
384
|
+
markSerialized(store, task, holder.task, holder.file);
|
|
385
|
+
return false;
|
|
386
|
+
}
|
|
387
|
+
const d = dispatchOne(store, emit, task, engine, targetSessionId);
|
|
388
|
+
if (!d) { skipped += 1; return false; }
|
|
389
|
+
dispatched.push(d); slotCount += 1;
|
|
390
|
+
for (const f of files) batchFiles.set(f, task.identifier);
|
|
391
|
+
return true;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
// 候选项队列:本批 todo + 复杂任务拆解出的子任务(子任务继承授权点=todo,同轮尽多派发)。
|
|
395
|
+
const candidates = todos.map((t) => ({ task: t, parent: null }));
|
|
396
|
+
for (let i = 0; i < candidates.length && slotCount < limit; i += 1) {
|
|
397
|
+
const t = candidates[i].task;
|
|
398
|
+
if (isHumanReviewTask(t)) { blockedByHumanReview.push({ task: t.identifier, reason: "human-review" }); continue; }
|
|
399
|
+
if (store.getTaskLock(t.id)) { skipped += 1; continue; }
|
|
400
|
+
if (dupKeys.has(logicalKeyOf(t, rootOf))) {
|
|
401
|
+
stopped.push({
|
|
402
|
+
task: t.identifier, requirementId: t.requirementId, requirement: requirementLabel(store, t.requirementId),
|
|
403
|
+
reason: "duplicate-of-existing", detail: "同需求同派生链下已存在同名任务:防重复派发(历史遗留副本不再派发)",
|
|
404
|
+
});
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
// 防重复下发:已有执行会话仍在执行(未返回结论)→ 停发并记录提醒。
|
|
408
|
+
const inflight = inflightOf(store, t);
|
|
409
|
+
if (inflight) { blockedByInflight.push({ task: t.identifier, ...inflight }); continue; }
|
|
410
|
+
// FR-86:关键阻塞 → 只停**该任务 + 依赖它的下游**(记影响域),同需求其余任务照常并行。
|
|
411
|
+
// 顺序上先于 FR-78 守卫:评审未过/上游未就绪是**批次级**的关键阻塞,原因比"近期成功窗口"这类
|
|
412
|
+
// 单任务守卫更根本 —— 若先判守卫,返工退回的任务会被报成 `recent-success`,把真实原因盖掉。
|
|
413
|
+
const key = keyBlockOf(store, t);
|
|
414
|
+
if (key) {
|
|
415
|
+
const entry = {
|
|
416
|
+
task: t.identifier, requirementId: t.requirementId, requirement: requirementLabel(store, t.requirementId),
|
|
417
|
+
reason: key.reason, detail: key.detail, downstream: downstreamOf(store, t),
|
|
418
|
+
};
|
|
419
|
+
stopped.push(entry);
|
|
420
|
+
if (key.reason === "upstream-blocked") blockedByUpstream.push({ task: t.identifier, reason: key.reason, detail: key.detail });
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
// FR-78:失败熔断 / 配额鉴权 / 近期成功窗口 → 不自动重试
|
|
424
|
+
const guard = typeof store.dispatchGuard === "function" ? store.dispatchGuard(t) : null;
|
|
425
|
+
if (guard) { blockedByGuard.push({ task: t.identifier, reason: guard }); continue; }
|
|
426
|
+
// FR-69:复杂任务 → 先自动拆解为关联子任务(子任务继承授权点=todo)→ 再派发子任务
|
|
427
|
+
if (isComplexTask(t)) {
|
|
428
|
+
const children = [];
|
|
429
|
+
for (const title of decomposeTitles(t)) {
|
|
430
|
+
const child = store.createTask({ projectId: project.id, title, status: "todo", parentId: t.id, description: t.description });
|
|
431
|
+
if (t.requirementId) { try { store.associateRequirementTask(t.requirementId, child.id); } catch { /* ignore */ } }
|
|
432
|
+
children.push(child);
|
|
433
|
+
}
|
|
434
|
+
try { store.updateTask(t.identifier, { expectedVersion: t.version, status: "in_progress" }); } catch { /* ignore */ }
|
|
435
|
+
emit("task.updated", { taskId: t.id, identifier: t.identifier, status: "in_progress" });
|
|
436
|
+
decomposed.push({ parent: t.identifier, children: children.map((c) => c.identifier) });
|
|
437
|
+
// 子任务同轮尽多派发(上限前直接派发,不再一条一条串)
|
|
438
|
+
for (const c of children) {
|
|
439
|
+
if (slotCount >= limit) {
|
|
440
|
+
stopped.push({
|
|
441
|
+
task: c.identifier, requirementId: c.requirementId, requirement: requirementLabel(store, c.requirementId),
|
|
442
|
+
reason: "batch-limit", detail: `同轮并发上限 ${limit} 已用满:本轮不再派发(下一轮继续)`,
|
|
443
|
+
});
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
dispatchSlot(c, targetFilesOf(c));
|
|
447
|
+
}
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
if (slotCount >= limit) {
|
|
451
|
+
stopped.push({
|
|
452
|
+
task: t.identifier, requirementId: t.requirementId, requirement: requirementLabel(store, t.requirementId),
|
|
453
|
+
reason: "batch-limit", detail: `同轮并发上限 ${limit} 已用满:本轮不再派发(下一轮继续)`,
|
|
454
|
+
});
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
dispatchSlot(t, targetFilesOf(t));
|
|
458
|
+
}
|
|
459
|
+
// 下发:①项目配了 CLI 执行器(`cli:*`)→ 由看板侧**真的**调 CLI 干活(后台);
|
|
460
|
+
// ②否则回落"把派发指令投递到项目主会话"(当前 DSH 版本受 preset/scope 限制,多会降级)。
|
|
461
|
+
const cliDelivery = deliverViaCliExecutors(store, project, dispatched, {
|
|
462
|
+
...(cliOptions ?? {}), onFinish: (info) => emit("task.updated", { identifier: info.task, status: "cli-finished" }),
|
|
463
|
+
});
|
|
464
|
+
const delivery = cliDelivery ?? await deliverDispatched(store, project, dispatched);
|
|
465
|
+
if (cliDelivery) {
|
|
466
|
+
for (const d of dispatched) {
|
|
467
|
+
if (!d.delivered) continue;
|
|
468
|
+
try { store.addComment(store.requireTaskRef(d.task).id, { body: `已交给 CLI 执行器 ${cliDelivery.executor} 后台执行(${d.task})` }); } catch { /* ignore */ }
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// FR-81:本轮结束后把各订阅游标之后的**终态事件**推送出去(工作台关着也能闭环回会话)。
|
|
472
|
+
// 投递失败不抛错:订阅自己保留游标、记 last_error,下轮(或人工 flush)自动重试。
|
|
473
|
+
let notifications = { count: 0, results: [] };
|
|
474
|
+
try { notifications = await store.flushNotifySubscriptions({ projectId: project.id }); } catch { notifications = { count: 0, results: [], error: "flush-failed" }; }
|
|
475
|
+
for (const item of notifications.results) {
|
|
476
|
+
if (item.delivered) emit("notify.delivered", { projectId: project.id, subscriptionId: item.id, kinds: item.kinds, count: item.count });
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
engine, engineSource, executionTeam, candidates: todos.length, dispatched, decomposed, skipped,
|
|
480
|
+
blockedByInflight, blockedByGuard, blockedByHumanReview, boundSessionId: delivery.boundSessionId, delivered: delivery.delivered,
|
|
481
|
+
deliveryVia: delivery.via ?? (delivery.delivered ? "session" : null),
|
|
482
|
+
notifications,
|
|
483
|
+
// FR-86:并行策略的**结构化裁剪结果**(每条带原因)。
|
|
484
|
+
// 已有字段名与语义一律不动:下面三项是**新增**的观测口径。
|
|
485
|
+
// · parallel —— 按需求条目汇总的"同轮尽多派发"结果(含每条需求被裁剪几条)
|
|
486
|
+
// · serialized —— 因共享文件冲突被显式串行化的任务(带冲突文件与让行对象)
|
|
487
|
+
// · stopped —— 被关键阻塞/并发上限裁剪的任务(带原因 + 影响域 downstream)
|
|
488
|
+
parallel: summarizeParallel(store, dispatched, stopped, serialized),
|
|
489
|
+
serialized,
|
|
490
|
+
stopped,
|
|
491
|
+
blockedByUpstream,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** 已开启"执行循环"的项目。 */
|
|
496
|
+
export function listLoopProjects(store) {
|
|
497
|
+
return store.listProjects().filter((p) => store.getProjectSettings(p.id).loop?.enabled);
|
|
498
|
+
}
|