Skip to content

Commit adff10a

Browse files
committed
fix(dream): degraded 轮跳过明细落库到 dream_runs.skipped (#104)
宽容路径上 validateDecisions 就地 splice 抹掉被跳决策(数组长度不变、内容已变), dream_runs.decisions 存的已是幸存列表,被跳条目只进 logger.warn——离线回放 degraded 轮无法判断被跳的是跨类型 merge、update 保护期还是 unknown id。 #137 只覆盖了整单拒绝(failed)路径。 - src/store.js: dream_runs 新增 skipped TEXT 列(JSON,addColumn 迁移,同 policy_epoch/run_type 先例),saveDreamRun 写入 + toDreamRun 读回; - src/dream.js: degraded 出口把 skipped 明细透传给 finish,写独立字段而不内联进 decisions(degraded 的 decisions 是合法子集,下游按「决策数组」消费); - test/dream.test.js: 断言 degraded 审计行含 index/action/error,且 decisions 不含内联标记; - lib/ 由 npm run sync 同步(仅 dream.js / store.js 内容变化)。 测试:839/840 通过。唯一失败 test/peer-blockers.test.js 的 peer-C(8 进程并发 原子递增)在本机沙箱下 spawn EPERM,与本改动无关。
1 parent 4666d56 commit adff10a

5 files changed

Lines changed: 97 additions & 16 deletions

File tree

‎dsh-mneme/lib/dream.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,12 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
657657
outcome: result.outcome,
658658
applied,
659659
summary_stored: summaryStored,
660-
receipt
660+
receipt,
661+
// Issue #104:degraded(合法子集已应用)轮被跳过的决策明细。写成独立字段
662+
// 而非内联进 decisions——degraded 的 decisions 是合法子集,下游按「决策
663+
// 数组」消费,内联标记会污染其它读者(失败轮的 _validationFailed 是整单
664+
// 拒绝哨兵,语义与「部分应用」不同,也不复用)。
665+
skipped: result.skipped
661666
});
662667
} catch (error) {
663668
logger?.warn?.(`dsh-mneme dream: failed to record audit run: ${String(error)}`);
@@ -978,7 +983,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
978983
conflicts,
979984
failures,
980985
frozen: frozenCount,
981-
summary: summaryStored
986+
summary: summaryStored,
987+
// Issue #104:只有真发生跳过时才落库(degraded 也可能仅因 summary 为空),
988+
// ok 轮留 NULL,避免「空数组」与「无此字段」两种假明细占据审计行。
989+
skipped: skippedInvalid ? skipped : undefined
982990
});
983991
}
984992

‎dsh-mneme/lib/store.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ CREATE TABLE IF NOT EXISTS dream_runs (
4343
summary_stored INTEGER NOT NULL DEFAULT 0,
4444
receipt TEXT NOT NULL,
4545
policy_epoch INTEGER NOT NULL DEFAULT 0, -- 裁决规则版本:规则升级后旧裁决降级为历史证据
46-
run_type TEXT NOT NULL DEFAULT 'auto' -- auto | sleep:睡眠周期的审计区分
46+
run_type TEXT NOT NULL DEFAULT 'auto', -- auto | sleep:睡眠周期的审计区分
47+
skipped TEXT -- JSON: degraded 轮被跳过的逐条明细(index/action/ids/error)
4748
);
4849
CREATE INDEX IF NOT EXISTS idx_dream_runs_created ON dream_runs(created_at);
4950
@@ -371,7 +372,9 @@ function toDreamRun(row) {
371372
summary_stored: row.summary_stored === 1,
372373
receipt: row.receipt,
373374
policy_epoch: row.policy_epoch ?? 0,
374-
run_type: row.run_type ?? "auto"
375+
run_type: row.run_type ?? "auto",
376+
// Issue #104:degraded 轮的逐条跳过明细(JSON 列,NULL = 该轮无跳过项)。
377+
skipped: row.skipped ? JSON.parse(row.skipped) : undefined
375378
};
376379
}
377380

