Skip to content

[PR] fix(agent): pass merged headers to pfRequestHeaders so watermark count is emitted (#5813) - #5815

Merged
ranxianglei merged 2 commits into
masterfrom
2026-11-06_pf-headers-merge-fix
Nov 10, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-11-06_pf-headers-merge-fix

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Problem

pluginFullResendWatermark was enabled in production (dsh lane) but the fastpath had 0 hits in 3,795 evaluations — every single one miss (no-stamp).

Root cause: pfRequestHeaders() in native-intercept.ts was called with outgoingHeadersOf(input, init) ?? {} — the client's own request headers — which does NOT contain x-bili-plugin-conversation when the lane stamps it via state.headersFor (the dsh production shape). The function needs that header to look up the per-session stamp, so it always returned {}: the count header was never emitted.

The asymmetry: pfLearnStamp IS called with the merged object (post-withHeaders), so it correctly learns T from x-bili-pf-seq. The client learns the watermark but can never send it back.

Fix

All three call sites in src/agent/native-intercept.ts now merge the headersFor result into the object passed to pfRequestHeaders:

Branch Before After
routed pfRequestHeaders(outgoingHeadersOf(input,init)) pfRequestHeaders({...outgoing, ...routedExtra})
self withHeaders(input, init, pfRequestHeaders(outgoing)) merges headersFor(url) into both the pf input AND the withHeaders call
rewrite pfRequestHeaders(outgoingHeadersOf(input,init)) pfRequestHeaders({...outgoing, ...rwRouted})

Tests

Two new regression tests in tests/pf-channel.test.ts cover the dsh-lane shape (convo header from headersFor, absent from client request) on both routed and self branches. All 14 pf-channel tests + 78 native-intercept tests pass.

Closes #5813

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-11-06_pf-headers-merge-fix (36a37f7)

Latest build — install this exact version

npm install -g billion-context@0.0.8921-pr.5815.761b04d

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
36a37f7 billion-context@0.0.8921-pr.5815.761b04d build
ec29e85 billion-context@0.0.8379-pr.5815.70adb2f build
37270bd billion-context@0.0.8373-pr.5815.b2aba9c build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5815.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title fix(agent): pass merged headers to pfRequestHeaders so watermark count is emitted (#5813) [PR] fix(agent): pass merged headers to pfRequestHeaders so watermark count is emitted (#5813) Nov 6, 2026
@ranxianglei
ranxianglei force-pushed the 2026-11-06_pf-headers-merge-fix branch from 37270bd to ec29e85 Compare November 6, 2026 14:39
@ranxianglei ranxianglei added bug Something isn't working dsh p2 scope/wiring labels Nov 6, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复 —— 检索了全部 open/closed issue 与在途 [PR],没有其它条目修同一机制(#5813 的 pfRequestHeaders 视图不对称);本线程即该缺陷的唯一修复 PR。

标签: bug · dsh · scope/wiring · p2(缺陷类;dsh 车道协议接线面;性能/内存退化但功能可用,故 p2 非 p1)。

独立审核结论(PR #5815)

我没有照抄 PR 描述,而是把分支拉到最新 master 上逐处重读了三个改动点、pfRequestHeaders/pfLearnStamp 的实现、headersFor 的全部赋值方与所有调用点。诊断成立:学习侧(post-withHeaders 合并视图)与发射侧(合并前视图)不一致,dsh 车道的会话头只由 headersFor 注入、从不进客户端原始请求,所以查找永远落空——「能学不会发」,与生产 3795 次评估全 miss(no-stamp) 完全吻合。修复打在决策点(盖戳处的入参),不是堵症状。

审核中我做的两件事(已推上 PR 分支):

  1. rebase 到当前 master(原分支落后 15 个提交,§7.2 要求 rebase 后才能谈可合并性)——干净无冲突,新 head ec29e859e;
  2. 补 routed 分支回归测试:PR 描述称两个测试覆盖「routed 和 self 分支」,实际跑的是 rewrite(8199 直连形)和 self(origin 形)——/bili/ 已路由形(routed 分支)对 headersFor 提供会话头的形状没有回归钉。按 Test Blind Spot Retrospective: Reinforcing Negative Assertion Discipline, Tightening Fake Upstream Strictness, Thin Inst | 测试盲区复盘:补反向断言纪律、假上游严格化、薄安装冒烟、形态矩阵与生命周期覆盖 #3617 门控纪律每个修复点都要有断言行,我补了 tests/pf-channel.test.ts 的 "chokepoint routed branch …"(+20 行,独立 commit)。

本地证据(rebased head):npm run typecheck 干净;定向套件 227/227 绿(pf-channel 15 / native-intercept 78 / fastpath / fastpath-scanner / lane-decisions-matrix)。
CI(head ec29e859e):static-gates(ubuntu-24)、mysql-contract、gate×3、unused-exports 已绿;11 分片 test 矩阵运行中,出结果我再补一条。

结构化八问

  1. 是什么问题:水位快路径客户端盖戳点拿「客户端自己的请求头」去查每会话水位,而 dsh 车道的会话头是插件注入的、不在里面 → count/tail-ids 永远发不出去,快路径 0 命中。
  2. 什么原因导致的:pfRequestHeaders() 在三个出网点被传入合并前视图(outgoingHeadersOf),而它需要合并后视图(客户端头 ∪ headersFor)才能找到会话 id;根因是「盖戳点输入 ≠ 实际线上视图」的学习/发射不对称,不在 pf-channel 状态机内部。
  3. 影响范围与严重程度:仅 headersFor 注入会话头的车道(目前只有 dsh)+ 开启水位灰度(约 5%)的会话;后果是快路径永不生效、全量重发的解析开销与内存驻留保留([Bug][dsh] 20k-message / 81 MiB-inbound session: host aborted 6× in 2 h — the #3050 resident-AST OOM is not bounded by sessions.hardMaxBytes (Windows, 0.1.210) #5403 的 OOM 缓解在该机器上不工作),legacy 路径功能完整可用 → p2,无数据丢失、无可用性影响。
  4. 怎么修复的:三个出网点(routed/self/rewrite)统一把与 withHeaders 相同的合并对象传给 pfRequestHeaders,让发射侧与学习侧看到同一视图。
  5. 是否有回归问题:逐项过——①键冲突时 headersFor 覆盖客户端头,发射/学习两视图一致,线上传输顺序不变;②rewrite/routed 分支 wire 的键与顺序未变,只改 pf 入参视图;非 dsh 车道 headersFor 为 undefined(展开空对象)= 零变化;③retry 路径复用 branchExtra,不受影响;④pf-* 握手头转发上游前照旧剥除,不外泄。唯一有意行为变更见下条披露。
  6. 是否破坏缓存:不涉及——只动客户端→代理这一段是否带上两个握手头,不触碰前缀缓存、折叠状态或存储。
  7. 是否改了设计方向:不涉及——只是把盖戳点入参对齐既有「一台状态机 + 一个发戳点」架构(KDD#15)。
  8. 是否引入了新的配置:不涉及。

⚠️ 行为变更披露(旧值 → 新值 + 原因)

self 分支(dsh profile bundle 型:provider URL 直指 bili 根):旧行为是 headersFor 的结果不上 wire(只用于 pf 查找),这类请求此前到代理时不带任何插件身份头;新行为随请求携带 x-bili-plugin: dsh + x-bili-plugin-conversation(+可选模型头)。原因:没有这些头服务端无法绑定会话,水位握手与服务端会话识别都无从谈起——这是 dsh 生产形状本来就该发出的形态;routed/rewrite 分支此前已在传这些头,self 是对齐而非新发明。波及面仅限 dsh 直连代理的请求,其余车道不受影响。

判定

代码审核通过、定向验证全绿,CI 矩阵绿后即可合并(合并动作按规矩由你执行):#5815

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

CI 结论(补上轮回复的运行中部分):全绿。 head ec29e859e 的主 CI run 完成:16 个 job —— 12 success / 4 skipped(文件门控未触发的 e2e 车道)/ 0 failed。期间因标签同步回 GitHub 触发过一次工作流重跑(旧批被并发组取消、新批重起),新旧两批的 CI run 均为 success,无失败项。

最终判定不变:可合并——代码审核通过、定向套件 227/227 绿、typecheck 干净、CI 全绿、行为变更已披露(self 分支 dsh 直连请求补上插件身份头)。合并动作按规矩由你执行:#5815

ci-check and others added 2 commits November 10, 2026 16:21
…t is emitted (#5813)

pfRequestHeaders was handed the pre-merge outgoing headers (outgoingHeadersOf
of the client request), which never contains x-bili-plugin-conversation when
the lane stamps it via state.headersFor (dsh lane production shape). The
count header was therefore silently dropped every turn: 3795 evaluations,
0 hits, all no-stamp.

All three call sites in native-intercept.ts now merge the headersFor result
into the object passed to pfRequestHeaders:
- routed branch: pfOutgoing + routedExtra
- self branch: also merges headersFor into withHeaders (was missing entirely)
- rewrite branch: rwOutgoing + rwRouted into pfRequestHeaders

Regression tests cover the dsh-lane shape (convo header from headersFor,
absent from client request) on both routed and self branches.
@ranxianglei
ranxianglei force-pushed the 2026-11-06_pf-headers-merge-fix branch from ec29e85 to 36a37f7 Compare November 10, 2026 08:48
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Conflict resolved & rebased. The PR branch was 575 commits behind master; master had rewritten the surrounding lines of all three pfRequestHeaders call sites in src/agent/native-intercept.ts via unrelated features (hop hygiene #4566, window-aware empty-slot refusal #5996/#5613, calibrated token pricing), which is what made the merge state dirty.

What I did this run:

  1. Rebased 2026-11-06_pf-headers-merge-fix onto current master (4dea18eabf).
  2. Resolved two content conflicts by taking the union of intent — kept master's new behavior AND applied the PR's merged-view fix at each site:
  3. Force-pushed (conflict-rebase only): remote head now ec29e859e1 → 36a37f75b2. Branch is a direct descendant of master (0 behind), so the conflict is fully cleared.

Local verification on rebased head: npm run typecheck clean · npm run build success · targeted suites 131/131 green (pf-channel incl. the 3 new #5813 regression tests + native-intercept + native-intercept-selfheal). Full-suite verdict = remote CI on the pushed head.

Honest status note: the mirror check-cache still reports the pre-push sha (ec29e859e, fetched 2026-11-06), so I could not read the post-push CI/merge state from the mirror this run; GitHub will re-run CI on the new head 36a37f75b2. Conflict resolution itself is confirmed via git (branch merges cleanly into master). No design decision was needed and no authorization gate was hit (client-side intercept fix + test only — no kernel/, no config surface).


结构化八问

  1. 是什么问题:PR 分支与 master 冲突(merge state dirty),无法合并——master 在本 PR 触碰的三个 pfRequestHeaders 出网点周围做了大量并发改动。
  2. 什么原因导致的:分支落后 master 575 个提交;master 借 hop 卫生([Bug][omp lane] lane port moves under live omp hosts, and the host's cached proxy hop survives re-targeting — every Anthropic turn dies (connection refused / SELF_SIGNED_CERT_IN_CHAIN) #4566)、空槽窗口拒绝(bili-native (dsh): empty intercept slot sends a 6.7 MiB / 2.1M-token body direct — the 8 MiB byte cap does not catch it, provider answers 400 CONTEXT_WINDOW_EXCEEDED #5996/[Bug][dsh] Model requests silently bypass the compression proxy (fail-open gate) — 927K-token prompt sent raw, 1-token reply, session bricked with 400 #5613)、校准 token 计价等独立特性重写了三个出网点的周边行,纯 merge 因此在同一区域产生内容冲突。
  3. 影响范围与严重程度:仅可合并性受阻——不 rebase 就合不了。冲突本身无运行时影响;底层修复([Bug][dsh] watermark fastpath never hits: the client never sends x-bili-pf-count because pfRequestHeaders is handed the pre-merge headers object #5813 水位 count 发射)逻辑未变。
  4. 怎么修复的:rebase 到当前 master(4dea18eabf);两处冲突按「意图并集」解——既保留 master 新增行为(hoplessForLoopback / captureStampedWindow),又落上 PR 的合并视图头;第三处(routed)自动合并干净。
  5. 是否有回归问题:逐点核对——三个出网点各自同时保住 master 行为(hop 卫生、窗口捕获)与 PR 修复(合并头视图);非 dsh 车道 headersFor 为 undefined(展开空对象)= 零变化;retry 路径复用 branchExtra 不受影响。本地 typecheck 干净、build 成功、定向套件 131/131 绿。
  6. 是否破坏缓存:不涉及——只动客户端→代理这一段的请求头组装,不触碰前缀缓存、折叠状态或存储。
  7. 是否改了设计方向:不涉及——仍是「一台状态机 + 单一盖戳点」架构(KDD#15)。
  8. 是否引入了新的配置:不涉及。

收工判定:冲突已解、分支已对齐 master,rebase 后可合并(合并动作按规矩由你执行)。CI 在新 head 36a37f75b2 上重跑后即可看全局绿红。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 (ai: vllm-qwen/qwen3.8-27b) Conflict resolved & rebased — done this run.

What happened: PR branch was 575 commits behind master; master had rewritten the surrounding lines of all three pfRequestHeaders call sites in src/agent/native-intercept.ts (hop hygiene #4566, window-aware refusal #5996/#5613) → dirty merge state.

What I did:

  1. Rebased 2026-11-06_pf-headers-merge-fix onto current master (4dea18eabf).
  2. Resolved two content conflicts by union of intent — kept master's new behavior AND applied the PR's merged-view fix:
  3. Force-pushed (conflict-rebase only): remote head ec29e859e1 → 36a37f75b2; branch now a direct descendant of master (0 behind) → conflict fully cleared.

Verified on rebased head: npm run typecheck clean · npm run build success · targeted suites 131/131 green (pf-channel incl. the 3 new #5813 regression tests + native-intercept + native-intercept-selfheal). Full-suite verdict = remote CI on the pushed head.

Remaining / risk: mirror check-cache still reported the pre-push sha (ec29e859e) this run, so post-push CI state wasn't readable from the mirror; GitHub re-runs CI on 36a37f75b2. Conflict resolution itself is git-confirmed clean. No design decision or authorization gate needed (client-side intercept fix + test only; no kernel/, no config surface).

Verdict: mergeable after rebase — merge action stays yours.

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: vllm-qwen/qwen3.8-27b)

Review verdict: APPROVE

This fixes the root cause named in #5813. The watermark chokepoint (pfRequestHeaders, the single emit point in src/agent/pf-channel.ts) was handed the pre-merge client headers, so when the conversation header comes from headersFor (the dsh lane shape — never present in the client's own request) it could not find the sid and silently dropped x-bili-pf-count every turn (the observed 3,795 evaluations / 0 hits). pfLearnStamp already saw the merged view, so learning worked while sending did not — exactly the asymmetry the issue describes.

Correctness. All three chokepoint branches in src/agent/native-intercept.ts now pass the merged view ({...outgoing, ...(headersFor ?? {})}) to pfRequestHeaders:

  • routed — native-intercept.ts:1892
  • self — native-intercept.ts:2129
  • rewrite — native-intercept.ts:2152

installNativeFetchIntercept is defined once here (native-intercept.ts:1289) and imported by all five lanes (dsh/omp/opencode-native/opencode/pi-native), so this single change fixes every lane after rebuild — matching the issue's "same shape in all five lanes" claim. The four pfLearnStamp call sites are correctly left unchanged (they already receive the stamped/merged headers).

The self branch additionally merges headersFor(url) into the actual send (withHeaders(..., {...selfRouted, ...selfPfExtra})). This is the issue's "second break" (§2.5/§4) and is disclosed in the PR table. It only fires for model-API URLs (the !isModelApiUrl guard returns earlier) and only for lanes that define headersFor (currently dsh only; the other four have it undefined ⇒ zero behavior change), so blast radius is contained and it strictly restores correct session attribution on the direct-to-proxy path.

Tests. Three new regression tests in tests/pf-channel.test.ts cover all three branches (rewrite/routed/self) for the dsh shape where the convo header comes from headersFor, not the client. Negative control traced by hand: pre-fix the client carries no CONVERSATION_HEADER, so pfRequestHeaders({}) returns {} and the turn-2 count assertion ("5"/"3"/"r5,r6") fails; post-fix the merged view finds the sid and emits count+tail-ids. Each test also asserts the unseeded turn-1 negative case. Deterministic (recording fetch, unique sids per the file's isolation discipline). CI ran this exact file green on the pushed head (all test shards + static-gates + gate; 0 failed / 0 pending per the mirror cache).

Note: the body says "two new regression tests … 14 pf-channel tests"; the diff actually adds three (the file now has 15 top-level tests) covering one more branch than described — coverage is broader than the prose, immaterial to correctness.

Scope. The server-side "read PF_COUNT_HEADER before scanMessagesSpans" optimization from issue §2.3 is intentionally not part of this fix — the issue itself lists it as optional/independent hardening, and once the client emits the count header the double-scan cost becomes moot (the scan result is used when a stamp is present) and the credited-miss breaker can arm normally. No action needed here.

No kernel/ change, no config-schema or persistence change, no upstream wire-shape change (the added headers are bili-internal handshake headers stripped before forwarding upstream per KDD #15). Clean 2-file diff (+85/-4), every line on-purpose. Base is current master (not stale). Review-clean.


结构化八问(中文,供人工复核)

  1. 是什么问题:dsh 车道里客户端侧的水位快路径(pluginFullResendWatermark)一次都没命中——因为发请求时 x-bili-pf-count 根本没被发出,服务端每轮都判 miss(no-stamp)。
  2. 什么原因导致的:pfRequestHeaders()(快路径唯一发射点)拿到的是 bili 还没并会话头之前的那个客户端请求对象;而 x-bili-plugin-conversation 只在 headersFor 里现拼、拼完进了另一个对象。于是按会话头查不到 slot、恒返回 {},count 头永远发不出去。学习侧 pfLearnStamp 拿的是合并后的对象,所以"学得会、发不出"——正是这个不对称根因。
  3. 影响范围与严重程度:仅影响显式开启 pluginFullResendWatermark 且走 dsh 进程内插件车道的会话(该车道靠 headersFor 打会话头)。表现为优化完全失效 + 每轮对同一多 MiB 请求体多做一趟全文扫描(性能损耗,非数据丢失);可用但不划算,p2 合理。
  4. 怎么修复的:让三个发射点(routed/self/rewrite)都把 headersFor 的结果并进传给 pfRequestHeaders 的头对象(合并视图),使会话头可见、slot 可查、count/tail-ids 得以发出;self 分支同时把 headersFor 并进实际发送(修掉 issue 说的"第二个断点")。改的是单一共享源 native-intercept.ts,五个车道重建后一并修复。
  5. 是否有回归问题:逐条过——①非 dsh 四车道 headersFor 为 undefined → 合并后等价于原样,零行为变化;②self 分支新增并入 headersFor 仅对模型 URL 生效(!isModelApiUrl 提前返回),控制面不受影响(既有测试已钉住);③pfLearnStamp 四处调用未动,学习语义不变;④spread 顺序 {...client, ...headersFor} 使 headersFor 为权威会话身份,符合设计。高危模式(阈值/时序/输出格式/fail-fast→吞错)均无漂移;CI 全绿。
  6. 是否破坏缓存:不涉及缓存失效策略。水位通道本身是"客户端回带上一轮 T",修复让它真正工作;不改变任何去重键/游标/TTL。
  7. 是否改了设计方向:没有。仍是同一水位握手机制,只是把发射侧喂对了头对象;不新增机制、不动架构边界。
  8. 是否引入了新的配置:无新 env/config/默认值。

判定:可合(APPROVE)——修法打到根因、覆盖全部三发射点/五车道实现、测试真实可兜底、CI 绿、diff 干净、无越界。合并仍由 owner 执行。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b) Done — APPROVE, no blocking findings. Full review posted on the PR (#5815, comment 6765307634); the formal APPROVE review event is blocked because this identity is the PR author (self-approve refused), so the verdict is carried in that comment.

One-line summary: root cause fixed at all three chokepoints (routed/self/rewrite) in src/agent/native-intercept.ts so the dsh lane's headersFor-provided conversation header reaches pfRequestHeaders and x-bili-pf-count is actually emitted; single shared source ⇒ all five lanes fixed after rebuild; 3 real regression tests cover all three branches (traced to fail pre-fix); CI green, base current master, no kernel/config/wire-schema change. Merge left to owner.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — APPROVE, no blocking findings.

What I did: reviewed PR #5815 end-to-end (PR body, linked issue #5813, full diff, surrounding source, test file, CI state via the mirror cache).

Outcome & evidence:

  • Root cause fixed correctly. All three chokepoint branches in src/agent/native-intercept.ts (routed :1892 / self :2129 / rewrite :2152) now pass the merged view {...outgoing, ...(headersFor ?? {})} into the single emit point pfRequestHeaders (src/agent/pf-channel.ts:115). Previously the dsh lane stamped x-bili-plugin-conversation only inside headersFor (a separate object), so the pre-merge client headers had no sid ⇒ x-bili-pf-count was dropped every turn (issue [Bug][dsh] watermark fastpath never hits: the client never sends x-bili-pf-count because pfRequestHeaders is handed the pre-merge headers object #5813: 3,795 evals / 0 hits). The four pfLearnStamp sites are correctly left unchanged (they already saw the merged view) — exactly the asymmetry described.
  • All five lanes covered. installNativeFetchIntercept is defined once (native-intercept.ts:1289) and imported by dsh/omp/opencode-native/opencode/pi-native, so this single change fixes every lane after rebuild. Non-dsh lanes have headersFor undefined ⇒ zero behavior change there. The self branch's extra merge of headersFor into the send is model-URL-only and dsh-only, so blast radius is contained.
  • Tests are real & regression-catching. 3 new tests in tests/pf-channel.test.ts cover rewrite/routed/self branches for the dsh shape; traced by hand to FAIL pre-fix (merged view absent ⇒ {} ⇒ turn-2 count assertion breaks); deterministic (recording fetch + unique sids).
  • CI green on the pushed head (mirror cache: 30 checks, 0 failed / 0 pending; e2e real-client lanes skipped/gated). Base is current master (not stale).
  • No kernel/, config-schema, persistence, or upstream wire-shape change (added headers are bili-internal, stripped before upstream per KDD docs: env vars primary config method + cleanup #15). Clean 2-file diff (+85/-4).

Where the verdict lives: formal APPROVE review event was refused (this identity is the PR author → can't self-approve), so the full review + Chinese 八问 checklist is posted as GitHub comment 6765307634 on PR #5815.

Merge left to owner (human-only).

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

Labels

bug Something isn't working dsh p2 scope/wiring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][dsh] watermark fastpath never hits: the client never sends x-bili-pf-count because pfRequestHeaders is handed the pre-merge headers object

2 participants