Skip to content

Commit ab68562

Browse files
committed
fix(dream): 评审修正——dream 种子按 run_type='auto' 过滤,审计行改记开跑时刻,接线补源码锁
1 parent 2af4743 commit ab68562

8 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
`lastRunAt` 只活在内存里,进程重启即归零——最小间隔 / 冷却闸对新实例放行,重启后立刻连发
99
(#89 Sample A/C 实测:横跨重启边界的 8.7 / 23.1 分钟间隔连发)。修复走审计表:`dream_runs`
1010
本来就逐 run 落库(failed/degraded 也算 run),新增 `store.lastDreamRunAt(runType)` 读回最近
11-
一次开跑时刻,构造 dream 调度器(全类型)与 sleep 调度器(`run_type='sleep'`)时注入种子——
12-
零 schema 迁移、零新配置键。回归测试 +3(审计读回与 run_type 过滤、dream 重启闸、sleep 冷却
13-
重启后按剩余窗口重排)。
11+
一次开跑时刻,构造 dream 调度器(`run_type='auto'`)与 sleep 调度器(`run_type='sleep'`)时
12+
注入种子——零 schema 迁移、零新配置键。审计行 `created_at` 同时改为记录**开跑时刻**而非完成
13+
时刻(run 耗时不应计入下一轮间隔窗口)。回归测试 +5(审计读回与 run_type 过滤、dream/sleep
14+
重启闸、index.js 接线源码锁 ×2)。
1415

1516
- **本地嵌入对 BGE 系用错池化(静默偏差,不报错)**:`LocalEmbedder.embed()` 对所有本地模型硬编码
1617
`pooling: "mean"`,但 BGE 系(含默认的 `Xenova/bge-small-zh-v1.5`)是按 **CLS** 训练的——模型自带的

‎dsh-mneme/lib/dream.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,8 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
822822
const logger = ctx.logger;
823823
let memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
824824
if (memories.length === 0) return { ok: true, applied: 0, skipped: true, summary: false };
825+
// Issue #89:开跑时刻既喂给审计行(created_at),也是调度器闸门的时间基准。
826+
const dreamStartedAt = Date.now();
825827
// v0.4.4 滑动窗口:只 consolidation 最近 dreamMaxSnapshotSize 条记忆,
826828
// 窗口外的旧记忆不进 snapshot(大记忆量下全量快照会撑爆 LLM 输入,配合
827829
// 隐式 keep 让 run 始终可收敛)。按 updated_at 倒序取前 maxSize 条。
@@ -888,6 +890,9 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
888890
try {
889891
service.saveDreamRun({
890892
id: runId,
893+
// Issue #89:审计行记开跑时刻而非完成时刻——lastDreamRunAt 以它做
894+
// 重启后的间隔种子,落完成时刻会让 run 耗时白白计入下一轮最小间隔。
895+
created_at: new Date(dreamStartedAt).toISOString(),
891896
status,
892897
error: result.error,
893898
provider: route?.provider,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ function deriveStatus(phases) {
611611
*/
612612
export async function runSleep(ctx, service, config, logger, semantic = null, signal = null) {
613613
const runId = randomUUID();
614+
// Issue #89:开跑时刻——审计行 created_at 的基准(= 冷却种子),run 耗时
615+
// (LLM 各 phase)不应计入重启后的冷却窗口。
616+
const sleepStartedAt = Date.now();
614617
const phases = {};
615618
const attempt = async (name, fn) => {
616619
if (signal?.aborted) return; // user resumed activity — stop before next phase
@@ -647,6 +650,9 @@ export async function runSleep(ctx, service, config, logger, semantic = null, si
647650
try {
648651
service.saveDreamRun({
649652
id: runId,
653+
// Issue #89:审计行记开跑时刻而非完成时刻——run 耗时(含 LLM 各 phase)
654+
// 不该计入重启后的 sleep 冷却种子。
655+
created_at: new Date(sleepStartedAt).toISOString(),
650656
status,
651657
provider: route?.provider,
652658
model: route?.model,

‎dsh-mneme/lib/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export const apply = (ctx, config) => {
459459
minIntervalMs: (cfg.dreamMinIntervalMinutes ?? 0) * 60000,
460460
logger: ctx.logger,
461461
semantic: { embedder, vectorIndex },
462-
lastRunAtSeed: store.lastDreamRunAt(),
462+
lastRunAtSeed: store.lastDreamRunAt("auto"),
463463
onRun: () => (dream ? dream.runDream(ctx, service, cfg) : Promise.resolve({ ok: true, skipped: true }))
464464
});
465465
service.setDreamHook(() => dream.maybeSchedule(service));

‎dsh-mneme/src/dream.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,8 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
822822
const logger = ctx.logger;
823823
let memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");
824824
if (memories.length === 0) return { ok: true, applied: 0, skipped: true, summary: false };
825+
// Issue #89:开跑时刻既喂给审计行(created_at),也是调度器闸门的时间基准。
826+
const dreamStartedAt = Date.now();
825827
// v0.4.4 滑动窗口:只 consolidation 最近 dreamMaxSnapshotSize 条记忆,
826828
// 窗口外的旧记忆不进 snapshot(大记忆量下全量快照会撑爆 LLM 输入,配合
827829
// 隐式 keep 让 run 始终可收敛)。按 updated_at 倒序取前 maxSize 条。
@@ -888,6 +890,9 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
888890
try {
889891
service.saveDreamRun({
890892
id: runId,
893+
// Issue #89:审计行记开跑时刻而非完成时刻——lastDreamRunAt 以它做
894+
// 重启后的间隔种子,落完成时刻会让 run 耗时白白计入下一轮最小间隔。
895+
created_at: new Date(dreamStartedAt).toISOString(),
891896
status,
892897
error: result.error,
893898
provider: route?.provider,

‎dsh-mneme/src/dream/sleep.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ function deriveStatus(phases) {
611611
*/
612612
export async function runSleep(ctx, service, config, logger, semantic = null, signal = null) {
613613
const runId = randomUUID();
614+
// Issue #89:开跑时刻——审计行 created_at 的基准(= 冷却种子),run 耗时
615+
// (LLM 各 phase)不应计入重启后的冷却窗口。
616+
const sleepStartedAt = Date.now();
614617
const phases = {};
615618
const attempt = async (name, fn) => {
616619
if (signal?.aborted) return; // user resumed activity — stop before next phase
@@ -647,6 +650,9 @@ export async function runSleep(ctx, service, config, logger, semantic = null, si
647650
try {
648651
service.saveDreamRun({
649652
id: runId,
653+
// Issue #89:审计行记开跑时刻而非完成时刻——run 耗时(含 LLM 各 phase)
654+
// 不该计入重启后的 sleep 冷却种子。
655+
created_at: new Date(sleepStartedAt).toISOString(),
650656
status,
651657
provider: route?.provider,
652658
model: route?.model,

‎dsh-mneme/src/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export const apply = (ctx, config) => {
459459
minIntervalMs: (cfg.dreamMinIntervalMinutes ?? 0) * 60000,
460460
logger: ctx.logger,
461461
semantic: { embedder, vectorIndex },
462-
lastRunAtSeed: store.lastDreamRunAt(),
462+
lastRunAtSeed: store.lastDreamRunAt("auto"),
463463
onRun: () => (dream ? dream.runDream(ctx, service, cfg) : Promise.resolve({ ok: true, skipped: true }))
464464
});
465465
service.setDreamHook(() => dream.maybeSchedule(service));

‎dsh-mneme/test/lastrunat-persist.test.js‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,24 @@ test("issue#89: store.lastDreamRunAt recovers the last run timestamp (with run_t
2121
store.saveDreamRun({ status: "ok", snapshot_hash: "h2", input_count: 1, receipt: "r", created_at: "2026-09-09T20:00:00.000Z", run_type: "sleep" });
2222
assert.equal(store.lastDreamRunAt(), Date.parse("2026-09-10T01:05:23.640Z"), "latest run regardless of type");
2323
assert.equal(store.lastDreamRunAt("sleep"), Date.parse("2026-09-09T20:00:00.000Z"), "filtered by run_type");
24+
assert.equal(store.lastDreamRunAt("auto"), Date.parse("2026-09-10T01:05:23.640Z"), "auto filter ignores sleep rows");
2425
store.close();
2526
});
2627

2728
test("issue#89: dream scheduler seeded from the audit trail — restart no longer bypasses the min interval", async () => {
2829
const store = createStore(":memory:");
2930
const service = createService({ store, mirror: null, config: {} });
3031
// 模拟上一进程刚跑过一轮(10ms 前开跑)并留下审计行,然后「重启」:
32+
// minIntervalMs 取 1min——断言窗口远宽于用例执行时间,真实时钟抖动(调度、
33+
// 慢盘)不会把 10ms 偏移推出闸门外造成间歇性假失败。
3134
store.saveDreamRun({
3235
status: "ok", snapshot_hash: "h", input_count: 1, receipt: "r",
3336
created_at: new Date(Date.now() - 10).toISOString(), run_type: "auto"
3437
});
3538
const dream = createDreamScheduler({
3639
onRun: async () => ({ ok: false, error: "llm failed" }),
37-
thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 5000,
38-
lastRunAtSeed: store.lastDreamRunAt(),
40+
thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 60000,
41+
lastRunAtSeed: store.lastDreamRunAt("auto"),
3942
logger: { warn: () => {} }
4043
});
4144
service.saveWithDedupe({ type: "project", title: "a", content: "x" });
@@ -72,3 +75,27 @@ test("issue#89: sleep scheduler seeded — cooldown survives a restart", async (
7275
assert.ok(rearm, "re-armed for the remaining cooldown window (not dropped, #187 semantics)");
7376
assert.equal(rearm.at, lastRunAt + 3_600_000 + 1000, "re-arms at CD expiry (+1s guard)");
7477
});
78+
79+
// --- CodeRabbit #291 findings 5/6:index.js 的 seed 注入是单行无分支接线,仓库
80+
// 没有插件级挂载 harness(apply 需要完整宿主 ctx),起全量挂载只为准两条断言
81+
// 不成比例——改为源码级锁:删掉 index.js 的对应注入行即红。import 用绝对路径,
82+
// 不依赖测试进程 CWD。
83+
import { readFileSync } from "node:fs";
84+
import { fileURLToPath } from "node:url";
85+
const indexSrc = readFileSync(fileURLToPath(new URL("../src/index.js", import.meta.url)), "utf8");
86+
87+
test("issue#89: index.js wires the dream scheduler seed from the audit trail", () => {
88+
assert.match(
89+
indexSrc,
90+
/lastRunAtSeed:\s*store\.lastDreamRunAt\("auto"\)/,
91+
"dream scheduler must seed its min-interval gate from dream_runs (run_type='auto') — remove this line and restart-bypass returns"
92+
);
93+
});
94+
95+
test("issue#89: index.js wires the sleep scheduler seed from the audit trail", () => {
96+
assert.match(
97+
indexSrc,
98+
/lastRunAtSeed:\s*store\.lastDreamRunAt\("sleep"\)/,
99+
"sleep scheduler must seed its cooldown gate from dream_runs (run_type='sleep') — remove this line and restart-bypass returns"
100+
);
101+
});

0 commit comments

Comments
 (0)