OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: 自动召回条目超预算时丢弃全部内容降级为裸 URI(resources/skills 无中间档位,长度门用字符而预算用 token)

Open feel-think opened this issue 2 weeks ago • 2 comments

Issue Origin

Observed or reproduced in a real environment

Bug Description

自动召回(context 面)在单条条目超过 per_entry_cap 时会丢弃该条目的全部内容,降级为裸 URI,而不是截断到预算内。对 resources / skills 这两类,abstract 之下没有中间档位,所以一超限就是"什么都没有"。

这条与 #7692 是同一链路的两个不同缺陷。#7692 是摘要内容写错(生产者侧,PR #7697 在修);本条是预算策略选错(消费者侧),表现为三个子缺陷:

缺陷 1:超限 = 丢光,而不是装下多少算多少

retrieve/context_assembler/budget.py 的 fits_cap() 只判"放得下/放不下",放不下就试下一档;budget.py:8 的 docstring 明确写着 "an oversized tier falls back to the previous one instead of being truncated",且 retrieve/context_assembler/ 全目录零处 truncate 调用。

而仓库里已经有现成的截断器:utils/token_estimation.py:56 truncate_text_to_token_budget(text, max_tokens, marker="\n…\n", head_ratio=0.75),已在 session/checkpoints.py:375、session/retention.py:211、retrieve/rerank_scoring.py:82 使用,只是没用在装配器上。

实测代价(我们部署里的 66 个资源摘要样本):默认 max_tokens=1600 时 per_entry_cap = 1600 // 10 * 2 = 320,

值
cap 320 下能进上下文的资源摘要 2 / 66
中位片段 token 430
中位超出量 仅 110 token

也就是说:为了省 110 token,丢掉了这条记忆 100% 的信息。用上面那个截断器模拟,66/66 都能有内容,中位保留 74% 文本。

缺陷 2:resources 在 abstract 之下没有档位

tiers.py: abstract_substitute() 对 resources / skills 返回 uri(有意不退回 overview,因为那需要读正文,见 params.py 里 "their bodies are large and may carry credentials, so deepening is opt-in");params.py: DEPTH_CEILING_BY_CATEGORY 只给 events 留了加深空间。所以档位阶梯是:

  • 记忆类:abstract → overview → uri(有中间内容档)
  • resources / skills:abstract → uri(直接掉底)

我们评测过一个"补档"方案:给 resources 钉 overview(detail={"resources":"overview"},服务端现已支持)。在默认预算、零额外开销下,8 个 query 的资源命中"有内容"从 1/22 → 14/22(每轮 token 1496 → 1508,几乎免费):

配置 资源命中出内容 裸 URI 条目 每轮 used
默认 1/22 29 1496
只钉 resources=overview 14/22 20 1508
只加预算 max_tokens=3000 16/22 7 2725
3000 + 钉 overview 18/22 5 2472

14 个真实资源文件逐条对比(abstract = 存储的生成摘要;overview = 读时从正文派生):

档位 中位片段 token cap 320 下装得下
abstract 440 0 / 14
overview 167 13 / 14

但补档解决不了全部问题,三个反例:

  1. 档位不单调。低档可能更大:我们一个 faq.md,abstract 394 字符 / 421 token,overview 1088 字符 / 801 token(其中标题树 1003 字符)。原因是 tiers.py: doc_overview() 取前 24 个标题(OVERVIEW_HEADING_LIMIT=24,该文件共 47 个),且 _HEADING_LINE 连锚点一起抓(## 基础概念 [](#基础概念)),中文标题再按约 1.5 token/字计。
  2. 目录类没有更低档。目录命中取自 .overview.md,candidate.abstract 为空,所以阶梯是 overview → uri。我们一个目录节点的 overview 是 3889 字符:max_tokens=20000(cap 4000)才显示,cap 320 / 520 / 600 / 1000 全部是裸 URI。
  3. 最好配置下仍有 5/22 是裸 URI(上表最后一行)。

另外补档有质量代价,不能当全局默认:对结构化文档(API 参考、wiki)overview 是很好的目录树,但对散文型摘要会退化成文件头。同一个 LCM 文件,overview 给出的是

# [lcm-ov] node=sum_5a82d75f3d9a06d8 conv=1 level=d1(condensed) children=8 span=2026-04-06T07:15~2026-04-07T01:42, parent=sum_27af7bdeb4a1ca4a
〔历史快照:…〕展开/取原文:见 ../NAVIGATION.md

而 abstract 给的是"Conv 11 压缩失败与 401 误判、v0.6.2→0.6.3 deadloop 事故根因……关键词:……"。换档把"发生了什么"换成了"这是哪个节点"。

缺陷 3:长度门用字符,预算用 token,中文两头对不上

摘要有长度契约 semantic.abstract_max_chars = 256(openviking_cli/utils/config/parser_config.py:715),单位是字符;而装配预算是 token,per_entry_cap = max_tokens // candidate_count * 2。中文按 utils/token_estimation.py 的 CJK 权重 6/4 计,约 1.5 token/字:

摘要长度 正文 token 含 <memory> 信封片段 默认 cap 320 cap 600(max_tokens 3000)
256 中文字(门上限) 384 427 ✗ ✓
200 中文字 300 343 ✗ ✓
185 中文字 278 320 ✓(临界) ✓
256 ASCII 字符 64 107 ✓ ✓

即:默认预算下,中文摘要最多只能 185 字才能进上下文,而门放行到 256 字。所以即使 PR #7697 给文件摘要补上 256 字符的门,中文摘要在默认预算下依然会降级成裸 URI;英文反而完全够。这是"用字符写一个按 token 花的约束"的必然结果。

补充一点:per_entry_cap 的分母是实际候选数,不总等于配额上限。我们 36 个跨主题 query 里,33 个是 10 条候选,另有 1 个 9 条、1 个 8 条、1 个 7 条(对应 cap 354 / 400 / 456)。候选少时 cap 自动变大,所以同一份摘要在不同轮次里的命运并不稳定。

Steps to Reproduce

  1. 服务端 0.5.4,默认检索配置(无 detail 覆盖)。客户端按 context 面默认参数请求:purpose="coding"、10 条候选、max_tokens 取默认 1600,故 per_entry_cap = 320。
  2. 库里有若干 resources 条目,其存储 abstract 为常见长度(中位数约 430 token 片段)。
  3. 发起 context 检索:
curl -s -X POST http://127.0.0.1:1933/api/v1/search/search \
  -H "Authorization: Bearer $OV_KEY" -H 'Content-Type: application/json' \
  -d '{"query":"lcm 对话摘要 conv426","mode":"context","purpose":"coding","score_threshold":0.35}'
  1. 观察 entries:资源类条目为 {"category":"resources","detail":"uri","text":""},stats.per_entry_cap=320、stats.tier_counts 里 uri 占多数。
  2. 同一 query 加 "detail":{"resources":"overview"} 再发:同样 1600 预算,多条资源变成 detail="overview" 并有正文。
  3. 再加 "max_tokens":3000 对比:cap 600,多数 abstract 能进。
  4. 对照:"max_tokens":20000(cap 4000)时,连 3889 字符的目录 overview 也能进,说明档位本身可用,卡的是 cap。

Expected Behavior

两条不变量:

  1. 注入块里的每条条目都应携带内容,除非该条目的内容确实为空。超预算时应截断到预算内(用仓库已有的 head+tail 截断器),而不是丢弃全部内容只留 URI。截断是通用兜底,与预算大小无关:per_entry_cap 由 max_tokens / candidates 推导,任何人调小预算都会重新触发同一问题。
  2. 每一类内容都应有可用的降级路径。resources / skills 在 abstract 之下缺一档,导致它们比记忆类更容易变成空指针。

Actual Behavior

  • 单条超 per_entry_cap 即试下一档,resources / skills 的下一档就是 uri(空),于是整条内容消失。
  • 默认配置下我们样本里 66 个资源摘要只有 2 个能进上下文;中位超出量仅 110 token。
  • 长度契约用字符、预算用 token,中文摘要即使被门截到 256 字符(约 427 token 片段)仍超默认 cap 320。
  • 目录类条目在 cap ≤1000 时全部为裸 URI(其 overview 可达 3889 字符)。
  • 同一份摘要在不同轮次可能因候选数不同(cap 320 / 354 / 400 / 456)而时有时无。

Minimal Reproducible Example

装配决策(retrieve/context_assembler/budget.py):

def per_entry_cap(max_tokens: int, candidate_count: int) -> int:
    return max(1, max_tokens // max(1, candidate_count) * 2)   # 1600 // 10 * 2 = 320

def fits_cap(tier: Tier, tokens: int) -> bool:
    return tier == "uri" or tokens <= cap          # 只有 uri 豁免,超了就不放

for tier in _tiers_down_from(candidate, start):    # resources: ["abstract", "uri"]
    text = tier_text(candidate, tier, contents=contents)
    if text is None:
        continue
    if not fits_cap(tier, entry.tokens):
        continue                                   # ← 抽象档放不下,直接掉到 uri

档位缺失(retrieve/context_assembler/tiers.py):

def abstract_substitute(candidate: Candidate) -> Tier:
    if candidate.category in FULL_BODY_ABSTRACT_CATEGORIES:   # 只有记忆类
        return "overview"
    return "uri"                                              # resources / skills 直接掉底

单位不对齐(openviking_cli/utils/config/parser_config.py 与 utils/token_estimation.py):

abstract_max_chars: int = 256      # 字符门
# CJK 权重 6/4 → 256 中文字 = 384 token;加 <memory> 信封 ≈ 427 片段 token > cap 320

Error Logs

无报错。静默降级,只能从响应里的 detail="uri"、text 为空、以及 stats.tier_counts / stats.per_entry_cap 看出来。这条静默性本身也让问题不容易被发现。

OpenViking Version

服务端 0.5.4(uv tool 安装)。已比对 PyPI 0.5.5 的 wheel:retrieve/context_assembler/{budget,tiers,params}.py、utils/token_estimation.py 与本机 0.5.4 逐字相同,最新版未修。

Python Version

3.14

Operating System

Linux

Model Backend

Other(OpenAI 兼容本地代理;摘要与 query_planner 用 deepseek-v4.1-flash-1017,embedding 用 qwen3-embedding-8b)

Additional Context

我们的建议(三层分工,各管一件事)

层 做什么 解决的问题
生产者 摘要长度按 token 定额(或按语言折算字符上限),让 abstract 天生 ≤ 典型 cap 从源头消灭"超限"
消费者·不变量 超限时截断到 cap 再注入,不再降级为 uri;per_entry_cap 只算正文,不含 URI 与 <memory> 信封 保证"任何预算下每条都有内容"
消费者·档位 把 overview 作为 resources 的可选兜底档(按类别显式开启),而不是全局默认 结构化文档收益大(实测零开销 1/22 → 14/22),散文摘要不退化

要点是:截断与补档不是二选一。补档让每个 token 换到更可用的内容,截断保证不会出现"空指针"。只有生产者按 token 定额,才能让截断很少被触发。

可用性缺口:detail / detail_by_category 目前只有 REST 与 MCP search 工具能传。客户端插件(如 dsh-memory-plugin 的 config-schema.mjs)与 ov search CLI 都没有对应旋钮,插件构造请求体时也从不发 detail,所以普通用户无法用配置开启补档,只能手工调 API 或在 DEFAULT_TIER_BY_CATEGORY 上改默认值。如果要走"配置可开"的路线,需要插件侧加旋钮。

临时缓解(我们在用的,供参考,不是修复):把客户端 recallMaxTokens 提到 3000(cap 600),实测 8 个 query 资源出内容 16/22、裸路径从 29 降到 7。代价是每轮召回注入多约 1.2k token,且不解决不变量问题(任何人把预算调回默认就复现)。

关联

  • #7692:同一链路的生产者侧缺陷(live context 摘要内联代码),PR #7697 在修;但如"缺陷 3"所述,256 字符的门对中文仍不够。
  • #3005:abstract_max_chars(256) 与 _truncate_abstract_bytes(50,000 字节) 不一致(已 closed,后续 #3289 只给 reranker 输入设了上限)。
  • #7170:并发召回覆盖去重台账(同一装配链路,独立问题)。

我们保留了两批评测的原始数据(66 个资源摘要的片段 token 分布、8 个 query × 多档位的 entries/stats 输出、14 个文件的 abstract vs overview 对比、36 个 query 的候选数分布)。如需要可以提供复现脚本,也可以按你们的偏好提交 PR。

feel-think avatar Nov 14 '26 17:11 feel-think

I reproduced the budget decision at main@00c8e7b1b8701fe0f0ee43d29b4f96667984b6d5 and found an additional distinction that may help choose the fix: unused global budget cannot restore an abstract that lost to the per-entry cap.

With ten distinct resource candidates, each carrying a 256-CJK-character abstract, plan_entries(candidates, {}, max_tokens=1600) returns:

per_entry_cap=320
returned=10, tier_counts={"uri": 10}, used_tokens=229
overview_upgrades=0, full_upgrades=0, spare_upgrades=0

For the synthetic URIs viking://resources/doc-{0..9}.md, each complete abstract fragment costs 410 estimated tokens. Replacing three URI fragments with their whole, already-retrieved abstracts would cost 1393 tokens including the remaining pointers and separators. That is an arithmetic fit calculation, not a patched-pipeline result. The current final pass only tries full; a resource's abstract ceiling excludes that target, so 1371 tokens remain unused even though several complete summaries fit.

I also exercised the real ASGI POST /api/v1/search/search route and assembly pipeline with synthetic search results/files, ten candidates, and purpose="coding":

256-character abstracts Content-bearing entries Resource/skill entries with content used_tokens / max_tokens
ASCII, defaults 10/10 5/5 888/1600
CJK, defaults 5/10 0/5 318/1600
CJK, detail={"resources":"overview"} 8/10 3/5 345/1600

The default CJK case reads only memory bodies; resource/skill bodies are not read. The explicit overview request additionally reads the three resource files. This preserves the opt-in body-read boundary established in #3746; a default overview fallback would need to preserve that boundary too.

Two policy choices seem separable:

  • A spare-budget pass over already-available abstracts could recover some whole summaries while keeping the existing non-truncation rule and tier ceilings. It would still leave pointers when the global budget cannot fit more whole entries.
  • Guaranteeing partial content requires a truncation policy, including how clients distinguish clipped text. The final max_tokens check must still include rendered URI/envelope/separator costs: in the resource-only example, even ten empty pointers already cost 229 estimated tokens, so “every hit has content at any budget” cannot hold under an arbitrarily small hard total budget.

Which direction should a focused follow-up preserve: whole-tier recovery from spare budget, or clipped content? I have not changed production code pending that boundary decision.

AI-only investigation on macOS/Python 3.14.10. The existing context-assembler suite passed (51 passed, seven dependency warnings). The HTTP probe substitutes retrieval results and file bodies; it does not validate the reporter's dataset, models, native storage, or deployment.

r266-tech avatar Nov 14 '26 21:11 r266-tech

汇报一下我们在真实部署数据上对 #7707 的验证。评审贴里的探针用的是合成检索结果,这里补一份真实负载的对照。

环境与方法

  • 服务端 OpenViking 0.5.4(我们自己的部署,检索/摘要均在生产路径上跑过一段时间)。
  • 候选来自真实库:8 个跨主题 query,各取 POST /api/v1/search/find(context_type=["resource"],limit 10)的真实命中,用真实 URI 与真实存储 abstract 构造 Candidate,只保留文件类(is_directory=False),共 68 条。
  • 对照实现:baseline = 本机 0.5.4 安装包里的 plan_entries;PR = 从 #7707 的 head(67696b9)取 retrieve/context_assembler/{budget,tiers}.py,把 tiers 注册进 sys.modules 后在同进程里调它的 plan_entries。两边喂同一批 Candidate 对象,contents={}(资源类默认档是 abstract,不需要读正文;PR 的 clip 路径用的是已在手的 abstract,同样不读)。
  • 已知偏差:真实装配器用的是分桶+重排后的 10 条混合候选,这里近似为单 query 的 10 条资源命中,所以绝对条数不等同于线上某一轮;但 per_entry_cap = max_tokens // candidates * 2 与公平份额的算术是一致的。

结果(总计 68 条资源)

max_tokens baseline 有内容条目 baseline 裸 URI baseline 每条正文 token(均值) PR 有内容条目 PR 裸 URI PR 每条正文 token(均值)
1600(默认) 5 63 348 68 0 140
2600 42 26 385 68 0 243
3000(我们现在用的) 47 21 393 68 0 284

默认 1600 下逐 query(资源文件):

query                    baseline(n/content/uri/used)     PR #7707(n/content/uri/used)
lcm 对话摘要 conv426        10 /  0 / 10 /  394            10 / 10 / 0 / 1600
OpenViking 召回 注入 机制     10 /  0 / 10 /  368            10 / 10 / 0 / 1600
dsh 插件 开发 配置            2 /  2 /  0 /  881             2 /  2 / 0 /  881
MSTR mNAV 持仓 成本价         8 /  1 /  7 /  614             8 /  8 / 0 / 1600
OKX 持仓 成本价 浮动盈亏       10 /  0 / 10 /  305            10 / 10 / 0 / 1600
QMT 交易链路 redis          10 /  0 / 10 /  308            10 / 10 / 0 / 1600
iOS 签名 SideStore 续期     10 /  1 /  9 /  608            10 / 10 / 0 / 1600
云主机 选型 价格 线路           8 /  2 /  6 /  863             8 /  8 / 0 / 1599

观察

  1. 原始症状在真实负载上消失了:三个预算档位下裸 URI 都归零,每条都有内容。我们最初报的"资源类只剩路径"(#7705 缺陷 1)确认被修好。
  2. 预算的角色变了:以前它决定"有没有内容",现在只决定"每条有多少内容"。因为公平份额是 剩余 ÷ 未放置候选数,10 条候选时每条大约拿到 max_tokens/10:默认 1600 → 每条约 140 token,3000 → 约 284 token。也就是说 max_tokens 仍是内容密度的旋钮,但不再是"要么全有要么全无"。
  3. fill.restored 在我们的负载下没触发(三个档位都是 0)。原因是候选多而摘要普遍在 400 token 上下:截断填满预算后没有余量还原整段。这不是问题,只是说明"少数整段 + 多数截断"这个混合形态在我们这种负载上不会自然出现;如果希望优先保几条整段,可能需要显式的配额,但按目前效果看没必要。
  4. 评审提到的指针饥饿在我们数据上不触发。我们的资源 URI 约 95 字符(约 25 token),3000 下公平份额约 300、1600 下约 160,都远够。我们没有 750 字符级的超长路径,所以没法在这里复现或证伪那一条;但它描述的结构问题(entry_budget 也套在裸 URI 上)从 diff 看是成立的,值得后续单独处理。

边界

  • 我只把 PR 头部的两个文件在同进程跑通,没有部署合并后的分支,所以这是装配函数的对照,不是端到端验证。
  • 覆盖范围仅 resources 文件类。目录类(其 overview 可达 3889 字符,我们此前实测要 cap ≥ 2000 才显示)、以及 abstract 缺失的 pointer-only 场景,这份数据没有覆盖。
  • 合入正式版本后,我们会在同一部署上按同样的 query 复跑一次端到端(真实 POST /api/v1/search/search),结果继续贴到这里。

feel-think avatar Nov 15 '26 04:11 feel-think