lingshu-skill 3.95.0 → 3.97.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/scripts/memory.py CHANGED
@@ -3097,10 +3097,39 @@ def injectable_lessons(query: str = "", k: int = 5) -> list:
3097
3097
  # 光有记忆不够,必须在**行动前自动检索并注入**,否则就是"有记忆但没搜"
3098
3098
  # (reactive agent)——模型会从零重新调研已经做过的事。
3099
3099
 
3100
- _PRIOR_MEM_SCORE = 0.35 # 记忆命中门槛:低于此不展示(防噪音)
3100
+ _PRIOR_MEM_SCORE = 0.35 # 记忆综合分下限(相关度+新鲜度+重要性,仅作次要排序)
3101
3101
  _PRIOR_MAX_ITEMS = 3 # 每类最多展示几条
3102
3102
 
3103
3103
 
3104
+ def _prior_need(q_terms: set) -> int:
3105
+ """要判定"相关"至少得命中几个查询词。
3106
+
3107
+ 为什么不能只看 recall 的综合分:它是 相关度+**新鲜度**+**重要性** 的加权,
3108
+ 最近/重要的记忆天然拿 0.35+,于是"完全无关但很新"的条目也能过线
3109
+ (实测某次注入命中两条覆盖仅 3% 的垃圾)。改按"查询关键词实际命中数"卡:
3110
+ 短查询要 2 个,长查询按 30% 递增(封顶 4),不够就**弃权不注入**
3111
+ (CBR 的"无适用案例"原则——强行复用错误案例比不给更糟)。
3112
+ """
3113
+ n = len(q_terms)
3114
+ if n <= 3:
3115
+ return 2
3116
+ return min(4, max(2, int(n * 0.3 + 0.999)))
3117
+
3118
+
3119
+ def _prior_hits(q_terms: set, text: str) -> int:
3120
+ return len(q_terms & terms(text))
3121
+
3122
+
3123
+ def _is_weak_text(text: str) -> bool:
3124
+ """标题碎片/极短片段不适合当"以前做过"的证据(如只有 ## 七、崩溃与检查)。"""
3125
+ s = str(text or "").strip()
3126
+ body = re.sub(r"^#{1,6}\s*", "", s)
3127
+ body = re.sub(r"[\s\W_]+", "", body)
3128
+ if len(body) < 12:
3129
+ return True
3130
+ return bool(re.match(r"^#{1,6}\s", s)) and len(body) < 40
3131
+
3132
+
3104
3133
  def prior_work(task: str, k: int = 6) -> dict:
