Skip to content

[行为确认] rebuildMessages pin 住第一条 user message:即使被 active block 覆盖仍原样转发——请确认意图并写入契约文档 #341

Closed

Description

@ranxianglei

来源:billion-context#1102(https://github.com/ranxianglei/billion-context/issues/1102,opencode instructions 指纹漂移)分析时发现。该行为与本 bug 正交(master 与修复后表现一致),但下游 e2e fixture 被迫适配它,故提交确认 + 文档化。

行为

src/prune.ts 的 rebuildMessages(acp-kernel 0.0.82 dist 核实)中,会话的第一条 user message 即使被 active block 覆盖也总是原样转发:

if (index === firstUserIndex && firstUserIndex >= 0) {
  result.push(messages[index]);
  continue;   // ← 跳过了下面的 covered 检查
}
if (covered.has(messages[index].id)) continue;

pin 检查先于 covered-by-active-block 的剪枝检查执行。因此压缩包含第一条 user message 的范围后,后续重建的 wire 里会同时出现渲染后的 summary([Compressed conversation section] …)和第一条 user message 的完整原文;其余被覆盖消息正常从 wire 消失。

证据

  • billion-context PR #1104(https://github.com/ranxianglei/billion-context/pull/1104)设计 e2e fixture 时观察到不对称折叠:compress 执行、block active(effIds 覆盖两条目标消息)之后,下一轮出站 body 中——user 侧目标当时是会话第一条 user message(m00001),按此规则仍原样转发;assistant 侧目标(m00002)正常消失。
  • master 与修复后表现一致,kernel 行为,与 bili 侧任何改动无关。
  • billion-context 测试现用"压缩目标前放一对 seed 消息"规避,并已在测试注释中写明该 pin。

影响

  • 下游消费者无法对第一条 user message 做压缩后 wire 字节级断言(fixture 必须适配)。
  • 估算压缩后 wire 体积 / prefix-cache 稳定性时需计入 pinned 消息保持原文(第一条 user message 常是任务描述,可能很长)。

问题 / 建议处理

看起来是有意设计(pin 住任务开场白以在压缩后保留 framing)。若有意:请在 kernel 契约/spec 文档中写明——billion-context AGENTS.md §7.3 已注明 "Codifying the kernel-side spec is a separate acp-kernel change — deferred"。若非有意:pin 检查应移到 covered 检查之后(或当消息属于 active block 时跳过)。

Activity

  1. ranxianglei commented on Sep 22, 2026

    @ranxianglei
    OwnerAuthor

    🤖 Powered by ework · qwen3.8-27b

    结论:有意设计,已确认并写入契约文档。未改代码。

    意图证据(三条独立佐证)

    1. 历史:pin 自 prune 首次实现(9747d18, v0.0.2)起就存在,且 pin 检查先于 covered 检查的顺序从未被改动过——不是某次重构的副产物。
    2. 测试:有专门的回归测试锁定该行为:
      • tests/state-prune.test.ts "prune preserves first user message even when covered"
      • tests/orphan-fixes.test.ts "prune: first user message pruned when covered (no duplication)",断言注释直接写明理由:"first user message always survives (even when covered — some providers reject 0-user)"
    3. 下游代码已适配它:src/boundaries.ts:313-317 的 blockVisibleInRange 显式处理了这个行为——当 block 覆盖第一条 user message 时,summary 锚在 pinned raw 之前,所以 range 判定必须同时检查 rendered summary 和 earliest surviving raw。若 pin 是无意的,边界解析不会专门为此补偿。

    理由

    严格 provider 拒绝没有 user message 的会话(Anthropic 要求对话从 user role 开始)。pin 保证只要输入至少有一条 user message,重建后的 wire 就至少保留一条(就是第一条本身;后续被覆盖的 user message 正常剪掉)。因此 issue 中提的另一方案——把 pin 移到 covered 检查之后——会破坏这个保证,不采纳。

    文档化(PR: https://github.com/ranxianglei/acp-kernel/pull/342)

    • DESIGN.md 新增 §8 "Prune Semantics & Wire Invariants" / §8.1 "First-user-message pin"(DESIGN.md 是 README 指认的 authoritative contract):行为定义、理由、以及对下游消费者的三条后果——
      • 压缩后 wire 不能对第一条 user message 做字节级消失断言(必须预期 summary + pinned 原文并存);
      • token 体积 / prefix-cache 稳定性估算需计入 pinned 消息保持原文;
      • boundary resolution 的补偿机制(上述第 3 点)。
    • src/prune.ts pin 处加注释:说明该检查顺序是契约的一部分、交换会怎样,防止未来有人把它当 bug "修掉"。纯注释,无逻辑改动。

    验证:基于最新 master(b30723f, v0.0.82)rebase,typecheck ✅、869/869 测试 ✅、build ✅。

    中文摘要:确认第一条 user message 的 pin 是有意的 provider 兼容性保证(拒绝 0-user 会话),已在 DESIGN.md §8 写入契约并在代码处加防护注释,无逻辑改动,PR #342 可合并。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions