Skip to content

Commit 437b059

Browse files
committed
fix(dream): archive 类型护栏 + 批量上限——价值判断式大扫除被拦下(#104 方向 2)
实测事故(#104):一轮 12 条互不相关主题被批量 archive,LLM 自述理由是 价值判断(「对核心知识库长期参考价值较低,保持整洁和高信噪比」),而 提示词对 archive 的定义是「重复或过时」;12 条中 8 条疑似误伤,含 rejected_solution / constraint / pitfall 等长保留类型——这些类型的存在 意义即长期保存,误归档损失最大。 - 类型护栏:长保留类型(preference / pattern / rejected_solution / constraint / pitfall)的 archive 决策必须命中「重复/过时」类理由 (中英双语词表,宁宽勿误拦),否则单条跳过记 skipped(run 记 degraded、明细进 dream_runs.skipped),绝不整单拒绝。conflict 裁决 的败者归档不经过 archive action,不受影响。 - 批量上限:dreamMaxArchivePerRun(默认 8),与 update 上限同类的全局 闸门——skipInvalid 也不豁免,超限整单拒绝;正常清理可调高。 - 提示词同步加固(中英):长保留类型勿归档 + 单轮 8 条上限,辅助防线。 - allowCrossTypeMerge 默认值维持 false(#26 的类型边界决策):483218131 的 288 条 skipped 数据里 94% 是跨类型 merge 被安全拦下,护栏在正确 工作;降默认值会把类型模糊的代价转嫁给全部用户,不加提示词已经 明文「仅合并同类型」。数据与结论记录在 PR/issue。 测试:新增 7 用例(护栏通过/跳过/边界/conflict 豁免/上限/上限可配/ 端到端);650 记忆窗口用例显式调高 archive 上限(考察点不同)。全量 1055 用例 0 fail。
1 parent f39b9b4 commit 437b059

11 files changed

Lines changed: 201 additions & 6 deletions

File tree

‎dsh-mneme/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ dsh web
413413
| `dreamCandidateMode` | `window` | dream 候选集构造:`window`(只取最近 `dreamMaxSnapshotSize` 条)/ `hybrid`(在此基础上并入向量翻出的高相似组)。纯时间窗口下,实测 45 对「双方活跃且 sim≥0.85」里 0 对能同时进窗口——该合并的一对几乎永远碰不到面。**已知边界**:dream 侧动作集仍是五分支(keep / merge / archive / update / conflict),hybrid 翻出的互补型 / 演进型对在 dream 里没有 `differentiate` / `supersede` 出口;正确出口在 sleep 侧(`sleepActionSet: full`)|
414414
| `dreamCandidateMax` | `0` | hybrid 的候选总量上限;`0` = 复用 `dreamMaxSnapshotSize`。候选总量与库总量解耦,输入成本不随库增长 |
415415
| `dreamCandidateMinSim` | `0.85` | hybrid 判「高相似」的阈值(与 sleep 的 normal 档对齐,两个模块共用同一个「高相似」定义) |
416+
| `dreamMaxArchivePerRun` | `8` | 单轮 archive 决策上限(超限整单拒绝,防一次性大扫除;正常清理可调高) |
416417
| `apiToken` | 空 | 可选 API 鉴权 token;设置后写操作与密钥接口要求 `Authorization: Bearer <apiToken>` |
417418
| `embedProvider` | `openai` | 语义后端:`openai`(默认,兼容 v0.1)/ `local`(ONNX 离线)/ `ollama` |
418419
| `localEmbedModel` | `Xenova/bge-small-zh-v1.5` | 本地 ONNX embedding 模型 |

‎dsh-mneme/lib/config.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ export const Config = z.object({
111111
// 记忆做 consolidation。大记忆量下全量快照会把 LLM 输入撑爆(636 记忆 →
112112
// 677 "missing" errors、applied=0),窗口外的旧记忆不进 snapshot。
113113
dreamMaxSnapshotSize: z.natural().min(1).max(1000).default(200),
114+
// Issue #104 方向 2:单轮 archive 决策上限。一次性大扫除(12 条互不相关主题
115+
// 被批量归档,其中 8 条疑似误伤长保留类型)是失控信号,与 update 上限同类的
116+
// 全局闸门:skipInvalid 也不豁免,超限整单拒绝。正常清理可调高。
117+
dreamMaxArchivePerRun: z.natural().min(1).max(200).default(8),
114118
// Issue #125:候选集构造方式。"window"(默认,等同现状)只取最近
115119
// dreamMaxSnapshotSize 条;"hybrid" 在此基础上并入**向量翻出的高相似组**——
116120
// 本机实测 45 对「双方活跃且 sim≥0.85」里 0 对能同时进窗口(窗口覆盖率 11.7%),

‎dsh-mneme/lib/dream.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
935935
}
936936
const { ok, errors, skipped, resolvedShortIds, coverageShortfall } = validateDecisions(decisions, snapshot, {
937937
maxUpdatePerRun: config.reflectionUpdateMaxPerRun,
938+
// Issue #104 方向 2:archive 批量上限(全局闸门,超限整单拒绝)。
939+
maxArchivePerRun: config.dreamMaxArchivePerRun,
938940
minAgeHours: config.reflectionUpdateMinAgeHours,
939941
// v0.4.4 fix:显式透传,用户配 dreamImplicitKeep:false 时严格模式必须
940942
// 真正生效,dreamMinExplicitCoverage 决定隐式 keep 下的覆盖率下限。

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ export const ACTIONS = new Set(["keep", "merge", "archive", "conflict", "update"
1111
// = preferred. observation (measured) > inferred (derived) > subjective (guess).
1212
const EPISTEMIC_PRIORITY = { observation: 3, inferred: 2, subjective: 1 };
1313

14+
// Issue #104 方向 2:archive 类型护栏的长保留类型集合——这些类型的存在意义
15+
// 就是长期保存(踩坑记录、约束、用户偏好、挖掘出的模式),LLM 以「价值判断」
16+
// 类理由批量归档它们时误伤损失最大。conflict 裁决的败者归档不经过 archive
17+
// action(走 applyDecisions 的 conflict 分支),不受此护栏影响。
18+
const ARCHIVE_GUARDED_TYPES = new Set(["preference", "pattern", "rejected_solution", "constraint", "pitfall"]);
19+
// 「重复 / 过时」类理由的关键词(中英双语)。命中才允许归档长保留类型;
20+
// 词表宁可放宽也不误拦——护栏只跳过单条并落明细,不整单拒绝。
21+
const ARCHIVE_RATIONALE_RE = /重复|过时|陈旧|过期|已被取代|被取代|冗余|不再使用|旧版|废弃|stale|outdated|obsolete|dup(?:licate|licat)?|superseded|redundant|deprecated|expired|replaced/i;
22+
1423
/**
1524
* Validate a dream decision list against a snapshot of eligible memories.
1625
* @param decisions - LLM-produced decision list. In skipInvalid mode, invalid
@@ -128,6 +137,21 @@ export function validateDecisions(decisions, snapshot, options = {}) {
128137
// update-specific field validation runs BEFORE claiming ids, so a failing
129138
// update never pollutes the claimed set (which drives the "every id must
130139
// appear in a decision" check below).
140+
// Issue #104 方向 2:archive 类型护栏。长保留类型的 archive 决策必须携带
141+
// 「重复 / 过时」类理由(提示词对 archive 的定义本就如此),「价值判断」类
142+
// 理由(保持整洁、参考价值低……)不放行——单条跳过记 skipped(run 记
143+
// degraded,明细进 dream_runs.skipped),绝不整单拒绝。未知 id 在下方通用
144+
// 校验里另行报错,这里 snapshot.get 为 undefined 时跳过护栏判断。
145+
if (local.length === 0 && d?.action === "archive") {
146+
const guardedTypes = new Set();
147+
for (const id of d.ids) {
148+
const mem = snapshot.get(id);
149+
if (mem && ARCHIVE_GUARDED_TYPES.has(mem.type)) guardedTypes.add(mem.type);
150+
}
151+
if (guardedTypes.size > 0 && !ARCHIVE_RATIONALE_RE.test(String(d.reason ?? ""))) {
152+
local.push(`${at}: archive of long-retention type(s) ${[...guardedTypes].join(", ")} needs a duplicate/outdated rationale (重复/过时), got: ${JSON.stringify(String(d.reason ?? "")).slice(0, 60)}`);
153+
}
154+
}
131155
if (local.length === 0 && d?.action === "update") {
132156
// 只能更新单条
133157
if (!Array.isArray(d.ids) || d.ids.length !== 1) {
@@ -216,6 +240,14 @@ export function validateDecisions(decisions, snapshot, options = {}) {
216240
if (createCount > maxCreatePerRun) {
217241
errors.push(`too many create decisions: ${createCount} > ${maxCreatePerRun}`);
218242
}
243+
// Issue #104 方向 2:archive 批量上限——一次性大扫除(12 条互不相关主题被
244+
// 批量归档)是失控信号,与 update 上限同一类全局闸门:skipInvalid 也不豁免,
245+
// 超限整单拒绝;正常的大规模清理由用户调高 dreamMaxArchivePerRun。
246+
const archiveCount = survivors.filter((d) => d.action === "archive").length;
247+
const maxArchivePerRun = options.maxArchivePerRun ?? 8;
248+
if (archiveCount > maxArchivePerRun) {
249+
errors.push(`too many archive decisions: ${archiveCount} > ${maxArchivePerRun}`);
250+
}
219251
// Issue #89 防洗白核心(#104 方向 1 重审后保留):全部决策都被跳过、合法
220252
// 子集为空时仍整单拒绝——宽容的前提是「有合法子集可应用」;模型输出整体
221253
// 非法(如全部幻觉 id)是真故障,不能被隐式 keep 洗成 ok。#104 只豁免

0 commit comments

Comments
 (0)