Skip to content

Commit a27c4ef

Browse files
committed
fix(pipeline): 匿名指纹补 step.at 等结构化字段,漏掉会合并两条不同 run 的违规
#507 合并后 CodeRabbit 复盘时发现首版判据仍有一处漏:匿名指纹只取了 step 的 id,没取 at。两条都缺 run.at、其余全同、只在 steps[0].at 上不同的 run 会共键, 后一条 violation-precedence 被去重吞掉——正是 #461 要修的那类「键塌缩」的 反面形态(该分开的被并了)。 同时**不能**把整个 step 对象序列化进去:note 是自由文本,进去会让「只差备注的 同一条观测」被当成两次。归一化 step(src/pipeline-runs.js:242 normalizeSteps) 的字段是 { id, at, skipped, detoured, refuted, note },收前五个、不收 note。 三个结局布尔也收,尽管它们对去重正确性是**惰性**的(violation 判定只读 doneIds 与 id 是否存在,refuted 根本不被读)——全收是为了让规则统一(指纹 = 归一化 step 的每个结构化字段,唯一例外是自由文本),且将来 violation 逻辑若 开始读某个旗标时不会突然开始漏合并。代价如实记在注释与测试里:两条只差惰性 旗标的 at-less run 会各报一条逐字相同的提议。 编码用 JSON.stringify 而不是手写 `${id}@${at}` 拼接:run 侧 step id 只过 isSafeSegment(src/pipeline-runs.js:83),`@` `,` `:` 都是合法 id,手写拼接会 把 [{A,t1},{B,t1},{C,t1}] 与 [{"A@t1,B",t1},{C,t1}] 撞成同一个键——那会新造 一类键塌缩。JSON 自带无歧义分隔。 生产不可达性没有变:readStoredRun(src/pipeline-runs.js:262)强制 run.at 存在, 正常账本永远走 `at:` 分支。这里纯属 suggestRetirements 直接调用边界的防御深度, #461 自己也是这样定位的(现网风险:低)。 测试:新增四条,均已验红→green(把指纹退回只取 step.id,两条转红): - 只差 steps[0].at 的两条 at-less run 各报一条 - 只差 steps[0].note 的两条 at-less run 仍是同一条(前瞻守卫,今天修复前后 都是 1,但把 note 塞进指纹就会转红) - 只差 refuted 的两条 at-less run 给出两条逐字相同的提议(锁旗标语义与代价) - step id 含 @/, 不撞键(锁 JSON.stringify 编码,退回手写拼接会漏报) 本地全量 2311 passed / 0 failed,lib 与 src 一致。 Refs #507
1 parent bac97a9 commit a27c4ef

3 files changed

Lines changed: 156 additions & 4 deletions

File tree

