lingshu-skill 5.42.0 → 5.44.0

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.
@@ -14,6 +14,11 @@ import shutil
14
14
  import sys
15
15
  import threading
16
16
  import time
17
+
18
+ # SPEED-1: load_persona 模块级微缓存(见 load_persona docstring)
19
+ _lp_cache = None
20
+ _lp_cache_mtime = None
21
+ _lp_cache_time = 0.0
17
22
  import zipfile
18
23
  from contextlib import contextmanager, nullcontext
19
24
  from datetime import datetime
@@ -259,6 +264,17 @@ def compile_persona() -> dict:
259
264
  p["drives"] = {}
260
265
  p.setdefault("onboarding", default_onboarding())
261
266
  if not READONLY:
267
+ # 空态守卫(与 save_persona 同一闸):盘上有人格且待写入为空态时拒绝——
268
+ # compile_persona 原先直写 atomic_json 绕过了 save_persona 的守卫,
269
+ # 曾导致 startup 路径在竞态窗口把清空态合法化写盘(12-03 18:20 事故)。
270
+ _disk_now = _slurp_json(HOME / "persona.json") if (HOME / "persona.json").exists() else None
271
+ _disk_named = bool((( _disk_now or {}).get("identity") or {}).get("名字"))
272
+ _incoming_named = bool(((p.get("identity") or {}).get("名字") or "").strip())
273
+ if _disk_named and not _incoming_named:
274
+ log_event("persona",
275
+ "compile_persona 拒绝覆盖:盘上人格有名字而重建产物为空态——疑似竞态,已拦下。",
276
+ level="error")
277
+ return _slurp_json(HOME / "persona.json") # 返回盘上完好人格
262
278
  atomic_json(HOME / "persona.json", p)
263
279
  return p
264
280
 
@@ -288,6 +304,17 @@ def _validate_persona_struct(obj, where: str = "persona.json") -> None:
288
304
  def load_persona() -> dict:
289
305
  ensure_home()
290
306
  path = HOME / "persona.json"
307
+ # SPEED-1 模块级微缓存(TTL 2s):build_injection 一次会调 24 次 load_persona,
308
+ # 每次都重读盘+跑迁移检查 ≈ 4ms × 24 ≈ 100ms。写路径(save_persona)主动失效,
309
+ # mtime 双检兜底外部改动——正确性与磁盘同源,只省重复读。
310
+ global _lp_cache, _lp_cache_mtime, _lp_cache_time
311
+ try:
312
+ _mt = path.stat().st_mtime_ns
313
+ except OSError:
314
+ _mt = None
315
+ if (_lp_cache is not None and _mt is not None and _mt == _lp_cache_mtime
316
+ and time.time() - _lp_cache_time < 2.0):
317
+ return json.loads(json.dumps(_lp_cache, ensure_ascii=False)) # 深拷贝防污染
291
318
  if path.exists():
292
319
  p = _slurp_json(path)
293
320
  if not isinstance(p, dict):
@@ -337,10 +364,31 @@ def load_persona() -> dict:
337
364
  changed = True
338
365
  if changed and not READONLY:
339
366
  save_persona(p)
367
+ _lp_cache, _lp_cache_mtime, _lp_cache_time = p, _mt, time.time()
340
368
  return p
341
369
 
342
370
 
343
371
  def save_persona(p: dict) -> None:
372
+ global _lp_cache, _lp_cache_mtime, _lp_cache_time
373
+ _lp_cache = None # 写路径主动失效缓存
374
+ # 空人格写盘守卫(2026-12-03 两次清空事故加固):
375
+ # 盘上已有完整人格(有名字)时,拒绝用 identity+traits 双空的默认态覆盖——
376
+ # 除非是 init --force 的显式重置(调用方传 _allow_empty=True 绕过)。
377
+ # 竞态窗口(测试进程写空态、后台做梦并发 save)由此被彻底堵死。
378
+ try:
379
+ _path = HOME / "persona.json"
380
+ if _path.exists():
381
+ _disk = _slurp_json(_path) or {}
382
+ _disk_named = bool(((_disk.get("identity") or {}).get("名字") or "").strip())
383
+ _incoming_named = bool(((p.get("identity") or {}).get("名字") or "").strip())
384
+ if _disk_named and not _incoming_named and not p.get("_allow_empty"):
385
+ log_event("persona",
386
+ "拒绝保存:盘上人格有名字而待写入人格为空态——疑似竞态覆盖,已拦下。"
387
+ "若确要重置请用 brain.py init --force(自带备份)。",
388
+ level="error")
389
+ return
390
+ except Exception:
391
+ pass # 守卫自身绝不阻塞正常保存
344
392
  with _locked(HOME / "persona.json"):
345
393
  atomic_json(HOME / "persona.json", p)
346
394