@@ -597,6 +600,9 @@ export function createStore(path) {
597600
// Legacy dream_runs without policy_epoch → backfill with the default epoch.
598601
addColumn("dream_runs", "policy_epoch", "ALTER TABLE dream_runs ADD COLUMN policy_epoch INTEGER NOT NULL DEFAULT 0");
599602
addColumn("dream_runs", "run_type", "ALTER TABLE dream_runs ADD COLUMN run_type TEXT NOT NULL DEFAULT 'auto'");
603+
// Issue #104:degraded(合法子集已应用)轮被跳过的决策明细。此前只进 logger.warn,
604+
// 离线回放 dream_runs 无法定位 degraded 成因(跨类型 merge / update 保护期 / unknown id)。
605+
addColumn("dream_runs", "skipped", "ALTER TABLE dream_runs ADD COLUMN skipped TEXT");
600606

601607
// Legacy mirror_state without v0.3.6 generation columns → add each missing
602608
// column idempotently (old DBs open cleanly, no data loss).
@@ -1126,16 +1132,20 @@ export function createStore(path) {
11261132
const now = nowIso();
11271133
const policyEpoch = Number.isInteger(run.policy_epoch) ? run.policy_epoch : 0;
11281134
const runType = run.run_type ?? "auto";
1135+
// Issue #104:degraded 轮的跳过明细(JSON)。与 decisions/outcome 同为可选
1136+
// 载荷,未提供时落 NULL(而不是 "[]"),审计行只记真实发生过的跳过。
1137+
const skipped = run.skipped !== undefined ? JSON.stringify(run.skipped) : null;
11291138
db.prepare(
11301139
`INSERT INTO dream_runs (id, created_at, status, error, provider, model, snapshot_hash,
1131-
input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type)
1132-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1140+
input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type, skipped)
1141+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11331142
ON CONFLICT(id) DO UPDATE SET
11341143
created_at=excluded.created_at, status=excluded.status, error=excluded.error,
11351144
provider=excluded.provider, model=excluded.model, snapshot_hash=excluded.snapshot_hash,
11361145
input_count=excluded.input_count, input=excluded.input, decisions=excluded.decisions,
11371146
outcome=excluded.outcome, applied=excluded.applied, summary_stored=excluded.summary_stored,
1138-
receipt=excluded.receipt, policy_epoch=excluded.policy_epoch, run_type=excluded.run_type`
1147+
receipt=excluded.receipt, policy_epoch=excluded.policy_epoch, run_type=excluded.run_type,
1148+
skipped=excluded.skipped`
11391149
).run(
11401150
id,
11411151
run.created_at ?? now,
@@ -1152,7 +1162,8 @@ export function createStore(path) {
11521162
run.summary_stored ? 1 : 0,
11531163
run.receipt,
11541164
policyEpoch,
1155-
runType
1165+
runType,
1166+
skipped
11561167
);
11571168
return getDreamRun(id);
11581169
}

‎dsh-mneme/src/dream.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,12 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
657657
outcome: result.outcome,
658658
applied,
659659
summary_stored: summaryStored,
660-
receipt
660+
receipt,
661+
// Issue #104:degraded(合法子集已应用)轮被跳过的决策明细。写成独立字段
662+
// 而非内联进 decisions——degraded 的 decisions 是合法子集,下游按「决策
663+
// 数组」消费,内联标记会污染其它读者(失败轮的 _validationFailed 是整单
664+
// 拒绝哨兵,语义与「部分应用」不同,也不复用)。
665+
skipped: result.skipped
661666
});
662667
} catch (error) {
663668
logger?.warn?.(`dsh-mneme dream: failed to record audit run: ${String(error)}`);
@@ -978,7 +983,10 @@ export function createDreamScheduler({ onRun, thresholdCount = 10, thresholdChar
978983
conflicts,
979984
failures,
980985
frozen: frozenCount,
981-
summary: summaryStored
986+
summary: summaryStored,
987+
// Issue #104:只有真发生跳过时才落库(degraded 也可能仅因 summary 为空),
988+
// ok 轮留 NULL,避免「空数组」与「无此字段」两种假明细占据审计行。
989+
skipped: skippedInvalid ? skipped : undefined
982990
});
983991
}
984992

‎dsh-mneme/src/store.js‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ CREATE TABLE IF NOT EXISTS dream_runs (
4343
summary_stored INTEGER NOT NULL DEFAULT 0,
4444
receipt TEXT NOT NULL,
4545
policy_epoch INTEGER NOT NULL DEFAULT 0, -- 裁决规则版本:规则升级后旧裁决降级为历史证据
46-
run_type TEXT NOT NULL DEFAULT 'auto' -- auto | sleep:睡眠周期的审计区分
46+
run_type TEXT NOT NULL DEFAULT 'auto', -- auto | sleep:睡眠周期的审计区分
47+
skipped TEXT -- JSON: degraded 轮被跳过的逐条明细(index/action/ids/error)
4748
);
4849
CREATE INDEX IF NOT EXISTS idx_dream_runs_created ON dream_runs(created_at);
4950
@@ -371,7 +372,9 @@ function toDreamRun(row) {
371372
summary_stored: row.summary_stored === 1,
372373
receipt: row.receipt,
373374
policy_epoch: row.policy_epoch ?? 0,
374-
run_type: row.run_type ?? "auto"
375+
run_type: row.run_type ?? "auto",
376+
// Issue #104:degraded 轮的逐条跳过明细(JSON 列,NULL = 该轮无跳过项)。
377+
skipped: row.skipped ? JSON.parse(row.skipped) : undefined
375378
};
376379
}
377380

@@ -597,6 +600,9 @@ export function createStore(path) {
597600
// Legacy dream_runs without policy_epoch → backfill with the default epoch.
598601
addColumn("dream_runs", "policy_epoch", "ALTER TABLE dream_runs ADD COLUMN policy_epoch INTEGER NOT NULL DEFAULT 0");
599602
addColumn("dream_runs", "run_type", "ALTER TABLE dream_runs ADD COLUMN run_type TEXT NOT NULL DEFAULT 'auto'");
603+
// Issue #104:degraded(合法子集已应用)轮被跳过的决策明细。此前只进 logger.warn,
604+
// 离线回放 dream_runs 无法定位 degraded 成因(跨类型 merge / update 保护期 / unknown id)。
605+
addColumn("dream_runs", "skipped", "ALTER TABLE dream_runs ADD COLUMN skipped TEXT");
600606

601607
// Legacy mirror_state without v0.3.6 generation columns → add each missing
602608
// column idempotently (old DBs open cleanly, no data loss).
@@ -1126,16 +1132,20 @@ export function createStore(path) {
11261132
const now = nowIso();
11271133
const policyEpoch = Number.isInteger(run.policy_epoch) ? run.policy_epoch : 0;
11281134
const runType = run.run_type ?? "auto";
1135+
// Issue #104:degraded 轮的跳过明细(JSON)。与 decisions/outcome 同为可选
1136+
// 载荷,未提供时落 NULL(而不是 "[]"),审计行只记真实发生过的跳过。
1137+
const skipped = run.skipped !== undefined ? JSON.stringify(run.skipped) : null;
11291138
db.prepare(
11301139
`INSERT INTO dream_runs (id, created_at, status, error, provider, model, snapshot_hash,
1131-
input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type)
1132-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
1140+
input_count, input, decisions, outcome, applied, summary_stored, receipt, policy_epoch, run_type, skipped)
1141+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11331142
ON CONFLICT(id) DO UPDATE SET
11341143
created_at=excluded.created_at, status=excluded.status, error=excluded.error,
11351144
provider=excluded.provider, model=excluded.model, snapshot_hash=excluded.snapshot_hash,
11361145
input_count=excluded.input_count, input=excluded.input, decisions=excluded.decisions,
11371146
outcome=excluded.outcome, applied=excluded.applied, summary_stored=excluded.summary_stored,
1138-
receipt=excluded.receipt, policy_epoch=excluded.policy_epoch, run_type=excluded.run_type`
1147+
receipt=excluded.receipt, policy_epoch=excluded.policy_epoch, run_type=excluded.run_type,
1148+
skipped=excluded.skipped`
11391149
).run(
11401150
id,
11411151
run.created_at ?? now,
@@ -1152,7 +1162,8 @@ export function createStore(path) {
11521162
run.summary_stored ? 1 : 0,
11531163
run.receipt,
11541164
policyEpoch,
1155-
runType
1165+
runType,
1166+
skipped
11561167
);
11571168
return getDreamRun(id);
11581169
}

‎dsh-mneme/test/dream.test.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,49 @@ test("issue#89: dream run with a skipped invalid decision lands the valid subset
11091109
store.close();
11101110
});
11111111

