Skip to content

Commit 2af4743

Browse files
committed
fix(dream): 节流与冷却时刻跨重启持久化——审计表恢复 lastRunAt 种子(slow-stack#89 连发根因)
1 parent 7412400 commit 2af4743

10 files changed

Lines changed: 140 additions & 8 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
## 🐛 修复
66

7+
- **autoDream / sleep 的节流与冷却时刻跨重启持久化(issue #89 连发根因)**:两个调度器的
8+
`lastRunAt` 只活在内存里,进程重启即归零——最小间隔 / 冷却闸对新实例放行,重启后立刻连发
9+
(#89 Sample A/C 实测:横跨重启边界的 8.7 / 23.1 分钟间隔连发)。修复走审计表:`dream_runs`
10+
本来就逐 run 落库(failed/degraded 也算 run),新增 `store.lastDreamRunAt(runType)` 读回最近
11+
一次开跑时刻,构造 dream 调度器(全类型)与 sleep 调度器(`run_type='sleep'`)时注入种子——
12+
零 schema 迁移、零新配置键。回归测试 +3(审计读回与 run_type 过滤、dream 重启闸、sleep 冷却
13+
重启后按剩余窗口重排)。
14+
715
- **本地嵌入对 BGE 系用错池化(静默偏差,不报错)**:`LocalEmbedder.embed()` 对所有本地模型硬编码
816
`pooling: "mean"`,但 BGE 系(含默认的 `Xenova/bge-small-zh-v1.5`)是按 **CLS** 训练的——模型自带的
917
`1_Pooling/config.json` 明确写着 `pooling_mode_cls_token: true` / `pooling_mode_mean_tokens: false`,

‎dsh-mneme/lib/dream.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,17 @@ export async function maintainIndexAfterDream(decisions, service, semantic) {
744744
if (embedder.modelHash) vectorIndex.markModel?.(embedder.modelHash, embedder.dimension);
745745
}
746746

747-
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, logger, semantic = null }) {
747+
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, logger, semantic = null, lastRunAtSeed = 0 }) {
748748
let pendingTimer = null;
749749
let running = false;
750750
let disposed = false;
751751
let baseline = { count: 0, chars: 0 };
752752
let inFlight = null;
753753
// Issue #89(请求 2):上一次实际开跑时刻。失败/degraded 的 run 也占用
754754
// 最小间隔——节流的目的正是防止失败调用连发;间隔内的触发静默跳过。
755-
let lastRunAt = 0;
755+
// lastRunAtSeed:调用方从 dream_runs 审计表读出的上次开跑时刻——
756+
// 内存变量进程重启即归零,闸门对新实例放行 → 重启后立刻连发(#89 根因)。
757+
let lastRunAt = lastRunAtSeed;
756758

757759
function shouldTrigger(service) {
758760
const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,14 @@ export function createSleepScheduler({
686686
onRun = null,
687687
now = () => Date.now(),
688688
setTimeoutFn = setTimeout,
689-
clearTimeoutFn = clearTimeout
689+
clearTimeoutFn = clearTimeout,
690+
// Issue #89 同族:冷却时刻由调用方从 dream_runs 审计表恢复(run_type='sleep')——
691+
// 内存变量进程重启即归零,冷却闸对新实例放行 → 重启后立即进入 sleep 冷却盲区。
692+
// 测试注入 0(默认)即保持旧行为。
693+
lastRunAtSeed = 0
690694
}) {
691695
let lastWriteAt = now();
692-
let lastRunAt = 0;
696+
let lastRunAt = lastRunAtSeed;
693697
let running = false;
694698
let disposed = false;
695699
let idleTimer = null;

‎dsh-mneme/lib/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +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(),
462463
onRun: () => (dream ? dream.runDream(ctx, service, cfg) : Promise.resolve({ ok: true, skipped: true }))
463464
});
464465
service.setDreamHook(() => dream.maybeSchedule(service));
@@ -475,6 +476,7 @@ export const apply = (ctx, config) => {
475476
service,
476477
config: cfg,
477478
logger: ctx.logger,
479+
lastRunAtSeed: store.lastDreamRunAt("sleep"),
478480
onRun: (signal) => (sleep ? runSleep(ctx, service, cfg, ctx.logger, { embedder, vectorIndex }, signal) : Promise.resolve({ ok: true, skipped: true }))
479481
});
480482
service.setSleepHook(() => sleep.noteWrite());

‎dsh-mneme/lib/store.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,22 @@ export function createStore(path) {
15321532
return rows.map(toDreamRun);
15331533
}
15341534

1535+
// Issue #89:上次实际开跑时刻(epoch ms),从审计表恢复——调度器的 lastRunAt
1536+
// 只活在内存里,进程重启即归零,闸门对新实例放行 → 重启后立刻连发。审计表
1537+
// 本来就逐 run 落库(failed/degraded 也算 run),直接读它就是事实源,零迁移。
1538+
function lastDreamRunAt(runType = null) {
1539+
const row = runType
1540+
? db.prepare(
1541+
"SELECT created_at FROM dream_runs WHERE run_type = ? ORDER BY created_at DESC, id LIMIT 1"
1542+
).get(runType)
1543+
: db.prepare(
1544+
"SELECT created_at FROM dream_runs ORDER BY created_at DESC, id LIMIT 1"
1545+
).get();
1546+
if (!row?.created_at) return 0;
1547+
const ms = Date.parse(row.created_at);
1548+
return Number.isFinite(ms) ? ms : 0;
1549+
}
1550+
15351551
/**
15361552
* Latest ruling-rule version seen on the audit trail. policy_epoch is a config
15371553
* value stamped onto each run by the caller; reading the newest row's epoch
@@ -2486,6 +2502,7 @@ export function createStore(path) {
24862502
needsEmbedding,
24872503
searchVector,
24882504
saveDreamRun,
2505+
lastDreamRunAt,
24892506
getDreamRun,
24902507
listDreamRuns,
24912508
getLatestPolicyEpoch,

‎dsh-mneme/src/dream.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,15 +744,17 @@ export async function maintainIndexAfterDream(decisions, service, semantic) {
744744
if (embedder.modelHash) vectorIndex.markModel?.(embedder.modelHash, embedder.dimension);
745745
}
746746

747-
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, logger, semantic = null }) {
747+
export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChars = 5000, delayMs = 2000, minIntervalMs = 0, logger, semantic = null, lastRunAtSeed = 0 }) {
748748
let pendingTimer = null;
749749
let running = false;
750750
let disposed = false;
751751
let baseline = { count: 0, chars: 0 };
752752
let inFlight = null;
753753
// Issue #89(请求 2):上一次实际开跑时刻。失败/degraded 的 run 也占用
754754
// 最小间隔——节流的目的正是防止失败调用连发;间隔内的触发静默跳过。
755-
let lastRunAt = 0;
755+
// lastRunAtSeed:调用方从 dream_runs 审计表读出的上次开跑时刻——
756+
// 内存变量进程重启即归零,闸门对新实例放行 → 重启后立刻连发(#89 根因)。
757+
let lastRunAt = lastRunAtSeed;
756758

757759
function shouldTrigger(service) {
758760
const memories = service.all().filter((m) => !m.archived && m.type !== "summary" && m.type !== "document");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,14 @@ export function createSleepScheduler({
686686
onRun = null,
687687
now = () => Date.now(),
688688
setTimeoutFn = setTimeout,
689-
clearTimeoutFn = clearTimeout
689+
clearTimeoutFn = clearTimeout,
690+
// Issue #89 同族:冷却时刻由调用方从 dream_runs 审计表恢复(run_type='sleep')——
691+
// 内存变量进程重启即归零,冷却闸对新实例放行 → 重启后立即进入 sleep 冷却盲区。
692+
// 测试注入 0(默认)即保持旧行为。
693+
lastRunAtSeed = 0
690694
}) {
691695
let lastWriteAt = now();
692-
let lastRunAt = 0;
696+
let lastRunAt = lastRunAtSeed;
693697
let running = false;
694698
let disposed = false;
695699
let idleTimer = null;

‎dsh-mneme/src/index.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +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(),
462463
onRun: () => (dream ? dream.runDream(ctx, service, cfg) : Promise.resolve({ ok: true, skipped: true }))
463464
});
464465
service.setDreamHook(() => dream.maybeSchedule(service));
@@ -475,6 +476,7 @@ export const apply = (ctx, config) => {
475476
service,
476477
config: cfg,
477478
logger: ctx.logger,
479+
lastRunAtSeed: store.lastDreamRunAt("sleep"),
478480
onRun: (signal) => (sleep ? runSleep(ctx, service, cfg, ctx.logger, { embedder, vectorIndex }, signal) : Promise.resolve({ ok: true, skipped: true }))
479481
});
480482
service.setSleepHook(() => sleep.noteWrite());

‎dsh-mneme/src/store.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,22 @@ export function createStore(path) {
15321532
return rows.map(toDreamRun);
15331533
}
15341534

1535+
// Issue #89:上次实际开跑时刻(epoch ms),从审计表恢复——调度器的 lastRunAt
1536+
// 只活在内存里,进程重启即归零,闸门对新实例放行 → 重启后立刻连发。审计表
1537+
// 本来就逐 run 落库(failed/degraded 也算 run),直接读它就是事实源,零迁移。
1538+
function lastDreamRunAt(runType = null) {
1539+
const row = runType
1540+
? db.prepare(
1541+
"SELECT created_at FROM dream_runs WHERE run_type = ? ORDER BY created_at DESC, id LIMIT 1"
1542+
).get(runType)
1543+
: db.prepare(
1544+
"SELECT created_at FROM dream_runs ORDER BY created_at DESC, id LIMIT 1"
1545+
).get();
1546+
if (!row?.created_at) return 0;
1547+
const ms = Date.parse(row.created_at);
1548+
return Number.isFinite(ms) ? ms : 0;
1549+
}
1550+
15351551
/**
15361552
* Latest ruling-rule version seen on the audit trail. policy_epoch is a config
15371553
* value stamped onto each run by the caller; reading the newest row's epoch
@@ -2486,6 +2502,7 @@ export function createStore(path) {
24862502
needsEmbedding,
24872503
searchVector,
24882504
saveDreamRun,
2505+
lastDreamRunAt,
24892506
getDreamRun,
24902507
listDreamRuns,
24912508
getLatestPolicyEpoch,
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
import { createStore } from "../src/store.js";
4+
import { createService } from "../src/service.js";
5+
import { createDreamScheduler } from "../src/dream.js";
6+
import { createSleepScheduler } from "../src/dream/sleep.js";
7+
8+
// 回归(issue #89):调度器的 lastRunAt 只活在内存里,进程重启即归零——
9+
// 最小间隔/冷却闸对新实例放行,重启后 autoDream/sleep 立即连发(Sample A/C 的
10+
// 根因)。修复 = 构造调度器时从 dream_runs 审计表恢复种子(store.lastDreamRunAt,
11+
// failed/degraded 也算 run,审计表本来就是逐 run 落库)。本文件锁三点:
12+
// 1. lastDreamRunAt 读回最近一次 run 的开跑时刻(含 run_type 过滤);
13+
// 2. dream 调度器带种子构造后,重启场景下最小间隔闸门立即生效;
14+
// 3. sleep 调度器带种子构造后,冷却期内不起跑、且按剩余窗口重排不丢触发权。
15+
16+
test("issue#89: store.lastDreamRunAt recovers the last run timestamp (with run_type filter)", () => {
17+
const store = createStore(":memory:");
18+
assert.equal(store.lastDreamRunAt(), 0, "empty trail → 0 (never ran)");
19+
assert.equal(store.lastDreamRunAt("sleep"), 0);
20+
store.saveDreamRun({ status: "ok", snapshot_hash: "h1", input_count: 1, receipt: "r", created_at: "2026-09-10T01:05:23.640Z", run_type: "auto" });
21+
store.saveDreamRun({ status: "ok", snapshot_hash: "h2", input_count: 1, receipt: "r", created_at: "2026-09-09T20:00:00.000Z", run_type: "sleep" });
22+
assert.equal(store.lastDreamRunAt(), Date.parse("2026-09-10T01:05:23.640Z"), "latest run regardless of type");
23+
assert.equal(store.lastDreamRunAt("sleep"), Date.parse("2026-09-09T20:00:00.000Z"), "filtered by run_type");
24+
store.close();
25+
});
26+
27+
test("issue#89: dream scheduler seeded from the audit trail — restart no longer bypasses the min interval", async () => {
28+
const store = createStore(":memory:");
29+
const service = createService({ store, mirror: null, config: {} });
30+
// 模拟上一进程刚跑过一轮(10ms 前开跑)并留下审计行,然后「重启」:
31+
store.saveDreamRun({
32+
status: "ok", snapshot_hash: "h", input_count: 1, receipt: "r",
33+
created_at: new Date(Date.now() - 10).toISOString(), run_type: "auto"
34+
});
35+
const dream = createDreamScheduler({
36+
onRun: async () => ({ ok: false, error: "llm failed" }),
37+
thresholdCount: 1, thresholdChars: 0, delayMs: 0, minIntervalMs: 5000,
38+
lastRunAtSeed: store.lastDreamRunAt(),
39+
logger: { warn: () => {} }
40+
});
41+
service.saveWithDedupe({ type: "project", title: "a", content: "x" });
42+
// 无种子时这里会返回 true(旧缺陷:重启后立刻连发);带种子必须被闸门拦下。
43+
assert.equal(dream.maybeSchedule(service), false, "seeded gate blocks immediately after 'restart'");
44+
store.close();
45+
});
46+
47+
test("issue#89: sleep scheduler seeded — cooldown survives a restart", async () => {
48+
let nowMs = 1_000_000;
49+
const timers = [];
50+
let seq = 1;
51+
let runs = 0;
52+
const lastRunAt = nowMs - 10 * 60_000; // 上一进程 10 分钟前跑过,CD 1h 还剩 50min
53+
const sched = createSleepScheduler({
54+
service: { enqueue: (fn) => fn() },
55+
config: { sleepModeEnabled: true, sleepIdleMinutes: 5, sleepMinIntervalHours: 1 },
56+
logger: { warn: () => {} },
57+
onRun: async () => { runs++; return { ok: true }; },
58+
now: () => nowMs,
59+
lastRunAtSeed: lastRunAt,
60+
setTimeoutFn: (fn, delay) => { const t = { id: seq++, at: nowMs + delay, fn }; timers.push(t); return t.id; },
61+
clearTimeoutFn: (id) => { const i = timers.findIndex((t) => t.id === id); if (i >= 0) timers.splice(i, 1); }
62+
});
63+
nowMs += 1000;
64+
sched.noteWrite(); // 挂 idle 闹钟
65+
const idleTimer = timers[0];
66+
assert.ok(idleTimer, "idle timer armed on write");
67+
nowMs = idleTimer.at;
68+
timers.length = 0;
69+
await idleTimer.fn();
70+
assert.equal(runs, 0, "restored cooldown blocks the run");
71+
const rearm = timers[0];
72+
assert.ok(rearm, "re-armed for the remaining cooldown window (not dropped, #187 semantics)");
73+
assert.equal(rearm.at, lastRunAt + 3_600_000 + 1000, "re-arms at CD expiry (+1s guard)");
74+
});

0 commit comments

Comments
 (0)