Skip to content

fix(search): event summary unreachable regex + ratio threshold + blank speaker turns (#3598) - #3617

Closed
RerankerGuo wants to merge 1 commit into
volcengine:mainfrom
RerankerGuo:fix/issue-3598-event-summary-recall-degradation
Closed

RerankerGuo wants to merge 1 commit into
volcengine:mainfrom
RerankerGuo:fix/issue-3598-event-summary-recall-degradation

Conversation

@RerankerGuo

Copy link
Copy Markdown
Contributor

背景

Issue #3598 报告召回(POST /api/v1/search/recall)在 mode=summary 时,事件(events)层几乎把全部 max_chars 预算花在完整逐字聊天记录上——观测到线上 17 篇事件文档共 51,752 字符,其中 92.5% 是 ChatLog,单篇最高 88% 的 turn 是工具调用空 turn。一个 type="summary" 片段本应只有 465 字符,却渲染出 6466 字符(相当于 full 片段)。原因是 3 个小缺陷各自独立,但组合起来放大了开销:

  1. _extract_event_summary() 正则从未匹配:模板写 # Summary / # 2026-07-24 (Friday) ChatLog:,旧正则要求 Summary: / <date> ChatLog:(均无 # 标题前缀),两个端点都错位 → 始终走 fallback(全文)。Reporter 第一次验证时以为只改锚点即可,重验后确认 锚点 + 终止符 都需要放松([Bug]: Event summary degradation in recall is unreachable — # Summary heading vs Summary: regex, and three defects that compound with it #3598 correction Bump actions/download-artifact from 4 to 7 #1)。
  2. 模板硬编码 ratio_threshold=0:events.yaml 的 content_template 把 get_event_content(ranges, summary, 0) 第三参写死为 0,len(summary) / len(original) >= 0 永远为 true → 永远返回逐字 pretty-print transcript,而不是当 summary 是原文 20% 以内时优先用 summary。方法默认值 0.2,模板单点覆写使分支成为死代码。
  3. 工具调用空 turn 也写 speaker 行:_format_contiguous_group.flush_current() 无条件追加 **speaker**: {content}。纯 ToolCallPart / ToolPart 的 turn 没有 TextPart,_format_merged_content 为空串,每 turn 产生 36 字节 **alice**: 空行;样本里 55% 的 turn 是空 turn,占单文档字节数 40%,并且这些空行继续进入 embedding template。

Defect #2(fallback 片段不计入 type 级 budget)Reporter 已纠正为有意设计(见代码里 type_quota_recall.py:449-451 原注释),因此未改动。

改动内容

openviking/retrieve/type_quota_recall.py — 放松正则两端

_extract_event_summary():

r"(?is)^\s*\#*\s*Summary:?\s*(.*?)(?:\n\s*\#*\s*\d{4}-\d{2}-\d{2}"
r"(?:\s*\([^)]+\))?\s*ChatLog:|\n\s*\#*\s*ChatLog:|\n\s*<!--\s*MEMORY_FIELDS|$)"

openviking/prompts/templates/memory/events.yaml — 删除 ratio_threshold=0

L103 原来:{{ extract_context.get_event_content(ranges, summary, 0) }}
改成:{{ extract_context.get_event_content(ranges, summary) }}
→ 走 ExtractContext.get_event_content 方法签名默认值 0.2。

openviking/session/memory/memory_updater.py — flush_current 跳过空内容

_format_contiguous_group.flush_current() 在 append 前加 if content.strip():。纯 ToolCall/ToolResult 消息不再产生空 speaker 行,同时减少进入 embedding_template: EventName / Goal / {{ content }} 的垃圾字符。

tests/unit/test_issue3598_event_summary.py — 回归测试 3 组

  • TestExtractEventSummaryAnchorMatch:6 种 terminator/writer shape(当前模板风格、legacy field 形式、MEMORY_FIELDS sentinel、summary-only resource branch、heading 变体)+ fallback 分支 + issue body 内最小可复现样例,共 8 个用例。
  • TestGetEventContentDefaultRatio:signature 默认值是 0.2(防止有人又把 0 写回去),以及 ratio=0.2 边界选择 summary vs original 的行为。
  • TestFormatContiguousGroupSkipsBlankTurns:纯 tool-call-only 组结果为 [],以及 text + tool 混合时只保留有文本的 speaker 行。

验证方式

注:项目根 conftest 导入链路中 openviking_cli/utils/async_utils.py:15 使用了 asyncio.AbstractEventLoop | None(Python 3.10+ union 语法),而当前执行环境是 Python 3.9,这与本次改动无关,是仓库已有的 baseline 环境限制。在配备 Python 3.10+ 的 CI 环境中可正常跑 pytest tests/unit/test_issue3598_event_summary.py。

影响范围

  • 召回 API:search_type_quota_recall 在 mode="summary" 下的片段体积从「全文」量级降到「实际 Summary section」量级(即 Reporter 观察到的 6466 → 465 char)。
  • 事件抽取大小:get_event_content 激活 20% 比例门限,summary 紧凑时 ChatLog 改为 summary,减小落盘与向量 embedding 体积。
  • 向量 embedding 质量:embedding_template 里 {{ content }} 不再包含数百字节的 "speaker: " 空行,召回相关性可提升(纯噪声被移除)。
  • 向后兼容:正则放宽两端,旧文档上 Summary: 字段形式仍被正确识别;$ 终止保证 resource-event branch 不被破坏。
  • 无破坏性:没有改签名 / 没有改 API contract / 没有移除任何字段。

Checklist

  • 正则锚点 / 终止符 两端 全部按 Reporter correction Bump actions/download-artifact from 4 to 7 #1 更新(非只改锚点)。
  • 模板 ratio_threshold=0 删除 → 方法签名默认 0.2。
  • flush_current 空串 guard。
  • 三个缺陷对应三组独立回归测试(失败时可精确定位到是哪一个 defect 回归)。
  • 最小可复现断言 1:1 对齐 issue body。
  • 向后兼容 legacy 文档形状。
  • (CI)Python 3.10+ 环境执行 pytest tests/unit/test_issue3598_event_summary.py -xvs。

…ines (#3598)

Issue #3598 reports three small defects that compound so recall spends nearly
its whole `max_chars` budget on verbatim event transcripts - on production
samples 92.5% of event-doc chars were ChatLog, and 55% of turns were tool-only
blanks lines. A single "summary-mode" fragment was observed at 6466 chars when
the real `# Summary` section was 465 chars.

### Defect #1 — `_extract_event_summary()` never matched template output
(verified in issue correction comment, was TWO mismatches not one). Template
writes `# Summary` + `# YYYY-MM-DD (Weekday) ChatLog:`. The regex wanted
`Summary:` + a bare `<date> ChatLog:` with no heading marker. Neither anchor
nor terminator matched, so `fallback` (full doc) was always used.

Relax the regex:
- anchor: `^\s*\#*\s*Summary:?\s*` — optional heading mark, optional colon
  (accepts current heading style, legacy field form, resource-event prose)
- terminator: `\n\s*\#*\s*(?:<date>|ChatLog:)` plus MEMORY_FIELDS sentinel and
  end-of-string for summary-only docs.

### Defect #3 — template hardcoded `ratio_threshold=0`
`events.yaml` content_template called `get_event_content(ranges, summary, 0)`,
short-circuiting `len(summary)/len(original) >= 0` to always return the full
verbatim transcript. Drop the third positional arg so the function default
(0.2) kicks in, matching the default intent.

### Defect #4 — blank speaker lines in ChatLog
`_format_contiguous_group.flush_current()` unconditionally appended
`**speaker**: {content}`. Tool-call-only turns carry no TextPart, so merged
content was `""`; every such turn added a blank 36-byte line, and those lines
then fed into the embedding template. Guard append with
`if content.strip():`; a session with 55% blank turns now saves ~40% of
its rendered event-doc bytes.

Tests added in `tests/unit/test_issue3598_event_summary.py`:
  - TestExtractEventSummaryAnchorMatch: 6 parametrized cases covering all
    writer shapes (current template, legacy field form, MEMORY_FIELDS
    terminator, summary-only resource branch, heading variances, fallback)
    plus exact minimal-repro body from issue.
  - TestGetEventContentDefaultRatio: signature default = 0.2 (not 0); ratio
    boundary behavior at 0.2.
  - TestFormatContiguousGroupSkipsBlankTurns: pure-tool groups yield [],
    text+tool mixture keeps only text turns.
@t0saki

t0saki commented Jul 30, 2026 •

Copy link
Copy Markdown
Collaborator

感谢这个 PR,也感谢 #3598 那份报告 —— 定位到具体行号、四条缺陷分别给了最小复现,还在复验后自己纠正了 defect #2 属有意设计。这种质量的报告对我们非常有用。

关闭的原因是召回读取端正在整体重做。

#3534(实现 RFC #3372,目前还是 draft,在评审中)把召回组装搬到服务端,openviking/retrieve/type_quota_recall.py 整个文件被删除,换成 openviking/retrieve/context_assembler/。新的抽取器 context_assembler/tiers.py::extract_summary_section 先匹配 #{1,3} Summary 标题形式,抽不到再回落到历史的 Summary: 字段形式 —— 和你放宽正则两端的思路一致。用 #3598 里那份最小样例在新分支上验证过,返回 'Shipped the thing.'。

比把正则修对更进一步的是:新的档位模型让 Summary 抽取成为 events 的默认档,而不是"超出预算才走的退路"。你观测到的 6466 → 465 这个收益在默认路径上直接拿到,不需要先触发降级。

所以你对这个文件的改动会和 #3534 冲突(你改、我们删),TestExtractEventSummaryAnchorMatch 那组测试也会随旧模块一起失效。需要说明的是 #3534 还没合,在它落地之前 main 上这条缺陷仍然存在。

本 PR 里另外两处改动(events.yaml 的 ratio_threshold=0、_format_contiguous_group() 的空内容 guard)在记忆写入端,不属于这次召回重做的范围,#3534 没有触及。关闭本 PR 只反映召回端的重做,不构成对这两处改动的评价。


In short (EN): closing because the recall read path is being rewritten server-side in #3534 (RFC #3372, still a draft), which deletes type_quota_recall.py outright — the new extractor matches the # Summary heading first and falls back to the legacy Summary: form, and Summary extraction is now the default tier for events rather than a degradation path, so the 6466 → 465 win lands without needing to trigger a fallback. Your change to that file would conflict with the rewrite, and TestExtractEventSummaryAnchorMatch goes away with the old module. Note the defect is still present on main until #3534 merges.

The other two changes here — ratio_threshold=0 in events.yaml and the blank-content guard in _format_contiguous_group() — are on the memory write path, outside the scope of this recall rework and untouched by #3534. Closing this PR reflects the recall rewrite only and is not a judgement on those two.

@t0saki t0saki closed this Jul 30, 2026
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants