openfeel 1.5.25 → 1.6.1
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/README.en.md +14 -0
- package/README.zh-CN.md +14 -0
- package/dist/commands/dashboard.d.ts.map +1 -1
- package/dist/commands/dashboard.js +10 -1
- package/dist/commands/dashboard.js.map +1 -1
- package/dist/commands/flow.d.ts.map +1 -1
- package/dist/commands/flow.js +126 -25
- package/dist/commands/flow.js.map +1 -1
- package/dist/commands/lint.d.ts +5 -1
- package/dist/commands/lint.d.ts.map +1 -1
- package/dist/commands/lint.js +373 -0
- package/dist/commands/lint.js.map +1 -1
- package/dist/commands/req.d.ts.map +1 -1
- package/dist/commands/req.js +814 -88
- package/dist/commands/req.js.map +1 -1
- package/dist/commands/stage.d.ts.map +1 -1
- package/dist/commands/stage.js +19 -0
- package/dist/commands/stage.js.map +1 -1
- package/dist/core/adherence.d.ts +135 -0
- package/dist/core/adherence.d.ts.map +1 -0
- package/dist/core/adherence.js +221 -0
- package/dist/core/adherence.js.map +1 -0
- package/dist/core/dashboard/aggregate.d.ts +39 -0
- package/dist/core/dashboard/aggregate.d.ts.map +1 -1
- package/dist/core/dashboard/aggregate.js +64 -5
- package/dist/core/dashboard/aggregate.js.map +1 -1
- package/dist/core/dashboard/render.d.ts.map +1 -1
- package/dist/core/dashboard/render.js +88 -1
- package/dist/core/dashboard/render.js.map +1 -1
- package/dist/core/dev-status.d.ts +25 -0
- package/dist/core/dev-status.d.ts.map +1 -1
- package/dist/core/dev-status.js +35 -0
- package/dist/core/dev-status.js.map +1 -1
- package/dist/core/errors.d.ts +38 -3
- package/dist/core/errors.d.ts.map +1 -1
- package/dist/core/errors.js +22 -3
- package/dist/core/errors.js.map +1 -1
- package/dist/core/flow/gate-override.d.ts +72 -0
- package/dist/core/flow/gate-override.d.ts.map +1 -0
- package/dist/core/flow/gate-override.js +71 -0
- package/dist/core/flow/gate-override.js.map +1 -0
- package/dist/core/flow-manager.d.ts +118 -3
- package/dist/core/flow-manager.d.ts.map +1 -1
- package/dist/core/flow-manager.js +357 -14
- package/dist/core/flow-manager.js.map +1 -1
- package/dist/core/i18n-data/en.d.ts.map +1 -1
- package/dist/core/i18n-data/en.js +96 -5
- package/dist/core/i18n-data/en.js.map +1 -1
- package/dist/core/i18n-data/zh-CN.d.ts.map +1 -1
- package/dist/core/i18n-data/zh-CN.js +96 -5
- package/dist/core/i18n-data/zh-CN.js.map +1 -1
- package/dist/core/lint-req.d.ts +17 -1
- package/dist/core/lint-req.d.ts.map +1 -1
- package/dist/core/lint-req.js +14 -2
- package/dist/core/lint-req.js.map +1 -1
- package/dist/core/plan/body-lint.d.ts +58 -0
- package/dist/core/plan/body-lint.d.ts.map +1 -0
- package/dist/core/plan/body-lint.js +197 -0
- package/dist/core/plan/body-lint.js.map +1 -0
- package/dist/core/plan/filled.d.ts +143 -0
- package/dist/core/plan/filled.d.ts.map +1 -0
- package/dist/core/plan/filled.js +244 -0
- package/dist/core/plan/filled.js.map +1 -0
- package/dist/core/plan/path.d.ts +24 -0
- package/dist/core/plan/path.d.ts.map +1 -1
- package/dist/core/plan/path.js +56 -0
- package/dist/core/plan/path.js.map +1 -1
- package/dist/core/req/aggregate.d.ts.map +1 -1
- package/dist/core/req/aggregate.js +5 -0
- package/dist/core/req/aggregate.js.map +1 -1
- package/dist/core/req/body-file.d.ts +103 -0
- package/dist/core/req/body-file.d.ts.map +1 -1
- package/dist/core/req/body-file.js +221 -2
- package/dist/core/req/body-file.js.map +1 -1
- package/dist/core/req/plan-file.d.ts +83 -0
- package/dist/core/req/plan-file.d.ts.map +1 -0
- package/dist/core/req/plan-file.js +205 -0
- package/dist/core/req/plan-file.js.map +1 -0
- package/dist/core/req/requirement-ops.d.ts +151 -15
- package/dist/core/req/requirement-ops.d.ts.map +1 -1
- package/dist/core/req/requirement-ops.js +402 -22
- package/dist/core/req/requirement-ops.js.map +1 -1
- package/dist/core/req/types.d.ts +63 -2
- package/dist/core/req/types.d.ts.map +1 -1
- package/dist/core/req/types.js +10 -2
- package/dist/core/req/types.js.map +1 -1
- package/dist/core/template-loader.d.ts.map +1 -1
- package/dist/core/template-loader.js +87 -0
- package/dist/core/template-loader.js.map +1 -1
- package/dist/core/templates.d.ts +1 -1
- package/dist/core/templates.d.ts.map +1 -1
- package/dist/core/templates.js +18 -0
- package/dist/core/templates.js.map +1 -1
- package/dsh/ACTION-GUIDE.md +50 -8
- package/dsh/ADAPTER.md +119 -13
- package/dsh/README.md +49 -26
- package/dsh/docs/compat/persistence.md +26 -0
- package/dsh/docs/compat/pipeline.md +24 -0
- package/dsh/docs/compat/req.md +29 -0
- package/dsh/docs/compat/stage.md +25 -0
- package/dsh/lib/adapters/dispatch-ledger.js +432 -0
- package/dsh/lib/adapters/dispatch.js +54 -6
- package/dsh/lib/adapters/next-action.js +336 -0
- package/dsh/lib/guard.js +694 -24
- package/dsh/lib/index.js +37 -0
- package/dsh/lib/surface/prompt.js +66 -178
- package/dsh/lib/tools/index.js +527 -12
- package/dsh/package.json +2 -2
- package/package.json +2 -1
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 派发台账(v1.6.0 / REQ-057;v1.6.1 / REQ-066 增闭环 closing)— DSH 适配器侧
|
|
3
|
+
*
|
|
4
|
+
* 文件:`<project>/.openfeel/dev/dispatch.jsonl`(UTF-8 / LF,一行一条 JSON)。
|
|
5
|
+
* 行 schema(plan §5.4):
|
|
6
|
+
* { dispatchId, ts, leadSessionId, origin, stage, phase,
|
|
7
|
+
* targetAgent, role, taskId, goal, files[], returnFormat, dispatchDepth }
|
|
8
|
+
* 可选闭环字段(v1.6.1 / REQ-066 A66.1,由 `closeDispatch` 原地补写):
|
|
9
|
+
* closing: { status: 'done'|'failed', artifacts: string[], durationMs: number, summary: string }
|
|
10
|
+
* closedAt: ISO 时间(**行的顶层字段**——A66.1 要求 closing 恰 4 键,时间戳不进 closing)
|
|
11
|
+
*
|
|
12
|
+
* 职责:把「派给谁、派什么、允许写哪些文件」变成可审计的记录,并在完成后补上
|
|
13
|
+
* 产物/耗时/失败原因,供 `feel_status.dispatch` 汇总(提示级,不做硬拦——用户裁定 ②)。
|
|
14
|
+
*
|
|
15
|
+
* 纪律:
|
|
16
|
+
* - 只有适配器进程直接 fs 写;该文件在 `PROTECTED_OPENFEEL_PATHS` 里,
|
|
17
|
+
* agent 的工具写(write/edit/shell)会被 guard 拒绝(防伪造「已派发」证据)。
|
|
18
|
+
* - 读写全部 best-effort:台账是观测手段,任何异常都不得阻断 `feel_*` 主流程。
|
|
19
|
+
* - 原子写沿用 `sessions.js` 的习惯:同目录 temp + rename(整文件重写,顺带 prune)。
|
|
20
|
+
* - 并发语义与 `sessions.js` 相同:无文件锁,读-改-写之间可能有丢行风险;
|
|
21
|
+
* 台账每行独立、去重按 `leadSessionId+taskId+goal`,丢一行只影响审计完整性,
|
|
22
|
+
* 不影响门禁判定(因此不引入锁)。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
26
|
+
import { randomBytes } from 'node:crypto';
|
|
27
|
+
import { dirname, join } from 'node:path';
|
|
28
|
+
|
|
29
|
+
/** 台账相对项目根的路径(POSIX 风格,供提示与测试断言)。 */
|
|
30
|
+
export const DISPATCH_LEDGER_REL_PATH = '.openfeel/dev/dispatch.jsonl';
|
|
31
|
+
|
|
32
|
+
/** 合法角色闭集(`feel_dispatch.role` 枚举同源)。 */
|
|
33
|
+
export const DISPATCH_ROLES = Object.freeze(['planner', 'executor', 'reviewer', 'tester', 'archiver']);
|
|
34
|
+
|
|
35
|
+
/** 台账保留时长(24h;超龄行在下次写入时清理)。 */
|
|
36
|
+
export const DISPATCH_RETENTION_MS = 24 * 60 * 60_000;
|
|
37
|
+
|
|
38
|
+
/** 台账行数上限(超出只保留最近 200 行)。 */
|
|
39
|
+
export const DISPATCH_MAX_RECORDS = 200;
|
|
40
|
+
|
|
41
|
+
/** 去重窗口(同 `leadSessionId+taskId+goal` 5 分钟内视为重复)。 */
|
|
42
|
+
export const DISPATCH_DEDUP_WINDOW_MS = 5 * 60_000;
|
|
43
|
+
|
|
44
|
+
/** 缺省返回格式(plan §5.4 的派发契约)。 */
|
|
45
|
+
export const DEFAULT_RETURN_FORMAT = '≤10 行:结论/改动文件/验证/阻塞';
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 台账文件绝对路径。
|
|
49
|
+
* @param {string} projectPath
|
|
50
|
+
* @returns {string}
|
|
51
|
+
*/
|
|
52
|
+
export function dispatchLedgerPath(projectPath) {
|
|
53
|
+
return join(projectPath, '.openfeel', 'dev', 'dispatch.jsonl');
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 取字符串字段(缺省 → '')。
|
|
58
|
+
* @param {unknown} value
|
|
59
|
+
* @returns {string}
|
|
60
|
+
*/
|
|
61
|
+
function asString(value) {
|
|
62
|
+
return typeof value === 'string' ? value : '';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 取字符串数组(过滤非字符串 / 空串)。
|
|
67
|
+
* @param {unknown} value
|
|
68
|
+
* @returns {string[]}
|
|
69
|
+
*/
|
|
70
|
+
function asStringArray(value) {
|
|
71
|
+
if (!Array.isArray(value)) return [];
|
|
72
|
+
return value
|
|
73
|
+
.filter((entry) => typeof entry === 'string')
|
|
74
|
+
.map((entry) => entry.trim())
|
|
75
|
+
.filter((entry) => entry !== '');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 归一化闭环段(v1.6.1 / REQ-066 A66.1)。
|
|
80
|
+
*
|
|
81
|
+
* 输出**恰 4 键**且键序固定:`status` / `artifacts` / `durationMs` / `summary`
|
|
82
|
+
* (`closedAt` 落行的顶层,见文件头注释——A66.1 不允许第 5 键混进 closing)。
|
|
83
|
+
* 非法输入(非对象 / status 不在闭集)→ `undefined`(读侧丢弃;写侧由调用方报错)。
|
|
84
|
+
*
|
|
85
|
+
* @param {unknown} raw
|
|
86
|
+
* @returns {{ status: 'done'|'failed', artifacts: string[], durationMs: number, summary: string } | undefined}
|
|
87
|
+
*/
|
|
88
|
+
function normalizeClosing(raw) {
|
|
89
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) return undefined;
|
|
90
|
+
const status = raw.status === 'done' ? 'done' : raw.status === 'failed' ? 'failed' : '';
|
|
91
|
+
if (status === '') return undefined;
|
|
92
|
+
const durationMs = typeof raw.durationMs === 'number' && Number.isFinite(raw.durationMs) && raw.durationMs >= 0
|
|
93
|
+
? Math.round(raw.durationMs)
|
|
94
|
+
: 0;
|
|
95
|
+
return {
|
|
96
|
+
status,
|
|
97
|
+
artifacts: asStringArray(raw.artifacts),
|
|
98
|
+
durationMs,
|
|
99
|
+
summary: asString(raw.summary),
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 归一化一条台账记录(字段齐全、类型稳定)。
|
|
105
|
+
* @param {Record<string, unknown>} raw
|
|
106
|
+
* @returns {Record<string, any>}
|
|
107
|
+
*/
|
|
108
|
+
function normalizeRecord(raw) {
|
|
109
|
+
const depth = Number.isInteger(raw?.dispatchDepth) && raw.dispatchDepth >= 0 ? raw.dispatchDepth : 1;
|
|
110
|
+
const closing = normalizeClosing(raw?.closing);
|
|
111
|
+
const closedAt = asString(raw?.closedAt);
|
|
112
|
+
return {
|
|
113
|
+
dispatchId: asString(raw?.dispatchId),
|
|
114
|
+
ts: asString(raw?.ts),
|
|
115
|
+
leadSessionId: asString(raw?.leadSessionId),
|
|
116
|
+
origin: asString(raw?.origin) || 'root',
|
|
117
|
+
stage: asString(raw?.stage),
|
|
118
|
+
phase: asString(raw?.phase),
|
|
119
|
+
targetAgent: asString(raw?.targetAgent),
|
|
120
|
+
role: asString(raw?.role),
|
|
121
|
+
taskId: asString(raw?.taskId),
|
|
122
|
+
goal: asString(raw?.goal),
|
|
123
|
+
files: asStringArray(raw?.files),
|
|
124
|
+
returnFormat: asString(raw?.returnFormat) || DEFAULT_RETURN_FORMAT,
|
|
125
|
+
dispatchDepth: depth,
|
|
126
|
+
// 可选闭环字段只增不改:未闭环的行不写这两个键(存量行字节不变)
|
|
127
|
+
...(closing !== undefined ? { closing } : {}),
|
|
128
|
+
...(closedAt !== '' ? { closedAt } : {}),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 读全部记录(文件缺失 / 单行损坏 → 跳过,绝不抛错)。
|
|
134
|
+
* @param {string} projectPath
|
|
135
|
+
* @returns {Array<Record<string, any>>}
|
|
136
|
+
*/
|
|
137
|
+
function readAllDispatches(projectPath) {
|
|
138
|
+
let text;
|
|
139
|
+
try {
|
|
140
|
+
text = readFileSync(dispatchLedgerPath(projectPath), 'utf-8');
|
|
141
|
+
} catch {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
const records = [];
|
|
145
|
+
for (const line of text.split(/\r?\n/)) {
|
|
146
|
+
if (line.trim() === '') continue;
|
|
147
|
+
try {
|
|
148
|
+
const parsed = JSON.parse(line);
|
|
149
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) continue;
|
|
150
|
+
records.push(normalizeRecord(parsed));
|
|
151
|
+
} catch {
|
|
152
|
+
// 损坏行跳过:台账是观测数据,不因一行坏掉丢失全部证据
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return records;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 读台账(按文件顺序,最多返回最近 `limit` 条)。
|
|
160
|
+
* @param {string} projectPath
|
|
161
|
+
* @param {{ limit?: number }} [options]
|
|
162
|
+
* @returns {Array<Record<string, any>>}
|
|
163
|
+
*/
|
|
164
|
+
export function readDispatches(projectPath, options = {}) {
|
|
165
|
+
const all = readAllDispatches(projectPath);
|
|
166
|
+
const limit = Number.isInteger(options.limit) && options.limit > 0 ? options.limit : null;
|
|
167
|
+
return limit === null ? all : all.slice(-limit);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 24h / 200 行清理(纯函数,便于单测)。
|
|
172
|
+
* @param {Array<Record<string, any>>} records
|
|
173
|
+
* @param {number} now 毫秒时间戳
|
|
174
|
+
* @returns {Array<Record<string, any>>}
|
|
175
|
+
*/
|
|
176
|
+
function pruneRecords(records, now) {
|
|
177
|
+
const cutoff = now - DISPATCH_RETENTION_MS;
|
|
178
|
+
const kept = records.filter((record) => {
|
|
179
|
+
const at = Date.parse(record.ts);
|
|
180
|
+
// 时间戳不可解析的行没有审计价值(也无法判断新旧),按陈旧丢弃
|
|
181
|
+
return Number.isFinite(at) && at >= cutoff;
|
|
182
|
+
});
|
|
183
|
+
return kept.length > DISPATCH_MAX_RECORDS ? kept.slice(-DISPATCH_MAX_RECORDS) : kept;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 原子写整份台账(同目录 temp + rename;目录不存在时幂等创建)。
|
|
188
|
+
* @param {string} projectPath
|
|
189
|
+
* @param {Array<Record<string, any>>} records
|
|
190
|
+
* @returns {string} 台账绝对路径
|
|
191
|
+
*/
|
|
192
|
+
function writeDispatches(projectPath, records) {
|
|
193
|
+
const file = dispatchLedgerPath(projectPath);
|
|
194
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
195
|
+
const body = records.map((record) => JSON.stringify(record)).join('\n');
|
|
196
|
+
const tmp = `${file}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
|
|
197
|
+
writeFileSync(tmp, body === '' ? '' : `${body}\n`, 'utf-8');
|
|
198
|
+
renameSync(tmp, file);
|
|
199
|
+
return file;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 推下一条 `dispatchId`(`d-0001` 形式;读现有记录的最大序号 +1)。
|
|
204
|
+
* @param {Array<Record<string, any>>} records
|
|
205
|
+
* @returns {string}
|
|
206
|
+
*/
|
|
207
|
+
function nextDispatchId(records) {
|
|
208
|
+
let max = 0;
|
|
209
|
+
for (const record of records) {
|
|
210
|
+
const match = /^d-(\d+)$/.exec(record.dispatchId);
|
|
211
|
+
if (match !== null) max = Math.max(max, Number(match[1]));
|
|
212
|
+
}
|
|
213
|
+
return `d-${String(max + 1).padStart(4, '0')}`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 追加一条派发记录(顺带 24h / 200 行清理)。
|
|
218
|
+
*
|
|
219
|
+
* 去重:同 `leadSessionId + taskId + goal` 且时间差 ≤ 5 分钟 → 返回既有记录
|
|
220
|
+
* 的 `dispatchId`,**不追加新行**(防模型重试造成假派发证据)。
|
|
221
|
+
*
|
|
222
|
+
* @param {string} projectPath
|
|
223
|
+
* @param {Record<string, unknown>} record
|
|
224
|
+
* @param {{ now?: number }} [options] `now` 注入便于单测(缺省 `Date.now()`)
|
|
225
|
+
* @returns {{ dispatchId: string, path: string, duplicate: boolean }}
|
|
226
|
+
*/
|
|
227
|
+
export function appendDispatch(projectPath, record = {}, options = {}) {
|
|
228
|
+
const now = Number.isFinite(options.now) ? /** @type {number} */ (options.now) : Date.now();
|
|
229
|
+
const incoming = normalizeRecord({
|
|
230
|
+
...record,
|
|
231
|
+
ts: asString(record.ts) || new Date(now).toISOString(),
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const existing = readAllDispatches(projectPath);
|
|
235
|
+
|
|
236
|
+
for (let index = existing.length - 1; index >= 0; index -= 1) {
|
|
237
|
+
const previous = existing[index];
|
|
238
|
+
if (
|
|
239
|
+
previous.leadSessionId === incoming.leadSessionId
|
|
240
|
+
&& previous.taskId === incoming.taskId
|
|
241
|
+
&& previous.goal === incoming.goal
|
|
242
|
+
) {
|
|
243
|
+
const at = Date.parse(previous.ts);
|
|
244
|
+
if (Number.isFinite(at) && Math.abs(now - at) <= DISPATCH_DEDUP_WINDOW_MS) {
|
|
245
|
+
return { dispatchId: previous.dispatchId, path: dispatchLedgerPath(projectPath), duplicate: true };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
incoming.dispatchId = nextDispatchId(existing);
|
|
251
|
+
const kept = pruneRecords([...existing, incoming], now);
|
|
252
|
+
const path = writeDispatches(projectPath, kept);
|
|
253
|
+
return { dispatchId: incoming.dispatchId, path, duplicate: false };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* 读台账的**原始行**(不归一化;用于 `closeDispatch` 的原地补写,保证未命中行字节不变)。
|
|
258
|
+
* @param {string} projectPath
|
|
259
|
+
* @returns {string[]}
|
|
260
|
+
*/
|
|
261
|
+
function readRawLedgerLines(projectPath) {
|
|
262
|
+
let text;
|
|
263
|
+
try {
|
|
264
|
+
text = readFileSync(dispatchLedgerPath(projectPath), 'utf-8');
|
|
265
|
+
} catch {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
return text.split(/\r?\n/).filter((line) => line.trim() !== '');
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 关闭一条派发(v1.6.1 / REQ-066 A66.1):按 `dispatchId` **原地补写** `closing` + 顶层 `closedAt`。
|
|
273
|
+
*
|
|
274
|
+
* 契约:
|
|
275
|
+
* - **行数 / 行序不变**,未命中的行字节不变(只对目标行 JSON.parse → 补键 → stringify);
|
|
276
|
+
* - **幂等**:同 id 重复 finish 且 `status` / `artifacts` / `summary` 相同 → `changed:false`,
|
|
277
|
+
* `ts` / `closing` / `closedAt` 都不二次改写(`durationMs` 未显式给出时沿用既有值,
|
|
278
|
+
* 避免「重复调用 = 耗时变大」);
|
|
279
|
+
* - `durationMs` 缺省 = `now - Date.parse(ts)`(非负整数;ts 不可解析 → 0);
|
|
280
|
+
* - 未知 id → `{ ok:false, code:'not_found' }` 且**零写盘**;status 非法 → `invalid_closing`。
|
|
281
|
+
*
|
|
282
|
+
* @param {string} projectPath
|
|
283
|
+
* @param {string} dispatchId
|
|
284
|
+
* @param {{ status?: 'done'|'failed', artifacts?: string[], durationMs?: number, summary?: string }} closing
|
|
285
|
+
* @param {{ now?: number }} [options] `now` 注入便于单测(缺省 `Date.now()`)
|
|
286
|
+
* @returns {{ ok: true, dispatchId: string, closedAt: string, changed: boolean, path: string }
|
|
287
|
+
* | { ok: false, code: 'not_found'|'invalid_closing', path: string }}
|
|
288
|
+
*/
|
|
289
|
+
export function closeDispatch(projectPath, dispatchId, closing = {}, options = {}) {
|
|
290
|
+
const file = dispatchLedgerPath(projectPath);
|
|
291
|
+
const id = asString(dispatchId).trim();
|
|
292
|
+
if (id === '') return { ok: false, code: 'not_found', path: file };
|
|
293
|
+
|
|
294
|
+
const now = Number.isFinite(options.now) ? /** @type {number} */ (options.now) : Date.now();
|
|
295
|
+
const lines = readRawLedgerLines(projectPath);
|
|
296
|
+
/** @type {Array<Record<string, any> | null>} */
|
|
297
|
+
const parsed = [];
|
|
298
|
+
let targetIndex = -1;
|
|
299
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
300
|
+
let value = null;
|
|
301
|
+
try {
|
|
302
|
+
const candidate = JSON.parse(lines[index]);
|
|
303
|
+
if (candidate !== null && typeof candidate === 'object' && !Array.isArray(candidate)) value = candidate;
|
|
304
|
+
} catch {
|
|
305
|
+
// 损坏行保持原样:不因一行坏掉改写整份台账
|
|
306
|
+
}
|
|
307
|
+
parsed.push(value);
|
|
308
|
+
if (targetIndex === -1 && value !== null && value.dispatchId === id) targetIndex = index;
|
|
309
|
+
}
|
|
310
|
+
if (targetIndex === -1) return { ok: false, code: 'not_found', path: file };
|
|
311
|
+
|
|
312
|
+
const target = /** @type {Record<string, any>} */ (parsed[targetIndex]);
|
|
313
|
+
const existing = normalizeClosing(target.closing);
|
|
314
|
+
const existingClosedAt = asString(target.closedAt);
|
|
315
|
+
const explicitDuration = typeof closing.durationMs === 'number'
|
|
316
|
+
&& Number.isFinite(closing.durationMs)
|
|
317
|
+
&& closing.durationMs >= 0;
|
|
318
|
+
const tsMs = Date.parse(asString(target.ts));
|
|
319
|
+
// 幂等关键:未显式给 durationMs 时优先沿用既有值(否则重复调用会因 now 变化而「改写」)。
|
|
320
|
+
const durationMs = explicitDuration
|
|
321
|
+
? Math.round(/** @type {number} */ (closing.durationMs))
|
|
322
|
+
: existing !== undefined
|
|
323
|
+
? existing.durationMs
|
|
324
|
+
: Number.isFinite(tsMs)
|
|
325
|
+
? Math.max(0, Math.round(now - tsMs))
|
|
326
|
+
: 0;
|
|
327
|
+
const next = normalizeClosing({ ...closing, durationMs });
|
|
328
|
+
if (next === undefined) return { ok: false, code: 'invalid_closing', path: file };
|
|
329
|
+
|
|
330
|
+
const sameClosing = existing !== undefined && JSON.stringify(existing) === JSON.stringify(next);
|
|
331
|
+
if (sameClosing && existingClosedAt !== '') {
|
|
332
|
+
return { ok: true, dispatchId: id, closedAt: existingClosedAt, changed: false, path: file };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const closedAt = sameClosing && existingClosedAt !== '' ? existingClosedAt : new Date(now).toISOString();
|
|
336
|
+
target.closing = next;
|
|
337
|
+
target.closedAt = closedAt;
|
|
338
|
+
|
|
339
|
+
const body = parsed
|
|
340
|
+
.map((value, index) => (value === null ? lines[index] : JSON.stringify(value)))
|
|
341
|
+
.join('\n');
|
|
342
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
343
|
+
const tmp = `${file}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
|
|
344
|
+
writeFileSync(tmp, body === '' ? '' : `${body}\n`, 'utf-8');
|
|
345
|
+
renameSync(tmp, file);
|
|
346
|
+
return { ok: true, dispatchId: id, closedAt, changed: true, path: file };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* 清理超龄 / 超上限记录(无变化时不重写文件)。
|
|
351
|
+
* @param {string} projectPath
|
|
352
|
+
* @param {{ now?: number }} [options]
|
|
353
|
+
* @returns {{ path: string, total: number, kept: number, removed: number }}
|
|
354
|
+
*/
|
|
355
|
+
export function pruneDispatches(projectPath, options = {}) {
|
|
356
|
+
const now = Number.isFinite(options.now) ? /** @type {number} */ (options.now) : Date.now();
|
|
357
|
+
const records = readAllDispatches(projectPath);
|
|
358
|
+
const kept = pruneRecords(records, now);
|
|
359
|
+
if (kept.length !== records.length) writeDispatches(projectPath, kept);
|
|
360
|
+
return {
|
|
361
|
+
path: dispatchLedgerPath(projectPath),
|
|
362
|
+
total: records.length,
|
|
363
|
+
kept: kept.length,
|
|
364
|
+
removed: records.length - kept.length,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** `feel_status.dispatch.records` 暴露的最近条数(足够证明「派发过」,不撑上下文)。 */
|
|
369
|
+
export const DISPATCH_STATUS_RECENT = 5;
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 台账里一条记录的摘要(`feel_status.dispatch.records` 的元素)。
|
|
373
|
+
*
|
|
374
|
+
* v1.6.1 / REQ-066 A66.2:透出 `closing`(无闭环 → null)与顶层 `closedAt`,
|
|
375
|
+
* 让「这条派发到底完成没有」在状态快照里可直接读。
|
|
376
|
+
*
|
|
377
|
+
* @param {Record<string, any>} record 已归一化的台账行
|
|
378
|
+
* @returns {Record<string, any>}
|
|
379
|
+
*/
|
|
380
|
+
function summarizeDispatch(record) {
|
|
381
|
+
return {
|
|
382
|
+
dispatchId: record.dispatchId,
|
|
383
|
+
ts: record.ts,
|
|
384
|
+
stage: record.stage,
|
|
385
|
+
phase: record.phase,
|
|
386
|
+
targetAgent: record.targetAgent,
|
|
387
|
+
role: record.role,
|
|
388
|
+
taskId: record.taskId,
|
|
389
|
+
goal: record.goal,
|
|
390
|
+
closing: record.closing ?? null,
|
|
391
|
+
closedAt: record.closedAt ?? null,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* 派发汇总(`feel_status.dispatch` 的数据源;v1.6.1 / REQ-066 A66.2 增 `open[]` / `closedRatio`)。
|
|
397
|
+
*
|
|
398
|
+
* - `required`:恒 `true`——**提示级**(用户裁定 ②:只做台账 + 强提示,不做硬拦);
|
|
399
|
+
* - `total`:台账总行数;
|
|
400
|
+
* - `sincePhase`:当前 `stage` + `phase` 范围内的派发条数;
|
|
401
|
+
* - `open[]`:当前 `stage` 范围内**未闭环**的派发(无 `closing` 或 `closing.status!=='done'`;
|
|
402
|
+
* 跨相位保留——早期相位没做完的派发不该因相位推进而「消失」);
|
|
403
|
+
* - `closedRatio`:全台账 `closing.status==='done'` 条数 / `total`(`total===0` → `null`,不出现 NaN);
|
|
404
|
+
* - `missingRoles`:当前 `stage` + `phase` 范围内**没有 `closing.status==='done'` 记录**的角色
|
|
405
|
+
* (五个闭集角色,固定顺序;v1.6.0 的「有 record 即算」口径已作废——A66.2);
|
|
406
|
+
* - `records`:最近 `DISPATCH_STATUS_RECENT` 条派发摘要(含 `closing` / `closedAt`)。
|
|
407
|
+
*
|
|
408
|
+
* @param {string} projectPath
|
|
409
|
+
* @param {{ stage?: string, phase?: string }} [options]
|
|
410
|
+
* @returns {{ required: true, total: number, sincePhase: number, open: Array<Record<string, any>>, closedRatio: number | null, missingRoles: string[], records: Array<Record<string, any>> }}
|
|
411
|
+
*/
|
|
412
|
+
export function dispatchSummary(projectPath, options = {}) {
|
|
413
|
+
const records = readAllDispatches(projectPath);
|
|
414
|
+
const stage = typeof options.stage === 'string' ? options.stage : '';
|
|
415
|
+
const phase = typeof options.phase === 'string' ? options.phase : '';
|
|
416
|
+
|
|
417
|
+
const stageScoped = stage === '' ? records : records.filter((record) => record.stage === stage);
|
|
418
|
+
const phaseScoped = phase === '' ? stageScoped : stageScoped.filter((record) => record.phase === phase);
|
|
419
|
+
const isClosedDone = (record) => record.closing !== undefined && record.closing !== null && record.closing.status === 'done';
|
|
420
|
+
const doneRoles = new Set(phaseScoped.filter(isClosedDone).map((record) => record.role));
|
|
421
|
+
const doneCount = records.filter(isClosedDone).length;
|
|
422
|
+
|
|
423
|
+
return {
|
|
424
|
+
required: true,
|
|
425
|
+
total: records.length,
|
|
426
|
+
sincePhase: phaseScoped.length,
|
|
427
|
+
open: stageScoped.filter((record) => !isClosedDone(record)).map(summarizeDispatch),
|
|
428
|
+
closedRatio: records.length === 0 ? null : doneCount / records.length,
|
|
429
|
+
missingRoles: DISPATCH_ROLES.filter((role) => !doneRoles.has(role)),
|
|
430
|
+
records: records.slice(-DISPATCH_STATUS_RECENT).map(summarizeDispatch),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
@@ -1,9 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 派发 / 返回契约(v1.
|
|
2
|
+
* 派发 / 返回契约(v1.6.0 / REQ-057)
|
|
3
3
|
*
|
|
4
4
|
* Feel 派发给 subagent 的能力/边界/返回格式模板。
|
|
5
5
|
* 目的:主会话不把子任务细节留在自己的上下文里;子 agent 不重复探索、不读写状态文件。
|
|
6
|
+
*
|
|
7
|
+
* v1.6.0 / T9 step2:`goal` 与 `boundaries.write` 改为**用真实快照填充**
|
|
8
|
+
* (stage / phase / phaseLabel / responsibleAgent / 关联需求),不再输出
|
|
9
|
+
* 泛型占位句——占位文案会被模型原样粘进 subagent prompt,等于没派发。
|
|
10
|
+
* `returnFormat` 是固定契约(≤10 行),不由快照派生。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 取当前阶段关联需求(**只用快照已有字段**,零新增 I/O)。
|
|
15
|
+
*
|
|
16
|
+
* 优先级:
|
|
17
|
+
* 1. `snapshot.recovery.requirement`(CLI 交接标记,`flow` 侧的权威值);
|
|
18
|
+
* 2. 回退 `snapshot.stages[*].requirementId`(`flow status --json` 的阶段条目)。
|
|
19
|
+
*
|
|
20
|
+
* @param {Record<string, any>} snapshot readSnapshot 结果
|
|
21
|
+
* @returns {string} 需求 ID;取不到 → 空串
|
|
6
22
|
*/
|
|
23
|
+
function requirementOf(snapshot) {
|
|
24
|
+
const recovery = snapshot?.recovery?.requirement;
|
|
25
|
+
if (typeof recovery === 'string' && recovery !== '') return recovery;
|
|
26
|
+
const stageId = snapshot?.focus?.stage;
|
|
27
|
+
const stages = Array.isArray(snapshot?.stages) ? snapshot.stages : [];
|
|
28
|
+
const stage = stages.find((entry) => entry?.id === stageId || entry?.name === stageId);
|
|
29
|
+
return typeof stage?.requirementId === 'string' ? stage.requirementId : '';
|
|
30
|
+
}
|
|
7
31
|
|
|
8
32
|
/**
|
|
9
33
|
* @param {Record<string, any>} snapshot readSnapshot 结果
|
|
@@ -11,16 +35,40 @@
|
|
|
11
35
|
*/
|
|
12
36
|
export function buildDispatchTemplate(snapshot) {
|
|
13
37
|
const focus = snapshot?.focus ?? {};
|
|
14
|
-
const
|
|
15
|
-
const
|
|
38
|
+
const hasFocus = typeof focus.stage === 'string' && focus.stage !== '';
|
|
39
|
+
const stage = hasFocus ? focus.stage : '(无活跃阶段)';
|
|
40
|
+
const phase = typeof focus.phase === 'string' && focus.phase !== '' ? focus.phase : (hasFocus ? '(无相位)' : '(无)');
|
|
41
|
+
const phaseLabel = typeof focus.phaseLabel === 'string' && focus.phaseLabel !== '' ? focus.phaseLabel : phase;
|
|
42
|
+
const agent = typeof focus.responsibleAgent === 'string' && focus.responsibleAgent !== ''
|
|
43
|
+
? focus.responsibleAgent
|
|
44
|
+
: '(未知角色)';
|
|
45
|
+
const requirement = requirementOf(snapshot);
|
|
46
|
+
const suggested = typeof focus.suggestedCommand === 'string' ? focus.suggestedCommand : '';
|
|
47
|
+
|
|
48
|
+
// goal 用真实调度上下文填充:做什么(阶段/相位)、给谁(Agent)、哪个需求、怎么算完成。
|
|
49
|
+
// 无活跃阶段时不留占位句,而是把「现在没有可派发对象」说清楚(避免被原样粘进 prompt)。
|
|
50
|
+
const goal = !hasFocus
|
|
51
|
+
? '当前无活跃阶段:先用 feel_status 选定阶段/需求,再据此填写一句话目标(须可验收:命令 + 退出码)'
|
|
52
|
+
: (requirement === ''
|
|
53
|
+
? `在 ${stage} 的 ${phase}(${phaseLabel})由 ${agent} 交付本相位产物;完成判据须可验收(给出命令与退出码)`
|
|
54
|
+
: `在 ${stage} 的 ${phase}(${phaseLabel})由 ${agent} 完成需求 ${requirement} 的本相位产物;完成判据须可验收(给出命令与退出码)`);
|
|
55
|
+
|
|
16
56
|
return {
|
|
17
|
-
goal
|
|
57
|
+
goal,
|
|
18
58
|
context: [
|
|
19
|
-
`阶段:${stage} / ${phase}`,
|
|
59
|
+
`阶段:${stage} / ${phase}(${phaseLabel});负责 Agent:${agent}`,
|
|
60
|
+
requirement === '' ? '关联需求:无(按阶段产物验收)' : `关联需求:${requirement}`,
|
|
61
|
+
...(suggested === '' ? [] : [`CLI 权威下一步:${suggested}`]),
|
|
20
62
|
'只附必要文件路径(plan / op / REV / 目标源码),不附整目录或整仓内容',
|
|
21
63
|
],
|
|
22
64
|
boundaries: {
|
|
23
|
-
|
|
65
|
+
// 写面按当前阶段派生:本任务文件由派发方在 goal 里点明,未点明的一律只读。
|
|
66
|
+
write: hasFocus
|
|
67
|
+
? [
|
|
68
|
+
`${stage} / ${phase} 下本任务的文件(派发时点明具体路径;未点明的一律只读)`,
|
|
69
|
+
'.openfeel/plan/**(仅计划/方案文档;需求正文走 feel_req action=body)',
|
|
70
|
+
]
|
|
71
|
+
: ['(无活跃阶段:无可写面;先开阶段或接取需求)'],
|
|
24
72
|
neverRead: ['dev_last*', 'dev/current.md', 'status.md', 'flow.json'],
|
|
25
73
|
neverWrite: ['dev_last*', 'dev/current.md', 'status.md', 'flow.json'],
|
|
26
74
|
escalate: '越界或需要用户决策时立即停止并回报,不要自行扩大范围',
|