‎dsh-mneme/lib/pipeline.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export function suggestRetirements(pipeline, { runs = [], now = Date.now(), obse
122122
//
123123
// at 存在(合法账本的唯一形态——normalizeRun 在写侧强制必填)走 `at:<值>`。
124124
// at 缺失(读侧被独立喂进非法形状,或未来写契约放宽)走 `anon:<内容指纹>`:
125-
// by + triggeredBy + 按序的 step id。用**内容**而不是数组索引,是因为
125+
// by + triggeredBy + 按序的结构化 step 字段(字段取舍见下方 anonymousRunKey)。
126+
// 用**内容**而不是数组索引,是因为
126127
// violationSeen 跨整本账本共享,而索引只在「本次遍历的数组顺序」里唯一——
127128
// 同一条 run 在账本里出现两次时,索引会让它的同一条违规被记成两条,
128129
// 那正是本条要修的那类「键塌缩」的反面形态(#507 评审)。
@@ -136,7 +137,7 @@ export function suggestRetirements(pipeline, { runs = [], now = Date.now(), obse
136137
// #0」会生成同一个键,后一条 violation-precedence 被静默吞掉。
137138
const runKey = isNonEmptyString(run.at)
138139
? `at:${run.at}`
139-
: `anon:${run.by ?? ""}|${run.triggeredBy ?? ""}|${(run.steps || []).map((s) => s?.id ?? "").join(",")}`;
140+
: anonymousRunKey(run);
140141
for (const step of pipeline.steps) {
141142
if (!doneIds.has(step.id) || !Array.isArray(step.needs)) continue;
142143
for (const need of step.needs) {
@@ -260,6 +261,47 @@ function isNonEmptyString(value) {
260261
return typeof value === "string" && value.trim() !== "";
261262
}
262263

264+
/**
265+
* at 缺失时一条 run 的判别键(#461 / CodeRabbit #507):给无时间戳的 run 一个
266+
* **按内容**的稳定身份,好让 violationSeen 只并掉「同一条 run 重复出现」的同类
267+
* 违规,而不把内容不同的两条 run 并成一条。
268+
*
269+
* 覆盖 normalizeSteps(src/pipeline-runs.js:242)产出的**全部结构化字段**——
270+
* id / at / skipped / detoured / refuted。之所以必须收 step.at:漏了它,两条只在
271+
* 某一步时间上不同的 at-less run 会共键,后一条的 violation-precedence 被去重吞掉
272+
* (#507 指出的正是这条)。
273+
*
274+
* 用 JSON.stringify 而不是手写 `${id}@${at}` 拼接:run 侧 step id 只过
275+
* isSafeSegment(src/pipeline-runs.js:83),`@` `,` `:` 都是合法 id,手写拼接会与
276+
* 「id 里恰好含这些字符」的另一条 step 撞串——那会新造一类键塌缩(把另一类 at-less
277+
* 输入从「不漏报」改成「漏报」)。JSON 自带无歧义分隔,顺带把 by/triggeredBy 里的
278+
* `|`、以及 Date 与 ISO 串(Date.toJSON 归一成 ISO)的歧义一并收掉。
279+
*
280+
* note 不进来:它是自由文本,序列化进去会让「只差备注的同一条观测」被当成两次。
281+
*
282+
* 如实记下代价:skipped/detoured/refuted 对去重正确性是**惰性**的——violation 判定
283+
* 只读 doneIds(由 id + skipped/detoured 决定)与 id 是否存在,refuted 根本不被读
284+
* (见下方 suggestRetirements)。所以收这三个布尔防不住任何错误合并,却会让两条只差
285+
* 这些旗标的 at-less run 各报一条**逐字相同**的提议。仍选择全收,是为了让规则统一
286+
* (指纹 = 归一化 step 的每个结构化字段,唯一例外是自由文本 note),且将来 violation
287+
* 逻辑若开始读某个旗标时不会突然开始漏合并;anon 路径本身没有身份锚点,接受这一条
288+
* 重复的代价。注意这条路径在生产不可达:readStoredRun(src/pipeline-runs.js:262)
289+
* 强制 run.at 存在,正常账本永远走 `at:` 分支,此处纯属直接调用边界的防御深度。
290+
*/
291+
function anonymousRunKey(run) {
292+
return `anon:${JSON.stringify({
293+
by: run.by ?? null,
294+
triggeredBy: run.triggeredBy ?? null,
295+
steps: (run.steps || []).map((s) => ({
296+
id: s?.id ?? null,
297+
at: s?.at ?? null,
298+
skipped: !!s?.skipped,
299+
detoured: !!s?.detoured,
300+
refuted: !!s?.refuted
301+
}))
302+
})}`;
303+
}
304+
263305
/**
264306
* 校验一条工作流。返回归一化后的对象;有任何问题就抛,不返回「部分可用」的东西
265307
* ——半个工作流比没有工作流更危险,因为它会被当成完整的跑。

‎dsh-mneme/src/pipeline.js‎

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ export function suggestRetirements(pipeline, { runs = [], now = Date.now(), obse
122122
//
123123
// at 存在(合法账本的唯一形态——normalizeRun 在写侧强制必填)走 `at:<值>`。
124124
// at 缺失(读侧被独立喂进非法形状,或未来写契约放宽)走 `anon:<内容指纹>`:
125-
// by + triggeredBy + 按序的 step id。用**内容**而不是数组索引,是因为
125+
// by + triggeredBy + 按序的结构化 step 字段(字段取舍见下方 anonymousRunKey)。
126+
// 用**内容**而不是数组索引,是因为
126127
// violationSeen 跨整本账本共享,而索引只在「本次遍历的数组顺序」里唯一——
127128
// 同一条 run 在账本里出现两次时,索引会让它的同一条违规被记成两条,
128129
// 那正是本条要修的那类「键塌缩」的反面形态(#507 评审)。
@@ -136,7 +137,7 @@ export function suggestRetirements(pipeline, { runs = [], now = Date.now(), obse
136137
// #0」会生成同一个键,后一条 violation-precedence 被静默吞掉。
137138
const runKey = isNonEmptyString(run.at)
138139
? `at:${run.at}`
139-
: `anon:${run.by ?? ""}|${run.triggeredBy ?? ""}|${(run.steps || []).map((s) => s?.id ?? "").join(",")}`;
140+
: anonymousRunKey(run);
140141
for (const step of pipeline.steps) {
141142
if (!doneIds.has(step.id) || !Array.isArray(step.needs)) continue;
142143
for (const need of step.needs) {
@@ -260,6 +261,47 @@ function isNonEmptyString(value) {
260261
return typeof value === "string" && value.trim() !== "";
261262
}
262263

264+
/**
265+
* at 缺失时一条 run 的判别键(#461 / CodeRabbit #507):给无时间戳的 run 一个
266+
* **按内容**的稳定身份,好让 violationSeen 只并掉「同一条 run 重复出现」的同类
267+
* 违规,而不把内容不同的两条 run 并成一条。
268+
*
269+
* 覆盖 normalizeSteps(src/pipeline-runs.js:242)产出的**全部结构化字段**——
270+
* id / at / skipped / detoured / refuted。之所以必须收 step.at:漏了它,两条只在
271+
* 某一步时间上不同的 at-less run 会共键,后一条的 violation-precedence 被去重吞掉
272+
* (#507 指出的正是这条)。
273+
*
274+
* 用 JSON.stringify 而不是手写 `${id}@${at}` 拼接:run 侧 step id 只过
275+
* isSafeSegment(src/pipeline-runs.js:83),`@` `,` `:` 都是合法 id,手写拼接会与
276+
* 「id 里恰好含这些字符」的另一条 step 撞串——那会新造一类键塌缩(把另一类 at-less
277+
* 输入从「不漏报」改成「漏报」)。JSON 自带无歧义分隔,顺带把 by/triggeredBy 里的
278+
* `|`、以及 Date 与 ISO 串(Date.toJSON 归一成 ISO)的歧义一并收掉。
279+
*
280+
* note 不进来:它是自由文本,序列化进去会让「只差备注的同一条观测」被当成两次。
281+
*
282+
* 如实记下代价:skipped/detoured/refuted 对去重正确性是**惰性**的——violation 判定
283+
* 只读 doneIds(由 id + skipped/detoured 决定)与 id 是否存在,refuted 根本不被读
284+
* (见下方 suggestRetirements)。所以收这三个布尔防不住任何错误合并,却会让两条只差
285+
* 这些旗标的 at-less run 各报一条**逐字相同**的提议。仍选择全收,是为了让规则统一
286+
* (指纹 = 归一化 step 的每个结构化字段,唯一例外是自由文本 note),且将来 violation
287+
* 逻辑若开始读某个旗标时不会突然开始漏合并;anon 路径本身没有身份锚点,接受这一条
288+
* 重复的代价。注意这条路径在生产不可达:readStoredRun(src/pipeline-runs.js:262)
289+
* 强制 run.at 存在,正常账本永远走 `at:` 分支,此处纯属直接调用边界的防御深度。
290+
*/
291+
function anonymousRunKey(run) {
292+
return `anon:${JSON.stringify({
293+
by: run.by ?? null,
294+
triggeredBy: run.triggeredBy ?? null,
295+
steps: (run.steps || []).map((s) => ({
296+
id: s?.id ?? null,
297+
at: s?.at ?? null,
298+
skipped: !!s?.skipped,
299+
detoured: !!s?.detoured,
300+
refuted: !!s?.refuted
301+
}))
302+
})}`;
303+
}
304+
263305
/**
264306
* 校验一条工作流。返回归一化后的对象;有任何问题就抛,不返回「部分可用」的东西
265307
* ——半个工作流比没有工作流更危险,因为它会被当成完整的跑。

‎dsh-mneme/test/pipeline-r1r2.test.js‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,71 @@ test("#461:at 存在时同轮重复违规仍只报一条(回归保护)", (
208208
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
209209
assert.equal(v.length, 1, "同一 at 的同一违规仍然只报一次");
210210
});
211+
212+
// #507(CodeRabbit):匿名指纹必须收 step 的 at。两条都缺 run.at、只在 steps[0].at 上
213+
// 不同的 run 是两次不同的观测;漏了 at 会让它们共键,后一条的 violation-precedence 被
214+
// 去重吞掉。修复前(只取 step.id)报 1 条,修复后(收 at)报 2 条——这是本条的 red→green。
215+
test("#507:匿名指纹收 step.at——只差 steps[0].at 的两条 at-less run 各报一条", () => {
216+
const p = makePipeline([
217+
step("A", { tool: "npm test" }),
218+
step("B", { tool: "git push", needs: ["A"] })
219+
]);
220+
const runs = [
221+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "2026-10-17T00:00:00Z" }] },
222+
{ at: undefined, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "2026-10-18T00:00:00Z" }] }
223+
];
224+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
225+
assert.equal(v.length, 2, "只差步骤时间的两条 at-less run 是两次观测,不能被并成一条");
226+
});
227+
228+
// #507:匿名指纹**不能**把整个 step 对象序列化进去——note 是自由文本,进去会让「只差
229+
// 备注的同一条观测」被当成两次。今天没有任何测试锁这条;不加,未来重构把 note 加回来
230+
// 还会全绿。这是前瞻守卫:当前修复前后都是 1,但把 note 塞进指纹就会转红(见复盘)。
231+
test("#507:匿名指纹排除 note——只差 steps[0].note 的两条 at-less run 仍是同一条", () => {
232+
const p = makePipeline([
233+
step("A", { tool: "npm test" }),
234+
step("B", { tool: "git push", needs: ["A"] })
235+
]);
236+
const runs = [
237+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", note: "第一次跑,网络慢" }] },
238+
{ at: undefined, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", note: "重跑,还是慢" }] }
239+
];
240+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
241+
assert.equal(v.length, 1, "note 是自由文本不是身份,只差备注应仍算同一条观测");
242+
});
243+
244+
// #507:锁定旗标语义。指纹覆盖归一化 step 的全部结构化字段(含 skipped/detoured/refuted),
245+
// 而这三个布尔对去重正确性是**惰性**的(violation 判定只读 doneIds 与 id 是否存在,
246+
// refuted 根本不被读)。代价如实记在这里:两条只差惰性旗标的 at-less run 会各报一条
247+
// **逐字相同**的提议。锁成 2 + deepEqual,防止将来无声漂移;anon 路径在生产不可达
248+
// (readStoredRun 强制 run.at),故此代价不落到真实账本上。
249+
test("#507:只差 refuted 的两条 at-less run 给出两条逐字相同的提议(锁旗标语义)", () => {
250+
const p = makePipeline([
251+
step("A", { tool: "npm test" }),
252+
step("B", { tool: "git push", needs: ["A"] })
253+
]);
254+
const runs = [
255+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", refuted: false }] },
256+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "B", at: "x", refuted: true }] }
257+
];
258+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
259+
assert.equal(v.length, 2, "refuted 在指纹里:两条只差 refuted 的 run 拿到两个键");
260+
assert.deepEqual(v[0], v[1], "两条提议逐字相同——这正是把惰性旗标收进指纹的代价");
261+
});
262+
263+
// #507:锁编码必须是**无歧义**序列化(JSON.stringify),不能退回手写 `${id}@${at}`。
264+
// run 侧 step id 只过 isSafeSegment(src/pipeline-runs.js:83),`@` `,` `:` 都是合法 id:
265+
// 手写拼接会把 [{A,t1},{B,t1},{C,t1}] 与 [{"A@t1,B",t1},{C,t1}] 撞成同一个键,run2 的
266+
// (C,P) 违规被静默吞掉(只报 3 条)。当前实现与更早的 id-only 实现都报 4;退回手写拼接会红。
267+
test("#507:step id 含 @/, 不撞键(锁 JSON.stringify 编码,手写拼接会漏报)", () => {
268+
const p = makePipeline([
269+
step("P"),
270+
step("A", { needs: ["P"] }), step("B", { needs: ["P"] }), step("C", { needs: ["P"] })
271+
]);
272+
const runs = [
273+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "A", at: "t1" }, { id: "B", at: "t1" }, { id: "C", at: "t1" }] },
274+
{ at: null, by: "human", triggeredBy: "demo", steps: [{ id: "A@t1,B", at: "t1" }, { id: "C", at: "t1" }] }
275+
];
276+
const v = suggestRetirements(p, { runs }).filter((s) => s.kind === "violation-precedence");
277+
assert.equal(v.length, 4, "两条 run 内容不同:run1 断 A/B/C 三条边、run2 断 C 一条,共 4 条");
278+
});

0 commit comments

Comments
 (0)