3105
3134
  """开工前先查:这件事以前做过 / 调研过吗。
3106
3135
 
@@ -3115,12 +3144,41 @@ def prior_work(task: str, k: int = 6) -> dict:
3115
3144
  "total": 0, "latest_ts": None, "summary": ""}
3116
3145
  if not task:
3117
3146
  return out
3147
+ # 相关性门槛(v3.96,v3 版):用**查询词覆盖率**(命中数 / 查询词数)判定。
3148
+ # 为什么不看 recall 综合分:它含新鲜度/重要性,最近记忆天然 0.35+,等于没筛。
3149
+ # 为什么不用"命中词数":中文 terms() 是字符二元组,"有没/没有/查下/研下"这类
3150
+ # 跨词碎片能凑够好几个"命中",无关旧对话也能过线。
3151
+ # 实测区分度(真库):真命中覆盖率 60~100%,垃圾 13~17% → 门槛 35% 且命中≥2。
3152
+ # 不够就**弃权不注入**(CBR 的"无适用案例"原则:给错案例比不给更糟)。
3153
+ _COV_MIN = 0.35
3154
+ _HIT_MIN = 2
3155
+ q_terms = {x for x in terms(task) if len(x) >= 2}
3156
+ out["gate"] = {"query_terms": len(q_terms), "min_coverage": _COV_MIN, "min_hits": _HIT_MIN}
3157
+
3158
+ def _cov(text: str) -> float:
3159
+ if not q_terms or not text:
3160
+ return 0.0
3161
+ return len(q_terms & terms(text)) / len(q_terms)
3162
+
3163
+ def _relevant(text: str) -> bool:
3164
+ if not q_terms or not text or _is_weak_text(text):
3165
+ return False
3166
+ hit = len(q_terms & terms(text))
3167
+ return hit >= _HIT_MIN and (hit / len(q_terms)) >= _COV_MIN
3168
+
3169
+ def _pass_text(text: str) -> bool:
3170
+ return bool(text) and not _is_weak_text(text)
3171
+
3118
3172
  # 1) 知识:已确认的结论优先复用(pending 的标出来,提醒"还没定论")
3119
3173
  try:
3120
3174
  import growth as _growth
3121
- for rec, score in _growth.search_knowledge(task, k=k)[:_PRIOR_MAX_ITEMS]:
3175
+ for rec, score in _growth.search_knowledge(task, k=k * 2):
3176
+ if len(out["knowledge"]) >= _PRIOR_MAX_ITEMS:
3177
+ break
3122
3178
  if _agg_like(rec):
3123
3179
  continue
3180
+ if not _relevant(f"{rec.get('title') or ''} {rec.get('body') or ''}"):
3181
+ continue
3124
3182
  out["knowledge"].append({
3125
3183
  "id": rec.get("id", ""),
3126
3184
  "title": str(rec.get("title") or "")[:120],
@@ -3129,8 +3187,12 @@ def prior_work(task: str, k: int = 6) -> dict:
3129
3187
  "created": int(_num(rec.get("created"), 0)),
3130
3188
  })
3131
3189
  # 1b) 笔记:交接笔记/日记里常有"这轮做了什么、结论是什么"的浓缩
3132
- for rec, score in _growth.search_notes(task, k=k)[:2]:
3133
- if score <= 0.02 or _agg_like(rec):
3190
+ for rec, score in _growth.search_notes(task, k=k * 2):
3191
+ if len(out["notes"]) >= 2:
3192
+ break
3193
+ if _agg_like(rec):
3194
+ continue
3195
+ if not _relevant(f"{rec.get('title') or ''} {rec.get('body') or ''}"):
3134
3196
  continue
3135
3197
  out["notes"].append({
3136
3198
  "id": rec.get("id", ""),
@@ -3140,10 +3202,16 @@ def prior_work(task: str, k: int = 6) -> dict:
3140
3202
  })
3141
3203
  except Exception as exc: # noqa: BLE001
3142
3204
  log_event("prior", f"查知识/笔记失败(不影响其它来源):{exc}", level="warn")
3143
- # 2) 教训:踩过的坑(词面命中优先)
3205
+
3206
+ # 2) 教训:只留 injectable_lessons 自己判为真命中的(它用词面交集/jaccard 打
3207
+ # _hit 标记);没打标记的是它"补足到 k"塞进来的无关条目。
3144
3208
  try:
3145
- for les in injectable_lessons(task, k=k)[:_PRIOR_MAX_ITEMS]:
3146
- if _agg_like(les):
3209
+ for les in injectable_lessons(task, k=k * 2):
3210
+ if len(out["lessons"]) >= _PRIOR_MAX_ITEMS:
3211
+ break
3212
+ if not les.get("_hit") or _agg_like(les):
3213
+ continue
3214
+ if not _relevant(str(les.get("text") or "")):
3147
3215
  continue
3148
3216
  out["lessons"].append({
3149
3217
  "id": les.get("id", ""),
@@ -3153,21 +3221,24 @@ def prior_work(task: str, k: int = 6) -> dict:
3153
3221
  })
3154
3222
  except Exception as exc: # noqa: BLE001
3155
3223
  log_event("prior", f"查教训失败:{exc}", level="warn")
3224
+
3156
3225
  # 3) 记忆:过程记录(strengthen=False 只读;mode="fast" 强制词面——
3157
3226
  # 这是每轮开工前的钩子,auto 会去调 embedding(实测 4.1s vs 37ms),
3158
3227
  # 每轮多 4 秒的延迟换那点语义收益不值。)
3228
+ # 门槛看 parts['relevance'](纯相关度),**不看** recall 的综合分——后者含
3229
+ # 新鲜度/重要性,最近记忆天然 0.35+,等于没筛。
3159
3230
  try:
3160
- for m, score, _parts in recall(task, k=k, strengthen=False, mode="fast")[:_PRIOR_MAX_ITEMS]:
3161
- if score < _PRIOR_MEM_SCORE:
3162
- continue
3163
- # 做梦机械聚合条目对"以前做过吗"没有增量信息(原文各自还在库里),跳过
3164
- if _agg_like(m):
3231
+ for m, score, _parts in recall(task, k=k * 3, strengthen=False, mode="fast"):
3232
+ if len(out["memories"]) >= _PRIOR_MAX_ITEMS:
3233
+ break
3234
+ rel = _num((_parts or {}).get("relevance"), 0.0)
3235
+ if _agg_like(m) or not _relevant(str(m.get("text") or "")):
3165
3236
  continue
3166
3237
  out["memories"].append({
3167
3238
  "id": m.get("id", ""),
3168
3239
  "text": str(m.get("text") or "")[:200],
3169
3240
  "created": int(_num(m.get("created"), 0)),
3170
- "score": round(float(score), 2),
3241
+ "score": round(float(rel), 3),
3171
3242
  })
3172
3243
  except Exception as exc: # noqa: BLE001
3173
3244
  log_event("prior", f"查记忆失败:{exc}", level="warn")