sage-mem 2.15.2 → 2.15.4

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/README.en.md CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  **Cross-session memory for DeepSeek Harness — plain markdown files, no database, no worker.**
14
14
 
15
- **Current release: `2.15.2`** — the npm badge above is the dynamic source of truth. **Contract baseline: `1.0.0`** — the first stable release, whose public commitments, the capability boundaries and the list of things it explicitly cannot do are frozen in the
15
+ **Current release: `2.15.4`** — the npm badge above is the dynamic source of truth. **Contract baseline: `1.0.0`** — the first stable release, whose public commitments, the capability boundaries and the list of things it explicitly cannot do are frozen in the
16
16
  [frozen contract](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md).
17
17
 
18
18
  If it is useful to you, a Star is the most direct feedback.
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  > File-based cross-session memory for DeepSeek Harness — every memory is a plain markdown file you can open, edit and delete. No database, no worker, no port.
16
16
 
17
- **当前发布版本:`2.15.2`** —— 顶部 npm 徽章是动态权威。**契约基线:`1.0.0`** —— 第一个稳定版,公开承诺、能力边界与「明确做不到」的清单冻结在
17
+ **当前发布版本:`2.15.4`** —— 顶部 npm 徽章是动态权威。**契约基线:`1.0.0`** —— 第一个稳定版,公开承诺、能力边界与「明确做不到」的清单冻结在
18
18
  [契约冻结](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md)。
19
19
 
20
20
  如果它对你有用,给个 Star 是最直接的反馈。
package/lib/client.js CHANGED
@@ -4837,6 +4837,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
4837
4837
  "todo.notice.stale": "文件刚被别处改过,已重新加载;这次改动没保存",
4838
4838
  "todo.notice.degraded": "宿主还是升级前的版本:这次写入没有版本校验,请确认内容正确",
4839
4839
  "todo.notice.undo": "已撤销上一步",
4840
+ "todo.notice.undoStale": "文件刚被别处改过,撤销 / 重做已作废(已重新加载)",
4840
4841
  "todo.err.noRemote": "宿主没有记忆 remote,读不了待办文件",
4841
4842
  "todo.err.read": "读不到待办文件:{error}",
4842
4843
  "todo.err.write": "写入被拒:{error}",
@@ -5482,6 +5483,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
5482
5483
  "todo.notice.stale": "The file changed elsewhere; reloaded. This edit was not saved.",
5483
5484
  "todo.notice.degraded": "Host predates this version: the write had no revision check — verify the content.",
5484
5485
  "todo.notice.undo": "Reverted the last edit",
5486
+ "todo.notice.undoStale": "The file changed elsewhere, so undo / redo was cancelled (reloaded)",
5485
5487
  "todo.err.noRemote": "This host exposes no memory remote, so the to-do file cannot be read",
5486
5488
  "todo.err.read": "Cannot read the to-do file: {error}",
5487
5489
  "todo.err.write": "Write rejected: {error}",
@@ -9935,6 +9937,19 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
9935
9937
  try { s.setItem(key, value); } catch (e) { /* 偏好丢了不影响主流程 */ }
9936
9938
  }
9937
9939
 
9940
+ /**
9941
+ * 撤销 / 重做现在还能不能做(2.15.3)。
9942
+ *
9943
+ * 判据:盘上的 revision 必须仍是我们**自己写出的那一版**。否则中间有人(AI、另一个页签)
9944
+ * 改过文件,写回整份快照会把那些改动一起抹掉 —— 那比「撤销失败」严重得多。
9945
+ * 拿不到 revision(老宿主 / 读失败)时按旧行为放行:没有判据可用时,宁可让用户能退回一步。
9946
+ */
9947
+ function todoUndoAllowed(liveRevision, writtenRevision) {
9948
+ if (typeof writtenRevision !== "string" || writtenRevision === "") return true;
9949
+ if (typeof liveRevision !== "string" || liveRevision === "") return true;
9950
+ return liveRevision === writtenRevision;
9951
+ }
9952
+
9938
9953
  /** 撤销步数归一:非数字 / 越界都收进 [3, 5],坏值(含 null / 空串)回默认 5。 */
