revert(memory): Hindsight document_id 改回每轮独立文档

commit dba821a 把 document_id 从带时间戳改成按 thread_id 确定性派生
(同一 thread 累积成一个文档)。经复核,改回原来的带微秒时间戳写法 ——
每轮对话的 retain 重新落到各自独立的 Hindsight 文档。

retain_every_n_turns 轮次节流(turn_index)与 aiohttp 客户端泄漏修复
保持不动,仅回退 document_id 这一处。

test_hindsight_provider.py 的 TestDocumentId 按"每轮独立文档"重写。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
xuhdandClaude Opus 4.7 committed 2026-05-18 15:24:13 +08:00
1 parent dba821a70e
commit 4aee413680
2 files changed
+22 -26

No files matched your search

@@ -20,8 +20,8 @@ HTTP 客户端方式接入一个用户自起的 Hindsight Docker 实例 (``local
document_id 与轮次计数
======================
本 provider 被 ``build_memory_manager`` 每轮重新构造 —— 是无状态的。因此:
- ``document_id`` 由 ``thread_id`` **确定性**派生,同一 thread 的所有轮次 retain
都落到同一个 Hindsight 文档,累积而非每轮碎片化成新文档。
- ``document_id`` 每次 ``initialize`` 用带时间戳的新值,每轮对话的 retain 落到
各自独立的 Hindsight 文档。
- ``retain_every_n_turns`` 的轮次判定依赖外部传入的 ``turn_index``
(由 ``MemoryMiddleware`` 按对话历史算出);未传时退回进程内 ``_turn_counter``。
@@ -168,14 +168,14 @@ class HindsightProvider(MemoryProvider):
return name.strip("-") or "deerflow"
def _reset_retain_batch(self) -> None:
"""重置 retain 批次状态。
"""重置 retain 批次状态 (新的 document_id + 轮次清零)。
``document_id`` 由 ``thread_id`` **确定性**派生 —— provider 虽然被每轮
重新构造,同一 thread 的所有轮次仍落到同一个 Hindsight 文档,累积成一份
而不是碎片化成一轮一个文档。无 thread 时 (例如 memory 工具镜像) 用统一
的兜底 id。轮次计数清零,仅作为 ``turn_index`` 缺失时的退路。
每次 ``initialize`` 用带时间戳的新 ``document_id`` —— 每轮对话的 retain
落到各自独立的 Hindsight 文档。轮次计数清零,仅作为 ``turn_index`` 缺失
时的退路。
"""
self._document_id = f"thread-{self._thread_id}" if self._thread_id else "thread-default"
ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
self._document_id = f"{self._thread_id or 'thread'}-{ts}"
self._turn_counter = 0
def shutdown(self) -> None: