Skip to content

Commit 5628f87

Browse files
committed
fix(scope): 普通 dream 的跨 scope conflict 决策停车人工裁决(issue #170 复核项 3)
- sleep 路径(scope-candidate-queue)拦截了跨 scope 相似对,但普通 dream 的 LLM 看得到全部记忆、可直接 conflict 归档别的 scope 的唯一副本——保护缺位 - dream.js:conflict 决策按 winner/loser 的 scope 键分流——跨 scope 对两种 模式都停车 conflict_pending(scopeConflictParkReason 专属 reason + 模型 裁决理由),绝不进 applyDecisions;同 scope 对照旧(freeze 停车/非 freeze 自动裁决) - 测试 +2:跨 scope 停车(loser 不归档/winner 无批注)+ 同 scope 照常裁决回归
1 parent c3aa399 commit 5628f87

5 files changed

Lines changed: 136 additions & 2 deletions

File tree

‎dsh-mneme/lib/dream.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { validateDecisions, applyDecisions } from "./dream/decisions.js";
22
import { clusterMemories, findPotentialConflicts, cosineSimilarity } from "./dream/clustering.js";
3+
import { scopeKeyOf } from "./scope.js";
34
import { createHash, randomUUID } from "node:crypto";
45
import { STR, langOf } from "./lang.js";
56
export { validateDecisions, applyDecisions, withEffortFallback, describeStreamFailure, resolveDreamEffort, resolveRoute };
@@ -989,7 +990,21 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
989990
// bounds the review queue; overflow is skipped with a warning.
990991
let frozenCount = 0;
991992
const frozenIds = [];
992-
const applyList = freezeEnabled ? decisions.filter((d) => d.action !== "conflict") : decisions;
993+
// v0.8.1(issue #170 复核项 3):跨 scope 的 conflict 决策两种模式都一律
994+
// 停车人工裁决、绝不自动归档——否则普通 dream 的 LLM 能归档别的 scope 的
995+
// 唯一副本,sleep 路径(scope-candidate-queue)的保护在这里缺位。同 scope
996+
// 的 conflict 决策照旧:freeze 停车 / 非 freeze 自动裁决。
997+
const isCrossScopeDecision = (d) => {
998+
const a = snapshot.get(d.winner);
999+
const b = snapshot.get(d.loser);
1000+
return !!a && !!b && (
1001+
scopeKeyOf(a.agent_scope) !== scopeKeyOf(b.agent_scope)
1002+
|| scopeKeyOf(a.workspace_scope) !== scopeKeyOf(b.workspace_scope)
1003+
);
1004+
};
1005+
const applyList = freezeEnabled
1006+
? decisions.filter((d) => d.action !== "conflict")
1007+
: decisions.filter((d) => d.action !== "conflict" || !isCrossScopeDecision(d));
9931008
if (freezeEnabled) {
9941009
const conflictsToFreeze = decisions.filter((d) => d.action === "conflict");
9951010
if (conflictsToFreeze.length > 0) {
@@ -1017,6 +1032,29 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
10171032
logger?.warn?.(`dsh-mneme dream: conflict freeze lookup failed: ${String(error)}`);
10181033
}
10191034
}
1035+
} else {
1036+
// 非 freeze:跨 scope conflict 停车(绝不进 applyDecisions)。带专属
1037+
// reason + 模型给出的裁决理由,队列里读起来是「归属裁决」而非重复项。
1038+
const crossScope = decisions.filter((d) => d.action === "conflict" && isCrossScopeDecision(d));
1039+
for (const d of crossScope) {
1040+
try {
1041+
const parked = service.saveConflictPending({
1042+
run_id: runId,
1043+
memory_a: d.winner,
1044+
memory_b: d.loser,
1045+
reason: STR.scopeConflictParkReason[language](d.reason)
1046+
});
1047+
if (parked != null) {
1048+
frozenCount++;
1049+
frozenIds.push(d.winner, d.loser);
1050+
}
1051+
} catch (error) {
1052+
logger?.warn?.(`dsh-mneme dream: failed to park cross-scope conflict ${d.winner}/${d.loser}: ${String(error)}`);
1053+
}
1054+
}
1055+
if (crossScope.length > 0) {
1056+
logger?.info?.(`[dsh-mneme] dream: parked ${crossScope.length} cross-scope conflict(s) for human ownership review`);
1057+
}
10201058
}
10211059

