Skip to content
Python

feat(sdk): store raw conversations already trimmed; history concatenates without re-trimming

alvinfei3772a69c6b37
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 00c9fe9..ca28e81 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,34 @@ Hy Memory and MemHub are versioned and released independently.
## Unreleased
+### Hy Memory (next release)
+
+- Raw conversations are stored already trimmed. MemHub now keeps every raw
+ forever and lite mode stores nothing else, so tool output and long pasted
+ logs no longer stay in full. One rule (`hy_memory/utils/raw_trim.py`) is
+ applied to each message when the raw is written:
+ - `user` and `system` turns are kept in full;
+ - `assistant` turns and other speakers (`[John]:`) longer than 500
+ characters, after stripping, are cut to the first 500 plus `...`. This is
+ the limit history injection used before;
+ - `tool` turns keep their head and tail, and the middle becomes
+ `[...N bytes omitted by truncate...]`, with the coding limit
+ `coding.tool_result_max_bytes` (`MEMORY_CODING_TOOL_RESULT_MAX_BYTES`,
+ default 2048).
+
+ The rule is idempotent. It covers chat `L1_RAW` in lite, pro and ultra (lite
+ keeps tool turns, because it has no coding route) and the `memory_history`
+ ADD record. The coding/working `coding_trace` was already trimmed when it
+ was written. Its tool and assistant truncations are now idempotent, so
+ reading the history back no longer rewrites the omitted-count markers. The
+ text embedded for `L1_RAW` is the trimmed text. New raws carry
+ `custom.raw_trimmed=true`.
+- Extractor history ("last k messages") now joins the stored raws directly
+ and does not trim them again. Raws written before this change have no
+ `raw_trimmed` flag, so they are still trimmed with the same rule when they
+ are read. The extractor still receives the current batch in full (tool
+ turns are still removed on the chat route).
+
## MemHub 1.0.5 — 2026-10-28
Build 36, on `hy-memory==2.0.8`. Linux x64/arm64 Runtime and macOS arm64
diff --git a/hy_memory/client.py b/hy_memory/client.py
index 8473917..99285cd 100644
--- a/hy_memory/client.py
+++ b/hy_memory/client.py
@@ -3089,9 +3089,18 @@ class HyMemoryClient:
if self._history_store is not None:
try:
isolation_key = self._build_isolation_key(uid, agent_id)
- # 从 content 或 messages 中提取用于记录的文本
+ # 从 content 或 messages 中提取用于记录的文本。messages 与 L1_RAW
+ # 同一规则先裁剪再落审计库(工具输出 / 长回复不原样长期保存)。
+ from .utils.raw_trim import trim_raw_messages
history_content = content or "; ".join(
- f"{m.role}: {m.content}" for m in chat_messages
+ f"{m.role}: {m.content}"
+ for m in trim_raw_messages(
+ chat_messages,
+ tool_max_bytes=getattr(
+ getattr(self._config, "coding", None),
+ "tool_result_max_bytes", None,
+ ),
+ )
)
await self._history_store.record_add(
memory_id=result.memory_id,
diff --git a/hy_memory/coding/preproc.py b/hy_memory/coding/preproc.py
index 7f5d05d..260bd71 100644
--- a/hy_memory/coding/preproc.py
+++ b/hy_memory/coding/preproc.py
@@ -11,6 +11,7 @@ Coding Memory 预处理工具(规则,零 LLM)
使用方式与 scene 路由见 docs/scene_coding_usage.md。
"""
+import re
from typing import Any, Dict, List, Optional, Tuple
from ..pipelines.base import ChatMessage
@@ -63,6 +64,13 @@ DEFAULT_MAX_TOOL_RESULT_BYTES = 2048
DEFAULT_HEAD_BYTES = 1024
DEFAULT_TAIL_BYTES = 512
+# 本模块自己产出的省略标记。截断结果会再次经过截断(落库前一次、读历史时又一次),
+# 认出自己的标记才能保证幂等:已截断的文本再截一次原样返回,而不是把省略计数改写。
+_TOOL_OMIT_MARKER_RE = re.compile(r"\n\n\[\.\.\.\d+ bytes omitted by truncate\.\.\.\]\n\n")
+# 标记本身的字节数上限(数字位数有限),用于判断「已截断且只超出一个标记的长度」。
+_TOOL_OMIT_MARKER_MAX_BYTES = 128
+_ASSISTANT_OMIT_MARKER_RE = re.compile(r" \[\.\.\.\d+ chars omitted\.\.\.\] ")
+
def truncate_tool_result_text(
text: str,
@@ -81,6 +89,12 @@ def truncate_tool_result_text(
raw = text.encode("utf-8")
if len(raw) <= max_bytes:
return text
+ # 已截断过(max_bytes 很小时标记本身会让结果略超上限):原样返回,保证幂等。
+ if (
+ len(raw) <= max_bytes + _TOOL_OMIT_MARKER_MAX_BYTES
+ and _TOOL_OMIT_MARKER_RE.search(text)
+ ):
+ return text
# 自定义 max_bytes 可能小于默认 head+tail(1024+512)。按原比例缩放内容窗口,
# 给省略标记预留空间,不能因为窗口和过大而把超限原文直接返回。
marker_budget = min(96, max(24, max_bytes // 4))
@@ -161,6 +175,10 @@ def truncate_assistant_text(
text = "" if text is None else str(text)
if len(text) <= head_chars + tail_chars:
return text
+ # 已截断过:中段恰好是本函数的省略标记 → 原样返回,保证幂等(否则会把
+ # 省略计数改写成标记自身的长度)。
+ if _ASSISTANT_OMIT_MARKER_RE.fullmatch(text[head_chars:len(text) - tail_chars]):
+ return text
omitted = len(text) - head_chars - tail_chars
return (
text[:head_chars]
diff --git a/hy_memory/pipelines/writer.py b/hy_memory/pipelines/writer.py
index 23d5a52..bb0d67e 100644
--- a/hy_memory/pipelines/writer.py
+++ b/hy_memory/pipelines/writer.py
@@ -280,6 +280,22 @@ class MemoryWriter(WritePipeline):
custom["source_raw_memory_ids"] = source_ids
return custom
+ def _flat_trimmed_raw(self, request: WriteRequest) -> str:
+ """把 request.messages 按 raw 统一规则裁剪后拼成 L1_RAW 文本。
+
+ 只裁落库副本;request.messages 不动(本次 extractor 输入仍用完整 messages)。
+ """
+ import copy as _copy
+ from ..utils.raw_trim import trim_raw_messages
+
+ _coding_cfg = getattr(getattr(self, "config", None), "coding", None)
+ _trimmed_req = _copy.copy(request)
+ _trimmed_req.messages = trim_raw_messages(
+ request.messages,
+ tool_max_bytes=getattr(_coding_cfg, "tool_result_max_bytes", None),
+ )
+ return _trimmed_req.get_flat_content()
+
@staticmethod
def _attach_client_meta(custom: Dict[str, Any], request: Optional[WriteRequest]) -> None:
"""给新建节点挂上调用方 meta(单份 list)。
@@ -2554,9 +2570,14 @@ class MemoryWriter(WritePipeline):
try:
# 参数校验
content = request.content
+ # raw 在写入时就按统一规则裁好(见 utils/raw_trim.py):工具输出、长
+ # assistant 回复不再原样长期存储;历史拼接直接用存下来的文本,不再二次裁剪。
+ # request.messages 保持原样——本次 extractor 输入仍是完整 messages。
+ _raw_trimmed = False
if not content and request.has_messages():
- content = request.get_flat_content()
+ content = self._flat_trimmed_raw(request)
request.content = content
+ _raw_trimmed = True
if not content:
response.error_code = 400
@@ -2627,6 +2648,9 @@ class MemoryWriter(WritePipeline):
vector_store = await self._get_vector_store()
layer_enum = MemoryLayer.from_string(suggested_layer)
_raw_custom: Dict[str, Any] = {}
+ if _raw_trimmed:
+ from ..utils.raw_trim import RAW_TRIMMED_KEY
+ _raw_custom[RAW_TRIMMED_KEY] = True
self._attach_client_meta(_raw_custom, request)
mem_node = MemoryNode(
user_id=request.user_id,
@@ -2929,13 +2953,16 @@ class MemoryWriter(WritePipeline):
agent_id: Optional[str] = None,
exclude_memory_id: str = "",
max_turns: int = 20,
- max_chars_assistant: int = 500,
+ max_chars_assistant: Optional[int] = None,
) -> str:
"""
获取该用户最近的 L1_RAW 对话记录,按轮次拆分后拼成 history context。
每条 L1_RAW 的 content 是多轮对话([user]: ...\n[assistant]: ...)。
- 拆成单条 message 后,user 消息完整保留,assistant 消息截取前 max_chars_assistant 字符。
+ raw 在写入时已按 utils/raw_trim.py 的规则裁好(custom[RAW_TRIMMED_KEY]),
+ 这里拆成单条 message 后直接拼接,不再二次裁剪。该规则之前写入的旧 raw
+ 没有这个标记,仍按同一(幂等)规则裁剪后再拼,避免旧的长回复 / 工具输出
+ 撑大 prompt。max_chars_assistant 只作用于这些旧 raw,默认同写入规则。
最终取最近 max_turns 条 message(注意单位是 message 条数,非对话轮数;
调用方按 k 轮 × 2 传入)。
@@ -2977,23 +3004,33 @@ class MemoryWriter(WritePipeline):
# 避免把正文里形如 "[注]: xxx" 的普通文本误判成消息边界。
import re as _re
from .base import parse_user_name_line
- _KNOWN_ROLES = {"user", "assistant", "system", "tool"}
# 带方括号的任意 label(含对方真人名字)
_labeled_prefix = _re.compile(r"^\[([^\]\n]{1,32})\]:\s?")
# 无方括号的已知 role(历史数据容错,沿用旧正则的 `\[?...\]?` 宽容度)
_bare_role_prefix = _re.compile(r"^(user|assistant|system|tool):\s?")
- # (label, content) 元组列表;label 为 role 或对方真人名字
- parsed: List[Tuple[str, str]] = []
+ from ..utils.raw_trim import (
+ DEFAULT_RAW_ASSISTANT_MAX_CHARS,
+ RAW_TRIMMED_KEY,
+ trim_raw_body,
+ )
+ if max_chars_assistant is None:
+ max_chars_assistant = DEFAULT_RAW_ASSISTANT_MAX_CHARS
+ _coding_cfg = getattr(getattr(self, "config", None), "coding", None)
+ _tool_max_bytes = getattr(_coding_cfg, "tool_result_max_bytes", None)
+
+ # (label, content, 是否旧 raw 需兼容裁剪) 元组列表;label 为 role 或对方真人名字
+ parsed: List[Tuple[str, str, bool]] = []
user_name: str = ""
for node in nodes:
content = node.content or ""
+ _legacy = not bool((node.custom or {}).get(RAW_TRIMMED_KEY))
for _line_idx, line in enumerate(content.split("\n")):
m = _labeled_prefix.match(line) or _bare_role_prefix.match(line)
if m:
label = m.group(1)
body = line[m.end():]
- parsed.append((label, body))
+ parsed.append((label, body, _legacy))
continue
# 名字行只可能是每个 node content 的第一行(get_flat_content 插在文首)。
# 逐 node 判断而非用 `not parsed`:parsed 跨 node 累积,第二个 node
@@ -3006,18 +3043,18 @@ class MemoryWriter(WritePipeline):
continue
if parsed:
# 续行:拼回上一条 message(保留换行)
- prev_label, prev_body = parsed[-1]
- parsed[-1] = (prev_label, prev_body + "\n" + line)
+ prev_label, prev_body, prev_legacy = parsed[-1]
+ parsed[-1] = (prev_label, prev_body + "\n" + line, prev_legacy)
else:
# 开头就是无前缀行(异常/老数据):当成 user 消息兜底
- parsed.append(("user", line))
+ parsed.append(("user", line, _legacy))
# 取最近 max_turns 条 message(按 message 截断,绝不从消息中间切开)
recent = parsed[-max_turns:] if len(parsed) > max_turns else parsed
if not recent:
return ""
- # 格式化:非 user 侧整条截断(含对方真人),user 完整保留
+ # 格式化:新 raw 直接拼接;旧 raw 按写入同一规则兼容裁剪
all_messages: List[str] = []
if user_name:
from .base import render_user_name_line
@@ -3025,17 +3062,18 @@ class MemoryWriter(WritePipeline):
_name_line = render_user_name_line(
user_name,
- is_chinese("\n".join(b for _, b in recent)),
+ is_chinese("\n".join(b for _, b, _ in recent)),
)
if _name_line:
all_messages.append(_name_line)
- for label, body in recent:
+ for label, body, legacy in recent:
+ if legacy:
+ body = trim_raw_body(
+ label, body,
+ assistant_max_chars=max_chars_assistant,
+ tool_max_bytes=_tool_max_bytes,
+ )
body = body.strip()
- # 截断口径:assistant + 对方真人(非已知 role 的 label)。
- # user 完整保留;system / tool 沿用历史行为(不截断)。
- _truncate = label == "assistant" or label not in _KNOWN_ROLES
- if _truncate and len(body) > max_chars_assistant:
- body = body[:max_chars_assistant] + "..."
all_messages.append(f"[{label}]: {body}")
return "\n".join(all_messages)
diff --git a/hy_memory/utils/raw_trim.py b/hy_memory/utils/raw_trim.py
new file mode 100644
index 0000000..071f5ca
--- /dev/null
+++ b/hy_memory/utils/raw_trim.py
@@ -0,0 +1,91 @@
+# -*- coding: utf-8 -*-
+"""
+原始对话(raw)落库前的统一裁剪规则(规则,零 LLM)。
+
+raw 会被长期保存(MemHub 已取消 raw 的保留期限;lite 模式下 raw 是唯一存储),
+不裁剪会把工具输出、大段粘贴日志原样存下来。因此 raw 在**写入时**就裁好,
+读取方(extractor 的「最近 k 条」历史注入)直接拼接,不再二次裁剪。
+
+规则按说话人标签(ChatMessage.speaker_label() / L1_RAW 里的 ``[label]``):
+
+- ``user``:完整保留;
+- ``system``:完整保留(与历史注入原口径一致);
+- ``tool``:头尾保留、中段替换为 ``[...N bytes omitted by truncate...]``,
+ 上限为 ``coding.tool_result_max_bytes``(env ``MEMORY_CODING_TOOL_RESULT_MAX_BYTES``,
+ 默认 2048),与 coding 链路同一个函数;
+- 其余(``assistant``、对方真人名字等非已知 role 的 label):strip 后超过
+ ``assistant_max_chars``(默认 500,即历史注入原来的 ``max_chars_assistant``)
+ 时截成前 N 个字符 + ``...``。
+
+所有规则确定且幂等:对已裁剪的文本再裁一次结果不变。
+"""
+
+from typing import List, Optional
+
+from ..pipelines.base import ChatMessage
+
+# 历史注入原 `_get_recent_history(max_chars_assistant=500)` 的默认值,现为唯一来源。
+DEFAULT_RAW_ASSISTANT_MAX_CHARS = 500
+
+# L1_RAW 节点 custom 上的标记:内容已按本模块规则在写入时裁剪。
+# 历史拼接据此区分「新 raw 直接拼接」与「本规则之前写入的旧 raw 需兼容裁剪」。
+RAW_TRIMMED_KEY = "raw_trimmed"
+
+# 不裁剪的标签(tool 单独处理)
+_KEEP_LABELS = frozenset({"user", "system"})
+
+
+def trim_raw_body(
+ label: str,
+ body: str,
+ *,
+ assistant_max_chars: int = DEFAULT_RAW_ASSISTANT_MAX_CHARS,
+ tool_max_bytes: Optional[int] = None,
+) -> str:
+ """按说话人标签裁剪单条消息正文。幂等。"""
+ if not isinstance(body, str):
+ body = "" if body is None else str(body)
+ if label in _KEEP_LABELS:
+ return body
+ if label == "tool":
+ from ..coding.preproc import (
+ DEFAULT_MAX_TOOL_RESULT_BYTES,
+ truncate_tool_result_text,
+ )
+
+ return truncate_tool_result_text(
+ body, max_bytes=int(tool_max_bytes or DEFAULT_MAX_TOOL_RESULT_BYTES),
+ )
+ stripped = body.strip()
+ if len(stripped) > assistant_max_chars:
+ return stripped[:assistant_max_chars] + "..."
+ return body
+
+
+def trim_raw_messages(
+ messages: List[ChatMessage],
+ *,
+ assistant_max_chars: int = DEFAULT_RAW_ASSISTANT_MAX_CHARS,
+ tool_max_bytes: Optional[int] = None,
+) -> List[ChatMessage]:
+ """返回裁剪后的新列表;不修改入参(messages 仍要原样给 extractor)。"""
+ out: List[ChatMessage] = []
+ for m in messages:
+ new_content = trim_raw_body(
+ m.speaker_label(),
+ m.content,
+ assistant_max_chars=assistant_max_chars,
+ tool_max_bytes=tool_max_bytes,
+ )
+ if new_content == m.content:
+ out.append(m)
+ continue
+ out.append(ChatMessage(
+ role=m.role,
+ content=new_content,
+ name=m.name,
+ tool_calls=list(m.tool_calls),
+ tool_call_id=m.tool_call_id,
+ tool_name=m.tool_name,
+ ))
+ return out
diff --git a/tests/test_raw_trim_on_store.py b/tests/test_raw_trim_on_store.py
new file mode 100644
index 0000000..efa98bf
--- /dev/null
+++ b/tests/test_raw_trim_on_store.py
@@ -0,0 +1,376 @@
+# -*- coding: utf-8 -*-
+"""
+raw 在写入时就裁剪好;历史拼接不再二次裁剪。
+
+规则(hy_memory/utils/raw_trim.py):
+ - user / system 完整保留
+ - assistant 与对方真人(非已知 role 的 label)strip 后超过 500 字符截成前 500 + "..."
+ - tool 头尾保留、中段替换为 "[...N bytes omitted by truncate...]"
+ (上限 coding.tool_result_max_bytes)
+ - 全部幂等
+
+覆盖:
+ 1. 规则本身 + 幂等(含 coding preproc 的两个截断函数)
+ 2. lite:L1_RAW 存的是裁剪后的文本(tool 头尾截断 + 标记),带 raw_trimmed 标记;
+ 审计 history 的 ADD 记录同样裁剪
+ 3. coding_trace 存的 tool result 已头尾截断
+ 4. pro:extractor 输入仍是完整 messages;历史 = 已存 raw 直接拼接;
+ 没有标记的旧 raw 仍按同一规则兼容裁剪
+"""
+
+import asyncio
+import json
+import sqlite3
+import types
+import uuid
+from datetime import datetime, timedelta
+from typing import Any, Dict, List, Optional
+
+import pytest
+
+from hy_memory.coding.preproc import (
+ truncate_assistant_text,
+ truncate_tool_result_text,
+)
+from hy_memory.models.memory import MemoryLayer, MemoryNode, MemoryStatus
+from hy_memory.pipelines.base import ChatMessage, WriteRequest
+from hy_memory.utils.raw_trim import (
+ DEFAULT_RAW_ASSISTANT_MAX_CHARS,
+ RAW_TRIMMED_KEY,
+ trim_raw_body,
+ trim_raw_messages,
+)
+
+_MARKER = "bytes omitted by truncate...]"
+_LONG_USER = "我把整段日志贴给你:" + "U" * 5000
+_LONG_ASSISTANT = "A" * 1200
+_LONG_TOOL = "H" * 3000 + "MIDDLE" + "T" * 3000
+
+
+def _messages() -> List[ChatMessage]:
+ return [
+ ChatMessage(role="system", content="S" * 900),
+ ChatMessage(role="user", content=_LONG_USER),
+ ChatMessage(role="assistant", content=_LONG_ASSISTANT),
+ ChatMessage(role="tool", content=_LONG_TOOL, tool_call_id="c1", tool_name="Bash"),
+ ChatMessage(role="assistant", name="John", content="J" * 800),
+ ]
+
+
+# ---------------------------------------------------------------- 1. 规则
+
+
+class TestTrimRule:
+ def test_user_kept_in_full(self):
+ assert trim_raw_body("user", _LONG_USER) == _LONG_USER
+
+ def test_system_kept_in_full(self):
+ assert trim_raw_body("system", "S" * 900) == "S" * 900
+
+ def test_long_assistant_cut_at_limit(self):
+ out = trim_raw_body("assistant", _LONG_ASSISTANT)
+ assert out == "A" * DEFAULT_RAW_ASSISTANT_MAX_CHARS + "..."
+
+ def test_short_assistant_untouched(self):
+ assert trim_raw_body("assistant", " short ") == " short "
+
+ def test_other_speaker_cut_like_assistant(self):
+ assert trim_raw_body("John", "J" * 800) == "J" * 500 + "..."
+
+ def test_tool_head_tail_with_marker(self):
+ out = trim_raw_body("tool", _LONG_TOOL, tool_max_bytes=2048)
+ assert out.startswith("H" * 100) and out.endswith("T" * 100)
+ assert "MIDDLE" not in out and _MARKER in out
+ assert len(out.encode("utf-8")) <= 2048
+
+ def test_messages_trimmed_without_mutating_input(self):
+ msgs = _messages()
+ out = trim_raw_messages(msgs, tool_max_bytes=512)
+ assert [m.content for m in msgs][1] == _LONG_USER
+ assert msgs[2].content == _LONG_ASSISTANT, "入参不能被修改"
+ assert out[1].content == _LONG_USER
+ assert out[2].content == "A" * 500 + "..."
+ assert _MARKER in out[3].content and out[3].tool_call_id == "c1"
+ assert out[4].content == "J" * 500 + "..." and out[4].name == "John"
+
+ @pytest.mark.parametrize("max_bytes", [48, 64, 200, 512, 2048])
+ def test_idempotent(self, max_bytes):
+ once = trim_raw_messages(_messages(), tool_max_bytes=max_bytes)
+ twice = trim_raw_messages(once, tool_max_bytes=max_bytes)
+ assert [m.content for m in twice] == [m.content for m in once]
+ for label in ("assistant", "John", "tool", "user", "system"):
+ for body in (_LONG_ASSISTANT, _LONG_TOOL, "中文" * 2000, " x " * 400):
+ t1 = trim_raw_body(label, body, tool_max_bytes=max_bytes)
+ assert trim_raw_body(label, t1, tool_max_bytes=max_bytes) == t1
+
+ def test_preproc_truncations_idempotent(self):
+ t = truncate_tool_result_text(_LONG_TOOL, max_bytes=64)
+ assert truncate_tool_result_text(t, max_bytes=64) == t
+ a = truncate_assistant_text("x" * 10000, head_chars=30, tail_chars=30)
+ assert truncate_assistant_text(a, head_chars=30, tail_chars=30) == a
+
+
+# ---------------------------------------------------------------- 2. lite 端到端
+
+
+def _lite_config(tmp_path):
+ from hy_memory import MemoryConfig
+
+ return MemoryConfig.from_dict({
+ "vector_store": {
+ "provider": "chroma",
+ "collection_name": f"test_raw_trim_{uuid.uuid4().hex[:8]}",
+ "persist_directory": str(tmp_path / "chroma"),
+ "embedding_dims": 8,
+ },
+ "embedder": {"embedding_dims": 8},
+ "cache": {"backend": "sqlite", "sqlite_path": str(tmp_path / "cache.db")},
+ "history": {"enable": True, "db_path": str(tmp_path / "history.db")},
+ "coding": {"enable": True, "tool_result_max_bytes": 512},
+ })
+
+
+def _install_fixed_embed(client):
+ vector = [1.0] + [0.0] * 7
+
+ async def one(_self, _text, use_cache=True):
+ return list(vector)
+
+ async def one_meta(_self, _text, use_cache=True):
+ return list(vector), {"cache_hit": False, "provider": "test"}
+
+ async def batch(_self, texts, use_cache=True):
+ return [list(vector) for _ in texts]
+
+ s = client._embed_service
+ s.embed = types.MethodType(one, s)
+ s.embed_queued = types.MethodType(one, s)
+ s.embed_with_meta = types.MethodType(one_meta, s)
+ s.embed_queued_with_meta = types.MethodType(one_meta, s)
+ s.embed_batch = types.MethodType(batch, s)
+
+
+def test_lite_stores_trimmed_raw(tmp_path, monkeypatch):
+ from hy_memory import HyMemoryClient
+
+ monkeypatch.setenv("MEMORY_DATA_DIR", str(tmp_path / "data"))
+ monkeypatch.setenv("MEMORY_LOG_DIR", str(tmp_path / "logs"))
+ monkeypatch.setenv("MEMORY_PIPELINE_TRACE_ENABLED", "false")
+ client = HyMemoryClient(_lite_config(tmp_path), mode="lite")
+ try:
+ _install_fixed_embed(client)
+ resp = client.add(
+ [
+ {"role": "user", "content": _LONG_USER},
+ {"role": "assistant", "content": _LONG_ASSISTANT,
+ "tool_calls": [{"id": "c1", "type": "function",
+ "function": {"name": "Bash", "arguments": "{}"}}]},
+ {"role": "tool", "tool_call_id": "c1", "content": _LONG_TOOL},
+ ],
+ user_id="u1",
+ )
+ assert resp["success"] is True, resp
+
+ async def _raw_nodes():
+ return await client._vector_store.list_by_user(
+ user_id="u1", layers=[MemoryLayer.L1_RAW],
+ with_vectors=False, allow_raw_layer=True,
+ )
+
+ nodes = client._loop_thread.run(_raw_nodes())
+ assert len(nodes) == 1
+ content = nodes[0].content
+ assert f"[user]: {_LONG_USER}" in content, "user 必须完整保留"
+ assert f"[assistant]: {'A' * 500}...\n" in content
+ tool_line = content.split("[tool]: ", 1)[1]
+ assert tool_line.startswith("H") and tool_line.endswith("T")
+ assert _MARKER in tool_line and "MIDDLE" not in tool_line
+ assert len(tool_line.encode("utf-8")) <= 512
+ assert (nodes[0].custom or {}).get(RAW_TRIMMED_KEY) is True
+
+ conn = sqlite3.connect(str(tmp_path / "history.db"))
+ try:
+ (audit,) = conn.execute(
+ "SELECT new_memory FROM memory_history WHERE event='ADD'"
+ ).fetchone()
+ finally:
+ conn.close()
+ assert _LONG_USER in audit and "MIDDLE" not in audit
+ assert "A" * 501 not in audit
+ finally:
+ client.close()
+
+
+# ---------------------------------------------------------------- 3. coding_trace
+
+
+def test_coding_trace_stores_truncated_tool_result(tmp_path):
+ from hy_memory.coding.writer import CodingWriter
+
+ captured: Dict[str, Any] = {}
+
+ class _Store:
+ config = types.SimpleNamespace(
+ coding=types.SimpleNamespace(tool_result_max_bytes=512),
+ extractor=types.SimpleNamespace(history_turns=5),
+ )
+
+ async def get_recent_trace_messages(self, **kw):
+ return []
+
+ async def append_trace(self, messages, **kw):
+ captured["messages"] = messages
+
+ class _Extractor:
+ async def extract(self, messages, **kw):
+ return [], []
+
+ w = CodingWriter.__new__(CodingWriter)
+ w.variant = "working"
+ w.store = _Store()
+ w.extractor = _Extractor()
+ asyncio.run(w.write(_messages(), user_id="u1"))
+
+ stored = {m.role: m.content for m in captured["messages"] if m.role != "assistant"}
+ assert stored["user"] == _LONG_USER
+ assert _MARKER in stored["tool"] and "MIDDLE" not in stored["tool"]
+ assert len(stored["tool"].encode("utf-8")) <= 512
+
+
+# ---------------------------------------------------------------- 4. pro writer
+
+
+class _Store:
+ supports_fulltext = False
+
+ def __init__(self):
+ self.nodes: Dict[str, MemoryNode] = {}
+
+ async def initialize(self):
+ pass
+
+ async def upsert(self, node: MemoryNode) -> str:
+ node.node_id = node.node_id or uuid.uuid4().hex
+ if node.gmt_created is None:
+ node.gmt_created = datetime.now()
+ self.nodes[node.node_id] = node
+ return node.node_id
+
+ async def get_by_id(self, node_id):
+ return self.nodes.get(node_id)
+
+ async def list_by_user(self, *a, layers=None, **kw):
+ return [
+ n for n in self.nodes.values()
+ if not layers or n.layer in layers
+ ]
+
+ async def search(self, *a, **kw):
+ return []
+
+
+class _Embed:
+ async def embed_queued_with_meta(self, text):
+ return [0.0] * 8, {"provider": "test"}
+
+ async def embed_queued(self, text):
+ return [0.0] * 8
+
+ async def embed_batch(self, texts):
+ return [[0.0] * 8 for _ in texts]
+
+
+class _CapturingAgent:
+ def __init__(self):
+ self.calls: List[Dict[str, Any]] = []
+
+ async def process_add(self, **kwargs):
+ from tests.test_shadow_l1_raw_on_empty_extract import _FakeAgentResult
+
+ self.calls.append(kwargs)
+ return _FakeAgentResult(success=True, extracted_info={"memory": [], "intentions": []})
+
+
+def _pro_writer(store):
+ from hy_memory.config import MemoryConfig
+ from hy_memory.pipelines.writer import MemoryWriter
+
+ w = MemoryWriter(MemoryConfig(), embed_service=_Embed(), vector_store=store, cache=None)
+ w._mem_agent = _CapturingAgent()
+ w._vector_store = store
+
+ async def _empty_tags(*a, **kw):
+ return []
+
+ w._collect_existing_tags = _empty_tags
+ return w
+
+
+def _write(w, msgs: List[ChatMessage]):
+ req = WriteRequest(
+ content="", messages=msgs, user_id="u1", agent_id="default_agent",
+ session_id="s1", content_type="messages",
+ extra={"agent_mode": "full", "mode": "pro"},
+ request_id=uuid.uuid4().hex,
+ )
+ return asyncio.run(w.write(req))
+
+
+def test_pro_extraction_input_unchanged_and_history_concatenates():
+ store = _Store()
+ w = _pro_writer(store)
+ turn1 = [
+ ChatMessage(role="user", content=_LONG_USER),
+ ChatMessage(role="assistant", content=_LONG_ASSISTANT),
+ ]
+ turn2 = [
+ ChatMessage(role="user", content="第二轮"),
+ ChatMessage(role="assistant", content="B" * 900),
+ ]
+ r1 = _write(w, turn1)
+ assert r1.memory_id
+ r2 = _write(w, turn2)
+ assert r2.memory_id
+
+ calls = w._mem_agent.calls
+ assert len(calls) == 2
+ # extractor 收到的仍是完整 messages(没有被落库裁剪影响)
+ for call, msgs in zip(calls, (turn1, turn2)):
+ assert json.loads(call["content"]) == [
+ {"role": m.role, "content": m.content} for m in msgs
+ ]
+
+ raw1 = store.nodes[r1.memory_id]
+ assert raw1.content == f"[user]: {_LONG_USER}\n[assistant]: {'A' * 500}..."
+ assert raw1.custom.get(RAW_TRIMMED_KEY) is True
+ # 历史 = 已存 raw 直接拼接(第一次写入时没有历史)
+ assert calls[0]["history_context"] == ""
+ assert calls[1]["history_context"] == raw1.content
+
+
+def test_history_does_not_retrim_new_raw_but_trims_legacy_raw():
+ """新 raw(带标记)原样拼接;旧 raw(无标记)按同一规则兼容裁剪。"""
+ from hy_memory.pipelines.writer import MemoryWriter
+
+ store = _Store()
+ base = datetime.now()
+ # 新 raw:内容里刻意放一条超过 500 的 assistant——带标记就不应再被裁
+ new_content = "[user]: 新问题\n[assistant]: " + "N" * 700
+ legacy_content = "[user]: 旧问题\n[assistant]: " + "O" * 700 + "\n[tool]: " + _LONG_TOOL
+ store.nodes["legacy"] = MemoryNode(
+ node_id="legacy", user_id="u1", agent_id="default_agent",
+ layer=MemoryLayer.L1_RAW, content=legacy_content,
+ status=MemoryStatus.ACTIVE, gmt_created=base - timedelta(seconds=2),
+ )
+ store.nodes["new"] = MemoryNode(
+ node_id="new", user_id="u1", agent_id="default_agent",
+ layer=MemoryLayer.L1_RAW, content=new_content,
+ status=MemoryStatus.ACTIVE, gmt_created=base - timedelta(seconds=1),
+ custom={RAW_TRIMMED_KEY: True},
+ )
+ w = MemoryWriter.__new__(MemoryWriter)
+ history = asyncio.run(w._get_recent_history(store, "u1", "default_agent", max_turns=20))
+
+ assert history.endswith(new_content), "新 raw 必须原样拼接"
+ assert "[assistant]: " + "O" * 500 + "...\n" in history
+ assert "MIDDLE" not in history and _MARKER in history