lingshu-skill 5.58.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 +14 -0
- package/README.md +1 -1
- package/SKILL-lite.md +1 -1
- package/SKILL-tiny.md +1 -1
- package/SKILL.md +1 -1
- package/package.json +1 -1
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +8 -8
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +14 -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/scripts/cli.py +43 -12
- package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +1235 -1214
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +58 -12
- package/scripts/cli.py +43 -12
- package/scripts/dream.py +1235 -1214
- package/scripts/memory.py +58 -12
|
@@ -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
|
# (回滚成功时数据是"旧的一致状态",比留着一份写坏的新数据安全)。
|
|
@@ -3075,8 +3107,8 @@ def _ledger_path():
|
|
|
3075
3107
|
return HOME / _LEDGER_FILE
|
|
3076
3108
|
|
|
3077
3109
|
|
|
3078
|
-
def
|
|
3079
|
-
"""
|
|
3110
|
+
def _ledger_add_strict(record: dict, reason: str = "purge") -> bool:
|
|
3111
|
+
"""LS-016: 把一条记录追加进软删除台账,返回是否成功。"""
|
|
3080
3112
|
try:
|
|
3081
3113
|
row = dict(record)
|
|
3082
3114
|
row["deleted_at"] = now_ts()
|
|
@@ -3085,10 +3117,15 @@ def _ledger_add(record: dict, reason: str = "purge") -> None:
|
|
|
3085
3117
|
with _locked(_ledger_path()):
|
|
3086
3118
|
with _ledger_path().open("a", encoding="utf-8") as f:
|
|
3087
3119
|
f.write(json.dumps(row, ensure_ascii=False) + "\n")
|
|
3088
|
-
|
|
3089
|
-
|
|
3120
|
+
return True
|
|
3121
|
+
except Exception as exc:
|
|
3122
|
+
log_event("purge", f"软删除台账严格写入失败:{exc}", level="error")
|
|
3123
|
+
return False
|
|
3090
3124
|
|
|
3091
3125
|
|
|
3126
|
+
def _ledger_add(record: dict, reason: str = "purge") -> None:
|
|
3127
|
+
"""把一条记录追加进软删除台账(调用方负责持锁)。失败不阻断删除主流程。"""
|
|
3128
|
+
_ledger_add_strict(record, reason)
|
|
3092
3129
|
def trash_list(limit: int = 50) -> list:
|
|
3093
3130
|
"""看软删除台账里有什么(最近优先)。"""
|
|
3094
3131
|
rows = []
|
|
@@ -3210,7 +3247,9 @@ def purge(memory_id: str, confirm_token: str) -> dict:
|
|
|
3210
3247
|
# 而 _ledger_add 会吞掉所有异常只记 warn —— 于是"删成功 + 台账写失败"
|
|
3211
3248
|
# (磁盘满/权限/文件被占)时,这条归档记忆被物理销毁且哪里都没有副本,
|
|
3212
3249
|
# 用户无法恢复,直接违反"只移不删、删除必须先留底"。
|
|
3213
|
-
|
|
3250
|
+
# LS-016(v5.59):台账写入失败时必须禁止物理删除!
|
|
3251
|
+
if not _ledger_add_strict(target, reason="purge-archive"):
|
|
3252
|
+
raise RuntimeError(f"软删除台账写入失败,为防数据丢失已中止物理删除:{memory_id}")
|
|
3214
3253
|
write_jsonl(HOME / "archive.jsonl", archive)
|
|
3215
3254
|
_cut_assoc_links(memory_id)
|
|
3216
3255
|
audit_log("user", "purge", memory_id, "物理删除(archive 副本)")
|
|
@@ -3224,7 +3263,9 @@ def purge(memory_id: str, confirm_token: str) -> dict:
|
|
|
3224
3263
|
raise ValueError(
|
|
3225
3264
|
f"淡忘记忆需冷静满 {_PURGE_COOLDOWN_DAYS} 天后才能物理删除"
|
|
3226
3265
|
f"(当前淡忘于 {fmt_date(forgotten_at)})")
|
|
3227
|
-
|
|
3266
|
+
# LS-016(v5.59):台账写入失败时必须禁止物理删除!
|
|
3267
|
+
if not _ledger_add_strict(target, reason="purge-dust"):
|
|
3268
|
+
raise RuntimeError(f"软删除台账写入失败,为防数据丢失已中止物理删除:{memory_id}")
|
|
3228
3269
|
records = [m for m in records if m.get("id") != memory_id]
|
|
3229
3270
|
save_all(records)
|
|
3230
3271
|
# archive 里可能也有同名副本(归档过的),一并清掉
|
|
@@ -3716,15 +3757,20 @@ def _sweep_locked(rows: list) -> int:
|
|
|
3716
3757
|
arch.append(r)
|
|
3717
3758
|
continue
|
|
3718
3759
|
keep.append(r)
|
|
3760
|
+
# LS-017(v5.59):归档失败时保留工作记忆原记录!
|
|
3761
|
+
actually_archived = []
|
|
3762
|
+
failed_arch = []
|
|
3719
3763
|
if arch:
|
|
3720
|
-
rows[:] = keep
|
|
3721
3764
|
path = _work_archive_path()
|
|
3722
3765
|
for r in arch:
|
|
3723
3766
|
try:
|
|
3724
3767
|
_append_jsonl(path, r)
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
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)
|
|
3728
3774
|
|
|
3729
3775
|
|
|
3730
3776
|
def work_sweep() -> int:
|
package/scripts/cli.py
CHANGED
|
@@ -743,6 +743,9 @@ def cmd_restore(zip_path: str, yes: bool = False, password: str | None = None,
|
|
|
743
743
|
# 只删白名单内的路径:记忆库里用户自己放进去的东西(草稿、导出包、
|
|
744
744
|
# 参考笔记)一律保留并汇报——"清空受管文件"不该变成"清空这个目录"。
|
|
745
745
|
kept: list[str] = []
|
|
746
|
+
# LS-011(v5.59):禁止 exact 恢复忽略受管文件删除失败!
|
|
747
|
+
# 若受管文件删除抛出 OSError/PermissionError,绝不能静默 pass 并输出"已回到备份时点"。
|
|
748
|
+
# 必须明确报错中止恢复流程。
|
|
746
749
|
for f in sorted(HOME.rglob("*"), reverse=True):
|
|
747
750
|
if f.is_dir():
|
|
748
751
|
continue
|
|
@@ -757,21 +760,49 @@ def cmd_restore(zip_path: str, yes: bool = False, password: str | None = None,
|
|
|
757
760
|
with _locked(f):
|
|
758
761
|
try:
|
|
759
762
|
f.unlink()
|
|
760
|
-
except OSError:
|
|
761
|
-
|
|
763
|
+
except OSError as unl_err:
|
|
764
|
+
raise RuntimeError(f"exact 恢复清理受管文件失败({rel} 被占用或拒绝):{unl_err}") from unl_err
|
|
762
765
|
if kept:
|
|
763
766
|
head = "、".join(kept[:5]) + (" …" if len(kept) > 5 else "")
|
|
764
767
|
print(f"保留 {len(kept)} 个非灵枢文件(restore 只清自己的东西):{head}")
|
|
765
|
-
|
|
766
|
-
#
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
768
|
+
|
|
769
|
+
# LS-010(v5.59):让恢复过程具备事务回滚能力!
|
|
770
|
+
# 记录所有已替换文件的旧版本备份副本,一旦任意文件 os.replace 失败,
|
|
771
|
+
# 立即逆序原子回滚所有已替换文件,绝不在磁盘留下新旧混合的脏库!
|
|
772
|
+
restored_backups = []
|
|
773
|
+
try:
|
|
774
|
+
for extracted in tmp_dir.rglob("*"):
|
|
775
|
+
if extracted.is_dir():
|
|
776
|
+
continue
|
|
777
|
+
rel = extracted.relative_to(tmp_dir)
|
|
778
|
+
final = (HOME / rel).resolve()
|
|
779
|
+
final.parent.mkdir(parents=True, exist_ok=True)
|
|
780
|
+
bak_curr = None
|
|
781
|
+
if final.exists():
|
|
782
|
+
bak_curr = final.with_name(final.name + f".restore-bak-{os.getpid()}-{int(now_ts() * 1000) % 1000000}")
|
|
783
|
+
try:
|
|
784
|
+
shutil.copy2(final, bak_curr)
|
|
785
|
+
except Exception:
|
|
786
|
+
bak_curr = None
|
|
787
|
+
with _locked(final):
|
|
788
|
+
os.replace(str(extracted), str(final))
|
|
789
|
+
if bak_curr:
|
|
790
|
+
restored_backups.append((final, bak_curr))
|
|
791
|
+
except BaseException as restore_err:
|
|
792
|
+
for target_f, bak_f in reversed(restored_backups):
|
|
793
|
+
try:
|
|
794
|
+
with _locked(target_f):
|
|
795
|
+
os.replace(str(bak_f), str(target_f))
|
|
796
|
+
except Exception:
|
|
797
|
+
pass
|
|
798
|
+
raise restore_err
|
|
799
|
+
finally:
|
|
800
|
+
for _, bak_f in restored_backups:
|
|
801
|
+
try:
|
|
802
|
+
if bak_f.exists():
|
|
803
|
+
os.unlink(bak_f)
|
|
804
|
+
except OSError:
|
|
805
|
+
pass
|
|
775
806
|
except ValueError as exc:
|
|
776
807
|
print(f"[错误] {exc}", file=sys.stderr)
|
|
777
808
|
return 1
|