9939
9954
  function todoClampUndoDepth(n) {
9940
9955
  if (n === null || n === undefined || n === "") return TODO_UNDO_DEFAULT;
@@ -10616,6 +10631,10 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
10616
10631
  const sErr = react.useState(null); const err = sErr[0], setErr = sErr[1];
10617
10632
  const sNotice = react.useState(""); const notice = sNotice[0], setNotice = sNotice[1];
10618
10633
  const sBusy = react.useState(false); const busy = sBusy[0], setBusy = sBusy[1];
10634
+ // 「我们自己写出的那一版」的 revision:撤销 / 重做只在文件仍停在这一版时才允许,
10635
+ // 否则整份快照会连别人(AI、另一个页签)期间的改动一起抹掉(2.15.3)。
10636
+ const sWritten = react.useState(null);
10637
+ const writtenRev = sWritten[0], setWrittenRev = sWritten[1];
10619
10638
  // 撤销 / 重做:元素是写入前的 `{content, revision}`。栈**只在内存里**,
10620
10639
  // 深度 3–5 可配(偏好落 localStorage);任何**新**写入都清空 redoStack —— 历史分叉了。
10621
10640
  const sDepth = react.useState(function () { return todoClampUndoDepth(todoPrefGet(TODO_PREF_UNDO)); });
@@ -10729,6 +10748,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
10729
10748
  setErr(r.error || t("todo.err.write", { error: String(r.code || "error") }));
10730
10749
  return;
10731
10750
  }
10751
+ setWrittenRev(typeof r.revision === "string" ? r.revision : null);
10732
10752
  setUndoStack(function (prev) { return prev.concat([snapshot]).slice(-undoDepth); });
10733
10753
  setRedoStack([]);
10734
10754
  setNotice(out.degraded ? t("todo.notice.degraded") : label);
@@ -10746,6 +10766,12 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
10746
10766
  */
10747
10767
  function writeSnapshot(target, label, onDone) {
10748
10768
  if (!target || typeof target.content !== "string") return;
10769
+ // 2.15.3:盘上已经不是我们写出的那一版 → 撤销 / 重做作废(否则会抹掉别人的改动)。
10770
+ if (!todoUndoAllowed(doc.revision, writtenRev)) {
10771
+ setNotice(t("todo.notice.undoStale"));
10772
+ load(false);
10773
+ return;
10774
+ }
10749
10775
  const revision = doc.revision;
10750
10776
  setBusy(true); setErr(null); setNotice("");
10751
10777
  writeWithFallback(remote, "writeFile", [TODO_FILE, target.content], revision ? { expectedRevision: revision } : undefined, null)
@@ -10757,6 +10783,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
10757
10783
  return;
10758
10784
  }
10759
10785
  if (typeof onDone === "function") onDone();
10786
+ setWrittenRev(typeof r.revision === "string" ? r.revision : null);
10760
10787
  setNotice(label);
10761
10788
  return load(false);
10762
10789
  }, function (e) { setErr(String((e && e.message) || e)); })
@@ -11185,6 +11212,7 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
11185
11212
  PREF_UNDO: TODO_PREF_UNDO,
11186
11213
  PREF_OPEN: TODO_PREF_OPEN,
11187
11214
  clampDepth: todoClampUndoDepth,
11215
+ undoAllowed: todoUndoAllowed,
11188
11216
  filterItems: todoFilterItems,
11189
11217
  groupKey: todoGroupKey,
11190
11218
  openMapParse: todoOpenMapParse,
@@ -15,7 +15,7 @@
15
15
  * 就是临时目录),拿模块级常量会写错地方 —— 那是会碰到真实记忆的危险错误。
16
16
  */
17
17
 
18
- import { ARCHIVE_DIR_NAME, addArchiveMeta, inspectDir, inspectMemoryEntry, localStamp, readArchivedMeta, stripArchiveMeta } from './scan.js'
18
+ import { ARCHIVE_DIR_NAME, addArchiveMeta, inspectDir, inspectMemoryEntry, localStamp, readArchivedMeta, shadowsReserved, stripArchiveMeta } from './scan.js'
19
19
  import { isReserved, nameKey, safeName } from './naming.js'
20
20
  import { revisionOf } from './chunks.js'
21
21
  import { retireTaken, returnSource, takeOverSource, writeFileAtomic, writeFileExclusive } from './util.js'
