Skip to content

Commit 16f1f97

Browse files
committed
fix(dream): 恢复 v0.6.9 的 skipInvalid 宽容校验路径(issue #89 回归)+ autoDream 最小触发间隔
issue #89(弱模型整单拒绝)三点全部核实: - v0.6.9(issue #26)实现的 dreamSkipInvalid(单条非法决策跳过 + 合法子集 应用 + run 记 degraded)与 allowCrossTypeMerge 开关在 v0.7.11 重写中丢失; - README Fail-safe 一节至今承诺宽容行为,文档与代码自此错讹; - qwen3.8-flash 等弱模型决策合规抖动下 4/4 全败,整单拒绝白烧 LLM 调用。 - decisions.js:原样移植 skipInvalid 双轨结构——单条错误进 local, skipInvalid 时记入 skipped 并从 decisions 原地剔除、不 claim 任何 id; 严格模式(不传开关)行为不变,sleep 路径维持严格。allowCrossTypeMerge 显式放宽跨类型合并;全局上限/覆盖率下限仍整单拒绝(刷爆上限是模型坏了, 不是轻微 schema 漂移)。 - dream.js:透传 config 开关;skipped 非空时 run 状态如实记为 degraded (ok:true 保持基线刷新语义),跳过明细进 warn 日志。 - config.js/settings.js:dreamSkipInvalid(默认 true)、allowCrossTypeMerge (默认 false)、dreamMinIntervalMinutes(0-10080,默认 0=不限)入 schema 与 feature flags 白名单(34 键)。 - 调度器:minIntervalMs 最小触发间隔——失败/degraded run 也占用间隔(节流 的目的正是防失败调用连发),间隔内触发静默跳过(issue #89 请求 2)。 - 测试 +8(v0.6.9 五用例移植 + degraded/严格 e2e + 节流 + 白名单计数), 696 全绿。
1 parent efd192a commit 16f1f97

12 files changed

Lines changed: 472 additions & 166 deletions

File tree

‎dsh-mneme/lib/config.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export const Config = z.object({
4747
dreamThresholdCount: z.natural().min(1).max(1000).default(10),
4848
dreamThresholdChars: z.natural().min(100).max(100000).default(5000),
4949
dreamDelayMs: z.natural().min(0).max(60000).default(2000),
50+
// autoDream 触发最小间隔(分钟,0 = 不限制,Issue #89 请求 2):高频写入
51+
// 场景下防止巩固调用(含失败重试)连发刷爆配额。间隔从每次实际开跑时刻
52+
// 起算,失败/degraded 的 run 也占用间隔;间隔内的触发请求静默跳过,下一次
53+
// 写入事件会重新评估。
54+
dreamMinIntervalMinutes: z.natural().min(0).max(10080).default(0),
5055
dreamProvider: z.string(),
5156
dreamModel: z.string(),
5257
dreamMaxTokens: z.natural().min(256).max(131072).default(8192),
@@ -74,6 +79,13 @@ export const Config = z.object({
7479
// → 整单拒绝,防止残缺输出被隐式 keep 洗白成 ok 后再被真实 apply。0-1,
7580
// 默认 0.5(至少显式覆盖一半 snapshot)。
7681
dreamMinExplicitCoverage: z.number().min(0).max(1).default(0.5),
82+
// Issue #89:v0.6.9(Issue #26)的宽容路径回归。默认跳过单条非法决策
83+
// (未知 id / 跨类型合并等)、应用合法子集、run 记 degraded;设 false 恢复
84+
// 整单拒绝的严格模式。全局上限与覆盖率下限不受此开关影响、始终整单拒绝。
85+
dreamSkipInvalid: z.boolean().default(true),
86+
// 显式开启后放宽跨类型合并检查(类型边界由用户自行承担);配合
87+
// dreamSkipInvalid 理解:关闭 skipInvalid 时跨类型 merge 直接整单拒绝。
88+
allowCrossTypeMerge: z.boolean().default(false),
7789
// Rule version for dream adjudication: when this bumps, older dream_runs
7890
// degrade to historical evidence (their receipts no longer drive live
7991
// decisions). Default 0 = no versioning in use yet.

‎dsh-mneme/lib/dream.js‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,15 @@ async function maintainIndexAfterDream(decisions, service, semantic) {
468468
if (embedder.modelHash) vectorIndex.markModel?.(embedder.modelHash, embedder.dimension);
469469
}
470470

471-
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, logger, semantic = null }) {
471+
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, logger, semantic = null }) {
472472
let pendingTimer = null;
473473
let running = false;
474474
let disposed = false;
475475
let baseline = { count: 0, chars: 0 };
476476
let inFlight = null;
477+
// Issue #89(请求 2):上一次实际开跑时刻。失败/degraded 的 run 也占用
478+
// 最小间隔——节流的目的正是防止失败调用连发;间隔内的触发静默跳过。
479+
let lastRunAt = 0;
477480

478481
function shouldTrigger(service) {
479482
const memories = service.all().filter((m) => !m.archived && m.type !== "summary");
@@ -486,11 +489,14 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
486489

487490
function maybeSchedule(service) {
488491
if (disposed || running || pendingTimer) return false;
492+
// Issue #89(请求 2):最小触发间隔闸门。
493+
if (minIntervalMs > 0 && Date.now() - lastRunAt < minIntervalMs) return false;
489494
const { trigger, count, chars } = shouldTrigger(service);
490495
if (!trigger) return false;
491496
pendingTimer = setTimeout(() => {
492497
pendingTimer = null;
493498
running = true;
499+
lastRunAt = Date.now();
494500
// Defer the onRun invocation so a synchronous throw cannot escape the
495501
// timer callback (which would crash the process) and skip the teardown.
496502
// Errors are logged, never swallowed silently. inFlight lets dispose()
@@ -704,18 +710,26 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
704710
logger?.warn?.(`dsh-mneme dream: no json array in llm output (raw length ${decisionText?.length ?? 0}; head: ${head})`);
705711
return finish({ ok: false, error: "no json array in llm output", summary: false });
706712
}
707-
const { ok, errors } = validateDecisions(decisions, snapshot, {
713+
const { ok, errors, skipped } = validateDecisions(decisions, snapshot, {
708714
maxUpdatePerRun: config.reflectionUpdateMaxPerRun,
709715
minAgeHours: config.reflectionUpdateMinAgeHours,
710716
// v0.4.4 fix:显式透传,用户配 dreamImplicitKeep:false 时严格模式必须
711717
// 真正生效,dreamMinExplicitCoverage 决定隐式 keep 下的覆盖率下限。
712718
dreamImplicitKeep: config.dreamImplicitKeep,
713-
dreamMinExplicitCoverage: config.dreamMinExplicitCoverage
719+
dreamMinExplicitCoverage: config.dreamMinExplicitCoverage,
720+
// Issue #89:v0.6.9(Issue #26)的宽容路径在 v0.7.11 重写中丢失——单条
721+
// 非法决策重新只跳过该条、合法子集照常应用(run 记为 degraded)。
722+
skipInvalid: config.dreamSkipInvalid !== false,
723+
allowCrossTypeMerge: config.allowCrossTypeMerge === true
714724
});
715725
if (!ok) {
716726
logger?.warn?.(`dsh-mneme dream: invalid decisions: ${errors.join("; ")}`);
717727
return finish({ ok: false, error: `invalid decisions: ${errors.length} errors`, summary: false });
718728
}
729+
const skippedInvalid = skipped.length > 0;
730+
if (skippedInvalid) {
731+
logger?.warn?.(`dsh-mneme dream: ${skipped.length} invalid decision(s) skipped (run degrades): ${skipped.map((s) => s.error).join("; ")}`);
732+
}
719733

720734
// Capture pre-update snapshots so the audit records what each update changed.
721735
const updateSnapshots = {};
@@ -872,9 +886,11 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
872886
// run. ok:false keeps the scheduler from moving the baseline.
873887
// ok — either real changes landed, or a fresh summary was stored
874888
// (all-keep + summary is a substantive summary refresh).
875-
// degraded — real consolidation landed but the summary came back empty/
876-
// missing: the store was absorbed (ok for the baseline) but
877-
// the run did not produce its full output (marked, not faked).
889+
// degraded — real consolidation landed but the run did not produce its
890+
// full output: the summary came back empty/missing, or
891+
// skipInvalid dropped individually-invalid decisions
892+
// (Issue #89 — marked, not faked). The valid subset was
893+
// absorbed (ok for the baseline).
878894
let status;
879895
let okResult;
880896
if (partial) {
@@ -884,7 +900,7 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
884900
status = summaryStored ? "ok" : "noop";
885901
okResult = summaryStored;
886902
} else {
887-
status = summaryStored ? "ok" : "degraded";
903+
status = summaryStored && !skippedInvalid ? "ok" : "degraded";
888904
okResult = true;
889905
}
890906
return finish({

‎dsh-mneme/lib/dream/decisions.js‎

Lines changed: 105 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ const EPISTEMIC_PRIORITY = { observation: 3, inferred: 2, subjective: 1 };
77

88
/**
99
* Validate a dream decision list against a snapshot of eligible memories.
10-
* @param decisions - LLM-produced decision list.
10+
* @param decisions - LLM-produced decision list. In skipInvalid mode, invalid
11+
* entries are spliced out of this array in place (the caller reuses the same
12+
* reference downstream for apply/audit); implicit keeps are appended here too.
1113
* @param snapshot - Map<id, memory> of eligible (non-archived, non-summary) entries.
12-
* @returns {{ok: boolean, errors: string[]}}
14+
* @returns {{ok: boolean, errors: string[], skipped?: Array<{index, action, ids, error}>}}
1315
*/
1416
export function validateDecisions(decisions, snapshot, options = {}) {
1517
const errors = [];
18+
// Issue #89 回归修复(v0.6.9 / Issue #26 的 skipInvalid 路径原样移植,该路径
19+
// 在 v0.7.11 重写中丢失):skipInvalid 开启时,单条非法决策只跳过该条(记录
20+
// 到 skipped、不 claim 任何 id),合法子集照常应用,调用方据此把 run 记为
21+
// degraded。全局信号(update/create 上限、显式覆盖率下限)依旧整单拒绝——
22+
// 刷爆上限或覆盖率不达标的输出是模型坏了,不是轻微 schema 漂移。
23+
const skipped = [];
24+
const survivors = [];
25+
const skipInvalid = options.skipInvalid === true;
1626
const maxUpdatePerRun = options.maxUpdatePerRun ?? 2;
1727
const minAgeHours = options.minAgeHours ?? 24;
1828
if (!Array.isArray(decisions) || decisions.length === 0) {
@@ -21,106 +31,122 @@ export function validateDecisions(decisions, snapshot, options = {}) {
2131
const claimed = new Set();
2232
for (const [index, d] of decisions.entries()) {
2333
const at = `decision[${index}]`;
34+
// 单条校验错误先进 local:skipInvalid 模式下整条跳过,严格模式下才并入
35+
// 全局 errors(沿用 v0.6.9 的双轨结构)。
36+
const local = [];
37+
const ids = d && d.action === "conflict" ? [d.winner, d.loser] : (d?.ids ?? []);
2438
if (!d || typeof d !== "object" || !ACTIONS.has(d.action)) {
25-
errors.push(`${at}: invalid action ${JSON.stringify(d?.action)}`);
26-
continue;
27-
}
28-
const ids = d.action === "conflict" ? [d.winner, d.loser] : (d.ids ?? []);
29-
if (d.action === "conflict") {
39+
local.push(`${at}: invalid action ${JSON.stringify(d?.action)}`);
40+
} else if (d.action === "conflict") {
3041
if (!d.winner || !d.loser || d.winner === d.loser) {
31-
errors.push(`${at}: conflict needs distinct winner and loser`);
32-
continue;
42+
local.push(`${at}: conflict needs distinct winner and loser`);
3343
}
3444
} else if (d.action === "create") {
3545
// Mint a fresh memory (sleep pattern discovery). Claims no existing id,
3646
// so it skips the claiming loop below; evidence is optional provenance
3747
// (already filtered to real ids by the caller) and is stored in content.
3848
if (typeof d.title !== "string" || !d.title.trim()) {
39-
errors.push(`${at}: create needs non-empty title`);
40-
continue;
49+
local.push(`${at}: create needs non-empty title`);
4150
}
4251
if (typeof d.content !== "string" || !d.content.trim()) {
43-
errors.push(`${at}: create needs non-empty content`);
44-
continue;
52+
local.push(`${at}: create needs non-empty content`);
4553
}
4654
if (d.importance !== undefined && (!Number.isInteger(d.importance) || d.importance < 1 || d.importance > 5)) {
47-
errors.push(`${at}: create importance must be an integer 1-5 when provided`);
55+
local.push(`${at}: create importance must be an integer 1-5 when provided`);
4856
}
4957
if (typeof d.type !== "string" || !d.type.trim()) {
50-
errors.push(`${at}: create needs non-empty type`);
58+
local.push(`${at}: create needs non-empty type`);
5159
}
52-
continue;
5360
} else if (!Array.isArray(d.ids) || d.ids.length === 0) {
54-
errors.push(`${at}: ${d.action} needs non-empty ids`);
55-
continue;
61+
local.push(`${at}: ${d.action} needs non-empty ids`);
5662
}
5763
// update-specific field validation runs BEFORE claiming ids, so a failing
5864
// update never pollutes the claimed set (which drives the "every id must
5965
// appear in a decision" check below).
60-
if (d.action === "update") {
66+
if (local.length === 0 && d?.action === "update") {
6167
// 只能更新单条
6268
if (!Array.isArray(d.ids) || d.ids.length !== 1) {
63-
errors.push(`${at}: update must target exactly one id`);
64-
continue;
65-
}
66-
// 必须产生实际变化
67-
const mem = snapshot.get(d.ids[0]);
68-
const hasChange = (d.title !== undefined && d.title !== mem?.title)
69-
|| (d.content !== undefined && d.content !== mem?.content)
70-
|| (d.importance !== undefined && d.importance !== mem?.importance);
71-
if (!hasChange) {
72-
errors.push(`${at}: update must change at least one field`);
73-
continue;
74-
}
75-
// 不能更新 summary
76-
if (mem?.type === "summary") {
77-
errors.push(`${at}: cannot update summary via update action`);
78-
continue;
79-
}
80-
// 保护期:新建记忆不可立即被 update(可配置)
81-
const ageHours = (Date.now() - new Date(mem?.created_at).getTime()) / 3600000;
82-
if (ageHours < minAgeHours) {
83-
errors.push(`${at}: memory too young (< ${minAgeHours}h)`);
84-
continue;
69+
local.push(`${at}: update must target exactly one id`);
70+
} else {
71+
// 必须产生实际变化
72+
const mem = snapshot.get(d.ids[0]);
73+
const hasChange = (d.title !== undefined && d.title !== mem?.title)
74+
|| (d.content !== undefined && d.content !== mem?.content)
75+
|| (d.importance !== undefined && d.importance !== mem?.importance);
76+
if (!hasChange) {
77+
local.push(`${at}: update must change at least one field`);
78+
} else if (mem?.type === "summary") {
79+
// 不能更新 summary
80+
local.push(`${at}: cannot update summary via update action`);
81+
} else {
82+
// 保护期:新建记忆不可立即被 update(可配置)
83+
const ageHours = (Date.now() - new Date(mem?.created_at).getTime()) / 3600000;
84+
if (ageHours < minAgeHours) {
85+
local.push(`${at}: memory too young (< ${minAgeHours}h)`);
86+
}
87+
}
8588
}
8689
}
87-
for (const id of ids) {
88-
const mem = snapshot.get(id);
89-
if (!mem) {
90-
errors.push(`${at}: unknown id ${JSON.stringify(id)}`);
91-
} else if (mem.archived || mem.type === "summary") {
92-
errors.push(`${at}: id ${JSON.stringify(id)} is archived or summary (not eligible)`);
90+
if (local.length === 0 && d && d.action !== "create") {
91+
// seen 捕获同一条决策内的重复 id;claimed 只含先前存活决策的 id
92+
// (被跳过的决策不 claim,其目标留给其它合法决策/隐式 keep)。
93+
const seen = new Set();
94+
for (const id of ids) {
95+
const mem = snapshot.get(id);
96+
if (!mem) {
97+
local.push(`${at}: unknown id ${JSON.stringify(id)}`);
98+
} else if (mem.archived || mem.type === "summary") {
99+
local.push(`${at}: id ${JSON.stringify(id)} is archived or summary (not eligible)`);
100+
}
101+
if (claimed.has(id) || seen.has(id)) {
102+
local.push(`${at}: id ${JSON.stringify(id)} claimed by multiple decisions`);
103+
}
104+
seen.add(id);
93105
}
94-
if (claimed.has(id)) {
95-
errors.push(`${at}: id ${JSON.stringify(id)} claimed by multiple decisions`);
106+
if (local.length === 0 && d.action === "merge") {
107+
if (!d.keepSource || !d.ids.includes(d.keepSource)) {
108+
local.push(`${at}: merge keepSource must be one of ids`);
109+
}
110+
if (typeof d.title !== "string" || !d.title.trim() || typeof d.content !== "string" || !d.content.trim()) {
111+
local.push(`${at}: merge needs non-empty title and content`);
112+
}
113+
if (d.importance !== undefined && (!Number.isInteger(d.importance) || d.importance < 1 || d.importance > 5)) {
114+
local.push(`${at}: merge importance must be an integer 1-5 when provided`);
115+
}
116+
// Merging across types would blur preference/project/decision boundaries
117+
// in the injected context; the snapshot carries each entry's type.
118+
// Issue #26 (P1):默认禁止跨类型合并。用户显式开启 allowCrossTypeMerge
119+
// 后放宽该检查,类型边界由用户自行承担。
120+
const mergeTypes = new Set(d.ids.map((id) => snapshot.get(id)?.type));
121+
if (mergeTypes.size > 1 && options.allowCrossTypeMerge !== true) {
122+
local.push(`${at}: merge ids span multiple types (${[...mergeTypes].join(", ")})`);
123+
}
96124
}
97-
claimed.add(id);
98125
}
99-
if (d.action === "merge") {
100-
if (!d.keepSource || !d.ids.includes(d.keepSource)) {
101-
errors.push(`${at}: merge keepSource must be one of ids`);
102-
}
103-
if (typeof d.title !== "string" || !d.title.trim() || typeof d.content !== "string" || !d.content.trim()) {
104-
errors.push(`${at}: merge needs non-empty title and content`);
105-
}
106-
if (d.importance !== undefined && (!Number.isInteger(d.importance) || d.importance < 1 || d.importance > 5)) {
107-
errors.push(`${at}: merge importance must be an integer 1-5 when provided`);
108-
}
109-
// Merging across types would blur preference/project/decision boundaries
110-
// in the injected context; the snapshot carries each entry's type.
111-
const mergeTypes = new Set(d.ids.map((id) => snapshot.get(id)?.type));
112-
if (mergeTypes.size > 1) {
113-
errors.push(`${at}: merge ids span multiple types (${[...mergeTypes].join(", ")})`);
126+
if (local.length > 0) {
127+
if (skipInvalid) {
128+
// 单条非法 → 跳过该决策,不 claim id(其目标记忆留给其它合法决策/隐式
129+
// keep),并记录到 skipped 供调用方日志/审计。信息性跳过绝不写入全局
130+
// errors,否则会误触发下方的整单拒绝。
131+
skipped.push({ index, action: d?.action, ids, error: local.join("; ") });
132+
} else {
133+
errors.push(...local);
114134
}
135+
continue;
136+
}
137+
if (d.action !== "create") {
138+
for (const id of ids) claimed.add(id);
115139
}
140+
survivors.push(d);
116141
}
117-
// Cap update churn: too many edits in one cycle signals a runaway model
118-
const updateCount = decisions.filter((d) => d.action === "update").length;
142+
// Cap update churn: too many edits in one cycle signals a runaway model.
143+
// 全局信号——skipInvalid 模式下依旧整单拒绝(见函数头注释)。
144+
const updateCount = survivors.filter((d) => d.action === "update").length;
119145
if (updateCount > maxUpdatePerRun) {
120146
errors.push(`too many update decisions: ${updateCount} > ${maxUpdatePerRun}`);
121147
}
122148
// Cap pattern minting per run (sleepMaxPatternPerRun passes through here).
123-
const createCount = decisions.filter((d) => d.action === "create").length;
149+
const createCount = survivors.filter((d) => d.action === "create").length;
124150
const maxCreatePerRun = options.maxCreatePerRun ?? 5;
125151
if (createCount > maxCreatePerRun) {
126152
errors.push(`too many create decisions: ${createCount} > ${maxCreatePerRun}`);
@@ -137,25 +163,31 @@ export function validateDecisions(decisions, snapshot, options = {}) {
137163
// snapshot(claimed.size / snapshot.size < dreamMinExplicitCoverage)时整单拒绝,
138164
// 而不是用 keep 把绝大部分 snapshot 全部"通过"。
139165
if (errors.length > 0) {
140-
return { ok: false, errors };
166+
return { ok: false, errors, skipped };
141167
}
142168
const minCoverage = options.dreamMinExplicitCoverage ?? 0.5;
143169
if (options.dreamImplicitKeep !== false) {
144170
const coverage = snapshot.size > 0 ? claimed.size / snapshot.size : 1;
145171
if (coverage < minCoverage) {
146172
errors.push(`explicit decision coverage ${Math.round(coverage * 100)}% < minimum ${Math.round(minCoverage * 100)}%`);
147-
return { ok: false, errors };
173+
return { ok: false, errors, skipped };
148174
}
149175
for (const id of snapshot.keys()) {
150-
if (!claimed.has(id)) decisions.push({ action: "keep", ids: [id] });
176+
if (!claimed.has(id)) survivors.push({ action: "keep", ids: [id] });
151177
}
152178
} else {
153179
for (const id of snapshot.keys()) {
154180
if (!claimed.has(id)) errors.push(`memory ${JSON.stringify(id)} missing from decisions`);
155181
}
156-
if (errors.length > 0) return { ok: false, errors };
182+
if (errors.length > 0) return { ok: false, errors, skipped };
183+
}
184+
// 调用方下游(apply/audit)复用同一 decisions 引用:就地同步为 survivors——
185+
// 在 skipInvalid 模式下去掉被跳过的非法决策;在隐式 keep 下追加补齐的 keep。
186+
// 内容一致时(无跳过、无补齐)为 no-op。
187+
if (survivors.length !== decisions.length) {
188+
decisions.splice(0, decisions.length, ...survivors);
157189
}
158-
return { ok: true, errors };
190+
return { ok: true, errors, skipped };
159191
}
160192

161193
/** Marker thrown when a decision target changed since the run snapshot. */

0 commit comments

Comments
 (0)