lingshu-skill 5.31.0 → 5.32.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
@@ -5819,8 +5819,33 @@ def build_injection(query: str = "", budget_tokens: int | None = None,
5819
5819
  "budget_frac": round(t / budget, 4) if budget else 0.0})
5820
5820
  total_chars += c
5821
5821
  total_tokens += t
5822
- # 自检:各段 chars 求和必须严格等于最终文本长度,否则账单不可信。
5823
- assert total_chars == len(_result), (total_chars, len(_result))
5822
+ # 自检:各段 chars 求和应等于最终文本长度。
5823
+ # ⚠ 不能用 assert(v5.31 实测修):
5824
+ # 1) 上方对 _result 做过指针标记替换,长度会变,按未替换的 out 累加必然不等;
5825
+ # 2) 生产代码用 assert 校验数据,`python -O` 下会被整体跳过;
5826
+ # 3) 它把观测工具变成崩溃源——`context-stats` 会因为这里直接退出。
5827
+ # 改为:按真实长度把差额归到 other 段(保证 Σ段 == 总长、账单可信),
5828
+ # 同时记 warn,真实装配 bug 仍然看得见。
5829
+ if total_chars != len(_result):
5830
+ _delta = len(_result) - total_chars
5831
+ for _s in sections:
5832
+ if _s["name"] == "other":
5833
+ _s["chars"] += _delta
5834
+ _s["tokens"] = int(math.ceil(_s["chars"] / ratio)) if _s["chars"] > 0 else 0
5835
+ _s["budget_frac"] = round(_s["tokens"] / budget, 4) if budget else 0.0
5836
+ break
5837
+ else:
5838
+ sections.append({"name": "other", "chars": _delta,
5839
+ "tokens": int(math.ceil(max(0, _delta) / ratio)),
5840
+ "budget_frac": 0.0})
5841
+ total_chars = len(_result)
5842
+ total_tokens = sum(_s["tokens"] for _s in sections)
5843
+ try:
5844
+ log_event("inject",
5845
+ f"注入账单字符数与实际不符(已按实际校正 {_delta:+d}):"
5846
+ "通常是指针标记替换改变了长度", level="warn")
5847
+ except Exception: # noqa: BLE001
5848
+ pass
5824
5849
  report.update({"sections": sections, "total_chars": total_chars,
5825
5850
  "total_tokens": total_tokens, "budget": budget,
5826
5851
  "chars_per_token": ratio})