1112+
// Issue #104(跟进 #137 失败路径):degraded(合法子集已应用)轮的跳过明细此前只进
1113+
// logger.warn,splice 之后就地从 decisions 里消失,落库的已是幸存列表——离线回放
1114+
// dream_runs 无法判断被跳的是跨类型 merge、update 保护期还是 unknown id。这里断言
1115+
// 明细进了独立的 skipped 列(而非内联进 decisions),且携带可定位的逐条原因。
1116+
test("issue#104: degraded run persists the skipped decision detail into dream_runs.skipped", async () => {
1117+
const { store, service } = dreamSetup();
1118+
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;
1119+
const b = service.saveWithDedupe({ type: "project", title: "旧B", content: "过时B" }).memory;
1120+
const pref = service.saveWithDedupe({ type: "preference", title: "语言", content: "中文" }).memory;
1121+
const c = service.saveWithDedupe({ type: "project", title: "旧C", content: "过时C" }).memory;
1122+
const ctx = {
1123+
logger: { warn: () => {}, info: () => {} },
1124+
llm: {
1125+
async *stream(options) {
1126+
const userText = options.messages.find((m) => m.role === "user")?.content?.[0]?.text ?? "";
1127+
if (userText.startsWith("id=")) {
1128+
// 一条跨类型 merge(非法 → 跳过)+ 两条合法 archive(显式覆盖 2/4,过 50% 下限)
1129+
yield { type: "text-delta", index: 0, text: JSON.stringify([
1130+
{ action: "merge", ids: [pref.id, a.id], keepSource: pref.id, title: "跨类型", content: "不应合并", importance: 4 },
1131+
{ action: "archive", ids: [b.id], reason: "stale" },
1132+
{ action: "archive", ids: [c.id], reason: "stale" }
1133+
]) };
1134+
} else {
1135+
yield { type: "text-delta", index: 0, text: "记忆库总览:degraded 轮的跳过明细可从审计行回放。" };
1136+
}
1137+
yield { type: "finish", reason: { kind: "stop" } };
1138+
}
1139+
}
1140+
};
1141+
const dream = createDreamScheduler({ thresholdCount: 1, thresholdChars: 0, delayMs: 0 });
1142+
const result = await dream.runDream(ctx, service, { dreamProvider: "mock", dreamModel: "mock-model" });
1143+
assert.equal(result.status, "degraded", "run degrades on the skipped decision");
1144+
// 落库验证:skipped 列不再是 NULL,逐条带 index/action/error,事后无需重跑即可定位成因。
1145+
const run = store.getDreamRun(result.runId);
1146+
assert.ok(Array.isArray(run?.skipped) && run.skipped.length === 1, "skipped detail persisted in dream_runs.skipped");
1147+
assert.equal(run.skipped[0].index, 0, "the skipped entry is the cross-type merge");
1148+
assert.equal(run.skipped[0].action, "merge");
1149+
assert.match(run.skipped[0].error, /multiple types/, "per-item skip reason is kept");
1150+
// 独立字段,不污染按「决策数组」消费 decisions 的下游读者。
1151+
assert.ok(!run.decisions.some((d) => d && d._skipped !== undefined), "decisions stays free of inline markers");
1152+
store.close();
1153+
});
1154+
11121155
test("issue#89: dreamSkipInvalid:false restores the whole-batch strict rejection", async () => {
11131156
const { store, service } = dreamSetup();
11141157
const a = service.saveWithDedupe({ type: "project", title: "旧A", content: "过时A" }).memory;

0 commit comments

Comments
 (0)