dsh-memory-domains 0.2.2 → 0.2.3

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/index.js CHANGED
@@ -101,9 +101,13 @@ function flushMeta() {
101
101
  try { fs.writeFileSync(metaFile(), JSON.stringify(Object.fromEntries(metaCache))) } catch { /* ok */ }
102
102
  }
103
103
 
104
+ let warming = false
104
105
  async function warmSessionMeta() {
105
- // 最近的会话优先预热;每 25 个落盘一次,进程中途死掉不丢已完成的进度
106
- const files = listSessionFiles(CURRENT.sessionsDir).sort((a, b) => b.mtime - a.mtime)
106
+ if (warming) return
107
+ warming = true
108
+ try {
109
+ // 最近的会话优先预热;每 25 个落盘一次,进程中途死掉不丢已完成的进度
110
+ const files = listSessionFiles(CURRENT.sessionsDir).sort((a, b) => b.mtime - a.mtime)
107
111
  let dirty = false, sinceFlush = 0
108
112
  for (const f of files) {
109
113
  const cur = metaCache.get(f.id)
@@ -117,6 +121,7 @@ async function warmSessionMeta() {
117
121
  await new Promise(r => setImmediate(r)) // 让出事件循环,不阻塞 API
118
122
  }
119
123
  if (dirty) flushMeta()
124
+ } finally { warming = false }
120
125
  }
121
126
 
122
127
  function sessionsPending() {
@@ -152,7 +157,10 @@ function domainList() {
152
157
 
153
158
  const api = {
154
159
  // GET /api/sessions — 会话清单(秒回:标题走缓存,pending>0 表示后台还在预热)
155
- 'GET /api/sessions': async () => ({
160
+ 'GET /api/sessions': async () => {
161
+ // 预热只在 apply 跑一次;运行期新会话靠这里自愈(pending>0 时补一轮,幂等+防重入)
162
+ if (sessionsPending() > 0) warmSessionMeta().catch(() => {})
163
+ return {
156
164
  ok: true,
157
165
  pending: sessionsPending(),
158
166
  sessions: listSessions(CURRENT.sessionsDir, { parseTitles: false }).map(s => ({
@@ -160,7 +168,8 @@ const api = {
160
168
  cwd: s.cwd ? String(s.cwd).split(/[\\/]/).pop() : '', // 只回 basename,不泄整机路径
161
169
  ws: s.ws, mtime: s.mtime, msgs: s.msgs,
162
170
  })),
163
- }),
171
+ }
172
+ },
164
173
 
165
174
  // ---- 领域 ----
166
175
  'GET /api/domains': async () => ({ ok: true, domains: domainList() }),
@@ -230,11 +239,12 @@ const api = {
230
239
  const body = await readBody(req)
231
240
  const session = String(body.session || '').trim()
232
241
  if (!session) return jsonErr(400, 'session required')
233
- const data = readSession(CURRENT.sessionsDir, session)
234
- if (!data || !data.msgs.length) return jsonErr(404, '会话不存在或没有可读消息')
235
- // 同一会话只允许一个 running job——绕过 UI 直连 API 也不会产出重复草稿
242
+ // 同一会话只允许一个 running job——绕过 UI 直连 API 也不会产出重复草稿;
243
+ // 先查重再解压轨迹,重复请求不白跑 IO
236
244
  const dup = [...reviewJobs.entries()].find(([, j]) => j.session === session && j.state === 'running')
237
245
  if (dup) return { ok: true, job: dup[0], deduped: true }
246
+ const data = readSession(CURRENT.sessionsDir, session)
247
+ if (!data || !data.msgs.length) return jsonErr(404, '会话不存在或没有可读消息')
238
248
  const jobId = `job-${Date.now().toString(36)}`
239
249
  reviewJobs.set(jobId, { id: jobId, state: 'running', session, started: Date.now() })
240
250
  // running 态兜底 TTL:客户端不轮询(关面板等)时 job 也不能在 Map 里永久滞留
package/lib/github.js CHANGED
@@ -28,7 +28,7 @@ export async function distillQueries(cfg, domain, { chat } = {}) {
28
28
  const clean = s => String(s || '').trim().replace(/^`+|`+$/g, '').slice(0, 80)
29
29
  const ok = s => s.length >= 2 && !/[。!?;:]/.test(s) // 全角句读是解释性文字的特征;ASCII ':' 保留(language:python 限定符)
30
30
  // 优先 JSON 数组解析;失败退回逐行解析(容忍模型不守格式)
31
- const m = String(text || '').match(/\[[\s\S]*\]/)
31
+ const m = String(text || '').match(/\[[^\[\]]*\]/) // 首个无嵌套括号组,防 '["a"] 说明 [b]' 贪婪误配
32
32
  if (m) {
33
33
  try {
34
34
  const arr = JSON.parse(m[0])
package/lib/store.js CHANGED
@@ -206,6 +206,12 @@ export function updateMemory(dir, scope, key, id, patch) {
206
206
  const prev = entries[i]
207
207
  const clean = {}
208
208
  for (const k of Object.keys(patch || {})) if (PATCHABLE.has(k)) clean[k] = patch[k]
209
+ // 与 addMemory 同规格:type 校验枚举、字符串截断
210
+ if (clean.type !== undefined && !TYPES.includes(clean.type)) delete clean.type
211
+ if (clean.title !== undefined) clean.title = String(clean.title).slice(0, 200)
212
+ if (clean.content !== undefined) clean.content = String(clean.content).slice(0, 8000)
213
+ if (clean.evidence !== undefined) clean.evidence = String(clean.evidence).slice(0, 500)
214
+ if (clean.status !== undefined) clean.status = clean.status === 'archived' ? 'archived' : 'active'
209
215
  const touched = CONTENT_FIELDS.some(f => clean[f] !== undefined && clean[f] !== prev[f])
210
216
  const next = { ...prev, ...clean, id: prev.id, updated_at: new Date().toISOString() }
211
217
  if (clean.priority !== undefined) { // 元数据不进 history;'normal' 即删除标记
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-memory-domains",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "exports": {