10221060
// CAS-guarded, per-decision-transactional apply against the run snapshot:

‎dsh-mneme/lib/lang.js‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dsh-mneme/src/dream.js‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { validateDecisions, applyDecisions } from "./dream/decisions.js";
22
import { clusterMemories, findPotentialConflicts, cosineSimilarity } from "./dream/clustering.js";
3+
import { scopeKeyOf } from "./scope.js";
34
import { createHash, randomUUID } from "node:crypto";
45
import { STR, langOf } from "./lang.js";
56
export { validateDecisions, applyDecisions, withEffortFallback, describeStreamFailure, resolveDreamEffort, resolveRoute };
@@ -989,7 +990,21 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
989990
// bounds the review queue; overflow is skipped with a warning.
990991
let frozenCount = 0;
991992
const frozenIds = [];
992-
const applyList = freezeEnabled ? decisions.filter((d) => d.action !== "conflict") : decisions;
993+
// v0.8.1(issue #170 复核项 3):跨 scope 的 conflict 决策两种模式都一律
994+
// 停车人工裁决、绝不自动归档——否则普通 dream 的 LLM 能归档别的 scope 的
995+
// 唯一副本,sleep 路径(scope-candidate-queue)的保护在这里缺位。同 scope
996+
// 的 conflict 决策照旧:freeze 停车 / 非 freeze 自动裁决。
997+
const isCrossScopeDecision = (d) => {
998+
const a = snapshot.get(d.winner);
999+
const b = snapshot.get(d.loser);
1000+
return !!a && !!b && (
1001+
scopeKeyOf(a.agent_scope) !== scopeKeyOf(b.agent_scope)
1002+
|| scopeKeyOf(a.workspace_scope) !== scopeKeyOf(b.workspace_scope)
1003+
);
1004+
};
1005+
const applyList = freezeEnabled
1006+
? decisions.filter((d) => d.action !== "conflict")
1007+
: decisions.filter((d) => d.action !== "conflict" || !isCrossScopeDecision(d));
9931008
if (freezeEnabled) {
9941009
const conflictsToFreeze = decisions.filter((d) => d.action === "conflict");
9951010
if (conflictsToFreeze.length > 0) {
@@ -1017,6 +1032,29 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
10171032
logger?.warn?.(`dsh-mneme dream: conflict freeze lookup failed: ${String(error)}`);
10181033
}
10191034
}
1035+
} else {
1036+
// 非 freeze:跨 scope conflict 停车(绝不进 applyDecisions)。带专属
1037+
// reason + 模型给出的裁决理由,队列里读起来是「归属裁决」而非重复项。
1038+
const crossScope = decisions.filter((d) => d.action === "conflict" && isCrossScopeDecision(d));
1039+
for (const d of crossScope) {
1040+
try {
1041+
const parked = service.saveConflictPending({
1042+
run_id: runId,
1043+
memory_a: d.winner,
1044+
memory_b: d.loser,
1045+
reason: STR.scopeConflictParkReason[language](d.reason)
1046+
});
1047+
if (parked != null) {
1048+
frozenCount++;
1049+
frozenIds.push(d.winner, d.loser);
1050+
}
1051+
} catch (error) {
1052+
logger?.warn?.(`dsh-mneme dream: failed to park cross-scope conflict ${d.winner}/${d.loser}: ${String(error)}`);
1053+
}
1054+
}
1055+
if (crossScope.length > 0) {
1056+
logger?.info?.(`[dsh-mneme] dream: parked ${crossScope.length} cross-scope conflict(s) for human ownership review`);
1057+
}
10201058
}
10211059

