Skip to content

Commit 1496be2

Browse files
authored
Merge branch 'main' into feat/tool-exposure-switches
2 parents cbc607a + 360f7c8 commit 1496be2

8 files changed

Lines changed: 287 additions & 7 deletions

File tree

‎dsh-mneme/lib/service.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,8 @@ export function createService({ store, mirror, config, onWrite, logger }) {
21202120
toApiList,
21212121
isVisibleInScope,
21222122
transaction,
2123+
getDistillCursor: (sessionId) => store.getDistillCursor(sessionId),
2124+
setDistillCursor: (sessionId, lastSeq) => store.setDistillCursor(sessionId, lastSeq),
21232125
enqueue,
21242126
setDreamHook(fn) { dreamHook = fn; },
21252127
setSleepHook(fn) { sleepHook = fn; },

‎dsh-mneme/lib/store.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ CREATE TABLE IF NOT EXISTS llm_audit_logs (
197197
CREATE INDEX IF NOT EXISTS idx_llm_audit_timestamp ON llm_audit_logs(timestamp);
198198
CREATE INDEX IF NOT EXISTS idx_llm_audit_source ON llm_audit_logs(trigger_source);
199199
200+
-- autoSummarize 的增量蒸馏游标:按 session.id 持久化最近一次成功消费的事件序。
201+
-- 游标是蒸馏窗口的恢复事实,不与 user_settings 或记忆内容混用。
202+
CREATE TABLE IF NOT EXISTS distill_cursors (
203+
session_id TEXT PRIMARY KEY,
204+
last_seq INTEGER NOT NULL,
205+
updated_at TEXT NOT NULL
206+
);
207+
200208
-- entity gene (v0.3.0): named entities mentioned across memories, with
201209
-- time-boxed attributes (valid_from → valid_until) and typed relations.
202210
-- Attributes follow the snapshot style: saveAttr invalidates the previous
@@ -789,6 +797,37 @@ export function createStore(path) {
789797
return ts;
790798
}
791799

800+
/** 返回指定会话持久化的 autoSummarize 游标;不存在时返回 undefined。 */
801+
function getDistillCursor(sessionId) {
802+
if (typeof sessionId !== "string" || sessionId.length === 0) return undefined;
803+
const row = db.prepare(
804+
"SELECT session_id, last_seq, updated_at FROM distill_cursors WHERE session_id = ?"
805+
).get(sessionId);
806+
return row
807+
? { session_id: row.session_id, last_seq: Number(row.last_seq), updated_at: row.updated_at }
808+
: undefined;
809+
}
810+
811+
/**
812+
* 单调持久化会话游标。调用方若同时写入记忆,必须放在外层 SQLite 事务内。
813+
*/
814+
function setDistillCursor(sessionId, lastSeq) {
815+
if (typeof sessionId !== "string" || sessionId.length === 0) {
816+
throw new TypeError("setDistillCursor: sessionId must be a non-empty string");
817+
}
818+
if (!Number.isSafeInteger(lastSeq)) {
819+
throw new TypeError("setDistillCursor: lastSeq must be a safe integer");
820+
}
821+
db.prepare(`
822+
INSERT INTO distill_cursors (session_id, last_seq, updated_at)
823+
VALUES (?, ?, ?)
824+
ON CONFLICT(session_id) DO UPDATE SET
825+
last_seq = MAX(distill_cursors.last_seq, excluded.last_seq),
826+
updated_at = excluded.updated_at
827+
`).run(sessionId, lastSeq, nowIso());
828+
return getDistillCursor(sessionId);
829+
}
830+
792831
function count(type, { minImportance = null, source = null, includeForgotten = false, includeArchived = false, onlyArchived = false, depositedOnly = false, updatedFrom = null, updatedTo = null, occurredFrom = null, occurredTo = null, visibility = null } = {}) {
793832
const clauses = [];
794833
const params = [];
@@ -2461,6 +2500,8 @@ export function createStore(path) {
24612500
getRecallEval,
24622501
listRecallEvals,
24632502
saveLlmAudit,
2503+
getDistillCursor,
2504+
setDistillCursor,
24642505
listLlmAudits,
24652506
countLlmAudits,
24662507
getLlmAuditStats,

‎dsh-mneme/lib/summarize.js‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,19 @@ export function createSummarizer(ctx, service, config, deps = {}) {
405405
}
406406
}
407407

408+
function persistCursor(sessionId, nextSeq) {
409+
if (!Number.isFinite(nextSeq)) return;
410+
if (typeof service.setDistillCursor !== "function") {
411+
throw new Error("dsh-mneme: persistent summarization cursors are unavailable");
412+
}
413+
service.setDistillCursor(sessionId, nextSeq);
414+
}
415+
416+
function commitCursor(sessionId, nextSeq) {
417+
persistCursor(sessionId, nextSeq);
418+
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(sessionId, nextSeq);
419+
}
420+
408421
/**
409422
* Issue #239(第 4 项):把这一轮蒸馏推迟到最近的「高峰结束」时刻。每会话只挂
410423
* 一个定时器(重复触发不叠加);被 summarizePeakMaxDeferMinutes 截断时到点照跑
@@ -500,7 +513,10 @@ export function createSummarizer(ctx, service, config, deps = {}) {
500513
let abortedRun = false;
501514
try {
502515
if (!route) return;
503-
const previousSeq = lastDistilledSeq.get(session.id);
516+
const persistedCursor = typeof service.getDistillCursor === "function"
517+
? service.getDistillCursor(session.id)
518+
: undefined;
519+
const previousSeq = persistedCursor?.last_seq ?? lastDistilledSeq.get(session.id);
504520
const triggerSeq = eventSeq(triggerEvent);
505521
// 只读取上次成功游标之后、当前 turn/end 之前的事件。旧版 snapshotEvents
506522
// 即使忽略范围参数,collectMessages 仍会按事件 seq 二次过滤。
@@ -519,7 +535,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
519535
if (!collected.messages.length) {
520536
// 没有可蒸馏的公开文本也算成功消费当前事件窗口,避免每个 turn/end
521537
// 都重新扫描同一批无内容事件;没有 seq 时则不提交不可验证的游标。
522-
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
538+
commitCursor(session.id, nextSeq);
523539
return;
524540
}
525541
const messages = collected.messages;
@@ -536,7 +552,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
536552
0
537553
);
538554
if (distillChars < minWindowChars) {
539-
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
555+
commitCursor(session.id, nextSeq);
540556
writeAudit({
541557
timestamp: new Date().toISOString(),
542558
model_id: route ? `${route.provider}:${route.model}` : "unknown",
@@ -712,7 +728,11 @@ export function createSummarizer(ctx, service, config, deps = {}) {
712728
...(dup ? { _mergeInto: dup.memory.id } : {})
713729
});
714730
}
731+
persistCursor(session.id, nextSeq);
715732
});
733+
} else {
734+
// 空数组是合法成功:没有记忆写入,但本次事件窗口仍然应被持久消费。
735+
persistCursor(session.id, nextSeq);
716736
}
717737
if (audit && (capped > 0 || deduped > 0)) {
718738
audit.metadata = {
@@ -724,6 +744,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
724744
// 记忆写入和解析都成功后才提交窗口;流失败、中止、解析失败或写入异常
725745
// 都会在此之前退出,从而保留窗口供下一次重试。
726746
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
747+
} catch (error) {
748+
if (audit) {
749+
audit.status = "error";
750+
audit.errorMessage = String(error?.message ?? error);
751+
}
752+
throw error;
727753
} finally {
728754
// Issue #127:aborted(会话关闭 / 插件 dispose)不占间隔,避免误伤该会话的
729755
// 下一次蒸馏;其余情况(含失败)的打点保留,与 dreamMinIntervalMinutes 一致。

‎dsh-mneme/src/service.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,8 @@ export function createService({ store, mirror, config, onWrite, logger }) {
21202120
toApiList,
21212121
isVisibleInScope,
21222122
transaction,
2123+
getDistillCursor: (sessionId) => store.getDistillCursor(sessionId),
2124+
setDistillCursor: (sessionId, lastSeq) => store.setDistillCursor(sessionId, lastSeq),
21232125
enqueue,
21242126
setDreamHook(fn) { dreamHook = fn; },
21252127
setSleepHook(fn) { sleepHook = fn; },

‎dsh-mneme/src/store.js‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ CREATE TABLE IF NOT EXISTS llm_audit_logs (
197197
CREATE INDEX IF NOT EXISTS idx_llm_audit_timestamp ON llm_audit_logs(timestamp);
198198
CREATE INDEX IF NOT EXISTS idx_llm_audit_source ON llm_audit_logs(trigger_source);
199199
200+
-- autoSummarize 的增量蒸馏游标:按 session.id 持久化最近一次成功消费的事件序。
201+
-- 游标是蒸馏窗口的恢复事实,不与 user_settings 或记忆内容混用。
202+
CREATE TABLE IF NOT EXISTS distill_cursors (
203+
session_id TEXT PRIMARY KEY,
204+
last_seq INTEGER NOT NULL,
205+
updated_at TEXT NOT NULL
206+
);
207+
200208
-- entity gene (v0.3.0): named entities mentioned across memories, with
201209
-- time-boxed attributes (valid_from → valid_until) and typed relations.
202210
-- Attributes follow the snapshot style: saveAttr invalidates the previous
@@ -789,6 +797,37 @@ export function createStore(path) {
789797
return ts;
790798
}
791799

800+
/** 返回指定会话持久化的 autoSummarize 游标;不存在时返回 undefined。 */
801+
function getDistillCursor(sessionId) {
802+
if (typeof sessionId !== "string" || sessionId.length === 0) return undefined;
803+
const row = db.prepare(
804+
"SELECT session_id, last_seq, updated_at FROM distill_cursors WHERE session_id = ?"
805+
).get(sessionId);
806+
return row
807+
? { session_id: row.session_id, last_seq: Number(row.last_seq), updated_at: row.updated_at }
808+
: undefined;
809+
}
810+
811+
/**
812+
* 单调持久化会话游标。调用方若同时写入记忆,必须放在外层 SQLite 事务内。
813+
*/
814+
function setDistillCursor(sessionId, lastSeq) {
815+
if (typeof sessionId !== "string" || sessionId.length === 0) {
816+
throw new TypeError("setDistillCursor: sessionId must be a non-empty string");
817+
}
818+
if (!Number.isSafeInteger(lastSeq)) {
819+
throw new TypeError("setDistillCursor: lastSeq must be a safe integer");
820+
}
821+
db.prepare(`
822+
INSERT INTO distill_cursors (session_id, last_seq, updated_at)
823+
VALUES (?, ?, ?)
824+
ON CONFLICT(session_id) DO UPDATE SET
825+
last_seq = MAX(distill_cursors.last_seq, excluded.last_seq),
826+
updated_at = excluded.updated_at
827+
`).run(sessionId, lastSeq, nowIso());
828+
return getDistillCursor(sessionId);
829+
}
830+
792831
function count(type, { minImportance = null, source = null, includeForgotten = false, includeArchived = false, onlyArchived = false, depositedOnly = false, updatedFrom = null, updatedTo = null, occurredFrom = null, occurredTo = null, visibility = null } = {}) {
793832
const clauses = [];
794833
const params = [];
@@ -2461,6 +2500,8 @@ export function createStore(path) {
24612500
getRecallEval,
24622501
listRecallEvals,
24632502
saveLlmAudit,
2503+
getDistillCursor,
2504+
setDistillCursor,
24642505
listLlmAudits,
24652506
countLlmAudits,
24662507
getLlmAuditStats,

‎dsh-mneme/src/summarize.js‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,19 @@ export function createSummarizer(ctx, service, config, deps = {}) {
405405
}
406406
}
407407

408+
function persistCursor(sessionId, nextSeq) {
409+
if (!Number.isFinite(nextSeq)) return;
410+
if (typeof service.setDistillCursor !== "function") {
411+
throw new Error("dsh-mneme: persistent summarization cursors are unavailable");
412+
}
413+
service.setDistillCursor(sessionId, nextSeq);
414+
}
415+
416+
function commitCursor(sessionId, nextSeq) {
417+
persistCursor(sessionId, nextSeq);
418+
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(sessionId, nextSeq);
419+
}
420+
408421
/**
409422
* Issue #239(第 4 项):把这一轮蒸馏推迟到最近的「高峰结束」时刻。每会话只挂
410423
* 一个定时器(重复触发不叠加);被 summarizePeakMaxDeferMinutes 截断时到点照跑
@@ -500,7 +513,10 @@ export function createSummarizer(ctx, service, config, deps = {}) {
500513
let abortedRun = false;
501514
try {
502515
if (!route) return;
503-
const previousSeq = lastDistilledSeq.get(session.id);
516+
const persistedCursor = typeof service.getDistillCursor === "function"
517+
? service.getDistillCursor(session.id)
518+
: undefined;
519+
const previousSeq = persistedCursor?.last_seq ?? lastDistilledSeq.get(session.id);
504520
const triggerSeq = eventSeq(triggerEvent);
505521
// 只读取上次成功游标之后、当前 turn/end 之前的事件。旧版 snapshotEvents
506522
// 即使忽略范围参数,collectMessages 仍会按事件 seq 二次过滤。
@@ -519,7 +535,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
519535
if (!collected.messages.length) {
520536
// 没有可蒸馏的公开文本也算成功消费当前事件窗口,避免每个 turn/end
521537
// 都重新扫描同一批无内容事件;没有 seq 时则不提交不可验证的游标。
522-
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
538+
commitCursor(session.id, nextSeq);
523539
return;
524540
}
525541
const messages = collected.messages;
@@ -536,7 +552,7 @@ export function createSummarizer(ctx, service, config, deps = {}) {
536552
0
537553
);
538554
if (distillChars < minWindowChars) {
539-
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
555+
commitCursor(session.id, nextSeq);
540556
writeAudit({
541557
timestamp: new Date().toISOString(),
542558
model_id: route ? `${route.provider}:${route.model}` : "unknown",
@@ -712,7 +728,11 @@ export function createSummarizer(ctx, service, config, deps = {}) {
712728
...(dup ? { _mergeInto: dup.memory.id } : {})
713729
});
714730
}
731+
persistCursor(session.id, nextSeq);
715732
});
733+
} else {
734+
// 空数组是合法成功:没有记忆写入,但本次事件窗口仍然应被持久消费。
735+
persistCursor(session.id, nextSeq);
716736
}
717737
if (audit && (capped > 0 || deduped > 0)) {
718738
audit.metadata = {
@@ -724,6 +744,12 @@ export function createSummarizer(ctx, service, config, deps = {}) {
724744
// 记忆写入和解析都成功后才提交窗口;流失败、中止、解析失败或写入异常
725745
// 都会在此之前退出,从而保留窗口供下一次重试。
726746
if (Number.isFinite(nextSeq)) lastDistilledSeq.set(session.id, nextSeq);
747+
} catch (error) {
748+
if (audit) {
749+
audit.status = "error";
750+
audit.errorMessage = String(error?.message ?? error);
751+
}
752+
throw error;
727753
} finally {
728754
// Issue #127:aborted(会话关闭 / 插件 dispose)不占间隔,避免误伤该会话的
729755
// 下一次蒸馏;其余情况(含失败)的打点保留,与 dreamMinIntervalMinutes 一致。

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ test("createStore initializes schema and opens db", () => {
1616
"SELECT name FROM sqlite_master WHERE type='table' AND name='memories'"
1717
).get();
1818
assert.ok(row, "memories table exists");
19+
const cursorTable = store.db.prepare(
20+
"SELECT name FROM sqlite_master WHERE type='table' AND name='distill_cursors'"
21+
).get();
22+
assert.ok(cursorTable, "distill cursor table exists");
23+
assert.equal(store.getDistillCursor("session-1"), undefined);
24+
const saved = store.setDistillCursor("session-1", 4);
25+
assert.equal(saved.last_seq, 4);
26+
assert.ok(saved.updated_at);
27+
assert.equal(store.setDistillCursor("session-1", 2).last_seq, 4, "cursor never moves backwards");
1928
store.close();
2029
});
2130

@@ -173,6 +182,10 @@ test("schema migration adds archived column to legacy database", () => {
173182
const store = createStore(dbPath);
174183
const cols = store.db.prepare("PRAGMA table_info(memories)").all().map((c) => c.name);
175184
assert.ok(cols.includes("archived"), "archived column added");
185+
const cursorTable = store.db.prepare(
186+
"SELECT name FROM sqlite_master WHERE type='table' AND name='distill_cursors'"
187+
).get();
188+
assert.ok(cursorTable, "new cursor table is created for legacy databases");
176189
store.close();
177190
} finally {
178191
rmSync(dir, { recursive: true, force: true });

0 commit comments

Comments
 (0)