@@ -28,13 +28,31 @@ import { randomUUID } from 'node:crypto'
28
28
  /** 归档区绝对路径(按调用方给的记忆目录算)。 */
29
29
  export const archiveDirOf = (memoryDir) => join(memoryDir, ARCHIVE_DIR_NAME)
30
30
 
31
- /** 名字闸门:保留名 → 非 .md / 带路径成分。返回 { ok:true, safe } 或 { ok:false, code, error }。 */
32
- function checkName(name, verb) {
31
+ /**
32
+ * 名字闸门:保留名 → 非 .md / 带路径成分 → **保留名的另一个名字**(8.3 短名 / 硬链接)。
33
+ *
34
+ * 返回 `{ ok:true, safe }` 或 `{ ok:false, code, error }`。
35
+ *
36
+ * ⚠️ 2.15.4:第三条原先只在 read / write / delete 三条路径上做(`index.js`、`write-protocol.js`),
37
+ * 归档 / 恢复 / 彻底删除这一侧漏了 —— 于是 `archive('MEMORY~1.MD')`(或一个指向索引的硬链接名)
38
+ * 能把**索引本体**搬进归档区,`deleteArchived` 那条更是不可逆。三处入口现在共用这一份判据。
39
+ *
40
+ * @param {string} dir — 名字所在目录(归档时是记忆根,恢复 / 删除时是归档区):
41
+ * 身份判据必须对着**真正要动的那个文件所在目录**算,否则算出来的是另一棵树的保留名身份。
42
+ */
43
+ async function checkName(dir, name, verb) {
33
44
  if (isReserved(name)) {
34
45
  return { ok: false, code: 'reserved', error: `sage-mem: reserved file, refusing to ${verb}: ${nameKey(name)}` }
35
46
  }
36
47
  const safe = safeName(name)
37
48
  if (!safe) return { ok: false, code: 'invalid_name', error: `sage-mem: invalid file name: ${String(name)}` }
49
+ if (await shadowsReserved(join(dir, safe))) {
50
+ return {
51
+ ok: false,
52
+ code: 'reserved',
53
+ error: `sage-mem: reserved file (它的另一个名字是 ${safe}),拒绝${verb}`,
54
+ }
55
+ }
38
56
  return { ok: true, safe }
39
57
  }
40
58
 
@@ -48,7 +66,7 @@ function checkName(name, verb) {
48
66
  * @returns {Promise<{ok:true,file:string,archivedReason:string}|{ok:false,code:string,error:string}>}
49
67
  */
50
68
  export async function archiveMemory(memoryDir, name, reason, at = localStamp()) {
51
- const gate = checkName(name, 'archive')
69
+ const gate = await checkName(memoryDir, name, '归档')
52
70
  if (!gate.ok) return gate
53
71
  const safe = gate.safe
54
72
  /**
@@ -231,7 +249,7 @@ export async function archiveMemory(memoryDir, name, reason, at = localStamp())
231
249
  * 覆盖等于悄悄改掉一条活记忆。
232
250
  */
233
251
  export async function restoreMemory(memoryDir, name) {
234
- const gate = checkName(name, 'restore')
252
+ const gate = await checkName(archiveDirOf(memoryDir), name, '恢复')
235
253
  if (!gate.ok) return gate
236
254
  const safe = gate.safe
237
255
  const dst = join(memoryDir, safe)
@@ -414,7 +432,7 @@ export function archivedRevisionOf(content) {
414
432
  * @returns {Promise<{ok:true,file:string}|{ok:false,code:string,error:string}>}
415
433
  */
416
434
  export async function deleteArchivedMemory(memoryDir, name, expectedArchivedAt = '', expectedRevision = '') {
417
- const gate = checkName(name, 'delete')
435
+ const gate = await checkName(archiveDirOf(memoryDir), name, '彻底删除')
418
436
  if (!gate.ok) return gate
419
437
  const safe = gate.safe
420
438
  const dirGate = await inspectDir(archiveDirOf(memoryDir))
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "sage-mem",
3
3
  "description": "Cross-session memory for DeepSeek Harness — plain markdown files, no database, no worker.",
4
- "version": "2.15.2",
4
+ "version": "2.15.4",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "icon": "./icon.png",