Skip to content

fix(capture): clamp an oversized turn before it reaches EvidenceInput - #504

Open
zuizhuai wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
zuizhuai:fix/live-capture-oversized-verbatim
Open

zuizhuai wants to merge 1 commit into
Tencent-Hunyuan:mainfrom
zuizhuai:fix/live-capture-oversized-verbatim

Conversation

@zuizhuai

Copy link
Copy Markdown
Contributor

这个 PR 修什么

_clamp_verbatim() 是随历史导入修复一起进来的,但只挂在 to_evidence() 里 —— 也就是只覆盖了文件扫描这一条链路。live 采集的两条链路自己构造 EvidenceInput,把源文本原样传了进去:

位置 字段
live_capture._hook_inputs verbatim=verbatim(hook payload)
live_capture._capture_transcript verbatim=row.verbatim(宿主 transcript)

于是一条超过 MAX_VERBATIM_CHARACTERS 的粘贴会直接从 capture_hook() 里抛出来:

pydantic_core._pydantic_core.ValidationError: 1 validation error for EvidenceInput
verbatim
  String should have at most 1000000 characters [type=string_too_long, ...]
    src/one_memory/live_capture.py:588

而它上面的 HTTP 闸门(capture_handoff.MAX_PAYLOAD_BYTES)有意放宽到 8 MB,两个限制之间就留出了一段"完全合法却不工作的输入"。hook 的契约是 202 whatever the gates decide, so a hook has nothing to print —— 这里抛异常,丢掉的不止这一条 turn,还有这个 hook 本该 stage 的全部内容。

改法

把截断函数从 imports/sources/common.py 下沉到 imports/models.py(挨着 MAX_VERBATIM_CHARACTERS 放),三条链路共用:

# imports/models.py
def clamp_verbatim(text: str) -> str:
    """Keep one pathological record from violating EvidenceInput's limit. ..."""
  • common.py::to_evidence 改用 clamp_verbatim(本地那份 _clamp_verbatim 删掉,MAX_VERBATIM_CHARACTERS 的导入也随之不再需要)
  • live_capture.py 的两处 verbatim= 各包一层

为什么必须共用同一个函数,而不是各写一份:verbatim 正是 identity_text() 哈希的来源。hook 采集和稍后对同一段话的 transcript 扫描必须截出同一段文本,否则同一个 turn 会被当成两条记录写两次。函数的 docstring 里也写明了这一点。

测试

在 tests/test_live_capture.py 追加两个用例(沿用文件里已有的 Home / _staged 夹具):

  • test_an_oversized_prompt_is_truncated_instead_of_failing_the_capture —— hook payload 路径
  • test_an_oversized_transcript_line_is_truncated_instead_of_failing_the_capture —— Stop 读 transcript 路径

都断言 CaptureOutcome.STAGED、len(verbatim) <= MAX_VERBATIM_CHARACTERS、且带 truncated: original 1000005 chars 标记。

红 / 绿:

修复前  2 failed  (ValidationError: String should have at most 1000000 characters)
修复后  2 passed
相关文件全量  tests/test_live_capture.py + test_import_oversized_verbatim.py + test_chat_exports.py
             49 passed
ruff(memhub/apps/core 的配置)  All checks passed

没有改的两处

memory_additions.py 和 memory_server/routes.py 里还各有一处 EvidenceInput,但它们的文本是调用方直接给的(client.add() / messages 接口)。那里应该用一个明确的 4xx 拒绝超长输入,而不是悄悄留前半截 —— 静默截断等于替调用方丢数据。所以这次不动,留作单独决定。

相关

`_clamp_verbatim()` (added with the history-import fix) only runs inside
`to_evidence()`, i.e. the file-scan lane. Both live-capture lanes build
`EvidenceInput` themselves and passed the source text through untouched:

  live_capture._hook_inputs        verbatim=verbatim       (hook payload)
  live_capture._capture_transcript verbatim=row.verbatim   (host transcript)

so a single pasted dump over `MAX_VERBATIM_CHARACTERS` raised
`ValidationError: verbatim — String should have at most 1000000 characters`
straight out of `capture_hook()`. The HTTP gate above it
(`capture_handoff.MAX_PAYLOAD_BYTES`) deliberately accepts up to 8 MB, so the
two limits left an ordinary-sized payload failing the turn: the hook contract
is "202 whatever the gates decide, so a hook has nothing to print", and a raise
here breaks it for the turn and everything the hook would have staged.

Move the clamp next to `MAX_VERBATIM_CHARACTERS` in `imports/models.py` and
have all three lanes call it. One function, not three copies, is also what
keeps identity stable: `verbatim` is what `identity_text()` hashes, so a hook
capture and the later transcript scan of the same words have to truncate to
the same text or the turn is filed twice.

The two remaining `EvidenceInput` sites are caller-supplied (`memory_additions`
and the messages route); those should reject an oversized payload with a 4xx
rather than silently keep the head, which is a separate decision and not
changed here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant