Skip to content

Commit e99241f

Browse files
committed
fix: v0.6.0 会话生命周期 code-review 修复(kimi-k2.7-code 审查 #1-#4)
- listBySession 默认过滤 session_disposed_at,includeDisposed 可显示全部(#1) - service.listBySession 透传 includeDisposed(#2) - toApiList 带 disposed 标记,restore 不再盲操作(#3) - 补 (session_id, session_disposed_at) 复合索引,置于 addColumn 迁移后(#4)
1 parent 3ed6413 commit e99241f

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎dsh-mneme/src/service.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,9 @@ export function createService({ store, mirror, config, onWrite, logger }) {
10021002
// session_id is optional on the wire: only carry it when present, so the
10031003
// DTO stays a lossless JSON object (undefined would vanish on serialize).
10041004
...(m.session_id != null ? { session_id: m.session_id } : {}),
1005+
// Disposed state rides along when set, so a restore flow is not a blind
1006+
// op — the caller can see which entries are hidden before restoreBySession.
1007+
...(m.session_disposed_at != null ? { disposed: true } : {}),
10051008
created_at: m.created_at,
10061009
updated_at: m.updated_at
10071010
}));
@@ -1363,7 +1366,7 @@ export function createService({ store, mirror, config, onWrite, logger }) {
13631366
}
13641367
return { restored };
13651368
},
1366-
listBySession: (sessionId) => toApiList(store.listBySession(sessionId)),
1369+
listBySession: (sessionId, opts = {}) => toApiList(store.listBySession(sessionId, opts)),
13671370
update: (id, p, ctx = {}) => {
13681371
const old = store.getById(id);
13691372
const updated = store.update(id, p);

‎dsh-mneme/src/store.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ export function createStore(path) {
578578
addColumn("memories", "quality_score", "ALTER TABLE memories ADD COLUMN quality_score REAL");
579579
addColumn("memories", "session_id", "ALTER TABLE memories ADD COLUMN session_id TEXT");
580580

581+
// Composite index for session-lifecycle queries (dispose/restore/listBySession).
582+
// Created post-migration, NOT in SCHEMA: on legacy DBs both columns arrive via
583+
// ADD COLUMN above, so the index would fail at db.exec(SCHEMA) time. CREATE
584+
// INDEX IF NOT EXISTS is atomic, so the two-process race is safe here.
585+
db.exec("CREATE INDEX IF NOT EXISTS idx_memories_session ON memories(session_id, session_disposed_at)");
586+
581587
// Legacy dream_runs without policy_epoch → backfill with the default epoch.
582588
addColumn("dream_runs", "policy_epoch", "ALTER TABLE dream_runs ADD COLUMN policy_epoch INTEGER NOT NULL DEFAULT 0");
583589
addColumn("dream_runs", "run_type", "ALTER TABLE dream_runs ADD COLUMN run_type TEXT NOT NULL DEFAULT 'auto'");
@@ -832,9 +838,13 @@ export function createStore(path) {
832838
// marks entries hidden because the session they were born in was deleted
833839
// (a reversible "undo" — restoreBySession clears it). They never clobber each
834840
// other: restoreBySession must not resurrect user-archived memories.
835-
function listBySession(sessionId) {
841+
// Mirrors list/search: disposed rows are hidden by default. A consumer that
842+
// needs to see the full picture (e.g. a restore flow that tells the user
843+
// "these N entries were hidden") opts in via includeDisposed.
844+
function listBySession(sessionId, { includeDisposed = false } = {}) {
845+
const disposedFilter = includeDisposed ? "" : "AND session_disposed_at IS NULL";
836846
const rows = db.prepare(
837-
"SELECT * FROM memories WHERE session_id = ? ORDER BY updated_at DESC"
847+
`SELECT * FROM memories WHERE session_id = ? ${disposedFilter} ORDER BY updated_at DESC`
838848
).all(sessionId);
839849
return rows.map(toRow);
840850
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,21 @@ test("listBySession returns only that session's memories via wire DTOs", () => {
415415
assert.equal(rows[0].title, "会话决策");
416416
assert.equal(rows[0].session_id, "sess-7");
417417
});
418+
419+
test("listBySession DTO hides disposed by default and flags it when included", () => {
420+
const store = createStore(":memory:");
421+
const svc = createService({ store, mirror: null, config: {} });
422+
const { memory } = svc.saveWithDedupe({ type: "decision", title: "会话决策", content: "d", session_id: "sess-9" });
423+
svc.disposeBySession("sess-9");
424+
425+
assert.equal(svc.listBySession("sess-9").length, 0, "disposed hidden by default");
426+
const visible = svc.listBySession("sess-9", { includeDisposed: true });
427+
assert.equal(visible.length, 1);
428+
assert.equal(visible[0].id, memory.id);
429+
assert.equal(visible[0].disposed, true, "DTO carries disposed marker so restore is not blind");
430+
431+
svc.restoreBySession("sess-9");
432+
const restored = svc.listBySession("sess-9");
433+
assert.equal(restored.length, 1, "restore brings it back to default view");
434+
assert.equal(restored[0].disposed, undefined, "marker cleared after restore");
435+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,14 @@ test("setDisposedBySession round-trips and listBySession sees all states", () =>
423423
assert.equal(bySession.length, 1);
424424
assert.equal(bySession[0].id, a.id);
425425
});
426+
427+
test("listBySession hides disposed by default, includeDisposed opts in", () => {
428+
const store = createStore(":memory:");
429+
const a = store.save({ type: "decision", title: "会话记忆", content: "c", session_id: "sess-y" });
430+
store.setDisposedBySession("sess-y", true);
431+
432+
assert.equal(store.listBySession("sess-y").length, 0, "disposed row hidden by default");
433+
const all = store.listBySession("sess-y", { includeDisposed: true });
434+
assert.equal(all.length, 1, "includeDisposed reveals it");
435+
assert.equal(all[0].id, a.id);
436+
});

0 commit comments

Comments
 (0)