sage-mem 2.20.0 → 2.20.1

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.20.0`** — 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.20.1`** — 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.20.0`** —— 顶部 npm 徽章是动态权威。**契约基线:`1.0.0`** —— 第一个稳定版,公开承诺、能力边界与「明确做不到」的清单冻结在
17
+ **当前发布版本:`2.20.1`** —— 顶部 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 是最直接的反馈。
@@ -57,27 +57,38 @@ export function createFileLease({
57
57
  }
58
58
  }
59
59
 
60
- /** 有一次尝试;busy / retry / io 三类结果由 acquire 决定是否再试。 */
61
- async function acquireOnce() {
62
- try {
63
- await mkdir(dirname(lockPath), { recursive: true })
64
- } catch (err) {
65
- return { ok: false, code: 'lock_io', reason: `创建锁目录失败:${err?.message ?? err}` }
66
- }
67
- const token = randomUUID()
68
- let fh
60
+ /** 原子创建自己的锁文件;EEXIST 是正常的竞争结果。 */
61
+ async function tryCreate(token) {
62
+ let fh = null
63
+ let created = false
69
64
  try {
70
65
  fh = await open(lockPath, 'wx')
66
+ created = true
71
67
  await fh.write(JSON.stringify({ ...ownerMeta(), pid: process.pid, at: Date.now(), token }))
72
68
  await fh.close()
73
69
  fh = null
74
70
  return { ok: true, token }
75
71
  } catch (err) {
76
72
  if (fh) await fh.close().catch(() => {})
77
- if (err?.code !== 'EEXIST') {
78
- return { ok: false, code: 'lock_io', reason: `创建锁失败:${err?.message ?? err}` }
73
+ // 自己创建后写入 / 关闭失败:清掉半截锁,别把后来者永久挡在外面。
74
+ if (created) await rm(lockPath, { force: true }).catch(() => {})
75
+ if (err?.code === 'EEXIST') {
76
+ return { ok: false, code: 'lock_busy', reason: '另一个进程正持有锁' }
79
77
  }
78
+ return { ok: false, code: 'lock_io', reason: `创建锁失败:${err?.message ?? err}` }
79
+ }
80
+ }
81
+
82
+ /** 有一次尝试;busy / retry / io 三类结果由 acquire 决定是否再试。 */
83
+ async function acquireOnce() {
84
+ try {
85
+ await mkdir(dirname(lockPath), { recursive: true })
86
+ } catch (err) {
87
+ return { ok: false, code: 'lock_io', reason: `创建锁目录失败:${err?.message ?? err}` }
80
88
  }
89
+ const token = randomUUID()
90
+ const fresh = await tryCreate(token)
91
+ if (fresh.ok || fresh.code !== 'lock_busy') return fresh
81
92
 
82
93
  const info = await stat(lockPath).catch(() => null)
83
94
  if (!info) return { ok: false, code: 'retry', reason: '锁刚消失' }
@@ -85,6 +96,14 @@ export function createFileLease({
85
96
  return { ok: false, code: 'lock_busy', reason: '另一个进程正持有锁' }
86
97
  }
87
98
 
99
+ // 上面的 info 可能已经是旧快照:rename 前再确认一次,绝不能拿旧判断去移走
100
+ // 别人刚创建的新锁。这是并发接管过期锁时「两个赢家」的第一道闸。
101
+ const recheck = await stat(lockPath).catch(() => null)
102
+ if (!recheck) return { ok: false, code: 'retry', reason: '锁在复核前消失' }
103
+ if (Date.now() - recheck.mtimeMs <= staleMs) {
104
+ return { ok: false, code: 'lock_busy', reason: '复核时锁已换成新鲜锁' }
105
+ }
106
+
88
107
  const before = await readJson(lockPath)
89
108
  const grabbed = `${lockPath}.stale-${randomUUID().replace(/-/g, '')}`
90
109
  const moveErr = await rename(lockPath, grabbed).then(
@@ -112,8 +131,11 @@ export function createFileLease({
112
131
  return { ok: false, code: 'lock_busy', reason: '复核时锁已被换主' }
113
132
  }
114
133
 
134
+ // 复核通过后**立刻**在同一轮创建新锁。旧实现先删 grabbed 再 return retry,
135
+ // 让「锁位置为空」的窗口跨了两次事件循环 —— 另一个 fast-path 正是从那里钻进来的。
136
+ const taken = await tryCreate(token)
115
137
  await rm(grabbed, { force: true }).catch(() => {})
116
- return { ok: false, code: 'retry', reason: '已清理过期锁,重试创建' }
138
+ return taken
117
139
  }
118
140
 
119
141
  /**
@@ -131,10 +153,13 @@ export function createFileLease({
131
153
  // 避免释放窗口里的瞬时 retry 在一微秒内把重试次数烧完。
132
154
  if (last.code === 'retry' && waitMs <= 0) continue
133
155
  if (last.code === 'lock_busy' && waitMs <= 0) return last
134
- const wait = backoffMs[Math.min(i, backoffMs.length - 1)] ?? 0
156
+ const base = backoffMs[Math.min(i, backoffMs.length - 1)] ?? 0
157
+ // 两个进程用同一串退避会同步醒来、反复互相撞(实测首轮只差 1/80)。
158
+ // 加 0..base 的抖动,打散轮询节拍;waitMs=0 的 gate 不走这里。
159
+ const wait = base > 0 ? base + Math.floor(Math.random() * (base + 1)) : 1
135
160
  const left = deadline - Date.now()
136
161
  if (left <= 0) return last
137
- await delay(Math.min(Math.max(wait, 1), left))
162
+ await delay(Math.min(wait, left))
138
163
  }
139
164
  return last
140
165
  }
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.20.0",
4
+ "version": "2.20.1",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "icon": "./icon.png",