lingshu-skill 5.57.0 → 5.59.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.
- package/CHANGELOG.md +28 -0
- package/README.md +1 -1
- package/SKILL-lite.md +1 -1
- package/SKILL-tiny.md +1 -1
- package/SKILL.md +1 -1
- package/assets/panel.html +5 -1
- package/cordis-plugin/lingshu-guard/plugin.js +25 -9
- package/package.json +1 -1
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +11 -11
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +28 -0
- package/scripts/astrbot_plugin_lingshu/engine/README.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +5 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +59 -14
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +5 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +1235 -1214
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +90 -21
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +26 -11
- package/scripts/cli.py +59 -14
- package/scripts/core.py +5 -1
- package/scripts/dream.py +1235 -1214
- package/scripts/memory.py +90 -21
- package/scripts/panel_server.py +26 -11
package/scripts/memory.py
CHANGED
|
@@ -1102,8 +1102,40 @@ def save_all(records: list) -> None:
|
|
|
1102
1102
|
except OSError:
|
|
1103
1103
|
pass
|
|
1104
1104
|
raise
|
|
1105
|
-
|
|
1106
|
-
|
|
1105
|
+
# LS-009(v5.59):为 save_all 增加多文件替换失败原子回滚!
|
|
1106
|
+
replaced_backups = [] # [(target_path, backup_path)]
|
|
1107
|
+
try:
|
|
1108
|
+
for tmp, p in staged:
|
|
1109
|
+
bak_old = None
|
|
1110
|
+
if p.exists():
|
|
1111
|
+
bak_old = p.with_name(p.name + f".rollback-{os.getpid()}-{int(now_ts() * 1000) % 1000000}")
|
|
1112
|
+
try:
|
|
1113
|
+
shutil.copy2(p, bak_old)
|
|
1114
|
+
except Exception:
|
|
1115
|
+
bak_old = None
|
|
1116
|
+
_replace_retry(tmp, p)
|
|
1117
|
+
if bak_old:
|
|
1118
|
+
replaced_backups.append((p, bak_old))
|
|
1119
|
+
except BaseException as replace_err:
|
|
1120
|
+
for target_p, bak_p in reversed(replaced_backups):
|
|
1121
|
+
try:
|
|
1122
|
+
_replace_retry(bak_p, target_p)
|
|
1123
|
+
except Exception:
|
|
1124
|
+
pass
|
|
1125
|
+
for tmp, _ in staged:
|
|
1126
|
+
try:
|
|
1127
|
+
if tmp.exists():
|
|
1128
|
+
os.unlink(tmp)
|
|
1129
|
+
except OSError:
|
|
1130
|
+
pass
|
|
1131
|
+
raise replace_err
|
|
1132
|
+
finally:
|
|
1133
|
+
for _, bak_p in replaced_backups:
|
|
1134
|
+
try:
|
|
1135
|
+
if bak_p.exists():
|
|
1136
|
+
os.unlink(bak_p)
|
|
1137
|
+
except OSError:
|
|
1138
|
+
pass
|
|
1107
1139
|
|
|
1108
1140
|
# 写后校验:JSON 可解析 + 末行完整 + 层级自洽。不通过就回滚并抛错
|
|
1109
1141
|
# (回滚成功时数据是"旧的一致状态",比留着一份写坏的新数据安全)。
|
|
@@ -2276,25 +2308,36 @@ def fetch(memory_id: str, include_archive: bool = True) -> dict:
|
|
|
2276
2308
|
return {"ok": True, "memory": rec, "reason": ""}
|
|
2277
2309
|
|
|
2278
2310
|
|
|
2279
|
-
def dereference(memory_id: str
|
|
2311
|
+
def dereference(memory_id: str,
|
|
2312
|
+
channel: str | None = None, group_id: str | None = None,
|
|
2313
|
+
user: str | None = None,
|
|
2314
|
+
target_user: str | None = None, asker: str | None = None) -> list:
|
|
2280
2315
|
"""#46 Memex 解引用:按 id 取一份摘要/蒸馏产物的证据原文。
|
|
2281
2316
|
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2317
|
+
LS-005(v5.58):解引用时必须继承隐私与通道隔离约束!
|
|
2318
|
+
公开/聚合摘要虽然可能在当前群可见,但它底层的 raw evidence 原文可能来自:
|
|
2319
|
+
1) 其他群的私聊/秘密发言(channel/group_id 隔离)
|
|
2320
|
+
2) 其他用户未授权披露给当前 asker 的私密记忆(隐私保护 target_user/asker/_consented)
|
|
2321
|
+
若不继承隐私约束,攻击者只需生成一条可见的聚合摘要,就能通过 dereference
|
|
2322
|
+
非法带出被隔离证据的全文(侧信道越权泄露)。
|
|
2285
2323
|
"""
|
|
2286
|
-
# 唯一前缀解析(与 memory.fetch 共用 _resolve_id,避免两套实现漂移)
|
|
2287
2324
|
rec, _amb = _resolve_id(memory_id, load_all())
|
|
2288
2325
|
if rec is None:
|
|
2289
2326
|
rec, _amb2 = _resolve_id(memory_id, load_archive())
|
|
2290
2327
|
_amb = _amb or _amb2
|
|
2291
2328
|
if rec is None:
|
|
2292
2329
|
return []
|
|
2330
|
+
|
|
2331
|
+
if target_user and asker and str(asker) != str(target_user).strip():
|
|
2332
|
+
if not _consented(target_user, group_id):
|
|
2333
|
+
return []
|
|
2334
|
+
|
|
2293
2335
|
ev_ids = [str(x) for x in (rec.get("evidence_ids") or rec.get("source_ids") or [])
|
|
2294
2336
|
if str(x).strip()]
|
|
2295
2337
|
if not ev_ids:
|
|
2296
|
-
|
|
2297
|
-
|
|
2338
|
+
if channel is not None or group_id is not None or user is not None:
|
|
2339
|
+
if not _channel_visible(rec, channel, group_id, user):
|
|
2340
|
+
return []
|
|
2298
2341
|
rec = dict(rec)
|
|
2299
2342
|
rec["_self"] = True
|
|
2300
2343
|
return [rec]
|
|
@@ -2302,6 +2345,13 @@ def dereference(memory_id: str) -> list:
|
|
|
2302
2345
|
out = []
|
|
2303
2346
|
for m in load_all() + load_archive():
|
|
2304
2347
|
if m.get("id") in want:
|
|
2348
|
+
if channel is not None or group_id is not None or user is not None:
|
|
2349
|
+
if not _channel_visible(m, channel, group_id, user):
|
|
2350
|
+
continue
|
|
2351
|
+
m_u = str(m.get("user") or "").strip()
|
|
2352
|
+
if m_u and asker and str(asker) != m_u:
|
|
2353
|
+
if not _consented(m_u, group_id):
|
|
2354
|
+
continue
|
|
2305
2355
|
out.append({"id": m["id"], "kind": m.get("kind", ""), "layer": m.get("layer", ""),
|
|
2306
2356
|
"text": m.get("text", ""), "created": m.get("created"),
|
|
2307
2357
|
"superseded_by": m.get("superseded_by")})
|
|
@@ -2310,7 +2360,6 @@ def dereference(memory_id: str) -> list:
|
|
|
2310
2360
|
break
|
|
2311
2361
|
return out
|
|
2312
2362
|
|
|
2313
|
-
|
|
2314
2363
|
# ---------------------------------------------------------------- 错卦:否定极性
|
|
2315
2364
|
# 「错卦」在易经里是六爻全变(按位取反)。借到检索上就是**否定极性**:
|
|
2316
2365
|
# "用 X" 和 "不要用 X" 是两个方向,不该拿到同一批无标记的结果。
|
|
@@ -2407,8 +2456,14 @@ def recall(query: str, k: int = 8, layers: tuple = ("cortex", "hippo"),
|
|
|
2407
2456
|
if not str(m.get("agent_id") or "").strip()
|
|
2408
2457
|
or str(m.get("agent_id")).strip() == aid]
|
|
2409
2458
|
if include_archive:
|
|
2459
|
+
# LS-004(v5.58):归档召回同样必须执行 agent_id 隔离。
|
|
2410
2460
|
archive = [m for m in load_archive() if m.get("id")
|
|
2411
2461
|
and _channel_visible(m, channel, group_id, user)]
|
|
2462
|
+
if agent_id:
|
|
2463
|
+
aid = str(agent_id).strip()
|
|
2464
|
+
archive = [m for m in archive
|
|
2465
|
+
if not str(m.get("agent_id") or "").strip()
|
|
2466
|
+
or str(m.get("agent_id")).strip() == aid]
|
|
2412
2467
|
# E-13:归档与现役层同 id 去重——同一记忆既在现役又有归档副本时只计现役一份,
|
|
2413
2468
|
# 避免重复计分/重复展示。
|
|
2414
2469
|
seen_ids = {m["id"] for m in records}
|
|
@@ -2989,7 +3044,7 @@ def recall(query: str, k: int = 8, layers: tuple = ("cortex", "hippo"),
|
|
|
2989
3044
|
if not ev_ids:
|
|
2990
3045
|
continue
|
|
2991
3046
|
try:
|
|
2992
|
-
ev = dereference(m["id"])[:2]
|
|
3047
|
+
ev = dereference(m["id"], channel=channel, group_id=group_id, user=user, target_user=target_user, asker=asker)[:2]
|
|
2993
3048
|
if ev:
|
|
2994
3049
|
parts["evidence"] = [{"id": e["id"], "text": (e.get("text") or "")[:300]}
|
|
2995
3050
|
for e in ev]
|
|
@@ -3052,8 +3107,8 @@ def _ledger_path():
|
|
|
3052
3107
|
return HOME / _LEDGER_FILE
|
|
3053
3108
|
|
|
3054
3109
|
|
|
3055
|
-
def
|
|
3056
|
-
"""
|
|
3110
|
+
def _ledger_add_strict(record: dict, reason: str = "purge") -> bool:
|
|
3111
|
+
"""LS-016: 把一条记录追加进软删除台账,返回是否成功。"""
|
|
3057
3112
|
try:
|
|
3058
3113
|
row = dict(record)
|
|
3059
3114
|
row["deleted_at"] = now_ts()
|
|
@@ -3062,10 +3117,15 @@ def _ledger_add(record: dict, reason: str = "purge") -> None:
|
|
|
3062
3117
|
with _locked(_ledger_path()):
|
|
3063
3118
|
with _ledger_path().open("a", encoding="utf-8") as f:
|
|
3064
3119
|
f.write(json.dumps(row, ensure_ascii=False) + "\n")
|
|
3065
|
-
|
|
3066
|
-
|
|
3120
|
+
return True
|
|
3121
|
+
except Exception as exc:
|
|
3122
|
+
log_event("purge", f"软删除台账严格写入失败:{exc}", level="error")
|
|
3123
|
+
return False
|
|
3067
3124
|
|
|
3068
3125
|
|
|
3126
|
+
def _ledger_add(record: dict, reason: str = "purge") -> None:
|
|
3127
|
+
"""把一条记录追加进软删除台账(调用方负责持锁)。失败不阻断删除主流程。"""
|
|
3128
|
+
_ledger_add_strict(record, reason)
|
|
3069
3129
|
def trash_list(limit: int = 50) -> list:
|
|
3070
3130
|
"""看软删除台账里有什么(最近优先)。"""
|
|
3071
3131
|
rows = []
|
|
@@ -3187,7 +3247,9 @@ def purge(memory_id: str, confirm_token: str) -> dict:
|
|
|
3187
3247
|
# 而 _ledger_add 会吞掉所有异常只记 warn —— 于是"删成功 + 台账写失败"
|
|
3188
3248
|
# (磁盘满/权限/文件被占)时,这条归档记忆被物理销毁且哪里都没有副本,
|
|
3189
3249
|
# 用户无法恢复,直接违反"只移不删、删除必须先留底"。
|
|
3190
|
-
|
|
3250
|
+
# LS-016(v5.59):台账写入失败时必须禁止物理删除!
|
|
3251
|
+
if not _ledger_add_strict(target, reason="purge-archive"):
|
|
3252
|
+
raise RuntimeError(f"软删除台账写入失败,为防数据丢失已中止物理删除:{memory_id}")
|
|
3191
3253
|
write_jsonl(HOME / "archive.jsonl", archive)
|
|
3192
3254
|
_cut_assoc_links(memory_id)
|
|
3193
3255
|
audit_log("user", "purge", memory_id, "物理删除(archive 副本)")
|
|
@@ -3201,7 +3263,9 @@ def purge(memory_id: str, confirm_token: str) -> dict:
|
|
|
3201
3263
|
raise ValueError(
|
|
3202
3264
|
f"淡忘记忆需冷静满 {_PURGE_COOLDOWN_DAYS} 天后才能物理删除"
|
|
3203
3265
|
f"(当前淡忘于 {fmt_date(forgotten_at)})")
|
|
3204
|
-
|
|
3266
|
+
# LS-016(v5.59):台账写入失败时必须禁止物理删除!
|
|
3267
|
+
if not _ledger_add_strict(target, reason="purge-dust"):
|
|
3268
|
+
raise RuntimeError(f"软删除台账写入失败,为防数据丢失已中止物理删除:{memory_id}")
|
|
3205
3269
|
records = [m for m in records if m.get("id") != memory_id]
|
|
3206
3270
|
save_all(records)
|
|
3207
3271
|
# archive 里可能也有同名副本(归档过的),一并清掉
|
|
@@ -3693,15 +3757,20 @@ def _sweep_locked(rows: list) -> int:
|
|
|
3693
3757
|
arch.append(r)
|
|
3694
3758
|
continue
|
|
3695
3759
|
keep.append(r)
|
|
3760
|
+
# LS-017(v5.59):归档失败时保留工作记忆原记录!
|
|
3761
|
+
actually_archived = []
|
|
3762
|
+
failed_arch = []
|
|
3696
3763
|
if arch:
|
|
3697
|
-
rows[:] = keep
|
|
3698
3764
|
path = _work_archive_path()
|
|
3699
3765
|
for r in arch:
|
|
3700
3766
|
try:
|
|
3701
3767
|
_append_jsonl(path, r)
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3768
|
+
actually_archived.append(r)
|
|
3769
|
+
except OSError as exc:
|
|
3770
|
+
log_event("working", f"工作记忆归档单条失败,保留在原工作区:{exc}", level="warn")
|
|
3771
|
+
failed_arch.append(r)
|
|
3772
|
+
rows[:] = keep + failed_arch
|
|
3773
|
+
return len(actually_archived)
|
|
3705
3774
|
|
|
3706
3775
|
|
|
3707
3776
|
def work_sweep() -> int:
|
package/scripts/panel_server.py
CHANGED
|
@@ -631,7 +631,7 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
631
631
|
self.send_header("Content-Security-Policy",
|
|
632
632
|
"default-src 'self'; script-src 'unsafe-inline'; "
|
|
633
633
|
"style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; "
|
|
634
|
-
"frame-ancestors '
|
|
634
|
+
"frame-ancestors 'self'") # 防恶意网页 iframe 嵌入面板(点击劫持)
|
|
635
635
|
self.end_headers()
|
|
636
636
|
if body:
|
|
637
637
|
self.wfile.write(body)
|
|
@@ -886,8 +886,20 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
886
886
|
# 读取接口同样要校验来源 + token:记忆是隐私数据,不能被任意页面/进程读走
|
|
887
887
|
# /api/ping 免鉴权:启动器用它判断"面板是否已在跑",那时它还拿不到 token
|
|
888
888
|
# (token 随进程随机生成、只注入页面)。它只回 app/port/pid,无记忆内容。
|
|
889
|
+
# LS-058 (v5.58):/api/panels/file 用于 iframe 加载,浏览器原生 <iframe> src 无法自定义 Request Headers。
|
|
890
|
+
# 增加对安全 URL query token 的等价鉴权(同 /api/events 一样支持 ?token=)。
|
|
891
|
+
def _file_token_valid():
|
|
892
|
+
if self._token_ok():
|
|
893
|
+
return True
|
|
894
|
+
qs_t = (parse_qs(url.query).get("token") or [""])[0].strip()
|
|
895
|
+
if not qs_t:
|
|
896
|
+
return False
|
|
897
|
+
day = time.strftime("%Y-%m-%d", time.localtime())
|
|
898
|
+
derived = hashlib.sha256((TOKEN + "|" + day).encode("utf-8")).hexdigest()[:32]
|
|
899
|
+
return hmac.compare_digest(qs_t.encode("utf-8"), TOKEN.encode("utf-8")) or hmac.compare_digest(qs_t.encode("utf-8"), derived.encode("utf-8"))
|
|
900
|
+
|
|
889
901
|
if (path.startswith("/api/") and path != "/api/ping"
|
|
890
|
-
and (not self._origin_ok() or not self._token_ok())):
|
|
902
|
+
and (not self._origin_ok() or (path != "/api/panels/file" and not self._token_ok()) or (path == "/api/panels/file" and not _file_token_valid()))):
|
|
891
903
|
self._json({"error": "forbidden"}, 403)
|
|
892
904
|
return
|
|
893
905
|
if path == "/api/overview":
|
|
@@ -1248,15 +1260,18 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
1248
1260
|
if as_page and "html" in ctype:
|
|
1249
1261
|
try:
|
|
1250
1262
|
text = body.decode("utf-8")
|
|
1251
|
-
#
|
|
1252
|
-
#
|
|
1253
|
-
#
|
|
1254
|
-
#
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1263
|
+
# LS-059 (v5.58):给第三方面板安全注入「当日派生 token」!
|
|
1264
|
+
# 旧逻辑只对 DEFAULT_NAME 注入主 TOKEN,而第三方面板里的 /*__PANEL_TOKEN__*/
|
|
1265
|
+
# 占位符从未被替换,导致第三方面板的 api() helper 发出的全部请求携带
|
|
1266
|
+
# 字符串 "/*__PANEL_TOKEN__*/",全部被后端 403 拒绝!
|
|
1267
|
+
# 现计算当日派生 token:SHA256(TOKEN || YYYY-MM-DD)[:32]
|
|
1268
|
+
day = time.strftime("%Y-%m-%d", time.localtime())
|
|
1269
|
+
derived_token = hashlib.sha256((TOKEN + "|" + day).encode("utf-8")).hexdigest()[:32]
|
|
1270
|
+
target_token = TOKEN if pname == panels.DEFAULT_NAME else derived_token
|
|
1271
|
+
|
|
1272
|
+
text = text.replace("/*__PANEL_TOKEN__*/", target_token)
|
|
1273
|
+
if "PANEL_TOKEN" not in text:
|
|
1274
|
+
text = text.replace("<head>", '<head>\n<script>const PANEL_TOKEN="' + target_token + '";</script>', 1)
|
|
1260
1275
|
body = text.encode("utf-8")
|
|
1261
1276
|
except UnicodeDecodeError:
|
|
1262
1277
|
pass # 非 UTF-8 HTML:原样下发,面板作者自己修
|