Skip to content

fix: seam forensics re-notes outbound body on proxy-initiated re-send hops (#1894) - #1899

Merged
ranxianglei merged 1 commit into
2026-10-02_fix-seam-nobaseline-false-positivefrom
2026-10-02_seam-hop-renote
Oct 2, 2026
Merged

ranxianglei merged 1 commit into
2026-10-02_fix-seam-nobaseline-false-positivefrom
2026-10-02_seam-hop-renote

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #1894

Stacked on #1893 — this branch builds on #1893's main-chokepoint noteForwardedBody; that PR must merge first (or this one rebases onto master afterwards). Until then this PR's diff against its base is exactly the four facets below.

What changed

The [cache-seam] forensics pair each settled usage sample with the outbound body noted via noteForwardedBody. #1893 made the main send chokepoint note the wire bytes, but three in-request, proxy-initiated re-send hops replace upstreamResult with MODIFIED bytes without re-noting — so when such a hop succeeds, the settled sample paired the FIRST-ATTEMPT (rejected) bytes against the previous baseline:

  1. Compat-role ladder (hop() in src/server.ts): re-sends applyCompatRoles output after a 400 role rejection — now re-notes fixed.body on success.
  2. Overflow refold (src/server.ts, 使用pi 压缩后就提示prompt is too long #1195): re-sends the refolded body after a 400/413 context overflow — now re-notes it on success.
  3. Fake-completion retry (resolveFakeCompletion, [bug] 假完成:模型把工具调用写成文字(无 tool_use 块)→ 轮次提前终止,agent 显示正常完成(#361 根因 follow-up) #371): re-sends the hinted body — now re-notes hinted when the retry is ACCEPTED (rejected retries leave the slot untouched, matching the original buffer being presented).
  4. Non-streaming Responses compress loop (compressLoopResponsesJson, found while working this issue): each per-round re-request settles its usage in the caller but never noted its own wire bytes — same staleness mechanism, now notes them (same contract as loop/core.ts fetchUpstream).

The non-streaming settle site no longer notes anything: it runs AFTER the hops, so pinning first-attempt bytes there would clobber the hop re-notes. Invariant going forward: every send path notes its own bytes at send time; the settle pairs with whatever was last noted.

Behavior change (diagnostic-only)

Old → new for the affected transitions only: the settled forensic pair compared first-attempt bytes vs previous baseline; it now compares the EFFECTIVE (post-hop) bytes vs previous baseline. Concretely, on a compat-role hop mid-session the LCP/message-index pointer moves from the full-baseline prefix (first attempt was byte-prefixed by the baseline) to the true divergence site (the rewritten role field). No accounting, closure, cache-key, retry, or wire impact — only the [cache-seam] pairing and the seamSuspects/providerSideMisses classification on the affected sample.

Config surface

None. No new config fields, env vars, or CLI flags.

Tests

  • New tests/issue1894-seam-hop-renote.test.ts: real proxy + role-rejecting fake upstream (Responses lane, mid-session flip to rejecting developer). Asserts the settled SeamEvent's LCP equals the ground-truth LCP of (baseline, post-hop bytes) and NOT the stale (baseline, first-attempt bytes) value. Negative control verified: without the source change the same fixture fails with lcpBytes 11239 (full stale baseline) instead of 55 (true divergence at the rewritten role).
  • Targeted batch green (29/29): issue1891-seam-nobaseline (the stacked PR's fixtures), cache-seam-detector/probes/fuzz, abort-rewind-cache-seam, responses-fold-cache-seam, responses-seam-probes (RP1–RP3 byte-stability), preflight-compress-responses, fix-1439-responses-json-triggers. Full suite via CI.
  • npm run typecheck green.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #1899 review 结论

独立验证(本地证据)

结构化七问

  1. 是什么问题:[cache-seam] 取证把每个 settled usage 样本与「记录的出站 body」配对;代理主动重发的 hop(compat-role 梯、overflow refold、fake-completion retry、非流式 Responses loop 重取)成功后,settled 样本配到的是首次尝试(被拒)的字节,而非实际生效的 post-hop 字节——seam 指针对准了错误的分歧点。
  2. 什么原因导致的:fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 只在主发送咽喉记一次 wire bytes,而三个 in-request hop 用改写后的字节替换 upstreamResult 却不重记;非流式 settle 点又在 hop 之后重新钉回首试字节,把 hop 该留的记号冲掉。机制层:「记录的 body」被钉死在某一时刻,没有跟随生效的发送。
  3. 怎么修复的:不变量移到发送时刻——每条发送路径在发送时记下自己的精确字节(主咽喉、各 hop 成功时、fake-completion 仅在接受时、loop 重取),settle 点不再钉任何东西,与最后记住的配对。四处 re-note 点逐一核对过与实际 fetch body 逐字节一致(fixed.body / refolded / hinted / wireBytes)。
  4. 是否有回归问题:触碰路径的既有行为逐项过——主咽喉语义不变;hop/refold/fake-completion 自身逻辑未动(note 是只读取证);side request / GET / HEAD 仍永不记录(seamBody 哨兵守卫,与「side 不得冲槽」契约一致);plugin 模式 settle(plugin.ts:1186)与流式 loop settle(loop/core.ts:296)在同一不变量下配对,两种压缩模式都推演过。唯一可观察变化就是已披露的诊断配对(受影响的样本 LCP 从全基线前缀移到真实分歧点)。fail-fast 变吞错、阈值/时序/输出格式漂移:无。
  5. 是否破坏缓存:不涉及——seamLastSent/seamLastSettled 是内存取证槽,cache key、前缀缓存、重试、去重键均不受影响。
  6. 是否改了设计方向:架构不转向,是把 fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 的单点捕获扩展成文档化不变量「每条发送路径在发送时自记」。一个有意的取舍:settle 点原有的安全网重钉(即本 bug 本身)被移除,今后新增发送路径必须在发送时自记,否则其 settle 会配到陈旧 body;不变量已在咽喉与 settle 两处注释固化。
  7. 是否引入了新的配置:不涉及(无新字段/env/flag,已对照 config surface 确认)。

残留(非阻塞):仅 facet 1(compat 梯)有专属 e2e fixture;facet 2–4 机制相同(发送时自记),由定向批次覆盖但无专属配对断言。本 PR 可接受;overflow-refold / fake-completion 的配对 fixture 如要补,留在本 issue 域内做 follow-up,不单拆。

合并前置(顺序要紧)

  1. 先合 fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893——本 PR 声明 base 是 fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 分支,若 fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 未落地就合这里,会把 fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 的 commit 一并带进 master。
  2. fix: seam detector no longer false-flags no-baseline first bills (#1891) #1893 落地后,把本分支 rebase 到 master(上面的模拟证明干净落上且 typecheck/定向绿),让 ci.yml 全套真正跑起来——今天它因 base 非 master 而未触发。
  3. 全量 CI 绿 → 合并。

判定:代码方向与实现正确、测试为真(负向对照复现)、diff 干净;待 #1893 先行 + rebase 后全量 CI 绿即可合并。

@ranxianglei
ranxianglei merged commit 8861865 into 2026-10-02_fix-seam-nobaseline-false-positive Oct 2, 2026
5 checks passed
@ranxianglei
ranxianglei deleted the 2026-10-02_seam-hop-renote branch October 15, 2026 05:32
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