dsh-lm-memory 0.19.3 → 0.19.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/server/viewer.mjs CHANGED
@@ -94,6 +94,9 @@ const saveConflictReview = (oldR, newR, verdict) => {
94
94
  } catch {}
95
95
  };
96
96
  // 带缓存的复核:返回 { total, needAction, pairs:[{...p, verdict}] };llm 不可用时 verdict 全为 null(未复核)
97
+ //裁决接口的重校验数据源:与列表页同一套规则(不触发模型复核,只取规则候选)
98
+ const conflictCandidatesCurrent = () => findConflictPairs(ro(), { maxPairs: 20 }).pairs;
99
+
97
100
  async function conflictCandidates() {
98
101
  const r = findConflictPairs(ro(), { maxPairs: 20 });
99
102
  const pairs = r.pairs.map((p) => ({ ...p, verdict: conflictReviewVerdict(p.old, p.new) }));
@@ -102,12 +105,14 @@ async function conflictCandidates() {
102
105
  const raw = await llm(buildReviewPrompt(pending));
103
106
  const verdicts = parseReview(raw, pending.length);
104
107
  if (verdicts) {
105
- verdicts.forEach((v, idx) => {
106
- const p = pending[idx];
107
- if (!p) return;
108
+ // v.i 是模型答的候选下标(buildReviewPrompt 按 pending 顺序编号),
109
+ // 必须用 v.i 定位;用遍历下标会在模型漏答时把判定错挂到别的对上。
110
+ for (const v of verdicts) {
111
+ const p = pending[v.i];
112
+ if (!p || p.verdict !== null) continue;
108
113
  p.verdict = v.verdict;
109
114
  saveConflictReview(p.old, p.new, v.verdict);
110
- });
115
+ }
111
116
  // 未拿到判定的残余(模型漏答)保持 null
112
117
  }
113
118
  }
@@ -612,6 +617,10 @@ const tailLines = (file, n) => {
612
617
  try { return fs.readFileSync(file, "utf8").trim().split("\n").slice(-n); } catch { return []; }
613
618
  };
614
619
  const parseLines = (lines) => lines.flatMap((line) => { try { return [JSON.parse(line)]; } catch { return []; } });
620
+ // viewer 自己的审计条目(与 memory-server 的 log.jsonl 同一文件、同一形态)
621
+ const auditViewer = (op, data = {}) => {
622
+ try { fs.appendFileSync(path.join(MEM_DIR, "log.jsonl"), JSON.stringify({ ts: new Date().toISOString(), op, by: "viewer", ...data }) + "\n"); } catch {}
623
+ };
615
624
 
616
625
  http.createServer(async (req, res) => {
617
626
  const json = (d, code = 200) => { res.writeHead(code, { "Content-Type": "application/json; charset=utf-8" }); res.end(JSON.stringify(d)); };
@@ -724,19 +733,38 @@ http.createServer(async (req, res) => {
724
733
  return json({ ok: String(r).includes("OK"), msg: String(r).slice(0, 80) });
725
734
  }
726
735
  if (u.pathname === "/api/conflict/resolve") {
736
+ // v0.19.4 复审加固:判定白名单、候选对重校验、乐观并发、写后审计。
737
+ const VERDICTS = { adopt: "superseded", keep: "related", unrelated: "unrelated" };
738
+ const verdict = VERDICTS[body.verdict];
739
+ if (!verdict) return json({ ok: false, msg: "verdict 必须是 adopt / keep / unrelated" }, 400);
727
740
  const oldId = Number(body.old), newId = Number(body.new);
728
- const oldRow = ro().prepare("SELECT id, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(oldId);
729
- const newRow = ro().prepare("SELECT id, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(newId);
730
- if (!oldRow || !newRow) return json({ ok: false, msg: "候选对已变化,请刷新后重试" });
731
- if (body.verdict === "adopt") {
732
- // 旧说法被接棒:逻辑删除旧条(保留审计),并把这对记为 superseded
733
- const r = await call("memory_forget", { id: oldId, reason: "conflict-adopt" });
734
- saveConflictReview(oldRow, newRow, "superseded");
735
- return json({ ok: String(r).includes("OK"), msg: "已采纳新说法,旧条转为历史(可审计)" });
741
+ if (!Number.isSafeInteger(oldId) || !Number.isSafeInteger(newId) || oldId === newId)
742
+ return json({ ok: false, msg: "old/new 必须是不同的记忆 id" }, 400);
743
+ const candidate = conflictCandidatesCurrent().find((p) =>
744
+ (p.old.id === oldId && p.new.id === newId) || (p.old.id === newId && p.new.id === oldId));
745
+ if (!candidate) return json({ ok: false, msg: "这对已不在待裁决列表(可能已裁决或库已变化),请刷新" }, 409);
746
+ const oldRow = ro().prepare("SELECT id, version, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(candidate.old.id);
747
+ const newRow = ro().prepare("SELECT id, version, kind, content, updated_at FROM memories WHERE id = ? AND status = 'active'").get(candidate.new.id);
748
+ if (!oldRow || !newRow) return json({ ok: false, msg: "候选对已变化,请刷新后重试" }, 409);
749
+ const expectOld = Number(body.expected_old_version), expectNew = Number(body.expected_new_version);
750
+ if (!Number.isSafeInteger(expectOld) || !Number.isSafeInteger(expectNew))
751
+ return json({ ok: false, msg: '缺少 expected_old_version / expected_new_version' }, 400);
752
+ if (expectOld !== oldRow.version)
753
+ return json({ ok: false, msg: `旧条版本已变化(当前 v${oldRow.version}),请刷新后重试` }, 409);
754
+ if (expectNew !== newRow.version)
755
+ return json({ ok: false, msg: `新条版本已变化(当前 v${newRow.version}),请刷新后重试` }, 409);
756
+ if (verdict === "superseded") {
757
+ // 版本校验下沉:expected_version 由 memory_forget 在写锁事务内原子比对;
758
+ // 版本冲突对客户端是 409(0.19.6 外审:失败不能伪装成 200)
759
+ const r = await call("memory_forget", { id: oldRow.id, reason: "conflict-adopt", expected_version: oldRow.version });
760
+ const msg = String(r).slice(0, 140);
761
+ if (!r.includes("OK")) return json({ ok: false, msg }, msg.startsWith("VERSION_CONFLICT") ? 409 : 502);
762
+ saveConflictReview(oldRow, newRow, verdict);
763
+ auditViewer("conflict_resolve", { old: oldRow.id, new: newRow.id, verdict: "superseded" });
764
+ return json({ ok: true, msg: "已采纳新说法,旧条转为历史(审计可查;默认召回不再返回)" });
736
765
  }
737
- // 保留旧说法 / 两条无关:只记录判定,不动任何内容
738
- const verdict = body.verdict === "keep" ? "related" : "unrelated";
739
766
  saveConflictReview(oldRow, newRow, verdict);
767
+ auditViewer("conflict_resolve", { old: oldRow.id, new: newRow.id, verdict });
740
768
  return json({ ok: true, msg: verdict === "related" ? "已记录:两条都保留" : "已记录:判为无关,不再提示" });
741
769
  }
742
770
  if (u.pathname === "/api/sleep") {
@@ -779,6 +807,8 @@ http.createServer(async (req, res) => {
779
807
  ts: l.ts, op: String(l.op ?? ""),
780
808
  by: String(l.by ?? "").replace("mcp:lm-memory", "mcp").replace("extract-helper", "helper"),
781
809
  detail: String(l.reason ?? l.error ?? l.sessionId ?? l.id ?? l.patch ?? "").slice(0, 70),
810
+ hard: l.hard === true || undefined, // 0.19.6 复审:审计需能还原删除方式与删除前状态
811
+ was: typeof l.was === "string" ? l.was : undefined,
782
812
  }));
783
813
  const opCount = {};
784
814
  for (const row of rows) opCount[row.op] = (opCount[row.op] ?? 0) + 1;
@@ -786,6 +816,8 @@ http.createServer(async (req, res) => {
786
816
  }
787
817
  json({ error: "not found" }, 404);
788
818
  } catch (e) { json({ error: String(e.message) }, 500); }
789
- }).listen(PORT, "127.0.0.1", () => {
790
- console.log(`lm-memory viewer → http://127.0.0.1:${PORT} (dir: ${MEM_DIR})`);
819
+ }).listen(PORT, "127.0.0.1", function () {
820
+ // PORT=0 时由 OS 分配实际端口(外审 P2-2:测试用动态端口,不再先占再放)
821
+ const actual = this.address().port;
822
+ console.log(`lm-memory viewer → http://127.0.0.1:${actual} (dir: ${MEM_DIR})`);
791
823
  });