10221060
// CAS-guarded, per-decision-transactional apply against the run snapshot:

‎dsh-mneme/src/lang.js‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dsh-mneme/test/scope-candidates.test.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from "node:test";
22
import assert from "node:assert/strict";
33
import { runSleep } from "../src/dream/sleep.js";
4+
import { createDreamScheduler } from "../src/dream.js";
45
import { createStore } from "../src/store.js";
56
import { createService } from "../src/service.js";
67
import { createVectorIndex } from "../src/vector-index.js";
@@ -153,6 +154,51 @@ test("reviewed pair does not re-queue until one of its sides changes (review ite
153154
store.close();
154155
});
155156

157+
test("dream (non-freeze): cross-scope conflict parks for review instead of being adjudicated (review item 3)", async () => {
158+
const { store, service } = setup();
159+
const w = service.saveWithDedupe({ type: "decision", title: "发布时间", content: "8月20日", importance: 4, agent_scope: "coder", agent_scope_source: "explicit" }).memory;
160+
const l = service.saveWithDedupe({ type: "decision", title: "发布时间旧", content: "8月15日", importance: 4, agent_scope: "writer", agent_scope_source: "explicit" }).memory;
161+
const ctx = {
162+
llm: { stream: async function* () {
163+
yield { type: "text-delta", text: JSON.stringify([{ action: "conflict", winner: w.id, loser: l.id, reason: "日期更新" }]) };
164+
yield { type: "finish", reason: { kind: "ok" } };
165+
} },
166+
logger: { warn: () => {}, info: () => {} }
167+
};
168+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
169+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-chat" });
170+
assert.equal(result.ok, true);
171+
assert.equal(result.applied, 0, "cross-scope conflict is NOT auto-adjudicated");
172+
assert.equal(result.frozen, 1, "parked pair shows up in the run summary");
173+
const pending = store.listConflictPending();
174+
assert.equal(pending.length, 1);
175+
assert.match(pending[0].reason, /跨作用域|cross-scope/, "dedicated reason");
176+
assert.equal(store.getById(l.id).archived, false, "loser NOT archived");
177+
assert.ok(!store.getById(w.id).content.includes("已否决旧信息"), "winner untouched");
178+
store.close();
179+
});
180+
181+
test("dream (non-freeze): same-scope conflict is still auto-adjudicated (regression)", async () => {
182+
const { store, service } = setup();
183+
const w = service.saveWithDedupe({ type: "decision", title: "发布时间", content: "8月20日", importance: 4, agent_scope: "coder", agent_scope_source: "explicit" }).memory;
184+
const l = service.saveWithDedupe({ type: "decision", title: "发布时间旧", content: "8月15日", importance: 4, agent_scope: "coder", agent_scope_source: "explicit" }).memory;
185+
const ctx = {
186+
llm: { stream: async function* () {
187+
yield { type: "text-delta", text: JSON.stringify([{ action: "conflict", winner: w.id, loser: l.id, reason: "日期更新" }]) };
188+
yield { type: "finish", reason: { kind: "ok" } };
189+
} },
190+
logger: { warn: () => {}, info: () => {} }
191+
};
192+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
193+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-chat" });
194+
assert.equal(result.ok, true);
195+
assert.equal(result.applied, 1, "same-scope conflict adjudicated as before");
196+
assert.equal(result.frozen, 0);
197+
assert.equal(store.listConflictPending().length, 0);
198+
assert.equal(store.getById(l.id).archived, true);
199+
store.close();
200+
});
201+
156202
test("non-freeze with an LLM: cross-scope pair bypasses adjudication; same-scope pair is adjudicated", async () => {
157203
const { store, service, vectorIndex } = setup();
158204
const [yNew, yOld] = savePair(service, vectorIndex, "缓存策略A", "缓存策略B", { agent: "coder" }, { agent: "coder" });

0 commit comments

Comments
 (0)